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 */
79 int sms_close_database()
84 sms_check_access(cl, name, argc, argv_ro)
91 struct query *get_query_by_name();
93 q = get_query_by_name(name, cl->args->sms_version_no);
94 if (q == (struct query *)0)
95 return(SMS_NO_HANDLE);
97 return(sms_verify_query(cl, q, argc, argv_ro));
100 sms_process_query(cl, name, argc, argv_ro, action, actarg)
108 register struct query *q;
110 register struct validate *v;
116 struct save_queue *sq;
117 struct query *get_query_by_name();
119 struct save_queue *sq_create();
122 /* list queries command */
123 if (!strcmp(name, "_list_queries")) {
124 list_queries(cl->args->sms_version_no, action, actarg);
128 /* help query command */
129 if (!strcmp(name, "_help")) {
132 q = get_query_by_name(argv_ro[0], cl->args->sms_version_no);
133 if (q == (struct query *)0) return(SMS_NO_HANDLE);
134 help_query(q, action, actarg);
138 /* get query structure, return error if named query does not exist */
139 q = get_query_by_name(name, cl->args->sms_version_no);
140 if (q == (struct query *)0) return(SMS_NO_HANDLE);
143 if (q->type != RETRIEVE) {
144 ## set lockmode session where readlock = exclusive
148 /* setup argument vector, verify access and arguments */
149 if ((status = sms_verify_query(cl, q, argc, argv_ro)) != SMS_SUCCESS)
152 /* perform any special query pre-processing */
153 if (v && v->pre_rtn) {
154 status = (*v->pre_rtn)(q, Argv, cl, 0);
155 if (status != SMS_SUCCESS)
161 /* for queries that do not permit wildcarding, check if row
164 status = validate_row(q, Argv, v);
165 if (status != SMS_EXISTS) break;
168 /* build "where" clause if needed */
170 build_qual(q->qual, q->argc, Argv, qual);
176 /* build "sort" clause if needed */
177 if (v && v->valobj) {
178 psort = build_sort(v, sort);
183 /* if there is a followup routine, then we must save the results */
184 /* of the first query for use by the followup routine */
185 /* if q->rvar = NULL, perform post_rtn only */
187 if (v && v->post_rtn) {
189 status = do_retrieve(q, pqual, psort, sq_save_args, sq);
190 if (status != SMS_SUCCESS) {
194 status = (*v->post_rtn)(q, sq, v, action, actarg, cl);
196 /* normal retrieve */
197 status = do_retrieve(q, pqual, psort, action, actarg);
199 if (status != SMS_SUCCESS) break;
201 status = (*v->post_rtn)(q, Argv, cl, action, actarg);
207 /* see if row already exists */
209 status = validate_row(q, Argv, v);
210 if (status != SMS_EXISTS) break;
213 /* build "where" clause and perform update */
214 /* if q->rvar = NULL, perform post_rtn only */
216 build_qual(q->qual, q->argc, Argv, qual);
217 status = do_update(q, &Argv[q->argc], qual, action, actarg);
218 if (status != SMS_SUCCESS) break;
220 if (strcmp(q->shortname, "sshi") && strcmp(q->shortname, "ssif")) {
221 ## repeat replace tblstats (updates = tblstats.updates + 1,
223 ## where tblstats.#table = @table
227 /* execute followup routine (if any) */
228 if (v->post_rtn) status = (*v->post_rtn)(q, Argv, cl);
233 /* see if row already exists */
235 status = validate_row(q, Argv, v);
236 if (status != SMS_NO_MATCH) break;
239 /* increment id number if necessary */
241 status = set_next_object_id(v->object_id, q->rtable);
242 if (status != SMS_SUCCESS) break;
245 /* build "where" clause if needed */
247 build_qual(q->qual, q->argc, Argv, qual);
253 /* perform the append */
254 /* if q->rvar = NULL, perform post_rtn only */
256 status = do_append(q, &Argv[q->argc], pqual, action, actarg);
257 if (status != SMS_SUCCESS) break;
259 ## repeat replace tblstats (appends = tblstats.appends + 1,
261 ## where tblstats.#table = @table
264 /* execute followup routine */
265 if (v->post_rtn) status = (*v->post_rtn)(q, Argv, cl);
269 /* see if row already exists */
271 status = validate_row(q, Argv, v);
272 if (status != SMS_EXISTS) break;
275 /* build "where" clause and perform delete */
276 /* if q->rvar = NULL, perform post_rtn only */
278 build_qual(q->qual, q->argc, Argv, qual);
279 status = do_delete(q, qual, action, actarg);
280 if (status != SMS_SUCCESS) break;
282 ## repeat replace tblstats (deletes = tblstats.deletes + 1,
284 ## where tblstats.#table = @table
287 /* execute followup routine */
288 if (v->post_rtn) status = (*v->post_rtn)(q, Argv, cl);
294 if (q->type != RETRIEVE) {
295 if (status == SMS_SUCCESS) {
296 ## end transaction /* commit to this */
297 ## set lockmode session where readlock = system
303 fprintf(journal, "%% %s %s %s",
304 cl->clname, cl->entity, ctime(&now));
305 fprintf(journal, "%s[%d] ", q->name, cl->args->sms_version_no);
306 for (i = 0; i < argc; i++) {
310 requote(buf, argv_ro[i], sizeof(buf));
317 if (status != SMS_DEADLOCK) {
318 ## abort /* it never happened */
323 if (status != SMS_SUCCESS && log_flags & LOG_RES)
324 com_err(whoami, status, " (Query failed)");
328 build_qual(fmt, argc, argv, qual)
340 for (i = 0; i < argc; i++) {
342 if (c++ == (char *)0) return(SMS_ARGS);
346 *(int *)&args[i] = *(int *)argv[i]; /* sigh */
348 return(SMS_INGRES_ERR);
357 sprintf(qual, fmt, args[0]);
361 sprintf(qual, fmt, args[0], args[1]);
365 sprintf(qual, fmt, args[0], args[1], args[2]);
369 sprintf(qual, fmt, args[0], args[1], args[2], args[3]);
377 register struct validate *v;
380 register struct valobj *vo;
389 if (vo->type == V_SORT) {
390 sprintf(elem, "RET_VAR%d", vo->index + 1);
391 if (*sort) strcat(sort, ", ");
397 return ((*sort) ? sort : 0);
401 /* Build arguement vector, verify query and arguments */
403 sms_verify_query(cl, q, argc, argv_ro)
411 register struct validate *v = q->validate;
413 register int privileged = 0;
415 /* copy the arguments into a local argv that we can modify */
416 if (argc >= QMAXARGS)
418 for (i = 0; i < argc; i++) {
419 if (strlen(argv_ro[i]) < ARGLEN)
420 strcpy(Argv[i], argv_ro[i]);
422 return(SMS_ARG_TOO_LONG);
425 /* check initial query access */
426 status = check_query_access(q, Argv, cl);
427 if (status != SMS_SUCCESS && status != SMS_PERM)
429 if (status == SMS_SUCCESS)
432 /* check argument count */
434 if (q->type == UPDATE || q->type == APPEND) argreq += q->vcnt;
435 if (argc != argreq) return(SMS_ARGS);
437 /* validate arguments */
438 if (v && v->valobj) {
439 status = validate_fields(q, Argv, v->valobj, v->objcnt);
440 if (status != SMS_SUCCESS) return(status);
443 /* perform special query access check */
444 if (!privileged && v && v->acs_rtn) {
445 status = (*v->acs_rtn)(q, Argv, cl);
446 if (status != SMS_SUCCESS && status != SMS_PERM)
448 if (status == SMS_SUCCESS)
452 return(privileged ? SMS_SUCCESS : SMS_PERM);
455 check_query_access(q, argv, cl)
465 ## static int def_uid;
470 /* get query access control list */
472 ## repeat retrieve (acl_id = capacls.list_id) where capacls.tag = @name
473 ## inquire_equel (rowcount = "rowcount", errorno = "errorno")
474 if (errorno != 0) return(SMS_INGRES_ERR);
475 if (rowcount == 0) return(SMS_PERM);
477 /* initialize default uid */
479 ## retrieve (def_uid = users.users_id) where users.login = "default"
482 /* check for default access */
483 ## range of m is members
484 ## repeat retrieve (exists = any(m.#member_id where m.list_id = @acl_id and
485 ## m.member_type = "USER" and m.#member_id = def_uid))
486 if (exists) return(SMS_SUCCESS);
488 /* parse client name */
489 status = get_client(cl, &client_type, &client_id);
490 if (status != SMS_SUCCESS) return(status);
492 /* see if client is in the list (or any of its sub-lists) */
493 exists = find_member("LIST", acl_id, client_type, client_id, 0);
494 return ((exists) ? SMS_SUCCESS : SMS_PERM);
497 get_client(cl, client_type, client_id)
507 if (cl->clname == NULL)
510 /* for now ignore instances */
513 /* if client is from local realm, get users_id */
514 if (!strcmp(krb->realm, krb_realm)) {
515 *client_id = cl->users_id;
516 *client_type = "USER";
520 /* otherwise use string_id */
522 ## repeat retrieve (member_id = strings.string_id)
523 ## where strings.string = @name
525 /* make sure we found a users or string id */
526 ## inquire_equel (rowcount = "rowcount")
527 if (rowcount == 0) return(SMS_PERM);
529 *client_type = "STRING";
530 *client_id = member_id;
534 ##find_member(list_type, list_id, member_type, member_id, sq)
537 ## char *member_type;
539 struct save_queue *sq;
544 struct save_queue *sq_create();
546 if (!strcmp(strtrim(list_type), strtrim(member_type)) &&
547 list_id == member_id)
550 /* see if client is a direct member of list */
551 ## repeat retrieve (exists = any(m.#member_id where
552 ## m.#list_id = @list_id and
553 ## m.#member_type = @member_type and
554 ## m.#member_id = @member_id))
555 if (exists) return(1);
557 /* are there any sub-lists? */
558 ## repeat retrieve (exists = any(m.#member_id where m.#list_id = @list_id and
559 ## m.#member_type = "LIST"))
560 if (!exists) return(0);
562 /* yes; now recurse through sublists */
564 /* create a save queue */
565 if (sq == (struct save_queue *)0) {
572 /* save all sublist ids */
573 ## range of m is members
574 ## retrieve (sublist = m.#member_id)
575 ## where m.#list_id = list_id and m.#member_type = "LIST"
577 sq_save_unique_data(sq, sublist);
580 if (child) return(0);
582 /* at top-level, check sub-lists for client (breadth-first search) */
583 while (sq_get_data(sq, &sublist)) {
584 exists = find_member(list_type, sublist, member_type, member_id, sq);
595 do_retrieve(q, pqual, psort, action, actarg)
596 register struct query *q;
608 static char **vaddrs = (char **)NULL;
613 if ((vaddrs = (char **)malloc(sizeof(char *) * QMAXARGS)) == NULL) {
614 com_err(whoami, SMS_NO_MEM, "setting up static argv");
617 for (i = 0; i < QMAXARGS; i++) {
618 if ((vaddrs[i] = malloc(QMAXARGSIZE)) == NULL) {
619 com_err(whoami, SMS_NO_MEM, "setting up static argv");
628 ## range of rvar is rtable
635 ## retrieve unique (param (q->tlist, vaddrs)) where cqual
638 (*action)(q->vcnt, vaddrs, actarg);
641 ## retrieve unique (param (q->tlist, vaddrs))
644 (*action)(q->vcnt, vaddrs, actarg);
651 ## retrieve unique (param (q->tlist, vaddrs)) where cqual
653 (*action)(q->vcnt, vaddrs, actarg);
656 ## retrieve unique (param (q->tlist, vaddrs))
658 (*action)(q->vcnt, vaddrs, actarg);
663 ## inquire_equel (rowcount = "rowcount", errorno = "errorno")
664 if (errorno != 0) return(SMS_INGRES_ERR);
665 return ((rowcount == 0) ? SMS_NO_MATCH : SMS_SUCCESS);
668 do_update(q, argv, qual, action, actarg)
669 register struct query *q;
682 ## range of rvar is rtable
685 ## replace rvar (param (q->tlist, argv))
688 ## inquire_equel (errorno = "errorno")
689 if (errorno == INGRES_BAD_INT)
691 else if (errorno != 0)
692 return(SMS_INGRES_ERR);
696 do_append(q, argv, pqual, action, actarg)
697 register struct query *q;
710 ## range of rvar is rtable
714 ## append to rtable (param (q->tlist, argv)) where cqual
716 ## append to rtable (param (q->tlist, argv))
719 ## inquire_equel (errorno = "errorno")
720 if (errorno == INGRES_BAD_INT)
722 else if (errorno != 0)
723 return(SMS_INGRES_ERR);
727 do_delete(q, qual, action, actarg)
728 register struct query *q;
740 ## range of rvar is rtable
743 ## delete rvar where cqual
745 ## inquire_equel (errorno = "errorno")
746 if (errorno != 0) return(SMS_INGRES_ERR);
752 ** set_next_object_id - set next object id in values table
754 ** Inputs: object - object name in values table and in objects
755 ** table - name of table objects are found in
757 ** - called before an APPEND operation to set the next object id to
758 ** be used for the new record to the next free value
762 set_next_object_id(object, table)
767 ## int rowcount, exists, value;
771 ## range of v is values
772 ## repeat retrieve (value = v.#value) where v.#name = @name
773 ## inquire_equel(rowcount = "rowcount")
777 ## retrieve (exists = any(tbl.name where tbl.name = value))
778 ## inquire_equel(rowcount = "rowcount")
783 if (value > MAX_ID_VALUE)
784 value = MIN_ID_VALUE;
785 ## retrieve (exists = any(tbl.name where tbl.name = value))
789 com_err(whoami, 0, "setting ID %s to %d", name, value);
790 ## repeat replace v (#value = @value) where v.#name = @name
795 /* For now this just checks the argc's. It should also see that there
796 * are no duplicate names.
799 sanity_check_queries()
802 int maxv = 0, maxa = 0;
803 extern int QueryCount1, QueryCount2;
804 extern struct query Queries1[], Queries2[];
805 #define MAX(x,y) ((x) > (y) ? (x) : (y))
807 for (i = 0; i < QueryCount1; i++) {
808 maxv = MAX(maxv, Queries1[i].vcnt);
809 maxa = MAX(maxa, Queries1[i].argc);
811 for (i = 0; i < QueryCount2; i++) {
812 maxv = MAX(maxv, Queries2[i].vcnt);
813 maxa = MAX(maxa, Queries2[i].argc);
815 if (MAX(maxv, maxa) > QMAXARGS) {
816 com_err(whoami, 0, "A query has more args than QMAXARGS");