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(q, argv, cl)
55 EXEC SQL BEGIN DECLARE SECTION;
58 EXEC SQL END DECLARE SECTION;
62 user = *(int *)argv[0];
64 EXEC SQL SELECT pop_id, potype INTO :id, :potype FROM users
65 WHERE users_id = :user;
66 if (dbms_errno) return(mr_errcode);
67 if (!strcmp(strtrim(potype), "POP"))
68 set_pop_usage(id, -1);
70 if (!strcmp(argv[1], "POP")) {
71 status = name_to_id(box, MACHINE_TABLE, &id);
72 if (status == MR_NO_MATCH)
76 EXEC SQL UPDATE users SET potype = 'POP', pop_id = :id
77 WHERE users_id = :user;
79 } else if (!strcmp(argv[1], "SMTP")) {
80 if (strchr(box, '/') || strchr(box, '|'))
82 status = name_to_id(box, STRINGS_TABLE, &id);
83 if (status == MR_NO_MATCH) {
87 EXEC SQL UPDATE users SET potype='SMTP', box_id = :id
88 WHERE users_id = :user;
89 } else /* argv[1] == "NONE" */ {
90 EXEC SQL UPDATE users SET potype='NONE'
91 WHERE users_id = :user;
94 set_pobox_modtime(q, argv, cl);
95 EXEC SQL UPDATE tblstats SET updates = updates+1, modtime=SYSDATE
96 WHERE table_name='users';
97 if (dbms_errno) return(mr_errcode);
102 /* get_list_info: passed a wildcard list name, returns lots of stuff about
103 * each list. This is tricky: first build a queue of all requested
104 * data. Rest of processing consists of fixing gid, ace_name, and modby.
107 int get_list_info(q, aargv, cl, action, actarg)
111 int (*action)(), actarg;
114 EXEC SQL BEGIN DECLARE SECTION;
115 char *name, acl_type[9], listname[33], active[5], public[5], hidden[5];
116 char maillist[5], grouplist[5], gid_str[6], desc[256];
117 char modtime[27], modwith[9];
118 int id, rowcount, acl_id, hid, modby_id;
119 EXEC SQL END DECLARE SECTION;
120 int returned, status;
121 struct save_queue *sq, *sq_create();
123 returned = rowcount = 0;
125 convert_wildcards(name);
128 EXEC SQL DECLARE csr102 CURSOR FOR SELECT list_id FROM list
129 WHERE name LIKE :name ESCAPE '*';
132 EXEC SQL OPEN csr102;
137 EXEC SQL FETCH csr102 INTO :id;
138 if(sqlca.sqlcode!=0) break;
139 sq_save_data(sq, (char *)id);
142 EXEC SQL CLOSE csr102;
144 if (dbms_errno) return(mr_errcode);
148 argv[0] = listname; argv[1] = active; argv[2] = public; argv[3] = hidden;
149 argv[4] = maillist; argv[5] = grouplist; argv[6] = gid_str;
150 argv[7] = acl_type; argv[9] = desc; argv[10] = modtime; argv[12] = modwith;
152 while (sq_get_data(sq, &id)) {
156 EXEC SQL SELECT name, active, publicflg,
157 hidden, hidden, maillist, grouplist, gid,
158 acl_type, acl_id, description,
159 TO_CHAR(modtime, 'DD-mon-YYYY HH24:MI:SS'), modby, modwith
160 INTO :listname, :active, :public, :hidden, :hid, :maillist,
161 :grouplist, :gid_str, :acl_type, :acl_id, :desc,
162 :modtime, :modby_id, :modwith
163 FROM list WHERE list_id = :id;
165 if (dbms_errno) return(mr_errcode);
168 if (atoi(gid_str) == -1)
169 argv[6] = UNIQUE_GID;
172 if (!strcmp(acl_type, "LIST")) {
173 status = id_to_name(acl_id, LIST_TABLE, &argv[8]);
174 } else if (!strcmp(acl_type, "USER")) {
175 status = id_to_name(acl_id, USERS_TABLE, &argv[8]);
176 } else if (!strcmp(acl_type, "KERBEROS")) {
177 status = id_to_name(acl_id, STRINGS_TABLE, &argv[8]);
178 } else if (!strcmp(acl_type, "NONE")) {
181 argv[8] = strsave("NONE");
185 argv[8] = strsave("???");
187 if (status && status != MR_NO_MATCH) return(status);
189 argv[11] = malloc(0);
191 status = id_to_name(modby_id, USERS_TABLE, &argv[11]);
193 status = id_to_name(-modby_id, STRINGS_TABLE, &argv[11]);
194 if (status && status != MR_NO_MATCH) return(status);
196 mr_trim_args(q->vcnt, argv);
198 (*action)(q->vcnt, argv, actarg);
204 if (dbms_errno) return(mr_errcode);
209 /* Add_member_to_list: do list flattening as we go! MAXLISTDEPTH is
210 * how many different ancestors a member is allowed to have.
213 #define MAXLISTDEPTH 1024
215 int add_member_to_list(q, argv, cl)
220 EXEC SQL BEGIN DECLARE SECTION;
221 int id, lid, mid, error, who, ref, rowcnt;
222 char *mtype, dtype[9], *entity;
223 EXEC SQL END DECLARE SECTION;
224 int ancestors[MAXLISTDEPTH], aref[MAXLISTDEPTH], acount, a;
225 int descendants[MAXLISTDEPTH], dref[MAXLISTDEPTH], dcount, d;
227 char *dtypes[MAXLISTDEPTH];
228 char *iargv[3], *buf;
230 lid = *(int *)argv[0];
232 mid = *(int *)argv[2];
233 /* if the member is already a direct member of the list, punt */
234 EXEC SQL SELECT COUNT(list_id) INTO :rowcnt FROM imembers
235 WHERE list_id = :lid AND member_id = :mid
236 AND member_type = :mtype AND direct = 1;
239 if (!strcasecmp(mtype, "STRING")) {
241 status = id_to_name(mid, STRINGS_TABLE, &buf);
242 if (status) return(status);
243 if (strchr(buf, '/') || strchr(buf, '|')) {
253 EXEC SQL DECLARE csr103 CURSOR FOR
254 SELECT list_id, ref_count FROM imembers
255 WHERE member_id = :lid AND member_type='LIST';
258 EXEC SQL OPEN csr103;
262 EXEC SQL FETCH csr103 INTO :id, :ref;
263 if(sqlca.sqlcode != 0) break;
265 ancestors[acount++] = id;
266 if (acount >= MAXLISTDEPTH) break;
268 EXEC SQL CLOSE csr103;
269 if (dbms_errno) return(mr_errcode);
270 if (acount >= MAXLISTDEPTH) {
273 descendants[0] = mid;
278 if (!strcmp(mtype, "LIST")) {
279 EXEC SQL DECLARE csr104 CURSOR FOR
280 SELECT member_id, member_type, ref_count
282 WHERE list_id = :mid;
285 EXEC SQL OPEN csr104;
289 EXEC SQL FETCH csr104 INTO :id, :dtype, :ref;
290 if(sqlca.sqlcode != 0) break;
293 dtypes[dcount] = "LIST";
296 dtypes[dcount] = "USER";
299 dtypes[dcount] = "STRING";
302 dtypes[dcount] = "KERBEROS";
309 descendants[dcount++] = id;
310 if (dcount >= MAXLISTDEPTH) {
315 EXEC SQL CLOSE csr104;
316 if (dbms_errno) return(mr_errcode);
320 for (a = 0; a < acount; a++) {
322 for (d = 0; d < dcount; d++) {
323 mid = descendants[d];
325 if (mid == lid && !strcmp(mtype, "LIST")) {
328 EXEC SQL SELECT COUNT(ref_count) INTO :rowcnt
330 WHERE list_id = :lid AND member_id = :mid
331 AND member_type = :mtype;
332 ref = aref[a] * dref[d];
334 if (a == 0 && d == 0) {
335 EXEC SQL UPDATE imembers
336 SET ref_count = ref_count+:ref, direct=1
337 WHERE list_id = :lid AND member_id = :mid
338 AND member_type = :mtype;
340 EXEC SQL UPDATE imembers
341 SET ref_count = ref_count+:ref
342 WHERE list_id = :lid AND member_id = :mid
343 AND member_type = :mtype;
346 incremental_clear_before();
347 if (a == 0 && d == 0) {
348 EXEC SQL INSERT INTO imembers
349 (list_id, member_id, direct, member_type, ref_count)
350 VALUES (:lid, :mid, 1, :mtype, 1);
352 EXEC SQL INSERT INTO imembers
353 (list_id, member_id, direct, member_type, ref_count)
354 VALUES (:lid, :mid, 0, :mtype, 1);
356 iargv[0] = (char *)lid;
358 iargv[2] = (char *)mid;
359 incremental_after(IMEMBERS_TABLE, 0, iargv);
363 lid = *(int *)argv[0];
367 SET modtime=SYSDATE, modby = :who, modwith = :entity
368 WHERE list_id = :lid;
369 if (dbms_errno) return(mr_errcode);
374 /* Delete_member_from_list: do list flattening as we go!
377 int delete_member_from_list(q, argv, cl)
382 EXEC SQL BEGIN DECLARE SECTION;
383 int id, lid, mid, cnt, error, who, ref;
384 char *mtype, dtype[9], *entity;
385 EXEC SQL END DECLARE SECTION;
386 int ancestors[MAXLISTDEPTH], aref[MAXLISTDEPTH], acount, a;
387 int descendants[MAXLISTDEPTH], dref[MAXLISTDEPTH], dcount, d;
388 char *dtypes[MAXLISTDEPTH];
391 lid = *(int *)argv[0];
393 mid = *(int *)argv[2];
394 /* if the member is not a direct member of the list, punt */
395 EXEC SQL SELECT COUNT(list_id) INTO :cnt FROM imembers
396 WHERE list_id = :lid AND member_id = :mid
397 AND member_type = :mtype AND direct = 1;
398 if (dbms_errno) return(mr_errcode);
404 EXEC SQL DECLARE csr105 CURSOR FOR
405 SELECT list_id, ref_count FROM imembers
406 WHERE member_id = :lid AND member_type = 'LIST';
409 EXEC SQL OPEN csr105;
413 EXEC SQL FETCH csr105 INTO :id, :ref;
414 if(sqlca.sqlcode!=0) break;
416 ancestors[acount++] = id;
417 if (acount >= MAXLISTDEPTH) break;
419 EXEC SQL CLOSE csr105;
422 if (acount >= MAXLISTDEPTH)
424 descendants[0] = mid;
429 if (!strcmp(mtype, "LIST")) {
430 EXEC SQL DECLARE csr106 CURSOR FOR
431 SELECT member_id, member_type, ref_count FROM imembers
432 WHERE list_id = :mid;
435 EXEC SQL OPEN csr106;
439 EXEC SQL FETCH csr106 INTO :id, :dtype, :ref;
440 if(sqlca.sqlcode!=0) break;
443 dtypes[dcount] = "LIST";
446 dtypes[dcount] = "USER";
449 dtypes[dcount] = "STRING";
452 dtypes[dcount] = "KERBEROS";
459 descendants[dcount++] = id;
460 if (dcount >= MAXLISTDEPTH) break;
462 EXEC SQL CLOSE csr106;
468 for (a = 0; a < acount; a++) {
470 for (d = 0; d < dcount; d++) {
471 mid = descendants[d];
473 if (mid == lid && !strcmp(mtype, "LIST")) {
476 EXEC SQL SELECT ref_count INTO :cnt FROM imembers
477 WHERE list_id = :lid AND member_id = :mid AND member_type = :mtype;
478 ref = aref[a] * dref[d];
480 iargv[0] = (char *)lid;
482 iargv[2] = (char *)mid;
483 incremental_before(IMEMBERS_TABLE, 0, iargv);
484 EXEC SQL DELETE FROM imembers
485 WHERE list_id = :lid AND member_id = :mid
486 AND member_type= :mtype;
487 incremental_clear_after();
488 } else if (a == 0 && d == 0) {
489 EXEC SQL UPDATE imembers
490 SET ref_count = ref_count - :ref, direct = 0
491 WHERE list_id = :lid AND member_id = :mid
492 AND member_type = :mtype;
494 EXEC SQL UPDATE imembers
495 SET ref_count = ref_count - :ref
496 WHERE list_id = :lid AND member_id = :mid
497 AND member_type = :mtype;
501 lid = *(int *)argv[0];
504 EXEC SQL UPDATE list SET modtime = SYSDATE, modby = :who, modwith = :entity
505 WHERE list_id = :lid;
506 if (dbms_errno) return(mr_errcode);
511 /* get_ace_use - given a type and a name, return a type and a name.
512 * The ace_type is one of "LIST", "USER", "RLIST", or "RUSER" in argv[0],
513 * and argv[1] will contain the ID of the entity in question. The R*
514 * types mean to recursively look at every containing list, not just
515 * when the object in question is a direct member. On return, the
516 * usage type will be one of LIST, SERVICE, FILESYS, QUOTA, QUERY, or ZEPHYR.
519 int get_ace_use(q, argv, cl, action, actarg)
523 int (*action)(), actarg;
526 EXEC SQL BEGIN DECLARE SECTION;
529 EXEC SQL END DECLARE SECTION;
530 struct save_queue *sq, *sq_create();
533 aid = *(int *)argv[1];
534 if (!strcmp(atype, "LIST") || !strcmp(atype, "USER") ||
535 !strcmp(atype, "KERBEROS")) {
536 return(get_ace_internal(atype, aid, action, actarg));
540 if (!strcmp(atype, "RLIST")) {
541 sq_save_data(sq, (char *)aid);
542 /* get all the list_id's of containing lists */
543 EXEC SQL DECLARE csr107 CURSOR FOR
544 SELECT list_id FROM imembers
545 WHERE member_type='LIST' AND member_id = :aid;
548 EXEC SQL OPEN csr107;
552 EXEC SQL FETCH csr107 INTO :listid;
553 if(sqlca.sqlcode != 0) break;
554 sq_save_unique_data(sq, (char *)listid);
556 EXEC SQL CLOSE csr107;
557 /* now process each one */
558 while (sq_get_data(sq, &id)) {
559 if (get_ace_internal("LIST", id, action, actarg) == MR_SUCCESS)
564 if (!strcmp(atype, "RUSER")) {
565 EXEC SQL DECLARE csr108 CURSOR FOR
566 SELECT list_id FROM imembers
567 WHERE member_type='USER' AND member_id = :aid;
570 EXEC SQL OPEN csr108;
574 EXEC SQL FETCH csr108 INTO :listid;
575 if(sqlca.sqlcode != 0) break;
576 sq_save_data(sq, (char *)listid);
578 EXEC SQL CLOSE csr108;
579 /* now process each one */
580 while (sq_get_data(sq, &id)) {
581 if (get_ace_internal("LIST", id, action, actarg) == MR_SUCCESS)
584 if (get_ace_internal("USER", aid, action, actarg) == MR_SUCCESS)
588 if (!strcmp(atype, "RKERBEROS")) {
589 EXEC SQL DECLARE csr109 CURSOR FOR
590 SELECT list_id FROM imembers
591 WHERE member_type='KERBEROS' AND member_id = :aid;
594 EXEC SQL OPEN csr109;
598 EXEC SQL FETCH csr109 INTO :listid;
599 if(sqlca.sqlcode != 0) break;
600 sq_save_data(sq, (char*)listid);
602 EXEC SQL CLOSE csr109;
603 /* now process each one */
604 while (sq_get_data(sq, &id)) {
605 if (get_ace_internal("LIST", id, action, actarg) == MR_SUCCESS)
608 if (get_ace_internal("KERBEROS", aid, action, actarg) == MR_SUCCESS)
613 if (dbms_errno) return(mr_errcode);
614 if (!found) return(MR_NO_MATCH);
619 /* This looks up a single list or user for ace use. atype must be "USER"
620 * or "LIST", and aid is the ID of the corresponding object. This is used
621 * by get_ace_use above.
624 int get_ace_internal(atype, aid, action, actarg)
625 EXEC SQL BEGIN DECLARE SECTION;
627 EXEC SQL END DECLARE SECTION;
628 int aid, (*action)(), actarg;
632 EXEC SQL BEGIN DECLARE SECTION;
634 EXEC SQL END DECLARE SECTION;
637 if (!strcmp(atype, "LIST")) {
638 rargv[0] = "FILESYS";
639 EXEC SQL DECLARE csr110 CURSOR FOR
640 SELECT label FROM filesys
644 EXEC SQL OPEN csr110;
648 EXEC SQL FETCH csr110 INTO :name;
649 if(sqlca.sqlcode != 0) break;
650 (*action)(2, rargv, actarg);
653 EXEC SQL CLOSE csr110;
656 EXEC SQL DECLARE csr111 CURSOR FOR
657 SELECT capability FROM capacls
658 WHERE list_id = :aid ;
661 EXEC SQL OPEN csr111;
665 EXEC SQL FETCH csr111 INTO :name ;
666 if(sqlca.sqlcode != 0) break;
667 (*action)(2, rargv, actarg);
670 EXEC SQL CLOSE csr111;
671 } else if (!strcmp(atype, "USER")) {
672 rargv[0] = "FILESYS";
673 EXEC SQL DECLARE csr112 CURSOR FOR
674 SELECT label FROM filesys
678 EXEC SQL OPEN csr112;
682 EXEC SQL FETCH csr112 INTO :name ;
683 if(sqlca.sqlcode != 0) break;
684 (*action)(2, rargv, actarg);
687 EXEC SQL CLOSE csr112;
691 EXEC SQL DECLARE csr113 CURSOR FOR
692 SELECT name FROM list
693 WHERE acl_type = :atype AND acl_id = :aid;
696 EXEC SQL OPEN csr113;
700 EXEC SQL FETCH csr113 INTO :name;
701 if(sqlca.sqlcode != 0) break;
702 (*action)(2, rargv, actarg);
705 EXEC SQL CLOSE csr113;
707 rargv[0] = "SERVICE";
708 EXEC SQL DECLARE csr114 CURSOR FOR
709 SELECT name FROM servers
710 WHERE acl_type = :atype AND acl_id = :aid;
713 EXEC SQL OPEN csr114;
717 EXEC SQL FETCH csr114 INTO :name;
718 if(sqlca.sqlcode != 0) break;
719 (*action)(2, rargv, actarg);
722 EXEC SQL CLOSE csr114;
724 rargv[0] = "HOSTACCESS";
725 EXEC SQL DECLARE csr115 CURSOR FOR
726 SELECT name FROM machine m, hostaccess ha
727 WHERE m.mach_id = ha.mach_id AND ha.acl_type = :atype
728 AND ha.acl_id = :aid;
731 EXEC SQL OPEN csr115;
735 EXEC SQL FETCH csr115 INTO :name;
736 if(sqlca.sqlcode != 0) break;
737 (*action)(2, rargv, actarg);
740 EXEC SQL CLOSE csr115;
743 EXEC SQL DECLARE csr116 CURSOR FOR
744 SELECT class FROM zephyr z
745 WHERE z.xmt_type = :atype AND z.xmt_id = :aid
746 OR z.sub_type = :atype AND z.sub_id = :aid
747 OR z.iws_type = :atype AND z.iws_id = :aid
748 OR z.iui_type = :atype AND z.iui_id = :aid;
751 EXEC SQL OPEN csr116;
755 EXEC SQL FETCH csr116 INTO :name;
756 if(sqlca.sqlcode != 0) break;
757 (*action)(2, rargv, actarg);
760 EXEC SQL CLOSE csr116;
762 if (!found) return(MR_NO_MATCH);
767 /* get_lists_of_member - given a type and a name, return the name and flags
768 * of all of the lists of the given member. The member_type is one of
769 * "LIST", "USER", "STRING", "RLIST", "RUSER", or "RSTRING" in argv[0],
770 * and argv[1] will contain the ID of the entity in question. The R*
771 * types mean to recursively look at every containing list, not just
772 * when the object in question is a direct member.
775 int get_lists_of_member(q, argv, cl, action, actarg)
779 int (*action)(), actarg;
781 int found = 0, direct = 1;
783 EXEC SQL BEGIN DECLARE SECTION;
786 char name[33], active[5], public[5], hidden[5], maillist[5], grouplist[5];
787 EXEC SQL END DECLARE SECTION;
790 aid = *(int *)argv[1];
791 if (!strcmp(atype, "RLIST")) {
795 if (!strcmp(atype, "RUSER")) {
799 if (!strcmp(atype, "RSTRING")) {
803 if (!strcmp(atype, "RKERBEROS")) {
813 rargv[5] = grouplist;
815 EXEC SQL DECLARE csr117a CURSOR FOR
816 SELECT l.name, l.active, l.publicflg, l.hidden,
817 l.maillist, l.grouplist
818 FROM list l, imembers im
819 WHERE l.list_id = im.list_id AND im.direct = 1
820 AND im.member_type = :atype AND im.member_id = :aid;
823 EXEC SQL OPEN csr117a;
827 EXEC SQL FETCH csr117a
828 INTO :name, :active, :public, :hidden, :maillist, :grouplist;
829 if(sqlca.sqlcode != 0) break;
830 (*action)(6, rargv, actarg);
833 EXEC SQL CLOSE csr117a;
835 EXEC SQL DECLARE csr117b CURSOR FOR
836 SELECT l.name, l.active, l.publicflg, l.hidden,
837 l.maillist, l.grouplist
838 FROM list l, imembers im
839 WHERE l.list_id = im.list_id
840 AND im.member_type = :atype AND im.member_id = :aid;
843 EXEC SQL OPEN csr117b;
847 EXEC SQL FETCH csr117b
848 INTO :name, :active, :public, :hidden, :maillist, :grouplist;
849 if(sqlca.sqlcode != 0) break;
850 (*action)(6, rargv, actarg);
853 EXEC SQL CLOSE csr117b;
856 if (dbms_errno) return(mr_errcode);
857 if (!found) return(MR_NO_MATCH);
862 /* qualified_get_lists: passed "TRUE", "FALSE", or "DONTCARE" for each of
863 * the five flags associated with each list. It will return the name of
864 * each list that meets the quailifications. It does this by building a
865 * where clause based on the arguments, then doing a retrieve.
868 static char *lflags[5] = { "active", "publicflg", "hidden", "maillist", "grouplist" };
870 int qualified_get_lists(q, argv, cl, action, actarg)
874 int (*action)(), actarg;
876 return(qualified_get(q, argv, action, actarg, "l.list_id != 0",
877 "l", "name", lflags));
881 /* get_members_of_list - this gets only direct members */
883 int get_members_of_list(q, argv, cl, action, actarg)
887 int (*action)(), actarg;
889 return(gmol_internal(q, argv, cl, action, actarg, 1));
892 /* get_end_members_of_list - this gets direct or indirect members */
894 int get_end_members_of_list(q, argv, cl, action, actarg)
898 int (*action)(), actarg;
900 return(gmol_internal(q, argv, cl, action, actarg, 0));
903 /** gmol_internal - optimized query for retrieval of list members
904 ** used by both get_members_of_list and get_end_members_of_list
910 ** - retrieve USER members, then LIST members, then STRING members
913 int gmol_internal(q, argv, cl, action, actarg, flag)
917 int (*action)(), actarg, flag;
919 EXEC SQL BEGIN DECLARE SECTION;
920 int list_id, member_id, direct;
921 char member_name[129], member_type[9];
922 EXEC SQL END DECLARE SECTION;
926 /* true/false flag indicates whether to display only direct members. */
932 list_id = *(int *)argv[0];
934 targv[1] = member_name;
936 EXEC SQL DECLARE csr119 CURSOR FOR
937 SELECT u.login FROM users u, imembers im
938 WHERE im.list_id = :list_id AND im.member_type = 'USER'
939 AND im.member_id = u.users_id AND im.direct > :direct
943 EXEC SQL OPEN csr119;
947 EXEC SQL FETCH csr119 INTO :member_name;
948 if(sqlca.sqlcode != 0) break;
949 (*action)(2, targv, actarg);
951 EXEC SQL CLOSE csr119;
952 if (dbms_errno) return(mr_errcode);
955 EXEC SQL DECLARE csr120 CURSOR FOR
956 SELECT l.name FROM list l, imembers im
957 WHERE im.list_id = :list_id AND im.member_type='LIST'
958 AND im.member_id = l.list_id AND im.direct > :direct
962 EXEC SQL OPEN csr120;
966 EXEC SQL FETCH csr120 INTO :member_name;
967 if(sqlca.sqlcode != 0) break;
968 (*action)(2, targv, actarg);
970 EXEC SQL CLOSE csr120;
971 if (dbms_errno) return(mr_errcode);
974 EXEC SQL DECLARE csr121 CURSOR FOR
975 SELECT str.string FROM strings str, imembers im
976 WHERE im.list_id = :list_id AND im.member_type='STRING'
977 AND im.member_id = str.string_id AND im.direct > :direct
981 EXEC SQL OPEN csr121;
985 EXEC SQL FETCH csr121 INTO :member_name;
986 if(sqlca.sqlcode != 0) break;
987 (*action)(2, targv, actarg);
989 EXEC SQL CLOSE csr121;
990 if (dbms_errno) return(mr_errcode);
992 targv[0] = "KERBEROS";
993 EXEC SQL DECLARE csr122 CURSOR FOR
994 SELECT str.string FROM strings str, imembers im
995 WHERE im.list_id = :list_id AND im.member_type='KERBEROS'
996 AND im.member_id = str.string_id
997 AND im.direct > :direct
1001 EXEC SQL OPEN csr122;
1005 EXEC SQL FETCH csr122 INTO :member_name;
1006 if(sqlca.sqlcode != 0) break;
1007 (*action)(2, targv, actarg);
1009 EXEC SQL CLOSE csr122;
1010 if (dbms_errno) return(mr_errcode);
1016 /* count_members_of_list: this is a simple query, but it cannot be done
1017 * through the dispatch table.
1020 int count_members_of_list(q, argv, cl, action, actarg)
1024 int (*action)(), actarg;
1026 EXEC SQL BEGIN DECLARE SECTION;
1028 EXEC SQL END DECLARE SECTION;
1029 char *rargv[1], countbuf[5];
1031 list = *(int *)argv[0];
1032 rargv[0] = countbuf;
1033 EXEC SQL SELECT count (*) INTO :ct FROM imembers
1034 WHERE list_id = :list AND direct=1;
1035 if (dbms_errno) return(mr_errcode);
1036 sprintf(countbuf, "%d", ct);
1037 (*action)(1, rargv, actarg);
1042 /* qualified_get_server: passed "TRUE", "FALSE", or "DONTCARE" for each of
1043 * the three flags associated with each service. It will return the name of
1044 * each service that meets the quailifications. It does this by building a
1045 * where clause based on the arguments, then doing a retrieve.
1048 static char *sflags[3] = { "enable", "inprogress", "harderror" };
1050 int qualified_get_server(q, argv, cl, action, actarg)
1054 int (*action)(), actarg;
1056 return(qualified_get(q, argv, action, actarg, "s.name is not null",
1057 "s", "name", sflags));
1058 /* of course, name will never be null, but we need something there
1059 to make qualified_get happy */
1063 /* generic qualified get routine, used by qualified_get_lists,
1064 * qualified_get_server, and qualified_get_serverhost.
1066 * start - a simple where clause, must not be empty
1067 * range - the name of the range variable
1068 * field - the field to return
1069 * flags - an array of strings, names of the flag variables
1072 int qualified_get(q, argv, action, actarg, start, range, field, flags)
1074 char *argv[], *start, *range, *field, *flags[];
1075 int (*action)(), actarg;
1081 strcpy(qual, start);
1082 for (i = 0; i < q->argc; i++) {
1083 if (!strcmp(argv[i], "TRUE")) {
1084 sprintf(buf, " AND %s.%s != 0", range, flags[i]);
1085 (void) strcat(qual, buf);
1086 } else if (!strcmp(argv[i], "FALSE")) {
1087 sprintf(buf, " AND %s.%s = 0", range, flags[i]);
1088 (void) strcat(qual, buf);
1092 sprintf(stmt_buf,"SELECT %s.%s FROM %s %s WHERE %s",range,field,
1093 table_name[q->rtable],range,qual);
1094 return do_for_all_rows(stmt_buf, 1, action, actarg);
1098 /* qualified_get_serverhost: passed "TRUE", "FALSE", or "DONTCARE" for each of
1099 * the five flags associated with each serverhost. It will return the name of
1100 * each service and host that meets the quailifications. It does this by
1101 * building a where clause based on the arguments, then doing a retrieve.
1104 static char *shflags[6] = { "service", "enable", "override", "success",
1105 "inprogress", "hosterror" };
1107 int qualified_get_serverhost(q, argv, cl, action, actarg)
1111 int (*action)(), actarg;
1113 char qual[256], buf[32];
1116 sprintf(qual, "m.mach_id = sh.mach_id AND sh.service = UPPER('%s')",
1118 for (i = 1; i < q->argc; i++) {
1119 if (!strcmp(argv[i], "TRUE")) {
1120 sprintf(buf, " AND sh.%s != 0", shflags[i]);
1122 } else if (!strcmp(argv[i], "FALSE")) {
1123 sprintf(buf, " AND sh.%s = 0", shflags[i]);
1128 sprintf(stmt_buf, "SELECT sh.service, m.name FROM serverhosts sh, "
1129 "machine m WHERE %s", qual);
1130 return do_for_all_rows(stmt_buf, 2, action, actarg);
1134 /* register_user - change user's login name and allocate a pobox, group,
1135 * filesystem, and quota for them. The user's status must start out as 0,
1136 * and is left as 2. Arguments are: user's UID, new login name, and user's
1137 * type for filesystem allocation (MR_FS_STUDENT, MR_FS_FACULTY,
1138 * MR_FS_STAFF, MR_FS_MISC).
1141 int register_user(q, argv, cl)
1146 EXEC SQL BEGIN DECLARE SECTION;
1147 char *login, *entity, directory[129], machname[33];
1148 int who, rowcount, mid, uid, users_id, utype, list_id;
1149 int ostatus, nstatus, gidval, fsidval;
1150 static int m_id = 0, def_quota = 0;
1151 EXEC SQL END DECLARE SECTION;
1152 char buffer[256], *aargv[3];
1154 entity = cl->entity;
1155 who = cl->client_id;
1157 uid = atoi(argv[0]);
1159 utype = atoi(argv[2]);
1162 EXEC SQL SELECT users_id, status INTO :users_id, :ostatus
1164 WHERE unix_uid = :uid AND (status=0 OR status=5 OR status=6);
1166 if (sqlca.sqlerrd[2] == 0)
1167 return(MR_NO_MATCH);
1168 if (sqlca.sqlerrd[2] > 1)
1169 return(MR_NOT_UNIQUE);
1171 /* check new login name */
1172 EXEC SQL SELECT COUNT(login) INTO :rowcount FROM users
1173 WHERE login = :login AND users_id != :users_id;
1174 if (dbms_errno) return(mr_errcode);
1175 if (rowcount > 0) return(MR_IN_USE);
1176 EXEC SQL SELECT COUNT(name) INTO :rowcount FROM list
1177 WHERE name = :login;
1178 if (dbms_errno) return(mr_errcode);
1179 if (rowcount > 0) return(MR_IN_USE);
1180 EXEC SQL SELECT COUNT(label) INTO :rowcount FROM filesys
1181 WHERE label = :login;
1182 if (dbms_errno) return(mr_errcode);
1183 if (rowcount > 0) return(MR_IN_USE);
1184 EXEC SQL SELECT COUNT(name) INTO :rowcount FROM alias
1185 WHERE name = :login AND type='FILESYS';
1186 if (dbms_errno) return(mr_errcode);
1187 if (rowcount > 0) return(MR_IN_USE);
1188 com_err(whoami, 0, "login name OK");
1190 /* choose place for pobox, put in mid */
1191 EXEC SQL DECLARE csr130 CURSOR FOR
1192 SELECT sh.mach_id, m.name FROM serverhosts sh, machine m
1193 WHERE sh.service='POP' AND sh.mach_id=m.mach_id
1194 AND sh.value2 - sh.value1 =
1195 (SELECT MAX(value2 - value1) FROM serverhosts
1196 WHERE service = 'POP');
1199 EXEC SQL OPEN csr130;
1202 EXEC SQL FETCH csr130 INTO :mid, :machname;
1203 if (sqlca.sqlerrd[2] == 0) {
1204 EXEC SQL CLOSE csr130;
1205 if (dbms_errno) return(mr_errcode);
1206 return(MR_NO_POBOX);
1208 EXEC SQL CLOSE csr130;
1209 if (dbms_errno) return(mr_errcode);
1212 /* change login name, set pobox */
1213 sprintf(buffer, "u.users_id = %d", users_id);
1214 incremental_before(USERS_TABLE, buffer, 0);
1216 if (ostatus == 5 || ostatus == 6)
1218 EXEC SQL UPDATE users SET login = :login, status = :nstatus,
1219 modtime=SYSDATE, modby = :who, modwith = :entity, potype='POP',
1220 pop_id = :mid, pmodtime=SYSDATE, pmodby = :who, pmodwith = :entity
1221 WHERE users_id = :users_id;
1223 if (dbms_errno) return(mr_errcode);
1224 if (sqlca.sqlerrd[2] != 1)
1225 return(MR_INTERNAL);
1226 set_pop_usage(mid, 1);
1227 com_err(whoami, 0, "set login name to %s and pobox to %s", login,
1229 incremental_after(USERS_TABLE, buffer, 0);
1232 /* Cell Name (I know, it shouldn't be hard coded...) */
1233 strcpy(machname, "ATHENA.MIT.EDU");
1234 EXEC SQL SELECT mach_id INTO :m_id FROM machine
1235 WHERE name = :machname;
1238 EXEC SQL SELECT list_id INTO :list_id FROM list
1241 /* create filesystem */
1242 if (set_next_object_id("filsys_id", FILESYS_TABLE, 0))
1244 incremental_clear_before();
1245 if (islower(login[0]) && islower(login[1])) {
1246 sprintf(directory, "/afs/athena.mit.edu/user/%c/%c/%s",
1247 login[0], login[1], login);
1249 sprintf(directory, "/afs/athena.mit.edu/user/other/%s", login);
1252 EXEC SQL SELECT value INTO :fsidval FROM numvalues
1253 WHERE numvalues.name='filsys_id';
1254 EXEC SQL INSERT INTO filesys
1255 (filsys_id, phys_id, label, type, mach_id, name,
1256 mount, rwaccess, comments, owner, owners, createflg,
1257 lockertype, modtime, modby, modwith)
1259 (:fsidval, 0, :login, 'AFS', :m_id, :directory,
1260 '/mit/' || :login, 'w', 'User Locker', :users_id, :list_id, 1,
1261 'HOMEDIR', SYSDATE, :who, :entity);
1263 if (dbms_errno) return(mr_errcode);
1264 if (sqlca.sqlerrd[2] != 1)
1265 return(MR_INTERNAL);
1266 sprintf(buffer,"fs.filsys_id = %d",fsidval);
1267 incremental_after(FILESYS_TABLE, buffer, 0);
1270 if (def_quota == 0) {
1271 EXEC SQL SELECT value INTO :def_quota FROM numvalues
1272 WHERE name='def_quota';
1273 if (dbms_errno) return(mr_errcode);
1274 if (sqlca.sqlerrd[2] != 1)
1275 return(MR_NO_QUOTA);
1278 incremental_clear_before();
1279 EXEC SQL INSERT INTO quota
1280 (entity_id, filsys_id, type, quota, phys_id, modtime, modby, modwith)
1282 (0, :fsidval, 'ANY', :def_quota, 0, SYSDATE, :who, :entity);
1283 if (dbms_errno) return(mr_errcode);
1284 if (sqlca.sqlerrd[2] != 1)
1285 return(MR_INTERNAL);
1289 sprintf(buffer, "q.entity_id = 0 and q.filsys_id = %d and q.type = 'ANY'", fsidval);
1290 incremental_after(QUOTA_TABLE, buffer, aargv);
1291 com_err(whoami, 0, "quota of %d assigned", def_quota);
1292 if (dbms_errno) return(mr_errcode);
1294 cache_entry(login, USERS_TABLE, users_id);
1296 EXEC SQL UPDATE tblstats SET updates=updates+1, modtime=SYSDATE
1297 WHERE table_name='users';
1298 EXEC SQL UPDATE tblstats SET appends=appends+1, modtime=SYSDATE
1299 WHERE table_name='filesys' OR table_name='quota';
1300 if (dbms_errno) return(mr_errcode);
1306 /** set_pop_usage - incr/decr usage count for pop server in serverhosts talbe
1310 ** delta (will be +/- 1)
1313 ** - incr/decr value field in serverhosts table for pop/mach_id
1317 int set_pop_usage(id, cnt)
1318 EXEC SQL BEGIN DECLARE SECTION;
1320 EXEC SQL END DECLARE SECTION;
1323 EXEC SQL UPDATE serverhosts SET value1 = value1 + :cnt
1324 WHERE serverhosts.service = 'POP' AND serverhosts.mach_id = :id;
1326 if (dbms_errno) return(mr_errcode);