6 * Copyright (C) 1987 by the Massachusetts Institute of Technology
7 * For copying and distribution information, please see the file
13 static char *rcsid_qsupport_dc = "$Header$";
16 #include <mit-copyright.h>
17 #include "mr_server.h"
21 EXEC SQL INCLUDE sqlca;
24 extern char *whoami, *table_name[];
25 extern int dbms_errno, mr_errcode;
27 EXEC SQL BEGIN DECLARE SECTION;
28 extern char stmt_buf[];
29 EXEC SQL END DECLARE SECTION;
31 EXEC SQL WHENEVER SQLERROR DO dbmserr();
33 int get_ace_internal(char *atypex, int aid, int (*action)(), int actarg);
34 int gmol_internal(struct query *q, char *argv[], client *cl,
35 int (*action)(), int actarg, int flag);
36 int qualified_get(struct query *q, char *argv[], int (*action)(), int actarg,
37 char *start, char *range, char *field, char *flags[]);
40 /* Special query routines */
42 /* set_pobox - this does all of the real work.
43 * argv = user_id, type, box
44 * if type is POP, then box should be a machine, and its ID should be put in
45 * pop_id. If type is SMTP, then box should be a string and its ID should
46 * be put in box_id. If type is NONE, then box doesn't matter.
50 int set_pobox(struct query *q, char **argv, client *cl)
52 EXEC SQL BEGIN DECLARE SECTION;
55 EXEC SQL END DECLARE SECTION;
59 user = *(int *)argv[0];
61 EXEC SQL SELECT pop_id, potype INTO :id, :potype FROM users
62 WHERE users_id = :user;
65 if (!strcmp(strtrim(potype), "POP"))
66 set_pop_usage(id, -1);
68 if (!strcmp(argv[1], "POP"))
70 status = name_to_id(box, MACHINE_TABLE, &id);
71 if (status == MR_NO_MATCH)
75 EXEC SQL UPDATE users SET potype = 'POP', pop_id = :id
76 WHERE users_id = :user;
79 else if (!strcmp(argv[1], "SMTP"))
81 if (strchr(box, '/') || strchr(box, '|'))
83 status = name_to_id(box, STRINGS_TABLE, &id);
84 if (status == MR_NO_MATCH)
88 EXEC SQL UPDATE users SET potype = 'SMTP', box_id = :id
89 WHERE users_id = :user;
91 else /* argv[1] == "NONE" */
93 EXEC SQL UPDATE users SET potype = 'NONE'
94 WHERE users_id = :user;
97 set_pobox_modtime(q, argv, cl);
98 EXEC SQL UPDATE tblstats SET updates = updates + 1, modtime = SYSDATE
99 WHERE table_name = 'users';
106 /* get_list_info: passed a wildcard list name, returns lots of stuff about
107 * each list. This is tricky: first build a queue of all requested
108 * data. Rest of processing consists of fixing gid, ace_name, and modby.
111 int get_list_info(struct query *q, char **aargv, client *cl,
112 int (*action)(), int actarg)
115 EXEC SQL BEGIN DECLARE SECTION;
116 char *name, acl_type[9], listname[33], active[5], public[5], hidden[5];
117 char maillist[5], grouplist[5], gid_str[6], desc[256];
118 char modtime[27], modwith[9];
119 int id, rowcount, acl_id, hid, modby_id;
120 EXEC SQL END DECLARE SECTION;
121 int returned, status;
122 struct save_queue *sq, *sq_create();
124 returned = rowcount = 0;
126 convert_wildcards(name);
129 EXEC SQL DECLARE csr102 CURSOR FOR SELECT list_id FROM list
130 WHERE name LIKE :name ESCAPE '*';
133 EXEC SQL OPEN csr102;
138 EXEC SQL FETCH csr102 INTO :id;
141 sq_save_data(sq, (char *)id);
144 EXEC SQL CLOSE csr102;
151 argv[0] = listname; argv[1] = active; argv[2] = public; argv[3] = hidden;
152 argv[4] = maillist; argv[5] = grouplist; argv[6] = gid_str;
153 argv[7] = acl_type; argv[9] = desc; argv[10] = modtime; argv[12] = modwith;
155 while (sq_get_data(sq, &id))
160 EXEC SQL SELECT name, active, publicflg,
161 hidden, hidden, maillist, grouplist, gid,
162 acl_type, acl_id, description,
163 TO_CHAR(modtime, 'DD-mon-YYYY HH24:MI:SS'), modby, modwith
164 INTO :listname, :active, :public, :hidden, :hid, :maillist,
165 :grouplist, :gid_str, :acl_type, :acl_id, :desc,
166 :modtime, :modby_id, :modwith
167 FROM list WHERE list_id = :id;
173 if (atoi(gid_str) == -1)
174 argv[6] = UNIQUE_GID;
177 if (!strcmp(acl_type, "LIST"))
178 status = id_to_name(acl_id, LIST_TABLE, &argv[8]);
179 else if (!strcmp(acl_type, "USER"))
180 status = id_to_name(acl_id, USERS_TABLE, &argv[8]);
181 else if (!strcmp(acl_type, "KERBEROS"))
182 status = id_to_name(acl_id, STRINGS_TABLE, &argv[8]);
183 else if (!strcmp(acl_type, "NONE"))
187 argv[8] = strsave("NONE");
193 argv[8] = strsave("???");
195 if (status && status != MR_NO_MATCH)
198 argv[11] = malloc(0);
200 status = id_to_name(modby_id, USERS_TABLE, &argv[11]);
202 status = id_to_name(-modby_id, STRINGS_TABLE, &argv[11]);
203 if (status && status != MR_NO_MATCH)
206 mr_trim_args(q->vcnt, argv);
208 (*action)(q->vcnt, argv, actarg);
220 /* Add_member_to_list: do list flattening as we go! MAXLISTDEPTH is
221 * how many different ancestors a member is allowed to have.
224 #define MAXLISTDEPTH 1024
226 int add_member_to_list(struct query *q, char **argv, client *cl)
228 EXEC SQL BEGIN DECLARE SECTION;
229 int id, lid, mid, error, who, ref, rowcnt;
230 char *mtype, dtype[9], *entity;
231 EXEC SQL END DECLARE SECTION;
232 int ancestors[MAXLISTDEPTH], aref[MAXLISTDEPTH], acount, a;
233 int descendants[MAXLISTDEPTH], dref[MAXLISTDEPTH], dcount, d;
235 char *dtypes[MAXLISTDEPTH];
236 char *iargv[3], *buf;
238 lid = *(int *)argv[0];
240 mid = *(int *)argv[2];
241 /* if the member is already a direct member of the list, punt */
242 EXEC SQL SELECT COUNT(list_id) INTO :rowcnt FROM imembers
243 WHERE list_id = :lid AND member_id = :mid
244 AND member_type = :mtype AND direct = 1;
247 if (!strcasecmp(mtype, "STRING"))
250 status = id_to_name(mid, STRINGS_TABLE, &buf);
253 if (strchr(buf, '/') || strchr(buf, '|'))
264 EXEC SQL DECLARE csr103 CURSOR FOR
265 SELECT list_id, ref_count FROM imembers
266 WHERE member_id = :lid AND member_type = 'LIST';
269 EXEC SQL OPEN csr103;
274 EXEC SQL FETCH csr103 INTO :id, :ref;
278 ancestors[acount++] = id;
279 if (acount >= MAXLISTDEPTH)
282 EXEC SQL CLOSE csr103;
285 if (acount >= MAXLISTDEPTH)
287 descendants[0] = mid;
292 if (!strcmp(mtype, "LIST"))
294 EXEC SQL DECLARE csr104 CURSOR FOR
295 SELECT member_id, member_type, ref_count
297 WHERE list_id = :mid;
300 EXEC SQL OPEN csr104;
305 EXEC SQL FETCH csr104 INTO :id, :dtype, :ref;
311 dtypes[dcount] = "LIST";
314 dtypes[dcount] = "USER";
317 dtypes[dcount] = "STRING";
320 dtypes[dcount] = "KERBEROS";
327 descendants[dcount++] = id;
328 if (dcount >= MAXLISTDEPTH)
334 EXEC SQL CLOSE csr104;
340 for (a = 0; a < acount; a++)
343 for (d = 0; d < dcount; d++)
345 mid = descendants[d];
347 if (mid == lid && !strcmp(mtype, "LIST"))
349 EXEC SQL SELECT COUNT(ref_count) INTO :rowcnt
351 WHERE list_id = :lid AND member_id = :mid
352 AND member_type = :mtype;
353 ref = aref[a] * dref[d];
356 if (a == 0 && d == 0)
358 EXEC SQL UPDATE imembers
359 SET ref_count = ref_count + :ref, direct = 1
360 WHERE list_id = :lid AND member_id = :mid
361 AND member_type = :mtype;
365 EXEC SQL UPDATE imembers
366 SET ref_count = ref_count + :ref
367 WHERE list_id = :lid AND member_id = :mid
368 AND member_type = :mtype;
373 incremental_clear_before();
374 if (a == 0 && d == 0)
376 EXEC SQL INSERT INTO imembers
377 (list_id, member_id, direct, member_type, ref_count)
378 VALUES (:lid, :mid, 1, :mtype, 1);
382 EXEC SQL INSERT INTO imembers
383 (list_id, member_id, direct, member_type, ref_count)
384 VALUES (:lid, :mid, 0, :mtype, 1);
386 iargv[0] = (char *)lid;
388 iargv[2] = (char *)mid;
389 incremental_after(IMEMBERS_TABLE, 0, iargv);
393 lid = *(int *)argv[0];
397 SET modtime = SYSDATE, modby = :who, modwith = :entity
398 WHERE list_id = :lid;
405 /* Delete_member_from_list: do list flattening as we go!
408 int delete_member_from_list(struct query *q, char **argv, client *cl)
410 EXEC SQL BEGIN DECLARE SECTION;
411 int id, lid, mid, cnt, error, who, ref;
412 char *mtype, dtype[9], *entity;
413 EXEC SQL END DECLARE SECTION;
414 int ancestors[MAXLISTDEPTH], aref[MAXLISTDEPTH], acount, a;
415 int descendants[MAXLISTDEPTH], dref[MAXLISTDEPTH], dcount, d;
416 char *dtypes[MAXLISTDEPTH];
419 lid = *(int *)argv[0];
421 mid = *(int *)argv[2];
422 /* if the member is not a direct member of the list, punt */
423 EXEC SQL SELECT COUNT(list_id) INTO :cnt FROM imembers
424 WHERE list_id = :lid AND member_id = :mid
425 AND member_type = :mtype AND direct = 1;
433 EXEC SQL DECLARE csr105 CURSOR FOR
434 SELECT list_id, ref_count FROM imembers
435 WHERE member_id = :lid AND member_type = 'LIST';
438 EXEC SQL OPEN csr105;
443 EXEC SQL FETCH csr105 INTO :id, :ref;
447 ancestors[acount++] = id;
448 if (acount >= MAXLISTDEPTH)
451 EXEC SQL CLOSE csr105;
454 if (acount >= MAXLISTDEPTH)
456 descendants[0] = mid;
461 if (!strcmp(mtype, "LIST"))
463 EXEC SQL DECLARE csr106 CURSOR FOR
464 SELECT member_id, member_type, ref_count FROM imembers
465 WHERE list_id = :mid;
468 EXEC SQL OPEN csr106;
473 EXEC SQL FETCH csr106 INTO :id, :dtype, :ref;
479 dtypes[dcount] = "LIST";
482 dtypes[dcount] = "USER";
485 dtypes[dcount] = "STRING";
488 dtypes[dcount] = "KERBEROS";
495 descendants[dcount++] = id;
496 if (dcount >= MAXLISTDEPTH)
499 EXEC SQL CLOSE csr106;
505 for (a = 0; a < acount; a++)
508 for (d = 0; d < dcount; d++)
510 mid = descendants[d];
512 if (mid == lid && !strcmp(mtype, "LIST"))
514 EXEC SQL SELECT ref_count INTO :cnt FROM imembers
515 WHERE list_id = :lid AND member_id = :mid AND member_type = :mtype;
516 ref = aref[a] * dref[d];
519 iargv[0] = (char *)lid;
521 iargv[2] = (char *)mid;
522 incremental_before(IMEMBERS_TABLE, 0, iargv);
523 EXEC SQL DELETE FROM imembers
524 WHERE list_id = :lid AND member_id = :mid
525 AND member_type= :mtype;
526 incremental_clear_after();
528 else if (a == 0 && d == 0)
530 EXEC SQL UPDATE imembers
531 SET ref_count = ref_count - :ref, direct = 0
532 WHERE list_id = :lid AND member_id = :mid
533 AND member_type = :mtype;
537 EXEC SQL UPDATE imembers
538 SET ref_count = ref_count - :ref
539 WHERE list_id = :lid AND member_id = :mid
540 AND member_type = :mtype;
544 lid = *(int *)argv[0];
547 EXEC SQL UPDATE list SET modtime = SYSDATE, modby = :who, modwith = :entity
548 WHERE list_id = :lid;
555 /* get_ace_use - given a type and a name, return a type and a name.
556 * The ace_type is one of "LIST", "USER", "RLIST", or "RUSER" in argv[0],
557 * and argv[1] will contain the ID of the entity in question. The R*
558 * types mean to recursively look at every containing list, not just
559 * when the object in question is a direct member. On return, the
560 * usage type will be one of LIST, SERVICE, FILESYS, QUOTA, QUERY, or ZEPHYR.
563 int get_ace_use(struct query *q, char *argv[], client *cl,
564 int (*action)(), int actarg)
567 EXEC SQL BEGIN DECLARE SECTION;
570 EXEC SQL END DECLARE SECTION;
571 struct save_queue *sq, *sq_create();
574 aid = *(int *)argv[1];
575 if (!strcmp(atype, "LIST") || !strcmp(atype, "USER") ||
576 !strcmp(atype, "KERBEROS"))
577 return get_ace_internal(atype, aid, action, actarg);
580 if (!strcmp(atype, "RLIST"))
582 sq_save_data(sq, (char *)aid);
583 /* get all the list_id's of containing lists */
584 EXEC SQL DECLARE csr107 CURSOR FOR
585 SELECT list_id FROM imembers
586 WHERE member_type = 'LIST' AND member_id = :aid;
589 EXEC SQL OPEN csr107;
594 EXEC SQL FETCH csr107 INTO :listid;
597 sq_save_unique_data(sq, (char *)listid);
599 EXEC SQL CLOSE csr107;
600 /* now process each one */
601 while (sq_get_data(sq, &id))
603 if (get_ace_internal("LIST", id, action, actarg) == MR_SUCCESS)
608 if (!strcmp(atype, "RUSER"))
610 EXEC SQL DECLARE csr108 CURSOR FOR
611 SELECT list_id FROM imembers
612 WHERE member_type = 'USER' AND member_id = :aid;
615 EXEC SQL OPEN csr108;
620 EXEC SQL FETCH csr108 INTO :listid;
623 sq_save_data(sq, (char *)listid);
625 EXEC SQL CLOSE csr108;
626 /* now process each one */
627 while (sq_get_data(sq, &id))
629 if (get_ace_internal("LIST", id, action, actarg) == MR_SUCCESS)
632 if (get_ace_internal("USER", aid, action, actarg) == MR_SUCCESS)
636 if (!strcmp(atype, "RKERBEROS"))
638 EXEC SQL DECLARE csr109 CURSOR FOR
639 SELECT list_id FROM imembers
640 WHERE member_type = 'KERBEROS' AND member_id = :aid;
643 EXEC SQL OPEN csr109;
648 EXEC SQL FETCH csr109 INTO :listid;
651 sq_save_data(sq, (char *)listid);
653 EXEC SQL CLOSE csr109;
654 /* now process each one */
655 while (sq_get_data(sq, &id))
657 if (get_ace_internal("LIST", id, action, actarg) == MR_SUCCESS)
660 if (get_ace_internal("KERBEROS", aid, action, actarg) == MR_SUCCESS)
673 /* This looks up a single list or user for ace use. atype must be "USER"
674 * or "LIST", and aid is the ID of the corresponding object. This is used
675 * by get_ace_use above.
678 int get_ace_internal(char *atype, int aid, int (*action)(), int actarg)
682 EXEC SQL BEGIN DECLARE SECTION;
683 char name[33], *type = atype;
685 EXEC SQL END DECLARE SECTION;
688 if (!strcmp(atype, "LIST"))
690 rargv[0] = "FILESYS";
691 EXEC SQL DECLARE csr110 CURSOR FOR
692 SELECT label FROM filesys
696 EXEC SQL OPEN csr110;
701 EXEC SQL FETCH csr110 INTO :name;
704 (*action)(2, rargv, actarg);
707 EXEC SQL CLOSE csr110;
710 EXEC SQL DECLARE csr111 CURSOR FOR
711 SELECT capability FROM capacls
715 EXEC SQL OPEN csr111;
720 EXEC SQL FETCH csr111 INTO :name;
723 (*action)(2, rargv, actarg);
726 EXEC SQL CLOSE csr111;
728 else if (!strcmp(atype, "USER"))
730 rargv[0] = "FILESYS";
731 EXEC SQL DECLARE csr112 CURSOR FOR
732 SELECT label FROM filesys
736 EXEC SQL OPEN csr112;
741 EXEC SQL FETCH csr112 INTO :name;
744 (*action)(2, rargv, actarg);
747 EXEC SQL CLOSE csr112;
751 EXEC SQL DECLARE csr113 CURSOR FOR
752 SELECT name FROM list
753 WHERE acl_type = :type AND acl_id = :id;
756 EXEC SQL OPEN csr113;
761 EXEC SQL FETCH csr113 INTO :name;
764 (*action)(2, rargv, actarg);
767 EXEC SQL CLOSE csr113;
769 rargv[0] = "SERVICE";
770 EXEC SQL DECLARE csr114 CURSOR FOR
771 SELECT name FROM servers
772 WHERE acl_type = :type AND acl_id = :id;
775 EXEC SQL OPEN csr114;
780 EXEC SQL FETCH csr114 INTO :name;
783 (*action)(2, rargv, actarg);
786 EXEC SQL CLOSE csr114;
788 rargv[0] = "HOSTACCESS";
789 EXEC SQL DECLARE csr115 CURSOR FOR
790 SELECT name FROM machine m, hostaccess ha
791 WHERE m.mach_id = ha.mach_id AND ha.acl_type = :type
795 EXEC SQL OPEN csr115;
800 EXEC SQL FETCH csr115 INTO :name;
803 (*action)(2, rargv, actarg);
806 EXEC SQL CLOSE csr115;
809 EXEC SQL DECLARE csr116 CURSOR FOR
810 SELECT class FROM zephyr z
811 WHERE z.xmt_type = :type AND z.xmt_id = :id
812 OR z.sub_type = :type AND z.sub_id = :id
813 OR z.iws_type = :type AND z.iws_id = :id
814 OR z.iui_type = :type AND z.iui_id = :id;
817 EXEC SQL OPEN csr116;
822 EXEC SQL FETCH csr116 INTO :name;
825 (*action)(2, rargv, actarg);
828 EXEC SQL CLOSE csr116;
836 /* get_lists_of_member - given a type and a name, return the name and flags
837 * of all of the lists of the given member. The member_type is one of
838 * "LIST", "USER", "STRING", "RLIST", "RUSER", or "RSTRING" in argv[0],
839 * and argv[1] will contain the ID of the entity in question. The R*
840 * types mean to recursively look at every containing list, not just
841 * when the object in question is a direct member.
844 int get_lists_of_member(struct query *q, char *argv[], client *cl,
845 int (*action)(), int actarg)
847 int found = 0, direct = 1;
849 EXEC SQL BEGIN DECLARE SECTION;
852 char name[33], active[5], public[5], hidden[5], maillist[5], grouplist[5];
853 EXEC SQL END DECLARE SECTION;
856 aid = *(int *)argv[1];
857 if (!strcmp(atype, "RLIST"))
862 if (!strcmp(atype, "RUSER"))
867 if (!strcmp(atype, "RSTRING"))
872 if (!strcmp(atype, "RKERBEROS"))
883 rargv[5] = grouplist;
886 EXEC SQL DECLARE csr117a CURSOR FOR
887 SELECT l.name, l.active, l.publicflg, l.hidden, l.maillist, l.grouplist
888 FROM list l, imembers im
889 WHERE l.list_id = im.list_id AND im.direct = 1
890 AND im.member_type = :atype AND im.member_id = :aid;
893 EXEC SQL OPEN csr117a;
898 EXEC SQL FETCH csr117a
899 INTO :name, :active, :public, :hidden, :maillist, :grouplist;
902 (*action)(6, rargv, actarg);
905 EXEC SQL CLOSE csr117a;
909 EXEC SQL DECLARE csr117b CURSOR FOR
910 SELECT l.name, l.active, l.publicflg, l.hidden, l.maillist, l.grouplist
911 FROM list l, imembers im
912 WHERE l.list_id = im.list_id
913 AND im.member_type = :atype AND im.member_id = :aid;
916 EXEC SQL OPEN csr117b;
921 EXEC SQL FETCH csr117b
922 INTO :name, :active, :public, :hidden, :maillist, :grouplist;
925 (*action)(6, rargv, actarg);
928 EXEC SQL CLOSE csr117b;
939 /* qualified_get_lists: passed "TRUE", "FALSE", or "DONTCARE" for each of
940 * the five flags associated with each list. It will return the name of
941 * each list that meets the quailifications. It does this by building a
942 * where clause based on the arguments, then doing a retrieve.
945 static char *lflags[5] = { "active", "publicflg", "hidden", "maillist", "grouplist" };
947 int qualified_get_lists(struct query *q, char *argv[], client *cl,
948 int (*action)(), int actarg)
950 return qualified_get(q, argv, action, actarg, "l.list_id != 0",
951 "l", "name", lflags);
955 /* get_members_of_list - this gets only direct members */
957 int get_members_of_list(struct query *q, char *argv[], client *cl,
958 int (*action)(), int actarg)
960 return gmol_internal(q, argv, cl, action, actarg, 1);
963 /* get_end_members_of_list - this gets direct or indirect members */
965 int get_end_members_of_list(struct query *q, char *argv[], client *cl,
966 int (*action)(), int actarg)
968 return gmol_internal(q, argv, cl, action, actarg, 0);
971 /** gmol_internal - optimized query for retrieval of list members
972 ** used by both get_members_of_list and get_end_members_of_list
978 ** - retrieve USER members, then LIST members, then STRING members
981 int gmol_internal(struct query *q, char *argv[], client *cl,
982 int (*action)(), int actarg, int flag)
984 EXEC SQL BEGIN DECLARE SECTION;
985 int list_id, member_id, direct;
986 char member_name[129], member_type[9];
987 EXEC SQL END DECLARE SECTION;
991 /* true/false flag indicates whether to display only direct members. */
997 list_id = *(int *)argv[0];
999 targv[1] = member_name;
1001 EXEC SQL DECLARE csr119 CURSOR FOR
1002 SELECT u.login FROM users u, imembers im
1003 WHERE im.list_id = :list_id AND im.member_type = 'USER'
1004 AND im.member_id = u.users_id AND im.direct > :direct
1008 EXEC SQL OPEN csr119;
1013 EXEC SQL FETCH csr119 INTO :member_name;
1016 (*action)(2, targv, actarg);
1018 EXEC SQL CLOSE csr119;
1023 EXEC SQL DECLARE csr120 CURSOR FOR
1024 SELECT l.name FROM list l, imembers im
1025 WHERE im.list_id = :list_id AND im.member_type = 'LIST'
1026 AND im.member_id = l.list_id AND im.direct > :direct
1030 EXEC SQL OPEN csr120;
1035 EXEC SQL FETCH csr120 INTO :member_name;
1038 (*action)(2, targv, actarg);
1040 EXEC SQL CLOSE csr120;
1044 targv[0] = "STRING";
1045 EXEC SQL DECLARE csr121 CURSOR FOR
1046 SELECT str.string FROM strings str, imembers im
1047 WHERE im.list_id = :list_id AND im.member_type = 'STRING'
1048 AND im.member_id = str.string_id AND im.direct > :direct
1052 EXEC SQL OPEN csr121;
1057 EXEC SQL FETCH csr121 INTO :member_name;
1060 (*action)(2, targv, actarg);
1062 EXEC SQL CLOSE csr121;
1066 targv[0] = "KERBEROS";
1067 EXEC SQL DECLARE csr122 CURSOR FOR
1068 SELECT str.string FROM strings str, imembers im
1069 WHERE im.list_id = :list_id AND im.member_type = 'KERBEROS'
1070 AND im.member_id = str.string_id
1071 AND im.direct > :direct
1075 EXEC SQL OPEN csr122;
1080 EXEC SQL FETCH csr122 INTO :member_name;
1083 (*action)(2, targv, actarg);
1085 EXEC SQL CLOSE csr122;
1093 /* count_members_of_list: this is a simple query, but it cannot be done
1094 * through the dispatch table.
1097 int count_members_of_list(struct query *q, char *argv[], client *cl,
1098 int (*action)(), int actarg)
1100 EXEC SQL BEGIN DECLARE SECTION;
1102 EXEC SQL END DECLARE SECTION;
1103 char *rargv[1], countbuf[5];
1105 list = *(int *)argv[0];
1106 rargv[0] = countbuf;
1107 EXEC SQL SELECT count (*) INTO :ct FROM imembers
1108 WHERE list_id = :list AND direct = 1;
1111 sprintf(countbuf, "%d", ct);
1112 (*action)(1, rargv, actarg);
1117 /* qualified_get_server: passed "TRUE", "FALSE", or "DONTCARE" for each of
1118 * the three flags associated with each service. It will return the name of
1119 * each service that meets the quailifications. It does this by building a
1120 * where clause based on the arguments, then doing a retrieve.
1123 static char *sflags[3] = { "enable", "inprogress", "harderror" };
1125 int qualified_get_server(struct query *q, char *argv[], client *cl,
1126 int (*action)(), int actarg)
1128 return qualified_get(q, argv, action, actarg, "s.name is not null",
1129 "s", "name", sflags);
1130 /* of course, name will never be null, but we need something there
1131 to make qualified_get happy */
1135 /* generic qualified get routine, used by qualified_get_lists,
1136 * qualified_get_server, and qualified_get_serverhost.
1138 * start - a simple where clause, must not be empty
1139 * range - the name of the range variable
1140 * field - the field to return
1141 * flags - an array of strings, names of the flag variables
1144 int qualified_get(struct query *q, char *argv[], int (*action)(), int actarg,
1145 char *start, char *range, char *field, char *flags[])
1151 strcpy(qual, start);
1152 for (i = 0; i < q->argc; i++)
1154 if (!strcmp(argv[i], "TRUE"))
1156 sprintf(buf, " AND %s.%s != 0", range, flags[i]);
1159 else if (!strcmp(argv[i], "FALSE"))
1161 sprintf(buf, " AND %s.%s = 0", range, flags[i]);
1166 sprintf(stmt_buf, "SELECT %s.%s FROM %s %s WHERE %s", range, field,
1167 table_name[q->rtable], range, qual);
1168 return do_for_all_rows(stmt_buf, 1, action, actarg);
1172 /* qualified_get_serverhost: passed "TRUE", "FALSE", or "DONTCARE" for each of
1173 * the five flags associated with each serverhost. It will return the name of
1174 * each service and host that meets the quailifications. It does this by
1175 * building a where clause based on the arguments, then doing a retrieve.
1178 static char *shflags[6] = { "service", "enable", "override", "success",
1179 "inprogress", "hosterror" };
1181 int qualified_get_serverhost(struct query *q, char *argv[], client *cl,
1182 int (*action)(), int actarg)
1184 char qual[256], buf[32];
1187 sprintf(qual, "m.mach_id = sh.mach_id AND sh.service = UPPER('%s')",
1189 for (i = 1; i < q->argc; i++)
1191 if (!strcmp(argv[i], "TRUE"))
1193 sprintf(buf, " AND sh.%s != 0", shflags[i]);
1196 else if (!strcmp(argv[i], "FALSE"))
1198 sprintf(buf, " AND sh.%s = 0", shflags[i]);
1203 sprintf(stmt_buf, "SELECT sh.service, m.name FROM serverhosts sh, "
1204 "machine m WHERE %s", qual);
1205 return do_for_all_rows(stmt_buf, 2, action, actarg);
1209 /* register_user - change user's login name and allocate a pobox, group,
1210 * filesystem, and quota for them. The user's status must start out as 0,
1211 * and is left as 2. Arguments are: user's UID, new login name, and user's
1212 * type for filesystem allocation (MR_FS_STUDENT, MR_FS_FACULTY,
1213 * MR_FS_STAFF, MR_FS_MISC).
1216 int register_user(struct query *q, char **argv, client *cl)
1218 EXEC SQL BEGIN DECLARE SECTION;
1219 char *login, *entity, directory[129], machname[33];
1220 int who, rowcount, mid, uid, users_id, utype, list_id;
1221 int ostatus, nstatus, gidval, fsidval;
1222 static int m_id = 0, def_quota = 0;
1223 EXEC SQL END DECLARE SECTION;
1224 char buffer[256], *aargv[3];
1226 entity = cl->entity;
1227 who = cl->client_id;
1229 uid = atoi(argv[0]);
1231 utype = atoi(argv[2]);
1234 EXEC SQL SELECT users_id, status INTO :users_id, :ostatus
1236 WHERE unix_uid = :uid AND (status = 0 OR status = 5 OR status = 6);
1238 if (sqlca.sqlerrd[2] == 0)
1240 if (sqlca.sqlerrd[2] > 1)
1241 return MR_NOT_UNIQUE;
1243 /* check new login name */
1244 EXEC SQL SELECT COUNT(login) INTO :rowcount FROM users
1245 WHERE login = :login AND users_id != :users_id;
1250 EXEC SQL SELECT COUNT(name) INTO :rowcount FROM list
1251 WHERE name = :login;
1256 EXEC SQL SELECT COUNT(label) INTO :rowcount FROM filesys
1257 WHERE label = :login;
1262 EXEC SQL SELECT COUNT(name) INTO :rowcount FROM alias
1263 WHERE name = :login AND type = 'FILESYS';
1268 com_err(whoami, 0, "login name OK");
1270 /* choose place for pobox, put in mid */
1271 EXEC SQL DECLARE csr130 CURSOR FOR
1272 SELECT sh.mach_id, m.name FROM serverhosts sh, machine m
1273 WHERE sh.service = 'POP' AND sh.mach_id = m.mach_id
1274 AND sh.value2 - sh.value1 = (SELECT MAX(value2 - value1) FROM serverhosts
1275 WHERE service = 'POP');
1278 EXEC SQL OPEN csr130;
1281 EXEC SQL FETCH csr130 INTO :mid, :machname;
1282 if (sqlca.sqlerrd[2] == 0)
1284 EXEC SQL CLOSE csr130;
1291 EXEC SQL CLOSE csr130;
1296 /* change login name, set pobox */
1297 sprintf(buffer, "u.users_id = %d", users_id);
1298 incremental_before(USERS_TABLE, buffer, 0);
1300 if (ostatus == 5 || ostatus == 6)
1302 EXEC SQL UPDATE users SET login = :login, status = :nstatus,
1303 modtime = SYSDATE, modby = :who, modwith = :entity, potype = 'POP',
1304 pop_id = :mid, pmodtime = SYSDATE, pmodby = :who, pmodwith = :entity
1305 WHERE users_id = :users_id;
1309 if (sqlca.sqlerrd[2] != 1)
1311 set_pop_usage(mid, 1);
1312 com_err(whoami, 0, "set login name to %s and pobox to %s", login,
1314 incremental_after(USERS_TABLE, buffer, 0);
1318 /* Cell Name (I know, it shouldn't be hard coded...) */
1319 strcpy(machname, "ATHENA.MIT.EDU");
1320 EXEC SQL SELECT mach_id INTO :m_id FROM machine
1321 WHERE name = :machname;
1324 EXEC SQL SELECT list_id INTO :list_id FROM list
1325 WHERE name = 'wheel';
1327 /* create filesystem */
1328 if (set_next_object_id("filsys_id", FILESYS_TABLE, 0))
1330 incremental_clear_before();
1331 if (islower(login[0]) && islower(login[1]))
1333 sprintf(directory, "/afs/athena.mit.edu/user/%c/%c/%s",
1334 login[0], login[1], login);
1337 sprintf(directory, "/afs/athena.mit.edu/user/other/%s", login);
1339 EXEC SQL SELECT value INTO :fsidval FROM numvalues
1340 WHERE numvalues.name = 'filsys_id';
1341 EXEC SQL INSERT INTO filesys
1342 (filsys_id, phys_id, label, type, mach_id, name,
1343 mount, rwaccess, comments, owner, owners, createflg,
1344 lockertype, modtime, modby, modwith)
1346 (:fsidval, 0, :login, 'AFS', :m_id, :directory,
1347 '/mit/' || :login, 'w', 'User Locker', :users_id, :list_id, 1,
1348 'HOMEDIR', SYSDATE, :who, :entity);
1352 if (sqlca.sqlerrd[2] != 1)
1354 sprintf(buffer, "fs.filsys_id = %d", fsidval);
1355 incremental_after(FILESYS_TABLE, buffer, 0);
1360 EXEC SQL SELECT value INTO :def_quota FROM numvalues
1361 WHERE name = 'def_quota';
1364 if (sqlca.sqlerrd[2] != 1)
1367 incremental_clear_before();
1368 EXEC SQL INSERT INTO quota
1369 (entity_id, filsys_id, type, quota, phys_id, modtime, modby, modwith)
1370 VALUES (0, :fsidval, 'ANY', :def_quota, 0, SYSDATE, :who, :entity);
1373 if (sqlca.sqlerrd[2] != 1)
1378 sprintf(buffer, "q.entity_id = 0 and q.filsys_id = %d and q.type = 'ANY'",
1380 incremental_after(QUOTA_TABLE, buffer, aargv);
1381 com_err(whoami, 0, "quota of %d assigned", def_quota);
1385 cache_entry(login, USERS_TABLE, users_id);
1387 EXEC SQL UPDATE tblstats SET updates = updates + 1, modtime = SYSDATE
1388 WHERE table_name = 'users';
1389 EXEC SQL UPDATE tblstats SET appends = appends + 1, modtime = SYSDATE
1390 WHERE table_name = 'filesys' OR table_name = 'quota';
1398 /** set_pop_usage - incr/decr usage count for pop server in serverhosts talbe
1402 ** delta (will be +/- 1)
1405 ** - incr/decr value field in serverhosts table for pop/mach_id
1409 int set_pop_usage(id, cnt)
1412 EXEC SQL BEGIN DECLARE SECTION;
1413 int iid = id, icnt = cnt;
1414 EXEC SQL END DECLARE SECTION;
1416 EXEC SQL UPDATE serverhosts SET value1 = value1 + :icnt
1417 WHERE serverhosts.service = 'POP' AND serverhosts.mach_id = :iid;