source: trunk/third/moira/gen/boot.pc @ 24447

Revision 24447, 6.4 KB checked in by broder, 14 years ago (diff)
In moira: * New SVN snapshot.
Line 
1/* $Id: boot.pc 3988 2010-02-27 06:24:35Z zacheiss $
2 *
3 * This generates the bootptab and associated files.
4 *
5 * Copyright (C) 1992-1998 by the Massachusetts Institute of Technology.
6 * For copying and distribution information, please see the file
7 * <mit-copyright.h>.
8 */
9
10#include <mit-copyright.h>
11#include <moira.h>
12#include <moira_site.h>
13
14#include <sys/types.h>
15
16#include <ctype.h>
17#include <stdio.h>
18#include <stdlib.h>
19#include <string.h>
20
21#include "util.h"
22
23EXEC SQL INCLUDE sqlca;
24
25RCSID("$HeadURL: svn+ssh://svn.mit.edu/moira/trunk/moira/gen/boot.pc $ $Id: boot.pc 3988 2010-02-27 06:24:35Z zacheiss $");
26
27char *whoami = "boot.gen";
28char *db = "moira/moira";
29
30void sqlerr(void);
31
32int main(int argc, char **argv)
33{
34  EXEC SQL BEGIN DECLARE SECTION;
35  char name[MACHINE_NAME_SIZE], hwaddr[MACHINE_HWADDR_SIZE];
36  char ohwaddr[MACHINE_HWADDR_SIZE], hwtype[PRINTERS_HWTYPE_SIZE];
37  char addr[MACHINE_ADDRESS_SIZE], location[PRINTERS_LOCATION_SIZE];
38  char contact[PRINTERS_CONTACT_SIZE], logaddr[MACHINE_ADDRESS_SIZE];
39  char modtime[PRINTERS_MODTIME_SIZE], type[PRINTERS_TYPE_SIZE];
40  char *unixtime_fmt = UNIXTIME_FMT;
41  char host[MACHINE_ADDRESS_SIZE], types[SERVERHOSTS_VALUE3_SIZE];
42  int mid, alcount;
43  EXEC SQL END DECLARE SECTION;
44  char shortname[MACHINE_NAME_SIZE], net[MACHINE_ADDRESS_SIZE];
45  char filename[MAXPATHLEN];
46  struct {
47    char types[SERVERHOSTS_VALUE3_SIZE];
48    char host[MACHINE_ADDRESS_SIZE];
49  } *allowlist;
50  char *p, *q;
51  int i, allows, typelen;
52  TARFILE *tf;
53  FILE *out;
54  time_t now = time(NULL);
55
56  EXEC SQL CONNECT :db;
57
58  EXEC SQL WHENEVER SQLERROR DO sqlerr();
59
60  /* Get print spoolers for allow lists. */
61  EXEC SQL SELECT COUNT(service) INTO :alcount FROM serverhosts sh
62    WHERE sh.service = 'PRINT' or sh.service = 'CUPS-PRINT';
63  allowlist = malloc(alcount * sizeof(*allowlist));
64
65  EXEC SQL DECLARE csr_spool CURSOR FOR
66    SELECT m.address, sh.value3 FROM machine m, serverhosts sh
67    WHERE m.mach_id = sh.mach_id AND
68    (sh.service = 'PRINT' OR sh.service = 'CUPS-PRINT');
69  EXEC SQL OPEN csr_spool;
70  for (i = 0; i < alcount; i++)
71    {
72      EXEC SQL FETCH csr_spool INTO :host, :types;
73      if (sqlca.sqlcode)
74        sqlerr();
75      strcpy(allowlist[i].host, strtrim(host));
76      strcpy(allowlist[i].types, strtrim(types));
77    }
78  EXEC SQL CLOSE csr_spool;
79
80  /* Now build the tar file. */
81  sprintf(filename, "%s/boot.out", DCM_DIR);
82  tf = tarfile_open(filename);
83
84  /* Build bootptab.print */
85
86  out = tarfile_start(tf, "/var/boot/bootptab.print", 0755, 0, 0,
87                      "root", "root", now);
88  ohwaddr[0] = '\0';
89  EXEC SQL DECLARE csr_boot CURSOR FOR
90    SELECT LOWER(m.name), m.hwaddr, m.address, m2.address,
91    pr.location, pr.contact, pr.hwtype
92    FROM printers pr, machine m, machine m2, machine m3
93    WHERE pr.type != 'ALIAS' AND pr.mach_id != 0
94    AND pr.mach_id = m.mach_id AND pr.loghost = m2.mach_id
95    AND pr.status = 1 and pr.mach_id = m3.mach_id AND m3.status !=3
96    ORDER BY m.hwaddr;
97  EXEC SQL OPEN csr_boot;
98  while (1)
99    {
100      EXEC SQL FETCH csr_boot INTO :name, :hwaddr, :addr, :logaddr,
101        :location, :contact, :hwtype;
102      if (sqlca.sqlcode)
103        break;
104
105      strtrim(hwaddr);
106      if (!*hwaddr || !strcasecmp(hwaddr, "unknown"))
107        continue;
108      if (!strcmp(hwaddr, ohwaddr))
109        {
110          fprintf(stderr, "Ignoring duplicate hwaddr %s\n", hwaddr);
111          continue;
112        }
113      strcpy(ohwaddr, hwaddr);
114
115      strtrim(name);
116      strtrim(addr);
117      strtrim(logaddr);
118      strtrim(location);
119      strtrim(contact);
120      strtrim(hwtype);
121      strcpy(shortname, name);
122      if ((p = strchr(shortname, '.')))
123        *p = '\0';
124
125      if ((p = strchr(addr, '.')) && (q = strchr(++p, '.')))
126        {
127          strncpy(net, p, q - p);
128          net[q - p] = '\0';
129        }
130      else
131        continue;
132
133      fprintf(out, "# %s: %s\n# contact: %s\n%s:\\\n\t:tc=net%s.global:\\\n"
134              "\t:ht=ethernet:\\\n\t:ha=%s:\\\n\t:ip=%s:\\\n"
135              "\t:lg=%s:\\\n\t:vm=rfc1048:",
136              shortname, location, contact, name, net, hwaddr, addr, logaddr);
137      if (!strncmp(hwtype, "HP", 2))
138        fprintf(out, "\\\n\t:T144=\"/hp/%s\":", shortname);
139      fprintf(out, "\n\n");
140    }
141  EXEC SQL CLOSE csr_boot;
142  tarfile_end(tf);
143
144  /* Now generate /var/boot/hp/ files */
145  EXEC SQL DECLARE csr_boot2 CURSOR FOR
146    SELECT LOWER(m.name), m.hwaddr, m2.address, m3.address, pr.type,
147    pr.location, pr.contact, TO_CHAR(pr.modtime, :unixtime_fmt)
148    FROM printers pr, machine m, machine m2, machine m3
149    WHERE pr.hwtype LIKE 'HP%' AND pr.mach_id != 0
150    AND pr.mach_id = m.mach_id AND m.status !=3 AND pr.rm = m2.mach_id
151    AND pr.loghost = m3.mach_id AND pr.type != 'ALIAS' AND pr.status = 1;
152  EXEC SQL OPEN csr_boot2;
153  while (1)
154    {
155      EXEC SQL FETCH csr_boot2 INTO :name, :hwaddr, :addr, :logaddr,
156        :type, :location, :contact, :modtime;
157      if (sqlca.sqlcode)
158        break;
159
160      strtrim(hwaddr);
161      if (!*hwaddr || !strcasecmp(hwaddr, "unknown"))
162        continue;
163
164      strtrim(name);
165      strtrim(addr);
166      strtrim(logaddr);
167      strtrim(type);
168      strtrim(location);
169      strtrim(contact);
170      strcpy(shortname, name);
171      if ((p = strchr(shortname, '.')))
172        *p = '\0';
173
174      /* We create it as foo.new so boot.sh can append the passwords
175       * and other data and rename it.
176       */
177      sprintf(filename, "/var/boot/hp/%s.new", shortname);
178      out = tarfile_start(tf, filename, 0755, 0, 0, "root", "root",
179                          unixtime(modtime));
180
181      fprintf(out, "name: %s\nlocation: %s\ncontact: %s\n\n", shortname,
182              *location ? location : "unknown",
183              *contact ? contact : "unknown");
184      if (*logaddr)
185        fprintf(out, "trap-dest: %s\nallow: %s\n", logaddr, logaddr);
186      fprintf(out, "allow: %s\n", addr);
187      typelen = strlen(type);
188      for (i = allows = 0; i < alcount && allows < 9; i++)
189        {
190          char *p;
191
192          /* Don't list the spoolhost twice. */
193          if (!strcmp(allowlist[i].host, addr))
194            continue;
195
196          p = strstr(allowlist[i].types, type);
197          if (!p)
198            continue;
199
200          /* Make sure the match was real, and not just because one type
201           * is a substring of another type.
202           */
203          if (p != allowlist[i].types && *(p - 1) != ',' && *(p - 1) != ' ')
204            continue;
205          p += typelen;
206          if (*p && *p != ',' && *p != ' ')
207            continue;
208
209          fprintf(out, "allow: %s\n", allowlist[i].host);
210          allows++;
211        }
212      /* Rest of data is same for all printers and is appended from a
213       * a file on the boot server.
214       */
215
216      tarfile_end(tf);
217    }
218
219  tarfile_close(tf);
220
221  exit(MR_SUCCESS);
222}
223
224void sqlerr(void)
225{
226  db_error(sqlca.sqlcode);
227}
Note: See TracBrowser for help on using the repository browser.