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);
55 void client_read(client *cl)
59 status = mr_cont_receive(cl->con, &cl->req);
62 else if (status != MR_SUCCESS)
64 cl->state = CL_CLOSING;
65 if (status != MR_NOT_CONNECTED)
66 com_err(whoami, status, "while reading from socket");
70 pn = cl->req.u.mr_procno;
71 if (pn < 0 || pn > MR_MAX_PROC)
73 com_err(whoami, 0, "procno out of range");
74 client_reply(cl, MR_UNKNOWN_PROC);
75 mr_destroy_reply(cl->req);
78 log_args(procnames[pn], 2, cl->req.mr_argc, cl->req.mr_argv);
80 if (dormant == ASLEEP && pn != MR_NOOP && pn != MR_MOTD)
82 client_reply(cl, MR_DOWN);
83 com_err(whoami, MR_DOWN, "(query refused)");
84 mr_destroy_reply(cl->req);
88 /* make sure this gets cleared before every operation */
94 client_reply(cl, MR_SUCCESS);
114 client_reply(cl, MR_PERM);
121 mr_destroy_reply(cl->req);
122 memset(&cl->req, 0, sizeof(mr_params));
125 /* Set the final return status for a query. We always keep one more
126 free slot in cl->tuples[] than we're using so that this can't fail */
127 void client_reply(client *cl, long status)
129 cl->tuples[cl->ntuples].u.mr_status = status;
130 cl->tuples[cl->ntuples].mr_argc = 0;
131 cl->tuples[cl->ntuples].mr_argl = NULL;
132 cl->tuples[cl->ntuples].mr_argv = NULL;
136 void client_return_tuple(client *cl, int argc, char **argv)
138 if (cl->state == CL_CLOSING || dbms_errno)
141 if (cl->ntuples == cl->tuplessize - 1)
143 int newsize = (cl->tuplessize + 4) * 2;
144 mr_params *newtuples;
146 newtuples = realloc(cl->tuples, newsize * sizeof(mr_params));
150 dbms_errno = mr_errcode = MR_NO_MEM;
153 cl->tuplessize = newsize;
154 cl->tuples = newtuples;
157 cl->tuples[cl->ntuples].u.mr_status = MR_MORE_DATA;
158 cl->tuples[cl->ntuples].mr_argc = argc;
159 cl->tuples[cl->ntuples].mr_argl = NULL;
160 cl->tuples[cl->ntuples].mr_argv = mr_copy_args(argv, argc);
164 void client_write(client *cl)
168 status = mr_send(cl->con, &cl->tuples[cl->nexttuple]);
171 com_err(whoami, status, "writing to socket");
172 cl->state = CL_CLOSING;
177 if (cl->nexttuple == cl->ntuples)
182 void free_rtn_tuples(client *cl)
184 for (cl->ntuples--; cl->ntuples >= 0; cl->ntuples--)
185 free_argv(cl->tuples[cl->ntuples].mr_argv,
186 cl->tuples[cl->ntuples].mr_argc);
189 cl->tuples = xmalloc(sizeof(mr_params));
191 cl->ntuples = cl->nexttuple = 0;
194 void do_retr(client *cl)
199 if (cl->req.mr_argc < 1)
201 client_reply(cl, MR_ARGS);
202 com_err(whoami, MR_ARGS, "got nameless query");
205 queryname = cl->req.mr_argv[0];
208 if (!strcmp(queryname, "_list_users"))
209 status = list_users(cl);
211 status = mr_process_query(cl, queryname, cl->req.mr_argc - 1,
212 cl->req.mr_argv + 1, retr_callback, cl);
214 client_reply(cl, status);
216 com_err(whoami, 0, "Query complete.");
219 int retr_callback(int argc, char **argv, void *p_cl)
223 mr_trim_args(argc, argv);
224 client_return_tuple(cl, argc, argv);
227 void do_access(client *cl)
231 if (cl->req.mr_argc < 1)
233 client_reply(cl, MR_ARGS);
234 com_err(whoami, MR_ARGS, "got nameless access");
238 status = mr_check_access(cl, cl->req.mr_argv[0], cl->req.mr_argc - 1,
239 cl->req.mr_argv + 1);
240 client_reply(cl, status);
242 com_err(whoami, 0, "Access check complete.");
245 void get_motd(client *cl)
251 if (stat(MOIRA_MOTD_FILE, &statb) == -1)
253 client_reply(cl, MR_SUCCESS);
257 buffer = malloc(statb.st_size + 1);
260 client_reply(cl, MR_NO_MEM);
264 motd = open(MOIRA_MOTD_FILE, 0, O_RDONLY);
267 read(motd, buffer, statb.st_size);
269 buffer[statb.st_size] = '\0';
270 client_return_tuple(cl, 1, &buffer);
271 client_reply(cl, MR_SUCCESS);
274 client_reply(cl, errno);
279 int list_users(client *cl)
287 for (i = 0; i < nclients; i++)
289 client *c = clients[i];
291 argv[1] = inet_ntoa(c->haddr.sin_addr);
293 sprintf(buf, "port %d", ntohs(c->haddr.sin_port));
294 argv[3] = ctime(&c->last_time_used);
295 cp = strchr(argv[3], '\n');
299 sprintf(buf1, "[#%d]", c->id);
300 client_return_tuple(cl, 5, argv);