From a428372f6574f90270fb54c640d281d5479c50db Mon Sep 17 00:00:00 2001 From: danw Date: Wed, 27 Jan 1999 19:39:21 +0000 Subject: [PATCH] bootptab-generating DCM --- gen/boot.pc | 205 ++++++++++++++++++++++++++++++++++++++++++++++++++++ gen/boot.sh | 46 ++++++++++++ 2 files changed, 251 insertions(+) create mode 100644 gen/boot.pc create mode 100755 gen/boot.sh diff --git a/gen/boot.pc b/gen/boot.pc new file mode 100644 index 00000000..73c2d8d6 --- /dev/null +++ b/gen/boot.pc @@ -0,0 +1,205 @@ +/* $Id$ + * + * This generates the bootptab and associated files. + * + * Copyright (C) 1992-1998 by the Massachusetts Institute of Technology. + * For copying and distribution information, please see the file + * . + */ + +#include +#include +#include + +#include + +#include +#include +#include +#include + +#include "util.h" + +EXEC SQL INCLUDE sqlca; + +RCSID("$Header$"); + +char *whoami = "boot.gen"; +char *db = "moira/moira"; + +void sqlerr(void); + +int main(int argc, char **argv) +{ + EXEC SQL BEGIN DECLARE SECTION; + char name[MACHINE_NAME_SIZE], hwaddr[MACHINE_HWADDR_SIZE]; + char addr[MACHINE_ADDRESS_SIZE], location[PRINTERS_LOCATION_SIZE]; + char contact[PRINTERS_CONTACT_SIZE], logaddr[MACHINE_ADDRESS_SIZE]; + char modtime[PRINTERS_MODTIME_SIZE], type[PRINTERS_TYPE_SIZE]; + char *unixtime_fmt = UNIXTIME_FMT; + char host[MACHINE_ADDRESS_SIZE], types[SERVERHOSTS_VALUE3_SIZE]; + int mid, alcount; + EXEC SQL END DECLARE SECTION; + char shortname[MACHINE_NAME_SIZE], net[MACHINE_ADDRESS_SIZE]; + char filename[MAXPATHLEN]; + struct { + char types[SERVERHOSTS_VALUE3_SIZE]; + char host[MACHINE_ADDRESS_SIZE]; + } *allowlist; + char *p, *q; + int i, allows, typelen; + TARFILE *tf; + FILE *out; + time_t now = time(NULL); + + EXEC SQL CONNECT :db; + + EXEC SQL WHENEVER SQLERROR DO sqlerr(); + + /* Get print spoolers for allow lists. */ + EXEC SQL SELECT COUNT(service) INTO :alcount FROM serverhosts + WHERE service = 'PRINT'; + allowlist = malloc(alcount * sizeof(*allowlist)); + + EXEC SQL DECLARE csr_spool CURSOR FOR + SELECT m.address, sh.value3 FROM machine m, serverhosts sh + WHERE m.mach_id = sh.mach_id AND sh.service = 'PRINT'; + EXEC SQL OPEN csr_spool; + for (i = 0; i < alcount; i++) + { + EXEC SQL FETCH csr_spool INTO :host, :types; + if (sqlca.sqlcode) + sqlerr(); + strcpy(allowlist[i].host, strtrim(host)); + strcpy(allowlist[i].types, strtrim(types)); + } + EXEC SQL CLOSE csr_spool; + + /* Now build the tar file. */ + sprintf(filename, "%s/boot.out", DCM_DIR); + tf = tarfile_open(filename); + + /* Build bootptab.print */ + + out = tarfile_start(tf, "/var/boot/bootptab.print", 0755, 0, 0, + "root", "root", now); + EXEC SQL DECLARE csr_boot CURSOR FOR + SELECT LOWER(m.name), m.hwaddr, m.address, m2.address, + pr.location, pr.contact + FROM printers pr, machine m, machine m2 + WHERE pr.hwtype = 'HP' AND pr.mach_id != 0 + AND pr.mach_id = m.mach_id AND pr.loghost = m2.mach_id + AND pr.type != 'ALIAS'; + EXEC SQL OPEN csr_boot; + while (1) + { + EXEC SQL FETCH csr_boot INTO :name, :hwaddr, :addr, :logaddr, + :location, :contact; + if (sqlca.sqlcode) + break; + + strtrim(hwaddr); + if (!*hwaddr || !strcasecmp(hwaddr, "unknown")) + continue; + + strtrim(name); + strtrim(addr); + strtrim(logaddr); + strtrim(location); + strtrim(contact); + strcpy(shortname, name); + if ((p = strchr(shortname, '.'))) + *p = '\0'; + + if ((p = strchr(addr, '.')) && (q = strchr(++p, '.'))) + { + strncpy(net, p, q - p); + net[q - p] = '\0'; + } + else + continue; + + fprintf(out, "# %s: %s\n# contact: %s\n%s:\\\n\t:tc=net%s.global:\\\n" + "\t:ht=ethernet:\\\n\t:ha=%s:\\\n\t:ip=%s:\\\n" + "\t:lg=%s:\\\n\t:T144=\"/hp/%s\":\\\n\t:vm=rfc1048:\n\n", + shortname, location, contact, name, net, hwaddr, addr, logaddr, + shortname); + } + EXEC SQL CLOSE csr_boot; + tarfile_end(tf); + + /* Now generate /var/boot/hp/ files */ + EXEC SQL DECLARE csr_boot2 CURSOR FOR + SELECT LOWER(m.name), m2.address, m3.address, pr.type, + pr.location, pr.contact, TO_CHAR(pr.modtime, :unixtime_fmt) + FROM printers pr, machine m, machine m2, machine m3 + WHERE pr.hwtype = 'HP' AND pr.mach_id != 0 + AND pr.mach_id = m.mach_id AND pr.rm = m2.mach_id + AND pr.loghost = m3.mach_id; + EXEC SQL OPEN csr_boot2; + while (1) + { + EXEC SQL FETCH csr_boot2 INTO :name, :addr, :logaddr, :type, + :location, :contact, :modtime; + if (sqlca.sqlcode) + break; + + strtrim(name); + strtrim(addr); + strtrim(logaddr); + strtrim(type); + strtrim(location); + strtrim(contact); + strcpy(shortname, name); + if ((p = strchr(shortname, '.'))) + *p = '\0'; + + sprintf(filename, "/var/boot/hp/%s", shortname); + out = tarfile_start(tf, filename, 0755, 0, 0, "root", "root", + unixtime(modtime)); + + fprintf(out, "name: %s\nlocation: %s\ncontact: %s\n\n", + shortname, location, contact); + fprintf(out, "allow: %s\nallow: %s\n", logaddr, addr); + typelen = strlen(type); + for (i = allows = 0; i < alcount && allows < 9; i++) + { + char *p; + + /* Don't list the spoolhost twice. */ + if (!strcmp(allowlist[i].host, addr)) + continue; + + p = strstr(allowlist[i].types, type); + if (!p) + continue; + + /* Make sure the match was real, and not just because one type + * is a substring of another type. + */ + if (p != allowlist[i].types && *(p - 1) != ',' && *(p - 1) != ' ') + continue; + p += typelen; + if (*p && *p != ',' && *p != ' ') + continue; + + fprintf(out, "allow: %s\n", allowlist[i].host); + allows++; + } + fprintf(out, "trap-dest: %s\n", logaddr); + /* Rest of data is same for all printers and is appended from a + * a file on the boot server. + */ + + tarfile_end(tf); + } + + tarfile_close(tf); + + exit(MR_SUCCESS); +} + +void sqlerr(void) +{ + db_error(sqlca.sqlcode); +} diff --git a/gen/boot.sh b/gen/boot.sh new file mode 100755 index 00000000..41e12c77 --- /dev/null +++ b/gen/boot.sh @@ -0,0 +1,46 @@ +#! /bin/sh +# $Id$ + +# The following exit codes are defined and MUST BE CONSISTENT with the +# error codes the library uses: +MR_MISSINGFILE=47836473 +MR_MKCRED=47836474 +MR_TARERR=47836476 + +PATH=... +TARFILE=/var/tmp/boot.out +BOOTGEN=/var/boot/bootptab.print +BOOTLOCAL=/var/boot/bootptab.local +BOOTFILE=/var/boot/bootptab +PSWDFILE=/var/boot/hp.add + +# Alert if the tar file or other needed files do not exist +test -r $TARFILE || exit $MR_MISSINGFILE +test -r $BOOTLOCAL || exit $MR_MISSINGFILE +test -r $PSWDFILE || exit $MR_MISSINGFILE + +# Unpack the tar file, getting only files that are newer than the +# on-disk copies (-u). When extracting files under /var/boot/hp, +# tack on a .new so we can recognize them in the next step +cd / +pax -ru -s '|/var/boot/hp/.*$/&.new/' -f $TARFILE || exit $MR_TARERR + +# Append passwords, etc., to the new files +for f in find /var/boot/hp -name \*.new -print; do + cat $PSWDFILE >> $f + mv $f /var/boot/hp/`basename $f .new` +done + +# Build full bootptab +cat $BOOTLOCAL $BOOTGEN > $BOOTFILE + +# Signal bootpd? XXX + +# Remove out-of-date hp files +find /var/boot/hp -mtime +1 -exec rm -f {} \; + +# cleanup +test -f $TARFILE && rm -f $TARFILE +test -f $0 && rm -f $0 + +exit 0 -- 2.45.2