3 * Special query routines
5 * Copyright (C) 1987-1998 by the Massachusetts Institute of Technology
6 * For copying and distribution information, please see the file
10 #include <mit-copyright.h>
11 #include "mr_server.h"
19 EXEC SQL INCLUDE sqlca;
23 extern char *whoami, *table_name[];
24 extern int dbms_errno, mr_errcode;
26 EXEC SQL BEGIN DECLARE SECTION;
27 extern char stmt_buf[];
28 EXEC SQL END DECLARE SECTION;
30 EXEC SQL WHENEVER SQLERROR DO dbmserr();
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[]);
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.
47 int set_pobox(struct query *q, char **argv, client *cl)
49 EXEC SQL BEGIN DECLARE SECTION;
51 char *box, potype[USERS_POTYPE_SIZE];
52 EXEC SQL END DECLARE SECTION;
56 user = *(int *)argv[0];
58 EXEC SQL SELECT pop_id, potype INTO :id, :potype FROM users
59 WHERE users_id = :user;
62 if (!strcmp(strtrim(potype), "POP") ||
63 (!strcmp(strtrim(potype), "SPLIT") && id))
64 set_pop_usage(id, -1);
66 if (!strcmp(argv[1], "POP"))
68 status = name_to_id(box, MACHINE_TABLE, &id);
69 if (status == MR_NO_MATCH)
73 EXEC SQL UPDATE users SET potype = 'POP', pop_id = :id, imap_id = 0
74 WHERE users_id = :user;
77 else if (!strcmp(argv[1], "SMTP") || !strcmp(argv[1], "SPLIT"))
79 if (strchr(box, '/') || strchr(box, '|'))
81 status = name_to_id(box, STRINGS_TABLE, &id);
82 if (status == MR_NO_MATCH)
87 /* If going from SMTP or NONE to SPLIT, make sure we have a valid
90 if (!strcmp(potype, "SMTP") || !strcmp(potype, "NONE"))
92 status = set_pobox_pop(q, argv, cl);
96 strlcpy(potype, argv[1], sizeof(potype));
97 EXEC SQL UPDATE users SET potype = :potype, box_id = :id
98 WHERE users_id = :user;
100 else if (!strcmp(argv[1], "IMAP"))
102 EXEC SQL SELECT filsys_id INTO :id FROM filesys
103 WHERE label = :box AND type = 'IMAP';
106 EXEC SQL UPDATE users SET potype = 'IMAP', imap_id = :id, pop_id = 0
107 WHERE users_id = :user;
109 else /* argv[1] == "NONE" */
111 EXEC SQL UPDATE users SET potype = 'NONE'
112 WHERE users_id = :user;
115 set_pobox_modtime(q, argv, cl);
116 EXEC SQL UPDATE tblstats SET updates = updates + 1, modtime = SYSDATE
117 WHERE table_name = 'users';
123 /* set_pobox_pop: Revert to existing POP or IMAP pobox.
124 * Also take care of keeping track of the post office usage.
126 int set_pobox_pop(struct query *q, char **argv, client *cl)
128 EXEC SQL BEGIN DECLARE SECTION;
129 int id, pid, iid, mid;
130 char type[USERS_POTYPE_SIZE];
131 EXEC SQL END DECLARE SECTION;
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))
141 EXEC SQL SELECT mach_id INTO :mid FROM machine
142 WHERE mach_id = :pid;
143 if (sqlca.sqlerrd[2] == 0)
145 EXEC SQL UPDATE users SET potype = 'POP' WHERE users_id = :id;
146 if (!strcmp(strtrim(type), "POP"))
147 set_pop_usage(mid, 1);
151 EXEC SQL SELECT filsys_id INTO :mid FROM filesys
152 WHERE filsys_id = :iid;
153 if (sqlca.sqlerrd[2] == 0)
155 EXEC SQL UPDATE users SET potype = 'IMAP' WHERE users_id = :id;
158 set_pobox_modtime(q, argv, cl);
159 EXEC SQL UPDATE tblstats SET updates = updates + 1, modtime = SYSDATE
160 WHERE table_name = 'users';
167 /* Add_member_to_list: do list flattening as we go! MAXLISTDEPTH is
168 * how many different ancestors a member is allowed to have.
171 #define MAXLISTDEPTH 1024
173 int add_member_to_list(struct query *q, char **argv, client *cl)
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;
182 char *dtypes[MAXLISTDEPTH];
183 char *iargv[3], *buf;
185 lid = *(int *)argv[0];
187 mid = *(int *)argv[2];
188 tag = !strcmp(q->shortname, "atml") ? *(int *)argv[3] : 0;
190 if (acl_access_check(lid, cl))
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;
199 if (!strcasecmp(mtype, "STRING"))
202 status = id_to_name(mid, STRINGS_TABLE, &buf);
205 if (strchr(buf, '/') || strchr(buf, '|'))
216 EXEC SQL DECLARE csr103 CURSOR FOR
217 SELECT list_id, ref_count FROM imembers
218 WHERE member_id = :lid AND member_type = 'LIST';
221 EXEC SQL OPEN csr103;
226 EXEC SQL FETCH csr103 INTO :id, :ref;
230 ancestors[acount++] = id;
231 if (acount >= MAXLISTDEPTH)
234 EXEC SQL CLOSE csr103;
237 if (acount >= MAXLISTDEPTH)
239 descendants[0] = mid;
244 if (!strcmp(mtype, "LIST"))
246 EXEC SQL DECLARE csr104 CURSOR FOR
247 SELECT member_id, member_type, ref_count
249 WHERE list_id = :mid;
252 EXEC SQL OPEN csr104;
257 EXEC SQL FETCH csr104 INTO :id, :dtype, :ref;
263 dtypes[dcount] = "LIST";
266 dtypes[dcount] = "USER";
269 dtypes[dcount] = "STRING";
272 dtypes[dcount] = "KERBEROS";
279 descendants[dcount++] = id;
280 if (dcount >= MAXLISTDEPTH)
286 EXEC SQL CLOSE csr104;
292 for (a = 0; a < acount; a++)
295 for (d = 0; d < dcount; d++)
297 mid = descendants[d];
299 if (mid == lid && !strcmp(mtype, "LIST"))
301 EXEC SQL SELECT COUNT(ref_count) INTO :rowcnt
303 WHERE list_id = :lid AND member_id = :mid
304 AND member_type = :mtype;
305 ref = aref[a] * dref[d];
308 if (a == 0 && d == 0)
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;
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;
325 incremental_clear_before();
326 if (a == 0 && d == 0)
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);
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);
338 iargv[0] = (char *)lid;
340 iargv[2] = (char *)mid;
341 incremental_after(IMEMBERS_TABLE, 0, iargv);
345 lid = *(int *)argv[0];
349 SET modtime = SYSDATE, modby = :who, modwith = :entity
350 WHERE list_id = :lid;
357 /* Delete_member_from_list: do list flattening as we go!
360 int delete_member_from_list(struct query *q, char **argv, client *cl)
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];
371 lid = *(int *)argv[0];
373 mid = *(int *)argv[2];
375 if (acl_access_check(lid, cl))
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;
389 EXEC SQL DECLARE csr105 CURSOR FOR
390 SELECT list_id, ref_count FROM imembers
391 WHERE member_id = :lid AND member_type = 'LIST';
394 EXEC SQL OPEN csr105;
399 EXEC SQL FETCH csr105 INTO :id, :ref;
403 ancestors[acount++] = id;
404 if (acount >= MAXLISTDEPTH)
407 EXEC SQL CLOSE csr105;
410 if (acount >= MAXLISTDEPTH)
412 descendants[0] = mid;
417 if (!strcmp(mtype, "LIST"))
419 EXEC SQL DECLARE csr106 CURSOR FOR
420 SELECT member_id, member_type, ref_count FROM imembers
421 WHERE list_id = :mid;
424 EXEC SQL OPEN csr106;
429 EXEC SQL FETCH csr106 INTO :id, :dtype, :ref;
435 dtypes[dcount] = "LIST";
438 dtypes[dcount] = "USER";
441 dtypes[dcount] = "STRING";
444 dtypes[dcount] = "KERBEROS";
451 descendants[dcount++] = id;
452 if (dcount >= MAXLISTDEPTH)
455 EXEC SQL CLOSE csr106;
461 for (a = 0; a < acount; a++)
464 for (d = 0; d < dcount; d++)
466 mid = descendants[d];
468 if (mid == lid && !strcmp(mtype, "LIST"))
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];
475 iargv[0] = (char *)lid;
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();
484 else if (a == 0 && d == 0)
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;
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;
500 lid = *(int *)argv[0];
503 EXEC SQL UPDATE list SET modtime = SYSDATE, modby = :who, modwith = :entity
504 WHERE list_id = :lid;
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
516 int acl_access_check(int list_id, client *cl)
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;
523 /* Check if the list is directly a capacl */
524 EXEC SQL SELECT COUNT(list_id) INTO :c1 FROM capacls WHERE list_id=:lid;
526 /* Check if the list is a member (direct or indirect) of a list that
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;
533 if (c1 == 0 && c2 == 0)
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);
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.
553 int get_ace_use(struct query *q, char *argv[], client *cl,
554 int (*action)(int, char *[], void *), void *actarg)
557 EXEC SQL BEGIN DECLARE SECTION;
560 EXEC SQL END DECLARE SECTION;
561 struct save_queue *sq;
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);
570 if (!strcmp(atype, "RLIST"))
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;
579 EXEC SQL OPEN csr107;
584 EXEC SQL FETCH csr107 INTO :listid;
587 sq_save_unique_data(sq, (void *)listid);
589 EXEC SQL CLOSE csr107;
590 /* now process each one */
591 while (sq_get_data(sq, &id))
593 if (get_ace_internal("LIST", id, action, actarg) == MR_SUCCESS)
598 if (!strcmp(atype, "RUSER"))
600 EXEC SQL DECLARE csr108 CURSOR FOR
601 SELECT list_id FROM imembers
602 WHERE member_type = 'USER' AND member_id = :aid;
605 EXEC SQL OPEN csr108;
610 EXEC SQL FETCH csr108 INTO :listid;
613 sq_save_data(sq, (void *)listid);
615 EXEC SQL CLOSE csr108;
616 /* now process each one */
617 while (sq_get_data(sq, &id))
619 if (get_ace_internal("LIST", id, action, actarg) == MR_SUCCESS)
622 if (get_ace_internal("USER", aid, action, actarg) == MR_SUCCESS)
626 if (!strcmp(atype, "RKERBEROS"))
628 EXEC SQL DECLARE csr109 CURSOR FOR
629 SELECT list_id FROM imembers
630 WHERE member_type = 'KERBEROS' AND member_id = :aid;
633 EXEC SQL OPEN csr109;
638 EXEC SQL FETCH csr109 INTO :listid;
641 sq_save_data(sq, (void *)listid);
643 EXEC SQL CLOSE csr109;
644 /* now process each one */
645 while (sq_get_data(sq, &id))
647 if (get_ace_internal("LIST", id, action, actarg) == MR_SUCCESS)
650 if (get_ace_internal("KERBEROS", aid, action, actarg) == MR_SUCCESS)
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.
668 int get_ace_internal(char *atype, int aid,
669 int (*action)(int, char *[], void *), void *actarg)
673 EXEC SQL BEGIN DECLARE SECTION;
674 char name[MAX_FIELD_WIDTH], *type = atype;
676 EXEC SQL END DECLARE SECTION;
679 if (!strcmp(atype, "LIST"))
681 rargv[0] = "FILESYS";
682 EXEC SQL DECLARE csr110 CURSOR FOR
683 SELECT label FROM filesys
687 EXEC SQL OPEN csr110;
692 EXEC SQL FETCH csr110 INTO :name;
695 (*action)(2, rargv, actarg);
698 EXEC SQL CLOSE csr110;
701 EXEC SQL DECLARE csr111 CURSOR FOR
702 SELECT capability FROM capacls
706 EXEC SQL OPEN csr111;
711 EXEC SQL FETCH csr111 INTO :name;
714 (*action)(2, rargv, actarg);
717 EXEC SQL CLOSE csr111;
719 else if (!strcmp(atype, "USER"))
721 rargv[0] = "FILESYS";
722 EXEC SQL DECLARE csr112 CURSOR FOR
723 SELECT label FROM filesys
727 EXEC SQL OPEN csr112;
732 EXEC SQL FETCH csr112 INTO :name;
735 (*action)(2, rargv, actarg);
738 EXEC SQL CLOSE csr112;
742 EXEC SQL DECLARE csr113 CURSOR FOR
743 SELECT name FROM list
744 WHERE acl_type = :type AND acl_id = :id;
747 EXEC SQL OPEN csr113;
752 EXEC SQL FETCH csr113 INTO :name;
755 (*action)(2, rargv, actarg);
758 EXEC SQL CLOSE csr113;
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;
766 EXEC SQL OPEN csr114;
771 EXEC SQL FETCH csr114 INTO :name;
774 (*action)(2, rargv, actarg);
777 EXEC SQL CLOSE csr114;
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
786 EXEC SQL OPEN csr115;
791 EXEC SQL FETCH csr115 INTO :name;
794 (*action)(2, rargv, actarg);
797 EXEC SQL CLOSE csr115;
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;
806 EXEC SQL OPEN csr115a;
811 EXEC SQL FETCH csr115a INTO :name;
814 (*action)(2, rargv, actarg);
817 EXEC SQL CLOSE csr115a;
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;
828 EXEC SQL OPEN csr116;
833 EXEC SQL FETCH csr116 INTO :name;
836 (*action)(2, rargv, actarg);
839 EXEC SQL CLOSE csr116;
847 int ghbo_internal(char *atype, int aid,
848 int (*action)(int, char *[], void *), void *actarg)
852 EXEC SQL BEGIN DECLARE SECTION;
853 char name[MACHINE_NAME_SIZE], *type = atype;
855 EXEC SQL END DECLARE SECTION;
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;
864 EXEC SQL OPEN csr115b;
869 EXEC SQL FETCH csr115b INTO :name;
872 (*action)(1, rargv, actarg);
875 EXEC SQL CLOSE csr115b;
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)
887 EXEC SQL BEGIN DECLARE SECTION;
890 EXEC SQL END DECLARE SECTION;
891 struct save_queue *sq;
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);
900 if (!strcmp(atype, "RLIST"))
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;
909 EXEC SQL OPEN csr107q;
914 EXEC SQL FETCH csr107q INTO :listid;
917 sq_save_unique_data(sq, (void *)listid);
919 EXEC SQL CLOSE csr107q;
920 /* now process each one */
921 while (sq_get_data(sq, &id))
923 if (ghbo_internal("LIST", id, action, actarg) == MR_SUCCESS)
928 if (!strcmp(atype, "RUSER"))
930 EXEC SQL DECLARE csr108q CURSOR FOR
931 SELECT list_id FROM imembers
932 WHERE member_type = 'USER' AND member_id = :aid;
935 EXEC SQL OPEN csr108q;
940 EXEC SQL FETCH csr108q INTO :listid;
943 sq_save_data(sq, (void *)listid);
945 EXEC SQL CLOSE csr108q;
946 /* now process each one */
947 while (sq_get_data(sq, &id))
949 if (ghbo_internal("LIST", id, action, actarg) == MR_SUCCESS)
952 if (ghbo_internal("USER", aid, action, actarg) == MR_SUCCESS)
956 if (!strcmp(atype, "RKERBEROS"))
958 EXEC SQL DECLARE csr109q CURSOR FOR
959 SELECT list_id FROM imembers
960 WHERE member_type = 'KERBEROS' AND member_id = :aid;
963 EXEC SQL OPEN csr109q;
968 EXEC SQL FETCH csr109q INTO :listid;
971 sq_save_data(sq, (void *)listid);
973 EXEC SQL CLOSE csr109q;
974 /* now process each one */
975 while (sq_get_data(sq, &id))
977 if (ghbo_internal("LIST", id, action, actarg) == MR_SUCCESS)
980 if (ghbo_internal("KERBEROS", aid, action, actarg) == MR_SUCCESS)
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.
1000 int get_lists_of_member(struct query *q, char *argv[], client *cl,
1001 int (*action)(int, char *[], void *), void *actarg)
1003 int found = 0, direct = 1;
1005 EXEC SQL BEGIN DECLARE SECTION;
1008 char name[LIST_NAME_SIZE];
1009 char active[5], public[5], hidden[5], maillist[5], grouplist[5];
1010 EXEC SQL END DECLARE SECTION;
1013 aid = *(int *)argv[1];
1014 if (!strcmp(atype, "RLIST"))
1019 if (!strcmp(atype, "RUSER"))
1024 if (!strcmp(atype, "RSTRING"))
1029 if (!strcmp(atype, "RKERBEROS"))
1039 rargv[4] = maillist;
1040 rargv[5] = grouplist;
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;
1050 EXEC SQL OPEN csr117a;
1055 EXEC SQL FETCH csr117a
1056 INTO :name, :active, :public, :hidden, :maillist, :grouplist;
1059 (*action)(6, rargv, actarg);
1062 EXEC SQL CLOSE csr117a;
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;
1073 EXEC SQL OPEN csr117b;
1078 EXEC SQL FETCH csr117b
1079 INTO :name, :active, :public, :hidden, :maillist, :grouplist;
1082 (*action)(6, rargv, actarg);
1085 EXEC SQL CLOSE csr117b;
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.
1102 static char *lflags[5] = { "active", "publicflg", "hidden", "maillist", "grouplist" };
1104 int qualified_get_lists(struct query *q, char *argv[], client *cl,
1105 int (*action)(int, char *[], void *), void *actarg)
1107 return qualified_get(q, argv, action, actarg, "l.list_id != 0",
1108 "l", "name", lflags);
1112 int get_members_of_list(struct query *q, char *argv[], client *cl,
1113 int (*action)(int, char *[], void *), void *actarg)
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;
1122 /* For gmol or gtml, only get direct members. For geml, get all. */
1123 if (!strcmp(q->shortname, "geml"))
1128 /* For gmol or geml, only return type and name. For gtml, return tag too. */
1129 if (!strcmp(q->shortname, "gtml"))
1134 list_id = *(int *)argv[0];
1136 targv[1] = member_name;
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;
1147 EXEC SQL OPEN csr119;
1152 EXEC SQL FETCH csr119 INTO :member_name, :tag;
1155 (*action)(targc, targv, actarg);
1157 EXEC SQL CLOSE csr119;
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;
1169 EXEC SQL OPEN csr120;
1174 EXEC SQL FETCH csr120 INTO :member_name, :tag;
1177 (*action)(targc, targv, actarg);
1179 EXEC SQL CLOSE csr120;
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;
1191 EXEC SQL OPEN csr121;
1196 EXEC SQL FETCH csr121 INTO :member_name, :tag;
1199 (*action)(targc, targv, actarg);
1201 EXEC SQL CLOSE csr121;
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;
1213 EXEC SQL OPEN csr122;
1218 EXEC SQL FETCH csr122 INTO :member_name, :tag;
1221 (*action)(targc, targv, actarg);
1223 EXEC SQL CLOSE csr122;
1231 /* count_members_of_list: this is a simple query, but it cannot be done
1232 * through the dispatch table.
1235 int count_members_of_list(struct query *q, char *argv[], client *cl,
1236 int (*action)(int, char *[], void *), void *actarg)
1238 EXEC SQL BEGIN DECLARE SECTION;
1240 EXEC SQL END DECLARE SECTION;
1241 char *rargv[1], countbuf[5];
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;
1249 sprintf(countbuf, "%d", ct);
1250 (*action)(1, rargv, actarg);
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.
1261 static char *sflags[3] = { "enable", "inprogress", "harderror" };
1263 int qualified_get_server(struct query *q, char *argv[], client *cl,
1264 int (*action)(int, char *[], void *), void *actarg)
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 */
1273 /* generic qualified get routine, used by qualified_get_lists,
1274 * qualified_get_server, and qualified_get_serverhost.
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
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[])
1290 strcpy(qual, start);
1291 for (i = 0; i < q->argc; i++)
1293 if (!strcmp(argv[i], "TRUE"))
1295 sprintf(buf, " AND %s.%s != 0", range, flags[i]);
1298 else if (!strcmp(argv[i], "FALSE"))
1300 sprintf(buf, " AND %s.%s = 0", range, flags[i]);
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);
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.
1317 static char *shflags[6] = { "service", "enable", "override", "success",
1318 "inprogress", "hosterror" };
1320 int qualified_get_serverhost(struct query *q, char *argv[], client *cl,
1321 int (*action)(int, char *[], void *),
1324 char qual[256], buf[32];
1327 sprintf(qual, "m.mach_id = sh.mach_id AND sh.service = UPPER('%s')",
1329 for (i = 1; i < q->argc; i++)
1331 if (!strcmp(argv[i], "TRUE"))
1333 sprintf(buf, " AND sh.%s != 0", shflags[i]);
1336 else if (!strcmp(argv[i], "FALSE"))
1338 sprintf(buf, " AND sh.%s = 0", shflags[i]);
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);
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)
1355 int register_user(struct query *q, char **argv, client *cl)
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;
1365 static int m_id, def_quota, def_imap_quota, list_id;
1366 EXEC SQL END DECLARE SECTION;
1367 char buffer[256], *aargv[3];
1371 EXEC SQL SELECT list_id INTO :list_id FROM list
1372 WHERE name = 'wheel';
1374 EXEC SQL SELECT mach_id INTO :m_id FROM machine
1375 WHERE name = 'ATHENA.MIT.EDU';
1377 EXEC SQL SELECT value INTO :def_quota FROM numvalues
1378 WHERE name = 'def_quota';
1379 if (sqlca.sqlerrd[2] != 1)
1382 EXEC SQL SELECT value INTO :def_imap_quota FROM numvalues
1383 WHERE name = 'def_imap_quota';
1384 if (sqlca.sqlerrd[2] != 1)
1388 entity = cl->entity;
1389 who = cl->client_id;
1391 uid = atoi(argv[0]);
1396 EXEC SQL SELECT users_id, status INTO :users_id, :ostatus
1398 WHERE unix_uid = :uid AND (status = 0 OR status = 5 OR status = 6);
1400 if (sqlca.sqlerrd[2] == 0)
1402 if (sqlca.sqlerrd[2] > 1)
1403 return MR_NOT_UNIQUE;
1405 /* check new login name */
1406 EXEC SQL SELECT COUNT(login) INTO :rowcount FROM users
1407 WHERE login = :login AND users_id != :users_id;
1412 EXEC SQL SELECT COUNT(name) INTO :rowcount FROM list
1413 WHERE LOWER(name) = :login;
1418 EXEC SQL SELECT COUNT(label) INTO :rowcount FROM filesys
1419 WHERE label = :login;
1424 EXEC SQL SELECT COUNT(label) INTO :rowcount
1425 FROM filesys WHERE label = :login || '.po';
1430 EXEC SQL SELECT owner INTO :tmp FROM filesys
1431 WHERE label = :login || '.po';
1434 if ((ostatus == 0) || (tmp != users_id))
1436 EXEC SQL SELECT count(potype) INTO :rowcount FROM users WHERE
1437 login = :login AND potype = 'IMAP';
1443 EXEC SQL SELECT COUNT(name) INTO :rowcount FROM alias
1444 WHERE ( name = :login OR name = :login || '.po' )
1445 AND type = 'FILESYS';
1450 com_err(whoami, 0, "login name OK");
1452 EXEC SQL SELECT COUNT(potype) INTO :rowcount FROM users WHERE
1453 login = :login AND potype = 'POP';
1459 /* choose type and location for pobox */
1462 if (!strcmp(potype, "POP"))
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');
1472 EXEC SQL OPEN csr130;
1475 EXEC SQL FETCH csr130 INTO :popid, :machname;
1476 if (sqlca.sqlerrd[2] == 0)
1478 EXEC SQL CLOSE csr130;
1485 EXEC SQL CLOSE csr130;
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));
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
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
1511 EXEC SQL OPEN csr_rusr_imap;
1514 EXEC SQL FETCH csr_rusr_imap INTO :tmp, :npid, :mid, :dir, :machname;
1515 if (sqlca.sqlerrd[2] == 0)
1517 EXEC SQL CLOSE csr_rusr_imap;
1524 EXEC SQL CLOSE csr_rusr_imap;
1529 /* create filesystem */
1530 if (set_next_object_id("filsys_id", FILESYS_TABLE, 0))
1532 incremental_clear_before();
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)
1541 (:popid, :npid, :login || '.po', 'IMAP', :mid, :dir,
1542 CHR(0), 'w', 'IMAP box', :users_id, :list_id, 1,
1543 'USER', SYSDATE, :who, :entity);
1547 if (sqlca.sqlerrd[2] != 1)
1549 sprintf(buffer, "fs.filsys_id = %d", popid);
1550 incremental_after(FILESYS_TABLE, buffer, 0);
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);
1560 if (sqlca.sqlerrd[2] != 1)
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);
1571 EXEC SQL UPDATE nfsphys SET allocated = allocated + :def_imap_quota
1572 WHERE nfsphys_id = :npid;
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),
1581 /* change login name, set pobox */
1582 sprintf(buffer, "u.users_id = %d", users_id);
1583 incremental_before(USERS_TABLE, buffer, 0);
1585 if (ostatus == 5 || ostatus == 6)
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;
1594 if (sqlca.sqlerrd[2] != 1)
1597 /* Only update usage count if we created a POP pobox. */
1598 if (!strcmp(potype, "POP") && !po_exists)
1599 set_pop_usage(mid, 1);
1601 com_err(whoami, 0, "set login name to %s", login);
1602 incremental_after(USERS_TABLE, buffer, 0);
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);
1608 /* create filesystem */
1609 if (set_next_object_id("filsys_id", FILESYS_TABLE, 0))
1611 incremental_clear_before();
1612 if (islower(login[0]) && islower(login[1]))
1614 sprintf(directory, "/afs/athena.mit.edu/user/%c/%c/%s",
1615 login[0], login[1], login);
1618 sprintf(directory, "/afs/athena.mit.edu/user/other/%s", login);
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)
1627 (:fsidval, 0, :login, 'AFS', :m_id, :directory,
1628 '/mit/' || :login, 'w', 'User Locker', :users_id, :list_id, 1,
1629 'HOMEDIR', SYSDATE, :who, :entity);
1633 if (sqlca.sqlerrd[2] != 1)
1635 sprintf(buffer, "fs.filsys_id = %d", fsidval);
1636 incremental_after(FILESYS_TABLE, buffer, 0);
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);
1645 if (sqlca.sqlerrd[2] != 1)
1650 sprintf(buffer, "q.entity_id = 0 and q.filsys_id = %d and q.type = 'ANY'",
1652 incremental_after(QUOTA_TABLE, buffer, aargv);
1653 com_err(whoami, 0, "quota of %d assigned", def_quota);
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';
1666 /** set_pop_usage - incr/decr usage count for pop server in serverhosts talbe
1670 ** delta (will be +/- 1)
1673 ** - incr/decr value field in serverhosts table for pop/mach_id
1677 int set_pop_usage(id, cnt)
1680 EXEC SQL BEGIN DECLARE SECTION;
1681 int iid = id, icnt = cnt;
1682 EXEC SQL END DECLARE SECTION;
1684 EXEC SQL UPDATE serverhosts SET value1 = value1 + :icnt
1685 WHERE serverhosts.service = 'POP' AND serverhosts.mach_id = :iid;
1693 int do_user_reservation(struct query *q, char *argv[], client *cl)
1695 EXEC SQL BEGIN DECLARE SECTION;
1696 char resv[USERS_RESERVATIONS_SIZE];
1697 char *trans, name[ALIAS_NAME_SIZE];
1699 EXEC SQL END DECLARE SECTION;
1701 uid = *(int *)argv[0];
1704 EXEC SQL SELECT UPPER(name) INTO :name FROM alias
1705 WHERE type = 'RESERVE' AND LOWER(trans) = LOWER(:trans);
1708 if (sqlca.sqlerrd[2] != 1)
1712 EXEC SQL SELECT reservations INTO :resv FROM users
1713 WHERE users_id = :uid;
1718 if (!strcmp(q->shortname, "aurv"))
1720 if (strchr(resv, *name))
1722 if (strlen(resv) == USERS_RESERVATIONS_SIZE - 1)
1723 return MR_ARG_TOO_LONG;
1729 char *p = strchr(resv, *name);
1732 memmove(p, p + 1, strlen(p));
1735 EXEC SQL UPDATE users SET reservations = NVL(:resv, CHR(0))
1736 WHERE users_id = :uid;
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);
1745 int get_user_reservations(struct query *q, char **argv, client *cl,
1746 int (*action)(int, char *[], void *), void *actarg)
1748 EXEC SQL BEGIN DECLARE SECTION;
1749 char resv[USERS_RESERVATIONS_SIZE], *p;
1750 char trans[ALIAS_TRANS_SIZE], name[2] = { 0, 0 };
1752 EXEC SQL END DECLARE SECTION;
1755 uid = *(int *)argv[0];
1757 EXEC SQL SELECT reservations INTO :resv FROM users
1758 WHERE users_id = :uid;
1764 while (*p && !isspace(*p))
1766 name[0] = toupper(*p);
1767 EXEC SQL SELECT trans INTO :trans FROM alias
1768 WHERE type = 'RESERVE' AND UPPER(name) = :name;
1771 if (sqlca.sqlerrd[2] != 1)
1772 sprintf(trans, "Unknown (%s)", name);
1773 (*action)(1, targv, actarg);
1779 int get_user_by_reservation(struct query *q, char **argv, client *cl,
1780 int (*action)(int, char *[], void *), void *actarg)
1782 EXEC SQL BEGIN DECLARE SECTION;
1783 char resv[USERS_RESERVATIONS_SIZE], login[USERS_LOGIN_SIZE];
1784 char *trans, name[ALIAS_NAME_SIZE];
1786 EXEC SQL END DECLARE SECTION;
1791 EXEC SQL SELECT UPPER(name) INTO :name FROM alias
1792 WHERE type = 'RESERVE' AND LOWER(trans) = LOWER(:trans);
1795 if (sqlca.sqlerrd[2] != 1)
1799 EXEC SQL DECLARE csr_gubr CURSOR FOR
1800 SELECT login FROM users WHERE reservations LIKE '%' || :name || '%';
1801 EXEC SQL OPEN csr_gubr;
1808 EXEC SQL FETCH csr_gubr INTO :login;
1811 (*action)(1, targv, actarg);
1813 EXEC SQL CLOSE csr_gubr;