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