]>
Commit | Line | Data |
---|---|---|
a428372f | 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 addr[MACHINE_ADDRESS_SIZE], location[PRINTERS_LOCATION_SIZE]; | |
37 | char contact[PRINTERS_CONTACT_SIZE], logaddr[MACHINE_ADDRESS_SIZE]; | |
38 | char modtime[PRINTERS_MODTIME_SIZE], type[PRINTERS_TYPE_SIZE]; | |
39 | char *unixtime_fmt = UNIXTIME_FMT; | |
40 | char host[MACHINE_ADDRESS_SIZE], types[SERVERHOSTS_VALUE3_SIZE]; | |
41 | int mid, alcount; | |
42 | EXEC SQL END DECLARE SECTION; | |
43 | char shortname[MACHINE_NAME_SIZE], net[MACHINE_ADDRESS_SIZE]; | |
44 | char filename[MAXPATHLEN]; | |
45 | struct { | |
46 | char types[SERVERHOSTS_VALUE3_SIZE]; | |
47 | char host[MACHINE_ADDRESS_SIZE]; | |
48 | } *allowlist; | |
49 | char *p, *q; | |
50 | int i, allows, typelen; | |
51 | TARFILE *tf; | |
52 | FILE *out; | |
53 | time_t now = time(NULL); | |
54 | ||
55 | EXEC SQL CONNECT :db; | |
56 | ||
57 | EXEC SQL WHENEVER SQLERROR DO sqlerr(); | |
58 | ||
59 | /* Get print spoolers for allow lists. */ | |
60 | EXEC SQL SELECT COUNT(service) INTO :alcount FROM serverhosts | |
61 | WHERE service = 'PRINT'; | |
62 | allowlist = malloc(alcount * sizeof(*allowlist)); | |
63 | ||
64 | EXEC SQL DECLARE csr_spool CURSOR FOR | |
65 | SELECT m.address, sh.value3 FROM machine m, serverhosts sh | |
66 | WHERE m.mach_id = sh.mach_id AND sh.service = 'PRINT'; | |
67 | EXEC SQL OPEN csr_spool; | |
68 | for (i = 0; i < alcount; i++) | |
69 | { | |
70 | EXEC SQL FETCH csr_spool INTO :host, :types; | |
71 | if (sqlca.sqlcode) | |
72 | sqlerr(); | |
73 | strcpy(allowlist[i].host, strtrim(host)); | |
74 | strcpy(allowlist[i].types, strtrim(types)); | |
75 | } | |
76 | EXEC SQL CLOSE csr_spool; | |
77 | ||
78 | /* Now build the tar file. */ | |
79 | sprintf(filename, "%s/boot.out", DCM_DIR); | |
80 | tf = tarfile_open(filename); | |
81 | ||
82 | /* Build bootptab.print */ | |
83 | ||
84 | out = tarfile_start(tf, "/var/boot/bootptab.print", 0755, 0, 0, | |
85 | "root", "root", now); | |
86 | EXEC SQL DECLARE csr_boot CURSOR FOR | |
87 | SELECT LOWER(m.name), m.hwaddr, m.address, m2.address, | |
88 | pr.location, pr.contact | |
89 | FROM printers pr, machine m, machine m2 | |
90 | WHERE pr.hwtype = 'HP' AND pr.mach_id != 0 | |
91 | AND pr.mach_id = m.mach_id AND pr.loghost = m2.mach_id | |
92 | AND pr.type != 'ALIAS'; | |
93 | EXEC SQL OPEN csr_boot; | |
94 | while (1) | |
95 | { | |
96 | EXEC SQL FETCH csr_boot INTO :name, :hwaddr, :addr, :logaddr, | |
97 | :location, :contact; | |
98 | if (sqlca.sqlcode) | |
99 | break; | |
100 | ||
101 | strtrim(hwaddr); | |
102 | if (!*hwaddr || !strcasecmp(hwaddr, "unknown")) | |
103 | continue; | |
104 | ||
105 | strtrim(name); | |
106 | strtrim(addr); | |
107 | strtrim(logaddr); | |
108 | strtrim(location); | |
109 | strtrim(contact); | |
110 | strcpy(shortname, name); | |
111 | if ((p = strchr(shortname, '.'))) | |
112 | *p = '\0'; | |
113 | ||
114 | if ((p = strchr(addr, '.')) && (q = strchr(++p, '.'))) | |
115 | { | |
116 | strncpy(net, p, q - p); | |
117 | net[q - p] = '\0'; | |
118 | } | |
119 | else | |
120 | continue; | |
121 | ||
122 | fprintf(out, "# %s: %s\n# contact: %s\n%s:\\\n\t:tc=net%s.global:\\\n" | |
123 | "\t:ht=ethernet:\\\n\t:ha=%s:\\\n\t:ip=%s:\\\n" | |
124 | "\t:lg=%s:\\\n\t:T144=\"/hp/%s\":\\\n\t:vm=rfc1048:\n\n", | |
125 | shortname, location, contact, name, net, hwaddr, addr, logaddr, | |
126 | shortname); | |
127 | } | |
128 | EXEC SQL CLOSE csr_boot; | |
129 | tarfile_end(tf); | |
130 | ||
131 | /* Now generate /var/boot/hp/ files */ | |
132 | EXEC SQL DECLARE csr_boot2 CURSOR FOR | |
1712cf93 | 133 | SELECT LOWER(m.name), m.hwaddr, m2.address, m3.address, pr.type, |
a428372f | 134 | pr.location, pr.contact, TO_CHAR(pr.modtime, :unixtime_fmt) |
135 | FROM printers pr, machine m, machine m2, machine m3 | |
136 | WHERE pr.hwtype = 'HP' AND pr.mach_id != 0 | |
137 | AND pr.mach_id = m.mach_id AND pr.rm = m2.mach_id | |
2a5c1709 | 138 | AND pr.loghost = m3.mach_id AND pr.type != 'ALIAS'; |
a428372f | 139 | EXEC SQL OPEN csr_boot2; |
140 | while (1) | |
141 | { | |
1712cf93 | 142 | EXEC SQL FETCH csr_boot2 INTO :name, :hwaddr, :addr, :logaddr, |
143 | :type, :location, :contact, :modtime; | |
a428372f | 144 | if (sqlca.sqlcode) |
145 | break; | |
146 | ||
1712cf93 | 147 | strtrim(hwaddr); |
148 | if (!*hwaddr || !strcasecmp(hwaddr, "unknown")) | |
149 | continue; | |
150 | ||
a428372f | 151 | strtrim(name); |
152 | strtrim(addr); | |
153 | strtrim(logaddr); | |
154 | strtrim(type); | |
155 | strtrim(location); | |
156 | strtrim(contact); | |
157 | strcpy(shortname, name); | |
158 | if ((p = strchr(shortname, '.'))) | |
159 | *p = '\0'; | |
160 | ||
161 | sprintf(filename, "/var/boot/hp/%s", shortname); | |
162 | out = tarfile_start(tf, filename, 0755, 0, 0, "root", "root", | |
163 | unixtime(modtime)); | |
164 | ||
2a5c1709 | 165 | fprintf(out, "name: %s\nlocation: %s\ncontact: %s\n\n", shortname, |
166 | *location ? location : "unknown", | |
167 | *contact ? contact : "unknown"); | |
168 | if (*logaddr) | |
169 | fprintf(out, "trap-dest: %s\nallow: %s\n", logaddr, logaddr); | |
170 | fprintf(out, "allow: %s\n", addr); | |
a428372f | 171 | typelen = strlen(type); |
172 | for (i = allows = 0; i < alcount && allows < 9; i++) | |
173 | { | |
174 | char *p; | |
175 | ||
176 | /* Don't list the spoolhost twice. */ | |
177 | if (!strcmp(allowlist[i].host, addr)) | |
178 | continue; | |
179 | ||
180 | p = strstr(allowlist[i].types, type); | |
181 | if (!p) | |
182 | continue; | |
183 | ||
184 | /* Make sure the match was real, and not just because one type | |
185 | * is a substring of another type. | |
186 | */ | |
187 | if (p != allowlist[i].types && *(p - 1) != ',' && *(p - 1) != ' ') | |
188 | continue; | |
189 | p += typelen; | |
190 | if (*p && *p != ',' && *p != ' ') | |
191 | continue; | |
192 | ||
193 | fprintf(out, "allow: %s\n", allowlist[i].host); | |
194 | allows++; | |
195 | } | |
a428372f | 196 | /* Rest of data is same for all printers and is appended from a |
197 | * a file on the boot server. | |
198 | */ | |
199 | ||
200 | tarfile_end(tf); | |
201 | } | |
202 | ||
203 | tarfile_close(tf); | |
204 | ||
205 | exit(MR_SUCCESS); | |
206 | } | |
207 | ||
208 | void sqlerr(void) | |
209 | { | |
210 | db_error(sqlca.sqlcode); | |
211 | } |