6 * Copyright (C) 1987, 1988 by the Massachusetts Institute of Technology
7 * For copying and distribution information, please see the file
13 static char *rcsid_qrtn_qc = "$Header$";
16 #include <mit-copyright.h>
18 #include "sms_server.h"
22 static int ingres_errno = 0;
26 #define INGRES_BAD_INT 4111
27 #define INGRES_BAD_DATE 4302
28 #define INGRES_DEADLOCK 4700
31 * ingerr: (supposedly) called when Ingres indicates an error.
32 * I have not yet been able to get this to work to intercept a
33 * database open error.
36 static int ingerr(num)
43 ingres_errno = SMS_INTEGER;
46 ingres_errno = SMS_DATE;
49 com_err(whoami, 0, "INGRES deadlock detected");
50 ingres_errno = SMS_DEADLOCK;
53 ingres_errno = SMS_INGRES_ERR;
54 com_err(whoami, SMS_INGRES_ERR, " code %d\n", *num);
55 critical_alert("SMS", "SMS server encountered INGRES ERROR %d", *num);
61 int sms_open_database()
66 /* initialize local argv */
67 for (i = 0; i < 16; i++)
68 Argv[i] = malloc(ARGLEN);
74 /* open the database */
76 ## set lockmode session where readlock = exclusive
80 int sms_close_database()
85 sms_check_access(cl, name, argc, argv_ro)
92 struct query *get_query_by_name();
94 q = get_query_by_name(name, cl->args->sms_version_no);
95 if (q == (struct query *)0)
96 return(SMS_NO_HANDLE);
98 return(sms_verify_query(cl, q, argc, argv_ro));
101 sms_process_query(cl, name, argc, argv_ro, action, actarg)
109 register struct query *q;
111 register struct validate *v;
117 struct save_queue *sq;
118 struct query *get_query_by_name();
120 struct save_queue *sq_create();
123 /* list queries command */
124 if (!strcmp(name, "_list_queries")) {
125 list_queries(cl->args->sms_version_no, action, actarg);
129 /* help query command */
130 if (!strcmp(name, "_help")) {
133 q = get_query_by_name(argv_ro[0], cl->args->sms_version_no);
134 if (q == (struct query *)0) return(SMS_NO_HANDLE);
135 help_query(q, action, actarg);
139 /* get query structure, return error if named query does not exist */
140 q = get_query_by_name(name, cl->args->sms_version_no);
141 if (q == (struct query *)0) return(SMS_NO_HANDLE);
144 if (q->type != RETRIEVE) {
145 ## set lockmode session where readlock = exclusive
149 /* setup argument vector, verify access and arguments */
150 if ((status = sms_verify_query(cl, q, argc, argv_ro)) != SMS_SUCCESS)
153 /* perform any special query pre-processing */
154 if (v && v->pre_rtn) {
155 status = (*v->pre_rtn)(q, Argv, cl, 0);
156 if (status != SMS_SUCCESS)
162 /* for queries that do not permit wildcarding, check if row
165 status = validate_row(q, Argv, v);
166 if (status != SMS_EXISTS) break;
169 /* build "where" clause if needed */
171 build_qual(q->qual, q->argc, Argv, qual);
177 /* build "sort" clause if needed */
178 if (v && v->valobj) {
179 psort = build_sort(v, sort);
184 /* if there is a followup routine, then we must save the results */
185 /* of the first query for use by the followup routine */
186 /* if q->rvar = NULL, perform post_rtn only */
188 if (v && v->post_rtn) {
190 status = do_retrieve(q, pqual, psort, sq_save_args, sq);
191 if (status != SMS_SUCCESS) {
195 status = (*v->post_rtn)(q, sq, v, action, actarg, cl);
197 /* normal retrieve */
198 status = do_retrieve(q, pqual, psort, action, actarg);
200 if (status != SMS_SUCCESS) break;
202 status = (*v->post_rtn)(q, Argv, cl, action, actarg);
208 /* see if row already exists */
210 status = validate_row(q, Argv, v);
211 if (status != SMS_EXISTS) break;
214 /* build "where" clause and perform update */
215 /* if q->rvar = NULL, perform post_rtn only */
217 build_qual(q->qual, q->argc, Argv, qual);
218 status = do_update(q, &Argv[q->argc], qual, action, actarg);
219 if (status != SMS_SUCCESS) break;
221 if (strcmp(q->shortname, "sshi") && strcmp(q->shortname, "ssif")) {
222 ## repeat replace tblstats (updates = tblstats.updates + 1,
224 ## where tblstats.#table = @table
228 /* execute followup routine (if any) */
229 if (v->post_rtn) status = (*v->post_rtn)(q, Argv, cl);
234 /* see if row already exists */
236 status = validate_row(q, Argv, v);
237 if (status != SMS_NO_MATCH) break;
240 /* increment id number if necessary */
242 status = set_next_object_id(v->object_id, q->rtable);
243 if (status != SMS_SUCCESS) break;
246 /* build "where" clause if needed */
248 build_qual(q->qual, q->argc, Argv, qual);
254 /* perform the append */
255 /* if q->rvar = NULL, perform post_rtn only */
257 status = do_append(q, &Argv[q->argc], pqual, action, actarg);
258 if (status != SMS_SUCCESS) break;
260 ## repeat replace tblstats (appends = tblstats.appends + 1,
262 ## where tblstats.#table = @table
265 /* execute followup routine */
266 if (v->post_rtn) status = (*v->post_rtn)(q, Argv, cl);
270 /* see if row already exists */
272 status = validate_row(q, Argv, v);
273 if (status != SMS_EXISTS) break;
276 /* build "where" clause and perform delete */
277 /* if q->rvar = NULL, perform post_rtn only */
279 build_qual(q->qual, q->argc, Argv, qual);
280 status = do_delete(q, qual, action, actarg);
281 if (status != SMS_SUCCESS) break;
283 ## repeat replace tblstats (deletes = tblstats.deletes + 1,
285 ## where tblstats.#table = @table
288 /* execute followup routine */
289 if (v->post_rtn) status = (*v->post_rtn)(q, Argv, cl);
295 if (q->type != RETRIEVE) {
296 if (status == SMS_SUCCESS) {
297 ## end transaction /* commit to this */
298 ## set lockmode session where readlock = system
304 fprintf(journal, "%% %s %s %s",
305 cl->clname, cl->entity, ctime(&now));
306 fprintf(journal, "%s[%d] ", q->name, cl->args->sms_version_no);
307 for (i = 0; i < argc; i++) {
311 requote(buf, argv_ro[i], sizeof(buf));
318 if (status != SMS_DEADLOCK) {
319 ## abort /* it never happened */
324 if (status != SMS_SUCCESS && log_flags & LOG_RES)
325 com_err(whoami, status, " (Query failed)");
329 build_qual(fmt, argc, argv, qual)
341 for (i = 0; i < argc; i++) {
343 if (c++ == (char *)0) return(SMS_ARGS);
347 *(int *)&args[i] = *(int *)argv[i]; /* sigh */
349 return(SMS_INGRES_ERR);
358 sprintf(qual, fmt, args[0]);
362 sprintf(qual, fmt, args[0], args[1]);
366 sprintf(qual, fmt, args[0], args[1], args[2]);
370 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 if (argc >= QMAXARGS)
419 for (i = 0; i < argc; i++) {
420 if (strlen(argv_ro[i]) < ARGLEN)
421 strcpy(Argv[i], argv_ro[i]);
423 return(SMS_ARG_TOO_LONG);
426 /* check initial query access */
427 status = check_query_access(q, Argv, cl);
428 if (status != SMS_SUCCESS && status != SMS_PERM)
430 if (status == SMS_SUCCESS)
433 /* check argument count */
435 if (q->type == UPDATE || q->type == APPEND) argreq += q->vcnt;
436 if (argc != argreq) return(SMS_ARGS);
438 /* validate arguments */
439 if (v && v->valobj) {
440 status = validate_fields(q, Argv, v->valobj, v->objcnt);
441 if (status != SMS_SUCCESS) return(status);
444 /* perform special query access check */
445 if (!privileged && v && v->acs_rtn) {
446 status = (*v->acs_rtn)(q, Argv, cl);
447 if (status != SMS_SUCCESS && status != SMS_PERM)
449 if (status == SMS_SUCCESS)
453 return(privileged ? SMS_SUCCESS : SMS_PERM);
456 check_query_access(q, argv, cl)
466 ## static int def_uid;
471 /* get query access control list */
473 ## repeat retrieve (acl_id = capacls.list_id) where capacls.tag = @name
474 ## inquire_equel (rowcount = "rowcount", errorno = "errorno")
475 if (errorno != 0) return(SMS_INGRES_ERR);
476 if (rowcount == 0) return(SMS_PERM);
478 /* initialize default uid */
480 ## retrieve (def_uid = users.users_id) where users.login = "default"
483 /* check for default access */
484 ## range of m is members
485 ## repeat retrieve (exists = any(m.#member_id where m.list_id = @acl_id and
486 ## m.member_type = "USER" and m.#member_id = def_uid))
487 if (exists) return(SMS_SUCCESS);
489 /* parse client name */
490 status = get_client(cl, &client_type, &client_id);
491 if (status != SMS_SUCCESS) return(status);
493 /* see if client is in the list (or any of its sub-lists) */
494 exists = find_member("LIST", acl_id, client_type, client_id, 0);
495 return ((exists) ? SMS_SUCCESS : SMS_PERM);
498 get_client(cl, client_type, client_id)
508 if (cl->clname == NULL)
511 /* for now ignore instances */
514 /* if client is from local realm, get users_id */
515 if (!strcmp(krb->realm, krb_realm)) {
516 *client_id = cl->users_id;
517 *client_type = "USER";
521 /* otherwise use string_id */
523 ## repeat retrieve (member_id = strings.string_id)
524 ## where strings.string = @name
526 /* make sure we found a users or string id */
527 ## inquire_equel (rowcount = "rowcount")
528 if (rowcount == 0) return(SMS_PERM);
530 *client_type = "STRING";
531 *client_id = member_id;
535 ##find_member(list_type, list_id, member_type, member_id, sq)
538 ## char *member_type;
540 struct save_queue *sq;
545 struct save_queue *sq_create();
547 if (!strcmp(strtrim(list_type), strtrim(member_type)) &&
548 list_id == member_id)
551 /* see if client is a direct member of list */
552 ## repeat retrieve (exists = any(m.#member_id where
553 ## m.#list_id = @list_id and
554 ## m.#member_type = @member_type and
555 ## m.#member_id = @member_id))
556 if (exists) return(1);
558 /* are there any sub-lists? */
559 ## repeat retrieve (exists = any(m.#member_id where m.#list_id = @list_id and
560 ## m.#member_type = "LIST"))
561 if (!exists) return(0);
563 /* yes; now recurse through sublists */
565 /* create a save queue */
566 if (sq == (struct save_queue *)0) {
573 /* save all sublist ids */
574 ## range of m is members
575 ## retrieve (sublist = m.#member_id)
576 ## where m.#list_id = list_id and m.#member_type = "LIST"
578 sq_save_unique_data(sq, sublist);
581 if (child) return(0);
583 /* at top-level, check sub-lists for client (breadth-first search) */
584 while (sq_get_data(sq, &sublist)) {
585 exists = find_member(list_type, sublist, member_type, member_id, sq);
596 do_retrieve(q, pqual, psort, action, actarg)
597 register struct query *q;
609 static char **vaddrs = (char **)NULL;
614 if ((vaddrs = (char **)malloc(sizeof(char *) * QMAXARGS)) == NULL) {
615 com_err(whoami, SMS_NO_MEM, "setting up static argv");
618 for (i = 0; i < QMAXARGS; i++) {
619 if ((vaddrs[i] = malloc(QMAXARGSIZE)) == NULL) {
620 com_err(whoami, SMS_NO_MEM, "setting up static argv");
629 ## range of rvar is rtable
636 ## retrieve unique (param (q->tlist, vaddrs)) where cqual
639 (*action)(q->vcnt, vaddrs, actarg);
642 ## retrieve unique (param (q->tlist, vaddrs))
645 (*action)(q->vcnt, vaddrs, actarg);
652 ## retrieve unique (param (q->tlist, vaddrs)) where cqual
654 (*action)(q->vcnt, vaddrs, actarg);
657 ## retrieve unique (param (q->tlist, vaddrs))
659 (*action)(q->vcnt, vaddrs, actarg);
664 ## inquire_equel (rowcount = "rowcount", errorno = "errorno")
665 if (errorno != 0) return(SMS_INGRES_ERR);
666 return ((rowcount == 0) ? SMS_NO_MATCH : SMS_SUCCESS);
669 do_update(q, argv, qual, action, actarg)
670 register struct query *q;
683 ## range of rvar is rtable
686 ## replace rvar (param (q->tlist, argv))
689 ## inquire_equel (errorno = "errorno")
690 if (errorno == INGRES_BAD_INT)
692 else if (errorno != 0)
693 return(SMS_INGRES_ERR);
697 do_append(q, argv, pqual, action, actarg)
698 register struct query *q;
711 ## range of rvar is rtable
715 ## append to rtable (param (q->tlist, argv)) where cqual
717 ## append to rtable (param (q->tlist, argv))
720 ## inquire_equel (errorno = "errorno")
721 if (errorno == INGRES_BAD_INT)
723 else if (errorno != 0)
724 return(SMS_INGRES_ERR);
728 do_delete(q, qual, action, actarg)
729 register struct query *q;
741 ## range of rvar is rtable
744 ## delete rvar where cqual
746 ## inquire_equel (errorno = "errorno")
747 if (errorno != 0) return(SMS_INGRES_ERR);
753 ** set_next_object_id - set next object id in values table
755 ** Inputs: object - object name in values table and in objects
756 ** table - name of table objects are found in
758 ** - called before an APPEND operation to set the next object id to
759 ** be used for the new record to the next free value
763 set_next_object_id(object, table)
768 ## int rowcount, exists, value;
772 ## range of v is values
773 ## repeat retrieve (value = v.#value) where v.#name = @name
774 ## inquire_equel(rowcount = "rowcount")
778 ## retrieve (exists = any(tbl.name where tbl.name = value))
779 ## inquire_equel(rowcount = "rowcount")
784 if (value > MAX_ID_VALUE)
785 value = MIN_ID_VALUE;
786 ## retrieve (exists = any(tbl.name where tbl.name = value))
790 com_err(whoami, 0, "setting ID %s to %d", name, value);
791 ## repeat replace v (#value = @value) where v.#name = @name
796 /* For now this just checks the argc's. It should also see that there
797 * are no duplicate names.
800 sanity_check_queries()
803 int maxv = 0, maxa = 0;
804 extern int QueryCount1, QueryCount2;
805 extern struct query Queries1[], Queries2[];
806 #define MAX(x,y) ((x) > (y) ? (x) : (y))
808 for (i = 0; i < QueryCount1; i++) {
809 maxv = MAX(maxv, Queries1[i].vcnt);
810 maxa = MAX(maxa, Queries1[i].argc);
812 for (i = 0; i < QueryCount2; i++) {
813 maxv = MAX(maxv, Queries2[i].vcnt);
814 maxa = MAX(maxa, Queries2[i].argc);
816 if (MAX(maxv, maxa) > QMAXARGS) {
817 com_err(whoami, 0, "A query has more args than QMAXARGS");