3 * This generates the dhcpd.conf.print and associated files.
5 * Copyright (C) 1992-1998 by the Massachusetts Institute of Technology.
6 * For copying and distribution information, please see the file
10 #include <mit-copyright.h>
12 #include <moira_site.h>
14 #include <sys/types.h>
23 #define MACHINE_CHWADDR_SIZE 18
25 EXEC SQL INCLUDE sqlca;
29 char *whoami = "dhcp.gen";
30 char *db = "moira/moira";
32 void hwcolonify(char *from, char *to);
36 int main(int argc, char **argv)
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];
48 EXEC SQL END DECLARE SECTION;
49 char shortname[MACHINE_NAME_SIZE], net[MACHINE_ADDRESS_SIZE];
50 char filename[MAXPATHLEN];
52 char types[SERVERHOSTS_VALUE3_SIZE];
53 char host[MACHINE_ADDRESS_SIZE];
56 int i, allows, typelen;
59 time_t now = time(NULL);
63 EXEC SQL WHENEVER SQLERROR DO sqlerr();
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));
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++)
76 EXEC SQL FETCH csr_spool INTO :host, :types;
79 strcpy(allowlist[i].host, strtrim(host));
80 strcpy(allowlist[i].types, strtrim(types));
82 EXEC SQL CLOSE csr_spool;
84 /* Now build the tar file. */
85 sprintf(filename, "%s/dhcp.out", DCM_DIR);
86 tf = tarfile_open(filename);
88 /* Build dhcpd.conf.print */
90 out = tarfile_start(tf, "/var/boot/dhcpd.conf.print", 0755, 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 AND m.status = 1 AND m.address != 'unassigned'
101 EXEC SQL OPEN csr_boot;
104 EXEC SQL FETCH csr_boot INTO :name, :hwaddr, :addr, :logaddr,
105 :location, :contact, :hwtype;
110 if (!*hwaddr || !strcasecmp(hwaddr, "unknown"))
112 if (!strcmp(hwaddr, ohwaddr))
114 fprintf(stderr, "Ignoring duplicate hwaddr %s\n", hwaddr);
117 strcpy(ohwaddr, hwaddr);
119 hwcolonify(hwaddr, chwaddr);
127 strcpy(shortname, name);
128 if ((p = strchr(shortname, '.')))
131 if ((p = strchr(addr, '.')) && (q = strchr(++p, '.')))
133 strncpy(net, p, q - p);
139 fprintf(out, "# %s: %s\n# contact: %s\nhost %s {\n\t"
140 "hardware ethernet %s;\n\tfixed-address %s;\n",
141 shortname, location, contact, name, chwaddr, addr);
143 if (strlen(logaddr) != 0)
144 fprintf(out, "\toption log-servers %s;\n", logaddr);
146 if (!strncmp(hwtype, "HP", 2))
147 fprintf(out, "\toption option-144 \"/hp/%s\";\n", shortname);
148 fprintf(out, "}\n\n");
150 EXEC SQL CLOSE csr_boot;
153 /* Now generate /var/boot/hp/ files */
154 EXEC SQL DECLARE csr_boot2 CURSOR FOR
155 SELECT LOWER(m.name), m.hwaddr, m2.address, m3.address, pr.type,
156 pr.location, pr.contact, TO_CHAR(pr.modtime, :unixtime_fmt)
157 FROM printers pr, machine m, machine m2, machine m3
158 WHERE pr.hwtype LIKE 'HP%' AND pr.mach_id != 0
159 AND pr.mach_id = m.mach_id AND pr.rm = m2.mach_id
160 AND pr.loghost = m3.mach_id AND pr.type != 'ALIAS';
161 EXEC SQL OPEN csr_boot2;
164 EXEC SQL FETCH csr_boot2 INTO :name, :hwaddr, :addr, :logaddr,
165 :type, :location, :contact, :modtime;
170 if (!*hwaddr || !strcasecmp(hwaddr, "unknown"))
179 strcpy(shortname, name);
180 if ((p = strchr(shortname, '.')))
183 /* We create it as foo.new so dhcp.sh can append the passwords
184 * and other data and rename it.
186 sprintf(filename, "/var/boot/hp/%s.new", shortname);
187 out = tarfile_start(tf, filename, 0755, 0, 0, "root", "root",
190 fprintf(out, "name: %s\nlocation: %s\ncontact: %s\n\n", shortname,
191 *location ? location : "unknown",
192 *contact ? contact : "unknown");
194 fprintf(out, "trap-dest: %s\nallow: %s\n", logaddr, logaddr);
195 fprintf(out, "allow: %s\n", addr);
196 typelen = strlen(type);
197 for (i = allows = 0; i < alcount && allows < 9; i++)
201 /* Don't list the spoolhost twice. */
202 if (!strcmp(allowlist[i].host, addr))
205 p = strstr(allowlist[i].types, type);
209 /* Make sure the match was real, and not just because one type
210 * is a substring of another type.
212 if (p != allowlist[i].types && *(p - 1) != ',' && *(p - 1) != ' ')
215 if (*p && *p != ',' && *p != ' ')
218 fprintf(out, "allow: %s\n", allowlist[i].host);
221 /* Rest of data is same for all printers and is appended from a
222 * a file on the boot server.
233 void hwcolonify(char *from, char *to)
238 for (f = 0 ; f < MACHINE_HWADDR_SIZE - 1 ; )
252 db_error(sqlca.sqlcode);