summaryrefslogtreecommitdiff
path: root/usr.sbin/bootpd/dumptab.c
blob: b9baccac157280fa7468b2e310ba5dccd6d6645b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
/*
 * dumptab.c - handles dumping the database
 */

#include <sys/types.h>
#include <netinet/in.h>
#include <arpa/inet.h>			/* inet_ntoa */

#include <stdio.h>
#include <stdlib.h>
#include <syslog.h>
#include <time.h>

#ifndef USE_BFUNCS
#include <memory.h>
/* Yes, memcpy is OK here (no overlapped copies). */
#define bcopy(a,b,c)    memcpy(b,a,c)
#define bzero(p,l)      memset(p,0,l)
#define bcmp(a,b,c)     memcmp(a,b,c)
#endif

#include "bootp.h"
#include "hash.h"
#include "hwaddr.h"
#include "report.h"
#include "patchlevel.h"
#include "bootpd.h"

#ifdef	__STDC__
#define P(args) args
#else
#define P(args) ()
#endif

static void dump_generic P((FILE *, struct shared_bindata *));
static void dump_host P((FILE *, struct host *));
static void list_ipaddresses P((FILE *, struct in_addr_list *));

#undef P

#ifndef	DEBUG
void
dumptab(filename)
	char *filename;
{
	report(LOG_INFO, "No dumptab support!");
}

#else /* DEBUG */

/*
 * Dump the internal memory database to bootpd_dump.
 */

void
dumptab(filename)
	char *filename;
{
	int n;
	struct host *hp;
	FILE *fp;
	time_t t;
	/* Print symbols in alphabetical order for reader's convenience. */
	static char legend[] = "#\n# Legend:\t(see bootptab.5)\n\
#\tfirst field -- hostname (not indented)\n\
#\tbf -- bootfile\n\
#\tbs -- bootfile size in 512-octet blocks\n\
#\tcs -- cookie servers\n\
#\tdf -- dump file name\n\
#\tdn -- domain name\n\
#\tds -- domain name servers\n\
#\tef -- extension file\n\
#\tex -- exec file (YORK_EX_OPTION)\n\
#\tgw -- gateways\n\
#\tha -- hardware address\n\
#\thd -- home directory for bootfiles\n\
#\thn -- host name set for client\n\
#\tht -- hardware type\n\
#\tim -- impress servers\n\
#\tip -- host IP address\n\
#\tlg -- log servers\n\
#\tlp -- LPR servers\n\
#\tms -- message size\n\
#\tmw -- min wait (secs)\n\
#\tns -- IEN-116 name servers\n\
#\tnt -- NTP servers (RFC 1129)\n\
#\tra -- reply address override\n\
#\trl -- resource location protocol servers\n\
#\trp -- root path\n\
#\tsa -- boot server address\n\
#\tsm -- subnet mask\n\
#\tsw -- swap server\n\
#\ttc -- template host (points to similar host entry)\n\
#\ttd -- TFTP directory\n\
#\tto -- time offset (seconds)\n\
#\tts -- time servers\n\
#\tvm -- vendor magic number\n\
#\tyd -- YP (NIS) domain\n\
#\tys -- YP (NIS) servers\n\
#\tTn -- generic option tag n\n\
\n";

	/*
	 * Open bootpd.dump file.
	 */
	if ((fp = fopen(filename, "w")) == NULL) {
		report(LOG_ERR, "error opening \"%s\": %s",
			   filename, get_errmsg());
		exit(1);
	}
	t = time(NULL);
	fprintf(fp, "\n# %s %s.%d\n", progname, VERSION, PATCHLEVEL);
	fprintf(fp, "# %s: dump of bootp server database.\n", filename);
	fprintf(fp, "# Dump taken %s", ctime(&t));
	fwrite(legend, 1, sizeof(legend) - 1, fp);

	n = 0;
	for (hp = (struct host *) hash_FirstEntry(nmhashtable); hp != NULL;
		 hp = (struct host *) hash_NextEntry(nmhashtable)) {
		dump_host(fp, hp);
		fprintf(fp, "\n");
		n++;
	}
	fclose(fp);

	report(LOG_INFO, "dumped %d entries to \"%s\".", n, filename);
}



/*
 * Dump all the available information on the host pointed to by "hp".
 * The output is sent to the file pointed to by "fp".
 */

