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