6 * Copyright (C) 1987 by the Massachusetts Institute of Technology
9 * Revision 1.14 1988-01-14 13:37:57 mar
10 * 1. separate access checking from setup routines
11 * 2. check max argument length
13 * Revision 1.14 88/01/14 13:14:37 mar
14 * 1. separate access checking from setup routines
15 * 2. check max argument length
17 * Revision 1.13 88/01/04 12:02:13 mar
18 * moved transaction start before pre-routines (wesommer)
20 * Revision 1.13 87/11/12 18:13:12 wesommer
21 * Move transaction boundary to include the pre routine.
23 * Revision 1.12 87/09/12 20:42:11 wesommer
24 * Clean up after Gretzinger: cl->kname is not valid unless cl->clname is
27 * Revision 1.12 87/09/12 20:06:46 wesommer
28 * Fix security hole/null dereference bug: if clname is NULL, return
29 * permission denied in get_client.
31 * Revision 1.11 87/09/01 16:10:01 wesommer
32 * This change was made by Mike, who didn't feel like checking it in.
33 * Temp hack: ignore instances.
35 * Revision 1.10 87/08/28 14:57:51 mike
36 * Modified sms_query to not enclose RETRIEVE queries in begin/end transaction.
37 * This was necessary to allow get_all_poboxes and get_groups_of_all_users
38 * to temporarily change the Ingres lockmode.
40 * Revision 1.9 87/08/22 17:47:38 wesommer
41 * Cleanup (these changes were by Mike).
43 * Revision 1.8 87/08/10 16:22:26 mike
44 * wesommer modified error reporting.
46 * Revision 1.7 87/08/04 01:49:20 wesommer
47 * Rearranged messages.
49 * Revision 1.6 87/08/04 01:30:54 wesommer
50 * Mike's changes; checked in prior to working over messages.
52 * Revision 1.5 87/06/21 16:37:58 wesommer
53 * Changed include files, reindented things.
56 * Revision 1.4 87/06/08 05:03:27 wesommer
57 * Reindented; added header and trailer.
62 static char *rcsid_qrtn_qc = "$Header$";
66 #include "sms_server.h"
72 static int ingres_errno = 0;
76 * ingerr: (supposedly) called when Ingres indicates an error.
77 * I have not yet been able to get this to work to intercept a
78 * database open error.
81 static int ingerr(num)
84 ingres_errno = SMS_INGRES_ERR;
85 com_err(whoami, SMS_INGRES_ERR, " code %d\n", ingres_errno);
89 int sms_open_database()
93 /* initialize local argv */
94 for (i = 0; i < 16; i++)
95 Argv[i] = (char *)malloc(ARGLEN);
101 /* open the database */
106 int sms_close_database()
111 sms_check_access(cl, name, argc, argv_ro)
118 struct query *get_query_by_name();
120 q = get_query_by_name(name);
121 if (q == (struct query *)0)
122 return(SMS_NO_HANDLE);
124 return(sms_verify_query(cl, q, argc, argv_ro));
127 sms_process_query(cl, name, argc, argv_ro, action, actarg)
135 register struct query *q;
137 register struct validate *v;
143 struct save_queue *sq;
144 struct query *get_query_by_name();
146 struct save_queue *sq_create();
149 /* list queries command */
150 if (!strcmp(name, "_list_queries")) {
151 list_queries(action, actarg);
155 /* help query command */
156 if (!strcmp(name, "_help")) {
157 q = get_query_by_name(argv_ro[0]);
158 if (q == (struct query *)0) return(SMS_NO_HANDLE);
159 help_query(q, action, actarg);
163 /* get query structure, return error if named query does not exist */
164 q = get_query_by_name(name);
165 if (q == (struct query *)0) return(SMS_NO_HANDLE);
168 if (q->type != RETRIEVE)
171 /* setup argument vector, verify access and arguments */
172 if ((status = sms_verify_query(cl, q, argc, argv_ro)) != SMS_SUCCESS)
175 /* perform any special query pre-processing */
176 if (v && v->pre_rtn) {
177 status = (*v->pre_rtn)(q, Argv, cl, 0);
178 if (status != SMS_SUCCESS)
184 /* for queries that do not permit wildcarding, check if row
187 status = validate_row(q, Argv, v);
188 if (status != SMS_EXISTS) break;
191 /* build "where" clause if needed */
193 build_qual(q->qual, q->argc, Argv, qual);
199 /* build "sort" clause if needed */
200 if (v && v->valobj) {
201 psort = build_sort(v, sort);
206 /* if there is a followup routine, then we must save the results */
207 /* of the first query for use by the followup routine */
208 /* if q->rvar = NULL, perform post_rtn only */
210 if (v && v->post_rtn) {
212 status = do_retrieve(q, pqual, psort, sq_save_args, sq);
213 if (status != SMS_SUCCESS) {
217 status = (*v->post_rtn)(q, sq, v, action, actarg);
219 /* normal retrieve */
220 status = do_retrieve(q, pqual, psort, action, actarg);
222 if (status != SMS_SUCCESS) break;
224 ## repeat replace tblstats (retrieves = tblstats.retrieves + 1)
225 ## where tblstats.#table = @table
227 status = (*v->post_rtn)(q, Argv, action, actarg);
233 /* see if row already exists */
235 status = validate_row(q, Argv, v);
236 if (status != SMS_EXISTS) break;
239 /* build "where" clause and perform update */
240 /* if q->rvar = NULL, perform post_rtn only */
242 build_qual(q->qual, q->argc, Argv, qual);
243 status = do_update(q, &Argv[q->argc], qual, action, actarg);
244 if (status != SMS_SUCCESS) break;
246 ## repeat replace tblstats (updates = tblstats.updates + 1,
248 ## where tblstats.#table = @table
251 /* execute followup routine (if any) */
252 if (v->post_rtn) status = (*v->post_rtn)(q, Argv);
257 /* see if row already exists */
259 status = validate_row(q, Argv, v);
260 if (status != SMS_NO_MATCH) break;
263 /* increment id number if necessary */
265 status = set_next_object_id(v->object_id);
266 if (status != SMS_SUCCESS) break;
269 /* build "where" clause if needed */
271 build_qual(q->qual, q->argc, Argv, qual);
277 /* perform the append */
278 /* if q->rvar = NULL, perform post_rtn only */
280 status = do_append(q, &Argv[q->argc], pqual, action, actarg);
281 if (status != SMS_SUCCESS) break;
283 ## repeat replace tblstats (appends = tblstats.appends + 1,
285 ## where tblstats.#table = @table
288 /* execute followup routine */
289 if (v->post_rtn) status = (*v->post_rtn)(q, Argv);
293 /* see if row already exists */
295 status = validate_row(q, Argv, v);
296 if (status != SMS_EXISTS) break;
299 /* build "where" clause and perform delete */
300 /* if q->rvar = NULL, perform post_rtn only */
302 build_qual(q->qual, q->argc, Argv, qual);
303 status = do_delete(q, qual, action, actarg);
304 if (status != SMS_SUCCESS) break;
306 ## repeat replace tblstats (deletes = tblstats.deletes + 1,
308 ## where tblstats.#table = @table
311 /* execute followup routine */
312 if (v->post_rtn) status = (*v->post_rtn)(q, Argv);
318 if (q->type != RETRIEVE) {
319 if (status == SMS_SUCCESS) {
320 ## end transaction /* commit to this */
322 ## abort /* it never happened */
326 if (status != SMS_SUCCESS && log_flags & LOG_RES)
327 com_err(whoami, status, " (Query failed)");
331 build_qual(fmt, argc, argv, qual)
342 for (i = 0; i < argc; i++) {
343 c = (char *)index(c, '%');
344 if (c++ == (char *)0) return(SMS_ARGS);
348 *(int *)&args[i] = *(int *)argv[i]; /* sigh */
350 return(SMS_INGRES_ERR);
359 sprintf(qual, fmt, args[0]);
363 sprintf(qual, fmt, args[0], args[1]);
367 sprintf(qual, fmt, args[0], args[1], args[2]);
371 sprintf(qual, fmt, args[0], args[1], args[2], args[3]);
378 register struct validate *v;
381 register struct valobj *vo;
390 if (vo->type == V_SORT) {
391 sprintf(elem, "RET_VAR%d", vo->index + 1);
392 if (*sort) strcat(sort, ", ");
398 return ((*sort) ? sort : 0);
402 /* Build arguement vector, verify query and arguments */
404 sms_verify_query(cl, q, argc, argv_ro)
412 register struct validate *v = q->validate;
414 register int privileged = 0;
416 /* copy the arguments into a local argv that we can modify */
417 for (i = 0; i < argc; i++) {
418 if (strlen(argv_ro[i]) < ARGLEN)
419 strcpy(Argv[i], argv_ro[i]);
421 return(SMS_ARG_TOO_LONG);
424 /* check initial query access */
425 status = check_query_access(q, Argv, cl);
426 if (status != SMS_SUCCESS && status != SMS_PERM)
428 if (status == SMS_SUCCESS)
431 /* check argument count */
433 if (q->type == UPDATE || q->type == APPEND) argreq += q->vcnt;
434 if (argc != argreq) return(SMS_ARGS);
436 /* validate arguments */
437 if (v && v->valobj) {
438 status = validate_fields(q, Argv, v->valobj, v->objcnt);
439 if (status != SMS_SUCCESS) return(status);
442 /* perform special query access check */
443 if (!privileged && v && v->acs_rtn) {
444 status = (*v->acs_rtn)(q, Argv, cl);
445 if (status != SMS_SUCCESS && status != SMS_PERM)
447 if (status == SMS_SUCCESS)
451 return(privileged ? SMS_SUCCESS : SMS_PERM);
454 check_query_access(q, argv, cl)
464 ## static int def_uid;
469 /* get query access control list */
471 ## repeat retrieve (acl_id = capacls.list_id) where capacls.tag = @name
472 ## inquire_equel (rowcount = "rowcount", errorno = "errorno")
473 if (errorno != 0) return(SMS_INGRES_ERR);
474 if (rowcount == 0) return(SMS_PERM);
476 /* initialize default uid */
478 ## retrieve (def_uid = users.users_id) where users.login = "default"
481 /* check for default access */
482 ## range of m is members
483 ## repeat retrieve (exists = any(m.#member_id where m.list_id = @acl_id and
484 ## m.member_type = "USER" and m.#member_id = def_uid))
485 if (exists) return(SMS_SUCCESS);
487 /* parse client name */
488 status = get_client(cl, &client_type, &client_id);
489 if (status != SMS_SUCCESS) return(status);
491 /* see if client is in the list (or any of its sub-lists) */
492 exists = find_member(acl_id, client_type, client_id, 0);
493 return ((exists) ? SMS_SUCCESS : SMS_PERM);
496 get_client(cl, client_type, client_id)
506 if (cl->clname == NULL)
509 /* for now ignore instances */
512 /* if client is from local realm, get users_id */
513 if (!strcmp(krb->realm, krb_realm)) {
515 ## repeat retrieve (member_id = users.users_id) where users.login = @name
516 *client_type = "USER";
518 /* otherwise use string_id */
520 ## repeat retrieve (member_id = strings.string_id)
521 ## where strings.string = @name
522 *client_type = "STRING";
525 /* make sure we found a users or string id */
526 ## inquire_equel (rowcount = "rowcount")
527 if (rowcount == 0) return(SMS_PERM);
529 *client_id = member_id;
533 ##find_member(list_id, member_type, member_id, sq)
535 ## char *member_type;
537 struct save_queue *sq;
542 struct save_queue *sq_create();
544 /* see if client is a direct member of list */
545 ## repeat retrieve (exists = any(m.#member_id where
546 ## m.#list_id = @list_id and
547 ## m.#member_type = @member_type and
548 ## m.#member_id = @member_id))
549 if (exists) return(1);
551 /* are there any sub-lists? */
552 ## repeat retrieve (exists = any(m.#member_id where m.#list_id = @list_id and
553 ## m.#member_type = "LIST"))
554 if (!exists) return(0);
556 /* yes; now recurse through sublists */
558 /* create a save queue */
559 if (sq == (struct save_queue *)0) {
566 /* save all sublist ids */
567 ## range of m is members
568 ## retrieve (sublist = m.#member_id)
569 ## where m.#list_id = list_id and m.#member_type = "LIST"
571 sq_save_unique_data(sq, sublist);
576 /* at top-level, check sub-lists for client (breadth-first search) */
577 while (sq_get_data(sq, &sublist)) {
578 exists = find_member(sublist, member_type, member_id, sq);
586 do_retrieve(q, pqual, psort, action, actarg)
587 register struct query *q;
603 ## range of rvar is rtable
610 ## retrieve unique (param (q->tlist, q->vaddr)) where cqual
613 (*action)(q->vcnt, q->vaddr, actarg);
616 ## retrieve unique (param (q->tlist, q->vaddr))
619 (*action)(q->vcnt, q->vaddr, actarg);
626 ## retrieve unique (param (q->tlist, q->vaddr)) where cqual
628 (*action)(q->vcnt, q->vaddr, actarg);
631 ## retrieve unique (param (q->tlist, q->vaddr))
633 (*action)(q->vcnt, q->vaddr, actarg);
638 ## inquire_equel (rowcount = "rowcount", errorno = "errorno")
639 if (errorno != 0) return(SMS_INGRES_ERR);
640 return ((rowcount == 0) ? SMS_NO_MATCH : SMS_SUCCESS);
643 do_update(q, argv, qual, action, actarg)
644 register struct query *q;
658 ## range of rvar is rtable
661 ## replace rvar (param (q->tlist, argv))
664 ## inquire_equel (errorno = "errorno")
665 if (errorno != 0) return(SMS_INGRES_ERR);
669 do_append(q, argv, pqual, action, actarg)
670 register struct query *q;
683 ## range of rvar is rtable
687 ## append to rtable (param (q->tlist, argv)) where cqual
689 ## append to rtable (param (q->tlist, argv))
692 ## inquire_equel (errorno = "errorno")
693 if (errorno != 0) return(SMS_INGRES_ERR);
697 do_delete(q, qual, action, actarg)
698 register struct query *q;
710 ## range of rvar is rtable
713 ## delete rvar where cqual
715 ## inquire_equel (errorno = "errorno")
716 if (errorno != 0) return(SMS_INGRES_ERR);
725 * c-continued-statement-offset: 4
727 * c-argdecl-indent: 4