3 * Query-processing routines
5 * Copyright (C) 1987-1998 by the Massachusetts Institute of Technology
6 * For copying and distribution information, please see the file
11 #include <mit-copyright.h>
12 #include "mr_server.h"
20 EXEC SQL INCLUDE sqlca; /* SQL Communications Area */
21 EXEC SQL INCLUDE sqlda; /* SQL Descriptor Area */
26 EXEC SQL BEGIN DECLARE SECTION;
28 char stmt_buf[MR_STMTBUF_LEN];
29 EXEC SQL END DECLARE SECTION;
32 extern char *table_name[];
33 extern char *sqlbuffer[QMAXARGS];
37 EXEC SQL BEGIN DECLARE SECTION;
38 int query_timeout = 30;
39 char *database = "moira";
40 EXEC SQL END DECLARE SECTION;
44 int mr_verify_query(client *cl, struct query *q, int argc, char *argv_ro[]);
45 int do_retrieve(struct query *q, char *pqual, char *psort,
46 int (*action)(int, char *[], void *), void *actarg);
47 int do_update(struct query *q, char *argv[], char *qual,
48 int (*action)(int, char *[], void *), void *actarg);
49 int do_append(struct query *q, char *argv[], char *pqual,
50 int (*action)(int, char *[], void *), void *actarg);
51 int do_delete(struct query *q, char *qual,
52 int (*action)(int, char *[], void *), void *actarg);
53 void build_sql_stmt(char *result_buf, char *cmd, char *targetlist,
54 char *argv[], char *qual);
55 char *build_sort(struct validate *v, char *sort);
56 char *sqlstrstr(char *str, char *pat);
57 void optimize_sql_stmt(char *buf);
59 SQLDA *mr_alloc_sqlda(void);
60 void sqlglm(char *, int *, int *);
63 * dbmserr: Called when the DBMS indicates an error.
68 EXEC SQL BEGIN DECLARE SECTION;
70 EXEC SQL END DECLARE SECTION;
71 int bufsize = 256, msglength = 0;
73 dbms_errno = -sqlca.sqlcode;
74 mr_errcode = MR_DBMS_ERR;
75 com_err(whoami, MR_DBMS_ERR, " code %d\n", dbms_errno);
76 sqlglm(err_msg, &bufsize, &msglength);
77 err_msg[msglength] = 0;
78 com_err(whoami, 0, "SQL error text = %s", err_msg);
79 critical_alert("MOIRA", "Moira server encountered DBMS ERROR %d\n%s",
83 /* This is declarative, not executed. Applies from here on, in this file. */
84 EXEC SQL WHENEVER SQLERROR DO dbmserr();
86 int mr_open_database(void)
89 static first_open = 1;
95 /* initialize local argv */
96 for (i = 0; i < 16; i++)
97 Argv[i] = xmalloc(MAX_FIELD_WIDTH);
99 mr_sqlda = mr_alloc_sqlda();
108 /* open the database */
109 EXEC SQL CONNECT :database IDENTIFIED BY :database;
114 EXEC SQL SELECT data_length INTO :mr_sig_length FROM user_tab_columns
115 WHERE table_name = 'USERS' and column_name = 'SIGNATURE';
116 EXEC SQL COMMIT WORK;
123 void mr_close_database(void)
126 EXEC SQL COMMIT RELEASE;
129 int mr_check_access(client *cl, char *name, int argc, char *argv_ro[])
136 q = get_query_by_name(name);
140 return mr_verify_query(cl, q, argc, argv_ro);
143 int mr_process_query(client *cl, char *name, int argc, char *argv_ro[],
144 int (*action)(int, char *[], void *), void *actarg)
153 EXEC SQL BEGIN DECLARE SECTION;
155 EXEC SQL END DECLARE SECTION;
156 struct save_queue *sq;
161 /* list queries command */
162 if (!strcmp(name, "_list_queries"))
164 list_queries(action, actarg);
168 /* help query command */
169 if (!strcmp(name, "_help"))
173 q = get_query_by_name(argv_ro[0]);
176 help_query(q, action, actarg);
180 /* get query structure, return error if named query does not exist */
181 q = get_query_by_name(name);
186 /* setup argument vector, verify access and arguments */
187 if ((status = mr_verify_query(cl, q, argc, argv_ro)) != MR_SUCCESS)
190 /* perform any special query pre-processing */
193 status = (*v->pre_rtn)(q, Argv, cl);
194 if (status != MR_SUCCESS)
201 /* for queries that do not permit wildcarding, check if row
205 status = validate_row(q, Argv, v);
206 if (status != MR_EXISTS)
210 /* build "where" clause if needed */
213 build_qual(q->qual, q->argc, Argv, qual);
219 /* build "sort" clause if needed */
221 psort = build_sort(v, sort);
225 /* if there is a followup routine, then we must save the results */
226 /* of the first query for use by the followup routine */
227 /* if q->rvar = NULL, perform post_rtn only */
230 if (v && v->post_rtn)
233 status = do_retrieve(q, pqual, psort, sq_save_args, sq);
234 if (status != MR_SUCCESS)
239 status = (*v->post_rtn)(q, sq, v, action, actarg, cl);
243 /* normal retrieve */
244 status = do_retrieve(q, pqual, psort, action, actarg);
246 if (status != MR_SUCCESS)
250 status = (*v->post_rtn)(q, Argv, cl, action, actarg);
255 /* see if row already exists */
258 status = validate_row(q, Argv, v);
259 if (status != MR_EXISTS)
263 /* build "where" clause and perform update */
264 /* if q->rvar = NULL, perform post_rtn only */
267 build_qual(q->qual, q->argc, Argv, qual);
268 incremental_before(q->rtable, qual, argv_ro);
269 status = do_update(q, &Argv[q->argc], qual, action, actarg);
270 incremental_after(q->rtable, qual, argv_ro);
271 if (status != MR_SUCCESS)
273 flush_name(argv_ro[0], q->rtable);
274 table = table_name[q->rtable];
275 if (strcmp(q->shortname, "sshi") && strcmp(q->shortname, "ssif"))
277 EXEC SQL UPDATE tblstats
278 SET updates = updates + 1, modtime = SYSDATE
279 WHERE table_name = :table;
283 /* execute followup routine (if any) */
285 status = (*v->post_rtn)(q, Argv, cl);
290 /* see if row already exists */
293 status = validate_row(q, Argv, v);
294 if (status != MR_NO_MATCH)
298 /* build "where" clause if needed */
301 build_qual(q->qual, q->argc, Argv, qual);
307 /* perform the append */
308 /* if q->rvar = NULL, perform post_rtn only */
311 incremental_clear_before();
312 status = do_append(q, &Argv[q->argc], pqual, action, actarg);
313 if (status != MR_SUCCESS)
315 if (v && v->object_id)
317 sprintf(qual, "%s.%s = %s", q->rvar, v->object_id,
318 Argv[q->argc + q->vcnt]);
319 incremental_after(q->rtable, qual, argv_ro);
322 incremental_after(q->rtable, pqual, argv_ro);
324 table = table_name[q->rtable];
325 EXEC SQL UPDATE tblstats
326 SET appends = appends + 1, modtime = SYSDATE
327 WHERE table_name = :table;
330 /* execute followup routine */
332 status = (*v->post_rtn)(q, Argv, cl);
336 /* see if row already exists */
339 status = validate_row(q, Argv, v);
340 if (status != MR_EXISTS)
344 /* build "where" clause and perform delete */
345 /* if q->rvar = NULL, perform post_rtn only */
348 build_qual(q->qual, q->argc, Argv, qual);
349 table = table_name[q->rtable];
350 incremental_before(q->rtable, qual, argv_ro);
351 status = do_delete(q, qual, action, actarg);
352 incremental_clear_after();
353 if (status != MR_SUCCESS)
355 flush_name(argv_ro[0], q->rtable);
356 EXEC SQL UPDATE tblstats
357 SET deletes = deletes + 1, modtime = SYSDATE
358 WHERE table_name = :table;
361 /* execute followup routine */
363 status = (*v->post_rtn)(q, Argv, cl);
371 if (status == MR_SUCCESS && dbms_errno != 0)
373 com_err(whoami, MR_INTERNAL, "Server didn't notice DBMS ERROR %d",
378 if (q->type == RETRIEVE)
379 EXEC SQL COMMIT WORK;
382 if (status == MR_SUCCESS)
384 EXEC SQL COMMIT WORK;
391 fprintf(journal, "%% %s %s %s",
392 cl->clname, cl->entity, ctime(&now));
393 fprintf(journal, "%s ", q->name);
394 for (i = 0; i < argc; i++)
398 buf = requote(argv_ro[i]);
405 incremental_update();
410 EXEC SQL ROLLBACK WORK;
414 cache_commit(); /* commit following abort is safe */
416 if (status != MR_SUCCESS)
417 com_err(whoami, status, " (Query failed)");
421 void build_qual(char *fmt_buf, int argc, char *argv[], char *qual)
425 for (res = qual, fmt = fmt_buf; *fmt; fmt++)
433 case '%': /* %% -> % */
443 *res++ = '\''; /* double the ' */
450 res += sprintf(res, "%d", *(int *)*argv++);
452 default: /* Swallow other %? pairs */
460 *res++ = *fmt; /* text -> result buffer */
465 char *build_sort(struct validate *v, char *sort)
477 if (vo->type == V_SORT)
479 sprintf(elem, "%d", vo->index + 1); /* Result column number */
487 return (*sort) ? sort : 0;
491 /* Build arguement vector, verify query and arguments */
495 int mr_verify_query(client *cl, struct query *q, int argc, char *argv_ro[])
499 struct validate *v = q->validate;
501 char *to, *fr, *stop;
505 /* check argument count */
507 if (q->type == UPDATE || q->type == APPEND)
512 /* copy the arguments into a local argv that we can modify */
513 for (i = 0; i < argc; i++)
515 for (to = Argv[i], fr = argv_ro[i], stop = to + MAX_FIELD_WIDTH; (*fr) && (to < stop);)
519 return MR_ARG_TOO_LONG;
522 if (to > Argv[i] && *--to == '\\')
526 /* check initial query access */
527 status = check_query_access(q, Argv, cl);
528 if (status != MR_SUCCESS && status != MR_PERM)
530 if (status == MR_SUCCESS)
533 /* validate arguments */
536 status = validate_fields(q, Argv, v->valobj, v->objcnt);
537 if (status != MR_SUCCESS)
541 /* perform special query access check */
542 if (!privileged && v && v->acs_rtn)
544 status = (*v->acs_rtn)(q, Argv, cl);
545 if (status != MR_SUCCESS && status != MR_PERM)
547 if (status == MR_SUCCESS)
551 return privileged ? MR_SUCCESS : MR_PERM;
555 /* This routine caches info from the database. Each query acl is stored
556 * in the query structure, and whether that acl contains everybody.
559 int check_query_access(struct query *q, char *argv[], client *cl)
561 EXEC SQL BEGIN DECLARE SECTION;
565 EXEC SQL END DECLARE SECTION;
567 /* initialize default uid */
569 EXEC SQL SELECT users_id INTO :def_uid FROM users WHERE login = 'default';
571 /* get query access control list */
577 EXEC SQL SELECT list_id INTO :acl_id FROM capacls WHERE tag = :name;
578 if (sqlca.sqlcode < 0)
580 if (sqlca.sqlcode == SQL_NO_MATCH)
584 /* check for default access */
585 EXEC SQL SELECT member_id INTO :acl_id FROM imembers
586 WHERE list_id = :acl_id AND member_type = 'USER'
587 AND member_id = :def_uid;
588 if (sqlca.sqlerrd[2] == 0)
597 if (find_member("LIST", acl_id, cl))
604 int find_member(char *list_type, int list_id, client *cl)
606 EXEC SQL BEGIN DECLARE SECTION;
607 int flag, users_id, client_id;
608 EXEC SQL END DECLARE SECTION;
610 if (!strcmp(strtrim(list_type), "USER") && list_id == cl->users_id)
613 if (!strcmp(strtrim(list_type), "KERBEROS") && list_id == -cl->client_id)
616 /* see if client is a member of list */
618 users_id = cl->users_id;
619 client_id = -cl->client_id;
620 EXEC SQL SELECT COUNT(member_id) INTO :flag FROM imembers
621 WHERE list_id = :list_id
622 AND ( ( member_type = 'USER' AND member_id = :users_id )
623 OR (member_type = 'KERBEROS' AND member_id = :client_id ) );
624 if (sqlca.sqlcode == 0)
630 int do_retrieve(struct query *q, char *pqual, char *psort,
631 int (*action)(int, char *[], void *), void *actarg)
633 build_sql_stmt(stmt_buf, "SELECT", q->tlist, NULL, pqual);
636 strcat(stmt_buf, " ORDER BY ");
637 strcat(stmt_buf, psort);
640 return do_for_all_rows(stmt_buf, q->vcnt, action, actarg);
643 char *sqlstrstr(char *str, char *pat)
649 if (*str == '\'') /* Skip over single-quote delimited substrings */
651 while (*++str && (*str != '\''))
659 while (*++p && (*++s == *p))
662 p = pat; /* failed */
665 while (*p && *++str);
672 void optimize_sql_stmt(char *buf)
674 char *point = buf, *pat, *eopat, *esc1, *esc2, *csr;
676 for (point = buf; (point = sqlstrstr(point, "LIKE")); point++)
678 /* Now pointing to string "LIKE" */
680 /* Look at next word */
681 for (pat = point + 4; *pat == ' '; pat++)
684 /* Is it a single-quote delimited string? */
688 /* look for "escape" clause - save escape character */
689 /* 1. Find end of pattern */
690 for (eopat = pat + 1; 1; eopat++)
694 if (eopat[1] == '\'') /* single-quote is self-escaping */
701 /* 2. Look at next word */
702 for (esc1 = eopat; *++esc1 == ' ';)
705 /* 3. esc1 = 0 if not "ESCAPE '?'", where the ? may be any character. */
706 if (strncmp(esc1, "ESCAPE", 6))
711 for (esc2 = esc1 + 6; *esc2 == ' '; esc2++)
715 continue; /* Bad SQL syntax. Skip. */
716 /* esc2 now points at the escape character itself */
718 continue; /* Weird escape string. Skip. */
723 /* Is pattern free from special characters? */
724 for (csr = pat; csr < eopat; csr++)
726 if ((*csr == '%') || (*csr == '_') || (*csr == *esc2))
730 continue; /* Uses pattern matching. Skip. */
732 /* Optimize the query statement */
733 /* 1. Change "LIKE" to " = " */
734 memcpy(point, " = ", 4);
736 /* 2. Change "ESCAPE" to " " */
739 memset(esc1, ' ', 6);
740 /* 3. Change "'*'" to " " */
741 /* (Changes '''' to " ") */
743 memset(esc2 - 1, ' ', (*esc2 == '\'') ? 4 : 3);
748 void build_sql_stmt(char *result_buf, char *cmd, char *targetlist,
749 char *argv[], char *qual)
751 char fmt_buf[MR_STMTBUF_LEN];
755 sprintf(fmt_buf, "%s %s WHERE %s", cmd, targetlist, qual);
757 sprintf(fmt_buf, "%s %s", cmd, targetlist);
759 for (res = result_buf, fmt = fmt_buf; *fmt; fmt++)
767 case '%': /* %% -> % */
777 *res++ = '\''; /* double the ' */
784 res += sprintf(res, "%d", *(int *)*argv++);
786 default: /* Swallow other %? pairs */
794 *res++ = *fmt; /* text -> result buffer */
798 optimize_sql_stmt(result_buf);
801 int do_update(struct query *q, char *argv[], char *qual,
802 int (*action)(int, char *[], void *), void *actarg)
804 build_sql_stmt(stmt_buf, "UPDATE", q->tlist, argv, qual);
805 EXEC SQL EXECUTE IMMEDIATE :stmt_buf;
811 int do_append(struct query *q, char *argv[], char *pqual,
812 int (*action)(int, char *[], void *), void *actarg)
814 build_sql_stmt(stmt_buf, "INSERT", q->tlist, argv, pqual);
815 EXEC SQL EXECUTE IMMEDIATE :stmt_buf;
821 int do_delete(struct query *q, char *qual,
822 int (*action)(int, char *[], void *), void *actarg)
824 sprintf(stmt_buf, "DELETE FROM %s WHERE %s", table_name[q->rtable], qual);
825 EXEC SQL EXECUTE IMMEDIATE :stmt_buf;
833 ** set_next_object_id - set next object id in values table
835 ** Inputs: object - object name in values table and in objects
836 ** table - name of table objects are found in
837 ** limit - should the ID be range limited
839 ** - called before an APPEND operation to set the next object id to
840 ** be used for the new record to the next free value
844 int set_next_object_id(char *object, enum tables table, int limit)
846 EXEC SQL BEGIN DECLARE SECTION;
849 EXEC SQL END DECLARE SECTION;
852 EXEC SQL SELECT value INTO :value FROM numvalues WHERE name = :obj;
853 if (sqlca.sqlerrd[2] != 1)
856 starting_value = value;
859 if (limit && value > MAX_ID_VALUE)
860 value = MIN_ID_VALUE;
862 sprintf(stmt_buf, "SELECT %s FROM %s WHERE %s = %d",
863 object, table_name[table], object, value);
865 if (sqlca.sqlcode < 0)
867 if (sqlca.sqlcode == SQL_NO_MATCH)
871 if (limit && value == starting_value)
873 com_err(whoami, 0, "All id values have been used");
878 com_err(whoami, 0, "setting ID %s to %d", object, value);
879 EXEC SQL UPDATE numvalues SET value = :value WHERE name = :obj;
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(char *name, char *login, int ok, int *kid, int *uid)
890 EXEC SQL BEGIN DECLARE SECTION;
893 EXEC SQL END DECLARE SECTION;
899 EXEC SQL SELECT km.users_id, km.string_id INTO :u_id, :k_id
900 FROM krbmap km, strings str
901 WHERE km.string_id = str.string_id AND str.string = :krbname;
902 EXEC SQL COMMIT WORK;
907 if (sqlca.sqlerrd[2] == 1)
914 if (name_to_id(name, STRINGS_TABLE, &k_id) == MR_SUCCESS)
923 if (name_to_id(login, USERS_TABLE, uid) != MR_SUCCESS)
934 /* For now this just checks the argc's. It should also see that there
935 * are no duplicate names.
938 void sanity_check_queries(void)
941 int maxv = 0, maxa = 0;
942 extern int QueryCount2;
943 extern struct query Queries2[];
945 #define MAX(x, y) ((x) > (y) ? (x) : (y))
947 for (i = 0; i < QueryCount2; i++)
949 maxv = MAX(maxv, Queries2[i].vcnt);
950 maxa = MAX(maxa, Queries2[i].argc);
952 if (MAX(maxv, maxa) > QMAXARGS)
954 com_err(whoami, 0, "A query has more args than QMAXARGS");
960 /* Generically do a SELECT, storing the results in the provided buffers */
962 void dosql(char *buffers[])
964 int i, errcode = 0, errlen;
966 EXEC SQL PREPARE inc_stmt FROM :stmt_buf;
969 EXEC SQL DECLARE inc_crs CURSOR FOR inc_stmt;
970 EXEC SQL OPEN inc_crs;
971 mr_sqlda->N = QMAXARGS;
972 EXEC SQL DESCRIBE SELECT LIST FOR inc_stmt INTO mr_sqlda;
973 mr_sqlda->N = mr_sqlda->F;
974 for (i = 0; i < mr_sqlda->N; i++)
976 mr_sqlda->V[i] = buffers[i];
978 mr_sqlda->L[i] = MAX_FIELD_WIDTH;
980 EXEC SQL FETCH inc_crs USING DESCRIPTOR mr_sqlda;
982 /* if we got an error from the FETCH, we have to preserve it or the
983 close will reset it and the caller will think nothing happened */
986 errcode = sqlca.sqlcode;
987 errlen = sqlca.sqlerrm.sqlerrml;
990 EXEC SQL CLOSE inc_crs;
993 sqlca.sqlcode = errcode;
994 sqlca.sqlerrm.sqlerrml = errlen;
998 int do_for_all_rows(char *query, int count,
999 int (*action)(int, char *[], void *), void *actarg)
1001 int i, rowcount = 0;
1002 EXEC SQL BEGIN DECLARE SECTION;
1004 EXEC SQL END DECLARE SECTION;
1006 EXEC SQL PREPARE stmt FROM :q;
1009 EXEC SQL DECLARE curs CURSOR FOR stmt;
1011 mr_sqlda->N = count;
1012 EXEC SQL DESCRIBE SELECT LIST FOR stmt INTO mr_sqlda;
1013 mr_sqlda->N = mr_sqlda->F;
1014 for (i = 0; i < mr_sqlda->N; i++)
1016 mr_sqlda->V[i] = sqlbuffer[i];
1017 mr_sqlda->T[i] = 97;
1018 mr_sqlda->L[i] = MAX_FIELD_WIDTH;
1023 EXEC SQL FETCH curs USING DESCRIPTOR mr_sqlda;
1026 (*action)(count, sqlbuffer, actarg);
1029 EXEC SQL CLOSE curs;
1033 return (rowcount == 0) ? MR_NO_MATCH : MR_SUCCESS;