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