6 * Copyright (C) 1987 by the Massachusetts Institute of Technology
9 * Revision 1.9 1987-08-22 17:47:38 wesommer
10 * Cleanup (these changes were by Mike).
12 * Revision 1.8 87/08/10 16:22:26 mike
13 * wesommer modified error reporting.
15 * Revision 1.7 87/08/04 01:49:20 wesommer
16 * Rearranged messages.
18 * Revision 1.6 87/08/04 01:30:54 wesommer
19 * Mike's changes; checked in prior to working over messages.
21 * Revision 1.5 87/06/21 16:37:58 wesommer
22 * Changed include files, reindented things.
25 * Revision 1.4 87/06/08 05:03:27 wesommer
26 * Reindented; added header and trailer.
31 static char *rcsid_qrtn_qc = "$Header$";
35 #include "sms_server.h"
41 static int ingres_errno = 0;
45 * ingerr: (supposedly) called when Ingres indicates an error.
46 * I have not yet been able to get this to work to intercept a
47 * database open error.
50 static int ingerr(num)
53 ingres_errno = SMS_INGRES_ERR;
54 com_err(whoami, SMS_INGRES_ERR, " code %d\n", ingres_errno);
58 int sms_open_database()
62 /* initialize local argv */
63 for (i = 0; i < 16; i++)
64 Argv[i] = (char *)malloc(128);
70 /* open the database */
75 int sms_close_database()
80 sms_check_access(cl, name, argc, argv_ro)
86 register struct query *q;
89 register struct validate *v;
91 register int privileged;
92 struct query *get_query_by_name();
97 q = get_query_by_name(name);
98 if (q == (struct query *)0) return(SMS_NO_HANDLE);
101 /* copy the arguments into a local argv that we can modify */
102 for (i = 0; i < argc; i++)
103 strcpy(Argv[i], argv_ro[i]);
105 /* check initial query access */
106 status = check_query_access(q, Argv, cl);
107 privileged = (status == SMS_SUCCESS) ? 1 : 0;
108 if (status != SMS_SUCCESS && !(v && (v->pre_rtn == access_user ||
109 v->pre_rtn == access_pop ||
110 v->pre_rtn == access_list)))
113 /* check argument count */
115 if (q->type == UPDATE || q->type == APPEND) argreq += q->vcnt;
116 if (argc != argreq) return(SMS_ARGS);
118 /* validate arguments */
119 if (v && v->valobj) {
120 status = validate_fields(q, Argv, v->valobj, v->objcnt);
121 if (status != SMS_SUCCESS) return(status);
124 /* perform special query access check */
125 if (v && v->pre_rtn) {
126 status = (*v->pre_rtn)(q, Argv, cl, 1);
127 if (status != SMS_SUCCESS && (status != SMS_PERM || !privileged))
134 sms_process_query(cl, name, argc, argv_ro, action, actarg)
142 register struct query *q;
146 register struct validate *v;
153 struct save_queue *sq;
154 struct query *get_query_by_name();
156 struct save_queue *sq_create();
162 /* copy the arguments into a local argv that we can modify */
163 for (i = 0; i < argc; i++)
164 strcpy(Argv[i], argv_ro[i]);
166 /* list queries command */
167 if (!strcmp(name, "_list_queries")) {
168 list_queries(action, actarg);
172 /* help query command */
173 if (!strcmp(name, "_help")) {
174 q = get_query_by_name(Argv[0]);
175 if (q == (struct query *)0) return(SMS_NO_HANDLE);
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);
182 if (q == (struct query *)0) return(SMS_NO_HANDLE);
185 /* check query access */
186 status = check_query_access(q, Argv, cl);
187 privileged = (status == SMS_SUCCESS) ? 1 : 0;
188 if (!privileged && !(status == SMS_PERM &&
189 (v && (v->pre_rtn == access_user ||
190 v->pre_rtn == access_pop ||
191 v->pre_rtn == access_list))))
194 /* check argument count */
196 if (q->type == UPDATE || q->type == APPEND) argreq += q->vcnt;
197 if (argc != argreq) return(SMS_ARGS);
199 /* validate arguments */
200 if (v && v->valobj) {
201 status = validate_fields(q, Argv, v->valobj, v->objcnt);
202 if (status != SMS_SUCCESS) return(status);
205 /* perform any special query pre-processing */
206 if (v && v->pre_rtn) {
207 status = (*v->pre_rtn)(q, Argv, cl, 0);
208 if (status != SMS_SUCCESS && (status != SMS_PERM || !privileged))
216 /* for queries that do not permit wildcarding, check if row
219 status = validate_row(q, Argv, v);
220 if (status != SMS_EXISTS) break;
223 /* build "where" clause if needed */
225 build_qual(q->qual, q->argc, Argv, qual);
231 /* build "sort" clause if needed */
232 if (v && v->valobj) {
233 psort = build_sort(v, sort);
238 /* if there is a followup routine, then we must save the results */
239 /* of the first query for use by the followup routine */
240 /* if q->rvar = NULL, perform post_rtn only */
242 if (v && v->post_rtn) {
244 status = do_retrieve(q, pqual, psort, sq_save_args, sq);
245 if (status != SMS_SUCCESS) {
249 status = (*v->post_rtn)(q, sq, v, action, actarg);
251 /* normal retrieve */
252 status = do_retrieve(q, pqual, psort, action, actarg);
254 if (status != SMS_SUCCESS) break;
256 ## repeat replace tblstats (retrieves = tblstats.retrieves + 1)
257 ## where tblstats.#table = @table
259 status = (*v->post_rtn)(q, Argv, action, actarg);
265 /* see if row already exists */
267 status = validate_row(q, Argv, v);
268 if (status != SMS_EXISTS) break;
271 /* build "where" clause and perform update */
272 /* if q->rvar = NULL, perform post_rtn only */
274 build_qual(q->qual, q->argc, Argv, qual);
275 status = do_update(q, &Argv[q->argc], qual, action, actarg);
276 if (status != SMS_SUCCESS) break;
278 ## repeat replace tblstats (updates = tblstats.updates + 1,
280 ## where tblstats.#table = @table
283 /* execute followup routine (if any) */
284 if (v->post_rtn) status = (*v->post_rtn)(q, Argv);
289 /* see if row already exists */
291 status = validate_row(q, Argv, v);
292 if (status != SMS_NO_MATCH) break;
295 /* increment id number if necessary */
296 if (v->object_id) set_next_object_id(v->object_id);
298 /* build "where" clause if needed */
300 build_qual(q->qual, q->argc, Argv, qual);
306 /* perform the append */
307 /* if q->rvar = NULL, perform post_rtn only */
309 status = do_append(q, &Argv[q->argc], pqual, action, actarg);
310 if (status != SMS_SUCCESS) break;
312 ## repeat replace tblstats (appends = tblstats.appends + 1,
314 ## where tblstats.#table = @table
317 /* execute followup routine */
318 if (v->post_rtn) status = (*v->post_rtn)(q, Argv);
322 /* see if row already exists */
324 status = validate_row(q, Argv, v);
325 if (status != SMS_EXISTS) break;
328 /* build "where" clause and perform delete */
329 /* if q->rvar = NULL, perform post_rtn only */
331 build_qual(q->qual, q->argc, Argv, qual);
332 status = do_delete(q, qual, action, actarg);
333 if (status != SMS_SUCCESS) break;
335 ## repeat replace tblstats (deletes = tblstats.deletes + 1,
337 ## where tblstats.#table = @table
340 /* execute followup routine */
341 if (v->post_rtn) status = (*v->post_rtn)(q, Argv);
346 if (status == SMS_SUCCESS)
351 if (status != SMS_SUCCESS && log_flags & LOG_RES)
352 com_err(whoami, status, " (Query failed)");
356 build_qual(fmt, argc, argv, qual)
367 for (i = 0; i < argc; i++) {
368 c = (char *)index(c, '%');
369 if (c++ == (char *)0) return(SMS_ARGS);
373 *(int *)&args[i] = *(int *)argv[i]; /* sigh */
375 return(SMS_INGRES_ERR);
384 sprintf(qual, fmt, args[0]);
388 sprintf(qual, fmt, args[0], args[1]);
392 sprintf(qual, fmt, args[0], args[1], args[2]);
396 sprintf(qual, fmt, args[0], args[1], args[2], args[3]);
403 register struct validate *v;
406 register struct valobj *vo;
415 if (vo->type == V_SORT) {
416 sprintf(elem, "RET_VAR%d", vo->index + 1);
417 if (*sort) strcat(sort, ", ");
423 return ((*sort) ? sort : 0);
426 check_query_access(q, argv, cl)
436 ## static int def_uid;
441 /* get query access control list */
443 ## repeat retrieve (acl_id = capacls.list_id) where capacls.tag = @name
444 ## inquire_equel (rowcount = "rowcount", errorno = "errorno")
445 if (errorno != 0) return(SMS_INGRES_ERR);
446 if (rowcount == 0) return(SMS_PERM);
448 /* initialize default uid */
450 ## retrieve (def_uid = users.users_id) where users.login = "default"
453 /* check for default access */
454 ## range of m is members
455 ## repeat retrieve (exists = any(m.#member_id where m.list_id = @acl_id and
456 ## m.member_type = "USER" and m.#member_id = def_uid))
457 if (exists) return(SMS_SUCCESS);
459 /* parse client name */
460 status = get_client(cl, &client_type, &client_id);
461 if (status != SMS_SUCCESS) return(status);
463 /* see if client is in the list (or any of its sub-lists) */
464 exists = find_member(acl_id, client_type, client_id, 0);
465 return ((exists) ? SMS_SUCCESS : SMS_PERM);
468 get_client(cl, client_type, client_id)
478 /* for now accept only null instances */
480 if (krb->inst[0]) return(SMS_PERM);
482 /* if client is from local realm, get users_id */
483 if (!strcmp(krb->realm, krb_realm)) {
485 ## repeat retrieve (member_id = users.users_id) where users.login = @name
486 *client_type = "USER";
488 /* otherwise use string_id */
490 ## repeat retrieve (member_id = strings.string_id)
491 ## where strings.string = @name
492 *client_type = "STRING";
495 /* make sure we found a users or string id */
496 ## inquire_equel (rowcount = "rowcount")
497 if (rowcount == 0) return(SMS_PERM);
499 *client_id = member_id;
503 ##find_member(list_id, member_type, member_id, sq)
505 ## char *member_type;
507 struct save_queue *sq;
512 struct save_queue *sq_create();
514 /* see if client is a direct member of list */
515 ## repeat retrieve (exists = any(m.#member_id where
516 ## m.#list_id = @list_id and
517 ## m.#member_type = @member_type and
518 ## m.#member_id = @member_id))
519 if (exists) return(1);
521 /* are there any sub-lists? */
522 ## repeat retrieve (exists = any(m.#member_id where m.#list_id = @list_id and
523 ## m.#member_type = "LIST"))
524 if (!exists) return(0);
526 /* yes; now recurse through sublists */
528 /* create a save queue */
529 if (sq == (struct save_queue *)0) {
536 /* save all sublist ids */
537 ## range of m is members
538 ## retrieve (sublist = m.#member_id)
539 ## where m.#list_id = list_id and m.#member_type = "LIST"
541 sq_save_unique_data(sq, sublist);
546 /* at top-level, check sub-lists for client (breadth-first search) */
547 while (sq_get_data(sq, &sublist)) {
548 exists = find_member(sublist, member_type, member_id, sq);
556 do_retrieve(q, pqual, psort, action, actarg)
557 register struct query *q;
573 ## range of rvar is rtable
580 ## retrieve unique (param (q->tlist, q->vaddr)) where cqual
583 (*action)(q->vcnt, q->vaddr, actarg);
586 ## retrieve unique (param (q->tlist, q->vaddr))
589 (*action)(q->vcnt, q->vaddr, actarg);
596 ## retrieve unique (param (q->tlist, q->vaddr)) where cqual
598 (*action)(q->vcnt, q->vaddr, actarg);
601 ## retrieve unique (param (q->tlist, q->vaddr))
603 (*action)(q->vcnt, q->vaddr, actarg);
608 ## inquire_equel (rowcount = "rowcount", errorno = "errorno")
609 if (errorno != 0) return(SMS_INGRES_ERR);
610 return ((rowcount == 0) ? SMS_NO_MATCH : SMS_SUCCESS);
613 do_update(q, argv, qual, action, actarg)
614 register struct query *q;
628 ## range of rvar is rtable
631 ## replace rvar (param (q->tlist, argv))
634 ## inquire_equel (errorno = "errorno")
635 if (errorno != 0) return(SMS_INGRES_ERR);
639 do_append(q, argv, pqual, action, actarg)
640 register struct query *q;
653 ## range of rvar is rtable
657 ## append to rtable (param (q->tlist, argv)) where cqual
659 ## append to rtable (param (q->tlist, argv))
662 ## inquire_equel (errorno = "errorno")
663 if (errorno != 0) return(SMS_INGRES_ERR);
667 do_delete(q, qual, action, actarg)
668 register struct query *q;
680 ## range of rvar is rtable
683 ## delete rvar where cqual
685 ## inquire_equel (errorno = "errorno")
686 if (errorno != 0) return(SMS_INGRES_ERR);
695 * c-continued-statement-offset: 4
697 * c-argdecl-indent: 4