xref: /dragonfly/libexec/bootpd/dumptab.c (revision b40e316c)
1 /*
2  * dumptab.c - handles dumping the database
3  *
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  */
7 
8 #include <sys/types.h>
9 #include <netinet/in.h>
10 #include <arpa/inet.h>			/* inet_ntoa */
11 
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <syslog.h>
15 #include <time.h>
16 
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
24 
25 #include "bootp.h"
26 #include "hash.h"
27 #include "hwaddr.h"
28 #include "report.h"
29 #include "patchlevel.h"
30 #include "bootpd.h"
31 
32 #ifdef	__STDC__
33 #define P(args) args
34 #else
35 #define P(args) ()
36 #endif
37 
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
43 
44 #undef P
45 
46 #ifndef	DEBUG
47 void
48 dumptab(filename)
49 	char *filename;
50 {
51 	report(LOG_INFO, "No dumptab support!");
52 }
53 
54 #else /* DEBUG */
55 
56 /*
57  * Dump the internal memory database to bootpd_dump.
58  */
59 
60 void
61 dumptab(filename)
62 	char *filename;
63 {
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";
107 
108 	/*
109 	 * Open bootpd.dump file.
110 	 */
111 	if ((fp = fopen(filename, "w")) == NULL) {
112 		report(LOG_ERR, "error opening \"%s\": %s",
113 			   filename, get_errmsg());
114 		exit(1);
115 	}
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);
121 
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++;
128 	}
129 	fclose(fp);
130 
131 	report(LOG_INFO, "dumped %d entries to \"%s\".", n, filename);
132 }
133 
134 
135 
136 /*
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".
139  */
140 
141 static void
142 dump_host(fp, hp)
143 	FILE *fp;
144 	struct host *hp;
145 {
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);
152 		}
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);
159 			}
160 		}
161 		if (hp->flags.cookie_server) {
162 			fprintf(fp, "\\\n\t:cs=");
163 			list_ipaddresses(fp, hp->cookie_server);
164 			fprintf(fp, ":");
165 		}
166 		if (hp->flags.dump_file) {
167 			fprintf(fp, "\\\n\t:df=%s:", hp->dump_file->string);
168 		}
169 		if (hp->flags.domain_name) {
170 			fprintf(fp, "\\\n\t:dn=%s:", hp->domain_name->string);
171 		}
172 		if (hp->flags.domain_server) {
173 			fprintf(fp, "\\\n\t:ds=");
174 			list_ipaddresses(fp, hp->domain_server);
175 			fprintf(fp, ":");
176 		}
177 		if (hp->flags.exten_file) {
178 			fprintf(fp, "\\\n\t:ef=%s:", hp->exten_file->string);
179 		}
180 		if (hp->flags.exec_file) {
181 			fprintf(fp, "\\\n\t:ex=%s:", hp->exec_file->string);
182 		}
183 		if (hp->flags.gateway) {
184 			fprintf(fp, "\\\n\t:gw=");
185 			list_ipaddresses(fp, hp->gateway);
186 			fprintf(fp, ":");
187 		}
188 		/* FdC: swap_server (see below) */
189 		if (hp->flags.homedir) {
190 			fprintf(fp, "\\\n\t:hd=%s:", hp->homedir->string);
191 		}
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:");
197 		}
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));
204 			}
205 		}
206 		if (hp->flags.impress_server) {
207 			fprintf(fp, "\\\n\t:im=");
208 			list_ipaddresses(fp, hp->impress_server);
209 			fprintf(fp, ":");
210 		}
211 		/* NetBSD: swap_server (see below) */
212 		if (hp->flags.iaddr) {
213 			fprintf(fp, "\\\n\t:ip=%s:", inet_ntoa(hp->iaddr));
214 		}
215 		if (hp->flags.log_server) {
216 			fprintf(fp, "\\\n\t:lg=");
217 			list_ipaddresses(fp, hp->log_server);
218 			fprintf(fp, ":");
219 		}
220 		if (hp->flags.lpr_server) {
221 			fprintf(fp, "\\\n\t:lp=");
222 			list_ipaddresses(fp, hp->lpr_server);
223 			fprintf(fp, ":");
224 		}
225 		if (hp->flags.msg_size) {
226 			fprintf(fp, "\\\n\t:ms=%lu:", (u_long)hp->msg_size);
227 		}
228 		if (hp->flags.min_wait) {
229 			fprintf(fp, "\\\n\t:mw=%lu:", (u_long)hp->min_wait);
230 		}
231 		if (hp->flags.name_server) {
232 			fprintf(fp, "\\\n\t:ns=");
233 			list_ipaddresses(fp, hp->name_server);
234 			fprintf(fp, ":");
235 		}
236 		if (hp->flags.ntp_server) {
237 			fprintf(fp, "\\\n\t:nt=");
238 			list_ipaddresses(fp, hp->ntp_server);
239 			fprintf(fp, ":");
240 		}
241 		if (hp->flags.reply_addr) {
242 			fprintf(fp, "\\\n\t:ra=%s:", inet_ntoa(hp->reply_addr));
243 		}
244 		if (hp->flags.rlp_server) {
245 			fprintf(fp, "\\\n\t:rl=");
246 			list_ipaddresses(fp, hp->rlp_server);
247 			fprintf(fp, ":");
248 		}
249 		if (hp->flags.root_path) {
250 			fprintf(fp, "\\\n\t:rp=%s:", hp->root_path->string);
251 		}
252 		if (hp->flags.bootserver) {
253 			fprintf(fp, "\\\n\t:sa=%s:", inet_ntoa(hp->bootserver));
254 		}
255 		if (hp->flags.subnet_mask) {
256 			fprintf(fp, "\\\n\t:sm=%s:", inet_ntoa(hp->subnet_mask));
257 		}
258 		if (hp->flags.swap_server) {
259 			fprintf(fp, "\\\n\t:sw=%s:", inet_ntoa(hp->subnet_mask));
260 		}
261 		if (hp->flags.tftpdir) {
262 			fprintf(fp, "\\\n\t:td=%s:", hp->tftpdir->string);
263 		}
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);
269 		}
270 		if (hp->flags.time_server) {
271 			fprintf(fp, "\\\n\t:ts=");
272 			list_ipaddresses(fp, hp->time_server);
273 			fprintf(fp, ":");
274 		}
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]));
287 			}
288 		}
289 		if (hp->flags.nis_domain) {
290 			fprintf(fp, "\\\n\t:yd=%s:",
291 					hp->nis_domain->string);
292 		}
293 		if (hp->flags.nis_server) {
294 			fprintf(fp, "\\\n\t:ys=");
295 			list_ipaddresses(fp, hp->nis_server);
296 			fprintf(fp, ":");
297 		}
298 		/*
299 		 * XXX - Add new tags here (or above,
300 		 * so they print in alphabetical order).
301 		 */
302 
303 		if (hp->flags.generic) {
304 			dump_generic(fp, hp->generic);
305 		}
306 	}
307 }
308 
309 
310 static void
311 dump_generic(fp, generic)
312 	FILE *fp;
313 	struct shared_bindata *generic;
314 {
315 	u_char *bp = generic->data;
316 	u_char *ep = bp + generic->length;
317 	u_char tag;
318 	int len;
319 
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;
330 		}
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, ".");
338 		}
339 		fprintf(fp, ":");
340 	}
341 }
342 
343 
344 
345 /*
346  * Dump an entire struct in_addr_list of IP addresses to the indicated file.
347  *
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.
351  *
352  * Null lists produce no output (and no error).
353  */
354 
355 static void
356 list_ipaddresses(fp, ipptr)
357 	FILE *fp;
358 	struct in_addr_list *ipptr;
359 {
360 	unsigned count;
361 	struct in_addr *addrptr;
362 
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, ", ");
371 		}
372 	}
373 }
374 
375 #endif /* DEBUG */
376 
377 /*
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:
387  */
388