6 * Copyright (C) 1987 by the Massachusetts Institute of Technology
9 * Revision 1.10 1987-08-28 14:57:51 mike
10 * Modified sms_query to not enclose RETRIEVE queries in begin/end transaction.
11 * This was necessary to allow get_all_poboxes and get_groups_of_all_users
12 * to temporarily change the Ingres lockmode.
14 * Revision 1.9 87/08/22 17:47:38 wesommer
15 * Cleanup (these changes were by Mike).
17 * Revision 1.8 87/08/10 16:22:26 mike
18 * wesommer modified error reporting.
20 * Revision 1.7 87/08/04 01:49:20 wesommer
21 * Rearranged messages.
23 * Revision 1.6 87/08/04 01:30:54 wesommer
24 * Mike's changes; checked in prior to working over messages.
26 * Revision 1.5 87/06/21 16:37:58 wesommer
27 * Changed include files, reindented things.
30 * Revision 1.4 87/06/08 05:03:27 wesommer
31 * Reindented; added header and trailer.
36 static char *rcsid_qrtn_qc = "$Header$";
40 #include "sms_server.h"
46 static int ingres_errno = 0;
50 * ingerr: (supposedly) called when Ingres indicates an error.
51 * I have not yet been able to get this to work to intercept a
52 * database open error.
55 static int ingerr(num)
58 ingres_errno = SMS_INGRES_ERR;
59 com_err(whoami, SMS_INGRES_ERR, " code %d\n", ingres_errno);
63 int sms_open_database()
67 /* initialize local argv */
68 for (i = 0; i < 16; i++)
69 Argv[i] = (char *)malloc(128);
75 /* open the database */
80 int sms_close_database()
85 sms_check_access(cl, name, argc, argv_ro)
91 register struct query *q;
94 register struct validate *v;
96 register int privileged;
97 struct query *get_query_by_name();
102 q = get_query_by_name(name);
103 if (q == (struct query *)0) return(SMS_NO_HANDLE);
106 /* copy the arguments into a local argv that we can modify */
107 for (i = 0; i < argc; i++)
108 strcpy(Argv[i], argv_ro[i]);
110 /* check initial query access */
111 status = check_query_access(q, Argv, cl);
112 privileged = (status == SMS_SUCCESS) ? 1 : 0;
113 if (status != SMS_SUCCESS && !(v && (v->pre_rtn == access_user ||
114 v->pre_rtn == access_pop ||
115 v->pre_rtn == access_list)))
118 /* check argument count */
120 if (q->type == UPDATE || q->type == APPEND) argreq += q->vcnt;
121 if (argc != argreq) return(SMS_ARGS);
123 /* validate arguments */
124 if (v && v->valobj) {
125 status = validate_fields(q, Argv, v->valobj, v->objcnt);
126 if (status != SMS_SUCCESS) return(status);
129 /* perform special query access check */
130 if (v && v->pre_rtn) {
131 status = (*v->pre_rtn)(q, Argv, cl, 1);
132 if (status != SMS_SUCCESS && (status != SMS_PERM || !privileged))
139 sms_process_query(cl, name, argc, argv_ro, action, actarg)
147 register struct query *q;
151 register struct validate *v;
158 struct save_queue *sq;
159 struct query *get_query_by_name();
161 struct save_queue *sq_create();
167 /* copy the arguments into a local argv that we can modify */
168 for (i = 0; i < argc; i++)
169 strcpy(Argv[i], argv_ro[i]);
171 /* list queries command */
172 if (!strcmp(name, "_list_queries")) {
173 list_queries(action, actarg);
177 /* help query command */
178 if (!strcmp(name, "_help")) {
179 q = get_query_by_name(Argv[0]);
180 if (q == (struct query *)0) return(SMS_NO_HANDLE);
181 help_query(q, action, actarg);
185 /* get query structure, return error if named query does not exist */
186 q = get_query_by_name(name);
187 if (q == (struct query *)0) return(SMS_NO_HANDLE);
190 /* check query access */
191 status = check_query_access(q, Argv, cl);
192 privileged = (status == SMS_SUCCESS) ? 1 : 0;
193 if (!privileged && !(status == SMS_PERM &&
194 (v && (v->pre_rtn == access_user ||
195 v->pre_rtn == access_pop ||
196 v->pre_rtn == access_list))))
199 /* check argument count */
201 if (q->type == UPDATE || q->type == APPEND) argreq += q->vcnt;
202 if (argc != argreq) return(SMS_ARGS);
204 /* validate arguments */
205 if (v && v->valobj) {
206 status = validate_fields(q, Argv, v->valobj, v->objcnt);
207 if (status != SMS_SUCCESS) return(status);
210 /* perform any special query pre-processing */
211 if (v && v->pre_rtn) {
212 status = (*v->pre_rtn)(q, Argv, cl, 0);
213 if (status != SMS_SUCCESS && (status != SMS_PERM || !privileged))
217 if (q->type != RETRIEVE)
222 /* for queries that do not permit wildcarding, check if row
225 status = validate_row(q, Argv, v);
226 if (status != SMS_EXISTS) break;
229 /* build "where" clause if needed */
231 build_qual(q->qual, q->argc, Argv, qual);
237 /* build "sort" clause if needed */
238 if (v && v->valobj) {
239 psort = build_sort(v, sort);
244 /* if there is a followup routine, then we must save the results */
245 /* of the first query for use by the followup routine */
246 /* if q->rvar = NULL, perform post_rtn only */
248 if (v && v->post_rtn) {
250 status = do_retrieve(q, pqual, psort, sq_save_args, sq);
251 if (status != SMS_SUCCESS) {
255 status = (*v->post_rtn)(q, sq, v, action, actarg);
257 /* normal retrieve */
258 status = do_retrieve(q, pqual, psort, action, actarg);
260 if (status != SMS_SUCCESS) break;
262 ## repeat replace tblstats (retrieves = tblstats.retrieves + 1)
263 ## where tblstats.#table = @table
265 status = (*v->post_rtn)(q, Argv, action, actarg);
271 /* see if row already exists */
273 status = validate_row(q, Argv, v);
274 if (status != SMS_EXISTS) break;
277 /* build "where" clause and perform update */
278 /* if q->rvar = NULL, perform post_rtn only */
280 build_qual(q->qual, q->argc, Argv, qual);
281 status = do_update(q, &Argv[q->argc], qual, action, actarg);
282 if (status != SMS_SUCCESS) break;
284 ## repeat replace tblstats (updates = tblstats.updates + 1,
286 ## where tblstats.#table = @table
289 /* execute followup routine (if any) */
290 if (v->post_rtn) status = (*v->post_rtn)(q, Argv);
295 /* see if row already exists */
297 status = validate_row(q, Argv, v);
298 if (status != SMS_NO_MATCH) break;
301 /* increment id number if necessary */
302 if (v->object_id) set_next_object_id(v->object_id);
304 /* build "where" clause if needed */
306 build_qual(q->qual, q->argc, Argv, qual);
312 /* perform the append */
313 /* if q->rvar = NULL, perform post_rtn only */
315 status = do_append(q, &Argv[q->argc], pqual, action, actarg);
316 if (status != SMS_SUCCESS) break;
318 ## repeat replace tblstats (appends = tblstats.appends + 1,
320 ## where tblstats.#table = @table
323 /* execute followup routine */
324 if (v->post_rtn) status = (*v->post_rtn)(q, Argv);
328 /* see if row already exists */
330 status = validate_row(q, Argv, v);
331 if (status != SMS_EXISTS) break;
334 /* build "where" clause and perform delete */
335 /* if q->rvar = NULL, perform post_rtn only */
337 build_qual(q->qual, q->argc, Argv, qual);
338 status = do_delete(q, qual, action, actarg);
339 if (status != SMS_SUCCESS) break;
341 ## repeat replace tblstats (deletes = tblstats.deletes + 1,
343 ## where tblstats.#table = @table
346 /* execute followup routine */
347 if (v->post_rtn) status = (*v->post_rtn)(q, Argv);
352 if (q->type != RETRIEVE) {
353 if (status == SMS_SUCCESS) {
360 if (status != SMS_SUCCESS && log_flags & LOG_RES)
361 com_err(whoami, status, " (Query failed)");
365 build_qual(fmt, argc, argv, qual)
376 for (i = 0; i < argc; i++) {
377 c = (char *)index(c, '%');
378 if (c++ == (char *)0) return(SMS_ARGS);
382 *(int *)&args[i] = *(int *)argv[i]; /* sigh */
384 return(SMS_INGRES_ERR);
393 sprintf(qual, fmt, args[0]);
397 sprintf(qual, fmt, args[0], args[1]);
401 sprintf(qual, fmt, args[0], args[1], args[2]);
405 sprintf(qual, fmt, args[0], args[1], args[2], args[3]);
412 register struct validate *v;
415 register struct valobj *vo;
424 if (vo->type == V_SORT) {
425 sprintf(elem, "RET_VAR%d", vo->index + 1);
426 if (*sort) strcat(sort, ", ");
432 return ((*sort) ? sort : 0);
435 check_query_access(q, argv, cl)
445 ## static int def_uid;
450 /* get query access control list */
452 ## repeat retrieve (acl_id = capacls.list_id) where capacls.tag = @name
453 ## inquire_equel (rowcount = "rowcount", errorno = "errorno")
454 if (errorno != 0) return(SMS_INGRES_ERR);
455 if (rowcount == 0) return(SMS_PERM);
457 /* initialize default uid */
459 ## retrieve (def_uid = users.users_id) where users.login = "default"
462 /* check for default access */
463 ## range of m is members
464 ## repeat retrieve (exists = any(m.#member_id where m.list_id = @acl_id and
465 ## m.member_type = "USER" and m.#member_id = def_uid))
466 if (exists) return(SMS_SUCCESS);
468 /* parse client name */
469 status = get_client(cl, &client_type, &client_id);
470 if (status != SMS_SUCCESS) return(status);
472 /* see if client is in the list (or any of its sub-lists) */
473 exists = find_member(acl_id, client_type, client_id, 0);
474 return ((exists) ? SMS_SUCCESS : SMS_PERM);
477 get_client(cl, client_type, client_id)
487 /* for now accept only null instances */
489 if (krb->inst[0]) return(SMS_PERM);
491 /* if client is from local realm, get users_id */
492 if (!strcmp(krb->realm, krb_realm)) {
494 ## repeat retrieve (member_id = users.users_id) where users.login = @name
495 *client_type = "USER";
497 /* otherwise use string_id */
499 ## repeat retrieve (member_id = strings.string_id)
500 ## where strings.string = @name
501 *client_type = "STRING";
504 /* make sure we found a users or string id */
505 ## inquire_equel (rowcount = "rowcount")
506 if (rowcount == 0) return(SMS_PERM);
508 *client_id = member_id;
512 ##find_member(list_id, member_type, member_id, sq)
514 ## char *member_type;
516 struct save_queue *sq;
521 struct save_queue *sq_create();
523 /* see if client is a direct member of list */
524 ## repeat retrieve (exists = any(m.#member_id where
525 ## m.#list_id = @list_id and
526 ## m.#member_type = @member_type and
527 ## m.#member_id = @member_id))
528 if (exists) return(1);
530 /* are there any sub-lists? */
531 ## repeat retrieve (exists = any(m.#member_id where m.#list_id = @list_id and
532 ## m.#member_type = "LIST"))
533 if (!exists) return(0);
535 /* yes; now recurse through sublists */
537 /* create a save queue */
538 if (sq == (struct save_queue *)0) {
545 /* save all sublist ids */
546 ## range of m is members
547 ## retrieve (sublist = m.#member_id)
548 ## where m.#list_id = list_id and m.#member_type = "LIST"
550 sq_save_unique_data(sq, sublist);
555 /* at top-level, check sub-lists for client (breadth-first search) */
556 while (sq_get_data(sq, &sublist)) {
557 exists = find_member(sublist, member_type, member_id, sq);
565 do_retrieve(q, pqual, psort, action, actarg)
566 register struct query *q;
582 ## range of rvar is rtable
589 ## retrieve unique (param (q->tlist, q->vaddr)) where cqual
592 (*action)(q->vcnt, q->vaddr, actarg);
595 ## retrieve unique (param (q->tlist, q->vaddr))
598 (*action)(q->vcnt, q->vaddr, actarg);
605 ## retrieve unique (param (q->tlist, q->vaddr)) where cqual
607 (*action)(q->vcnt, q->vaddr, actarg);
610 ## retrieve unique (param (q->tlist, q->vaddr))
612 (*action)(q->vcnt, q->vaddr, actarg);
617 ## inquire_equel (rowcount = "rowcount", errorno = "errorno")
618 if (errorno != 0) return(SMS_INGRES_ERR);
619 return ((rowcount == 0) ? SMS_NO_MATCH : SMS_SUCCESS);
622 do_update(q, argv, qual, action, actarg)
623 register struct query *q;
637 ## range of rvar is rtable
640 ## replace rvar (param (q->tlist, argv))
643 ## inquire_equel (errorno = "errorno")
644 if (errorno != 0) return(SMS_INGRES_ERR);
648 do_append(q, argv, pqual, action, actarg)
649 register struct query *q;
662 ## range of rvar is rtable
666 ## append to rtable (param (q->tlist, argv)) where cqual
668 ## append to rtable (param (q->tlist, argv))
671 ## inquire_equel (errorno = "errorno")
672 if (errorno != 0) return(SMS_INGRES_ERR);
676 do_delete(q, qual, action, actarg)
677 register struct query *q;
689 ## range of rvar is rtable
692 ## delete rvar where cqual
694 ## inquire_equel (errorno = "errorno")
695 if (errorno != 0) return(SMS_INGRES_ERR);
704 * c-continued-statement-offset: 4
706 * c-argdecl-indent: 4