6 * Copyright (C) 1987 by the Massachusetts Institute of Technology
9 * Revision 1.7 1987-08-04 01:49:20 wesommer
10 * Rearranged messages.
12 * Revision 1.6 87/08/04 01:30:54 wesommer
13 * Mike's changes; checked in prior to working over messages.
15 * Revision 1.5 87/06/21 16:37:58 wesommer
16 * Changed include files, reindented things.
19 * Revision 1.4 87/06/08 05:03:27 wesommer
20 * Reindented; added header and trailer.
25 static char *rcsid_qrtn_qc = "$Header$";
29 #include "sms_server.h"
35 static int ingres_errno = 0;
39 * ingerr: (supposedly) called when Ingres indicates an error.
40 * I have not yet been able to get this to work to intercept a
41 * database open error.
44 static int ingerr(num)
47 ingres_errno = SMS_INGRES_ERR;
51 int sms_open_database()
55 /* initialize local argv */
56 for (i = 0; i < 16; i++)
57 Argv[i] = (char *)malloc(128);
63 /* open the database */
68 int sms_close_database()
73 sms_check_access(cl, name, argc, argv_ro)
79 register struct query *q;
82 register struct validate *v;
84 register int privileged;
85 struct query *get_query_by_name();
89 q = get_query_by_name(name);
90 if (q == (struct query *)0) return(SMS_NO_HANDLE);
93 /* copy the arguments into a local argv that we can modify */
94 for (i = 0; i < argc; i++)
95 strcpy(Argv[i], argv_ro[i]);
97 /* check initial query access */
98 status = check_query_access(q, Argv, cl);
99 privileged = (status == SMS_SUCCESS) ? 1 : 0;
100 if (status != SMS_SUCCESS && !(v && (v->pre_rtn == access_user ||
101 v->pre_rtn == access_pop)))
104 /* check argument count */
106 if (q->type == UPDATE || q->type == APPEND) argreq += q->vcnt;
107 if (argc != argreq) return(SMS_ARGS);
109 /* validate arguments */
110 if (v && v->valobj) {
111 status = validate_fields(q, Argv, v->valobj, v->objcnt);
112 if (status != SMS_SUCCESS) return(status);
115 /* perform special query access check */
116 if (v && v->pre_rtn) {
117 status = (*v->pre_rtn)(q, Argv, cl, 1);
118 if (status != SMS_SUCCESS && (status != SMS_PERM || !privileged))
125 sms_process_query(cl, name, argc, argv_ro, action, actarg)
133 register struct query *q;
137 register struct validate *v;
142 struct save_queue *sq;
143 struct query *get_query_by_name();
145 struct save_queue *sq_create();
148 /* copy the arguments into a local argv that we can modify */
149 for (i = 0; i < argc; i++)
150 strcpy(Argv[i], argv_ro[i]);
152 /* list queries command */
153 if (!strcmp(name, "_list_queries")) {
154 list_queries(action, actarg);
158 /* help query command */
159 if (!strcmp(name, "_help")) {
160 q = get_query_by_name(Argv[0]);
161 if (q == (struct query *)0) return(SMS_NO_HANDLE);
162 help_query(q, action, actarg);
166 /* get query structure, return error if named query does not exist */
167 q = get_query_by_name(name);
168 if (q == (struct query *)0) return(SMS_NO_HANDLE);
171 /* check query access */
172 status = check_query_access(q, Argv, cl);
173 privileged = (status == SMS_SUCCESS) ? 1 : 0;
174 if (status != SMS_SUCCESS && !(v && (v->pre_rtn == access_user)))
177 /* check argument count */
179 if (q->type == UPDATE || q->type == APPEND) argreq += q->vcnt;
180 if (argc != argreq) return(SMS_ARGS);
182 /* validate arguments */
183 if (v && v->valobj) {
184 status = validate_fields(q, Argv, v->valobj, v->objcnt);
185 if (status != SMS_SUCCESS) return(status);
188 /* perform any special query pre-processing */
189 if (v && v->pre_rtn) {
190 status = (*v->pre_rtn)(q, Argv, cl, 0);
191 if (status != SMS_SUCCESS && (status != SMS_PERM || !privileged))
199 /* for queries that do not permit wildcarding, check if row
202 status = validate_row(q, Argv, v);
203 if (status != SMS_EXISTS) break;
206 /* build "where" clause if needed */
208 build_qual(q->qual, q->argc, Argv, qual);
214 /* if there is a followup routine, then we must save the results */
215 /* of the first query for use by the followup routine */
216 /* if q->rtable = NULL, perform post_rtn only */
217 if (table = q->rtable) {
218 if (v && v->post_rtn) {
220 status = do_retrieve(q, pqual, sq_save_args, sq);
221 if (status != SMS_SUCCESS) {
225 status = (*v->post_rtn)(q, sq, v, action, actarg);
227 /* normal retrieve */
228 status = do_retrieve(q, pqual, action, actarg);
230 if (status != SMS_SUCCESS) break;
231 ## repeat replace tblstats (retrieves = tblstats.retrieves + 1)
232 ## where tblstats.#table = @table
234 status = (*v->post_rtn)(q, Argv, action, actarg);
240 /* see if row already exists */
242 status = validate_row(q, Argv, v);
243 if (status != SMS_EXISTS) break;
246 /* build "where" clause and perform update */
247 /* if q->rtable = NULL, perform post_rtn only */
248 if (table = q->rtable) {
249 build_qual(q->qual, q->argc, Argv, qual);
250 status = do_update(q, &Argv[q->argc], qual, action, actarg);
251 if (status != SMS_SUCCESS) break;
252 ## repeat replace tblstats (updates = tblstats.updates + 1,
254 ## where tblstats.#table = @table
257 /* execute followup routine (if any) */
258 if (v->post_rtn) status = (*v->post_rtn)(q, Argv);
263 /* see if row already exists */
265 status = validate_row(q, Argv, v);
266 if (status != SMS_NO_MATCH) break;
269 /* increment id number if necessary */
270 if (v->object_id) set_next_object_id(v->object_id);
272 /* perform the append */
273 /* if q->rtable = NULL, perform post_rtn only */
274 if (table = q->rtable) {
275 status = do_append(q, &Argv[q->argc], action, actarg);
276 if (status != SMS_SUCCESS) break;
277 ## repeat replace tblstats (appends = tblstats.appends + 1,
279 ## where tblstats.#table = @table
282 /* execute followup routine */
283 if (v->post_rtn) status = (*v->post_rtn)(q, Argv);
287 /* see if row already exists */
289 status = validate_row(q, Argv, v);
290 if (status != SMS_EXISTS) break;
293 /* build "where" clause and perform delete */
294 /* if q->rtable = NULL, perform post_rtn only */
295 if (table = q->rtable) {
296 build_qual(q->qual, q->argc, Argv, qual);
297 status = do_delete(q, qual, action, actarg);
298 if (status != SMS_SUCCESS) break;
299 ## repeat replace tblstats (deletes = tblstats.deletes + 1,
301 ## where tblstats.#table = @table
304 /* execute followup routine */
305 if (v->post_rtn) status = (*v->post_rtn)(q, Argv);
310 if (status == SMS_SUCCESS)
315 if (status != SMS_SUCCESS) com_err(whoami, status, " (Query failed)");
319 build_qual(fmt, argc, argv, qual)
330 for (i = 0; i < argc; i++) {
331 c = (char *)index(c, '%');
332 if (c++ == (char *)0) return(SMS_ARGS);
336 *(int *)&args[i] = *(int *)argv[i]; /* sigh */
338 return(SMS_INGRES_ERR);
347 sprintf(qual, fmt, args[0]);
351 sprintf(qual, fmt, args[0], args[1]);
355 sprintf(qual, fmt, args[0], args[1], args[2]);
359 sprintf(qual, fmt, args[0], args[1], args[2], args[3]);
364 check_query_access(q, argv, cl)
373 ## static int def_uid;
378 /* get query access control list */
380 ## repeat retrieve (acl_id = capacls.list_id) where capacls.tag = @name
381 ## inquire_equel (rowcount = "rowcount")
382 if (rowcount == 0) return(SMS_PERM);
384 /* initialize default uid */
386 ## retrieve (def_uid = users.users_id) where users.login = "default"
389 /* check for default access */
390 ## range of m is members
391 ## repeat retrieve (exists = any(m.#member_id where m.list_id = @acl_id and
392 ## m.member_type = "USER" and m.#member_id = def_uid))
393 if (exists) return(SMS_SUCCESS);
395 /* parse client name */
396 status = get_client(cl, &client_type, &client_id);
397 if (status != SMS_SUCCESS) return(status);
399 /* see if client is in the list (or any of its sub-lists) */
400 exists = find_member(acl_id, client_type, client_id, 0);
401 return ((exists) ? SMS_SUCCESS : SMS_PERM);
404 get_client(cl, client_type, client_id)
414 /* for now accept only null instances */
416 if (krb->inst[0]) return(SMS_PERM);
418 /* if client is from local realm, get users_id */
419 if (!strcmp(krb->realm, krb_realm)) {
421 ## repeat retrieve (member_id = users.users_id) where users.login = @name
422 *client_type = "USER";
424 /* otherwise use string_id */
426 ## repeat retrieve (member_id = strings.string_id)
427 ## where strings.string = @name
428 *client_type = "STRING";
431 /* make sure we found a users or string id */
432 ## inquire_equel (rowcount = "rowcount")
433 if (rowcount == 0) return(SMS_PERM);
435 *client_id = member_id;
439 ##find_member(list_id, member_type, member_id, sq)
441 ## char *member_type;
443 struct save_queue *sq;
448 struct save_queue *sq_create();
450 /* see if client is a direct member of list */
451 ## repeat retrieve (exists = any(m.#member_id where
452 ## m.#list_id = @list_id and
453 ## m.#member_type = @member_type and
454 ## m.#member_id = @member_id))
455 if (exists) return(1);
457 /* are there any sub-lists? */
458 ## repeat retrieve (exists = any(m.#member_id where m.#list_id = @list_id and
459 ## m.#member_type = "LIST"))
460 if (!exists) return(0);
462 /* yes; now recurse through sublists */
464 /* create a save queue */
465 if (sq == (struct save_queue *)0) {
472 /* save all sublist ids */
473 ## range of m is members
474 ## retrieve (sublist = m.#member_id)
475 ## where m.#list_id = list_id and m.#member_type = "LIST"
477 sq_save_unique_data(sq, sublist);
482 /* at top-level, check sub-lists for client (breadth-first search) */
483 while (sq_get_data(sq, &sublist)) {
484 exists = find_member(sublist, member_type, member_id, sq);
492 do_retrieve(q, qual, action, actarg)
493 register struct query *q;
506 ## range of rvar is rtable
511 ## retrieve unique (param (q->tlist, q->vaddr)) where cqual
513 (*action)(q->vcnt, q->vaddr, actarg);
516 ## retrieve unique (param (q->tlist, q->vaddr))
518 (*action)(q->vcnt, q->vaddr, actarg);
522 ## inquire_equel (rowcount = "rowcount")
524 return ((rowcount == 0) ? SMS_NO_MATCH : SMS_SUCCESS);
527 do_update(q, argv, qual, action, actarg)
528 register struct query *q;
541 ## range of rvar is rtable
544 ## replace rvar (param (q->tlist, argv))
550 do_append(q, argv, action, actarg)
551 register struct query *q;
562 ## range of rvar is rtable
566 ## append to rtable (param (q->tlist, argv)) where cqual
568 ## append to rtable (param (q->tlist, argv))
574 do_delete(q, qual, action, actarg)
575 register struct query *q;
586 ## range of rvar is rtable
589 ## delete rvar where cqual
599 * c-continued-statement-offset: 4
601 * c-argdecl-indent: 4