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