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