source: trunk/third/moira/gen/dhcp.pc @ 25455

Revision 25455, 7.5 KB checked in by jdreed, 12 years ago (diff)
In moira: * Re-snapshot moira at r4073 to pick up new changes to clients; the eunice issue described in the previous entry is no longer relevant
Line 
1/* $Id: dhcp.pc 4051 2011-09-08 18:46:37Z zacheiss $
2 *
3 * This generates the dhcpd.conf.print 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#define HWADDRMAP_CHWADDR_SIZE 18
24
25EXEC SQL INCLUDE sqlca;
26
27RCSID("$HeadURL: svn+ssh://svn.mit.edu/moira/trunk/moira/gen/dhcp.pc $ $Id: dhcp.pc 4051 2011-09-08 18:46:37Z zacheiss $");
28
29char *whoami = "dhcp.gen";
30char *db = "moira/moira";
31
32void hwcolonify(char *from, char *to);
33
34void sqlerr(void);
35
36int main(int argc, char **argv)
37{
38  EXEC SQL BEGIN DECLARE SECTION;
39  char name[MACHINE_NAME_SIZE];
40  char hwaddr[HWADDRMAP_HWADDR_SIZE], chwaddr[HWADDRMAP_CHWADDR_SIZE];
41  char ohwaddr[HWADDRMAP_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, status;
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  if (argc != 2)
66    {
67      fprintf(stderr, "usage: %s [outfile]\n", argv[0]);
68      exit(MR_ARGS);
69    }
70
71  /* Get print spoolers for allow lists. */
72  EXEC SQL SELECT COUNT(service) INTO :alcount FROM serverhosts sh, machine m
73    WHERE (sh.service = 'PRINT' OR sh.service = 'CUPS-PRINT')
74    AND sh.mach_id = m.mach_id
75    AND (m.status = 1 OR m.status = 2) AND m.address != 'unassigned';
76  allowlist = malloc(alcount * sizeof(*allowlist));
77
78  EXEC SQL DECLARE csr_spool CURSOR FOR
79    SELECT m.address, sh.value3 FROM machine m, serverhosts sh
80    WHERE m.mach_id = sh.mach_id AND
81    (sh.service = 'PRINT' OR sh.service = 'CUPS-PRINT')
82    AND (m.status = 1 OR m.status = 2) AND m.address != 'unassigned';
83  EXEC SQL OPEN csr_spool;
84  for (i = 0; i < alcount; i++)
85    {
86      EXEC SQL FETCH csr_spool INTO :host, :types;
87      if (sqlca.sqlcode)
88        sqlerr();
89      strcpy(allowlist[i].host, strtrim(host));
90      strcpy(allowlist[i].types, strtrim(types));
91    }
92  EXEC SQL CLOSE csr_spool;
93
94  /* Now build the tar file. */
95  tf = tarfile_open(argv[1]);
96
97  /* Build dhcpd.conf.print */
98
99  out = tarfile_start(tf, "/var/boot/dhcpd.conf.print", 0755, 0, 0,
100                      "root", "root", now);
101  ohwaddr[0] = '\0';
102  EXEC SQL DECLARE csr_boot CURSOR FOR
103    SELECT LOWER(m.name), hw.hwaddr, m.address, m2.address,
104    pr.location, pr.contact, pr.hwtype
105    FROM printers pr, machine m, machine m2, hwaddrmap hw
106    WHERE pr.type != 'ALIAS' AND pr.mach_id != 0
107    AND pr.mach_id = m.mach_id AND m.mach_id = hw.mach_id AND m.status != 3
108    AND pr.loghost = m2.mach_id AND (pr.status = 1 OR pr.status = 2 OR pr.status = 4)
109    ORDER BY hw.hwaddr, m.name;
110  EXEC SQL OPEN csr_boot;
111  while (1)
112    {
113      EXEC SQL FETCH csr_boot INTO :name, :hwaddr, :addr, :logaddr,
114        :location, :contact, :hwtype;
115      if (sqlca.sqlcode)
116        break;
117
118      strtrim(hwaddr);
119      if (!*hwaddr || !strcasecmp(hwaddr, "unknown"))
120        continue;
121      if (!strcmp(hwaddr, ohwaddr))
122        {
123          fprintf(stderr, "Ignoring duplicate hwaddr %s\n", hwaddr);
124          continue;
125        }
126      strcpy(ohwaddr, hwaddr);
127
128      hwcolonify(hwaddr, chwaddr);
129
130      strtrim(name);
131      strtrim(addr);
132      strtrim(logaddr);
133      strtrim(location);
134      strtrim(contact);
135      strtrim(hwtype);
136      strcpy(shortname, name);
137      if ((p = strchr(shortname, '.')))
138        *p = '\0';
139
140      if ((p = strchr(addr, '.')) && (q = strchr(++p, '.')))
141        {
142          strncpy(net, p, q - p);
143          net[q - p] = '\0';
144        }
145      else
146        continue;
147
148      fprintf(out, "# %s: %s\n# contact: %s\nhost %s {\n\t"
149              "hardware ethernet %s;\n\tfixed-address %s;\n",
150              shortname, location, contact, hwaddr, chwaddr, addr);
151     
152      if (strlen(logaddr) != 0)
153              fprintf(out, "\toption log-servers %s;\n", logaddr);
154             
155      if (!strncmp(hwtype, "HP", 2))
156        fprintf(out, "\toption option-144 \"/hp/%s\";\n", shortname);
157      fprintf(out, "}\n\n");
158    }
159  EXEC SQL CLOSE csr_boot;
160  tarfile_end(tf);
161
162  /* Now generate /var/boot/hp/ files */
163  EXEC SQL DECLARE csr_boot2 CURSOR FOR
164    SELECT LOWER(m.name), hw.hwaddr, m2.address, m3.address, pr.type,
165    pr.location, pr.contact, TO_CHAR(pr.modtime, :unixtime_fmt), pr.status
166    FROM printers pr, machine m, machine m2, machine m3, hwaddrmap hw
167    WHERE pr.hwtype LIKE 'HP%' AND pr.mach_id != 0
168    AND pr.mach_id = m.mach_id AND m.mach_id = hw.mach_id AND m.status != 3
169    AND pr.rm = m2.mach_id AND pr.loghost = m3.mach_id AND pr.type != 'ALIAS'
170    AND (pr.status = 1 OR pr.status = 2 OR pr.status = 4);
171  EXEC SQL OPEN csr_boot2;
172  while (1)
173    {
174      EXEC SQL FETCH csr_boot2 INTO :name, :hwaddr, :addr, :logaddr,
175        :type, :location, :contact, :modtime, :status;
176      if (sqlca.sqlcode)
177        break;
178
179      strtrim(hwaddr);
180      if (!*hwaddr || !strcasecmp(hwaddr, "unknown"))
181        continue;
182
183      strtrim(name);
184      strtrim(addr);
185      strtrim(logaddr);
186      strtrim(type);
187      strtrim(location);
188      strtrim(contact);
189      strcpy(shortname, name);
190      if ((p = strchr(shortname, '.')))
191        *p = '\0';
192
193      /* We create it as foo.new so dhcp.sh can append the passwords
194       * and other data and rename it.
195       */
196      sprintf(filename, "/var/boot/hp/%s.new", shortname);
197      out = tarfile_start(tf, filename, 0755, 0, 0, "root", "root",
198                          unixtime(modtime));
199
200      fprintf(out, "name: %s\nlocation: %s\ncontact: %s\n\n", shortname,
201              *location ? location : "unknown",
202              *contact ? contact : "unknown");
203
204      if (*logaddr)
205        fprintf(out, "trap-dest: %s\n", logaddr);
206
207      /* Don't output any allow: lines for status 2 printers; they should be unrestricted. */
208      if (status == 1 || status == 4)
209        {
210          if (*logaddr)
211            fprintf(out, "allow: %s\n", logaddr);
212          fprintf(out, "allow: %s\n", addr);
213          typelen = strlen(type);
214          for (i = allows = 0; i < alcount && allows < 9; i++)
215            {
216              char *p;
217             
218              /* Don't list the spoolhost twice. */
219              if (!strcmp(allowlist[i].host, addr))
220                continue;
221             
222              p = strstr(allowlist[i].types, type);
223              if (!p)
224                continue;
225             
226              /* Make sure the match was real, and not just because one type
227               * is a substring of another type.
228               */
229              if (p != allowlist[i].types && *(p - 1) != ',' && *(p - 1) != ' ')
230                continue;
231              p += typelen;
232              if (*p && *p != ',' && *p != ' ')
233                continue;
234             
235              fprintf(out, "allow: %s\n", allowlist[i].host);
236              allows++;
237            }
238        }
239         
240      /* Rest of data is same for all printers and is appended from a
241       * a file on the boot server.
242       */
243
244      tarfile_end(tf);
245    }
246
247  tarfile_close(tf);
248
249  exit(MR_SUCCESS);
250}
251
252void hwcolonify(char *from, char *to)
253{
254  int f = 0, t = 0;
255  int mod = 2;
256
257  for (f = 0 ; f < HWADDRMAP_HWADDR_SIZE - 1 ; )
258    {
259      to[t++] = from[f++];
260      if (f % mod == 0)
261        to[t++] = ':';
262    }
263       
264  if (f % mod == 0)
265    t--;
266  to[t] = '\0';
267}
268
269void sqlerr(void)
270{
271  db_error(sqlca.sqlcode);
272}
Note: See TracBrowser for help on using the repository browser.