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_INT
47 #define INGRES_TIMEOUT
48 #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) {
64 * case INGRES_BAD_INT:
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.
77 * case INGRES_TIMEOUT:
78 * mr_errcode = MR_BUSY;
79 * com_err(whoami, 0, "timed out getting lock");
82 /* This probably has no equivalent.
83 * case INGRES_NO_RANGE:
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", sqlca.sqlcode);
92 critical_alert("MOIRA", "Moira server encountered INGRES ERROR %d", ingres_errno);
96 /* This is declarative, not executed. Applies from here on, in the file */
97 EXEC SQL WHENEVER SQLERROR CALL ingerr;
99 int mr_open_database()
103 MR_SQLDA_T *mr_alloc_SQLDA();
104 static first_open = 1;
109 /* initialize local argv */
110 for (i = 0; i < 16; i++)
111 Argv[i] = malloc(ARGLEN);
113 SQLDA = mr_alloc_SQLDA();
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) {
377 EXEC SQL COMMIT WORK;
379 if (status == MR_SUCCESS) {
380 EXEC SQL COMMIT WORK;
386 fprintf(journal, "%% %s %s %s",
387 cl->clname, cl->entity, ctime(&now));
388 fprintf(journal, "%s[%d] ", q->name, cl->args->mr_version_no);
389 for (i = 0; i < argc; i++) {
393 requote(buf, argv_ro[i], sizeof(buf));
399 incremental_update();
402 if (ingres_errno != INGRES_DEADLOCK) {
403 EXEC SQL ROLLBACK WORK;
408 cache_commit(); /* commit following abort is safe */
410 if (status != MR_SUCCESS && log_flags & LOG_RES)
411 com_err(whoami, status, " (Query failed)");
415 build_qual(fmt, argc, argv, qual)
427 for (i = 0; i < argc; i++) {
429 if (c++ == (char *)0) return(MR_ARGS);
433 *(int *)&args[i] = *(int *)argv[i]; /* sigh */
435 return(MR_INGRES_ERR);
437 if (c = index(c, '%')) {
438 args[i] = args[i - 1];
447 sprintf(qual, fmt, args[0]);
451 sprintf(qual, fmt, args[0], args[1]);
455 sprintf(qual, fmt, args[0], args[1], args[2]);
459 sprintf(qual, fmt, args[0], args[1], args[2], args[3]);
463 com_err(whoami, MR_INTERNAL,
464 "Internal arg count error processing query");
472 register struct validate *v;
475 register struct valobj *vo;
484 if (vo->type == V_SORT) {
485 sprintf(elem, "%d", vo->index + 1); /* Result column number */
486 if (*sort) strcat(sort, ", ");
492 return ((*sort) ? sort : 0);
496 /* Build arguement vector, verify query and arguments */
498 mr_verify_query(cl, q, argc, argv_ro)
506 register struct validate *v = q->validate;
508 register int privileged = 0;
511 /* copy the arguments into a local argv that we can modify */
512 if (argc >= QMAXARGS)
514 for (i = 0; i < argc; i++) {
515 if ((len = strlen(argv_ro[i])) < ARGLEN)
516 strcpy(Argv[i], argv_ro[i]);
518 return(MR_ARG_TOO_LONG);
519 if (Argv[i][len-1] == '\\')
523 /* check initial query access */
524 status = check_query_access(q, Argv, cl);
525 if (status != MR_SUCCESS && status != MR_PERM)
527 if (status == MR_SUCCESS)
530 /* check argument count */
532 if (q->type == UPDATE || q->type == APPEND) argreq += q->vcnt;
533 if (argc != argreq) return(MR_ARGS);
535 /* validate arguments */
536 if (v && v->valobj) {
537 status = validate_fields(q, Argv, v->valobj, v->objcnt);
538 if (status != MR_SUCCESS) return(status);
541 /* perform special query access check */
542 if (!privileged && v && v->acs_rtn) {
543 status = (*v->acs_rtn)(q, Argv, cl);
544 if (status != MR_SUCCESS && status != MR_PERM)
546 if (status == MR_SUCCESS)
550 return(privileged ? MR_SUCCESS : MR_PERM);
554 /* This routine caches info from the database. Each query acl is stored
555 * in the query structure, and whether that acl contains everybody.
558 check_query_access(q, argv, cl)
563 EXEC SQL BEGIN DECLARE SECTION;
569 EXEC SQL END DECLARE SECTION;
574 /* initialize default uid */
576 EXEC SQL SELECT users_id INTO :def_uid FROM users WHERE login='default';
579 /* get query access control list */
584 EXEC SQL SELECT list_id INTO :acl_id FROM capacls WHERE tag = :name;
585 if (sqlca.sqlcode < 0) return(MR_INGRES_ERR);
586 if (sqlca.sqlcode == 100) return(MR_PERM);
589 /* check for default access */
590 EXEC SQL SELECT member_id INTO :acl_id FROM imembers
591 WHERE list_id = :acl_id AND member_type = 'USER'
592 AND member_id = :def_uid;
593 if (sqlca.sqlerrd[2] == 0)
602 if (get_client(cl, &client_type, &client_id) != MR_SUCCESS)
604 if (find_member("LIST", acl_id, client_type, client_id, 0))
611 get_client(cl, client_type, client_id)
616 if (cl->users_id > 0) {
617 *client_id = cl->users_id;
618 *client_type = "USER";
622 if (cl->client_id < 0) {
623 *client_id = -cl->users_id;
624 *client_type = "KERBEROS";
631 find_member(list_type, list_id, member_type, member_id)
633 EXEC SQL BEGIN DECLARE SECTION;
637 EXEC SQL END DECLARE SECTION;
639 EXEC SQL BEGIN DECLARE SECTION;
641 EXEC SQL END DECLARE SECTION;
643 if (!strcmp(strtrim(list_type), strtrim(member_type)) &&
644 list_id == member_id)
647 /* see if client is a direct member of list */
649 EXEC SQL SELECT member_id INTO :flag FROM imembers
650 WHERE list_id = :list_id AND member_type = :member_type
651 AND member_id = :member_id;
652 if(flag!=0) flag=1; /** Not strictly necessary */
653 if (sqlca.sqlcode == 0)
659 do_retrieve(q, pqual, psort, action, actarg)
660 register struct query *q;
661 EXEC SQL BEGIN DECLARE SECTION;
664 EXEC SQL END DECLARE SECTION;
668 static char **vaddrs = (char **)NULL;
674 if ((vaddrs = (char **)malloc(sizeof(char *) * QMAXARGS)) == NULL) {
675 com_err(whoami, MR_NO_MEM, "setting up static argv");
678 for (i = 0; i < QMAXARGS; i++) {
679 vaddrs[i]=SQLDA->sqlvar[i].sqldata;
688 * retrieve unique (param (q->tlist, vaddrs)) where cqual
691 * (*action)(q->vcnt, vaddrs, actarg);
694 * retrieve unique (param (q->tlist, vaddrs))
697 * (*action)(q->vcnt, vaddrs, actarg);
704 * retrieve unique (param (q->tlist, vaddrs)) where cqual
706 * (*action)(q->vcnt, vaddrs, actarg);
709 * retrieve unique (param (q->tlist, vaddrs))
711 * (*action)(q->vcnt, vaddrs, actarg);
717 build_sql_stmt(stmt_buf,"SELECT",q->tlist,vaddrs,pqual);
718 if(psort) { strcat(stmt_buf," ORDER BY "); strcat(stmt_buf,psort); }
719 EXEC SQL PREPARE stmt INTO :SQLDA USING NAMES FROM :stmt_buf;
720 if((mr_errcode=mr_check_SQLDA(SQLDA)) != MR_SUCCESS)
722 EXEC SQL DECLARE csr001 CURSOR FOR stmt;
723 EXEC SQL OPEN csr001;
726 EXEC SQL FETCH csr001 USING DESCRIPTOR :SQLDA;
727 if(sqlca.sqlcode != 0) break;
728 mr_fix_nulls_in_SQLDA(SQLDA);
729 (*action)(q->vcnt, vaddrs, actarg);
732 EXEC SQL CLOSE csr001;
734 if (mr_errcode) return(mr_errcode);
735 return ((rowcount == 0) ? MR_NO_MATCH : MR_SUCCESS);
738 build_sql_stmt(result_buf,cmd,targetlist,argv,qual)
745 char fmt_buf[MR_STMTBUF_LEN];
747 char *res=result_buf, *tmp=tmp_buf, *fmt=fmt_buf;
750 sprintf(fmt_buf,"%s %s",cmd,targetlist);
751 if(qual) { strcat(fmt_buf," WHERE "); strcat(fmt_buf,qual); }
753 for(state=0;*fmt != '\0';fmt++) {
756 if(*fmt=='%') { /* formatting -> tmp */
759 } else *res++ = *fmt; /* text -> res */
762 if((*fmt=='%') && (tmp==tmp_buf+1)) { /* %% -> % */
766 } else if(isalpha(*fmt) && (*fmt!='h') && (*fmt!='l')) { /* end of formatting */
770 sprintf(res,tmp_buf,*argv++); /* print to result buffer */
773 } else *tmp++ = *fmt; /* keep copying the formatting to tmp */
780 do_update(q, argv, qual, action, actarg)
781 register struct query *q;
787 build_sql_stmt(stmt_buf,"UPDATE",q->tlist,argv,qual);
788 EXEC SQL EXECUTE IMMEDIATE :stmt_buf;
789 if (mr_errcode) return(mr_errcode);
793 do_append(q, argv, pqual, action, actarg)
794 register struct query *q;
800 build_sql_stmt(stmt_buf,"INSERT",q->tlist,argv,pqual);
801 EXEC SQL EXECUTE IMMEDIATE :stmt_buf;
802 if (mr_errcode) return(mr_errcode);
806 do_delete(q, qual, action, actarg)
807 register struct query *q;
812 sprintf(stmt_buf,"DELETE FROM %s WHERE %s",q->rtable,qual);
813 EXEC SQL EXECUTE IMMEDIATE :stmt_buf;
814 if (mr_errcode) return(mr_errcode);
820 ** set_next_object_id - set next object id in values table
822 ** Inputs: object - object name in values table and in objects
823 ** table - name of table objects are found in
824 ** limit - should the ID be range limited
826 ** - called before an APPEND operation to set the next object id to
827 ** be used for the new record to the next free value
831 set_next_object_id(object, table_name, limit)
832 EXEC SQL BEGIN DECLARE SECTION;
836 EXEC SQL END DECLARE SECTION;
838 EXEC SQL BEGIN DECLARE SECTION;
840 EXEC SQL END DECLARE SECTION;
843 EXEC SQL SELECT value INTO :value FROM numvalues WHERE name = :object;
844 if (sqlca.sqlerrd[2] != 1)
848 * retrieve (exists = any(tbl.name where tbl.name = value))
850 sprintf(stmt_buf,"SELECT %s FROM %s WHERE %s=:value",object,table_name,object); /** Will this work??? */
851 EXEC SQL PREPARE stmt INTO :SQLDA USING NAMES FROM :stmt_buf;
852 EXEC SQL DECLARE csr002 CURSOR FOR stmt;
854 EXEC SQL OPEN csr002;
855 EXEC SQL FETCH csr002 USING DESCRIPTOR :SQLDA;
856 if(sqlca.sqlcode == 0) {
858 EXEC SQL FETCH csr002 USING DESCRIPTOR :SQLDA;
859 if(sqlca.sqlcode == 0) rowcount++;
861 EXEC SQL CLOSE csr002;
867 if (limit && value > MAX_ID_VALUE) /* Potential infinite loop */
868 value = MIN_ID_VALUE;
870 * retrieve (exists = any(tbl.name where tbl.name = value))
873 /** Does the following work like I think it should ??? */
874 EXEC SQL OPEN csr002;
875 EXEC SQL FETCH csr002 USING DESCRIPTOR :SQLDA;
876 if(sqlca.sqlcode == 100) break;
877 EXEC SQL CLOSE csr002;
879 EXEC SQL CLOSE csr002;
882 com_err(whoami, 0, "setting ID %s to %d", object, value);
883 EXEC SQL UPDATE numvalues SET value = :value WHERE name = :object;
888 /* Turn a kerberos name into the user's ID of the account that principal
889 * owns. Sets the kerberos ID and user ID.
892 int set_krb_mapping(name, login, ok, kid, uid)
899 EXEC SQL BEGIN DECLARE SECTION;
902 EXEC SQL END DECLARE SECTION;
908 EXEC SQL SELECT k.users_id, k.string_id INTO :u_id, :k_id
909 FROM krbmap k, strings s
910 WHERE k.string_id = s.string_id AND s.string = :krbname;
912 if (ingres_errno) return(mr_errcode);
914 if (sqlca.sqlerrd[2] == 1) { /* rowcount */
920 if (name_to_id(name, "STRINGS", &k_id) == MR_SUCCESS)
928 if (name_to_id(login, "USERS", uid) != MR_SUCCESS)
933 if (ingres_errno) return(mr_errcode);
938 /* For now this just checks the argc's. It should also see that there
939 * are no duplicate names.
942 sanity_check_queries()
945 int maxv = 0, maxa = 0;
946 #ifdef MULTIPROTOCOLS
947 extern int QueryCount1, QueryCount2;
948 extern struct query Queries1[], Queries2[];
950 extern int QueryCount2;
951 extern struct query Queries2[];
952 #endif MULTIPROTOCOLS
954 #define MAX(x,y) ((x) > (y) ? (x) : (y))
956 #ifdef MULTIPROTOCOLS
957 for (i = 0; i < QueryCount1; i++) {
958 maxv = MAX(maxv, Queries1[i].vcnt);
959 maxa = MAX(maxa, Queries1[i].argc);
961 #endif MULTIPROTOCOLS
962 for (i = 0; i < QueryCount2; i++) {
963 maxv = MAX(maxv, Queries2[i].vcnt);
964 maxa = MAX(maxa, Queries2[i].argc);
966 if (MAX(maxv, maxa) > QMAXARGS) {
967 com_err(whoami, 0, "A query has more args than QMAXARGS");