6 * Copyright (C) 1987 by the Massachusetts Institute of Technology
9 * Revision 1.3 1987-07-30 00:26:11 wesommer
10 * Changes by mike prior to "appends" fix.
12 Revision 1.2 87/07/29 16:00:39 wesommer
15 Revision 1.1 87/07/29 15:13:57 wesommer
21 static char *rcsid_qsupport_qc = "$Header$";
25 #include "sms_server.h"
32 /* Specialized Access Routines */
35 ** access_user - verify that client name equals specified login name
37 ** Used by: update_user_shell
38 ** update_finger_by_login
40 ** - since field validation routines are called first, a users_id is
41 ** now in argv[0] instead of the login name. Therefore, we must
42 ** convert the client name to a users_id.
45 access_user(q, argv, cl)
50 register struct krbname *krb;
55 client_name = cl->kname.name;
56 ## repeat retrieve (client_id = users.users_id)
57 ## where users.login = @client_name
58 ## inquire_equel (rowcount = "rowcount")
59 if (rowcount != 1) return(SMS_PERM);
60 if (client_id != *(int *)argv[0]) return(SMS_PERM);
66 ** access_pop - same as access_user plus verifies that a user has only one
67 ** mailbox of type "POP"
76 ** - if q->name = "add_pobox" and type = "POP",
77 ** verify that no POP box already exists for user
82 access_pop(q, argv, cl)
92 if (!bcmp(q->name, "add_pobox", 10) && !bcmp(argv[1], "POP", 4)) {
93 users_id = *(int *)argv[0];
94 mach_id = *(int *)argv[2];
96 ## range of p is pobox
97 ## repeat retrieve (exists = any(p.#box where p.#users_id = @users_id
99 ## and p.#mach_id = @mach_id
100 ## and p.#box = @box))
101 if (exists) return(SMS_EXISTS);
104 return(access_user(q, argv, cl));
108 ** access_list - check access for adding or deleting list members
110 ** Inputs: argv[0] - list_id
111 ** cl->krb.name - client name
113 ** - check that client is a member of the access control list
114 ** - OR, if q->shortname == {amtl | dfml} and
115 ** if list.flags & LF_PUBLIC, allow access if client = member
119 access_list(q, argv, cl)
133 list_id = *(int *)argv[0];
134 ## repeat retrieve (acl_id = list.#acl_id, flags = list.#flags)
135 ## where list.#list_id = @list_id
137 /* parse client structure */
138 status = get_client(cl, &client_type, &client_id);
139 if (status != SMS_SUCCESS) return(status);
141 /* if amtl or dmfl and list is public allow client to add or delete self */
142 if (!bcmp("amtl", q->shortname, 4) || !bcmp("dmfl", q->shortname, 4)) {
143 if ((flags & LF_PUBLIC) && !bcmp("USER", argv[1], 4)) {
144 member_id = *(int *)argv[2];
145 if (member_id == client_id) return(SMS_SUCCESS);
149 /* check for client in access control list */
150 exists = find_member(acl_id, client_type, client_id, 0);
151 if (!exists) return(SMS_PERM);
157 ** Setup routine for add_user
159 ** Inputs: argv[0] - login
164 ** - if argv[1] == "#" then set argv[1] = next(uid)
165 ** - if argv[0] == "#" then set argv[0] = "#<uid>"
169 setup_add_user(q, argv, cl, access_check)
171 register char *argv[];
178 if (access_check) return(SMS_SUCCESS);
180 if (!bcmp(argv[1], "#", 2)) {
181 ## range of u is users
182 ## range of v is values
183 ## repeat retrieve (nuid = v.value) where v.name = "uid"
187 ## repeat retrieve (exists = any(u.#uid where u.#uid = @nuid))
189 ## repeat replace v (value = @nuid) where v.name = "uid"
190 sprintf(argv[1], "%d", nuid);
193 if (!bcmp(argv[0], "#", 2)) {
194 sprintf(argv[0], "#%s", argv[1]);
201 ** Setup routine for add_group
205 ** Description: allocate next gid and store in values table
209 setup_add_group(q, argv, cl, access_check)
218 if (access_check) return(SMS_SUCCESS);
220 ## range of g is groups
221 ## range of v is values
222 ## repeat retrieve (ngid = v.value) where v.name = "gid"
226 ## repeat retrieve (exists = any(g.#gid where g.#gid = @ngid))
229 ## repeat replace v (value = @ngid) where v.name = "gid"
233 /* Followup Routines */
235 set_user_modtime(q, argv)
242 ## repeat replace u (modtime = "now") where u.#login = @login
246 set_user_modtime_by_id(q, argv)
252 users_id = *(int *)argv[0];
253 ## repeat replace users (modtime = "now") where users.#users_id = @users_id
257 set_list_modtime(q, argv)
264 ## repeat replace list (modtime = "now") where list.name = @list_name
268 set_list_modtime_by_id(q, argv)
274 list_id = *(int *)argv[0];
275 ## repeat replace list (modtime = "now") where list.#list_id = @list_id
279 set_finger_modtime(q, argv)
285 users_id = *(int *)argv[0];
286 ## repeat replace f (modtime = "now") where f.#users_id = @users_id
291 ** set_pop_usage - incr/decr usage count for pop server in serverhosts talbe
294 ** q->name - "add_pobox" or "delete_pobox"
298 ** - incr/decr value field in serverhosts table for pop/mach_id
302 set_pop_usage(q, argv)
308 mach_id = *(int *)argv[2];
309 ## range of sh is serverhosts
311 if (!bcmp(q->name, "add_pobox", 10)) {
312 ## repeat replace sh (value1 = sh.value1 + 1)
313 ## where sh.service = "pop" and sh.#mach_id = @mach_id
314 } else if (!bcmp(q->name, "delete_pobox", 13)) {
315 ## repeat replace sh (value1 = sh.value1 - 1)
316 ## where sh.service = "pop" and sh.#mach_id = @mach_id
323 ** delete_list_members - called after the delete_list query to clean up
326 ** Inputs: argv[0] - list_id
329 ** - foreach string member: decr string refc; ifzero, delete string
330 ** - delete all members entries for this list_id
334 delete_list_members(q, argv)
336 register char *argv[];
342 struct save_queue *sq;
343 struct save_queue *sq_create();
345 list_id = *(int *)argv[0];
348 ## range of m is members
349 ## repeat retrieve (string_id = m.member_id)
350 ## where m.#list_id = @list_id and m.member_type = "STRING"
352 sq_save_data(sq, string_id);
355 while (sq_get_data(sq, &string_id)) {
356 ## range of s is strings
357 ## repeat retrieve (refc = s.#refc) where s.#string_id = @string_id
358 ## inquire_equel (rowcount = "rowcount")
359 if (rowcount == 0) continue;
361 ## repeat delete s where s.#string_id = @string_id
363 ## repeat replace s (#refc = @refc) where s.#string_id = @string_id
368 ## repeat delete m where m.#list_id = @list_id
374 ** grvd_support - Support routine for get_rvd_servers query
377 ** q - grvd query structure
378 ** sq - save_queue struture: contains list of {machine, oper_acl_id,
379 ** admin_acl_id, shutdown_acl_id} records.
380 ** v - validate structure (not used)
381 ** action - action routine
382 ** actarg - action routine argument
385 ** - translate acl_ids to list names
389 grvd_support(q, sq, v, action, actarg)
391 struct save_queue *sq;
400 ## char shutdown[33];
407 ## range of l is list
409 while (sq_get_data(sq, &argv)) {
410 sscanf(argv[0], "%d", &list_id);
411 ## repeat retrieve (oper = l.name) where l.#list_id = @list_id
412 sscanf(argv[1], "%d", &list_id);
413 ## repeat retrieve (admin = l.name) where l.#list_id = @list_id
414 sscanf(argv[2], "%d", &list_id);
415 ## repeat retrieve (shutdown = l.name) where l.#list_id = @list_id
417 (*action)(3, targv, actarg);
428 ** set_next_object_id - set next object id in values table
430 ** Inputs: object - object name in values table
432 ** - called before an APPEND operation to set the next object id to
433 ** be used for the new record
437 set_next_object_id(object)
443 ## range of v is values
444 ## repeat replace v (value = v.value + 1) where v.#name = @name
449 ** get_query_need - check modtime of query's associated table against given
450 ** time and return true if greater (false if not)
453 ** argv[0] - query name
454 ** argv[1] - time to compare against
458 get_query_need(q, argv, action, actarg)
460 register char *argv[];
464 ## char *last_get_time;
468 struct query *get_query_by_name();
470 q1 = get_query_by_name(argv[0]);
472 last_get_time = argv[1];
475 if (q1->type != RETRIEVE || table == (char *)0) return(SMS_NO_MATCH);
477 ## range of tbs is tblstats
478 ## repeat retrieve (need = any(tbs.modtime where tbs.#table = @table and
479 ## tbs.modtime > @last_get_time))
481 result = (need) ? "true" : "false";
482 (*action)(1, &result, actarg);
488 ** add_locker - special query routine for creating a user locker
491 ** argv[0] - users_id
492 ** argv[1] - machine_id
494 ** argv[3] - initial quota
497 ** - get prefix directory (dir) for mount point on specified machine/device
498 ** - create filesys entry (label=<login>, type=NFS, machine=<machine>,
499 ** mount=<dir>/<login>, access=w, acl=dbadmin)
500 ** - increment allocated in nfsphys by quota
501 ** - create nfsquota entry
504 ** - SMS_NFSPHYS - machine/device does not exist in nfsphys
505 ** - SMS_FILESYS_EXISTS - file system already exists
510 register struct query *q;
526 users_id = *(int *)argv[0];
527 mach_id = *(int *)argv[1];
529 sscanf(argv[3], "%d", "a);
531 ## range of u is users
532 ## range of f is filesys
533 ## range of np is nfsphys
534 ## range of tbs is tblstats
537 ## repeat retrieve (login = u.#login) where u.#users_id = @users_id
539 /* get user's acl id */
540 ## repeat retrieve (user_acl = list.list_id) where list.name = @login
542 /* get filesystem directory prefix; give error if machine/device
543 pair not in nfsphys table */
544 ## repeat retrieve (dir = np.#dir, allocated = np.#allocated)
545 ## where np.#mach_id = @mach_id and np.#device = device
546 ## inquire_equel (rowcount = "rowcount")
547 if (rowcount == 0) return(SMS_NFSPHYS);
549 /* make sure a filesys with user's name does not already exist */
550 ## repeat retrieve (rowcount = any(f.label where f.label = @login))
551 if (rowcount != 0) return(SMS_FILESYS_EXISTS);
553 /* create a new filesys */
554 sprintf(locker, "%s/%s", dir, login);
555 sprintf(mount, "/mit/%s", login);
556 ## repeat append filesys
557 ## (#label = @login, type = "NFS", #mach_id = @mach_id,
558 ## name = @locker, access = "w", order = 1, #mount = @mount,
559 ## acl_id = @user_acl)
560 ## repeat replace tbs (appends = tbs.appends + 1, modtime = "now")
561 ## where tbs.table = "filesys"
563 /* increment usage count in nfsphys table */
565 ## replace np (#allocated = allocated)
566 ## where np.#mach_id = mach_id and np.#device = device
567 ## repeat replace tbs (updates = tbs.updates + 1, modtime = "now")
568 ## where tbs.table = "nfsphys"
570 /* create nfsquota entry */
571 ## append nfsquota (#users_id = users_id, #mach_id = mach_id,
572 ## #device = device, #quota = quota)
573 ## repeat replace tbs (appends = appends + 1, modtime = "now")
574 ## where tbs.table = "nfsquota"
580 ** delete_locker - special query routine for deleting a user locker
583 ** argv[0] - users_id
584 ** argv[1] - machine_id
589 ** - delete filesys entry (label=<login>)
590 ** - decrement allocated in nfsphys by quota
591 ** - delete nfsquota entry
594 ** - SMS_FILESYS - no filesys exists for user
598 delete_locker(q, argv)
599 register struct query *q;
600 register char *argv[];
610 users_id = *(int *)argv[0];
611 mach_id = *(int *)argv[1];
613 sscanf(argv[3], "%d", "a);
615 ## range of u is users
616 ## range of f is filesys
617 ## range of np is nfsphys
618 ## range of nq is nfsquota
619 ## range of tbs is tblstats
622 ## repeat retrieve (login = u.#login) where u.#users_id = @users_id
624 /* delete the filesys entry */
625 ## repeat delete f where f.label = @login
626 ## inquire_equel (rowcount = "rowcount")
627 if (rowcount == 0) return(SMS_FILESYS);
628 ## repeat replace tbs (deletes = tbs.deletes + 1, modtime = "now")
629 ## where tbs.table = "filesys"
631 /* decrement usage count in nfsphys table */
632 ## replace np (#allocated = np.#allocated - quota)
633 ## where np.#mach_id = mach_id and np.#device = device
634 ## repeat replace tbs (updates = tbs.updates + 1, modtime = "now")
635 ## where tbs.table = "nfsphys"
637 /* delete nfsquota entry */
638 ## delete nq where nq.#users_id = users_id and nq.#mach_id = mach_id and
639 ## nq.#device = device
640 ## repeat replace tbs (deletes = tbs.deletes + 1, modtime = "now")
641 ## where tbs.table = "nfsquota"
646 /* Validation Routines */
648 validate_row(q, argv, v)
649 register struct query *q;
651 register struct validate *v;
659 /* build where clause */
660 build_qual(v->qual, v->argc, argv, qual);
662 /* setup ingres variables */
667 /* tell the logfile what we're doing */
668 com_err(whoami, 0, "validating row");
669 com_err(whoami, 0, qual);
671 /* look for the record */
672 ## range of rvar is table
673 ## retrieve (rowcount = count(rvar.name where qual))
674 com_err(whoami, 0, "row validated");
675 if (rowcount == 0) return(SMS_NO_MATCH);
676 if (rowcount > 1) return(SMS_NOT_UNIQUE);
680 validate_fields(q, argv, vo, n)
682 register char *argv[];
683 register struct valobj *vo;
692 sprintf(buf, "validating %s in %s: %s",
693 vo->namefield, vo->table, argv[vo->index]);
694 com_err(whoami, 0, buf);
695 status = validate_name(argv, vo);
699 sprintf(buf, "validating %s in %s: %s",
700 vo->idfield, vo->table, argv[vo->index]);
701 com_err(whoami, 0, buf);
702 status = validate_id(argv, vo);
706 sprintf(buf, "validating %s type: %s",
707 vo->table, argv[vo->index]);
708 com_err(whoami, 0, buf);
709 status = validate_type(argv, vo);
713 sprintf(buf, "validating type-specific data: %s",
715 com_err(whoami, 0, buf);
716 status = validate_typedata(q, argv, vo);
725 if (status != SMS_EXISTS) return(status);
732 validate_id(argv, vo)
734 register struct valobj *vo;
743 name = argv[vo->index];
745 namefield = vo->namefield;
746 idfield = vo->idfield;
747 ## retrieve (id = table.idfield) where table.namefield = name
748 ## inquire_equel (rowcount = "rowcount")
749 if (rowcount != 1) return(vo->error);
750 *(int *)argv[vo->index] = id;
754 validate_name(argv, vo)
756 register struct valobj *vo;
763 name = argv[vo->index];
765 namefield = vo->namefield;
766 ## retrieve (rowcount = countu(table.namefield
767 ## where table.namefield = name))
768 return ((rowcount == 1) ? SMS_EXISTS : vo->error);
771 validate_type(argv, vo)
773 register struct valobj *vo;
780 typename = vo->table;
781 value = argv[vo->index];
783 /* uppercase type fields */
784 for (c = value; *c; c++) if (islower(*c)) *c = toupper(*c);
786 ## range of a is alias
787 ## repeat retrieve (rowcount = count(a.trans where a.name = @typename and
788 ## a.type = "TYPE" and
789 ## a.trans = @value))
790 return ((rowcount == 1) ? SMS_EXISTS : vo->error);
793 /* validate member or type-specific data field */
795 validate_typedata(q, argv, vo)
796 register struct query *q;
797 register char *argv[];
798 register struct valobj *vo;
802 ## char data_type[17];
807 /* get named object */
808 name = argv[vo->index];
810 /* get field type string (known to be at index-1) */
811 field_type = argv[vo->index-1];
813 /* get corresponding data type associated with field type name */
814 ## repeat retrieve (data_type = alias.trans)
815 ## where alias.#name = @field_type and alias.type = "TYPEDATA"
816 ## inquire_equel (rowcount = "rowcount")
817 if (rowcount != 1) return(SMS_TYPE);
819 /* now retrieve the record id corresponding to the named object */
821 if (!strcmp(data_type, "user")) {
823 ## repeat retrieve (id = users.users_id) where users.login = @name
824 ## inquire_equel (rowcount = "rowcount")
825 if (rowcount != 1) return(SMS_USER);
827 } else if (!strcmp(data_type, "list")) {
829 ## repeat retrieve (id = list.list_id) where list.#name = @name
830 ## inquire_equel (rowcount = "rowcount")
831 if (rowcount != 1) return(SMS_LIST);
833 } else if (!strcmp(data_type, "machine")) {
835 ## repeat retrieve (id = machine.mach_id) where machine.#name = @name
836 ## inquire_equel (rowcount = "rowcount")
837 if (rowcount != 1) return(SMS_MACHINE);
839 } else if (!strcmp(data_type, "string")) {
841 ## range of s is strings
842 ## repeat retrieve (id = s.string_id, refc = s.#refc)
843 ## where s.string = @name
844 ## inquire_equel (rowcount = "rowcount")
846 if (q->type != APPEND) return(SMS_STRING);
847 ## range of v is values
848 ## retrieve (id = v.value) where v.#name = "strings_id"
850 ## replace v (value = id) where v.#name = "strings_id"
851 ## append to strings (string_id = id, string = name, #refc = 1)
852 } else if (rowcount == 1) {
853 if (q->type == APPEND || q->type == DELETE) {
854 refc += (q->type == APPEND) ? 1 : -1;
856 ## replace s (#refc = refc) where s.string_id = id
858 ## delete s where s.string_id = id
866 /* now set value in argv */
867 *(int *)argv[vo->index] = id;
873 translate_ids(q, sq, v, action, actarg)
874 register struct query *q;
875 register struct save_queue *sq;
876 register struct validate *v;
877 register int (*action)();
882 ## char data_type[17];
889 for (i = 0; i < v->objcnt; i++) {
891 if (vo->type == V_FOLLOWUP) break;
895 while (sq_get_data(sq, &argv)) {
899 sscanf(argv[i], "%d", &id);
901 name = (char *)malloc(129);
904 /* get field type string (known to be at index-1) */
905 field_type = argv[vo->index-1];
907 /* get corresponding data type associated with field type name */
908 ## repeat retrieve (data_type = alias.trans)
909 ## where alias.#name = @field_type and alias.type = "TYPEDATA"
910 ## inquire_equel (rowcount = "rowcount")
912 sprintf(name, "%d", id);
913 (*action)(q->vcnt, argv, actarg);
917 /* retrieve object name */
919 if (!strcmp(data_type, "user")) {
921 ## repeat retrieve (name = users.login) where users.users_id = @id
922 ## inquire_equel (rowcount = "rowcount")
924 } else if (!strcmp(data_type, "list")) {
926 ## repeat retrieve (name = list.#name) where list.list_id = @id
927 ## inquire_equel (rowcount = "rowcount")
929 } else if (!strcmp(data_type, "machine")) {
931 ## repeat retrieve (name = machine.#name) where machine.mach_id = @id
932 ## inquire_equel (rowcount = "rowcount")
934 } else if (!strcmp(data_type, "string")) {
936 ## repeat retrieve (name = strings.string)
937 ## where strings.string_id = @id
938 ## inquire_equel (rowcount = "rowcount")
944 /* if there wasn't a corresponding object name, then use the id */
945 if (rowcount != 1) sprintf(name, "%d", id);
948 (*action)(q->vcnt, argv, actarg);
950 /* free saved data */
951 for (i = 0; i < q->vcnt; i++)
957 return (SMS_SUCCESS);
964 * c-continued-statement-offset: 4
966 * c-argdecl-indent: 4