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 MR_SQLDA_T *SQLDA=&_SQLDA;
25 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 /** Maybe this should be replaced by something like tytso's sql_error
43 #define INGRES_BAD_DATE1 41206
44 #define INGRES_BAD_DATE2 40207
45 #define INGRES_DEADLOCK 49900
47 #define INGRES_BAD_INT
48 #define INGRES_TIMEOUT
49 #define INGRES_NO_RANGE
53 * ingerr: (supposedly) called when Ingres indicates an error.
54 * I have not yet been able to get this to work to intercept a
55 * database open error.
60 ingres_errno = -sqlca.sqlcode;
62 switch (ingres_errno) {
65 mr_errcode = MR_INTEGER;
68 case INGRES_BAD_DATE1:
69 case INGRES_BAD_DATE2:
73 mr_errcode = MR_DEADLOCK;
74 com_err(whoami, 0, "INGRES deadlock detected");
76 /* I just don't know what the equivalent to this is, yet.
79 com_err(whoami, 0, "timed out getting lock");
82 /* This probably has no equivalent.
84 mr_errcode = MR_INGRES_SOFTFAIL;
85 com_err(whoami, 0, "INGRES missing range statement");
89 /** Add the INGRES error_text to the alert message ??? **/
90 mr_errcode = MR_INGRES_ERR;
91 com_err(whoami, MR_INGRES_ERR, " code %d\n", *num);
92 critical_alert("MOIRA", "Moira server encountered INGRES ERROR %d", *num);
98 /* This is declarative, not executed. Moved to emphasize the fact. */
99 EXEC SQL WHENEVER SQLERROR CALL ingerr;
101 int mr_open_database()
105 static first_open = 1;
110 /* initialize local argv */
111 for (i = 0; i < 16; i++)
112 Argv[i] = malloc(ARGLEN);
114 /* EXEC SQL WHENEVER SQLERROR CALL ingerr; */
122 /* open the database */
124 EXEC SQL CONNECT moira;
125 EXEC SQL set lockmode session where level = table, timeout = :query_timeout;
126 EXEC SQL set lockmode on capacls where readlock = shared;
127 EXEC SQL set lockmode on alias where readlock = shared;
130 EXEC SQL DATABASE moira
135 int mr_close_database()
143 mr_check_access(cl, name, argc, argv_ro)
150 struct query *get_query_by_name();
155 q = get_query_by_name(name, cl->args->mr_version_no);
156 if (q == (struct query *)0)
157 return(MR_NO_HANDLE);
159 return(mr_verify_query(cl, q, argc, argv_ro));
162 mr_process_query(cl, name, argc, argv_ro, action, actarg)
170 register struct query *q;
172 register struct validate *v;
177 EXEC SQL BEGIN DECLARE SECTION;
179 EXEC SQL END DECLARE SECTION;
180 struct save_queue *sq;
181 struct query *get_query_by_name();
183 struct save_queue *sq_create();
189 /* list queries command */
190 if (!strcmp(name, "_list_queries")) {
191 list_queries(cl->args->mr_version_no, action, actarg);
195 /* help query command */
196 if (!strcmp(name, "_help")) {
199 q = get_query_by_name(argv_ro[0], cl->args->mr_version_no);
200 if (q == (struct query *)0) return(MR_NO_HANDLE);
201 help_query(q, action, actarg);
205 /* get query structure, return error if named query does not exist */
206 q = get_query_by_name(name, cl->args->mr_version_no);
207 if (q == (struct query *)0) return(MR_NO_HANDLE);
210 /* setup argument vector, verify access and arguments */
211 if ((status = mr_verify_query(cl, q, argc, argv_ro)) != MR_SUCCESS)
214 /* perform any special query pre-processing */
215 if (v && v->pre_rtn) {
216 status = (*v->pre_rtn)(q, Argv, cl, 0);
217 if (status != MR_SUCCESS)
223 /* for queries that do not permit wildcarding, check if row
226 status = validate_row(q, Argv, v);
227 if (status != MR_EXISTS) break;
230 /* build "where" clause if needed */
232 build_qual(q->qual, q->argc, Argv, qual);
238 /* build "sort" clause if needed */
239 if (v && v->valobj) {
240 psort = build_sort(v, sort);
245 /* if there is a followup routine, then we must save the results */
246 /* of the first query for use by the followup routine */
247 /* if q->rvar = NULL, perform post_rtn only */
249 if (v && v->post_rtn) {
251 status = do_retrieve(q, pqual, psort, sq_save_args, sq);
252 if (status != MR_SUCCESS) {
256 status = (*v->post_rtn)(q, sq, v, action, actarg, cl);
258 /* normal retrieve */
259 status = do_retrieve(q, pqual, psort, action, actarg);
261 if (status != MR_SUCCESS) break;
263 status = (*v->post_rtn)(q, Argv, cl, action, actarg);
269 /* see if row already exists */
271 status = validate_row(q, Argv, v);
272 if (status != MR_EXISTS) break;
275 /* build "where" clause and perform update */
276 /* if q->rvar = NULL, perform post_rtn only */
278 build_qual(q->qual, q->argc, Argv, qual);
279 incremental_before(q->rtable, qual, argv_ro);
280 status = do_update(q, &Argv[q->argc], qual, action, actarg);
281 incremental_after(q->rtable, qual, argv_ro);
282 if (status != MR_SUCCESS) break;
283 flush_name(argv_ro[0], q->rtable);
284 table_name = q->rtable;
285 if (strcmp(q->shortname, "sshi") && strcmp(q->shortname, "ssif")) {
286 EXEC SQL UPDATE tblstats
287 SET updates = updates + 1, modtime = 'now'
288 WHERE table_name = :table_name;
292 /* execute followup routine (if any) */
293 if (v->post_rtn) status = (*v->post_rtn)(q, Argv, cl);
298 /* see if row already exists */
300 status = validate_row(q, Argv, v);
301 if (status != MR_NO_MATCH) break;
304 /* increment id number if necessary */
306 status = set_next_object_id(v->object_id, q->rtable, 0);
307 if (status != MR_SUCCESS) break;
310 /* build "where" clause if needed */
312 build_qual(q->qual, q->argc, Argv, qual);
318 /* perform the append */
319 /* if q->rvar = NULL, perform post_rtn only */
321 incremental_clear_before();
322 status = do_append(q, &Argv[q->argc], pqual, action, actarg);
323 if (status != MR_SUCCESS) break;
324 if (v && v->object_id) {
325 sprintf(qual, "%s.%s = values.value and values.name = '%s',
326 q->rtable, v->object_id, v->object_id);
327 incremental_after(q->rtable, qual, argv_ro);
329 incremental_after(q->rtable, pqual, argv_ro);
331 table_name = q->rtable;
332 EXEC SQL UPDATE tblstats
333 SET appends = appends + 1, modtime = 'now'
334 WHERE table_name = :table_name;
337 /* execute followup routine */
338 if (v->post_rtn) status = (*v->post_rtn)(q, Argv, cl);
342 /* see if row already exists */
344 status = validate_row(q, Argv, v);
345 if (status != MR_EXISTS) break;
348 /* build "where" clause and perform delete */
349 /* if q->rvar = NULL, perform post_rtn only */
351 build_qual(q->qual, q->argc, Argv, qual);
352 table_name = q->rtable;
353 incremental_before(q->rtable, qual, argv_ro);
354 status = do_delete(q, qual, action, actarg);
355 incremental_clear_after();
356 if (status != MR_SUCCESS) break;
357 flush_name(argv_ro[0], q->rtable);
358 EXEC SQL UPDATE tblstats
359 SET deletes = deletes + 1, modtime = 'now'
360 WHERE table_name = :table_name;
363 /* execute followup routine */
364 if (v->post_rtn) status = (*v->post_rtn)(q, Argv, cl);
370 if (status == MR_SUCCESS && ingres_errno != 0) {
371 com_err(whoami, MR_INTERNAL, "Server didn't notice INGRES ERROR %d",
376 if (q->type == RETRIEVE) {
378 EXEC SQL END TRANSACTION;
381 EXEC SQL COMMIT WORK;
384 if (status == MR_SUCCESS) {
386 EXEC SQL END TRANSACTION;
389 EXEC SQL COMMIT WORK;
396 fprintf(journal, "%% %s %s %s",
397 cl->clname, cl->entity, ctime(&now));
398 fprintf(journal, "%s[%d] ", q->name, cl->args->mr_version_no);
399 for (i = 0; i < argc; i++) {
403 requote(buf, argv_ro[i], sizeof(buf));
409 incremental_update();
412 if (ingres_errno != INGRES_DEADLOCK) {
417 EXEC SQL ROLLBACK WORK;
423 cache_commit(); /* commit following abort is safe */
425 if (status != MR_SUCCESS && log_flags & LOG_RES)
426 com_err(whoami, status, " (Query failed)");
430 build_qual(fmt, argc, argv, qual)
442 for (i = 0; i < argc; i++) {
444 if (c++ == (char *)0) return(MR_ARGS);
448 *(int *)&args[i] = *(int *)argv[i]; /* sigh */
450 return(MR_INGRES_ERR);
452 if (c = index(c, '%')) {
453 args[i] = args[i - 1];
462 sprintf(qual, fmt, args[0]);
466 sprintf(qual, fmt, args[0], args[1]);
470 sprintf(qual, fmt, args[0], args[1], args[2]);
474 sprintf(qual, fmt, args[0], args[1], args[2], args[3]);
478 com_err(whoami, MR_INTERNAL,
479 "Internal arg count error processing query");
487 register struct validate *v;
490 register struct valobj *vo;
499 if (vo->type == V_SORT) {
500 sprintf(elem, "%d", vo->index + 1); /* Result column number */
501 if (*sort) strcat(sort, ", ");
507 return ((*sort) ? sort : 0);
511 /* Build arguement vector, verify query and arguments */
513 mr_verify_query(cl, q, argc, argv_ro)
521 register struct validate *v = q->validate;
523 register int privileged = 0;
526 /* copy the arguments into a local argv that we can modify */
527 if (argc >= QMAXARGS)
529 for (i = 0; i < argc; i++) {
530 if ((len = strlen(argv_ro[i])) < ARGLEN)
531 strcpy(Argv[i], argv_ro[i]);
533 return(MR_ARG_TOO_LONG);
534 if (Argv[i][len-1] == '\\')
538 /* check initial query access */
539 status = check_query_access(q, Argv, cl);
540 if (status != MR_SUCCESS && status != MR_PERM)
542 if (status == MR_SUCCESS)
545 /* check argument count */
547 if (q->type == UPDATE || q->type == APPEND) argreq += q->vcnt;
548 if (argc != argreq) return(MR_ARGS);
550 /* validate arguments */
551 if (v && v->valobj) {
552 status = validate_fields(q, Argv, v->valobj, v->objcnt);
553 if (status != MR_SUCCESS) return(status);
556 /* perform special query access check */
557 if (!privileged && v && v->acs_rtn) {
558 status = (*v->acs_rtn)(q, Argv, cl);
559 if (status != MR_SUCCESS && status != MR_PERM)
561 if (status == MR_SUCCESS)
565 return(privileged ? MR_SUCCESS : MR_PERM);
569 /* This routine caches info from the database. Each query acl is stored
570 * in the query structure, and whether that acl contains everybody.
573 check_query_access(q, argv, cl)
578 EXEC SQL BEGIN DECLARE SECTION;
584 EXEC SQL END DECLARE SECTION;
589 /* initialize default uid */
591 EXEC SQL SELECT users_id INTO :def_uid FROM users WHERE login='default';
594 /* get query access control list */
599 EXEC SQL SELECT list_id INTO :acl_id FROM capacls WHERE tag = :name;
600 if (sqlca.sqlcode < 0) return(MR_INGRES_ERR);
601 if (sqlca.sqlcode == 100) return(MR_PERM);
604 /* check for default access */
605 EXEC SQL SELECT member_id INTO :acl_id FROM imembers
606 WHERE list_id = :acl_id AND member_type = 'USER'
607 AND member_id = :def_uid;
608 if (sqlca.sqlerrd[2] == 0)
617 if (get_client(cl, &client_type, &client_id) != MR_SUCCESS)
619 if (find_member("LIST", acl_id, client_type, client_id, 0))
626 get_client(cl, client_type, client_id)
631 if (cl->users_id > 0) {
632 *client_id = cl->users_id;
633 *client_type = "USER";
637 if (cl->client_id < 0) {
638 *client_id = -cl->users_id;
639 *client_type = "KERBEROS";
646 find_member(list_type, list_id, member_type, member_id)
648 EXEC SQL BEGIN DECLARE SECTION;
652 EXEC SQL END DECLARE SECTION;
654 EXEC SQL BEGIN DECLARE SECTION;
656 EXEC SQL END DECLARE SECTION;
658 if (!strcmp(strtrim(list_type), strtrim(member_type)) &&
659 list_id == member_id)
662 /* see if client is a direct member of list */
664 EXEC SQL SELECT member_id INTO :flag FROM imembers
665 WHERE list_id = :list_id AND member_type = :member_type
666 AND member_id = :member_id;
667 if(flag!=0) flag=1; /** Not strictly necessary */
668 if (sqlca.sqlcode == 0)
674 do_retrieve(q, pqual, psort, action, actarg)
675 register struct query *q;
676 EXEC SQL BEGIN DECLARE SECTION;
679 EXEC SQL END DECLARE SECTION;
683 static char **vaddrs = (char **)NULL;
688 if ((vaddrs = (char **)malloc(sizeof(char *) * QMAXARGS)) == NULL) {
689 com_err(whoami, MR_NO_MEM, "setting up static argv");
692 for (i = 0; i < QMAXARGS; i++) {
693 if ((vaddrs[i] = malloc(QMAXARGSIZE)) == NULL) {
694 com_err(whoami, MR_NO_MEM, "setting up static argv");
705 * retrieve unique (param (q->tlist, vaddrs)) where cqual
708 * (*action)(q->vcnt, vaddrs, actarg);
711 * retrieve unique (param (q->tlist, vaddrs))
714 * (*action)(q->vcnt, vaddrs, actarg);
721 * retrieve unique (param (q->tlist, vaddrs)) where cqual
723 * (*action)(q->vcnt, vaddrs, actarg);
726 * retrieve unique (param (q->tlist, vaddrs))
728 * (*action)(q->vcnt, vaddrs, actarg);
734 build_sql_stmt(stmt_buf,"SELECT",q->tlist,argv,pqual);
735 if(psort) { strcat(stmt_buf," ORDER BY "); strcat(stmt_buf,psort); }
736 EXEC SQL PREPARE stmt FROM :stmt_buf;
737 EXEC SQL DESCRIBE stmt INTO :SQLDA;
738 if(SQLDA->sqld > MR_DYN_VARS) {
739 comm_err(whoami, MR_INTERNAL,
740 "Internal arg count error processing dynamic query");
744 for(i=0;i<SQLDA->sqld;i++) {
745 SQLDA->sqlvar[i].sqldata=vaddr[i];
746 SQLDA->sqlvar[i].sqllen=QMAXARGSIZE-2;
748 EXEC SQL DECLARE csr001 CURSOR FOR stmt;
749 EXEC SQL OPEN csr001;
751 EXEC SQL FETCH csr001 USING DESCRIPTOR :SQLDA;
752 if(sqlca.sqlcode != 0) break;
753 (*action)(q->vcnt, vaddrs, actarg);
755 EXEC SQL CLOSE csr001;
757 if (mr_errcode) return(mr_errcode);
758 return ((sqlca.sqlerrd[2] == 0) ? MR_NO_MATCH : MR_SUCCESS);
761 build_sql_stmt(result_buf,cmd,targetlist,argv,qual)
768 char fmt_buf[MR_STMTBUF_LEN];
770 char *res=result_buf, *tmp=tmp_buf, *fmt=fmt_buf;
773 sprintf(fmt_buf,"%s %s",cmd,targetlist);
774 if(qual) { strcat(fmt_buf," WHERE "); strcat(fmt_buf,qual); }
776 for(i=0,state=0;*fmt != '\0';fmt++) {
779 if(*fmt=='%') { /* formatting -> tmp */
782 } else *res++=*fmt; /* text -> res */
785 if((*fmt=='%') && (tmp==tmp_buf+1)) { /* %% -> % */
789 } else if(isalpha(*fmt) && (*fmt!='h') && (*fmt!='l')) { /* end of formatting */
793 sprintf(res,tmp_buf,*argv++); /* print to result buffer */
796 } else *tmp++=*fmt; /* keep copying the formatting to tmp */
803 do_update(q, argv, qual, action, actarg)
804 register struct query *q;
810 build_sql_stmt(stmt_buf,"UPDATE",q->tlist,argv,qual);
811 EXEC SQL EXECUTE IMMEDIATE stmt_buf;
812 if (mr_errcode) return(mr_errcode);
816 do_append(q, argv, pqual, action, actarg)
817 register struct query *q;
823 build_sql_stmt(stmt_buf,"INSERT",q->tlist,argv,pqual);
824 EXEC SQL EXECUTE IMMEDIATE stmt_buf;
825 if (mr_errcode) return(mr_errcode);
829 do_delete(q, qual, action, actarg)
830 register struct query *q;
835 sprintf(stmt_buf,"DELETE FROM %s WHERE %s",q->rtable,qual);
836 EXEC SQL EXECUTE IMMEDIATE :stmt_buf;
837 if (mr_errcode) return(mr_errcode);
843 ** set_next_object_id - set next object id in values table
845 ** Inputs: object - object name in values table and in objects
846 ** table - name of table objects are found in
847 ** limit - should the ID be range limited
849 ** - called before an APPEND operation to set the next object id to
850 ** be used for the new record to the next free value
854 set_next_object_id(object, table_name, limit)
855 EXEC SQL BEGIN DECLARE SECTION;
859 EXEC SQL END DECLARE SECTION;
861 EXEC SQL BEGIN DECLARE SECTION;
863 EXEC SQL END DECLARE SECTION;
865 EXEC SQL SELECT value INTO :value FROM numvalues WHERE name = :object;
866 if (sqlca.sqlerrd[2] != 1)
870 /* retrieve (exists = any(tbl.name where tbl.name = value)) */
872 /* sprintf(buf,"SELECT name INTO :dummy FROM %s WHERE name=:value",table_name); */
873 EXEC SQL SELECT name INTO :value FROM :table_name WHERE name = :value; /** */
874 if (sqlca.sqlerrd[2] != 1)
876 while (sqlca.sqlerrd[2] != 0) {
878 if (limit && value > MAX_ID_VALUE)
879 value = MIN_ID_VALUE;
881 /* retrieve (exists = any(tbl.name where tbl.name = value)) */
883 EXEC SQL SELECT name INTO :value FROM :table_name WHERE name = :value; /** */
887 com_err(whoami, 0, "setting ID %s to %d", name, value);
888 EXEC SQL UPDATE numvalues SET value = :value WHERE name = :object;
893 /* Turn a kerberos name into the user's ID of the account that principal
894 * owns. Sets the kerberos ID and user ID.
897 int set_krb_mapping(name, login, ok, kid, uid)
904 EXEC SQL BEGIN DECLARE SECTION;
907 EXEC SQL END DECLARE SECTION;
913 EXEC SQL SELECT k.users_id, k.string_id INTO :u_id, :k_id
914 FROM krbmap k, strings s
915 WHERE k.string_id = s.string_id AND s.string = :krbname;
917 if (ingres_errno) return(mr_errcode);
919 if (sqlca.sqlerrd[2] == 1) { /* rowcount */
925 if (name_to_id(name, "STRINGS", &k_id) == MR_SUCCESS)
933 if (name_to_id(login, "USERS", uid) != MR_SUCCESS)
938 if (ingres_errno) return(mr_errcode);
943 /* For now this just checks the argc's. It should also see that there
944 * are no duplicate names.
947 sanity_check_queries()
950 int maxv = 0, maxa = 0;
951 #ifdef MULTIPROTOCOLS
952 extern int QueryCount1, QueryCount2;
953 extern struct query Queries1[], Queries2[];
955 extern int QueryCount2;
956 extern struct query Queries2[];
957 #endif MULTIPROTOCOLS
959 #define MAX(x,y) ((x) > (y) ? (x) : (y))
961 #ifdef MULTIPROTOCOLS
962 for (i = 0; i < QueryCount1; i++) {
963 maxv = MAX(maxv, Queries1[i].vcnt);
964 maxa = MAX(maxa, Queries1[i].argc);
966 #endif MULTIPROTOCOLS
967 for (i = 0; i < QueryCount2; i++) {
968 maxv = MAX(maxv, Queries2[i].vcnt);
969 maxa = MAX(maxa, Queries2[i].argc);
971 if (MAX(maxv, maxa) > QMAXARGS) {
972 com_err(whoami, 0, "A query has more args than QMAXARGS");