1 | /* $Id: boot.pc,v 1.8 2008-11-20 16:30:06 zacheiss Exp $ |
---|
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: /afs/athena.mit.edu/astaff/project/moiradev/repository/moira/gen/boot.pc,v 1.8 2008-11-20 16:30:06 zacheiss Exp $"); |
---|
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 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 |
---|
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 | ORDER BY m.hwaddr; |
---|
96 | EXEC SQL OPEN csr_boot; |
---|
97 | while (1) |
---|
98 | { |
---|
99 | EXEC SQL FETCH csr_boot INTO :name, :hwaddr, :addr, :logaddr, |
---|
100 | :location, :contact, :hwtype; |
---|
101 | if (sqlca.sqlcode) |
---|
102 | break; |
---|
103 | |
---|
104 | strtrim(hwaddr); |
---|
105 | if (!*hwaddr || !strcasecmp(hwaddr, "unknown")) |
---|
106 | continue; |
---|
107 | if (!strcmp(hwaddr, ohwaddr)) |
---|
108 | { |
---|
109 | fprintf(stderr, "Ignoring duplicate hwaddr %s\n", hwaddr); |
---|
110 | continue; |
---|
111 | } |
---|
112 | strcpy(ohwaddr, hwaddr); |
---|
113 | |
---|
114 | strtrim(name); |
---|
115 | strtrim(addr); |
---|
116 | strtrim(logaddr); |
---|
117 | strtrim(location); |
---|
118 | strtrim(contact); |
---|
119 | strtrim(hwtype); |
---|
120 | strcpy(shortname, name); |
---|
121 | if ((p = strchr(shortname, '.'))) |
---|
122 | *p = '\0'; |
---|
123 | |
---|
124 | if ((p = strchr(addr, '.')) && (q = strchr(++p, '.'))) |
---|
125 | { |
---|
126 | strncpy(net, p, q - p); |
---|
127 | net[q - p] = '\0'; |
---|
128 | } |
---|
129 | else |
---|
130 | continue; |
---|
131 | |
---|
132 | fprintf(out, "# %s: %s\n# contact: %s\n%s:\\\n\t:tc=net%s.global:\\\n" |
---|
133 | "\t:ht=ethernet:\\\n\t:ha=%s:\\\n\t:ip=%s:\\\n" |
---|
134 | "\t:lg=%s:\\\n\t:vm=rfc1048:", |
---|
135 | shortname, location, contact, name, net, hwaddr, addr, logaddr); |
---|
136 | if (!strncmp(hwtype, "HP", 2)) |
---|
137 | fprintf(out, "\\\n\t:T144=\"/hp/%s\":", shortname); |
---|
138 | fprintf(out, "\n\n"); |
---|
139 | } |
---|
140 | EXEC SQL CLOSE csr_boot; |
---|
141 | tarfile_end(tf); |
---|
142 | |
---|
143 | /* Now generate /var/boot/hp/ files */ |
---|
144 | EXEC SQL DECLARE csr_boot2 CURSOR FOR |
---|
145 | SELECT LOWER(m.name), m.hwaddr, m2.address, m3.address, pr.type, |
---|
146 | pr.location, pr.contact, TO_CHAR(pr.modtime, :unixtime_fmt) |
---|
147 | FROM printers pr, machine m, machine m2, machine m3 |
---|
148 | WHERE pr.hwtype LIKE 'HP%' AND pr.mach_id != 0 |
---|
149 | AND pr.mach_id = m.mach_id AND pr.rm = m2.mach_id |
---|
150 | AND pr.loghost = m3.mach_id AND pr.type != 'ALIAS'; |
---|
151 | EXEC SQL OPEN csr_boot2; |
---|
152 | while (1) |
---|
153 | { |
---|
154 | EXEC SQL FETCH csr_boot2 INTO :name, :hwaddr, :addr, :logaddr, |
---|
155 | :type, :location, :contact, :modtime; |
---|
156 | if (sqlca.sqlcode) |
---|
157 | break; |
---|
158 | |
---|
159 | strtrim(hwaddr); |
---|
160 | if (!*hwaddr || !strcasecmp(hwaddr, "unknown")) |
---|
161 | continue; |
---|
162 | |
---|
163 | strtrim(name); |
---|
164 | strtrim(addr); |
---|
165 | strtrim(logaddr); |
---|
166 | strtrim(type); |
---|
167 | strtrim(location); |
---|
168 | strtrim(contact); |
---|
169 | strcpy(shortname, name); |
---|
170 | if ((p = strchr(shortname, '.'))) |
---|
171 | *p = '\0'; |
---|
172 | |
---|
173 | /* We create it as foo.new so boot.sh can append the passwords |
---|
174 | * and other data and rename it. |
---|
175 | */ |
---|
176 | sprintf(filename, "/var/boot/hp/%s.new", shortname); |
---|
177 | out = tarfile_start(tf, filename, 0755, 0, 0, "root", "root", |
---|
178 | unixtime(modtime)); |
---|
179 | |
---|
180 | fprintf(out, "name: %s\nlocation: %s\ncontact: %s\n\n", shortname, |
---|
181 | *location ? location : "unknown", |
---|
182 | *contact ? contact : "unknown"); |
---|
183 | if (*logaddr) |
---|
184 | fprintf(out, "trap-dest: %s\nallow: %s\n", logaddr, logaddr); |
---|
185 | fprintf(out, "allow: %s\n", addr); |
---|
186 | typelen = strlen(type); |
---|
187 | for (i = allows = 0; i < alcount && allows < 9; i++) |
---|
188 | { |
---|
189 | char *p; |
---|
190 | |
---|
191 | /* Don't list the spoolhost twice. */ |
---|
192 | if (!strcmp(allowlist[i].host, addr)) |
---|
193 | continue; |
---|
194 | |
---|
195 | p = strstr(allowlist[i].types, type); |
---|
196 | if (!p) |
---|
197 | continue; |
---|
198 | |
---|
199 | /* Make sure the match was real, and not just because one type |
---|
200 | * is a substring of another type. |
---|
201 | */ |
---|
202 | if (p != allowlist[i].types && *(p - 1) != ',' && *(p - 1) != ' ') |
---|
203 | continue; |
---|
204 | p += typelen; |
---|
205 | if (*p && *p != ',' && *p != ' ') |
---|
206 | continue; |
---|
207 | |
---|
208 | fprintf(out, "allow: %s\n", allowlist[i].host); |
---|
209 | allows++; |
---|
210 | } |
---|
211 | /* Rest of data is same for all printers and is appended from a |
---|
212 | * a file on the boot server. |
---|
213 | */ |
---|
214 | |
---|
215 | tarfile_end(tf); |
---|
216 | } |
---|
217 | |
---|
218 | tarfile_close(tf); |
---|
219 | |
---|
220 | exit(MR_SUCCESS); |
---|
221 | } |
---|
222 | |
---|
223 | void sqlerr(void) |
---|
224 | { |
---|
225 | db_error(sqlca.sqlcode); |
---|
226 | } |
---|