]>
Commit | Line | Data |
---|---|---|
d26cae4e | 1 | /* |
2 | * $Source$ | |
3 | * $Author$ | |
4 | * $Header$ | |
5 | * | |
83526631 | 6 | * Copyright (C) 1987, 1988 by the Massachusetts Institute of Technology |
7ee33a8d | 7 | * For copying and distribution information, please see the file |
8 | * <mit-copyright.h>. | |
faad6f39 | 9 | * |
d26cae4e | 10 | */ |
11 | ||
12 | #ifndef lint | |
13 | static char *rcsid_qrtn_qc = "$Header$"; | |
14 | #endif lint | |
15 | ||
7ee33a8d | 16 | #include <mit-copyright.h> |
97479f6f | 17 | #include "query.h" |
97479f6f | 18 | #include "sms_server.h" |
19 | ||
20 | char *Argv[16]; | |
21 | ||
ca7c2f3d | 22 | int ingres_errno = 0; |
23 | int sms_errcode = 0; | |
24 | ## int query_timeout = 30; | |
7536027d | 25 | extern char *whoami; |
83526631 | 26 | extern FILE *journal; |
97479f6f | 27 | |
8fcb440e | 28 | #define INGRES_BAD_INT 4111 |
29 | #define INGRES_BAD_DATE 4302 | |
4a7b7001 | 30 | #define INGRES_DEADLOCK 4700 |
ca7c2f3d | 31 | #define INGRES_TIMEOUT 4702 |
8fcb440e | 32 | |
d26cae4e | 33 | /* |
34 | * ingerr: (supposedly) called when Ingres indicates an error. | |
35 | * I have not yet been able to get this to work to intercept a | |
36 | * database open error. | |
37 | */ | |
38 | ||
39 | static int ingerr(num) | |
40 | int *num; | |
97479f6f | 41 | { |
ca7c2f3d | 42 | ingres_errno = *num; |
8fcb440e | 43 | |
44 | switch (*num) { | |
45 | case INGRES_BAD_INT: | |
ca7c2f3d | 46 | sms_errcode = SMS_INTEGER; |
8fcb440e | 47 | break; |
48 | case INGRES_BAD_DATE: | |
ca7c2f3d | 49 | sms_errcode = SMS_DATE; |
8fcb440e | 50 | break; |
4a7b7001 | 51 | case INGRES_DEADLOCK: |
ca7c2f3d | 52 | sms_errcode = SMS_DEADLOCK; |
7ee33a8d | 53 | com_err(whoami, 0, "INGRES deadlock detected"); |
ca7c2f3d | 54 | break; |
55 | case INGRES_TIMEOUT: | |
56 | sms_errcode = SMS_BUSY; | |
57 | com_err(whoami, 0, "timed out getting lock"); | |
4a7b7001 | 58 | break; |
8fcb440e | 59 | default: |
ca7c2f3d | 60 | sms_errcode = SMS_INGRES_ERR; |
8fcb440e | 61 | com_err(whoami, SMS_INGRES_ERR, " code %d\n", *num); |
0a7ae10e | 62 | critical_alert("SMS", "SMS server encountered INGRES ERROR %d", *num); |
8fcb440e | 63 | return (*num); |
64 | } | |
65 | return (0); | |
97479f6f | 66 | } |
67 | ||
d26cae4e | 68 | int sms_open_database() |
97479f6f | 69 | { |
d26cae4e | 70 | register int i; |
83526631 | 71 | char *malloc(); |
73c902bf | 72 | static first_open = 1; |
97479f6f | 73 | |
73c902bf | 74 | if (first_open) { |
75 | first_open = 0; | |
97479f6f | 76 | |
73c902bf | 77 | /* initialize local argv */ |
78 | for (i = 0; i < 16; i++) | |
79 | Argv[i] = malloc(ARGLEN); | |
80 | ||
81 | IIseterr(ingerr); | |
82 | incremental_init(); | |
83 | flush_cache(); | |
84 | } | |
97479f6f | 85 | |
d26cae4e | 86 | ingres_errno = 0; |
d10e38ec | 87 | sms_errcode = 0; |
73c902bf | 88 | |
d26cae4e | 89 | /* open the database */ |
90 | ## ingres sms | |
ca7c2f3d | 91 | ## set lockmode session where level = table, timeout = query_timeout |
45266a6f | 92 | ## set lockmode on capacls where readlock = shared |
93 | ## set lockmode on alias where readlock = shared | |
d26cae4e | 94 | return ingres_errno; |
97479f6f | 95 | } |
96 | ||
d26cae4e | 97 | int sms_close_database() |
97479f6f | 98 | { |
73c902bf | 99 | flush_cache(); |
d26cae4e | 100 | ## exit |
97479f6f | 101 | } |
102 | ||
7536027d | 103 | sms_check_access(cl, name, argc, argv_ro) |
104 | client *cl; | |
105 | char *name; | |
106 | int argc; | |
107 | char *argv_ro[]; | |
108 | { | |
9969e1bc | 109 | struct query *q; |
7536027d | 110 | struct query *get_query_by_name(); |
7536027d | 111 | |
ca7c2f3d | 112 | ingres_errno = 0; |
d10e38ec | 113 | sms_errcode = 0; |
ca7c2f3d | 114 | |
83526631 | 115 | q = get_query_by_name(name, cl->args->sms_version_no); |
9969e1bc | 116 | if (q == (struct query *)0) |
117 | return(SMS_NO_HANDLE); | |
7536027d | 118 | |
9969e1bc | 119 | return(sms_verify_query(cl, q, argc, argv_ro)); |
7536027d | 120 | } |
121 | ||
122 | sms_process_query(cl, name, argc, argv_ro, action, actarg) | |
123 | client *cl; | |
d26cae4e | 124 | char *name; |
125 | int argc; | |
126 | char *argv_ro[]; | |
127 | int (*action)(); | |
128 | char *actarg; | |
97479f6f | 129 | { |
d26cae4e | 130 | register struct query *q; |
7536027d | 131 | register int status; |
7536027d | 132 | register struct validate *v; |
7536027d | 133 | char qual[256]; |
cc1231a1 | 134 | char sort[32]; |
7536027d | 135 | char *pqual; |
cc1231a1 | 136 | char *psort; |
6a84801f | 137 | ## char *table, *rvar; |
7536027d | 138 | struct save_queue *sq; |
d26cae4e | 139 | struct query *get_query_by_name(); |
7536027d | 140 | int sq_save_args(); |
141 | struct save_queue *sq_create(); | |
cc1231a1 | 142 | char *build_sort(); |
d26cae4e | 143 | |
ca7c2f3d | 144 | ingres_errno = 0; |
d10e38ec | 145 | sms_errcode = 0; |
ca7c2f3d | 146 | |
7536027d | 147 | /* list queries command */ |
148 | if (!strcmp(name, "_list_queries")) { | |
83526631 | 149 | list_queries(cl->args->sms_version_no, action, actarg); |
7536027d | 150 | return(SMS_SUCCESS); |
151 | } | |
152 | ||
153 | /* help query command */ | |
154 | if (!strcmp(name, "_help")) { | |
83526631 | 155 | if (argc < 1) |
156 | return(SMS_ARGS); | |
157 | q = get_query_by_name(argv_ro[0], cl->args->sms_version_no); | |
7536027d | 158 | if (q == (struct query *)0) return(SMS_NO_HANDLE); |
159 | help_query(q, action, actarg); | |
160 | return(SMS_SUCCESS); | |
161 | } | |
162 | ||
163 | /* get query structure, return error if named query does not exist */ | |
83526631 | 164 | q = get_query_by_name(name, cl->args->sms_version_no); |
7536027d | 165 | if (q == (struct query *)0) return(SMS_NO_HANDLE); |
166 | v = q->validate; | |
167 | ||
7ee33a8d | 168 | if (q->type != RETRIEVE) { |
d353058b | 169 | ## begin transaction |
7ee33a8d | 170 | } |
d353058b | 171 | |
9969e1bc | 172 | /* setup argument vector, verify access and arguments */ |
173 | if ((status = sms_verify_query(cl, q, argc, argv_ro)) != SMS_SUCCESS) | |
d353058b | 174 | goto out; |
7536027d | 175 | |
176 | /* perform any special query pre-processing */ | |
177 | if (v && v->pre_rtn) { | |
178 | status = (*v->pre_rtn)(q, Argv, cl, 0); | |
9969e1bc | 179 | if (status != SMS_SUCCESS) |
d353058b | 180 | goto out; |
7536027d | 181 | } |
182 | ||
d26cae4e | 183 | switch (q->type) { |
faad6f39 | 184 | case RETRIEVE: |
7536027d | 185 | /* for queries that do not permit wildcarding, check if row |
186 | uniquely exists */ | |
187 | if (v && v->field) { | |
188 | status = validate_row(q, Argv, v); | |
189 | if (status != SMS_EXISTS) break; | |
190 | } | |
191 | ||
192 | /* build "where" clause if needed */ | |
d26cae4e | 193 | if (q->qual) { |
194 | build_qual(q->qual, q->argc, Argv, qual); | |
7536027d | 195 | pqual = qual; |
196 | } else { | |
197 | pqual = 0; | |
198 | } | |
199 | ||
cc1231a1 | 200 | /* build "sort" clause if needed */ |
201 | if (v && v->valobj) { | |
202 | psort = build_sort(v, sort); | |
203 | } else { | |
204 | psort = 0; | |
205 | } | |
206 | ||
7536027d | 207 | /* if there is a followup routine, then we must save the results */ |
208 | /* of the first query for use by the followup routine */ | |
cc1231a1 | 209 | /* if q->rvar = NULL, perform post_rtn only */ |
210 | if (q->rvar) { | |
7536027d | 211 | if (v && v->post_rtn) { |
212 | sq = sq_create(); | |
cc1231a1 | 213 | status = do_retrieve(q, pqual, psort, sq_save_args, sq); |
7536027d | 214 | if (status != SMS_SUCCESS) { |
215 | sq_destroy(sq); | |
216 | break; | |
217 | } | |
83526631 | 218 | status = (*v->post_rtn)(q, sq, v, action, actarg, cl); |
7536027d | 219 | } else { |
220 | /* normal retrieve */ | |
cc1231a1 | 221 | status = do_retrieve(q, pqual, psort, action, actarg); |
7536027d | 222 | } |
223 | if (status != SMS_SUCCESS) break; | |
7536027d | 224 | } else { |
83526631 | 225 | status = (*v->post_rtn)(q, Argv, cl, action, actarg); |
7536027d | 226 | } |
227 | ||
d26cae4e | 228 | break; |
229 | ||
faad6f39 | 230 | case UPDATE: |
7536027d | 231 | /* see if row already exists */ |
232 | if (v->field) { | |
233 | status = validate_row(q, Argv, v); | |
234 | if (status != SMS_EXISTS) break; | |
235 | } | |
236 | ||
237 | /* build "where" clause and perform update */ | |
cc1231a1 | 238 | /* if q->rvar = NULL, perform post_rtn only */ |
239 | if (q->rvar) { | |
7536027d | 240 | build_qual(q->qual, q->argc, Argv, qual); |
2755928d | 241 | incremental_before(q->rtable, qual, argv_ro); |
7536027d | 242 | status = do_update(q, &Argv[q->argc], qual, action, actarg); |
2755928d | 243 | incremental_after(q->rtable, qual, argv_ro); |
7536027d | 244 | if (status != SMS_SUCCESS) break; |
73c902bf | 245 | flush_name(argv_ro[0], q->rtable); |
cc1231a1 | 246 | table = q->rtable; |
37e49d47 | 247 | if (strcmp(q->shortname, "sshi") && strcmp(q->shortname, "ssif")) { |
248 | ## repeat replace tblstats (updates = tblstats.updates + 1, | |
249 | ## modtime = "now") | |
250 | ## where tblstats.#table = @table | |
251 | } | |
d26cae4e | 252 | } |
7536027d | 253 | |
254 | /* execute followup routine (if any) */ | |
83526631 | 255 | if (v->post_rtn) status = (*v->post_rtn)(q, Argv, cl); |
7536027d | 256 | |
d26cae4e | 257 | break; |
97479f6f | 258 | |
faad6f39 | 259 | case APPEND: |
7536027d | 260 | /* see if row already exists */ |
261 | if (v->field) { | |
262 | status = validate_row(q, Argv, v); | |
263 | if (status != SMS_NO_MATCH) break; | |
d26cae4e | 264 | } |
7536027d | 265 | |
266 | /* increment id number if necessary */ | |
d353058b | 267 | if (v->object_id) { |
83526631 | 268 | status = set_next_object_id(v->object_id, q->rtable); |
d353058b | 269 | if (status != SMS_SUCCESS) break; |
270 | } | |
7536027d | 271 | |
cc1231a1 | 272 | /* build "where" clause if needed */ |
273 | if (q->qual) { | |
274 | build_qual(q->qual, q->argc, Argv, qual); | |
275 | pqual = qual; | |
276 | } else { | |
277 | pqual = 0; | |
278 | } | |
279 | ||
7536027d | 280 | /* perform the append */ |
cc1231a1 | 281 | /* if q->rvar = NULL, perform post_rtn only */ |
282 | if (q->rvar) { | |
2755928d | 283 | incremental_clear_before(); |
cc1231a1 | 284 | status = do_append(q, &Argv[q->argc], pqual, action, actarg); |
7536027d | 285 | if (status != SMS_SUCCESS) break; |
2755928d | 286 | if (v && v->object_id) { |
287 | sprintf(qual, "%s.%s = values.value and values.name = \"%s\"", | |
288 | q->rvar, v->object_id, v->object_id); | |
289 | incremental_after(q->rtable, qual, argv_ro); | |
290 | } else | |
291 | incremental_after(q->rtable, pqual, argv_ro); | |
292 | ||
cc1231a1 | 293 | table = q->rtable; |
7536027d | 294 | ## repeat replace tblstats (appends = tblstats.appends + 1, |
295 | ## modtime = "now") | |
296 | ## where tblstats.#table = @table | |
297 | } | |
298 | ||
299 | /* execute followup routine */ | |
83526631 | 300 | if (v->post_rtn) status = (*v->post_rtn)(q, Argv, cl); |
d26cae4e | 301 | break; |
97479f6f | 302 | |
faad6f39 | 303 | case DELETE: |
7536027d | 304 | /* see if row already exists */ |
305 | if (v->field) { | |
306 | status = validate_row(q, Argv, v); | |
307 | if (status != SMS_EXISTS) break; | |
308 | } | |
309 | ||
310 | /* build "where" clause and perform delete */ | |
cc1231a1 | 311 | /* if q->rvar = NULL, perform post_rtn only */ |
312 | if (q->rvar) { | |
7536027d | 313 | build_qual(q->qual, q->argc, Argv, qual); |
6a84801f | 314 | table = q->rtable; |
315 | rvar = q->rvar; | |
316 | ## range of rvar is table | |
2755928d | 317 | incremental_before(q->rtable, qual, argv_ro); |
7536027d | 318 | status = do_delete(q, qual, action, actarg); |
2755928d | 319 | incremental_clear_after(); |
7536027d | 320 | if (status != SMS_SUCCESS) break; |
73c902bf | 321 | flush_name(argv_ro[0], q->rtable); |
7536027d | 322 | ## repeat replace tblstats (deletes = tblstats.deletes + 1, |
323 | ## modtime = "now") | |
324 | ## where tblstats.#table = @table | |
97479f6f | 325 | } |
7536027d | 326 | |
327 | /* execute followup routine */ | |
83526631 | 328 | if (v->post_rtn) status = (*v->post_rtn)(q, Argv, cl); |
d26cae4e | 329 | break; |
7536027d | 330 | |
d26cae4e | 331 | } |
7536027d | 332 | |
d353058b | 333 | out: |
e83ef509 | 334 | if (status == SMS_SUCCESS && ingres_errno != 0) { |
335 | com_err(whoami, SMS_INTERNAL, "Server didn't notice INGRES ERROR %d", | |
336 | ingres_errno); | |
337 | status = sms_errcode; | |
338 | } | |
339 | ||
3df9e21e | 340 | if (q->type != RETRIEVE) { |
341 | if (status == SMS_SUCCESS) { | |
d353058b | 342 | ## end transaction /* commit to this */ |
83526631 | 343 | if (journal) { |
344 | char buf[1024], *bp; | |
345 | int i; | |
346 | extern time_t now; | |
347 | ||
348 | fprintf(journal, "%% %s %s %s", | |
349 | cl->clname, cl->entity, ctime(&now)); | |
350 | fprintf(journal, "%s[%d] ", q->name, cl->args->sms_version_no); | |
351 | for (i = 0; i < argc; i++) { | |
352 | if (i != 0) { | |
353 | putc(' ', journal); | |
354 | } | |
355 | requote(buf, argv_ro[i], sizeof(buf)); | |
356 | fputs(buf, journal); | |
357 | } | |
358 | putc('\n', journal); | |
359 | fflush(journal); | |
360 | } | |
2755928d | 361 | incremental_update(); |
3df9e21e | 362 | } else { |
ca7c2f3d | 363 | if (ingres_errno != INGRES_DEADLOCK) { |
7ee33a8d | 364 | ## abort /* it never happened */ |
365 | } | |
2755928d | 366 | incremental_flush(); |
3df9e21e | 367 | } |
ca7c2f3d | 368 | ## set lockmode session where readlock = system |
369 | } | |
370 | ||
1bf7f927 | 371 | if (status != SMS_SUCCESS && log_flags & LOG_RES) |
372 | com_err(whoami, status, " (Query failed)"); | |
7536027d | 373 | return(status); |
97479f6f | 374 | } |
375 | ||
376 | build_qual(fmt, argc, argv, qual) | |
377 | char *fmt; | |
378 | int argc; | |
379 | char *argv[]; | |
380 | char *qual; | |
381 | { | |
7536027d | 382 | register char *c; |
383 | register int i; | |
384 | char *args[4]; | |
83526631 | 385 | char *index(); |
7536027d | 386 | |
387 | c = fmt; | |
388 | for (i = 0; i < argc; i++) { | |
83526631 | 389 | c = index(c, '%'); |
7536027d | 390 | if (c++ == (char *)0) return(SMS_ARGS); |
391 | if (*c == 's') | |
392 | args[i] = argv[i]; | |
393 | else if (*c == 'd') | |
394 | *(int *)&args[i] = *(int *)argv[i]; /* sigh */ | |
395 | else | |
396 | return(SMS_INGRES_ERR); | |
397 | } | |
398 | ||
d26cae4e | 399 | switch (argc) { |
400 | case 0: | |
401 | strcpy(qual, fmt); | |
402 | break; | |
403 | ||
404 | case 1: | |
7536027d | 405 | sprintf(qual, fmt, args[0]); |
d26cae4e | 406 | break; |
407 | ||
408 | case 2: | |
7536027d | 409 | sprintf(qual, fmt, args[0], args[1]); |
d26cae4e | 410 | break; |
411 | ||
412 | case 3: | |
7536027d | 413 | sprintf(qual, fmt, args[0], args[1], args[2]); |
d26cae4e | 414 | break; |
415 | ||
416 | case 4: | |
7536027d | 417 | sprintf(qual, fmt, args[0], args[1], args[2], args[3]); |
d26cae4e | 418 | break; |
419 | } | |
83526631 | 420 | return(SMS_SUCCESS); |
97479f6f | 421 | } |
422 | ||
cc1231a1 | 423 | char * |
424 | build_sort(v, sort) | |
425 | register struct validate *v; | |
426 | char *sort; | |
427 | { | |
428 | register struct valobj *vo; | |
429 | register int n; | |
430 | char elem[16]; | |
431 | ||
432 | n = v->objcnt; | |
433 | vo = v->valobj; | |
434 | *sort = 0; | |
435 | ||
436 | while (--n >= 0) { | |
437 | if (vo->type == V_SORT) { | |
438 | sprintf(elem, "RET_VAR%d", vo->index + 1); | |
439 | if (*sort) strcat(sort, ", "); | |
440 | strcat(sort, elem); | |
441 | } | |
442 | vo++; | |
443 | } | |
444 | ||
445 | return ((*sort) ? sort : 0); | |
446 | } | |
447 | ||
9969e1bc | 448 | |
449 | /* Build arguement vector, verify query and arguments */ | |
450 | ||
451 | sms_verify_query(cl, q, argc, argv_ro) | |
452 | client *cl; | |
453 | struct query *q; | |
454 | int argc; | |
455 | char *argv_ro[]; | |
456 | { | |
457 | register int argreq; | |
458 | register int status; | |
459 | register struct validate *v = q->validate; | |
460 | register int i; | |
461 | register int privileged = 0; | |
02d466cf | 462 | int len; |
9969e1bc | 463 | |
464 | /* copy the arguments into a local argv that we can modify */ | |
7ee33a8d | 465 | if (argc >= QMAXARGS) |
466 | return(SMS_ARGS); | |
9969e1bc | 467 | for (i = 0; i < argc; i++) { |
02d466cf | 468 | if ((len = strlen(argv_ro[i])) < ARGLEN) |
9969e1bc | 469 | strcpy(Argv[i], argv_ro[i]); |
470 | else | |
471 | return(SMS_ARG_TOO_LONG); | |
02d466cf | 472 | if (Argv[i][len-1] == '\\') |
473 | return(SMS_BAD_CHAR); | |
9969e1bc | 474 | } |
475 | ||
476 | /* check initial query access */ | |
477 | status = check_query_access(q, Argv, cl); | |
478 | if (status != SMS_SUCCESS && status != SMS_PERM) | |
479 | return(status); | |
480 | if (status == SMS_SUCCESS) | |
481 | privileged++; | |
482 | ||
483 | /* check argument count */ | |
484 | argreq = q->argc; | |
485 | if (q->type == UPDATE || q->type == APPEND) argreq += q->vcnt; | |
486 | if (argc != argreq) return(SMS_ARGS); | |
487 | ||
488 | /* validate arguments */ | |
489 | if (v && v->valobj) { | |
490 | status = validate_fields(q, Argv, v->valobj, v->objcnt); | |
491 | if (status != SMS_SUCCESS) return(status); | |
492 | } | |
493 | ||
494 | /* perform special query access check */ | |
495 | if (!privileged && v && v->acs_rtn) { | |
496 | status = (*v->acs_rtn)(q, Argv, cl); | |
497 | if (status != SMS_SUCCESS && status != SMS_PERM) | |
498 | return(status); | |
499 | if (status == SMS_SUCCESS) | |
500 | privileged++; | |
501 | } | |
502 | ||
503 | return(privileged ? SMS_SUCCESS : SMS_PERM); | |
504 | } | |
505 | ||
2755928d | 506 | |
507 | /* This routine caches info from the database. Each query acl is stored | |
508 | * in the query structure, and whether that acl contains everybody. | |
509 | */ | |
510 | ||
7536027d | 511 | check_query_access(q, argv, cl) |
512 | struct query *q; | |
513 | char *argv[]; | |
514 | client *cl; | |
d26cae4e | 515 | ##{ |
7536027d | 516 | ## char *name; |
517 | ## int acl_id; | |
518 | ## int exists; | |
d26cae4e | 519 | ## int rowcount; |
cc1231a1 | 520 | ## int errorno; |
7536027d | 521 | ## static int def_uid; |
522 | int status; | |
523 | int client_id; | |
524 | char *client_type; | |
525 | ||
7536027d | 526 | /* initialize default uid */ |
527 | if (def_uid == 0) { | |
528 | ## retrieve (def_uid = users.users_id) where users.login = "default" | |
d26cae4e | 529 | } |
97479f6f | 530 | |
2755928d | 531 | /* get query access control list */ |
532 | if (q->acl != 0) | |
533 | acl_id = q->acl; | |
534 | else { | |
535 | name = q->shortname; | |
73c902bf | 536 | ## retrieve (acl_id = capacls.list_id) where capacls.tag = name |
2755928d | 537 | ## inquire_equel (rowcount = "rowcount", errorno = "errorno") |
538 | if (errorno != 0) return(SMS_INGRES_ERR); | |
539 | if (rowcount == 0) return(SMS_PERM); | |
540 | q->acl = acl_id; | |
541 | ||
542 | /* check for default access */ | |
73c902bf | 543 | ## retrieve (exists = any(imembers.#member_id where |
544 | ## imembers.list_id = acl_id and | |
545 | ## imembers.member_type = "USER" and | |
546 | ## imembers.#member_id = def_uid)) | |
2755928d | 547 | q->everybody = exists; |
548 | } | |
549 | ||
550 | if (q->everybody) | |
551 | return(SMS_SUCCESS); | |
552 | ||
db8a5dda | 553 | if (get_client(cl, &client_type, &client_id) != SMS_SUCCESS) |
554 | return(SMS_PERM); | |
555 | if (find_member("LIST", acl_id, client_type, client_id, 0)) | |
2755928d | 556 | return(SMS_SUCCESS); |
557 | else | |
558 | return(SMS_PERM); | |
559 | ##} | |
560 | ||
561 | ||
7536027d | 562 | get_client(cl, client_type, client_id) |
563 | client *cl; | |
564 | char **client_type; | |
565 | int *client_id; | |
2755928d | 566 | { |
567 | if (cl->users_id > 0) { | |
83526631 | 568 | *client_id = cl->users_id; |
7536027d | 569 | *client_type = "USER"; |
83526631 | 570 | return(SMS_SUCCESS); |
7536027d | 571 | } |
83526631 | 572 | |
2755928d | 573 | if (cl->client_id < 0) { |
574 | *client_id = -cl->users_id; | |
575 | *client_type = "KERBEROS"; | |
576 | return(SMS_SUCCESS); | |
577 | } | |
7536027d | 578 | |
2755928d | 579 | return(SMS_PERM); |
580 | } | |
97479f6f | 581 | |
dff355ae | 582 | ##find_member(list_type, list_id, member_type, member_id) |
83526631 | 583 | char *list_type; |
7536027d | 584 | ## int list_id; |
585 | ## char *member_type; | |
586 | ## int member_id; | |
7536027d | 587 | ##{ |
dff355ae | 588 | ## int exists, errorno; |
7536027d | 589 | |
e45a151a | 590 | if (!strcmp(strtrim(list_type), strtrim(member_type)) && |
591 | list_id == member_id) | |
592 | return(1); | |
593 | ||
7536027d | 594 | /* see if client is a direct member of list */ |
2755928d | 595 | ## repeat retrieve (exists = any(imembers.#member_id where |
596 | ## imembers.#list_id = @list_id and | |
597 | ## imembers.#member_type = @member_type and | |
598 | ## imembers.#member_id = @member_id)) | |
dff355ae | 599 | ## inquire_equel(errorno = "errorno") |
600 | if (errorno == 0) | |
601 | return(exists); | |
602 | else | |
603 | return(0); | |
d26cae4e | 604 | ##} |
97479f6f | 605 | |
83526631 | 606 | |
cc1231a1 | 607 | do_retrieve(q, pqual, psort, action, actarg) |
d26cae4e | 608 | register struct query *q; |
cc1231a1 | 609 | char *pqual; |
610 | char *psort; | |
d26cae4e | 611 | int (*action)(); |
612 | char *actarg; | |
613 | ##{ | |
614 | ## char *rvar; | |
615 | ## char *rtable; | |
616 | ## char *cqual; | |
cc1231a1 | 617 | ## char *csort; |
d26cae4e | 618 | ## int rowcount; |
cc1231a1 | 619 | ## int errorno; |
83526631 | 620 | static char **vaddrs = (char **)NULL; |
621 | ||
622 | if (!vaddrs) { | |
623 | register int i; | |
624 | ||
625 | if ((vaddrs = (char **)malloc(sizeof(char *) * QMAXARGS)) == NULL) { | |
626 | com_err(whoami, SMS_NO_MEM, "setting up static argv"); | |
627 | exit(1); | |
628 | } | |
629 | for (i = 0; i < QMAXARGS; i++) { | |
630 | if ((vaddrs[i] = malloc(QMAXARGSIZE)) == NULL) { | |
631 | com_err(whoami, SMS_NO_MEM, "setting up static argv"); | |
632 | exit(1); | |
633 | } | |
634 | } | |
635 | } | |
7536027d | 636 | |
d26cae4e | 637 | if (q->rvar) { |
638 | rvar = q->rvar; | |
639 | rtable = q->rtable; | |
640 | ## range of rvar is rtable | |
641 | } | |
97479f6f | 642 | |
cc1231a1 | 643 | if (psort) { |
644 | csort = psort; | |
645 | if (pqual) { | |
646 | cqual = pqual; | |
83526631 | 647 | ## retrieve unique (param (q->tlist, vaddrs)) where cqual |
cc1231a1 | 648 | ## sort by csort |
649 | ## { | |
83526631 | 650 | (*action)(q->vcnt, vaddrs, actarg); |
cc1231a1 | 651 | ## } |
652 | } else { | |
83526631 | 653 | ## retrieve unique (param (q->tlist, vaddrs)) |
cc1231a1 | 654 | ## sort by csort |
655 | ## { | |
83526631 | 656 | (*action)(q->vcnt, vaddrs, actarg); |
cc1231a1 | 657 | ## } |
658 | } | |
659 | ||
7536027d | 660 | } else { |
cc1231a1 | 661 | if (pqual) { |
662 | cqual = pqual; | |
83526631 | 663 | ## retrieve unique (param (q->tlist, vaddrs)) where cqual |
cc1231a1 | 664 | ## { |
83526631 | 665 | (*action)(q->vcnt, vaddrs, actarg); |
cc1231a1 | 666 | ## } |
667 | } else { | |
83526631 | 668 | ## retrieve unique (param (q->tlist, vaddrs)) |
cc1231a1 | 669 | ## { |
83526631 | 670 | (*action)(q->vcnt, vaddrs, actarg); |
cc1231a1 | 671 | ## } |
672 | } | |
7536027d | 673 | } |
97479f6f | 674 | |
51dfb5b8 | 675 | if (sms_errcode) return(sms_errcode); |
676 | ## inquire_equel (rowcount = "rowcount") | |
7536027d | 677 | return ((rowcount == 0) ? SMS_NO_MATCH : SMS_SUCCESS); |
d26cae4e | 678 | ##} |
97479f6f | 679 | |
e38c941b | 680 | do_update(q, argv, qual, action, actarg) |
d26cae4e | 681 | register struct query *q; |
682 | char *argv[]; | |
683 | char *qual; | |
684 | int (*action)(); | |
685 | char *actarg; | |
686 | ##{ | |
687 | ## char *rvar; | |
688 | ## char *rtable; | |
689 | ## char *cqual; | |
cc1231a1 | 690 | ## int errorno; |
97479f6f | 691 | |
d26cae4e | 692 | rvar = q->rvar; |
693 | rtable = q->rtable; | |
694 | ## range of rvar is rtable | |
97479f6f | 695 | |
d26cae4e | 696 | cqual = qual; |
697 | ## replace rvar (param (q->tlist, argv)) | |
698 | ## where cqual | |
97479f6f | 699 | |
51dfb5b8 | 700 | if (sms_errcode) return(sms_errcode); |
7536027d | 701 | return(SMS_SUCCESS); |
d26cae4e | 702 | ##} |
97479f6f | 703 | |
cc1231a1 | 704 | do_append(q, argv, pqual, action, actarg) |
d26cae4e | 705 | register struct query *q; |
706 | char *argv[]; | |
cc1231a1 | 707 | char *pqual; |
d26cae4e | 708 | int (*action)(); |
709 | char *actarg; | |
710 | ##{ | |
711 | ## char *rvar; | |
712 | ## char *rtable; | |
7536027d | 713 | ## char *cqual; |
cc1231a1 | 714 | ## int errorno; |
97479f6f | 715 | |
d26cae4e | 716 | rvar = q->rvar; |
717 | rtable = q->rtable; | |
718 | ## range of rvar is rtable | |
97479f6f | 719 | |
cc1231a1 | 720 | if (pqual) { |
721 | cqual = pqual; | |
7536027d | 722 | ## append to rtable (param (q->tlist, argv)) where cqual |
723 | } else { | |
724 | ## append to rtable (param (q->tlist, argv)) | |
725 | } | |
97479f6f | 726 | |
51dfb5b8 | 727 | if (sms_errcode) return(sms_errcode); |
7536027d | 728 | return(SMS_SUCCESS); |
d26cae4e | 729 | ##} |
97479f6f | 730 | |
e38c941b | 731 | do_delete(q, qual, action, actarg) |
d26cae4e | 732 | register struct query *q; |
733 | char *qual; | |
734 | int (*action)(); | |
735 | char *actarg; | |
736 | ##{ | |
737 | ## char *rvar; | |
738 | ## char *rtable; | |
739 | ## char *cqual; | |
cc1231a1 | 740 | ## int errorno; |
97479f6f | 741 | |
d26cae4e | 742 | rvar = q->rvar; |
743 | rtable = q->rtable; | |
744 | ## range of rvar is rtable | |
97479f6f | 745 | |
d26cae4e | 746 | cqual = qual; |
747 | ## delete rvar where cqual | |
97479f6f | 748 | |
51dfb5b8 | 749 | if (sms_errcode) return(sms_errcode); |
7536027d | 750 | return(SMS_SUCCESS); |
d26cae4e | 751 | ##} |
97479f6f | 752 | |
d26cae4e | 753 | |
83526631 | 754 | /** |
755 | ** set_next_object_id - set next object id in values table | |
756 | ** | |
757 | ** Inputs: object - object name in values table and in objects | |
758 | ** table - name of table objects are found in | |
759 | ** | |
760 | ** - called before an APPEND operation to set the next object id to | |
761 | ** be used for the new record to the next free value | |
762 | ** | |
763 | **/ | |
764 | ||
765 | set_next_object_id(object, table) | |
766 | char *object; | |
767 | char *table; | |
768 | ##{ | |
769 | ## char *name, *tbl; | |
770 | ## int rowcount, exists, value; | |
771 | ||
772 | name = object; | |
773 | tbl = table; | |
73c902bf | 774 | ## repeat retrieve (value = values.#value) where values.#name = @name |
83526631 | 775 | ## inquire_equel(rowcount = "rowcount") |
776 | if (rowcount != 1) | |
8fcb440e | 777 | return(SMS_NO_ID); |
83526631 | 778 | |
779 | ## retrieve (exists = any(tbl.name where tbl.name = value)) | |
780 | ## inquire_equel(rowcount = "rowcount") | |
781 | if (rowcount != 1) | |
8fcb440e | 782 | return(SMS_NO_ID); |
83526631 | 783 | while (exists) { |
784 | value++; | |
8fcb440e | 785 | if (value > MAX_ID_VALUE) |
786 | value = MIN_ID_VALUE; | |
83526631 | 787 | ## retrieve (exists = any(tbl.name where tbl.name = value)) |
788 | } | |
789 | ||
790 | if (LOG_RES) | |
791 | com_err(whoami, 0, "setting ID %s to %d", name, value); | |
73c902bf | 792 | ## repeat replace values (#value = @value) where values.#name = @name |
83526631 | 793 | return(SMS_SUCCESS); |
794 | ##} | |
795 | ||
796 | ||
2755928d | 797 | /* Turn a kerberos name into the user's ID of the account that principal |
798 | * owns. Sets the kerberos ID and user ID. | |
799 | */ | |
800 | ||
801 | set_krb_mapping(name, login, ok, kid, uid) | |
802 | char *name; | |
803 | char *login; | |
804 | int ok; | |
805 | int *kid; | |
806 | int *uid; | |
807 | ##{ | |
808 | ## int u_id, k_id, rowcount; | |
809 | ## char *krbname; | |
810 | ||
811 | krbname = name; | |
812 | *kid = 0; | |
813 | *uid = 0; | |
814 | ||
73c902bf | 815 | ## repeat retrieve (u_id = krbmap.#users_id, k_id = krbmap.#string_id) |
816 | ## where krbmap.string_id = strings.string_id and strings.string = @krbname | |
2755928d | 817 | ## inquire_equel (rowcount = "rowcount") |
818 | ||
819 | if (rowcount == 1) { | |
820 | *kid = -k_id; | |
821 | *uid = u_id; | |
822 | return; | |
823 | } | |
824 | ||
51dfb5b8 | 825 | if (name_to_id(name, "STRINGS", &k_id) == SMS_SUCCESS) |
73c902bf | 826 | *kid = -k_id; |
2755928d | 827 | |
828 | if (!ok) { | |
829 | *uid = *kid; | |
830 | return; | |
831 | } | |
832 | ||
51dfb5b8 | 833 | if (name_to_id(login, "USERS", uid) != SMS_SUCCESS) |
73c902bf | 834 | *uid = 0; |
835 | ||
2755928d | 836 | if (*kid == 0) |
837 | *kid = *uid; | |
838 | ##} | |
839 | ||
840 | ||
83526631 | 841 | /* For now this just checks the argc's. It should also see that there |
842 | * are no duplicate names. | |
843 | */ | |
844 | ||
845 | sanity_check_queries() | |
846 | { | |
847 | register int i; | |
848 | int maxv = 0, maxa = 0; | |
c5a0c32c | 849 | #ifdef MULTIPROTOCOLS |
83526631 | 850 | extern int QueryCount1, QueryCount2; |
851 | extern struct query Queries1[], Queries2[]; | |
c5a0c32c | 852 | #else |
853 | extern int QueryCount2; | |
854 | extern struct query Queries2[]; | |
855 | #endif MULTIPROTOCOLS | |
856 | ||
83526631 | 857 | #define MAX(x,y) ((x) > (y) ? (x) : (y)) |
858 | ||
c5a0c32c | 859 | #ifdef MULTIPROTOCOLS |
83526631 | 860 | for (i = 0; i < QueryCount1; i++) { |
861 | maxv = MAX(maxv, Queries1[i].vcnt); | |
862 | maxa = MAX(maxa, Queries1[i].argc); | |
863 | } | |
c5a0c32c | 864 | #endif MULTIPROTOCOLS |
83526631 | 865 | for (i = 0; i < QueryCount2; i++) { |
866 | maxv = MAX(maxv, Queries2[i].vcnt); | |
867 | maxa = MAX(maxa, Queries2[i].argc); | |
868 | } | |
869 | if (MAX(maxv, maxa) > QMAXARGS) { | |
870 | com_err(whoami, 0, "A query has more args than QMAXARGS"); | |
871 | exit(1); | |
872 | } | |
873 | } |