6 * Copyright (C) 1987 by the Massachusetts Institute of Technology
9 * Revision 1.8 1987-08-18 15:05:20 wesommer
10 * Fixed definition of add_locker.
12 Revision 1.7 87/08/04 01:49:41 wesommer
15 Revision 1.6 87/08/04 01:10:02 wesommer
16 Changes by mike; checked in prior to my hacking.
18 Revision 1.5 87/07/30 14:54:13 wesommer
19 Added debugging code in an attempt to catch a flakey problem.
21 Revision 1.4 87/07/30 00:30:21 wesommer
22 replaced appends = appends+1 with appends = tbs.appends+1
24 Revision 1.3 87/07/30 00:26:11 wesommer
25 Changes by mike prior to "appends" fix.
27 Revision 1.2 87/07/29 16:00:39 wesommer
30 Revision 1.1 87/07/29 15:13:57 wesommer
36 static char *rcsid_qsupport_qc = "$Header$";
40 #include "sms_server.h"
47 /* Specialized Access Routines */
50 ** access_user - verify that client name equals specified login name
52 ** Used by: update_user_shell
53 ** update_finger_by_login
55 ** - since field validation routines are called first, a users_id is
56 ** now in argv[0] instead of the login name. Therefore, we must
57 ** convert the client name to a users_id.
60 access_user(q, argv, cl)
65 register struct krbname *krb;
70 client_name = cl->kname.name;
71 ## repeat retrieve (client_id = users.users_id)
72 ## where users.login = @client_name
73 ## inquire_equel (rowcount = "rowcount")
74 if (rowcount != 1) return(SMS_PERM);
75 if (client_id != *(int *)argv[0]) return(SMS_PERM);
81 ** access_pop - same as access_user plus verifies that a user has only one
82 ** mailbox of type "POP"
91 ** - if q->name = "add_pobox" and type = "POP",
92 ** verify that no POP box already exists for user
97 access_pop(q, argv, cl)
107 if (!bcmp(q->name, "add_pobox", 10) && !bcmp(argv[1], "POP", 4)) {
108 users_id = *(int *)argv[0];
109 mach_id = *(int *)argv[2];
111 ## range of p is pobox
112 ## repeat retrieve (exists = any(p.#box where p.#users_id = @users_id
113 ## and p.type = "POP"
114 ## and p.#mach_id = @mach_id
115 ## and p.#box = @box))
116 if (exists) return(SMS_EXISTS);
119 return(access_user(q, argv, cl));
123 ** access_list - check access for adding or deleting list members
125 ** Inputs: argv[0] - list_id
126 ** cl->krb.name - client name
128 ** - check that client is a member of the access control list
129 ** - OR, if q->shortname == {amtl | dfml} and
130 ** if list.flags & LF_PUBLIC, allow access if client = member
134 access_list(q, argv, cl)
148 list_id = *(int *)argv[0];
149 ## repeat retrieve (acl_id = list.#acl_id, flags = list.#flags)
150 ## where list.#list_id = @list_id
152 /* parse client structure */
153 status = get_client(cl, &client_type, &client_id);
154 if (status != SMS_SUCCESS) return(status);
156 /* if amtl or dmfl and list is public allow client to add or delete self */
157 if (!bcmp("amtl", q->shortname, 4) || !bcmp("dmfl", q->shortname, 4)) {
158 if ((flags & LF_PUBLIC) && !bcmp("USER", argv[1], 4)) {
159 member_id = *(int *)argv[2];
160 if (member_id == client_id) return(SMS_SUCCESS);
164 /* check for client in access control list */
165 exists = find_member(acl_id, client_type, client_id, 0);
166 if (!exists) return(SMS_PERM);
172 ** Setup routine for add_group
176 ** Description: allocate next gid and store in values table
180 setup_add_group(q, argv, cl, access_check)
190 status = access_list(q, argv, cl);
192 if (status != SMS_SUCCESS || access_check) return(status);
194 ## range of g is groups
195 ## range of v is values
196 ## repeat retrieve (ngid = v.value) where v.name = "gid"
200 ## repeat retrieve (exists = any(g.#gid where g.#gid = @ngid))
203 ## repeat replace v (value = @ngid) where v.name = "gid"
208 ** Setup routine for add_user
210 ** Inputs: argv[0] - login
215 ** - if argv[1] == "#" then set argv[1] = next(uid)
216 ** - if argv[0] == "#" then set argv[0] = "#<uid>"
220 setup_add_user(q, argv, cl, access_check)
222 register char *argv[];
229 if (access_check) return(SMS_SUCCESS);
231 if (!bcmp(argv[1], "#", 2)) {
232 ## range of u is users
233 ## range of v is values
234 ## repeat retrieve (nuid = v.value) where v.name = "uid"
238 ## repeat retrieve (exists = any(u.#uid where u.#uid = @nuid))
240 ## repeat replace v (value = @nuid) where v.name = "uid"
241 sprintf(argv[1], "%d", nuid);
244 if (!bcmp(argv[0], "#", 2)) {
245 sprintf(argv[0], "#%s", argv[1]);
252 ** followup_add_user - add finger entry, set_user_modtime
253 ** followup_delete_user - delete finger entry
256 ** argv[0] - login (add_user)
257 ** argv[0] - users_id (delete_user)
261 followup_add_user(q, argv)
270 ## char fullname[128];
276 /* get user information */
277 ## range of u is users
278 ## repeat retrieve (users_id = u.#users_id, last = u.#last,
279 ## first = u.#first, middle = u.#middle)
280 ## where u.#login = @login
285 while (*cp1) *cp2++ = *cp1++;
288 if (*cp1 == 0) cp2--;
289 while (*cp1) *cp2++ = *cp1++;
292 while (*cp2++ = *cp1++) ;
294 /* create a finger entry */
295 ## repeat append finger (#users_id = @users_id, #fullname = @fullname)
297 /* set modtime (creation time) on user */
298 ## repeat replace u (modtime = "now") where u.#users_id = @users_id
303 followup_delete_user(q, argv)
309 users_id = *(int *)argv[0];
310 ## repeat delete finger where finger.#users_id = @users_id
315 ** setup_add_filesys - verify existance of referenced file systems
316 ** setup_update_filesys - same, except argv[1..5] --> argv[2..6]
318 ** Inputs: Add Update
319 ** argv[0] - label label
320 ** argv[1] - type new label
321 ** argv[2] - mach_id type
322 ** argv[3] - name mach_id
323 ** argv[4] - mount name
324 ** argv[5] - access mount
329 ** * verify mach_id/name in rvdvirt
330 ** * verify access in {r, x, R, X}
332 ** * extract directory prefix from name
333 ** * verify mach_id/dir in nfsphys
334 ** * verify access in {r, w, R, W}
337 ** SMS_RVD - no such rvd
338 ** SMS_NFS - specified directory not exported
339 ** SMS_FILESYS_ACCESS - invalid filesys access
343 setup_add_filesys(q, argv)
353 mach_id = *(int *)argv[2];
357 if (!bcmp(type, "RVD", 3))
358 return (check_rvd(mach_id, name, access));
359 else if (!bcmp(type, "NFS", 3))
360 return (check_nfs(mach_id, name, access));
365 setup_update_filesys(q, argv)
375 mach_id = *(int *)argv[3];
379 if (!bcmp(type, "RVD", 3))
380 return (check_rvd(mach_id, name, access));
381 else if (!bcmp(type, "NFS", 3))
382 return (check_nfs(mach_id, name, access));
387 ##check_rvd(mach_id, name, access)
395 ## range of rv is rvdvirt
396 ## retrieve (rowcount = any(rv.#name where rv.#mach_id = mach_id and
398 if (rowcount == 0) return(SMS_RVD);
400 caccess = (isupper(*access)) ? tolower(*access) : *access;
401 if (caccess != 'r' && caccess != 'x') return(SMS_FILESYS_ACCESS);
406 ##check_nfs(mach_id, name, access)
417 caccess = (isupper(*access)) ? tolower(*access) : *access;
418 if (caccess != 'r' && caccess != 'w') return(SMS_FILESYS_ACCESS);
420 ## range of np is nfsphys
421 ## retrieve (dir = np.#dir) where np.#mach_id = mach_id
426 if (*cp1++ != *cp2) break;
429 if (*cp2 == 0) return(SMS_SUCCESS);
435 /* Followup Routines */
437 set_user_modtime(q, argv)
444 ## repeat replace u (modtime = "now") where u.#login = @login
448 set_user_modtime_by_id(q, argv)
454 users_id = *(int *)argv[0];
455 ## repeat replace users (modtime = "now") where users.#users_id = @users_id
459 set_list_modtime(q, argv)
466 ## repeat replace list (modtime = "now") where list.name = @list_name
470 set_list_modtime_by_id(q, argv)
476 list_id = *(int *)argv[0];
477 ## repeat replace list (modtime = "now") where list.#list_id = @list_id
481 set_finger_modtime(q, argv)
487 users_id = *(int *)argv[0];
488 ## repeat replace f (modtime = "now") where f.#users_id = @users_id
493 ** set_pop_usage - incr/decr usage count for pop server in serverhosts talbe
496 ** q->name - "add_pobox" or "delete_pobox"
500 ** - incr/decr value field in serverhosts table for pop/mach_id
504 set_pop_usage(q, argv)
510 mach_id = *(int *)argv[2];
511 ## range of sh is serverhosts
513 if (!bcmp(q->name, "add_pobox", 10)) {
514 ## repeat replace sh (value1 = sh.value1 + 1)
515 ## where sh.service = "pop" and sh.#mach_id = @mach_id
516 } else if (!bcmp(q->name, "delete_pobox", 13)) {
517 ## repeat replace sh (value1 = sh.value1 - 1)
518 ## where sh.service = "pop" and sh.#mach_id = @mach_id
526 ** delete_current_quota - adjust nfsphys values on xxx_quota queries.
531 ** argv[2] - users_id
532 ** argv[3] - quota (add_new_quota only)
535 ** delete_current_quota:
536 ** - find nfsquota entry
537 ** - decrement nfsphys.allocated by nfsquota.quota
539 ** - increment nfsphys.allocated by quota
543 add_new_quota(q, argv)
545 register char *argv[];
551 mach_id = *(int*)argv[0];
553 quota = *(int *)argv[3];
555 ## range of np is nfsphys
556 ## repeat replace np (allocated = np.allocated + @quota)
557 ## where np.#mach_id = @mach_id and np.#device = @device
562 delete_current_quota(q, argv, cl, access_check)
564 register char *argv[];
573 if (access_check) return(SMS_SUCCESS);
575 mach_id = *(int *)argv[0];
577 users_id = *(int *)argv[2];
579 ## range of np is nfsphys
580 ## range of nq is nfsquota
581 ## repeat retrieve (quota = nq.#quota)
582 ## where nq.#mach_id = @mach_id and nq.#device = @device and
583 ## nq.#users_id = @users_id
584 ## repeat replace np (allocated = np.allocated - @quota)
585 ## where np.#mach_id = @mach_id and np.#device = @device
591 ** delete_list_members - called after the delete_list query to clean up
594 ** Inputs: argv[0] - list_id
597 ** - foreach string member: decr string refc; ifzero, delete string
598 ** - delete all members entries for this list_id
602 delete_list_members(q, argv)
604 register char *argv[];
610 struct save_queue *sq;
611 struct save_queue *sq_create();
613 list_id = *(int *)argv[0];
616 ## range of m is members
617 ## repeat retrieve (string_id = m.member_id)
618 ## where m.#list_id = @list_id and m.member_type = "STRING"
620 sq_save_data(sq, string_id);
623 while (sq_get_data(sq, &string_id)) {
624 ## range of s is strings
625 ## repeat retrieve (refc = s.#refc) where s.#string_id = @string_id
626 ## inquire_equel (rowcount = "rowcount")
627 if (rowcount == 0) continue;
629 ## repeat delete s where s.#string_id = @string_id
631 ## repeat replace s (#refc = @refc) where s.#string_id = @string_id
636 ## repeat delete m where m.#list_id = @list_id
642 ** grvd_support - Support routine for get_rvd_servers query
645 ** q - grvd query structure
646 ** sq - save_queue struture: contains list of {machine, oper_acl_id,
647 ** admin_acl_id, shutdown_acl_id} records.
648 ** v - validate structure (not used)
649 ** action - action routine
650 ** actarg - action routine argument
653 ** - translate acl_ids to list names
657 grvd_support(q, sq, v, action, actarg)
659 struct save_queue *sq;
668 ## char shutdown[33];
675 ## range of l is list
677 while (sq_get_data(sq, &argv)) {
678 sscanf(argv[0], "%d", &list_id);
679 ## repeat retrieve (oper = l.name) where l.#list_id = @list_id
680 sscanf(argv[1], "%d", &list_id);
681 ## repeat retrieve (admin = l.name) where l.#list_id = @list_id
682 sscanf(argv[2], "%d", &list_id);
683 ## repeat retrieve (shutdown = l.name) where l.#list_id = @list_id
685 (*action)(3, targv, actarg);
695 gars_support(q, sq, v, action, actarg)
697 struct save_queue *sq;
706 ## char shutdown[33];
713 ## range of l is list
715 while (sq_get_data(sq, &argv)) {
716 sscanf(argv[1], "%d", &list_id);
717 ## repeat retrieve (oper = l.name) where l.#list_id = @list_id
718 sscanf(argv[2], "%d", &list_id);
719 ## repeat retrieve (admin = l.name) where l.#list_id = @list_id
720 sscanf(argv[3], "%d", &list_id);
721 ## repeat retrieve (shutdown = l.name) where l.#list_id = @list_id
724 (*action)(4, targv, actarg);
736 ** set_next_object_id - set next object id in values table
738 ** Inputs: object - object name in values table
740 ** - called before an APPEND operation to set the next object id to
741 ** be used for the new record
745 set_next_object_id(object)
751 ## range of v is values
752 ## repeat replace v (value = v.value + 1) where v.#name = @name
757 ** get_query_need - check modtime of query's associated table against given
758 ** time and return true if greater (false if not)
761 ** argv[0] - query name
762 ** argv[1] - time to compare against
766 get_query_need(q, argv, action, actarg)
768 register char *argv[];
772 ## char *last_get_time;
776 struct query *get_query_by_name();
778 q1 = get_query_by_name(argv[0]);
780 last_get_time = argv[1];
783 if (q1->type != RETRIEVE) return(SMS_NO_MATCH);
785 ## range of tbs is tblstats
786 ## repeat retrieve (need = any(tbs.modtime where tbs.#table = @table and
787 ## tbs.modtime > @last_get_time))
789 result = (need) ? "true" : "false";
790 (*action)(1, &result, actarg);
796 ** get_list_is_maillist
806 get_list_is_group(q, argv, action, actarg)
816 list_id = *(int *)argv[0];
818 ## range of g is groups
819 ## repeat retrieve (exists = any(g.#list_id where g.#list_id = @list_id))
821 result = (exists) ? "true" : "false";
822 (*action)(1, &result, actarg);
826 get_list_is_maillist(q, argv, action, actarg)
836 list_id = *(int *)argv[0];
838 ## range of ml is maillists
839 ## repeat retrieve (exists = any(ml.#list_id where ml.#list_id = @list_id))
841 result = (exists) ? "true" : "false";
842 (*action)(1, &result, actarg);
848 ** add_locker - special query routine for creating a user locker
851 ** argv[0] - users_id
852 ** argv[1] - machine_id
854 ** argv[3] - initial quota
857 ** - get prefix directory (dir) for mount point on specified machine/device
858 ** - create filesys entry (label=<login>, type=NFS, machine=<machine>,
859 ** mount=<dir>/<login>, access=w, acl=dbadmin)
860 ** - increment allocated in nfsphys by quota
861 ** - create nfsquota entry
864 ** - SMS_NFSPHYS - machine/device does not exist in nfsphys
865 ** - SMS_FILESYS_EXISTS - file system already exists
870 register struct query *q;
886 users_id = *(int *)argv[0];
887 mach_id = *(int *)argv[1];
889 sscanf(argv[3], "%d", "a);
891 ## range of u is users
892 ## range of f is filesys
893 ## range of np is nfsphys
894 ## range of tbs is tblstats
897 ## repeat retrieve (login = u.#login) where u.#users_id = @users_id
899 /* get user's acl id */
900 ## repeat retrieve (user_acl = list.list_id) where list.name = @login
902 /* get filesystem directory prefix; give error if machine/device
903 pair not in nfsphys table */
904 printf("np.mach_id = %d and np.device = %s\n", mach_id, device);
906 ## repeat retrieve (dir = np.#dir, allocated = np.#allocated)
907 ## where np.#mach_id = @mach_id and np.#device = @device
908 ## inquire_equel (rowcount = "rowcount")
909 if (rowcount == 0) return(SMS_NFSPHYS);
911 /* make sure a filesys with user's name does not already exist */
912 ## repeat retrieve (rowcount = any(f.label where f.label = @login))
913 if (rowcount != 0) return(SMS_FILESYS_EXISTS);
915 /* create a new filesys */
916 sprintf(locker, "%s/%s", dir, login);
917 sprintf(mount, "/mit/%s", login);
918 ## repeat append filesys
919 ## (#label = @login, type = "NFS", #mach_id = @mach_id,
920 ## name = @locker, access = "w", order = 1, #mount = @mount,
921 ## acl_id = @user_acl)
922 ## repeat replace tbs (appends = tbs.appends + 1, modtime = "now")
923 ## where tbs.table = "filesys"
925 /* increment usage count in nfsphys table */
927 ## replace np (#allocated = allocated)
928 ## where np.#mach_id = mach_id and np.#device = device
929 ## repeat replace tbs (updates = tbs.updates + 1, modtime = "now")
930 ## where tbs.table = "nfsphys"
932 /* create nfsquota entry */
933 ## append nfsquota (#users_id = users_id, #mach_id = mach_id,
934 ## #device = device, #quota = quota)
935 ## repeat replace tbs (appends = tbs.appends + 1, modtime = "now")
936 ## where tbs.table = "nfsquota"
942 ** delete_locker - special query routine for deleting a user locker
945 ** argv[0] - users_id
946 ** argv[1] - machine_id
951 ** - delete filesys entry (label=<login>)
952 ** - decrement allocated in nfsphys by quota
953 ** - delete nfsquota entry
956 ** - SMS_FILESYS - no filesys exists for user
960 delete_locker(q, argv)
961 register struct query *q;
962 register char *argv[];
972 users_id = *(int *)argv[0];
973 mach_id = *(int *)argv[1];
975 sscanf(argv[3], "%d", "a);
977 ## range of u is users
978 ## range of f is filesys
979 ## range of np is nfsphys
980 ## range of nq is nfsquota
981 ## range of tbs is tblstats
984 ## repeat retrieve (login = u.#login) where u.#users_id = @users_id
986 /* delete the filesys entry */
987 ## repeat delete f where f.label = @login
988 ## inquire_equel (rowcount = "rowcount")
989 if (rowcount == 0) return(SMS_FILESYS);
990 ## repeat replace tbs (deletes = tbs.deletes + 1, modtime = "now")
991 ## where tbs.table = "filesys"
993 /* decrement usage count in nfsphys table */
994 ## replace np (#allocated = np.#allocated - quota)
995 ## where np.#mach_id = mach_id and np.#device = device
996 ## repeat replace tbs (updates = tbs.updates + 1, modtime = "now")
997 ## where tbs.table = "nfsphys"
999 /* delete nfsquota entry */
1000 ## delete nq where nq.#users_id = users_id and nq.#mach_id = mach_id and
1001 ## nq.#device = device
1002 ## repeat replace tbs (deletes = tbs.deletes + 1, modtime = "now")
1003 ## where tbs.table = "nfsquota"
1005 return(SMS_SUCCESS);
1009 ** add_user_group - create a group for a user and add user to group
1015 ** - verify specified user exists
1016 ** - create a list of same name as user
1017 ** - add user as a member of the list
1021 add_user_group(q, argv)
1033 /* verify user exists */
1034 ## repeat retrieve (users_id = users.#users_id) where users.#login = @login
1035 ## inquire_equel (exists = "rowcount")
1036 if (exists != 1) return(SMS_USER);
1038 /* verify list does not exist */
1039 ## repeat retrieve (exists = any(list.name where list.name = @login))
1040 if (exists) return(SMS_LIST);
1042 /* get new list_id */
1043 ## repeat retrieve (list_id = values.value) where values.name = "list_id"
1045 ## repeat replace values (value = @list_id) where values.name = "list_id"
1047 /* create the list */
1048 ## repeat append list (name = @login, #list_id = @list_id, flags = 1,
1049 ## desc = "User Group", acl_id = @list_id,
1050 ## expdate = "today" + "5 years", modtime = "now")
1052 /* add user to list */
1053 ## repeat append members (#list_id = @list_id, member_type = "USER",
1054 ## member_id = @users_id)
1057 ## range of g is groups
1058 ## range of v is values
1059 ## repeat retrieve (gid = v.value) where v.name = "gid"
1063 ## repeat retrieve (exists = any(g.#gid where g.#gid = @gid))
1065 ## repeat replace v (value = @gid) where v.name = "gid"
1067 /* add list to group table */
1068 ## repeat append groups (#list_id = @list_id, ltid = list.tid, #gid = @gid)
1069 ## where list.#list_id = @list_id
1071 /* and we're done */
1072 return(SMS_SUCCESS);
1077 ** get_members_of_list - optimized query for retrieval of list members
1080 ** argv[0] - list_id
1083 ** - retrieve USER members, then LIST members, then STRING members
1087 get_members_of_list(q, argv, action, actarg)
1094 ## char member_name[129];
1097 list_id = *(int *)argv[0];
1099 targv[1] = member_name;
1101 ## range of m is members
1102 ## repeat retrieve (member_name = users.login)
1103 ## where m.#list_id = @list_id and m.member_type = "USER"
1104 ## and m.member_id = users.users_id
1105 ## sort by #member_name
1107 (*action)(2, targv, actarg);
1111 ## repeat retrieve (member_name = list.name)
1112 ## where m.#list_id = @list_id and m.member_type = "LIST"
1113 ## and m.member_id = list.#list_id
1114 ## sort by #member_name
1116 (*action)(2, targv, actarg);
1119 targv[0] = "STRING";
1120 ## repeat retrieve (member_name = strings.string)
1121 ## where m.#list_id = @list_id and m.member_type = "STRING"
1122 ## and m.member_id = strings.string_id
1123 ## sort by #member_name
1125 (*action)(2, targv, actarg);
1128 return(SMS_SUCCESS);
1132 ** get_groups_of_user - optimized query for retrieval of all groups to
1133 ** which a user belongs
1137 get_groups_of_user(q, argv, action, actarg)
1144 ## char list_name[33];
1149 users_id = *(int *)argv[0];
1150 targv[0] = list_name;
1153 ## range of m is members
1155 ## repeat retrieve (list_name = list.name, gid = text(groups.#gid))
1156 ## where m.member_id = @users_id and m.member_type = "USER" and
1157 ## m.list_id = groups.list_id and groups.ltid = list.tid
1158 ## sort by #list_name
1160 (*action)(2, targv, actarg);
1162 ## inquire_equel (rowcount = "rowcount")
1164 return ((rowcount = 0) ? SMS_NO_MATCH : SMS_SUCCESS);
1168 ** get_all_poboxes - optimized query for retrieval of all poboxes
1171 ** - retrieve LOCAL boxes, then POP boxes, then FOREIGN boxes
1175 get_all_poboxes(q, argv, action, actarg)
1182 ## char machine[129];
1191 ## range of p is pobox
1192 ## repeat retrieve (login=users.#login, machine = #machine.name, box=p.#box)
1193 ## where p.type = "LOCAL" and p.users_id = users.users_id
1194 ## and p.mach_id = #machine.mach_id
1196 (*action)(4, targv, actarg);
1200 ## repeat retrieve (login=users.#login, machine = #machine.name, box=p.#box)
1201 ## where p.type = "POP" and p.users_id = users.users_id
1202 ## and p.mach_id = #machine.mach_id
1204 (*action)(4, targv, actarg);
1207 targv[1] = "FOREIGN";
1208 ## repeat retrieve (login=users.#login, machine=strings.string, box=p.#box)
1209 ## where p.type = "FOREIGN" and p.users_id = users.users_id
1210 ## and p.mach_id = strings.string_id
1212 (*action)(4, targv, actarg);
1215 return(SMS_SUCCESS);
1218 /* Validation Routines */
1220 validate_row(q, argv, v)
1221 register struct query *q;
1223 register struct validate *v;
1231 /* build where clause */
1232 build_qual(v->qual, v->argc, argv, qual);
1234 /* setup ingres variables */
1239 if (log_flags & LOG_RES)
1240 /* tell the logfile what we're doing */
1241 com_err(whoami, 0, "validating row: %s", qual);
1243 /* look for the record */
1244 ## range of rvar is table
1245 ## retrieve (rowcount = count(rvar.name where qual))
1246 if (rowcount == 0) return(SMS_NO_MATCH);
1247 if (rowcount > 1) return(SMS_NOT_UNIQUE);
1251 validate_fields(q, argv, vo, n)
1253 register char *argv[];
1254 register struct valobj *vo;
1257 register int status;
1262 if (log_flags & LOG_RES)
1263 com_err(whoami, 0, "validating %s in %s: %s",
1264 vo->namefield, vo->table, argv[vo->index]);
1265 status = validate_name(argv, vo);
1269 if (log_flags & LOG_RES)
1270 com_err(whoami, 0, "validating %s in %s: %s",
1271 vo->idfield, vo->table, argv[vo->index]);
1272 status = validate_id(argv, vo);
1276 if (log_flags & LOG_RES)
1277 com_err(whoami, 0, "validating date: %s", argv[vo->index]);
1278 status = validate_date(argv, vo);
1282 if (log_flags & LOG_RES)
1283 com_err(whoami, 0, "validating %s type: %s",
1284 vo->table, argv[vo->index]);
1285 status = validate_type(argv, vo);
1289 if (log_flags & LOG_RES)
1290 com_err(whoami, 0, "validating typed data (%s): %s",
1291 argv[vo->index - 1], argv[vo->index]);
1292 status = validate_typedata(q, argv, vo);
1296 status = SMS_EXISTS;
1300 status = SMS_EXISTS;
1305 if (status != SMS_EXISTS) return(status);
1309 return(SMS_SUCCESS);
1312 validate_id(argv, vo)
1314 register struct valobj *vo;
1323 name = argv[vo->index];
1325 namefield = vo->namefield;
1326 idfield = vo->idfield;
1327 ## retrieve (id = table.idfield) where table.namefield = name
1328 ## inquire_equel (rowcount = "rowcount")
1329 if (rowcount != 1) return(vo->error);
1330 *(int *)argv[vo->index] = id;
1334 validate_name(argv, vo)
1336 register struct valobj *vo;
1343 name = argv[vo->index];
1345 namefield = vo->namefield;
1346 ## retrieve (rowcount = countu(table.namefield
1347 ## where table.namefield = name))
1348 return ((rowcount == 1) ? SMS_EXISTS : vo->error);
1351 validate_date(argv, vo)
1359 idate = argv[vo->index];
1361 ## retrieve (dd = interval("years", date(idate) - date("today")))
1362 ## inquire_equel (errorno = "errorno")
1363 if (errorno != 0 || dd > 5.0) return(SMS_DATE);
1364 return(SMS_SUCCESS);
1367 validate_type(argv, vo)
1369 register struct valobj *vo;
1376 typename = vo->table;
1377 value = argv[vo->index];
1379 /* uppercase type fields */
1380 for (c = value; *c; c++) if (islower(*c)) *c = toupper(*c);
1382 ## range of a is alias
1383 ## repeat retrieve (rowcount = count(a.trans where a.name = @typename and
1384 ## a.type = "TYPE" and
1385 ## a.trans = @value))
1386 return ((rowcount == 1) ? SMS_EXISTS : vo->error);
1389 /* validate member or type-specific data field */
1391 validate_typedata(q, argv, vo)
1392 register struct query *q;
1393 register char *argv[];
1394 register struct valobj *vo;
1397 ## char *field_type;
1398 ## char data_type[17];
1403 /* get named object */
1404 name = argv[vo->index];
1406 /* get field type string (known to be at index-1) */
1407 field_type = argv[vo->index-1];
1409 /* get corresponding data type associated with field type name */
1410 ## repeat retrieve (data_type = alias.trans)
1411 ## where alias.#name = @field_type and alias.type = "TYPEDATA"
1412 ## inquire_equel (rowcount = "rowcount")
1413 if (rowcount != 1) return(SMS_TYPE);
1415 /* now retrieve the record id corresponding to the named object */
1417 if (!strcmp(data_type, "user")) {
1419 ## repeat retrieve (id = users.users_id) where users.login = @name
1420 ## inquire_equel (rowcount = "rowcount")
1421 if (rowcount != 1) return(SMS_USER);
1423 } else if (!strcmp(data_type, "list")) {
1425 ## repeat retrieve (id = list.list_id) where list.#name = @name
1426 ## inquire_equel (rowcount = "rowcount")
1427 if (rowcount != 1) return(SMS_LIST);
1429 } else if (!strcmp(data_type, "machine")) {
1431 ## repeat retrieve (id = machine.mach_id) where machine.#name = @name
1432 ## inquire_equel (rowcount = "rowcount")
1433 if (rowcount != 1) return(SMS_MACHINE);
1435 } else if (!strcmp(data_type, "string")) {
1437 ## range of s is strings
1438 ## repeat retrieve (id = s.string_id, refc = s.#refc)
1439 ## where s.string = @name
1440 ## inquire_equel (rowcount = "rowcount")
1441 if (rowcount == 0) {
1442 if (q->type != APPEND) return(SMS_STRING);
1443 ## range of v is values
1444 ## retrieve (id = v.value) where v.#name = "strings_id"
1446 ## replace v (value = id) where v.#name = "strings_id"
1447 ## append to strings (string_id = id, string = name, #refc = 1)
1448 } else if (rowcount == 1) {
1449 if (q->type == APPEND || q->type == DELETE) {
1450 refc += (q->type == APPEND) ? 1 : -1;
1452 ## replace s (#refc = refc) where s.string_id = id
1454 ## delete s where s.string_id = id
1462 /* now set value in argv */
1463 *(int *)argv[vo->index] = id;
1465 return (SMS_EXISTS);
1469 translate_ids(q, sq, v, action, actarg)
1470 register struct query *q;
1471 register struct save_queue *sq;
1472 register struct validate *v;
1473 register int (*action)();
1477 ## char *field_type;
1478 ## char data_type[17];
1485 for (i = 0; i < v->objcnt; i++) {
1487 if (vo->type == V_FOLLOWUP) break;
1491 while (sq_get_data(sq, &argv)) {
1495 sscanf(argv[i], "%d", &id);
1497 name = (char *)malloc(129);
1500 /* get field type string (known to be at index-1) */
1501 field_type = argv[vo->index-1];
1503 /* get corresponding data type associated with field type name */
1504 ## repeat retrieve (data_type = alias.trans)
1505 ## where alias.#name = @field_type and alias.type = "TYPEDATA"
1506 ## inquire_equel (rowcount = "rowcount")
1507 if (rowcount != 1) {
1508 sprintf(name, "%d", id);
1509 (*action)(q->vcnt, argv, actarg);
1513 /* retrieve object name */
1515 if (!strcmp(data_type, "user")) {
1517 ## repeat retrieve (name = users.login) where users.users_id = @id
1518 ## inquire_equel (rowcount = "rowcount")
1520 } else if (!strcmp(data_type, "list")) {
1522 ## repeat retrieve (name = list.#name) where list.list_id = @id
1523 ## inquire_equel (rowcount = "rowcount")
1525 } else if (!strcmp(data_type, "machine")) {
1527 ## repeat retrieve (name = machine.#name) where machine.mach_id = @id
1528 ## inquire_equel (rowcount = "rowcount")
1530 } else if (!strcmp(data_type, "string")) {
1532 ## repeat retrieve (name = strings.string)
1533 ## where strings.string_id = @id
1534 ## inquire_equel (rowcount = "rowcount")
1540 /* if there wasn't a corresponding object name, then use the id */
1541 if (rowcount != 1) sprintf(name, "%d", id);
1544 (*action)(q->vcnt, argv, actarg);
1546 /* free saved data */
1547 for (i = 0; i < q->vcnt; i++)
1553 return (SMS_SUCCESS);
1560 * c-continued-statement-offset: 4
1561 * c-brace-offset: -4
1562 * c-argdecl-indent: 4
1563 * c-label-offset: -4