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 */
75 int sms_close_database()
80 sms_check_access(cl, name, argc, argv_ro)
87 struct query *get_query_by_name();
89 q = get_query_by_name(name, cl->args->sms_version_no);
90 if (q == (struct query *)0)
91 return(SMS_NO_HANDLE);
93 return(sms_verify_query(cl, q, argc, argv_ro));
96 sms_process_query(cl, name, argc, argv_ro, action, actarg)
104 register struct query *q;
106 register struct validate *v;
112 struct save_queue *sq;
113 struct query *get_query_by_name();
115 struct save_queue *sq_create();
118 /* list queries command */
119 if (!strcmp(name, "_list_queries")) {
120 list_queries(cl->args->sms_version_no, action, actarg);
124 /* help query command */
125 if (!strcmp(name, "_help")) {
128 q = get_query_by_name(argv_ro[0], cl->args->sms_version_no);
129 if (q == (struct query *)0) return(SMS_NO_HANDLE);
130 help_query(q, action, actarg);
134 /* get query structure, return error if named query does not exist */
135 q = get_query_by_name(name, cl->args->sms_version_no);
136 if (q == (struct query *)0) return(SMS_NO_HANDLE);
139 if (q->type != RETRIEVE)
142 /* setup argument vector, verify access and arguments */
143 if ((status = sms_verify_query(cl, q, argc, argv_ro)) != SMS_SUCCESS)
146 /* perform any special query pre-processing */
147 if (v && v->pre_rtn) {
148 status = (*v->pre_rtn)(q, Argv, cl, 0);
149 if (status != SMS_SUCCESS)
155 /* for queries that do not permit wildcarding, check if row
158 status = validate_row(q, Argv, v);
159 if (status != SMS_EXISTS) break;
162 /* build "where" clause if needed */
164 build_qual(q->qual, q->argc, Argv, qual);
170 /* build "sort" clause if needed */
171 if (v && v->valobj) {
172 psort = build_sort(v, sort);
177 /* if there is a followup routine, then we must save the results */
178 /* of the first query for use by the followup routine */
179 /* if q->rvar = NULL, perform post_rtn only */
181 if (v && v->post_rtn) {
183 status = do_retrieve(q, pqual, psort, sq_save_args, sq);
184 if (status != SMS_SUCCESS) {
188 status = (*v->post_rtn)(q, sq, v, action, actarg, cl);
190 /* normal retrieve */
191 status = do_retrieve(q, pqual, psort, action, actarg);
193 if (status != SMS_SUCCESS) break;
195 ## repeat replace tblstats (retrieves = tblstats.retrieves + 1)
196 ## where tblstats.#table = @table
198 status = (*v->post_rtn)(q, Argv, cl, action, actarg);
204 /* see if row already exists */
206 status = validate_row(q, Argv, v);
207 if (status != SMS_EXISTS) break;
210 /* build "where" clause and perform update */
211 /* if q->rvar = NULL, perform post_rtn only */
213 build_qual(q->qual, q->argc, Argv, qual);
214 status = do_update(q, &Argv[q->argc], qual, action, actarg);
215 if (status != SMS_SUCCESS) break;
217 if (strcmp(q->shortname, "sshi") && strcmp(q->shortname, "ssif")) {
218 ## repeat replace tblstats (updates = tblstats.updates + 1,
220 ## where tblstats.#table = @table
224 /* execute followup routine (if any) */
225 if (v->post_rtn) status = (*v->post_rtn)(q, Argv, cl);
230 /* see if row already exists */
232 status = validate_row(q, Argv, v);
233 if (status != SMS_NO_MATCH) break;
236 /* increment id number if necessary */
238 status = set_next_object_id(v->object_id, q->rtable);
239 if (status != SMS_SUCCESS) break;
242 /* build "where" clause if needed */
244 build_qual(q->qual, q->argc, Argv, qual);
250 /* perform the append */
251 /* if q->rvar = NULL, perform post_rtn only */
253 status = do_append(q, &Argv[q->argc], pqual, action, actarg);
254 if (status != SMS_SUCCESS) break;
256 ## repeat replace tblstats (appends = tblstats.appends + 1,
258 ## where tblstats.#table = @table
261 /* execute followup routine */
262 if (v->post_rtn) status = (*v->post_rtn)(q, Argv, cl);
266 /* see if row already exists */
268 status = validate_row(q, Argv, v);
269 if (status != SMS_EXISTS) break;
272 /* build "where" clause and perform delete */
273 /* if q->rvar = NULL, perform post_rtn only */
275 build_qual(q->qual, q->argc, Argv, qual);
276 status = do_delete(q, qual, action, actarg);
277 if (status != SMS_SUCCESS) break;
279 ## repeat replace tblstats (deletes = tblstats.deletes + 1,
281 ## where tblstats.#table = @table
284 /* execute followup routine */
285 if (v->post_rtn) status = (*v->post_rtn)(q, Argv, cl);
291 if (q->type != RETRIEVE) {
292 if (status == SMS_SUCCESS) {
293 ## end transaction /* commit to this */
299 fprintf(journal, "%% %s %s %s",
300 cl->clname, cl->entity, ctime(&now));
301 fprintf(journal, "%s[%d] ", q->name, cl->args->sms_version_no);
302 for (i = 0; i < argc; i++) {
306 requote(buf, argv_ro[i], sizeof(buf));
313 ## abort /* it never happened */
317 if (status != SMS_SUCCESS && log_flags & LOG_RES)
318 com_err(whoami, status, " (Query failed)");
322 build_qual(fmt, argc, argv, qual)
334 for (i = 0; i < argc; i++) {
336 if (c++ == (char *)0) return(SMS_ARGS);
340 *(int *)&args[i] = *(int *)argv[i]; /* sigh */
342 return(SMS_INGRES_ERR);
351 sprintf(qual, fmt, args[0]);
355 sprintf(qual, fmt, args[0], args[1]);
359 sprintf(qual, fmt, args[0], args[1], args[2]);
363 sprintf(qual, fmt, args[0], args[1], args[2], args[3]);
371 register struct validate *v;
374 register struct valobj *vo;
383 if (vo->type == V_SORT) {
384 sprintf(elem, "RET_VAR%d", vo->index + 1);
385 if (*sort) strcat(sort, ", ");
391 return ((*sort) ? sort : 0);
395 /* Build arguement vector, verify query and arguments */
397 sms_verify_query(cl, q, argc, argv_ro)
405 register struct validate *v = q->validate;
407 register int privileged = 0;
409 /* copy the arguments into a local argv that we can modify */
410 for (i = 0; i < argc; i++) {
411 if (strlen(argv_ro[i]) < ARGLEN)
412 strcpy(Argv[i], argv_ro[i]);
414 return(SMS_ARG_TOO_LONG);
417 /* check initial query access */
418 status = check_query_access(q, Argv, cl);
419 if (status != SMS_SUCCESS && status != SMS_PERM)
421 if (status == SMS_SUCCESS)
424 /* check argument count */
426 if (q->type == UPDATE || q->type == APPEND) argreq += q->vcnt;
427 if (argc != argreq) return(SMS_ARGS);
429 /* validate arguments */
430 if (v && v->valobj) {
431 status = validate_fields(q, Argv, v->valobj, v->objcnt);
432 if (status != SMS_SUCCESS) return(status);
435 /* perform special query access check */
436 if (!privileged && v && v->acs_rtn) {
437 status = (*v->acs_rtn)(q, Argv, cl);
438 if (status != SMS_SUCCESS && status != SMS_PERM)
440 if (status == SMS_SUCCESS)
444 return(privileged ? SMS_SUCCESS : SMS_PERM);
447 check_query_access(q, argv, cl)
457 ## static int def_uid;
462 /* get query access control list */
464 ## repeat retrieve (acl_id = capacls.list_id) where capacls.tag = @name
465 ## inquire_equel (rowcount = "rowcount", errorno = "errorno")
466 if (errorno != 0) return(SMS_INGRES_ERR);
467 if (rowcount == 0) return(SMS_PERM);
469 /* initialize default uid */
471 ## retrieve (def_uid = users.users_id) where users.login = "default"
474 /* check for default access */
475 ## range of m is members
476 ## repeat retrieve (exists = any(m.#member_id where m.list_id = @acl_id and
477 ## m.member_type = "USER" and m.#member_id = def_uid))
478 if (exists) return(SMS_SUCCESS);
480 /* parse client name */
481 status = get_client(cl, &client_type, &client_id);
482 if (status != SMS_SUCCESS) return(status);
484 /* see if client is in the list (or any of its sub-lists) */
485 exists = find_member("LIST", acl_id, client_type, client_id, 0);
486 return ((exists) ? SMS_SUCCESS : SMS_PERM);
489 get_client(cl, client_type, client_id)
499 if (cl->clname == NULL)
502 /* for now ignore instances */
505 /* if client is from local realm, get users_id */
506 if (!strcmp(krb->realm, krb_realm)) {
507 *client_id = cl->users_id;
508 *client_type = "USER";
512 /* otherwise use string_id */
514 ## repeat retrieve (member_id = strings.string_id)
515 ## where strings.string = @name
517 /* make sure we found a users or string id */
518 ## inquire_equel (rowcount = "rowcount")
519 if (rowcount == 0) return(SMS_PERM);
521 *client_type = "STRING";
522 *client_id = member_id;
526 ##find_member(list_type, list_id, member_type, member_id, sq)
529 ## char *member_type;
531 struct save_queue *sq;
536 struct save_queue *sq_create();
538 /* see if client is a direct member of list */
539 ## repeat retrieve (exists = any(m.#member_id where
540 ## m.#list_id = @list_id and
541 ## m.#member_type = @member_type and
542 ## m.#member_id = @member_id))
543 if (exists) return(1);
545 /* are there any sub-lists? */
546 ## repeat retrieve (exists = any(m.#member_id where m.#list_id = @list_id and
547 ## m.#member_type = "LIST"))
548 if (!exists) return(0);
550 /* yes; now recurse through sublists */
552 /* create a save queue */
553 if (sq == (struct save_queue *)0) {
560 /* save all sublist ids */
561 ## range of m is members
562 ## retrieve (sublist = m.#member_id)
563 ## where m.#list_id = list_id and m.#member_type = "LIST"
565 sq_save_unique_data(sq, sublist);
568 if (child) return(0);
570 /* at top-level, check sub-lists for client (breadth-first search) */
571 while (sq_get_data(sq, &sublist)) {
572 exists = find_member(list_type, sublist, member_type, member_id, sq);
583 do_retrieve(q, pqual, psort, action, actarg)
584 register struct query *q;
596 static char **vaddrs = (char **)NULL;
601 if ((vaddrs = (char **)malloc(sizeof(char *) * QMAXARGS)) == NULL) {
602 com_err(whoami, SMS_NO_MEM, "setting up static argv");
605 for (i = 0; i < QMAXARGS; i++) {
606 if ((vaddrs[i] = malloc(QMAXARGSIZE)) == NULL) {
607 com_err(whoami, SMS_NO_MEM, "setting up static argv");
616 ## range of rvar is rtable
623 ## retrieve unique (param (q->tlist, vaddrs)) where cqual
626 (*action)(q->vcnt, vaddrs, actarg);
629 ## retrieve unique (param (q->tlist, vaddrs))
632 (*action)(q->vcnt, vaddrs, actarg);
639 ## retrieve unique (param (q->tlist, vaddrs)) where cqual
641 (*action)(q->vcnt, vaddrs, actarg);
644 ## retrieve unique (param (q->tlist, vaddrs))
646 (*action)(q->vcnt, vaddrs, actarg);
651 ## inquire_equel (rowcount = "rowcount", errorno = "errorno")
652 if (errorno != 0) return(SMS_INGRES_ERR);
653 return ((rowcount == 0) ? SMS_NO_MATCH : SMS_SUCCESS);
656 do_update(q, argv, qual, action, actarg)
657 register struct query *q;
670 ## range of rvar is rtable
673 ## replace rvar (param (q->tlist, argv))
676 ## inquire_equel (errorno = "errorno")
677 if (errorno == INGRES_BAD_INT)
679 else if (errorno != 0)
680 return(SMS_INGRES_ERR);
684 do_append(q, argv, pqual, action, actarg)
685 register struct query *q;
698 ## range of rvar is rtable
702 ## append to rtable (param (q->tlist, argv)) where cqual
704 ## append to rtable (param (q->tlist, argv))
707 ## inquire_equel (errorno = "errorno")
708 if (errorno == INGRES_BAD_INT)
710 else if (errorno != 0)
711 return(SMS_INGRES_ERR);
715 do_delete(q, qual, action, actarg)
716 register struct query *q;
728 ## range of rvar is rtable
731 ## delete rvar where cqual
733 ## inquire_equel (errorno = "errorno")
734 if (errorno != 0) return(SMS_INGRES_ERR);
740 ** set_next_object_id - set next object id in values table
742 ** Inputs: object - object name in values table and in objects
743 ** table - name of table objects are found in
745 ** - called before an APPEND operation to set the next object id to
746 ** be used for the new record to the next free value
750 set_next_object_id(object, table)
755 ## int rowcount, exists, value;
759 ## range of v is values
760 ## repeat retrieve (value = v.#value) where v.#name = @name
761 ## inquire_equel(rowcount = "rowcount")
765 ## retrieve (exists = any(tbl.name where tbl.name = value))
766 ## inquire_equel(rowcount = "rowcount")
771 if (value > MAX_ID_VALUE)
772 value = MIN_ID_VALUE;
773 ## retrieve (exists = any(tbl.name where tbl.name = value))
777 com_err(whoami, 0, "setting ID %s to %d", name, value);
778 ## repeat replace v (#value = @value) where v.#name = @name
783 /* For now this just checks the argc's. It should also see that there
784 * are no duplicate names.
787 sanity_check_queries()
790 int maxv = 0, maxa = 0;
791 extern int QueryCount1, QueryCount2;
792 extern struct query Queries1[], Queries2[];
793 #define MAX(x,y) ((x) > (y) ? (x) : (y))
795 for (i = 0; i < QueryCount1; i++) {
796 maxv = MAX(maxv, Queries1[i].vcnt);
797 maxa = MAX(maxa, Queries1[i].argc);
799 for (i = 0; i < QueryCount2; i++) {
800 maxv = MAX(maxv, Queries2[i].vcnt);
801 maxa = MAX(maxa, Queries2[i].argc);
803 if (MAX(maxv, maxa) > QMAXARGS) {
804 com_err(whoami, 0, "A query has more args than QMAXARGS");
814 * c-continued-statement-offset: 4
816 * c-argdecl-indent: 4