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