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 status = (*v->post_rtn)(q, Argv, cl, action, actarg);
201 /* see if row already exists */
203 status = validate_row(q, Argv, v);
204 if (status != SMS_EXISTS) break;
207 /* build "where" clause and perform update */
208 /* if q->rvar = NULL, perform post_rtn only */
210 build_qual(q->qual, q->argc, Argv, qual);
211 status = do_update(q, &Argv[q->argc], qual, action, actarg);
212 if (status != SMS_SUCCESS) break;
214 if (strcmp(q->shortname, "sshi") && strcmp(q->shortname, "ssif")) {
215 ## repeat replace tblstats (updates = tblstats.updates + 1,
217 ## where tblstats.#table = @table
221 /* execute followup routine (if any) */
222 if (v->post_rtn) status = (*v->post_rtn)(q, Argv, cl);
227 /* see if row already exists */
229 status = validate_row(q, Argv, v);
230 if (status != SMS_NO_MATCH) break;
233 /* increment id number if necessary */
235 status = set_next_object_id(v->object_id, q->rtable);
236 if (status != SMS_SUCCESS) break;
239 /* build "where" clause if needed */
241 build_qual(q->qual, q->argc, Argv, qual);
247 /* perform the append */
248 /* if q->rvar = NULL, perform post_rtn only */
250 status = do_append(q, &Argv[q->argc], pqual, action, actarg);
251 if (status != SMS_SUCCESS) break;
253 ## repeat replace tblstats (appends = tblstats.appends + 1,
255 ## where tblstats.#table = @table
258 /* execute followup routine */
259 if (v->post_rtn) status = (*v->post_rtn)(q, Argv, cl);
263 /* see if row already exists */
265 status = validate_row(q, Argv, v);
266 if (status != SMS_EXISTS) break;
269 /* build "where" clause and perform delete */
270 /* if q->rvar = NULL, perform post_rtn only */
272 build_qual(q->qual, q->argc, Argv, qual);
273 status = do_delete(q, qual, action, actarg);
274 if (status != SMS_SUCCESS) break;
276 ## repeat replace tblstats (deletes = tblstats.deletes + 1,
278 ## where tblstats.#table = @table
281 /* execute followup routine */
282 if (v->post_rtn) status = (*v->post_rtn)(q, Argv, cl);
288 if (q->type != RETRIEVE) {
289 if (status == SMS_SUCCESS) {
290 ## end transaction /* commit to this */
296 fprintf(journal, "%% %s %s %s",
297 cl->clname, cl->entity, ctime(&now));
298 fprintf(journal, "%s[%d] ", q->name, cl->args->sms_version_no);
299 for (i = 0; i < argc; i++) {
303 requote(buf, argv_ro[i], sizeof(buf));
310 ## abort /* it never happened */
314 if (status != SMS_SUCCESS && log_flags & LOG_RES)
315 com_err(whoami, status, " (Query failed)");
319 build_qual(fmt, argc, argv, qual)
331 for (i = 0; i < argc; i++) {
333 if (c++ == (char *)0) return(SMS_ARGS);
337 *(int *)&args[i] = *(int *)argv[i]; /* sigh */
339 return(SMS_INGRES_ERR);
348 sprintf(qual, fmt, args[0]);
352 sprintf(qual, fmt, args[0], args[1]);
356 sprintf(qual, fmt, args[0], args[1], args[2]);
360 sprintf(qual, fmt, args[0], args[1], args[2], args[3]);
368 register struct validate *v;
371 register struct valobj *vo;
380 if (vo->type == V_SORT) {
381 sprintf(elem, "RET_VAR%d", vo->index + 1);
382 if (*sort) strcat(sort, ", ");
388 return ((*sort) ? sort : 0);
392 /* Build arguement vector, verify query and arguments */
394 sms_verify_query(cl, q, argc, argv_ro)
402 register struct validate *v = q->validate;
404 register int privileged = 0;
406 /* copy the arguments into a local argv that we can modify */
407 for (i = 0; i < argc; i++) {
408 if (strlen(argv_ro[i]) < ARGLEN)
409 strcpy(Argv[i], argv_ro[i]);
411 return(SMS_ARG_TOO_LONG);
414 /* check initial query access */
415 status = check_query_access(q, Argv, cl);
416 if (status != SMS_SUCCESS && status != SMS_PERM)
418 if (status == SMS_SUCCESS)
421 /* check argument count */
423 if (q->type == UPDATE || q->type == APPEND) argreq += q->vcnt;
424 if (argc != argreq) return(SMS_ARGS);
426 /* validate arguments */
427 if (v && v->valobj) {
428 status = validate_fields(q, Argv, v->valobj, v->objcnt);
429 if (status != SMS_SUCCESS) return(status);
432 /* perform special query access check */
433 if (!privileged && v && v->acs_rtn) {
434 status = (*v->acs_rtn)(q, Argv, cl);
435 if (status != SMS_SUCCESS && status != SMS_PERM)
437 if (status == SMS_SUCCESS)
441 return(privileged ? SMS_SUCCESS : SMS_PERM);
444 check_query_access(q, argv, cl)
454 ## static int def_uid;
459 /* get query access control list */
461 ## repeat retrieve (acl_id = capacls.list_id) where capacls.tag = @name
462 ## inquire_equel (rowcount = "rowcount", errorno = "errorno")
463 if (errorno != 0) return(SMS_INGRES_ERR);
464 if (rowcount == 0) return(SMS_PERM);
466 /* initialize default uid */
468 ## retrieve (def_uid = users.users_id) where users.login = "default"
471 /* check for default access */
472 ## range of m is members
473 ## repeat retrieve (exists = any(m.#member_id where m.list_id = @acl_id and
474 ## m.member_type = "USER" and m.#member_id = def_uid))
475 if (exists) return(SMS_SUCCESS);
477 /* parse client name */
478 status = get_client(cl, &client_type, &client_id);
479 if (status != SMS_SUCCESS) return(status);
481 /* see if client is in the list (or any of its sub-lists) */
482 exists = find_member("LIST", acl_id, client_type, client_id, 0);
483 return ((exists) ? SMS_SUCCESS : SMS_PERM);
486 get_client(cl, client_type, client_id)
496 if (cl->clname == NULL)
499 /* for now ignore instances */
502 /* if client is from local realm, get users_id */
503 if (!strcmp(krb->realm, krb_realm)) {
504 *client_id = cl->users_id;
505 *client_type = "USER";
509 /* otherwise use string_id */
511 ## repeat retrieve (member_id = strings.string_id)
512 ## where strings.string = @name
514 /* make sure we found a users or string id */
515 ## inquire_equel (rowcount = "rowcount")
516 if (rowcount == 0) return(SMS_PERM);
518 *client_type = "STRING";
519 *client_id = member_id;
523 ##find_member(list_type, list_id, member_type, member_id, sq)
526 ## char *member_type;
528 struct save_queue *sq;
533 struct save_queue *sq_create();
535 /* see if client is a direct member of list */
536 ## repeat retrieve (exists = any(m.#member_id where
537 ## m.#list_id = @list_id and
538 ## m.#member_type = @member_type and
539 ## m.#member_id = @member_id))
540 if (exists) return(1);
542 /* are there any sub-lists? */
543 ## repeat retrieve (exists = any(m.#member_id where m.#list_id = @list_id and
544 ## m.#member_type = "LIST"))
545 if (!exists) return(0);
547 /* yes; now recurse through sublists */
549 /* create a save queue */
550 if (sq == (struct save_queue *)0) {
557 /* save all sublist ids */
558 ## range of m is members
559 ## retrieve (sublist = m.#member_id)
560 ## where m.#list_id = list_id and m.#member_type = "LIST"
562 sq_save_unique_data(sq, sublist);
565 if (child) return(0);
567 /* at top-level, check sub-lists for client (breadth-first search) */
568 while (sq_get_data(sq, &sublist)) {
569 exists = find_member(list_type, sublist, member_type, member_id, sq);
580 do_retrieve(q, pqual, psort, action, actarg)
581 register struct query *q;
593 static char **vaddrs = (char **)NULL;
598 if ((vaddrs = (char **)malloc(sizeof(char *) * QMAXARGS)) == NULL) {
599 com_err(whoami, SMS_NO_MEM, "setting up static argv");
602 for (i = 0; i < QMAXARGS; i++) {
603 if ((vaddrs[i] = malloc(QMAXARGSIZE)) == NULL) {
604 com_err(whoami, SMS_NO_MEM, "setting up static argv");
613 ## range of rvar is rtable
620 ## retrieve unique (param (q->tlist, vaddrs)) where cqual
623 (*action)(q->vcnt, vaddrs, actarg);
626 ## retrieve unique (param (q->tlist, vaddrs))
629 (*action)(q->vcnt, vaddrs, actarg);
636 ## retrieve unique (param (q->tlist, vaddrs)) where cqual
638 (*action)(q->vcnt, vaddrs, actarg);
641 ## retrieve unique (param (q->tlist, vaddrs))
643 (*action)(q->vcnt, vaddrs, actarg);
648 ## inquire_equel (rowcount = "rowcount", errorno = "errorno")
649 if (errorno != 0) return(SMS_INGRES_ERR);
650 return ((rowcount == 0) ? SMS_NO_MATCH : SMS_SUCCESS);
653 do_update(q, argv, qual, action, actarg)
654 register struct query *q;
667 ## range of rvar is rtable
670 ## replace rvar (param (q->tlist, argv))
673 ## inquire_equel (errorno = "errorno")
674 if (errorno == INGRES_BAD_INT)
676 else if (errorno != 0)
677 return(SMS_INGRES_ERR);
681 do_append(q, argv, pqual, action, actarg)
682 register struct query *q;
695 ## range of rvar is rtable
699 ## append to rtable (param (q->tlist, argv)) where cqual
701 ## append to rtable (param (q->tlist, argv))
704 ## inquire_equel (errorno = "errorno")
705 if (errorno == INGRES_BAD_INT)
707 else if (errorno != 0)
708 return(SMS_INGRES_ERR);
712 do_delete(q, qual, action, actarg)
713 register struct query *q;
725 ## range of rvar is rtable
728 ## delete rvar where cqual
730 ## inquire_equel (errorno = "errorno")
731 if (errorno != 0) return(SMS_INGRES_ERR);
737 ** set_next_object_id - set next object id in values table
739 ** Inputs: object - object name in values table and in objects
740 ** table - name of table objects are found in
742 ** - called before an APPEND operation to set the next object id to
743 ** be used for the new record to the next free value
747 set_next_object_id(object, table)
752 ## int rowcount, exists, value;
756 ## range of v is values
757 ## repeat retrieve (value = v.#value) where v.#name = @name
758 ## inquire_equel(rowcount = "rowcount")
762 ## retrieve (exists = any(tbl.name where tbl.name = value))
763 ## inquire_equel(rowcount = "rowcount")
768 if (value > MAX_ID_VALUE)
769 value = MIN_ID_VALUE;
770 ## retrieve (exists = any(tbl.name where tbl.name = value))
774 com_err(whoami, 0, "setting ID %s to %d", name, value);
775 ## repeat replace v (#value = @value) where v.#name = @name
780 /* For now this just checks the argc's. It should also see that there
781 * are no duplicate names.
784 sanity_check_queries()
787 int maxv = 0, maxa = 0;
788 extern int QueryCount1, QueryCount2;
789 extern struct query Queries1[], Queries2[];
790 #define MAX(x,y) ((x) > (y) ? (x) : (y))
792 for (i = 0; i < QueryCount1; i++) {
793 maxv = MAX(maxv, Queries1[i].vcnt);
794 maxa = MAX(maxa, Queries1[i].argc);
796 for (i = 0; i < QueryCount2; i++) {
797 maxv = MAX(maxv, Queries2[i].vcnt);
798 maxa = MAX(maxa, Queries2[i].argc);
800 if (MAX(maxv, maxa) > QMAXARGS) {
801 com_err(whoami, 0, "A query has more args than QMAXARGS");
811 * c-continued-statement-offset: 4
813 * c-argdecl-indent: 4