5 * Copyright (C) 1987-1998 by the Massachusetts Institute of Technology
6 * For copying and distribution information, please see the file
11 #include <mit-copyright.h>
12 #include "mr_server.h"
16 #include <sys/types.h>
18 #include <arpa/inet.h>
19 #include <netinet/in.h>
30 extern client **clients;
33 extern int dbms_errno, mr_errcode;
35 void do_call(client *cl);
36 void free_rtn_tuples(client *cp);
37 int retr_callback(int argc, char **argv, void *p_cl);
38 int list_users(client *cl);
39 void do_retr(client *cl);
40 void do_access(client *cl);
41 void get_motd(client *cl);
56 void client_read(client *cl)
60 status = mr_cont_receive(cl->con, &cl->req);
63 else if (status != MR_SUCCESS)
65 cl->state = CL_CLOSING;
66 if (status != MR_NOT_CONNECTED)
67 com_err(whoami, status, "while reading from socket");
71 pn = cl->req.u.mr_procno;
72 if (pn < 0 || pn > MR_MAX_PROC)
74 com_err(whoami, 0, "procno out of range");
75 client_reply(cl, MR_UNKNOWN_PROC);
76 mr_destroy_reply(cl->req);
79 log_args(procnames[pn], 2, cl->req.mr_argc, cl->req.mr_argv);
81 if (dormant == ASLEEP && pn != MR_NOOP && pn != MR_MOTD)
83 client_reply(cl, MR_DOWN);
84 com_err(whoami, MR_DOWN, "(query refused)");
85 mr_destroy_reply(cl->req);
89 /* make sure this gets cleared before every operation */
95 client_reply(cl, MR_SUCCESS);
115 client_reply(cl, MR_PERM);
126 mr_destroy_reply(cl->req);
127 memset(&cl->req, 0, sizeof(mr_params));
130 /* Set the final return status for a query. We always keep one more
131 free slot in cl->tuples[] than we're using so that this can't fail */
132 void client_reply(client *cl, long status)
134 cl->tuples[cl->ntuples].u.mr_status = status;
135 cl->tuples[cl->ntuples].mr_argc = 0;
136 cl->tuples[cl->ntuples].mr_argl = NULL;
137 cl->tuples[cl->ntuples].mr_argv = NULL;
141 void client_return_tuple(client *cl, int argc, char **argv)
143 if (cl->state == CL_CLOSING || dbms_errno)
146 if (cl->ntuples == cl->tuplessize - 1)
148 int newsize = (cl->tuplessize + 4) * 2;
149 mr_params *newtuples;
151 newtuples = realloc(cl->tuples, newsize * sizeof(mr_params));
155 dbms_errno = mr_errcode = MR_NO_MEM;
158 cl->tuplessize = newsize;
159 cl->tuples = newtuples;
162 cl->tuples[cl->ntuples].u.mr_status = MR_MORE_DATA;
163 cl->tuples[cl->ntuples].mr_argc = argc;
164 cl->tuples[cl->ntuples].mr_argl = NULL;
165 cl->tuples[cl->ntuples].mr_argv = mr_copy_args(argv, argc);
169 void client_write(client *cl)
173 status = mr_send(cl->con, &cl->tuples[cl->nexttuple]);
176 com_err(whoami, status, "writing to socket");
177 cl->state = CL_CLOSING;
182 if (cl->nexttuple == cl->ntuples)
187 void free_rtn_tuples(client *cl)
189 for (cl->ntuples--; cl->ntuples >= 0; cl->ntuples--)
190 free_argv(cl->tuples[cl->ntuples].mr_argv,
191 cl->tuples[cl->ntuples].mr_argc);
194 cl->tuples = xmalloc(sizeof(mr_params));
196 cl->ntuples = cl->nexttuple = 0;
199 void do_retr(client *cl)
204 if (cl->req.mr_argc < 1)
206 client_reply(cl, MR_ARGS);
207 com_err(whoami, MR_ARGS, "got nameless query");
210 queryname = cl->req.mr_argv[0];
213 if (!strcmp(queryname, "_list_users"))
214 status = list_users(cl);
216 status = mr_process_query(cl, queryname, cl->req.mr_argc - 1,
217 cl->req.mr_argv + 1, retr_callback, cl);
219 client_reply(cl, status);
221 com_err(whoami, 0, "Query complete.");
224 int retr_callback(int argc, char **argv, void *p_cl)
228 mr_trim_args(argc, argv);
229 client_return_tuple(cl, argc, argv);
232 void do_access(client *cl)
236 if (cl->req.mr_argc < 1)
238 client_reply(cl, MR_ARGS);
239 com_err(whoami, MR_ARGS, "got nameless access");
243 status = mr_check_access(cl, cl->req.mr_argv[0], cl->req.mr_argc - 1,
244 cl->req.mr_argv + 1);
245 client_reply(cl, status);
247 com_err(whoami, 0, "Access check complete.");
250 void get_motd(client *cl)
256 if (stat(MOIRA_MOTD_FILE, &statb) == -1)
258 client_reply(cl, MR_SUCCESS);
262 buffer = malloc(statb.st_size + 1);
265 client_reply(cl, MR_NO_MEM);
269 motd = open(MOIRA_MOTD_FILE, 0, O_RDONLY);
272 read(motd, buffer, statb.st_size);
274 buffer[statb.st_size] = '\0';
275 client_return_tuple(cl, 1, &buffer);
276 client_reply(cl, MR_SUCCESS);
279 client_reply(cl, errno);
284 int list_users(client *cl)
292 for (i = 0; i < nclients; i++)
294 client *c = clients[i];
296 argv[1] = inet_ntoa(c->haddr.sin_addr);
298 sprintf(buf, "port %d", ntohs(c->haddr.sin_port));
299 argv[3] = ctime(&c->last_time_used);
300 cp = strchr(argv[3], '\n');
304 sprintf(buf1, "[#%d]", c->id);
305 client_return_tuple(cl, 5, argv);