static void
dump_host(fp, hp)
	FILE *fp;
	struct host *hp;
{
	/* Print symbols in alphabetical order for reader's convenience. */
	if (hp) {
		fprintf(fp, "%s:", (hp->hostname ?
							hp->hostname->string : "?"));
		if (hp->flags.bootfile) {
			fprintf(fp, "\\\n\t:bf=%s:", hp->bootfile->string);
		}
		if (hp->flags.bootsize) {
			fprintf(fp, "\\\n\t:bs=");
			if (hp->flags.bootsize_auto) {
				fprintf(fp, "auto:");
			} else {
				fprintf(fp, "%d:", hp->bootsize);
			}
		}
		if (hp->flags.cookie_server) {
			fprintf(fp, "\\\n\t:cs=");
			list_ipaddresses(fp, hp->cookie_server);
			fprintf(fp, ":");
		}
		if (hp->flags.dump_file) {
			fprintf(fp, "\\\n\t:df=%s:", hp->dump_file->string);
		}
		if (hp->flags.domain_name) {
			fprintf(fp, "\\\n\t:dn=%s:", hp->domain_name->string);
		}
		if (hp->flags.domain_server) {
			fprintf(fp, "\\\n\t:ds=");
			list_ipaddresses(fp, hp->domain_server);
			fprintf(fp, ":");
		}
		if (hp->flags.exten_file) {
			fprintf(fp, "\\\n\t:ef=%s:", hp->exten_file->string);
		}
		if (hp->flags.exec_file) {
			fprintf(fp, "\\\n\t:ex=%s:", hp->exec_file->string);
		}
		if (hp->flags.gateway) {
			fprintf(fp, "\\\n\t:gw=");
			list_ipaddresses(fp, hp->gateway);
			fprintf(fp, ":");
		}
		/* FdC: swap_server (see below) */
		if (hp->flags.homedir) {
			fprintf(fp, "\\\n\t:hd=%s:", hp->homedir->string);
		}
		/* FdC: dump_file (see above) */
		/* FdC: domain_name (see above) */
		/* FdC: root_path (see below) */
		if (hp->flags.name_switch && hp->flags.send_name) {
			fprintf(fp, "\\\n\t:hn:");
		}
		if (hp->flags.htype) {
			int hlen = haddrlength(hp->htype);
			fprintf(fp, "\\\n\t:ht=%u:", (unsigned) hp->htype);
			if (hp->flags.haddr) {
				fprintf(fp, "ha=\"%s\":",
						haddrtoa(hp->haddr, hlen));
			}
		}
		if (hp->flags.impress_server) {
			fprintf(fp, "\\\n\t:im=");
			list_ipaddresses(fp, hp->impress_server);
			fprintf(fp, ":");
		}
		/* NetBSD: swap_server (see below) */
		if (hp->flags.iaddr) {
			fprintf(fp, "\\\n\t:ip=%s:", inet_ntoa(hp->iaddr));
		}
		if (hp->flags.log_server) {
			fprintf(fp, "\\\n\t:lg=");
			list_ipaddresses(fp, hp->log_server);
			fprintf(fp, ":");
		}
		if (hp->flags.lpr_server) {
			fprintf(fp, "\\\n\t:lp=");
			list_ipaddresses(fp, hp->lpr_server);
			fprintf(fp, ":");
		}
		if (hp->flags.msg_size) {
			fprintf(fp, "\\\n\t:ms=%d:", hp->msg_size);
		}
		if (hp->flags.min_wait) {
			fprintf(fp, "\\\n\t:mw=%d:", hp->min_wait);
		}
		if (hp->flags.name_server) {
			fprintf(fp, "\\\n\t:ns=");
			list_ipaddresses(fp, hp->name_server);
			fprintf(fp, ":");
		}
		if (hp->flags.ntp_server) {
			fprintf(fp, "\\\n\t:nt=");
			list_ipaddresses(fp, hp->ntp_server);
			fprintf(fp, ":");
		}
		if (hp->flags.reply_addr) {
			fprintf(fp, "\\\n\t:ra=%s:", inet_ntoa(hp->reply_addr));
		}
		if (hp->flags.rlp_server) {
			fprintf(fp, "\\\n\t:rl=");
			list_ipaddresses(fp, hp->rlp_server);
			fprintf(fp, ":");
		}
		if (hp->flags.root_path) {
			fprintf(fp, "\\\n\t:rp=%s:", hp->root_path->string);
		}
		if (hp->flags.bootserver) {
			fprintf(fp, "\\\n\t:sa=%s:", inet_ntoa(hp->bootserver));
		}
		if (hp->flags.subnet_mask) {
			fprintf(fp, "\\\n\t:sm=%s:", inet_ntoa(hp->subnet_mask));
		}
		if (hp->flags.swap_server) {
			fprintf(fp, "\\\n\t:sw=%s:", inet_ntoa(hp->subnet_mask));
		}
		if (hp->flags.tftpdir) {
			fprintf(fp, "\\\n\t:td=%s:", hp->tftpdir->string);
		}
		/* NetBSD: rootpath (see above) */
		/* NetBSD: domainname (see above) */
		/* NetBSD: dumpfile (see above) */
		if (hp->flags.time_offset) {
			fprintf(fp, "\\\n\t:to=%ld:", hp->time_offset);
		}
		if (hp->flags.time_server) {
			fprintf(fp, "\\\n\t:ts=");
			list_ipaddresses(fp, hp->time_server);
			fprintf(fp, ":");
		}
		if (hp->flags.vm_cookie) {
			fprintf(fp, "\\\n\t:vm=");
			if (!bcmp(hp->vm_cookie, vm_rfc1048, 4)) {
				fprintf(fp, "rfc1048:");
			} else if (!bcmp(hp->vm_cookie, vm_cmu, 4)) {
				fprintf(fp, "cmu:");
			} else {
				fprintf(fp, "%d.%d.%d.%d:",
						(int) ((hp->vm_cookie)[0]),
						(int) ((hp->vm_cookie)[1]),
						(int) ((hp->vm_cookie)[2]),
						(int) ((hp->vm_cookie)[3]));
			}
		}
		if (hp->flags.nis_domain) {
			fprintf(fp, "\\\n\t:yd=%s:",
					hp->nis_domain->string);
		}
		if (hp->flags.nis_server) {
			fprintf(fp, "\\\n\t:ys=");
			list_ipaddresses(fp, hp->nis_server);
			fprintf(fp, ":");
		}
		/*
		 * XXX - Add new tags here (or above,
		 * so they print in alphabetical order).
		 */

		if (hp->flags.generic) {
			dump_generic(fp, hp->generic);
		}
	}
}


