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>
18 #include "mr_server.h"
23 EXEC SQL INCLUDE sqlca;
24 EXEC SQL INCLUDE sqlda;
27 extern char *whoami, *strsave();
28 extern int ingres_errno, mr_errcode;
30 EXEC SQL BEGIN DECLARE SECTION;
31 extern char stmt_buf[];
32 EXEC SQL END DECLARE SECTION;
34 EXEC SQL WHENEVER SQLERROR CALL ingerr;
37 /* Special query routines */
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 SMTP, then box should be a string and its ID should
43 * be put in box_id. If type is NONE, then box doesn't matter.
46 int set_pobox(q, argv, cl)
51 EXEC SQL BEGIN DECLARE SECTION;
54 EXEC SQL END DECLARE SECTION;
58 user = *(int *)argv[0];
60 EXEC SQL REPEATED SELECT pop_id, potype INTO :id, :potype FROM users
61 WHERE users_id = :user;
62 if (ingres_errno) return(mr_errcode);
63 if (!strcmp(strtrim(potype), "POP"))
64 set_pop_usage(id, -1);
66 if (!strcmp(argv[1], "POP")) {
67 status = name_to_id(box, "MACHINE", &id);
68 if (status == MR_NO_MATCH)
72 EXEC SQL REPEATED UPDATE users SET potype = 'POP', pop_id = :id
73 WHERE users_id = :user;
75 } else if (!strcmp(argv[1], "SMTP")) {
76 if (index(box, '/') || index(box, '|'))
78 status = name_to_id(box, "STRING", &id);
79 if (status == MR_NO_MATCH) {
83 EXEC SQL REPEATED UPDATE users SET potype='SMTP', box_id = :id
84 WHERE users_id = :user;
85 } else /* argv[1] == "NONE" */ {
86 EXEC SQL REPEATED UPDATE users SET potype='NONE'
87 WHERE users_id = :user;
90 set_pobox_modtime(q, argv, cl);
91 EXEC SQL REPEATED UPDATE tblstats SET updates = updates+1, modtime='now'
92 WHERE table_name='users';
93 if (ingres_errno) return(mr_errcode);
98 /* get_list_info: passed a wildcard list name, returns lots of stuff about
99 * each list. This is tricky: first build a queue of all requested
100 * data. Rest of processing consists of fixing gid, ace_name, and modby.
103 get_list_info(q, aargv, cl, action, actarg)
104 register struct query *q;
107 register int (*action)();
111 EXEC SQL BEGIN DECLARE SECTION;
112 char *name, acl_type[9], listname[33], active[5], public[5], hidden[5];
113 char maillist[5], grouplist[5], gid_str[6], acl_name[256], desc[256];
114 char modtime[27], modby[256], modwith[9];
115 int id, rowcount, acl_id, hid, modby_id;
117 EXEC SQL END DECLARE SECTION;
118 int returned, status;
119 struct save_queue *sq, *sq_create();
121 returned = rowcount = 0;
123 convert_wildcards(name);
126 sprintf(qual,"name LIKE '%s' ESCAPE '*'",name);
127 optimize_sql_stmt(qual);
128 EXEC SQL DECLARE csr102 CURSOR FOR SELECT list_id FROM list
132 EXEC SQL OPEN csr102;
137 EXEC SQL FETCH csr102 INTO :id;
138 if(sqlca.sqlcode!=0) break;
139 sq_save_data(sq, id);
142 EXEC SQL CLOSE csr102;
144 if (ingres_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 REPEATED SELECT name, CHAR(active), CHAR(publicflg),
157 CHAR(hidden), hidden, CHAR(maillist), CHAR(grouplist), CHAR(gid),
158 TRIM(acl_type), acl_id, description, CHAR(modtime), modby, modwith
159 INTO :listname, :active, :public, :hidden, :hid, :maillist,
160 :grouplist, :gid_str, :acl_type, :acl_id, :desc,
161 :modtime, :modby_id, :modwith
162 FROM list WHERE list_id = :id;
164 if (ingres_errno) return(mr_errcode);
166 if (atoi(gid_str) == -1)
167 argv[6] = UNIQUE_GID;
170 if (!strcmp(acl_type, "LIST")) {
171 status = id_to_name(acl_id, "LIST", &argv[8]);
172 } else if (!strcmp(acl_type, "USER")) {
173 status = id_to_name(acl_id, "USER", &argv[8]);
174 } else if (!strcmp(acl_type, "KERBEROS")) {
175 status = id_to_name(acl_id, "STRING", &argv[8]);
176 } else if (!strcmp(acl_type, "NONE")) {
179 argv[8] = strsave("NONE");
183 argv[8] = strsave("???");
185 if (status && status != MR_NO_MATCH) return(status);
187 argv[11] = malloc(0);
189 status = id_to_name(modby_id, "USER", &argv[11]);
191 status = id_to_name(-modby_id, "STRING", &argv[11]);
192 if (status && status != MR_NO_MATCH) return(status);
194 mr_trim_args(q->vcnt, argv);
196 (*action)(q->vcnt, argv, actarg);
202 if (ingres_errno) return(mr_errcode);
207 /* Add_member_to_list: do list flattening as we go! MAXLISTDEPTH is
208 * how many different ancestors a member is allowed to have.
211 #define MAXLISTDEPTH 1024
213 int add_member_to_list(q, argv, cl)
218 EXEC SQL BEGIN DECLARE SECTION;
219 int id, lid, mid, error, who, ref, rowcnt;
220 char *mtype, dtype[9], *entity;
221 EXEC SQL END DECLARE SECTION;
222 int ancestors[MAXLISTDEPTH], aref[MAXLISTDEPTH], acount, a;
223 int descendants[MAXLISTDEPTH], dref[MAXLISTDEPTH], dcount, d;
225 char *dtypes[MAXLISTDEPTH];
226 char *iargv[3], *buf;
228 lid = *(int *)argv[0];
230 mid = *(int *)argv[2];
231 /* if the member is already a direct member of the list, punt */
232 EXEC SQL REPEATED SELECT COUNT(list_id) INTO :rowcnt FROM imembers
233 WHERE list_id = :lid AND member_id = :mid
234 AND member_type = :mtype AND direct = 1;
237 if (!strcasecmp(mtype, "STRING")) {
239 status = id_to_name(mid, "STRING", &buf);
240 if (status) return(status);
241 if (index(buf, '/') || index(buf, '|')) {
251 EXEC SQL DECLARE csr103 CURSOR FOR
252 SELECT list_id, ref_count FROM imembers
253 WHERE member_id = :lid AND member_type='LIST';
256 EXEC SQL OPEN csr103;
260 EXEC SQL FETCH csr103 INTO :id, :ref;
261 if(sqlca.sqlcode != 0) break;
263 ancestors[acount++] = id;
264 if (acount >= MAXLISTDEPTH) break;
266 EXEC SQL CLOSE csr103;
267 if (ingres_errno) return(mr_errcode);
268 if (acount >= MAXLISTDEPTH) {
271 descendants[0] = mid;
276 if (!strcmp(mtype, "LIST")) {
277 EXEC SQL DECLARE csr104 CURSOR FOR
278 SELECT member_id, member_type, ref_count
280 WHERE list_id = :mid;
283 EXEC SQL OPEN csr104;
287 EXEC SQL FETCH csr104 INTO :id, :dtype, :ref;
288 if(sqlca.sqlcode != 0) break;
291 dtypes[dcount] = "LIST";
294 dtypes[dcount] = "USER";
297 dtypes[dcount] = "STRING";
300 dtypes[dcount] = "KERBEROS";
307 descendants[dcount++] = id;
308 if (dcount >= MAXLISTDEPTH) {
313 EXEC SQL CLOSE csr104;
314 if (ingres_errno) return(mr_errcode);
318 for (a = 0; a < acount; a++) {
320 for (d = 0; d < dcount; d++) {
321 mid = descendants[d];
323 if (mid == lid && !strcmp(mtype, "LIST")) {
326 EXEC SQL REPEATED SELECT COUNT(ref_count) INTO :rowcnt
328 WHERE list_id = :lid AND member_id = :mid
329 AND member_type = :mtype;
330 ref = aref[a] * dref[d];
332 if (a == 0 && d == 0) {
333 EXEC SQL UPDATE imembers
334 SET ref_count = ref_count+:ref, direct=1
335 WHERE list_id = :lid AND member_id = :mid
336 AND member_type = :mtype;
338 EXEC SQL UPDATE imembers
339 SET ref_count = ref_count+:ref
340 WHERE list_id = :lid AND member_id = :mid
341 AND member_type = :mtype;
344 incremental_clear_before();
345 if (a == 0 && d == 0) {
346 EXEC SQL INSERT INTO imembers
347 (list_id, member_id, direct, member_type, ref_count)
348 VALUES (:lid, :mid, 1, :mtype, 1);
350 EXEC SQL INSERT INTO imembers
351 (list_id, member_id, member_type, ref_count)
352 VALUES (:lid, :mid, :mtype, 1);
354 iargv[0] = (char *)lid;
356 iargv[2] = (char *)mid;
357 incremental_after("members", 0, iargv);
361 lid = *(int *)argv[0];
364 EXEC SQL REPEATED UPDATE list
365 SET modtime='now', modby = :who, modwith = :entity
366 WHERE list_id = :lid;
367 if (ingres_errno) return(mr_errcode);
372 /* Delete_member_from_list: do list flattening as we go!
375 int delete_member_from_list(q, argv, cl)
380 EXEC SQL BEGIN DECLARE SECTION;
381 int id, lid, mid, cnt, error, who, ref;
382 char *mtype, dtype[9], *entity;
383 EXEC SQL END DECLARE SECTION;
384 int ancestors[MAXLISTDEPTH], aref[MAXLISTDEPTH], acount, a;
385 int descendants[MAXLISTDEPTH], dref[MAXLISTDEPTH], dcount, d;
386 char *dtypes[MAXLISTDEPTH];
389 lid = *(int *)argv[0];
391 mid = *(int *)argv[2];
392 /* if the member is not a direct member of the list, punt */
393 EXEC SQL REPEATED SELECT COUNT(list_id) INTO :cnt FROM imembers
394 WHERE list_id = :lid AND member_id = :mid
395 AND member_type = :mtype AND direct = 1;
396 if (ingres_errno) return(mr_errcode);
402 EXEC SQL DECLARE csr105 CURSOR FOR
403 SELECT list_id, ref_count FROM imembers
404 WHERE member_id = :lid AND member_type = 'LIST';
407 EXEC SQL OPEN csr105;
411 EXEC SQL FETCH csr105 INTO :id, :ref;
412 if(sqlca.sqlcode!=0) break;
414 ancestors[acount++] = id;
415 if (acount >= MAXLISTDEPTH) break;
417 EXEC SQL CLOSE csr105;
420 if (acount >= MAXLISTDEPTH)
422 descendants[0] = mid;
427 if (!strcmp(mtype, "LIST")) {
428 EXEC SQL DECLARE csr106 CURSOR FOR
429 SELECT member_id, member_type, ref_count FROM imembers
430 WHERE list_id = :mid;
433 EXEC SQL OPEN csr106;
437 EXEC SQL FETCH csr106 INTO :id, :dtype, :ref;
438 if(sqlca.sqlcode!=0) break;
441 dtypes[dcount] = "LIST";
444 dtypes[dcount] = "USER";
447 dtypes[dcount] = "STRING";
450 dtypes[dcount] = "KERBEROS";
457 descendants[dcount++] = id;
458 if (dcount >= MAXLISTDEPTH) break;
460 EXEC SQL CLOSE csr106;
466 for (a = 0; a < acount; a++) {
468 for (d = 0; d < dcount; d++) {
469 mid = descendants[d];
471 if (mid == lid && !strcmp(mtype, "LIST")) {
474 EXEC SQL REPEATED SELECT ref_count INTO :cnt FROM imembers
475 WHERE list_id = :lid AND member_id = :mid AND member_type = :mtype;
476 ref = aref[a] * dref[d];
478 iargv[0] = (char *)lid;
480 iargv[2] = (char *)mid;
481 incremental_before("members", 0, iargv);
482 EXEC SQL DELETE FROM imembers
483 WHERE list_id = :lid AND member_id = :mid
484 AND member_type= :mtype;
485 incremental_clear_after();
486 } else if (a == 0 && d == 0) {
487 EXEC SQL UPDATE imembers
488 SET ref_count = ref_count - :ref, direct = 0
489 WHERE list_id = :lid AND member_id = :mid
490 AND member_type = :mtype;
492 EXEC SQL UPDATE imembers
493 SET ref_count = ref_count - :ref
494 WHERE list_id = :lid AND member_id = :mid
495 AND member_type = :mtype;
499 lid = *(int *)argv[0];
502 EXEC SQL UPDATE list SET modtime = 'now', modby = :who, modwith = :entity
503 WHERE list_id = :lid;
504 if (ingres_errno) return(mr_errcode);
509 /* get_ace_use - given a type and a name, return a type and a name.
510 * The ace_type is one of "LIST", "USER", "RLIST", or "RUSER" in argv[0],
511 * and argv[1] will contain the ID of the entity in question. The R*
512 * types mean to recursively look at every containing list, not just
513 * when the object in question is a direct member. On return, the
514 * usage type will be one of LIST, SERVICE, FILESYS, QUOTA, QUERY, or ZEPHYR.
517 int get_ace_use(q, argv, cl, action, actarg)
525 EXEC SQL BEGIN DECLARE SECTION;
528 EXEC SQL END DECLARE SECTION;
529 struct save_queue *sq, *sq_create();
532 aid = *(int *)argv[1];
533 if (!strcmp(atype, "LIST") || !strcmp(atype, "USER") ||
534 !strcmp(atype, "KERBEROS")) {
535 return(get_ace_internal(atype, aid, action, actarg));
539 if (!strcmp(atype, "RLIST")) {
540 sq_save_data(sq, aid);
541 /* get all the list_id's of containing lists */
542 EXEC SQL DECLARE csr107 CURSOR FOR
543 SELECT list_id FROM imembers
544 WHERE member_type='LIST' AND member_id = :aid;
547 EXEC SQL OPEN csr107;
551 EXEC SQL FETCH csr107 INTO :listid;
552 if(sqlca.sqlcode != 0) break;
553 sq_save_unique_data(sq, listid);
555 EXEC SQL CLOSE csr107;
556 /* now process each one */
557 while (sq_get_data(sq, &id)) {
558 if (get_ace_internal("LIST", id, action, actarg) == MR_SUCCESS)
563 if (!strcmp(atype, "RUSER")) {
564 EXEC SQL DECLARE csr108 CURSOR FOR
565 SELECT list_id FROM imembers
566 WHERE member_type='USER' AND member_id = :aid;
569 EXEC SQL OPEN csr108;
573 EXEC SQL FETCH csr108 INTO :listid;
574 if(sqlca.sqlcode != 0) break;
575 sq_save_data(sq, listid);
577 EXEC SQL CLOSE csr108;
578 /* now process each one */
579 while (sq_get_data(sq, &id)) {
580 if (get_ace_internal("LIST", id, action, actarg) == MR_SUCCESS)
583 if (get_ace_internal("USER", aid, action, actarg) == MR_SUCCESS)
587 if (!strcmp(atype, "RKERBERO")) {
588 EXEC SQL DECLARE csr109 CURSOR FOR
589 SELECT list_id FROM imembers
590 WHERE member_type='KERBEROS' AND member_id = :aid;
593 EXEC SQL OPEN csr109;
597 EXEC SQL FETCH csr109 INTO :listid;
598 if(sqlca.sqlcode != 0) break;
599 sq_save_data(sq, listid);
601 EXEC SQL CLOSE csr109;
602 /* now process each one */
603 while (sq_get_data(sq, &id)) {
604 if (get_ace_internal("LIST", id, action, actarg) == MR_SUCCESS)
607 if (get_ace_internal("KERBEROS", aid, action, actarg) == MR_SUCCESS)
612 if (ingres_errno) return(mr_errcode);
613 if (!found) return(MR_NO_MATCH);
618 /* This looks up a single list or user for ace use. atype must be "USER"
619 * or "LIST", and aid is the ID of the corresponding object. This is used
620 * by get_ace_use above.
623 get_ace_internal(atype, aid, action, actarg)
624 EXEC SQL BEGIN DECLARE SECTION;
627 EXEC SQL END DECLARE SECTION;
633 EXEC SQL BEGIN DECLARE SECTION;
635 EXEC SQL END DECLARE SECTION;
638 if (!strcmp(atype, "LIST")) {
639 rargv[0] = "FILESYS";
640 EXEC SQL DECLARE csr110 CURSOR FOR
641 SELECT label FROM filesys
645 EXEC SQL OPEN csr110;
649 EXEC SQL FETCH csr110 INTO :name;
650 if(sqlca.sqlcode != 0) break;
651 (*action)(2, rargv, actarg);
654 EXEC SQL CLOSE csr110;
657 EXEC SQL DECLARE csr111 CURSOR FOR
658 SELECT capability FROM capacls
659 WHERE list_id = :aid ;
662 EXEC SQL OPEN csr111;
666 EXEC SQL FETCH csr111 INTO :name ;
667 if(sqlca.sqlcode != 0) break;
668 (*action)(2, rargv, actarg);
671 EXEC SQL CLOSE csr111;
672 } else if (!strcmp(atype, "USER")) {
673 rargv[0] = "FILESYS";
674 EXEC SQL DECLARE csr112 CURSOR FOR
675 SELECT label FROM filesys
679 EXEC SQL OPEN csr112;
683 EXEC SQL FETCH csr112 INTO :name ;
684 if(sqlca.sqlcode != 0) break;
685 (*action)(2, rargv, actarg);
688 EXEC SQL CLOSE csr112;
692 EXEC SQL DECLARE csr113 CURSOR FOR
693 SELECT name FROM list
694 WHERE acl_type = :atype AND acl_id = :aid;
697 EXEC SQL OPEN csr113;
701 EXEC SQL FETCH csr113 INTO :name;
702 if(sqlca.sqlcode != 0) break;
703 (*action)(2, rargv, actarg);
706 EXEC SQL CLOSE csr113;
708 rargv[0] = "SERVICE";
709 EXEC SQL DECLARE csr114 CURSOR FOR
710 SELECT name FROM servers
711 WHERE acl_type = :atype AND acl_id = :aid;
714 EXEC SQL OPEN csr114;
718 EXEC SQL FETCH csr114 INTO :name;
719 if(sqlca.sqlcode != 0) break;
720 (*action)(2, rargv, actarg);
723 EXEC SQL CLOSE csr114;
725 rargv[0] = "HOSTACCESS";
726 EXEC SQL DECLARE csr115 CURSOR FOR
727 SELECT name FROM machine m, hostaccess ha
728 WHERE m.mach_id = ha.mach_id AND ha.acl_type = :atype
729 AND ha.acl_id = :aid;
732 EXEC SQL OPEN csr115;
736 EXEC SQL FETCH csr115 INTO :name;
737 if(sqlca.sqlcode != 0) break;
738 (*action)(2, rargv, actarg);
741 EXEC SQL CLOSE csr115;
744 EXEC SQL DECLARE csr116 CURSOR FOR
745 SELECT class FROM zephyr z
746 WHERE z.xmt_type = :atype AND z.xmt_id = :aid
747 OR z.sub_type = :atype AND z.sub_id = :aid
748 OR z.iws_type = :atype AND z.iws_id = :aid
749 OR z.iui_type = :atype AND z.iui_id = :aid;
752 EXEC SQL OPEN csr116;
756 EXEC SQL FETCH csr116 INTO :name;
757 if(sqlca.sqlcode != 0) break;
758 (*action)(2, rargv, actarg);
761 EXEC SQL CLOSE csr116;
763 if (!found) return(MR_NO_MATCH);
768 /* get_lists_of_member - given a type and a name, return the name and flags
769 * of all of the lists of the given member. The member_type is one of
770 * "LIST", "USER", "STRING", "RLIST", "RUSER", or "RSTRING" in argv[0],
771 * and argv[1] will contain the ID of the entity in question. The R*
772 * types mean to recursively look at every containing list, not just
773 * when the object in question is a direct member.
776 int get_lists_of_member(q, argv, cl, action, actarg)
783 int found = 0, direct = 1;
785 EXEC SQL BEGIN DECLARE SECTION;
788 char name[33], active[5], public[5], hidden[5], maillist[5], grouplist[5];
789 EXEC SQL END DECLARE SECTION;
792 aid = *(int *)argv[1];
793 if (!strcmp(atype, "RLIST")) {
797 if (!strcmp(atype, "RUSER")) {
801 if (!strcmp(atype, "RSTRING")) {
805 if (!strcmp(atype, "RKERBEROS")) {
815 rargv[5] = grouplist;
817 EXEC SQL DECLARE csr117a CURSOR FOR
818 SELECT l.name, CHAR(l.active), CHAR(l.publicflg), CHAR(l.hidden),
819 CHAR(l.maillist), CHAR(l.grouplist)
820 FROM list l, imembers im
821 WHERE l.list_id = im.list_id AND im.direct = 1
822 AND im.member_type = :atype AND im.member_id = :aid;
825 EXEC SQL OPEN csr117a;
829 EXEC SQL FETCH csr117a
830 INTO :name, :active, :public, :hidden, :maillist, :grouplist;
831 if(sqlca.sqlcode != 0) break;
832 (*action)(6, rargv, actarg);
835 EXEC SQL CLOSE csr117a;
837 EXEC SQL DECLARE csr117b CURSOR FOR
838 SELECT l.name, CHAR(l.active), CHAR(l.publicflg), CHAR(l.hidden),
839 CHAR(l.maillist), CHAR(l.grouplist)
840 FROM list l, imembers im
841 WHERE l.list_id = im.list_id
842 AND im.member_type = :atype AND im.member_id = :aid;
845 EXEC SQL OPEN csr117b;
849 EXEC SQL FETCH csr117b
850 INTO :name, :active, :public, :hidden, :maillist, :grouplist;
851 if(sqlca.sqlcode != 0) break;
852 (*action)(6, rargv, actarg);
855 EXEC SQL CLOSE csr117b;
858 if (ingres_errno) return(mr_errcode);
859 if (!found) return(MR_NO_MATCH);
864 /* qualified_get_lists: passed "TRUE", "FALSE", or "DONTCARE" for each of
865 * the five flags associated with each list. It will return the name of
866 * each list that meets the quailifications. It does this by building a
867 * where clause based on the arguments, then doing a retrieve.
870 static char *lflags[5] = { "active", "publicflg", "hidden", "maillist", "grouplist" };
872 int qualified_get_lists(q, argv, cl, action, actarg)
879 return(qualified_get(q, argv, action, actarg, "l.list_id != 0",
880 "l", "name", lflags));
884 /* get_members_of_list - this gets only direct members */
886 get_members_of_list(q, argv, cl, action, actarg)
893 return(gmol_internal(q, argv, cl, action, actarg, 1));
896 /* get_end_members_of_list - this gets direct or indirect members */
898 get_end_members_of_list(q, argv, cl, action, actarg)
905 return(gmol_internal(q, argv, cl, action, actarg, 0));
908 /** gmol_internal - optimized query for retrieval of list members
909 ** used by both get_members_of_list and get_end_members_of_list
915 ** - retrieve USER members, then LIST members, then STRING members
918 gmol_internal(q, argv, cl, action, actarg, flag)
926 EXEC SQL BEGIN DECLARE SECTION;
927 int list_id, member_id, direct;
928 char member_name[129], member_type[9];
929 EXEC SQL END DECLARE SECTION;
932 struct save_queue *sq;
934 /* true/false flag indicates whether to display only direct members. */
940 list_id = *(int *)argv[0];
944 EXEC SQL DECLARE csr118 CURSOR FOR
945 SELECT member_type, member_id FROM imembers
946 WHERE list_id = :list_id AND direct > :direct;
949 EXEC SQL OPEN csr118;
953 EXEC SQL FETCH csr118 INTO :member_type, :member_id;
954 if (sqlca.sqlcode != 0) break;
957 sq_save_data(sq, ((int)member_type[0] << 24) | (member_id & 0xffffff));
959 EXEC SQL CLOSE csr118;
962 targv[1] = malloc(0);
963 while (sq_remove_data(sq, &member_id)) {
964 switch (member_id >> 24) {
967 id_to_name(member_id & 0xffffff, "USER", &targv[1]);
968 (*action)(2, targv, actarg);
972 id_to_name(member_id & 0xffffff, "LIST", &targv[1]);
973 (*action)(2, targv, actarg);
977 id_to_name(member_id & 0xffffff, "STRING", &targv[1]);
978 (*action)(2, targv, actarg);
981 targv[0] = "KERBEROS";
982 id_to_name(member_id & 0xffffff, "STRING", &targv[1]);
983 (*action)(2, targv, actarg);
996 targv[1] = member_name;
998 EXEC SQL DECLARE csr119 CURSOR FOR
999 SELECT u.login FROM users u, imembers im
1000 WHERE im.list_id = :list_id AND im.member_type = 'USER'
1001 AND im.member_id = u.users_id AND im.direct > :direct
1005 EXEC SQL OPEN csr119;
1009 EXEC SQL FETCH csr119 INTO :member_name;
1010 if(sqlca.sqlcode != 0) break;
1011 (*action)(2, targv, actarg);
1013 EXEC SQL CLOSE csr119;
1014 if (ingres_errno) return(mr_errcode);
1017 EXEC SQL DECLARE csr120 CURSOR FOR
1018 SELECT l.name FROM list l, imembers im
1019 WHERE im.list_id = :list_id AND im.member_type='LIST'
1020 AND im.member_id = l.list_id AND im.direct > :direct
1024 EXEC SQL OPEN csr120;
1028 EXEC SQL FETCH csr120 INTO :member_name;
1029 if(sqlca.sqlcode != 0) break;
1030 (*action)(2, targv, actarg);
1032 EXEC SQL CLOSE csr120;
1033 if (ingres_errno) return(mr_errcode);
1035 targv[0] = "STRING";
1036 EXEC SQL DECLARE csr121 CURSOR FOR
1037 SELECT CHAR(str.string) FROM strings str, imembers im
1038 WHERE im.list_id = :list_id AND im.member_type='STRING'
1039 AND im.member_id = str.string_id AND im.direct > :direct
1043 EXEC SQL OPEN csr121;
1047 EXEC SQL FETCH csr121 INTO :member_name;
1048 if(sqlca.sqlcode != 0) break;
1049 (*action)(2, targv, actarg);
1051 EXEC SQL CLOSE csr121;
1052 if (ingres_errno) return(mr_errcode);
1054 targv[0] = "KERBEROS";
1055 EXEC SQL DECLARE csr122 CURSOR FOR
1056 SELECT CHAR(str.string) FROM strings str, imembers im
1057 WHERE im.list_id = :list_id AND im.member_type='KERBEROS'
1058 AND im.member_id = str.string_id
1059 AND im.direct > :direct
1063 EXEC SQL OPEN csr122;
1067 EXEC SQL FETCH csr122 INTO :member_name;
1068 if(sqlca.sqlcode != 0) break;
1069 (*action)(2, targv, actarg);
1071 EXEC SQL CLOSE csr122;
1072 if (ingres_errno) return(mr_errcode);
1078 /* count_members_of_list: this is a simple query, but it cannot be done
1079 * through the dispatch table.
1082 int count_members_of_list(q, argv, cl, action, actarg)
1089 EXEC SQL BEGIN DECLARE SECTION;
1091 EXEC SQL END DECLARE SECTION;
1092 char *rargv[1], countbuf[5];
1094 list = *(int *)argv[0];
1095 rargv[0] = countbuf;
1096 EXEC SQL REPEATED SELECT count (*) INTO :ct FROM imembers
1097 WHERE list_id = :list AND direct=1;
1098 if (ingres_errno) return(mr_errcode);
1099 sprintf(countbuf, "%d", ct);
1100 (*action)(1, rargv, actarg);
1105 /* qualified_get_server: passed "TRUE", "FALSE", or "DONTCARE" for each of
1106 * the three flags associated with each service. It will return the name of
1107 * each service that meets the quailifications. It does this by building a
1108 * where clause based on the arguments, then doing a retrieve.
1111 static char *sflags[3] = { "enable", "inprogress", "harderror" };
1113 int qualified_get_server(q, argv, cl, action, actarg)
1120 return(qualified_get(q, argv, action, actarg, "s.name != ''",
1121 "s", "name", sflags));
1125 /* generic qualified get routine, used by qualified_get_lists,
1126 * qualified_get_server, and qualified_get_serverhost.
1128 * start - a simple where clause, must not be empty
1129 * range - the name of the range variable
1130 * field - the field to return
1131 * flags - an array of strings, names of the flag variables
1134 int qualified_get(q, argv, action, actarg, start, range, field, flags)
1144 char name[33], qual[256];
1146 char *rargv[1], buf[32];
1148 strcpy(qual, start);
1149 for (i = 0; i < q->argc; i++) {
1150 if (!strcmp(argv[i], "TRUE")) {
1151 sprintf(buf, " AND %s.%s != 0", range, flags[i]);
1152 (void) strcat(qual, buf);
1153 } else if (!strcmp(argv[i], "FALSE")) {
1154 sprintf(buf, " AND %s.%s = 0", range, flags[i]);
1155 (void) strcat(qual, buf);
1159 rargv[0] = SQLDA->sqlvar[0].sqldata;
1160 sprintf(stmt_buf,"SELECT CHAR(%s.%s) FROM %s %s WHERE %s",range,field,q->rtable,range,qual);
1161 EXEC SQL PREPARE stmt INTO :SQLDA USING NAMES FROM :stmt_buf;
1163 return(MR_INTERNAL);
1164 EXEC SQL DECLARE csr123 CURSOR FOR stmt;
1165 EXEC SQL OPEN csr123;
1167 EXEC SQL FETCH csr123 USING DESCRIPTOR :SQLDA;
1168 if(sqlca.sqlcode != 0) break;
1170 (*action)(1, rargv, actarg);
1172 EXEC SQL CLOSE csr123;
1173 if (ingres_errno) return(mr_errcode);
1175 return(MR_NO_MATCH);
1180 /* qualified_get_serverhost: passed "TRUE", "FALSE", or "DONTCARE" for each of
1181 * the five flags associated with each serverhost. It will return the name of
1182 * each service and host that meets the quailifications. It does this by
1183 * building a where clause based on the arguments, then doing a retrieve.
1186 static char *shflags[6] = { "service", "enable", "override", "success",
1187 "inprogress", "hosterror" };
1189 int qualified_get_serverhost(q, argv, cl, action, actarg)
1196 EXEC SQL BEGIN DECLARE SECTION;
1197 char sname[33], mname[33], qual[256];
1198 EXEC SQL END DECLARE SECTION;
1199 char *rargv[2], buf[32];
1202 sprintf(qual, "m.mach_id = sh.mach_id AND sh.service = uppercase('%s')",
1204 for (i = 1; i < q->argc; i++) {
1205 if (!strcmp(argv[i], "TRUE")) {
1206 sprintf(buf, " AND sh.%s != 0", shflags[i]);
1208 } else if (!strcmp(argv[i], "FALSE")) {
1209 sprintf(buf, " AND sh.%s = 0", shflags[i]);
1216 EXEC SQL DECLARE csr124 CURSOR FOR
1217 SELECT sh.service, m.name FROM serverhosts sh, machine m
1221 EXEC SQL OPEN csr124;
1225 EXEC SQL FETCH csr124 INTO :sname, :mname;
1226 if(sqlca.sqlcode != 0) break;
1228 (*action)(2, rargv, actarg);
1230 EXEC SQL CLOSE csr124;
1232 if (ingres_errno) return(mr_errcode);
1234 return(MR_NO_MATCH);
1239 /* register_user - change user's login name and allocate a pobox, group,
1240 * filesystem, and quota for them. The user's status must start out as 0,
1241 * and is left as 2. Arguments are: user's UID, new login name, and user's
1242 * type for filesystem allocation (MR_FS_STUDENT, MR_FS_FACULTY,
1243 * MR_FS_STAFF, MR_FS_MISC).
1246 register_user(q, argv, cl)
1251 EXEC SQL BEGIN DECLARE SECTION;
1252 char *login, dir[65], *entity, directory[129], machname[33];
1253 int who, rowcount, mid, uid, users_id, flag, utype, nid, list_id, quota;
1254 int size, alloc, pid, ostatus, nstatus, gidval, fsidval, npidval;
1255 static int m_id = 0, def_quota = 0;
1256 EXEC SQL END DECLARE SECTION;
1257 char buffer[256], *aargv[3];
1259 entity = cl->entity;
1260 who = cl->client_id;
1262 uid = atoi(argv[0]);
1264 utype = atoi(argv[2]);
1267 EXEC SQL REPEATED SELECT users_id, status INTO :users_id, :ostatus
1269 WHERE uid = :uid AND (status=0 OR status=5 OR status=6);
1271 if (sqlca.sqlerrd[2] == 0)
1272 return(MR_NO_MATCH);
1273 if (sqlca.sqlerrd[2] > 1)
1274 return(MR_NOT_UNIQUE);
1276 /* check new login name */
1277 EXEC SQL REPEATED SELECT COUNT(login) INTO :rowcount FROM users
1278 WHERE login = :login AND users_id != :users_id;
1279 if (ingres_errno) return(mr_errcode);
1280 if (rowcount > 0) return(MR_IN_USE);
1281 EXEC SQL REPEATED SELECT COUNT(name) INTO :rowcount FROM list
1282 WHERE name = :login;
1283 if (ingres_errno) return(mr_errcode);
1284 if (rowcount > 0) return(MR_IN_USE);
1285 EXEC SQL REPEATED SELECT COUNT(label) INTO :rowcount FROM filesys
1286 WHERE label = :login;
1287 if (ingres_errno) return(mr_errcode);
1288 if (rowcount > 0) return(MR_IN_USE);
1289 com_err(whoami, 0, "login name OK");
1291 /* choose place for pobox, put in mid */
1292 EXEC SQL DECLARE csr130 CURSOR FOR
1293 SELECT sh.mach_id, m.name FROM serverhosts sh, machine m
1294 WHERE sh.service='POP' AND sh.mach_id=m.mach_id
1295 AND sh.value2 - sh.value1 =
1296 (SELECT MAX(value2 - value1) FROM serverhosts
1297 WHERE service = 'POP');
1300 EXEC SQL OPEN csr130;
1303 EXEC SQL FETCH csr130 INTO :mid, :machname;
1304 if (sqlca.sqlerrd[2] == 0) {
1305 EXEC SQL CLOSE csr130;
1306 if (ingres_errno) return(mr_errcode);
1307 return(MR_NO_POBOX);
1309 EXEC SQL CLOSE csr130;
1310 if (ingres_errno) return(mr_errcode);
1313 /* change login name, set pobox */
1314 sprintf(buffer, "u.users_id = %d", users_id);
1315 incremental_before("users", buffer, 0);
1317 if (ostatus == 5 || ostatus == 6)
1319 EXEC SQL REPEATED UPDATE users SET login = :login, status = :nstatus,
1320 modtime='now', modby = :who, modwith = :entity, potype='POP',
1321 pop_id = :mid, pmodtime='now', pmodby = :who, pmodwith = :entity
1322 WHERE users_id = :users_id;
1324 if (ingres_errno) return(mr_errcode);
1325 if (sqlca.sqlerrd[2] != 1)
1326 return(MR_INTERNAL);
1327 set_pop_usage(mid, 1);
1328 com_err(whoami, 0, "set login name to %s and pobox to %s", login,
1330 incremental_after("users", buffer, 0);
1332 /* create group list */
1333 if (set_next_object_id("gid", "list", 1))
1335 if (set_next_object_id("list_id", "list", 0))
1337 EXEC SQL REPEATED SELECT value INTO :list_id FROM numvalues
1338 WHERE name='list_id';
1339 if (ingres_errno) return(mr_errcode);
1340 if (sqlca.sqlerrd[2] != 1)
1341 return(MR_INTERNAL);
1342 incremental_clear_before();
1343 EXEC SQL SELECT value INTO :gidval FROM numvalues WHERE name = 'gid';
1344 EXEC SQL REPEATED INSERT INTO list
1345 (name, list_id, active, publicflg, hidden, maillist, grouplist,
1346 gid, description, acl_type, acl_id,
1347 modtime, modby, modwith)
1348 VALUES (:login, :list_id, 1, 0, 0, 0, 1,
1349 :gidval, 'User Group', 'USER', :users_id,
1350 'now', :who, :entity);
1351 if (ingres_errno) return(mr_errcode);
1352 if (sqlca.sqlerrd[2] != 1)
1353 return(MR_INTERNAL);
1354 sprintf(buffer, "l.list_id = %d", list_id);
1355 incremental_after("list", buffer, 0);
1356 aargv[0] = (char *) list_id;
1358 aargv[2] = (char *) users_id;
1359 incremental_clear_before();
1360 EXEC SQL REPEATED INSERT INTO imembers
1361 (list_id, member_type, member_id, ref_count, direct)
1362 VALUES (:list_id, 'USER', :users_id, 1, 1);
1363 if (ingres_errno) return(mr_errcode);
1364 if (sqlca.sqlerrd[2] != 1)
1365 return(MR_INTERNAL);
1366 incremental_after("members", 0, aargv);
1369 /* Cell Name (I know, it shouldn't be hard coded...) */
1370 strcpy(machname, "ATHENA.MIT.EDU");
1371 EXEC SQL SELECT mach_id INTO :m_id FROM machine
1372 WHERE name = :machname;
1375 /* create filesystem */
1376 if (set_next_object_id("filsys_id", "filesys", 0))
1378 incremental_clear_before();
1379 if (islower(login[0]) && islower(login[1])) {
1380 sprintf(directory, "/afs/athena.mit.edu/user/%c/%c/%s",
1381 login[0], login[1], login);
1383 sprintf(directory, "/afs/athena.mit.edu/user/other/%s", login);
1386 EXEC SQL SELECT value INTO :fsidval FROM numvalues
1387 WHERE numvalues.name='filsys_id';
1388 EXEC SQL REPEATED INSERT INTO filesys
1389 (filsys_id, phys_id, label, type, mach_id, name,
1390 mount, access, comments, owner, owners, createflg,
1391 lockertype, modtime, modby, modwith)
1393 (:fsidval, 0, :login, 'AFS', :m_id, :directory,
1394 '/mit/'+:login, 'w', 'User Locker', :users_id, :list_id, 1,
1395 'HOMEDIR', 'now', :who, :entity);
1397 if (ingres_errno) return(mr_errcode);
1398 if (sqlca.sqlerrd[2] != 1)
1399 return(MR_INTERNAL);
1400 sprintf(buffer,"fs.filsys_id = %d",fsidval);
1401 incremental_after("filesys", buffer, 0);
1404 if (def_quota == 0) {
1405 EXEC SQL REPEATED SELECT value INTO :def_quota FROM numvalues
1406 WHERE name='def_quota';
1407 if (ingres_errno) return(mr_errcode);
1408 if (sqlca.sqlerrd[2] != 1)
1409 return(MR_NO_QUOTA);
1412 incremental_clear_before();
1413 EXEC SQL REPEATED INSERT INTO quota
1414 (entity_id, filsys_id, type, quota, phys_id, modtime, modby, modwith)
1416 (0, :fsidval, 'ANY', :def_quota, 0, 'now', :who, :entity);
1417 if (ingres_errno) return(mr_errcode);
1418 if (sqlca.sqlerrd[2] != 1)
1419 return(MR_INTERNAL);
1423 sprintf(buffer, "q.entity_id = 0 and q.filsys_id = %d and q.type = 'ANY'", fsidval);
1424 incremental_after("quota", buffer, aargv);
1425 com_err(whoami, 0, "quota of %d assigned", def_quota);
1426 if (ingres_errno) return(mr_errcode);
1428 cache_entry(login, "USER", users_id);
1430 EXEC SQL REPEATED UPDATE tblstats SET updates=updates+1, modtime='now'
1431 WHERE table_name='users';
1432 EXEC SQL REPEATED UPDATE tblstats SET appends=appends+1, modtime='now'
1433 WHERE table_name='list' OR table_name='filesys' OR table_name='quota';
1434 if (ingres_errno) return(mr_errcode);
1440 /** set_pop_usage - incr/decr usage count for pop server in serverhosts talbe
1444 ** delta (will be +/- 1)
1447 ** - incr/decr value field in serverhosts table for pop/mach_id
1451 int set_pop_usage(id, cnt)
1452 EXEC SQL BEGIN DECLARE SECTION;
1455 EXEC SQL END DECLARE SECTION;
1457 EXEC SQL REPEATED UPDATE serverhosts SET value1 = value1 + :cnt
1458 WHERE serverhosts.service = 'POP' AND serverhosts.mach_id = :id;
1460 if (ingres_errno) return(mr_errcode);
1466 special-case `ghal "*" "name"' since Ingres does it slowly */
1468 int get_hostalias(q, argv, cl, action, actarg)
1475 EXEC SQL BEGIN DECLARE SECTION;
1476 char *alias=argv[0], *machine=argv[1], qual[BUFSIZ], *p;
1478 EXEC SQL END DECLARE SECTION;
1480 for(p=machine; *p; p++) {
1481 if(*p=='%' || *p=='_') break;
1488 /* machine has no wildcards, so we can do it the fast way */
1489 EXEC SQL REPEATED SELECT mach_id INTO :id FROM machine
1490 WHERE name=:machine;
1491 if(ingres_errno) return(mr_errcode);
1493 sprintf(qual, "a.mach_id = %d AND m.mach_id = %d AND a.name LIKE '%s' ESCAPE '*'", id, id, alias);
1494 return do_retrieve(q, qual, 0, action, actarg);
1497 /* not the special case... do the normal query */
1498 build_qual(q->qual, q->argc, argv, qual);
1499 return do_retrieve(q, qual, 0, action, actarg);