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);
81 /* open the database */
83 ## set lockmode session where level = table, timeout = query_timeout
87 int sms_close_database()
92 sms_check_access(cl, name, argc, argv_ro)
99 struct query *get_query_by_name();
103 q = get_query_by_name(name, cl->args->sms_version_no);
104 if (q == (struct query *)0)
105 return(SMS_NO_HANDLE);
107 return(sms_verify_query(cl, q, argc, argv_ro));
110 sms_process_query(cl, name, argc, argv_ro, action, actarg)
118 register struct query *q;
120 register struct validate *v;
126 struct save_queue *sq;
127 struct query *get_query_by_name();
129 struct save_queue *sq_create();
134 /* list queries command */
135 if (!strcmp(name, "_list_queries")) {
136 list_queries(cl->args->sms_version_no, action, actarg);
140 /* help query command */
141 if (!strcmp(name, "_help")) {
144 q = get_query_by_name(argv_ro[0], cl->args->sms_version_no);
145 if (q == (struct query *)0) return(SMS_NO_HANDLE);
146 help_query(q, action, actarg);
150 /* get query structure, return error if named query does not exist */
151 q = get_query_by_name(name, cl->args->sms_version_no);
152 if (q == (struct query *)0) return(SMS_NO_HANDLE);
155 if (q->type != RETRIEVE) {
156 ## set lockmode session where readlock = exclusive
160 /* setup argument vector, verify access and arguments */
161 if ((status = sms_verify_query(cl, q, argc, argv_ro)) != SMS_SUCCESS)
164 /* perform any special query pre-processing */
165 if (v && v->pre_rtn) {
166 status = (*v->pre_rtn)(q, Argv, cl, 0);
167 if (status != SMS_SUCCESS)
173 /* for queries that do not permit wildcarding, check if row
176 status = validate_row(q, Argv, v);
177 if (status != SMS_EXISTS) break;
180 /* build "where" clause if needed */
182 build_qual(q->qual, q->argc, Argv, qual);
188 /* build "sort" clause if needed */
189 if (v && v->valobj) {
190 psort = build_sort(v, sort);
195 /* if there is a followup routine, then we must save the results */
196 /* of the first query for use by the followup routine */
197 /* if q->rvar = NULL, perform post_rtn only */
199 if (v && v->post_rtn) {
201 status = do_retrieve(q, pqual, psort, sq_save_args, sq);
202 if (status != SMS_SUCCESS) {
206 status = (*v->post_rtn)(q, sq, v, action, actarg, cl);
208 /* normal retrieve */
209 status = do_retrieve(q, pqual, psort, action, actarg);
211 if (status != SMS_SUCCESS) break;
213 status = (*v->post_rtn)(q, Argv, cl, action, actarg);
219 /* see if row already exists */
221 status = validate_row(q, Argv, v);
222 if (status != SMS_EXISTS) break;
225 /* build "where" clause and perform update */
226 /* if q->rvar = NULL, perform post_rtn only */
228 build_qual(q->qual, q->argc, Argv, qual);
229 status = do_update(q, &Argv[q->argc], qual, action, actarg);
230 if (status != SMS_SUCCESS) break;
232 if (strcmp(q->shortname, "sshi") && strcmp(q->shortname, "ssif")) {
233 ## repeat replace tblstats (updates = tblstats.updates + 1,
235 ## where tblstats.#table = @table
239 /* execute followup routine (if any) */
240 if (v->post_rtn) status = (*v->post_rtn)(q, Argv, cl);
245 /* see if row already exists */
247 status = validate_row(q, Argv, v);
248 if (status != SMS_NO_MATCH) break;
251 /* increment id number if necessary */
253 status = set_next_object_id(v->object_id, q->rtable);
254 if (status != SMS_SUCCESS) break;
257 /* build "where" clause if needed */
259 build_qual(q->qual, q->argc, Argv, qual);
265 /* perform the append */
266 /* if q->rvar = NULL, perform post_rtn only */
268 status = do_append(q, &Argv[q->argc], pqual, action, actarg);
269 if (status != SMS_SUCCESS) break;
271 ## repeat replace tblstats (appends = tblstats.appends + 1,
273 ## where tblstats.#table = @table
276 /* execute followup routine */
277 if (v->post_rtn) status = (*v->post_rtn)(q, Argv, cl);
281 /* see if row already exists */
283 status = validate_row(q, Argv, v);
284 if (status != SMS_EXISTS) break;
287 /* build "where" clause and perform delete */
288 /* if q->rvar = NULL, perform post_rtn only */
290 build_qual(q->qual, q->argc, Argv, qual);
291 status = do_delete(q, qual, action, actarg);
292 if (status != SMS_SUCCESS) break;
294 ## repeat replace tblstats (deletes = tblstats.deletes + 1,
296 ## where tblstats.#table = @table
299 /* execute followup routine */
300 if (v->post_rtn) status = (*v->post_rtn)(q, Argv, cl);
306 if (q->type != RETRIEVE) {
307 if (status == SMS_SUCCESS) {
308 ## end transaction /* commit to this */
314 fprintf(journal, "%% %s %s %s",
315 cl->clname, cl->entity, ctime(&now));
316 fprintf(journal, "%s[%d] ", q->name, cl->args->sms_version_no);
317 for (i = 0; i < argc; i++) {
321 requote(buf, argv_ro[i], sizeof(buf));
328 if (ingres_errno != INGRES_DEADLOCK) {
329 ## abort /* it never happened */
332 ## set lockmode session where readlock = system
335 if (status == SMS_SUCCESS && ingres_errno != 0) {
336 critical_alert("SMS", "Server didn't notice INGRES ERROR %d",
338 status = SMS_INTERNAL;
341 if (status != SMS_SUCCESS && log_flags & LOG_RES)
342 com_err(whoami, status, " (Query failed)");
343 /* until all the clients know about SMS_BUSY, convert this error
344 * to one they will recognize.
346 if (status == SMS_BUSY)
347 status = SMS_DEADLOCK;
351 build_qual(fmt, argc, argv, qual)
363 for (i = 0; i < argc; i++) {
365 if (c++ == (char *)0) return(SMS_ARGS);
369 *(int *)&args[i] = *(int *)argv[i]; /* sigh */
371 return(SMS_INGRES_ERR);
380 sprintf(qual, fmt, args[0]);
384 sprintf(qual, fmt, args[0], args[1]);
388 sprintf(qual, fmt, args[0], args[1], args[2]);
392 sprintf(qual, fmt, args[0], args[1], args[2], args[3]);
400 register struct validate *v;
403 register struct valobj *vo;
412 if (vo->type == V_SORT) {
413 sprintf(elem, "RET_VAR%d", vo->index + 1);
414 if (*sort) strcat(sort, ", ");
420 return ((*sort) ? sort : 0);
424 /* Build arguement vector, verify query and arguments */
426 sms_verify_query(cl, q, argc, argv_ro)
434 register struct validate *v = q->validate;
436 register int privileged = 0;
438 /* copy the arguments into a local argv that we can modify */
439 if (argc >= QMAXARGS)
441 for (i = 0; i < argc; i++) {
442 if (strlen(argv_ro[i]) < ARGLEN)
443 strcpy(Argv[i], argv_ro[i]);
445 return(SMS_ARG_TOO_LONG);
448 /* check initial query access */
449 status = check_query_access(q, Argv, cl);
450 if (status != SMS_SUCCESS && status != SMS_PERM)
452 if (status == SMS_SUCCESS)
455 /* check argument count */
457 if (q->type == UPDATE || q->type == APPEND) argreq += q->vcnt;
458 if (argc != argreq) return(SMS_ARGS);
460 /* validate arguments */
461 if (v && v->valobj) {
462 status = validate_fields(q, Argv, v->valobj, v->objcnt);
463 if (status != SMS_SUCCESS) return(status);
466 /* perform special query access check */
467 if (!privileged && v && v->acs_rtn) {
468 status = (*v->acs_rtn)(q, Argv, cl);
469 if (status != SMS_SUCCESS && status != SMS_PERM)
471 if (status == SMS_SUCCESS)
475 return(privileged ? SMS_SUCCESS : SMS_PERM);
478 check_query_access(q, argv, cl)
488 ## static int def_uid;
493 /* get query access control list */
495 ## repeat retrieve (acl_id = capacls.list_id) where capacls.tag = @name
496 ## inquire_equel (rowcount = "rowcount", errorno = "errorno")
497 if (errorno != 0) return(SMS_INGRES_ERR);
498 if (rowcount == 0) return(SMS_PERM);
500 /* initialize default uid */
502 ## retrieve (def_uid = users.users_id) where users.login = "default"
505 /* check for default access */
506 ## range of m is members
507 ## repeat retrieve (exists = any(m.#member_id where m.list_id = @acl_id and
508 ## m.member_type = "USER" and m.#member_id = def_uid))
509 if (exists) return(SMS_SUCCESS);
511 /* parse client name */
512 status = get_client(cl, &client_type, &client_id);
513 if (status != SMS_SUCCESS) return(status);
515 /* see if client is in the list (or any of its sub-lists) */
516 exists = find_member("LIST", acl_id, client_type, client_id, 0);
517 return ((exists) ? SMS_SUCCESS : SMS_PERM);
520 get_client(cl, client_type, client_id)
530 if (cl->clname == NULL)
533 /* for now ignore instances */
536 /* if client is from local realm, get users_id */
537 if (!strcmp(krb->realm, krb_realm)) {
538 *client_id = cl->users_id;
539 *client_type = "USER";
543 /* otherwise use string_id */
545 ## repeat retrieve (member_id = strings.string_id)
546 ## where strings.string = @name
548 /* make sure we found a users or string id */
549 ## inquire_equel (rowcount = "rowcount")
550 if (rowcount == 0) return(SMS_PERM);
552 *client_type = "STRING";
553 *client_id = member_id;
557 ##find_member(list_type, list_id, member_type, member_id, sq)
560 ## char *member_type;
562 struct save_queue *sq;
567 struct save_queue *sq_create();
569 if (!strcmp(strtrim(list_type), strtrim(member_type)) &&
570 list_id == member_id)
573 /* see if client is a direct member of list */
574 ## repeat retrieve (exists = any(m.#member_id where
575 ## m.#list_id = @list_id and
576 ## m.#member_type = @member_type and
577 ## m.#member_id = @member_id))
578 if (exists) return(1);
580 /* are there any sub-lists? */
581 ## repeat retrieve (exists = any(m.#member_id where m.#list_id = @list_id and
582 ## m.#member_type = "LIST"))
583 if (!exists) return(0);
585 /* yes; now recurse through sublists */
587 /* create a save queue */
588 if (sq == (struct save_queue *)0) {
595 /* save all sublist ids */
596 ## range of m is members
597 ## retrieve (sublist = m.#member_id)
598 ## where m.#list_id = list_id and m.#member_type = "LIST"
600 sq_save_unique_data(sq, sublist);
603 if (child) return(0);
605 /* at top-level, check sub-lists for client (breadth-first search) */
606 while (sq_get_data(sq, &sublist)) {
607 exists = find_member(list_type, sublist, member_type, member_id, sq);
618 do_retrieve(q, pqual, psort, action, actarg)
619 register struct query *q;
631 static char **vaddrs = (char **)NULL;
636 if ((vaddrs = (char **)malloc(sizeof(char *) * QMAXARGS)) == NULL) {
637 com_err(whoami, SMS_NO_MEM, "setting up static argv");
640 for (i = 0; i < QMAXARGS; i++) {
641 if ((vaddrs[i] = malloc(QMAXARGSIZE)) == NULL) {
642 com_err(whoami, SMS_NO_MEM, "setting up static argv");
651 ## range of rvar is rtable
658 ## retrieve unique (param (q->tlist, vaddrs)) where cqual
661 (*action)(q->vcnt, vaddrs, actarg);
664 ## retrieve unique (param (q->tlist, vaddrs))
667 (*action)(q->vcnt, vaddrs, actarg);
674 ## retrieve unique (param (q->tlist, vaddrs)) where cqual
676 (*action)(q->vcnt, vaddrs, actarg);
679 ## retrieve unique (param (q->tlist, vaddrs))
681 (*action)(q->vcnt, vaddrs, actarg);
686 ## inquire_equel (rowcount = "rowcount", errorno = "errorno")
687 if (errorno != 0) return(SMS_INGRES_ERR);
688 return ((rowcount == 0) ? SMS_NO_MATCH : SMS_SUCCESS);
691 do_update(q, argv, qual, action, actarg)
692 register struct query *q;
705 ## range of rvar is rtable
708 ## replace rvar (param (q->tlist, argv))
711 ## inquire_equel (errorno = "errorno")
712 if (errorno == INGRES_BAD_INT)
714 else if (errorno != 0)
715 return(SMS_INGRES_ERR);
719 do_append(q, argv, pqual, action, actarg)
720 register struct query *q;
733 ## range of rvar is rtable
737 ## append to rtable (param (q->tlist, argv)) where cqual
739 ## append to rtable (param (q->tlist, argv))
742 ## inquire_equel (errorno = "errorno")
743 if (errorno == INGRES_BAD_INT)
745 else if (errorno != 0)
746 return(SMS_INGRES_ERR);
750 do_delete(q, qual, action, actarg)
751 register struct query *q;
763 ## range of rvar is rtable
766 ## delete rvar where cqual
768 ## inquire_equel (errorno = "errorno")
769 if (errorno != 0) return(SMS_INGRES_ERR);
775 ** set_next_object_id - set next object id in values table
777 ** Inputs: object - object name in values table and in objects
778 ** table - name of table objects are found in
780 ** - called before an APPEND operation to set the next object id to
781 ** be used for the new record to the next free value
785 set_next_object_id(object, table)
790 ## int rowcount, exists, value;
794 ## range of v is values
795 ## repeat retrieve (value = v.#value) where v.#name = @name
796 ## inquire_equel(rowcount = "rowcount")
800 ## retrieve (exists = any(tbl.name where tbl.name = value))
801 ## inquire_equel(rowcount = "rowcount")
806 if (value > MAX_ID_VALUE)
807 value = MIN_ID_VALUE;
808 ## retrieve (exists = any(tbl.name where tbl.name = value))
812 com_err(whoami, 0, "setting ID %s to %d", name, value);
813 ## repeat replace v (#value = @value) where v.#name = @name
818 /* For now this just checks the argc's. It should also see that there
819 * are no duplicate names.
822 sanity_check_queries()
825 int maxv = 0, maxa = 0;
826 extern int QueryCount1, QueryCount2;
827 extern struct query Queries1[], Queries2[];
828 #define MAX(x,y) ((x) > (y) ? (x) : (y))
830 for (i = 0; i < QueryCount1; i++) {
831 maxv = MAX(maxv, Queries1[i].vcnt);
832 maxa = MAX(maxa, Queries1[i].argc);
834 for (i = 0; i < QueryCount2; i++) {
835 maxv = MAX(maxv, Queries2[i].vcnt);
836 maxa = MAX(maxa, Queries2[i].argc);
838 if (MAX(maxv, maxa) > QMAXARGS) {
839 com_err(whoami, 0, "A query has more args than QMAXARGS");