]> andersk Git - moira.git/blame - gen/boot.pc
add a NVL so "uhha foo ''" works
[moira.git] / gen / boot.pc
CommitLineData
a428372f 1/* $Id$
2 *
3 * This generates the bootptab and associated files.
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
23EXEC SQL INCLUDE sqlca;
24
25RCSID("$Header$");
26
27char *whoami = "boot.gen";
28char *db = "moira/moira";
29
30void sqlerr(void);
31
32int main(int argc, char **argv)
33{
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];
41 int mid, alcount;
42 EXEC SQL END DECLARE SECTION;
43 char shortname[MACHINE_NAME_SIZE], net[MACHINE_ADDRESS_SIZE];
44 char filename[MAXPATHLEN];
45 struct {
46 char types[SERVERHOSTS_VALUE3_SIZE];
47 char host[MACHINE_ADDRESS_SIZE];
48 } *allowlist;
49 char *p, *q;
50 int i, allows, typelen;
51 TARFILE *tf;
52 FILE *out;
53 time_t now = time(NULL);
54
55 EXEC SQL CONNECT :db;
56
57 EXEC SQL WHENEVER SQLERROR DO sqlerr();
58
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));
63
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++)
69 {
70 EXEC SQL FETCH csr_spool INTO :host, :types;
71 if (sqlca.sqlcode)
72 sqlerr();
73 strcpy(allowlist[i].host, strtrim(host));
74 strcpy(allowlist[i].types, strtrim(types));
75 }
76 EXEC SQL CLOSE csr_spool;
77
78 /* Now build the tar file. */
79 sprintf(filename, "%s/boot.out", DCM_DIR);
80 tf = tarfile_open(filename);
81
82 /* Build bootptab.print */
83
84 out = tarfile_start(tf, "/var/boot/bootptab.print", 0755, 0, 0,
85 "root", "root", now);
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;
94 while (1)
95 {
96 EXEC SQL FETCH csr_boot INTO :name, :hwaddr, :addr, :logaddr,
97 :location, :contact;
98 if (sqlca.sqlcode)
99 break;
100
101 strtrim(hwaddr);
102 if (!*hwaddr || !strcasecmp(hwaddr, "unknown"))
103 continue;
104
105 strtrim(name);
106 strtrim(addr);
107 strtrim(logaddr);
108 strtrim(location);
109 strtrim(contact);
110 strcpy(shortname, name);
111 if ((p = strchr(shortname, '.')))
112 *p = '\0';
113
114 if ((p = strchr(addr, '.')) && (q = strchr(++p, '.')))
115 {
116 strncpy(net, p, q - p);
117 net[q - p] = '\0';
118 }
119 else
120 continue;
121
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,
126 shortname);
127 }
128 EXEC SQL CLOSE csr_boot;
129 tarfile_end(tf);
130
131 /* Now generate /var/boot/hp/ files */
132 EXEC SQL DECLARE csr_boot2 CURSOR FOR
133 SELECT LOWER(m.name), 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;
140 while (1)
141 {
142 EXEC SQL FETCH csr_boot2 INTO :name, :addr, :logaddr, :type,
143 :location, :contact, :modtime;
144 if (sqlca.sqlcode)
145 break;
146
147 strtrim(name);
148 strtrim(addr);
149 strtrim(logaddr);
150 strtrim(type);
151 strtrim(location);
152 strtrim(contact);
153 strcpy(shortname, name);
154 if ((p = strchr(shortname, '.')))
155 *p = '\0';
156
157 sprintf(filename, "/var/boot/hp/%s", shortname);
158 out = tarfile_start(tf, filename, 0755, 0, 0, "root", "root",
159 unixtime(modtime));
160
161 fprintf(out, "name: %s\nlocation: %s\ncontact: %s\n\n",
162 shortname, location, contact);
163 fprintf(out, "allow: %s\nallow: %s\n", logaddr, addr);
164 typelen = strlen(type);
165 for (i = allows = 0; i < alcount && allows < 9; i++)
166 {
167 char *p;
168
169 /* Don't list the spoolhost twice. */
170 if (!strcmp(allowlist[i].host, addr))
171 continue;
172
173 p = strstr(allowlist[i].types, type);
174 if (!p)
175 continue;
176
177 /* Make sure the match was real, and not just because one type
178 * is a substring of another type.
179 */
180 if (p != allowlist[i].types && *(p - 1) != ',' && *(p - 1) != ' ')
181 continue;
182 p += typelen;
183 if (*p && *p != ',' && *p != ' ')
184 continue;
185
186 fprintf(out, "allow: %s\n", allowlist[i].host);
187 allows++;
188 }
189 fprintf(out, "trap-dest: %s\n", logaddr);
190 /* Rest of data is same for all printers and is appended from a
191 * a file on the boot server.
192 */
193
194 tarfile_end(tf);
195 }
196
197 tarfile_close(tf);
198
199 exit(MR_SUCCESS);
200}
201
202void sqlerr(void)
203{
204 db_error(sqlca.sqlcode);
205}
This page took 0.119806 seconds and 5 git commands to generate.