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