]> andersk Git - moira.git/blob - server/qsupport.pc
Don't check for a valid local po box if we're changing from one SMTP address to another
[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    1024
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, 1);
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, 1);
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
512 /* Don't allow someone to add someone to a list which is the acl of a
513  * query unless they're on the list acl, even if they're on the amtl
514  * query acl! Also, don't allow someone proxying to add someone to a
515  * capacl.
516  */
517 int acl_access_check(int list_id, client *cl)
518 {
519   EXEC SQL BEGIN DECLARE SECTION;
520   int c1, c2, lid = list_id, acl_id;
521   char acl_type[LIST_ACL_TYPE_SIZE];
522   EXEC SQL END DECLARE SECTION;
523
524   /* Check if the list is directly a capacl */
525   EXEC SQL SELECT COUNT(list_id) INTO :c1 FROM capacls WHERE list_id=:lid;
526
527   /* Check if the list is a member (direct or indirect) of a list that
528      is a capacl */
529   EXEC SQL SELECT COUNT(l1.list_id) INTO :c2 FROM list l1, list l2,
530     imembers im, capacls c WHERE c.list_id = l2.list_id AND
531     im.list_id = l2.list_id AND im.member_type = 'LIST' AND
532     im.member_id = l1.list_id AND l1.list_id = :lid;
533
534   if (c1 == 0 && c2 == 0)
535     return 0;
536
537   if (cl->proxy_id)
538     return 1;
539
540   EXEC SQL SELECT acl_type, acl_id INTO :acl_type, :acl_id
541     FROM list WHERE list_id=:lid;
542   return !find_member(acl_type, acl_id, cl);
543 }
544
545
546 /* get_ace_use - given a type and a name, return a type and a name.
547  * The ace_type is one of "LIST", "USER", "RLIST", or "RUSER" in argv[0],
548  * and argv[1] will contain the ID of the entity in question.  The R*
549  * types mean to recursively look at every containing list, not just
550  * when the object in question is a direct member.  On return, the
551  * usage type will be one of LIST, SERVICE, FILESYS, QUOTA, QUERY, or ZEPHYR.
552  */
553
554 int get_ace_use(struct query *q, char *argv[], client *cl,
555                 int (*action)(int, char *[], void *), void *actarg)
556 {
557   int found = 0;
558   EXEC SQL BEGIN DECLARE SECTION;
559   char *atype;
560   int aid, listid, id;
561   EXEC SQL END DECLARE SECTION;
562   struct save_queue *sq;
563
564   atype = argv[0];
565   aid = *(int *)argv[1];
566   if (!strcmp(atype, "LIST") || !strcmp(atype, "USER") ||
567       !strcmp(atype, "KERBEROS"))
568     return get_ace_internal(atype, aid, action, actarg);
569
570   sq = sq_create();
571   if (!strcmp(atype, "RLIST"))
572     {
573       sq_save_data(sq, (void *)aid);
574       /* get all the list_id's of containing lists */
575       EXEC SQL DECLARE csr107 CURSOR FOR
576         SELECT list_id FROM imembers
577         WHERE member_type = 'LIST' AND member_id = :aid;
578       if (dbms_errno)
579         return mr_errcode;
580       EXEC SQL OPEN csr107;
581       if (dbms_errno)
582         return mr_errcode;
583       while (1)
584         {
585           EXEC SQL FETCH csr107 INTO :listid;
586           if (sqlca.sqlcode)
587             break;
588           sq_save_unique_data(sq, (void *)listid);
589         }
590       EXEC SQL CLOSE csr107;
591       /* now process each one */
592       while (sq_get_data(sq, &id))
593         {
594           if (get_ace_internal("LIST", id, action, actarg) == MR_SUCCESS)
595             found++;
596         }
597     }
598
599   if (!strcmp(atype, "RUSER"))
600     {
601       EXEC SQL DECLARE csr108 CURSOR FOR
602         SELECT list_id FROM imembers
603         WHERE member_type = 'USER' AND member_id = :aid;
604       if (dbms_errno)
605         return mr_errcode;
606       EXEC SQL OPEN csr108;
607       if (dbms_errno)
608         return mr_errcode;
609       while (1)
610         {
611           EXEC SQL FETCH csr108 INTO :listid;
612           if (sqlca.sqlcode)
613             break;
614           sq_save_data(sq, (void *)listid);
615         }
616       EXEC SQL CLOSE csr108;
617       /* now process each one */
618       while (sq_get_data(sq, &id))
619         {
620           if (get_ace_internal("LIST", id, action, actarg) == MR_SUCCESS)
621             found++;
622         }
623       if (get_ace_internal("USER", aid, action, actarg) == MR_SUCCESS)
624         found++;
625     }
626
627   if (!strcmp(atype, "RKERBEROS"))
628     {
629       EXEC SQL DECLARE csr109 CURSOR FOR
630         SELECT list_id FROM imembers
631         WHERE member_type = 'KERBEROS' AND member_id = :aid;
632       if (dbms_errno)
633         return mr_errcode;
634       EXEC SQL OPEN csr109;
635       if (dbms_errno)
636         return mr_errcode;
637       while (1)
638         {
639           EXEC SQL FETCH csr109 INTO :listid;
640           if (sqlca.sqlcode)
641             break;
642           sq_save_data(sq, (void *)listid);
643         }
644       EXEC SQL CLOSE csr109;
645       /* now process each one */
646       while (sq_get_data(sq, &id))
647         {
648           if (get_ace_internal("LIST", id, action, actarg) == MR_SUCCESS)
649             found++;
650         }
651       if (get_ace_internal("KERBEROS", aid, action, actarg) == MR_SUCCESS)
652         found++;
653     }
654
655   sq_destroy(sq);
656   if (dbms_errno)
657     return mr_errcode;
658   if (!found)
659     return MR_NO_MATCH;
660   return MR_SUCCESS;
661 }
662
663
664 /* This looks up a single list or user for ace use.  atype must be "USER"
665  * or "LIST", and aid is the ID of the corresponding object.  This is used
666  * by get_ace_use above.
667  */
668
669 int get_ace_internal(char *atype, int aid,
670                      int (*action)(int, char *[], void *), void *actarg)
671 {
672   char *rargv[2];
673   int found = 0;
674   EXEC SQL BEGIN DECLARE SECTION;
675   char name[MAX_FIELD_WIDTH], *type = atype;
676   int id = aid;
677   EXEC SQL END DECLARE SECTION;
678
679   rargv[1] = name;
680   if (!strcmp(atype, "LIST"))
681     {
682       rargv[0] = "FILESYS";
683       EXEC SQL DECLARE csr110 CURSOR FOR
684         SELECT label FROM filesys
685         WHERE owners = :id;
686       if (dbms_errno)
687         return mr_errcode;
688       EXEC SQL OPEN csr110;
689       if (dbms_errno)
690         return mr_errcode;
691       while (1)
692         {
693           EXEC SQL FETCH csr110 INTO :name;
694           if (sqlca.sqlcode)
695             break;
696           (*action)(2, rargv, actarg);
697           found++;
698         }
699       EXEC SQL CLOSE csr110;
700
701       rargv[0] = "QUERY";
702       EXEC SQL DECLARE csr111 CURSOR FOR
703         SELECT capability FROM capacls
704         WHERE list_id = :id;
705       if (dbms_errno)
706         return mr_errcode;
707       EXEC SQL OPEN csr111;
708       if (dbms_errno)
709         return mr_errcode;
710       while (1)
711         {
712           EXEC SQL FETCH csr111 INTO :name;
713           if (sqlca.sqlcode)
714             break;
715           (*action)(2, rargv, actarg);
716           found++;
717         }
718       EXEC SQL CLOSE csr111;
719     }
720   else if (!strcmp(atype, "USER"))
721     {
722       rargv[0] = "FILESYS";
723       EXEC SQL DECLARE csr112 CURSOR FOR
724         SELECT label FROM filesys
725         WHERE owner = :id;
726       if (dbms_errno)
727         return mr_errcode;
728       EXEC SQL OPEN csr112;
729       if (dbms_errno)
730         return mr_errcode;
731       while (1)
732         {
733           EXEC SQL FETCH csr112 INTO :name;
734           if (sqlca.sqlcode)
735             break;
736           (*action)(2, rargv, actarg);
737           found++;
738         }
739       EXEC SQL CLOSE csr112;
740     }
741
742   rargv[0] = "LIST";
743   EXEC SQL DECLARE csr113 CURSOR FOR
744     SELECT name FROM list
745     WHERE acl_type = :type AND acl_id = :id;
746   if (dbms_errno)
747     return mr_errcode;
748   EXEC SQL OPEN csr113;
749   if (dbms_errno)
750     return mr_errcode;
751   while (1)
752     {
753       EXEC SQL FETCH csr113 INTO :name;
754       if (sqlca.sqlcode)
755         break;
756       (*action)(2, rargv, actarg);
757       found++;
758     }
759   EXEC SQL CLOSE csr113;
760
761   rargv[0] = "SERVICE";
762   EXEC SQL DECLARE csr114 CURSOR FOR
763     SELECT name FROM servers
764     WHERE acl_type = :type AND acl_id = :id;
765   if (dbms_errno)
766     return mr_errcode;
767   EXEC SQL OPEN csr114;
768   if (dbms_errno)
769     return mr_errcode;
770   while (1)
771     {
772       EXEC SQL FETCH csr114 INTO :name;
773       if (sqlca.sqlcode)
774         break;
775       (*action)(2, rargv, actarg);
776       found++;
777     }
778   EXEC SQL CLOSE csr114;
779
780   rargv[0] = "HOSTACCESS";
781   EXEC SQL DECLARE csr115 CURSOR FOR
782     SELECT name FROM machine m, hostaccess ha
783     WHERE m.mach_id = ha.mach_id AND ha.acl_type = :type
784     AND ha.acl_id = :id;
785   if (dbms_errno)
786     return mr_errcode;
787   EXEC SQL OPEN csr115;
788   if (dbms_errno)
789     return mr_errcode;
790   while (1)
791     {
792       EXEC SQL FETCH csr115 INTO :name;
793       if (sqlca.sqlcode)
794         break;
795       (*action)(2, rargv, actarg);
796       found++;
797     }
798   EXEC SQL CLOSE csr115;
799
800   rargv[0] = "MACHINE";
801   EXEC SQL DECLARE csr115a CURSOR FOR
802     SELECT name FROM machine m
803     WHERE m.owner_type = :type
804     AND m.owner_id = :id;
805   if (dbms_errno)
806     return mr_errcode;
807   EXEC SQL OPEN csr115a;
808   if (dbms_errno)
809     return mr_errcode;
810   while (1)
811     {
812       EXEC SQL FETCH csr115a INTO :name;
813       if (sqlca.sqlcode)
814         break;
815       (*action)(2, rargv, actarg);
816       found++;
817     }
818   EXEC SQL CLOSE csr115a;
819
820   rargv[0] = "ZEPHYR";
821   EXEC SQL DECLARE csr116 CURSOR FOR
822     SELECT class FROM zephyr z
823     WHERE z.xmt_type = :type AND z.xmt_id = :id
824     OR z.sub_type = :type AND z.sub_id = :id
825     OR z.iws_type = :type AND z.iws_id = :id
826     OR z.iui_type = :type AND z.iui_id = :id;
827   if (dbms_errno)
828     return mr_errcode;
829   EXEC SQL OPEN csr116;
830   if (dbms_errno)
831     return mr_errcode;
832   while (1)
833     {
834       EXEC SQL FETCH csr116 INTO :name;
835       if (sqlca.sqlcode)
836         break;
837       (*action)(2, rargv, actarg);
838       found++;
839     }
840   EXEC SQL CLOSE csr116;
841   
842   if (!found)
843     return MR_NO_MATCH;
844   return MR_SUCCESS;
845 }
846
847 /* ghbo_internal */
848 int ghbo_internal(char *atype, int aid,
849                   int (*action)(int, char *[], void *), void *actarg)
850 {
851   char *rargv[1];
852   int found = 0;
853   EXEC SQL BEGIN DECLARE SECTION;
854   char name[MACHINE_NAME_SIZE], *type = atype;
855   int id = aid;
856   EXEC SQL END DECLARE SECTION;
857
858   rargv[0] = name;
859   EXEC SQL DECLARE csr115b CURSOR FOR
860     SELECT name FROM machine m
861     WHERE m.owner_type = :type
862     AND m.owner_id = :id;
863   if (dbms_errno)
864     return mr_errcode;
865   EXEC SQL OPEN csr115b;
866   if (dbms_errno)
867     return mr_errcode;
868   while (1)
869     {
870       EXEC SQL FETCH csr115b INTO :name;
871       if (sqlca.sqlcode)
872         break;
873       (*action)(1, rargv, actarg);
874       found++;
875     }
876   EXEC SQL CLOSE csr115b;
877   
878   if (!found)
879     return MR_NO_MATCH;
880   return MR_SUCCESS;
881 }
882
883 /* get_host_by_owner - like gaus but limited to hosts */
884 int get_host_by_owner(struct query *q, char *argv[], client *cl,
885                       int (*action)(int, char *[], void *), void *actarg)
886 {
887   int found = 0;
888   EXEC SQL BEGIN DECLARE SECTION;
889   char *atype;
890   int aid, listid, id;
891   EXEC SQL END DECLARE SECTION;
892   struct save_queue *sq;
893
894   atype = argv[0];
895   aid = *(int *)argv[1];
896   if (!strcmp(atype, "LIST") || !strcmp(atype, "USER") ||
897       !strcmp(atype, "KERBEROS"))
898     return ghbo_internal(atype, aid, action, actarg);
899
900   sq = sq_create();
901   if (!strcmp(atype, "RLIST"))
902     {
903       sq_save_data(sq, (void *)aid);
904       /* get all the list_id's of containing lists */
905       EXEC SQL DECLARE csr107q CURSOR FOR
906         SELECT list_id FROM imembers
907         WHERE member_type = 'LIST' AND member_id = :aid;
908       if (dbms_errno)
909         return mr_errcode;
910       EXEC SQL OPEN csr107q;
911       if (dbms_errno)
912         return mr_errcode;
913       while (1)
914         {
915           EXEC SQL FETCH csr107q INTO :listid;
916           if (sqlca.sqlcode)
917             break;
918           sq_save_unique_data(sq, (void *)listid);
919         }
920       EXEC SQL CLOSE csr107q;
921       /* now process each one */
922       while (sq_get_data(sq, &id))
923         {
924           if (ghbo_internal("LIST", id, action, actarg) == MR_SUCCESS)
925             found++;
926         }
927     }
928
929   if (!strcmp(atype, "RUSER"))
930     {
931       EXEC SQL DECLARE csr108q CURSOR FOR
932         SELECT list_id FROM imembers
933         WHERE member_type = 'USER' AND member_id = :aid;
934       if (dbms_errno)
935         return mr_errcode;
936       EXEC SQL OPEN csr108q;
937       if (dbms_errno)
938         return mr_errcode;
939       while (1)
940         {
941           EXEC SQL FETCH csr108q INTO :listid;
942           if (sqlca.sqlcode)
943             break;
944           sq_save_data(sq, (void *)listid);
945         }
946       EXEC SQL CLOSE csr108q;
947       /* now process each one */
948       while (sq_get_data(sq, &id))
949         {
950           if (ghbo_internal("LIST", id, action, actarg) == MR_SUCCESS)
951             found++;
952         }
953       if (ghbo_internal("USER", aid, action, actarg) == MR_SUCCESS)
954         found++;
955     }
956
957   if (!strcmp(atype, "RKERBEROS"))
958     {
959       EXEC SQL DECLARE csr109q CURSOR FOR
960         SELECT list_id FROM imembers
961         WHERE member_type = 'KERBEROS' AND member_id = :aid;
962       if (dbms_errno)
963         return mr_errcode;
964       EXEC SQL OPEN csr109q;
965       if (dbms_errno)
966         return mr_errcode;
967       while (1)
968         {
969           EXEC SQL FETCH csr109q INTO :listid;
970           if (sqlca.sqlcode)
971             break;
972           sq_save_data(sq, (void *)listid);
973         }
974       EXEC SQL CLOSE csr109q;
975       /* now process each one */
976       while (sq_get_data(sq, &id))
977         {
978           if (ghbo_internal("LIST", id, action, actarg) == MR_SUCCESS)
979             found++;
980         }
981       if (ghbo_internal("KERBEROS", aid, action, actarg) == MR_SUCCESS)
982         found++;
983     }
984
985   sq_destroy(sq);
986   if (dbms_errno)
987     return mr_errcode;
988   if (!found)
989     return MR_NO_MATCH;
990   return MR_SUCCESS;
991 }
992
993 /* get_lists_of_member - given a type and a name, return the name and flags
994  * of all of the lists of the given member.  The member_type is one of
995  * "LIST", "USER", "STRING", "RLIST", "RUSER", or "RSTRING" in argv[0],
996  * and argv[1] will contain the ID of the entity in question.  The R*
997  * types mean to recursively look at every containing list, not just
998  * when the object in question is a direct member.
999  */
1000
1001 int get_lists_of_member(struct query *q, char *argv[], client *cl,
1002                         int (*action)(int, char *[], void *), void *actarg)
1003 {
1004   int found = 0, direct = 1;
1005   char *rargv[6];
1006   EXEC SQL BEGIN DECLARE SECTION;
1007   char *atype;
1008   int aid;
1009   char name[LIST_NAME_SIZE];
1010   char active[5], public[5], hidden[5], maillist[5], grouplist[5];
1011   EXEC SQL END DECLARE SECTION;
1012
1013   atype = argv[0];
1014   aid = *(int *)argv[1];
1015   if (!strcmp(atype, "RLIST"))
1016     {
1017       atype = "LIST";
1018       direct = 0;
1019     }
1020   if (!strcmp(atype, "RUSER"))
1021     {
1022       atype = "USER";
1023       direct = 0;
1024     }
1025   if (!strcmp(atype, "RSTRING"))
1026     {
1027       atype = "STRING";
1028       direct = 0;
1029     }
1030   if (!strcmp(atype, "RKERBEROS"))
1031     {
1032       atype = "KERBEROS";
1033       direct = 0;
1034     }
1035
1036   rargv[0] = name;
1037   rargv[1] = active;
1038   rargv[2] = public;
1039   rargv[3] = hidden;
1040   rargv[4] = maillist;
1041   rargv[5] = grouplist;
1042   if (direct)
1043     {
1044       EXEC SQL DECLARE csr117a CURSOR FOR
1045         SELECT l.name, l.active, l.publicflg, l.hidden, l.maillist, l.grouplist
1046         FROM list l, imembers im
1047         WHERE l.list_id = im.list_id AND im.direct = 1
1048         AND im.member_type = :atype AND im.member_id = :aid;
1049       if (dbms_errno)
1050         return mr_errcode;
1051       EXEC SQL OPEN csr117a;
1052       if (dbms_errno)
1053         return mr_errcode;
1054       while (1)
1055         {
1056           EXEC SQL FETCH csr117a
1057             INTO :name, :active, :public, :hidden, :maillist, :grouplist;
1058           if (sqlca.sqlcode)
1059             break;
1060           (*action)(6, rargv, actarg);
1061           found++;
1062         }
1063       EXEC SQL CLOSE csr117a;
1064     }
1065   else
1066     {
1067       EXEC SQL DECLARE csr117b CURSOR FOR
1068         SELECT l.name, l.active, l.publicflg, l.hidden, l.maillist, l.grouplist
1069         FROM list l, imembers im
1070         WHERE l.list_id = im.list_id
1071         AND im.member_type = :atype AND im.member_id = :aid;
1072       if (dbms_errno)
1073         return mr_errcode;
1074       EXEC SQL OPEN csr117b;
1075       if (dbms_errno)
1076         return mr_errcode;
1077       while (1)
1078         {
1079           EXEC SQL FETCH csr117b
1080             INTO :name, :active, :public, :hidden, :maillist, :grouplist;
1081           if (sqlca.sqlcode)
1082             break;
1083           (*action)(6, rargv, actarg);
1084           found++;
1085         }
1086       EXEC SQL CLOSE csr117b;
1087     }
1088
1089   if (dbms_errno)
1090     return mr_errcode;
1091   if (!found)
1092     return MR_NO_MATCH;
1093   return MR_SUCCESS;
1094 }
1095
1096
1097 /* qualified_get_lists: passed "TRUE", "FALSE", or "DONTCARE" for each of
1098  * the five flags associated with each list.  It will return the name of
1099  * each list that meets the quailifications.  It does this by building a
1100  * where clause based on the arguments, then doing a retrieve.
1101  */
1102
1103 static char *lflags[5] = { "active", "publicflg", "hidden", "maillist", "grouplist" };
1104
1105 int qualified_get_lists(struct query *q, char *argv[], client *cl,
1106                         int (*action)(int, char *[], void *), void *actarg)
1107 {
1108   return qualified_get(q, argv, action, actarg, "l.list_id != 0",
1109                        "l", "name", lflags);
1110 }
1111
1112
1113 int get_members_of_list(struct query *q, char *argv[], client *cl,
1114                         int (*action)(int, char *[], void *), void *actarg)
1115 {
1116   EXEC SQL BEGIN DECLARE SECTION;
1117   int list_id, direct;
1118   char member_name[MAX_FIELD_WIDTH], tag[STRINGS_STRING_SIZE];
1119   EXEC SQL END DECLARE SECTION;
1120   char *targv[3];
1121   int targc;
1122
1123   /* For gmol or gtml, only get direct members. For geml, get all. */
1124   if (!strcmp(q->shortname, "geml"))
1125     direct = -1;
1126   else
1127     direct = 0;
1128
1129   /* For gmol or geml, only return type and name. For gtml, return tag too. */
1130   if (!strcmp(q->shortname, "gtml"))
1131     targc = 3;
1132   else
1133     targc = 2;
1134
1135   list_id = *(int *)argv[0];
1136
1137   targv[1] = member_name;
1138   targv[2] = tag;
1139
1140   targv[0] = "USER";
1141   EXEC SQL DECLARE csr119 CURSOR FOR
1142     SELECT u.login, s.string FROM users u, imembers im, strings s
1143     WHERE im.list_id = :list_id AND im.member_type = 'USER'
1144     AND im.member_id = u.users_id AND im.direct > :direct
1145     AND s.string_id = im.tag ORDER BY 1;
1146   if (dbms_errno)
1147     return mr_errcode;
1148   EXEC SQL OPEN csr119;
1149   if (dbms_errno)
1150     return mr_errcode;
1151   while (1)
1152     {
1153       EXEC SQL FETCH csr119 INTO :member_name, :tag;
1154       if (sqlca.sqlcode)
1155         break;
1156       (*action)(targc, targv, actarg);
1157     }
1158   EXEC SQL CLOSE csr119;
1159   if (dbms_errno)
1160     return mr_errcode;
1161
1162   targv[0] = "LIST";
1163   EXEC SQL DECLARE csr120 CURSOR FOR
1164     SELECT l.name, s.string FROM list l, imembers im, strings s
1165     WHERE im.list_id = :list_id AND im.member_type = 'LIST'
1166     AND im.member_id = l.list_id AND im.direct > :direct
1167     AND s.string_id = im.tag ORDER BY 1;
1168   if (dbms_errno)
1169     return mr_errcode;
1170   EXEC SQL OPEN csr120;
1171   if (dbms_errno)
1172     return mr_errcode;
1173   while (1)
1174     {
1175       EXEC SQL FETCH csr120 INTO :member_name, :tag;
1176       if (sqlca.sqlcode)
1177         break;
1178       (*action)(targc, targv, actarg);
1179     }
1180   EXEC SQL CLOSE csr120;
1181   if (dbms_errno)
1182     return mr_errcode;
1183
1184   targv[0] = "STRING";
1185   EXEC SQL DECLARE csr121 CURSOR FOR
1186     SELECT str.string, s.string FROM strings str, imembers im, strings s
1187     WHERE im.list_id = :list_id AND im.member_type = 'STRING'
1188     AND im.member_id = str.string_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 csr121;
1193   if (dbms_errno)
1194     return mr_errcode;
1195   while (1)
1196     {
1197       EXEC SQL FETCH csr121 INTO :member_name, :tag;
1198       if (sqlca.sqlcode)
1199         break;
1200       (*action)(targc, targv, actarg);
1201     }
1202   EXEC SQL CLOSE csr121;
1203   if (dbms_errno)
1204     return mr_errcode;
1205
1206   targv[0] = "KERBEROS";
1207   EXEC SQL DECLARE csr122 CURSOR FOR
1208     SELECT str.string, s.string FROM strings str, imembers im, strings s
1209     WHERE im.list_id = :list_id AND im.member_type = 'KERBEROS'
1210     AND im.member_id = str.string_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 csr122;
1215   if (dbms_errno)
1216     return mr_errcode;
1217   while (1)
1218     {
1219       EXEC SQL FETCH csr122 INTO :member_name, :tag;
1220       if (sqlca.sqlcode)
1221         break;
1222       (*action)(targc, targv, actarg);
1223     }
1224   EXEC SQL CLOSE csr122;
1225   if (dbms_errno)
1226     return mr_errcode;
1227
1228   return MR_SUCCESS;
1229 }
1230
1231
1232 /* count_members_of_list: this is a simple query, but it cannot be done
1233  * through the dispatch table.
1234  */
1235
1236 int count_members_of_list(struct query *q, char *argv[], client *cl,
1237                           int (*action)(int, char *[], void *), void *actarg)
1238 {
1239   EXEC SQL BEGIN DECLARE SECTION;
1240   int  list, ct = 0;
1241   EXEC SQL END DECLARE SECTION;
1242   char *rargv[1], countbuf[5];
1243
1244   list = *(int *)argv[0];
1245   rargv[0] = countbuf;
1246   EXEC SQL SELECT count (*) INTO :ct FROM imembers
1247     WHERE list_id = :list AND direct = 1;
1248   if (dbms_errno)
1249     return mr_errcode;
1250   sprintf(countbuf, "%d", ct);
1251   (*action)(1, rargv, actarg);
1252   return MR_SUCCESS;
1253 }
1254
1255
1256 /* qualified_get_server: passed "TRUE", "FALSE", or "DONTCARE" for each of
1257  * the three flags associated with each service.  It will return the name of
1258  * each service that meets the quailifications.  It does this by building a
1259  * where clause based on the arguments, then doing a retrieve.
1260  */
1261
1262 static char *sflags[3] = { "enable", "inprogress", "harderror" };
1263
1264 int qualified_get_server(struct query *q, char *argv[], client *cl,
1265                          int (*action)(int, char *[], void *), void *actarg)
1266 {
1267   return qualified_get(q, argv, action, actarg, "s.name is not null",
1268                        "s", "name", sflags);
1269   /* of course, name will never be null, but we need something there
1270      to make qualified_get happy */
1271 }
1272
1273
1274 /* generic qualified get routine, used by qualified_get_lists,
1275  * qualified_get_server, and qualified_get_serverhost.
1276  *   Args:
1277  *      start - a simple where clause, must not be empty
1278  *      range - the name of the range variable
1279  *      field - the field to return
1280  *      flags - an array of strings, names of the flag variables
1281  */
1282
1283 int qualified_get(struct query *q, char *argv[],
1284                   int (*action)(int, char *[], void *), void *actarg,
1285                   char *start, char *range, char *field, char *flags[])
1286 {
1287   char qual[256];
1288   int i;
1289   char buf[32];
1290
1291   strcpy(qual, start);
1292   for (i = 0; i < q->argc; i++)
1293     {
1294       if (!strcmp(argv[i], "TRUE"))
1295         {
1296           sprintf(buf, " AND %s.%s != 0", range, flags[i]);
1297           strcat(qual, buf);
1298         }
1299       else if (!strcmp(argv[i], "FALSE"))
1300         {
1301           sprintf(buf, " AND %s.%s = 0", range, flags[i]);
1302           strcat(qual, buf);
1303         }
1304     }
1305
1306   sprintf(stmt_buf, "SELECT %s.%s FROM %s %s WHERE %s", range, field,
1307           table_name[q->rtable], range, qual);
1308   return do_for_all_rows(stmt_buf, 1, action, actarg);
1309 }
1310
1311
1312 /* qualified_get_serverhost: passed "TRUE", "FALSE", or "DONTCARE" for each of
1313  * the five flags associated with each serverhost.  It will return the name of
1314  * each service and host that meets the quailifications.  It does this by
1315  * building a where clause based on the arguments, then doing a retrieve.
1316  */
1317
1318 static char *shflags[6] = { "service", "enable", "override", "success",
1319                             "inprogress", "hosterror" };
1320
1321 int qualified_get_serverhost(struct query *q, char *argv[], client *cl,
1322                              int (*action)(int, char *[], void *),
1323                              void *actarg)
1324 {
1325   char qual[256], buf[32];
1326   int i;
1327
1328   sprintf(qual, "m.mach_id = sh.mach_id AND sh.service = UPPER('%s')",
1329           argv[0]);
1330   for (i = 1; i < q->argc; i++)
1331     {
1332       if (!strcmp(argv[i], "TRUE"))
1333         {
1334           sprintf(buf, " AND sh.%s != 0", shflags[i]);
1335           strcat(qual, buf);
1336         }
1337       else if (!strcmp(argv[i], "FALSE"))
1338         {
1339           sprintf(buf, " AND sh.%s = 0", shflags[i]);
1340           strcat(qual, buf);
1341         }
1342     }
1343
1344   sprintf(stmt_buf, "SELECT sh.service, m.name FROM serverhosts sh, "
1345           "machine m WHERE %s", qual);
1346   return do_for_all_rows(stmt_buf, 2, action, actarg);
1347 }
1348
1349
1350 /* register_user - change user's login name and allocate a pobox, group,
1351  * filesystem, and quota for them.  The user's status must start out as 0,
1352  * and is left as 2.  Arguments are: user's UID, new login name, and
1353  * pobox type ("POP" = POP, "IMAP" or numeric = IMAP)
1354  */
1355
1356 int register_user(struct query *q, char **argv, client *cl)
1357 {
1358   EXEC SQL BEGIN DECLARE SECTION;
1359   char *login, *entity;
1360   char directory[FILESYS_NAME_SIZE], machname[MACHINE_NAME_SIZE];
1361   char dir[NFSPHYS_DIR_SIZE], *potype;
1362   int who, rowcount, mid, uid, users_id;
1363   int ostatus, nstatus, fsidval, popid;
1364   int npid, tmp;
1365   int po_exists = 0;
1366   static int m_id, def_quota, def_imap_quota, list_id;
1367   EXEC SQL END DECLARE SECTION;
1368   char buffer[256], *aargv[3];
1369
1370   if (!m_id)
1371     {
1372       EXEC SQL SELECT list_id INTO :list_id FROM list
1373         WHERE name = 'wheel';
1374
1375       EXEC SQL SELECT mach_id INTO :m_id FROM machine
1376         WHERE name = 'ATHENA.MIT.EDU';
1377
1378       EXEC SQL SELECT value INTO :def_quota FROM numvalues
1379         WHERE name = 'def_quota';
1380       if (sqlca.sqlerrd[2] != 1)
1381         return MR_NO_QUOTA;
1382
1383       EXEC SQL SELECT value INTO :def_imap_quota FROM numvalues
1384         WHERE name = 'def_imap_quota';
1385       if (sqlca.sqlerrd[2] != 1)
1386         return MR_NO_QUOTA;
1387     }
1388
1389   entity = cl->entity;
1390   who = cl->client_id;
1391
1392   uid = atoi(argv[0]);
1393   login = argv[1];
1394   potype = argv[2];
1395
1396   /* find user */
1397   EXEC SQL SELECT users_id, status INTO :users_id, :ostatus
1398     FROM users
1399     WHERE unix_uid = :uid AND (status = 0 OR status = 5 OR status = 6);
1400
1401   if (sqlca.sqlerrd[2] == 0)
1402     return MR_NO_MATCH;
1403   if (sqlca.sqlerrd[2] > 1)
1404     return MR_NOT_UNIQUE;
1405
1406   /* check new login name */
1407   EXEC SQL SELECT COUNT(login) INTO :rowcount FROM users
1408     WHERE login = :login AND users_id != :users_id;
1409   if (dbms_errno)
1410     return mr_errcode;
1411   if (rowcount > 0)
1412     return MR_IN_USE;
1413   EXEC SQL SELECT COUNT(name) INTO :rowcount FROM list
1414     WHERE LOWER(name) = :login;
1415   if (dbms_errno)
1416     return mr_errcode;
1417   if (rowcount > 0)
1418     return MR_IN_USE;
1419   EXEC SQL SELECT COUNT(label) INTO :rowcount FROM filesys
1420     WHERE label = :login;
1421   if (dbms_errno)
1422     return mr_errcode;
1423   if (rowcount > 0)
1424     return MR_IN_USE;
1425   EXEC SQL SELECT COUNT(label) INTO :rowcount  
1426     FROM filesys WHERE label = :login || '.po';
1427   if (dbms_errno)
1428     return mr_errcode;
1429   if (rowcount > 0)
1430     {
1431       EXEC SQL SELECT owner INTO :tmp FROM filesys 
1432         WHERE label = :login || '.po';
1433       if (dbms_errno)
1434         return mr_errcode;
1435       if ((ostatus == 0) || (tmp != users_id))
1436         return MR_IN_USE;
1437       EXEC SQL SELECT count(potype) INTO :rowcount FROM users WHERE
1438         login = :login AND potype = 'IMAP';
1439       if (dbms_errno)
1440         return mr_errcode;
1441       if (rowcount > 0)
1442         po_exists = 1;
1443     }
1444   EXEC SQL SELECT COUNT(name) INTO :rowcount FROM alias
1445     WHERE ( name = :login OR name = :login || '.po' )
1446     AND type = 'FILESYS';
1447   if (dbms_errno)
1448     return mr_errcode;
1449   if (rowcount > 0)
1450     return MR_IN_USE;
1451   com_err(whoami, 0, "login name OK");
1452
1453   EXEC SQL SELECT COUNT(potype) INTO :rowcount FROM users WHERE
1454     login = :login AND potype = 'POP';
1455   if (dbms_errno)
1456     return mr_errcode;
1457   if (rowcount > 0)
1458     po_exists = 1;
1459
1460   /* choose type and location for pobox */
1461   if (!po_exists)
1462     {
1463       if (!strcmp(potype, "POP"))
1464         {
1465
1466           EXEC SQL DECLARE csr130 CURSOR FOR
1467             SELECT sh.mach_id, m.name FROM serverhosts sh, machine m
1468             WHERE sh.service = 'POP' AND sh.mach_id = m.mach_id
1469             AND sh.value2 - sh.value1 =
1470             (SELECT MAX(value2 - value1) FROM serverhosts WHERE service = 'POP');
1471           if (dbms_errno)
1472             return mr_errcode;
1473           EXEC SQL OPEN csr130;
1474           if (dbms_errno)
1475             return mr_errcode;
1476           EXEC SQL FETCH csr130 INTO :popid, :machname;
1477           if (sqlca.sqlerrd[2] == 0)
1478             {
1479               EXEC SQL CLOSE csr130;
1480               if (dbms_errno)
1481                 return mr_errcode;
1482               return MR_NO_POBOX;
1483             }
1484           else
1485             {
1486               EXEC SQL CLOSE csr130;
1487               if (dbms_errno)
1488                 return mr_errcode;
1489             }
1490       
1491           EXEC SQL UPDATE users SET potype = 'POP', pop_id = :popid
1492             WHERE users_id = :users_id;
1493           com_err(whoami, 0, "pobox set to POP:%s", strtrim(machname));
1494         }         
1495       else
1496         {
1497       /* Select all IMAP nfsphys entries in order of increasing
1498        * (allocated - partsize).  The partitions will almost always be 
1499        * overallocated, but we choose the one that is the least 
1500        * overallocated.
1501        */
1502           potype = "IMAP";
1503           
1504           EXEC SQL DECLARE csr_rusr_imap CURSOR FOR
1505             SELECT np.allocated - np.partsize, np.nfsphys_id, np.mach_id,
1506             np.dir, m.name FROM serverhosts sh, nfsphys np, machine m
1507             WHERE sh.service = 'POSTOFFICE' AND sh.mach_id = np.mach_id
1508             AND m.mach_id = np.mach_id
1509             ORDER BY 1;
1510           if (dbms_errno)
1511             return mr_errcode;
1512           EXEC SQL OPEN csr_rusr_imap;
1513           if (dbms_errno)
1514             return mr_errcode;
1515           EXEC SQL FETCH csr_rusr_imap INTO :tmp, :npid, :mid, :dir, :machname;
1516           if (sqlca.sqlerrd[2] == 0)
1517             {
1518               EXEC SQL CLOSE csr_rusr_imap;
1519               if (dbms_errno)
1520                 return mr_errcode;
1521               return MR_NO_POBOX;
1522             }
1523           else
1524             {
1525               EXEC SQL CLOSE csr_rusr_imap;
1526               if (dbms_errno)
1527                 return mr_errcode;
1528             }
1529
1530           /* create filesystem */
1531           if (set_next_object_id("filsys_id", FILESYS_TABLE, 0))
1532             return MR_NO_ID;
1533           incremental_clear_before();
1534
1535           EXEC SQL SELECT value INTO :popid FROM numvalues
1536             WHERE numvalues.name = 'filsys_id';
1537           EXEC SQL INSERT INTO filesys
1538             (filsys_id, phys_id, label, type, mach_id, name,
1539              mount, rwaccess, comments, owner, owners, createflg,
1540              lockertype, modtime, modby, modwith)
1541             VALUES
1542             (:popid, :npid, :login || '.po', 'IMAP', :mid, :dir,
1543              CHR(0), 'w', 'IMAP box', :users_id, :list_id, 1,
1544              'USER', SYSDATE, :who, :entity);
1545
1546           if (dbms_errno)
1547             return mr_errcode;
1548           if (sqlca.sqlerrd[2] != 1)
1549             return MR_INTERNAL;
1550           sprintf(buffer, "fs.filsys_id = %d", popid);
1551           incremental_after(FILESYS_TABLE, buffer, 0);
1552
1553           /* set quota */
1554           incremental_clear_before();
1555           EXEC SQL INSERT INTO quota
1556             (entity_id, filsys_id, type, quota, phys_id, modtime, modby, modwith)
1557             VALUES (:users_id, :popid, 'USER', :def_imap_quota, :npid,
1558                     SYSDATE, :who, :entity);
1559           if (dbms_errno)
1560             return mr_errcode;
1561           if (sqlca.sqlerrd[2] != 1)
1562             return MR_INTERNAL;
1563           aargv[0] = login;
1564           aargv[1] = "USER";
1565           aargv[2] = login;
1566           sprintf(buffer, "q.entity_id = %d and q.filsys_id = %d and "
1567                   "q.type = 'USER'", users_id, popid);
1568           incremental_after(QUOTA_TABLE, buffer, aargv);
1569           if (dbms_errno)
1570             return mr_errcode;
1571
1572           EXEC SQL UPDATE nfsphys SET allocated = allocated + :def_imap_quota
1573             WHERE nfsphys_id = :npid;
1574
1575           EXEC SQL UPDATE users SET potype = 'IMAP', imap_id = :popid
1576             WHERE users_id = :users_id;
1577           com_err(whoami, 0, "pobox set to IMAP:%s:%s", strtrim(machname),
1578                   strtrim(dir));
1579         }
1580     }
1581   
1582   /* change login name, set pobox */
1583   sprintf(buffer, "u.users_id = %d", users_id);
1584   incremental_before(USERS_TABLE, buffer, 0);
1585   nstatus = 2;
1586   if (ostatus == 5 || ostatus == 6)
1587     nstatus = 1;
1588   EXEC SQL UPDATE users SET login = :login, status = :nstatus,
1589     modtime = SYSDATE, modby = :who, modwith = :entity,
1590     pmodtime = SYSDATE, pmodby = :who, pmodwith = :entity
1591     WHERE users_id = :users_id;
1592
1593   if (dbms_errno)
1594     return mr_errcode;
1595   if (sqlca.sqlerrd[2] != 1)
1596     return MR_INTERNAL;
1597   
1598   /* Only update usage count if we created a POP pobox. */
1599   if (!strcmp(potype, "POP") && !po_exists)
1600     set_pop_usage(mid, 1);
1601   
1602   com_err(whoami, 0, "set login name to %s", login);
1603   incremental_after(USERS_TABLE, buffer, 0);
1604
1605   /* We've just changed the login name in the DB; recache it in case
1606      the wrong thing got into the cache earlier. */
1607   cache_entry(login, USERS_TABLE, users_id);
1608
1609   /* create filesystem */
1610   if (set_next_object_id("filsys_id", FILESYS_TABLE, 0))
1611     return MR_NO_ID;
1612   incremental_clear_before();
1613   if (islower(login[0]) && islower(login[1]))
1614     {
1615       sprintf(directory, "/afs/athena.mit.edu/user/%c/%c/%s",
1616               login[0], login[1], login);
1617     }
1618   else
1619     sprintf(directory, "/afs/athena.mit.edu/user/other/%s", login);
1620
1621   EXEC SQL SELECT value INTO :fsidval FROM numvalues
1622     WHERE numvalues.name = 'filsys_id';
1623   EXEC SQL INSERT INTO filesys
1624     (filsys_id, phys_id, label, type, mach_id, name,
1625      mount, rwaccess, comments, owner, owners, createflg,
1626      lockertype, modtime, modby, modwith)
1627     VALUES
1628     (:fsidval, 0, :login, 'AFS', :m_id, :directory,
1629      '/mit/' || :login, 'w', 'User Locker', :users_id, :list_id, 1,
1630      'HOMEDIR', SYSDATE, :who, :entity);
1631
1632   if (dbms_errno)
1633     return mr_errcode;
1634   if (sqlca.sqlerrd[2] != 1)
1635     return MR_INTERNAL;
1636   sprintf(buffer, "fs.filsys_id = %d", fsidval);
1637   incremental_after(FILESYS_TABLE, buffer, 0);
1638
1639   /* set quota */
1640   incremental_clear_before();
1641   EXEC SQL INSERT INTO quota
1642     (entity_id, filsys_id, type, quota, phys_id, modtime, modby, modwith)
1643     VALUES (0, :fsidval, 'ANY', :def_quota, 0, SYSDATE, :who, :entity);
1644   if (dbms_errno)
1645     return mr_errcode;
1646   if (sqlca.sqlerrd[2] != 1)
1647     return MR_INTERNAL;
1648   aargv[0] = login;
1649   aargv[1] = "ANY";
1650   aargv[2] = login;
1651   sprintf(buffer, "q.entity_id = 0 and q.filsys_id = %d and q.type = 'ANY'",
1652           fsidval);
1653   incremental_after(QUOTA_TABLE, buffer, aargv);
1654   com_err(whoami, 0, "quota of %d assigned", def_quota);
1655   if (dbms_errno)
1656     return mr_errcode;
1657
1658   EXEC SQL UPDATE tblstats SET updates = updates + 1, modtime = SYSDATE
1659     WHERE table_name = 'users';
1660   EXEC SQL UPDATE tblstats SET appends = appends + 1, modtime = SYSDATE
1661     WHERE table_name = 'filesys' OR table_name = 'quota';
1662   if (dbms_errno)
1663     return mr_errcode;
1664   return MR_SUCCESS;
1665 }
1666
1667 /** set_pop_usage - incr/decr usage count for pop server in serverhosts talbe
1668  **
1669  ** Inputs:
1670  **   id of machine
1671  **   delta (will be +/- 1)
1672  **
1673  ** Description:
1674  **   - incr/decr value field in serverhosts table for pop/mach_id
1675  **
1676  **/
1677
1678 int set_pop_usage(id, cnt)
1679     int id, cnt;
1680 {
1681   EXEC SQL BEGIN DECLARE SECTION;
1682   int iid = id, icnt = cnt;
1683   EXEC SQL END DECLARE SECTION;
1684
1685   EXEC SQL UPDATE serverhosts SET value1 = value1 + :icnt
1686     WHERE serverhosts.service = 'POP' AND serverhosts.mach_id = :iid;
1687
1688   if (dbms_errno)
1689     return mr_errcode;
1690   return MR_SUCCESS;
1691 }
1692
1693
1694 int do_user_reservation(struct query *q, char *argv[], client *cl)
1695 {
1696   EXEC SQL BEGIN DECLARE SECTION;
1697   char resv[USERS_RESERVATIONS_SIZE];
1698   char *trans, name[ALIAS_NAME_SIZE];
1699   int uid;
1700   EXEC SQL END DECLARE SECTION;
1701
1702   uid = *(int *)argv[0];
1703   trans = argv[1];
1704
1705   EXEC SQL SELECT UPPER(name) INTO :name FROM alias
1706     WHERE type = 'RESERVE' AND LOWER(trans) = LOWER(:trans);
1707   if (dbms_errno)
1708     return mr_errcode;
1709   if (sqlca.sqlerrd[2] != 1)
1710     return MR_STRING;
1711   name[1] = '\0';
1712
1713   EXEC SQL SELECT reservations INTO :resv FROM users
1714     WHERE users_id = :uid;
1715   if (dbms_errno)
1716     return mr_errcode;
1717   strtrim(resv);
1718
1719   if (!strcmp(q->shortname, "aurv"))
1720     {
1721       if (strchr(resv, *name))
1722         return MR_EXISTS;
1723       if (strlen(resv) == USERS_RESERVATIONS_SIZE - 1)
1724         return MR_ARG_TOO_LONG;
1725
1726       strcat(resv, name);
1727     }
1728   else
1729     {
1730       char *p = strchr(resv, *name);
1731       if (!p)
1732         return MR_NO_MATCH;
1733       memmove(p, p + 1, strlen(p));
1734     }
1735
1736   EXEC SQL UPDATE users SET reservations = NVL(:resv, CHR(0))
1737     WHERE users_id = :uid;
1738   if (dbms_errno)
1739     return mr_errcode;
1740
1741   EXEC SQL UPDATE tblstats SET updates = updates + 1, modtime = SYSDATE
1742     WHERE table_name = 'users';
1743   return set_modtime_by_id(q, argv, cl);
1744 }
1745
1746 int get_user_reservations(struct query *q, char **argv, client *cl,
1747                           int (*action)(int, char *[], void *), void *actarg)
1748 {
1749   EXEC SQL BEGIN DECLARE SECTION;
1750   char resv[USERS_RESERVATIONS_SIZE], *p;
1751   char trans[ALIAS_TRANS_SIZE], name[2] = { 0, 0 };
1752   int uid;
1753   EXEC SQL END DECLARE SECTION;
1754   char *targv[1];
1755
1756   uid = *(int *)argv[0];
1757
1758   EXEC SQL SELECT reservations INTO :resv FROM users
1759     WHERE users_id = :uid;
1760   if (dbms_errno)
1761     return mr_errcode;
1762
1763   targv[0] = trans;
1764   p = resv;
1765   while (*p && !isspace(*p))
1766     {
1767       name[0] = toupper(*p);
1768       EXEC SQL SELECT trans INTO :trans FROM alias
1769         WHERE type = 'RESERVE' AND UPPER(name) = :name;
1770       if (dbms_errno)
1771         return mr_errcode;
1772       if (sqlca.sqlerrd[2] != 1)
1773         sprintf(trans, "Unknown (%s)", name);
1774       (*action)(1, targv, actarg);
1775       p++;
1776     }
1777   return MR_SUCCESS;
1778 }
1779
1780 int get_user_by_reservation(struct query *q, char **argv, client *cl,
1781                             int (*action)(int, char *[], void *), void *actarg)
1782 {
1783   EXEC SQL BEGIN DECLARE SECTION;
1784   char resv[USERS_RESERVATIONS_SIZE], login[USERS_LOGIN_SIZE];
1785   char *trans, name[ALIAS_NAME_SIZE];
1786   int uid;
1787   EXEC SQL END DECLARE SECTION;
1788   char *targv[1];
1789
1790   trans = argv[0];
1791
1792   EXEC SQL SELECT UPPER(name) INTO :name FROM alias
1793     WHERE type = 'RESERVE' AND LOWER(trans) = LOWER(:trans);
1794   if (dbms_errno)
1795     return mr_errcode;
1796   if (sqlca.sqlerrd[2] != 1)
1797     return MR_STRING;
1798   name[1] = '\0';
1799
1800   EXEC SQL DECLARE csr_gubr CURSOR FOR
1801     SELECT login FROM users WHERE reservations LIKE '%' || :name || '%';
1802   EXEC SQL OPEN csr_gubr;
1803   if (dbms_errno)
1804     return mr_errcode;
1805
1806   targv[0] = login;
1807   while (1)
1808     {
1809       EXEC SQL FETCH csr_gubr INTO :login;
1810       if (sqlca.sqlcode)
1811         break;
1812       (*action)(1, targv, actarg);
1813     }
1814   EXEC SQL CLOSE csr_gubr;
1815
1816   return MR_SUCCESS;
1817 }
This page took 0.196064 seconds and 5 git commands to generate.