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