6 * Copyright (C) 1987, 1988 by the Massachusetts Institute of Technology
11 static char *rcsid_qrtn_qc = "$Header$";
15 #include "sms_server.h"
19 static int ingres_errno = 0;
23 #define INGRES_BAD_INT 4111
24 #define INGRES_BAD_DATE 4302
25 #define INGRES_DEADLOCK 4700
28 * ingerr: (supposedly) called when Ingres indicates an error.
29 * I have not yet been able to get this to work to intercept a
30 * database open error.
33 static int ingerr(num)
40 ingres_errno = SMS_INTEGER;
43 ingres_errno = SMS_DATE;
46 ingres_errno = SMS_DEADLOCK;
49 ingres_errno = SMS_INGRES_ERR;
50 com_err(whoami, SMS_INGRES_ERR, " code %d\n", *num);
51 critical_alert("SMS", "SMS server encountered INGRES ERROR %d", *num);
57 int sms_open_database()
62 /* initialize local argv */
63 for (i = 0; i < 16; i++)
64 Argv[i] = malloc(ARGLEN);
70 /* open the database */
72 ## set lockmode session where readlock = exclusive
76 int sms_close_database()
81 sms_check_access(cl, name, argc, argv_ro)
88 struct query *get_query_by_name();
90 q = get_query_by_name(name, cl->args->sms_version_no);
91 if (q == (struct query *)0)
92 return(SMS_NO_HANDLE);
94 return(sms_verify_query(cl, q, argc, argv_ro));
97 sms_process_query(cl, name, argc, argv_ro, action, actarg)
105 register struct query *q;
107 register struct validate *v;
113 struct save_queue *sq;
114 struct query *get_query_by_name();
116 struct save_queue *sq_create();
119 /* list queries command */
120 if (!strcmp(name, "_list_queries")) {
121 list_queries(cl->args->sms_version_no, action, actarg);
125 /* help query command */
126 if (!strcmp(name, "_help")) {
129 q = get_query_by_name(argv_ro[0], cl->args->sms_version_no);
130 if (q == (struct query *)0) return(SMS_NO_HANDLE);
131 help_query(q, action, actarg);
135 /* get query structure, return error if named query does not exist */
136 q = get_query_by_name(name, cl->args->sms_version_no);
137 if (q == (struct query *)0) return(SMS_NO_HANDLE);
140 if (q->type != RETRIEVE)
143 /* setup argument vector, verify access and arguments */
144 if ((status = sms_verify_query(cl, q, argc, argv_ro)) != SMS_SUCCESS)
147 /* perform any special query pre-processing */
148 if (v && v->pre_rtn) {
149 status = (*v->pre_rtn)(q, Argv, cl, 0);
150 if (status != SMS_SUCCESS)
156 /* for queries that do not permit wildcarding, check if row
159 status = validate_row(q, Argv, v);
160 if (status != SMS_EXISTS) break;
163 /* build "where" clause if needed */
165 build_qual(q->qual, q->argc, Argv, qual);
171 /* build "sort" clause if needed */
172 if (v && v->valobj) {
173 psort = build_sort(v, sort);
178 /* if there is a followup routine, then we must save the results */
179 /* of the first query for use by the followup routine */
180 /* if q->rvar = NULL, perform post_rtn only */
182 if (v && v->post_rtn) {
184 status = do_retrieve(q, pqual, psort, sq_save_args, sq);
185 if (status != SMS_SUCCESS) {
189 status = (*v->post_rtn)(q, sq, v, action, actarg, cl);
191 /* normal retrieve */
192 status = do_retrieve(q, pqual, psort, action, actarg);
194 if (status != SMS_SUCCESS) break;
196 status = (*v->post_rtn)(q, Argv, cl, action, actarg);
202 /* see if row already exists */
204 status = validate_row(q, Argv, v);
205 if (status != SMS_EXISTS) break;
208 /* build "where" clause and perform update */
209 /* if q->rvar = NULL, perform post_rtn only */
211 build_qual(q->qual, q->argc, Argv, qual);
212 status = do_update(q, &Argv[q->argc], qual, action, actarg);
213 if (status != SMS_SUCCESS) break;
215 if (strcmp(q->shortname, "sshi") && strcmp(q->shortname, "ssif")) {
216 ## repeat replace tblstats (updates = tblstats.updates + 1,
218 ## where tblstats.#table = @table
222 /* execute followup routine (if any) */
223 if (v->post_rtn) status = (*v->post_rtn)(q, Argv, cl);
228 /* see if row already exists */
230 status = validate_row(q, Argv, v);
231 if (status != SMS_NO_MATCH) break;
234 /* increment id number if necessary */
236 status = set_next_object_id(v->object_id, q->rtable);
237 if (status != SMS_SUCCESS) break;
240 /* build "where" clause if needed */
242 build_qual(q->qual, q->argc, Argv, qual);
248 /* perform the append */
249 /* if q->rvar = NULL, perform post_rtn only */
251 status = do_append(q, &Argv[q->argc], pqual, action, actarg);
252 if (status != SMS_SUCCESS) break;
254 ## repeat replace tblstats (appends = tblstats.appends + 1,
256 ## where tblstats.#table = @table
259 /* execute followup routine */
260 if (v->post_rtn) status = (*v->post_rtn)(q, Argv, cl);
264 /* see if row already exists */
266 status = validate_row(q, Argv, v);
267 if (status != SMS_EXISTS) break;
270 /* build "where" clause and perform delete */
271 /* if q->rvar = NULL, perform post_rtn only */
273 build_qual(q->qual, q->argc, Argv, qual);
274 status = do_delete(q, qual, action, actarg);
275 if (status != SMS_SUCCESS) break;
277 ## repeat replace tblstats (deletes = tblstats.deletes + 1,
279 ## where tblstats.#table = @table
282 /* execute followup routine */
283 if (v->post_rtn) status = (*v->post_rtn)(q, Argv, cl);
289 if (q->type != RETRIEVE) {
290 if (status == SMS_SUCCESS) {
291 ## end transaction /* commit to this */
297 fprintf(journal, "%% %s %s %s",
298 cl->clname, cl->entity, ctime(&now));
299 fprintf(journal, "%s[%d] ", q->name, cl->args->sms_version_no);
300 for (i = 0; i < argc; i++) {
304 requote(buf, argv_ro[i], sizeof(buf));
311 ## abort /* it never happened */
315 if (status != SMS_SUCCESS && log_flags & LOG_RES)
316 com_err(whoami, status, " (Query failed)");
320 build_qual(fmt, argc, argv, qual)
332 for (i = 0; i < argc; i++) {
334 if (c++ == (char *)0) return(SMS_ARGS);
338 *(int *)&args[i] = *(int *)argv[i]; /* sigh */
340 return(SMS_INGRES_ERR);
349 sprintf(qual, fmt, args[0]);
353 sprintf(qual, fmt, args[0], args[1]);
357 sprintf(qual, fmt, args[0], args[1], args[2]);
361 sprintf(qual, fmt, args[0], args[1], args[2], args[3]);
369 register struct validate *v;
372 register struct valobj *vo;
381 if (vo->type == V_SORT) {
382 sprintf(elem, "RET_VAR%d", vo->index + 1);
383 if (*sort) strcat(sort, ", ");
389 return ((*sort) ? sort : 0);
393 /* Build arguement vector, verify query and arguments */
395 sms_verify_query(cl, q, argc, argv_ro)
403 register struct validate *v = q->validate;
405 register int privileged = 0;
407 /* copy the arguments into a local argv that we can modify */
408 for (i = 0; i < argc; i++) {
409 if (strlen(argv_ro[i]) < ARGLEN)
410 strcpy(Argv[i], argv_ro[i]);
412 return(SMS_ARG_TOO_LONG);
415 /* check initial query access */
416 status = check_query_access(q, Argv, cl);
417 if (status != SMS_SUCCESS && status != SMS_PERM)
419 if (status == SMS_SUCCESS)
422 /* check argument count */
424 if (q->type == UPDATE || q->type == APPEND) argreq += q->vcnt;
425 if (argc != argreq) return(SMS_ARGS);
427 /* validate arguments */
428 if (v && v->valobj) {
429 status = validate_fields(q, Argv, v->valobj, v->objcnt);
430 if (status != SMS_SUCCESS) return(status);
433 /* perform special query access check */
434 if (!privileged && v && v->acs_rtn) {
435 status = (*v->acs_rtn)(q, Argv, cl);
436 if (status != SMS_SUCCESS && status != SMS_PERM)
438 if (status == SMS_SUCCESS)
442 return(privileged ? SMS_SUCCESS : SMS_PERM);
445 check_query_access(q, argv, cl)
455 ## static int def_uid;
460 /* get query access control list */
462 ## repeat retrieve (acl_id = capacls.list_id) where capacls.tag = @name
463 ## inquire_equel (rowcount = "rowcount", errorno = "errorno")
464 if (errorno != 0) return(SMS_INGRES_ERR);
465 if (rowcount == 0) return(SMS_PERM);
467 /* initialize default uid */
469 ## retrieve (def_uid = users.users_id) where users.login = "default"
472 /* check for default access */
473 ## range of m is members
474 ## repeat retrieve (exists = any(m.#member_id where m.list_id = @acl_id and
475 ## m.member_type = "USER" and m.#member_id = def_uid))
476 if (exists) return(SMS_SUCCESS);
478 /* parse client name */
479 status = get_client(cl, &client_type, &client_id);
480 if (status != SMS_SUCCESS) return(status);
482 /* see if client is in the list (or any of its sub-lists) */
483 exists = find_member("LIST", acl_id, client_type, client_id, 0);
484 return ((exists) ? SMS_SUCCESS : SMS_PERM);
487 get_client(cl, client_type, client_id)
497 if (cl->clname == NULL)
500 /* for now ignore instances */
503 /* if client is from local realm, get users_id */
504 if (!strcmp(krb->realm, krb_realm)) {
505 *client_id = cl->users_id;
506 *client_type = "USER";
510 /* otherwise use string_id */
512 ## repeat retrieve (member_id = strings.string_id)
513 ## where strings.string = @name
515 /* make sure we found a users or string id */
516 ## inquire_equel (rowcount = "rowcount")
517 if (rowcount == 0) return(SMS_PERM);
519 *client_type = "STRING";
520 *client_id = member_id;
524 ##find_member(list_type, list_id, member_type, member_id, sq)
527 ## char *member_type;
529 struct save_queue *sq;
534 struct save_queue *sq_create();
536 if (!strcmp(strtrim(list_type), strtrim(member_type)) &&
537 list_id == member_id)
540 /* see if client is a direct member of list */
541 ## repeat retrieve (exists = any(m.#member_id where
542 ## m.#list_id = @list_id and
543 ## m.#member_type = @member_type and
544 ## m.#member_id = @member_id))
545 if (exists) return(1);
547 /* are there any sub-lists? */
548 ## repeat retrieve (exists = any(m.#member_id where m.#list_id = @list_id and
549 ## m.#member_type = "LIST"))
550 if (!exists) return(0);
552 /* yes; now recurse through sublists */
554 /* create a save queue */
555 if (sq == (struct save_queue *)0) {
562 /* save all sublist ids */
563 ## range of m is members
564 ## retrieve (sublist = m.#member_id)
565 ## where m.#list_id = list_id and m.#member_type = "LIST"
567 sq_save_unique_data(sq, sublist);
570 if (child) return(0);
572 /* at top-level, check sub-lists for client (breadth-first search) */
573 while (sq_get_data(sq, &sublist)) {
574 exists = find_member(list_type, sublist, member_type, member_id, sq);
585 do_retrieve(q, pqual, psort, action, actarg)
586 register struct query *q;
598 static char **vaddrs = (char **)NULL;
603 if ((vaddrs = (char **)malloc(sizeof(char *) * QMAXARGS)) == NULL) {
604 com_err(whoami, SMS_NO_MEM, "setting up static argv");
607 for (i = 0; i < QMAXARGS; i++) {
608 if ((vaddrs[i] = malloc(QMAXARGSIZE)) == NULL) {
609 com_err(whoami, SMS_NO_MEM, "setting up static argv");
618 ## range of rvar is rtable
625 ## retrieve unique (param (q->tlist, vaddrs)) where cqual
628 (*action)(q->vcnt, vaddrs, actarg);
631 ## retrieve unique (param (q->tlist, vaddrs))
634 (*action)(q->vcnt, vaddrs, actarg);
641 ## retrieve unique (param (q->tlist, vaddrs)) where cqual
643 (*action)(q->vcnt, vaddrs, actarg);
646 ## retrieve unique (param (q->tlist, vaddrs))
648 (*action)(q->vcnt, vaddrs, actarg);
653 ## inquire_equel (rowcount = "rowcount", errorno = "errorno")
654 if (errorno != 0) return(SMS_INGRES_ERR);
655 return ((rowcount == 0) ? SMS_NO_MATCH : SMS_SUCCESS);
658 do_update(q, argv, qual, action, actarg)
659 register struct query *q;
672 ## range of rvar is rtable
675 ## replace rvar (param (q->tlist, argv))
678 ## inquire_equel (errorno = "errorno")
679 if (errorno == INGRES_BAD_INT)
681 else if (errorno != 0)
682 return(SMS_INGRES_ERR);
686 do_append(q, argv, pqual, action, actarg)
687 register struct query *q;
700 ## range of rvar is rtable
704 ## append to rtable (param (q->tlist, argv)) where cqual
706 ## append to rtable (param (q->tlist, argv))
709 ## inquire_equel (errorno = "errorno")
710 if (errorno == INGRES_BAD_INT)
712 else if (errorno != 0)
713 return(SMS_INGRES_ERR);
717 do_delete(q, qual, action, actarg)
718 register struct query *q;
730 ## range of rvar is rtable
733 ## delete rvar where cqual
735 ## inquire_equel (errorno = "errorno")
736 if (errorno != 0) return(SMS_INGRES_ERR);
742 ** set_next_object_id - set next object id in values table
744 ** Inputs: object - object name in values table and in objects
745 ** table - name of table objects are found in
747 ** - called before an APPEND operation to set the next object id to
748 ** be used for the new record to the next free value
752 set_next_object_id(object, table)
757 ## int rowcount, exists, value;
761 ## range of v is values
762 ## repeat retrieve (value = v.#value) where v.#name = @name
763 ## inquire_equel(rowcount = "rowcount")
767 ## retrieve (exists = any(tbl.name where tbl.name = value))
768 ## inquire_equel(rowcount = "rowcount")
773 if (value > MAX_ID_VALUE)
774 value = MIN_ID_VALUE;
775 ## retrieve (exists = any(tbl.name where tbl.name = value))
779 com_err(whoami, 0, "setting ID %s to %d", name, value);
780 ## repeat replace v (#value = @value) where v.#name = @name
785 /* For now this just checks the argc's. It should also see that there
786 * are no duplicate names.
789 sanity_check_queries()
792 int maxv = 0, maxa = 0;
793 extern int QueryCount1, QueryCount2;
794 extern struct query Queries1[], Queries2[];
795 #define MAX(x,y) ((x) > (y) ? (x) : (y))
797 for (i = 0; i < QueryCount1; i++) {
798 maxv = MAX(maxv, Queries1[i].vcnt);
799 maxa = MAX(maxa, Queries1[i].argc);
801 for (i = 0; i < QueryCount2; i++) {
802 maxv = MAX(maxv, Queries2[i].vcnt);
803 maxa = MAX(maxa, Queries2[i].argc);
805 if (MAX(maxv, maxa) > QMAXARGS) {
806 com_err(whoami, 0, "A query has more args than QMAXARGS");
816 * c-continued-statement-offset: 4
818 * c-argdecl-indent: 4