]> andersk Git - moira.git/blame - server/qrtn.pc
Remove `delete_user_by_uid' since it's never been used in any logs we have,
[moira.git] / server / qrtn.pc
CommitLineData
1a41acb7 1/*
2 * $Source$
3 * $Author$
4 * $Header$
b070f8a1 5 *
1a41acb7 6 * Copyright (C) 1987, 1988 by the Massachusetts Institute of Technology
7 * For copying and distribution information, please see the file
8 * <mit-copyright.h>.
9 *
b070f8a1 10 */
11
12#ifndef lint
048b1b94 13static char *rcsid_qrtn_dc = "$Header$";
b070f8a1 14#endif lint
15
16#include <mit-copyright.h>
cc0088db 17#include <string.h>
b070f8a1 18#include "mr_server.h"
03c05291 19#include "query.h"
048b1b94 20EXEC SQL INCLUDE sqlca; /* SQL Communications Area */
21EXEC SQL INCLUDE sqlda; /* SQL Descriptor Area */
22#include "qrtn.h"
23
03c05291 24SQLDA *mr_sqlda;
048b1b94 25EXEC SQL BEGIN DECLARE SECTION;
1741dca0 26int mr_sig_length;
048b1b94 27int idummy;
28char cdummy[MR_CDUMMY_LEN];
29char stmt_buf[MR_STMTBUF_LEN];
30EXEC SQL END DECLARE SECTION;
b070f8a1 31
30cf9ed3 32char *Argv[QMAXARGS];
03c05291 33extern char *table_name[];
34extern char *sqlbuffer[QMAXARGS];
b070f8a1 35
03c05291 36int dbms_errno = 0;
b070f8a1 37int mr_errcode = 0;
048b1b94 38EXEC SQL BEGIN DECLARE SECTION;
39int query_timeout = 30;
923a939b 40char *database = "moira";
048b1b94 41EXEC SQL END DECLARE SECTION;
b070f8a1 42extern char *whoami;
43extern FILE *journal;
44
03c05291 45int mr_verify_query(client *cl, struct query *q, int argc, char *argv_ro[]);
68b5b948 46int do_retrieve(struct query *q, char *pqual, int (*action)(), char *actarg);
03c05291 47int do_update(struct query *q, char *argv[], char *qual,
48 int (*action)(), char *actarg);
49int do_append(struct query *q, char *argv[], char *pqual,
50 int (*action)(), char *actarg);
51int do_delete(struct query *q, char *qual,
52 int (*action)(), char *actarg);
60140d51 53char *build_sql_stmt(char *cmd, char *targetlist, char *argv[],
54 int argc, char *qual, char *sort);
03c05291 55
56/* from qvalidate.dc */
57int validate_fields(struct query *q, char *argv[], struct valobj *vo, int n);
58int validate_row(struct query *q, char *argv[], struct validate *v);
59
45bf7573 60
048b1b94 61/*
03c05291 62 * dbmserr: Called when the DBMS indicates an error.
b070f8a1 63 */
64
03c05291 65void dbmserr(void)
b070f8a1 66{
45bf7573 67 EXEC SQL BEGIN DECLARE SECTION;
68 char err_msg[256];
69 EXEC SQL END DECLARE SECTION;
03c05291 70 int bufsize=256, msglength=0;
71
72 dbms_errno = -sqlca.sqlcode;
73 mr_errcode = MR_DBMS_ERR;
74 com_err(whoami, MR_DBMS_ERR, " code %d\n", dbms_errno);
75 sqlglm(err_msg, &bufsize, &msglength);
76 err_msg[msglength]=0;
77 com_err(whoami, 0, "SQL error text = %s", err_msg);
78 critical_alert("MOIRA", "Moira server encountered DBMS ERROR %d\n%s",
79 dbms_errno, err_msg);
b070f8a1 80}
81
45bf7573 82/* This is declarative, not executed. Applies from here on, in this file. */
03c05291 83EXEC SQL WHENEVER SQLERROR DO dbmserr();
048b1b94 84
03c05291 85int mr_open_database(void)
b070f8a1 86{
87 register int i;
03c05291 88 SQLDA *mr_alloc_sqlda();
b070f8a1 89 static first_open = 1;
90
91 if (first_open) {
92 first_open = 0;
93
94 /* initialize local argv */
95 for (i = 0; i < 16; i++)
96 Argv[i] = malloc(ARGLEN);
97
03c05291 98 mr_sqlda = mr_alloc_sqlda();
c9a214e4 99
b070f8a1 100 incremental_init();
101 flush_cache();
102 }
103
03c05291 104 dbms_errno = 0;
b070f8a1 105 mr_errcode = 0;
106
107 /* open the database */
03c05291 108 EXEC SQL CONNECT :database IDENTIFIED BY :database;
109
110 if(dbms_errno)
1741dca0 111 return(mr_errcode);
112
03c05291 113 EXEC SQL SELECT data_length INTO :mr_sig_length FROM user_tab_columns WHERE table_name='USERS' and column_name='SIGNATURE';
3e6ff9e4 114 EXEC SQL COMMIT WORK;
03c05291 115 if(dbms_errno)
1741dca0 116 return(mr_errcode);
117
118 return(MR_SUCCESS);
b070f8a1 119}
120
03c05291 121void mr_close_database(void)
b070f8a1 122{
123 flush_cache();
03c05291 124 EXEC SQL COMMIT RELEASE;
b070f8a1 125}
126
03c05291 127int mr_check_access(cl, name, argc, argv_ro)
128 client *cl;
129 char *name, *argv_ro[];
130 int argc;
b070f8a1 131{
132 struct query *q;
b070f8a1 133
03c05291 134 dbms_errno = 0;
b070f8a1 135 mr_errcode = 0;
136
137 q = get_query_by_name(name, cl->args->mr_version_no);
138 if (q == (struct query *)0)
139 return(MR_NO_HANDLE);
140
048b1b94 141 return(mr_verify_query(cl, q, argc, argv_ro));
b070f8a1 142}
143
03c05291 144int mr_process_query(cl, name, argc, argv_ro, action, actarg)
145 client *cl;
146 char *name, *argv_ro[], *actarg;
147 int argc, (*action)();
b070f8a1 148{
149 register struct query *q;
150 register int status;
151 register struct validate *v;
60140d51 152 char *qual = NULL;
048b1b94 153 EXEC SQL BEGIN DECLARE SECTION;
03c05291 154 char *table;
048b1b94 155 EXEC SQL END DECLARE SECTION;
b070f8a1 156 struct save_queue *sq;
b070f8a1 157
03c05291 158 dbms_errno = 0;
b070f8a1 159 mr_errcode = 0;
160
161 /* list queries command */
162 if (!strcmp(name, "_list_queries")) {
163 list_queries(cl->args->mr_version_no, action, actarg);
164 return(MR_SUCCESS);
165 }
166
167 /* help query command */
168 if (!strcmp(name, "_help")) {
169 if (argc < 1)
170 return(MR_ARGS);
171 q = get_query_by_name(argv_ro[0], cl->args->mr_version_no);
172 if (q == (struct query *)0) return(MR_NO_HANDLE);
173 help_query(q, action, actarg);
174 return(MR_SUCCESS);
175 }
176
177 /* get query structure, return error if named query does not exist */
178 q = get_query_by_name(name, cl->args->mr_version_no);
179 if (q == (struct query *)0) return(MR_NO_HANDLE);
180 v = q->validate;
181
182 /* setup argument vector, verify access and arguments */
183 if ((status = mr_verify_query(cl, q, argc, argv_ro)) != MR_SUCCESS)
184 goto out;
185
186 /* perform any special query pre-processing */
187 if (v && v->pre_rtn) {
188 status = (*v->pre_rtn)(q, Argv, cl, 0);
189 if (status != MR_SUCCESS)
190 goto out;
191 }
192
193 switch (q->type) {
194 case RETRIEVE:
195 /* for queries that do not permit wildcarding, check if row
196 uniquely exists */
197 if (v && v->field) {
048b1b94 198 status = validate_row(q, Argv, v);
b070f8a1 199 if (status != MR_EXISTS) break;
200 }
201
b070f8a1 202 /* if there is a followup routine, then we must save the results */
203 /* of the first query for use by the followup routine */
204 /* if q->rvar = NULL, perform post_rtn only */
205 if (q->rvar) {
60140d51 206 if (q->qual) qual = build_qual(q->qual, q->argc, Argv);
b070f8a1 207 if (v && v->post_rtn) {
208 sq = sq_create();
60140d51 209 status = do_retrieve(q, qual, sq_save_args, (char *)sq);
b070f8a1 210 if (status != MR_SUCCESS) {
211 sq_destroy(sq);
212 break;
213 }
214 status = (*v->post_rtn)(q, sq, v, action, actarg, cl);
215 } else {
216 /* normal retrieve */
60140d51 217 status = do_retrieve(q, qual, action, actarg);
b070f8a1 218 }
219 if (status != MR_SUCCESS) break;
220 } else {
221 status = (*v->post_rtn)(q, Argv, cl, action, actarg);
222 }
223
224 break;
225
226 case UPDATE:
227 /* see if row already exists */
228 if (v->field) {
229 status = validate_row(q, Argv, v);
230 if (status != MR_EXISTS) break;
231 }
232
233 /* build "where" clause and perform update */
234 /* if q->rvar = NULL, perform post_rtn only */
235 if (q->rvar) {
60140d51 236 qual = build_qual(q->qual, q->argc, Argv);
b070f8a1 237 incremental_before(q->rtable, qual, argv_ro);
238 status = do_update(q, &Argv[q->argc], qual, action, actarg);
239 incremental_after(q->rtable, qual, argv_ro);
240 if (status != MR_SUCCESS) break;
241 flush_name(argv_ro[0], q->rtable);
03c05291 242 table = table_name[q->rtable];
b070f8a1 243 if (strcmp(q->shortname, "sshi") && strcmp(q->shortname, "ssif")) {
048b1b94 244 EXEC SQL UPDATE tblstats
03c05291 245 SET updates = updates + 1, modtime = SYSDATE
246 WHERE table_name = :table;
b070f8a1 247 }
248 }
249
250 /* execute followup routine (if any) */
251 if (v->post_rtn) status = (*v->post_rtn)(q, Argv, cl);
252
253 break;
254
255 case APPEND:
256 /* see if row already exists */
257 if (v->field) {
258 status = validate_row(q, Argv, v);
259 if (status != MR_NO_MATCH) break;
260 }
261
b070f8a1 262 /* perform the append */
263 /* if q->rvar = NULL, perform post_rtn only */
264 if (q->rvar) {
60140d51 265 if (q->qual) qual = build_qual(q->qual, q->argc, Argv);
b070f8a1 266 incremental_clear_before();
60140d51 267 status = do_append(q, &Argv[q->argc], qual, action, actarg);
b070f8a1 268 if (status != MR_SUCCESS) break;
269 if (v && v->object_id) {
60140d51 270 qual = realloc(qual, 128);
1b247a36 271 sprintf(qual, "%s.%s = %s",q->rvar, v->object_id,
45bf7573 272 Argv[q->argc+q->vcnt]);
b070f8a1 273 incremental_after(q->rtable, qual, argv_ro);
274 } else
60140d51 275 incremental_after(q->rtable, qual, argv_ro);
b070f8a1 276
03c05291 277 table = table_name[q->rtable];
048b1b94 278 EXEC SQL UPDATE tblstats
03c05291 279 SET appends = appends + 1, modtime = SYSDATE
280 WHERE table_name = :table;
b070f8a1 281 }
282
283 /* execute followup routine */
284 if (v->post_rtn) status = (*v->post_rtn)(q, Argv, cl);
285 break;
286
287 case DELETE:
288 /* see if row already exists */
289 if (v->field) {
290 status = validate_row(q, Argv, v);
291 if (status != MR_EXISTS) break;
292 }
293
294 /* build "where" clause and perform delete */
295 /* if q->rvar = NULL, perform post_rtn only */
296 if (q->rvar) {
60140d51 297 qual = build_qual(q->qual, q->argc, Argv);
03c05291 298 table = table_name[q->rtable];
048b1b94 299 incremental_before(q->rtable, qual, argv_ro);
b070f8a1 300 status = do_delete(q, qual, action, actarg);
301 incremental_clear_after();
302 if (status != MR_SUCCESS) break;
303 flush_name(argv_ro[0], q->rtable);
048b1b94 304 EXEC SQL UPDATE tblstats
03c05291 305 SET deletes = deletes + 1, modtime = SYSDATE
306 WHERE table_name = :table;
b070f8a1 307 }
308
309 /* execute followup routine */
310 if (v->post_rtn) status = (*v->post_rtn)(q, Argv, cl);
311 break;
312
313 }
314
315out:
03c05291 316 if (status == MR_SUCCESS && dbms_errno != 0) {
317 com_err(whoami, MR_INTERNAL, "Server didn't notice DBMS ERROR %d",
318 dbms_errno);
b070f8a1 319 status = mr_errcode;
320 }
321
048b1b94 322 if (q->type == RETRIEVE) {
048b1b94 323 EXEC SQL COMMIT WORK;
048b1b94 324 } else {
b070f8a1 325 if (status == MR_SUCCESS) {
048b1b94 326 EXEC SQL COMMIT WORK;
b070f8a1 327 if (journal) {
03c05291 328 char buf[1024];
b070f8a1 329 int i;
330 extern time_t now;
331
332 fprintf(journal, "%% %s %s %s",
333 cl->clname, cl->entity, ctime(&now));
334 fprintf(journal, "%s[%d] ", q->name, cl->args->mr_version_no);
335 for (i = 0; i < argc; i++) {
336 if (i != 0) {
337 putc(' ', journal);
338 }
339 requote(buf, argv_ro[i], sizeof(buf));
340 fputs(buf, journal);
341 }
342 putc('\n', journal);
343 fflush(journal);
344 }
345 incremental_update();
346 } else {
1a41acb7 347 cache_abort();
03c05291 348 EXEC SQL ROLLBACK WORK;
b070f8a1 349 incremental_flush();
350 }
351 }
1a41acb7 352 cache_commit(); /* commit following abort is safe */
60140d51 353 if (qual) free(qual);
b070f8a1 354
355 if (status != MR_SUCCESS && log_flags & LOG_RES)
356 com_err(whoami, status, " (Query failed)");
357 return(status);
358}
359
60140d51 360char *build_qual(fmt_buf, argc, argv)
361 char *fmt_buf, *argv[];
03c05291 362 int argc;
b070f8a1 363{
60140d51 364 char *res, *result_buf, *fmt, *arg, *like;
365
366 result_buf = malloc(2*(strlen(fmt_buf) + argc*ARGLEN));
367
368 res = result_buf;
369 fmt = fmt_buf;
370
371 /* Look through the format for LIKE expressions and arguments.
372 Substitute in the arguments, and simplify the `LIKE's to `='s
373 where possible. */
374
375 while (*fmt) {
376 like = strstr(fmt, "LIKE");
377 arg = strchr(fmt, '%');
378
379 if (!like && !arg) {
380 /* only plain text remains */
381 strcpy(res, fmt);
382 break;
383 } else if (!like || arg < like) {
384 /* copy to arg, then substitute */
385 strncpy(res, fmt, arg - fmt - 1);
386 res += arg - fmt;
387 if(*++arg) {
388 switch(*arg++) {
389 case '%':
390 *res++ = '%';
391 break;
392 case 's':
393 if(*argv[0]) {
394 char *p = *argv;
395 while(*p) {
396 if(*p == '\'') *res++ = '\'';
397 *res++ = *p++;
398 }
03c05291 399 }
60140d51 400 argv++;
401 break;
402 case 'd':
403 res += sprintf(res, "%d", *(int *)*argv++);
404 break;
03c05291 405 }
60140d51 406 }
407 fmt = arg;
408 arg = strchr(fmt, '%');
409 } else {
410 /* copy over up to the arg, then copy and convert the arg */
411 char *p;
412 int escape = 0;
413
414 strncpy(res, fmt, arg - fmt - 1);
415 res += arg - fmt;
416 for (p = *argv++; *p; p++) {
417 switch (*p) {
418 case '\'': *res++ = '\''; *res++ = '\''; break;
419 case '*': *res++ = '%'; break;
420 case '?': *res++ = '_'; break;
421 case '%':
422 case '_': *res++ = '*'; *res++ = *p; escape = 1; break;
423 default: *res++ = *p;
424 }
425 }
426 if (escape) res += sprintf(res, " ESCAPE '*'");
427
428 fmt += 2;
429 arg = strchr(fmt, '%');
430 like = strstr(fmt, "LIKE");
431 }
b070f8a1 432 }
60140d51 433
434 result_buf = realloc(result_buf, strlen(result_buf) + 1);
435 return result_buf;
b070f8a1 436}
437
60140d51 438/* Build argument vector, verify query and arguments */
b070f8a1 439
f1bc925a 440int privileged;
441
03c05291 442int mr_verify_query(cl, q, argc, argv_ro)
443 client *cl;
444 struct query *q;
445 int argc;
446 char *argv_ro[];
b070f8a1 447{
448 register int argreq;
449 register int status;
450 register struct validate *v = q->validate;
451 register int i;
99e09b48 452 register char *to,*fr,*stop;
b070f8a1 453
f1bc925a 454 privileged = 0;
455
b4530d60 456 /* check argument count */
457 argreq = q->argc;
458 if (q->type == UPDATE || q->type == APPEND) argreq += q->vcnt;
459 if (argc != argreq) return(MR_ARGS);
460
b070f8a1 461 /* copy the arguments into a local argv that we can modify */
b070f8a1 462 for (i = 0; i < argc; i++) {
03c05291 463 for (to=Argv[i], fr=argv_ro[i], stop=to+ARGLEN; (*fr) && (to<stop);)
464 *to++ = *fr++;
465
99e09b48 466 if (*fr)
467 return(MR_ARG_TOO_LONG);
468 *to='\0';
469
470 if (*--to == '\\')
b070f8a1 471 return(MR_BAD_CHAR);
472 }
473
474 /* check initial query access */
475 status = check_query_access(q, Argv, cl);
476 if (status != MR_SUCCESS && status != MR_PERM)
477 return(status);
478 if (status == MR_SUCCESS)
479 privileged++;
480
b070f8a1 481 /* validate arguments */
482 if (v && v->valobj) {
483 status = validate_fields(q, Argv, v->valobj, v->objcnt);
484 if (status != MR_SUCCESS) return(status);
485 }
486
487 /* perform special query access check */
488 if (!privileged && v && v->acs_rtn) {
489 status = (*v->acs_rtn)(q, Argv, cl);
490 if (status != MR_SUCCESS && status != MR_PERM)
491 return(status);
492 if (status == MR_SUCCESS)
f1bc925a 493 return(MR_SUCCESS);
b070f8a1 494 }
495
496 return(privileged ? MR_SUCCESS : MR_PERM);
497}
498
499
500/* This routine caches info from the database. Each query acl is stored
501 * in the query structure, and whether that acl contains everybody.
502 */
503
03c05291 504int check_query_access(q, argv, cl)
505 struct query *q;
506 char *argv[];
507 client *cl;
048b1b94 508{
509 EXEC SQL BEGIN DECLARE SECTION;
510 char *name;
511 int acl_id;
048b1b94 512 static int def_uid;
513 EXEC SQL END DECLARE SECTION;
b070f8a1 514
515 /* initialize default uid */
516 if (def_uid == 0) {
048b1b94 517 EXEC SQL SELECT users_id INTO :def_uid FROM users WHERE login='default';
b070f8a1 518 }
519
520 /* get query access control list */
521 if (q->acl != 0)
522 acl_id = q->acl;
523 else {
524 name = q->shortname;
048b1b94 525 EXEC SQL SELECT list_id INTO :acl_id FROM capacls WHERE tag = :name;
03c05291 526 if (sqlca.sqlcode < 0) return(MR_DBMS_ERR);
527 if (sqlca.sqlcode == SQL_NO_MATCH) return(MR_PERM);
b070f8a1 528 q->acl = acl_id;
529
530 /* check for default access */
048b1b94 531 EXEC SQL SELECT member_id INTO :acl_id FROM imembers
532 WHERE list_id = :acl_id AND member_type = 'USER'
533 AND member_id = :def_uid;
534 if (sqlca.sqlerrd[2] == 0)
535 q->everybody = 0;
536 else
537 q->everybody = 1;
b070f8a1 538 }
539
540 if (q->everybody)
541 return(MR_SUCCESS);
542
f3c08a60 543 if (find_member("LIST", acl_id, cl))
b070f8a1 544 return(MR_SUCCESS);
545 else
546 return(MR_PERM);
048b1b94 547}
b070f8a1 548
549
f3c08a60 550int find_member(list_type, list_id, cl)
551 char *list_type;
552 int list_id;
03c05291 553 client *cl;
048b1b94 554{
555 EXEC SQL BEGIN DECLARE SECTION;
f3c08a60 556 int flag, users_id, client_id;
048b1b94 557 EXEC SQL END DECLARE SECTION;
b070f8a1 558
f3c08a60 559 if (!strcmp(strtrim(list_type), "USER") &&
560 list_id == cl->users_id)
561 return(1);
562
563 if (!strcmp(strtrim(list_type), "KERBEROS") &&
564 list_id == -cl->client_id)
b070f8a1 565 return(1);
566
f3c08a60 567 /* see if client is a member of list */
048b1b94 568 flag = 0;
f3c08a60 569 users_id = cl->users_id;
570 client_id = -cl->client_id;
2a467737 571 EXEC SQL SELECT COUNT(member_id) INTO :flag FROM imembers
572 WHERE list_id = :list_id
f3c08a60 573 AND ( ( member_type = 'USER' AND member_id = :users_id )
574 OR (member_type = 'KERBEROS' AND member_id = :client_id ) );
048b1b94 575 if (sqlca.sqlcode == 0)
576 return(flag);
577 return(0);
578}
b070f8a1 579
580
68b5b948 581int do_retrieve(q, pqual, action, actarg)
03c05291 582 struct query *q;
68b5b948 583 char *pqual, *actarg;
03c05291 584 int (*action)();
048b1b94 585{
60140d51 586 char *stmt;
587 int status;
cc0088db 588
60140d51 589 stmt = build_sql_stmt("SELECT",q->tlist,NULL,0,pqual,q->sort);
590 status = do_for_all_rows(stmt_buf, q->vcnt, action, (int)actarg);
591 free(stmt);
592 return status;
cc0088db 593}
594
60140d51 595char *build_sql_stmt(cmd, targetlist, argv, argc, qual, sort)
596 char *cmd, *targetlist, *argv[], *qual, *sort;
597 int argc;
048b1b94 598{
60140d51 599 char *result_buf;
600 int len;
99e09b48 601 register char *res, *fmt;
602
60140d51 603 len = strlen(cmd) + strlen(targetlist) + argc*ARGLEN + 3;
604 if (qual) len += strlen(qual) + 6;
605 if (sort) len += strlen(sort) + 10;
606 result_buf = malloc(len);
607
608 sprintf(result_buf, "%s ", cmd);
99e09b48 609
60140d51 610 for(res=strchr(result_buf, '\0'), fmt=targetlist; *fmt; fmt++) {
99e09b48 611 if(*fmt=='%') {
612 if(*++fmt) {
613 switch(*fmt) {
60140d51 614 case '%':
99e09b48 615 *res++ = *fmt;
616 break;
617 case 's':
618 if(*argv[0]) {
03c05291 619 char *p=*argv;
620 while(*p) {
60140d51 621 if(*p=='\'') *res++='\'';
03c05291 622 *res++=*p++;
623 }
99e09b48 624 }
625 argv++;
626 break;
627 case 'd':
03c05291 628 res+=sprintf(res,"%d",*(int *)*argv++);
99e09b48 629 break;
60140d51 630 default:
99e09b48 631 break;
632 }
633 } else break;
60140d51 634 } else *res++ = *fmt;
048b1b94 635 }
636 *res='\0';
cc0088db 637
60140d51 638 if (qual)
639 res += sprintf(res, " WHERE %s", qual);
640
641 if(sort)
642 res += sprintf(res, " ORDER BY %s", sort);
643
644 result_buf = realloc(result_buf, strlen(result_buf));
645
646 return result_buf;
048b1b94 647}
b070f8a1 648
03c05291 649int do_update(q, argv, qual, action, actarg)
650 struct query *q;
651 char *argv[], *qual, *actarg;
652 int (*action)();
048b1b94 653{
60140d51 654 EXEC SQL BEGIN DECLARE SECTION;
655 char *stmt;
656 EXEC SQL END DECLARE SECTION;
657
658 stmt = build_sql_stmt("UPDATE",q->tlist,argv,q->vcnt,qual,NULL);
659 EXEC SQL EXECUTE IMMEDIATE :stmt;
660 free(stmt);
b070f8a1 661 if (mr_errcode) return(mr_errcode);
662 return(MR_SUCCESS);
048b1b94 663}
b070f8a1 664
03c05291 665int do_append(q, argv, pqual, action, actarg)
666 struct query *q;
667 char *argv[], *pqual, *actarg;
668 int (*action)();
048b1b94 669{
60140d51 670 EXEC SQL BEGIN DECLARE SECTION;
671 char *stmt;
672 EXEC SQL END DECLARE SECTION;
673
674 stmt = build_sql_stmt("INSERT",q->tlist,argv,q->vcnt,pqual,NULL);
675 EXEC SQL EXECUTE IMMEDIATE :stmt;
676 free(stmt);
b070f8a1 677 if (mr_errcode) return(mr_errcode);
678 return(MR_SUCCESS);
048b1b94 679}
b070f8a1 680
03c05291 681int do_delete(q, qual, action, actarg)
682 struct query *q;
683 char *qual, *actarg;
684 int (*action)();
048b1b94 685{
03c05291 686 sprintf(stmt_buf,"DELETE FROM %s WHERE %s",table_name[q->rtable],qual);
048b1b94 687 EXEC SQL EXECUTE IMMEDIATE :stmt_buf;
b070f8a1 688 if (mr_errcode) return(mr_errcode);
689 return(MR_SUCCESS);
048b1b94 690}
b070f8a1 691
692
693/**
694 ** set_next_object_id - set next object id in values table
695 **
696 ** Inputs: object - object name in values table and in objects
697 ** table - name of table objects are found in
1a41acb7 698 ** limit - should the ID be range limited
b070f8a1 699 **
700 ** - called before an APPEND operation to set the next object id to
701 ** be used for the new record to the next free value
702 **
703 **/
704
03c05291 705int set_next_object_id(object, table, limit)
706EXEC SQL BEGIN DECLARE SECTION;
707 char *object;
708EXEC SQL END DECLARE SECTION;
709 enum tables table;
710 int limit;
048b1b94 711{
712 EXEC SQL BEGIN DECLARE SECTION;
c9a214e4 713 int value;
048b1b94 714 EXEC SQL END DECLARE SECTION;
45bf7573 715 int starting_value;
716
048b1b94 717 EXEC SQL SELECT value INTO :value FROM numvalues WHERE name = :object;
718 if (sqlca.sqlerrd[2] != 1)
1a41acb7 719 return(MR_NO_ID);
720
45bf7573 721 starting_value=value;
c9a214e4 722 while (1) {
45bf7573 723 if (limit && value > MAX_ID_VALUE)
724 value = MIN_ID_VALUE;
725
03c05291 726 sprintf(stmt_buf,"SELECT %s FROM %s WHERE %s=%d",
727 object,table_name[table],object,value);
728 dosql(sqlbuffer);
45bf7573 729 if (sqlca.sqlcode < 0) return(mr_errcode);
03c05291 730 if (sqlca.sqlcode == SQL_NO_MATCH) break;
45bf7573 731
45bf7573 732 value++;
733 if (limit && value == starting_value) {
734 com_err(whoami,0,"All id values have been used");
735 return(MR_NO_ID);
736 }
b070f8a1 737 }
738
739 if (LOG_RES)
c9a214e4 740 com_err(whoami, 0, "setting ID %s to %d", object, value);
048b1b94 741 EXEC SQL UPDATE numvalues SET value = :value WHERE name = :object;
b070f8a1 742 return(MR_SUCCESS);
048b1b94 743}
b070f8a1 744
745
746/* Turn a kerberos name into the user's ID of the account that principal
747 * owns. Sets the kerberos ID and user ID.
748 */
749
750int set_krb_mapping(name, login, ok, kid, uid)
03c05291 751 char *name, *login;
752 int ok, *kid, *uid;
048b1b94 753{
754 EXEC SQL BEGIN DECLARE SECTION;
755 int u_id, k_id;
756 char *krbname;
757 EXEC SQL END DECLARE SECTION;
b070f8a1 758
759 krbname = name;
760 *kid = 0;
761 *uid = 0;
762
fbeb0263 763 EXEC SQL SELECT km.users_id, km.string_id INTO :u_id, :k_id
764 FROM krbmap km, strings str
765 WHERE km.string_id = str.string_id AND str.string = :krbname;
3e6ff9e4 766 EXEC SQL COMMIT WORK;
048b1b94 767
03c05291 768 if (dbms_errno) return(mr_errcode);
b070f8a1 769
048b1b94 770 if (sqlca.sqlerrd[2] == 1) { /* rowcount */
b070f8a1 771 *kid = -k_id;
772 *uid = u_id;
773 return(MR_SUCCESS);
774 }
775
03c05291 776 if (name_to_id(name, STRINGS_TABLE, &k_id) == MR_SUCCESS)
b070f8a1 777 *kid = -k_id;
778
048b1b94 779 if (!ok) {
b070f8a1 780 *uid = *kid;
781 return(MR_SUCCESS);
782 }
783
03c05291 784 if (name_to_id(login, USERS_TABLE, uid) != MR_SUCCESS)
b070f8a1 785 *uid = 0;
786
787 if (*kid == 0)
788 *kid = *uid;
03c05291 789 if (dbms_errno) return(mr_errcode);
b070f8a1 790 return(MR_SUCCESS);
048b1b94 791}
b070f8a1 792
793
794/* For now this just checks the argc's. It should also see that there
795 * are no duplicate names.
796 */
797
03c05291 798void sanity_check_queries(void)
b070f8a1 799{
800 register int i;
801 int maxv = 0, maxa = 0;
b070f8a1 802 extern int QueryCount2;
803 extern struct query Queries2[];
b070f8a1 804
805#define MAX(x,y) ((x) > (y) ? (x) : (y))
806
b070f8a1 807 for (i = 0; i < QueryCount2; i++) {
808 maxv = MAX(maxv, Queries2[i].vcnt);
809 maxa = MAX(maxa, Queries2[i].argc);
810 }
811 if (MAX(maxv, maxa) > QMAXARGS) {
812 com_err(whoami, 0, "A query has more args than QMAXARGS");
813 exit(1);
814 }
815}
048b1b94 816
03c05291 817
818/* Generically do a SELECT, storing the results in the provided buffers */
819
820void dosql(buffers)
821 char *buffers[];
822{
823 int i, errcode=0, errlen;
824
825 EXEC SQL PREPARE inc_stmt FROM :stmt_buf;
826 if(sqlca.sqlcode) return;
827 EXEC SQL DECLARE inc_crs CURSOR FOR inc_stmt;
828 EXEC SQL OPEN inc_crs;
829 mr_sqlda->N = QMAXARGS;
830 EXEC SQL DESCRIBE SELECT LIST FOR inc_stmt INTO mr_sqlda;
831 mr_sqlda->N = mr_sqlda->F;
832 for(i=0; i<mr_sqlda->N; i++) {
833 mr_sqlda->V[i]=buffers[i];
834 mr_sqlda->T[i]=97;
835 mr_sqlda->L[i]=ARGLEN;
836 }
837 EXEC SQL FETCH inc_crs USING DESCRIPTOR mr_sqlda;
838
839 /* if we got an error from the FETCH, we have to preserve it or the
840 close will reset it and the caller will think nothing happened */
841 if(sqlca.sqlcode) {
842 errcode=sqlca.sqlcode;
843 errlen=sqlca.sqlerrm.sqlerrml;
844 }
845
846 EXEC SQL CLOSE inc_crs;
847 if(errcode) {
848 sqlca.sqlcode=errcode;
849 sqlca.sqlerrm.sqlerrml=errlen;
850 }
851}
852
853int do_for_all_rows(query, count, action, actarg)
854EXEC SQL BEGIN DECLARE SECTION;
855 char *query;
856EXEC SQL END DECLARE SECTION;
857 int count, (*action)(), actarg;
858{
859 int i, rowcount=0;
860
861 EXEC SQL PREPARE stmt FROM :query;
862 if(sqlca.sqlcode) return;
863 EXEC SQL DECLARE curs CURSOR FOR stmt;
864 EXEC SQL OPEN curs;
865 mr_sqlda->N = count;
866 EXEC SQL DESCRIBE SELECT LIST FOR stmt INTO mr_sqlda;
867 mr_sqlda->N = mr_sqlda->F;
868 for(i=0; i<mr_sqlda->N; i++) {
869 mr_sqlda->V[i]=sqlbuffer[i];
870 mr_sqlda->T[i]=97;
871 mr_sqlda->L[i]=ARGLEN;
872 }
873
874 while(1) {
875 EXEC SQL FETCH curs USING DESCRIPTOR mr_sqlda;
876 if(sqlca.sqlcode != 0) break;
877 (*action)(count, sqlbuffer, actarg);
878 rowcount++;
879 }
880 EXEC SQL CLOSE curs;
881
882 if (mr_errcode) return(mr_errcode);
883 return ((rowcount == 0) ? MR_NO_MATCH : MR_SUCCESS);
884}
885
886
048b1b94 887/* eof:qrtn.dc */
This page took 0.398885 seconds and 5 git commands to generate.