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;
24 * ingerr: (supposedly) called when Ingres indicates an error.
25 * I have not yet been able to get this to work to intercept a
26 * database open error.
29 static int ingerr(num)
32 ingres_errno = SMS_INGRES_ERR;
33 com_err(whoami, SMS_INGRES_ERR, " code %d\n", ingres_errno);
37 int sms_open_database()
42 /* initialize local argv */
43 for (i = 0; i < 16; i++)
44 Argv[i] = malloc(ARGLEN);
50 /* open the database */
55 int sms_close_database()
60 sms_check_access(cl, name, argc, argv_ro)
67 struct query *get_query_by_name();
69 q = get_query_by_name(name, cl->args->sms_version_no);
70 if (q == (struct query *)0)
71 return(SMS_NO_HANDLE);
73 return(sms_verify_query(cl, q, argc, argv_ro));
76 sms_process_query(cl, name, argc, argv_ro, action, actarg)
84 register struct query *q;
86 register struct validate *v;
92 struct save_queue *sq;
93 struct query *get_query_by_name();
95 struct save_queue *sq_create();
98 /* list queries command */
99 if (!strcmp(name, "_list_queries")) {
100 list_queries(cl->args->sms_version_no, action, actarg);
104 /* help query command */
105 if (!strcmp(name, "_help")) {
108 q = get_query_by_name(argv_ro[0], cl->args->sms_version_no);
109 if (q == (struct query *)0) return(SMS_NO_HANDLE);
110 help_query(q, action, actarg);
114 /* get query structure, return error if named query does not exist */
115 q = get_query_by_name(name, cl->args->sms_version_no);
116 if (q == (struct query *)0) return(SMS_NO_HANDLE);
119 if (q->type != RETRIEVE)
122 /* setup argument vector, verify access and arguments */
123 if ((status = sms_verify_query(cl, q, argc, argv_ro)) != SMS_SUCCESS)
126 /* perform any special query pre-processing */
127 if (v && v->pre_rtn) {
128 status = (*v->pre_rtn)(q, Argv, cl, 0);
129 if (status != SMS_SUCCESS)
135 /* for queries that do not permit wildcarding, check if row
138 status = validate_row(q, Argv, v);
139 if (status != SMS_EXISTS) break;
142 /* build "where" clause if needed */
144 build_qual(q->qual, q->argc, Argv, qual);
150 /* build "sort" clause if needed */
151 if (v && v->valobj) {
152 psort = build_sort(v, sort);
157 /* if there is a followup routine, then we must save the results */
158 /* of the first query for use by the followup routine */
159 /* if q->rvar = NULL, perform post_rtn only */
161 if (v && v->post_rtn) {
163 status = do_retrieve(q, pqual, psort, sq_save_args, sq);
164 if (status != SMS_SUCCESS) {
168 status = (*v->post_rtn)(q, sq, v, action, actarg, cl);
170 /* normal retrieve */
171 status = do_retrieve(q, pqual, psort, action, actarg);
173 if (status != SMS_SUCCESS) break;
175 ## repeat replace tblstats (retrieves = tblstats.retrieves + 1)
176 ## where tblstats.#table = @table
178 status = (*v->post_rtn)(q, Argv, cl, action, actarg);
184 /* see if row already exists */
186 status = validate_row(q, Argv, v);
187 if (status != SMS_EXISTS) break;
190 /* build "where" clause and perform update */
191 /* if q->rvar = NULL, perform post_rtn only */
193 build_qual(q->qual, q->argc, Argv, qual);
194 status = do_update(q, &Argv[q->argc], qual, action, actarg);
195 if (status != SMS_SUCCESS) break;
197 ## repeat replace tblstats (updates = tblstats.updates + 1,
199 ## where tblstats.#table = @table
202 /* execute followup routine (if any) */
203 if (v->post_rtn) status = (*v->post_rtn)(q, Argv, cl);
208 /* see if row already exists */
210 status = validate_row(q, Argv, v);
211 if (status != SMS_NO_MATCH) break;
214 /* increment id number if necessary */
216 status = set_next_object_id(v->object_id, q->rtable);
217 if (status != SMS_SUCCESS) break;
220 /* build "where" clause if needed */
222 build_qual(q->qual, q->argc, Argv, qual);
228 /* perform the append */
229 /* if q->rvar = NULL, perform post_rtn only */
231 status = do_append(q, &Argv[q->argc], pqual, action, actarg);
232 if (status != SMS_SUCCESS) break;
234 ## repeat replace tblstats (appends = tblstats.appends + 1,
236 ## where tblstats.#table = @table
239 /* execute followup routine */
240 if (v->post_rtn) status = (*v->post_rtn)(q, Argv, cl);
244 /* see if row already exists */
246 status = validate_row(q, Argv, v);
247 if (status != SMS_EXISTS) break;
250 /* build "where" clause and perform delete */
251 /* if q->rvar = NULL, perform post_rtn only */
253 build_qual(q->qual, q->argc, Argv, qual);
254 status = do_delete(q, qual, action, actarg);
255 if (status != SMS_SUCCESS) break;
257 ## repeat replace tblstats (deletes = tblstats.deletes + 1,
259 ## where tblstats.#table = @table
262 /* execute followup routine */
263 if (v->post_rtn) status = (*v->post_rtn)(q, Argv, cl);
269 if (q->type != RETRIEVE) {
270 if (status == SMS_SUCCESS) {
271 ## end transaction /* commit to this */
277 fprintf(journal, "%% %s %s %s",
278 cl->clname, cl->entity, ctime(&now));
279 fprintf(journal, "%s[%d] ", q->name, cl->args->sms_version_no);
280 for (i = 0; i < argc; i++) {
284 requote(buf, argv_ro[i], sizeof(buf));
291 ## abort /* it never happened */
295 if (status != SMS_SUCCESS && log_flags & LOG_RES)
296 com_err(whoami, status, " (Query failed)");
300 build_qual(fmt, argc, argv, qual)
312 for (i = 0; i < argc; i++) {
314 if (c++ == (char *)0) return(SMS_ARGS);
318 *(int *)&args[i] = *(int *)argv[i]; /* sigh */
320 return(SMS_INGRES_ERR);
329 sprintf(qual, fmt, args[0]);
333 sprintf(qual, fmt, args[0], args[1]);
337 sprintf(qual, fmt, args[0], args[1], args[2]);
341 sprintf(qual, fmt, args[0], args[1], args[2], args[3]);
349 register struct validate *v;
352 register struct valobj *vo;
361 if (vo->type == V_SORT) {
362 sprintf(elem, "RET_VAR%d", vo->index + 1);
363 if (*sort) strcat(sort, ", ");
369 return ((*sort) ? sort : 0);
373 /* Build arguement vector, verify query and arguments */
375 sms_verify_query(cl, q, argc, argv_ro)
383 register struct validate *v = q->validate;
385 register int privileged = 0;
387 /* copy the arguments into a local argv that we can modify */
388 for (i = 0; i < argc; i++) {
389 if (strlen(argv_ro[i]) < ARGLEN)
390 strcpy(Argv[i], argv_ro[i]);
392 return(SMS_ARG_TOO_LONG);
395 /* check initial query access */
396 status = check_query_access(q, Argv, cl);
397 if (status != SMS_SUCCESS && status != SMS_PERM)
399 if (status == SMS_SUCCESS)
402 /* check argument count */
404 if (q->type == UPDATE || q->type == APPEND) argreq += q->vcnt;
405 if (argc != argreq) return(SMS_ARGS);
407 /* validate arguments */
408 if (v && v->valobj) {
409 status = validate_fields(q, Argv, v->valobj, v->objcnt);
410 if (status != SMS_SUCCESS) return(status);
413 /* perform special query access check */
414 if (!privileged && v && v->acs_rtn) {
415 status = (*v->acs_rtn)(q, Argv, cl);
416 if (status != SMS_SUCCESS && status != SMS_PERM)
418 if (status == SMS_SUCCESS)
422 return(privileged ? SMS_SUCCESS : SMS_PERM);
425 check_query_access(q, argv, cl)
435 ## static int def_uid;
440 /* get query access control list */
442 ## repeat retrieve (acl_id = capacls.list_id) where capacls.tag = @name
443 ## inquire_equel (rowcount = "rowcount", errorno = "errorno")
444 if (errorno != 0) return(SMS_INGRES_ERR);
445 if (rowcount == 0) return(SMS_PERM);
447 /* initialize default uid */
449 ## retrieve (def_uid = users.users_id) where users.login = "default"
452 /* check for default access */
453 ## range of m is members
454 ## repeat retrieve (exists = any(m.#member_id where m.list_id = @acl_id and
455 ## m.member_type = "USER" and m.#member_id = def_uid))
456 if (exists) return(SMS_SUCCESS);
458 /* parse client name */
459 status = get_client(cl, &client_type, &client_id);
460 if (status != SMS_SUCCESS) return(status);
462 /* see if client is in the list (or any of its sub-lists) */
463 exists = find_member("LIST", acl_id, client_type, client_id, 0);
464 return ((exists) ? SMS_SUCCESS : SMS_PERM);
467 get_client(cl, client_type, client_id)
477 if (cl->clname == NULL)
480 /* for now ignore instances */
483 /* if client is from local realm, get users_id */
484 if (!strcmp(krb->realm, krb_realm)) {
485 *client_id = cl->users_id;
486 *client_type = "USER";
490 /* otherwise use string_id */
492 ## repeat retrieve (member_id = strings.string_id)
493 ## where strings.string = @name
495 /* make sure we found a users or string id */
496 ## inquire_equel (rowcount = "rowcount")
497 if (rowcount == 0) return(SMS_PERM);
499 *client_type = "STRING";
500 *client_id = member_id;
504 ##find_member(list_type, list_id, member_type, member_id, sq)
507 ## char *member_type;
509 struct save_queue *sq;
514 struct save_queue *sq_create();
516 /* see if client is a direct member of list */
517 ## repeat retrieve (exists = any(m.#member_id where
518 ## m.#list_id = @list_id and
519 ## m.#member_type = @member_type and
520 ## m.#member_id = @member_id))
521 if (exists) return(1);
523 /* are there any sub-lists? */
524 ## repeat retrieve (exists = any(m.#member_id where m.#list_id = @list_id and
525 ## m.#member_type = "LIST"))
526 if (!exists) return(0);
528 /* yes; now recurse through sublists */
530 /* create a save queue */
531 if (sq == (struct save_queue *)0) {
538 /* save all sublist ids */
539 ## range of m is members
540 ## retrieve (sublist = m.#member_id)
541 ## where m.#list_id = list_id and m.#member_type = "LIST"
543 sq_save_unique_data(sq, sublist);
546 if (child) return(0);
548 /* at top-level, check sub-lists for client (breadth-first search) */
549 while (sq_get_data(sq, &sublist)) {
550 exists = find_member(list_type, sublist, member_type, member_id, sq);
561 do_retrieve(q, pqual, psort, action, actarg)
562 register struct query *q;
574 static char **vaddrs = (char **)NULL;
579 if ((vaddrs = (char **)malloc(sizeof(char *) * QMAXARGS)) == NULL) {
580 com_err(whoami, SMS_NO_MEM, "setting up static argv");
583 for (i = 0; i < QMAXARGS; i++) {
584 if ((vaddrs[i] = malloc(QMAXARGSIZE)) == NULL) {
585 com_err(whoami, SMS_NO_MEM, "setting up static argv");
594 ## range of rvar is rtable
601 ## retrieve unique (param (q->tlist, vaddrs)) where cqual
604 (*action)(q->vcnt, vaddrs, actarg);
607 ## retrieve unique (param (q->tlist, vaddrs))
610 (*action)(q->vcnt, vaddrs, actarg);
617 ## retrieve unique (param (q->tlist, vaddrs)) where cqual
619 (*action)(q->vcnt, vaddrs, actarg);
622 ## retrieve unique (param (q->tlist, vaddrs))
624 (*action)(q->vcnt, vaddrs, actarg);
629 ## inquire_equel (rowcount = "rowcount", errorno = "errorno")
630 if (errorno != 0) return(SMS_INGRES_ERR);
631 return ((rowcount == 0) ? SMS_NO_MATCH : SMS_SUCCESS);
634 do_update(q, argv, qual, action, actarg)
635 register struct query *q;
648 ## range of rvar is rtable
651 ## replace rvar (param (q->tlist, argv))
654 ## inquire_equel (errorno = "errorno")
655 if (errorno != 0) return(SMS_INGRES_ERR);
659 do_append(q, argv, pqual, action, actarg)
660 register struct query *q;
673 ## range of rvar is rtable
677 ## append to rtable (param (q->tlist, argv)) where cqual
679 ## append to rtable (param (q->tlist, argv))
682 ## inquire_equel (errorno = "errorno")
683 if (errorno != 0) return(SMS_INGRES_ERR);
687 do_delete(q, qual, action, actarg)
688 register struct query *q;
700 ## range of rvar is rtable
703 ## delete rvar where cqual
705 ## inquire_equel (errorno = "errorno")
706 if (errorno != 0) return(SMS_INGRES_ERR);
712 ** set_next_object_id - set next object id in values table
714 ** Inputs: object - object name in values table and in objects
715 ** table - name of table objects are found in
717 ** - called before an APPEND operation to set the next object id to
718 ** be used for the new record to the next free value
722 set_next_object_id(object, table)
727 ## int rowcount, exists, value;
731 ## range of v is values
732 ## repeat retrieve (value = v.#value) where v.#name = @name
733 ## inquire_equel(rowcount = "rowcount")
735 return(SMS_INGRES_ERR);
737 ## retrieve (exists = any(tbl.name where tbl.name = value))
738 ## inquire_equel(rowcount = "rowcount")
740 return(SMS_INGRES_ERR);
743 ## retrieve (exists = any(tbl.name where tbl.name = value))
747 com_err(whoami, 0, "setting ID %s to %d", name, value);
748 ## repeat replace v (#value = @value) where v.#name = @name
753 /* For now this just checks the argc's. It should also see that there
754 * are no duplicate names.
757 sanity_check_queries()
760 int maxv = 0, maxa = 0;
761 extern int QueryCount1, QueryCount2;
762 extern struct query Queries1[], Queries2[];
763 #define MAX(x,y) ((x) > (y) ? (x) : (y))
765 for (i = 0; i < QueryCount1; i++) {
766 maxv = MAX(maxv, Queries1[i].vcnt);
767 maxa = MAX(maxa, Queries1[i].argc);
769 for (i = 0; i < QueryCount2; i++) {
770 maxv = MAX(maxv, Queries2[i].vcnt);
771 maxa = MAX(maxa, Queries2[i].argc);
773 if (MAX(maxv, maxa) > QMAXARGS) {
774 com_err(whoami, 0, "A query has more args than QMAXARGS");
784 * c-continued-statement-offset: 4
786 * c-argdecl-indent: 4