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;
26 char cdummy[MR_CDUMMY_LEN];
27 char stmt_buf[MR_STMTBUF_LEN];
28 EXEC SQL END DECLARE SECTION;
34 EXEC SQL BEGIN DECLARE SECTION;
35 int query_timeout = 30;
36 EXEC SQL END DECLARE SECTION;
40 /** Maybe this should be replaced by something like tytso's sql_error */
42 #define INGRES_BAD_DATE1 41206
43 #define INGRES_BAD_DATE2 40207
44 #define INGRES_DEADLOCK 49900
46 #define INGRES_BAD_COLUMN 30110
47 #define INGRES_ASGN_ERR 40204
48 #define INGRES_NO_CURSOR 30120
49 #define INGRES_NO_STMT 30130
52 #define INGRES_BAD_INT
53 #define INGRES_TIMEOUT
54 #define INGRES_NO_RANGE
59 * ingerr: (supposedly) called when Ingres indicates an error.
60 * I have not yet been able to get this to work to intercept a
61 * database open error.
66 EXEC SQL BEGIN DECLARE SECTION;
68 EXEC SQL END DECLARE SECTION;
69 ingres_errno = -sqlca.sqlcode;
71 switch (ingres_errno) {
73 * case INGRES_BAD_INT:
74 * mr_errcode = MR_INTEGER;
77 case INGRES_BAD_DATE1:
78 case INGRES_BAD_DATE2:
82 mr_errcode = MR_DEADLOCK;
83 com_err(whoami, 0, "INGRES deadlock detected");
85 /* I just don't know what the equivalent to this is, yet.
86 * case INGRES_TIMEOUT:
87 * mr_errcode = MR_BUSY;
88 * com_err(whoami, 0, "timed out getting lock");
91 /* This probably has no equivalent.
92 * case INGRES_NO_RANGE:
93 * mr_errcode = MR_INGRES_SOFTFAIL;
94 * com_err(whoami, 0, "INGRES missing range statement");
98 /* #ifdef-ing these out lets default: give me the INGRES text */
99 case INGRES_BAD_COLUMN:
100 mr_errcode = MR_INTERNAL;
101 com_err(whoami, 0, "Bad column name in query table");
103 case INGRES_ASGN_ERR:
104 mr_errcode = MR_INTERNAL;
105 com_err(whoami, 0, "Error in SQL assignment statement");
107 case INGRES_NO_CURSOR:
108 mr_errcode = MR_INTERNAL;
109 com_err(whoami, 0, "Cursor not opened");
112 mr_errcode = MR_INTERNAL;
113 com_err(whoami, 0, "Statement not declared");
117 /** Add the INGRES error_text to the alert message ??? **/
118 mr_errcode = MR_INGRES_ERR;
119 com_err(whoami, MR_INGRES_ERR, " code %d\n", ingres_errno);
120 EXEC SQL INQUIRE_SQL(:err_msg = errortext);
121 com_err(whoami, 0, "SQL error text = %s", err_msg);
122 critical_alert("MOIRA", "Moira server encountered INGRES ERROR %d", ingres_errno);
126 /* This is declarative, not executed. Applies from here on, in this file. */
127 EXEC SQL WHENEVER SQLERROR CALL ingerr;
129 int mr_open_database()
133 MR_SQLDA_T *mr_alloc_SQLDA();
134 static first_open = 1;
139 /* initialize local argv */
140 for (i = 0; i < 16; i++)
141 Argv[i] = malloc(ARGLEN);
143 SQLDA = mr_alloc_SQLDA();
152 /* open the database */
154 EXEC SQL CONNECT moira;
155 EXEC SQL set lockmode session where level = table, timeout = :query_timeout;
156 EXEC SQL set lockmode on capacls where readlock = shared;
157 EXEC SQL set lockmode on alias where readlock = shared;
160 EXEC SQL DATABASE moira
165 int mr_close_database()
173 mr_check_access(cl, name, argc, argv_ro)
180 struct query *get_query_by_name();
185 q = get_query_by_name(name, cl->args->mr_version_no);
186 if (q == (struct query *)0)
187 return(MR_NO_HANDLE);
189 return(mr_verify_query(cl, q, argc, argv_ro));
192 mr_process_query(cl, name, argc, argv_ro, action, actarg)
200 register struct query *q;
202 register struct validate *v;
207 EXEC SQL BEGIN DECLARE SECTION;
209 EXEC SQL END DECLARE SECTION;
210 struct save_queue *sq;
211 struct query *get_query_by_name();
213 struct save_queue *sq_create();
219 /* list queries command */
220 if (!strcmp(name, "_list_queries")) {
221 list_queries(cl->args->mr_version_no, action, actarg);
225 /* help query command */
226 if (!strcmp(name, "_help")) {
229 q = get_query_by_name(argv_ro[0], cl->args->mr_version_no);
230 if (q == (struct query *)0) return(MR_NO_HANDLE);
231 help_query(q, action, actarg);
235 /* get query structure, return error if named query does not exist */
236 q = get_query_by_name(name, cl->args->mr_version_no);
237 if (q == (struct query *)0) return(MR_NO_HANDLE);
240 /* setup argument vector, verify access and arguments */
241 if ((status = mr_verify_query(cl, q, argc, argv_ro)) != MR_SUCCESS)
244 /* perform any special query pre-processing */
245 if (v && v->pre_rtn) {
246 status = (*v->pre_rtn)(q, Argv, cl, 0);
247 if (status != MR_SUCCESS)
253 /* for queries that do not permit wildcarding, check if row
256 status = validate_row(q, Argv, v);
257 if (status != MR_EXISTS) break;
260 /* build "where" clause if needed */
262 build_qual(q->qual, q->argc, Argv, qual);
268 /* build "sort" clause if needed */
269 if (v && v->valobj) {
270 psort = build_sort(v, sort);
275 /* if there is a followup routine, then we must save the results */
276 /* of the first query for use by the followup routine */
277 /* if q->rvar = NULL, perform post_rtn only */
279 if (v && v->post_rtn) {
281 status = do_retrieve(q, pqual, psort, sq_save_args, sq);
282 if (status != MR_SUCCESS) {
286 status = (*v->post_rtn)(q, sq, v, action, actarg, cl);
288 /* normal retrieve */
289 status = do_retrieve(q, pqual, psort, action, actarg);
291 if (status != MR_SUCCESS) break;
293 status = (*v->post_rtn)(q, Argv, cl, action, actarg);
299 /* see if row already exists */
301 status = validate_row(q, Argv, v);
302 if (status != MR_EXISTS) break;
305 /* build "where" clause and perform update */
306 /* if q->rvar = NULL, perform post_rtn only */
308 build_qual(q->qual, q->argc, Argv, qual);
309 incremental_before(q->rtable, qual, argv_ro);
310 status = do_update(q, &Argv[q->argc], qual, action, actarg);
311 incremental_after(q->rtable, qual, argv_ro);
312 if (status != MR_SUCCESS) break;
313 flush_name(argv_ro[0], q->rtable);
314 table_name = q->rtable;
315 if (strcmp(q->shortname, "sshi") && strcmp(q->shortname, "ssif")) {
316 EXEC SQL UPDATE tblstats
317 SET updates = updates + 1, modtime = 'now'
318 WHERE table_name = :table_name;
322 /* execute followup routine (if any) */
323 if (v->post_rtn) status = (*v->post_rtn)(q, Argv, cl);
328 /* see if row already exists */
330 status = validate_row(q, Argv, v);
331 if (status != MR_NO_MATCH) break;
335 /* This is now done by a valobj, which also fetches the id value */
337 /* increment id number if necessary */
339 status = set_next_object_id(v->object_id, q->rtable, 0);
340 if (status != MR_SUCCESS) break;
344 /* build "where" clause if needed */
346 build_qual(q->qual, q->argc, Argv, qual);
352 /* perform the append */
353 /* if q->rvar = NULL, perform post_rtn only */
355 incremental_clear_before();
356 status = do_append(q, &Argv[q->argc], pqual, action, actarg);
357 if (status != MR_SUCCESS) break;
358 if (v && v->object_id) {
359 sprintf(qual, "%s.%s = %s",q->rtable, v->object_id,
360 Argv[q->argc+q->vcnt]);
361 incremental_after(q->rtable, qual, argv_ro);
363 incremental_after(q->rtable, pqual, argv_ro);
365 table_name = q->rtable;
366 EXEC SQL UPDATE tblstats
367 SET appends = appends + 1, modtime = 'now'
368 WHERE table_name = :table_name;
371 /* execute followup routine */
372 if (v->post_rtn) status = (*v->post_rtn)(q, Argv, cl);
376 /* see if row already exists */
378 status = validate_row(q, Argv, v);
379 if (status != MR_EXISTS) break;
382 /* build "where" clause and perform delete */
383 /* if q->rvar = NULL, perform post_rtn only */
385 build_qual(q->qual, q->argc, Argv, qual);
386 table_name = q->rtable;
387 incremental_before(q->rtable, qual, argv_ro);
388 status = do_delete(q, qual, action, actarg);
389 incremental_clear_after();
390 if (status != MR_SUCCESS) break;
391 flush_name(argv_ro[0], q->rtable);
392 EXEC SQL UPDATE tblstats
393 SET deletes = deletes + 1, modtime = 'now'
394 WHERE table_name = :table_name;
397 /* execute followup routine */
398 if (v->post_rtn) status = (*v->post_rtn)(q, Argv, cl);
404 if (status == MR_SUCCESS && ingres_errno != 0) {
405 com_err(whoami, MR_INTERNAL, "Server didn't notice INGRES ERROR %d",
410 if (q->type == RETRIEVE) {
411 EXEC SQL COMMIT WORK;
413 if (status == MR_SUCCESS) {
414 EXEC SQL COMMIT WORK;
420 fprintf(journal, "%% %s %s %s",
421 cl->clname, cl->entity, ctime(&now));
422 fprintf(journal, "%s[%d] ", q->name, cl->args->mr_version_no);
423 for (i = 0; i < argc; i++) {
427 requote(buf, argv_ro[i], sizeof(buf));
433 incremental_update();
436 if (ingres_errno != INGRES_DEADLOCK) {
437 EXEC SQL ROLLBACK WORK;
442 cache_commit(); /* commit following abort is safe */
444 if (status != MR_SUCCESS && log_flags & LOG_RES)
445 com_err(whoami, status, " (Query failed)");
449 build_qual(fmt, argc, argv, qual)
461 for (i = 0; i < argc; i++) {
463 if (c++ == (char *)0) return(MR_ARGS);
467 *(int *)&args[i] = *(int *)argv[i]; /* sigh */
469 return(MR_INGRES_ERR);
471 if (c = index(c, '%')) {
472 args[i] = args[i - 1];
481 sprintf(qual, fmt, args[0]);
485 sprintf(qual, fmt, args[0], args[1]);
489 sprintf(qual, fmt, args[0], args[1], args[2]);
493 sprintf(qual, fmt, args[0], args[1], args[2], args[3]);
497 com_err(whoami, MR_INTERNAL,
498 "Internal arg count error processing query");
506 register struct validate *v;
509 register struct valobj *vo;
518 if (vo->type == V_SORT) {
519 sprintf(elem, "%d", vo->index + 1); /* Result column number */
520 if (*sort) strcat(sort, ", ");
526 return ((*sort) ? sort : 0);
530 /* Build arguement vector, verify query and arguments */
532 mr_verify_query(cl, q, argc, argv_ro)
540 register struct validate *v = q->validate;
542 register int privileged = 0;
543 register char *to,*fr,*stop;
545 /* copy the arguments into a local argv that we can modify */
546 if (argc >= QMAXARGS)
548 for (i = 0; i < argc; i++) {
549 /* Single quotes must be doubled for SQL */
550 for (to=Argv[i], fr=argv_ro[i], stop=to+ARGLEN; (*fr) && (to<stop);) {
556 return(MR_ARG_TOO_LONG);
563 /* check initial query access */
564 status = check_query_access(q, Argv, cl);
565 if (status != MR_SUCCESS && status != MR_PERM)
567 if (status == MR_SUCCESS)
570 /* check argument count */
572 if (q->type == UPDATE || q->type == APPEND) argreq += q->vcnt;
573 if (argc != argreq) return(MR_ARGS);
575 /* validate arguments */
576 if (v && v->valobj) {
577 status = validate_fields(q, Argv, v->valobj, v->objcnt);
578 if (status != MR_SUCCESS) return(status);
581 /* perform special query access check */
582 if (!privileged && v && v->acs_rtn) {
583 status = (*v->acs_rtn)(q, Argv, cl);
584 if (status != MR_SUCCESS && status != MR_PERM)
586 if (status == MR_SUCCESS)
590 return(privileged ? MR_SUCCESS : MR_PERM);
594 /* This routine caches info from the database. Each query acl is stored
595 * in the query structure, and whether that acl contains everybody.
598 check_query_access(q, argv, cl)
603 EXEC SQL BEGIN DECLARE SECTION;
609 EXEC SQL END DECLARE SECTION;
614 /* initialize default uid */
616 EXEC SQL SELECT users_id INTO :def_uid FROM users WHERE login='default';
619 /* get query access control list */
624 EXEC SQL SELECT list_id INTO :acl_id FROM capacls WHERE tag = :name;
625 if (sqlca.sqlcode < 0) return(MR_INGRES_ERR);
626 if (sqlca.sqlcode == 100) return(MR_PERM);
629 /* check for default access */
630 EXEC SQL SELECT member_id INTO :acl_id FROM imembers
631 WHERE list_id = :acl_id AND member_type = 'USER'
632 AND member_id = :def_uid;
633 if (sqlca.sqlerrd[2] == 0)
642 if (get_client(cl, &client_type, &client_id) != MR_SUCCESS)
644 if (find_member("LIST", acl_id, client_type, client_id, 0))
651 get_client(cl, client_type, client_id)
656 if (cl->users_id > 0) {
657 *client_id = cl->users_id;
658 *client_type = "USER";
662 if (cl->client_id < 0) {
663 *client_id = -cl->users_id;
664 *client_type = "KERBEROS";
671 find_member(list_type, list_id, member_type, member_id)
673 EXEC SQL BEGIN DECLARE SECTION;
677 EXEC SQL END DECLARE SECTION;
679 EXEC SQL BEGIN DECLARE SECTION;
681 EXEC SQL END DECLARE SECTION;
683 if (!strcmp(strtrim(list_type), strtrim(member_type)) &&
684 list_id == member_id)
687 /* see if client is a direct member of list */
689 EXEC SQL SELECT member_id INTO :flag FROM imembers
690 WHERE list_id = :list_id AND member_type = :member_type
691 AND member_id = :member_id;
692 if(flag!=0) flag=1; /** Not strictly necessary */
693 if (sqlca.sqlcode == 0)
699 do_retrieve(q, pqual, psort, action, actarg)
700 register struct query *q;
701 EXEC SQL BEGIN DECLARE SECTION;
704 EXEC SQL END DECLARE SECTION;
708 static char **vaddrs = (char **)NULL;
714 if ((vaddrs = (char **)malloc(sizeof(char *) * QMAXARGS)) == NULL) {
715 com_err(whoami, MR_NO_MEM, "setting up static argv");
718 for (i = 0; i < QMAXARGS; i++) {
719 vaddrs[i]=SQLDA->sqlvar[i].sqldata;
723 build_sql_stmt(stmt_buf,"SELECT",q->tlist,vaddrs,pqual);
724 if(psort) { strcat(stmt_buf," ORDER BY "); strcat(stmt_buf,psort); }
725 EXEC SQL PREPARE stmt INTO :SQLDA USING NAMES FROM :stmt_buf;
728 EXEC SQL DECLARE csr001 CURSOR FOR stmt;
729 EXEC SQL OPEN csr001;
732 EXEC SQL FETCH csr001 USING DESCRIPTOR :SQLDA;
733 if(sqlca.sqlcode != 0) break;
734 (*action)(q->vcnt, vaddrs, actarg);
737 EXEC SQL CLOSE csr001;
739 if (mr_errcode) return(mr_errcode);
740 return ((rowcount == 0) ? MR_NO_MATCH : MR_SUCCESS);
743 build_sql_stmt(result_buf,cmd,targetlist,argv,qual)
750 char fmt_buf[MR_STMTBUF_LEN];
751 register char *res, *fmt;
754 sprintf(fmt_buf,"%s %s WHERE %s",cmd,targetlist,qual);
756 sprintf(fmt_buf,"%s %s",cmd,targetlist);
758 for(res=result_buf, fmt=fmt_buf; *fmt; fmt++) {
762 case '%': /* %% -> % */
774 sprintf(res,"%d",*(int *)*argv++); /* print to result buffer */
777 default: /* Swallow other %? pairs */
781 } else *res++ = *fmt; /* text -> result buffer */
786 do_update(q, argv, qual, action, actarg)
787 register struct query *q;
793 build_sql_stmt(stmt_buf,"UPDATE",q->tlist,argv,qual);
794 EXEC SQL EXECUTE IMMEDIATE :stmt_buf;
795 if (mr_errcode) return(mr_errcode);
799 do_append(q, argv, pqual, action, actarg)
800 register struct query *q;
806 build_sql_stmt(stmt_buf,"INSERT",q->tlist,argv,pqual);
807 EXEC SQL EXECUTE IMMEDIATE :stmt_buf;
808 if (mr_errcode) return(mr_errcode);
812 do_delete(q, qual, action, actarg)
813 register struct query *q;
818 sprintf(stmt_buf,"DELETE FROM %s WHERE %s",q->rtable,qual);
819 EXEC SQL EXECUTE IMMEDIATE :stmt_buf;
820 if (mr_errcode) return(mr_errcode);
826 ** set_next_object_id - set next object id in values table
828 ** Inputs: object - object name in values table and in objects
829 ** table - name of table objects are found in
830 ** limit - should the ID be range limited
832 ** - called before an APPEND operation to set the next object id to
833 ** be used for the new record to the next free value
837 set_next_object_id(object, table_name, limit)
838 EXEC SQL BEGIN DECLARE SECTION;
842 EXEC SQL END DECLARE SECTION;
844 EXEC SQL BEGIN DECLARE SECTION;
846 EXEC SQL END DECLARE SECTION;
849 EXEC SQL SELECT value INTO :value FROM numvalues WHERE name = :object;
850 if (sqlca.sqlerrd[2] != 1)
853 starting_value=value;
855 if (limit && value > MAX_ID_VALUE)
856 value = MIN_ID_VALUE;
858 sprintf(stmt_buf,"SELECT %s FROM %s WHERE %s=%d",object,table_name,object,value);
859 EXEC SQL PREPARE stmt INTO :SQLDA USING NAMES FROM :stmt_buf;
862 EXEC SQL DECLARE csr002 CURSOR FOR stmt;
863 EXEC SQL OPEN csr002;
864 EXEC SQL FETCH csr002 USING DESCRIPTOR :SQLDA;
865 if (sqlca.sqlcode < 0) return(mr_errcode);
866 if (sqlca.sqlcode == 100) break;
868 EXEC SQL CLOSE csr002;
870 if (limit && value == starting_value) {
871 com_err(whoami,0,"All id values have been used");
875 EXEC SQL CLOSE csr002;
878 com_err(whoami, 0, "setting ID %s to %d", object, value);
879 EXEC SQL UPDATE numvalues SET value = :value WHERE name = :object;
884 /* Turn a kerberos name into the user's ID of the account that principal
885 * owns. Sets the kerberos ID and user ID.
888 int set_krb_mapping(name, login, ok, kid, uid)
895 EXEC SQL BEGIN DECLARE SECTION;
898 EXEC SQL END DECLARE SECTION;
904 EXEC SQL SELECT k.users_id, k.string_id INTO :u_id, :k_id
905 FROM krbmap k, strings s
906 WHERE k.string_id = s.string_id AND s.string = :krbname;
908 if (ingres_errno) return(mr_errcode);
910 if (sqlca.sqlerrd[2] == 1) { /* rowcount */
916 if (name_to_id(name, "STRINGS", &k_id) == MR_SUCCESS)
924 if (name_to_id(login, "USERS", uid) != MR_SUCCESS)
929 if (ingres_errno) return(mr_errcode);
934 /* For now this just checks the argc's. It should also see that there
935 * are no duplicate names.
938 sanity_check_queries()
941 int maxv = 0, maxa = 0;
942 #ifdef MULTIPROTOCOLS
943 extern int QueryCount1, QueryCount2;
944 extern struct query Queries1[], Queries2[];
946 extern int QueryCount2;
947 extern struct query Queries2[];
948 #endif MULTIPROTOCOLS
950 #define MAX(x,y) ((x) > (y) ? (x) : (y))
952 #ifdef MULTIPROTOCOLS
953 for (i = 0; i < QueryCount1; i++) {
954 maxv = MAX(maxv, Queries1[i].vcnt);
955 maxa = MAX(maxa, Queries1[i].argc);
957 #endif MULTIPROTOCOLS
958 for (i = 0; i < QueryCount2; i++) {
959 maxv = MAX(maxv, Queries2[i].vcnt);
960 maxa = MAX(maxa, Queries2[i].argc);
962 if (MAX(maxv, maxa) > QMAXARGS) {
963 com_err(whoami, 0, "A query has more args than QMAXARGS");