1 | /* $Id: dhcp.pc,v 1.7 2008-11-20 16:30:06 zacheiss Exp $ |
---|
2 | * |
---|
3 | * This generates the dhcpd.conf.print 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 | #define MACHINE_CHWADDR_SIZE 18 |
---|
24 | |
---|
25 | EXEC SQL INCLUDE sqlca; |
---|
26 | |
---|
27 | RCSID("$Header: /afs/.athena.mit.edu/astaff/project/moiradev/repository/moira/gen/dhcp.pc,v 1.7 2008-11-20 16:30:06 zacheiss Exp $"); |
---|
28 | |
---|
29 | char *whoami = "dhcp.gen"; |
---|
30 | char *db = "moira/moira"; |
---|
31 | |
---|
32 | void hwcolonify(char *from, char *to); |
---|
33 | |
---|
34 | void sqlerr(void); |
---|
35 | |
---|
36 | int main(int argc, char **argv) |
---|
37 | { |
---|
38 | EXEC SQL BEGIN DECLARE SECTION; |
---|
39 | char name[MACHINE_NAME_SIZE], hwaddr[MACHINE_HWADDR_SIZE]; |
---|
40 | char chwaddr[MACHINE_CHWADDR_SIZE]; |
---|
41 | char ohwaddr[MACHINE_HWADDR_SIZE], hwtype[PRINTERS_HWTYPE_SIZE]; |
---|
42 | char addr[MACHINE_ADDRESS_SIZE], location[PRINTERS_LOCATION_SIZE]; |
---|
43 | char contact[PRINTERS_CONTACT_SIZE], logaddr[MACHINE_ADDRESS_SIZE]; |
---|
44 | char modtime[PRINTERS_MODTIME_SIZE], type[PRINTERS_TYPE_SIZE]; |
---|
45 | char *unixtime_fmt = UNIXTIME_FMT; |
---|
46 | char host[MACHINE_ADDRESS_SIZE], types[SERVERHOSTS_VALUE3_SIZE]; |
---|
47 | int mid, alcount; |
---|
48 | EXEC SQL END DECLARE SECTION; |
---|
49 | char shortname[MACHINE_NAME_SIZE], net[MACHINE_ADDRESS_SIZE]; |
---|
50 | char filename[MAXPATHLEN]; |
---|
51 | struct { |
---|
52 | char types[SERVERHOSTS_VALUE3_SIZE]; |
---|
53 | char host[MACHINE_ADDRESS_SIZE]; |
---|
54 | } *allowlist; |
---|
55 | char *p, *q; |
---|
56 | int i, allows, typelen; |
---|
57 | TARFILE *tf; |
---|
58 | FILE *out; |
---|
59 | time_t now = time(NULL); |
---|
60 | |
---|
61 | EXEC SQL CONNECT :db; |
---|
62 | |
---|
63 | EXEC SQL WHENEVER SQLERROR DO sqlerr(); |
---|
64 | |
---|
65 | if (argc != 2) |
---|
66 | { |
---|
67 | fprintf(stderr, "usage: %s [outfile]\n", argv[0]); |
---|
68 | exit(MR_ARGS); |
---|
69 | } |
---|
70 | |
---|
71 | /* Get print spoolers for allow lists. */ |
---|
72 | EXEC SQL SELECT COUNT(service) INTO :alcount FROM serverhosts sh, machine m |
---|
73 | WHERE (sh.service = 'PRINT' OR sh.service = 'CUPS-PRINT') |
---|
74 | AND sh.mach_id = m.mach_id |
---|
75 | AND m.status = 1 AND m.address != 'unassigned'; |
---|
76 | allowlist = malloc(alcount * sizeof(*allowlist)); |
---|
77 | |
---|
78 | EXEC SQL DECLARE csr_spool CURSOR FOR |
---|
79 | SELECT m.address, sh.value3 FROM machine m, serverhosts sh |
---|
80 | WHERE m.mach_id = sh.mach_id AND |
---|
81 | (sh.service = 'PRINT' OR sh.service = 'CUPS-PRINT') |
---|
82 | AND m.status = 1 AND m.address != 'unassigned'; |
---|
83 | EXEC SQL OPEN csr_spool; |
---|
84 | for (i = 0; i < alcount; i++) |
---|
85 | { |
---|
86 | EXEC SQL FETCH csr_spool INTO :host, :types; |
---|
87 | if (sqlca.sqlcode) |
---|
88 | sqlerr(); |
---|
89 | strcpy(allowlist[i].host, strtrim(host)); |
---|
90 | strcpy(allowlist[i].types, strtrim(types)); |
---|
91 | } |
---|
92 | EXEC SQL CLOSE csr_spool; |
---|
93 | |
---|
94 | /* Now build the tar file. */ |
---|
95 | tf = tarfile_open(argv[1]); |
---|
96 | |
---|
97 | /* Build dhcpd.conf.print */ |
---|
98 | |
---|
99 | out = tarfile_start(tf, "/var/boot/dhcpd.conf.print", 0755, 0, 0, |
---|
100 | "root", "root", now); |
---|
101 | ohwaddr[0] = '\0'; |
---|
102 | EXEC SQL DECLARE csr_boot CURSOR FOR |
---|
103 | SELECT LOWER(m.name), m.hwaddr, m.address, m2.address, |
---|
104 | pr.location, pr.contact, pr.hwtype |
---|
105 | FROM printers pr, machine m, machine m2 |
---|
106 | WHERE pr.type != 'ALIAS' AND pr.mach_id != 0 |
---|
107 | AND pr.mach_id = m.mach_id AND pr.loghost = m2.mach_id |
---|
108 | ORDER BY m.hwaddr; |
---|
109 | EXEC SQL OPEN csr_boot; |
---|
110 | while (1) |
---|
111 | { |
---|
112 | EXEC SQL FETCH csr_boot INTO :name, :hwaddr, :addr, :logaddr, |
---|
113 | :location, :contact, :hwtype; |
---|
114 | if (sqlca.sqlcode) |
---|
115 | break; |
---|
116 | |
---|
117 | strtrim(hwaddr); |
---|
118 | if (!*hwaddr || !strcasecmp(hwaddr, "unknown")) |
---|
119 | continue; |
---|
120 | if (!strcmp(hwaddr, ohwaddr)) |
---|
121 | { |
---|
122 | fprintf(stderr, "Ignoring duplicate hwaddr %s\n", hwaddr); |
---|
123 | continue; |
---|
124 | } |
---|
125 | strcpy(ohwaddr, hwaddr); |
---|
126 | |
---|
127 | hwcolonify(hwaddr, chwaddr); |
---|
128 | |
---|
129 | strtrim(name); |
---|
130 | strtrim(addr); |
---|
131 | strtrim(logaddr); |
---|
132 | strtrim(location); |
---|
133 | strtrim(contact); |
---|
134 | strtrim(hwtype); |
---|
135 | strcpy(shortname, name); |
---|
136 | if ((p = strchr(shortname, '.'))) |
---|
137 | *p = '\0'; |
---|
138 | |
---|
139 | if ((p = strchr(addr, '.')) && (q = strchr(++p, '.'))) |
---|
140 | { |
---|
141 | strncpy(net, p, q - p); |
---|
142 | net[q - p] = '\0'; |
---|
143 | } |
---|
144 | else |
---|
145 | continue; |
---|
146 | |
---|
147 | fprintf(out, "# %s: %s\n# contact: %s\nhost %s {\n\t" |
---|
148 | "hardware ethernet %s;\n\tfixed-address %s;\n", |
---|
149 | shortname, location, contact, name, chwaddr, addr); |
---|
150 | |
---|
151 | if (strlen(logaddr) != 0) |
---|
152 | fprintf(out, "\toption log-servers %s;\n", logaddr); |
---|
153 | |
---|
154 | if (!strncmp(hwtype, "HP", 2)) |
---|
155 | fprintf(out, "\toption option-144 \"/hp/%s\";\n", shortname); |
---|
156 | fprintf(out, "}\n\n"); |
---|
157 | } |
---|
158 | EXEC SQL CLOSE csr_boot; |
---|
159 | tarfile_end(tf); |
---|
160 | |
---|
161 | /* Now generate /var/boot/hp/ files */ |
---|
162 | EXEC SQL DECLARE csr_boot2 CURSOR FOR |
---|
163 | SELECT LOWER(m.name), m.hwaddr, m2.address, m3.address, pr.type, |
---|
164 | pr.location, pr.contact, TO_CHAR(pr.modtime, :unixtime_fmt) |
---|
165 | FROM printers pr, machine m, machine m2, machine m3 |
---|
166 | WHERE pr.hwtype LIKE 'HP%' AND pr.mach_id != 0 |
---|
167 | AND pr.mach_id = m.mach_id AND pr.rm = m2.mach_id |
---|
168 | AND pr.loghost = m3.mach_id AND pr.type != 'ALIAS'; |
---|
169 | EXEC SQL OPEN csr_boot2; |
---|
170 | while (1) |
---|
171 | { |
---|
172 | EXEC SQL FETCH csr_boot2 INTO :name, :hwaddr, :addr, :logaddr, |
---|
173 | :type, :location, :contact, :modtime; |
---|
174 | if (sqlca.sqlcode) |
---|
175 | break; |
---|
176 | |
---|
177 | strtrim(hwaddr); |
---|
178 | if (!*hwaddr || !strcasecmp(hwaddr, "unknown")) |
---|
179 | continue; |
---|
180 | |
---|
181 | strtrim(name); |
---|
182 | strtrim(addr); |
---|
183 | strtrim(logaddr); |
---|
184 | strtrim(type); |
---|
185 | strtrim(location); |
---|
186 | strtrim(contact); |
---|
187 | strcpy(shortname, name); |
---|
188 | if ((p = strchr(shortname, '.'))) |
---|
189 | *p = '\0'; |
---|
190 | |
---|
191 | /* We create it as foo.new so dhcp.sh can append the passwords |
---|
192 | * and other data and rename it. |
---|
193 | */ |
---|
194 | sprintf(filename, "/var/boot/hp/%s.new", shortname); |
---|
195 | out = tarfile_start(tf, filename, 0755, 0, 0, "root", "root", |
---|
196 | unixtime(modtime)); |
---|
197 | |
---|
198 | fprintf(out, "name: %s\nlocation: %s\ncontact: %s\n\n", shortname, |
---|
199 | *location ? location : "unknown", |
---|
200 | *contact ? contact : "unknown"); |
---|
201 | if (*logaddr) |
---|
202 | fprintf(out, "trap-dest: %s\nallow: %s\n", logaddr, logaddr); |
---|
203 | fprintf(out, "allow: %s\n", addr); |
---|
204 | typelen = strlen(type); |
---|
205 | for (i = allows = 0; i < alcount && allows < 9; i++) |
---|
206 | { |
---|
207 | char *p; |
---|
208 | |
---|
209 | /* Don't list the spoolhost twice. */ |
---|
210 | if (!strcmp(allowlist[i].host, addr)) |
---|
211 | continue; |
---|
212 | |
---|
213 | p = strstr(allowlist[i].types, type); |
---|
214 | if (!p) |
---|
215 | continue; |
---|
216 | |
---|
217 | /* Make sure the match was real, and not just because one type |
---|
218 | * is a substring of another type. |
---|
219 | */ |
---|
220 | if (p != allowlist[i].types && *(p - 1) != ',' && *(p - 1) != ' ') |
---|
221 | continue; |
---|
222 | p += typelen; |
---|
223 | if (*p && *p != ',' && *p != ' ') |
---|
224 | continue; |
---|
225 | |
---|
226 | fprintf(out, "allow: %s\n", allowlist[i].host); |
---|
227 | allows++; |
---|
228 | } |
---|
229 | /* Rest of data is same for all printers and is appended from a |
---|
230 | * a file on the boot server. |
---|
231 | */ |
---|
232 | |
---|
233 | tarfile_end(tf); |
---|
234 | } |
---|
235 | |
---|
236 | tarfile_close(tf); |
---|
237 | |
---|
238 | exit(MR_SUCCESS); |
---|
239 | } |
---|
240 | |
---|
241 | void hwcolonify(char *from, char *to) |
---|
242 | { |
---|
243 | int f = 0, t = 0; |
---|
244 | int mod = 2; |
---|
245 | |
---|
246 | for (f = 0 ; f < MACHINE_HWADDR_SIZE - 1 ; ) |
---|
247 | { |
---|
248 | to[t++] = from[f++]; |
---|
249 | if (f % mod == 0) |
---|
250 | to[t++] = ':'; |
---|
251 | } |
---|
252 | |
---|
253 | if (f % mod == 0) |
---|
254 | t--; |
---|
255 | to[t] = '\0'; |
---|
256 | } |
---|
257 | |
---|
258 | void sqlerr(void) |
---|
259 | { |
---|
260 | db_error(sqlca.sqlcode); |
---|
261 | } |
---|