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