]> andersk Git - moira.git/blob - server/qsupport.pc
b7b4fdbac4debdeef3e57963914b02cbdc4ac73a
[moira.git] / server / qsupport.pc
1 /* $Id$
2  *
3  * Special query routines
4  *
5  * Copyright (C) 1987-1998 by the Massachusetts Institute of Technology
6  * For copying and distribution information, please see the file
7  * <mit-copyright.h>.
8  */
9
10 #include <mit-copyright.h>
11 #include "mr_server.h"
12 #include "query.h"
13 #include "qrtn.h"
14
15 #include <ctype.h>
16 #include <stdlib.h>
17 #include <string.h>
18
19 EXEC SQL INCLUDE sqlca;
20
21 RCSID("$Header$");
22
23 extern char *whoami, *table_name[];
24 extern int dbms_errno, mr_errcode;
25
26 EXEC SQL BEGIN DECLARE SECTION;
27 extern char stmt_buf[];
28 EXEC SQL END DECLARE SECTION;
29
30 EXEC SQL WHENEVER SQLERROR DO dbmserr();
31
32 int get_ace_internal(char *atypex, int aid,
33                      int (*action)(int, char *[], void *), void *actarg);
34 int qualified_get(struct query *q, char *argv[],
35                   int (*action)(int, char *[], void *), void *actarg,
36                   char *start, char *range, char *field, char *flags[]);
37
38
39 /* set_pobox - this does all of the real work.
40  *       argv = user_id, type, box
41  * if type is POP, then box should be a machine, and its ID should be put in
42  * pop_id.  If type is IMAP, then box should be a filesys, and its ID should
43  * be put in pop_id.  If type is SMTP, then box should be a string and its
44  * ID should be put in box_id.  If type is NONE, then box doesn't matter.
45  */
46
47 int set_pobox(struct query *q, char **argv, client *cl)
48 {
49   EXEC SQL BEGIN DECLARE SECTION;
50   int user, id;
51   char *box, potype[USERS_POTYPE_SIZE];
52   EXEC SQL END DECLARE SECTION;
53   int status;
54
55   box = argv[2];
56   user = *(int *)argv[0];
57
58   EXEC SQL SELECT pop_id, potype INTO :id, :potype FROM users
59     WHERE users_id = :user;
60   if (dbms_errno)
61     return mr_errcode;
62   if (!strcmp(strtrim(potype), "POP") ||
63       (!strcmp(strtrim(potype), "SPLIT") && id))
64     set_pop_usage(id, -1);
65
66   if (!strcmp(argv[1], "POP"))
67     {
68       status = name_to_id(box, MACHINE_TABLE, &id);
69       if (status == MR_NO_MATCH)
70         return MR_MACHINE;
71       else if (status)
72         return status;
73       EXEC SQL UPDATE users SET potype = 'POP', pop_id = :id, imap_id = 0
74         WHERE users_id = :user;
75       set_pop_usage(id, 1);
76     }
77   else if (!strcmp(argv[1], "SMTP") || !strcmp(argv[1], "SPLIT"))
78     {
79       if (strchr(box, '/') || strchr(box, '|'))
80         return MR_BAD_CHAR;
81       status = name_to_id(box, STRINGS_TABLE, &id);
82       if (status == MR_NO_MATCH)
83         id = add_string(box);
84       else if (status)
85         return status;
86
87       /* If going from SMTP or NONE to SPLIT, make sure we have a valid
88        * POP or IMAP box.
89        */
90       if ((!strcmp(potype, "SMTP") || !strcmp(potype, "NONE")) && 
91            !strcmp(argv[1], "SPLIT"))
92         {
93           status = set_pobox_pop(q, argv, cl);
94           if (status)
95             return status;
96         }
97       strlcpy(potype, argv[1], sizeof(potype));
98       EXEC SQL UPDATE users SET potype = :potype, box_id = :id
99         WHERE users_id = :user;
100     }
101   else if (!strcmp(argv[1], "IMAP"))
102     {
103       EXEC SQL SELECT filsys_id INTO :id FROM filesys
104         WHERE label = :box AND type = 'IMAP';
105       if (sqlca.sqlcode)
106         return MR_FILESYS;
107       EXEC SQL UPDATE users SET potype = 'IMAP', imap_id = :id, pop_id = 0
108         WHERE users_id = :user;
109     }
110   else /* argv[1] == "NONE" */
111     {
112       EXEC SQL UPDATE users SET potype = 'NONE'
113         WHERE users_id = :user;
114     }
115
116   set_pobox_modtime(q, argv, cl);
117   EXEC SQL UPDATE tblstats SET updates = updates + 1, modtime = SYSDATE
118     WHERE table_name = 'users';
119   if (dbms_errno)
120     return mr_errcode;
121   return MR_SUCCESS;
122 }
123
124 /* set_pobox_pop: Revert to existing POP or IMAP pobox.
125  * Also take care of keeping track of the post office usage.
126  */
127 int set_pobox_pop(struct query *q, char **argv, client *cl)
128 {
129   EXEC SQL BEGIN DECLARE SECTION;
130   int id, pid, iid, mid;
131   char type[USERS_POTYPE_SIZE];
132   EXEC SQL END DECLARE SECTION;
133
134   id = *(int *)argv[0];
135   EXEC SQL SELECT potype, pop_id, imap_id INTO :type, :pid, :iid
136     FROM users WHERE users_id = :id;
137   if (sqlca.sqlerrd[2] == 0 || (pid == 0 && iid == 0))
138     return MR_MACHINE;
139
140   if (pid)
141     {
142       EXEC SQL SELECT mach_id INTO :mid FROM machine
143         WHERE mach_id = :pid;
144       if (sqlca.sqlerrd[2] == 0)
145         return MR_MACHINE;
146       EXEC SQL UPDATE users SET potype = 'POP' WHERE users_id = :id;
147       if (!strcmp(strtrim(type), "POP"))
148         set_pop_usage(mid, 1);
149     }
150   else
151     {
152       EXEC SQL SELECT filsys_id INTO :mid FROM filesys
153         WHERE filsys_id = :iid;
154       if (sqlca.sqlerrd[2] == 0)
155         return MR_MACHINE;
156       EXEC SQL UPDATE users SET potype = 'IMAP' WHERE users_id = :id;
157     }
158
159   set_pobox_modtime(q, argv, cl);
160   EXEC SQL UPDATE tblstats SET updates = updates + 1, modtime = SYSDATE
161     WHERE table_name = 'users';
162   if (dbms_errno)
163     return mr_errcode;
164   return MR_SUCCESS;
165 }
166
167
168 /* Add_member_to_list: do list flattening as we go!  MAXLISTDEPTH is
169  * how many different ancestors a member is allowed to have.
170  */
171
172 #define MAXLISTDEPTH    3072
173
174 int add_member_to_list(struct query *q, char **argv, client *cl)
175 {
176   EXEC SQL BEGIN DECLARE SECTION;
177   int id, lid, mid, tag, error, who, ref, rowcnt;
178   char *mtype, dtype[IMEMBERS_MEMBER_TYPE_SIZE], *entity;
179   EXEC SQL END DECLARE SECTION;
180   int ancestors[MAXLISTDEPTH], aref[MAXLISTDEPTH], acount, a;
181   int descendants[MAXLISTDEPTH], dref[MAXLISTDEPTH], dcount, d;
182   int status;
183   char *dtypes[MAXLISTDEPTH];
184   char *iargv[3], *buf;
185
186   lid = *(int *)argv[0];
187   mtype = argv[1];
188   mid = *(int *)argv[2];
189   tag = !strcmp(q->shortname, "atml") ? *(int *)argv[3] : 0;
190
191   if (acl_access_check(lid, cl))
192     return MR_PERM;
193
194   /* if the member is already a direct member of the list, punt */
195   EXEC SQL SELECT COUNT(list_id) INTO :rowcnt FROM imembers
196     WHERE list_id = :lid AND member_id = :mid
197     AND member_type = :mtype AND direct = 1;
198   if (rowcnt > 0)
199     return MR_EXISTS;
200   if (!strcasecmp(mtype, "STRING"))
201     {
202       buf = malloc(0);
203       status = id_to_name(mid, STRINGS_TABLE, &buf);
204       if (status)
205         return status;
206       if (strchr(buf, '/') || strchr(buf, '|') || strchr(buf, ','))
207         {
208           free(buf);
209           return MR_BAD_CHAR;
210         }
211       free(buf);
212     }
213
214   ancestors[0] = lid;
215   aref[0] = 1;
216   acount = 1;
217   EXEC SQL DECLARE csr103 CURSOR FOR
218     SELECT list_id, ref_count   FROM imembers
219     WHERE member_id = :lid AND member_type = 'LIST';
220   if (dbms_errno)
221     return mr_errcode;
222   EXEC SQL OPEN csr103;
223   if (dbms_errno)
224     return mr_errcode;
225   while (1)
226     {
227       EXEC SQL FETCH csr103 INTO :id, :ref;
228       if (sqlca.sqlcode)
229         break;
230       aref[acount] = ref;
231       ancestors[acount++] = id;
232       if (acount >= MAXLISTDEPTH)
233         break;
234     }
235   EXEC SQL CLOSE csr103;
236   if (dbms_errno)
237     return mr_errcode;
238   if (acount >= MAXLISTDEPTH)
239     return MR_INTERNAL;
240   descendants[0] = mid;
241   dtypes[0] = mtype;
242   dref[0] = 1;
243   dcount = 1;
244   error = 0;
245   if (!strcmp(mtype, "LIST"))
246     {
247       EXEC SQL DECLARE csr104 CURSOR FOR
248         SELECT member_id, member_type, ref_count
249         FROM imembers
250         WHERE list_id = :mid;
251       if (dbms_errno)
252         return mr_errcode;
253       EXEC SQL OPEN csr104;
254       if (dbms_errno)
255         return mr_errcode;
256       while (1)
257         {
258           EXEC SQL FETCH csr104 INTO :id, :dtype, :ref;
259           if (sqlca.sqlcode)
260             break;
261           switch (dtype[0])
262             {
263             case 'L':
264               dtypes[dcount] = "LIST";
265               break;
266             case 'U':
267               dtypes[dcount] = "USER";
268               break;
269             case 'S':
270               dtypes[dcount] = "STRING";
271               break;
272             case 'K':
273               dtypes[dcount] = "KERBEROS";
274               break;
275             case 'M':
276               dtypes[dcount] = "MACHINE";
277               break;
278             default:
279               error++;
280               break;
281             }
282           dref[dcount] = ref;
283           descendants[dcount++] = id;
284           if (dcount >= MAXLISTDEPTH)
285             {
286               error++;
287               break;
288             }
289         }
290       EXEC SQL CLOSE csr104;
291       if (dbms_errno)
292         return mr_errcode;
293       if (error)
294         return MR_INTERNAL;
295     }
296   for (a = 0; a < acount; a++)
297     {
298       lid = ancestors[a];
299       for (d = 0; d < dcount; d++)
300         {
301           mid = descendants[d];
302           mtype = dtypes[d];
303           if (mid == lid && !strcmp(mtype, "LIST"))
304             return MR_LISTLOOP;
305           EXEC SQL SELECT COUNT(ref_count) INTO :rowcnt
306             FROM imembers
307             WHERE list_id = :lid AND member_id = :mid
308             AND member_type = :mtype;
309           ref = aref[a] * dref[d];
310           if (rowcnt > 0)
311             {
312               if (a == 0 && d == 0)
313                 {
314                   EXEC SQL UPDATE imembers
315                     SET ref_count = ref_count + :ref, direct = 1, tag = :tag
316                     WHERE list_id = :lid AND member_id = :mid
317                     AND member_type = :mtype;
318                 }
319               else
320                 {
321                   EXEC SQL UPDATE imembers
322                     SET ref_count = ref_count + :ref
323                     WHERE list_id = :lid AND member_id = :mid
324                     AND member_type = :mtype;
325                 }
326             }
327           else
328             {
329               incremental_clear_before();
330               if (a == 0 && d == 0)
331                 {
332                   EXEC SQL INSERT INTO imembers
333                     (list_id, member_type, member_id, tag, direct, ref_count)
334                     VALUES (:lid, :mtype, :mid, :tag, 1, :ref);
335                 }
336               else
337                 {
338                   EXEC SQL INSERT INTO imembers
339                     (list_id, member_type, member_id, tag, direct, ref_count)
340                     VALUES (:lid, :mtype, :mid, :tag, 0, :ref);
341                 }
342               iargv[0] = (char *)lid;
343               iargv[1] = mtype;
344               iargv[2] = (char *)mid;
345               incremental_after(IMEMBERS_TABLE, 0, iargv);
346             }
347         }
348     }
349   lid = *(int *)argv[0];
350   entity = cl->entity;
351   who = cl->client_id;
352   EXEC SQL UPDATE list
353     SET modtime = SYSDATE, modby = :who, modwith = :entity
354     WHERE list_id = :lid;
355   if (dbms_errno)
356     return mr_errcode;
357   return MR_SUCCESS;
358 }
359
360
361 /* Delete_member_from_list: do list flattening as we go!
362  */
363
364 int delete_member_from_list(struct query *q, char **argv, client *cl)
365 {
366   EXEC SQL BEGIN DECLARE SECTION;
367   int id, lid, mid, cnt, error, who, ref;
368   char *mtype, dtype[IMEMBERS_MEMBER_TYPE_SIZE], *entity;
369   EXEC SQL END DECLARE SECTION;
370   int ancestors[MAXLISTDEPTH], aref[MAXLISTDEPTH], acount, a;
371   int descendants[MAXLISTDEPTH], dref[MAXLISTDEPTH], dcount, d;
372   char *dtypes[MAXLISTDEPTH];
373   char *iargv[3];
374
375   lid = *(int *)argv[0];
376   mtype = argv[1];
377   mid = *(int *)argv[2];
378
379   if (acl_access_check(lid, cl))
380     return MR_PERM;
381
382   /* if the member is not a direct member of the list, punt */
383   EXEC SQL SELECT COUNT(list_id) INTO :cnt FROM imembers
384     WHERE list_id = :lid AND member_id = :mid
385     AND member_type = :mtype AND direct = 1;
386   if (dbms_errno)
387     return mr_errcode;
388   if (cnt == 0)
389     return MR_NO_MATCH;
390   ancestors[0] = lid;
391   aref[0] = 1;
392   acount = 1;
393   EXEC SQL DECLARE csr105 CURSOR FOR
394     SELECT list_id, ref_count FROM imembers
395     WHERE member_id = :lid AND member_type = 'LIST';
396   if (dbms_errno)
397     return mr_errcode;
398   EXEC SQL OPEN csr105;
399   if (dbms_errno)
400     return mr_errcode;
401   while (1)
402     {
403       EXEC SQL FETCH csr105 INTO :id, :ref;
404       if (sqlca.sqlcode)
405         break;
406       aref[acount] = ref;
407       ancestors[acount++] = id;
408       if (acount >= MAXLISTDEPTH)
409         break;
410     }
411   EXEC SQL CLOSE csr105;
412   if (dbms_errno)
413     return mr_errcode;
414   if (acount >= MAXLISTDEPTH)
415     return MR_INTERNAL;
416   descendants[0] = mid;
417   dtypes[0] = mtype;
418   dref[0] = 1;
419   dcount = 1;
420   error = 0;
421   if (!strcmp(mtype, "LIST"))
422     {
423       EXEC SQL DECLARE csr106 CURSOR FOR
424         SELECT member_id, member_type, ref_count FROM imembers
425         WHERE list_id = :mid;
426       if (dbms_errno)
427         return mr_errcode;
428       EXEC SQL OPEN csr106;
429       if (dbms_errno)
430         return mr_errcode;
431       while (1)
432         {
433           EXEC SQL FETCH csr106 INTO :id, :dtype, :ref;
434           if (sqlca.sqlcode)
435             break;
436           switch (dtype[0])
437             {
438             case 'L':
439               dtypes[dcount] = "LIST";
440               break;
441             case 'U':
442               dtypes[dcount] = "USER";
443               break;
444             case 'S':
445               dtypes[dcount] = "STRING";
446               break;
447             case 'K':
448               dtypes[dcount] = "KERBEROS";
449               break;
450             case 'M':
451               dtypes[dcount] = "MACHINE";
452               break;
453             default:
454               error++;
455               break;
456             }
457           dref[dcount] = ref;
458           descendants[dcount++] = id;
459           if (dcount >= MAXLISTDEPTH)
460             break;
461         }
462       EXEC SQL CLOSE csr106;
463       if (dbms_errno)
464         return mr_errcode;
465       if (error)
466         return MR_INTERNAL;
467     }
468   for (a = 0; a < acount; a++)
469     {
470       lid = ancestors[a];
471       for (d = 0; d < dcount; d++)
472         {
473           mid = descendants[d];
474           mtype = dtypes[d];
475           if (mid == lid && !strcmp(mtype, "LIST"))
476             return MR_LISTLOOP;
477           EXEC SQL SELECT ref_count INTO :cnt FROM imembers
478             WHERE list_id = :lid AND member_id = :mid AND member_type = :mtype;
479           ref = aref[a] * dref[d];
480           if (cnt <= ref)
481             {
482               iargv[0] = (char *)lid;
483               iargv[1] = mtype;
484               iargv[2] = (char *)mid;
485               incremental_before(IMEMBERS_TABLE, 0, iargv);
486               EXEC SQL DELETE FROM imembers
487                 WHERE list_id = :lid AND member_id = :mid
488                 AND member_type= :mtype;
489               incremental_clear_after();
490             }
491           else if (a == 0 && d == 0)
492             {
493               EXEC SQL UPDATE imembers
494                 SET ref_count = ref_count - :ref, direct = 0
495                 WHERE list_id = :lid AND member_id = :mid
496                 AND member_type = :mtype;
497             }
498           else
499             {
500               EXEC SQL UPDATE imembers
501                 SET ref_count = ref_count - :ref
502                 WHERE list_id = :lid AND member_id = :mid
503                 AND member_type = :mtype;
504             }
505         }
506     }
507   lid = *(int *)argv[0];
508   entity = cl->entity;
509   who = cl->client_id;
510   EXEC SQL UPDATE list SET modtime = SYSDATE, modby = :who, modwith = :entity
511     WHERE list_id = :lid;
512   if (dbms_errno)
513     return mr_errcode;
514   return MR_SUCCESS;
515 }
516
517 int tag_member_of_list(struct query *q, char **argv, client *cl)
518 {
519   EXEC SQL BEGIN DECLARE SECTION;
520   int lid, mid, cnt, tag;
521   char *mtype;
522   EXEC SQL END DECLARE SECTION;
523   char *iargv[3];
524
525   lid = *(int *)argv[0];
526   mtype = argv[1];
527   mid = *(int *)argv[2];
528   tag = *(int *)argv[3];
529
530   EXEC SQL SELECT COUNT(member_id) INTO :cnt FROM imembers
531     WHERE member_id = :mid AND member_type = :mtype AND
532     list_id = :lid;
533   if (dbms_errno)
534     return mr_errcode;
535   if (cnt == 0)
536     return MR_NO_MATCH;
537
538   incremental_clear_before();
539   EXEC SQL UPDATE imembers SET tag = :tag WHERE list_id = :lid 
540     AND member_type = :mtype AND member_id = :mid;
541   if (dbms_errno)
542     return mr_errcode;
543   
544   iargv[0] = (char *)lid;
545   iargv[1] = mtype;
546   iargv[2] = (char *)mid;
547   incremental_after(IMEMBERS_TABLE, 0, iargv);
548
549   return MR_SUCCESS;
550 }
551
552 /* Don't allow someone to add someone to a list which is the acl of a
553  * query unless they're on the list acl, even if they're on the amtl
554  * query acl! Also, don't allow someone proxying to add someone to a
555  * capacl.
556  */
557 int acl_access_check(int list_id, client *cl)
558 {
559   EXEC SQL BEGIN DECLARE SECTION;
560   int c1, c2, lid = list_id, acl_id, memacl_id;
561   char acl_type[LIST_ACL_TYPE_SIZE], memacl_type[LIST_ACL_TYPE_SIZE];
562   EXEC SQL END DECLARE SECTION;
563
564   /* Check if the list is directly a capacl */
565   EXEC SQL SELECT COUNT(list_id) INTO :c1 FROM capacls WHERE list_id=:lid;
566
567   /* Check if the list is a member (direct or indirect) of a list that
568      is a capacl */
569   EXEC SQL SELECT COUNT(l1.list_id) INTO :c2 FROM list l1, list l2,
570     imembers im, capacls c WHERE c.list_id = l2.list_id AND
571     im.list_id = l2.list_id AND im.member_type = 'LIST' AND
572     im.member_id = l1.list_id AND l1.list_id = :lid;
573
574   if (c1 == 0 && c2 == 0)
575     return 0;
576
577   if (cl->proxy_id)
578     return 1;
579
580   EXEC SQL SELECT acl_type, acl_id, memacl_type, memacl_id
581     INTO :acl_type, :acl_id, :memacl_type, :memacl_id
582     FROM list WHERE list_id=:lid;
583
584   if (!find_member(acl_type, acl_id, cl))
585     {
586       if (!find_member(memacl_type, memacl_id, cl))
587         return 1;
588     }
589
590   return 0;
591 }
592
593
594 /* get_ace_use - given a type and a name, return a type and a name.
595  * The ace_type is one of "LIST", "USER", "RLIST", or "RUSER" in argv[0],
596  * and argv[1] will contain the ID of the entity in question.  The R*
597  * types mean to recursively look at every containing list, not just
598  * when the object in question is a direct member.  On return, the
599  * usage type will be one of LIST, SERVICE, FILESYS, QUOTA, QUERY, or ZEPHYR.
600  */
601
602 int get_ace_use(struct query *q, char *argv[], client *cl,
603                 int (*action)(int, char *[], void *), void *actarg)
604 {
605   int found = 0;
606   EXEC SQL BEGIN DECLARE SECTION;
607   char *atype;
608   int aid, listid, id;
609   EXEC SQL END DECLARE SECTION;
610   struct save_queue *sq;
611
612   atype = argv[0];
613   aid = *(int *)argv[1];
614   if (!strcmp(atype, "LIST") || !strcmp(atype, "USER") ||
615       !strcmp(atype, "KERBEROS"))
616     return get_ace_internal(atype, aid, action, actarg);
617
618   sq = sq_create();
619   if (!strcmp(atype, "RLIST"))
620     {
621       sq_save_data(sq, (void *)aid);
622       /* get all the list_id's of containing lists */
623       EXEC SQL DECLARE csr107 CURSOR FOR
624         SELECT list_id FROM imembers
625         WHERE member_type = 'LIST' AND member_id = :aid;
626       if (dbms_errno)
627         return mr_errcode;
628       EXEC SQL OPEN csr107;
629       if (dbms_errno)
630         return mr_errcode;
631       while (1)
632         {
633           EXEC SQL FETCH csr107 INTO :listid;
634           if (sqlca.sqlcode)
635             break;
636           sq_save_unique_data(sq, (void *)listid);
637         }
638       EXEC SQL CLOSE csr107;
639       /* now process each one */
640       while (sq_get_data(sq, &id))
641         {
642           if (get_ace_internal("LIST", id, action, actarg) == MR_SUCCESS)
643             found++;
644         }
645     }
646
647   if (!strcmp(atype, "RUSER"))
648     {
649       EXEC SQL DECLARE csr108 CURSOR FOR
650         SELECT list_id FROM imembers
651         WHERE member_type = 'USER' AND member_id = :aid;
652       if (dbms_errno)
653         return mr_errcode;
654       EXEC SQL OPEN csr108;
655       if (dbms_errno)
656         return mr_errcode;
657       while (1)
658         {
659           EXEC SQL FETCH csr108 INTO :listid;
660           if (sqlca.sqlcode)
661             break;
662           sq_save_data(sq, (void *)listid);
663         }
664       EXEC SQL CLOSE csr108;
665       /* now process each one */
666       while (sq_get_data(sq, &id))
667         {
668           if (get_ace_internal("LIST", id, action, actarg) == MR_SUCCESS)
669             found++;
670         }
671       if (get_ace_internal("USER", aid, action, actarg) == MR_SUCCESS)
672         found++;
673     }
674
675   if (!strcmp(atype, "RKERBEROS"))
676     {
677       EXEC SQL DECLARE csr109 CURSOR FOR
678         SELECT list_id FROM imembers
679         WHERE member_type = 'KERBEROS' AND member_id = :aid;
680       if (dbms_errno)
681         return mr_errcode;
682       EXEC SQL OPEN csr109;
683       if (dbms_errno)
684         return mr_errcode;
685       while (1)
686         {
687           EXEC SQL FETCH csr109 INTO :listid;
688           if (sqlca.sqlcode)
689             break;
690           sq_save_data(sq, (void *)listid);
691         }
692       EXEC SQL CLOSE csr109;
693       /* now process each one */
694       while (sq_get_data(sq, &id))
695         {
696           if (get_ace_internal("LIST", id, action, actarg) == MR_SUCCESS)
697             found++;
698         }
699       if (get_ace_internal("KERBEROS", aid, action, actarg) == MR_SUCCESS)
700         found++;
701     }
702
703   sq_destroy(sq);
704   if (dbms_errno)
705     return mr_errcode;
706   if (!found)
707     return MR_NO_MATCH;
708   return MR_SUCCESS;
709 }
710
711
712 /* This looks up a single list or user for ace use.  atype must be "USER"
713  * or "LIST", and aid is the ID of the corresponding object.  This is used
714  * by get_ace_use above.
715  */
716
717 int get_ace_internal(char *atype, int aid,
718                      int (*action)(int, char *[], void *), void *actarg)
719 {
720   char *rargv[2];
721   int found = 0;
722   EXEC SQL BEGIN DECLARE SECTION;
723   char name[MAX_FIELD_WIDTH], *type = atype;
724   int id = aid;
725   EXEC SQL END DECLARE SECTION;
726
727   rargv[1] = name;
728   if (!strcmp(atype, "LIST"))
729     {
730       rargv[0] = "FILESYS";
731       EXEC SQL DECLARE csr110 CURSOR FOR
732         SELECT label FROM filesys
733         WHERE owners = :id;
734       if (dbms_errno)
735         return mr_errcode;
736       EXEC SQL OPEN csr110;
737       if (dbms_errno)
738         return mr_errcode;
739       while (1)
740         {
741           EXEC SQL FETCH csr110 INTO :name;
742           if (sqlca.sqlcode)
743             break;
744           (*action)(2, rargv, actarg);
745           found++;
746         }
747       EXEC SQL CLOSE csr110;
748
749       rargv[0] = "QUERY";
750       EXEC SQL DECLARE csr111 CURSOR FOR
751         SELECT capability FROM capacls
752         WHERE list_id = :id;
753       if (dbms_errno)
754         return mr_errcode;
755       EXEC SQL OPEN csr111;
756       if (dbms_errno)
757         return mr_errcode;
758       while (1)
759         {
760           EXEC SQL FETCH csr111 INTO :name;
761           if (sqlca.sqlcode)
762             break;
763           (*action)(2, rargv, actarg);
764           found++;
765         }
766       EXEC SQL CLOSE csr111;
767     }
768   else if (!strcmp(atype, "USER"))
769     {
770       rargv[0] = "FILESYS";
771       EXEC SQL DECLARE csr112 CURSOR FOR
772         SELECT label FROM filesys
773         WHERE owner = :id;
774       if (dbms_errno)
775         return mr_errcode;
776       EXEC SQL OPEN csr112;
777       if (dbms_errno)
778         return mr_errcode;
779       while (1)
780         {
781           EXEC SQL FETCH csr112 INTO :name;
782           if (sqlca.sqlcode)
783             break;
784           (*action)(2, rargv, actarg);
785           found++;
786         }
787       EXEC SQL CLOSE csr112;
788     }
789
790   rargv[0] = "LIST";
791   EXEC SQL DECLARE csr113 CURSOR FOR
792     SELECT name FROM list
793     WHERE (acl_type = :type AND acl_id = :id)
794     OR (memacl_type = :type AND memacl_id = :id);
795   if (dbms_errno)
796     return mr_errcode;
797   EXEC SQL OPEN csr113;
798   if (dbms_errno)
799     return mr_errcode;
800   while (1)
801     {
802       EXEC SQL FETCH csr113 INTO :name;
803       if (sqlca.sqlcode)
804         break;
805       (*action)(2, rargv, actarg);
806       found++;
807     }
808   EXEC SQL CLOSE csr113;
809
810   rargv[0] = "SERVICE";
811   EXEC SQL DECLARE csr114 CURSOR FOR
812     SELECT name FROM servers
813     WHERE acl_type = :type AND acl_id = :id;
814   if (dbms_errno)
815     return mr_errcode;
816   EXEC SQL OPEN csr114;
817   if (dbms_errno)
818     return mr_errcode;
819   while (1)
820     {
821       EXEC SQL FETCH csr114 INTO :name;
822       if (sqlca.sqlcode)
823         break;
824       (*action)(2, rargv, actarg);
825       found++;
826     }
827   EXEC SQL CLOSE csr114;
828
829   rargv[0] = "HOSTACCESS";
830   EXEC SQL DECLARE csr115 CURSOR FOR
831     SELECT name FROM machine m, hostaccess ha
832     WHERE m.mach_id = ha.mach_id AND ha.acl_type = :type
833     AND ha.acl_id = :id;
834   if (dbms_errno)
835     return mr_errcode;
836   EXEC SQL OPEN csr115;
837   if (dbms_errno)
838     return mr_errcode;
839   while (1)
840     {
841       EXEC SQL FETCH csr115 INTO :name;
842       if (sqlca.sqlcode)
843         break;
844       (*action)(2, rargv, actarg);
845       found++;
846     }
847   EXEC SQL CLOSE csr115;
848
849   rargv[0] = "MACHINE";
850   EXEC SQL DECLARE csr115a CURSOR FOR
851     SELECT name FROM machine m
852     WHERE m.owner_type = :type
853     AND m.owner_id = :id;
854   if (dbms_errno)
855     return mr_errcode;
856   EXEC SQL OPEN csr115a;
857   if (dbms_errno)
858     return mr_errcode;
859   while (1)
860     {
861       EXEC SQL FETCH csr115a INTO :name;
862       if (sqlca.sqlcode)
863         break;
864       (*action)(2, rargv, actarg);
865       found++;
866     }
867   EXEC SQL CLOSE csr115a;
868
869   rargv[0] = "ZEPHYR";
870   EXEC SQL DECLARE csr116 CURSOR FOR
871     SELECT class FROM zephyr z
872     WHERE z.xmt_type = :type AND z.xmt_id = :id
873     OR z.sub_type = :type AND z.sub_id = :id
874     OR z.iws_type = :type AND z.iws_id = :id
875     OR z.iui_type = :type AND z.iui_id = :id
876     OR z.owner_type = :type AND z.owner_id = :id;
877   if (dbms_errno)
878     return mr_errcode;
879   EXEC SQL OPEN csr116;
880   if (dbms_errno)
881     return mr_errcode;
882   while (1)
883     {
884       EXEC SQL FETCH csr116 INTO :name;
885       if (sqlca.sqlcode)
886         break;
887       (*action)(2, rargv, actarg);
888       found++;
889     }
890   EXEC SQL CLOSE csr116;
891
892   rargv[0] = "CONTAINER";
893   EXEC SQL DECLARE csr117c CURSOR FOR
894     SELECT name FROM containers c
895     WHERE c.acl_type = :type AND c.acl_id = :id;
896   if (dbms_errno)
897     return mr_errcode;
898   EXEC SQL OPEN csr117c;
899   while (1)
900     {
901       EXEC SQL FETCH csr117c INTO :name;
902       if (sqlca.sqlcode)
903         break;
904       (*action)(2, rargv, actarg);
905       found++;
906     }
907   EXEC SQL CLOSE csr117c;
908
909   rargv[0] = "CONTAINER-MEMACL";
910   EXEC SQL DECLARE csr117d CURSOR FOR
911     SELECT name FROM containers c
912     WHERE c.memacl_type = :type AND c.memacl_id = :id;
913   if (dbms_errno)
914     return mr_errcode;
915   EXEC SQL OPEN csr117d;
916   while (1)
917     {
918       EXEC SQL FETCH csr117d INTO :name;
919       if (sqlca.sqlcode)
920         break;
921       (*action)(2, rargv, actarg);
922       found++;
923     }
924   EXEC SQL CLOSE csr117d;
925
926   if (!found)
927     return MR_NO_MATCH;
928   return MR_SUCCESS;
929 }
930
931 /* ghbo_internal */
932 int ghbo_internal(char *atype, int aid,
933                   int (*action)(int, char *[], void *), void *actarg)
934 {
935   char *rargv[1];
936   int found = 0;
937   EXEC SQL BEGIN DECLARE SECTION;
938   char name[MACHINE_NAME_SIZE], *type = atype;
939   int id = aid;
940   EXEC SQL END DECLARE SECTION;
941
942   rargv[0] = name;
943   EXEC SQL DECLARE csr115b CURSOR FOR
944     SELECT name FROM machine m
945     WHERE m.owner_type = :type
946     AND m.owner_id = :id;
947   if (dbms_errno)
948     return mr_errcode;
949   EXEC SQL OPEN csr115b;
950   if (dbms_errno)
951     return mr_errcode;
952   while (1)
953     {
954       EXEC SQL FETCH csr115b INTO :name;
955       if (sqlca.sqlcode)
956         break;
957       (*action)(1, rargv, actarg);
958       found++;
959     }
960   EXEC SQL CLOSE csr115b;
961   
962   if (!found)
963     return MR_NO_MATCH;
964   return MR_SUCCESS;
965 }
966
967 /* get_host_by_owner - like gaus but limited to hosts */
968 int get_host_by_owner(struct query *q, char *argv[], client *cl,
969                       int (*action)(int, char *[], void *), void *actarg)
970 {
971   int found = 0;
972   EXEC SQL BEGIN DECLARE SECTION;
973   char *atype;
974   int aid, listid, id;
975   EXEC SQL END DECLARE SECTION;
976   struct save_queue *sq;
977
978   atype = argv[0];
979   aid = *(int *)argv[1];
980   if (!strcmp(atype, "LIST") || !strcmp(atype, "USER") ||
981       !strcmp(atype, "KERBEROS"))
982     return ghbo_internal(atype, aid, action, actarg);
983
984   sq = sq_create();
985   if (!strcmp(atype, "RLIST"))
986     {
987       sq_save_data(sq, (void *)aid);
988       /* get all the list_id's of containing lists */
989       EXEC SQL DECLARE csr107q CURSOR FOR
990         SELECT list_id FROM imembers
991         WHERE member_type = 'LIST' AND member_id = :aid;
992       if (dbms_errno)
993         return mr_errcode;
994       EXEC SQL OPEN csr107q;
995       if (dbms_errno)
996         return mr_errcode;
997       while (1)
998         {
999           EXEC SQL FETCH csr107q INTO :listid;
1000           if (sqlca.sqlcode)
1001             break;
1002           sq_save_unique_data(sq, (void *)listid);
1003         }
1004       EXEC SQL CLOSE csr107q;
1005       /* now process each one */
1006       while (sq_get_data(sq, &id))
1007         {
1008           if (ghbo_internal("LIST", id, action, actarg) == MR_SUCCESS)
1009             found++;
1010         }
1011     }
1012
1013   if (!strcmp(atype, "RUSER"))
1014     {
1015       EXEC SQL DECLARE csr108q CURSOR FOR
1016         SELECT list_id FROM imembers
1017         WHERE member_type = 'USER' AND member_id = :aid;
1018       if (dbms_errno)
1019         return mr_errcode;
1020       EXEC SQL OPEN csr108q;
1021       if (dbms_errno)
1022         return mr_errcode;
1023       while (1)
1024         {
1025           EXEC SQL FETCH csr108q INTO :listid;
1026           if (sqlca.sqlcode)
1027             break;
1028           sq_save_data(sq, (void *)listid);
1029         }
1030       EXEC SQL CLOSE csr108q;
1031       /* now process each one */
1032       while (sq_get_data(sq, &id))
1033         {
1034           if (ghbo_internal("LIST", id, action, actarg) == MR_SUCCESS)
1035             found++;
1036         }
1037       if (ghbo_internal("USER", aid, action, actarg) == MR_SUCCESS)
1038         found++;
1039     }
1040
1041   if (!strcmp(atype, "RKERBEROS"))
1042     {
1043       EXEC SQL DECLARE csr109q CURSOR FOR
1044         SELECT list_id FROM imembers
1045         WHERE member_type = 'KERBEROS' AND member_id = :aid;
1046       if (dbms_errno)
1047         return mr_errcode;
1048       EXEC SQL OPEN csr109q;
1049       if (dbms_errno)
1050         return mr_errcode;
1051       while (1)
1052         {
1053           EXEC SQL FETCH csr109q INTO :listid;
1054           if (sqlca.sqlcode)
1055             break;
1056           sq_save_data(sq, (void *)listid);
1057         }
1058       EXEC SQL CLOSE csr109q;
1059       /* now process each one */
1060       while (sq_get_data(sq, &id))
1061         {
1062           if (ghbo_internal("LIST", id, action, actarg) == MR_SUCCESS)
1063             found++;
1064         }
1065       if (ghbo_internal("KERBEROS", aid, action, actarg) == MR_SUCCESS)
1066         found++;
1067     }
1068
1069   sq_destroy(sq);
1070   if (dbms_errno)
1071     return mr_errcode;
1072   if (!found)
1073     return MR_NO_MATCH;
1074   return MR_SUCCESS;
1075 }
1076
1077 int guas_internal(char *atype, int aid,
1078                   int (*action)(int, char *[], void *), void *actarg)
1079 {
1080   char *rargv[1];
1081   int found = 0;
1082   EXEC SQL BEGIN DECLARE SECTION;
1083   char login[USERS_LOGIN_SIZE], *type = atype;
1084   int id = aid;
1085   EXEC SQL END DECLARE SECTION;
1086
1087   rargv[0] = login;
1088   EXEC SQL DECLARE csr115sp CURSOR FOR
1089     SELECT login FROM users u
1090     WHERE u.sponsor_type = :type
1091     AND u.sponsor_id = :id;
1092   if (dbms_errno)
1093     return mr_errcode;
1094   EXEC SQL OPEN csr115sp;
1095   if (dbms_errno)
1096     return mr_errcode;
1097   while (1)
1098     {
1099       EXEC SQL FETCH csr115sp INTO :login;
1100       if (sqlca.sqlcode)
1101         break;
1102       (*action)(1, rargv, actarg);
1103       found++;
1104     }
1105   EXEC SQL CLOSE csr115sp;
1106   
1107   if (!found)
1108     return MR_NO_MATCH;
1109   return MR_SUCCESS;
1110 }
1111
1112 /* get_user_account_by_sponsor - like gaus but limited to user accounts */
1113 int get_user_account_by_sponsor(struct query *q, char *argv[], client *cl,
1114                                 int (*action)(int, char *[], void *),
1115                                 void *actarg)
1116 {
1117   int found = 0;
1118   EXEC SQL BEGIN DECLARE SECTION;
1119   char *atype;
1120   int aid, listid, id;
1121   EXEC SQL END DECLARE SECTION;
1122   struct save_queue *sq;
1123
1124   atype = argv[0];
1125   aid = *(int *)argv[1];
1126   if (!strcmp(atype, "LIST") || !strcmp(atype, "USER") ||
1127       !strcmp(atype, "KERBEROS"))
1128     return guas_internal(atype, aid, action, actarg);
1129
1130   sq = sq_create();
1131   if (!strcmp(atype, "RLIST"))
1132     {
1133       sq_save_data(sq, (void *)aid);
1134       /* get all the list_id's of containing lists */
1135       EXEC SQL DECLARE csr107sp CURSOR FOR
1136         SELECT list_id FROM imembers
1137         WHERE member_type = 'LIST' AND member_id = :aid;
1138       if (dbms_errno)
1139         return mr_errcode;
1140       EXEC SQL OPEN csr107sp;
1141       if (dbms_errno)
1142         return mr_errcode;
1143       while (1)
1144         {
1145           EXEC SQL FETCH csr107sp INTO :listid;
1146           if (sqlca.sqlcode)
1147             break;
1148           sq_save_unique_data(sq, (void *)listid);
1149         }
1150       EXEC SQL CLOSE csr107sp;
1151       /* now process each one */
1152       while (sq_get_data(sq, &id))
1153         {
1154           if (guas_internal("LIST", id, action, actarg) == MR_SUCCESS)
1155             found++;
1156         }
1157     }
1158
1159   if (!strcmp(atype, "RUSER"))
1160     {
1161       EXEC SQL DECLARE csr108sp CURSOR FOR
1162         SELECT list_id FROM imembers
1163         WHERE member_type = 'USER' AND member_id = :aid;
1164       if (dbms_errno)
1165         return mr_errcode;
1166       EXEC SQL OPEN csr108sp;
1167       if (dbms_errno)
1168         return mr_errcode;
1169       while (1)
1170         {
1171           EXEC SQL FETCH csr108sp INTO :listid;
1172           if (sqlca.sqlcode)
1173             break;
1174           sq_save_data(sq, (void *)listid);
1175         }
1176       EXEC SQL CLOSE csr108sp;
1177       /* now process each one */
1178       while (sq_get_data(sq, &id))
1179         {
1180           if (guas_internal("LIST", id, action, actarg) == MR_SUCCESS)
1181             found++;
1182         }
1183       if (guas_internal("USER", aid, action, actarg) == MR_SUCCESS)
1184         found++;
1185     }
1186
1187   if (!strcmp(atype, "RKERBEROS"))
1188     {
1189       EXEC SQL DECLARE csr109sp CURSOR FOR
1190         SELECT list_id FROM imembers
1191         WHERE member_type = 'KERBEROS' AND member_id = :aid;
1192       if (dbms_errno)
1193         return mr_errcode;
1194       EXEC SQL OPEN csr109sp;
1195       if (dbms_errno)
1196         return mr_errcode;
1197       while (1)
1198         {
1199           EXEC SQL FETCH csr109sp INTO :listid;
1200           if (sqlca.sqlcode)
1201             break;
1202           sq_save_data(sq, (void *)listid);
1203         }
1204       EXEC SQL CLOSE csr109sp;
1205       /* now process each one */
1206       while (sq_get_data(sq, &id))
1207         {
1208           if (guas_internal("LIST", id, action, actarg) == MR_SUCCESS)
1209             found++;
1210         }
1211       if (guas_internal("KERBEROS", aid, action, actarg) == MR_SUCCESS)
1212         found++;
1213     }
1214
1215   sq_destroy(sq);
1216   if (dbms_errno)
1217     return mr_errcode;
1218   if (!found)
1219     return MR_NO_MATCH;
1220   return MR_SUCCESS;
1221 }
1222
1223 /* get_lists_of_member - given a type and a name, return the name and flags
1224  * of all of the lists of the given member.  The member_type is one of
1225  * "LIST", "USER", "STRING", "KERBEROS", "MACHINE", "RLIST", "RUSER", 
1226  * "RSTRING", "RKERBEROS", or "RMACHINE" in argv[0], and argv[1] will contain 
1227  * the ID of the entity in question.  The R* types mean to recursively look 
1228  * at every containing list, not just when the object in question is a direct 
1229  * member.
1230  */
1231
1232 int get_lists_of_member(struct query *q, char *argv[], client *cl,
1233                         int (*action)(int, char *[], void *), void *actarg)
1234 {
1235   int found = 0, direct = 1;
1236   char *rargv[6];
1237   EXEC SQL BEGIN DECLARE SECTION;
1238   char *atype;
1239   int aid;
1240   char name[LIST_NAME_SIZE];
1241   char active[5], public[5], hidden[5], maillist[5], grouplist[5];
1242   EXEC SQL END DECLARE SECTION;
1243
1244   atype = argv[0];
1245   aid = *(int *)argv[1];
1246   if (!strcmp(atype, "RLIST"))
1247     {
1248       atype = "LIST";
1249       direct = 0;
1250     }
1251   if (!strcmp(atype, "RUSER"))
1252     {
1253       atype = "USER";
1254       direct = 0;
1255     }
1256   if (!strcmp(atype, "RSTRING"))
1257     {
1258       atype = "STRING";
1259       direct = 0;
1260     }
1261   if (!strcmp(atype, "RKERBEROS"))
1262     {
1263       atype = "KERBEROS";
1264       direct = 0;
1265     }
1266   if (!strcmp(atype, "RMACHINE"))
1267     {
1268       atype = "MACHINE";
1269       direct = 0;
1270     }
1271
1272   rargv[0] = name;
1273   rargv[1] = active;
1274   rargv[2] = public;
1275   rargv[3] = hidden;
1276   rargv[4] = maillist;
1277   rargv[5] = grouplist;
1278   if (direct)
1279     {
1280       EXEC SQL DECLARE csr117a CURSOR FOR
1281         SELECT l.name, l.active, l.publicflg, l.hidden, l.maillist, l.grouplist
1282         FROM list l, imembers im
1283         WHERE l.list_id = im.list_id AND im.direct = 1
1284         AND im.member_type = :atype AND im.member_id = :aid;
1285       if (dbms_errno)
1286         return mr_errcode;
1287       EXEC SQL OPEN csr117a;
1288       if (dbms_errno)
1289         return mr_errcode;
1290       while (1)
1291         {
1292           EXEC SQL FETCH csr117a
1293             INTO :name, :active, :public, :hidden, :maillist, :grouplist;
1294           if (sqlca.sqlcode)
1295             break;
1296           (*action)(6, rargv, actarg);
1297           found++;
1298         }
1299       EXEC SQL CLOSE csr117a;
1300     }
1301   else
1302     {
1303       EXEC SQL DECLARE csr117b CURSOR FOR
1304         SELECT l.name, l.active, l.publicflg, l.hidden, l.maillist, l.grouplist
1305         FROM list l, imembers im
1306         WHERE l.list_id = im.list_id
1307         AND im.member_type = :atype AND im.member_id = :aid;
1308       if (dbms_errno)
1309         return mr_errcode;
1310       EXEC SQL OPEN csr117b;
1311       if (dbms_errno)
1312         return mr_errcode;
1313       while (1)
1314         {
1315           EXEC SQL FETCH csr117b
1316             INTO :name, :active, :public, :hidden, :maillist, :grouplist;
1317           if (sqlca.sqlcode)
1318             break;
1319           (*action)(6, rargv, actarg);
1320           found++;
1321         }
1322       EXEC SQL CLOSE csr117b;
1323     }
1324
1325   if (dbms_errno)
1326     return mr_errcode;
1327   if (!found)
1328     return MR_NO_MATCH;
1329   return MR_SUCCESS;
1330 }
1331
1332
1333 /* qualified_get_lists: passed "TRUE", "FALSE", or "DONTCARE" for each of
1334  * the five flags associated with each list.  It will return the name of
1335  * each list that meets the quailifications.  It does this by building a
1336  * where clause based on the arguments, then doing a retrieve.
1337  */
1338
1339 static char *lflags[5] = { "active", "publicflg", "hidden", "maillist", "grouplist" };
1340
1341 int qualified_get_lists(struct query *q, char *argv[], client *cl,
1342                         int (*action)(int, char *[], void *), void *actarg)
1343 {
1344   return qualified_get(q, argv, action, actarg, "l.list_id != 0",
1345                        "l", "name", lflags);
1346 }
1347
1348
1349 int get_members_of_list(struct query *q, char *argv[], client *cl,
1350                         int (*action)(int, char *[], void *), void *actarg)
1351 {
1352   EXEC SQL BEGIN DECLARE SECTION;
1353   int list_id, direct;
1354   char member_name[MAX_FIELD_WIDTH], tag[STRINGS_STRING_SIZE];
1355   EXEC SQL END DECLARE SECTION;
1356   char *targv[3];
1357   int targc;
1358
1359   /* For gmol or gtml, only get direct members. For geml, get all. */
1360   if (!strcmp(q->shortname, "geml"))
1361     direct = -1;
1362   else
1363     direct = 0;
1364
1365   /* For gmol or geml, only return type and name. For gtml, return tag too. */
1366   if (!strcmp(q->shortname, "gtml"))
1367     targc = 3;
1368   else
1369     targc = 2;
1370
1371   list_id = *(int *)argv[0];
1372
1373   targv[1] = member_name;
1374   targv[2] = tag;
1375
1376   targv[0] = "USER";
1377   EXEC SQL DECLARE csr119 CURSOR FOR
1378     SELECT u.login, s.string FROM users u, imembers im, strings s
1379     WHERE im.list_id = :list_id AND im.member_type = 'USER'
1380     AND im.member_id = u.users_id AND im.direct > :direct
1381     AND s.string_id = im.tag ORDER BY 1;
1382   if (dbms_errno)
1383     return mr_errcode;
1384   EXEC SQL OPEN csr119;
1385   if (dbms_errno)
1386     return mr_errcode;
1387   while (1)
1388     {
1389       EXEC SQL FETCH csr119 INTO :member_name, :tag;
1390       if (sqlca.sqlcode)
1391         break;
1392       (*action)(targc, targv, actarg);
1393     }
1394   EXEC SQL CLOSE csr119;
1395   if (dbms_errno)
1396     return mr_errcode;
1397
1398   targv[0] = "LIST";
1399   EXEC SQL DECLARE csr120 CURSOR FOR
1400     SELECT l.name, s.string FROM list l, imembers im, strings s
1401     WHERE im.list_id = :list_id AND im.member_type = 'LIST'
1402     AND im.member_id = l.list_id AND im.direct > :direct
1403     AND s.string_id = im.tag ORDER BY 1;
1404   if (dbms_errno)
1405     return mr_errcode;
1406   EXEC SQL OPEN csr120;
1407   if (dbms_errno)
1408     return mr_errcode;
1409   while (1)
1410     {
1411       EXEC SQL FETCH csr120 INTO :member_name, :tag;
1412       if (sqlca.sqlcode)
1413         break;
1414       (*action)(targc, targv, actarg);
1415     }
1416   EXEC SQL CLOSE csr120;
1417   if (dbms_errno)
1418     return mr_errcode;
1419
1420   targv[0] = "STRING";
1421   EXEC SQL DECLARE csr121 CURSOR FOR
1422     SELECT str.string, s.string FROM strings str, imembers im, strings s
1423     WHERE im.list_id = :list_id AND im.member_type = 'STRING'
1424     AND im.member_id = str.string_id AND im.direct > :direct
1425     AND s.string_id = im.tag ORDER BY 1;
1426   if (dbms_errno)
1427     return mr_errcode;
1428   EXEC SQL OPEN csr121;
1429   if (dbms_errno)
1430     return mr_errcode;
1431   while (1)
1432     {
1433       EXEC SQL FETCH csr121 INTO :member_name, :tag;
1434       if (sqlca.sqlcode)
1435         break;
1436       (*action)(targc, targv, actarg);
1437     }
1438   EXEC SQL CLOSE csr121;
1439   if (dbms_errno)
1440     return mr_errcode;
1441
1442   targv[0] = "KERBEROS";
1443   EXEC SQL DECLARE csr122 CURSOR FOR
1444     SELECT str.string, s.string FROM strings str, imembers im, strings s
1445     WHERE im.list_id = :list_id AND im.member_type = 'KERBEROS'
1446     AND im.member_id = str.string_id AND im.direct > :direct
1447     AND s.string_id = im.tag ORDER BY 1;
1448   if (dbms_errno)
1449     return mr_errcode;
1450   EXEC SQL OPEN csr122;
1451   if (dbms_errno)
1452     return mr_errcode;
1453   while (1)
1454     {
1455       EXEC SQL FETCH csr122 INTO :member_name, :tag;
1456       if (sqlca.sqlcode)
1457         break;
1458       (*action)(targc, targv, actarg);
1459     }
1460   EXEC SQL CLOSE csr122;
1461   if (dbms_errno)
1462     return mr_errcode;
1463
1464   targv[0] = "MACHINE";
1465   EXEC SQL DECLARE csr123 CURSOR FOR
1466     SELECT m.name, s.string FROM machine m, imembers im, strings s
1467     WHERE im.list_id = :list_id AND im.member_type = 'MACHINE'
1468     AND im.member_id = m.mach_id AND im.direct > :direct
1469     AND s.string_id = im.tag ORDER BY 1;
1470   if (dbms_errno)
1471     return mr_errcode;
1472   EXEC SQL OPEN csr123;
1473   if (dbms_errno)
1474     return mr_errcode;
1475   while (1)
1476     {
1477       EXEC SQL FETCH csr123 INTO :member_name, :tag;
1478       if (sqlca.sqlcode)
1479         break;
1480       (*action)(targc, targv, actarg);
1481     }
1482   EXEC SQL CLOSE csr123;
1483   if (dbms_errno)
1484     return mr_errcode;
1485
1486   return MR_SUCCESS;
1487 }
1488
1489
1490 /* count_members_of_list: this is a simple query, but it cannot be done
1491  * through the dispatch table.
1492  */
1493
1494 int count_members_of_list(struct query *q, char *argv[], client *cl,
1495                           int (*action)(int, char *[], void *), void *actarg)
1496 {
1497   EXEC SQL BEGIN DECLARE SECTION;
1498   int  list, ct = 0;
1499   EXEC SQL END DECLARE SECTION;
1500   char *rargv[1], countbuf[5];
1501
1502   list = *(int *)argv[0];
1503   rargv[0] = countbuf;
1504   EXEC SQL SELECT count (*) INTO :ct FROM imembers
1505     WHERE list_id = :list AND direct = 1;
1506   if (dbms_errno)
1507     return mr_errcode;
1508   sprintf(countbuf, "%d", ct);
1509   (*action)(1, rargv, actarg);
1510   return MR_SUCCESS;
1511 }
1512
1513
1514 /* qualified_get_server: passed "TRUE", "FALSE", or "DONTCARE" for each of
1515  * the three flags associated with each service.  It will return the name of
1516  * each service that meets the quailifications.  It does this by building a
1517  * where clause based on the arguments, then doing a retrieve.
1518  */
1519
1520 static char *sflags[3] = { "enable", "inprogress", "harderror" };
1521
1522 int qualified_get_server(struct query *q, char *argv[], client *cl,
1523                          int (*action)(int, char *[], void *), void *actarg)
1524 {
1525   return qualified_get(q, argv, action, actarg, "s.name is not null",
1526                        "s", "name", sflags);
1527   /* of course, name will never be null, but we need something there
1528      to make qualified_get happy */
1529 }
1530
1531
1532 /* generic qualified get routine, used by qualified_get_lists,
1533  * qualified_get_server, and qualified_get_serverhost.
1534  *   Args:
1535  *      start - a simple where clause, must not be empty
1536  *      range - the name of the range variable
1537  *      field - the field to return
1538  *      flags - an array of strings, names of the flag variables
1539  */
1540
1541 int qualified_get(struct query *q, char *argv[],
1542                   int (*action)(int, char *[], void *), void *actarg,
1543                   char *start, char *range, char *field, char *flags[])
1544 {
1545   char qual[256];
1546   int i;
1547   char buf[32];
1548
1549   strcpy(qual, start);
1550   for (i = 0; i < q->argc; i++)
1551     {
1552       if (!strcmp(argv[i], "TRUE"))
1553         {
1554           sprintf(buf, " AND %s.%s != 0", range, flags[i]);
1555           strcat(qual, buf);
1556         }
1557       else if (!strcmp(argv[i], "FALSE"))
1558         {
1559           sprintf(buf, " AND %s.%s = 0", range, flags[i]);
1560           strcat(qual, buf);
1561         }
1562     }
1563
1564   sprintf(stmt_buf, "SELECT %s.%s FROM %s %s WHERE %s", range, field,
1565           table_name[q->rtable], range, qual);
1566   return do_for_all_rows(stmt_buf, 1, action, actarg);
1567 }
1568
1569
1570 /* qualified_get_serverhost: passed "TRUE", "FALSE", or "DONTCARE" for each of
1571  * the five flags associated with each serverhost.  It will return the name of
1572  * each service and host that meets the quailifications.  It does this by
1573  * building a where clause based on the arguments, then doing a retrieve.
1574  */
1575
1576 static char *shflags[6] = { "service", "enable", "override", "success",
1577                             "inprogress", "hosterror" };
1578
1579 int qualified_get_serverhost(struct query *q, char *argv[], client *cl,
1580                              int (*action)(int, char *[], void *),
1581                              void *actarg)
1582 {
1583   char qual[256], buf[32];
1584   int i;
1585
1586   sprintf(qual, "m.mach_id = sh.mach_id AND sh.service = UPPER('%s')",
1587           argv[0]);
1588   for (i = 1; i < q->argc; i++)
1589     {
1590       if (!strcmp(argv[i], "TRUE"))
1591         {
1592           sprintf(buf, " AND sh.%s != 0", shflags[i]);
1593           strcat(qual, buf);
1594         }
1595       else if (!strcmp(argv[i], "FALSE"))
1596         {
1597           sprintf(buf, " AND sh.%s = 0", shflags[i]);
1598           strcat(qual, buf);
1599         }
1600     }
1601
1602   sprintf(stmt_buf, "SELECT sh.service, m.name FROM serverhosts sh, "
1603           "machine m WHERE %s", qual);
1604   return do_for_all_rows(stmt_buf, 2, action, actarg);
1605 }
1606
1607
1608 /* register_user - change user's login name and allocate a pobox, group,
1609  * filesystem, and quota for them.  The user's status must start out as 0,
1610  * and is left as 2.  Arguments are: user's UID, new login name, and
1611  * pobox type ("POP" = POP, "IMAP" or numeric = IMAP)
1612  */
1613
1614 int register_user(struct query *q, char **argv, client *cl)
1615 {
1616   EXEC SQL BEGIN DECLARE SECTION;
1617   char *login, *entity;
1618   char directory[FILESYS_NAME_SIZE], machname[MACHINE_NAME_SIZE];
1619   char dir[NFSPHYS_DIR_SIZE], *potype;
1620   int who, rowcount, mid, uid, users_id;
1621   int ostatus, nstatus, fsidval, popid;
1622   int npid, tmp;
1623   int po_exists = 0;
1624   static int m_id, def_quota, def_imap_quota, list_id;
1625   EXEC SQL END DECLARE SECTION;
1626   char buffer[256], *aargv[3];
1627
1628   if (!m_id)
1629     {
1630       EXEC SQL SELECT list_id INTO :list_id FROM list
1631         WHERE name = 'wheel';
1632
1633       EXEC SQL SELECT mach_id INTO :m_id FROM machine
1634         WHERE name = 'ATHENA.MIT.EDU';
1635     }
1636
1637   EXEC SQL SELECT value INTO :def_quota FROM numvalues
1638     WHERE name = 'def_quota';
1639   if (sqlca.sqlerrd[2] != 1)
1640     return MR_NO_QUOTA;
1641
1642   EXEC SQL SELECT value INTO :def_imap_quota FROM numvalues
1643     WHERE name = 'def_imap_quota';
1644   if (sqlca.sqlerrd[2] != 1)
1645     return MR_NO_QUOTA;
1646
1647   entity = cl->entity;
1648   who = cl->client_id;
1649
1650   uid = atoi(argv[0]);
1651   login = argv[1];
1652   potype = argv[2];
1653
1654   /* find user */
1655   EXEC SQL SELECT users_id, status INTO :users_id, :ostatus
1656     FROM users
1657     WHERE unix_uid = :uid AND
1658     (status = 0 OR status = 5 OR status = 6 OR status = 9);
1659
1660   if (sqlca.sqlerrd[2] == 0)
1661     return MR_NO_MATCH;
1662   if (sqlca.sqlerrd[2] > 1)
1663     return MR_NOT_UNIQUE;
1664
1665   /* check new login name */
1666   EXEC SQL SELECT COUNT(login) INTO :rowcount FROM users
1667     WHERE login = :login AND users_id != :users_id;
1668   if (dbms_errno)
1669     return mr_errcode;
1670   if (rowcount > 0)
1671     return MR_IN_USE;
1672
1673   /* Remove this check when we're allowing username reuse again. */
1674   EXEC SQL SELECT COUNT(login) INTO :rowcount FROM userhistory
1675     WHERE login = :login;
1676   if (dbms_errno)
1677     return mr_errcode;
1678   if (rowcount > 0)
1679     return MR_IN_USE;
1680
1681   EXEC SQL SELECT COUNT(name) INTO :rowcount FROM list
1682     WHERE LOWER(name) = :login;
1683   if (dbms_errno)
1684     return mr_errcode;
1685   if (rowcount > 0)
1686     return MR_IN_USE;
1687   EXEC SQL SELECT COUNT(label) INTO :rowcount FROM filesys
1688     WHERE label = :login;
1689   if (dbms_errno)
1690     return mr_errcode;
1691   if (rowcount > 0)
1692     return MR_IN_USE;
1693   EXEC SQL SELECT COUNT(label) INTO :rowcount  
1694     FROM filesys WHERE label = :login || '.po';
1695   if (dbms_errno)
1696     return mr_errcode;
1697   if (rowcount > 0)
1698     {
1699       EXEC SQL SELECT owner INTO :tmp FROM filesys 
1700         WHERE label = :login || '.po';
1701       if (dbms_errno)
1702         return mr_errcode;
1703       if ((ostatus == 0 || ostatus == 9) || (tmp != users_id))
1704         return MR_IN_USE;
1705       else
1706         po_exists = 1;
1707     }
1708   EXEC SQL SELECT COUNT(name) INTO :rowcount FROM alias
1709     WHERE ( name = :login OR name = :login || '.po' )
1710     AND type = 'FILESYS';
1711   if (dbms_errno)
1712     return mr_errcode;
1713   if (rowcount > 0)
1714     return MR_IN_USE;
1715   com_err(whoami, 0, "login name OK");
1716
1717   EXEC SQL SELECT COUNT(potype) INTO :rowcount FROM users WHERE
1718     login = :login AND potype = 'POP';
1719   if (dbms_errno)
1720     return mr_errcode;
1721   if (rowcount > 0)
1722     po_exists = 1;
1723
1724   /* choose type and location for pobox */
1725   if (!po_exists)
1726     {
1727       if (!strcmp(potype, "POP"))
1728         {
1729
1730           EXEC SQL DECLARE csr130 CURSOR FOR
1731             SELECT sh.mach_id, m.name FROM serverhosts sh, machine m
1732             WHERE sh.service = 'POP' AND sh.mach_id = m.mach_id
1733             AND sh.value2 - sh.value1 =
1734             (SELECT MAX(value2 - value1) FROM serverhosts WHERE service = 'POP');
1735           if (dbms_errno)
1736             return mr_errcode;
1737           EXEC SQL OPEN csr130;
1738           if (dbms_errno)
1739             return mr_errcode;
1740           EXEC SQL FETCH csr130 INTO :popid, :machname;
1741           if (sqlca.sqlerrd[2] == 0)
1742             {
1743               EXEC SQL CLOSE csr130;
1744               if (dbms_errno)
1745                 return mr_errcode;
1746               return MR_NO_POBOX;
1747             }
1748           else
1749             {
1750               EXEC SQL CLOSE csr130;
1751               if (dbms_errno)
1752                 return mr_errcode;
1753             }
1754       
1755           EXEC SQL UPDATE users SET potype = 'POP', pop_id = :popid
1756             WHERE users_id = :users_id;
1757           com_err(whoami, 0, "pobox set to POP:%s", strtrim(machname));
1758         }         
1759       else
1760         {
1761       /* Select all IMAP nfsphys entries in order of increasing
1762        * (allocated - partsize).  The partitions will almost always be 
1763        * overallocated, but we choose the one that is the least 
1764        * overallocated.
1765        */
1766           potype = "IMAP";
1767           
1768           EXEC SQL DECLARE csr_rusr_imap CURSOR FOR
1769             SELECT np.allocated - np.partsize, np.nfsphys_id, np.mach_id,
1770             np.dir, m.name FROM serverhosts sh, nfsphys np, machine m
1771             WHERE sh.service = 'POSTOFFICE' AND sh.mach_id = np.mach_id
1772             AND m.mach_id = np.mach_id
1773             ORDER BY 1;
1774           if (dbms_errno)
1775             return mr_errcode;
1776           EXEC SQL OPEN csr_rusr_imap;
1777           if (dbms_errno)
1778             return mr_errcode;
1779           EXEC SQL FETCH csr_rusr_imap INTO :tmp, :npid, :mid, :dir, :machname;
1780           if (sqlca.sqlerrd[2] == 0)
1781             {
1782               EXEC SQL CLOSE csr_rusr_imap;
1783               if (dbms_errno)
1784                 return mr_errcode;
1785               return MR_NO_POBOX;
1786             }
1787           else
1788             {
1789               EXEC SQL CLOSE csr_rusr_imap;
1790               if (dbms_errno)
1791                 return mr_errcode;
1792             }
1793
1794           /* create filesystem */
1795           if (set_next_object_id("filsys_id", FILESYS_TABLE, 0))
1796             return MR_NO_ID;
1797           incremental_clear_before();
1798
1799           EXEC SQL SELECT value INTO :popid FROM numvalues
1800             WHERE numvalues.name = 'filsys_id';
1801           EXEC SQL INSERT INTO filesys
1802             (filsys_id, phys_id, label, type, mach_id, name,
1803              mount, rwaccess, comments, owner, owners, createflg,
1804              lockertype, modtime, modby, modwith)
1805             VALUES
1806             (:popid, :npid, :login || '.po', 'IMAP', :mid, :dir,
1807              CHR(0), 'w', 'IMAP box', :users_id, :list_id, 1,
1808              'USER', SYSDATE, :who, :entity);
1809
1810           if (dbms_errno)
1811             return mr_errcode;
1812           if (sqlca.sqlerrd[2] != 1)
1813             return MR_INTERNAL;
1814           sprintf(buffer, "fs.filsys_id = %d", popid);
1815           incremental_after(FILESYS_TABLE, buffer, 0);
1816
1817           /* set quota */
1818           incremental_clear_before();
1819           EXEC SQL INSERT INTO quota
1820             (entity_id, filsys_id, type, quota, phys_id, modtime, modby, modwith)
1821             VALUES (:users_id, :popid, 'USER', :def_imap_quota, :npid,
1822                     SYSDATE, :who, :entity);
1823           if (dbms_errno)
1824             return mr_errcode;
1825           if (sqlca.sqlerrd[2] != 1)
1826             return MR_INTERNAL;
1827           aargv[0] = login;
1828           aargv[1] = "USER";
1829           aargv[2] = login;
1830           sprintf(buffer, "q.entity_id = %d and q.filsys_id = %d and "
1831                   "q.type = 'USER'", users_id, popid);
1832           incremental_after(QUOTA_TABLE, buffer, aargv);
1833           if (dbms_errno)
1834             return mr_errcode;
1835
1836           EXEC SQL UPDATE nfsphys SET allocated = allocated + :def_imap_quota
1837             WHERE nfsphys_id = :npid;
1838
1839           EXEC SQL UPDATE users SET potype = 'IMAP', imap_id = :popid
1840             WHERE users_id = :users_id;
1841           com_err(whoami, 0, "pobox set to IMAP:%s:%s", strtrim(machname),
1842                   strtrim(dir));
1843         }
1844     }
1845   
1846   /* change login name, set pobox */
1847   sprintf(buffer, "u.users_id = %d", users_id);
1848   incremental_before(USERS_TABLE, buffer, 0);
1849   nstatus = 2;
1850   if (ostatus == 5 || ostatus == 6 || ostatus == 9)
1851     nstatus = 1;
1852   EXEC SQL UPDATE users SET login = :login, status = :nstatus,
1853     modtime = SYSDATE, modby = :who, modwith = :entity,
1854     pmodtime = SYSDATE, pmodby = :who, pmodwith = :entity,
1855     created = SYSDATE, creator = :who
1856     WHERE users_id = :users_id;
1857
1858   if (dbms_errno)
1859     return mr_errcode;
1860   if (sqlca.sqlerrd[2] != 1)
1861     return MR_INTERNAL;
1862   
1863   /* Only update usage count if we created a POP pobox. */
1864   if (!strcmp(potype, "POP") && !po_exists)
1865     set_pop_usage(mid, 1);
1866   
1867   com_err(whoami, 0, "set login name to %s", login);
1868   incremental_after(USERS_TABLE, buffer, 0);
1869
1870   /* create filesystem */
1871   if (set_next_object_id("filsys_id", FILESYS_TABLE, 0))
1872     return MR_NO_ID;
1873   incremental_clear_before();
1874   if (islower(login[0]) && islower(login[1]))
1875     {
1876       sprintf(directory, "/afs/athena.mit.edu/user/%c/%c/%s",
1877               login[0], login[1], login);
1878     }
1879   else
1880     sprintf(directory, "/afs/athena.mit.edu/user/other/%s", login);
1881
1882   EXEC SQL SELECT value INTO :fsidval FROM numvalues
1883     WHERE numvalues.name = 'filsys_id';
1884   EXEC SQL INSERT INTO filesys
1885     (filsys_id, phys_id, label, type, mach_id, name,
1886      mount, rwaccess, comments, owner, owners, createflg,
1887      lockertype, modtime, modby, modwith)
1888     VALUES
1889     (:fsidval, 0, :login, 'AFS', :m_id, :directory,
1890      '/mit/' || :login, 'w', 'User Locker', :users_id, :list_id, 1,
1891      'HOMEDIR', SYSDATE, :who, :entity);
1892
1893   if (dbms_errno)
1894     return mr_errcode;
1895   if (sqlca.sqlerrd[2] != 1)
1896     return MR_INTERNAL;
1897   sprintf(buffer, "fs.filsys_id = %d", fsidval);
1898   incremental_after(FILESYS_TABLE, buffer, 0);
1899
1900   /* set quota */
1901   incremental_clear_before();
1902   EXEC SQL INSERT INTO quota
1903     (entity_id, filsys_id, type, quota, phys_id, modtime, modby, modwith)
1904     VALUES (0, :fsidval, 'ANY', :def_quota, 0, SYSDATE, :who, :entity);
1905   if (dbms_errno)
1906     return mr_errcode;
1907   if (sqlca.sqlerrd[2] != 1)
1908     return MR_INTERNAL;
1909   aargv[0] = login;
1910   aargv[1] = "ANY";
1911   aargv[2] = login;
1912   sprintf(buffer, "q.entity_id = 0 and q.filsys_id = %d and q.type = 'ANY'",
1913           fsidval);
1914   incremental_after(QUOTA_TABLE, buffer, aargv);
1915   com_err(whoami, 0, "quota of %d assigned", def_quota);
1916   if (dbms_errno)
1917     return mr_errcode;
1918
1919   EXEC SQL UPDATE tblstats SET updates = updates + 1, modtime = SYSDATE
1920     WHERE table_name = 'users';
1921   EXEC SQL UPDATE tblstats SET appends = appends + 1, modtime = SYSDATE
1922     WHERE table_name = 'filesys' OR table_name = 'quota';
1923   if (dbms_errno)
1924     return mr_errcode;
1925   return MR_SUCCESS;
1926 }
1927
1928 /** set_pop_usage - incr/decr usage count for pop server in serverhosts talbe
1929  **
1930  ** Inputs:
1931  **   id of machine
1932  **   delta (will be +/- 1)
1933  **
1934  ** Description:
1935  **   - incr/decr value field in serverhosts table for pop/mach_id
1936  **
1937  **/
1938
1939 int set_pop_usage(id, cnt)
1940     int id, cnt;
1941 {
1942   EXEC SQL BEGIN DECLARE SECTION;
1943   int iid = id, icnt = cnt;
1944   EXEC SQL END DECLARE SECTION;
1945
1946   EXEC SQL UPDATE serverhosts SET value1 = value1 + :icnt
1947     WHERE serverhosts.service = 'POP' AND serverhosts.mach_id = :iid;
1948
1949   if (dbms_errno)
1950     return mr_errcode;
1951   return MR_SUCCESS;
1952 }
1953
1954
1955 int do_user_reservation(struct query *q, char *argv[], client *cl)
1956 {
1957   EXEC SQL BEGIN DECLARE SECTION;
1958   char resv[USERS_RESERVATIONS_SIZE];
1959   char *trans, name[ALIAS_NAME_SIZE];
1960   int uid;
1961   EXEC SQL END DECLARE SECTION;
1962
1963   uid = *(int *)argv[0];
1964   trans = argv[1];
1965
1966   EXEC SQL SELECT UPPER(name) INTO :name FROM alias
1967     WHERE type = 'RESERVE' AND LOWER(trans) = LOWER(:trans);
1968   if (dbms_errno)
1969     return mr_errcode;
1970   if (sqlca.sqlerrd[2] != 1)
1971     return MR_STRING;
1972   name[1] = '\0';
1973
1974   EXEC SQL SELECT reservations INTO :resv FROM users
1975     WHERE users_id = :uid;
1976   if (dbms_errno)
1977     return mr_errcode;
1978   strtrim(resv);
1979
1980   if (!strcmp(q->shortname, "aurv"))
1981     {
1982       if (strchr(resv, *name))
1983         return MR_EXISTS;
1984       if (strlen(resv) == USERS_RESERVATIONS_SIZE - 1)
1985         return MR_ARG_TOO_LONG;
1986
1987       strcat(resv, name);
1988     }
1989   else
1990     {
1991       char *p = strchr(resv, *name);
1992       if (!p)
1993         return MR_NO_MATCH;
1994       memmove(p, p + 1, strlen(p));
1995     }
1996
1997   EXEC SQL UPDATE users SET reservations = NVL(:resv, CHR(0))
1998     WHERE users_id = :uid;
1999   if (dbms_errno)
2000     return mr_errcode;
2001
2002   EXEC SQL UPDATE tblstats SET updates = updates + 1, modtime = SYSDATE
2003     WHERE table_name = 'users';
2004   return set_modtime_by_id(q, argv, cl);
2005 }
2006
2007 int get_user_reservations(struct query *q, char **argv, client *cl,
2008                           int (*action)(int, char *[], void *), void *actarg)
2009 {
2010   EXEC SQL BEGIN DECLARE SECTION;
2011   char resv[USERS_RESERVATIONS_SIZE], *p;
2012   char trans[ALIAS_TRANS_SIZE], name[2] = { 0, 0 };
2013   int uid;
2014   EXEC SQL END DECLARE SECTION;
2015   char *targv[1];
2016
2017   uid = *(int *)argv[0];
2018
2019   EXEC SQL SELECT reservations INTO :resv FROM users
2020     WHERE users_id = :uid;
2021   if (dbms_errno)
2022     return mr_errcode;
2023
2024   targv[0] = trans;
2025   p = resv;
2026   while (*p && !isspace(*p))
2027     {
2028       name[0] = toupper(*p);
2029       EXEC SQL SELECT trans INTO :trans FROM alias
2030         WHERE type = 'RESERVE' AND UPPER(name) = :name;
2031       if (dbms_errno)
2032         return mr_errcode;
2033       if (sqlca.sqlerrd[2] != 1)
2034         sprintf(trans, "Unknown (%s)", name);
2035       (*action)(1, targv, actarg);
2036       p++;
2037     }
2038   return MR_SUCCESS;
2039 }
2040
2041 int get_user_by_reservation(struct query *q, char **argv, client *cl,
2042                             int (*action)(int, char *[], void *), void *actarg)
2043 {
2044   EXEC SQL BEGIN DECLARE SECTION;
2045   char resv[USERS_RESERVATIONS_SIZE], login[USERS_LOGIN_SIZE];
2046   char *trans, name[ALIAS_NAME_SIZE];
2047   int uid;
2048   EXEC SQL END DECLARE SECTION;
2049   char *targv[1];
2050
2051   trans = argv[0];
2052
2053   EXEC SQL SELECT UPPER(name) INTO :name FROM alias
2054     WHERE type = 'RESERVE' AND LOWER(trans) = LOWER(:trans);
2055   if (dbms_errno)
2056     return mr_errcode;
2057   if (sqlca.sqlerrd[2] != 1)
2058     return MR_STRING;
2059   name[1] = '\0';
2060
2061   EXEC SQL DECLARE csr_gubr CURSOR FOR
2062     SELECT login FROM users WHERE reservations LIKE '%' || :name || '%';
2063   EXEC SQL OPEN csr_gubr;
2064   if (dbms_errno)
2065     return mr_errcode;
2066
2067   targv[0] = login;
2068   while (1)
2069     {
2070       EXEC SQL FETCH csr_gubr INTO :login;
2071       if (sqlca.sqlcode)
2072         break;
2073       (*action)(1, targv, actarg);
2074     }
2075   EXEC SQL CLOSE csr_gubr;
2076
2077   return MR_SUCCESS;
2078 }
2079
2080 int update_container(struct query *q, char *argv[], client *cl)
2081 {
2082   EXEC SQL BEGIN DECLARE SECTION;
2083   int cnt_id, acl_id, memacl_id, who, flag;
2084   char name[CONTAINERS_NAME_SIZE], newchildname[CONTAINERS_NAME_SIZE];
2085   char* newname, *entity, *description, *location, *contact, *acl_type, *memacl_type;
2086   EXEC SQL END DECLARE SECTION;
2087   char* tmpchar;
2088   int cnt, childid;
2089   char childname[CONTAINERS_NAME_SIZE];
2090   char *qual;
2091   int index = 0;
2092
2093   cnt_id = *(int *)argv[index++];
2094   newname = argv[index++];
2095
2096   if (q->version >= 9)
2097     flag = atoi(argv[index++]);
2098
2099   description = argv[index++];
2100   location = argv[index++];
2101   contact = argv[index++];
2102   acl_type = argv[index++];
2103   acl_id = *(int *)argv[index++];
2104   memacl_type = argv[index++];
2105   memacl_id = *(int *)argv[index++];
2106   entity = cl->entity;
2107   who = cl->client_id;
2108
2109   EXEC SQL SELECT name INTO :name
2110     FROM containers
2111     WHERE cnt_id = :cnt_id; 
2112
2113   /* trim off the trailing spaces */
2114    strcpy(name, strtrim(name));
2115
2116    qual = xmalloc(MAX_FIELD_WIDTH);     
2117    sprintf(qual, "name = '%s'", name);
2118    incremental_before(CONTAINERS_TABLE, qual, argv);
2119
2120   /* if we are renaming the container */
2121   if (strcmp(name, newname))
2122   {
2123     /* make sure that only the name part of the name has been changed */
2124     tmpchar = strrchr(name, '/');
2125     /* not a top-level name */
2126     if (tmpchar)
2127     {
2128       cnt = tmpchar - name + 1;
2129       /* the parent part of the old and new name should be identical */
2130       if (strncmp(name, newname, cnt))
2131         return MR_NEW_CONTAINER_NAME;
2132     }
2133     /* top level name, new name should be a top level name too */
2134     else
2135     {
2136       if (strrchr(newname, '/'))
2137         return MR_NEW_CONTAINER_NAME;
2138     }
2139
2140     /* update the name for this container */
2141     EXEC SQL UPDATE containers
2142       SET name = :newname
2143     WHERE cnt_id = :cnt_id;
2144
2145     if (dbms_errno)
2146       return mr_errcode;
2147
2148     /* get names for its child containers */
2149     EXEC SQL DECLARE csr_ucon CURSOR FOR
2150       SELECT name, cnt_id FROM containers WHERE name LIKE :name || '/' || '%';
2151   
2152     EXEC SQL OPEN csr_ucon;
2153     if (dbms_errno)
2154       return mr_errcode;
2155
2156     while (1)
2157     {
2158       EXEC SQL FETCH csr_ucon INTO :childname, :childid;
2159       if (sqlca.sqlcode)
2160               break;
2161
2162       strcpy(childname, strtrim(childname));
2163       /* concatenate the new parent name with the existing sub-container name
2164        * we get the sub-containers new name */
2165       tmpchar = childname + strlen(name);
2166       strcpy(newchildname, newname);
2167       strcat(newchildname, tmpchar);
2168
2169       /* update the name */
2170       EXEC SQL UPDATE containers
2171         SET name = :newchildname, modtime = SYSDATE, modby = :who, modwith = :entity
2172       WHERE cnt_id = :childid;
2173
2174       if (sqlca.sqlcode)
2175         break;
2176     }
2177   
2178     EXEC SQL CLOSE csr_ucon; 
2179     if (dbms_errno)
2180       return mr_errcode;
2181   }
2182
2183   /* update the remaining fields */
2184   if (q->version >= 9)
2185   {
2186         EXEC SQL UPDATE containers
2187                 SET publicflg= :flag, description = NVL(:description, CHR(0)), location = NVL(:location, CHR(0)),
2188                         contact = NVL(:contact, CHR(0)), acl_type = :acl_type, acl_id = :acl_id,
2189                         memacl_type = :memacl_type, memacl_id = :memacl_id,
2190                         modtime = SYSDATE, modby = :who, modwith = :entity
2191                 WHERE cnt_id = :cnt_id;
2192   }
2193   else
2194   {
2195     EXEC SQL UPDATE containers
2196                 SET publicflg= :flag, description = NVL(:description, CHR(0)), location = NVL(:location, CHR(0)),
2197                         contact = NVL(:contact, CHR(0)), acl_type = :acl_type, acl_id = :acl_id,
2198                         memacl_type = :memacl_type, memacl_id = :memacl_id,
2199                         modtime = SYSDATE, modby = :who, modwith = :entity
2200                 WHERE cnt_id = :cnt_id;
2201   }
2202
2203   if (dbms_errno)
2204     return mr_errcode;
2205
2206   sprintf(qual, "name = '%s'", newname);
2207   incremental_after(CONTAINERS_TABLE, qual, argv);
2208     
2209   return MR_SUCCESS;
2210 }
2211
2212 int get_machines_of_container(struct query *q, char *argv[], client *cl,
2213                         int (*action)(int, char *[], void *), void *actarg)
2214 {
2215   EXEC SQL BEGIN DECLARE SECTION;
2216   int cnt_id, isrecursive;
2217   char machinename[MACHINE_NAME_SIZE], containername[CONTAINERS_NAME_SIZE];
2218   char *qs;
2219   EXEC SQL END DECLARE SECTION;
2220
2221   char querystring[512], tmp [256];
2222   char *rargv[2];
2223   int found = 0;
2224   
2225   rargv[0] = machinename;
2226   rargv[1] = containername;
2227
2228   cnt_id = *(int *)argv[0];
2229   isrecursive = atoi(argv[1]);
2230
2231   /* get the container name */
2232   
2233   EXEC SQL SELECT name INTO :containername
2234     FROM containers
2235     WHERE cnt_id = :cnt_id; 
2236
2237   /* trim off the trailing spaces */
2238   strcpy(containername, strtrim(containername));
2239
2240   strcpy(querystring, "SELECT a.name, b.name FROM machine a, containers b, mcntmap c ");
2241   strcat(querystring, "WHERE a.mach_id = c.mach_id AND b.cnt_id = c.cnt_id ");
2242   
2243   if (!isrecursive)
2244     sprintf(tmp, "AND b.cnt_id = %d ", cnt_id);
2245   else
2246     sprintf(tmp, "AND (b.cnt_id = %d OR LOWER(b.name) LIKE LOWER('%s/%%')) ", 
2247             cnt_id, containername);
2248
2249   strcat(querystring, tmp);
2250   strcat(querystring, "ORDER BY b.name, a.name");
2251
2252   qs = querystring;
2253
2254   EXEC SQL PREPARE stmt FROM :qs;
2255   if (sqlca.sqlcode)
2256     return MR_INTERNAL;
2257   EXEC SQL DECLARE curs_gmnm CURSOR FOR stmt;
2258   EXEC SQL OPEN curs_gmnm;
2259   
2260    while (1)
2261         {
2262           EXEC SQL FETCH curs_gmnm INTO :machinename, :containername;
2263           if (sqlca.sqlcode)
2264             break;
2265           (*action)(2, rargv, actarg);
2266           found++;
2267         }
2268
2269   EXEC SQL CLOSE curs_gmnm;
2270   if (!found)
2271     return MR_NO_MATCH;
2272   return MR_SUCCESS;
2273 }
2274
2275 int get_subcontainers_of_container(struct query *q, char *argv[], client *cl,
2276                         int (*action)(int, char *[], void *), void *actarg)
2277 {
2278   EXEC SQL BEGIN DECLARE SECTION;
2279   int cnt_id, isrecursive;
2280   char containername[CONTAINERS_NAME_SIZE], subcontainername[CONTAINERS_NAME_SIZE];
2281   char *qs;
2282   EXEC SQL END DECLARE SECTION;
2283
2284   char querystring[2048], tmp [1024];
2285   char *rargv[1];
2286   int found = 0;
2287   
2288   rargv[0] = subcontainername;
2289
2290   cnt_id = *(int *)argv[0];
2291   isrecursive = atoi(argv[1]);
2292
2293   /* get the container name */
2294   
2295   EXEC SQL SELECT name INTO :containername
2296     FROM containers
2297     WHERE cnt_id = :cnt_id; 
2298
2299   /* trim off the trailing spaces */
2300   strcpy(containername, strtrim(containername));
2301
2302   strcpy(querystring, "SELECT name FROM containers ");
2303   
2304   if (!isrecursive)
2305     sprintf(tmp, "WHERE LOWER(name) LIKE LOWER('%s/%%') and LOWER(name) NOT LIKE LOWER('%s/%%/%%') ",
2306             containername, containername);
2307   else
2308     sprintf(tmp, "WHERE LOWER(name) LIKE LOWER('%s/%%') ", containername);
2309
2310   strcat(querystring, tmp);
2311   strcat(querystring, "ORDER BY name");
2312
2313   qs = querystring;
2314
2315   EXEC SQL PREPARE stmt FROM :qs;
2316   if (sqlca.sqlcode)
2317     return MR_INTERNAL;
2318   EXEC SQL DECLARE curs_gsoc CURSOR FOR stmt;
2319   EXEC SQL OPEN curs_gsoc;
2320   
2321    while (1)
2322         {
2323           EXEC SQL FETCH curs_gsoc INTO :subcontainername;
2324           if (sqlca.sqlcode)
2325             break;
2326           (*action)(1, rargv, actarg);
2327           found++;
2328         }
2329
2330   EXEC SQL CLOSE curs_gsoc;
2331   if (!found)
2332     return MR_NO_MATCH;
2333   return MR_SUCCESS;
2334 }
2335
2336 int set_container_list(struct query *q, char *argv[], client *cl)
2337 {
2338   EXEC SQL BEGIN DECLARE SECTION;
2339   int cnt_id, list_id;
2340   EXEC SQL END DECLARE SECTION;
2341
2342   cnt_id = *(int *)argv[0];
2343   list_id = *(int *)argv[1];
2344
2345   EXEC SQL UPDATE containers SET list_id = :list_id WHERE cnt_id = :cnt_id;
2346   if (dbms_errno)
2347     return mr_errcode;
2348
2349   return MR_SUCCESS;
2350 }
This page took 0.327331 seconds and 3 git commands to generate.