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")) &&
91 !strcmp(argv[1], "SPLIT"))
93 status = set_pobox_pop(q, argv, cl);
97 strlcpy(potype, argv[1], sizeof(potype));
98 EXEC SQL UPDATE users SET potype = :potype, box_id = :id
99 WHERE users_id = :user;
101 else if (!strcmp(argv[1], "IMAP"))
103 EXEC SQL SELECT filsys_id INTO :id FROM filesys
104 WHERE label = :box AND type = 'IMAP';
107 EXEC SQL UPDATE users SET potype = 'IMAP', imap_id = :id, pop_id = 0
108 WHERE users_id = :user;
110 else /* argv[1] == "NONE" */
112 EXEC SQL UPDATE users SET potype = 'NONE'
113 WHERE users_id = :user;
116 set_pobox_modtime(q, argv, cl);
117 EXEC SQL UPDATE tblstats SET updates = updates + 1, modtime = SYSDATE
118 WHERE table_name = 'users';
124 /* set_pobox_pop: Revert to existing POP or IMAP pobox.
125 * Also take care of keeping track of the post office usage.
127 int set_pobox_pop(struct query *q, char **argv, client *cl)
129 EXEC SQL BEGIN DECLARE SECTION;
130 int id, pid, iid, mid;
131 char type[USERS_POTYPE_SIZE];
132 EXEC SQL END DECLARE SECTION;
134 id = *(int *)argv[0];
135 EXEC SQL SELECT potype, pop_id, imap_id INTO :type, :pid, :iid
136 FROM users WHERE users_id = :id;
137 if (sqlca.sqlerrd[2] == 0 || (pid == 0 && iid == 0))
142 EXEC SQL SELECT mach_id INTO :mid FROM machine
143 WHERE mach_id = :pid;
144 if (sqlca.sqlerrd[2] == 0)
146 EXEC SQL UPDATE users SET potype = 'POP' WHERE users_id = :id;
147 if (!strcmp(strtrim(type), "POP"))
148 set_pop_usage(mid, 1);
152 EXEC SQL SELECT filsys_id INTO :mid FROM filesys
153 WHERE filsys_id = :iid;
154 if (sqlca.sqlerrd[2] == 0)
156 EXEC SQL UPDATE users SET potype = 'IMAP' WHERE users_id = :id;
159 set_pobox_modtime(q, argv, cl);
160 EXEC SQL UPDATE tblstats SET updates = updates + 1, modtime = SYSDATE
161 WHERE table_name = 'users';
168 /* Add_member_to_list: do list flattening as we go! MAXLISTDEPTH is
169 * how many different ancestors a member is allowed to have.
172 #define MAXLISTDEPTH 2048
174 int add_member_to_list(struct query *q, char **argv, client *cl)
176 EXEC SQL BEGIN DECLARE SECTION;
177 int id, lid, mid, tag, error, who, ref, rowcnt;
178 char *mtype, dtype[IMEMBERS_MEMBER_TYPE_SIZE], *entity;
179 EXEC SQL END DECLARE SECTION;
180 int ancestors[MAXLISTDEPTH], aref[MAXLISTDEPTH], acount, a;
181 int descendants[MAXLISTDEPTH], dref[MAXLISTDEPTH], dcount, d;
183 char *dtypes[MAXLISTDEPTH];
184 char *iargv[3], *buf;
186 lid = *(int *)argv[0];
188 mid = *(int *)argv[2];
189 tag = !strcmp(q->shortname, "atml") ? *(int *)argv[3] : 0;
191 if (acl_access_check(lid, cl))
194 /* if the member is already a direct member of the list, punt */
195 EXEC SQL SELECT COUNT(list_id) INTO :rowcnt FROM imembers
196 WHERE list_id = :lid AND member_id = :mid
197 AND member_type = :mtype AND direct = 1;
200 if (!strcasecmp(mtype, "STRING"))
203 status = id_to_name(mid, STRINGS_TABLE, &buf);
206 if (strchr(buf, '/') || strchr(buf, '|'))
217 EXEC SQL DECLARE csr103 CURSOR FOR
218 SELECT list_id, ref_count FROM imembers
219 WHERE member_id = :lid AND member_type = 'LIST';
222 EXEC SQL OPEN csr103;
227 EXEC SQL FETCH csr103 INTO :id, :ref;
231 ancestors[acount++] = id;
232 if (acount >= MAXLISTDEPTH)
235 EXEC SQL CLOSE csr103;
238 if (acount >= MAXLISTDEPTH)
240 descendants[0] = mid;
245 if (!strcmp(mtype, "LIST"))
247 EXEC SQL DECLARE csr104 CURSOR FOR
248 SELECT member_id, member_type, ref_count
250 WHERE list_id = :mid;
253 EXEC SQL OPEN csr104;
258 EXEC SQL FETCH csr104 INTO :id, :dtype, :ref;
264 dtypes[dcount] = "LIST";
267 dtypes[dcount] = "USER";
270 dtypes[dcount] = "STRING";
273 dtypes[dcount] = "KERBEROS";
280 descendants[dcount++] = id;
281 if (dcount >= MAXLISTDEPTH)
287 EXEC SQL CLOSE csr104;
293 for (a = 0; a < acount; a++)
296 for (d = 0; d < dcount; d++)
298 mid = descendants[d];
300 if (mid == lid && !strcmp(mtype, "LIST"))
302 EXEC SQL SELECT COUNT(ref_count) INTO :rowcnt
304 WHERE list_id = :lid AND member_id = :mid
305 AND member_type = :mtype;
306 ref = aref[a] * dref[d];
309 if (a == 0 && d == 0)
311 EXEC SQL UPDATE imembers
312 SET ref_count = ref_count + :ref, direct = 1, tag = :tag
313 WHERE list_id = :lid AND member_id = :mid
314 AND member_type = :mtype;
318 EXEC SQL UPDATE imembers
319 SET ref_count = ref_count + :ref
320 WHERE list_id = :lid AND member_id = :mid
321 AND member_type = :mtype;
326 incremental_clear_before();
327 if (a == 0 && d == 0)
329 EXEC SQL INSERT INTO imembers
330 (list_id, member_type, member_id, tag, direct, ref_count)
331 VALUES (:lid, :mtype, :mid, :tag, 1, :ref);
335 EXEC SQL INSERT INTO imembers
336 (list_id, member_type, member_id, tag, direct, ref_count)
337 VALUES (:lid, :mtype, :mid, :tag, 0, :ref);
339 iargv[0] = (char *)lid;
341 iargv[2] = (char *)mid;
342 incremental_after(IMEMBERS_TABLE, 0, iargv);
346 lid = *(int *)argv[0];
350 SET modtime = SYSDATE, modby = :who, modwith = :entity
351 WHERE list_id = :lid;
358 /* Delete_member_from_list: do list flattening as we go!
361 int delete_member_from_list(struct query *q, char **argv, client *cl)
363 EXEC SQL BEGIN DECLARE SECTION;
364 int id, lid, mid, cnt, error, who, ref;
365 char *mtype, dtype[IMEMBERS_MEMBER_TYPE_SIZE], *entity;
366 EXEC SQL END DECLARE SECTION;
367 int ancestors[MAXLISTDEPTH], aref[MAXLISTDEPTH], acount, a;
368 int descendants[MAXLISTDEPTH], dref[MAXLISTDEPTH], dcount, d;
369 char *dtypes[MAXLISTDEPTH];
372 lid = *(int *)argv[0];
374 mid = *(int *)argv[2];
376 if (acl_access_check(lid, cl))
379 /* if the member is not a direct member of the list, punt */
380 EXEC SQL SELECT COUNT(list_id) INTO :cnt FROM imembers
381 WHERE list_id = :lid AND member_id = :mid
382 AND member_type = :mtype AND direct = 1;
390 EXEC SQL DECLARE csr105 CURSOR FOR
391 SELECT list_id, ref_count FROM imembers
392 WHERE member_id = :lid AND member_type = 'LIST';
395 EXEC SQL OPEN csr105;
400 EXEC SQL FETCH csr105 INTO :id, :ref;
404 ancestors[acount++] = id;
405 if (acount >= MAXLISTDEPTH)
408 EXEC SQL CLOSE csr105;
411 if (acount >= MAXLISTDEPTH)
413 descendants[0] = mid;
418 if (!strcmp(mtype, "LIST"))
420 EXEC SQL DECLARE csr106 CURSOR FOR
421 SELECT member_id, member_type, ref_count FROM imembers
422 WHERE list_id = :mid;
425 EXEC SQL OPEN csr106;
430 EXEC SQL FETCH csr106 INTO :id, :dtype, :ref;
436 dtypes[dcount] = "LIST";
439 dtypes[dcount] = "USER";
442 dtypes[dcount] = "STRING";
445 dtypes[dcount] = "KERBEROS";
452 descendants[dcount++] = id;
453 if (dcount >= MAXLISTDEPTH)
456 EXEC SQL CLOSE csr106;
462 for (a = 0; a < acount; a++)
465 for (d = 0; d < dcount; d++)
467 mid = descendants[d];
469 if (mid == lid && !strcmp(mtype, "LIST"))
471 EXEC SQL SELECT ref_count INTO :cnt FROM imembers
472 WHERE list_id = :lid AND member_id = :mid AND member_type = :mtype;
473 ref = aref[a] * dref[d];
476 iargv[0] = (char *)lid;
478 iargv[2] = (char *)mid;
479 incremental_before(IMEMBERS_TABLE, 0, iargv);
480 EXEC SQL DELETE FROM imembers
481 WHERE list_id = :lid AND member_id = :mid
482 AND member_type= :mtype;
483 incremental_clear_after();
485 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;
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;
511 int tag_member_of_list(struct query *q, char **argv, client *cl)
513 EXEC SQL BEGIN DECLARE SECTION;
514 int lid, mid, cnt, tag;
516 EXEC SQL END DECLARE SECTION;
519 lid = *(int *)argv[0];
521 mid = *(int *)argv[2];
522 tag = *(int *)argv[3];
524 EXEC SQL SELECT COUNT(member_id) INTO :cnt FROM imembers
525 WHERE member_id = :mid AND member_type = :mtype AND
532 incremental_clear_before();
533 EXEC SQL UPDATE imembers SET tag = :tag WHERE list_id = :lid
534 AND member_type = :mtype AND member_id = :mid;
538 iargv[0] = (char *)lid;
540 iargv[2] = (char *)mid;
541 incremental_after(IMEMBERS_TABLE, 0, iargv);
546 /* Don't allow someone to add someone to a list which is the acl of a
547 * query unless they're on the list acl, even if they're on the amtl
548 * query acl! Also, don't allow someone proxying to add someone to a
551 int acl_access_check(int list_id, client *cl)
553 EXEC SQL BEGIN DECLARE SECTION;
554 int c1, c2, lid = list_id, acl_id, memacl_id;
555 char acl_type[LIST_ACL_TYPE_SIZE], memacl_type[LIST_ACL_TYPE_SIZE];
556 EXEC SQL END DECLARE SECTION;
558 /* Check if the list is directly a capacl */
559 EXEC SQL SELECT COUNT(list_id) INTO :c1 FROM capacls WHERE list_id=:lid;
561 /* Check if the list is a member (direct or indirect) of a list that
563 EXEC SQL SELECT COUNT(l1.list_id) INTO :c2 FROM list l1, list l2,
564 imembers im, capacls c WHERE c.list_id = l2.list_id AND
565 im.list_id = l2.list_id AND im.member_type = 'LIST' AND
566 im.member_id = l1.list_id AND l1.list_id = :lid;
568 if (c1 == 0 && c2 == 0)
574 EXEC SQL SELECT acl_type, acl_id, memacl_type, memacl_id
575 INTO :acl_type, :acl_id, :memacl_type, :memacl_id
576 FROM list WHERE list_id=:lid;
578 if (!find_member(acl_type, acl_id, cl))
580 if (!find_member(memacl_type, memacl_id, cl))
588 /* get_ace_use - given a type and a name, return a type and a name.
589 * The ace_type is one of "LIST", "USER", "RLIST", or "RUSER" in argv[0],
590 * and argv[1] will contain the ID of the entity in question. The R*
591 * types mean to recursively look at every containing list, not just
592 * when the object in question is a direct member. On return, the
593 * usage type will be one of LIST, SERVICE, FILESYS, QUOTA, QUERY, or ZEPHYR.
596 int get_ace_use(struct query *q, char *argv[], client *cl,
597 int (*action)(int, char *[], void *), void *actarg)
600 EXEC SQL BEGIN DECLARE SECTION;
603 EXEC SQL END DECLARE SECTION;
604 struct save_queue *sq;
607 aid = *(int *)argv[1];
608 if (!strcmp(atype, "LIST") || !strcmp(atype, "USER") ||
609 !strcmp(atype, "KERBEROS"))
610 return get_ace_internal(atype, aid, action, actarg);
613 if (!strcmp(atype, "RLIST"))
615 sq_save_data(sq, (void *)aid);
616 /* get all the list_id's of containing lists */
617 EXEC SQL DECLARE csr107 CURSOR FOR
618 SELECT list_id FROM imembers
619 WHERE member_type = 'LIST' AND member_id = :aid;
622 EXEC SQL OPEN csr107;
627 EXEC SQL FETCH csr107 INTO :listid;
630 sq_save_unique_data(sq, (void *)listid);
632 EXEC SQL CLOSE csr107;
633 /* now process each one */
634 while (sq_get_data(sq, &id))
636 if (get_ace_internal("LIST", id, action, actarg) == MR_SUCCESS)
641 if (!strcmp(atype, "RUSER"))
643 EXEC SQL DECLARE csr108 CURSOR FOR
644 SELECT list_id FROM imembers
645 WHERE member_type = 'USER' AND member_id = :aid;
648 EXEC SQL OPEN csr108;
653 EXEC SQL FETCH csr108 INTO :listid;
656 sq_save_data(sq, (void *)listid);
658 EXEC SQL CLOSE csr108;
659 /* now process each one */
660 while (sq_get_data(sq, &id))
662 if (get_ace_internal("LIST", id, action, actarg) == MR_SUCCESS)
665 if (get_ace_internal("USER", aid, action, actarg) == MR_SUCCESS)
669 if (!strcmp(atype, "RKERBEROS"))
671 EXEC SQL DECLARE csr109 CURSOR FOR
672 SELECT list_id FROM imembers
673 WHERE member_type = 'KERBEROS' AND member_id = :aid;
676 EXEC SQL OPEN csr109;
681 EXEC SQL FETCH csr109 INTO :listid;
684 sq_save_data(sq, (void *)listid);
686 EXEC SQL CLOSE csr109;
687 /* now process each one */
688 while (sq_get_data(sq, &id))
690 if (get_ace_internal("LIST", id, action, actarg) == MR_SUCCESS)
693 if (get_ace_internal("KERBEROS", aid, action, actarg) == MR_SUCCESS)
706 /* This looks up a single list or user for ace use. atype must be "USER"
707 * or "LIST", and aid is the ID of the corresponding object. This is used
708 * by get_ace_use above.
711 int get_ace_internal(char *atype, int aid,
712 int (*action)(int, char *[], void *), void *actarg)
716 EXEC SQL BEGIN DECLARE SECTION;
717 char name[MAX_FIELD_WIDTH], *type = atype;
719 EXEC SQL END DECLARE SECTION;
722 if (!strcmp(atype, "LIST"))
724 rargv[0] = "FILESYS";
725 EXEC SQL DECLARE csr110 CURSOR FOR
726 SELECT label FROM filesys
730 EXEC SQL OPEN csr110;
735 EXEC SQL FETCH csr110 INTO :name;
738 (*action)(2, rargv, actarg);
741 EXEC SQL CLOSE csr110;
744 EXEC SQL DECLARE csr111 CURSOR FOR
745 SELECT capability FROM capacls
749 EXEC SQL OPEN csr111;
754 EXEC SQL FETCH csr111 INTO :name;
757 (*action)(2, rargv, actarg);
760 EXEC SQL CLOSE csr111;
762 else if (!strcmp(atype, "USER"))
764 rargv[0] = "FILESYS";
765 EXEC SQL DECLARE csr112 CURSOR FOR
766 SELECT label FROM filesys
770 EXEC SQL OPEN csr112;
775 EXEC SQL FETCH csr112 INTO :name;
778 (*action)(2, rargv, actarg);
781 EXEC SQL CLOSE csr112;
785 EXEC SQL DECLARE csr113 CURSOR FOR
786 SELECT name FROM list
787 WHERE (acl_type = :type AND acl_id = :id)
788 OR (memacl_type = :type AND memacl_id = :id);
791 EXEC SQL OPEN csr113;
796 EXEC SQL FETCH csr113 INTO :name;
799 (*action)(2, rargv, actarg);
802 EXEC SQL CLOSE csr113;
804 rargv[0] = "SERVICE";
805 EXEC SQL DECLARE csr114 CURSOR FOR
806 SELECT name FROM servers
807 WHERE acl_type = :type AND acl_id = :id;
810 EXEC SQL OPEN csr114;
815 EXEC SQL FETCH csr114 INTO :name;
818 (*action)(2, rargv, actarg);
821 EXEC SQL CLOSE csr114;
823 rargv[0] = "HOSTACCESS";
824 EXEC SQL DECLARE csr115 CURSOR FOR
825 SELECT name FROM machine m, hostaccess ha
826 WHERE m.mach_id = ha.mach_id AND ha.acl_type = :type
830 EXEC SQL OPEN csr115;
835 EXEC SQL FETCH csr115 INTO :name;
838 (*action)(2, rargv, actarg);
841 EXEC SQL CLOSE csr115;
843 rargv[0] = "MACHINE";
844 EXEC SQL DECLARE csr115a CURSOR FOR
845 SELECT name FROM machine m
846 WHERE m.owner_type = :type
847 AND m.owner_id = :id;
850 EXEC SQL OPEN csr115a;
855 EXEC SQL FETCH csr115a INTO :name;
858 (*action)(2, rargv, actarg);
861 EXEC SQL CLOSE csr115a;
864 EXEC SQL DECLARE csr116 CURSOR FOR
865 SELECT class FROM zephyr z
866 WHERE z.xmt_type = :type AND z.xmt_id = :id
867 OR z.sub_type = :type AND z.sub_id = :id
868 OR z.iws_type = :type AND z.iws_id = :id
869 OR z.iui_type = :type AND z.iui_id = :id
870 OR z.owner_type = :type AND z.owner_id = :id;
873 EXEC SQL OPEN csr116;
878 EXEC SQL FETCH csr116 INTO :name;
881 (*action)(2, rargv, actarg);
884 EXEC SQL CLOSE csr116;
892 int ghbo_internal(char *atype, int aid,
893 int (*action)(int, char *[], void *), void *actarg)
897 EXEC SQL BEGIN DECLARE SECTION;
898 char name[MACHINE_NAME_SIZE], *type = atype;
900 EXEC SQL END DECLARE SECTION;
903 EXEC SQL DECLARE csr115b CURSOR FOR
904 SELECT name FROM machine m
905 WHERE m.owner_type = :type
906 AND m.owner_id = :id;
909 EXEC SQL OPEN csr115b;
914 EXEC SQL FETCH csr115b INTO :name;
917 (*action)(1, rargv, actarg);
920 EXEC SQL CLOSE csr115b;
927 /* get_host_by_owner - like gaus but limited to hosts */
928 int get_host_by_owner(struct query *q, char *argv[], client *cl,
929 int (*action)(int, char *[], void *), void *actarg)
932 EXEC SQL BEGIN DECLARE SECTION;
935 EXEC SQL END DECLARE SECTION;
936 struct save_queue *sq;
939 aid = *(int *)argv[1];
940 if (!strcmp(atype, "LIST") || !strcmp(atype, "USER") ||
941 !strcmp(atype, "KERBEROS"))
942 return ghbo_internal(atype, aid, action, actarg);
945 if (!strcmp(atype, "RLIST"))
947 sq_save_data(sq, (void *)aid);
948 /* get all the list_id's of containing lists */
949 EXEC SQL DECLARE csr107q CURSOR FOR
950 SELECT list_id FROM imembers
951 WHERE member_type = 'LIST' AND member_id = :aid;
954 EXEC SQL OPEN csr107q;
959 EXEC SQL FETCH csr107q INTO :listid;
962 sq_save_unique_data(sq, (void *)listid);
964 EXEC SQL CLOSE csr107q;
965 /* now process each one */
966 while (sq_get_data(sq, &id))
968 if (ghbo_internal("LIST", id, action, actarg) == MR_SUCCESS)
973 if (!strcmp(atype, "RUSER"))
975 EXEC SQL DECLARE csr108q CURSOR FOR
976 SELECT list_id FROM imembers
977 WHERE member_type = 'USER' AND member_id = :aid;
980 EXEC SQL OPEN csr108q;
985 EXEC SQL FETCH csr108q INTO :listid;
988 sq_save_data(sq, (void *)listid);
990 EXEC SQL CLOSE csr108q;
991 /* now process each one */
992 while (sq_get_data(sq, &id))
994 if (ghbo_internal("LIST", id, action, actarg) == MR_SUCCESS)
997 if (ghbo_internal("USER", aid, action, actarg) == MR_SUCCESS)
1001 if (!strcmp(atype, "RKERBEROS"))
1003 EXEC SQL DECLARE csr109q CURSOR FOR
1004 SELECT list_id FROM imembers
1005 WHERE member_type = 'KERBEROS' AND member_id = :aid;
1008 EXEC SQL OPEN csr109q;
1013 EXEC SQL FETCH csr109q INTO :listid;
1016 sq_save_data(sq, (void *)listid);
1018 EXEC SQL CLOSE csr109q;
1019 /* now process each one */
1020 while (sq_get_data(sq, &id))
1022 if (ghbo_internal("LIST", id, action, actarg) == MR_SUCCESS)
1025 if (ghbo_internal("KERBEROS", aid, action, actarg) == MR_SUCCESS)
1037 /* get_lists_of_member - given a type and a name, return the name and flags
1038 * of all of the lists of the given member. The member_type is one of
1039 * "LIST", "USER", "STRING", "RLIST", "RUSER", or "RSTRING" in argv[0],
1040 * and argv[1] will contain the ID of the entity in question. The R*
1041 * types mean to recursively look at every containing list, not just
1042 * when the object in question is a direct member.
1045 int get_lists_of_member(struct query *q, char *argv[], client *cl,
1046 int (*action)(int, char *[], void *), void *actarg)
1048 int found = 0, direct = 1;
1050 EXEC SQL BEGIN DECLARE SECTION;
1053 char name[LIST_NAME_SIZE];
1054 char active[5], public[5], hidden[5], maillist[5], grouplist[5];
1055 EXEC SQL END DECLARE SECTION;
1058 aid = *(int *)argv[1];
1059 if (!strcmp(atype, "RLIST"))
1064 if (!strcmp(atype, "RUSER"))
1069 if (!strcmp(atype, "RSTRING"))
1074 if (!strcmp(atype, "RKERBEROS"))
1084 rargv[4] = maillist;
1085 rargv[5] = grouplist;
1088 EXEC SQL DECLARE csr117a CURSOR FOR
1089 SELECT l.name, l.active, l.publicflg, l.hidden, l.maillist, l.grouplist
1090 FROM list l, imembers im
1091 WHERE l.list_id = im.list_id AND im.direct = 1
1092 AND im.member_type = :atype AND im.member_id = :aid;
1095 EXEC SQL OPEN csr117a;
1100 EXEC SQL FETCH csr117a
1101 INTO :name, :active, :public, :hidden, :maillist, :grouplist;
1104 (*action)(6, rargv, actarg);
1107 EXEC SQL CLOSE csr117a;
1111 EXEC SQL DECLARE csr117b CURSOR FOR
1112 SELECT l.name, l.active, l.publicflg, l.hidden, l.maillist, l.grouplist
1113 FROM list l, imembers im
1114 WHERE l.list_id = im.list_id
1115 AND im.member_type = :atype AND im.member_id = :aid;
1118 EXEC SQL OPEN csr117b;
1123 EXEC SQL FETCH csr117b
1124 INTO :name, :active, :public, :hidden, :maillist, :grouplist;
1127 (*action)(6, rargv, actarg);
1130 EXEC SQL CLOSE csr117b;
1141 /* qualified_get_lists: passed "TRUE", "FALSE", or "DONTCARE" for each of
1142 * the five flags associated with each list. It will return the name of
1143 * each list that meets the quailifications. It does this by building a
1144 * where clause based on the arguments, then doing a retrieve.
1147 static char *lflags[5] = { "active", "publicflg", "hidden", "maillist", "grouplist" };
1149 int qualified_get_lists(struct query *q, char *argv[], client *cl,
1150 int (*action)(int, char *[], void *), void *actarg)
1152 return qualified_get(q, argv, action, actarg, "l.list_id != 0",
1153 "l", "name", lflags);
1157 int get_members_of_list(struct query *q, char *argv[], client *cl,
1158 int (*action)(int, char *[], void *), void *actarg)
1160 EXEC SQL BEGIN DECLARE SECTION;
1161 int list_id, direct;
1162 char member_name[MAX_FIELD_WIDTH], tag[STRINGS_STRING_SIZE];
1163 EXEC SQL END DECLARE SECTION;
1167 /* For gmol or gtml, only get direct members. For geml, get all. */
1168 if (!strcmp(q->shortname, "geml"))
1173 /* For gmol or geml, only return type and name. For gtml, return tag too. */
1174 if (!strcmp(q->shortname, "gtml"))
1179 list_id = *(int *)argv[0];
1181 targv[1] = member_name;
1185 EXEC SQL DECLARE csr119 CURSOR FOR
1186 SELECT u.login, s.string FROM users u, imembers im, strings s
1187 WHERE im.list_id = :list_id AND im.member_type = 'USER'
1188 AND im.member_id = u.users_id AND im.direct > :direct
1189 AND s.string_id = im.tag ORDER BY 1;
1192 EXEC SQL OPEN csr119;
1197 EXEC SQL FETCH csr119 INTO :member_name, :tag;
1200 (*action)(targc, targv, actarg);
1202 EXEC SQL CLOSE csr119;
1207 EXEC SQL DECLARE csr120 CURSOR FOR
1208 SELECT l.name, s.string FROM list l, imembers im, strings s
1209 WHERE im.list_id = :list_id AND im.member_type = 'LIST'
1210 AND im.member_id = l.list_id AND im.direct > :direct
1211 AND s.string_id = im.tag ORDER BY 1;
1214 EXEC SQL OPEN csr120;
1219 EXEC SQL FETCH csr120 INTO :member_name, :tag;
1222 (*action)(targc, targv, actarg);
1224 EXEC SQL CLOSE csr120;
1228 targv[0] = "STRING";
1229 EXEC SQL DECLARE csr121 CURSOR FOR
1230 SELECT str.string, s.string FROM strings str, imembers im, strings s
1231 WHERE im.list_id = :list_id AND im.member_type = 'STRING'
1232 AND im.member_id = str.string_id AND im.direct > :direct
1233 AND s.string_id = im.tag ORDER BY 1;
1236 EXEC SQL OPEN csr121;
1241 EXEC SQL FETCH csr121 INTO :member_name, :tag;
1244 (*action)(targc, targv, actarg);
1246 EXEC SQL CLOSE csr121;
1250 targv[0] = "KERBEROS";
1251 EXEC SQL DECLARE csr122 CURSOR FOR
1252 SELECT str.string, s.string FROM strings str, imembers im, strings s
1253 WHERE im.list_id = :list_id AND im.member_type = 'KERBEROS'
1254 AND im.member_id = str.string_id AND im.direct > :direct
1255 AND s.string_id = im.tag ORDER BY 1;
1258 EXEC SQL OPEN csr122;
1263 EXEC SQL FETCH csr122 INTO :member_name, :tag;
1266 (*action)(targc, targv, actarg);
1268 EXEC SQL CLOSE csr122;
1276 /* count_members_of_list: this is a simple query, but it cannot be done
1277 * through the dispatch table.
1280 int count_members_of_list(struct query *q, char *argv[], client *cl,
1281 int (*action)(int, char *[], void *), void *actarg)
1283 EXEC SQL BEGIN DECLARE SECTION;
1285 EXEC SQL END DECLARE SECTION;
1286 char *rargv[1], countbuf[5];
1288 list = *(int *)argv[0];
1289 rargv[0] = countbuf;
1290 EXEC SQL SELECT count (*) INTO :ct FROM imembers
1291 WHERE list_id = :list AND direct = 1;
1294 sprintf(countbuf, "%d", ct);
1295 (*action)(1, rargv, actarg);
1300 /* qualified_get_server: passed "TRUE", "FALSE", or "DONTCARE" for each of
1301 * the three flags associated with each service. It will return the name of
1302 * each service that meets the quailifications. It does this by building a
1303 * where clause based on the arguments, then doing a retrieve.
1306 static char *sflags[3] = { "enable", "inprogress", "harderror" };
1308 int qualified_get_server(struct query *q, char *argv[], client *cl,
1309 int (*action)(int, char *[], void *), void *actarg)
1311 return qualified_get(q, argv, action, actarg, "s.name is not null",
1312 "s", "name", sflags);
1313 /* of course, name will never be null, but we need something there
1314 to make qualified_get happy */
1318 /* generic qualified get routine, used by qualified_get_lists,
1319 * qualified_get_server, and qualified_get_serverhost.
1321 * start - a simple where clause, must not be empty
1322 * range - the name of the range variable
1323 * field - the field to return
1324 * flags - an array of strings, names of the flag variables
1327 int qualified_get(struct query *q, char *argv[],
1328 int (*action)(int, char *[], void *), void *actarg,
1329 char *start, char *range, char *field, char *flags[])
1335 strcpy(qual, start);
1336 for (i = 0; i < q->argc; i++)
1338 if (!strcmp(argv[i], "TRUE"))
1340 sprintf(buf, " AND %s.%s != 0", range, flags[i]);
1343 else if (!strcmp(argv[i], "FALSE"))
1345 sprintf(buf, " AND %s.%s = 0", range, flags[i]);
1350 sprintf(stmt_buf, "SELECT %s.%s FROM %s %s WHERE %s", range, field,
1351 table_name[q->rtable], range, qual);
1352 return do_for_all_rows(stmt_buf, 1, action, actarg);
1356 /* qualified_get_serverhost: passed "TRUE", "FALSE", or "DONTCARE" for each of
1357 * the five flags associated with each serverhost. It will return the name of
1358 * each service and host that meets the quailifications. It does this by
1359 * building a where clause based on the arguments, then doing a retrieve.
1362 static char *shflags[6] = { "service", "enable", "override", "success",
1363 "inprogress", "hosterror" };
1365 int qualified_get_serverhost(struct query *q, char *argv[], client *cl,
1366 int (*action)(int, char *[], void *),
1369 char qual[256], buf[32];
1372 sprintf(qual, "m.mach_id = sh.mach_id AND sh.service = UPPER('%s')",
1374 for (i = 1; i < q->argc; i++)
1376 if (!strcmp(argv[i], "TRUE"))
1378 sprintf(buf, " AND sh.%s != 0", shflags[i]);
1381 else if (!strcmp(argv[i], "FALSE"))
1383 sprintf(buf, " AND sh.%s = 0", shflags[i]);
1388 sprintf(stmt_buf, "SELECT sh.service, m.name FROM serverhosts sh, "
1389 "machine m WHERE %s", qual);
1390 return do_for_all_rows(stmt_buf, 2, action, actarg);
1394 /* register_user - change user's login name and allocate a pobox, group,
1395 * filesystem, and quota for them. The user's status must start out as 0,
1396 * and is left as 2. Arguments are: user's UID, new login name, and
1397 * pobox type ("POP" = POP, "IMAP" or numeric = IMAP)
1400 int register_user(struct query *q, char **argv, client *cl)
1402 EXEC SQL BEGIN DECLARE SECTION;
1403 char *login, *entity;
1404 char directory[FILESYS_NAME_SIZE], machname[MACHINE_NAME_SIZE];
1405 char dir[NFSPHYS_DIR_SIZE], *potype;
1406 int who, rowcount, mid, uid, users_id;
1407 int ostatus, nstatus, fsidval, popid;
1410 static int m_id, def_quota, def_imap_quota, list_id;
1411 EXEC SQL END DECLARE SECTION;
1412 char buffer[256], *aargv[3];
1416 EXEC SQL SELECT list_id INTO :list_id FROM list
1417 WHERE name = 'wheel';
1419 EXEC SQL SELECT mach_id INTO :m_id FROM machine
1420 WHERE name = 'ATHENA.MIT.EDU';
1422 EXEC SQL SELECT value INTO :def_quota FROM numvalues
1423 WHERE name = 'def_quota';
1424 if (sqlca.sqlerrd[2] != 1)
1427 EXEC SQL SELECT value INTO :def_imap_quota FROM numvalues
1428 WHERE name = 'def_imap_quota';
1429 if (sqlca.sqlerrd[2] != 1)
1433 entity = cl->entity;
1434 who = cl->client_id;
1436 uid = atoi(argv[0]);
1441 EXEC SQL SELECT users_id, status INTO :users_id, :ostatus
1443 WHERE unix_uid = :uid AND (status = 0 OR status = 5 OR status = 6);
1445 if (sqlca.sqlerrd[2] == 0)
1447 if (sqlca.sqlerrd[2] > 1)
1448 return MR_NOT_UNIQUE;
1450 /* check new login name */
1451 EXEC SQL SELECT COUNT(login) INTO :rowcount FROM users
1452 WHERE login = :login AND users_id != :users_id;
1457 EXEC SQL SELECT COUNT(name) INTO :rowcount FROM list
1458 WHERE LOWER(name) = :login;
1463 EXEC SQL SELECT COUNT(label) INTO :rowcount FROM filesys
1464 WHERE label = :login;
1469 EXEC SQL SELECT COUNT(label) INTO :rowcount
1470 FROM filesys WHERE label = :login || '.po';
1475 EXEC SQL SELECT owner INTO :tmp FROM filesys
1476 WHERE label = :login || '.po';
1479 if ((ostatus == 0) || (tmp != users_id))
1484 EXEC SQL SELECT COUNT(name) INTO :rowcount FROM alias
1485 WHERE ( name = :login OR name = :login || '.po' )
1486 AND type = 'FILESYS';
1491 com_err(whoami, 0, "login name OK");
1493 EXEC SQL SELECT COUNT(potype) INTO :rowcount FROM users WHERE
1494 login = :login AND potype = 'POP';
1500 /* choose type and location for pobox */
1503 if (!strcmp(potype, "POP"))
1506 EXEC SQL DECLARE csr130 CURSOR FOR
1507 SELECT sh.mach_id, m.name FROM serverhosts sh, machine m
1508 WHERE sh.service = 'POP' AND sh.mach_id = m.mach_id
1509 AND sh.value2 - sh.value1 =
1510 (SELECT MAX(value2 - value1) FROM serverhosts WHERE service = 'POP');
1513 EXEC SQL OPEN csr130;
1516 EXEC SQL FETCH csr130 INTO :popid, :machname;
1517 if (sqlca.sqlerrd[2] == 0)
1519 EXEC SQL CLOSE csr130;
1526 EXEC SQL CLOSE csr130;
1531 EXEC SQL UPDATE users SET potype = 'POP', pop_id = :popid
1532 WHERE users_id = :users_id;
1533 com_err(whoami, 0, "pobox set to POP:%s", strtrim(machname));
1537 /* Select all IMAP nfsphys entries in order of increasing
1538 * (allocated - partsize). The partitions will almost always be
1539 * overallocated, but we choose the one that is the least
1544 EXEC SQL DECLARE csr_rusr_imap CURSOR FOR
1545 SELECT np.allocated - np.partsize, np.nfsphys_id, np.mach_id,
1546 np.dir, m.name FROM serverhosts sh, nfsphys np, machine m
1547 WHERE sh.service = 'POSTOFFICE' AND sh.mach_id = np.mach_id
1548 AND m.mach_id = np.mach_id
1552 EXEC SQL OPEN csr_rusr_imap;
1555 EXEC SQL FETCH csr_rusr_imap INTO :tmp, :npid, :mid, :dir, :machname;
1556 if (sqlca.sqlerrd[2] == 0)
1558 EXEC SQL CLOSE csr_rusr_imap;
1565 EXEC SQL CLOSE csr_rusr_imap;
1570 /* create filesystem */
1571 if (set_next_object_id("filsys_id", FILESYS_TABLE, 0))
1573 incremental_clear_before();
1575 EXEC SQL SELECT value INTO :popid FROM numvalues
1576 WHERE numvalues.name = 'filsys_id';
1577 EXEC SQL INSERT INTO filesys
1578 (filsys_id, phys_id, label, type, mach_id, name,
1579 mount, rwaccess, comments, owner, owners, createflg,
1580 lockertype, modtime, modby, modwith)
1582 (:popid, :npid, :login || '.po', 'IMAP', :mid, :dir,
1583 CHR(0), 'w', 'IMAP box', :users_id, :list_id, 1,
1584 'USER', SYSDATE, :who, :entity);
1588 if (sqlca.sqlerrd[2] != 1)
1590 sprintf(buffer, "fs.filsys_id = %d", popid);
1591 incremental_after(FILESYS_TABLE, buffer, 0);
1594 incremental_clear_before();
1595 EXEC SQL INSERT INTO quota
1596 (entity_id, filsys_id, type, quota, phys_id, modtime, modby, modwith)
1597 VALUES (:users_id, :popid, 'USER', :def_imap_quota, :npid,
1598 SYSDATE, :who, :entity);
1601 if (sqlca.sqlerrd[2] != 1)
1606 sprintf(buffer, "q.entity_id = %d and q.filsys_id = %d and "
1607 "q.type = 'USER'", users_id, popid);
1608 incremental_after(QUOTA_TABLE, buffer, aargv);
1612 EXEC SQL UPDATE nfsphys SET allocated = allocated + :def_imap_quota
1613 WHERE nfsphys_id = :npid;
1615 EXEC SQL UPDATE users SET potype = 'IMAP', imap_id = :popid
1616 WHERE users_id = :users_id;
1617 com_err(whoami, 0, "pobox set to IMAP:%s:%s", strtrim(machname),
1622 /* change login name, set pobox */
1623 sprintf(buffer, "u.users_id = %d", users_id);
1624 incremental_before(USERS_TABLE, buffer, 0);
1626 if (ostatus == 5 || ostatus == 6)
1628 EXEC SQL UPDATE users SET login = :login, status = :nstatus,
1629 modtime = SYSDATE, modby = :who, modwith = :entity,
1630 pmodtime = SYSDATE, pmodby = :who, pmodwith = :entity
1631 WHERE users_id = :users_id;
1635 if (sqlca.sqlerrd[2] != 1)
1638 /* Only update usage count if we created a POP pobox. */
1639 if (!strcmp(potype, "POP") && !po_exists)
1640 set_pop_usage(mid, 1);
1642 com_err(whoami, 0, "set login name to %s", login);
1643 incremental_after(USERS_TABLE, buffer, 0);
1645 /* create filesystem */
1646 if (set_next_object_id("filsys_id", FILESYS_TABLE, 0))
1648 incremental_clear_before();
1649 if (islower(login[0]) && islower(login[1]))
1651 sprintf(directory, "/afs/athena.mit.edu/user/%c/%c/%s",
1652 login[0], login[1], login);
1655 sprintf(directory, "/afs/athena.mit.edu/user/other/%s", login);
1657 EXEC SQL SELECT value INTO :fsidval FROM numvalues
1658 WHERE numvalues.name = 'filsys_id';
1659 EXEC SQL INSERT INTO filesys
1660 (filsys_id, phys_id, label, type, mach_id, name,
1661 mount, rwaccess, comments, owner, owners, createflg,
1662 lockertype, modtime, modby, modwith)
1664 (:fsidval, 0, :login, 'AFS', :m_id, :directory,
1665 '/mit/' || :login, 'w', 'User Locker', :users_id, :list_id, 1,
1666 'HOMEDIR', SYSDATE, :who, :entity);
1670 if (sqlca.sqlerrd[2] != 1)
1672 sprintf(buffer, "fs.filsys_id = %d", fsidval);
1673 incremental_after(FILESYS_TABLE, buffer, 0);
1676 incremental_clear_before();
1677 EXEC SQL INSERT INTO quota
1678 (entity_id, filsys_id, type, quota, phys_id, modtime, modby, modwith)
1679 VALUES (0, :fsidval, 'ANY', :def_quota, 0, SYSDATE, :who, :entity);
1682 if (sqlca.sqlerrd[2] != 1)
1687 sprintf(buffer, "q.entity_id = 0 and q.filsys_id = %d and q.type = 'ANY'",
1689 incremental_after(QUOTA_TABLE, buffer, aargv);
1690 com_err(whoami, 0, "quota of %d assigned", def_quota);
1694 EXEC SQL UPDATE tblstats SET updates = updates + 1, modtime = SYSDATE
1695 WHERE table_name = 'users';
1696 EXEC SQL UPDATE tblstats SET appends = appends + 1, modtime = SYSDATE
1697 WHERE table_name = 'filesys' OR table_name = 'quota';
1703 /** set_pop_usage - incr/decr usage count for pop server in serverhosts talbe
1707 ** delta (will be +/- 1)
1710 ** - incr/decr value field in serverhosts table for pop/mach_id
1714 int set_pop_usage(id, cnt)
1717 EXEC SQL BEGIN DECLARE SECTION;
1718 int iid = id, icnt = cnt;
1719 EXEC SQL END DECLARE SECTION;
1721 EXEC SQL UPDATE serverhosts SET value1 = value1 + :icnt
1722 WHERE serverhosts.service = 'POP' AND serverhosts.mach_id = :iid;
1730 int do_user_reservation(struct query *q, char *argv[], client *cl)
1732 EXEC SQL BEGIN DECLARE SECTION;
1733 char resv[USERS_RESERVATIONS_SIZE];
1734 char *trans, name[ALIAS_NAME_SIZE];
1736 EXEC SQL END DECLARE SECTION;
1738 uid = *(int *)argv[0];
1741 EXEC SQL SELECT UPPER(name) INTO :name FROM alias
1742 WHERE type = 'RESERVE' AND LOWER(trans) = LOWER(:trans);
1745 if (sqlca.sqlerrd[2] != 1)
1749 EXEC SQL SELECT reservations INTO :resv FROM users
1750 WHERE users_id = :uid;
1755 if (!strcmp(q->shortname, "aurv"))
1757 if (strchr(resv, *name))
1759 if (strlen(resv) == USERS_RESERVATIONS_SIZE - 1)
1760 return MR_ARG_TOO_LONG;
1766 char *p = strchr(resv, *name);
1769 memmove(p, p + 1, strlen(p));
1772 EXEC SQL UPDATE users SET reservations = NVL(:resv, CHR(0))
1773 WHERE users_id = :uid;
1777 EXEC SQL UPDATE tblstats SET updates = updates + 1, modtime = SYSDATE
1778 WHERE table_name = 'users';
1779 return set_modtime_by_id(q, argv, cl);
1782 int get_user_reservations(struct query *q, char **argv, client *cl,
1783 int (*action)(int, char *[], void *), void *actarg)
1785 EXEC SQL BEGIN DECLARE SECTION;
1786 char resv[USERS_RESERVATIONS_SIZE], *p;
1787 char trans[ALIAS_TRANS_SIZE], name[2] = { 0, 0 };
1789 EXEC SQL END DECLARE SECTION;
1792 uid = *(int *)argv[0];
1794 EXEC SQL SELECT reservations INTO :resv FROM users
1795 WHERE users_id = :uid;
1801 while (*p && !isspace(*p))
1803 name[0] = toupper(*p);
1804 EXEC SQL SELECT trans INTO :trans FROM alias
1805 WHERE type = 'RESERVE' AND UPPER(name) = :name;
1808 if (sqlca.sqlerrd[2] != 1)
1809 sprintf(trans, "Unknown (%s)", name);
1810 (*action)(1, targv, actarg);
1816 int get_user_by_reservation(struct query *q, char **argv, client *cl,
1817 int (*action)(int, char *[], void *), void *actarg)
1819 EXEC SQL BEGIN DECLARE SECTION;
1820 char resv[USERS_RESERVATIONS_SIZE], login[USERS_LOGIN_SIZE];
1821 char *trans, name[ALIAS_NAME_SIZE];
1823 EXEC SQL END DECLARE SECTION;
1828 EXEC SQL SELECT UPPER(name) INTO :name FROM alias
1829 WHERE type = 'RESERVE' AND LOWER(trans) = LOWER(:trans);
1832 if (sqlca.sqlerrd[2] != 1)
1836 EXEC SQL DECLARE csr_gubr CURSOR FOR
1837 SELECT login FROM users WHERE reservations LIKE '%' || :name || '%';
1838 EXEC SQL OPEN csr_gubr;
1845 EXEC SQL FETCH csr_gubr INTO :login;
1848 (*action)(1, targv, actarg);
1850 EXEC SQL CLOSE csr_gubr;
1855 int update_container(struct query *q, char *argv[], client *cl)
1857 EXEC SQL BEGIN DECLARE SECTION;
1858 int cnt_id, acl_id, memacl_id, who;
1859 char name[CONTAINERS_NAME_SIZE], newchildname[CONTAINERS_NAME_SIZE];
1860 char* newname, *entity, *description, *location, *contact, *acl_type, *memacl_type;
1861 EXEC SQL END DECLARE SECTION;
1864 char childname[CONTAINERS_NAME_SIZE];
1866 cnt_id = *(int *)argv[0];
1868 description = argv[2];
1872 acl_id = *(int *)argv[6];
1873 memacl_type = argv[7];
1874 memacl_id = *(int *)argv[8];
1875 entity = cl->entity;
1876 who = cl->client_id;
1878 EXEC SQL SELECT name INTO :name
1880 WHERE cnt_id = :cnt_id;
1882 /* trim off the trailing spaces */
1883 strcpy(name, strtrim(name));
1885 /* if we are renaming the container */
1886 if (strcmp(name, newname))
1888 /* make sure that only the name part of the name has been changed */
1889 tmpchar = strrchr(name, '/');
1890 /* not a top-level name */
1893 cnt = tmpchar - name + 1;
1894 /* the parent part of the old and new name should be identical */
1895 if (strncmp(name, newname, cnt))
1896 return MR_NEW_CONTAINER_NAME;
1898 /* top level name, new name should be a top level name too */
1901 if (strrchr(newname, '/'))
1902 return MR_NEW_CONTAINER_NAME;
1905 /* update the name for this container */
1906 EXEC SQL UPDATE containers
1908 WHERE cnt_id = :cnt_id;
1913 /* get names for its child containers */
1914 EXEC SQL DECLARE csr_ucon CURSOR FOR
1915 SELECT name, cnt_id FROM containers WHERE name LIKE :name || '/' || '%';
1917 EXEC SQL OPEN csr_ucon;
1923 EXEC SQL FETCH csr_ucon INTO :childname, :childid;
1927 strcpy(childname, strtrim(childname));
1928 /* concatenate the new parent name with the existing sub-container name
1929 * we get the sub-containers new name */
1930 tmpchar = childname + strlen(name);
1931 strcpy(newchildname, newname);
1932 strcat(newchildname, tmpchar);
1934 /* update the name */
1935 EXEC SQL UPDATE containers
1936 SET name = :newchildname, modtime = SYSDATE, modby = :who, modwith = :entity
1937 WHERE cnt_id = :childid;
1943 EXEC SQL CLOSE csr_ucon;
1948 /* update the remaining fields */
1949 EXEC SQL UPDATE containers
1950 SET description = NVL(:description, CHR(0)), location = NVL(:location, CHR(0)),
1951 contact = NVL(:contact, CHR(0)), acl_type = :acl_type, acl_id = :acl_id,
1952 memacl_type = :memacl_type, memacl_id = :memacl_id,
1953 modtime = SYSDATE, modby = :who, modwith = :entity
1954 WHERE cnt_id = :cnt_id;
1962 int get_machines_of_container(struct query *q, char *argv[], client *cl,
1963 int (*action)(int, char *[], void *), void *actarg)
1965 EXEC SQL BEGIN DECLARE SECTION;
1966 int cnt_id, isrecursive;
1967 char machinename[MACHINE_NAME_SIZE], containername[CONTAINERS_NAME_SIZE];
1969 EXEC SQL END DECLARE SECTION;
1971 char querystring[512], tmp [256];
1975 rargv[0] = machinename;
1976 rargv[1] = containername;
1978 cnt_id = *(int *)argv[0];
1979 isrecursive = atoi(argv[1]);
1981 /* get the container name */
1983 EXEC SQL SELECT name INTO :containername
1985 WHERE cnt_id = :cnt_id;
1987 /* trim off the trailing spaces */
1988 strcpy(containername, strtrim(containername));
1990 strcpy(querystring, "SELECT a.name, b.name FROM machine a, containers b, mcntmap c ");
1991 strcat(querystring, "WHERE a.mach_id = c.mach_id AND b.cnt_id = c.cnt_id ");
1994 sprintf(tmp, "AND b.cnt_id = %d ", cnt_id);
1996 sprintf(tmp, "AND (b.cnt_id = %d OR b.name LIKE '%s/%%') ", cnt_id, containername);
1998 strcat(querystring, tmp);
1999 strcat(querystring, "ORDER BY b.name, a.name");
2003 EXEC SQL PREPARE stmt FROM :qs;
2006 EXEC SQL DECLARE curs_gmnm CURSOR FOR stmt;
2007 EXEC SQL OPEN curs_gmnm;
2011 EXEC SQL FETCH curs_gmnm INTO :machinename, :containername;
2014 (*action)(2, rargv, actarg);
2018 EXEC SQL CLOSE curs_gmnm;
2024 int get_subcontainers_of_container(struct query *q, char *argv[], client *cl,
2025 int (*action)(int, char *[], void *), void *actarg)
2027 EXEC SQL BEGIN DECLARE SECTION;
2028 int cnt_id, isrecursive;
2029 char containername[CONTAINERS_NAME_SIZE], subcontainername[CONTAINERS_NAME_SIZE];
2031 EXEC SQL END DECLARE SECTION;
2033 char querystring[512], tmp [256];
2037 rargv[0] = subcontainername;
2039 cnt_id = *(int *)argv[0];
2040 isrecursive = atoi(argv[1]);
2042 /* get the container name */
2044 EXEC SQL SELECT name INTO :containername
2046 WHERE cnt_id = :cnt_id;
2048 /* trim off the trailing spaces */
2049 strcpy(containername, strtrim(containername));
2051 strcpy(querystring, "SELECT name FROM containers ");
2054 sprintf(tmp, "WHERE name LIKE '%s/%%' and name NOT LIKE '%s/%%/%%' ", containername,
2057 sprintf(tmp, "WHERE name LIKE '%s/%%' ", containername);
2059 strcat(querystring, tmp);
2060 strcat(querystring, "ORDER BY name");
2064 EXEC SQL PREPARE stmt FROM :qs;
2067 EXEC SQL DECLARE curs_gsoc CURSOR FOR stmt;
2068 EXEC SQL OPEN curs_gsoc;
2072 EXEC SQL FETCH curs_gsoc INTO :subcontainername;
2075 (*action)(1, rargv, actarg);
2079 EXEC SQL CLOSE curs_gsoc;