]> andersk Git - moira.git/blob - server/qsupport.pc
add get_host_by_owner (ghbo) query
[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         {
92           status = set_pobox_pop(q, argv, cl);
93           if (status)
94             return status;
95         }
96       strlcpy(potype, argv[1], sizeof(potype));
97       EXEC SQL UPDATE users SET potype = :potype, box_id = :id
98         WHERE users_id = :user;
99     }
100   else if (!strcmp(argv[1], "IMAP"))
101     {
102       EXEC SQL SELECT filsys_id INTO :id FROM filesys
103         WHERE label = :box AND type = 'IMAP';
104       if (sqlca.sqlcode)
105         return MR_FILESYS;
106       EXEC SQL UPDATE users SET potype = 'IMAP', imap_id = :id, pop_id = 0
107         WHERE users_id = :user;
108     }
109   else /* argv[1] == "NONE" */
110     {
111       EXEC SQL UPDATE users SET potype = 'NONE'
112         WHERE users_id = :user;
113     }
114
115   set_pobox_modtime(q, argv, cl);
116   EXEC SQL UPDATE tblstats SET updates = updates + 1, modtime = SYSDATE
117     WHERE table_name = 'users';
118   if (dbms_errno)
119     return mr_errcode;
120   return MR_SUCCESS;
121 }
122
123 /* set_pobox_pop: Revert to existing POP or IMAP pobox.
124  * Also take care of keeping track of the post office usage.
125  */
126 int set_pobox_pop(struct query *q, char **argv, client *cl)
127 {
128   EXEC SQL BEGIN DECLARE SECTION;
129   int id, pid, iid, mid;
130   char type[USERS_POTYPE_SIZE];
131   EXEC SQL END DECLARE SECTION;
132
133   id = *(int *)argv[0];
134   EXEC SQL SELECT potype, pop_id, imap_id INTO :type, :pid, :iid
135     FROM users WHERE users_id = :id;
136   if (sqlca.sqlerrd[2] == 0 || (pid == 0 && iid == 0))
137     return MR_MACHINE;
138
139   if (pid)
140     {
141       EXEC SQL SELECT mach_id INTO :mid FROM machine
142         WHERE mach_id = :pid;
143       if (sqlca.sqlerrd[2] == 0)
144         return MR_MACHINE;
145       EXEC SQL UPDATE users SET potype = 'POP' WHERE users_id = :id;
146       if (!strcmp(strtrim(type), "POP"))
147         set_pop_usage(mid, 1);
148     }
149   else
150     {
151       EXEC SQL SELECT filsys_id INTO :mid FROM filesys
152         WHERE filsys_id = :iid;
153       if (sqlca.sqlerrd[2] == 0)
154         return MR_MACHINE;
155       EXEC SQL UPDATE users SET potype = 'IMAP' WHERE users_id = :id;
156     }
157
158   set_pobox_modtime(q, argv, cl);
159   EXEC SQL UPDATE tblstats SET updates = updates + 1, modtime = SYSDATE
160     WHERE table_name = 'users';
161   if (dbms_errno)
162     return mr_errcode;
163   return MR_SUCCESS;
164 }
165
166
167 /* Add_member_to_list: do list flattening as we go!  MAXLISTDEPTH is
168  * how many different ancestors a member is allowed to have.
169  */
170
171 #define MAXLISTDEPTH    1024
172
173 int add_member_to_list(struct query *q, char **argv, client *cl)
174 {
175   EXEC SQL BEGIN DECLARE SECTION;
176   int id, lid, mid, tag, error, who, ref, rowcnt;
177   char *mtype, dtype[IMEMBERS_MEMBER_TYPE_SIZE], *entity;
178   EXEC SQL END DECLARE SECTION;
179   int ancestors[MAXLISTDEPTH], aref[MAXLISTDEPTH], acount, a;
180   int descendants[MAXLISTDEPTH], dref[MAXLISTDEPTH], dcount, d;
181   int status;
182   char *dtypes[MAXLISTDEPTH];
183   char *iargv[3], *buf;
184
185   lid = *(int *)argv[0];
186   mtype = argv[1];
187   mid = *(int *)argv[2];
188   tag = !strcmp(q->shortname, "atml") ? *(int *)argv[3] : 0;
189
190   if (acl_access_check(lid, cl))
191     return MR_PERM;
192
193   /* if the member is already a direct member of the list, punt */
194   EXEC SQL SELECT COUNT(list_id) INTO :rowcnt FROM imembers
195     WHERE list_id = :lid AND member_id = :mid
196     AND member_type = :mtype AND direct = 1;
197   if (rowcnt > 0)
198     return MR_EXISTS;
199   if (!strcasecmp(mtype, "STRING"))
200     {
201       buf = malloc(0);
202       status = id_to_name(mid, STRINGS_TABLE, &buf);
203       if (status)
204         return status;
205       if (strchr(buf, '/') || strchr(buf, '|'))
206         {
207           free(buf);
208           return MR_BAD_CHAR;
209         }
210       free(buf);
211     }
212
213   ancestors[0] = lid;
214   aref[0] = 1;
215   acount = 1;
216   EXEC SQL DECLARE csr103 CURSOR FOR
217     SELECT list_id, ref_count   FROM imembers
218     WHERE member_id = :lid AND member_type = 'LIST';
219   if (dbms_errno)
220     return mr_errcode;
221   EXEC SQL OPEN csr103;
222   if (dbms_errno)
223     return mr_errcode;
224   while (1)
225     {
226       EXEC SQL FETCH csr103 INTO :id, :ref;
227       if (sqlca.sqlcode)
228         break;
229       aref[acount] = ref;
230       ancestors[acount++] = id;
231       if (acount >= MAXLISTDEPTH)
232         break;
233     }
234   EXEC SQL CLOSE csr103;
235   if (dbms_errno)
236     return mr_errcode;
237   if (acount >= MAXLISTDEPTH)
238     return MR_INTERNAL;
239   descendants[0] = mid;
240   dtypes[0] = mtype;
241   dref[0] = 1;
242   dcount = 1;
243   error = 0;
244   if (!strcmp(mtype, "LIST"))
245     {
246       EXEC SQL DECLARE csr104 CURSOR FOR
247         SELECT member_id, member_type, ref_count
248         FROM imembers
249         WHERE list_id = :mid;
250       if (dbms_errno)
251         return mr_errcode;
252       EXEC SQL OPEN csr104;
253       if (dbms_errno)
254         return mr_errcode;
255       while (1)
256         {
257           EXEC SQL FETCH csr104 INTO :id, :dtype, :ref;
258           if (sqlca.sqlcode)
259             break;
260           switch (dtype[0])
261             {
262             case 'L':
263               dtypes[dcount] = "LIST";
264               break;
265             case 'U':
266               dtypes[dcount] = "USER";
267               break;
268             case 'S':
269               dtypes[dcount] = "STRING";
270               break;
271             case 'K':
272               dtypes[dcount] = "KERBEROS";
273               break;
274             default:
275               error++;
276               break;
277             }
278           dref[dcount] = ref;
279           descendants[dcount++] = id;
280           if (dcount >= MAXLISTDEPTH)
281             {
282               error++;
283               break;
284             }
285         }
286       EXEC SQL CLOSE csr104;
287       if (dbms_errno)
288         return mr_errcode;
289       if (error)
290         return MR_INTERNAL;
291     }
292   for (a = 0; a < acount; a++)
293     {
294       lid = ancestors[a];
295       for (d = 0; d < dcount; d++)
296         {
297           mid = descendants[d];
298           mtype = dtypes[d];
299           if (mid == lid && !strcmp(mtype, "LIST"))
300             return MR_LISTLOOP;
301           EXEC SQL SELECT COUNT(ref_count) INTO :rowcnt
302             FROM imembers
303             WHERE list_id = :lid AND member_id = :mid
304             AND member_type = :mtype;
305           ref = aref[a] * dref[d];
306           if (rowcnt > 0)
307             {
308               if (a == 0 && d == 0)
309                 {
310                   EXEC SQL UPDATE imembers
311                     SET ref_count = ref_count + :ref, direct = 1, tag = :tag
312                     WHERE list_id = :lid AND member_id = :mid
313                     AND member_type = :mtype;
314                 }
315               else
316                 {
317                   EXEC SQL UPDATE imembers
318                     SET ref_count = ref_count + :ref
319                     WHERE list_id = :lid AND member_id = :mid
320                     AND member_type = :mtype;
321                 }
322             }
323           else
324             {
325               incremental_clear_before();
326               if (a == 0 && d == 0)
327                 {
328                   EXEC SQL INSERT INTO imembers
329                     (list_id, member_type, member_id, tag, direct, ref_count)
330                     VALUES (:lid, :mtype, :mid, :tag, 1, 1);
331                 }
332               else
333                 {
334                   EXEC SQL INSERT INTO imembers
335                     (list_id, member_type, member_id, tag, direct, ref_count)
336                     VALUES (:lid, :mtype, :mid, :tag, 0, 1);
337                 }
338               iargv[0] = (char *)lid;
339               iargv[1] = mtype;
340               iargv[2] = (char *)mid;
341               incremental_after(IMEMBERS_TABLE, 0, iargv);
342             }
343         }
344     }
345   lid = *(int *)argv[0];
346   entity = cl->entity;
347   who = cl->client_id;
348   EXEC SQL UPDATE list
349     SET modtime = SYSDATE, modby = :who, modwith = :entity
350     WHERE list_id = :lid;
351   if (dbms_errno)
352     return mr_errcode;
353   return MR_SUCCESS;
354 }
355
356
357 /* Delete_member_from_list: do list flattening as we go!
358  */
359
360 int delete_member_from_list(struct query *q, char **argv, client *cl)
361 {
362   EXEC SQL BEGIN DECLARE SECTION;
363   int id, lid, mid, cnt, error, who, ref;
364   char *mtype, dtype[IMEMBERS_MEMBER_TYPE_SIZE], *entity;
365   EXEC SQL END DECLARE SECTION;
366   int ancestors[MAXLISTDEPTH], aref[MAXLISTDEPTH], acount, a;
367   int descendants[MAXLISTDEPTH], dref[MAXLISTDEPTH], dcount, d;
368   char *dtypes[MAXLISTDEPTH];
369   char *iargv[3];
370
371   lid = *(int *)argv[0];
372   mtype = argv[1];
373   mid = *(int *)argv[2];
374
375   if (acl_access_check(lid, cl))
376     return MR_PERM;
377
378   /* if the member is not a direct member of the list, punt */
379   EXEC SQL SELECT COUNT(list_id) INTO :cnt FROM imembers
380     WHERE list_id = :lid AND member_id = :mid
381     AND member_type = :mtype AND direct = 1;
382   if (dbms_errno)
383     return mr_errcode;
384   if (cnt == 0)
385     return MR_NO_MATCH;
386   ancestors[0] = lid;
387   aref[0] = 1;
388   acount = 1;
389   EXEC SQL DECLARE csr105 CURSOR FOR
390     SELECT list_id, ref_count FROM imembers
391     WHERE member_id = :lid AND member_type = 'LIST';
392   if (dbms_errno)
393     return mr_errcode;
394   EXEC SQL OPEN csr105;
395   if (dbms_errno)
396     return mr_errcode;
397   while (1)
398     {
399       EXEC SQL FETCH csr105 INTO :id, :ref;
400       if (sqlca.sqlcode)
401         break;
402       aref[acount] = ref;
403       ancestors[acount++] = id;
404       if (acount >= MAXLISTDEPTH)
405         break;
406     }
407   EXEC SQL CLOSE csr105;
408   if (dbms_errno)
409     return mr_errcode;
410   if (acount >= MAXLISTDEPTH)
411     return MR_INTERNAL;
412   descendants[0] = mid;
413   dtypes[0] = mtype;
414   dref[0] = 1;
415   dcount = 1;
416   error = 0;
417   if (!strcmp(mtype, "LIST"))
418     {
419       EXEC SQL DECLARE csr106 CURSOR FOR
420         SELECT member_id, member_type, ref_count FROM imembers
421         WHERE list_id = :mid;
422       if (dbms_errno)
423         return mr_errcode;
424       EXEC SQL OPEN csr106;
425       if (dbms_errno)
426         return mr_errcode;
427       while (1)
428         {
429           EXEC SQL FETCH csr106 INTO :id, :dtype, :ref;
430           if (sqlca.sqlcode)
431             break;
432           switch (dtype[0])
433             {
434             case 'L':
435               dtypes[dcount] = "LIST";
436               break;
437             case 'U':
438               dtypes[dcount] = "USER";
439               break;
440             case 'S':
441               dtypes[dcount] = "STRING";
442               break;
443             case 'K':
444               dtypes[dcount] = "KERBEROS";
445               break;
446             default:
447               error++;
448               break;
449             }
450           dref[dcount] = ref;
451           descendants[dcount++] = id;
452           if (dcount >= MAXLISTDEPTH)
453             break;
454         }
455       EXEC SQL CLOSE csr106;
456       if (dbms_errno)
457         return mr_errcode;
458       if (error)
459         return MR_INTERNAL;
460     }
461   for (a = 0; a < acount; a++)
462     {
463       lid = ancestors[a];
464       for (d = 0; d < dcount; d++)
465         {
466           mid = descendants[d];
467           mtype = dtypes[d];
468           if (mid == lid && !strcmp(mtype, "LIST"))
469             return MR_LISTLOOP;
470           EXEC SQL SELECT ref_count INTO :cnt FROM imembers
471             WHERE list_id = :lid AND member_id = :mid AND member_type = :mtype;
472           ref = aref[a] * dref[d];
473           if (cnt <= ref)
474             {
475               iargv[0] = (char *)lid;
476               iargv[1] = mtype;
477               iargv[2] = (char *)mid;
478               incremental_before(IMEMBERS_TABLE, 0, iargv);
479               EXEC SQL DELETE FROM imembers
480                 WHERE list_id = :lid AND member_id = :mid
481                 AND member_type= :mtype;
482               incremental_clear_after();
483             }
484           else if (a == 0 && d == 0)
485             {
486               EXEC SQL UPDATE imembers
487                 SET ref_count = ref_count - :ref, direct = 0
488                 WHERE list_id = :lid AND member_id = :mid
489                 AND member_type = :mtype;
490             }
491           else
492             {
493               EXEC SQL UPDATE imembers
494                 SET ref_count = ref_count - :ref
495                 WHERE list_id = :lid AND member_id = :mid
496                 AND member_type = :mtype;
497             }
498         }
499     }
500   lid = *(int *)argv[0];
501   entity = cl->entity;
502   who = cl->client_id;
503   EXEC SQL UPDATE list SET modtime = SYSDATE, modby = :who, modwith = :entity
504     WHERE list_id = :lid;
505   if (dbms_errno)
506     return mr_errcode;
507   return MR_SUCCESS;
508 }
509
510
511 /* Don't allow someone to add someone to a list which is the acl of a
512  * query unless they're on the list acl, even if they're on the amtl
513  * query acl! Also, don't allow someone proxying to add someone to a
514  * capacl.
515  */
516 int acl_access_check(int list_id, client *cl)
517 {
518   EXEC SQL BEGIN DECLARE SECTION;
519   int c1, c2, lid = list_id, acl_id;
520   char acl_type[LIST_ACL_TYPE_SIZE];
521   EXEC SQL END DECLARE SECTION;
522
523   /* Check if the list is directly a capacl */
524   EXEC SQL SELECT COUNT(list_id) INTO :c1 FROM capacls WHERE list_id=:lid;
525
526   /* Check if the list is a member (direct or indirect) of a list that
527      is a capacl */
528   EXEC SQL SELECT COUNT(l1.list_id) INTO :c2 FROM list l1, list l2,
529     imembers im, capacls c WHERE c.list_id = l2.list_id AND
530     im.list_id = l2.list_id AND im.member_type = 'LIST' AND
531     im.member_id = l1.list_id AND l1.list_id = :lid;
532
533   if (c1 == 0 && c2 == 0)
534     return 0;
535
536   if (cl->proxy_id)
537     return 1;
538
539   EXEC SQL SELECT acl_type, acl_id INTO :acl_type, :acl_id
540     FROM list WHERE list_id=:lid;
541   return !find_member(acl_type, acl_id, cl);
542 }
543
544
545 /* get_ace_use - given a type and a name, return a type and a name.
546  * The ace_type is one of "LIST", "USER", "RLIST", or "RUSER" in argv[0],
547  * and argv[1] will contain the ID of the entity in question.  The R*
548  * types mean to recursively look at every containing list, not just
549  * when the object in question is a direct member.  On return, the
550  * usage type will be one of LIST, SERVICE, FILESYS, QUOTA, QUERY, or ZEPHYR.
551  */
552
553 int get_ace_use(struct query *q, char *argv[], client *cl,
554                 int (*action)(int, char *[], void *), void *actarg)
555 {
556   int found = 0;
557   EXEC SQL BEGIN DECLARE SECTION;
558   char *atype;
559   int aid, listid, id;
560   EXEC SQL END DECLARE SECTION;
561   struct save_queue *sq;
562
563   atype = argv[0];
564   aid = *(int *)argv[1];
565   if (!strcmp(atype, "LIST") || !strcmp(atype, "USER") ||
566       !strcmp(atype, "KERBEROS"))
567     return get_ace_internal(atype, aid, action, actarg);
568
569   sq = sq_create();
570   if (!strcmp(atype, "RLIST"))
571     {
572       sq_save_data(sq, (void *)aid);
573       /* get all the list_id's of containing lists */
574       EXEC SQL DECLARE csr107 CURSOR FOR
575         SELECT list_id FROM imembers
576         WHERE member_type = 'LIST' AND member_id = :aid;
577       if (dbms_errno)
578         return mr_errcode;
579       EXEC SQL OPEN csr107;
580       if (dbms_errno)
581         return mr_errcode;
582       while (1)
583         {
584           EXEC SQL FETCH csr107 INTO :listid;
585           if (sqlca.sqlcode)
586             break;
587           sq_save_unique_data(sq, (void *)listid);
588         }
589       EXEC SQL CLOSE csr107;
590       /* now process each one */
591       while (sq_get_data(sq, &id))
592         {
593           if (get_ace_internal("LIST", id, action, actarg) == MR_SUCCESS)
594             found++;
595         }
596     }
597
598   if (!strcmp(atype, "RUSER"))
599     {
600       EXEC SQL DECLARE csr108 CURSOR FOR
601         SELECT list_id FROM imembers
602         WHERE member_type = 'USER' AND member_id = :aid;
603       if (dbms_errno)
604         return mr_errcode;
605       EXEC SQL OPEN csr108;
606       if (dbms_errno)
607         return mr_errcode;
608       while (1)
609         {
610           EXEC SQL FETCH csr108 INTO :listid;
611           if (sqlca.sqlcode)
612             break;
613           sq_save_data(sq, (void *)listid);
614         }
615       EXEC SQL CLOSE csr108;
616       /* now process each one */
617       while (sq_get_data(sq, &id))
618         {
619           if (get_ace_internal("LIST", id, action, actarg) == MR_SUCCESS)
620             found++;
621         }
622       if (get_ace_internal("USER", aid, action, actarg) == MR_SUCCESS)
623         found++;
624     }
625
626   if (!strcmp(atype, "RKERBEROS"))
627     {
628       EXEC SQL DECLARE csr109 CURSOR FOR
629         SELECT list_id FROM imembers
630         WHERE member_type = 'KERBEROS' AND member_id = :aid;
631       if (dbms_errno)
632         return mr_errcode;
633       EXEC SQL OPEN csr109;
634       if (dbms_errno)
635         return mr_errcode;
636       while (1)
637         {
638           EXEC SQL FETCH csr109 INTO :listid;
639           if (sqlca.sqlcode)
640             break;
641           sq_save_data(sq, (void *)listid);
642         }
643       EXEC SQL CLOSE csr109;
644       /* now process each one */
645       while (sq_get_data(sq, &id))
646         {
647           if (get_ace_internal("LIST", id, action, actarg) == MR_SUCCESS)
648             found++;
649         }
650       if (get_ace_internal("KERBEROS", aid, action, actarg) == MR_SUCCESS)
651         found++;
652     }
653
654   sq_destroy(sq);
655   if (dbms_errno)
656     return mr_errcode;
657   if (!found)
658     return MR_NO_MATCH;
659   return MR_SUCCESS;
660 }
661
662
663 /* This looks up a single list or user for ace use.  atype must be "USER"
664  * or "LIST", and aid is the ID of the corresponding object.  This is used
665  * by get_ace_use above.
666  */
667
668 int get_ace_internal(char *atype, int aid,
669                      int (*action)(int, char *[], void *), void *actarg)
670 {
671   char *rargv[2];
672   int found = 0;
673   EXEC SQL BEGIN DECLARE SECTION;
674   char name[MAX_FIELD_WIDTH], *type = atype;
675   int id = aid;
676   EXEC SQL END DECLARE SECTION;
677
678   rargv[1] = name;
679   if (!strcmp(atype, "LIST"))
680     {
681       rargv[0] = "FILESYS";
682       EXEC SQL DECLARE csr110 CURSOR FOR
683         SELECT label FROM filesys
684         WHERE owners = :id;
685       if (dbms_errno)
686         return mr_errcode;
687       EXEC SQL OPEN csr110;
688       if (dbms_errno)
689         return mr_errcode;
690       while (1)
691         {
692           EXEC SQL FETCH csr110 INTO :name;
693           if (sqlca.sqlcode)
694             break;
695           (*action)(2, rargv, actarg);
696           found++;
697         }
698       EXEC SQL CLOSE csr110;
699
700       rargv[0] = "QUERY";
701       EXEC SQL DECLARE csr111 CURSOR FOR
702         SELECT capability FROM capacls
703         WHERE list_id = :id;
704       if (dbms_errno)
705         return mr_errcode;
706       EXEC SQL OPEN csr111;
707       if (dbms_errno)
708         return mr_errcode;
709       while (1)
710         {
711           EXEC SQL FETCH csr111 INTO :name;
712           if (sqlca.sqlcode)
713             break;
714           (*action)(2, rargv, actarg);
715           found++;
716         }
717       EXEC SQL CLOSE csr111;
718     }
719   else if (!strcmp(atype, "USER"))
720     {
721       rargv[0] = "FILESYS";
722       EXEC SQL DECLARE csr112 CURSOR FOR
723         SELECT label FROM filesys
724         WHERE owner = :id;
725       if (dbms_errno)
726         return mr_errcode;
727       EXEC SQL OPEN csr112;
728       if (dbms_errno)
729         return mr_errcode;
730       while (1)
731         {
732           EXEC SQL FETCH csr112 INTO :name;
733           if (sqlca.sqlcode)
734             break;
735           (*action)(2, rargv, actarg);
736           found++;
737         }
738       EXEC SQL CLOSE csr112;
739     }
740
741   rargv[0] = "LIST";
742   EXEC SQL DECLARE csr113 CURSOR FOR
743     SELECT name FROM list
744     WHERE acl_type = :type AND acl_id = :id;
745   if (dbms_errno)
746     return mr_errcode;
747   EXEC SQL OPEN csr113;
748   if (dbms_errno)
749     return mr_errcode;
750   while (1)
751     {
752       EXEC SQL FETCH csr113 INTO :name;
753       if (sqlca.sqlcode)
754         break;
755       (*action)(2, rargv, actarg);
756       found++;
757     }
758   EXEC SQL CLOSE csr113;
759
760   rargv[0] = "SERVICE";
761   EXEC SQL DECLARE csr114 CURSOR FOR
762     SELECT name FROM servers
763     WHERE acl_type = :type AND acl_id = :id;
764   if (dbms_errno)
765     return mr_errcode;
766   EXEC SQL OPEN csr114;
767   if (dbms_errno)
768     return mr_errcode;
769   while (1)
770     {
771       EXEC SQL FETCH csr114 INTO :name;
772       if (sqlca.sqlcode)
773         break;
774       (*action)(2, rargv, actarg);
775       found++;
776     }
777   EXEC SQL CLOSE csr114;
778
779   rargv[0] = "HOSTACCESS";
780   EXEC SQL DECLARE csr115 CURSOR FOR
781     SELECT name FROM machine m, hostaccess ha
782     WHERE m.mach_id = ha.mach_id AND ha.acl_type = :type
783     AND ha.acl_id = :id;
784   if (dbms_errno)
785     return mr_errcode;
786   EXEC SQL OPEN csr115;
787   if (dbms_errno)
788     return mr_errcode;
789   while (1)
790     {
791       EXEC SQL FETCH csr115 INTO :name;
792       if (sqlca.sqlcode)
793         break;
794       (*action)(2, rargv, actarg);
795       found++;
796     }
797   EXEC SQL CLOSE csr115;
798
799   rargv[0] = "MACHINE";
800   EXEC SQL DECLARE csr115a CURSOR FOR
801     SELECT name FROM machine m
802     WHERE m.owner_type = :type
803     AND m.owner_id = :id;
804   if (dbms_errno)
805     return mr_errcode;
806   EXEC SQL OPEN csr115a;
807   if (dbms_errno)
808     return mr_errcode;
809   while (1)
810     {
811       EXEC SQL FETCH csr115a INTO :name;
812       if (sqlca.sqlcode)
813         break;
814       (*action)(2, rargv, actarg);
815       found++;
816     }
817   EXEC SQL CLOSE csr115a;
818
819   rargv[0] = "ZEPHYR";
820   EXEC SQL DECLARE csr116 CURSOR FOR
821     SELECT class FROM zephyr z
822     WHERE z.xmt_type = :type AND z.xmt_id = :id
823     OR z.sub_type = :type AND z.sub_id = :id
824     OR z.iws_type = :type AND z.iws_id = :id
825     OR z.iui_type = :type AND z.iui_id = :id;
826   if (dbms_errno)
827     return mr_errcode;
828   EXEC SQL OPEN csr116;
829   if (dbms_errno)
830     return mr_errcode;
831   while (1)
832     {
833       EXEC SQL FETCH csr116 INTO :name;
834       if (sqlca.sqlcode)
835         break;
836       (*action)(2, rargv, actarg);
837       found++;
838     }
839   EXEC SQL CLOSE csr116;
840   
841   if (!found)
842     return MR_NO_MATCH;
843   return MR_SUCCESS;
844 }
845
846 /* ghbo_internal */
847 int ghbo_internal(char *atype, int aid,
848                   int (*action)(int, char *[], void *), void *actarg)
849 {
850   char *rargv[1];
851   int found = 0;
852   EXEC SQL BEGIN DECLARE SECTION;
853   char name[MACHINE_NAME_SIZE], *type = atype;
854   int id = aid;
855   EXEC SQL END DECLARE SECTION;
856
857   rargv[0] = name;
858   EXEC SQL DECLARE csr115b CURSOR FOR
859     SELECT name FROM machine m
860     WHERE m.owner_type = :type
861     AND m.owner_id = :id;
862   if (dbms_errno)
863     return mr_errcode;
864   EXEC SQL OPEN csr115b;
865   if (dbms_errno)
866     return mr_errcode;
867   while (1)
868     {
869       EXEC SQL FETCH csr115b INTO :name;
870       if (sqlca.sqlcode)
871         break;
872       (*action)(1, rargv, actarg);
873       found++;
874     }
875   EXEC SQL CLOSE csr115b;
876   
877   if (!found)
878     return MR_NO_MATCH;
879   return MR_SUCCESS;
880 }
881
882 /* get_host_by_owner - like gaus but limited to hosts */
883 int get_host_by_owner(struct query *q, char *argv[], client *cl,
884                       int (*action)(int, char *[], void *), void *actarg)
885 {
886   int found = 0;
887   EXEC SQL BEGIN DECLARE SECTION;
888   char *atype;
889   int aid, listid, id;
890   EXEC SQL END DECLARE SECTION;
891   struct save_queue *sq;
892
893   atype = argv[0];
894   aid = *(int *)argv[1];
895   if (!strcmp(atype, "LIST") || !strcmp(atype, "USER") ||
896       !strcmp(atype, "KERBEROS"))
897     return ghbo_internal(atype, aid, action, actarg);
898
899   sq = sq_create();
900   if (!strcmp(atype, "RLIST"))
901     {
902       sq_save_data(sq, (void *)aid);
903       /* get all the list_id's of containing lists */
904       EXEC SQL DECLARE csr107q CURSOR FOR
905         SELECT list_id FROM imembers
906         WHERE member_type = 'LIST' AND member_id = :aid;
907       if (dbms_errno)
908         return mr_errcode;
909       EXEC SQL OPEN csr107q;
910       if (dbms_errno)
911         return mr_errcode;
912       while (1)
913         {
914           EXEC SQL FETCH csr107q INTO :listid;
915           if (sqlca.sqlcode)
916             break;
917           sq_save_unique_data(sq, (void *)listid);
918         }
919       EXEC SQL CLOSE csr107q;
920       /* now process each one */
921       while (sq_get_data(sq, &id))
922         {
923           if (ghbo_internal("LIST", id, action, actarg) == MR_SUCCESS)
924             found++;
925         }
926     }
927
928   if (!strcmp(atype, "RUSER"))
929     {
930       EXEC SQL DECLARE csr108q CURSOR FOR
931         SELECT list_id FROM imembers
932         WHERE member_type = 'USER' AND member_id = :aid;
933       if (dbms_errno)
934         return mr_errcode;
935       EXEC SQL OPEN csr108q;
936       if (dbms_errno)
937         return mr_errcode;
938       while (1)
939         {
940           EXEC SQL FETCH csr108q INTO :listid;
941           if (sqlca.sqlcode)
942             break;
943           sq_save_data(sq, (void *)listid);
944         }
945       EXEC SQL CLOSE csr108q;
946       /* now process each one */
947       while (sq_get_data(sq, &id))
948         {
949           if (ghbo_internal("LIST", id, action, actarg) == MR_SUCCESS)
950             found++;
951         }
952       if (ghbo_internal("USER", aid, action, actarg) == MR_SUCCESS)
953         found++;
954     }
955
956   if (!strcmp(atype, "RKERBEROS"))
957     {
958       EXEC SQL DECLARE csr109q CURSOR FOR
959         SELECT list_id FROM imembers
960         WHERE member_type = 'KERBEROS' AND member_id = :aid;
961       if (dbms_errno)
962         return mr_errcode;
963       EXEC SQL OPEN csr109q;
964       if (dbms_errno)
965         return mr_errcode;
966       while (1)
967         {
968           EXEC SQL FETCH csr109q INTO :listid;
969           if (sqlca.sqlcode)
970             break;
971           sq_save_data(sq, (void *)listid);
972         }
973       EXEC SQL CLOSE csr109q;
974       /* now process each one */
975       while (sq_get_data(sq, &id))
976         {
977           if (ghbo_internal("LIST", id, action, actarg) == MR_SUCCESS)
978             found++;
979         }
980       if (ghbo_internal("KERBEROS", aid, action, actarg) == MR_SUCCESS)
981         found++;
982     }
983
984   sq_destroy(sq);
985   if (dbms_errno)
986     return mr_errcode;
987   if (!found)
988     return MR_NO_MATCH;
989   return MR_SUCCESS;
990 }
991
992 /* get_lists_of_member - given a type and a name, return the name and flags
993  * of all of the lists of the given member.  The member_type is one of
994  * "LIST", "USER", "STRING", "RLIST", "RUSER", or "RSTRING" in argv[0],
995  * and argv[1] will contain the ID of the entity in question.  The R*
996  * types mean to recursively look at every containing list, not just
997  * when the object in question is a direct member.
998  */
999
1000 int get_lists_of_member(struct query *q, char *argv[], client *cl,
1001                         int (*action)(int, char *[], void *), void *actarg)
1002 {
1003   int found = 0, direct = 1;
1004   char *rargv[6];
1005   EXEC SQL BEGIN DECLARE SECTION;
1006   char *atype;
1007   int aid;
1008   char name[LIST_NAME_SIZE];
1009   char active[5], public[5], hidden[5], maillist[5], grouplist[5];
1010   EXEC SQL END DECLARE SECTION;
1011
1012   atype = argv[0];
1013   aid = *(int *)argv[1];
1014   if (!strcmp(atype, "RLIST"))
1015     {
1016       atype = "LIST";
1017       direct = 0;
1018     }
1019   if (!strcmp(atype, "RUSER"))
1020     {
1021       atype = "USER";
1022       direct = 0;
1023     }
1024   if (!strcmp(atype, "RSTRING"))
1025     {
1026       atype = "STRING";
1027       direct = 0;
1028     }
1029   if (!strcmp(atype, "RKERBEROS"))
1030     {
1031       atype = "KERBEROS";
1032       direct = 0;
1033     }
1034
1035   rargv[0] = name;
1036   rargv[1] = active;
1037   rargv[2] = public;
1038   rargv[3] = hidden;
1039   rargv[4] = maillist;
1040   rargv[5] = grouplist;
1041   if (direct)
1042     {
1043       EXEC SQL DECLARE csr117a CURSOR FOR
1044         SELECT l.name, l.active, l.publicflg, l.hidden, l.maillist, l.grouplist
1045         FROM list l, imembers im
1046         WHERE l.list_id = im.list_id AND im.direct = 1
1047         AND im.member_type = :atype AND im.member_id = :aid;
1048       if (dbms_errno)
1049         return mr_errcode;
1050       EXEC SQL OPEN csr117a;
1051       if (dbms_errno)
1052         return mr_errcode;
1053       while (1)
1054         {
1055           EXEC SQL FETCH csr117a
1056             INTO :name, :active, :public, :hidden, :maillist, :grouplist;
1057           if (sqlca.sqlcode)
1058             break;
1059           (*action)(6, rargv, actarg);
1060           found++;
1061         }
1062       EXEC SQL CLOSE csr117a;
1063     }
1064   else
1065     {
1066       EXEC SQL DECLARE csr117b CURSOR FOR
1067         SELECT l.name, l.active, l.publicflg, l.hidden, l.maillist, l.grouplist
1068         FROM list l, imembers im
1069         WHERE l.list_id = im.list_id
1070         AND im.member_type = :atype AND im.member_id = :aid;
1071       if (dbms_errno)
1072         return mr_errcode;
1073       EXEC SQL OPEN csr117b;
1074       if (dbms_errno)
1075         return mr_errcode;
1076       while (1)
1077         {
1078           EXEC SQL FETCH csr117b
1079             INTO :name, :active, :public, :hidden, :maillist, :grouplist;
1080           if (sqlca.sqlcode)
1081             break;
1082           (*action)(6, rargv, actarg);
1083           found++;
1084         }
1085       EXEC SQL CLOSE csr117b;
1086     }
1087
1088   if (dbms_errno)
1089     return mr_errcode;
1090   if (!found)
1091     return MR_NO_MATCH;
1092   return MR_SUCCESS;
1093 }
1094
1095
1096 /* qualified_get_lists: passed "TRUE", "FALSE", or "DONTCARE" for each of
1097  * the five flags associated with each list.  It will return the name of
1098  * each list that meets the quailifications.  It does this by building a
1099  * where clause based on the arguments, then doing a retrieve.
1100  */
1101
1102 static char *lflags[5] = { "active", "publicflg", "hidden", "maillist", "grouplist" };
1103
1104 int qualified_get_lists(struct query *q, char *argv[], client *cl,
1105                         int (*action)(int, char *[], void *), void *actarg)
1106 {
1107   return qualified_get(q, argv, action, actarg, "l.list_id != 0",
1108                        "l", "name", lflags);
1109 }
1110
1111
1112 int get_members_of_list(struct query *q, char *argv[], client *cl,
1113                         int (*action)(int, char *[], void *), void *actarg)
1114 {
1115   EXEC SQL BEGIN DECLARE SECTION;
1116   int list_id, direct;
1117   char member_name[MAX_FIELD_WIDTH], tag[STRINGS_STRING_SIZE];
1118   EXEC SQL END DECLARE SECTION;
1119   char *targv[3];
1120   int targc;
1121
1122   /* For gmol or gtml, only get direct members. For geml, get all. */
1123   if (!strcmp(q->shortname, "geml"))
1124     direct = -1;
1125   else
1126     direct = 0;
1127
1128   /* For gmol or geml, only return type and name. For gtml, return tag too. */
1129   if (!strcmp(q->shortname, "gtml"))
1130     targc = 3;
1131   else
1132     targc = 2;
1133
1134   list_id = *(int *)argv[0];
1135
1136   targv[1] = member_name;
1137   targv[2] = tag;
1138
1139   targv[0] = "USER";
1140   EXEC SQL DECLARE csr119 CURSOR FOR
1141     SELECT u.login, s.string FROM users u, imembers im, strings s
1142     WHERE im.list_id = :list_id AND im.member_type = 'USER'
1143     AND im.member_id = u.users_id AND im.direct > :direct
1144     AND s.string_id = im.tag ORDER BY 1;
1145   if (dbms_errno)
1146     return mr_errcode;
1147   EXEC SQL OPEN csr119;
1148   if (dbms_errno)
1149     return mr_errcode;
1150   while (1)
1151     {
1152       EXEC SQL FETCH csr119 INTO :member_name, :tag;
1153       if (sqlca.sqlcode)
1154         break;
1155       (*action)(targc, targv, actarg);
1156     }
1157   EXEC SQL CLOSE csr119;
1158   if (dbms_errno)
1159     return mr_errcode;
1160
1161   targv[0] = "LIST";
1162   EXEC SQL DECLARE csr120 CURSOR FOR
1163     SELECT l.name, s.string FROM list l, imembers im, strings s
1164     WHERE im.list_id = :list_id AND im.member_type = 'LIST'
1165     AND im.member_id = l.list_id AND im.direct > :direct
1166     AND s.string_id = im.tag ORDER BY 1;
1167   if (dbms_errno)
1168     return mr_errcode;
1169   EXEC SQL OPEN csr120;
1170   if (dbms_errno)
1171     return mr_errcode;
1172   while (1)
1173     {
1174       EXEC SQL FETCH csr120 INTO :member_name, :tag;
1175       if (sqlca.sqlcode)
1176         break;
1177       (*action)(targc, targv, actarg);
1178     }
1179   EXEC SQL CLOSE csr120;
1180   if (dbms_errno)
1181     return mr_errcode;
1182
1183   targv[0] = "STRING";
1184   EXEC SQL DECLARE csr121 CURSOR FOR
1185     SELECT str.string, s.string FROM strings str, imembers im, strings s
1186     WHERE im.list_id = :list_id AND im.member_type = 'STRING'
1187     AND im.member_id = str.string_id AND im.direct > :direct
1188     AND s.string_id = im.tag ORDER BY 1;
1189   if (dbms_errno)
1190     return mr_errcode;
1191   EXEC SQL OPEN csr121;
1192   if (dbms_errno)
1193     return mr_errcode;
1194   while (1)
1195     {
1196       EXEC SQL FETCH csr121 INTO :member_name, :tag;
1197       if (sqlca.sqlcode)
1198         break;
1199       (*action)(targc, targv, actarg);
1200     }
1201   EXEC SQL CLOSE csr121;
1202   if (dbms_errno)
1203     return mr_errcode;
1204
1205   targv[0] = "KERBEROS";
1206   EXEC SQL DECLARE csr122 CURSOR FOR
1207     SELECT str.string, s.string FROM strings str, imembers im, strings s
1208     WHERE im.list_id = :list_id AND im.member_type = 'KERBEROS'
1209     AND im.member_id = str.string_id AND im.direct > :direct
1210     AND s.string_id = im.tag ORDER BY 1;
1211   if (dbms_errno)
1212     return mr_errcode;
1213   EXEC SQL OPEN csr122;
1214   if (dbms_errno)
1215     return mr_errcode;
1216   while (1)
1217     {
1218       EXEC SQL FETCH csr122 INTO :member_name, :tag;
1219       if (sqlca.sqlcode)
1220         break;
1221       (*action)(targc, targv, actarg);
1222     }
1223   EXEC SQL CLOSE csr122;
1224   if (dbms_errno)
1225     return mr_errcode;
1226
1227   return MR_SUCCESS;
1228 }
1229
1230
1231 /* count_members_of_list: this is a simple query, but it cannot be done
1232  * through the dispatch table.
1233  */
1234
1235 int count_members_of_list(struct query *q, char *argv[], client *cl,
1236                           int (*action)(int, char *[], void *), void *actarg)
1237 {
1238   EXEC SQL BEGIN DECLARE SECTION;
1239   int  list, ct = 0;
1240   EXEC SQL END DECLARE SECTION;
1241   char *rargv[1], countbuf[5];
1242
1243   list = *(int *)argv[0];
1244   rargv[0] = countbuf;
1245   EXEC SQL SELECT count (*) INTO :ct FROM imembers
1246     WHERE list_id = :list AND direct = 1;
1247   if (dbms_errno)
1248     return mr_errcode;
1249   sprintf(countbuf, "%d", ct);
1250   (*action)(1, rargv, actarg);
1251   return MR_SUCCESS;
1252 }
1253
1254
1255 /* qualified_get_server: passed "TRUE", "FALSE", or "DONTCARE" for each of
1256  * the three flags associated with each service.  It will return the name of
1257  * each service that meets the quailifications.  It does this by building a
1258  * where clause based on the arguments, then doing a retrieve.
1259  */
1260
1261 static char *sflags[3] = { "enable", "inprogress", "harderror" };
1262
1263 int qualified_get_server(struct query *q, char *argv[], client *cl,
1264                          int (*action)(int, char *[], void *), void *actarg)
1265 {
1266   return qualified_get(q, argv, action, actarg, "s.name is not null",
1267                        "s", "name", sflags);
1268   /* of course, name will never be null, but we need something there
1269      to make qualified_get happy */
1270 }
1271
1272
1273 /* generic qualified get routine, used by qualified_get_lists,
1274  * qualified_get_server, and qualified_get_serverhost.
1275  *   Args:
1276  *      start - a simple where clause, must not be empty
1277  *      range - the name of the range variable
1278  *      field - the field to return
1279  *      flags - an array of strings, names of the flag variables
1280  */
1281
1282 int qualified_get(struct query *q, char *argv[],
1283                   int (*action)(int, char *[], void *), void *actarg,
1284                   char *start, char *range, char *field, char *flags[])
1285 {
1286   char qual[256];
1287   int i;
1288   char buf[32];
1289
1290   strcpy(qual, start);
1291   for (i = 0; i < q->argc; i++)
1292     {
1293       if (!strcmp(argv[i], "TRUE"))
1294         {
1295           sprintf(buf, " AND %s.%s != 0", range, flags[i]);
1296           strcat(qual, buf);
1297         }
1298       else if (!strcmp(argv[i], "FALSE"))
1299         {
1300           sprintf(buf, " AND %s.%s = 0", range, flags[i]);
1301           strcat(qual, buf);
1302         }
1303     }
1304
1305   sprintf(stmt_buf, "SELECT %s.%s FROM %s %s WHERE %s", range, field,
1306           table_name[q->rtable], range, qual);
1307   return do_for_all_rows(stmt_buf, 1, action, actarg);
1308 }
1309
1310
1311 /* qualified_get_serverhost: passed "TRUE", "FALSE", or "DONTCARE" for each of
1312  * the five flags associated with each serverhost.  It will return the name of
1313  * each service and host that meets the quailifications.  It does this by
1314  * building a where clause based on the arguments, then doing a retrieve.
1315  */
1316
1317 static char *shflags[6] = { "service", "enable", "override", "success",
1318                             "inprogress", "hosterror" };
1319
1320 int qualified_get_serverhost(struct query *q, char *argv[], client *cl,
1321                              int (*action)(int, char *[], void *),
1322                              void *actarg)
1323 {
1324   char qual[256], buf[32];
1325   int i;
1326
1327   sprintf(qual, "m.mach_id = sh.mach_id AND sh.service = UPPER('%s')",
1328           argv[0]);
1329   for (i = 1; i < q->argc; i++)
1330     {
1331       if (!strcmp(argv[i], "TRUE"))
1332         {
1333           sprintf(buf, " AND sh.%s != 0", shflags[i]);
1334           strcat(qual, buf);
1335         }
1336       else if (!strcmp(argv[i], "FALSE"))
1337         {
1338           sprintf(buf, " AND sh.%s = 0", shflags[i]);
1339           strcat(qual, buf);
1340         }
1341     }
1342
1343   sprintf(stmt_buf, "SELECT sh.service, m.name FROM serverhosts sh, "
1344           "machine m WHERE %s", qual);
1345   return do_for_all_rows(stmt_buf, 2, action, actarg);
1346 }
1347
1348
1349 /* register_user - change user's login name and allocate a pobox, group,
1350  * filesystem, and quota for them.  The user's status must start out as 0,
1351  * and is left as 2.  Arguments are: user's UID, new login name, and
1352  * pobox type ("POP" = POP, "IMAP" or numeric = IMAP)
1353  */
1354
1355 int register_user(struct query *q, char **argv, client *cl)
1356 {
1357   EXEC SQL BEGIN DECLARE SECTION;
1358   char *login, *entity;
1359   char directory[FILESYS_NAME_SIZE], machname[MACHINE_NAME_SIZE];
1360   char dir[NFSPHYS_DIR_SIZE], *potype;
1361   int who, rowcount, mid, uid, users_id;
1362   int ostatus, nstatus, fsidval, popid;
1363   int npid, tmp;
1364   int po_exists = 0;
1365   static int m_id, def_quota, def_imap_quota, list_id;
1366   EXEC SQL END DECLARE SECTION;
1367   char buffer[256], *aargv[3];
1368
1369   if (!m_id)
1370     {
1371       EXEC SQL SELECT list_id INTO :list_id FROM list
1372         WHERE name = 'wheel';
1373
1374       EXEC SQL SELECT mach_id INTO :m_id FROM machine
1375         WHERE name = 'ATHENA.MIT.EDU';
1376
1377       EXEC SQL SELECT value INTO :def_quota FROM numvalues
1378         WHERE name = 'def_quota';
1379       if (sqlca.sqlerrd[2] != 1)
1380         return MR_NO_QUOTA;
1381
1382       EXEC SQL SELECT value INTO :def_imap_quota FROM numvalues
1383         WHERE name = 'def_imap_quota';
1384       if (sqlca.sqlerrd[2] != 1)
1385         return MR_NO_QUOTA;
1386     }
1387
1388   entity = cl->entity;
1389   who = cl->client_id;
1390
1391   uid = atoi(argv[0]);
1392   login = argv[1];
1393   potype = argv[2];
1394
1395   /* find user */
1396   EXEC SQL SELECT users_id, status INTO :users_id, :ostatus
1397     FROM users
1398     WHERE unix_uid = :uid AND (status = 0 OR status = 5 OR status = 6);
1399
1400   if (sqlca.sqlerrd[2] == 0)
1401     return MR_NO_MATCH;
1402   if (sqlca.sqlerrd[2] > 1)
1403     return MR_NOT_UNIQUE;
1404
1405   /* check new login name */
1406   EXEC SQL SELECT COUNT(login) INTO :rowcount FROM users
1407     WHERE login = :login AND users_id != :users_id;
1408   if (dbms_errno)
1409     return mr_errcode;
1410   if (rowcount > 0)
1411     return MR_IN_USE;
1412   EXEC SQL SELECT COUNT(name) INTO :rowcount FROM list
1413     WHERE LOWER(name) = :login;
1414   if (dbms_errno)
1415     return mr_errcode;
1416   if (rowcount > 0)
1417     return MR_IN_USE;
1418   EXEC SQL SELECT COUNT(label) INTO :rowcount FROM filesys
1419     WHERE label = :login;
1420   if (dbms_errno)
1421     return mr_errcode;
1422   if (rowcount > 0)
1423     return MR_IN_USE;
1424   EXEC SQL SELECT COUNT(label) INTO :rowcount  
1425     FROM filesys WHERE label = :login || '.po';
1426   if (dbms_errno)
1427     return mr_errcode;
1428   if (rowcount > 0)
1429     {
1430       EXEC SQL SELECT owner INTO :tmp FROM filesys 
1431         WHERE label = :login || '.po';
1432       if (dbms_errno)
1433         return mr_errcode;
1434       if ((ostatus == 0) || (tmp != users_id))
1435         return MR_IN_USE;
1436       EXEC SQL SELECT count(potype) INTO :rowcount FROM users WHERE
1437         login = :login AND potype = 'IMAP';
1438       if (dbms_errno)
1439         return mr_errcode;
1440       if (rowcount > 0)
1441         po_exists = 1;
1442     }
1443   EXEC SQL SELECT COUNT(name) INTO :rowcount FROM alias
1444     WHERE ( name = :login OR name = :login || '.po' )
1445     AND type = 'FILESYS';
1446   if (dbms_errno)
1447     return mr_errcode;
1448   if (rowcount > 0)
1449     return MR_IN_USE;
1450   com_err(whoami, 0, "login name OK");
1451
1452   EXEC SQL SELECT COUNT(potype) INTO :rowcount FROM users WHERE
1453     login = :login AND potype = 'POP';
1454   if (dbms_errno)
1455     return mr_errcode;
1456   if (rowcount > 0)
1457     po_exists = 1;
1458
1459   /* choose type and location for pobox */
1460   if (!po_exists)
1461     {
1462       if (!strcmp(potype, "POP"))
1463         {
1464
1465           EXEC SQL DECLARE csr130 CURSOR FOR
1466             SELECT sh.mach_id, m.name FROM serverhosts sh, machine m
1467             WHERE sh.service = 'POP' AND sh.mach_id = m.mach_id
1468             AND sh.value2 - sh.value1 =
1469             (SELECT MAX(value2 - value1) FROM serverhosts WHERE service = 'POP');
1470           if (dbms_errno)
1471             return mr_errcode;
1472           EXEC SQL OPEN csr130;
1473           if (dbms_errno)
1474             return mr_errcode;
1475           EXEC SQL FETCH csr130 INTO :popid, :machname;
1476           if (sqlca.sqlerrd[2] == 0)
1477             {
1478               EXEC SQL CLOSE csr130;
1479               if (dbms_errno)
1480                 return mr_errcode;
1481               return MR_NO_POBOX;
1482             }
1483           else
1484             {
1485               EXEC SQL CLOSE csr130;
1486               if (dbms_errno)
1487                 return mr_errcode;
1488             }
1489       
1490           EXEC SQL UPDATE users SET potype = 'POP', pop_id = :popid
1491             WHERE users_id = :users_id;
1492           com_err(whoami, 0, "pobox set to POP:%s", strtrim(machname));
1493         }         
1494       else
1495         {
1496       /* Select all IMAP nfsphys entries in order of increasing
1497        * (allocated - partsize).  The partitions will almost always be 
1498        * overallocated, but we choose the one that is the least 
1499        * overallocated.
1500        */
1501           potype = "IMAP";
1502           
1503           EXEC SQL DECLARE csr_rusr_imap CURSOR FOR
1504             SELECT np.allocated - np.partsize, np.nfsphys_id, np.mach_id,
1505             np.dir, m.name FROM serverhosts sh, nfsphys np, machine m
1506             WHERE sh.service = 'POSTOFFICE' AND sh.mach_id = np.mach_id
1507             AND m.mach_id = np.mach_id
1508             ORDER BY 1;
1509           if (dbms_errno)
1510             return mr_errcode;
1511           EXEC SQL OPEN csr_rusr_imap;
1512           if (dbms_errno)
1513             return mr_errcode;
1514           EXEC SQL FETCH csr_rusr_imap INTO :tmp, :npid, :mid, :dir, :machname;
1515           if (sqlca.sqlerrd[2] == 0)
1516             {
1517               EXEC SQL CLOSE csr_rusr_imap;
1518               if (dbms_errno)
1519                 return mr_errcode;
1520               return MR_NO_POBOX;
1521             }
1522           else
1523             {
1524               EXEC SQL CLOSE csr_rusr_imap;
1525               if (dbms_errno)
1526                 return mr_errcode;
1527             }
1528
1529           /* create filesystem */
1530           if (set_next_object_id("filsys_id", FILESYS_TABLE, 0))
1531             return MR_NO_ID;
1532           incremental_clear_before();
1533
1534           EXEC SQL SELECT value INTO :popid FROM numvalues
1535             WHERE numvalues.name = 'filsys_id';
1536           EXEC SQL INSERT INTO filesys
1537             (filsys_id, phys_id, label, type, mach_id, name,
1538              mount, rwaccess, comments, owner, owners, createflg,
1539              lockertype, modtime, modby, modwith)
1540             VALUES
1541             (:popid, :npid, :login || '.po', 'IMAP', :mid, :dir,
1542              CHR(0), 'w', 'IMAP box', :users_id, :list_id, 1,
1543              'USER', SYSDATE, :who, :entity);
1544
1545           if (dbms_errno)
1546             return mr_errcode;
1547           if (sqlca.sqlerrd[2] != 1)
1548             return MR_INTERNAL;
1549           sprintf(buffer, "fs.filsys_id = %d", popid);
1550           incremental_after(FILESYS_TABLE, buffer, 0);
1551
1552           /* set quota */
1553           incremental_clear_before();
1554           EXEC SQL INSERT INTO quota
1555             (entity_id, filsys_id, type, quota, phys_id, modtime, modby, modwith)
1556             VALUES (:users_id, :popid, 'USER', :def_imap_quota, :npid,
1557                     SYSDATE, :who, :entity);
1558           if (dbms_errno)
1559             return mr_errcode;
1560           if (sqlca.sqlerrd[2] != 1)
1561             return MR_INTERNAL;
1562           aargv[0] = login;
1563           aargv[1] = "USER";
1564           aargv[2] = login;
1565           sprintf(buffer, "q.entity_id = %d and q.filsys_id = %d and "
1566                   "q.type = 'USER'", users_id, popid);
1567           incremental_after(QUOTA_TABLE, buffer, aargv);
1568           if (dbms_errno)
1569             return mr_errcode;
1570
1571           EXEC SQL UPDATE nfsphys SET allocated = allocated + :def_imap_quota
1572             WHERE nfsphys_id = :npid;
1573
1574           EXEC SQL UPDATE users SET potype = 'IMAP', imap_id = :popid
1575             WHERE users_id = :users_id;
1576           com_err(whoami, 0, "pobox set to IMAP:%s:%s", strtrim(machname),
1577                   strtrim(dir));
1578         }
1579     }
1580   
1581   /* change login name, set pobox */
1582   sprintf(buffer, "u.users_id = %d", users_id);
1583   incremental_before(USERS_TABLE, buffer, 0);
1584   nstatus = 2;
1585   if (ostatus == 5 || ostatus == 6)
1586     nstatus = 1;
1587   EXEC SQL UPDATE users SET login = :login, status = :nstatus,
1588     modtime = SYSDATE, modby = :who, modwith = :entity,
1589     pmodtime = SYSDATE, pmodby = :who, pmodwith = :entity
1590     WHERE users_id = :users_id;
1591
1592   if (dbms_errno)
1593     return mr_errcode;
1594   if (sqlca.sqlerrd[2] != 1)
1595     return MR_INTERNAL;
1596   
1597   /* Only update usage count if we created a POP pobox. */
1598   if (!strcmp(potype, "POP") && !po_exists)
1599     set_pop_usage(mid, 1);
1600   
1601   com_err(whoami, 0, "set login name to %s", login);
1602   incremental_after(USERS_TABLE, buffer, 0);
1603
1604   /* We've just changed the login name in the DB; recache it in case
1605      the wrong thing got into the cache earlier. */
1606   cache_entry(login, USERS_TABLE, users_id);
1607
1608   /* create filesystem */
1609   if (set_next_object_id("filsys_id", FILESYS_TABLE, 0))
1610     return MR_NO_ID;
1611   incremental_clear_before();
1612   if (islower(login[0]) && islower(login[1]))
1613     {
1614       sprintf(directory, "/afs/athena.mit.edu/user/%c/%c/%s",
1615               login[0], login[1], login);
1616     }
1617   else
1618     sprintf(directory, "/afs/athena.mit.edu/user/other/%s", login);
1619
1620   EXEC SQL SELECT value INTO :fsidval FROM numvalues
1621     WHERE numvalues.name = 'filsys_id';
1622   EXEC SQL INSERT INTO filesys
1623     (filsys_id, phys_id, label, type, mach_id, name,
1624      mount, rwaccess, comments, owner, owners, createflg,
1625      lockertype, modtime, modby, modwith)
1626     VALUES
1627     (:fsidval, 0, :login, 'AFS', :m_id, :directory,
1628      '/mit/' || :login, 'w', 'User Locker', :users_id, :list_id, 1,
1629      'HOMEDIR', SYSDATE, :who, :entity);
1630
1631   if (dbms_errno)
1632     return mr_errcode;
1633   if (sqlca.sqlerrd[2] != 1)
1634     return MR_INTERNAL;
1635   sprintf(buffer, "fs.filsys_id = %d", fsidval);
1636   incremental_after(FILESYS_TABLE, buffer, 0);
1637
1638   /* set quota */
1639   incremental_clear_before();
1640   EXEC SQL INSERT INTO quota
1641     (entity_id, filsys_id, type, quota, phys_id, modtime, modby, modwith)
1642     VALUES (0, :fsidval, 'ANY', :def_quota, 0, SYSDATE, :who, :entity);
1643   if (dbms_errno)
1644     return mr_errcode;
1645   if (sqlca.sqlerrd[2] != 1)
1646     return MR_INTERNAL;
1647   aargv[0] = login;
1648   aargv[1] = "ANY";
1649   aargv[2] = login;
1650   sprintf(buffer, "q.entity_id = 0 and q.filsys_id = %d and q.type = 'ANY'",
1651           fsidval);
1652   incremental_after(QUOTA_TABLE, buffer, aargv);
1653   com_err(whoami, 0, "quota of %d assigned", def_quota);
1654   if (dbms_errno)
1655     return mr_errcode;
1656
1657   EXEC SQL UPDATE tblstats SET updates = updates + 1, modtime = SYSDATE
1658     WHERE table_name = 'users';
1659   EXEC SQL UPDATE tblstats SET appends = appends + 1, modtime = SYSDATE
1660     WHERE table_name = 'filesys' OR table_name = 'quota';
1661   if (dbms_errno)
1662     return mr_errcode;
1663   return MR_SUCCESS;
1664 }
1665
1666 /** set_pop_usage - incr/decr usage count for pop server in serverhosts talbe
1667  **
1668  ** Inputs:
1669  **   id of machine
1670  **   delta (will be +/- 1)
1671  **
1672  ** Description:
1673  **   - incr/decr value field in serverhosts table for pop/mach_id
1674  **
1675  **/
1676
1677 int set_pop_usage(id, cnt)
1678     int id, cnt;
1679 {
1680   EXEC SQL BEGIN DECLARE SECTION;
1681   int iid = id, icnt = cnt;
1682   EXEC SQL END DECLARE SECTION;
1683
1684   EXEC SQL UPDATE serverhosts SET value1 = value1 + :icnt
1685     WHERE serverhosts.service = 'POP' AND serverhosts.mach_id = :iid;
1686
1687   if (dbms_errno)
1688     return mr_errcode;
1689   return MR_SUCCESS;
1690 }
1691
1692
1693 int do_user_reservation(struct query *q, char *argv[], client *cl)
1694 {
1695   EXEC SQL BEGIN DECLARE SECTION;
1696   char resv[USERS_RESERVATIONS_SIZE];
1697   char *trans, name[ALIAS_NAME_SIZE];
1698   int uid;
1699   EXEC SQL END DECLARE SECTION;
1700
1701   uid = *(int *)argv[0];
1702   trans = argv[1];
1703
1704   EXEC SQL SELECT UPPER(name) INTO :name FROM alias
1705     WHERE type = 'RESERVE' AND LOWER(trans) = LOWER(:trans);
1706   if (dbms_errno)
1707     return mr_errcode;
1708   if (sqlca.sqlerrd[2] != 1)
1709     return MR_STRING;
1710   name[1] = '\0';
1711
1712   EXEC SQL SELECT reservations INTO :resv FROM users
1713     WHERE users_id = :uid;
1714   if (dbms_errno)
1715     return mr_errcode;
1716   strtrim(resv);
1717
1718   if (!strcmp(q->shortname, "aurv"))
1719     {
1720       if (strchr(resv, *name))
1721         return MR_EXISTS;
1722       if (strlen(resv) == USERS_RESERVATIONS_SIZE - 1)
1723         return MR_ARG_TOO_LONG;
1724
1725       strcat(resv, name);
1726     }
1727   else
1728     {
1729       char *p = strchr(resv, *name);
1730       if (!p)
1731         return MR_NO_MATCH;
1732       memmove(p, p + 1, strlen(p));
1733     }
1734
1735   EXEC SQL UPDATE users SET reservations = NVL(:resv, CHR(0))
1736     WHERE users_id = :uid;
1737   if (dbms_errno)
1738     return mr_errcode;
1739
1740   EXEC SQL UPDATE tblstats SET updates = updates + 1, modtime = SYSDATE
1741     WHERE table_name = 'users';
1742   return set_modtime_by_id(q, argv, cl);
1743 }
1744
1745 int get_user_reservations(struct query *q, char **argv, client *cl,
1746                           int (*action)(int, char *[], void *), void *actarg)
1747 {
1748   EXEC SQL BEGIN DECLARE SECTION;
1749   char resv[USERS_RESERVATIONS_SIZE], *p;
1750   char trans[ALIAS_TRANS_SIZE], name[2] = { 0, 0 };
1751   int uid;
1752   EXEC SQL END DECLARE SECTION;
1753   char *targv[1];
1754
1755   uid = *(int *)argv[0];
1756
1757   EXEC SQL SELECT reservations INTO :resv FROM users
1758     WHERE users_id = :uid;
1759   if (dbms_errno)
1760     return mr_errcode;
1761
1762   targv[0] = trans;
1763   p = resv;
1764   while (*p && !isspace(*p))
1765     {
1766       name[0] = toupper(*p);
1767       EXEC SQL SELECT trans INTO :trans FROM alias
1768         WHERE type = 'RESERVE' AND UPPER(name) = :name;
1769       if (dbms_errno)
1770         return mr_errcode;
1771       if (sqlca.sqlerrd[2] != 1)
1772         sprintf(trans, "Unknown (%s)", name);
1773       (*action)(1, targv, actarg);
1774       p++;
1775     }
1776   return MR_SUCCESS;
1777 }
1778
1779 int get_user_by_reservation(struct query *q, char **argv, client *cl,
1780                             int (*action)(int, char *[], void *), void *actarg)
1781 {
1782   EXEC SQL BEGIN DECLARE SECTION;
1783   char resv[USERS_RESERVATIONS_SIZE], login[USERS_LOGIN_SIZE];
1784   char *trans, name[ALIAS_NAME_SIZE];
1785   int uid;
1786   EXEC SQL END DECLARE SECTION;
1787   char *targv[1];
1788
1789   trans = argv[0];
1790
1791   EXEC SQL SELECT UPPER(name) INTO :name FROM alias
1792     WHERE type = 'RESERVE' AND LOWER(trans) = LOWER(:trans);
1793   if (dbms_errno)
1794     return mr_errcode;
1795   if (sqlca.sqlerrd[2] != 1)
1796     return MR_STRING;
1797   name[1] = '\0';
1798
1799   EXEC SQL DECLARE csr_gubr CURSOR FOR
1800     SELECT login FROM users WHERE reservations LIKE '%' || :name || '%';
1801   EXEC SQL OPEN csr_gubr;
1802   if (dbms_errno)
1803     return mr_errcode;
1804
1805   targv[0] = login;
1806   while (1)
1807     {
1808       EXEC SQL FETCH csr_gubr INTO :login;
1809       if (sqlca.sqlcode)
1810         break;
1811       (*action)(1, targv, actarg);
1812     }
1813   EXEC SQL CLOSE csr_gubr;
1814
1815   return MR_SUCCESS;
1816 }
This page took 1.301816 seconds and 5 git commands to generate.