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