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()
73 /* initialize local argv */
74 for (i = 0; i < 16; i++)
75 Argv[i] = malloc(ARGLEN);
82 /* open the database */
84 ## set lockmode session where level = table, timeout = query_timeout
85 ## set lockmode on capacls where readlock = shared
86 ## set lockmode on alias where readlock = shared
90 int sms_close_database()
95 sms_check_access(cl, name, argc, argv_ro)
102 struct query *get_query_by_name();
106 q = get_query_by_name(name, cl->args->sms_version_no);
107 if (q == (struct query *)0)
108 return(SMS_NO_HANDLE);
110 return(sms_verify_query(cl, q, argc, argv_ro));
113 sms_process_query(cl, name, argc, argv_ro, action, actarg)
121 register struct query *q;
123 register struct validate *v;
129 struct save_queue *sq;
130 struct query *get_query_by_name();
132 struct save_queue *sq_create();
137 /* list queries command */
138 if (!strcmp(name, "_list_queries")) {
139 list_queries(cl->args->sms_version_no, action, actarg);
143 /* help query command */
144 if (!strcmp(name, "_help")) {
147 q = get_query_by_name(argv_ro[0], cl->args->sms_version_no);
148 if (q == (struct query *)0) return(SMS_NO_HANDLE);
149 help_query(q, action, actarg);
153 /* get query structure, return error if named query does not exist */
154 q = get_query_by_name(name, cl->args->sms_version_no);
155 if (q == (struct query *)0) return(SMS_NO_HANDLE);
158 if (q->type != RETRIEVE) {
162 /* setup argument vector, verify access and arguments */
163 if ((status = sms_verify_query(cl, q, argc, argv_ro)) != SMS_SUCCESS)
166 /* perform any special query pre-processing */
167 if (v && v->pre_rtn) {
168 status = (*v->pre_rtn)(q, Argv, cl, 0);
169 if (status != SMS_SUCCESS)
175 /* for queries that do not permit wildcarding, check if row
178 status = validate_row(q, Argv, v);
179 if (status != SMS_EXISTS) break;
182 /* build "where" clause if needed */
184 build_qual(q->qual, q->argc, Argv, qual);
190 /* build "sort" clause if needed */
191 if (v && v->valobj) {
192 psort = build_sort(v, sort);
197 /* if there is a followup routine, then we must save the results */
198 /* of the first query for use by the followup routine */
199 /* if q->rvar = NULL, perform post_rtn only */
201 if (v && v->post_rtn) {
203 status = do_retrieve(q, pqual, psort, sq_save_args, sq);
204 if (status != SMS_SUCCESS) {
208 status = (*v->post_rtn)(q, sq, v, action, actarg, cl);
210 /* normal retrieve */
211 status = do_retrieve(q, pqual, psort, action, actarg);
213 if (status != SMS_SUCCESS) break;
215 status = (*v->post_rtn)(q, Argv, cl, action, actarg);
221 /* see if row already exists */
223 status = validate_row(q, Argv, v);
224 if (status != SMS_EXISTS) break;
227 /* build "where" clause and perform update */
228 /* if q->rvar = NULL, perform post_rtn only */
230 build_qual(q->qual, q->argc, Argv, qual);
231 incremental_before(q->rtable, qual, argv_ro);
232 status = do_update(q, &Argv[q->argc], qual, action, actarg);
233 incremental_after(q->rtable, qual, argv_ro);
234 if (status != SMS_SUCCESS) break;
236 if (strcmp(q->shortname, "sshi") && strcmp(q->shortname, "ssif")) {
237 ## repeat replace tblstats (updates = tblstats.updates + 1,
239 ## where tblstats.#table = @table
243 /* execute followup routine (if any) */
244 if (v->post_rtn) status = (*v->post_rtn)(q, Argv, cl);
249 /* see if row already exists */
251 status = validate_row(q, Argv, v);
252 if (status != SMS_NO_MATCH) break;
255 /* increment id number if necessary */
257 status = set_next_object_id(v->object_id, q->rtable);
258 if (status != SMS_SUCCESS) break;
261 /* build "where" clause if needed */
263 build_qual(q->qual, q->argc, Argv, qual);
269 /* perform the append */
270 /* if q->rvar = NULL, perform post_rtn only */
272 incremental_clear_before();
273 status = do_append(q, &Argv[q->argc], pqual, action, actarg);
274 if (status != SMS_SUCCESS) break;
275 if (v && v->object_id) {
276 sprintf(qual, "%s.%s = values.value and values.name = \"%s\"",
277 q->rvar, v->object_id, v->object_id);
278 incremental_after(q->rtable, qual, argv_ro);
280 incremental_after(q->rtable, pqual, argv_ro);
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, cl);
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 incremental_before(q->rtable, qual, argv_ro);
304 status = do_delete(q, qual, action, actarg);
305 incremental_clear_after();
306 if (status != SMS_SUCCESS) break;
308 ## repeat replace tblstats (deletes = tblstats.deletes + 1,
310 ## where tblstats.#table = @table
313 /* execute followup routine */
314 if (v->post_rtn) status = (*v->post_rtn)(q, Argv, cl);
320 if (q->type != RETRIEVE) {
321 if (status == SMS_SUCCESS) {
322 ## end transaction /* commit to this */
328 fprintf(journal, "%% %s %s %s",
329 cl->clname, cl->entity, ctime(&now));
330 fprintf(journal, "%s[%d] ", q->name, cl->args->sms_version_no);
331 for (i = 0; i < argc; i++) {
335 requote(buf, argv_ro[i], sizeof(buf));
341 incremental_update();
343 if (ingres_errno != INGRES_DEADLOCK) {
344 ## abort /* it never happened */
348 ## set lockmode session where readlock = system
351 if (status == SMS_SUCCESS && ingres_errno != 0) {
352 critical_alert("SMS", "Server didn't notice INGRES ERROR %d",
354 status = SMS_INTERNAL;
357 if (status != SMS_SUCCESS && log_flags & LOG_RES)
358 com_err(whoami, status, " (Query failed)");
362 build_qual(fmt, argc, argv, qual)
374 for (i = 0; i < argc; i++) {
376 if (c++ == (char *)0) return(SMS_ARGS);
380 *(int *)&args[i] = *(int *)argv[i]; /* sigh */
382 return(SMS_INGRES_ERR);
391 sprintf(qual, fmt, args[0]);
395 sprintf(qual, fmt, args[0], args[1]);
399 sprintf(qual, fmt, args[0], args[1], args[2]);
403 sprintf(qual, fmt, args[0], args[1], args[2], args[3]);
411 register struct validate *v;
414 register struct valobj *vo;
423 if (vo->type == V_SORT) {
424 sprintf(elem, "RET_VAR%d", vo->index + 1);
425 if (*sort) strcat(sort, ", ");
431 return ((*sort) ? sort : 0);
435 /* Build arguement vector, verify query and arguments */
437 sms_verify_query(cl, q, argc, argv_ro)
445 register struct validate *v = q->validate;
447 register int privileged = 0;
450 /* copy the arguments into a local argv that we can modify */
451 if (argc >= QMAXARGS)
453 for (i = 0; i < argc; i++) {
454 if ((len = strlen(argv_ro[i])) < ARGLEN)
455 strcpy(Argv[i], argv_ro[i]);
457 return(SMS_ARG_TOO_LONG);
458 if (Argv[i][len-1] == '\\')
459 return(SMS_BAD_CHAR);
462 /* check initial query access */
463 status = check_query_access(q, Argv, cl);
464 if (status != SMS_SUCCESS && status != SMS_PERM)
466 if (status == SMS_SUCCESS)
469 /* check argument count */
471 if (q->type == UPDATE || q->type == APPEND) argreq += q->vcnt;
472 if (argc != argreq) return(SMS_ARGS);
474 /* validate arguments */
475 if (v && v->valobj) {
476 status = validate_fields(q, Argv, v->valobj, v->objcnt);
477 if (status != SMS_SUCCESS) return(status);
480 /* perform special query access check */
481 if (!privileged && v && v->acs_rtn) {
482 status = (*v->acs_rtn)(q, Argv, cl);
483 if (status != SMS_SUCCESS && status != SMS_PERM)
485 if (status == SMS_SUCCESS)
489 return(privileged ? SMS_SUCCESS : SMS_PERM);
493 /* This routine caches info from the database. Each query acl is stored
494 * in the query structure, and whether that acl contains everybody.
497 check_query_access(q, argv, cl)
507 ## static int def_uid;
512 /* initialize default uid */
514 ## retrieve (def_uid = users.users_id) where users.login = "default"
517 /* get query access control list */
522 ## repeat retrieve (acl_id = capacls.list_id) where capacls.tag = @name
523 ## inquire_equel (rowcount = "rowcount", errorno = "errorno")
524 if (errorno != 0) return(SMS_INGRES_ERR);
525 if (rowcount == 0) return(SMS_PERM);
528 /* check for default access */
529 ## repeat retrieve (exists = any(imembers.#member_id where
530 ## imembers.list_id = @acl_id and
531 ## imembers.member_type = "USER" and
532 ## imembers.#member_id = def_uid))
533 q->everybody = exists;
539 if (get_client(cl, &client_type, &client_id) != SMS_SUCCESS)
541 if (find_member("LIST", acl_id, client_type, client_id, 0))
548 get_client(cl, client_type, client_id)
553 if (cl->users_id > 0) {
554 *client_id = cl->users_id;
555 *client_type = "USER";
559 if (cl->client_id < 0) {
560 *client_id = -cl->users_id;
561 *client_type = "KERBEROS";
568 ##find_member(list_type, list_id, member_type, member_id)
571 ## char *member_type;
574 ## int exists, errorno;
576 if (!strcmp(strtrim(list_type), strtrim(member_type)) &&
577 list_id == member_id)
580 /* see if client is a direct member of list */
581 ## repeat retrieve (exists = any(imembers.#member_id where
582 ## imembers.#list_id = @list_id and
583 ## imembers.#member_type = @member_type and
584 ## imembers.#member_id = @member_id))
585 ## inquire_equel(errorno = "errorno")
593 do_retrieve(q, pqual, psort, action, actarg)
594 register struct query *q;
606 static char **vaddrs = (char **)NULL;
611 if ((vaddrs = (char **)malloc(sizeof(char *) * QMAXARGS)) == NULL) {
612 com_err(whoami, SMS_NO_MEM, "setting up static argv");
615 for (i = 0; i < QMAXARGS; i++) {
616 if ((vaddrs[i] = malloc(QMAXARGSIZE)) == NULL) {
617 com_err(whoami, SMS_NO_MEM, "setting up static argv");
626 ## range of rvar is rtable
633 ## retrieve unique (param (q->tlist, vaddrs)) where cqual
636 (*action)(q->vcnt, vaddrs, actarg);
639 ## retrieve unique (param (q->tlist, vaddrs))
642 (*action)(q->vcnt, vaddrs, actarg);
649 ## retrieve unique (param (q->tlist, vaddrs)) where cqual
651 (*action)(q->vcnt, vaddrs, actarg);
654 ## retrieve unique (param (q->tlist, vaddrs))
656 (*action)(q->vcnt, vaddrs, actarg);
661 ## inquire_equel (rowcount = "rowcount", errorno = "errorno")
662 if (errorno != 0) return(SMS_INGRES_ERR);
663 return ((rowcount == 0) ? SMS_NO_MATCH : SMS_SUCCESS);
666 do_update(q, argv, qual, action, actarg)
667 register struct query *q;
680 ## range of rvar is rtable
683 ## replace rvar (param (q->tlist, argv))
686 ## inquire_equel (errorno = "errorno")
687 if (errorno == INGRES_BAD_INT)
689 else if (errorno != 0)
690 return(SMS_INGRES_ERR);
694 do_append(q, argv, pqual, action, actarg)
695 register struct query *q;
708 ## range of rvar is rtable
712 ## append to rtable (param (q->tlist, argv)) where cqual
714 ## append to rtable (param (q->tlist, argv))
717 ## inquire_equel (errorno = "errorno")
718 if (errorno == INGRES_BAD_INT)
720 else if (errorno != 0)
721 return(SMS_INGRES_ERR);
725 do_delete(q, qual, action, actarg)
726 register struct query *q;
738 ## range of rvar is rtable
741 ## delete rvar where cqual
743 ## inquire_equel (errorno = "errorno")
744 if (errorno != 0) return(SMS_INGRES_ERR);
750 ** set_next_object_id - set next object id in values table
752 ** Inputs: object - object name in values table and in objects
753 ** table - name of table objects are found in
755 ** - called before an APPEND operation to set the next object id to
756 ** be used for the new record to the next free value
760 set_next_object_id(object, table)
765 ## int rowcount, exists, value;
769 ## range of v is values
770 ## repeat retrieve (value = v.#value) where v.#name = @name
771 ## inquire_equel(rowcount = "rowcount")
775 ## retrieve (exists = any(tbl.name where tbl.name = value))
776 ## inquire_equel(rowcount = "rowcount")
781 if (value > MAX_ID_VALUE)
782 value = MIN_ID_VALUE;
783 ## retrieve (exists = any(tbl.name where tbl.name = value))
787 com_err(whoami, 0, "setting ID %s to %d", name, value);
788 ## repeat replace v (#value = @value) where v.#name = @name
793 /* This looks up a login name and returns the SMS internal ID. It is used
794 * by authenticate to put the users_id in the client structure.
797 int get_users_id(name)
805 ## range of u is users
806 ## repeat retrieve (id = u.#users_id) where u.#login = @login
807 ## inquire_equel (rowcount = "rowcount")
816 /* Turn a kerberos name into the user's ID of the account that principal
817 * owns. Sets the kerberos ID and user ID.
820 set_krb_mapping(name, login, ok, kid, uid)
827 ## int u_id, k_id, rowcount;
834 ## range of k is krbmap
835 ## range of s is strings
836 ## repeat retrieve (u_id = k.#users_id, k_id = k.#string_id)
837 ## where k.string_id = s.string_id and s.string = @krbname
838 ## inquire_equel (rowcount = "rowcount")
846 ## repeat retrieve (k_id = s.#string_id) where s.string = @krbname
847 ## inquire_equel (rowcount = "rowcount")
858 *uid = get_users_id(login);
864 /* For now this just checks the argc's. It should also see that there
865 * are no duplicate names.
868 sanity_check_queries()
871 int maxv = 0, maxa = 0;
872 #ifdef MULTIPROTOCOLS
873 extern int QueryCount1, QueryCount2;
874 extern struct query Queries1[], Queries2[];
876 extern int QueryCount2;
877 extern struct query Queries2[];
878 #endif MULTIPROTOCOLS
880 #define MAX(x,y) ((x) > (y) ? (x) : (y))
882 #ifdef MULTIPROTOCOLS
883 for (i = 0; i < QueryCount1; i++) {
884 maxv = MAX(maxv, Queries1[i].vcnt);
885 maxa = MAX(maxa, Queries1[i].argc);
887 #endif MULTIPROTOCOLS
888 for (i = 0; i < QueryCount2; i++) {
889 maxv = MAX(maxv, Queries2[i].vcnt);
890 maxa = MAX(maxa, Queries2[i].argc);
892 if (MAX(maxv, maxa) > QMAXARGS) {
893 com_err(whoami, 0, "A query has more args than QMAXARGS");