dumptab.c revision 1.6 1 /* $NetBSD: dumptab.c,v 1.6 2002/07/14 00:26:17 wiz Exp $ */
2
3 #include <sys/cdefs.h>
4 #ifndef lint
5 __RCSID("$NetBSD: dumptab.c,v 1.6 2002/07/14 00:26:17 wiz Exp $");
6 #endif
7
8 /*
9 * dumptab.c - handles dumping the database
10 */
11
12 #include <sys/types.h>
13 #include <netinet/in.h>
14 #include <arpa/inet.h> /* inet_ntoa */
15
16 #include <stdio.h>
17 #include <stdlib.h>
18 #include <syslog.h>
19 #include <time.h>
20
21 #ifndef USE_BFUNCS
22 #include <memory.h>
23 /* Yes, memcpy is OK here (no overlapped copies). */
24 #define bcopy(a,b,c) memcpy(b,a,c)
25 #define bzero(p,l) memset(p,0,l)
26 #define bcmp(a,b,c) memcmp(a,b,c)
27 #endif
28
29 #include "bootp.h"
30 #include "hash.h"
31 #include "hwaddr.h"
32 #include "report.h"
33 #include "patchlevel.h"
34 #include "bootpd.h"
35
36 static void dump_generic(FILE *, struct shared_bindata *);
37 static void dump_host(FILE *, struct host *);
38 static void list_ipaddresses(FILE *, struct in_addr_list *);
39 void dumptab(char *);
40
41 #ifndef DEBUG
42 void
43 dumptab(char *filename)
44 {
45 report(LOG_INFO, "No dumptab support!");
46 }
47
48 #else /* DEBUG */
49
50 /*
51 * Dump the internal memory database to bootpd_dump.
52 */
53
54 void
55 dumptab(char *filename)
56 {
57 int n;
58 struct host *hp;
59 FILE *fp;
60 time_t t;
61 /* Print symbols in alphabetical order for reader's convenience. */
62 static char legend[] = "#\n# Legend:\t(see bootptab.5)\n\
63 #\tfirst field -- hostname (not indented)\n\
64 #\tbf -- bootfile\n\
65 #\tbs -- bootfile size in 512-octet blocks\n\
66 #\tcs -- cookie servers\n\
67 #\tdf -- dump file name\n\
68 #\tdn -- domain name\n\
69 #\tds -- domain name servers\n\
70 #\tef -- extension file\n\
71 #\tex -- exec file (YORK_EX_OPTION)\n\
72 #\tgw -- gateways\n\
73 #\tha -- hardware address\n\
74 #\thd -- home directory for bootfiles\n\
75 #\thn -- host name set for client\n\
76 #\tht -- hardware type\n\
77 #\tim -- impress servers\n\
78 #\tip -- host IP address\n\
79 #\tlg -- log servers\n\
80 #\tlp -- LPR servers\n\
81 #\tms -- message size\n\
82 #\tmw -- min wait (secs)\n\
83 #\tns -- IEN-116 name servers\n\
84 #\tnt -- NTP servers (RFC 1129)\n\
85 #\tra -- reply address override\n\
86 #\trl -- resource location protocol servers\n\
87 #\trp -- root path\n\
88 #\tsa -- boot server address\n\
89 #\tsm -- subnet mask\n\
90 #\tsw -- swap server\n\
91 #\ttc -- template host (points to similar host entry)\n\
92 #\ttd -- TFTP directory\n\
93 #\tto -- time offset (seconds)\n\
94 #\tts -- time servers\n\
95 #\tvm -- vendor magic number\n\
96 #\tyd -- YP (NIS) domain\n\
97 #\tys -- YP (NIS) servers\n\
98 #\tTn -- generic option tag n\n\
99 \n";
100
101 /*
102 * Open bootpd.dump file.
103 */
104 if ((fp = fopen(filename, "w")) == NULL) {
105 report(LOG_ERR, "error opening \"%s\": %s",
106 filename, get_errmsg());
107 exit(1);
108 }
109 t = time(NULL);
110 fprintf(fp, "\n# %s %s.%d\n", progname, VERSION, PATCHLEVEL);
111 fprintf(fp, "# %s: dump of bootp server database.\n", filename);
112 fprintf(fp, "# Dump taken %s", ctime(&t));
113 fwrite(legend, 1, sizeof(legend) - 1, fp);
114
115 n = 0;
116 for (hp = (struct host *) hash_FirstEntry(nmhashtable); hp != NULL;
117 hp = (struct host *) hash_NextEntry(nmhashtable)) {
118 dump_host(fp, hp);
119 fprintf(fp, "\n");
120 n++;
121 }
122 fclose(fp);
123
124 report(LOG_INFO, "dumped %d entries to \"%s\".", n, filename);
125 }
126
127
129
130 /*
131 * Dump all the available information on the host pointed to by "hp".
132 * The output is sent to the file pointed to by "fp".
133 */
134
135 static void
136 dump_host(FILE *fp, struct host *hp)
137 {
138 /* Print symbols in alphabetical order for reader's convenience. */
139 if (hp) {
140 fprintf(fp, "%s:", (hp->hostname ?
141 hp->hostname->string : "?"));
142 if (hp->flags.bootfile) {
143 fprintf(fp, "\\\n\t:bf=%s:", hp->bootfile->string);
144 }
145 if (hp->flags.bootsize) {
146 fprintf(fp, "\\\n\t:bs=");
147 if (hp->flags.bootsize_auto) {
148 fprintf(fp, "auto:");
149 } else {
150 fprintf(fp, "%d:", hp->bootsize);
151 }
152 }
153 if (hp->flags.cookie_server) {
154 fprintf(fp, "\\\n\t:cs=");
155 list_ipaddresses(fp, hp->cookie_server);
156 fprintf(fp, ":");
157 }
158 if (hp->flags.dump_file) {
159 fprintf(fp, "\\\n\t:df=%s:", hp->dump_file->string);
160 }
161 if (hp->flags.domain_name) {
162 fprintf(fp, "\\\n\t:dn=%s:", hp->domain_name->string);
163 }
164 if (hp->flags.domain_server) {
165 fprintf(fp, "\\\n\t:ds=");
166 list_ipaddresses(fp, hp->domain_server);
167 fprintf(fp, ":");
168 }
169 if (hp->flags.exten_file) {
170 fprintf(fp, "\\\n\t:ef=%s:", hp->exten_file->string);
171 }
172 if (hp->flags.exec_file) {
173 fprintf(fp, "\\\n\t:ex=%s:", hp->exec_file->string);
174 }
175 if (hp->flags.gateway) {
176 fprintf(fp, "\\\n\t:gw=");
177 list_ipaddresses(fp, hp->gateway);
178 fprintf(fp, ":");
179 }
180 /* FdC: swap_server (see below) */
181 if (hp->flags.homedir) {
182 fprintf(fp, "\\\n\t:hd=%s:", hp->homedir->string);
183 }
184 /* FdC: dump_file (see above) */
185 /* FdC: domain_name (see above) */
186 /* FdC: root_path (see below) */
187 if (hp->flags.name_switch && hp->flags.send_name) {
188 fprintf(fp, "\\\n\t:hn:");
189 }
190 if (hp->flags.htype) {
191 int hlen = haddrlength(hp->htype);
192 fprintf(fp, "\\\n\t:ht=%u:", (unsigned) hp->htype);
193 if (hp->flags.haddr) {
194 fprintf(fp, "ha=\"%s\":",
195 haddrtoa(hp->haddr, hlen));
196 }
197 }
198 if (hp->flags.impress_server) {
199 fprintf(fp, "\\\n\t:im=");
200 list_ipaddresses(fp, hp->impress_server);
201 fprintf(fp, ":");
202 }
203 /* NetBSD: swap_server (see below) */
204 if (hp->flags.iaddr) {
205 fprintf(fp, "\\\n\t:ip=%s:", inet_ntoa(hp->iaddr));
206 }
207 if (hp->flags.log_server) {
208 fprintf(fp, "\\\n\t:lg=");
209 list_ipaddresses(fp, hp->log_server);
210 fprintf(fp, ":");
211 }
212 if (hp->flags.lpr_server) {
213 fprintf(fp, "\\\n\t:lp=");
214 list_ipaddresses(fp, hp->lpr_server);
215 fprintf(fp, ":");
216 }
217 if (hp->flags.msg_size) {
218 fprintf(fp, "\\\n\t:ms=%d:", hp->msg_size);
219 }
220 if (hp->flags.min_wait) {
221 fprintf(fp, "\\\n\t:mw=%d:", hp->min_wait);
222 }
223 if (hp->flags.name_server) {
224 fprintf(fp, "\\\n\t:ns=");
225 list_ipaddresses(fp, hp->name_server);
226 fprintf(fp, ":");
227 }
228 if (hp->flags.ntp_server) {
229 fprintf(fp, "\\\n\t:nt=");
230 list_ipaddresses(fp, hp->ntp_server);
231 fprintf(fp, ":");
232 }
233 if (hp->flags.reply_addr) {
234 fprintf(fp, "\\\n\t:ra=%s:", inet_ntoa(hp->reply_addr));
235 }
236 if (hp->flags.rlp_server) {
237 fprintf(fp, "\\\n\t:rl=");
238 list_ipaddresses(fp, hp->rlp_server);
239 fprintf(fp, ":");
240 }
241 if (hp->flags.root_path) {
242 fprintf(fp, "\\\n\t:rp=%s:", hp->root_path->string);
243 }
244 if (hp->flags.bootserver) {
245 fprintf(fp, "\\\n\t:sa=%s:", inet_ntoa(hp->bootserver));
246 }
247 if (hp->flags.subnet_mask) {
248 fprintf(fp, "\\\n\t:sm=%s:", inet_ntoa(hp->subnet_mask));
249 }
250 if (hp->flags.swap_server) {
251 fprintf(fp, "\\\n\t:sw=%s:", inet_ntoa(hp->subnet_mask));
252 }
253 if (hp->flags.tftpdir) {
254 fprintf(fp, "\\\n\t:td=%s:", hp->tftpdir->string);
255 }
256 /* NetBSD: rootpath (see above) */
257 /* NetBSD: domainname (see above) */
258 /* NetBSD: dumpfile (see above) */
259 if (hp->flags.time_offset) {
260 fprintf(fp, "\\\n\t:to=%ld:", (long)hp->time_offset);
261 }
262 if (hp->flags.time_server) {
263 fprintf(fp, "\\\n\t:ts=");
264 list_ipaddresses(fp, hp->time_server);
265 fprintf(fp, ":");
266 }
267 if (hp->flags.vm_cookie) {
268 fprintf(fp, "\\\n\t:vm=");
269 if (!bcmp(hp->vm_cookie, vm_rfc1048, 4)) {
270 fprintf(fp, "rfc1048:");
271 } else if (!bcmp(hp->vm_cookie, vm_cmu, 4)) {
272 fprintf(fp, "cmu:");
273 } else {
274 fprintf(fp, "%d.%d.%d.%d:",
275 (int) ((hp->vm_cookie)[0]),
276 (int) ((hp->vm_cookie)[1]),
277 (int) ((hp->vm_cookie)[2]),
278 (int) ((hp->vm_cookie)[3]));
279 }
280 }
281 if (hp->flags.nis_domain) {
282 fprintf(fp, "\\\n\t:yd=%s:",
283 hp->nis_domain->string);
284 }
285 if (hp->flags.nis_server) {
286 fprintf(fp, "\\\n\t:ys=");
287 list_ipaddresses(fp, hp->nis_server);
288 fprintf(fp, ":");
289 }
290 /*
291 * XXX - Add new tags here (or above,
292 * so they print in alphabetical order).
293 */
294
295 if (hp->flags.generic) {
296 dump_generic(fp, hp->generic);
297 }
298 }
299 }
300
301
303 static void
304 dump_generic(FILE *fp, struct shared_bindata *generic)
305 {
306 u_char *bp = generic->data;
307 u_char *ep = bp + generic->length;
308 u_char tag;
309 int len;
310
311 while (bp < ep) {
312 tag = *bp++;
313 if (tag == TAG_PAD)
314 continue;
315 if (tag == TAG_END)
316 return;
317 len = *bp++;
318 if (bp + len > ep) {
319 fprintf(fp, " #junk in generic! :");
320 return;
321 }
322 fprintf(fp, "\\\n\t:T%d=", tag);
323 while (len) {
324 fprintf(fp, "%02X", *bp);
325 bp++;
326 len--;
327 if (len)
328 fprintf(fp, ".");
329 }
330 fprintf(fp, ":");
331 }
332 }
333
334
336
337 /*
338 * Dump an entire struct in_addr_list of IP addresses to the indicated file.
339 *
340 * The addresses are printed in standard ASCII "dot" notation and separated
341 * from one another by a single space. A single leading space is also
342 * printed before the first adddress.
343 *
344 * Null lists produce no output (and no error).
345 */
346
347 static void
348 list_ipaddresses(FILE *fp, struct in_addr_list *ipptr)
349 {
350 unsigned count;
351 struct in_addr *addrptr;
352
353 if (ipptr) {
354 count = ipptr->addrcount;
355 addrptr = ipptr->addr;
356 while (count > 0) {
357 fprintf(fp, "%s", inet_ntoa(*addrptr++));
358 count--;
359 if (count)
360 fprintf(fp, ", ");
361 }
362 }
363 }
364
365 #endif /* DEBUG */
366
367 /*
368 * Local Variables:
369 * tab-width: 4
370 * c-indent-level: 4
371 * c-argdecl-indent: 4
372 * c-continued-statement-offset: 4
373 * c-continued-brace-offset: -4
374 * c-label-offset: -4
375 * c-brace-offset: 0
376 * End:
377 */
378