]> andersk Git - moira.git/blob - server/qsupport.pc
Don't require a valid account number if we're setting a host to be deleted.
[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    2048
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, '|'))
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             default:
276               error++;
277               break;
278             }
279           dref[dcount] = ref;
280           descendants[dcount++] = id;
281           if (dcount >= MAXLISTDEPTH)
282             {
283               error++;
284               break;
285             }
286         }
287       EXEC SQL CLOSE csr104;
288       if (dbms_errno)
289         return mr_errcode;
290       if (error)
291         return MR_INTERNAL;
292     }
293   for (a = 0; a < acount; a++)
294     {
295       lid = ancestors[a];
296       for (d = 0; d < dcount; d++)
297         {
298           mid = descendants[d];
299           mtype = dtypes[d];
300           if (mid == lid && !strcmp(mtype, "LIST"))
301             return MR_LISTLOOP;
302           EXEC SQL SELECT COUNT(ref_count) INTO :rowcnt
303             FROM imembers
304             WHERE list_id = :lid AND member_id = :mid
305             AND member_type = :mtype;
306           ref = aref[a] * dref[d];
307           if (rowcnt > 0)
308             {
309               if (a == 0 && d == 0)
310                 {
311                   EXEC SQL UPDATE imembers
312                     SET ref_count = ref_count + :ref, direct = 1, tag = :tag
313                     WHERE list_id = :lid AND member_id = :mid
314                     AND member_type = :mtype;
315                 }
316               else
317                 {
318                   EXEC SQL UPDATE imembers
319                     SET ref_count = ref_count + :ref
320                     WHERE list_id = :lid AND member_id = :mid
321                     AND member_type = :mtype;
322                 }
323             }
324           else
325             {
326               incremental_clear_before();
327               if (a == 0 && d == 0)
328                 {
329                   EXEC SQL INSERT INTO imembers
330                     (list_id, member_type, member_id, tag, direct, ref_count)
331                     VALUES (:lid, :mtype, :mid, :tag, 1, :ref);
332                 }
333               else
334                 {
335                   EXEC SQL INSERT INTO imembers
336                     (list_id, member_type, member_id, tag, direct, ref_count)
337                     VALUES (:lid, :mtype, :mid, :tag, 0, :ref);
338                 }
339               iargv[0] = (char *)lid;
340               iargv[1] = mtype;
341               iargv[2] = (char *)mid;
342               incremental_after(IMEMBERS_TABLE, 0, iargv);
343             }
344         }
345     }
346   lid = *(int *)argv[0];
347   entity = cl->entity;
348   who = cl->client_id;
349   EXEC SQL UPDATE list
350     SET modtime = SYSDATE, modby = :who, modwith = :entity
351     WHERE list_id = :lid;
352   if (dbms_errno)
353     return mr_errcode;
354   return MR_SUCCESS;
355 }
356
357
358 /* Delete_member_from_list: do list flattening as we go!
359  */
360
361 int delete_member_from_list(struct query *q, char **argv, client *cl)
362 {
363   EXEC SQL BEGIN DECLARE SECTION;
364   int id, lid, mid, cnt, error, who, ref;
365   char *mtype, dtype[IMEMBERS_MEMBER_TYPE_SIZE], *entity;
366   EXEC SQL END DECLARE SECTION;
367   int ancestors[MAXLISTDEPTH], aref[MAXLISTDEPTH], acount, a;
368   int descendants[MAXLISTDEPTH], dref[MAXLISTDEPTH], dcount, d;
369   char *dtypes[MAXLISTDEPTH];
370   char *iargv[3];
371
372   lid = *(int *)argv[0];
373   mtype = argv[1];
374   mid = *(int *)argv[2];
375
376   if (acl_access_check(lid, cl))
377     return MR_PERM;
378
379   /* if the member is not a direct member of the list, punt */
380   EXEC SQL SELECT COUNT(list_id) INTO :cnt FROM imembers
381     WHERE list_id = :lid AND member_id = :mid
382     AND member_type = :mtype AND direct = 1;
383   if (dbms_errno)
384     return mr_errcode;
385   if (cnt == 0)
386     return MR_NO_MATCH;
387   ancestors[0] = lid;
388   aref[0] = 1;
389   acount = 1;
390   EXEC SQL DECLARE csr105 CURSOR FOR
391     SELECT list_id, ref_count FROM imembers
392     WHERE member_id = :lid AND member_type = 'LIST';
393   if (dbms_errno)
394     return mr_errcode;
395   EXEC SQL OPEN csr105;
396   if (dbms_errno)
397     return mr_errcode;
398   while (1)
399     {
400       EXEC SQL FETCH csr105 INTO :id, :ref;
401       if (sqlca.sqlcode)
402         break;
403       aref[acount] = ref;
404       ancestors[acount++] = id;
405       if (acount >= MAXLISTDEPTH)
406         break;
407     }
408   EXEC SQL CLOSE csr105;
409   if (dbms_errno)
410     return mr_errcode;
411   if (acount >= MAXLISTDEPTH)
412     return MR_INTERNAL;
413   descendants[0] = mid;
414   dtypes[0] = mtype;
415   dref[0] = 1;
416   dcount = 1;
417   error = 0;
418   if (!strcmp(mtype, "LIST"))
419     {
420       EXEC SQL DECLARE csr106 CURSOR FOR
421         SELECT member_id, member_type, ref_count FROM imembers
422         WHERE list_id = :mid;
423       if (dbms_errno)
424         return mr_errcode;
425       EXEC SQL OPEN csr106;
426       if (dbms_errno)
427         return mr_errcode;
428       while (1)
429         {
430           EXEC SQL FETCH csr106 INTO :id, :dtype, :ref;
431           if (sqlca.sqlcode)
432             break;
433           switch (dtype[0])
434             {
435             case 'L':
436               dtypes[dcount] = "LIST";
437               break;
438             case 'U':
439               dtypes[dcount] = "USER";
440               break;
441             case 'S':
442               dtypes[dcount] = "STRING";
443               break;
444             case 'K':
445               dtypes[dcount] = "KERBEROS";
446               break;
447             default:
448               error++;
449               break;
450             }
451           dref[dcount] = ref;
452           descendants[dcount++] = id;
453           if (dcount >= MAXLISTDEPTH)
454             break;
455         }
456       EXEC SQL CLOSE csr106;
457       if (dbms_errno)
458         return mr_errcode;
459       if (error)
460         return MR_INTERNAL;
461     }
462   for (a = 0; a < acount; a++)
463     {
464       lid = ancestors[a];
465       for (d = 0; d < dcount; d++)
466         {
467           mid = descendants[d];
468           mtype = dtypes[d];
469           if (mid == lid && !strcmp(mtype, "LIST"))
470             return MR_LISTLOOP;
471           EXEC SQL SELECT ref_count INTO :cnt FROM imembers
472             WHERE list_id = :lid AND member_id = :mid AND member_type = :mtype;
473           ref = aref[a] * dref[d];
474           if (cnt <= ref)
475             {
476               iargv[0] = (char *)lid;
477               iargv[1] = mtype;
478               iargv[2] = (char *)mid;
479               incremental_before(IMEMBERS_TABLE, 0, iargv);
480               EXEC SQL DELETE FROM imembers
481                 WHERE list_id = :lid AND member_id = :mid
482                 AND member_type= :mtype;
483               incremental_clear_after();
484             }
485           else if (a == 0 && d == 0)
486             {
487               EXEC SQL UPDATE imembers
488                 SET ref_count = ref_count - :ref, direct = 0
489                 WHERE list_id = :lid AND member_id = :mid
490                 AND member_type = :mtype;
491             }
492           else
493             {
494               EXEC SQL UPDATE imembers
495                 SET ref_count = ref_count - :ref
496                 WHERE list_id = :lid AND member_id = :mid
497                 AND member_type = :mtype;
498             }
499         }
500     }
501   lid = *(int *)argv[0];
502   entity = cl->entity;
503   who = cl->client_id;
504   EXEC SQL UPDATE list SET modtime = SYSDATE, modby = :who, modwith = :entity
505     WHERE list_id = :lid;
506   if (dbms_errno)
507     return mr_errcode;
508   return MR_SUCCESS;
509 }
510
511 int tag_member_of_list(struct query *q, char **argv, client *cl)
512 {
513   EXEC SQL BEGIN DECLARE SECTION;
514   int lid, mid, cnt, tag;
515   char *mtype;
516   EXEC SQL END DECLARE SECTION;
517   char *iargv[3];
518
519   lid = *(int *)argv[0];
520   mtype = argv[1];
521   mid = *(int *)argv[2];
522   tag = *(int *)argv[3];
523
524   EXEC SQL SELECT COUNT(member_id) INTO :cnt FROM imembers
525     WHERE member_id = :mid AND member_type = :mtype AND
526     list_id = :lid;
527   if (dbms_errno)
528     return mr_errcode;
529   if (cnt == 0)
530     return MR_NO_MATCH;
531
532   incremental_clear_before();
533   EXEC SQL UPDATE imembers SET tag = :tag WHERE list_id = :lid 
534     AND member_type = :mtype AND member_id = :mid;
535   if (dbms_errno)
536     return mr_errcode;
537   
538   iargv[0] = (char *)lid;
539   iargv[1] = mtype;
540   iargv[2] = (char *)mid;
541   incremental_after(IMEMBERS_TABLE, 0, iargv);
542
543   return MR_SUCCESS;
544 }
545
546 /* Don't allow someone to add someone to a list which is the acl of a
547  * query unless they're on the list acl, even if they're on the amtl
548  * query acl! Also, don't allow someone proxying to add someone to a
549  * capacl.
550  */
551 int acl_access_check(int list_id, client *cl)
552 {
553   EXEC SQL BEGIN DECLARE SECTION;
554   int c1, c2, lid = list_id, acl_id, memacl_id;
555   char acl_type[LIST_ACL_TYPE_SIZE], memacl_type[LIST_ACL_TYPE_SIZE];
556   EXEC SQL END DECLARE SECTION;
557
558   /* Check if the list is directly a capacl */
559   EXEC SQL SELECT COUNT(list_id) INTO :c1 FROM capacls WHERE list_id=:lid;
560
561   /* Check if the list is a member (direct or indirect) of a list that
562      is a capacl */
563   EXEC SQL SELECT COUNT(l1.list_id) INTO :c2 FROM list l1, list l2,
564     imembers im, capacls c WHERE c.list_id = l2.list_id AND
565     im.list_id = l2.list_id AND im.member_type = 'LIST' AND
566     im.member_id = l1.list_id AND l1.list_id = :lid;
567
568   if (c1 == 0 && c2 == 0)
569     return 0;
570
571   if (cl->proxy_id)
572     return 1;
573
574   EXEC SQL SELECT acl_type, acl_id, memacl_type, memacl_id
575     INTO :acl_type, :acl_id, :memacl_type, :memacl_id
576     FROM list WHERE list_id=:lid;
577
578   if (!find_member(acl_type, acl_id, cl))
579     {
580       if (!find_member(memacl_type, memacl_id, cl))
581         return 1;
582     }
583
584   return 0;
585 }
586
587
588 /* get_ace_use - given a type and a name, return a type and a name.
589  * The ace_type is one of "LIST", "USER", "RLIST", or "RUSER" in argv[0],
590  * and argv[1] will contain the ID of the entity in question.  The R*
591  * types mean to recursively look at every containing list, not just
592  * when the object in question is a direct member.  On return, the
593  * usage type will be one of LIST, SERVICE, FILESYS, QUOTA, QUERY, or ZEPHYR.
594  */
595
596 int get_ace_use(struct query *q, char *argv[], client *cl,
597                 int (*action)(int, char *[], void *), void *actarg)
598 {
599   int found = 0;
600   EXEC SQL BEGIN DECLARE SECTION;
601   char *atype;
602   int aid, listid, id;
603   EXEC SQL END DECLARE SECTION;
604   struct save_queue *sq;
605
606   atype = argv[0];
607   aid = *(int *)argv[1];
608   if (!strcmp(atype, "LIST") || !strcmp(atype, "USER") ||
609       !strcmp(atype, "KERBEROS"))
610     return get_ace_internal(atype, aid, action, actarg);
611
612   sq = sq_create();
613   if (!strcmp(atype, "RLIST"))
614     {
615       sq_save_data(sq, (void *)aid);
616       /* get all the list_id's of containing lists */
617       EXEC SQL DECLARE csr107 CURSOR FOR
618         SELECT list_id FROM imembers
619         WHERE member_type = 'LIST' AND member_id = :aid;
620       if (dbms_errno)
621         return mr_errcode;
622       EXEC SQL OPEN csr107;
623       if (dbms_errno)
624         return mr_errcode;
625       while (1)
626         {
627           EXEC SQL FETCH csr107 INTO :listid;
628           if (sqlca.sqlcode)
629             break;
630           sq_save_unique_data(sq, (void *)listid);
631         }
632       EXEC SQL CLOSE csr107;
633       /* now process each one */
634       while (sq_get_data(sq, &id))
635         {
636           if (get_ace_internal("LIST", id, action, actarg) == MR_SUCCESS)
637             found++;
638         }
639     }
640
641   if (!strcmp(atype, "RUSER"))
642     {
643       EXEC SQL DECLARE csr108 CURSOR FOR
644         SELECT list_id FROM imembers
645         WHERE member_type = 'USER' AND member_id = :aid;
646       if (dbms_errno)
647         return mr_errcode;
648       EXEC SQL OPEN csr108;
649       if (dbms_errno)
650         return mr_errcode;
651       while (1)
652         {
653           EXEC SQL FETCH csr108 INTO :listid;
654           if (sqlca.sqlcode)
655             break;
656           sq_save_data(sq, (void *)listid);
657         }
658       EXEC SQL CLOSE csr108;
659       /* now process each one */
660       while (sq_get_data(sq, &id))
661         {
662           if (get_ace_internal("LIST", id, action, actarg) == MR_SUCCESS)
663             found++;
664         }
665       if (get_ace_internal("USER", aid, action, actarg) == MR_SUCCESS)
666         found++;
667     }
668
669   if (!strcmp(atype, "RKERBEROS"))
670     {
671       EXEC SQL DECLARE csr109 CURSOR FOR
672         SELECT list_id FROM imembers
673         WHERE member_type = 'KERBEROS' AND member_id = :aid;
674       if (dbms_errno)
675         return mr_errcode;
676       EXEC SQL OPEN csr109;
677       if (dbms_errno)
678         return mr_errcode;
679       while (1)
680         {
681           EXEC SQL FETCH csr109 INTO :listid;
682           if (sqlca.sqlcode)
683             break;
684           sq_save_data(sq, (void *)listid);
685         }
686       EXEC SQL CLOSE csr109;
687       /* now process each one */
688       while (sq_get_data(sq, &id))
689         {
690           if (get_ace_internal("LIST", id, action, actarg) == MR_SUCCESS)
691             found++;
692         }
693       if (get_ace_internal("KERBEROS", aid, action, actarg) == MR_SUCCESS)
694         found++;
695     }
696
697   sq_destroy(sq);
698   if (dbms_errno)
699     return mr_errcode;
700   if (!found)
701     return MR_NO_MATCH;
702   return MR_SUCCESS;
703 }
704
705
706 /* This looks up a single list or user for ace use.  atype must be "USER"
707  * or "LIST", and aid is the ID of the corresponding object.  This is used
708  * by get_ace_use above.
709  */
710
711 int get_ace_internal(char *atype, int aid,
712                      int (*action)(int, char *[], void *), void *actarg)
713 {
714   char *rargv[2];
715   int found = 0;
716   EXEC SQL BEGIN DECLARE SECTION;
717   char name[MAX_FIELD_WIDTH], *type = atype;
718   int id = aid;
719   EXEC SQL END DECLARE SECTION;
720
721   rargv[1] = name;
722   if (!strcmp(atype, "LIST"))
723     {
724       rargv[0] = "FILESYS";
725       EXEC SQL DECLARE csr110 CURSOR FOR
726         SELECT label FROM filesys
727         WHERE owners = :id;
728       if (dbms_errno)
729         return mr_errcode;
730       EXEC SQL OPEN csr110;
731       if (dbms_errno)
732         return mr_errcode;
733       while (1)
734         {
735           EXEC SQL FETCH csr110 INTO :name;
736           if (sqlca.sqlcode)
737             break;
738           (*action)(2, rargv, actarg);
739           found++;
740         }
741       EXEC SQL CLOSE csr110;
742
743       rargv[0] = "QUERY";
744       EXEC SQL DECLARE csr111 CURSOR FOR
745         SELECT capability FROM capacls
746         WHERE list_id = :id;
747       if (dbms_errno)
748         return mr_errcode;
749       EXEC SQL OPEN csr111;
750       if (dbms_errno)
751         return mr_errcode;
752       while (1)
753         {
754           EXEC SQL FETCH csr111 INTO :name;
755           if (sqlca.sqlcode)
756             break;
757           (*action)(2, rargv, actarg);
758           found++;
759         }
760       EXEC SQL CLOSE csr111;
761     }
762   else if (!strcmp(atype, "USER"))
763     {
764       rargv[0] = "FILESYS";
765       EXEC SQL DECLARE csr112 CURSOR FOR
766         SELECT label FROM filesys
767         WHERE owner = :id;
768       if (dbms_errno)
769         return mr_errcode;
770       EXEC SQL OPEN csr112;
771       if (dbms_errno)
772         return mr_errcode;
773       while (1)
774         {
775           EXEC SQL FETCH csr112 INTO :name;
776           if (sqlca.sqlcode)
777             break;
778           (*action)(2, rargv, actarg);
779           found++;
780         }
781       EXEC SQL CLOSE csr112;
782     }
783
784   rargv[0] = "LIST";
785   EXEC SQL DECLARE csr113 CURSOR FOR
786     SELECT name FROM list
787     WHERE (acl_type = :type AND acl_id = :id)
788     OR (memacl_type = :type AND memacl_id = :id);
789   if (dbms_errno)
790     return mr_errcode;
791   EXEC SQL OPEN csr113;
792   if (dbms_errno)
793     return mr_errcode;
794   while (1)
795     {
796       EXEC SQL FETCH csr113 INTO :name;
797       if (sqlca.sqlcode)
798         break;
799       (*action)(2, rargv, actarg);
800       found++;
801     }
802   EXEC SQL CLOSE csr113;
803
804   rargv[0] = "SERVICE";
805   EXEC SQL DECLARE csr114 CURSOR FOR
806     SELECT name FROM servers
807     WHERE acl_type = :type AND acl_id = :id;
808   if (dbms_errno)
809     return mr_errcode;
810   EXEC SQL OPEN csr114;
811   if (dbms_errno)
812     return mr_errcode;
813   while (1)
814     {
815       EXEC SQL FETCH csr114 INTO :name;
816       if (sqlca.sqlcode)
817         break;
818       (*action)(2, rargv, actarg);
819       found++;
820     }
821   EXEC SQL CLOSE csr114;
822
823   rargv[0] = "HOSTACCESS";
824   EXEC SQL DECLARE csr115 CURSOR FOR
825     SELECT name FROM machine m, hostaccess ha
826     WHERE m.mach_id = ha.mach_id AND ha.acl_type = :type
827     AND ha.acl_id = :id;
828   if (dbms_errno)
829     return mr_errcode;
830   EXEC SQL OPEN csr115;
831   if (dbms_errno)
832     return mr_errcode;
833   while (1)
834     {
835       EXEC SQL FETCH csr115 INTO :name;
836       if (sqlca.sqlcode)
837         break;
838       (*action)(2, rargv, actarg);
839       found++;
840     }
841   EXEC SQL CLOSE csr115;
842
843   rargv[0] = "MACHINE";
844   EXEC SQL DECLARE csr115a CURSOR FOR
845     SELECT name FROM machine m
846     WHERE m.owner_type = :type
847     AND m.owner_id = :id;
848   if (dbms_errno)
849     return mr_errcode;
850   EXEC SQL OPEN csr115a;
851   if (dbms_errno)
852     return mr_errcode;
853   while (1)
854     {
855       EXEC SQL FETCH csr115a INTO :name;
856       if (sqlca.sqlcode)
857         break;
858       (*action)(2, rargv, actarg);
859       found++;
860     }
861   EXEC SQL CLOSE csr115a;
862
863   rargv[0] = "ZEPHYR";
864   EXEC SQL DECLARE csr116 CURSOR FOR
865     SELECT class FROM zephyr z
866     WHERE z.xmt_type = :type AND z.xmt_id = :id
867     OR z.sub_type = :type AND z.sub_id = :id
868     OR z.iws_type = :type AND z.iws_id = :id
869     OR z.iui_type = :type AND z.iui_id = :id
870     OR z.owner_type = :type AND z.owner_id = :id;
871   if (dbms_errno)
872     return mr_errcode;
873   EXEC SQL OPEN csr116;
874   if (dbms_errno)
875     return mr_errcode;
876   while (1)
877     {
878       EXEC SQL FETCH csr116 INTO :name;
879       if (sqlca.sqlcode)
880         break;
881       (*action)(2, rargv, actarg);
882       found++;
883     }
884   EXEC SQL CLOSE csr116;
885   
886   if (!found)
887     return MR_NO_MATCH;
888   return MR_SUCCESS;
889 }
890
891 /* ghbo_internal */
892 int ghbo_internal(char *atype, int aid,
893                   int (*action)(int, char *[], void *), void *actarg)
894 {
895   char *rargv[1];
896   int found = 0;
897   EXEC SQL BEGIN DECLARE SECTION;
898   char name[MACHINE_NAME_SIZE], *type = atype;
899   int id = aid;
900   EXEC SQL END DECLARE SECTION;
901
902   rargv[0] = name;
903   EXEC SQL DECLARE csr115b CURSOR FOR
904     SELECT name FROM machine m
905     WHERE m.owner_type = :type
906     AND m.owner_id = :id;
907   if (dbms_errno)
908     return mr_errcode;
909   EXEC SQL OPEN csr115b;
910   if (dbms_errno)
911     return mr_errcode;
912   while (1)
913     {
914       EXEC SQL FETCH csr115b INTO :name;
915       if (sqlca.sqlcode)
916         break;
917       (*action)(1, rargv, actarg);
918       found++;
919     }
920   EXEC SQL CLOSE csr115b;
921   
922   if (!found)
923     return MR_NO_MATCH;
924   return MR_SUCCESS;
925 }
926
927 /* get_host_by_owner - like gaus but limited to hosts */
928 int get_host_by_owner(struct query *q, char *argv[], client *cl,
929                       int (*action)(int, char *[], void *), void *actarg)
930 {
931   int found = 0;
932   EXEC SQL BEGIN DECLARE SECTION;
933   char *atype;
934   int aid, listid, id;
935   EXEC SQL END DECLARE SECTION;
936   struct save_queue *sq;
937
938   atype = argv[0];
939   aid = *(int *)argv[1];
940   if (!strcmp(atype, "LIST") || !strcmp(atype, "USER") ||
941       !strcmp(atype, "KERBEROS"))
942     return ghbo_internal(atype, aid, action, actarg);
943
944   sq = sq_create();
945   if (!strcmp(atype, "RLIST"))
946     {
947       sq_save_data(sq, (void *)aid);
948       /* get all the list_id's of containing lists */
949       EXEC SQL DECLARE csr107q CURSOR FOR
950         SELECT list_id FROM imembers
951         WHERE member_type = 'LIST' AND member_id = :aid;
952       if (dbms_errno)
953         return mr_errcode;
954       EXEC SQL OPEN csr107q;
955       if (dbms_errno)
956         return mr_errcode;
957       while (1)
958         {
959           EXEC SQL FETCH csr107q INTO :listid;
960           if (sqlca.sqlcode)
961             break;
962           sq_save_unique_data(sq, (void *)listid);
963         }
964       EXEC SQL CLOSE csr107q;
965       /* now process each one */
966       while (sq_get_data(sq, &id))
967         {
968           if (ghbo_internal("LIST", id, action, actarg) == MR_SUCCESS)
969             found++;
970         }
971     }
972
973   if (!strcmp(atype, "RUSER"))
974     {
975       EXEC SQL DECLARE csr108q CURSOR FOR
976         SELECT list_id FROM imembers
977         WHERE member_type = 'USER' AND member_id = :aid;
978       if (dbms_errno)
979         return mr_errcode;
980       EXEC SQL OPEN csr108q;
981       if (dbms_errno)
982         return mr_errcode;
983       while (1)
984         {
985           EXEC SQL FETCH csr108q INTO :listid;
986           if (sqlca.sqlcode)
987             break;
988           sq_save_data(sq, (void *)listid);
989         }
990       EXEC SQL CLOSE csr108q;
991       /* now process each one */
992       while (sq_get_data(sq, &id))
993         {
994           if (ghbo_internal("LIST", id, action, actarg) == MR_SUCCESS)
995             found++;
996         }
997       if (ghbo_internal("USER", aid, action, actarg) == MR_SUCCESS)
998         found++;
999     }
1000
1001   if (!strcmp(atype, "RKERBEROS"))
1002     {
1003       EXEC SQL DECLARE csr109q CURSOR FOR
1004         SELECT list_id FROM imembers
1005         WHERE member_type = 'KERBEROS' AND member_id = :aid;
1006       if (dbms_errno)
1007         return mr_errcode;
1008       EXEC SQL OPEN csr109q;
1009       if (dbms_errno)
1010         return mr_errcode;
1011       while (1)
1012         {
1013           EXEC SQL FETCH csr109q INTO :listid;
1014           if (sqlca.sqlcode)
1015             break;
1016           sq_save_data(sq, (void *)listid);
1017         }
1018       EXEC SQL CLOSE csr109q;
1019       /* now process each one */
1020       while (sq_get_data(sq, &id))
1021         {
1022           if (ghbo_internal("LIST", id, action, actarg) == MR_SUCCESS)
1023             found++;
1024         }
1025       if (ghbo_internal("KERBEROS", aid, action, actarg) == MR_SUCCESS)
1026         found++;
1027     }
1028
1029   sq_destroy(sq);
1030   if (dbms_errno)
1031     return mr_errcode;
1032   if (!found)
1033     return MR_NO_MATCH;
1034   return MR_SUCCESS;
1035 }
1036
1037 /* get_lists_of_member - given a type and a name, return the name and flags
1038  * of all of the lists of the given member.  The member_type is one of
1039  * "LIST", "USER", "STRING", "RLIST", "RUSER", or "RSTRING" in argv[0],
1040  * and argv[1] will contain the ID of the entity in question.  The R*
1041  * types mean to recursively look at every containing list, not just
1042  * when the object in question is a direct member.
1043  */
1044
1045 int get_lists_of_member(struct query *q, char *argv[], client *cl,
1046                         int (*action)(int, char *[], void *), void *actarg)
1047 {
1048   int found = 0, direct = 1;
1049   char *rargv[6];
1050   EXEC SQL BEGIN DECLARE SECTION;
1051   char *atype;
1052   int aid;
1053   char name[LIST_NAME_SIZE];
1054   char active[5], public[5], hidden[5], maillist[5], grouplist[5];
1055   EXEC SQL END DECLARE SECTION;
1056
1057   atype = argv[0];
1058   aid = *(int *)argv[1];
1059   if (!strcmp(atype, "RLIST"))
1060     {
1061       atype = "LIST";
1062       direct = 0;
1063     }
1064   if (!strcmp(atype, "RUSER"))
1065     {
1066       atype = "USER";
1067       direct = 0;
1068     }
1069   if (!strcmp(atype, "RSTRING"))
1070     {
1071       atype = "STRING";
1072       direct = 0;
1073     }
1074   if (!strcmp(atype, "RKERBEROS"))
1075     {
1076       atype = "KERBEROS";
1077       direct = 0;
1078     }
1079
1080   rargv[0] = name;
1081   rargv[1] = active;
1082   rargv[2] = public;
1083   rargv[3] = hidden;
1084   rargv[4] = maillist;
1085   rargv[5] = grouplist;
1086   if (direct)
1087     {
1088       EXEC SQL DECLARE csr117a CURSOR FOR
1089         SELECT l.name, l.active, l.publicflg, l.hidden, l.maillist, l.grouplist
1090         FROM list l, imembers im
1091         WHERE l.list_id = im.list_id AND im.direct = 1
1092         AND im.member_type = :atype AND im.member_id = :aid;
1093       if (dbms_errno)
1094         return mr_errcode;
1095       EXEC SQL OPEN csr117a;
1096       if (dbms_errno)
1097         return mr_errcode;
1098       while (1)
1099         {
1100           EXEC SQL FETCH csr117a
1101             INTO :name, :active, :public, :hidden, :maillist, :grouplist;
1102           if (sqlca.sqlcode)
1103             break;
1104           (*action)(6, rargv, actarg);
1105           found++;
1106         }
1107       EXEC SQL CLOSE csr117a;
1108     }
1109   else
1110     {
1111       EXEC SQL DECLARE csr117b CURSOR FOR
1112         SELECT l.name, l.active, l.publicflg, l.hidden, l.maillist, l.grouplist
1113         FROM list l, imembers im
1114         WHERE l.list_id = im.list_id
1115         AND im.member_type = :atype AND im.member_id = :aid;
1116       if (dbms_errno)
1117         return mr_errcode;
1118       EXEC SQL OPEN csr117b;
1119       if (dbms_errno)
1120         return mr_errcode;
1121       while (1)
1122         {
1123           EXEC SQL FETCH csr117b
1124             INTO :name, :active, :public, :hidden, :maillist, :grouplist;
1125           if (sqlca.sqlcode)
1126             break;
1127           (*action)(6, rargv, actarg);
1128           found++;
1129         }
1130       EXEC SQL CLOSE csr117b;
1131     }
1132
1133   if (dbms_errno)
1134     return mr_errcode;
1135   if (!found)
1136     return MR_NO_MATCH;
1137   return MR_SUCCESS;
1138 }
1139
1140
1141 /* qualified_get_lists: passed "TRUE", "FALSE", or "DONTCARE" for each of
1142  * the five flags associated with each list.  It will return the name of
1143  * each list that meets the quailifications.  It does this by building a
1144  * where clause based on the arguments, then doing a retrieve.
1145  */
1146
1147 static char *lflags[5] = { "active", "publicflg", "hidden", "maillist", "grouplist" };
1148
1149 int qualified_get_lists(struct query *q, char *argv[], client *cl,
1150                         int (*action)(int, char *[], void *), void *actarg)
1151 {
1152   return qualified_get(q, argv, action, actarg, "l.list_id != 0",
1153                        "l", "name", lflags);
1154 }
1155
1156
1157 int get_members_of_list(struct query *q, char *argv[], client *cl,
1158                         int (*action)(int, char *[], void *), void *actarg)
1159 {
1160   EXEC SQL BEGIN DECLARE SECTION;
1161   int list_id, direct;
1162   char member_name[MAX_FIELD_WIDTH], tag[STRINGS_STRING_SIZE];
1163   EXEC SQL END DECLARE SECTION;
1164   char *targv[3];
1165   int targc;
1166
1167   /* For gmol or gtml, only get direct members. For geml, get all. */
1168   if (!strcmp(q->shortname, "geml"))
1169     direct = -1;
1170   else
1171     direct = 0;
1172
1173   /* For gmol or geml, only return type and name. For gtml, return tag too. */
1174   if (!strcmp(q->shortname, "gtml"))
1175     targc = 3;
1176   else
1177     targc = 2;
1178
1179   list_id = *(int *)argv[0];
1180
1181   targv[1] = member_name;
1182   targv[2] = tag;
1183
1184   targv[0] = "USER";
1185   EXEC SQL DECLARE csr119 CURSOR FOR
1186     SELECT u.login, s.string FROM users u, imembers im, strings s
1187     WHERE im.list_id = :list_id AND im.member_type = 'USER'
1188     AND im.member_id = u.users_id AND im.direct > :direct
1189     AND s.string_id = im.tag ORDER BY 1;
1190   if (dbms_errno)
1191     return mr_errcode;
1192   EXEC SQL OPEN csr119;
1193   if (dbms_errno)
1194     return mr_errcode;
1195   while (1)
1196     {
1197       EXEC SQL FETCH csr119 INTO :member_name, :tag;
1198       if (sqlca.sqlcode)
1199         break;
1200       (*action)(targc, targv, actarg);
1201     }
1202   EXEC SQL CLOSE csr119;
1203   if (dbms_errno)
1204     return mr_errcode;
1205
1206   targv[0] = "LIST";
1207   EXEC SQL DECLARE csr120 CURSOR FOR
1208     SELECT l.name, s.string FROM list l, imembers im, strings s
1209     WHERE im.list_id = :list_id AND im.member_type = 'LIST'
1210     AND im.member_id = l.list_id AND im.direct > :direct
1211     AND s.string_id = im.tag ORDER BY 1;
1212   if (dbms_errno)
1213     return mr_errcode;
1214   EXEC SQL OPEN csr120;
1215   if (dbms_errno)
1216     return mr_errcode;
1217   while (1)
1218     {
1219       EXEC SQL FETCH csr120 INTO :member_name, :tag;
1220       if (sqlca.sqlcode)
1221         break;
1222       (*action)(targc, targv, actarg);
1223     }
1224   EXEC SQL CLOSE csr120;
1225   if (dbms_errno)
1226     return mr_errcode;
1227
1228   targv[0] = "STRING";
1229   EXEC SQL DECLARE csr121 CURSOR FOR
1230     SELECT str.string, s.string FROM strings str, imembers im, strings s
1231     WHERE im.list_id = :list_id AND im.member_type = 'STRING'
1232     AND im.member_id = str.string_id AND im.direct > :direct
1233     AND s.string_id = im.tag ORDER BY 1;
1234   if (dbms_errno)
1235     return mr_errcode;
1236   EXEC SQL OPEN csr121;
1237   if (dbms_errno)
1238     return mr_errcode;
1239   while (1)
1240     {
1241       EXEC SQL FETCH csr121 INTO :member_name, :tag;
1242       if (sqlca.sqlcode)
1243         break;
1244       (*action)(targc, targv, actarg);
1245     }
1246   EXEC SQL CLOSE csr121;
1247   if (dbms_errno)
1248     return mr_errcode;
1249
1250   targv[0] = "KERBEROS";
1251   EXEC SQL DECLARE csr122 CURSOR FOR
1252     SELECT str.string, s.string FROM strings str, imembers im, strings s
1253     WHERE im.list_id = :list_id AND im.member_type = 'KERBEROS'
1254     AND im.member_id = str.string_id AND im.direct > :direct
1255     AND s.string_id = im.tag ORDER BY 1;
1256   if (dbms_errno)
1257     return mr_errcode;
1258   EXEC SQL OPEN csr122;
1259   if (dbms_errno)
1260     return mr_errcode;
1261   while (1)
1262     {
1263       EXEC SQL FETCH csr122 INTO :member_name, :tag;
1264       if (sqlca.sqlcode)
1265         break;
1266       (*action)(targc, targv, actarg);
1267     }
1268   EXEC SQL CLOSE csr122;
1269   if (dbms_errno)
1270     return mr_errcode;
1271
1272   return MR_SUCCESS;
1273 }
1274
1275
1276 /* count_members_of_list: this is a simple query, but it cannot be done
1277  * through the dispatch table.
1278  */
1279
1280 int count_members_of_list(struct query *q, char *argv[], client *cl,
1281                           int (*action)(int, char *[], void *), void *actarg)
1282 {
1283   EXEC SQL BEGIN DECLARE SECTION;
1284   int  list, ct = 0;
1285   EXEC SQL END DECLARE SECTION;
1286   char *rargv[1], countbuf[5];
1287
1288   list = *(int *)argv[0];
1289   rargv[0] = countbuf;
1290   EXEC SQL SELECT count (*) INTO :ct FROM imembers
1291     WHERE list_id = :list AND direct = 1;
1292   if (dbms_errno)
1293     return mr_errcode;
1294   sprintf(countbuf, "%d", ct);
1295   (*action)(1, rargv, actarg);
1296   return MR_SUCCESS;
1297 }
1298
1299
1300 /* qualified_get_server: passed "TRUE", "FALSE", or "DONTCARE" for each of
1301  * the three flags associated with each service.  It will return the name of
1302  * each service that meets the quailifications.  It does this by building a
1303  * where clause based on the arguments, then doing a retrieve.
1304  */
1305
1306 static char *sflags[3] = { "enable", "inprogress", "harderror" };
1307
1308 int qualified_get_server(struct query *q, char *argv[], client *cl,
1309                          int (*action)(int, char *[], void *), void *actarg)
1310 {
1311   return qualified_get(q, argv, action, actarg, "s.name is not null",
1312                        "s", "name", sflags);
1313   /* of course, name will never be null, but we need something there
1314      to make qualified_get happy */
1315 }
1316
1317
1318 /* generic qualified get routine, used by qualified_get_lists,
1319  * qualified_get_server, and qualified_get_serverhost.
1320  *   Args:
1321  *      start - a simple where clause, must not be empty
1322  *      range - the name of the range variable
1323  *      field - the field to return
1324  *      flags - an array of strings, names of the flag variables
1325  */
1326
1327 int qualified_get(struct query *q, char *argv[],
1328                   int (*action)(int, char *[], void *), void *actarg,
1329                   char *start, char *range, char *field, char *flags[])
1330 {
1331   char qual[256];
1332   int i;
1333   char buf[32];
1334
1335   strcpy(qual, start);
1336   for (i = 0; i < q->argc; i++)
1337     {
1338       if (!strcmp(argv[i], "TRUE"))
1339         {
1340           sprintf(buf, " AND %s.%s != 0", range, flags[i]);
1341           strcat(qual, buf);
1342         }
1343       else if (!strcmp(argv[i], "FALSE"))
1344         {
1345           sprintf(buf, " AND %s.%s = 0", range, flags[i]);
1346           strcat(qual, buf);
1347         }
1348     }
1349
1350   sprintf(stmt_buf, "SELECT %s.%s FROM %s %s WHERE %s", range, field,
1351           table_name[q->rtable], range, qual);
1352   return do_for_all_rows(stmt_buf, 1, action, actarg);
1353 }
1354
1355
1356 /* qualified_get_serverhost: passed "TRUE", "FALSE", or "DONTCARE" for each of
1357  * the five flags associated with each serverhost.  It will return the name of
1358  * each service and host that meets the quailifications.  It does this by
1359  * building a where clause based on the arguments, then doing a retrieve.
1360  */
1361
1362 static char *shflags[6] = { "service", "enable", "override", "success",
1363                             "inprogress", "hosterror" };
1364
1365 int qualified_get_serverhost(struct query *q, char *argv[], client *cl,
1366                              int (*action)(int, char *[], void *),
1367                              void *actarg)
1368 {
1369   char qual[256], buf[32];
1370   int i;
1371
1372   sprintf(qual, "m.mach_id = sh.mach_id AND sh.service = UPPER('%s')",
1373           argv[0]);
1374   for (i = 1; i < q->argc; i++)
1375     {
1376       if (!strcmp(argv[i], "TRUE"))
1377         {
1378           sprintf(buf, " AND sh.%s != 0", shflags[i]);
1379           strcat(qual, buf);
1380         }
1381       else if (!strcmp(argv[i], "FALSE"))
1382         {
1383           sprintf(buf, " AND sh.%s = 0", shflags[i]);
1384           strcat(qual, buf);
1385         }
1386     }
1387
1388   sprintf(stmt_buf, "SELECT sh.service, m.name FROM serverhosts sh, "
1389           "machine m WHERE %s", qual);
1390   return do_for_all_rows(stmt_buf, 2, action, actarg);
1391 }
1392
1393
1394 /* register_user - change user's login name and allocate a pobox, group,
1395  * filesystem, and quota for them.  The user's status must start out as 0,
1396  * and is left as 2.  Arguments are: user's UID, new login name, and
1397  * pobox type ("POP" = POP, "IMAP" or numeric = IMAP)
1398  */
1399
1400 int register_user(struct query *q, char **argv, client *cl)
1401 {
1402   EXEC SQL BEGIN DECLARE SECTION;
1403   char *login, *entity;
1404   char directory[FILESYS_NAME_SIZE], machname[MACHINE_NAME_SIZE];
1405   char dir[NFSPHYS_DIR_SIZE], *potype;
1406   int who, rowcount, mid, uid, users_id;
1407   int ostatus, nstatus, fsidval, popid;
1408   int npid, tmp;
1409   int po_exists = 0;
1410   static int m_id, def_quota, def_imap_quota, list_id;
1411   EXEC SQL END DECLARE SECTION;
1412   char buffer[256], *aargv[3];
1413
1414   if (!m_id)
1415     {
1416       EXEC SQL SELECT list_id INTO :list_id FROM list
1417         WHERE name = 'wheel';
1418
1419       EXEC SQL SELECT mach_id INTO :m_id FROM machine
1420         WHERE name = 'ATHENA.MIT.EDU';
1421
1422       EXEC SQL SELECT value INTO :def_quota FROM numvalues
1423         WHERE name = 'def_quota';
1424       if (sqlca.sqlerrd[2] != 1)
1425         return MR_NO_QUOTA;
1426
1427       EXEC SQL SELECT value INTO :def_imap_quota FROM numvalues
1428         WHERE name = 'def_imap_quota';
1429       if (sqlca.sqlerrd[2] != 1)
1430         return MR_NO_QUOTA;
1431     }
1432
1433   entity = cl->entity;
1434   who = cl->client_id;
1435
1436   uid = atoi(argv[0]);
1437   login = argv[1];
1438   potype = argv[2];
1439
1440   /* find user */
1441   EXEC SQL SELECT users_id, status INTO :users_id, :ostatus
1442     FROM users
1443     WHERE unix_uid = :uid AND (status = 0 OR status = 5 OR status = 6);
1444
1445   if (sqlca.sqlerrd[2] == 0)
1446     return MR_NO_MATCH;
1447   if (sqlca.sqlerrd[2] > 1)
1448     return MR_NOT_UNIQUE;
1449
1450   /* check new login name */
1451   EXEC SQL SELECT COUNT(login) INTO :rowcount FROM users
1452     WHERE login = :login AND users_id != :users_id;
1453   if (dbms_errno)
1454     return mr_errcode;
1455   if (rowcount > 0)
1456     return MR_IN_USE;
1457   EXEC SQL SELECT COUNT(name) INTO :rowcount FROM list
1458     WHERE LOWER(name) = :login;
1459   if (dbms_errno)
1460     return mr_errcode;
1461   if (rowcount > 0)
1462     return MR_IN_USE;
1463   EXEC SQL SELECT COUNT(label) INTO :rowcount FROM filesys
1464     WHERE label = :login;
1465   if (dbms_errno)
1466     return mr_errcode;
1467   if (rowcount > 0)
1468     return MR_IN_USE;
1469   EXEC SQL SELECT COUNT(label) INTO :rowcount  
1470     FROM filesys WHERE label = :login || '.po';
1471   if (dbms_errno)
1472     return mr_errcode;
1473   if (rowcount > 0)
1474     {
1475       EXEC SQL SELECT owner INTO :tmp FROM filesys 
1476         WHERE label = :login || '.po';
1477       if (dbms_errno)
1478         return mr_errcode;
1479       if ((ostatus == 0) || (tmp != users_id))
1480         return MR_IN_USE;
1481       else
1482         po_exists = 1;
1483     }
1484   EXEC SQL SELECT COUNT(name) INTO :rowcount FROM alias
1485     WHERE ( name = :login OR name = :login || '.po' )
1486     AND type = 'FILESYS';
1487   if (dbms_errno)
1488     return mr_errcode;
1489   if (rowcount > 0)
1490     return MR_IN_USE;
1491   com_err(whoami, 0, "login name OK");
1492
1493   EXEC SQL SELECT COUNT(potype) INTO :rowcount FROM users WHERE
1494     login = :login AND potype = 'POP';
1495   if (dbms_errno)
1496     return mr_errcode;
1497   if (rowcount > 0)
1498     po_exists = 1;
1499
1500   /* choose type and location for pobox */
1501   if (!po_exists)
1502     {
1503       if (!strcmp(potype, "POP"))
1504         {
1505
1506           EXEC SQL DECLARE csr130 CURSOR FOR
1507             SELECT sh.mach_id, m.name FROM serverhosts sh, machine m
1508             WHERE sh.service = 'POP' AND sh.mach_id = m.mach_id
1509             AND sh.value2 - sh.value1 =
1510             (SELECT MAX(value2 - value1) FROM serverhosts WHERE service = 'POP');
1511           if (dbms_errno)
1512             return mr_errcode;
1513           EXEC SQL OPEN csr130;
1514           if (dbms_errno)
1515             return mr_errcode;
1516           EXEC SQL FETCH csr130 INTO :popid, :machname;
1517           if (sqlca.sqlerrd[2] == 0)
1518             {
1519               EXEC SQL CLOSE csr130;
1520               if (dbms_errno)
1521                 return mr_errcode;
1522               return MR_NO_POBOX;
1523             }
1524           else
1525             {
1526               EXEC SQL CLOSE csr130;
1527               if (dbms_errno)
1528                 return mr_errcode;
1529             }
1530       
1531           EXEC SQL UPDATE users SET potype = 'POP', pop_id = :popid
1532             WHERE users_id = :users_id;
1533           com_err(whoami, 0, "pobox set to POP:%s", strtrim(machname));
1534         }         
1535       else
1536         {
1537       /* Select all IMAP nfsphys entries in order of increasing
1538        * (allocated - partsize).  The partitions will almost always be 
1539        * overallocated, but we choose the one that is the least 
1540        * overallocated.
1541        */
1542           potype = "IMAP";
1543           
1544           EXEC SQL DECLARE csr_rusr_imap CURSOR FOR
1545             SELECT np.allocated - np.partsize, np.nfsphys_id, np.mach_id,
1546             np.dir, m.name FROM serverhosts sh, nfsphys np, machine m
1547             WHERE sh.service = 'POSTOFFICE' AND sh.mach_id = np.mach_id
1548             AND m.mach_id = np.mach_id
1549             ORDER BY 1;
1550           if (dbms_errno)
1551             return mr_errcode;
1552           EXEC SQL OPEN csr_rusr_imap;
1553           if (dbms_errno)
1554             return mr_errcode;
1555           EXEC SQL FETCH csr_rusr_imap INTO :tmp, :npid, :mid, :dir, :machname;
1556           if (sqlca.sqlerrd[2] == 0)
1557             {
1558               EXEC SQL CLOSE csr_rusr_imap;
1559               if (dbms_errno)
1560                 return mr_errcode;
1561               return MR_NO_POBOX;
1562             }
1563           else
1564             {
1565               EXEC SQL CLOSE csr_rusr_imap;
1566               if (dbms_errno)
1567                 return mr_errcode;
1568             }
1569
1570           /* create filesystem */
1571           if (set_next_object_id("filsys_id", FILESYS_TABLE, 0))
1572             return MR_NO_ID;
1573           incremental_clear_before();
1574
1575           EXEC SQL SELECT value INTO :popid FROM numvalues
1576             WHERE numvalues.name = 'filsys_id';
1577           EXEC SQL INSERT INTO filesys
1578             (filsys_id, phys_id, label, type, mach_id, name,
1579              mount, rwaccess, comments, owner, owners, createflg,
1580              lockertype, modtime, modby, modwith)
1581             VALUES
1582             (:popid, :npid, :login || '.po', 'IMAP', :mid, :dir,
1583              CHR(0), 'w', 'IMAP box', :users_id, :list_id, 1,
1584              'USER', SYSDATE, :who, :entity);
1585
1586           if (dbms_errno)
1587             return mr_errcode;
1588           if (sqlca.sqlerrd[2] != 1)
1589             return MR_INTERNAL;
1590           sprintf(buffer, "fs.filsys_id = %d", popid);
1591           incremental_after(FILESYS_TABLE, buffer, 0);
1592
1593           /* set quota */
1594           incremental_clear_before();
1595           EXEC SQL INSERT INTO quota
1596             (entity_id, filsys_id, type, quota, phys_id, modtime, modby, modwith)
1597             VALUES (:users_id, :popid, 'USER', :def_imap_quota, :npid,
1598                     SYSDATE, :who, :entity);
1599           if (dbms_errno)
1600             return mr_errcode;
1601           if (sqlca.sqlerrd[2] != 1)
1602             return MR_INTERNAL;
1603           aargv[0] = login;
1604           aargv[1] = "USER";
1605           aargv[2] = login;
1606           sprintf(buffer, "q.entity_id = %d and q.filsys_id = %d and "
1607                   "q.type = 'USER'", users_id, popid);
1608           incremental_after(QUOTA_TABLE, buffer, aargv);
1609           if (dbms_errno)
1610             return mr_errcode;
1611
1612           EXEC SQL UPDATE nfsphys SET allocated = allocated + :def_imap_quota
1613             WHERE nfsphys_id = :npid;
1614
1615           EXEC SQL UPDATE users SET potype = 'IMAP', imap_id = :popid
1616             WHERE users_id = :users_id;
1617           com_err(whoami, 0, "pobox set to IMAP:%s:%s", strtrim(machname),
1618                   strtrim(dir));
1619         }
1620     }
1621   
1622   /* change login name, set pobox */
1623   sprintf(buffer, "u.users_id = %d", users_id);
1624   incremental_before(USERS_TABLE, buffer, 0);
1625   nstatus = 2;
1626   if (ostatus == 5 || ostatus == 6)
1627     nstatus = 1;
1628   EXEC SQL UPDATE users SET login = :login, status = :nstatus,
1629     modtime = SYSDATE, modby = :who, modwith = :entity,
1630     pmodtime = SYSDATE, pmodby = :who, pmodwith = :entity,
1631     created = SYSDATE, creator = :who
1632     WHERE users_id = :users_id;
1633
1634   if (dbms_errno)
1635     return mr_errcode;
1636   if (sqlca.sqlerrd[2] != 1)
1637     return MR_INTERNAL;
1638   
1639   /* Only update usage count if we created a POP pobox. */
1640   if (!strcmp(potype, "POP") && !po_exists)
1641     set_pop_usage(mid, 1);
1642   
1643   com_err(whoami, 0, "set login name to %s", login);
1644   incremental_after(USERS_TABLE, buffer, 0);
1645
1646   /* create filesystem */
1647   if (set_next_object_id("filsys_id", FILESYS_TABLE, 0))
1648     return MR_NO_ID;
1649   incremental_clear_before();
1650   if (islower(login[0]) && islower(login[1]))
1651     {
1652       sprintf(directory, "/afs/athena.mit.edu/user/%c/%c/%s",
1653               login[0], login[1], login);
1654     }
1655   else
1656     sprintf(directory, "/afs/athena.mit.edu/user/other/%s", login);
1657
1658   EXEC SQL SELECT value INTO :fsidval FROM numvalues
1659     WHERE numvalues.name = 'filsys_id';
1660   EXEC SQL INSERT INTO filesys
1661     (filsys_id, phys_id, label, type, mach_id, name,
1662      mount, rwaccess, comments, owner, owners, createflg,
1663      lockertype, modtime, modby, modwith)
1664     VALUES
1665     (:fsidval, 0, :login, 'AFS', :m_id, :directory,
1666      '/mit/' || :login, 'w', 'User Locker', :users_id, :list_id, 1,
1667      'HOMEDIR', SYSDATE, :who, :entity);
1668
1669   if (dbms_errno)
1670     return mr_errcode;
1671   if (sqlca.sqlerrd[2] != 1)
1672     return MR_INTERNAL;
1673   sprintf(buffer, "fs.filsys_id = %d", fsidval);
1674   incremental_after(FILESYS_TABLE, buffer, 0);
1675
1676   /* set quota */
1677   incremental_clear_before();
1678   EXEC SQL INSERT INTO quota
1679     (entity_id, filsys_id, type, quota, phys_id, modtime, modby, modwith)
1680     VALUES (0, :fsidval, 'ANY', :def_quota, 0, SYSDATE, :who, :entity);
1681   if (dbms_errno)
1682     return mr_errcode;
1683   if (sqlca.sqlerrd[2] != 1)
1684     return MR_INTERNAL;
1685   aargv[0] = login;
1686   aargv[1] = "ANY";
1687   aargv[2] = login;
1688   sprintf(buffer, "q.entity_id = 0 and q.filsys_id = %d and q.type = 'ANY'",
1689           fsidval);
1690   incremental_after(QUOTA_TABLE, buffer, aargv);
1691   com_err(whoami, 0, "quota of %d assigned", def_quota);
1692   if (dbms_errno)
1693     return mr_errcode;
1694
1695   EXEC SQL UPDATE tblstats SET updates = updates + 1, modtime = SYSDATE
1696     WHERE table_name = 'users';
1697   EXEC SQL UPDATE tblstats SET appends = appends + 1, modtime = SYSDATE
1698     WHERE table_name = 'filesys' OR table_name = 'quota';
1699   if (dbms_errno)
1700     return mr_errcode;
1701   return MR_SUCCESS;
1702 }
1703
1704 /** set_pop_usage - incr/decr usage count for pop server in serverhosts talbe
1705  **
1706  ** Inputs:
1707  **   id of machine
1708  **   delta (will be +/- 1)
1709  **
1710  ** Description:
1711  **   - incr/decr value field in serverhosts table for pop/mach_id
1712  **
1713  **/
1714
1715 int set_pop_usage(id, cnt)
1716     int id, cnt;
1717 {
1718   EXEC SQL BEGIN DECLARE SECTION;
1719   int iid = id, icnt = cnt;
1720   EXEC SQL END DECLARE SECTION;
1721
1722   EXEC SQL UPDATE serverhosts SET value1 = value1 + :icnt
1723     WHERE serverhosts.service = 'POP' AND serverhosts.mach_id = :iid;
1724
1725   if (dbms_errno)
1726     return mr_errcode;
1727   return MR_SUCCESS;
1728 }
1729
1730
1731 int do_user_reservation(struct query *q, char *argv[], client *cl)
1732 {
1733   EXEC SQL BEGIN DECLARE SECTION;
1734   char resv[USERS_RESERVATIONS_SIZE];
1735   char *trans, name[ALIAS_NAME_SIZE];
1736   int uid;
1737   EXEC SQL END DECLARE SECTION;
1738
1739   uid = *(int *)argv[0];
1740   trans = argv[1];
1741
1742   EXEC SQL SELECT UPPER(name) INTO :name FROM alias
1743     WHERE type = 'RESERVE' AND LOWER(trans) = LOWER(:trans);
1744   if (dbms_errno)
1745     return mr_errcode;
1746   if (sqlca.sqlerrd[2] != 1)
1747     return MR_STRING;
1748   name[1] = '\0';
1749
1750   EXEC SQL SELECT reservations INTO :resv FROM users
1751     WHERE users_id = :uid;
1752   if (dbms_errno)
1753     return mr_errcode;
1754   strtrim(resv);
1755
1756   if (!strcmp(q->shortname, "aurv"))
1757     {
1758       if (strchr(resv, *name))
1759         return MR_EXISTS;
1760       if (strlen(resv) == USERS_RESERVATIONS_SIZE - 1)
1761         return MR_ARG_TOO_LONG;
1762
1763       strcat(resv, name);
1764     }
1765   else
1766     {
1767       char *p = strchr(resv, *name);
1768       if (!p)
1769         return MR_NO_MATCH;
1770       memmove(p, p + 1, strlen(p));
1771     }
1772
1773   EXEC SQL UPDATE users SET reservations = NVL(:resv, CHR(0))
1774     WHERE users_id = :uid;
1775   if (dbms_errno)
1776     return mr_errcode;
1777
1778   EXEC SQL UPDATE tblstats SET updates = updates + 1, modtime = SYSDATE
1779     WHERE table_name = 'users';
1780   return set_modtime_by_id(q, argv, cl);
1781 }
1782
1783 int get_user_reservations(struct query *q, char **argv, client *cl,
1784                           int (*action)(int, char *[], void *), void *actarg)
1785 {
1786   EXEC SQL BEGIN DECLARE SECTION;
1787   char resv[USERS_RESERVATIONS_SIZE], *p;
1788   char trans[ALIAS_TRANS_SIZE], name[2] = { 0, 0 };
1789   int uid;
1790   EXEC SQL END DECLARE SECTION;
1791   char *targv[1];
1792
1793   uid = *(int *)argv[0];
1794
1795   EXEC SQL SELECT reservations INTO :resv FROM users
1796     WHERE users_id = :uid;
1797   if (dbms_errno)
1798     return mr_errcode;
1799
1800   targv[0] = trans;
1801   p = resv;
1802   while (*p && !isspace(*p))
1803     {
1804       name[0] = toupper(*p);
1805       EXEC SQL SELECT trans INTO :trans FROM alias
1806         WHERE type = 'RESERVE' AND UPPER(name) = :name;
1807       if (dbms_errno)
1808         return mr_errcode;
1809       if (sqlca.sqlerrd[2] != 1)
1810         sprintf(trans, "Unknown (%s)", name);
1811       (*action)(1, targv, actarg);
1812       p++;
1813     }
1814   return MR_SUCCESS;
1815 }
1816
1817 int get_user_by_reservation(struct query *q, char **argv, client *cl,
1818                             int (*action)(int, char *[], void *), void *actarg)
1819 {
1820   EXEC SQL BEGIN DECLARE SECTION;
1821   char resv[USERS_RESERVATIONS_SIZE], login[USERS_LOGIN_SIZE];
1822   char *trans, name[ALIAS_NAME_SIZE];
1823   int uid;
1824   EXEC SQL END DECLARE SECTION;
1825   char *targv[1];
1826
1827   trans = argv[0];
1828
1829   EXEC SQL SELECT UPPER(name) INTO :name FROM alias
1830     WHERE type = 'RESERVE' AND LOWER(trans) = LOWER(:trans);
1831   if (dbms_errno)
1832     return mr_errcode;
1833   if (sqlca.sqlerrd[2] != 1)
1834     return MR_STRING;
1835   name[1] = '\0';
1836
1837   EXEC SQL DECLARE csr_gubr CURSOR FOR
1838     SELECT login FROM users WHERE reservations LIKE '%' || :name || '%';
1839   EXEC SQL OPEN csr_gubr;
1840   if (dbms_errno)
1841     return mr_errcode;
1842
1843   targv[0] = login;
1844   while (1)
1845     {
1846       EXEC SQL FETCH csr_gubr INTO :login;
1847       if (sqlca.sqlcode)
1848         break;
1849       (*action)(1, targv, actarg);
1850     }
1851   EXEC SQL CLOSE csr_gubr;
1852
1853   return MR_SUCCESS;
1854 }
1855
1856 int update_container(struct query *q, char *argv[], client *cl)
1857 {
1858   EXEC SQL BEGIN DECLARE SECTION;
1859   int cnt_id, acl_id, memacl_id, who;
1860   char name[CONTAINERS_NAME_SIZE], newchildname[CONTAINERS_NAME_SIZE];
1861   char* newname, *entity, *description, *location, *contact, *acl_type, *memacl_type;
1862   EXEC SQL END DECLARE SECTION;
1863   char* tmpchar;
1864   int cnt, childid;
1865   char childname[CONTAINERS_NAME_SIZE];
1866   char *qual;
1867
1868   cnt_id = *(int *)argv[0];
1869   newname = argv[1];
1870   description = argv[2];
1871   location = argv[3];
1872   contact = argv[4];
1873   acl_type = argv[5];
1874   acl_id = *(int *)argv[6];
1875   memacl_type = argv[7];
1876   memacl_id = *(int *)argv[8];
1877   entity = cl->entity;
1878   who = cl->client_id;
1879
1880   EXEC SQL SELECT name INTO :name
1881     FROM containers
1882     WHERE cnt_id = :cnt_id; 
1883
1884   /* trim off the trailing spaces */
1885    strcpy(name, strtrim(name));
1886
1887    qual = xmalloc(MAX_FIELD_WIDTH);     
1888    sprintf(qual, "name = '%s'", name);
1889    incremental_before(CONTAINERS_TABLE, qual, argv);
1890
1891   /* if we are renaming the container */
1892   if (strcmp(name, newname))
1893   {
1894     /* make sure that only the name part of the name has been changed */
1895     tmpchar = strrchr(name, '/');
1896     /* not a top-level name */
1897     if (tmpchar)
1898     {
1899       cnt = tmpchar - name + 1;
1900       /* the parent part of the old and new name should be identical */
1901       if (strncmp(name, newname, cnt))
1902         return MR_NEW_CONTAINER_NAME;
1903     }
1904     /* top level name, new name should be a top level name too */
1905     else
1906     {
1907       if (strrchr(newname, '/'))
1908         return MR_NEW_CONTAINER_NAME;
1909     }
1910
1911     /* update the name for this container */
1912     EXEC SQL UPDATE containers
1913       SET name = :newname
1914     WHERE cnt_id = :cnt_id;
1915
1916     if (dbms_errno)
1917       return mr_errcode;
1918
1919     /* get names for its child containers */
1920     EXEC SQL DECLARE csr_ucon CURSOR FOR
1921       SELECT name, cnt_id FROM containers WHERE name LIKE :name || '/' || '%';
1922   
1923     EXEC SQL OPEN csr_ucon;
1924     if (dbms_errno)
1925       return mr_errcode;
1926
1927     while (1)
1928     {
1929       EXEC SQL FETCH csr_ucon INTO :childname, :childid;
1930       if (sqlca.sqlcode)
1931               break;
1932
1933       strcpy(childname, strtrim(childname));
1934       /* concatenate the new parent name with the existing sub-container name
1935        * we get the sub-containers new name */
1936       tmpchar = childname + strlen(name);
1937       strcpy(newchildname, newname);
1938       strcat(newchildname, tmpchar);
1939
1940       /* update the name */
1941       EXEC SQL UPDATE containers
1942         SET name = :newchildname, modtime = SYSDATE, modby = :who, modwith = :entity
1943       WHERE cnt_id = :childid;
1944
1945       if (sqlca.sqlcode)
1946         break;
1947     }
1948   
1949     EXEC SQL CLOSE csr_ucon; 
1950     if (dbms_errno)
1951       return mr_errcode;
1952   }
1953
1954   /* update the remaining fields */
1955   EXEC SQL UPDATE containers 
1956     SET description = NVL(:description, CHR(0)), location = NVL(:location, CHR(0)), 
1957       contact = NVL(:contact, CHR(0)), acl_type = :acl_type, acl_id = :acl_id, 
1958       memacl_type = :memacl_type, memacl_id = :memacl_id, 
1959       modtime = SYSDATE, modby = :who, modwith = :entity
1960     WHERE cnt_id = :cnt_id;
1961
1962   if (dbms_errno)
1963     return mr_errcode;
1964
1965   sprintf(qual, "name = '%s'", newname);
1966   incremental_after(CONTAINERS_TABLE, qual, argv);
1967     
1968   return MR_SUCCESS;
1969 }
1970
1971 int get_machines_of_container(struct query *q, char *argv[], client *cl,
1972                         int (*action)(int, char *[], void *), void *actarg)
1973 {
1974   EXEC SQL BEGIN DECLARE SECTION;
1975   int cnt_id, isrecursive;
1976   char machinename[MACHINE_NAME_SIZE], containername[CONTAINERS_NAME_SIZE];
1977   char *qs;
1978   EXEC SQL END DECLARE SECTION;
1979
1980   char querystring[512], tmp [256];
1981   char *rargv[2];
1982   int found = 0;
1983   
1984   rargv[0] = machinename;
1985   rargv[1] = containername;
1986
1987   cnt_id = *(int *)argv[0];
1988   isrecursive = atoi(argv[1]);
1989
1990   /* get the container name */
1991   
1992   EXEC SQL SELECT name INTO :containername
1993     FROM containers
1994     WHERE cnt_id = :cnt_id; 
1995
1996   /* trim off the trailing spaces */
1997   strcpy(containername, strtrim(containername));
1998
1999   strcpy(querystring, "SELECT a.name, b.name FROM machine a, containers b, mcntmap c ");
2000   strcat(querystring, "WHERE a.mach_id = c.mach_id AND b.cnt_id = c.cnt_id ");
2001   
2002   if (!isrecursive)
2003     sprintf(tmp, "AND b.cnt_id = %d ", cnt_id);
2004   else
2005     sprintf(tmp, "AND (b.cnt_id = %d OR b.name LIKE '%s/%%') ", cnt_id, containername);
2006
2007   strcat(querystring, tmp);
2008   strcat(querystring, "ORDER BY b.name, a.name");
2009
2010   qs = querystring;
2011
2012   EXEC SQL PREPARE stmt FROM :qs;
2013   if (sqlca.sqlcode)
2014     return MR_INTERNAL;
2015   EXEC SQL DECLARE curs_gmnm CURSOR FOR stmt;
2016   EXEC SQL OPEN curs_gmnm;
2017   
2018    while (1)
2019         {
2020           EXEC SQL FETCH curs_gmnm INTO :machinename, :containername;
2021           if (sqlca.sqlcode)
2022             break;
2023           (*action)(2, rargv, actarg);
2024           found++;
2025         }
2026
2027   EXEC SQL CLOSE curs_gmnm;
2028   if (!found)
2029     return MR_NO_MATCH;
2030   return MR_SUCCESS;
2031 }
2032
2033 int get_subcontainers_of_container(struct query *q, char *argv[], client *cl,
2034                         int (*action)(int, char *[], void *), void *actarg)
2035 {
2036   EXEC SQL BEGIN DECLARE SECTION;
2037   int cnt_id, isrecursive;
2038   char containername[CONTAINERS_NAME_SIZE], subcontainername[CONTAINERS_NAME_SIZE];
2039   char *qs;
2040   EXEC SQL END DECLARE SECTION;
2041
2042   char querystring[2048], tmp [1024];
2043   char *rargv[1];
2044   int found = 0;
2045   
2046   rargv[0] = subcontainername;
2047
2048   cnt_id = *(int *)argv[0];
2049   isrecursive = atoi(argv[1]);
2050
2051   /* get the container name */
2052   
2053   EXEC SQL SELECT name INTO :containername
2054     FROM containers
2055     WHERE cnt_id = :cnt_id; 
2056
2057   /* trim off the trailing spaces */
2058   strcpy(containername, strtrim(containername));
2059
2060   strcpy(querystring, "SELECT name FROM containers ");
2061   
2062   if (!isrecursive)
2063     sprintf(tmp, "WHERE name LIKE '%s/%%' and name NOT LIKE '%s/%%/%%' ", containername, 
2064         containername);
2065   else
2066     sprintf(tmp, "WHERE name LIKE '%s/%%' ", containername);
2067
2068   strcat(querystring, tmp);
2069   strcat(querystring, "ORDER BY name");
2070
2071   qs = querystring;
2072
2073   EXEC SQL PREPARE stmt FROM :qs;
2074   if (sqlca.sqlcode)
2075     return MR_INTERNAL;
2076   EXEC SQL DECLARE curs_gsoc CURSOR FOR stmt;
2077   EXEC SQL OPEN curs_gsoc;
2078   
2079    while (1)
2080         {
2081           EXEC SQL FETCH curs_gsoc INTO :subcontainername;
2082           if (sqlca.sqlcode)
2083             break;
2084           (*action)(1, rargv, actarg);
2085           found++;
2086         }
2087
2088   EXEC SQL CLOSE curs_gsoc;
2089   if (!found)
2090     return MR_NO_MATCH;
2091   return MR_SUCCESS;
2092 }
This page took 0.32607 seconds and 5 git commands to generate.