Home | History | Annotate | Line # | Download | only in common
dumptab.c revision 1.1.1.1
      1 /*
      2  * dumptab.c - handles dumping the database
      3  */
      4 
      5 #include <sys/types.h>
      6 #include <netinet/in.h>
      7 #include <arpa/inet.h>			/* inet_ntoa */
      8 
      9 #include <stdio.h>
     10 #include <stdlib.h>
     11 #include <syslog.h>
     12 #include <time.h>
     13 
     14 #ifndef USE_BFUNCS
     15 #include <memory.h>
     16 /* Yes, memcpy is OK here (no overlapped copies). */
     17 #define bcopy(a,b,c)    memcpy(b,a,c)
     18 #define bzero(p,l)      memset(p,0,l)
     19 #define bcmp(a,b,c)     memcmp(a,b,c)
     20 #endif
     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 #ifdef	__STDC__
     30 #define P(args) args
     31 #else
     32 #define P(args) ()
     33 #endif
     34 
     35 static void dump_generic P((FILE *, struct shared_bindata *));
     36 static void dump_host P((FILE *, struct host *));
     37 static void list_ipaddresses P((FILE *, struct in_addr_list *));
     38 
     39 #undef P
     40 
     41 #ifndef	DEBUG
     42 void
     43 dumptab(filename)
     44 	char *filename;
     45 {
     46 	report(LOG_INFO, "No dumptab support!");
     47 }
     48 
     49 #else /* DEBUG */
     50 
     51 /*
     52  * Dump the internal memory database to bootpd_dump.
     53  */
     54 
     55 void
     56 dumptab(filename)
     57 	char *filename;
     58 {
     59 	int n;
     60 	struct host *hp;
     61 	FILE *fp;
     62 	long t;
     63 	/* Print symbols in alphabetical order for reader's convenience. */
     64 	static char legend[] = "#\n# Legend:\t(see bootptab.5)\n\
     65 #\tfirst field -- hostname (not indented)\n\
     66 #\tbf -- bootfile\n\
     67 #\tbs -- bootfile size in 512-octet blocks\n\
     68 #\tcs -- cookie servers\n\
     69 #\tdf -- dump file name\n\
     70 #\tdn -- domain name\n\
     71 #\tds -- domain name servers\n\
     72 #\tef -- extension file\n\
     73 #\tex -- exec file (YORK_EX_OPTION)\n\
     74 #\tgw -- gateways\n\
     75 #\tha -- hardware address\n\
     76 #\thd -- home directory for bootfiles\n\
     77 #\thn -- host name set for client\n\
     78 #\tht -- hardware type\n\
     79 #\tim -- impress servers\n\
     80 #\tip -- host IP address\n\
     81 #\tlg -- log servers\n\
     82 #\tlp -- LPR servers\n\
     83 #\tns -- IEN-116 name servers\n\
     84 #\tnt -- NTP servers (RFC 1129)\n\
     85 #\tra -- reply address override\n\
     86 #\trl -- resource location protocol servers\n\
     87 #\trp -- root path\n\
     88 #\tsa -- boot server address\n\
     89 #\tsm -- subnet mask\n\
     90 #\tsw -- swap server\n\
     91 #\ttc -- template host (points to similar host entry)\n\
     92 #\ttd -- TFTP directory\n\
     93 #\tto -- time offset (seconds)\n\
     94 #\tts -- time servers\n\
     95 #\tvm -- vendor magic number\n\
     96 #\tyd -- YP (NIS) domain\n\
     97 #\tys -- YP (NIS) servers\n\
     98 #\tTn -- generic option tag n\n\
     99 \n";
    100 
    101 	/*
    102 	 * Open bootpd.dump file.
    103 	 */
    104 	if ((fp = fopen(filename, "w")) == NULL) {
    105 		report(LOG_ERR, "error opening \"%s\": %s",
    106 			   filename, get_errmsg());
    107 		exit(1);
    108 	}
    109 	t = time(NULL);
    110 	fprintf(fp, "\n# %s %s.%d\n", progname, VERSION, PATCHLEVEL);
    111 	fprintf(fp, "# %s: dump of bootp server database.\n", filename);
    112 	fprintf(fp, "# Dump taken %s", ctime(&t));
    113 	fwrite(legend, 1, sizeof(legend) - 1, fp);
    114 
    115 	n = 0;
    116 	for (hp = (struct host *) hash_FirstEntry(nmhashtable); hp != NULL;
    117 		 hp = (struct host *) hash_NextEntry(nmhashtable)) {
    118 		dump_host(fp, hp);
    119 		fprintf(fp, "\n");
    120 		n++;
    121 	}
    122 	fclose(fp);
    123 
    124 	report(LOG_INFO, "dumped %d entries to \"%s\".", n, filename);
    125 }
    126 
    127 
    129 
    130 /*
    131  * Dump all the available information on the host pointed to by "hp".
    132  * The output is sent to the file pointed to by "fp".
    133  */
    134 
    135 static void
    136 dump_host(fp, hp)
    137 	FILE *fp;
    138 	struct host *hp;
    139 {
    140 	/* Print symbols in alphabetical order for reader's convenience. */
    141 	if (hp) {
    142 		fprintf(fp, "%s:", (hp->hostname ?
    143 							hp->hostname->string : "?"));
    144 		if (hp->flags.bootfile) {
    145 			fprintf(fp, "\\\n\t:bf=%s:", hp->bootfile->string);
    146 		}
    147 		if (hp->flags.bootsize) {
    148 			fprintf(fp, "\\\n\t:bs=");
    149 			if (hp->flags.bootsize_auto) {
    150 				fprintf(fp, "auto:");
    151 			} else {
    152 				fprintf(fp, "%d:", hp->bootsize);
    153 			}
    154 		}
    155 		if (hp->flags.cookie_server) {
    156 			fprintf(fp, "\\\n\t:cs=");
    157 			list_ipaddresses(fp, hp->cookie_server);
    158 			fprintf(fp, ":");
    159 		}
    160 		if (hp->flags.dump_file) {
    161 			fprintf(fp, "\\\n\t:df=%s:", hp->dump_file->string);
    162 		}
    163 		if (hp->flags.domain_name) {
    164 			fprintf(fp, "\\\n\t:dn=%s:", hp->domain_name->string);
    165 		}
    166 		if (hp->flags.domain_server) {
    167 			fprintf(fp, "\\\n\t:ds=");
    168 			list_ipaddresses(fp, hp->domain_server);
    169 			fprintf(fp, ":");
    170 		}
    171 		if (hp->flags.exten_file) {
    172 			fprintf(fp, "\\\n\t:ef=%s:", hp->exten_file->string);
    173 		}
    174 		if (hp->flags.exec_file) {
    175 			fprintf(fp, "\\\n\t:ex=%s:", hp->exec_file->string);
    176 		}
    177 		if (hp->flags.gateway) {
    178 			fprintf(fp, "\\\n\t:gw=");
    179 			list_ipaddresses(fp, hp->gateway);
    180 			fprintf(fp, ":");
    181 		}
    182 		/* FdC: swap_server (see below) */
    183 		if (hp->flags.homedir) {
    184 			fprintf(fp, "\\\n\t:hd=%s:", hp->homedir->string);
    185 		}
    186 		/* FdC: dump_file (see above) */
    187 		/* FdC: domain_name (see above) */
    188 		/* FdC: root_path (see below) */
    189 		if (hp->flags.name_switch && hp->flags.send_name) {
    190 			fprintf(fp, "\\\n\t:hn:");
    191 		}
    192 		if (hp->flags.htype) {
    193 			int hlen = haddrlength(hp->htype);
    194 			fprintf(fp, "\\\n\t:ht=%u:", (unsigned) hp->htype);
    195 			if (hp->flags.haddr) {
    196 				fprintf(fp, "ha=\"%s\":",
    197 						haddrtoa(hp->haddr, hlen));
    198 			}
    199 		}
    200 		if (hp->flags.impress_server) {
    201 			fprintf(fp, "\\\n\t:im=");
    202 			list_ipaddresses(fp, hp->impress_server);
    203 			fprintf(fp, ":");
    204 		}
    205 		/* NetBSD: swap_server (see below) */
    206 		if (hp->flags.iaddr) {
    207 			fprintf(fp, "\\\n\t:ip=%s:", inet_ntoa(hp->iaddr));
    208 		}
    209 		if (hp->flags.log_server) {
    210 			fprintf(fp, "\\\n\t:lg=");
    211 			list_ipaddresses(fp, hp->log_server);
    212 			fprintf(fp, ":");
    213 		}
    214 		if (hp->flags.lpr_server) {
    215 			fprintf(fp, "\\\n\t:lp=");
    216 			list_ipaddresses(fp, hp->lpr_server);
    217 			fprintf(fp, ":");
    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:", 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 
    299 static void
    300 dump_generic(fp, generic)
    301 	FILE *fp;
    302 	struct shared_bindata *generic;
    303 {
    304 	u_char *bp = generic->data;
    305 	u_char *ep = bp + generic->length;
    306 	u_char tag;
    307 	int len;
    308 
    309 	while (bp < ep) {
    310 		tag = *bp++;
    311 		if (tag == TAG_PAD)
    312 			continue;
    313 		if (tag == TAG_END)
    314 			return;
    315 		len = *bp++;
    316 		if (bp + len > ep) {
    317 			fprintf(fp, " #junk in generic! :");
    318 			return;
    319 		}
    320 		fprintf(fp, "\\\n\t:T%d=", tag);
    321 		while (len) {
    322 			fprintf(fp, "%02X", *bp);
    323 			bp++;
    324 			len--;
    325 			if (len)
    326 				fprintf(fp, ".");
    327 		}
    328 		fprintf(fp, ":");
    329 	}
    330 }
    331 
    332 
    334 
    335 /*
    336  * Dump an entire struct in_addr_list of IP addresses to the indicated file.
    337  *
    338  * The addresses are printed in standard ASCII "dot" notation and separated
    339  * from one another by a single space.  A single leading space is also
    340  * printed before the first adddress.
    341  *
    342  * Null lists produce no output (and no error).
    343  */
    344 
    345 static void
    346 list_ipaddresses(fp, ipptr)
    347 	FILE *fp;
    348 	struct in_addr_list *ipptr;
    349 {
    350 	unsigned count;
    351 	struct in_addr *addrptr;
    352 
    353 	if (ipptr) {
    354 		count = ipptr->addrcount;
    355 		addrptr = ipptr->addr;
    356 		while (count > 0) {
    357 			fprintf(fp, "%s", inet_ntoa(*addrptr++));
    358 			count--;
    359 			if (count)
    360 				fprintf(fp, ", ");
    361 		}
    362 	}
    363 }
    364 
    365 #endif /* DEBUG */
    366 
    367 /*
    368  * Local Variables:
    369  * tab-width: 4
    370  * c-indent-level: 4
    371  * c-argdecl-indent: 4
    372  * c-continued-statement-offset: 4
    373  * c-continued-brace-offset: -4
    374  * c-label-offset: -4
    375  * c-brace-offset: 0
    376  * End:
    377  */
    378