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