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