static void
dump_generic(fp, generic)
	FILE *fp;
	struct shared_bindata *generic;
{
	u_char *bp = generic->data;
	u_char *ep = bp + generic->length;
	u_char tag;
	int len;

	while (bp < ep) {
		tag = *bp++;
		if (tag == TAG_PAD)
			continue;
		if (tag == TAG_END)
			return;
		len = *bp++;
		if (bp + len > ep) {
			fprintf(fp, " #junk in generic! :");
			return;
		}
		fprintf(fp, "\\\n\t:T%d=", tag);
		while (len) {
			fprintf(fp, "%02X", *bp);
			bp++;
			len--;
			if (len)
				fprintf(fp, ".");
		}
		fprintf(fp, ":");
	}
}



/*
 * Dump an entire struct in_addr_list of IP addresses to the indicated file.
 *
 * The addresses are printed in standard ASCII "dot" notation and separated
 * from one another by a single space.  A single leading space is also
 * printed before the first adddress.
 *
 * Null lists produce no output (and no error).
 */

static void
list_ipaddresses(fp, ipptr)
	FILE *fp;
	struct in_addr_list *ipptr;
{
	unsigned int count;
	struct in_addr *addrptr;

	if (ipptr) {
		count = ipptr->addrcount;
		addrptr = ipptr->addr;
		while (count > 0) {
			fprintf(fp, "%s", inet_ntoa(*addrptr++));
			count--;
			if (count)
				fprintf(fp, ", ");
		}
	}
}

#endif /* DEBUG */

/*
 * Local Variables:
 * tab-width: 4
 * c-indent-level: 4
 * c-argdecl-indent: 4
 * c-continued-statement-offset: 4
 * c-continued-brace-offset: -4
 * c-label-offset: -4
 * c-brace-offset: 0
 * End:
 */