]> andersk Git - moira.git/blame - server/mr_scall.c
detect query with no args
[moira.git] / server / mr_scall.c
CommitLineData
a3cf6921 1/*
2 * $Source$
3 * $Author$
4 * $Header$
5 *
6 * Copyright (C) 1987 by the Massachusetts Institute of Technology
c801de4c 7 * For copying and distribution information, please see the file
8 * <mit-copyright.h>.
a3cf6921 9 *
a3cf6921 10 */
11
12#ifndef lint
13static char *rcsid_sms_scall_c = "$Header$";
14#endif lint
15
c801de4c 16#include <mit-copyright.h>
57c08162 17#include <sys/types.h>
18#include <sys/stat.h>
9cad7c8f 19#include <sys/file.h>
a3cf6921 20#include <krb.h>
78849033 21#include <errno.h>
8165c536 22#include "query.h"
d548a4e7 23#include "mr_server.h"
a3cf6921 24extern char buf1[];
25extern int nclients;
26extern char *whoami;
28222705 27extern char *malloc();
8165c536 28extern int errno;
a3cf6921 29
30extern void clist_delete(), do_auth(), do_shutdown();
31void do_call();
78543660 32extern int ingres_errno, mr_errcode;
33static int row_count;
34
35/* Put this in a variable so that we can patch it if necessary */
36int max_row_count = 2048;
a3cf6921 37
38/*
39 * Welcome to the (finite state) machine (highest level).
40 */
41void
42do_client(cp)
43 client *cp;
44{
57c08162 45 struct stat stbuf;
46
ed31e0d1 47 free_rtn_tuples(cp);
a3cf6921 48 if (OP_STATUS(cp->pending_op) == OP_CANCELLED) {
a36b5bd7 49 com_err(whoami, 0, "Closed connection (now %d client%s, %d new queries, %d old)",
f17535a4 50 nclients-1,
a36b5bd7 51 nclients!=2?"s":"",
52 newqueries,
53 oldqueries);
a3cf6921 54 clist_delete(cp);
57c08162 55 /* if we no longer have any clients, and we're supposed to
56 * go down, then go down now.
57 */
58 if ((dormant == AWAKE) && (nclients == 0) &&
d548a4e7 59 (stat(MOIRA_MOTD_FILE, &stbuf) == 0)) {
57c08162 60 com_err(whoami, 0, "motd file exists, slumbertime");
61 dormant = SLEEPY;
62 }
a3cf6921 63 return;
64 }
65 switch (cp->action) {
66 case CL_ACCEPT:
67 case CL_SEND:
68 /* Start recieving next request */
d548a4e7 69 initialize_operation(cp->pending_op, mr_start_recv,
5dbd09a0 70 (char *)&cp->args, (int (*)())NULL);
a3cf6921 71 queue_operation(cp->con, CON_INPUT, cp->pending_op);
72 cp->action = CL_RECEIVE;
73 break;
74 case CL_RECEIVE:
75 /* Data is here. Process it & start it heading back */
76 do_call(cp); /* This may block for a while. */
d548a4e7 77 mr_destroy_reply(cp->args);
de24f12d 78 cp->args = NULL;
d548a4e7 79 initialize_operation(cp->pending_op, mr_start_send,
5dbd09a0 80 (char *)&cp->reply, (int (*)())NULL);
a3cf6921 81 queue_operation(cp->con, CON_OUTPUT, cp->pending_op);
82 cp->action = CL_SEND;
83 break;
84 }
85}
86
a3cf6921 87char *procnames[] = {
88 "noop",
89 "auth",
90 "shutdown",
78849033 91 "query",
630e8323 92 "access",
57c08162 93 "dcm",
94 "motd",
630e8323 95};
96
a3cf6921 97
98void
99do_call(cl)
100 client *cl;
101{
102 int pn;
4ce8321c 103 extern int ingres_errno;
d548a4e7 104 cl->reply.mr_argc = 0;
105 cl->reply.mr_status = 0;
106 cl->reply.mr_version_no = cl->args->mr_version_no;
107 if (((pn = cl->args->mr_procno) < 0) ||
108 (pn > MR_MAX_PROC)) {
a3cf6921 109 com_err(whoami, 0, "procno out of range");
d548a4e7 110 cl->reply.mr_status = MR_UNKNOWN_PROC;
a3cf6921 111 return;
112 }
630e8323 113 if (log_flags & LOG_ARGS)
d548a4e7 114 log_args(procnames[pn], cl->args->mr_version_no,
115 cl->args->mr_argc, cl->args->mr_argv);
630e8323 116 else if (log_flags & LOG_REQUESTS)
f17535a4 117 com_err(whoami, 0, "%s", procnames[pn]);
a3cf6921 118
57c08162 119 if ((dormant == ASLEEP || dormant == GROGGY) &&
d548a4e7 120 pn != MR_NOOP && pn != MR_MOTD) {
121 cl->reply.mr_status = MR_DOWN;
57c08162 122 if (log_flags & LOG_RES)
d548a4e7 123 com_err(whoami, MR_DOWN, "(query refused)");
57c08162 124 return;
125 }
126
4ce8321c 127 /* make sure this gets cleared before every operation */
128 ingres_errno = 0;
129
a3cf6921 130 switch(pn) {
d548a4e7 131 case MR_NOOP:
132 cl->reply.mr_status = 0;
a3cf6921 133 return;
3630192b 134
d548a4e7 135 case MR_AUTH:
a3cf6921 136 do_auth(cl);
137 return;
78849033 138
d548a4e7 139 case MR_QUERY:
a3cf6921 140 do_retr(cl);
141 return;
a3cf6921 142
d548a4e7 143 case MR_ACCESS:
3630192b 144 do_access(cl);
145 return;
146
d548a4e7 147 case MR_SHUTDOWN:
a3cf6921 148 do_shutdown(cl);
149 return;
8165c536 150
d548a4e7 151 case MR_DO_UPDATE:
a36b5bd7 152 trigger_dcm(0, 0, cl);
8165c536 153 return;
57c08162 154
d548a4e7 155 case MR_MOTD:
57c08162 156 get_motd(cl);
157 return;
a3cf6921 158 }
159}
160
ed31e0d1 161free_rtn_tuples(cp)
162 client *cp;
163{
164 register returned_tuples *temp;
165 for (temp=cp->first; temp && OP_DONE(temp->op); ) {
166 register returned_tuples *t1=temp;
167 temp = t1->next;
168 if (t1 == cp->last) cp->last = NULL;
28222705 169
d548a4e7 170 mr_destroy_reply(t1->retval);
ed31e0d1 171 delete_operation(t1->op);
172 free(t1);
173 }
174 cp->first = temp;
175}
176
78849033 177retr_callback(argc, argv, p_cp)
28222705 178 register int argc;
179 register char **argv;
78849033 180 char *p_cp;
181{
182 register client *cp = (client *)p_cp;
78543660 183 mr_params *arg_tmp;
184 returned_tuples *tp;
185 OPERATION op_tmp;
186 register char **nargv;
187 register int i;
188
189 if (row_count++ >= max_row_count) {
190 ingres_errno = mr_errcode = MR_NO_MEM;
191 return;
192 }
193
78849033 194 /*
195 * This takes too much advantage of the fact that
196 * serialization of the data happens during the queue operation.
197 */
78543660 198 arg_tmp = (mr_params *)db_alloc(sizeof(mr_params));
199 tp = (returned_tuples *)db_alloc(sizeof(returned_tuples));
200 nargv = (char **)malloc(argc * sizeof(char *));
ed31e0d1 201
78543660 202 op_tmp = create_operation();
78849033 203
d548a4e7 204 if (mr_trim_args(argc, argv) == MR_NO_MEM) {
205 com_err(whoami, MR_NO_MEM, "while trimming args");
a36b5bd7 206 }
630e8323 207 if (log_flags & LOG_RESP)
d548a4e7 208 log_args("return: ", cp->args->mr_version_no, argc, argv);
630e8323 209
ed31e0d1 210 tp->op = op_tmp;
211 tp->retval = arg_tmp;
212 tp->next = NULL;
78849033 213
d548a4e7 214 arg_tmp->mr_status = MR_MORE_DATA;
215 arg_tmp->mr_version_no = cp->args->mr_version_no;
216 arg_tmp->mr_argc = argc;
217 arg_tmp->mr_argv = nargv;
28222705 218 for (i = 0; i < argc; i++) {
219 register int len = strlen(argv[i]) + 1;
220 nargv[i] = malloc(len);
221 bcopy(argv[i], nargv[i], len);
222 }
d548a4e7 223 arg_tmp->mr_flattened = (char *)NULL;
224 arg_tmp->mr_argl = (int *)NULL;
ed31e0d1 225
226 if (cp->last) {
227 cp->last->next = tp;
228 cp->last = tp;
229 } else {
230 cp->last = cp->first = tp;
231 }
232
78849033 233 reset_operation(op_tmp);
d548a4e7 234 initialize_operation(op_tmp, mr_start_send, (char *)arg_tmp,
78849033 235 (int (*)())NULL);
236 queue_operation(cp->con, CON_OUTPUT, op_tmp);
237}
238
de24f12d 239list_users(callbk, callarg)
240 int (*callbk)();
241 char *callarg;
242{
243 char *argv[6];
244 char buf[30];
245 char buf1[30];
246 int i;
247 extern client **clients;
248 extern char *inet_ntoa();
249 char *cp;
250 char *index();
251 char *ctime();
252
253 for (i = 0; i < nclients; i++) {
254 register client *cl = clients[i];
255 if (cl->clname)
256 argv[0] = cl->clname;
257 else argv[0] = "unauthenticated";
258
259 argv[1] = inet_ntoa(cl->haddr.sin_addr);
260 argv[2] = buf;
261 sprintf(buf, "port %d", ntohs(cl->haddr.sin_port));
262 argv[3] = ctime(&cl->last_time_used);
263 cp = index(argv[3], '\n');
264 if (cp) *cp = '\0';
265 argv[4] = buf1;
266 sprintf(buf1, "[#%d]", cl->id);
267 (*callbk)(5, argv, callarg);
268 }
269 return 0;
270}
78849033 271
a3cf6921 272do_retr(cl)
de24f12d 273 register client *cl;
a3cf6921 274{
de24f12d 275 register char *queryname;
276
d548a4e7 277 cl->reply.mr_argc = 0;
278 cl->reply.mr_status = 0;
78543660 279 row_count = 0;
630e8323 280
c473dda1 281 if (cl->args->mr_argc < 1) {
282 cl->reply.mr_status = MR_ARGS;
283 com_err(whoami, MR_ARGS, "got nameless query");
284 return;
285 }
d548a4e7 286 queryname = cl->args->mr_argv[0];
de24f12d 287
d548a4e7 288 if (cl->args->mr_version_no == MR_VERSION_2)
a36b5bd7 289 newqueries++;
290 else
291 oldqueries++;
292
de24f12d 293 if (strcmp(queryname, "_list_users") == 0)
d548a4e7 294 cl->reply.mr_status = list_users(retr_callback, (char *)cl);
de24f12d 295 else {
d548a4e7 296 cl->reply.mr_status =
297 mr_process_query(cl,
c473dda1 298 queryname,
d548a4e7 299 cl->args->mr_argc-1,
300 cl->args->mr_argv+1,
de24f12d 301 retr_callback,
302 (char *)cl);
303 }
78543660 304 if (row_count >= max_row_count) {
305 critical_alert("moirad", "attempted query %s with %d rows\n",
306 queryname, row_count);
307 }
308
630e8323 309 if (log_flags & LOG_RES)
310 com_err(whoami, 0, "Query complete.");
a3cf6921 311}
78849033 312
3630192b 313do_access(cl)
314 client *cl;
315{
c473dda1 316 if (cl->args->mr_argc < 1) {
317 cl->reply.mr_status = MR_ARGS;
318 com_err(whoami, MR_ARGS, "got nameless access");
319 return;
320 }
d548a4e7 321 cl->reply.mr_argc = 0;
630e8323 322
d548a4e7 323 cl->reply.mr_status =
324 mr_check_access(cl,
325 cl->args->mr_argv[0],
326 cl->args->mr_argc-1,
327 cl->args->mr_argv+1);
3630192b 328
329 com_err(whoami, 0, "Access check complete.");
330}
8165c536 331
a36b5bd7 332
333/* trigger_dcm is also used as a followup routine to the
334 * set_server_host_override query, hence the two dummy arguments.
335 */
336
8165c536 337struct query pseudo_query = {
338 "trigger_dcm",
339 "tdcm",
340};
341
a36b5bd7 342trigger_dcm(dummy0, dummy1, cl)
343 int dummy0, dummy1;
8165c536 344 client *cl;
345{
346 register int pid;
185f76ce 347 char prog[128];
348
d548a4e7 349 cl->reply.mr_argc = 0;
8165c536 350
d548a4e7 351 if (cl->reply.mr_status = check_query_access(&pseudo_query, 0, cl) )
352 return(cl->reply.mr_status);
8165c536 353
185f76ce 354 sprintf(prog, "%s/startdcm", BIN_DIR);
8165c536 355 pid = vfork();
356 switch (pid) {
357 case 0:
9ddf3daa 358 for (dummy0 = getdtablesize() - 1; dummy0 > 2; dummy0--)
359 close(dummy0);
185f76ce 360 execl(prog, "startdcm", 0);
8165c536 361 exit(1);
362
363 case -1:
d548a4e7 364 cl->reply.mr_status = errno;
a36b5bd7 365 return(0);
8165c536 366
367 default:
a36b5bd7 368 return(0);
8165c536 369 }
370}
371
57c08162 372
373get_motd(cl)
374client *cl;
375{
9cad7c8f 376 int motd, len;
57c08162 377 char buffer[1024];
378 char *arg[1];
379
380 arg[0] = buffer;
d548a4e7 381 cl->reply.mr_status = 0;
382 motd = open(MOIRA_MOTD_FILE, 0, O_RDONLY);
9cad7c8f 383 if (motd < 0) return;
384 len = read(motd, buffer, sizeof(buffer) - 1);
385 close(motd);
386 buffer[len] = 0;
78543660 387 row_count = 0;
57c08162 388 retr_callback(1, arg, cl);
d548a4e7 389 cl->reply.mr_status = 0;
57c08162 390}
This page took 0.145874 seconds and 5 git commands to generate.