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 121 122 /* 123 * Dump all the available information on the host pointed to by "hp". 124 * The output is sent to the file pointed to by "fp". 125 */ 126 127 static void 128 dump_host(FILE *fp, struct host *hp) 129 { 130 /* Print symbols in alphabetical order for reader's convenience. */ 131 if (hp) { 132 fprintf(fp, "%s:", (hp->hostname ? 133 hp->hostname->string : "?")); 134 if (hp->flags.bootfile) { 135 fprintf(fp, "\\\n\t:bf=%s:", hp->bootfile->string); 136 } 137 if (hp->flags.bootsize) { 138 fprintf(fp, "\\\n\t:bs="); 139 if (hp->flags.bootsize_auto) { 140 fprintf(fp, "auto:"); 141 } else { 142 fprintf(fp, "%d:", hp->bootsize); 143 } 144 } 145 if (hp->flags.cookie_server) { 146 fprintf(fp, "\\\n\t:cs="); 147 list_ipaddresses(fp, hp->cookie_server); 148 fprintf(fp, ":"); 149 } 150 if (hp->flags.dump_file) { 151 fprintf(fp, "\\\n\t:df=%s:", hp->dump_file->string); 152 } 153 if (hp->flags.domain_name) { 154 fprintf(fp, "\\\n\t:dn=%s:", hp->domain_name->string); 155 } 156 if (hp->flags.domain_server) { 157 fprintf(fp, "\\\n\t:ds="); 158 list_ipaddresses(fp, hp->domain_server); 159 fprintf(fp, ":"); 160 } 161 if (hp->flags.exten_file) { 162 fprintf(fp, "\\\n\t:ef=%s:", hp->exten_file->string); 163 } 164 if (hp->flags.exec_file) { 165 fprintf(fp, "\\\n\t:ex=%s:", hp->exec_file->string); 166 } 167 if (hp->flags.gateway) { 168 fprintf(fp, "\\\n\t:gw="); 169 list_ipaddresses(fp, hp->gateway); 170 fprintf(fp, ":"); 171 } 172 /* FdC: swap_server (see below) */ 173 if (hp->flags.homedir) { 174 fprintf(fp, "\\\n\t:hd=%s:", hp->homedir->string); 175 } 176 /* FdC: dump_file (see above) */ 177 /* FdC: domain_name (see above) */ 178 /* FdC: root_path (see below) */ 179 if (hp->flags.name_switch && hp->flags.send_name) { 180 fprintf(fp, "\\\n\t:hn:"); 181 } 182 if (hp->flags.htype) { 183 int hlen = haddrlength(hp->htype); 184 fprintf(fp, "\\\n\t:ht=%u:", (unsigned) hp->htype); 185 if (hp->flags.haddr) { 186 fprintf(fp, "ha=\"%s\":", 187 haddrtoa(hp->haddr, hlen)); 188 } 189 } 190 if (hp->flags.impress_server) { 191 fprintf(fp, "\\\n\t:im="); 192 list_ipaddresses(fp, hp->impress_server); 193 fprintf(fp, ":"); 194 } 195 /* NetBSD: swap_server (see below) */ 196 if (hp->flags.iaddr) { 197 fprintf(fp, "\\\n\t:ip=%s:", inet_ntoa(hp->iaddr)); 198 } 199 if (hp->flags.log_server) { 200 fprintf(fp, "\\\n\t:lg="); 201 list_ipaddresses(fp, hp->log_server); 202 fprintf(fp, ":"); 203 } 204 if (hp->flags.lpr_server) { 205 fprintf(fp, "\\\n\t:lp="); 206 list_ipaddresses(fp, hp->lpr_server); 207 fprintf(fp, ":"); 208 } 209 if (hp->flags.msg_size) { 210 fprintf(fp, "\\\n\t:ms=%d:", hp->msg_size); 211 } 212 if (hp->flags.min_wait) { 213 fprintf(fp, "\\\n\t:mw=%d:", hp->min_wait); 214 } 215 if (hp->flags.name_server) { 216 fprintf(fp, "\\\n\t:ns="); 217 list_ipaddresses(fp, hp->name_server); 218 fprintf(fp, ":"); 219 } 220 if (hp->flags.ntp_server) { 221 fprintf(fp, "\\\n\t:nt="); 222 list_ipaddresses(fp, hp->ntp_server); 223 fprintf(fp, ":"); 224 } 225 if (hp->flags.reply_addr) { 226 fprintf(fp, "\\\n\t:ra=%s:", inet_ntoa(hp->reply_addr)); 227 } 228 if (hp->flags.rlp_server) { 229 fprintf(fp, "\\\n\t:rl="); 230 list_ipaddresses(fp, hp->rlp_server); 231 fprintf(fp, ":"); 232 } 233 if (hp->flags.root_path) { 234 fprintf(fp, "\\\n\t:rp=%s:", hp->root_path->string); 235 } 236 if (hp->flags.bootserver) { 237 fprintf(fp, "\\\n\t:sa=%s:", inet_ntoa(hp->bootserver)); 238 } 239 if (hp->flags.subnet_mask) { 240 fprintf(fp, "\\\n\t:sm=%s:", inet_ntoa(hp->subnet_mask)); 241 } 242 if (hp->flags.swap_server) { 243 fprintf(fp, "\\\n\t:sw=%s:", inet_ntoa(hp->subnet_mask)); 244 } 245 if (hp->flags.tftpdir) { 246 fprintf(fp, "\\\n\t:td=%s:", hp->tftpdir->string); 247 } 248 /* NetBSD: rootpath (see above) */ 249 /* NetBSD: domainname (see above) */ 250 /* NetBSD: dumpfile (see above) */ 251 if (hp->flags.time_offset) { 252 fprintf(fp, "\\\n\t:to=%ld:", (long)hp->time_offset); 253 } 254 if (hp->flags.time_server) { 255 fprintf(fp, "\\\n\t:ts="); 256 list_ipaddresses(fp, hp->time_server); 257 fprintf(fp, ":"); 258 } 259 if (hp->flags.vm_cookie) { 260 fprintf(fp, "\\\n\t:vm="); 261 if (!bcmp(hp->vm_cookie, vm_rfc1048, 4)) { 262 fprintf(fp, "rfc1048:"); 263 } else if (!bcmp(hp->vm_cookie, vm_cmu, 4)) { 264 fprintf(fp, "cmu:"); 265 } else { 266 fprintf(fp, "%d.%d.%d.%d:", 267 (int) ((hp->vm_cookie)[0]), 268 (int) ((hp->vm_cookie)[1]), 269 (int) ((hp->vm_cookie)[2]), 270 (int) ((hp->vm_cookie)[3])); 271 } 272 } 273 if (hp->flags.nis_domain) { 274 fprintf(fp, "\\\n\t:yd=%s:", 275 hp->nis_domain->string); 276 } 277 if (hp->flags.nis_server) { 278 fprintf(fp, "\\\n\t:ys="); 279 list_ipaddresses(fp, hp->nis_server); 280 fprintf(fp, ":"); 281 } 282 /* 283 * XXX - Add new tags here (or above, 284 * so they print in alphabetical order). 285 */ 286 287 if (hp->flags.generic) { 288 dump_generic(fp, hp->generic); 289 } 290 } 291 } 292 293 294 static void 295 dump_generic(FILE *fp, struct shared_bindata *generic) 296 { 297 u_char *bp = generic->data; 298 u_char *ep = bp + generic->length; 299 u_char tag; 300 int len; 301 302 while (bp < ep) { 303 tag = *bp++; 304 if (tag == TAG_PAD) 305 continue; 306 if (tag == TAG_END) 307 return; 308 len = *bp++; 309 if (bp + len > ep) { 310 fprintf(fp, " #junk in generic! :"); 311 return; 312 } 313 fprintf(fp, "\\\n\t:T%d=", tag); 314 while (len) { 315 fprintf(fp, "%02X", *bp); 316 bp++; 317 len--; 318 if (len) 319 fprintf(fp, "."); 320 } 321 fprintf(fp, ":"); 322 } 323 } 324 325 326 327 /* 328 * Dump an entire struct in_addr_list of IP addresses to the indicated file. 329 * 330 * The addresses are printed in standard ASCII "dot" notation and separated 331 * from one another by a single space. A single leading space is also 332 * printed before the first address. 333 * 334 * Null lists produce no output (and no error). 335 */ 336 337 static void 338 list_ipaddresses(FILE *fp, struct in_addr_list *ipptr) 339 { 340 unsigned count; 341 struct in_addr *addrptr; 342 343 if (ipptr) { 344 count = ipptr->addrcount; 345 addrptr = ipptr->addr; 346 while (count > 0) { 347 fprintf(fp, "%s", inet_ntoa(*addrptr++)); 348 count--; 349 if (count) 350 fprintf(fp, ", "); 351 } 352 } 353 } 354 355 #endif /* DEBUG */ 356 357 /* 358 * Local Variables: 359 * tab-width: 4 360 * c-indent-level: 4 361 * c-argdecl-indent: 4 362 * c-continued-statement-offset: 4 363 * c-continued-brace-offset: -4 364 * c-label-offset: -4 365 * c-brace-offset: 0 366 * End: 367 */ 368