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"
24 ## int query_timeout = 30;
28 #define INGRES_BAD_INT 4111
29 #define INGRES_BAD_DATE 4302
30 #define INGRES_DEADLOCK 4700
31 #define INGRES_TIMEOUT 4702
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.
39 static int ingerr(num)
46 sms_errcode = SMS_INTEGER;
49 sms_errcode = SMS_DATE;
52 sms_errcode = SMS_DEADLOCK;
53 com_err(whoami, 0, "INGRES deadlock detected");
56 sms_errcode = SMS_BUSY;
57 com_err(whoami, 0, "timed out getting lock");
60 sms_errcode = SMS_INGRES_ERR;
61 com_err(whoami, SMS_INGRES_ERR, " code %d\n", *num);
62 critical_alert("SMS", "SMS server encountered INGRES ERROR %d", *num);
68 int sms_open_database()
72 static first_open = 1;
77 /* initialize local argv */
78 for (i = 0; i < 16; i++)
79 Argv[i] = malloc(ARGLEN);
89 /* open the database */
91 ## set lockmode session where level = table, timeout = query_timeout
92 ## set lockmode on capacls where readlock = shared
93 ## set lockmode on alias where readlock = shared
97 int sms_close_database()
103 sms_check_access(cl, name, argc, argv_ro)
110 struct query *get_query_by_name();
115 q = get_query_by_name(name, cl->args->sms_version_no);
116 if (q == (struct query *)0)
117 return(SMS_NO_HANDLE);
119 return(sms_verify_query(cl, q, argc, argv_ro));
122 sms_process_query(cl, name, argc, argv_ro, action, actarg)
130 register struct query *q;
132 register struct validate *v;
137 ## char *table, *rvar;
138 struct save_queue *sq;
139 struct query *get_query_by_name();
141 struct save_queue *sq_create();
147 /* list queries command */
148 if (!strcmp(name, "_list_queries")) {
149 list_queries(cl->args->sms_version_no, action, actarg);
153 /* help query command */
154 if (!strcmp(name, "_help")) {
157 q = get_query_by_name(argv_ro[0], cl->args->sms_version_no);
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, cl->args->sms_version_no);
165 if (q == (struct query *)0) return(SMS_NO_HANDLE);
168 if (q->type != RETRIEVE) {
172 /* setup argument vector, verify access and arguments */
173 if ((status = sms_verify_query(cl, q, argc, argv_ro)) != SMS_SUCCESS)
176 /* perform any special query pre-processing */
177 if (v && v->pre_rtn) {
178 status = (*v->pre_rtn)(q, Argv, cl, 0);
179 if (status != SMS_SUCCESS)
185 /* for queries that do not permit wildcarding, check if row
188 status = validate_row(q, Argv, v);
189 if (status != SMS_EXISTS) break;
192 /* build "where" clause if needed */
194 build_qual(q->qual, q->argc, Argv, qual);
200 /* build "sort" clause if needed */
201 if (v && v->valobj) {
202 psort = build_sort(v, sort);
207 /* if there is a followup routine, then we must save the results */
208 /* of the first query for use by the followup routine */
209 /* if q->rvar = NULL, perform post_rtn only */
211 if (v && v->post_rtn) {
213 status = do_retrieve(q, pqual, psort, sq_save_args, sq);
214 if (status != SMS_SUCCESS) {
218 status = (*v->post_rtn)(q, sq, v, action, actarg, cl);
220 /* normal retrieve */
221 status = do_retrieve(q, pqual, psort, action, actarg);
223 if (status != SMS_SUCCESS) break;
225 status = (*v->post_rtn)(q, Argv, cl, action, actarg);
231 /* see if row already exists */
233 status = validate_row(q, Argv, v);
234 if (status != SMS_EXISTS) break;
237 /* build "where" clause and perform update */
238 /* if q->rvar = NULL, perform post_rtn only */
240 build_qual(q->qual, q->argc, Argv, qual);
241 incremental_before(q->rtable, qual, argv_ro);
242 status = do_update(q, &Argv[q->argc], qual, action, actarg);
243 incremental_after(q->rtable, qual, argv_ro);
244 if (status != SMS_SUCCESS) break;
245 flush_name(argv_ro[0], q->rtable);
247 if (strcmp(q->shortname, "sshi") && strcmp(q->shortname, "ssif")) {
248 ## repeat replace tblstats (updates = tblstats.updates + 1,
250 ## where tblstats.#table = @table
254 /* execute followup routine (if any) */
255 if (v->post_rtn) status = (*v->post_rtn)(q, Argv, cl);
260 /* see if row already exists */
262 status = validate_row(q, Argv, v);
263 if (status != SMS_NO_MATCH) break;
266 /* increment id number if necessary */
268 status = set_next_object_id(v->object_id, q->rtable);
269 if (status != SMS_SUCCESS) break;
272 /* build "where" clause if needed */
274 build_qual(q->qual, q->argc, Argv, qual);
280 /* perform the append */
281 /* if q->rvar = NULL, perform post_rtn only */
283 incremental_clear_before();
284 status = do_append(q, &Argv[q->argc], pqual, action, actarg);
285 if (status != SMS_SUCCESS) break;
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);
291 incremental_after(q->rtable, pqual, argv_ro);
294 ## repeat replace tblstats (appends = tblstats.appends + 1,
296 ## where tblstats.#table = @table
299 /* execute followup routine */
300 if (v->post_rtn) status = (*v->post_rtn)(q, Argv, cl);
304 /* see if row already exists */
306 status = validate_row(q, Argv, v);
307 if (status != SMS_EXISTS) break;
310 /* build "where" clause and perform delete */
311 /* if q->rvar = NULL, perform post_rtn only */
313 build_qual(q->qual, q->argc, Argv, qual);
316 ## range of rvar is table
317 incremental_before(q->rtable, qual, argv_ro);
318 status = do_delete(q, qual, action, actarg);
319 incremental_clear_after();
320 if (status != SMS_SUCCESS) break;
321 flush_name(argv_ro[0], q->rtable);
322 ## repeat replace tblstats (deletes = tblstats.deletes + 1,
324 ## where tblstats.#table = @table
327 /* execute followup routine */
328 if (v->post_rtn) status = (*v->post_rtn)(q, Argv, cl);
334 if (status == SMS_SUCCESS && ingres_errno != 0) {
335 com_err(whoami, SMS_INTERNAL, "Server didn't notice INGRES ERROR %d",
337 status = sms_errcode;
340 if (q->type != RETRIEVE) {
341 if (status == SMS_SUCCESS) {
342 ## end transaction /* commit to this */
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++) {
355 requote(buf, argv_ro[i], sizeof(buf));
361 incremental_update();
363 if (ingres_errno != INGRES_DEADLOCK) {
364 ## abort /* it never happened */
368 ## set lockmode session where readlock = system
371 if (status != SMS_SUCCESS && log_flags & LOG_RES)
372 com_err(whoami, status, " (Query failed)");
376 build_qual(fmt, argc, argv, qual)
388 for (i = 0; i < argc; i++) {
390 if (c++ == (char *)0) return(SMS_ARGS);
394 *(int *)&args[i] = *(int *)argv[i]; /* sigh */
396 return(SMS_INGRES_ERR);
405 sprintf(qual, fmt, args[0]);
409 sprintf(qual, fmt, args[0], args[1]);
413 sprintf(qual, fmt, args[0], args[1], args[2]);
417 sprintf(qual, fmt, args[0], args[1], args[2], args[3]);
425 register struct validate *v;
428 register struct valobj *vo;
437 if (vo->type == V_SORT) {
438 sprintf(elem, "RET_VAR%d", vo->index + 1);
439 if (*sort) strcat(sort, ", ");
445 return ((*sort) ? sort : 0);
449 /* Build arguement vector, verify query and arguments */
451 sms_verify_query(cl, q, argc, argv_ro)
459 register struct validate *v = q->validate;
461 register int privileged = 0;
464 /* copy the arguments into a local argv that we can modify */
465 if (argc >= QMAXARGS)
467 for (i = 0; i < argc; i++) {
468 if ((len = strlen(argv_ro[i])) < ARGLEN)
469 strcpy(Argv[i], argv_ro[i]);
471 return(SMS_ARG_TOO_LONG);
472 if (Argv[i][len-1] == '\\')
473 return(SMS_BAD_CHAR);
476 /* check initial query access */
477 status = check_query_access(q, Argv, cl);
478 if (status != SMS_SUCCESS && status != SMS_PERM)
480 if (status == SMS_SUCCESS)
483 /* check argument count */
485 if (q->type == UPDATE || q->type == APPEND) argreq += q->vcnt;
486 if (argc != argreq) return(SMS_ARGS);
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);
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)
499 if (status == SMS_SUCCESS)
503 return(privileged ? SMS_SUCCESS : SMS_PERM);
507 /* This routine caches info from the database. Each query acl is stored
508 * in the query structure, and whether that acl contains everybody.
511 check_query_access(q, argv, cl)
521 ## static int def_uid;
526 /* initialize default uid */
528 ## retrieve (def_uid = users.users_id) where users.login = "default"
531 /* get query access control list */
536 ## retrieve (acl_id = capacls.list_id) where capacls.tag = name
537 ## inquire_equel (rowcount = "rowcount", errorno = "errorno")
538 if (errorno != 0) return(SMS_INGRES_ERR);
539 if (rowcount == 0) return(SMS_PERM);
542 /* check for default access */
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))
547 q->everybody = exists;
553 if (get_client(cl, &client_type, &client_id) != SMS_SUCCESS)
555 if (find_member("LIST", acl_id, client_type, client_id, 0))
562 get_client(cl, client_type, client_id)
567 if (cl->users_id > 0) {
568 *client_id = cl->users_id;
569 *client_type = "USER";
573 if (cl->client_id < 0) {
574 *client_id = -cl->users_id;
575 *client_type = "KERBEROS";
582 ##find_member(list_type, list_id, member_type, member_id)
585 ## char *member_type;
588 ## int exists, errorno;
590 if (!strcmp(strtrim(list_type), strtrim(member_type)) &&
591 list_id == member_id)
594 /* see if client is a direct member of list */
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))
599 ## inquire_equel(errorno = "errorno")
607 do_retrieve(q, pqual, psort, action, actarg)
608 register struct query *q;
620 static char **vaddrs = (char **)NULL;
625 if ((vaddrs = (char **)malloc(sizeof(char *) * QMAXARGS)) == NULL) {
626 com_err(whoami, SMS_NO_MEM, "setting up static argv");
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");
640 ## range of rvar is rtable
647 ## retrieve unique (param (q->tlist, vaddrs)) where cqual
650 (*action)(q->vcnt, vaddrs, actarg);
653 ## retrieve unique (param (q->tlist, vaddrs))
656 (*action)(q->vcnt, vaddrs, actarg);
663 ## retrieve unique (param (q->tlist, vaddrs)) where cqual
665 (*action)(q->vcnt, vaddrs, actarg);
668 ## retrieve unique (param (q->tlist, vaddrs))
670 (*action)(q->vcnt, vaddrs, actarg);
675 if (sms_errcode) return(sms_errcode);
676 ## inquire_equel (rowcount = "rowcount")
677 return ((rowcount == 0) ? SMS_NO_MATCH : SMS_SUCCESS);
680 do_update(q, argv, qual, action, actarg)
681 register struct query *q;
694 ## range of rvar is rtable
697 ## replace rvar (param (q->tlist, argv))
700 if (sms_errcode) return(sms_errcode);
704 do_append(q, argv, pqual, action, actarg)
705 register struct query *q;
718 ## range of rvar is rtable
722 ## append to rtable (param (q->tlist, argv)) where cqual
724 ## append to rtable (param (q->tlist, argv))
727 if (sms_errcode) return(sms_errcode);
731 do_delete(q, qual, action, actarg)
732 register struct query *q;
744 ## range of rvar is rtable
747 ## delete rvar where cqual
749 if (sms_errcode) return(sms_errcode);
755 ** set_next_object_id - set next object id in values table
757 ** Inputs: object - object name in values table and in objects
758 ** table - name of table objects are found in
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
765 set_next_object_id(object, table)
770 ## int rowcount, exists, value;
774 ## repeat retrieve (value = values.#value) where values.#name = @name
775 ## inquire_equel(rowcount = "rowcount")
779 ## retrieve (exists = any(tbl.name where tbl.name = value))
780 ## inquire_equel(rowcount = "rowcount")
785 if (value > MAX_ID_VALUE)
786 value = MIN_ID_VALUE;
787 ## retrieve (exists = any(tbl.name where tbl.name = value))
791 com_err(whoami, 0, "setting ID %s to %d", name, value);
792 ## repeat replace values (#value = @value) where values.#name = @name
797 /* Turn a kerberos name into the user's ID of the account that principal
798 * owns. Sets the kerberos ID and user ID.
801 set_krb_mapping(name, login, ok, kid, uid)
808 ## int u_id, k_id, rowcount;
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
817 ## inquire_equel (rowcount = "rowcount")
825 if (name_to_id(name, "STRINGS", &k_id) == SMS_SUCCESS)
833 if (name_to_id(login, "USERS", uid) != SMS_SUCCESS)
841 /* For now this just checks the argc's. It should also see that there
842 * are no duplicate names.
845 sanity_check_queries()
848 int maxv = 0, maxa = 0;
849 #ifdef MULTIPROTOCOLS
850 extern int QueryCount1, QueryCount2;
851 extern struct query Queries1[], Queries2[];
853 extern int QueryCount2;
854 extern struct query Queries2[];
855 #endif MULTIPROTOCOLS
857 #define MAX(x,y) ((x) > (y) ? (x) : (y))
859 #ifdef MULTIPROTOCOLS
860 for (i = 0; i < QueryCount1; i++) {
861 maxv = MAX(maxv, Queries1[i].vcnt);
862 maxa = MAX(maxa, Queries1[i].argc);
864 #endif MULTIPROTOCOLS
865 for (i = 0; i < QueryCount2; i++) {
866 maxv = MAX(maxv, Queries2[i].vcnt);
867 maxa = MAX(maxa, Queries2[i].argc);
869 if (MAX(maxv, maxa) > QMAXARGS) {
870 com_err(whoami, 0, "A query has more args than QMAXARGS");