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_dc = "$Header$";
16 #include <mit-copyright.h>
18 #include "mr_server.h"
19 EXEC SQL INCLUDE sqlca; /* SQL Communications Area */
20 EXEC SQL INCLUDE sqlda; /* SQL Descriptor Area */
24 EXEC SQL BEGIN DECLARE SECTION;
27 char cdummy[MR_CDUMMY_LEN];
28 char stmt_buf[MR_STMTBUF_LEN];
29 EXEC SQL END DECLARE SECTION;
35 EXEC SQL BEGIN DECLARE SECTION;
36 int query_timeout = 30;
37 EXEC SQL END DECLARE SECTION;
41 #define INGRES_BAD_DATE1 40206
42 #define INGRES_BAD_DATE2 40207
43 #define INGRES_DEADLOCK 49900
44 #define INGRES_TIMEOUT 39100
45 #define INGRES_BAD_COLUMN 30110
46 #define INGRES_ASGN_ERR 40204
47 #define INGRES_NO_CURSOR 30120
48 #define INGRES_NO_STMT 30130
51 * ingerr: Called when Ingres indicates an error.
56 EXEC SQL BEGIN DECLARE SECTION;
58 EXEC SQL END DECLARE SECTION;
59 ingres_errno = -sqlca.sqlcode;
61 switch (ingres_errno) {
62 case INGRES_BAD_DATE1:
63 case INGRES_BAD_DATE2:
67 mr_errcode = MR_DEADLOCK;
68 com_err(whoami, 0, "INGRES deadlock detected");
71 /* May be something other than timeout! #39100 is "Unknown error"
72 * Really should parse the error message enough to decide if it's a timeout */
74 com_err(whoami, 0, "timed out getting lock");
76 /* These should never come up unless someone breaks the query table */
77 case INGRES_NO_CURSOR:
78 mr_errcode = MR_INTERNAL;
79 com_err(whoami, 0, "Cursor not opened");
82 mr_errcode = MR_INTERNAL;
83 com_err(whoami, 0, "Statement not declared");
85 case INGRES_BAD_COLUMN:
86 mr_errcode = MR_INTERNAL;
87 com_err(whoami, 0, "Bad column name in query table");
90 mr_errcode = MR_INTERNAL;
91 com_err(whoami, 0, "Error in SQL assignment statement");
95 mr_errcode = MR_INGRES_ERR;
96 com_err(whoami, MR_INGRES_ERR, " code %d\n", ingres_errno);
97 EXEC SQL INQUIRE_SQL(:err_msg = errortext);
98 com_err(whoami, 0, "SQL error text = %s", err_msg);
99 critical_alert("MOIRA", "Moira server encountered INGRES ERROR %d", ingres_errno);
103 /* This is declarative, not executed. Applies from here on, in this file. */
104 EXEC SQL WHENEVER SQLERROR CALL ingerr;
106 int mr_open_database()
110 MR_SQLDA_T *mr_alloc_SQLDA();
111 static first_open = 1;
116 /* initialize local argv */
117 for (i = 0; i < 16; i++)
118 Argv[i] = malloc(ARGLEN);
120 SQLDA = mr_alloc_SQLDA();
129 /* open the database */
131 EXEC SQL CONNECT moira;
133 return (ingres_errno);
134 EXEC SQL set lockmode session where level = table, timeout = :query_timeout;
135 EXEC SQL set lockmode on capacls where readlock = shared;
136 EXEC SQL set lockmode on alias where readlock = shared;
139 EXEC SQL DATABASE moira
145 EXEC SQL SELECT SIZE(signature) INTO :mr_sig_length FROM users WHERE users_id=0; /* Harmless on second open */
152 int mr_close_database()
160 mr_check_access(cl, name, argc, argv_ro)
167 struct query *get_query_by_name();
172 q = get_query_by_name(name, cl->args->mr_version_no);
173 if (q == (struct query *)0)
174 return(MR_NO_HANDLE);
176 return(mr_verify_query(cl, q, argc, argv_ro));
179 mr_process_query(cl, name, argc, argv_ro, action, actarg)
187 register struct query *q;
189 register struct validate *v;
194 EXEC SQL BEGIN DECLARE SECTION;
196 EXEC SQL END DECLARE SECTION;
197 struct save_queue *sq;
198 struct query *get_query_by_name();
200 struct save_queue *sq_create();
206 /* list queries command */
207 if (!strcmp(name, "_list_queries")) {
208 list_queries(cl->args->mr_version_no, action, actarg);
212 /* help query command */
213 if (!strcmp(name, "_help")) {
216 q = get_query_by_name(argv_ro[0], cl->args->mr_version_no);
217 if (q == (struct query *)0) return(MR_NO_HANDLE);
218 help_query(q, action, actarg);
222 /* get query structure, return error if named query does not exist */
223 q = get_query_by_name(name, cl->args->mr_version_no);
224 if (q == (struct query *)0) return(MR_NO_HANDLE);
227 /* setup argument vector, verify access and arguments */
228 if ((status = mr_verify_query(cl, q, argc, argv_ro)) != MR_SUCCESS)
231 /* perform any special query pre-processing */
232 if (v && v->pre_rtn) {
233 status = (*v->pre_rtn)(q, Argv, cl, 0);
234 if (status != MR_SUCCESS)
240 /* for queries that do not permit wildcarding, check if row
243 status = validate_row(q, Argv, v);
244 if (status != MR_EXISTS) break;
247 /* build "where" clause if needed */
249 build_qual(q->qual, q->argc, Argv, qual);
255 /* build "sort" clause if needed */
256 if (v && v->valobj) {
257 psort = build_sort(v, sort);
262 /* if there is a followup routine, then we must save the results */
263 /* of the first query for use by the followup routine */
264 /* if q->rvar = NULL, perform post_rtn only */
266 if (v && v->post_rtn) {
268 status = do_retrieve(q, pqual, psort, sq_save_args, sq);
269 if (status != MR_SUCCESS) {
273 status = (*v->post_rtn)(q, sq, v, action, actarg, cl);
275 /* normal retrieve */
276 status = do_retrieve(q, pqual, psort, action, actarg);
278 if (status != MR_SUCCESS) break;
280 status = (*v->post_rtn)(q, Argv, cl, action, actarg);
286 /* see if row already exists */
288 status = validate_row(q, Argv, v);
289 if (status != MR_EXISTS) break;
292 /* build "where" clause and perform update */
293 /* if q->rvar = NULL, perform post_rtn only */
295 build_qual(q->qual, q->argc, Argv, qual);
296 incremental_before(q->rtable, qual, argv_ro);
297 status = do_update(q, &Argv[q->argc], qual, action, actarg);
298 incremental_after(q->rtable, qual, argv_ro);
299 if (status != MR_SUCCESS) break;
300 flush_name(argv_ro[0], q->rtable);
301 table_name = q->rtable;
302 if (strcmp(q->shortname, "sshi") && strcmp(q->shortname, "ssif")) {
303 EXEC SQL UPDATE tblstats
304 SET updates = updates + 1, modtime = 'now'
305 WHERE table_name = :table_name;
309 /* execute followup routine (if any) */
310 if (v->post_rtn) status = (*v->post_rtn)(q, Argv, cl);
315 /* see if row already exists */
317 status = validate_row(q, Argv, v);
318 if (status != MR_NO_MATCH) break;
321 /* build "where" clause if needed */
323 build_qual(q->qual, q->argc, Argv, qual);
329 /* perform the append */
330 /* if q->rvar = NULL, perform post_rtn only */
332 incremental_clear_before();
333 status = do_append(q, &Argv[q->argc], pqual, action, actarg);
334 if (status != MR_SUCCESS) break;
335 if (v && v->object_id) {
336 sprintf(qual, "%s.%s = %s",q->rvar, v->object_id,
337 Argv[q->argc+q->vcnt]);
338 incremental_after(q->rtable, qual, argv_ro);
340 incremental_after(q->rtable, pqual, argv_ro);
342 table_name = q->rtable;
343 EXEC SQL UPDATE tblstats
344 SET appends = appends + 1, modtime = 'now'
345 WHERE table_name = :table_name;
348 /* execute followup routine */
349 if (v->post_rtn) status = (*v->post_rtn)(q, Argv, cl);
353 /* see if row already exists */
355 status = validate_row(q, Argv, v);
356 if (status != MR_EXISTS) break;
359 /* build "where" clause and perform delete */
360 /* if q->rvar = NULL, perform post_rtn only */
362 build_qual(q->qual, q->argc, Argv, qual);
363 table_name = q->rtable;
364 incremental_before(q->rtable, qual, argv_ro);
365 status = do_delete(q, qual, action, actarg);
366 incremental_clear_after();
367 if (status != MR_SUCCESS) break;
368 flush_name(argv_ro[0], q->rtable);
369 EXEC SQL UPDATE tblstats
370 SET deletes = deletes + 1, modtime = 'now'
371 WHERE table_name = :table_name;
374 /* execute followup routine */
375 if (v->post_rtn) status = (*v->post_rtn)(q, Argv, cl);
381 if (status == MR_SUCCESS && ingres_errno != 0) {
382 com_err(whoami, MR_INTERNAL, "Server didn't notice INGRES ERROR %d",
387 if (q->type == RETRIEVE) {
388 EXEC SQL COMMIT WORK;
390 if (status == MR_SUCCESS) {
391 EXEC SQL COMMIT WORK;
397 fprintf(journal, "%% %s %s %s",
398 cl->clname, cl->entity, ctime(&now));
399 fprintf(journal, "%s[%d] ", q->name, cl->args->mr_version_no);
400 for (i = 0; i < argc; i++) {
404 requote(buf, argv_ro[i], sizeof(buf));
410 incremental_update();
413 if (ingres_errno != INGRES_DEADLOCK) {
414 EXEC SQL ROLLBACK WORK;
419 cache_commit(); /* commit following abort is safe */
421 if (status != MR_SUCCESS && log_flags & LOG_RES)
422 com_err(whoami, status, " (Query failed)");
426 build_qual(fmt, argc, argv, qual)
438 for (i = 0; i < argc; i++) {
440 if (c++ == (char *)0) return(MR_ARGS);
444 *(int *)&args[i] = *(int *)argv[i]; /* sigh */
446 return(MR_INGRES_ERR);
448 if (c = index(c, '%')) {
449 args[i] = args[i - 1];
458 sprintf(qual, fmt, args[0]);
462 sprintf(qual, fmt, args[0], args[1]);
466 sprintf(qual, fmt, args[0], args[1], args[2]);
470 sprintf(qual, fmt, args[0], args[1], args[2], args[3]);
474 com_err(whoami, MR_INTERNAL,
475 "Internal arg count error processing query");
483 register struct validate *v;
486 register struct valobj *vo;
495 if (vo->type == V_SORT) {
496 sprintf(elem, "%d", vo->index + 1); /* Result column number */
497 if (*sort) strcat(sort, ", ");
503 return ((*sort) ? sort : 0);
507 /* Build arguement vector, verify query and arguments */
509 mr_verify_query(cl, q, argc, argv_ro)
517 register struct validate *v = q->validate;
519 register int privileged = 0;
520 register char *to,*fr,*stop;
522 /* copy the arguments into a local argv that we can modify */
523 if (argc >= QMAXARGS)
525 for (i = 0; i < argc; i++) {
526 /* Single quotes must be doubled for SQL */
527 for (to=Argv[i], fr=argv_ro[i], stop=to+ARGLEN; (*fr) && (to<stop);) {
533 return(MR_ARG_TOO_LONG);
540 /* check initial query access */
541 status = check_query_access(q, Argv, cl);
542 if (status != MR_SUCCESS && status != MR_PERM)
544 if (status == MR_SUCCESS)
547 /* check argument count */
549 if (q->type == UPDATE || q->type == APPEND) argreq += q->vcnt;
550 if (argc != argreq) return(MR_ARGS);
552 /* validate arguments */
553 if (v && v->valobj) {
554 status = validate_fields(q, Argv, v->valobj, v->objcnt);
555 if (status != MR_SUCCESS) return(status);
558 /* perform special query access check */
559 if (!privileged && v && v->acs_rtn) {
560 status = (*v->acs_rtn)(q, Argv, cl);
561 if (status != MR_SUCCESS && status != MR_PERM)
563 if (status == MR_SUCCESS)
567 return(privileged ? MR_SUCCESS : MR_PERM);
571 /* This routine caches info from the database. Each query acl is stored
572 * in the query structure, and whether that acl contains everybody.
575 check_query_access(q, argv, cl)
580 EXEC SQL BEGIN DECLARE SECTION;
586 EXEC SQL END DECLARE SECTION;
591 /* initialize default uid */
593 EXEC SQL SELECT users_id INTO :def_uid FROM users WHERE login='default';
596 /* get query access control list */
601 EXEC SQL SELECT list_id INTO :acl_id FROM capacls WHERE tag = :name;
602 if (sqlca.sqlcode < 0) return(MR_INGRES_ERR);
603 if (sqlca.sqlcode == 100) return(MR_PERM);
606 /* check for default access */
607 EXEC SQL SELECT member_id INTO :acl_id FROM imembers
608 WHERE list_id = :acl_id AND member_type = 'USER'
609 AND member_id = :def_uid;
610 if (sqlca.sqlerrd[2] == 0)
619 if (get_client(cl, &client_type, &client_id) != MR_SUCCESS)
621 if (find_member("LIST", acl_id, client_type, client_id, 0))
628 get_client(cl, client_type, client_id)
633 if (cl->users_id > 0) {
634 *client_id = cl->users_id;
635 *client_type = "USER";
639 if (cl->client_id < 0) {
640 *client_id = -cl->users_id;
641 *client_type = "KERBEROS";
648 find_member(list_type, list_id, member_type, member_id)
650 EXEC SQL BEGIN DECLARE SECTION;
654 EXEC SQL END DECLARE SECTION;
656 EXEC SQL BEGIN DECLARE SECTION;
658 EXEC SQL END DECLARE SECTION;
660 if (!strcmp(strtrim(list_type), strtrim(member_type)) &&
661 list_id == member_id)
664 /* see if client is a direct member of list */
666 EXEC SQL SELECT member_id INTO :flag FROM imembers
667 WHERE list_id = :list_id AND member_type = :member_type
668 AND member_id = :member_id;
669 if(flag!=0) flag=1; /** Not strictly necessary */
670 if (sqlca.sqlcode == 0)
676 do_retrieve(q, pqual, psort, action, actarg)
677 register struct query *q;
678 EXEC SQL BEGIN DECLARE SECTION;
681 EXEC SQL END DECLARE SECTION;
685 static char **vaddrs = (char **)NULL;
691 if ((vaddrs = (char **)malloc(sizeof(char *) * QMAXARGS)) == NULL) {
692 com_err(whoami, MR_NO_MEM, "setting up static argv");
695 for (i = 0; i < QMAXARGS; i++) {
696 vaddrs[i]=SQLDA->sqlvar[i].sqldata;
700 build_sql_stmt(stmt_buf,"SELECT",q->tlist,vaddrs,pqual);
701 if(psort) { strcat(stmt_buf," ORDER BY "); strcat(stmt_buf,psort); }
702 EXEC SQL PREPARE stmt INTO :SQLDA USING NAMES FROM :stmt_buf;
705 EXEC SQL DECLARE csr001 CURSOR FOR stmt;
706 EXEC SQL OPEN csr001;
709 EXEC SQL FETCH csr001 USING DESCRIPTOR :SQLDA;
710 if(sqlca.sqlcode != 0) break;
711 (*action)(q->vcnt, vaddrs, actarg);
714 EXEC SQL CLOSE csr001;
716 if (mr_errcode) return(mr_errcode);
717 return ((rowcount == 0) ? MR_NO_MATCH : MR_SUCCESS);
720 build_sql_stmt(result_buf,cmd,targetlist,argv,qual)
727 char fmt_buf[MR_STMTBUF_LEN];
728 register char *res, *fmt;
731 sprintf(fmt_buf,"%s %s WHERE %s",cmd,targetlist,qual);
733 sprintf(fmt_buf,"%s %s",cmd,targetlist);
735 for(res=result_buf, fmt=fmt_buf; *fmt; fmt++) {
739 case '%': /* %% -> % */
751 sprintf(res,"%d",*(int *)*argv++); /* print to result buffer */
754 default: /* Swallow other %? pairs */
758 } else *res++ = *fmt; /* text -> result buffer */
763 do_update(q, argv, qual, action, actarg)
764 register struct query *q;
770 build_sql_stmt(stmt_buf,"UPDATE",q->tlist,argv,qual);
771 EXEC SQL EXECUTE IMMEDIATE :stmt_buf;
772 if (mr_errcode) return(mr_errcode);
776 do_append(q, argv, pqual, action, actarg)
777 register struct query *q;
783 build_sql_stmt(stmt_buf,"INSERT",q->tlist,argv,pqual);
784 EXEC SQL EXECUTE IMMEDIATE :stmt_buf;
785 if (mr_errcode) return(mr_errcode);
789 do_delete(q, qual, action, actarg)
790 register struct query *q;
795 sprintf(stmt_buf,"DELETE FROM %s WHERE %s",q->rtable,qual);
796 EXEC SQL EXECUTE IMMEDIATE :stmt_buf;
797 if (mr_errcode) return(mr_errcode);
803 ** set_next_object_id - set next object id in values table
805 ** Inputs: object - object name in values table and in objects
806 ** table - name of table objects are found in
807 ** limit - should the ID be range limited
809 ** - called before an APPEND operation to set the next object id to
810 ** be used for the new record to the next free value
814 set_next_object_id(object, table_name, limit)
815 EXEC SQL BEGIN DECLARE SECTION;
819 EXEC SQL END DECLARE SECTION;
821 EXEC SQL BEGIN DECLARE SECTION;
823 EXEC SQL END DECLARE SECTION;
826 EXEC SQL SELECT value INTO :value FROM numvalues WHERE name = :object;
827 if (sqlca.sqlerrd[2] != 1)
830 starting_value=value;
832 if (limit && value > MAX_ID_VALUE)
833 value = MIN_ID_VALUE;
835 sprintf(stmt_buf,"SELECT %s FROM %s WHERE %s=%d",object,table_name,object,value);
836 EXEC SQL PREPARE stmt INTO :SQLDA USING NAMES FROM :stmt_buf;
839 EXEC SQL DECLARE csr002 CURSOR FOR stmt;
840 EXEC SQL OPEN csr002;
841 EXEC SQL FETCH csr002 USING DESCRIPTOR :SQLDA;
842 if (sqlca.sqlcode < 0) return(mr_errcode);
843 if (sqlca.sqlcode == 100) break;
845 EXEC SQL CLOSE csr002;
847 if (limit && value == starting_value) {
848 com_err(whoami,0,"All id values have been used");
852 EXEC SQL CLOSE csr002;
855 com_err(whoami, 0, "setting ID %s to %d", object, value);
856 EXEC SQL UPDATE numvalues SET value = :value WHERE name = :object;
861 /* Turn a kerberos name into the user's ID of the account that principal
862 * owns. Sets the kerberos ID and user ID.
865 int set_krb_mapping(name, login, ok, kid, uid)
872 EXEC SQL BEGIN DECLARE SECTION;
875 EXEC SQL END DECLARE SECTION;
881 EXEC SQL SELECT km.users_id, km.string_id INTO :u_id, :k_id
882 FROM krbmap km, strings str
883 WHERE km.string_id = str.string_id AND str.string = :krbname;
885 if (ingres_errno) return(mr_errcode);
887 if (sqlca.sqlerrd[2] == 1) { /* rowcount */
893 if (name_to_id(name, "STRINGS", &k_id) == MR_SUCCESS)
901 if (name_to_id(login, "USERS", uid) != MR_SUCCESS)
906 if (ingres_errno) return(mr_errcode);
911 /* For now this just checks the argc's. It should also see that there
912 * are no duplicate names.
915 sanity_check_queries()
918 int maxv = 0, maxa = 0;
919 #ifdef MULTIPROTOCOLS
920 extern int QueryCount1, QueryCount2;
921 extern struct query Queries1[], Queries2[];
923 extern int QueryCount2;
924 extern struct query Queries2[];
925 #endif MULTIPROTOCOLS
927 #define MAX(x,y) ((x) > (y) ? (x) : (y))
929 #ifdef MULTIPROTOCOLS
930 for (i = 0; i < QueryCount1; i++) {
931 maxv = MAX(maxv, Queries1[i].vcnt);
932 maxa = MAX(maxa, Queries1[i].argc);
934 #endif MULTIPROTOCOLS
935 for (i = 0; i < QueryCount2; i++) {
936 maxv = MAX(maxv, Queries2[i].vcnt);
937 maxa = MAX(maxa, Queries2[i].argc);
939 if (MAX(maxv, maxa) > QMAXARGS) {
940 com_err(whoami, 0, "A query has more args than QMAXARGS");