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;
27 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;
43 extern int QueryCount, max_version;
44 extern struct query Queries[];
46 /* Put this in a variable so that we can patch it if necessary */
47 int max_row_count = 4096;
49 int mr_verify_query(client *cl, struct query *q, int argc, char *argv_ro[]);
50 int do_retrieve(struct query *q, char *pqual,
51 int (*action)(int, char *[], void *), void *actarg);
52 int do_update(struct query *q, char *argv[], char *qual,
53 int (*action)(int, char *[], void *), void *actarg);
54 int do_append(struct query *q, char *argv[], char *pqual,
55 int (*action)(int, char *[], void *), void *actarg);
56 int do_delete(struct query *q, char *qual,
57 int (*action)(int, char *[], void *), void *actarg);
58 void build_sql_stmt(char *result_buf, char *cmd, char *targetlist,
59 char *argv[], char *qual);
61 SQLDA *mr_alloc_sqlda(void);
62 void sqlglm(char *, int *, int *);
65 * dbmserr: Called when the DBMS indicates an error.
70 EXEC SQL BEGIN DECLARE SECTION;
72 EXEC SQL END DECLARE SECTION;
73 int bufsize = 256, msglength = 0;
75 dbms_errno = -sqlca.sqlcode;
76 mr_errcode = MR_DBMS_ERR;
77 com_err(whoami, MR_DBMS_ERR, " code %d\n", dbms_errno);
78 sqlglm(err_msg, &bufsize, &msglength);
79 err_msg[msglength] = 0;
80 com_err(whoami, 0, "SQL error text = %s", err_msg);
81 critical_alert("MOIRA", "Moira server encountered DBMS ERROR %d\n%s",
85 /* This is declarative, not executed. Applies from here on, in this file. */
86 EXEC SQL WHENEVER SQLERROR DO dbmserr();
88 int mr_open_database(void)
91 static int first_open = 1;
97 /* initialize local argv */
98 for (i = 0; i < QMAXARGS; i++)
99 Argv[i] = xmalloc(MAX_FIELD_WIDTH);
101 mr_sqlda = mr_alloc_sqlda();
110 /* open the database */
111 EXEC SQL CONNECT :database IDENTIFIED BY :database;
116 EXEC SQL SELECT list_id INTO :proxy_acl FROM capacls
117 WHERE capability = 'proxy';
124 void mr_close_database(void)
127 EXEC SQL COMMIT RELEASE;
130 int mr_check_access(client *cl, char *name, int argc, char *argv_ro[])
137 q = get_query_by_name(name, cl->version);
141 return mr_verify_query(cl, q, argc, argv_ro);
144 int mr_process_query(client *cl, char *name, int argc, char *argv_ro[],
145 int (*action)(int, char *[], void *), void *actarg)
151 EXEC SQL BEGIN DECLARE SECTION;
153 EXEC SQL END DECLARE SECTION;
154 struct save_queue *sq;
159 /* list queries command */
160 if (!strcmp(name, "_list_queries"))
162 list_queries(cl, action, actarg);
166 /* help query command */
167 if (!strcmp(name, "_help"))
171 q = get_query_by_name(argv_ro[0], cl->version);
174 help_query(q, action, actarg);
178 /* get query structure, return error if named query does not exist */
179 q = get_query_by_name(name, cl->version);
184 /* setup argument vector, verify access and arguments */
185 if ((status = mr_verify_query(cl, q, argc, argv_ro)) != MR_SUCCESS)
188 /* perform any special query pre-processing */
191 status = (*v->pre_rtn)(q, Argv, cl);
192 if (status != MR_SUCCESS)
199 /* for queries that do not permit wildcarding, check if row
203 status = validate_row(q, Argv, v);
204 if (status != MR_EXISTS)
208 /* build "where" clause if needed */
210 qual = build_qual(q->qual, q->argc, Argv);
212 /* if there is a followup routine, then we must save the results */
213 /* of the first query for use by the followup routine */
214 /* if q->rvar = NULL, perform post_rtn only */
217 if (v && v->post_rtn)
220 status = do_retrieve(q, qual, sq_save_args, sq);
221 if (status != MR_SUCCESS)
226 while (sq_get_data(sq, &argv))
228 for (i = 0; i < q->vcnt; i++)
235 status = (*v->post_rtn)(q, sq, v, action, actarg, cl);
239 /* normal retrieve */
240 status = do_retrieve(q, qual, action, actarg);
242 if (status != MR_SUCCESS)
246 status = (*v->post_rtn)(q, Argv, cl, action, actarg);
251 /* see if row already exists */
254 status = validate_row(q, Argv, v);
255 if (status != MR_EXISTS)
259 /* build "where" clause and perform update */
260 /* if q->rvar = NULL, perform post_rtn only */
263 qual = build_qual(q->qual, q->argc, Argv);
264 incremental_before(q->rtable, qual, argv_ro);
265 status = do_update(q, &Argv[q->argc], qual, action, actarg);
266 incremental_after(q->rtable, qual, argv_ro);
267 if (status != MR_SUCCESS)
269 flush_name(argv_ro[0], q->rtable);
270 table = table_name[q->rtable];
271 if (strcmp(q->shortname, "sshi") && strcmp(q->shortname, "ssif"))
273 EXEC SQL UPDATE tblstats
274 SET updates = updates + 1, modtime = SYSDATE
275 WHERE table_name = :table;
279 /* execute followup routine (if any) */
281 status = (*v->post_rtn)(q, Argv, cl);
286 /* see if row already exists */
289 status = validate_row(q, Argv, v);
290 if (status != MR_NO_MATCH)
294 /* build "where" clause if needed */
296 qual = build_qual(q->qual, q->argc, Argv);
298 /* perform the append */
299 /* if q->rvar = NULL, perform post_rtn only */
302 incremental_clear_before();
303 status = do_append(q, &Argv[q->argc], qual, action, actarg);
304 if (status != MR_SUCCESS)
306 if (v && v->object_id)
308 qual = realloc(qual, 15 + strlen(q->rvar) +
309 strlen(Argv[q->argc + q->vcnt]));
310 sprintf(qual, "%s.%s = %s", q->rvar, v->object_id,
311 Argv[q->argc + q->vcnt]);
312 incremental_after(q->rtable, qual, argv_ro);
315 incremental_after(q->rtable, qual, argv_ro);
317 table = table_name[q->rtable];
318 EXEC SQL UPDATE tblstats
319 SET appends = appends + 1, modtime = SYSDATE
320 WHERE table_name = :table;
323 /* execute followup routine */
325 status = (*v->post_rtn)(q, Argv, cl);
329 /* see if row already exists */
332 status = validate_row(q, Argv, v);
333 if (status != MR_EXISTS)
337 /* build "where" clause and perform delete */
338 /* if q->rvar = NULL, perform post_rtn only */
341 qual = build_qual(q->qual, q->argc, Argv);
342 table = table_name[q->rtable];
343 incremental_before(q->rtable, qual, argv_ro);
344 status = do_delete(q, qual, action, actarg);
345 incremental_clear_after();
346 if (status != MR_SUCCESS)
348 flush_name(argv_ro[0], q->rtable);
349 EXEC SQL UPDATE tblstats
350 SET deletes = deletes + 1, modtime = SYSDATE
351 WHERE table_name = :table;
354 /* execute followup routine */
356 status = (*v->post_rtn)(q, Argv, cl);
366 if (status == MR_SUCCESS && dbms_errno != 0)
368 com_err(whoami, MR_INTERNAL, "Server didn't notice DBMS ERROR %d",
373 if (q->type == RETRIEVE)
374 EXEC SQL COMMIT WORK;
377 if (status == MR_SUCCESS)
379 EXEC SQL COMMIT WORK;
386 fprintf(journal, "%% %s %s %s",
387 cl->clname, cl->entity, ctime(&now));
388 fprintf(journal, "%s ", q->name);
389 for (i = 0; i < argc; i++)
393 buf = requote(argv_ro[i]);
400 incremental_update();
405 EXEC SQL ROLLBACK WORK;
409 cache_commit(); /* commit following abort is safe */
411 if (status != MR_SUCCESS)
412 com_err(whoami, status, " (Query failed)");
416 char *build_qual(char *fmt_buf, int argc, char *argv[])
418 char *res, *result_buf, *fmt, *arg, *like, *p;
420 result_buf = xmalloc(2 * (strlen(fmt_buf) + argc * ARGLEN));
425 like = strstr(fmt, "LIKE");
426 arg = strchr(fmt, '%');
428 /* Look through the format for LIKE expressions and arguments.
429 Substitute in the arguments, simplify the `LIKE's to `='s
430 where possible, and insert ESCAPE clauses where needed */
436 /* only plain text remains */
438 res = strchr(res, '\0');
441 else if (!like || arg < like)
443 /* regular arg: copy up to arg, then substitute */
444 strncpy(res, fmt, arg - fmt);
456 /* copy string, doubling single quotes */
467 res += sprintf(res, "%d", *(int *)*argv++);
472 arg = strchr(fmt, '%');
474 /* LIKE arg: copy over up to the arg, then copy and convert arg */
475 int escape = 0, pattern = 0;
476 char *likepos = res + (like - fmt);
478 strncpy(res, fmt, arg - fmt);
481 /* copy arg, converting UNIX globs to `SQL voodoo', and noting
482 if we'll need an ESCAPE clause */
483 for (p = *argv++; *p; p++)
489 *res++ = '%'; /* need to double for build_sql_stmt */
516 /* if no pattern characters, write over "LIKE" with " = " */
517 if (!pattern && !escape)
518 memcpy(likepos, " = ", 4);
521 while (*fmt && *fmt != ' ')
525 res += sprintf(res, " ESCAPE '*'");
527 arg = strchr(fmt, '%');
528 like = strstr(fmt, "LIKE");
533 result_buf = realloc(result_buf, strlen(result_buf) + 1);
537 /* Build arguement vector, verify query and arguments */
541 int mr_verify_query(client *cl, struct query *q, int argc, char *argv_ro[])
545 struct validate *v = q->validate;
547 char *to, *fr, *stop;
551 /* check argument count */
553 if (q->type == UPDATE || q->type == APPEND)
558 /* copy the arguments into a local argv that we can modify */
559 for (i = 0; i < argc; i++)
561 for (to = Argv[i], fr = argv_ro[i], stop = to + MAX_FIELD_WIDTH; (*fr) && (to < stop);)
565 return MR_ARG_TOO_LONG;
568 if (to > Argv[i] && *--to == '\\')
572 /* check initial query access, unless we're acting as a proxy */
575 status = check_query_access(q, Argv, cl);
576 if (status != MR_SUCCESS && status != MR_PERM)
578 if (status == MR_SUCCESS)
582 /* validate arguments */
585 status = validate_fields(q, Argv, v->valobj, v->objcnt);
586 if (status != MR_SUCCESS)
590 /* perform special query access check */
591 if (!privileged && v && v->acs_rtn)
593 status = (*v->acs_rtn)(q, Argv, cl);
594 if (status != MR_SUCCESS && status != MR_PERM)
596 if (status == MR_SUCCESS)
600 return privileged ? MR_SUCCESS : MR_PERM;
604 /* This routine caches info from the database. Each query acl is stored
605 * in the query structure, and whether that acl contains everybody.
608 int check_query_access(struct query *q, char *argv[], client *cl)
610 EXEC SQL BEGIN DECLARE SECTION;
614 EXEC SQL END DECLARE SECTION;
616 /* initialize default uid */
618 EXEC SQL SELECT users_id INTO :def_uid FROM users WHERE login = 'default';
620 /* get query access control list */
626 EXEC SQL SELECT list_id INTO :acl_id FROM capacls WHERE tag = :name;
627 if (sqlca.sqlcode < 0)
629 if (sqlca.sqlcode == SQL_NO_MATCH)
633 /* check for default access */
634 EXEC SQL SELECT member_id INTO :acl_id FROM imembers
635 WHERE list_id = :acl_id AND member_type = 'USER'
636 AND member_id = :def_uid;
637 if (sqlca.sqlerrd[2] == 0)
646 if (find_member("LIST", acl_id, cl))
652 int find_member(char *list_type, int list_id, client *cl)
654 EXEC SQL BEGIN DECLARE SECTION;
655 int flag, users_id, client_id;
656 EXEC SQL END DECLARE SECTION;
658 if (!strcmp(strtrim(list_type), "USER") && list_id == cl->users_id)
661 if (!strcmp(strtrim(list_type), "KERBEROS") && list_id == -cl->client_id)
664 /* see if client is a member of list */
666 users_id = cl->users_id;
667 client_id = -cl->client_id;
668 EXEC SQL SELECT COUNT(member_id) INTO :flag FROM imembers
669 WHERE list_id = :list_id
670 AND ( ( member_type = 'USER' AND member_id = :users_id )
671 OR (member_type = 'KERBEROS' AND member_id = :client_id ) );
672 if (sqlca.sqlcode == 0)
678 int do_retrieve(struct query *q, char *pqual,
679 int (*action)(int, char *[], void *), void *actarg)
681 build_sql_stmt(stmt_buf, "SELECT", q->tlist, NULL, pqual);
684 strcat(stmt_buf, " ORDER BY ");
685 strcat(stmt_buf, q->sort);
688 return do_for_all_rows(stmt_buf, q->vcnt, action, actarg);
691 void build_sql_stmt(char *result_buf, char *cmd, char *targetlist,
692 char *argv[], char *qual)
694 char fmt_buf[MR_STMTBUF_LEN];
698 sprintf(fmt_buf, "%s %s WHERE %s", cmd, targetlist, qual);
700 sprintf(fmt_buf, "%s %s", cmd, targetlist);
702 for (res = result_buf, fmt = fmt_buf; *fmt; fmt++)
710 case '%': /* %% -> % */
720 *res++ = '\''; /* double the ' */
727 res += sprintf(res, "%d", *(int *)*argv++);
729 default: /* Swallow other %? pairs */
737 *res++ = *fmt; /* text -> result buffer */
742 int do_update(struct query *q, char *argv[], char *qual,
743 int (*action)(int, char *[], void *), void *actarg)
745 build_sql_stmt(stmt_buf, "UPDATE", q->tlist, argv, qual);
746 EXEC SQL EXECUTE IMMEDIATE :stmt_buf;
752 int do_append(struct query *q, char *argv[], char *pqual,
753 int (*action)(int, char *[], void *), void *actarg)
755 build_sql_stmt(stmt_buf, "INSERT", q->tlist, argv, pqual);
756 EXEC SQL EXECUTE IMMEDIATE :stmt_buf;
762 int do_delete(struct query *q, char *qual,
763 int (*action)(int, char *[], void *), void *actarg)
765 sprintf(stmt_buf, "DELETE FROM %s WHERE %s", table_name[q->rtable], qual);
766 EXEC SQL EXECUTE IMMEDIATE :stmt_buf;
774 ** set_next_object_id - set next object id in values table
776 ** Inputs: object - object name in values table and in objects
777 ** table - name of table objects are found in
778 ** limit - should the ID be range limited
780 ** - called before an APPEND operation to set the next object id to
781 ** be used for the new record to the next free value
785 int set_next_object_id(char *object, enum tables table, int limit)
787 EXEC SQL BEGIN DECLARE SECTION;
790 EXEC SQL END DECLARE SECTION;
793 EXEC SQL SELECT value INTO :value FROM numvalues WHERE name = :obj;
794 if (sqlca.sqlerrd[2] != 1)
797 starting_value = value;
800 #ifdef ULTRIX_ID_HOLE
801 if (limit && value > 31999 && value < 32768)
804 if (limit && value > MAX_ID_VALUE)
805 value = MIN_ID_VALUE;
807 sprintf(stmt_buf, "SELECT %s FROM %s WHERE %s = %d",
808 object, table_name[table], object, value);
810 if (sqlca.sqlcode < 0)
812 if (sqlca.sqlcode == SQL_NO_MATCH)
816 if (limit && value == starting_value)
818 com_err(whoami, 0, "All id values have been used");
823 com_err(whoami, 0, "setting ID %s to %d", object, value);
824 EXEC SQL UPDATE numvalues SET value = :value WHERE name = :obj;
829 /* Turn a kerberos name into the user's ID of the account that principal
830 * owns. Sets the kerberos ID and user ID.
833 int set_krb_mapping(char *name, char *login, int ok, int *kid, int *uid)
835 EXEC SQL BEGIN DECLARE SECTION;
838 EXEC SQL END DECLARE SECTION;
844 EXEC SQL SELECT km.users_id, km.string_id INTO :u_id, :k_id
845 FROM krbmap km, strings str
846 WHERE km.string_id = str.string_id AND str.string = :krbname;
847 EXEC SQL COMMIT WORK;
852 if (sqlca.sqlerrd[2] == 1)
859 if (name_to_id(name, STRINGS_TABLE, &k_id) == MR_SUCCESS)
868 if (name_to_id(login, USERS_TABLE, uid) != MR_SUCCESS)
879 void sanity_check_queries(void)
882 int maxv = 0, maxa = 0;
884 #define MAX(x, y) ((x) > (y) ? (x) : (y))
886 for (i = 0; i < QueryCount; i++)
888 maxv = MAX(maxv, Queries[i].vcnt);
889 maxa = MAX(maxa, Queries[i].argc);
890 max_version = MAX(max_version, Queries[i].version);
892 if (MAX(maxv, maxa) > QMAXARGS)
894 com_err(whoami, 0, "A query has more args than QMAXARGS");
900 /* Generically do a SELECT, storing the results in the provided buffers */
902 void dosql(char *buffers[])
904 int i, errcode = 0, errlen;
906 EXEC SQL PREPARE inc_stmt FROM :stmt_buf;
909 EXEC SQL DECLARE inc_crs CURSOR FOR inc_stmt;
910 EXEC SQL OPEN inc_crs;
911 mr_sqlda->N = QMAXARGS;
912 EXEC SQL DESCRIBE SELECT LIST FOR inc_stmt INTO mr_sqlda;
913 mr_sqlda->N = mr_sqlda->F;
914 for (i = 0; i < mr_sqlda->N; i++)
916 mr_sqlda->V[i] = buffers[i];
918 mr_sqlda->L[i] = MAX_FIELD_WIDTH;
920 EXEC SQL FETCH inc_crs USING DESCRIPTOR mr_sqlda;
922 /* if we got an error from the FETCH, we have to preserve it or the
923 close will reset it and the caller will think nothing happened */
926 errcode = sqlca.sqlcode;
927 errlen = sqlca.sqlerrm.sqlerrml;
930 EXEC SQL CLOSE inc_crs;
933 sqlca.sqlcode = errcode;
934 sqlca.sqlerrm.sqlerrml = errlen;
938 int do_for_all_rows(char *query, int count,
939 int (*action)(int, char *[], void *), void *actarg)
942 EXEC SQL BEGIN DECLARE SECTION;
944 EXEC SQL END DECLARE SECTION;
946 EXEC SQL PREPARE stmt FROM :q;
949 EXEC SQL DECLARE curs CURSOR FOR stmt;
952 EXEC SQL DESCRIBE SELECT LIST FOR stmt INTO mr_sqlda;
953 mr_sqlda->N = mr_sqlda->F;
954 for (i = 0; i < mr_sqlda->N; i++)
956 mr_sqlda->V[i] = sqlbuffer[i];
958 mr_sqlda->L[i] = MAX_FIELD_WIDTH;
961 while (rowcount < max_row_count)
963 EXEC SQL FETCH curs USING DESCRIPTOR mr_sqlda;
966 (*action)(count, sqlbuffer, actarg);
973 if (rowcount == max_row_count)
975 critical_alert("moirad", "attempted query with too many rows");
978 else if (rowcount == 0)