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
84 ## set lockmode on capacls where readlock = shared
85 ## set lockmode on alias where readlock = shared
89 int sms_close_database()
94 sms_check_access(cl, name, argc, argv_ro)
101 struct query *get_query_by_name();
105 q = get_query_by_name(name, cl->args->sms_version_no);
106 if (q == (struct query *)0)
107 return(SMS_NO_HANDLE);
109 return(sms_verify_query(cl, q, argc, argv_ro));
112 sms_process_query(cl, name, argc, argv_ro, action, actarg)
120 register struct query *q;
122 register struct validate *v;
128 struct save_queue *sq;
129 struct query *get_query_by_name();
131 struct save_queue *sq_create();
136 /* list queries command */
137 if (!strcmp(name, "_list_queries")) {
138 list_queries(cl->args->sms_version_no, action, actarg);
142 /* help query command */
143 if (!strcmp(name, "_help")) {
146 q = get_query_by_name(argv_ro[0], cl->args->sms_version_no);
147 if (q == (struct query *)0) return(SMS_NO_HANDLE);
148 help_query(q, action, actarg);
152 /* get query structure, return error if named query does not exist */
153 q = get_query_by_name(name, cl->args->sms_version_no);
154 if (q == (struct query *)0) return(SMS_NO_HANDLE);
157 if (q->type != RETRIEVE) {
158 ## set lockmode session where readlock = exclusive
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 status = do_update(q, &Argv[q->argc], qual, action, actarg);
232 if (status != SMS_SUCCESS) break;
234 if (strcmp(q->shortname, "sshi") && strcmp(q->shortname, "ssif")) {
235 ## repeat replace tblstats (updates = tblstats.updates + 1,
237 ## where tblstats.#table = @table
241 /* execute followup routine (if any) */
242 if (v->post_rtn) status = (*v->post_rtn)(q, Argv, cl);
247 /* see if row already exists */
249 status = validate_row(q, Argv, v);
250 if (status != SMS_NO_MATCH) break;
253 /* increment id number if necessary */
255 status = set_next_object_id(v->object_id, q->rtable);
256 if (status != SMS_SUCCESS) break;
259 /* build "where" clause if needed */
261 build_qual(q->qual, q->argc, Argv, qual);
267 /* perform the append */
268 /* if q->rvar = NULL, perform post_rtn only */
270 status = do_append(q, &Argv[q->argc], pqual, action, actarg);
271 if (status != SMS_SUCCESS) break;
273 ## repeat replace tblstats (appends = tblstats.appends + 1,
275 ## where tblstats.#table = @table
278 /* execute followup routine */
279 if (v->post_rtn) status = (*v->post_rtn)(q, Argv, cl);
283 /* see if row already exists */
285 status = validate_row(q, Argv, v);
286 if (status != SMS_EXISTS) break;
289 /* build "where" clause and perform delete */
290 /* if q->rvar = NULL, perform post_rtn only */
292 build_qual(q->qual, q->argc, Argv, qual);
293 status = do_delete(q, qual, action, actarg);
294 if (status != SMS_SUCCESS) break;
296 ## repeat replace tblstats (deletes = tblstats.deletes + 1,
298 ## where tblstats.#table = @table
301 /* execute followup routine */
302 if (v->post_rtn) status = (*v->post_rtn)(q, Argv, cl);
308 if (q->type != RETRIEVE) {
309 if (status == SMS_SUCCESS) {
310 ## end transaction /* commit to this */
316 fprintf(journal, "%% %s %s %s",
317 cl->clname, cl->entity, ctime(&now));
318 fprintf(journal, "%s[%d] ", q->name, cl->args->sms_version_no);
319 for (i = 0; i < argc; i++) {
323 requote(buf, argv_ro[i], sizeof(buf));
330 if (ingres_errno != INGRES_DEADLOCK) {
331 ## abort /* it never happened */
334 ## set lockmode session where readlock = system
337 if (status == SMS_SUCCESS && ingres_errno != 0) {
338 critical_alert("SMS", "Server didn't notice INGRES ERROR %d",
340 status = SMS_INTERNAL;
343 if (status != SMS_SUCCESS && log_flags & LOG_RES)
344 com_err(whoami, status, " (Query failed)");
345 /* until all the clients know about SMS_BUSY, convert this error
346 * to one they will recognize.
348 if (status == SMS_BUSY)
349 status = SMS_DEADLOCK;
353 build_qual(fmt, argc, argv, qual)
365 for (i = 0; i < argc; i++) {
367 if (c++ == (char *)0) return(SMS_ARGS);
371 *(int *)&args[i] = *(int *)argv[i]; /* sigh */
373 return(SMS_INGRES_ERR);
382 sprintf(qual, fmt, args[0]);
386 sprintf(qual, fmt, args[0], args[1]);
390 sprintf(qual, fmt, args[0], args[1], args[2]);
394 sprintf(qual, fmt, args[0], args[1], args[2], args[3]);
402 register struct validate *v;
405 register struct valobj *vo;
414 if (vo->type == V_SORT) {
415 sprintf(elem, "RET_VAR%d", vo->index + 1);
416 if (*sort) strcat(sort, ", ");
422 return ((*sort) ? sort : 0);
426 /* Build arguement vector, verify query and arguments */
428 sms_verify_query(cl, q, argc, argv_ro)
436 register struct validate *v = q->validate;
438 register int privileged = 0;
441 /* copy the arguments into a local argv that we can modify */
442 if (argc >= QMAXARGS)
444 for (i = 0; i < argc; i++) {
445 if ((len = strlen(argv_ro[i])) < ARGLEN)
446 strcpy(Argv[i], argv_ro[i]);
448 return(SMS_ARG_TOO_LONG);
449 if (Argv[i][len-1] == '\\')
450 return(SMS_BAD_CHAR);
453 /* check initial query access */
454 status = check_query_access(q, Argv, cl);
455 if (status != SMS_SUCCESS && status != SMS_PERM)
457 if (status == SMS_SUCCESS)
460 /* check argument count */
462 if (q->type == UPDATE || q->type == APPEND) argreq += q->vcnt;
463 if (argc != argreq) return(SMS_ARGS);
465 /* validate arguments */
466 if (v && v->valobj) {
467 status = validate_fields(q, Argv, v->valobj, v->objcnt);
468 if (status != SMS_SUCCESS) return(status);
471 /* perform special query access check */
472 if (!privileged && v && v->acs_rtn) {
473 status = (*v->acs_rtn)(q, Argv, cl);
474 if (status != SMS_SUCCESS && status != SMS_PERM)
476 if (status == SMS_SUCCESS)
480 return(privileged ? SMS_SUCCESS : SMS_PERM);
483 check_query_access(q, argv, cl)
493 ## static int def_uid;
498 /* get query access control list */
500 ## repeat retrieve (acl_id = capacls.list_id) where capacls.tag = @name
501 ## inquire_equel (rowcount = "rowcount", errorno = "errorno")
502 if (errorno != 0) return(SMS_INGRES_ERR);
503 if (rowcount == 0) return(SMS_PERM);
505 /* initialize default uid */
507 ## retrieve (def_uid = users.users_id) where users.login = "default"
510 /* check for default access */
511 ## range of m is members
512 ## repeat retrieve (exists = any(m.#member_id where m.list_id = @acl_id and
513 ## m.member_type = "USER" and m.#member_id = def_uid))
514 if (exists) return(SMS_SUCCESS);
516 /* parse client name */
517 status = get_client(cl, &client_type, &client_id);
518 if (status != SMS_SUCCESS) return(status);
520 /* see if client is in the list (or any of its sub-lists) */
521 exists = find_member("LIST", acl_id, client_type, client_id, 0);
522 return ((exists) ? SMS_SUCCESS : SMS_PERM);
525 get_client(cl, client_type, client_id)
535 if (cl->clname == NULL)
538 /* for now ignore instances */
541 /* if client is from local realm, get users_id */
542 if (!strcmp(krb->realm, krb_realm)) {
543 *client_id = cl->users_id;
544 *client_type = "USER";
548 /* otherwise use string_id */
550 ## repeat retrieve (member_id = strings.string_id)
551 ## where strings.string = @name
553 /* make sure we found a users or string id */
554 ## inquire_equel (rowcount = "rowcount")
555 if (rowcount == 0) return(SMS_PERM);
557 *client_type = "STRING";
558 *client_id = member_id;
562 ##find_member(list_type, list_id, member_type, member_id, sq)
565 ## char *member_type;
567 struct save_queue *sq;
572 struct save_queue *sq_create();
574 if (!strcmp(strtrim(list_type), strtrim(member_type)) &&
575 list_id == member_id)
578 /* see if client is a direct member of list */
579 ## repeat retrieve (exists = any(m.#member_id where
580 ## m.#list_id = @list_id and
581 ## m.#member_type = @member_type and
582 ## m.#member_id = @member_id))
583 if (exists) return(1);
585 /* are there any sub-lists? */
586 ## repeat retrieve (exists = any(m.#member_id where m.#list_id = @list_id and
587 ## m.#member_type = "LIST"))
588 if (!exists) return(0);
590 /* yes; now recurse through sublists */
592 /* create a save queue */
593 if (sq == (struct save_queue *)0) {
600 /* save all sublist ids */
601 ## range of m is members
602 ## retrieve (sublist = m.#member_id)
603 ## where m.#list_id = list_id and m.#member_type = "LIST"
605 sq_save_unique_data(sq, sublist);
608 if (child) return(0);
610 /* at top-level, check sub-lists for client (breadth-first search) */
611 while (sq_get_data(sq, &sublist)) {
612 exists = find_member(list_type, sublist, member_type, member_id, sq);
623 do_retrieve(q, pqual, psort, action, actarg)
624 register struct query *q;
636 static char **vaddrs = (char **)NULL;
641 if ((vaddrs = (char **)malloc(sizeof(char *) * QMAXARGS)) == NULL) {
642 com_err(whoami, SMS_NO_MEM, "setting up static argv");
645 for (i = 0; i < QMAXARGS; i++) {
646 if ((vaddrs[i] = malloc(QMAXARGSIZE)) == NULL) {
647 com_err(whoami, SMS_NO_MEM, "setting up static argv");
656 ## range of rvar is rtable
663 ## retrieve unique (param (q->tlist, vaddrs)) where cqual
666 (*action)(q->vcnt, vaddrs, actarg);
669 ## retrieve unique (param (q->tlist, vaddrs))
672 (*action)(q->vcnt, vaddrs, actarg);
679 ## retrieve unique (param (q->tlist, vaddrs)) where cqual
681 (*action)(q->vcnt, vaddrs, actarg);
684 ## retrieve unique (param (q->tlist, vaddrs))
686 (*action)(q->vcnt, vaddrs, actarg);
691 ## inquire_equel (rowcount = "rowcount", errorno = "errorno")
692 if (errorno != 0) return(SMS_INGRES_ERR);
693 return ((rowcount == 0) ? SMS_NO_MATCH : SMS_SUCCESS);
696 do_update(q, argv, qual, action, actarg)
697 register struct query *q;
710 ## range of rvar is rtable
713 ## replace rvar (param (q->tlist, argv))
716 ## inquire_equel (errorno = "errorno")
717 if (errorno == INGRES_BAD_INT)
719 else if (errorno != 0)
720 return(SMS_INGRES_ERR);
724 do_append(q, argv, pqual, action, actarg)
725 register struct query *q;
738 ## range of rvar is rtable
742 ## append to rtable (param (q->tlist, argv)) where cqual
744 ## append to rtable (param (q->tlist, argv))
747 ## inquire_equel (errorno = "errorno")
748 if (errorno == INGRES_BAD_INT)
750 else if (errorno != 0)
751 return(SMS_INGRES_ERR);
755 do_delete(q, qual, action, actarg)
756 register struct query *q;
768 ## range of rvar is rtable
771 ## delete rvar where cqual
773 ## inquire_equel (errorno = "errorno")
774 if (errorno != 0) return(SMS_INGRES_ERR);
780 ** set_next_object_id - set next object id in values table
782 ** Inputs: object - object name in values table and in objects
783 ** table - name of table objects are found in
785 ** - called before an APPEND operation to set the next object id to
786 ** be used for the new record to the next free value
790 set_next_object_id(object, table)
795 ## int rowcount, exists, value;
799 ## range of v is values
800 ## repeat retrieve (value = v.#value) where v.#name = @name
801 ## inquire_equel(rowcount = "rowcount")
805 ## retrieve (exists = any(tbl.name where tbl.name = value))
806 ## inquire_equel(rowcount = "rowcount")
811 if (value > MAX_ID_VALUE)
812 value = MIN_ID_VALUE;
813 ## retrieve (exists = any(tbl.name where tbl.name = value))
817 com_err(whoami, 0, "setting ID %s to %d", name, value);
818 ## repeat replace v (#value = @value) where v.#name = @name
823 /* For now this just checks the argc's. It should also see that there
824 * are no duplicate names.
827 sanity_check_queries()
830 int maxv = 0, maxa = 0;
831 #ifdef MULTIPROTOCOLS
832 extern int QueryCount1, QueryCount2;
833 extern struct query Queries1[], Queries2[];
835 extern int QueryCount2;
836 extern struct query Queries2[];
837 #endif MULTIPROTOCOLS
839 #define MAX(x,y) ((x) > (y) ? (x) : (y))
841 #ifdef MULTIPROTOCOLS
842 for (i = 0; i < QueryCount1; i++) {
843 maxv = MAX(maxv, Queries1[i].vcnt);
844 maxa = MAX(maxa, Queries1[i].argc);
846 #endif MULTIPROTOCOLS
847 for (i = 0; i < QueryCount2; i++) {
848 maxv = MAX(maxv, Queries2[i].vcnt);
849 maxa = MAX(maxa, Queries2[i].argc);
851 if (MAX(maxv, maxa) > QMAXARGS) {
852 com_err(whoami, 0, "A query has more args than QMAXARGS");