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();
36 int prefetch_value(struct query *q, char **argv, client *cl);
37 int check_nfs(int mach_idx, char *name, char *access);
41 /* Setup routine for add_user
43 * Inputs: argv[0] - login
48 * - if argv[1] == UNIQUE_UID then set argv[1] = next(uid)
49 * - if argv[0] == UNIQUE_LOGIN then set argv[0] = "#<uid>"
52 int setup_ausr(struct query *q, char *argv[], client *cl)
55 EXEC SQL BEGIN DECLARE SECTION;
57 EXEC SQL END DECLARE SECTION;
59 if (!strcmp(q->shortname, "uusr") || !strcmp(q->shortname, "uuac"))
64 if (strlen(argv[row + 2]) + strlen(argv[row + 3]) +
65 strlen(argv[row + 4]) + 2 > USERS_FULLNAME_SIZE)
66 return MR_ARG_TOO_LONG;
68 if (!strcmp(argv[row], UNIQUE_UID) || atoi(argv[row]) == -1)
70 if ((err = set_next_object_id("unix_uid", USERS_TABLE, 1)))
72 EXEC SQL SELECT value INTO :nuid FROM numvalues WHERE name = 'unix_uid';
73 if (sqlca.sqlerrd[2] != 1)
75 sprintf(argv[row], "%d", nuid);
78 if (!strcmp(argv[0], UNIQUE_LOGIN) || atoi(argv[row]) == -1)
79 sprintf(argv[0], "#%s", argv[row]);
81 if ((mr_errcode = prefetch_value(q, argv, cl)) != MR_SUCCESS)
88 /* setup_dusr - verify that the user is no longer being referenced
89 * and may safely be deleted.
92 int setup_dusr(struct query *q, char *argv[], client *cl)
94 EXEC SQL BEGIN DECLARE SECTION;
96 EXEC SQL END DECLARE SECTION;
100 /* For now, only allow users to be deleted if their status is 0 */
101 EXEC SQL SELECT status INTO :flag FROM users WHERE users_id = :id;
102 if (flag != 0 && flag != 4)
105 EXEC SQL DELETE FROM quota WHERE entity_id = :id AND type = 'USER';
106 EXEC SQL DELETE FROM krbmap WHERE users_id = :id;
107 EXEC SQL SELECT COUNT(member_id) INTO :cnt FROM imembers
108 WHERE member_id = :id AND member_type = 'USER';
111 EXEC SQL SELECT COUNT(label) INTO :cnt FROM filesys
115 EXEC SQL SELECT COUNT(name) INTO :cnt FROM list
116 WHERE acl_id = :id AND acl_type = 'USER';
119 EXEC SQL SELECT COUNT(name) INTO :cnt FROM servers
120 WHERE acl_id = :id AND acl_type = 'USER';
123 EXEC SQL SELECT COUNT(acl_id) INTO :cnt FROM hostaccess
124 WHERE acl_id = :id AND acl_type = 'USER';
133 /* setup_spop: verify that there is already a valid POP machine_id in the
134 * pop_id field. Also take care of keeping track of the post office usage.
136 int setup_spop(struct query *q, char *argv[], client *cl)
138 EXEC SQL BEGIN DECLARE SECTION;
140 char type[USERS_POTYPE_SIZE];
141 EXEC SQL END DECLARE SECTION;
143 id = *(int *)argv[0];
144 EXEC SQL SELECT potype, pop_id INTO :type, :mid FROM users
145 WHERE users_id = :id;
146 if (sqlca.sqlerrd[2] == 0)
148 EXEC SQL SELECT mach_id INTO :mid FROM machine
149 WHERE mach_id = :mid;
150 if (sqlca.sqlerrd[2] == 0)
152 if (strcmp(strtrim(type), "POP"))
153 set_pop_usage(mid, 1);
158 /* setup_dpob: Take care of keeping track of the post office usage.
160 int setup_dpob(struct query *q, char *argv[], client *cl)
162 EXEC SQL BEGIN DECLARE SECTION;
164 char type[USERS_POTYPE_SIZE];
165 EXEC SQL END DECLARE SECTION;
167 user = *(int *)argv[0];
168 EXEC SQL SELECT potype, pop_id INTO :type, :id FROM users
169 WHERE users_id = :user;
173 if (!strcmp(strtrim(type), "POP"))
174 set_pop_usage(id, -1);
179 /* setup_dmac - verify that the machine is no longer being referenced
180 * and may safely be deleted.
183 int setup_dmac(struct query *q, char *argv[], client *cl)
185 EXEC SQL BEGIN DECLARE SECTION;
187 EXEC SQL END DECLARE SECTION;
189 id = *(int *)argv[0];
191 EXEC SQL SELECT status INTO :flag FROM machine
195 EXEC SQL SELECT COUNT(login) INTO :cnt FROM users
196 WHERE potype = 'POP' AND pop_id = :id;
199 EXEC SQL SELECT COUNT(mach_id) INTO :cnt FROM serverhosts
203 EXEC SQL SELECT COUNT(mach_id) INTO :cnt FROM nfsphys
207 EXEC SQL SELECT COUNT(mach_id) INTO :cnt FROM hostaccess
211 EXEC SQL SELECT COUNT(mach_id) INTO :cnt FROM printcap
215 EXEC SQL SELECT COUNT(quotaserver) INTO :cnt FROM printcap
216 WHERE quotaserver = :id;
219 EXEC SQL SELECT COUNT(mach_id) INTO :cnt FROM palladium
223 EXEC SQL SELECT COUNT(mach_id) INTO :cnt FROM hostalias
228 EXEC SQL DELETE FROM mcmap WHERE mach_id = :id;
235 /* setup_dsnt - verify that the subnet is no longer being referenced
236 * and may safely be deleted.
239 int setup_dsnt(struct query *q, char *argv[], client *cl)
241 EXEC SQL BEGIN DECLARE SECTION;
243 EXEC SQL END DECLARE SECTION;
245 id = *(int *)argv[0];
246 EXEC SQL SELECT COUNT(mach_id) INTO :cnt FROM machine
254 /* setup_dclu - verify that the cluster is no longer being referenced
255 * and may safely be deleted.
258 int setup_dclu(struct query *q, char *argv[], client *cl)
260 EXEC SQL BEGIN DECLARE SECTION;
262 EXEC SQL END DECLARE SECTION;
264 id = *(int *)argv[0];
265 EXEC SQL SELECT COUNT(mach_id) INTO :cnt FROM mcmap
269 EXEC SQL SELECT COUNT(clu_id) INTO :cnt FROM svc
279 /* setup_alis - if argv[5] is non-zero and argv[6] is UNIQUE_ID, then allocate
280 * a new gid and put it in argv[6]. Otherwise if argv[6] is UNIQUE_ID but
281 * argv[5] is not, then remember that UNIQUE_ID is being stored by putting
282 * a -1 there. Remember that this is also used for ulis, with the indexes
283 * at 6 & 7. Also check that the list name does not contain uppercase
284 * characters, control characters, @, or :.
287 static int badlistchars[] = {
288 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* ^@ - ^O */
289 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* ^P - ^_ */
290 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, /* SPACE - / */
291 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, /* 0 - ? */
292 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* @ - O */
293 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, /* P - _ */
294 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* ` - o */
295 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, /* p - ^? */
296 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
297 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
298 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
299 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
300 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
301 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
302 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
303 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
306 int setup_alis(struct query *q, char *argv[], client *cl)
308 EXEC SQL BEGIN DECLARE SECTION;
310 EXEC SQL END DECLARE SECTION;
314 if (!strcmp(q->shortname, "alis"))
316 else if (!strcmp(q->shortname, "ulis"))
321 EXEC SQL BEGIN DECLARE SECTION;
322 int lid = *(int *)argv[0];
323 EXEC SQL END DECLARE SECTION;
325 if (acl_access_check(lid, cl))
329 for (p = (unsigned char *) argv[idx]; *p; p++)
331 if (badlistchars[*p])
335 if (!strcmp(argv[6 + idx], UNIQUE_GID) || atoi(argv[6 + idx]) == -1)
337 if (atoi(argv[5 + idx]))
339 if ((err = set_next_object_id("gid", LIST_TABLE, 1)))
341 EXEC SQL SELECT value INTO :ngid FROM numvalues
345 sprintf(argv[6 + idx], "%d", ngid);
348 strcpy(argv[6 + idx], "-1");
351 if ((mr_errcode = prefetch_value(q, argv, cl)) != MR_SUCCESS)
358 /* setup_dlis - verify that the list is no longer being referenced
359 * and may safely be deleted.
362 int setup_dlis(struct query *q, char *argv[], client *cl)
365 EXEC SQL BEGIN DECLARE SECTION;
367 EXEC SQL END DECLARE SECTION;
369 id = *(int *)argv[0];
371 EXEC SQL SELECT COUNT(member_id) INTO :cnt FROM imembers
372 WHERE member_id = :id AND member_type = 'LIST';
376 EXEC SQL SELECT COUNT(member_id) INTO :cnt FROM imembers
377 WHERE member_id = :id AND member_type = 'LIST';
381 EXEC SQL SELECT COUNT(member_id) INTO :cnt FROM imembers
386 EXEC SQL SELECT COUNT(label) INTO :cnt FROM filesys WHERE owners = :id;
390 EXEC SQL SELECT COUNT(tag) INTO :cnt FROM capacls WHERE list_id = :id;
394 EXEC SQL SELECT COUNT(name) INTO :cnt FROM list
395 WHERE acl_id = :id AND acl_type = 'LIST' AND list_id != :id;
399 EXEC SQL SELECT COUNT(name) INTO :cnt FROM servers
400 WHERE acl_id = :id AND acl_type = 'LIST';
404 EXEC SQL SELECT COUNT(entity_id) INTO :cnt FROM quota
405 WHERE entity_id = :id AND type = 'GROUP';
409 EXEC SQL SELECT COUNT(acl_id) INTO :cnt FROM hostaccess
410 WHERE acl_id = :id AND acl_type = 'LIST';
414 EXEC SQL SELECT COUNT(class) INTO :cnt FROM zephyr z
415 WHERE z.xmt_type = 'LIST' AND z.xmt_id = :id
416 OR z.sub_type = 'LIST' AND z.sub_id = :id
417 OR z.iws_type = 'LIST' AND z.iws_id = :id
418 OR z.iui_type = 'LIST' AND z.iui_id = :id;
426 /* setup_dsin - verify that the service is no longer being referenced
427 * and may safely be deleted.
430 int setup_dsin(struct query *q, char *argv[], client *cl)
432 EXEC SQL BEGIN DECLARE SECTION;
435 EXEC SQL END DECLARE SECTION;
438 EXEC SQL SELECT COUNT(service) INTO :cnt FROM serverhosts
439 WHERE service = UPPER(:svrname);
443 EXEC SQL SELECT inprogress INTO :ec FROM servers
444 WHERE name = UPPER(:svrname);
454 /* setup_dshi - verify that the service-host is no longer being referenced
455 * and may safely be deleted.
458 int setup_dshi(struct query *q, char *argv[], client *cl)
460 EXEC SQL BEGIN DECLARE SECTION;
463 EXEC SQL END DECLARE SECTION;
466 id = *(int *)argv[1];
468 EXEC SQL SELECT inprogress INTO :ec FROM serverhosts
469 WHERE service = UPPER(:svrname) AND mach_id = :id;
480 ** setup_add_filesys - verify existance of referenced file systems
486 ** argv[5] - rwaccess
492 ** * extract directory prefix from name
493 ** * verify mach_id/dir in nfsphys
494 ** * verify rwaccess in {r, w, R, W}
496 ** Side effect: sets variable _var_phys_id to the ID of the physical
497 ** filesystem (nfsphys_id for NFS, 0 for RVD)
500 ** MR_NFS - specified directory not exported
501 ** MR_FILESYS_ACCESS - invalid filesys access
505 EXEC SQL BEGIN DECLARE SECTION;
507 EXEC SQL END DECLARE SECTION;
509 int setup_afil(struct query *q, char *argv[], client *cl)
513 EXEC SQL BEGIN DECLARE SECTION;
515 char ftype[FILESYS_TYPE_SIZE + 10], *rwaccess;
516 EXEC SQL END DECLARE SECTION;
519 mach_id = *(int *)argv[2];
524 sprintf(ftype, "fs_access_%s", type);
525 EXEC SQL SELECT COUNT(trans) INTO :ok FROM alias
526 WHERE name = :ftype AND type = 'TYPE' and trans = :rwaccess;
530 return MR_FILESYS_ACCESS;
532 if ((mr_errcode = prefetch_value(q, argv, cl)) != MR_SUCCESS)
535 if (!strcmp(type, "NFS"))
536 return check_nfs(mach_id, name, rwaccess);
542 /* Verify the arguments, depending on the FStype. Also, if this is an
543 * NFS filesystem, then update any quotas for that filesystem to reflect
547 int setup_ufil(struct query *q, char *argv[], client *cl)
551 EXEC SQL BEGIN DECLARE SECTION;
552 int fid, total, who, ok;
553 char *entity, ftype[FILESYS_TYPE_SIZE + 10], *access;
554 short int total_null;
555 EXEC SQL END DECLARE SECTION;
559 mach_id = *(int *)argv[3];
562 fid = *(int *)argv[0];
566 sprintf(ftype, "fs_access_%s", type);
567 EXEC SQL SELECT COUNT(trans) INTO :ok FROM alias
568 WHERE name = :ftype AND type = 'TYPE' AND trans = :access;
572 return MR_FILESYS_ACCESS;
574 EXEC SQL SELECT type INTO :ftype FROM filesys
575 WHERE filsys_id = :fid;
579 if (!strcmp(type, "NFS"))
581 status = check_nfs(mach_id, name, access);
582 EXEC SQL UPDATE quota SET phys_id = :_var_phys_id
583 WHERE filsys_id = :fid;
588 else if (!strcmp(type, "AFS") && strcmp(strtrim(ftype), "AFS")
589 && strcmp(strtrim(ftype), "ERR"))
592 EXEC SQL DELETE FROM quota
593 WHERE type = 'ANY' AND filsys_id = :fid;
594 EXEC SQL SELECT SUM (quota) INTO :total:total_null FROM quota
595 WHERE filsys_id = :fid AND phys_id != 0;
598 if (!total_null && (total != 0))
600 EXEC SQL INSERT INTO quota (quota, filsys_id, phys_id, entity_id,
601 type, modtime, modby, modwith)
602 VALUES (:total, :fid, 0, 0, 'ANY', SYSDATE, :who, :entity);
609 EXEC SQL UPDATE quota SET phys_id = 0 WHERE filsys_id = :fid;
617 /* Find the NFS physical partition that the named directory is on.
618 * This is done by comparing the dir against the mount point of the
619 * partition. To make sure we get the correct match when there is
620 * more than one, we sort the query in reverse order by dir name.
623 int check_nfs(int mach_id, char *name, char *access)
625 EXEC SQL BEGIN DECLARE SECTION;
626 char dir[NFSPHYS_DIR_SIZE];
628 EXEC SQL END DECLARE SECTION;
634 EXEC SQL DECLARE csr101 CURSOR FOR
635 SELECT nfsphys_id, dir FROM nfsphys
640 EXEC SQL OPEN csr101;
645 EXEC SQL FETCH csr101 INTO :_var_phys_id, :dir;
662 EXEC SQL CLOSE csr101;
669 /* setup_dfil: free any quota records and fsgroup info associated with
670 * a filesystem when it is deleted. Also adjust the allocation numbers.
673 int setup_dfil(struct query *q, char **argv, client *cl)
675 EXEC SQL BEGIN DECLARE SECTION;
676 int id, total, phys_id;
678 EXEC SQL END DECLARE SECTION;
680 id = *(int *)argv[0];
681 EXEC SQL SELECT SUM (quota) INTO :total:none FROM quota
682 WHERE filsys_id = :id;
687 /** What if there are multiple phys_id's per f/s? (bad data) **/
688 EXEC SQL SELECT phys_id INTO :phys_id FROM filesys
689 WHERE filsys_id = :id;
690 EXEC SQL UPDATE nfsphys SET allocated = allocated - :total
691 WHERE nfsphys_id = :phys_id;
694 EXEC SQL DELETE FROM quota WHERE filsys_id = :id;
695 EXEC SQL DELETE FROM fsgroup WHERE filsys_id = :id;
696 EXEC SQL DELETE FROM fsgroup WHERE group_id = :id;
703 /* setup_dnfp: check to see that the nfs physical partition does not have
704 * any filesystems assigned to it before allowing it to be deleted.
707 int setup_dnfp(struct query *q, char **argv, client *cl)
709 EXEC SQL BEGIN DECLARE SECTION;
712 EXEC SQL END DECLARE SECTION;
714 id = *(int *)argv[0];
716 EXEC SQL SELECT count(fs.rowid) INTO :cnt FROM filesys fs, nfsphys np
717 WHERE fs.mach_id = :id AND fs.phys_id = np.nfsphys_id
718 AND np.mach_id = :id AND np.dir = :dir;
727 /* setup_dqot: Remove allocation from nfsphys before deleting quota.
728 * argv[0] = filsys_id
729 * argv[1] = type if "update_quota" or "delete_quota"
730 * argv[2 or 1] = users_id or list_id
733 int setup_dqot(struct query *q, char **argv, client *cl)
735 EXEC SQL BEGIN DECLARE SECTION;
736 int quota, fs, id, physid;
738 EXEC SQL END DECLARE SECTION;
740 fs = *(int *)argv[0];
741 if (!strcmp(q->name, "update_quota") || !strcmp(q->name, "delete_quota"))
744 id = *(int *)argv[2];
749 id = *(int *)argv[1];
752 EXEC SQL SELECT quota INTO :quota FROM quota
753 WHERE type = :qtype AND entity_id = :id AND filsys_id = :fs;
754 EXEC SQL SELECT phys_id INTO :physid FROM filesys
755 WHERE filsys_id = :fs;
756 EXEC SQL UPDATE nfsphys SET allocated = allocated - :quota
757 WHERE nfsphys_id = :physid;
766 * This routine fetches an appropriate value from the numvalues table.
767 * It is a little hack to get around the fact that SQL doesn't let you
768 * do something like INSERT INTO table (foo) VALUES (other_table.bar).
770 * It is called from the query table as (*v->pre_rtn)(q, Argv, cl) or
771 * from within a setup_...() routine with the appropriate arguments.
773 * Correct functioning of this routine may depend on the assumption
774 * that this query is an APPEND.
777 int prefetch_value(struct query *q, char **argv, client *cl)
779 EXEC SQL BEGIN DECLARE SECTION;
780 char *name = q->validate->object_id;
782 EXEC SQL END DECLARE SECTION;
783 int status, limit, argc;
785 /* set next object id, limiting it if necessary */
786 if (!strcmp(name, "unix_uid") || !strcmp(name, "gid"))
787 limit = 1; /* So far as I know, this isn't needed. Just CMA. */
790 if ((status = set_next_object_id(name, q->rtable, limit)) != MR_SUCCESS)
793 /* fetch object id */
794 EXEC SQL SELECT value INTO :value FROM numvalues WHERE name = :name;
797 if (sqlca.sqlerrd[2] != 1)
800 argc = q->argc + q->vcnt; /* end of Argv for APPENDs */
801 sprintf(argv[argc], "%d", value);
806 /* prefetch_filesys():
807 * Fetches the phys_id from filesys based on the filsys_id in argv[0].
808 * Appends the filsys_id and the phys_id to the argv so they can be
809 * referenced in an INSERT into a table other than filesys. Also
810 * see comments at prefetch_value().
812 * Assumes the existence of a row where filsys_id = argv[0], since a
813 * filesys label has already been resolved to a filsys_id.
815 int prefetch_filesys(struct query *q, char **argv, client *cl)
817 EXEC SQL BEGIN DECLARE SECTION;
819 EXEC SQL END DECLARE SECTION;
822 fid = *(int *)argv[0];
823 EXEC SQL SELECT phys_id INTO :phid FROM filesys WHERE filsys_id = :fid;
827 argc = q->argc + q->vcnt;
828 sprintf(argv[argc++], "%d", phid);
829 sprintf(argv[argc], "%d", fid);
838 int setup_ahst(struct query *q, char **argv, client *cl)
840 EXEC SQL BEGIN DECLARE SECTION;
841 char *name, oldname[MACHINE_NAME_SIZE], vendor[MACHINE_VENDOR_SIZE];
842 char model[MACHINE_MODEL_SIZE], os[MACHINE_OS_SIZE];
843 int value, id, ssaddr, smask, shigh, slow, cnt;
844 unsigned int saddr, mask, high, low;
845 EXEC SQL END DECLARE SECTION;
849 id = *(int *)argv[0];
851 if (!strcmp(q->shortname, "uhst"))
854 EXEC SQL SELECT name, vendor, model, os
855 INTO :oldname, :vendor, :model, :os
856 FROM machine WHERE mach_id = :id;
861 if (row == 0 || strcasecmp(argv[1], oldname))
863 if (!hostname_check(argv[row]))
867 /* sanity check host vendor: must start with a letter, contain only
868 * letters, numerals, and hyphen, and end with an alphanumeric.
870 if (*argv[row + 1] && (row == 0 || strcasecmp(argv[2], vendor)))
872 char *p = argv[row + 1];
878 if ((!isalnum(*p) && *p != '-' && *p != '.') ||
879 (*p == '-' && p[1] == '.'))
882 if (!isalnum(*(p - 1)))
886 /* sanity check host type: must start with a letter, contain only
887 * letters, numerals, and hyphen, and end with an alphanumeric.
889 if (*argv[row + 2] && (row == 0 || strcasecmp(argv[3], model)))
891 char *p = argv[row + 2];
897 if ((!isalnum(*p) && *p != '-' && *p != '.') ||
898 (*p == '-' && p[1] == '.'))
901 if (!isalnum(*(p - 1)))
905 /* sanity check host os: must start with a letter, contain only
906 * letters, numerals, and hyphen, and end with an hyphen alphanumeric.
908 if (*argv[row + 3] && (row == 0 || strcasecmp(argv[4], os)))
910 char *p = argv[row + 3];
916 if ((!isalnum(*p) && *p != '-' && *p != '.') ||
917 (*p == '-' && p[1] == '.'))
920 if (!isalnum(*(p - 1)))
924 /* check for duplicate name */
926 EXEC SQL SELECT count(mach_id) INTO :cnt FROM hostalias
927 WHERE name = UPPER(:name);
934 if (!strcmp(argv[9 + row], "unassigned"))
936 else if (!strcmp(argv[9 + row], "unique"))
938 if (*(int *)argv[8 + row] == 0)
945 value = ntohl(inet_addr(argv[9 + row]));
954 * an address or unique was specified.
956 id = *(int *)argv[8 + row];
957 EXEC SQL SELECT saddr, mask, high, low INTO :ssaddr, :smask,
958 :shigh, :slow FROM subnet WHERE snet_id = :id;
961 saddr = (unsigned) ssaddr;
962 mask = (unsigned) smask;
963 high = (unsigned) shigh;
964 low = (unsigned) slow;
968 * someone specified an IP address for the host record
970 if ((value & mask) != saddr || value < low || value > high)
973 * run the address argument through inet_addr(). This
974 * has the effect that any out of bounds host addrs will
975 * be converted to a valid host addr. We do this now
976 * so that the uniqueness check works. We should also
977 * link in an inet_addr() that returns an error for
980 addr.s_addr = inet_addr(argv[9 + row]);
981 name = inet_ntoa(addr);
982 EXEC SQL SELECT count(mach_id) INTO :cnt FROM machine
983 WHERE address = :name;
989 * make IP address is unique. If this a modify request
990 * (row == 1), then we expect one record to exist.
992 if (row == 0 || (row == 1 && cnt > 1))
994 if (row == 1 && cnt == 1)
996 EXEC SQL SELECT mach_id INTO :id FROM machine
997 WHERE address = :name;
998 if (id != *(int *)argv[0])
1006 * a "unique" address was specified. Walk through the
1007 * range specified in the network record, return
1008 * error if no room left.
1010 for (id = low; id <= high; id++)
1012 if (((id & 0xff) == 0) || ((id & 0xff) == 255))
1014 addr.s_addr = htonl(id);
1015 name = inet_ntoa(addr);
1016 EXEC SQL SELECT count(mach_id) INTO :cnt FROM machine
1017 WHERE address = :name;
1029 * we have an address in value. Convert it to a string and store it.
1031 addr.s_addr = htonl(value);
1032 strcpy(argv[9 + row], inet_ntoa(addr));
1035 strcpy(argv[9 + row], "unassigned");
1037 /* status checking */
1038 value = atoi(argv[7 + row]);
1039 if (row == 0 && !(value == 1 || value == 0))
1043 id = *(int *)argv[0];
1044 EXEC SQL SELECT status INTO :cnt FROM machine WHERE mach_id = :id;
1049 EXEC SQL UPDATE machine SET statuschange = SYSDATE
1050 WHERE mach_id = :id;
1055 * If this is an update_host query, we're done.
1061 * For an add_host query, allocate and fill in a new machine id,
1062 * and then insert the creator id.
1064 if ((mr_errcode = prefetch_value(q, argv, cl)) != MR_SUCCESS)
1067 sprintf(argv[q->argc + q->vcnt + 1], "%d", cl->client_id);
1075 int setup_ahal(struct query *q, char **argv, client *cl)
1077 EXEC SQL BEGIN DECLARE SECTION;
1080 EXEC SQL END DECLARE SECTION;
1084 if (!hostname_check(argv[0]))
1087 EXEC SQL SELECT count(mach_id) INTO :cnt FROM machine WHERE
1088 name = UPPER(:name);
1098 * validate the rfc1035-ness of a hostname
1101 int hostname_check(char *name)
1106 /* sanity check name: must start with a letter, contain only
1107 * letters, numerals, and hyphen, and not end with a hyphen.
1108 * also make sure no label (the thing the .s seperate) is longer
1109 * than 63 characters.
1119 if ((!isalnum(*p) && *p != '-' && *p != '.') ||
1120 (*p == '-' && p[1] == '.'))
1127 if (*(p - 1) == '-')