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, mr_params req);
40 void do_access(client *cl, mr_params req);
41 void get_motd(client *cl);
43 /* Put this in a variable so that we can patch it if necessary */
44 int max_row_count = 4096;
58 void client_read(client *cl)
63 status = mr_receive(cl->con, &req);
64 if (status != MR_SUCCESS)
67 if (status != MR_NOT_CONNECTED)
68 com_err(whoami, status, "while reading from socket");
73 if (pn < 0 || pn > MR_MAX_PROC)
75 com_err(whoami, 0, "procno out of range");
76 client_reply(cl, MR_UNKNOWN_PROC);
77 mr_destroy_reply(req);
80 log_args(procnames[pn], 2, req.mr_argc, req.mr_argv);
82 if (dormant == ASLEEP && pn != MR_NOOP && pn != MR_MOTD)
84 client_reply(cl, MR_DOWN);
85 com_err(whoami, MR_DOWN, "(query refused)");
86 mr_destroy_reply(req);
90 /* make sure this gets cleared before every operation */
96 client_reply(cl, MR_SUCCESS);
116 client_reply(cl, MR_PERM);
123 mr_destroy_reply(req);
126 /* Set the final return status for a query. We always keep one more
127 free slot in cl->tuples[] than we're using so that this can't fail */
128 void client_reply(client *cl, long status)
130 cl->tuples[cl->ntuples].u.mr_status = status;
131 cl->tuples[cl->ntuples].mr_argc = 0;
132 cl->tuples[cl->ntuples].mr_argl = NULL;
133 cl->tuples[cl->ntuples].mr_argv = NULL;
137 void client_return_tuple(client *cl, int argc, char **argv)
139 if (cl->done || dbms_errno)
142 if (cl->ntuples == max_row_count)
144 dbms_errno = mr_errcode = MR_NO_MEM;
148 if (cl->ntuples == cl->tuplessize - 1)
150 int newsize = (cl->tuplessize + 4) * 2;
151 mr_params *newtuples;
153 newtuples = realloc(cl->tuples, newsize * sizeof(mr_params));
157 dbms_errno = mr_errcode = MR_NO_MEM;
160 cl->tuplessize = newsize;
161 cl->tuples = newtuples;
164 cl->tuples[cl->ntuples].u.mr_status = MR_MORE_DATA;
165 cl->tuples[cl->ntuples].mr_argc = argc;
166 cl->tuples[cl->ntuples].mr_argl = NULL;
167 cl->tuples[cl->ntuples].mr_argv = mr_copy_args(argv, argc);
171 void client_write(client *cl)
175 status = mr_send(cl->con, &cl->tuples[cl->nexttuple]);
178 com_err(whoami, status, "writing to socket");
184 if (cl->nexttuple == cl->ntuples)
189 void free_rtn_tuples(client *cl)
191 for (cl->ntuples--; cl->ntuples >= 0; cl->ntuples--)
192 free_argv(cl->tuples[cl->ntuples].mr_argv,
193 cl->tuples[cl->ntuples].mr_argc);
196 cl->tuples = xmalloc(sizeof(mr_params));
198 cl->ntuples = cl->nexttuple = 0;
201 void do_retr(client *cl, mr_params req)
208 client_reply(cl, MR_ARGS);
209 com_err(whoami, MR_ARGS, "got nameless query");
212 queryname = req.mr_argv[0];
215 if (!strcmp(queryname, "_list_users"))
216 status = list_users(cl);
218 status = mr_process_query(cl, queryname, req.mr_argc - 1, req.mr_argv + 1,
221 client_reply(cl, status);
223 if (cl->ntuples >= max_row_count)
225 critical_alert("moirad", "attempted query %s with too many rows\n",
229 com_err(whoami, 0, "Query complete.");
232 int retr_callback(int argc, char **argv, void *p_cl)
236 mr_trim_args(argc, argv);
237 client_return_tuple(cl, argc, argv);
240 void do_access(client *cl, mr_params req)
246 client_reply(cl, MR_ARGS);
247 com_err(whoami, MR_ARGS, "got nameless access");
251 status = mr_check_access(cl, req.mr_argv[0], req. mr_argc - 1,
253 client_reply(cl, status);
255 com_err(whoami, 0, "Access check complete.");
258 void get_motd(client *cl)
264 if (stat(MOIRA_MOTD_FILE, &statb) == -1)
266 client_reply(cl, MR_SUCCESS);
270 buffer = malloc(statb.st_size + 1);
273 client_reply(cl, MR_NO_MEM);
277 motd = open(MOIRA_MOTD_FILE, 0, O_RDONLY);
280 read(motd, buffer, statb.st_size);
282 buffer[statb.st_size] = '\0';
283 client_return_tuple(cl, 1, &buffer);
284 client_reply(cl, MR_SUCCESS);
287 client_reply(cl, errno);
292 int list_users(client *cl)
300 for (i = 0; i < nclients; i++)
302 client *c = clients[i];
304 argv[1] = inet_ntoa(c->haddr.sin_addr);
306 sprintf(buf, "port %d", ntohs(c->haddr.sin_port));
307 argv[3] = ctime(&c->last_time_used);
308 cp = strchr(argv[3], '\n');
312 sprintf(buf1, "[#%d]", c->id);
313 client_return_tuple(cl, 5, argv);