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