]> andersk Git - moira.git/blob - gen/boot.pc
don't generate hp/* files for printers with no hwaddr
[moira.git] / gen / boot.pc
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
23 EXEC SQL INCLUDE sqlca;
24
25 RCSID("$Header$");
26
27 char *whoami = "boot.gen";
28 char *db = "moira/moira";
29
30 void sqlerr(void);
31
32 int 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), 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;
140   while (1)
141     {
142       EXEC SQL FETCH csr_boot2 INTO :name, :hwaddr, :addr, :logaddr,
143         :type, :location, :contact, :modtime;
144       if (sqlca.sqlcode)
145         break;
146
147       strtrim(hwaddr);
148       if (!*hwaddr || !strcasecmp(hwaddr, "unknown"))
149         continue;
150
151       strtrim(name);
152       strtrim(addr);
153       strtrim(logaddr);
154       strtrim(type);
155       strtrim(location);
156       strtrim(contact);
157       strcpy(shortname, name);
158       if ((p = strchr(shortname, '.')))
159         *p = '\0';
160
161       sprintf(filename, "/var/boot/hp/%s", shortname);
162       out = tarfile_start(tf, filename, 0755, 0, 0, "root", "root",
163                           unixtime(modtime));
164
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++)
170         {
171           char *p;
172
173           /* Don't list the spoolhost twice. */
174           if (!strcmp(allowlist[i].host, addr))
175             continue;
176
177           p = strstr(allowlist[i].types, type);
178           if (!p)
179             continue;
180
181           /* Make sure the match was real, and not just because one type
182            * is a substring of another type.
183            */
184           if (p != allowlist[i].types && *(p - 1) != ',' && *(p - 1) != ' ')
185             continue;
186           p += typelen;
187           if (*p && *p != ',' && *p != ' ')
188             continue;
189
190           fprintf(out, "allow: %s\n", allowlist[i].host);
191           allows++;
192         }
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.
196        */
197
198       tarfile_end(tf);
199     }
200
201   tarfile_close(tf);
202
203   exit(MR_SUCCESS);
204 }
205
206 void sqlerr(void)
207 {
208   db_error(sqlca.sqlcode);
209 }
This page took 0.051624 seconds and 5 git commands to generate.