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"
15 #include <arpa/inet.h>
16 #include <netinet/in.h>
22 EXEC SQL INCLUDE sqlca;
27 extern int dbms_errno, mr_errcode;
29 EXEC SQL BEGIN DECLARE SECTION;
30 extern char stmt_buf[];
31 EXEC SQL END DECLARE SECTION;
33 EXEC SQL WHENEVER SQLERROR DO dbmserr();
35 int hostname_check(char *name);
36 int hostinfo_check(char *name, int num);
37 int prefetch_value(struct query *q, char **argv, client *cl);
38 int check_nfs(int mach_idx, char *name, char *access);
39 int check_mailman_listname(char *name, const char *suffix);
43 /* Setup routine for add_user
45 * Inputs: argv[0] - login
50 * - if argv[1] == UNIQUE_UID then set argv[1] = next(uid)
51 * - if argv[0] == UNIQUE_LOGIN then set argv[0] = "#<uid>"
54 int setup_ausr(struct query *q, char *argv[], client *cl)
57 EXEC SQL BEGIN DECLARE SECTION;
59 EXEC SQL END DECLARE SECTION;
61 if (!strcmp(q->shortname, "uusr") || !strcmp(q->shortname, "uuac"))
68 if (strlen(argv[row + 3]) + strlen(argv[row + 4]) +
69 strlen(argv[row + 5]) + 2 > USERS_FULLNAME_SIZE)
70 return MR_ARG_TOO_LONG;
74 if (strlen(argv[row + 2]) + strlen(argv[row + 3]) +
75 strlen(argv[row + 4]) + 2 > USERS_FULLNAME_SIZE)
76 return MR_ARG_TOO_LONG;
79 if (!strcmp(argv[row], UNIQUE_UID) || atoi(argv[row]) == -1)
81 if ((err = set_next_object_id("unix_uid", USERS_TABLE, 1)))
83 EXEC SQL SELECT value INTO :nuid FROM numvalues WHERE name = 'unix_uid';
84 if (sqlca.sqlerrd[2] != 1)
86 sprintf(argv[row], "%d", nuid);
89 if (!strcmp(argv[0], UNIQUE_LOGIN) || atoi(argv[row]) == -1)
90 sprintf(argv[0], "#%s", argv[row]);
92 if ((mr_errcode = prefetch_value(q, argv, cl)) != MR_SUCCESS)
95 /* If this is an UPDATE query, we're done. */
99 /* For an add query, we need to fill in the creator id. */
100 sprintf(argv[q->argc + q->vcnt + 1], "%d", cl->client_id);
105 /* setup_dusr - verify that the user is no longer being referenced
106 * and may safely be deleted.
109 int setup_dusr(struct query *q, char *argv[], client *cl)
111 EXEC SQL BEGIN DECLARE SECTION;
113 char resv[USERS_RESERVATIONS_SIZE];
114 EXEC SQL END DECLARE SECTION;
116 id = *(int *)argv[0];
118 /* For now, only allow users to be deleted if their status is
119 * one of 0, 4, or 8 (the various registerable statuses)
120 * and we have no reservations about deleting them.
122 EXEC SQL SELECT status, reservations INTO :flag, :resv
123 FROM users WHERE users_id = :id;
124 if ((flag != 0 && flag != 4 && flag != 8) || *resv)
127 EXEC SQL SELECT COUNT(member_id) INTO :cnt FROM imembers
128 WHERE member_id = :id AND member_type = 'USER';
131 EXEC SQL SELECT COUNT(label) INTO :cnt FROM filesys
135 EXEC SQL SELECT COUNT(name) INTO :cnt FROM list
136 WHERE acl_id = :id AND acl_type = 'USER';
139 EXEC SQL SELECT COUNT(name) INTO :cnt FROM servers
140 WHERE acl_id = :id AND acl_type = 'USER';
143 EXEC SQL SELECT COUNT(acl_id) INTO :cnt FROM hostaccess
144 WHERE acl_id = :id AND acl_type = 'USER';
150 EXEC SQL DELETE FROM quota WHERE entity_id = :id AND type = 'USER';
151 EXEC SQL DELETE FROM krbmap WHERE users_id = :id;
156 /* setup_dpob: Take care of keeping track of the post office usage.
158 int setup_dpob(struct query *q, char *argv[], client *cl)
160 EXEC SQL BEGIN DECLARE SECTION;
162 char type[USERS_POTYPE_SIZE];
163 EXEC SQL END DECLARE SECTION;
165 user = *(int *)argv[0];
166 EXEC SQL SELECT potype, pop_id INTO :type, :id FROM users
167 WHERE users_id = :user;
171 if (!strcmp(strtrim(type), "POP"))
172 set_pop_usage(id, -1);
177 /* setup_dmac - verify that the machine is no longer being referenced
178 * and may safely be deleted.
181 int setup_dmac(struct query *q, char *argv[], client *cl)
183 EXEC SQL BEGIN DECLARE SECTION;
185 EXEC SQL END DECLARE SECTION;
187 id = *(int *)argv[0];
189 EXEC SQL SELECT status INTO :flag FROM machine
193 EXEC SQL SELECT COUNT(login) INTO :cnt FROM users
194 WHERE potype = 'POP' AND pop_id = :id;
197 EXEC SQL SELECT COUNT(mach_id) INTO :cnt FROM serverhosts
201 EXEC SQL SELECT COUNT(mach_id) INTO :cnt FROM nfsphys
205 EXEC SQL SELECT COUNT(mach_id) INTO :cnt FROM hostaccess
209 EXEC SQL SELECT COUNT(mach_id) INTO :cnt FROM printers
213 EXEC SQL SELECT COUNT(rm) INTO :cnt FROM printers
217 EXEC SQL SELECT COUNT(rq) INTO :cnt FROM printers
221 EXEC SQL SELECT COUNT(mach_id) INTO :cnt FROM printservers
225 EXEC SQL SELECT COUNT(mach_id) INTO :cnt FROM hostalias
229 EXEC SQL SELECT COUNT(member_id) INTO :cnt FROM imembers
230 WHERE member_type = 'MACHINE' and member_id = :id;
234 EXEC SQL DELETE FROM mcmap WHERE mach_id = :id;
238 EXEC SQL DELETE FROM mcntmap WHERE mach_id = :id;
244 /* setup_asnt - verify that the data entered for the subnet is sane.
245 * In particular, make sure that the "low" and "high" addresses are
246 * correctly ordered, i.e., high > low.
249 int setup_asnt(struct query *q, char *argv[], client *cl)
251 int high, low, row, status;
252 char *account_number;
254 /* Check for asnt or usnt. */
255 if (q->type == APPEND)
260 low = atoi(argv[row + 7]);
261 high = atoi(argv[row + 8]);
262 status = atoi(argv[row + 2]);
263 account_number = argv[row + 4];
265 /* Don't allow Private subnets to be created without a valid billing
268 if (status == SNET_STATUS_PRIVATE_10MBPS ||
269 status == SNET_STATUS_PRIVATE_100MBPS ||
270 status == SNET_STATUS_PRIVATE_1000MBPS)
272 EXEC SQL SELECT account_number FROM accountnumbers
273 WHERE account_number = :account_number;
274 if (sqlca.sqlcode == SQL_NO_MATCH)
275 return MR_ACCOUNT_NUMBER;
278 /* Special case 0.0.0.0 and 255.255.255.255 */
279 if (!(low == 0 || low == -1 || high == 0 || high == -1))
283 /* If this is update_subnet, we're done. */
287 /* For an add_subnet query, allocate and fill in a new snet_id */
288 if ((mr_errcode = prefetch_value(q, argv, cl)) != MR_SUCCESS)
294 /* setup_dsnt - verify that the subnet is no longer being referenced
295 * and may safely be deleted.
298 int setup_dsnt(struct query *q, char *argv[], client *cl)
300 EXEC SQL BEGIN DECLARE SECTION;
302 EXEC SQL END DECLARE SECTION;
304 id = *(int *)argv[0];
305 EXEC SQL SELECT COUNT(mach_id) INTO :cnt FROM machine
313 /* setup_dclu - verify that the cluster is no longer being referenced
314 * and may safely be deleted.
317 int setup_dclu(struct query *q, char *argv[], client *cl)
319 EXEC SQL BEGIN DECLARE SECTION;
321 EXEC SQL END DECLARE SECTION;
323 id = *(int *)argv[0];
324 EXEC SQL SELECT COUNT(mach_id) INTO :cnt FROM mcmap
328 EXEC SQL SELECT COUNT(clu_id) INTO :cnt FROM svc
338 /* setup_alis - if argv[5] is non-zero and argv[6] is UNIQUE_ID, then allocate
339 * a new gid and put it in argv[6]. Otherwise if argv[6] is UNIQUE_ID but
340 * argv[5] is not, then remember that UNIQUE_ID is being stored by putting
341 * a -1 there. Remember that this is also used for ulis, with the indexes
342 * at 6 & 7. Also check that the list name does not contain uppercase
343 * characters, control characters, @, or :.
345 * Newlines in list descriptions do bad things to the aliases file
346 * moira generates, so make sure the description doesn't contain any, too.
349 static int badlistchars[] = {
350 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* ^@ - ^O */
351 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* ^P - ^_ */
352 1, 1, 1, 0, 0, 1, 0, 0, 1, 1, 1, 1, 1, 0, 0, 1, /* SPACE - / */
353 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, /* 0 - ? */
354 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* @ - O */
355 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, /* P - _ */
356 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* ` - o */
357 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 1, /* p - ^? */
358 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
359 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
360 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
361 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
362 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
363 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
364 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
365 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
368 int setup_alis(struct query *q, char *argv[], client *cl)
370 EXEC SQL BEGIN DECLARE SECTION;
371 int ngid, cnt, mailman, mailman_id;
373 EXEC SQL END DECLARE SECTION;
375 int idx, err, best = -1, usage;
377 if (!strcmp(q->shortname, "alis"))
379 else if (!strcmp(q->shortname, "ulis"))
384 desc = argv[9 + idx];
385 else if (q->version == 3)
386 desc = argv[10 + idx];
387 else if (q->version == 4)
388 desc = argv[12 + idx];
389 else if (q->version >= 10)
390 desc = argv[14 + idx];
394 EXEC SQL BEGIN DECLARE SECTION;
395 int lid = *(int *)argv[0];
396 EXEC SQL END DECLARE SECTION;
398 if (acl_access_check(lid, cl))
402 for (p = (unsigned char *) name; *p; p++)
404 if (badlistchars[*p])
408 for (p = (unsigned char *) desc; *p; p++)
414 /* Check that it doesn't conflict with a pre-existing weirdly-cased
416 EXEC SQL SELECT COUNT(name) INTO :cnt FROM list
417 WHERE LOWER(name) = :name AND name != :name;
421 if (!strcmp(argv[6 + idx], UNIQUE_GID) || atoi(argv[6 + idx]) == -1)
423 if (atoi(argv[5 + idx]))
425 if ((err = set_next_object_id("gid", LIST_TABLE, 1)))
427 EXEC SQL SELECT value INTO :ngid FROM numvalues
431 sprintf(argv[6 + idx], "%d", ngid);
434 strcpy(argv[6 + idx], "-1");
437 /* Don't let someone rename a list to one of the magic mailman names
438 * (foo-admin, etc) if foo already exists as a mailman list.
440 if ((err = check_mailman_listname(name, "-admin")) != MR_SUCCESS)
442 if ((err = check_mailman_listname(name, "-owner")) != MR_SUCCESS)
444 if ((err = check_mailman_listname(name, "-request")) != MR_SUCCESS)
447 if (q->version >= 10)
449 /* Don't let them take this name for a mailman list if we can't
450 * reserve the -admin, -owner, and -request names.
452 if (atoi(argv[8 + idx]))
454 EXEC SQL SELECT COUNT(name) INTO :cnt FROM list
455 WHERE (name = :name || '-admin' OR name = :name || '-owner' OR
456 name = :name || '-request');
461 /* Handle the [ANY] case for mailman server. */
462 mailman_id = *(int *)argv[9 + idx];
463 if (mailman_id == -1)
465 EXEC SQL DECLARE csr_mailman CURSOR FOR
466 SELECT mach_id FROM serverhosts WHERE service = 'MAILMAN'
470 EXEC SQL OPEN csr_mailman;
476 EXEC SQL FETCH csr_mailman INTO :mailman_id;
480 EXEC SQL SELECT COUNT(name) INTO :usage FROM list
481 WHERE mailman_id = :mailman_id;
483 if (best < 0 || usage < best)
486 *(int *)argv[9 + idx] = mailman_id;
490 EXEC SQL CLOSE csr_mailman;
499 if ((mr_errcode = prefetch_value(q, argv, cl)) != MR_SUCCESS)
506 /* setup_dlis - verify that the list is no longer being referenced
507 * and may safely be deleted.
510 int setup_dlis(struct query *q, char *argv[], client *cl)
513 EXEC SQL BEGIN DECLARE SECTION;
515 EXEC SQL END DECLARE SECTION;
517 id = *(int *)argv[0];
519 EXEC SQL SELECT COUNT(member_id) INTO :cnt FROM imembers
520 WHERE member_id = :id AND member_type = 'LIST';
524 EXEC SQL SELECT COUNT(member_id) INTO :cnt FROM imembers
525 WHERE member_id = :id AND member_type = 'LIST';
529 EXEC SQL SELECT COUNT(member_id) INTO :cnt FROM imembers
534 EXEC SQL SELECT COUNT(label) INTO :cnt FROM filesys WHERE owners = :id;
538 EXEC SQL SELECT COUNT(tag) INTO :cnt FROM capacls WHERE list_id = :id;
542 EXEC SQL SELECT COUNT(name) INTO :cnt FROM list
543 WHERE acl_id = :id AND acl_type = 'LIST' AND list_id != :id;
547 EXEC SQL SELECT COUNT(name) INTO :cnt FROM list
548 WHERE memacl_id = :id AND memacl_type = 'LIST' AND list_id != :id;
552 EXEC SQL SELECT COUNT(name) INTO :cnt FROM servers
553 WHERE acl_id = :id AND acl_type = 'LIST';
557 EXEC SQL SELECT COUNT(entity_id) INTO :cnt FROM quota
558 WHERE entity_id = :id AND type = 'GROUP';
562 EXEC SQL SELECT COUNT(acl_id) INTO :cnt FROM hostaccess
563 WHERE acl_id = :id AND acl_type = 'LIST';
567 EXEC SQL SELECT COUNT(class) INTO :cnt FROM zephyr z
568 WHERE z.xmt_type = 'LIST' AND z.xmt_id = :id
569 OR z.sub_type = 'LIST' AND z.sub_id = :id
570 OR z.iws_type = 'LIST' AND z.iws_id = :id
571 OR z.iui_type = 'LIST' AND z.iui_id = :id
572 OR z.owner_type = 'LIST' and z.owner_id = :id;
576 EXEC SQL SELECT COUNT(name) INTO :cnt FROM printers
577 WHERE lpc_acl = :id OR ac = :id;
581 EXEC SQL SELECT COUNT(mach_id) INTO :cnt FROM printservers
582 WHERE owner_type = 'LIST' AND owner_id = :id
587 EXEC SQL SELECT count(name) INTO :cnt FROM containers
588 WHERE acl_id = :id AND acl_type = 'LIST';
592 EXEC SQL SELECT count(name) INTO :cnt FROM containers
593 WHERE memacl_id = :id AND memacl_type = 'LIST';
601 /* setup_dsin - verify that the service is no longer being referenced
602 * and may safely be deleted.
605 int setup_dsin(struct query *q, char *argv[], client *cl)
607 EXEC SQL BEGIN DECLARE SECTION;
610 EXEC SQL END DECLARE SECTION;
613 EXEC SQL SELECT COUNT(service) INTO :cnt FROM serverhosts
614 WHERE service = UPPER(:svrname);
618 EXEC SQL SELECT inprogress INTO :ec FROM servers
619 WHERE name = UPPER(:svrname);
629 /* setup_dshi - verify that the service-host is no longer being referenced
630 * and may safely be deleted.
633 int setup_dshi(struct query *q, char *argv[], client *cl)
635 EXEC SQL BEGIN DECLARE SECTION;
638 EXEC SQL END DECLARE SECTION;
641 id = *(int *)argv[1];
643 EXEC SQL SELECT inprogress INTO :ec FROM serverhosts
644 WHERE service = UPPER(:svrname) AND mach_id = :id;
655 ** setup_add_filesys - verify existance of referenced file systems
661 ** argv[5] - rwaccess
666 ** - for type = NFS/IMAP:
667 ** * extract directory prefix from name
668 ** * verify mach_id/dir in nfsphys
669 ** * verify rwaccess in {r, w, R, W}
671 ** Side effect: sets variable _var_phys_id to the ID of the physical
672 ** filesystem (nfsphys_id for NFS, 0 for RVD)
675 ** MR_NFS - specified directory not exported
676 ** MR_FILESYS_ACCESS - invalid filesys access
680 EXEC SQL BEGIN DECLARE SECTION;
682 EXEC SQL END DECLARE SECTION;
684 int setup_afil(struct query *q, char *argv[], client *cl)
688 EXEC SQL BEGIN DECLARE SECTION;
690 char ftype[FILESYS_TYPE_SIZE + 10], *rwaccess;
691 EXEC SQL END DECLARE SECTION;
694 mach_id = *(int *)argv[2];
699 sprintf(ftype, "fs_access_%s", type);
700 EXEC SQL SELECT COUNT(trans) INTO :ok FROM alias
701 WHERE name = :ftype AND type = 'TYPE' and trans = :rwaccess;
705 return MR_FILESYS_ACCESS;
707 if ((mr_errcode = prefetch_value(q, argv, cl)) != MR_SUCCESS)
710 if (!strcmp(type, "NFS") || !strcmp(type, "IMAP"))
711 return check_nfs(mach_id, name, rwaccess);
717 /* Verify the arguments, depending on the FStype. Also, if this is an
718 * NFS filesystem, then update any quotas for that filesystem to reflect
722 int setup_ufil(struct query *q, char *argv[], client *cl)
726 EXEC SQL BEGIN DECLARE SECTION;
727 int fid, total, who, ok;
728 char *entity, ftype[FILESYS_TYPE_SIZE + 10], *access;
729 short int total_null;
730 EXEC SQL END DECLARE SECTION;
734 mach_id = *(int *)argv[3];
737 fid = *(int *)argv[0];
741 sprintf(ftype, "fs_access_%s", type);
742 EXEC SQL SELECT COUNT(trans) INTO :ok FROM alias
743 WHERE name = :ftype AND type = 'TYPE' AND trans = :access;
747 return MR_FILESYS_ACCESS;
749 EXEC SQL SELECT type INTO :ftype FROM filesys
750 WHERE filsys_id = :fid;
754 if (!strcmp(type, "NFS") || !strcmp(type, "IMAP"))
756 status = check_nfs(mach_id, name, access);
757 EXEC SQL UPDATE quota SET phys_id = :_var_phys_id
758 WHERE filsys_id = :fid;
763 else if (!strcmp(type, "AFS") && strcmp(strtrim(ftype), "AFS")
764 && strcmp(strtrim(ftype), "ERR"))
767 EXEC SQL DELETE FROM quota
768 WHERE type = 'ANY' AND filsys_id = :fid;
769 EXEC SQL SELECT SUM (quota) INTO :total:total_null FROM quota
770 WHERE filsys_id = :fid AND phys_id != 0;
773 if (!total_null && (total != 0))
775 EXEC SQL INSERT INTO quota (quota, filsys_id, phys_id, entity_id,
776 type, modtime, modby, modwith)
777 VALUES (:total, :fid, 0, 0, 'ANY', SYSDATE, :who, :entity);
784 EXEC SQL UPDATE quota SET phys_id = 0 WHERE filsys_id = :fid;
792 /* Find the NFS physical partition that the named directory is on.
793 * This is done by comparing the dir against the mount point of the
794 * partition. To make sure we get the correct match when there is
795 * more than one, we sort the query in reverse order by dir name.
798 int check_nfs(int mach_id, char *name, char *access)
800 EXEC SQL BEGIN DECLARE SECTION;
801 char dir[NFSPHYS_DIR_SIZE];
803 EXEC SQL END DECLARE SECTION;
809 EXEC SQL DECLARE csr101 CURSOR FOR
810 SELECT nfsphys_id, dir FROM nfsphys
815 EXEC SQL OPEN csr101;
820 EXEC SQL FETCH csr101 INTO :_var_phys_id, :dir;
837 EXEC SQL CLOSE csr101;
844 /* setup_dfil: free any quota records and fsgroup info associated with
845 * a filesystem when it is deleted. Also adjust the allocation numbers.
848 int setup_dfil(struct query *q, char **argv, client *cl)
850 EXEC SQL BEGIN DECLARE SECTION;
851 int id, total, phys_id;
853 EXEC SQL END DECLARE SECTION;
855 id = *(int *)argv[0];
856 EXEC SQL SELECT SUM (quota) INTO :total:none FROM quota
857 WHERE filsys_id = :id;
862 /** What if there are multiple phys_id's per f/s? (bad data) **/
863 EXEC SQL SELECT phys_id INTO :phys_id FROM filesys
864 WHERE filsys_id = :id;
865 EXEC SQL UPDATE nfsphys SET allocated = allocated - :total
866 WHERE nfsphys_id = :phys_id;
869 EXEC SQL DELETE FROM quota WHERE filsys_id = :id;
870 EXEC SQL DELETE FROM fsgroup WHERE filsys_id = :id;
871 EXEC SQL DELETE FROM fsgroup WHERE group_id = :id;
878 /* setup_dnfp: check to see that the nfs physical partition does not have
879 * any filesystems assigned to it before allowing it to be deleted.
882 int setup_dnfp(struct query *q, char **argv, client *cl)
884 EXEC SQL BEGIN DECLARE SECTION;
887 EXEC SQL END DECLARE SECTION;
889 id = *(int *)argv[0];
891 EXEC SQL SELECT count(fs.rowid) INTO :cnt FROM filesys fs, nfsphys np
892 WHERE fs.mach_id = :id AND fs.phys_id = np.nfsphys_id
893 AND np.mach_id = :id AND np.dir = :dir;
902 /* setup_dqot: Remove allocation from nfsphys before deleting quota.
903 * argv[0] = filsys_id
904 * argv[1] = type if "update_quota" or "delete_quota"
905 * argv[2 or 1] = users_id or list_id
908 int setup_dqot(struct query *q, char **argv, client *cl)
910 EXEC SQL BEGIN DECLARE SECTION;
911 int quota, fs, id, physid;
913 EXEC SQL END DECLARE SECTION;
915 fs = *(int *)argv[0];
916 if (!strcmp(q->name, "update_quota") || !strcmp(q->name, "delete_quota"))
919 id = *(int *)argv[2];
924 id = *(int *)argv[1];
927 EXEC SQL SELECT quota INTO :quota FROM quota
928 WHERE type = :qtype AND entity_id = :id AND filsys_id = :fs;
929 EXEC SQL SELECT phys_id INTO :physid FROM filesys
930 WHERE filsys_id = :fs;
931 EXEC SQL UPDATE nfsphys SET allocated = allocated - :quota
932 WHERE nfsphys_id = :physid;
941 * This routine fetches an appropriate value from the numvalues table.
942 * It is a little hack to get around the fact that SQL doesn't let you
943 * do something like INSERT INTO table (foo) VALUES (other_table.bar).
945 * It is called from the query table as (*v->pre_rtn)(q, Argv, cl) or
946 * from within a setup_...() routine with the appropriate arguments.
948 * Correct functioning of this routine may depend on the assumption
949 * that this query is an APPEND.
952 int prefetch_value(struct query *q, char **argv, client *cl)
954 EXEC SQL BEGIN DECLARE SECTION;
955 char *name = q->validate->object_id;
957 EXEC SQL END DECLARE SECTION;
958 int status, limit, argc;
960 /* set next object id, limiting it if necessary */
961 if (!strcmp(name, "unix_uid") || !strcmp(name, "gid"))
962 limit = 1; /* So far as I know, this isn't needed. Just CMA. */
965 if ((status = set_next_object_id(name, q->rtable, limit)) != MR_SUCCESS)
968 /* fetch object id */
969 EXEC SQL SELECT value INTO :value FROM numvalues WHERE name = :name;
972 if (sqlca.sqlerrd[2] != 1)
975 argc = q->argc + q->vcnt; /* end of Argv for APPENDs */
976 sprintf(argv[argc], "%d", value);
981 /* prefetch_filesys():
982 * Fetches the phys_id from filesys based on the filsys_id in argv[0].
983 * Appends the filsys_id and the phys_id to the argv so they can be
984 * referenced in an INSERT into a table other than filesys. Also
985 * see comments at prefetch_value().
987 * Assumes the existence of a row where filsys_id = argv[0], since a
988 * filesys label has already been resolved to a filsys_id.
990 int prefetch_filesys(struct query *q, char **argv, client *cl)
992 EXEC SQL BEGIN DECLARE SECTION;
994 EXEC SQL END DECLARE SECTION;
997 fid = *(int *)argv[0];
998 EXEC SQL SELECT phys_id INTO :phid FROM filesys WHERE filsys_id = :fid;
1002 argc = q->argc + q->vcnt;
1003 sprintf(argv[argc++], "%d", phid);
1004 sprintf(argv[argc], "%d", fid);
1013 int setup_ahst(struct query *q, char **argv, client *cl)
1015 EXEC SQL BEGIN DECLARE SECTION;
1016 char *name, oldname[MACHINE_NAME_SIZE], vendor[MACHINE_VENDOR_SIZE];
1017 char model[MACHINE_MODEL_SIZE], os[MACHINE_OS_SIZE];
1018 int value, id, ssaddr, smask, shigh, slow, cnt;
1019 unsigned int saddr, mask, high, low;
1020 EXEC SQL END DECLARE SECTION;
1022 struct in_addr addr;
1024 id = *(int *)argv[0];
1026 if (!strcmp(q->shortname, "uhst"))
1029 EXEC SQL SELECT name, vendor, model, os
1030 INTO :oldname, :vendor, :model, :os
1031 FROM machine WHERE mach_id = :id;
1038 else if (q->version >= 6 && q->version < 8)
1043 /* Sanity check name, vendor, model, and os. */
1044 if ((row == 0 || strcasecmp(argv[1], oldname)) &&
1045 !hostname_check(argv[row]))
1047 if ((row == 0 || strcasecmp(argv[2], vendor)) &&
1048 !hostinfo_check(argv[row + 1], 0))
1050 if ((row == 0 || strcasecmp(argv[3], model)) &&
1051 !hostinfo_check(argv[row + 2], 1))
1053 if ((row == 0 || strcasecmp(argv[4], os)) &&
1054 !hostinfo_check(argv[row + 3], 0))
1057 /* check for duplicate name */
1059 EXEC SQL SELECT count(mach_id) INTO :cnt FROM hostalias
1060 WHERE name = UPPER(:name);
1067 if (!strcmp(argv[9 + row + idx], "unassigned"))
1069 else if (!strcmp(argv[9 + row + idx], "unique"))
1071 if (*(int *)argv[8 + row + idx] == 0)
1078 value = ntohl(inet_addr(argv[9 + row + idx]));
1087 * an address or unique was specified.
1089 id = *(int *)argv[8 + row + idx];
1090 EXEC SQL SELECT saddr, mask, high, low INTO :ssaddr, :smask,
1091 :shigh, :slow FROM subnet WHERE snet_id = :id;
1094 saddr = (unsigned) ssaddr;
1095 mask = (unsigned) smask;
1096 high = (unsigned) shigh;
1097 low = (unsigned) slow;
1101 * someone specified an IP address for the host record
1103 if ((value & mask) != saddr || value < low || value > high)
1106 * run the address argument through inet_addr(). This
1107 * has the effect that any out of bounds host addrs will
1108 * be converted to a valid host addr. We do this now
1109 * so that the uniqueness check works. We should also
1110 * link in an inet_addr() that returns an error for
1113 addr.s_addr = inet_addr(argv[9 + row + idx]);
1114 name = inet_ntoa(addr);
1115 EXEC SQL SELECT count(mach_id) INTO :cnt FROM machine
1116 WHERE address = :name;
1122 * make IP address is unique. If this a modify request
1123 * (row == 1), then we expect one record to exist.
1125 if (row == 0 || (row == 1 && cnt > 1))
1127 if (row == 1 && cnt == 1)
1129 EXEC SQL SELECT mach_id INTO :id FROM machine
1130 WHERE address = :name;
1131 if (id != *(int *)argv[0])
1139 * a "unique" address was specified. Walk through the
1140 * range specified in the network record, return
1141 * error if no room left.
1143 for (id = low; id <= high; id++)
1145 if (((id & 0xff) == 0) || ((id & 0xff) == 255))
1147 addr.s_addr = htonl(id);
1148 name = inet_ntoa(addr);
1149 EXEC SQL SELECT count(mach_id) INTO :cnt FROM machine
1150 WHERE address = :name;
1162 * we have an address in value. Convert it to a string and store it.
1164 addr.s_addr = htonl(value);
1165 strcpy(argv[9 + row + idx], inet_ntoa(addr));
1168 strcpy(argv[9 + row + idx], "unassigned");
1170 /* status checking */
1171 value = atoi(argv[7 + row + idx]);
1172 if (row == 0 && !(value == 1 || value == 0))
1176 id = *(int *)argv[0];
1177 EXEC SQL SELECT status INTO :cnt FROM machine WHERE mach_id = :id;
1182 EXEC SQL UPDATE machine SET statuschange = SYSDATE
1183 WHERE mach_id = :id;
1188 * If this is an update_host query, we're done.
1194 * For an add_host query, allocate and fill in a new machine id,
1195 * and then insert the creator id.
1197 if ((mr_errcode = prefetch_value(q, argv, cl)) != MR_SUCCESS)
1200 sprintf(argv[q->argc + q->vcnt + 1], "%d", cl->client_id);
1208 int setup_ahal(struct query *q, char **argv, client *cl)
1210 EXEC SQL BEGIN DECLARE SECTION;
1213 EXEC SQL END DECLARE SECTION;
1217 if (!hostname_check(argv[0]))
1220 EXEC SQL SELECT count(mach_id) INTO :cnt FROM machine WHERE
1221 name = UPPER(:name);
1230 /* setup_uhha(): Check characters in hwaddr, and make sure it's not
1233 int setup_uhha(struct query *q, char **argv, client *cl)
1235 EXEC SQL BEGIN DECLARE SECTION;
1236 char *hwaddr = argv[1];
1238 EXEC SQL END DECLARE SECTION;
1241 if (*hwaddr && strcasecmp(hwaddr, "unknown"))
1243 for (p = hwaddr; *p; p++)
1250 if (p != hwaddr + 12)
1253 EXEC SQL SELECT COUNT(hwaddr) INTO :count
1254 FROM machine WHERE hwaddr = :hwaddr;
1256 return MR_NOT_UNIQUE;
1262 /* setup_aprn(): Make sure name/duplexname don't conflict with
1263 * anything. If [ANY] was specified for the spooling host, pick the
1264 * least loaded print server that serves this kind of printer.
1266 int setup_aprn(struct query *q, char **argv, client *cl)
1270 EXEC SQL BEGIN DECLARE SECTION;
1271 int mid, usage, count;
1272 char types[STRINGS_STRING_SIZE], *hwaddr, *name, *duplexname, *oldname;
1273 EXEC SQL END DECLARE SECTION;
1275 /* Check for aprn or uprn. */
1276 if (q->type == APPEND)
1281 name = argv[PRN_NAME + row];
1282 duplexname = argv[PRN_DUPLEXNAME + row];
1289 if (q->type == APPEND)
1291 EXEC SQL SELECT COUNT(name) INTO :count FROM printers
1292 WHERE name = :name OR duplexname = :name;
1296 EXEC SQL SELECT COUNT(name) INTO :count FROM printers
1297 WHERE ( name = :name OR duplexname = :name )
1298 AND name != :oldname;
1303 return MR_NOT_UNIQUE;
1308 if (q->type == APPEND)
1310 EXEC SQL SELECT COUNT(name) INTO :count FROM printers
1311 WHERE name = :duplexname OR duplexname = :duplexname;
1315 EXEC SQL SELECT COUNT(name) INTO :count FROM printers
1316 WHERE ( name = :duplexname OR duplexname = :duplexname )
1317 AND name != :oldname;
1323 return MR_NOT_UNIQUE;
1326 if (!strcmp(name, duplexname))
1327 return MR_NOT_UNIQUE;
1329 mid = *(int *)argv[PRN_RM + row];
1332 EXEC SQL DECLARE csr_rm CURSOR FOR
1333 SELECT ps.mach_id, s.string FROM printservers ps, strings s
1335 ( SELECT mach_id FROM serverhosts WHERE service = 'PRINT'
1337 AND ps.printer_types = s.string_id;
1340 EXEC SQL OPEN csr_rm;
1346 EXEC SQL FETCH csr_rm INTO :mid, :types;
1350 for (p = strtok(types, ", "); p; p = strtok(NULL, ", "))
1352 if (!strcasecmp(argv[PRN_TYPE + row], p))
1354 EXEC SQL SELECT COUNT(name) INTO :usage
1355 FROM printers WHERE rm = :mid;
1357 if (best < 0 || usage < best)
1360 *(int *)argv[PRN_RM + row] = mid;
1366 EXEC SQL CLOSE csr_rm;
1375 EXEC SQL SELECT mach_id INTO :mid FROM printservers
1376 WHERE mach_id = :mid;
1384 int setup_dpsv(struct query *q, char **argv, client *cl)
1387 EXEC SQL BEGIN DECLARE SECTION;
1389 EXEC SQL END DECLARE SECTION;
1391 id = *(int *)argv[0];
1393 EXEC SQL SELECT COUNT(rm) INTO :cnt FROM printers
1401 int setup_dcon(struct query *q, char *argv[], client *cl)
1403 EXEC SQL BEGIN DECLARE SECTION;
1405 char containername[CONTAINERS_NAME_SIZE];
1406 EXEC SQL END DECLARE SECTION;
1408 id = *(int *)argv[0];
1409 /* check to see if there are machines in this container */
1410 EXEC SQL SELECT COUNT(mach_id) INTO :cnt FROM mcntmap
1415 /* check to see if there are subcontainers in this container */
1417 /* get the container name */
1419 EXEC SQL SELECT name INTO :containername
1423 /* trim off the trailing spaces */
1424 strcpy(containername, strtrim(containername));
1426 EXEC SQL SELECT COUNT(cnt_id) INTO :cnt FROM containers
1427 WHERE LOWER(name) LIKE LOWER(:containername || '/' || '%');
1437 int setup_scli(struct query *q, char *argv[], client *cl)
1439 EXEC SQL BEGIN DECLARE SECTION;
1440 int cnt_id, list_id;
1441 EXEC SQL END DECLARE SECTION;
1443 cnt_id = *(int *)argv[0];
1444 /* Check if someone has already set the list for this container */
1445 EXEC SQL SELECT list_id INTO :list_id FROM containers
1446 WHERE cnt_id = :cnt_id;
1457 * validate the rfc1035/rfc1123-ness of a hostname
1460 int hostname_check(char *name)
1465 /* Sanity check name: must contain only letters, numerals, and
1466 * hyphen, and not start or end with a hyphen. Also make sure no
1467 * label (the thing the .s seperate) is longer than 63 characters,
1471 for (p = name, count = 0; *p; p++)
1474 if ((!isalnum(*p) && *p != '-' && *p != '.') ||
1475 (*p == '-' && p[1] == '.'))
1486 if (*(p - 1) == '-')
1491 int hostinfo_check(char *info, int num)
1498 /* Sanity check host hostinfo: must start with a letter (or number
1499 * if num is true), contain only letters, numerals, and hyphen, and
1500 * not end with a hyphen.
1503 if (!isalpha(*info) && (!num || !isdigit(*info)))
1505 for (p = info; *p; p++)
1507 if ((!isalnum(*p) && *p != '-' && *p != '.') ||
1508 (*p == '-' && p[1] == '.'))
1511 if (!isalnum(*(p - 1)))
1515 int setup_acon(struct query *q, char *argv[], client *cl)
1517 EXEC SQL BEGIN DECLARE SECTION;
1518 char containername[CONTAINERS_NAME_SIZE];
1519 EXEC SQL END DECLARE SECTION;
1523 memset(containername, 0, sizeof(containername));
1524 strcpy(containername, argv[0]);
1525 ptr = strrchr(containername, '/');
1526 /* sub container, check for parents */
1530 EXEC SQL SELECT * FROM containers
1531 WHERE lower(name) = lower(:containername);
1532 if (sqlca.sqlerrd[2] != 1)
1533 return MR_CONTAINER_NO_PARENT;
1536 if ((mr_errcode = prefetch_value(q, argv, cl)) != MR_SUCCESS)
1542 int check_mailman_listname(char *name, const char *suffix)
1545 EXEC SQL BEGIN DECLARE SECTION;
1547 EXEC SQL END DECLARE SECTION;
1549 p = strstr(name, suffix);
1552 if (strlen(name) == (p - name + strlen(suffix)))
1554 /* list is of the form "name-suffix" */
1556 EXEC SQL SELECT COUNT(name) INTO :cnt FROM list
1557 WHERE name = SUBSTR(:name, 1, :i) AND mailman = 1;