]> andersk Git - moira.git/blob - gen/boot.pc
f7f127578bbd712d1f96184dec38f3c35a0945ac
[moira.git] / gen / boot.pc
1 /* $Id$
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
23 EXEC SQL INCLUDE sqlca;
24
25 RCSID("$Header$");
26
27 char *whoami = "boot.gen";
28 char *db = "moira/moira";
29
30 void sqlerr(void);
31
32 int 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
62     WHERE service = '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 sh.service = 'PRINT';
68   EXEC SQL OPEN csr_spool;
69   for (i = 0; i < alcount; i++)
70     {
71       EXEC SQL FETCH csr_spool INTO :host, :types;
72       if (sqlca.sqlcode)
73         sqlerr();
74       strcpy(allowlist[i].host, strtrim(host));
75       strcpy(allowlist[i].types, strtrim(types));
76     }
77   EXEC SQL CLOSE csr_spool;
78
79   /* Now build the tar file. */
80   sprintf(filename, "%s/boot.out", DCM_DIR);
81   tf = tarfile_open(filename);
82
83   /* Build bootptab.print */
84
85   out = tarfile_start(tf, "/var/boot/bootptab.print", 0755, 0, 0,
86                       "root", "root", now);
87   ohwaddr[0] = '\0';
88   EXEC SQL DECLARE csr_boot CURSOR FOR
89     SELECT LOWER(m.name), m.hwaddr, m.address, m2.address,
90     pr.location, pr.contact, pr.hwtype
91     FROM printers pr, machine m, machine m2
92     WHERE pr.type != 'ALIAS' AND pr.mach_id != 0
93     AND pr.mach_id = m.mach_id AND pr.loghost = m2.mach_id
94     ORDER BY m.hwaddr;
95   EXEC SQL OPEN csr_boot;
96   while (1)
97     {
98       EXEC SQL FETCH csr_boot INTO :name, :hwaddr, :addr, :logaddr,
99         :location, :contact, :hwtype;
100       if (sqlca.sqlcode)
101         break;
102
103       strtrim(hwaddr);
104       if (!*hwaddr || !strcasecmp(hwaddr, "unknown"))
105         continue;
106       if (!strcmp(hwaddr, ohwaddr))
107         {
108           fprintf(stderr, "Ignoring duplicate hwaddr %s\n", hwaddr);
109           continue;
110         }
111       strcpy(ohwaddr, hwaddr);
112
113       strtrim(name);
114       strtrim(addr);
115       strtrim(logaddr);
116       strtrim(location);
117       strtrim(contact);
118       strtrim(hwtype);
119       strcpy(shortname, name);
120       if ((p = strchr(shortname, '.')))
121         *p = '\0';
122
123       if ((p = strchr(addr, '.')) && (q = strchr(++p, '.')))
124         {
125           strncpy(net, p, q - p);
126           net[q - p] = '\0';
127         }
128       else
129         continue;
130
131       fprintf(out, "# %s: %s\n# contact: %s\n%s:\\\n\t:tc=net%s.global:\\\n"
132               "\t:ht=ethernet:\\\n\t:ha=%s:\\\n\t:ip=%s:\\\n"
133               "\t:lg=%s:\\\n\t:vm=rfc1048:",
134               shortname, location, contact, name, net, hwaddr, addr, logaddr);
135       if (!strncmp(hwtype, "HP", 2))
136         fprintf(out, "\\\n\t:T144=\"/hp/%s\":", shortname);
137       fprintf(out, "\n\n");
138     }
139   EXEC SQL CLOSE csr_boot;
140   tarfile_end(tf);
141
142   /* Now generate /var/boot/hp/ files */
143   EXEC SQL DECLARE csr_boot2 CURSOR FOR
144     SELECT LOWER(m.name), m.hwaddr, m2.address, m3.address, pr.type,
145     pr.location, pr.contact, TO_CHAR(pr.modtime, :unixtime_fmt)
146     FROM printers pr, machine m, machine m2, machine m3
147     WHERE pr.hwtype LIKE 'HP%' AND pr.mach_id != 0
148     AND pr.mach_id = m.mach_id AND pr.rm = m2.mach_id
149     AND pr.loghost = m3.mach_id AND pr.type != 'ALIAS';
150   EXEC SQL OPEN csr_boot2;
151   while (1)
152     {
153       EXEC SQL FETCH csr_boot2 INTO :name, :hwaddr, :addr, :logaddr,
154         :type, :location, :contact, :modtime;
155       if (sqlca.sqlcode)
156         break;
157
158       strtrim(hwaddr);
159       if (!*hwaddr || !strcasecmp(hwaddr, "unknown"))
160         continue;
161
162       strtrim(name);
163       strtrim(addr);
164       strtrim(logaddr);
165       strtrim(type);
166       strtrim(location);
167       strtrim(contact);
168       strcpy(shortname, name);
169       if ((p = strchr(shortname, '.')))
170         *p = '\0';
171
172       /* We create it as foo.new so boot.sh can append the passwords
173        * and other data and rename it.
174        */
175       sprintf(filename, "/var/boot/hp/%s.new", shortname);
176       out = tarfile_start(tf, filename, 0755, 0, 0, "root", "root",
177                           unixtime(modtime));
178
179       fprintf(out, "name: %s\nlocation: %s\ncontact: %s\n\n", shortname,
180               *location ? location : "unknown",
181               *contact ? contact : "unknown");
182       if (*logaddr)
183         fprintf(out, "trap-dest: %s\nallow: %s\n", logaddr, logaddr);
184       fprintf(out, "allow: %s\n", addr);
185       typelen = strlen(type);
186       for (i = allows = 0; i < alcount && allows < 9; i++)
187         {
188           char *p;
189
190           /* Don't list the spoolhost twice. */
191           if (!strcmp(allowlist[i].host, addr))
192             continue;
193
194           p = strstr(allowlist[i].types, type);
195           if (!p)
196             continue;
197
198           /* Make sure the match was real, and not just because one type
199            * is a substring of another type.
200            */
201           if (p != allowlist[i].types && *(p - 1) != ',' && *(p - 1) != ' ')
202             continue;
203           p += typelen;
204           if (*p && *p != ',' && *p != ' ')
205             continue;
206
207           fprintf(out, "allow: %s\n", allowlist[i].host);
208           allows++;
209         }
210       /* Rest of data is same for all printers and is appended from a
211        * a file on the boot server.
212        */
213
214       tarfile_end(tf);
215     }
216
217   tarfile_close(tf);
218
219   exit(MR_SUCCESS);
220 }
221
222 void sqlerr(void)
223 {
224   db_error(sqlca.sqlcode);
225 }
This page took 0.074043 seconds and 3 git commands to generate.