]> andersk Git - moira.git/commitdiff
bootptab-generating DCM
authordanw <danw>
Wed, 27 Jan 1999 19:39:21 +0000 (19:39 +0000)
committerdanw <danw>
Wed, 27 Jan 1999 19:39:21 +0000 (19:39 +0000)
gen/boot.pc [new file with mode: 0644]
gen/boot.sh [new file with mode: 0755]

diff --git a/gen/boot.pc b/gen/boot.pc
new file mode 100644 (file)
index 0000000..73c2d8d
--- /dev/null
@@ -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
+ * <mit-copyright.h>.
+ */
+
+#include <mit-copyright.h>
+#include <moira.h>
+#include <moira_site.h>
+
+#include <sys/types.h>
+
+#include <ctype.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#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 (executable)
index 0000000..41e12c7
--- /dev/null
@@ -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
This page took 0.113627 seconds and 5 git commands to generate.