3 * This generates the bootptab 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 EXEC SQL INCLUDE sqlca;
27 char *whoami = "boot.gen";
28 char *db = "moira/moira";
32 int main(int argc, char **argv)
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];
42 EXEC SQL END DECLARE SECTION;
43 char shortname[MACHINE_NAME_SIZE], net[MACHINE_ADDRESS_SIZE];
44 char filename[MAXPATHLEN];
46 char types[SERVERHOSTS_VALUE3_SIZE];
47 char host[MACHINE_ADDRESS_SIZE];
50 int i, allows, typelen;
53 time_t now = time(NULL);
57 EXEC SQL WHENEVER SQLERROR DO sqlerr();
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));
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++)
70 EXEC SQL FETCH csr_spool INTO :host, :types;
73 strcpy(allowlist[i].host, strtrim(host));
74 strcpy(allowlist[i].types, strtrim(types));
76 EXEC SQL CLOSE csr_spool;
78 /* Now build the tar file. */
79 sprintf(filename, "%s/boot.out", DCM_DIR);
80 tf = tarfile_open(filename);
82 /* Build bootptab.print */
84 out = tarfile_start(tf, "/var/boot/bootptab.print", 0755, 0, 0,
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;
96 EXEC SQL FETCH csr_boot INTO :name, :hwaddr, :addr, :logaddr,
102 if (!*hwaddr || !strcasecmp(hwaddr, "unknown"))
110 strcpy(shortname, name);
111 if ((p = strchr(shortname, '.')))
114 if ((p = strchr(addr, '.')) && (q = strchr(++p, '.')))
116 strncpy(net, p, q - p);
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,
128 EXEC SQL CLOSE csr_boot;
131 /* Now generate /var/boot/hp/ files */
132 EXEC SQL DECLARE csr_boot2 CURSOR FOR
133 SELECT LOWER(m.name), m.hwaddr, m2.address, m3.address, pr.type,
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
138 AND pr.loghost = m3.mach_id;
139 EXEC SQL OPEN csr_boot2;
142 EXEC SQL FETCH csr_boot2 INTO :name, :hwaddr, :addr, :logaddr,
143 :type, :location, :contact, :modtime;
148 if (!*hwaddr || !strcasecmp(hwaddr, "unknown"))
157 strcpy(shortname, name);
158 if ((p = strchr(shortname, '.')))
161 sprintf(filename, "/var/boot/hp/%s", shortname);
162 out = tarfile_start(tf, filename, 0755, 0, 0, "root", "root",
165 fprintf(out, "name: %s\nlocation: %s\ncontact: %s\n\n",
166 shortname, location, contact);
167 fprintf(out, "allow: %s\nallow: %s\n", logaddr, addr);
168 typelen = strlen(type);
169 for (i = allows = 0; i < alcount && allows < 9; i++)
173 /* Don't list the spoolhost twice. */
174 if (!strcmp(allowlist[i].host, addr))
177 p = strstr(allowlist[i].types, type);
181 /* Make sure the match was real, and not just because one type
182 * is a substring of another type.
184 if (p != allowlist[i].types && *(p - 1) != ',' && *(p - 1) != ' ')
187 if (*p && *p != ',' && *p != ' ')
190 fprintf(out, "allow: %s\n", allowlist[i].host);
193 fprintf(out, "trap-dest: %s\n", logaddr);
194 /* Rest of data is same for all printers and is appended from a
195 * a file on the boot server.
208 db_error(sqlca.sqlcode);