1 /* $NetBSD: dumptab.c,v 1.6 2002/07/14 00:26:17 wiz Exp $ */ 2 3 #include <sys/cdefs.h> 4 #ifndef lint 5 __RCSID("$NetBSD: dumptab.c,v 1.6 2002/07/14 00:26:17 wiz 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 <syslog.h> 19 #include <time.h> 20 21 #ifndef USE_BFUNCS 22 #include <memory.h> 23 /* Yes, memcpy is OK here (no overlapped copies). */ 24 #define bcopy(a,b,c) memcpy(b,a,c) 25 #define bzero(p,l) memset(p,0,l) 26 #define bcmp(a,b,c) memcmp(a,b,c) 27 #endif 28 29 #include "bootp.h" 30 #include "hash.h" 31 #include "hwaddr.h" 32 #include "report.h" 33 #include "patchlevel.h" 34 #include "bootpd.h" 35 36 static void dump_generic(FILE *, struct shared_bindata *); 37 static void dump_host(FILE *, struct host *); 38 static void list_ipaddresses(FILE *, struct in_addr_list *); 39 void dumptab(char *); 40 41 #ifndef DEBUG 42 void 43 dumptab(char *filename) 44 { 45 report(LOG_INFO, "No dumptab support!"); 46 } 47 48 #else /* DEBUG */ 49 50 /* 51 * Dump the internal memory database to bootpd_dump. 52 */ 53 54 void 55 dumptab(char *filename) 56 { 57 int n; 58 struct host *hp; 59 FILE *fp; 60 time_t t; 61 /* Print symbols in alphabetical order for reader's convenience. */ 62 static char legend[] = "#\n# Legend:\t(see bootptab.5)\n\ 63 #\tfirst field -- hostname (not indented)\n\ 64 #\tbf -- bootfile\n\ 65 #\tbs -- bootfile size in 512-octet blocks\n\ 66 #\tcs -- cookie servers\n\ 67 #\tdf -- dump file name\n\ 68 #\tdn -- domain name\n\ 69 #\tds -- domain name servers\n\ 70 #\tef -- extension file\n\ 71 #\tex -- exec file (YORK_EX_OPTION)\n\ 72 #\tgw -- gateways\n\ 73 #\tha -- hardware address\n\ 74 #\thd -- home directory for bootfiles\n\ 75 #\thn -- host name set for client\n\ 76 #\tht -- hardware type\n\ 77 #\tim -- impress servers\n\ 78 #\tip -- host IP address\n\ 79 #\tlg -- log servers\n\ 80 #\tlp -- LPR servers\n\ 81 #\tms -- message size\n\ 82 #\tmw -- min wait (secs)\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 128 129 /* 130 * Dump all the available information on the host pointed to by "hp". 131 * The output is sent to the file pointed to by "fp". 132 */ 133 134 static void 135 dump_host(FILE *fp, struct host *hp) 136 { 137 /* Print symbols in alphabetical order for reader's convenience. */ 138 if (hp) { 139 fprintf(fp, "%s:", (hp->hostname ? 140 hp->hostname->string : "?")); 141 if (hp->flags.bootfile) { 142 fprintf(fp, "\\\n\t:bf=%s:", hp->bootfile->string); 143 } 144 if (hp->flags.bootsize) { 145 fprintf(fp, "\\\n\t:bs="); 146 if (hp->flags.bootsize_auto) { 147 fprintf(fp, "auto:"); 148 } else { 149 fprintf(fp, "%d:", hp->bootsize); 150 } 151 } 152 if (hp->flags.cookie_server) { 153 fprintf(fp, "\\\n\t:cs="); 154 list_ipaddresses(fp, hp->cookie_server); 155 fprintf(fp, ":"); 156 } 157 if (hp->flags.dump_file) { 158 fprintf(fp, "\\\n\t:df=%s:", hp->dump_file->string); 159 } 160 if (hp->flags.domain_name) { 161 fprintf(fp, "\\\n\t:dn=%s:", hp->domain_name->string); 162 } 163 if (hp->flags.domain_server) { 164 fprintf(fp, "\\\n\t:ds="); 165 list_ipaddresses(fp, hp->domain_server); 166 fprintf(fp, ":"); 167 } 168 if (hp->flags.exten_file) { 169 fprintf(fp, "\\\n\t:ef=%s:", hp->exten_file->string); 170 } 171 if (hp->flags.exec_file) { 172 fprintf(fp, "\\\n\t:ex=%s:", hp->exec_file->string); 173 } 174 if (hp->flags.gateway) { 175 fprintf(fp, "\\\n\t:gw="); 176 list_ipaddresses(fp, hp->gateway); 177 fprintf(fp, ":"); 178 } 179 /* FdC: swap_server (see below) */ 180 if (hp->flags.homedir) { 181 fprintf(fp, "\\\n\t:hd=%s:", hp->homedir->string); 182 } 183 /* FdC: dump_file (see above) */ 184 /* FdC: domain_name (see above) */ 185 /* FdC: root_path (see below) */ 186 if (hp->flags.name_switch && hp->flags.send_name) { 187 fprintf(fp, "\\\n\t:hn:"); 188 } 189 if (hp->flags.htype) { 190 int hlen = haddrlength(hp->htype); 191 fprintf(fp, "\\\n\t:ht=%u:", (unsigned) hp->htype); 192 if (hp->flags.haddr) { 193 fprintf(fp, "ha=\"%s\":", 194 haddrtoa(hp->haddr, hlen)); 195 } 196 } 197 if (hp->flags.impress_server) { 198 fprintf(fp, "\\\n\t:im="); 199 list_ipaddresses(fp, hp->impress_server); 200 fprintf(fp, ":"); 201 } 202 /* NetBSD: swap_server (see below) */ 203 if (hp->flags.iaddr) { 204 fprintf(fp, "\\\n\t:ip=%s:", inet_ntoa(hp->iaddr)); 205 } 206 if (hp->flags.log_server) { 207 fprintf(fp, "\\\n\t:lg="); 208 list_ipaddresses(fp, hp->log_server); 209 fprintf(fp, ":"); 210 } 211 if (hp->flags.lpr_server) { 212 fprintf(fp, "\\\n\t:lp="); 213 list_ipaddresses(fp, hp->lpr_server); 214 fprintf(fp, ":"); 215 } 216 if (hp->flags.msg_size) { 217 fprintf(fp, "\\\n\t:ms=%d:", hp->msg_size); 218 } 219 if (hp->flags.min_wait) { 220 fprintf(fp, "\\\n\t:mw=%d:", hp->min_wait); 221 } 222 if (hp->flags.name_server) { 223 fprintf(fp, "\\\n\t:ns="); 224 list_ipaddresses(fp, hp->name_server); 225 fprintf(fp, ":"); 226 } 227 if (hp->flags.ntp_server) { 228 fprintf(fp, "\\\n\t:nt="); 229 list_ipaddresses(fp, hp->ntp_server); 230 fprintf(fp, ":"); 231 } 232 if (hp->flags.reply_addr) { 233 fprintf(fp, "\\\n\t:ra=%s:", inet_ntoa(hp->reply_addr)); 234 } 235 if (hp->flags.rlp_server) { 236 fprintf(fp, "\\\n\t:rl="); 237 list_ipaddresses(fp, hp->rlp_server); 238 fprintf(fp, ":"); 239 } 240 if (hp->flags.root_path) { 241 fprintf(fp, "\\\n\t:rp=%s:", hp->root_path->string); 242 } 243 if (hp->flags.bootserver) { 244 fprintf(fp, "\\\n\t:sa=%s:", inet_ntoa(hp->bootserver)); 245 } 246 if (hp->flags.subnet_mask) { 247 fprintf(fp, "\\\n\t:sm=%s:", inet_ntoa(hp->subnet_mask)); 248 } 249 if (hp->flags.swap_server) { 250 fprintf(fp, "\\\n\t:sw=%s:", inet_ntoa(hp->subnet_mask)); 251 } 252 if (hp->flags.tftpdir) { 253 fprintf(fp, "\\\n\t:td=%s:", hp->tftpdir->string); 254 } 255 /* NetBSD: rootpath (see above) */ 256 /* NetBSD: domainname (see above) */ 257 /* NetBSD: dumpfile (see above) */ 258 if (hp->flags.time_offset) { 259 fprintf(fp, "\\\n\t:to=%ld:", (long)hp->time_offset); 260 } 261 if (hp->flags.time_server) { 262 fprintf(fp, "\\\n\t:ts="); 263 list_ipaddresses(fp, hp->time_server); 264 fprintf(fp, ":"); 265 } 266 if (hp->flags.vm_cookie) { 267 fprintf(fp, "\\\n\t:vm="); 268 if (!bcmp(hp->vm_cookie, vm_rfc1048, 4)) { 269 fprintf(fp, "rfc1048:"); 270 } else if (!bcmp(hp->vm_cookie, vm_cmu, 4)) { 271 fprintf(fp, "cmu:"); 272 } else { 273 fprintf(fp, "%d.%d.%d.%d:", 274 (int) ((hp->vm_cookie)[0]), 275 (int) ((hp->vm_cookie)[1]), 276 (int) ((hp->vm_cookie)[2]), 277 (int) ((hp->vm_cookie)[3])); 278 } 279 } 280 if (hp->flags.nis_domain) { 281 fprintf(fp, "\\\n\t:yd=%s:", 282 hp->nis_domain->string); 283 } 284 if (hp->flags.nis_server) { 285 fprintf(fp, "\\\n\t:ys="); 286 list_ipaddresses(fp, hp->nis_server); 287 fprintf(fp, ":"); 288 } 289 /* 290 * XXX - Add new tags here (or above, 291 * so they print in alphabetical order). 292 */ 293 294 if (hp->flags.generic) { 295 dump_generic(fp, hp->generic); 296 } 297 } 298 } 299 300 301 static void 302 dump_generic(FILE *fp, 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 333 334 /* 335 * Dump an entire struct in_addr_list of IP addresses to the indicated file. 336 * 337 * The addresses are printed in standard ASCII "dot" notation and separated 338 * from one another by a single space. A single leading space is also 339 * printed before the first adddress. 340 * 341 * Null lists produce no output (and no error). 342 */ 343 344 static void 345 list_ipaddresses(FILE *fp, struct in_addr_list *ipptr) 346 { 347 unsigned count; 348 struct in_addr *addrptr; 349 350 if (ipptr) { 351 count = ipptr->addrcount; 352 addrptr = ipptr->addr; 353 while (count > 0) { 354 fprintf(fp, "%s", inet_ntoa(*addrptr++)); 355 count--; 356 if (count) 357 fprintf(fp, ", "); 358 } 359 } 360 } 361 362 #endif /* DEBUG */ 363 364 /* 365 * Local Variables: 366 * tab-width: 4 367 * c-indent-level: 4 368 * c-argdecl-indent: 4 369 * c-continued-statement-offset: 4 370 * c-continued-brace-offset: -4 371 * c-label-offset: -4 372 * c-brace-offset: 0 373 * End: 374 */ 375