]> andersk Git - moira.git/blob - server/qrtn.pc
Move UNIX->SQL wildcard conversion from the validation stage to the
[moira.git] / server / qrtn.pc
1 /*
2  *      $Source$
3  *      $Author$
4  *      $Header$
5  *
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  * 
10  */
11
12 #ifndef lint
13 static char *rcsid_qrtn_dc = "$Header$";
14 #endif lint
15
16 #include <mit-copyright.h>
17 #include <string.h>
18 #include "mr_server.h"
19 #include "query.h"
20 EXEC SQL INCLUDE sqlca;  /* SQL Communications Area */
21 EXEC SQL INCLUDE sqlda;  /* SQL Descriptor Area */
22 #include "qrtn.h"
23
24 SQLDA *mr_sqlda;
25 EXEC SQL BEGIN DECLARE SECTION; 
26 int mr_sig_length;
27 int idummy;                            
28 char cdummy[MR_CDUMMY_LEN];            
29 char stmt_buf[MR_STMTBUF_LEN];
30 EXEC SQL END DECLARE SECTION; 
31
32 char *Argv[QMAXARGS];
33 extern char *table_name[];
34 extern char *sqlbuffer[QMAXARGS];
35
36 int dbms_errno = 0;
37 int mr_errcode = 0;
38 EXEC SQL BEGIN DECLARE SECTION; 
39 int query_timeout = 30;
40 char *database = "moira";
41 EXEC SQL END DECLARE SECTION;
42 extern char *whoami;
43 extern FILE *journal;
44
45 int mr_verify_query(client *cl, struct query *q, int argc, char *argv_ro[]);
46 int do_retrieve(struct query *q, char *pqual, int (*action)(), char *actarg);
47 int do_update(struct query *q, char *argv[], char *qual,
48               int (*action)(), char *actarg);
49 int do_append(struct query *q, char *argv[], char *pqual,
50               int (*action)(), char *actarg);
51 int do_delete(struct query *q, char *qual,
52               int (*action)(), char *actarg);
53 char *build_sql_stmt(char *cmd, char *targetlist, char *argv[],
54                      int argc, char *qual, char *sort);
55
56 /* from qvalidate.dc */
57 int validate_fields(struct query *q, char *argv[], struct valobj *vo, int n);
58 int validate_row(struct query *q, char *argv[], struct validate *v);
59
60
61 /*
62  * dbmserr: Called when the DBMS indicates an error.
63  */
64
65 void dbmserr(void)
66 {
67     EXEC SQL BEGIN DECLARE SECTION; 
68     char err_msg[256];
69     EXEC SQL END DECLARE SECTION;
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);
80 }
81
82 /* This is declarative, not executed.  Applies from here on, in this file. */
83 EXEC SQL WHENEVER SQLERROR DO dbmserr();
84
85 int mr_open_database(void)
86 {
87     register int i;
88     SQLDA *mr_alloc_sqlda();
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
98         mr_sqlda = mr_alloc_sqlda();       
99
100         incremental_init();
101         flush_cache();
102     }
103         
104     dbms_errno = 0;
105     mr_errcode = 0;
106
107     /* open the database */
108     EXEC SQL CONNECT :database IDENTIFIED BY :database;
109
110     if(dbms_errno) 
111         return(mr_errcode);
112
113     EXEC SQL SELECT data_length INTO :mr_sig_length FROM user_tab_columns WHERE table_name='USERS' and column_name='SIGNATURE';
114     EXEC SQL COMMIT WORK;
115     if(dbms_errno)
116         return(mr_errcode);
117
118     return(MR_SUCCESS);
119 }
120
121 void mr_close_database(void)
122 {
123     flush_cache();
124     EXEC SQL COMMIT RELEASE;
125 }
126
127 int mr_check_access(cl, name, argc, argv_ro)
128      client *cl;
129      char *name, *argv_ro[];
130      int argc;
131 {
132     struct query *q;
133
134     dbms_errno = 0;
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
141     return(mr_verify_query(cl, q, argc, argv_ro));    
142 }
143
144 int mr_process_query(cl, name, argc, argv_ro, action, actarg)
145      client *cl;
146      char *name, *argv_ro[], *actarg;
147      int argc, (*action)();
148 {
149     register struct query *q;
150     register int status;
151     register struct validate *v;
152     char *qual = NULL;
153     EXEC SQL BEGIN DECLARE SECTION; 
154     char *table;
155     EXEC SQL END DECLARE SECTION; 
156     struct save_queue *sq;
157
158     dbms_errno = 0;
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) {
198             status = validate_row(q, Argv, v); 
199             if (status != MR_EXISTS) break;
200         }
201
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) {
206             if (q->qual) qual = build_qual(q->qual, q->argc, Argv);
207             if (v && v->post_rtn) {
208                 sq = sq_create();
209                 status = do_retrieve(q, qual, sq_save_args, (char *)sq);
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 */
217                 status = do_retrieve(q, qual, action, actarg);
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) {
236             qual = build_qual(q->qual, q->argc, Argv);
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);
242             table = table_name[q->rtable];
243             if (strcmp(q->shortname, "sshi") && strcmp(q->shortname, "ssif")) {
244                 EXEC SQL UPDATE tblstats
245                   SET updates = updates + 1, modtime = SYSDATE
246                   WHERE table_name = :table;
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
262         /* perform the append */
263         /* if q->rvar = NULL, perform post_rtn only */
264         if (q->rvar) {
265             if (q->qual) qual = build_qual(q->qual, q->argc, Argv);
266             incremental_clear_before();
267             status = do_append(q, &Argv[q->argc], qual, action, actarg);
268             if (status != MR_SUCCESS) break;
269             if (v && v->object_id) {
270                 qual = realloc(qual, 128);
271                 sprintf(qual, "%s.%s = %s",q->rvar, v->object_id, 
272                         Argv[q->argc+q->vcnt]);
273                 incremental_after(q->rtable, qual, argv_ro);
274             } else
275               incremental_after(q->rtable, qual, argv_ro);
276
277             table = table_name[q->rtable];
278             EXEC SQL UPDATE tblstats
279               SET appends = appends + 1, modtime = SYSDATE
280               WHERE table_name = :table;
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) {
297             qual = build_qual(q->qual, q->argc, Argv);
298             table = table_name[q->rtable];
299             incremental_before(q->rtable, qual, argv_ro);
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);
304             EXEC SQL UPDATE tblstats
305               SET deletes = deletes + 1, modtime = SYSDATE
306               WHERE table_name = :table;
307         }
308
309         /* execute followup routine */
310         if (v->post_rtn) status = (*v->post_rtn)(q, Argv, cl);
311         break;
312
313     }
314
315 out:
316     if (status == MR_SUCCESS && dbms_errno != 0) {
317         com_err(whoami, MR_INTERNAL, "Server didn't notice DBMS ERROR %d",
318                        dbms_errno);
319         status = mr_errcode;
320     }
321
322     if (q->type == RETRIEVE) {
323         EXEC SQL COMMIT WORK;
324     } else {
325         if (status == MR_SUCCESS) {
326         EXEC SQL COMMIT WORK;
327             if (journal) {
328                 char buf[1024];
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 {
347             cache_abort();
348             EXEC SQL ROLLBACK WORK;
349             incremental_flush();
350         }
351     }
352     cache_commit(); /* commit following abort is safe */
353     if (qual) free(qual);
354
355     if (status != MR_SUCCESS && log_flags & LOG_RES)
356         com_err(whoami, status, " (Query failed)");
357     return(status);
358 }
359
360 char *build_qual(fmt_buf, argc, argv)
361      char *fmt_buf, *argv[];
362      int argc;
363 {
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                             }
399                         }
400                         argv++;
401                         break;
402                     case 'd':
403                         res += sprintf(res, "%d", *(int *)*argv++);
404                         break;
405                 }
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         }
432     }
433
434     result_buf = realloc(result_buf, strlen(result_buf) + 1);
435     return result_buf;
436 }
437
438 /* Build argument vector, verify query and arguments */
439
440 int privileged;
441
442 int mr_verify_query(cl, q, argc, argv_ro)
443      client *cl;
444      struct query *q;
445      int argc;
446      char *argv_ro[];
447 {
448     register int argreq;
449     register int status;
450     register struct validate *v = q->validate;
451     register int i;
452     register char *to,*fr,*stop;
453
454     privileged = 0;
455
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
461     /* copy the arguments into a local argv that we can modify */
462     for (i = 0; i < argc; i++) {
463         for (to=Argv[i], fr=argv_ro[i], stop=to+ARGLEN; (*fr) && (to<stop);)
464           *to++ = *fr++;
465
466         if (*fr) 
467           return(MR_ARG_TOO_LONG);
468         *to='\0';
469
470         if (*--to == '\\')
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
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)
493             return(MR_SUCCESS);
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
504 int check_query_access(q, argv, cl)
505      struct query *q;
506      char *argv[];
507      client *cl;
508 {
509     EXEC SQL BEGIN DECLARE SECTION; 
510     char *name;
511     int acl_id;
512     static int def_uid;
513     EXEC SQL END DECLARE SECTION; 
514
515     /* initialize default uid */
516     if (def_uid == 0) {
517         EXEC SQL SELECT users_id INTO :def_uid FROM users WHERE login='default';
518     }
519
520     /* get query access control list */
521     if (q->acl != 0)
522       acl_id = q->acl;
523     else {
524         name = q->shortname;
525         EXEC SQL SELECT list_id INTO :acl_id FROM capacls WHERE tag = :name;
526         if (sqlca.sqlcode < 0) return(MR_DBMS_ERR);
527         if (sqlca.sqlcode == SQL_NO_MATCH) return(MR_PERM);
528         q->acl = acl_id;
529
530         /* check for default access */
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;
538     }
539
540     if (q->everybody)
541       return(MR_SUCCESS);
542
543     if (find_member("LIST", acl_id, cl))
544       return(MR_SUCCESS);
545     else
546       return(MR_PERM);
547 }
548
549
550 int find_member(list_type, list_id, cl)
551      char *list_type;
552      int list_id;
553      client *cl;
554 {
555     EXEC SQL BEGIN DECLARE SECTION; 
556     int flag, users_id, client_id;
557     EXEC SQL END DECLARE SECTION; 
558
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)
565         return(1);
566
567     /* see if client is a member of list */
568     flag = 0;
569     users_id = cl->users_id;
570     client_id = -cl->client_id;
571     EXEC SQL SELECT COUNT(member_id) INTO :flag FROM imembers
572         WHERE list_id = :list_id
573         AND ( ( member_type = 'USER' AND member_id = :users_id )
574               OR (member_type = 'KERBEROS' AND member_id = :client_id ) );
575     if (sqlca.sqlcode == 0)
576       return(flag);            
577     return(0);
578 }
579
580
581 int do_retrieve(q, pqual, action, actarg)
582      struct query *q;
583      char *pqual, *actarg;
584      int (*action)();
585 {
586     char *stmt;
587     int status;
588
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;
593 }
594
595 char *build_sql_stmt(cmd, targetlist, argv, argc, qual, sort)
596      char *cmd, *targetlist, *argv[], *qual, *sort;
597      int argc;
598 {
599     char *result_buf;
600     int len;
601     register char *res, *fmt;
602
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);
609
610     for(res=strchr(result_buf, '\0'), fmt=targetlist; *fmt; fmt++) {
611         if(*fmt=='%') {
612             if(*++fmt) {
613                 switch(*fmt) {                       
614                   case '%':
615                     *res++ = *fmt;           
616                     break;
617                   case 's':
618                     if(*argv[0]) {
619                         char *p=*argv;
620                         while(*p) {
621                             if(*p=='\'') *res++='\'';
622                             *res++=*p++;
623                         }
624                     }
625                     argv++;
626                     break;
627                   case 'd':
628                     res+=sprintf(res,"%d",*(int *)*argv++);
629                     break;
630                   default:
631                     break;
632                 }
633             } else break;
634         } else *res++ = *fmt;
635     }
636     *res='\0';
637
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;
647 }
648
649 int do_update(q, argv, qual, action, actarg)
650      struct query *q;
651      char *argv[], *qual, *actarg;
652      int (*action)();
653 {
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);
661     if (mr_errcode) return(mr_errcode);
662     return(MR_SUCCESS);
663 }
664
665 int do_append(q, argv, pqual, action, actarg)
666      struct query *q;
667      char *argv[], *pqual, *actarg;
668      int (*action)();
669 {
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);
677     if (mr_errcode) return(mr_errcode);
678     return(MR_SUCCESS);
679 }
680
681 int do_delete(q, qual, action, actarg)
682      struct query *q;
683      char *qual, *actarg;
684      int (*action)();
685 {
686     sprintf(stmt_buf,"DELETE FROM %s WHERE %s",table_name[q->rtable],qual);
687     EXEC SQL EXECUTE IMMEDIATE :stmt_buf;
688     if (mr_errcode) return(mr_errcode);
689     return(MR_SUCCESS);
690 }
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
698  **         limit - should the ID be range limited
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
705 int set_next_object_id(object, table, limit)
706 EXEC SQL BEGIN DECLARE SECTION;
707      char *object;
708 EXEC SQL END DECLARE SECTION;
709      enum tables table;
710      int limit;
711 {
712     EXEC SQL BEGIN DECLARE SECTION; 
713     int value;
714     EXEC SQL END DECLARE SECTION;
715     int starting_value;
716     
717     EXEC SQL SELECT value INTO :value FROM numvalues WHERE name = :object;
718     if (sqlca.sqlerrd[2] != 1)
719         return(MR_NO_ID);
720
721     starting_value=value;
722     while (1) { 
723         if (limit && value > MAX_ID_VALUE) 
724             value = MIN_ID_VALUE;
725
726         sprintf(stmt_buf,"SELECT %s FROM %s WHERE %s=%d",
727                 object,table_name[table],object,value);
728         dosql(sqlbuffer);
729         if (sqlca.sqlcode < 0) return(mr_errcode);
730         if (sqlca.sqlcode == SQL_NO_MATCH) break;
731
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         }
737     }
738
739     if (LOG_RES)
740         com_err(whoami, 0, "setting ID %s to %d", object, value);
741     EXEC SQL UPDATE numvalues SET value = :value WHERE name = :object;
742     return(MR_SUCCESS);
743 }
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
750 int set_krb_mapping(name, login, ok, kid, uid)
751      char *name, *login;
752      int ok, *kid, *uid;
753 {
754     EXEC SQL BEGIN DECLARE SECTION; 
755     int u_id, k_id;
756     char *krbname;
757     EXEC SQL END DECLARE SECTION; 
758
759     krbname = name;
760     *kid = 0;
761     *uid = 0;
762
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;
766     EXEC SQL COMMIT WORK;
767
768     if (dbms_errno) return(mr_errcode);
769     
770     if (sqlca.sqlerrd[2] == 1) {    /* rowcount */
771         *kid = -k_id;
772         *uid = u_id;
773         return(MR_SUCCESS);
774     }
775
776     if (name_to_id(name, STRINGS_TABLE, &k_id) == MR_SUCCESS)
777       *kid = -k_id;
778
779     if (!ok) {             
780         *uid = *kid;
781         return(MR_SUCCESS);
782     }
783
784     if (name_to_id(login, USERS_TABLE, uid) != MR_SUCCESS)
785       *uid = 0;
786
787     if (*kid == 0)
788       *kid = *uid;
789     if (dbms_errno) return(mr_errcode);
790     return(MR_SUCCESS);
791 }
792
793
794 /* For now this just checks the argc's.  It should also see that there
795  * are no duplicate names.
796  */
797
798 void sanity_check_queries(void)
799 {
800     register int i;
801     int maxv = 0, maxa = 0;
802     extern int QueryCount2;
803     extern struct query Queries2[];
804
805 #define MAX(x,y) ((x) > (y) ? (x) : (y))
806
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 }
816
817
818 /* Generically do a SELECT, storing the results in the provided buffers */
819
820 void 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
853 int do_for_all_rows(query, count, action, actarg)
854 EXEC SQL BEGIN DECLARE SECTION;
855      char *query;
856 EXEC 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
887 /* eof:qrtn.dc */
This page took 0.413584 seconds and 5 git commands to generate.