home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1994 March / Source_Code_CD-ROM_Walnut_Creek_March_1994.iso / compsrcs / unix / volume27 / bootp22b / part02 < prev    next >
Encoding:
Text File  |  1993-10-11  |  93.4 KB  |  3,745 lines

  1. Newsgroups: comp.sources.unix
  2. From: gwr@mc.com (Gordon W. Ross)
  3. Subject: v27i064: bootp-2.2.B - RFC 1048 "bootp" server (w/ vendor extensions), Part02/02
  4. References: <1.750376278.23845@gw.home.vix.com>
  5. Sender: unix-sources-moderator@gw.home.vix.com
  6. Approved: vixie@gw.home.vix.com
  7.  
  8. Submitted-By: gwr@mc.com (Gordon W. Ross)
  9. Posting-Number: Volume 27, Issue 64
  10. Archive-Name: bootp-2.2.B/part02
  11.  
  12. #! /bin/sh
  13. # This is a shell archive.  Remove anything before this line, then unpack
  14. # it by saving it into a file and typing "sh file".  To overwrite existing
  15. # files, type "sh file -c".  You can also feed this as standard input via
  16. # unshar, or by typing "sh <file", e.g..  If this archive is complete, you
  17. # will see the following message at the end:
  18. #        "End of archive 2 (of 2)."
  19. # Contents:  bootpd.c readfile.c
  20. # Wrapped by vixie@gw.home.vix.com on Mon Oct 11 14:48:26 1993
  21. PATH=/bin:/usr/bin:/usr/ucb ; export PATH
  22. if test -f 'bootpd.c' -a "${1}" != "-c" ; then 
  23.   echo shar: Will not clobber existing file \"'bootpd.c'\"
  24. else
  25. echo shar: Extracting \"'bootpd.c'\" \(40573 characters\)
  26. sed "s/^X//" >'bootpd.c' <<'END_OF_FILE'
  27. X#ifndef _BLURB_
  28. X#define _BLURB_
  29. X/************************************************************************
  30. X          Copyright 1988, 1991 by Carnegie Mellon University
  31. X
  32. X                          All Rights Reserved
  33. X
  34. XPermission to use, copy, modify, and distribute this software and its
  35. Xdocumentation for any purpose and without fee is hereby granted, provided
  36. Xthat the above copyright notice appear in all copies and that both that
  37. Xcopyright notice and this permission notice appear in supporting
  38. Xdocumentation, and that the name of Carnegie Mellon University not be used
  39. Xin advertising or publicity pertaining to distribution of the software
  40. Xwithout specific, written prior permission.
  41. X
  42. XCARNEGIE MELLON UNIVERSITY DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS
  43. XSOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS.
  44. XIN NO EVENT SHALL CMU BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
  45. XDAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
  46. XPROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
  47. XACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
  48. XSOFTWARE.
  49. X************************************************************************/
  50. X#endif /* _BLURB_ */
  51. X
  52. X
  53. X#ifndef lint
  54. Xstatic char sccsid[] = "@(#)bootp.c    1.1 (Stanford) 1/22/86";
  55. Xstatic char rcsid[] = "$Header: /afs/andrew.cmu.edu/netdev/src/cmu/bootp-public/RCS/bootpd.c,v 1.3 1991/11/01 10:02:29 ww0n Exp ww0n $";
  56. X#endif
  57. X
  58. X
  59. X/*
  60. X * BOOTP (bootstrap protocol) server daemon.
  61. X *
  62. X * Answers BOOTP request packets from booting client machines.
  63. X * See [SRI-NIC]<RFC>RFC951.TXT for a description of the protocol.
  64. X * See [SRI-NIC]<RFC>RFC1048.TXT for vendor-information extensions.
  65. X * See accompanying man page -- bootpd.8
  66. X *
  67. X *
  68. X * HISTORY
  69. X *
  70. X * 01/22/86    Bill Croft at Stanford University
  71. X *            Created.
  72. X *
  73. X * 07/30/86     David Kovar at Carnegie Mellon University
  74. X *            Modified to work at CMU.
  75. X *
  76. X * 07/24/87    Drew D. Perkins at Carnegie Mellon University
  77. X *            Modified to use syslog instead of Kovar's
  78. X *            routines.  Add debugging dumps.  Many other fixups.
  79. X *
  80. X * 07/15/88    Walter L. Wimer at Carnegie Mellon University
  81. X *            Added vendor information to conform to RFC1048.
  82. X *            Adopted termcap-like file format to support above.
  83. X *            Added hash table lookup instead of linear search.
  84. X *            Other cleanups.
  85. X *
  86. X *
  87. X * BUGS
  88. X *
  89. X * Currently mallocs memory in a very haphazard manner.  As such, most of
  90. X * the program ends up core-resident all the time just to follow all the
  91. X * stupid pointers around. . . .
  92. X *
  93. X */
  94. X
  95. X
  96. X
  97. X
  98. X#include <sys/types.h>
  99. X#include <sys/socket.h>
  100. X#include <sys/ioctl.h>
  101. X#include <sys/file.h>
  102. X#include <sys/time.h>
  103. X#include <sys/stat.h>
  104. X#include <net/if.h>
  105. X#if defined(SUNOS4) || defined(SVR4)
  106. X#include <sys/sockio.h>
  107. X#include <net/if_arp.h>
  108. X#endif
  109. X#include <netinet/in.h>
  110. X#include <stdlib.h>
  111. X#include <signal.h>
  112. X#include <stdio.h>
  113. X#ifdef SVR4
  114. X#include <sys/termios.h>    /* Why? */
  115. X#include <string.h>
  116. X#include <sys/fcntl.h>    /* Why? */
  117. X#else
  118. X#include <strings.h>
  119. X#endif
  120. X#include <errno.h>
  121. X#include <ctype.h>
  122. X#include <netdb.h>
  123. X#ifdef SYSLOG
  124. X#include <syslog.h>
  125. X#endif
  126. X#include "bootp.h"
  127. X#include "hash.h"
  128. X#include "bootpd.h"
  129. X
  130. X#define HASHTABLESIZE        257    /* Hash table size (prime) */
  131. X#define DEFAULT_TIMEOUT         15L    /* Default timeout in minutes */
  132. X#define MAXPKT            (3*512) /* Maximum packet size */
  133. X
  134. X#ifdef DEBUG
  135. X#define CONFIG_FILE        "bootptab.debug"
  136. X#endif    /* XXX */
  137. X
  138. X#ifndef CONFIG_FILE
  139. X#define CONFIG_FILE        "/etc/bootptab"
  140. X#endif
  141. X#ifndef DUMP_FILE
  142. X#define DUMP_FILE        "/etc/bootpd.dump"
  143. X#endif
  144. X
  145. X#ifdef SVR4
  146. X#define bcopy(a,b,c)    memcpy(b,a,c)
  147. X#define bzero(p,l)      memset(p,0,l)
  148. X#define bcmp(a,b,c)     memcmp(a,b,c)
  149. X#define    index(a,b)    strchr(a,b)
  150. X#endif
  151. X
  152. X
  153. X
  154. X/*
  155. X * Externals, forward declarations, and global variables
  156. X */
  157. X
  158. Xextern char Version[];
  159. Xextern char *sys_errlist[];
  160. Xextern int  errno, sys_nerr;
  161. X
  162. Xvoid usage();
  163. Xvoid dump_host();
  164. Xvoid list_ipaddresses();
  165. X#ifdef VEND_CMU
  166. Xvoid dovend_cmu();
  167. X#endif
  168. Xvoid dovend_rfc1048();
  169. Xboolean hwlookcmp();
  170. Xboolean iplookcmp();
  171. Xvoid insert_generic();
  172. Xvoid insert_ip();
  173. Xvoid dumptab();
  174. Xint chk_access();
  175. Xvoid report();
  176. Xchar *get_errmsg();
  177. Xvoid answer_request();
  178. Xvoid forward_reply();
  179. X
  180. X/*
  181. X * IP port numbers for client and server obtained from /etc/services
  182. X */
  183. X
  184. Xu_short bootps_port, bootpc_port;
  185. X
  186. X
  187. X/*
  188. X * Internet socket and interface config structures
  189. X */
  190. X
  191. Xstruct sockaddr server_addr;
  192. Xstruct sockaddr client_addr;
  193. Xstruct ifreq ifreq[10];        /* Holds interface configuration */
  194. Xstruct ifconf ifconf;        /* Int. config ioctl block (pnts to ifreq) */
  195. Xstruct arpreq arpreq;        /* Arp request ioctl block */
  196. X
  197. X
  198. X/*
  199. X * General
  200. X */
  201. X
  202. Xint debug = 0;                /* Debugging flag (level) */
  203. Xint s;                    /* Socket file descriptor */
  204. Xchar *pktbuf;
  205. Xint pktlen;
  206. Xstruct timezone tzp;            /* Time zone offset for clients */
  207. Xstruct timeval tp;            /* Time (extra baggage) */
  208. Xlong secondswest;            /* Time zone offset in seconds */
  209. X
  210. X/*
  211. X * Globals below are associated with the bootp database file (bootptab).
  212. X */
  213. X
  214. Xchar *bootptab = NULL;
  215. X#ifdef DEBUG
  216. Xchar *bootpd_dump = NULL;
  217. X#endif
  218. X
  219. X
  220. X
  221. X/*
  222. X * Vendor magic cookies for CMU and RFC1048
  223. X */
  224. X
  225. Xunsigned char vm_cmu[4]        = VM_CMU;
  226. Xunsigned char vm_rfc1048[4] = VM_RFC1048;
  227. Xunsigned char vm_zero[4]    = { 0, 0, 0, 0 };
  228. X
  229. X
  230. X/*
  231. X * Hardware address lengths (in bytes) and network name based on hardware
  232. X * type code.  List in order specified by Assigned Numbers RFC; Array index
  233. X * is hardware type code.  Entries marked as zero are unknown to the author
  234. X * at this time.  .  .  .
  235. X */
  236. X
  237. Xstruct hwinfo hwinfolist[] = {
  238. X     { 0, "Reserved"     },    /* Type 0:  Reserved (don't use this)   */
  239. X     { 6, "Ethernet"     },    /* Type 1:  10Mb Ethernet (48 bits)    */
  240. X     { 1, "3Mb Ethernet" },    /* Type 2:   3Mb Ethernet (8 bits)    */
  241. X     { 0, "AX.25"     },    /* Type 3:  Amateur Radio AX.25        */
  242. X     { 1, "ProNET"     },    /* Type 4:  Proteon ProNET Token Ring   */
  243. X     { 0, "Chaos"     },    /* Type 5:  Chaos            */
  244. X     { 6, "IEEE 802"     },    /* Type 6:  IEEE 802 Networks        */
  245. X     { 0, "ARCNET"     }    /* Type 7:  ARCNET            */
  246. X};
  247. Xint hwinfocnt = sizeof(hwinfolist) / sizeof(hwinfolist[0]);
  248. X
  249. X/*
  250. X * Main hash tables
  251. X */
  252. X
  253. Xhash_tbl *hwhashtable;
  254. Xhash_tbl *iphashtable;
  255. Xhash_tbl *nmhashtable;
  256. X
  257. X
  258. X
  259. X
  260. X/*
  261. X * Initialization such as command-line processing is done and then the main
  262. X * server loop is started.
  263. X */
  264. X
  265. Xmain(argc, argv)
  266. X    int argc;
  267. X    char **argv;
  268. X{
  269. X    struct timeval actualtimeout, *timeout;
  270. X    struct bootp *bp;
  271. X    struct servent *servp;
  272. X    struct sockaddr_in *s_sin;
  273. X    char *stmp;
  274. X    int n, sa_len, ca_len;
  275. X    int nfound, readfds;
  276. X    int standalone;
  277. X
  278. X    stmp = NULL;
  279. X    actualtimeout.tv_usec = 0L;
  280. X    actualtimeout.tv_sec  = 60 * DEFAULT_TIMEOUT;
  281. X    timeout = &actualtimeout;
  282. X    s_sin = (struct sockaddr_in *) &server_addr;
  283. X
  284. X    /* Get space for receiving packets and composing replies. */
  285. X    pktbuf = malloc(MAXPKT);
  286. X    bp = (struct bootp *) pktbuf;
  287. X
  288. X    /*
  289. X     * Check to see if a socket was passed to us from inetd.
  290. X     *
  291. X     * Use getsockname() to determine if descriptor 0 is indeed a socket
  292. X     * (and thus we are probably a child of inetd) or if it is instead
  293. X     * something else and we are running standalone.
  294. X     */
  295. X    s = 0;
  296. X    sa_len = sizeof(server_addr);
  297. X    bzero((char *) &server_addr, sa_len);
  298. X    errno = 0;
  299. X    standalone = TRUE;
  300. X    if (getsockname(s, &server_addr, &sa_len) == 0) {
  301. X    /*
  302. X     * Descriptor 0 is a socket.  Assume we are a child of inetd.
  303. X     */
  304. X    if (s_sin->sin_family == AF_INET) {
  305. X        standalone = FALSE;
  306. X        bootps_port = ntohs(s_sin->sin_port);
  307. X    } else {
  308. X        report(LOG_INFO, "getsockname: not an INET socket\n");
  309. X    }
  310. X    }
  311. X
  312. X    /*
  313. X     * Read switches.
  314. X     */
  315. X    for (argc--, argv++; argc > 0; argc--, argv++) {
  316. X    if (argv[0][0] == '-') {
  317. X        switch (argv[0][1]) {
  318. X        case 't':
  319. X            if (argv[0][2]) {
  320. X            stmp = &(argv[0][2]);
  321. X            } else {
  322. X            argc--;
  323. X            argv++;
  324. X            stmp = argv[0];
  325. X            }
  326. X            if (!stmp || (sscanf(stmp, "%d", &n) != 1) || (n < 0)) {
  327. X            fprintf(stderr,
  328. X                "bootpd: invalid timeout specification\n");
  329. X            break;
  330. X            }
  331. X            actualtimeout.tv_sec = (long) (60 * n);
  332. X            /*
  333. X             * If the actual timeout is zero, pass a NULL pointer
  334. X             * to select so it blocks indefinitely, otherwise,
  335. X             * point to the actual timeout value.
  336. X             */
  337. X            timeout = (n > 0) ? &actualtimeout : NULL;
  338. X            break;
  339. X        case 'd':
  340. X            if (argv[0][2]) {
  341. X            stmp = &(argv[0][2]);
  342. X            } else if (argv[1][0] == '-') {
  343. X            /*
  344. X             * Backwards-compatible behavior:
  345. X             * no parameter, so just increment the debug flag.
  346. X             */
  347. X            debug++;
  348. X            break;
  349. X            } else {
  350. X            argc--;
  351. X            argv++;
  352. X            stmp = argv[0];
  353. X            }
  354. X            if (!stmp || (sscanf(stmp, "%d", &n) != 1) || (n < 0)) {
  355. X            fprintf(stderr,
  356. X                "bootpd: invalid debug level\n");
  357. X            break;
  358. X            }
  359. X            debug = n;
  360. X            break;
  361. X        case 's':
  362. X            standalone = TRUE;
  363. X            break;
  364. X        case 'i':
  365. X            standalone = FALSE;
  366. X            break;
  367. X        default:
  368. X            fprintf(stderr, "bootpd: unknown switch: -%c\n",
  369. X                argv[0][1]);
  370. X            usage();
  371. X            break;
  372. X        }
  373. X    } else {
  374. X        if (!bootptab) {
  375. X        bootptab = argv[0];
  376. X#ifdef DEBUG
  377. X        } else if (!bootpd_dump) {
  378. X        bootpd_dump = argv[0];
  379. X#endif
  380. X        } else {
  381. X        fprintf(stderr, "bootpd: unknown argument: %s\n", argv[0]);
  382. X        usage();
  383. X        }
  384. X    }
  385. X    }
  386. X
  387. X
  388. X    /*
  389. X     * Set default file names if not specified on command line
  390. X     */
  391. X    if (!bootptab) {
  392. X    bootptab = CONFIG_FILE;
  393. X    }
  394. X#ifdef DEBUG
  395. X    if (!bootpd_dump) {
  396. X    bootpd_dump = DUMP_FILE;
  397. X    }
  398. X#endif
  399. X
  400. X
  401. X    if (standalone) {
  402. X    /*
  403. X     * Go into background and disassociate from controlling terminal.
  404. X     * XXX - This is not the POSIX way... -gwr
  405. X     */
  406. X    if (debug < 3) {
  407. X        if (fork())
  408. X        exit(0);
  409. X        for (n = 0; n < 10; n++)
  410. X        (void) close(n);
  411. X        (void) open("/", O_RDONLY);
  412. X        (void) dup2(0, 1);
  413. X        (void) dup2(0, 2);
  414. X        n = open("/dev/tty", O_RDWR);
  415. X        if (n >= 0) {
  416. X        ioctl(n, TIOCNOTTY, (char *) 0);
  417. X        (void) close(n);
  418. X        }
  419. X    }
  420. X    /*
  421. X     * Nuke any timeout value
  422. X     */
  423. X    timeout = NULL;
  424. X    }
  425. X
  426. X
  427. X#ifdef SYSLOG
  428. X    /*
  429. X     * Initialize logging.
  430. X     */
  431. X#ifndef LOG_CONS
  432. X#define LOG_CONS    0    /* Don't bother if not defined... */
  433. X#endif
  434. X#ifndef LOG_DAEMON
  435. X#define LOG_DAEMON    0
  436. X#endif
  437. X    openlog("bootpd", LOG_PID | LOG_CONS, LOG_DAEMON);
  438. X#endif
  439. X
  440. X    /*
  441. X     * Log startup
  442. X     */
  443. X    report(LOG_INFO, "%s", Version);
  444. X
  445. X    /*
  446. X     * Get our timezone offset so we can give it to clients if the
  447. X     * configuration file doesn't specify one.
  448. X     */
  449. X    if (gettimeofday(&tp, &tzp) < 0) {
  450. X    secondswest = 0L;    /* Assume GMT for lack of anything better */
  451. X    report(LOG_ERR, "gettimeofday: %s\n", get_errmsg());
  452. X    } else {
  453. X    secondswest = 60L * tzp.tz_minuteswest;        /* Convert to seconds */
  454. X    }
  455. X
  456. X    /*
  457. X     * Allocate hash tables for hardware address, ip address, and hostname
  458. X     */
  459. X    hwhashtable = hash_Init(HASHTABLESIZE);
  460. X    iphashtable = hash_Init(HASHTABLESIZE);
  461. X    nmhashtable = hash_Init(HASHTABLESIZE);
  462. X    if (!(hwhashtable && iphashtable && nmhashtable)) {
  463. X    report(LOG_ERR, "Unable to allocate hash tables.\n");
  464. X    exit(1);
  465. X    }
  466. X
  467. X
  468. X    /*
  469. X     * Read the bootptab file once immediately upon startup.
  470. X     */
  471. X    readtab();
  472. X
  473. X
  474. X    if (standalone) {
  475. X    /*
  476. X     * Create a socket.
  477. X     */
  478. X    if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
  479. X        report(LOG_ERR, "socket: %s\n", get_network_errmsg());
  480. X        exit(1);
  481. X    }
  482. X
  483. X    /*
  484. X     * Get server's listening port number
  485. X     */
  486. X    servp = getservbyname("bootps", "udp");
  487. X    if (servp) {
  488. X        bootps_port = ntohs((u_short) servp->s_port);
  489. X    } else {
  490. X        report(LOG_ERR,
  491. X           "udp/bootps: unknown service -- assuming port %d\n",
  492. X           IPPORT_BOOTPS);
  493. X        bootps_port = (u_short) IPPORT_BOOTPS;
  494. X    }
  495. X
  496. X    /*
  497. X     * Bind socket to BOOTPS port.
  498. X     */
  499. X    s_sin->sin_family = AF_INET;
  500. X    s_sin->sin_addr.s_addr = INADDR_ANY;
  501. X    s_sin->sin_port = htons(bootps_port);
  502. X    if (bind(s, &server_addr, sizeof(*s_sin)) < 0) {
  503. X        report(LOG_ERR, "bind: %s\n", get_network_errmsg());
  504. X        exit(1);
  505. X    }
  506. X    }
  507. X
  508. X
  509. X    /*
  510. X     * Get destination port number so we can reply to client
  511. X     */
  512. X    servp = getservbyname("bootpc", "udp");
  513. X    if (servp) {
  514. X    bootpc_port = ntohs(servp->s_port);
  515. X    } else {
  516. X    report(LOG_ERR,
  517. X           "udp/bootpc: unknown service -- assuming port %d\n",
  518. X        IPPORT_BOOTPC);
  519. X    bootpc_port = (u_short) IPPORT_BOOTPC;
  520. X    }
  521. X
  522. X
  523. X    /*
  524. X     * Determine network configuration.
  525. X     */
  526. X    ifconf.ifc_len = sizeof(ifreq);
  527. X    ifconf.ifc_req = ifreq;
  528. X    if ((ioctl(s, SIOCGIFCONF, (caddr_t) &ifconf) < 0) ||
  529. X    (ifconf.ifc_len <= 0)) {
  530. X        report(LOG_ERR, "ioctl: %s\n", get_network_errmsg());
  531. X        exit(1);
  532. X    }
  533. X
  534. X
  535. X    /*
  536. X     * Set up signals to read or dump the table.
  537. X     */
  538. X    if ((int) signal(SIGHUP, readtab) < 0) {
  539. X    report(LOG_ERR, "signal: %s\n", get_errmsg());
  540. X    exit(1);
  541. X    }
  542. X#ifdef DEBUG
  543. X    if ((int) signal(SIGUSR1, dumptab) < 0) {
  544. X    report(LOG_ERR, "signal: %s\n", get_errmsg());
  545. X    exit(1);
  546. X    }
  547. X#endif
  548. X
  549. X    /*
  550. X     * Process incoming requests.
  551. X     */
  552. X    for (;;) {
  553. X    readfds = 1 << s;
  554. X    nfound = select(s + 1, (fd_set *)&readfds, NULL, NULL, timeout);
  555. X    if (nfound < 0) {
  556. X        if (errno != EINTR) {
  557. X        report(LOG_ERR, "select: %s\n", get_errmsg());
  558. X        }
  559. X        continue;
  560. X    }
  561. X    if (!(readfds & (1 << s))) {
  562. X        report(LOG_INFO, "exiting after %ld minutes of inactivity\n",
  563. X           actualtimeout.tv_sec / 60);
  564. X        exit(0);
  565. X    }
  566. X    ca_len = sizeof(client_addr);
  567. X    n = recvfrom(s, pktbuf, MAXPKT, 0, &client_addr, &ca_len);
  568. X    if (n <= 0) {
  569. X        continue;
  570. X    }
  571. X
  572. X    if (n < sizeof(struct bootp)) {
  573. X        if (debug) {
  574. X        report(LOG_INFO, "received short packet\n");
  575. X        }
  576. X        continue;
  577. X    }
  578. X    pktlen = n;
  579. X
  580. X    readtab();    /* maybe re-read bootptab */
  581. X    switch (bp->bp_op) {
  582. X    case BOOTREQUEST:
  583. X        /* Handle a request by answering with a reply. */
  584. X        answer_request();
  585. X        break;
  586. X
  587. X    case BOOTREPLY:
  588. X        /* Handle a reply by forwarding it to the client. */
  589. X        forward_reply();
  590. X        break;
  591. X    }
  592. X    }
  593. X}
  594. X
  595. X
  596. X
  597. X
  598. X/*
  599. X * Print "usage" message and exit
  600. X */
  601. X
  602. Xvoid usage()
  603. X{
  604. X    fprintf(stderr,
  605. X"usage:  bootpd [-d level] [-i] [-s] [-t timeout] [configfile [dumpfile]]\n");
  606. X    fprintf(stderr, "\t -d n\tset debug level\n");
  607. X    fprintf(stderr, "\t -i\tforce inetd mode (run as child of inetd)\n"); 
  608. X    fprintf(stderr, "\t -s\tforce standalone mode (run without inetd)\n");
  609. X    fprintf(stderr, "\t -t n\tset inetd exit timeout to n minutes\n");
  610. X    exit(1);
  611. X}
  612. X
  613. X
  614. X
  615. X/*
  616. X * Process BOOTREQUEST packet.
  617. X *
  618. X * (Note, this version of the bootpd.c server never forwards 
  619. X * the request to another server.  In our environment the 
  620. X * stand-alone gateways perform that function.)
  621. X *
  622. X * (Also this version does not interpret the hostname field of
  623. X * the request packet;  it COULD do a name->address lookup and
  624. X * forward the request there.)
  625. X */
  626. Xvoid
  627. Xanswer_request()
  628. X{
  629. X    register struct bootp *bp = (struct bootp *) pktbuf;
  630. X    register struct host *hp;
  631. X    register int n;
  632. X    char *path;
  633. X    struct host dummyhost;
  634. X    long bootsize;
  635. X    unsigned hlen, hashcode;
  636. X    char realpath[1024];
  637. X
  638. X    bp->bp_op = BOOTREPLY;
  639. X    if (bp->bp_ciaddr.s_addr == 0) { 
  640. X    /*
  641. X     * client doesnt know his IP address, 
  642. X     * search by hardware address.
  643. X     */
  644. X    if (debug) {
  645. X        report(LOG_INFO, "request from %s address %s\n",
  646. X            netname(bp->bp_htype),
  647. X            haddrtoa(bp->bp_chaddr, bp->bp_htype));
  648. X    }
  649. X
  650. X    dummyhost.htype = bp->bp_htype;
  651. X    hlen = haddrlength(bp->bp_htype);
  652. X    bcopy(bp->bp_chaddr, dummyhost.haddr, hlen);
  653. X    hashcode = hash_HashFunction(bp->bp_chaddr, hlen);
  654. X    hp = (struct host *) hash_Lookup(hwhashtable, hashcode, hwlookcmp,
  655. X                     &dummyhost);
  656. X    if (hp == NULL) {
  657. X        /* XXX - Do we need more control over this noise? -gwr */
  658. X        if (debug)
  659. X        report(LOG_INFO, "unknown client %s address %s\n",
  660. X               netname(bp->bp_htype),
  661. X               haddrtoa(bp->bp_chaddr, bp->bp_htype));
  662. X        return;    /* not found */
  663. X    }
  664. X    (bp->bp_yiaddr).s_addr = hp->iaddr.s_addr;
  665. X
  666. X    } else {
  667. X
  668. X    /*
  669. X     * search by IP address.
  670. X     */
  671. X    if (debug) {
  672. X        report(LOG_INFO, "request from IP addr %s\n",
  673. X            inet_ntoa(bp->bp_ciaddr));
  674. X    }
  675. X    dummyhost.iaddr.s_addr = bp->bp_ciaddr.s_addr;
  676. X    hashcode = hash_HashFunction((char *) &(bp->bp_ciaddr.s_addr), 4);
  677. X    hp = (struct host *) hash_Lookup(iphashtable, hashcode, iplookcmp,
  678. X                     &dummyhost);
  679. X    if (hp == NULL) {
  680. X        report(LOG_NOTICE,
  681. X            "IP address not found: %s\n", inet_ntoa(bp->bp_ciaddr));
  682. X        return;
  683. X    }
  684. X    }
  685. X
  686. X    if (debug) {
  687. X    report(LOG_INFO, "found %s %s\n", inet_ntoa(hp->iaddr),
  688. X        hp->hostname->string);
  689. X    }
  690. X
  691. X    /*
  692. X     * If a specific TFTP server address was specified in the bootptab file,
  693. X     * fill it in, otherwise zero it.
  694. X     */
  695. X    (bp->bp_siaddr).s_addr = (hp->flags.bootserver) ?
  696. X    hp->bootserver.s_addr : 0L;
  697. X
  698. X    /*
  699. X     * This next line is a bit of a mystery.  It seems to be vestigial
  700. X     * code (from Stanford???) which should probably be axed.
  701. X     */
  702. X    if (strcmp(bp->bp_file, "sunboot14") == 0)
  703. X    bp->bp_file[0] = 0;    /* pretend it's null */
  704. X
  705. X
  706. X    /*
  707. X     * Fill in the client's proper bootfile.
  708. X     *
  709. X     * If the client specifies an absolute path, try that file with a
  710. X     * ".host" suffix and then without.  If the file cannot be found, no
  711. X     * reply is made at all.
  712. X     *
  713. X     * [ I think this policy is too complicated.  Why not just send the
  714. X     *   reply if we know how, whether or not the file exists? -gwr ]
  715. X     *
  716. X     * If the client specifies a null or relative file, use the following
  717. X     * table to determine the appropriate action:
  718. X     *
  719. X     *  Homedir      Bootfile    Client's file
  720. X     * specified?   specified?   specification   Action
  721. X     * -------------------------------------------------------------------
  722. X     *      No          No          Null         Send null filename
  723. X     *      No          No          Relative     Discard request
  724. X     *      No          Yes         Null         Send if absolute else null
  725. X     *      No          Yes         Relative     Discard request
  726. X     *      Yes         No          Null         Send null filename
  727. X     *      Yes         No          Relative     Lookup with ".host"
  728. X     *      Yes         Yes         Null         Send home/boot or bootfile
  729. X     *      Yes         Yes         Relative     Lookup with ".host"
  730. X     *
  731. X     */
  732. X
  733. X    if (hp->flags.tftpdir) {
  734. X    strcpy(realpath, hp->tftpdir->string);
  735. X    path = &realpath[strlen(realpath)];
  736. X    } else {
  737. X    path = realpath;
  738. X    }
  739. X
  740. X    if (bp->bp_file[0]) {
  741. X    /*
  742. X     * The client specified a file.
  743. X     */
  744. X    if (bp->bp_file[0] == '/') {
  745. X        strcpy(path, bp->bp_file);        /* Absolute pathname */
  746. X    } else {
  747. X        if (hp->flags.homedir) {
  748. X        strcpy(path, hp->homedir->string);
  749. X        strcat(path, "/");
  750. X        strcat(path, bp->bp_file);
  751. X        } else {
  752. X        report(LOG_NOTICE,
  753. X            "requested file \"%s\" not found: hd unspecified\n",
  754. X            bp->bp_file);
  755. X        return;
  756. X        }
  757. X    }
  758. X    } else {
  759. X    /*
  760. X     * No file specified by the client.
  761. X     */
  762. X    if (hp->flags.bootfile && ((hp->bootfile->string)[0] == '/')) {
  763. X        strcpy(path, hp->bootfile->string);
  764. X    } else if (hp->flags.homedir && hp->flags.bootfile) {
  765. X        strcpy(path, hp->homedir->string);
  766. X        strcat(path, "/");
  767. X        strcat(path, hp->bootfile->string);
  768. X    } else {
  769. X        bzero(bp->bp_file, sizeof(bp->bp_file));
  770. X        goto skip_file;    /* Don't bother trying to access the file */
  771. X    }
  772. X    }
  773. X
  774. X    /*
  775. X     * First try to find the file with a ".host" suffix
  776. X     */
  777. X    n = strlen(path);
  778. X    strcat(path, ".");
  779. X    strcat(path, hp->hostname->string);
  780. X    if (chk_access(realpath, &bootsize) < 0) {
  781. X    path[n] = 0;            /* Try it without the suffix */
  782. X    if (chk_access(realpath, &bootsize) < 0) {
  783. X        if (bp->bp_file[0]) {
  784. X        /*
  785. X         * Client wanted specific file
  786. X         * and we didn't have it.
  787. X         */
  788. X        report(LOG_NOTICE,
  789. X            "requested file not found: \"%s\"\n", path);
  790. X        return;
  791. X        } else {
  792. X        /*
  793. X         * Client didn't ask for a specific file and we couldn't
  794. X         * access the default file, so just zero-out the bootfile
  795. X         * field in the packet and continue processing the reply.
  796. X         */
  797. X        bzero(bp->bp_file, sizeof(bp->bp_file));
  798. X        goto skip_file;
  799. X        }
  800. X    }
  801. X    }
  802. X    strcpy(bp->bp_file, path);
  803. X
  804. Xskip_file:  ;
  805. X
  806. X
  807. X
  808. X    if (debug > 1) {
  809. X    report(LOG_INFO, "vendor magic field is %d.%d.%d.%d\n",
  810. X        (int) ((bp->bp_vend)[0]),
  811. X        (int) ((bp->bp_vend)[1]),
  812. X        (int) ((bp->bp_vend)[2]),
  813. X        (int) ((bp->bp_vend)[3]));
  814. X    }
  815. X
  816. X    /*
  817. X     * If this host isn't set for automatic vendor info then copy the
  818. X     * specific cookie into the bootp packet, thus forcing a certain
  819. X     * reply format.
  820. X     */
  821. X    if (!hp->flags.vm_auto) {
  822. X        /*
  823. X         * If the client supplies a vendor magic number,
  824. X         * bootp probably should not change it...
  825. X         */
  826. X        if (debug > 1 && bcmp(bp->bp_vend, vm_zero, 4)) {
  827. X            report(LOG_INFO, "vendor magic field forced\n");
  828. X        }
  829. X        bcopy(hp->vm_cookie, bp->bp_vend, 4);
  830. X    }
  831. X
  832. X    /*
  833. X     * Figure out the format for the vendor-specific info.
  834. X     */
  835. X    if (!bcmp(bp->bp_vend, vm_rfc1048, 4)) {
  836. X        /* RFC1048 conformant bootp client */
  837. X        dovend_rfc1048(bp, hp, bootsize);
  838. X        if (debug > 1) {
  839. X            report(LOG_INFO, "sending reply (with RFC1048 options)\n");
  840. X        }
  841. X    }
  842. X#ifdef VEND_CMU
  843. X    else if (!bcmp(bp->bp_vend, vm_cmu, 4)) {
  844. X        dovend_cmu(bp, hp);
  845. X        if (debug > 1) {
  846. X            report(LOG_INFO, "sending reply (with CMU options)\n");
  847. X        }
  848. X    }
  849. X#endif
  850. X    else if (!bcmp(bp->bp_vend, vm_zero, 4)) {
  851. X        if (debug > 1) {
  852. X            report(LOG_INFO, "sending reply (no options)\n");
  853. X        }
  854. X    }
  855. X    else {
  856. X        if (debug > 1) {
  857. X            report(LOG_INFO, "sending reply (unknown options)\n");
  858. X        }
  859. X    }
  860. X
  861. X    sendreply(0);
  862. X}
  863. X
  864. X
  865. X
  866. X/*
  867. X * Process BOOTREPLY packet (something is using us as a gateway).
  868. X */
  869. X
  870. Xvoid
  871. Xforward_reply()
  872. X{
  873. X    if (debug) {
  874. X        report(LOG_INFO, "processing boot reply\n");
  875. X    }
  876. X    sendreply(1);
  877. X}
  878. X
  879. X
  880. X
  881. X/*
  882. X * Send a reply packet to the client.  'forward' flag is set if we are
  883. X * not the originator of this reply packet.
  884. X */
  885. Xsendreply(forward)
  886. X    int forward;
  887. X{
  888. X    register struct bootp *bp = (struct bootp *) pktbuf;
  889. X    struct in_addr dst;    /* XXX */
  890. X    struct sockaddr_in *c_sin;
  891. X
  892. X    c_sin = (struct sockaddr_in *) &client_addr;
  893. X
  894. X    c_sin->sin_port = htons(bootpc_port);
  895. X    /*
  896. X     * If the client IP address is specified, use that
  897. X     * else if gateway IP address is specified, use that
  898. X     * else make a temporary arp cache entry for the client's NEW 
  899. X     * IP/hardware address and use that.
  900. X     */
  901. X    if (bp->bp_ciaddr.s_addr) {
  902. X        dst = bp->bp_ciaddr;
  903. X    } else if (bp->bp_giaddr.s_addr && forward == 0) {
  904. X        dst = bp->bp_giaddr;
  905. X        c_sin->sin_port = htons(bootps_port);
  906. X    } else {
  907. X        dst = bp->bp_yiaddr;
  908. X        setarp(&dst, bp->bp_chaddr, bp->bp_hlen);
  909. X    }
  910. X
  911. X    if (forward == 0) {
  912. X        /*
  913. X         * If we are originating this reply, we
  914. X         * need to find our own interface address to
  915. X         * put in the bp_siaddr field of the reply.
  916. X         * If this server is multi-homed, pick the
  917. X         * 'best' interface (the one on the same net
  918. X         * as the client).
  919. X         */
  920. X        int maxmatch = 0;
  921. X        int len, m;
  922. X        register struct ifreq *ifrq, *ifrmax;
  923. X
  924. X        ifrmax = ifrq = &ifreq[0];
  925. X        len = ifconf.ifc_len;
  926. X        for (; len > 0; len -= sizeof(ifreq[0]), ifrq++) {
  927. X            m = nmatch(&dst, &((struct sockaddr_in *)
  928. X                      (&ifrq->ifr_addr))->sin_addr);
  929. X            if (m > maxmatch) {
  930. X                maxmatch = m;
  931. X                ifrmax = ifrq;
  932. X            }
  933. X        }
  934. X
  935. X        /* XXX - Should put anything here?  :gw=: ? */
  936. X        if (bp->bp_giaddr.s_addr == 0) {
  937. X            if (maxmatch == 0) {
  938. X                return;
  939. X            }
  940. X            bp->bp_giaddr = ((struct sockaddr_in *)
  941. X                (&ifrmax->ifr_addr))->sin_addr;
  942. X        }
  943. X
  944. X        /*
  945. X         * If a specific TFTP server address wasn't specified
  946. X         * in the bootptab file, fill in our own address.
  947. X         */
  948. X        if (bp->bp_siaddr.s_addr == 0) {
  949. X            bp->bp_siaddr = ((struct sockaddr_in *)
  950. X                     (&ifrmax->ifr_addr))->sin_addr;
  951. X        }
  952. X    }
  953. X
  954. X    c_sin->sin_addr = dst; 
  955. X    /* Send reply with same size packet as request used. */
  956. X    if (sendto(s, pktbuf, pktlen, 0,
  957. X           &client_addr, sizeof(*c_sin)) < 0) {
  958. X        report(LOG_ERR, "sendto: %s\n", get_network_errmsg());
  959. X    }
  960. X} /* sendreply */
  961. X
  962. X
  963. X
  964. X/*
  965. X * Return the number of leading bytes matching in the
  966. X * internet addresses supplied.
  967. X */
  968. Xnmatch(ca,cb)
  969. X    register char *ca, *cb;
  970. X{
  971. X    register n,m;
  972. X
  973. X    for (m = n = 0 ; n < 4 ; n++) {
  974. X    if (*ca++ != *cb++)
  975. X        return(m);
  976. X    m++;
  977. X    }
  978. X    return(m);
  979. X}
  980. X
  981. X
  982. X
  983. X/*
  984. X * Setup the arp cache so that IP address 'ia' will be temporarily
  985. X * bound to hardware address 'ha' of length 'len'.
  986. X */
  987. Xsetarp(ia, ha, len)
  988. X    struct in_addr *ia;
  989. X    byte *ha;
  990. X    int len;
  991. X{
  992. X    struct sockaddr_in *si;
  993. X    
  994. X    bzero((caddr_t)&arpreq, sizeof(arpreq));
  995. X    
  996. X    arpreq.arp_pa.sa_family = AF_INET;
  997. X    si = (struct sockaddr_in *) &arpreq.arp_pa;
  998. X    si->sin_addr = *ia;
  999. X
  1000. X    arpreq.arp_flags = ATF_INUSE | ATF_COM;
  1001. X    
  1002. X    bcopy(ha, arpreq.arp_ha.sa_data, len);
  1003. X
  1004. X    if (ioctl(s, SIOCSARP, (caddr_t)&arpreq) < 0) {
  1005. X        report(LOG_ERR, "ioctl(SIOCSARP): %s\n", get_network_errmsg());
  1006. X    }
  1007. X}
  1008. X
  1009. X
  1010. X
  1011. X/*
  1012. X * This call checks read access to a file.  It returns 0 if the file given
  1013. X * by "path" exists and is publically readable.  A value of -1 is returned if
  1014. X * access is not permitted or an error occurs.  Successful calls also
  1015. X * return the file size in bytes using the long pointer "filesize".
  1016. X *
  1017. X * The read permission bit for "other" users is checked.  This bit must be
  1018. X * set for tftpd(8) to allow clients to read the file.
  1019. X */
  1020. X
  1021. Xint chk_access(path, filesize)
  1022. Xchar *path;
  1023. Xlong *filesize;
  1024. X{
  1025. X    struct stat st;
  1026. X
  1027. X    if ((stat(path, &st) == 0) && (st.st_mode & (S_IREAD >> 6))) {
  1028. X    *filesize = (long) st.st_size;
  1029. X    return 0;
  1030. X    } else {
  1031. X    return -1;
  1032. X    }
  1033. X}
  1034. X
  1035. X
  1036. X
  1037. X#ifdef DEBUG
  1038. X
  1039. X/*
  1040. X * Dump the internal memory database to bootpd_dump.
  1041. X */
  1042. X
  1043. Xvoid dumptab()
  1044. X{
  1045. X    register int n;
  1046. X    register struct host *hp;
  1047. X    register FILE *fp;
  1048. X    long t;
  1049. X
  1050. X    /*
  1051. X     * Open bootpd.dump file.
  1052. X     */
  1053. X    if ((fp = fopen(bootpd_dump, "w")) == NULL) {
  1054. X    report(LOG_ERR, "error opening \"%s\": %s\n", bootpd_dump,
  1055. X            get_errmsg());
  1056. X    exit(1);
  1057. X    }
  1058. X
  1059. X    t = time(NULL);
  1060. X    fprintf(fp, "\n# %s\n", Version);
  1061. X    fprintf(fp, "# %s: dump of bootp server database.\n", bootpd_dump);
  1062. X    fprintf(fp, "#\n# Dump taken %s", ctime(&t));
  1063. X    fprintf(fp, "#\n#\n# Legend:\n");
  1064. X    fprintf(fp, "#\thd -- home directory\n");
  1065. X    fprintf(fp, "#\tbf -- bootfile\n");
  1066. X    fprintf(fp, "#\tbs -- bootfile size in 512-octet blocks\n");
  1067. X    fprintf(fp, "#\tcs -- cookie servers\n");
  1068. X    fprintf(fp, "#\tds -- domain name servers\n");
  1069. X    fprintf(fp, "#\tgw -- gateways\n");
  1070. X    fprintf(fp, "#\tha -- hardware address\n");
  1071. X    fprintf(fp, "#\thd -- home directory for bootfiles\n");
  1072. X    fprintf(fp, "#\tht -- hardware type\n");
  1073. X    fprintf(fp, "#\tim -- impress servers\n");
  1074. X    fprintf(fp, "#\tip -- host IP address\n");
  1075. X    fprintf(fp, "#\tlg -- log servers\n");
  1076. X    fprintf(fp, "#\tlp -- LPR servers\n");
  1077. X    fprintf(fp, "#\tns -- IEN-116 name servers\n");
  1078. X    fprintf(fp, "#\trl -- resource location protocol servers\n");
  1079. X    fprintf(fp, "#\tsm -- subnet mask\n");
  1080. X    fprintf(fp, "#\tto -- time offset (seconds)\n");
  1081. X    fprintf(fp, "#\tts -- time servers\n\n\n");
  1082. X
  1083. X    n = 0;
  1084. X    for (hp = (struct host *) hash_FirstEntry(nmhashtable); hp != NULL;
  1085. X     hp = (struct host *) hash_NextEntry(nmhashtable)) {
  1086. X        dump_host(fp, hp);
  1087. X        fprintf(fp, "\n");
  1088. X        n++;
  1089. X    }
  1090. X    fclose(fp);
  1091. X
  1092. X    report(LOG_INFO, "dumped %d entries to \"%s\".\n", n, bootpd_dump);
  1093. X}
  1094. X
  1095. X
  1096. X
  1097. X/*
  1098. X * Dump all the available information on the host pointed to by "hp".
  1099. X * The output is sent to the file pointed to by "fp".
  1100. X */
  1101. X
  1102. Xvoid dump_host(fp, hp)
  1103. XFILE *fp;
  1104. Xstruct host *hp;
  1105. X{
  1106. X    register int i;
  1107. X    register byte *dataptr;
  1108. X
  1109. X    if (hp) {
  1110. X    if (hp->hostname) {
  1111. X        fprintf(fp, "%s:", hp->hostname->string);
  1112. X    }
  1113. X    if (hp->flags.bootfile) {
  1114. X        fprintf(fp, "bf=%s:", hp->bootfile->string);
  1115. X    }
  1116. X    if (hp->flags.bootsize) {
  1117. X        fprintf(fp, "bs=");
  1118. X        if (hp->flags.bootsize_auto) {
  1119. X        fprintf(fp, "auto:");
  1120. X        } else {
  1121. X        fprintf(fp, "%d:", hp->bootsize);
  1122. X        }
  1123. X    }
  1124. X    if (hp->flags.cookie_server) {
  1125. X        fprintf(fp, "cs=");
  1126. X        list_ipaddresses(fp, hp->cookie_server);
  1127. X        fprintf(fp, ":");
  1128. X    }
  1129. X    if (hp->flags.domain_server) {
  1130. X        fprintf(fp, "ds=");
  1131. X        list_ipaddresses(fp, hp->domain_server);
  1132. X        fprintf(fp, ":");
  1133. X    }
  1134. X    if (hp->flags.gateway) {
  1135. X        fprintf(fp, "gw=");
  1136. X        list_ipaddresses(fp, hp->gateway);
  1137. X        fprintf(fp, ":");
  1138. X    }
  1139. X    if (hp->flags.homedir) {
  1140. X        fprintf(fp, "hd=%s:", hp->homedir->string);
  1141. X    }
  1142. X    if (hp->flags.name_switch && hp->flags.send_name) {
  1143. X        fprintf(fp, "hn:");
  1144. X    }
  1145. X    if (hp->flags.htype) {
  1146. X        fprintf(fp, "ht=%u:", (unsigned) hp->htype);
  1147. X        if (hp->flags.haddr) {
  1148. X        fprintf(fp, "ha=%s:", haddrtoa(hp->haddr, hp->htype));
  1149. X        }
  1150. X    }
  1151. X    if (hp->flags.impress_server) {
  1152. X        fprintf(fp, "im=");
  1153. X        list_ipaddresses(fp, hp->impress_server);
  1154. X        fprintf(fp, ":");
  1155. X    }
  1156. X    if (hp->flags.iaddr) {
  1157. X        fprintf(fp, "ip=%s:", inet_ntoa(hp->iaddr));
  1158. X    }
  1159. X    if (hp->flags.log_server) {
  1160. X        fprintf(fp, "lg=");
  1161. X        list_ipaddresses(fp, hp->log_server);
  1162. X        fprintf(fp, ":");
  1163. X    }
  1164. X    if (hp->flags.lpr_server) {
  1165. X        fprintf(fp, "lp=");
  1166. X        list_ipaddresses(fp, hp->lpr_server);
  1167. X        fprintf(fp, ":");
  1168. X    }
  1169. X    if (hp->flags.name_server) {
  1170. X        fprintf(fp, "ns=");
  1171. X        list_ipaddresses(fp, hp->name_server);
  1172. X        fprintf(fp, ":");
  1173. X    }
  1174. X    if (hp->flags.rlp_server) {
  1175. X        fprintf(fp, "rl=");
  1176. X        list_ipaddresses(fp, hp->rlp_server);
  1177. X        fprintf(fp, ":");
  1178. X    }
  1179. X    if (hp->flags.bootserver) {
  1180. X        fprintf(fp, "sa=%s:", inet_ntoa(hp->bootserver));
  1181. X    }
  1182. X    if (hp->flags.subnet_mask) {
  1183. X        fprintf(fp, "sm=%s:", inet_ntoa(hp->subnet_mask));
  1184. X    }
  1185. X    if (hp->flags.tftpdir) {
  1186. X        fprintf(fp, "td=%s:", hp->tftpdir->string);
  1187. X    }
  1188. X    if (hp->flags.time_offset) {
  1189. X        if (hp->flags.timeoff_auto) {
  1190. X        fprintf(fp, "to=auto:");
  1191. X        } else {
  1192. X        fprintf(fp, "to=%ld:", hp->time_offset);
  1193. X        }
  1194. X    }
  1195. X    if (hp->flags.time_server) {
  1196. X        fprintf(fp, "ts=");
  1197. X        list_ipaddresses(fp, hp->time_server);
  1198. X        fprintf(fp, ":");
  1199. X    }
  1200. X    if (hp->flags.vendor_magic) {
  1201. X        fprintf(fp, "vm=");
  1202. X        if (hp->flags.vm_auto) {
  1203. X        fprintf(fp, "auto:");
  1204. X        } else if (!bcmp(hp->vm_cookie, vm_cmu, 4)) {
  1205. X        fprintf(fp, "cmu:");
  1206. X        } else if (!bcmp(hp->vm_cookie, vm_rfc1048, 4)) {
  1207. X        fprintf(fp, "rfc1048");
  1208. X        } else {
  1209. X        fprintf(fp, "%d.%d.%d.%d:",
  1210. X                (int) ((hp->vm_cookie)[0]),
  1211. X                (int) ((hp->vm_cookie)[1]),
  1212. X                (int) ((hp->vm_cookie)[2]),
  1213. X                (int) ((hp->vm_cookie)[3]));
  1214. X        }
  1215. X    }
  1216. X    if (hp->flags.generic) {
  1217. X        fprintf(fp, "generic=");
  1218. X        dataptr = hp->generic->data;
  1219. X        for (i = hp->generic->length; i > 0; i--) {
  1220. X        fprintf(fp, "%02X", (int) *dataptr++);
  1221. X        }
  1222. X        fprintf(fp, ":");
  1223. X    }
  1224. X    }
  1225. X}
  1226. X
  1227. X
  1228. X
  1229. X/*
  1230. X * Dump an entire struct in_addr_list of IP addresses to the indicated file.
  1231. X *
  1232. X * The addresses are printed in standard ASCII "dot" notation and separated
  1233. X * from one another by a single space.  A single leading space is also
  1234. X * printed before the first adddress.
  1235. X *
  1236. X * Null lists produce no output (and no error).
  1237. X */
  1238. X
  1239. Xvoid list_ipaddresses(fp, ipptr)
  1240. X    FILE *fp;
  1241. X    struct in_addr_list *ipptr;
  1242. X{
  1243. X    register unsigned count;
  1244. X    register struct in_addr *addrptr;
  1245. X
  1246. X    if (ipptr) {
  1247. X    count = ipptr->addrcount;
  1248. X    addrptr = ipptr->addr;
  1249. X    if (count-- > 0) {
  1250. X        fprintf(fp, "%s", inet_ntoa(*addrptr++));
  1251. X        while (count-- > 0) {
  1252. X        fprintf(fp, " %s", inet_ntoa(*addrptr++));
  1253. X        }
  1254. X    }
  1255. X    }
  1256. X}
  1257. X#endif        /* DEBUG */
  1258. X
  1259. X
  1260. X
  1261. X#ifdef VEND_CMU
  1262. X
  1263. X/*
  1264. X * Insert the CMU "vendor" data for the host pointed to by "hp" into the
  1265. X * bootp packet pointed to by "bp".
  1266. X */
  1267. X
  1268. Xvoid dovend_cmu(bp, hp)
  1269. X    register struct bootp *bp;
  1270. X    register struct host *hp;
  1271. X{
  1272. X    struct cmu_vend *vendp;
  1273. X    register struct in_addr_list *taddr;
  1274. X
  1275. X    /*
  1276. X     * Initialize the entire vendor field to zeroes.
  1277. X     */
  1278. X    bzero(bp->bp_vend, sizeof(bp->bp_vend));
  1279. X
  1280. X    /*
  1281. X     * Fill in vendor information. Subnet mask, default gateway,
  1282. X     * domain name server, ien name server, time server
  1283. X     */
  1284. X    vendp = (struct cmu_vend *) bp->bp_vend;
  1285. X    if (hp->flags.subnet_mask) {
  1286. X    (vendp->v_smask).s_addr = hp->subnet_mask.s_addr;
  1287. X    (vendp->v_flags) |= VF_SMASK;
  1288. X    if (hp->flags.gateway) {
  1289. X        (vendp->v_dgate).s_addr = hp->gateway->addr->s_addr;
  1290. X    }
  1291. X    }
  1292. X    if (hp->flags.domain_server) {
  1293. X    taddr = hp->domain_server;
  1294. X    if (taddr->addrcount > 0) {
  1295. X        (vendp->v_dns1).s_addr = (taddr->addr)[0].s_addr;
  1296. X        if (taddr->addrcount > 1) {
  1297. X        (vendp->v_dns2).s_addr = (taddr->addr)[1].s_addr;
  1298. X        }
  1299. X    }
  1300. X    }
  1301. X    if (hp->flags.name_server) {
  1302. X    taddr = hp->name_server;
  1303. X    if (taddr->addrcount > 0) {
  1304. X        (vendp->v_ins1).s_addr = (taddr->addr)[0].s_addr;
  1305. X        if (taddr->addrcount > 1) {
  1306. X        (vendp->v_ins2).s_addr = (taddr->addr)[1].s_addr;
  1307. X        }
  1308. X    }
  1309. X    }
  1310. X    if (hp->flags.time_server) {
  1311. X    taddr = hp->time_server;
  1312. X    if (taddr->addrcount > 0) {
  1313. X        (vendp->v_ts1).s_addr = (taddr->addr)[0].s_addr;
  1314. X        if (taddr->addrcount > 1) {
  1315. X        (vendp->v_ts2).s_addr = (taddr->addr)[1].s_addr;
  1316. X        }
  1317. X    }
  1318. X    }
  1319. X    strcpy(vendp->v_magic, vm_cmu);    
  1320. X
  1321. X} /* dovend_cmu */
  1322. X
  1323. X#endif /* VEND_CMU */
  1324. X
  1325. X
  1326. X
  1327. X/*
  1328. X * Fill in the RFC1048 options.  All tag and length bytes provided
  1329. X * by the client must be left where they are.  Data associated with
  1330. X * each option are updated if we have a value for the option.
  1331. X *
  1332. X * This implementation does not change any tag or length bytes.
  1333. X *
  1334. X * One could append additional options when there is room after the
  1335. X * end record but this implementation does not do so.  One would need
  1336. X * a way to know which options should be "forced" into the list.
  1337. X */
  1338. Xvoid dovend_rfc1048(bp, hp, bootsize)
  1339. X    register struct bootp *bp;
  1340. X    register struct host *hp;
  1341. X    long bootsize;        /* "auto" value, from stat() */
  1342. X{
  1343. X    int i, n, len;
  1344. X    byte *vp, *ep;
  1345. X    byte tag;
  1346. X    char *tmpstr;
  1347. X    unsigned long ul;
  1348. X    unsigned short us;
  1349. X    
  1350. X    /*
  1351. X     * Compute pointers to beginning and end of option area.
  1352. X     * Note that options (formerly "vendor data") are now
  1353. X     * allowed to extend to the end of the packet, and are
  1354. X     * not limited to a maximum of BP_VEND_LEN bytes.
  1355. X     * That value is the MINIMUM size of the option area.
  1356. X     * (See the IETF draft: Clarifications and Extensions
  1357. X     * for the Bootstrap Protocol, W. Wimer, Sept. 1992.)
  1358. X     */
  1359. X    vp = bp->bp_vend;
  1360. X    ep = ((char *)bp) + pktlen;
  1361. X    
  1362. X    /* Step over the magic cookie (checked by caller). */
  1363. X    vp += 4;
  1364. X    
  1365. X    while (vp < ep) {
  1366. X    tag = *vp++;
  1367. X    
  1368. X    /* Check for tags with no data first. */
  1369. X    if (tag == TAG_PAD)
  1370. X        continue;
  1371. X    if (tag == TAG_END)
  1372. X        return;
  1373. X    
  1374. X    /* Now scan the length byte. */
  1375. X    len = *vp++;
  1376. X    
  1377. X    if (vp + len >= ep) {
  1378. X        report(LOG_ERR, "truncated field in vendor data\n");
  1379. X        /* do the best we can... */
  1380. X        len = ep - vp;
  1381. X    }
  1382. X    
  1383. X    switch (tag) {
  1384. X        
  1385. X    case TAG_SUBNET_MASK: /* may demand len==4 */
  1386. X        if (len >= 4) {
  1387. X        ul = hp->subnet_mask.s_addr;
  1388. X        bcopy((char*)&ul, vp, 4);
  1389. X        }
  1390. X        break;
  1391. X        
  1392. X    case TAG_TIME_OFFSET: /* may demand len==4 */
  1393. X        if (len >= 4) {
  1394. X        ul = (hp->flags.timeoff_auto) ?
  1395. X            secondswest : hp->time_offset;
  1396. X        ul = htonl(ul);
  1397. X        bcopy((char*)&ul, vp, 4);
  1398. X        }
  1399. X        break;
  1400. X        
  1401. X    case TAG_GATEWAY:
  1402. X        if (hp->flags.gateway)
  1403. X        insert_ip(hp->gateway, vp, len);
  1404. X        break;
  1405. X        
  1406. X    case TAG_TIME_SERVER:
  1407. X        if (hp->flags.time_server)
  1408. X        insert_ip(hp->time_server, vp, len);
  1409. X        break;
  1410. X        
  1411. X    case TAG_NAME_SERVER:
  1412. X        if (hp->flags.name_server)
  1413. X        insert_ip(hp->name_server, vp, len);
  1414. X        break;
  1415. X        
  1416. X    case TAG_DOMAIN_SERVER:
  1417. X        if (hp->flags.domain_server)
  1418. X        insert_ip(hp->domain_server, vp, len);
  1419. X        break;
  1420. X        
  1421. X    case TAG_LOG_SERVER:
  1422. X        if (hp->flags.log_server)
  1423. X        insert_ip(hp->log_server, vp, len);
  1424. X        break;
  1425. X        
  1426. X    case TAG_COOKIE_SERVER:
  1427. X        if (hp->flags.cookie_server)
  1428. X        insert_ip(hp->cookie_server, vp, len);
  1429. X        break;
  1430. X        
  1431. X    case TAG_LPR_SERVER:
  1432. X        if (hp->flags.lpr_server)
  1433. X        insert_ip(hp->lpr_server, vp, len);
  1434. X        break;
  1435. X        
  1436. X    case TAG_IMPRESS_SERVER:
  1437. X        if (hp->flags.impress_server)
  1438. X        insert_ip(hp->impress_server, vp, len);
  1439. X        break;
  1440. X        
  1441. X    case TAG_RLP_SERVER:
  1442. X        if (hp->flags.rlp_server)
  1443. X        insert_ip(hp->rlp_server, vp, len);
  1444. X        break;
  1445. X        
  1446. X    case TAG_HOSTNAME: /* of client */
  1447. X        if (hp->flags.name_switch &&
  1448. X        hp->flags.send_name) {
  1449. X        
  1450. X        /* Room for the whole name? */
  1451. X        n = strlen(hp->hostname->string);
  1452. X        if (n < len) {
  1453. X            bcopy(hp->hostname->string, vp, n);
  1454. X        } else {
  1455. X            /*
  1456. X             * Not enough room for fully qualified hostname,
  1457. X             * try stripping off the domain parts.
  1458. X             */
  1459. X            if (debug > 1)
  1460. X            report(LOG_INFO, "shortening hostname to fit\n");
  1461. X            tmpstr = index(hp->hostname->string, '.');
  1462. X            if (tmpstr) {
  1463. X            n = tmpstr - hp->hostname->string;
  1464. X            bcopy(hp->hostname->string, vp, n);
  1465. X            }
  1466. X        }
  1467. X        }
  1468. X        break;
  1469. X        
  1470. X    case TAG_BOOTSIZE:
  1471. X        if (len >= 2) {
  1472. X        if (hp->flags.bootsize_auto)
  1473. X            us = (bootsize + 511) / 512;    /* Round up */
  1474. X        else us = hp->bootsize;
  1475. X        bcopy((char*)&us, vp, 2);
  1476. X        }
  1477. X        break;
  1478. X        
  1479. X    default:
  1480. X        if (hp->flags.generic)
  1481. X        insert_generic(tag, hp->generic, vp, len);
  1482. X        break;
  1483. X        
  1484. X    } /* switch (tag) */
  1485. X    vp += len;
  1486. X    } /* while */
  1487. X    return;
  1488. X} /* dovend_rfc1048 */
  1489. X
  1490. X
  1491. X
  1492. X/*
  1493. X * Compare function to determine whether two hardware addresses are
  1494. X * equivalent.  Returns TRUE if "host1" and "host2" are equivalent, FALSE
  1495. X * otherwise.
  1496. X *
  1497. X * This function is used when retrieving elements from the hardware address
  1498. X * hash table.
  1499. X */
  1500. X
  1501. Xboolean hwlookcmp(host1, host2)
  1502. X    struct host *host1, *host2;
  1503. X{
  1504. X    if (host1->htype != host2->htype) {
  1505. X    return FALSE;
  1506. X    }
  1507. X    if (bcmp(host1->haddr, host2->haddr, haddrlength(host1->htype))) {
  1508. X    return FALSE;
  1509. X    }
  1510. X    return TRUE;
  1511. X}
  1512. X
  1513. X
  1514. X
  1515. X
  1516. X/*
  1517. X * Compare function for doing IP address hash table lookup.
  1518. X */
  1519. X
  1520. Xboolean iplookcmp(host1, host2)
  1521. X    struct host *host1, *host2;
  1522. X{
  1523. X    return (host1->iaddr.s_addr == host2->iaddr.s_addr);
  1524. X}
  1525. X
  1526. X
  1527. X
  1528. X/*
  1529. X * Copy a list of IP addresses to the memory buffer "dest".
  1530. X * The "iplist" is a pointer to a list of IP addresses
  1531. X * (struct in_addr_list), and "len" points to an integer which
  1532. X * indicates the size of the "dest" buffer.
  1533. X *
  1534. X * This is used to fill the vendor-specific area of a bootp packet in
  1535. X * conformance to RFC1048.
  1536. X */
  1537. X
  1538. Xvoid insert_ip(iplist, dest, len)
  1539. X    struct in_addr_list *iplist;
  1540. X    byte *dest;
  1541. X    int len;    /* space for this list */
  1542. X{
  1543. X    register struct in_addr *addrptr;
  1544. X    register unsigned addrcount;
  1545. X
  1546. X    if (!iplist) return;
  1547. X
  1548. X    addrptr = iplist->addr;
  1549. X    addrcount = iplist->addrcount;
  1550. X    while ((len >= 4) && (addrcount > 0)) {
  1551. X        bcopy(addrptr, dest, 4);
  1552. X        addrptr++;
  1553. X        addrcount--;
  1554. X        dest += 4;
  1555. X        len -= 4;
  1556. X    }
  1557. X}
  1558. X
  1559. X
  1560. X
  1561. X/*
  1562. X * Find an RFC1048 record of type "tag" and copy it to "dest".
  1563. X * The data in "gendata" is assumed to already in RFC1048 format.
  1564. X */
  1565. X
  1566. Xvoid insert_generic(tag, gendata, dest, destlen)
  1567. X    int tag;
  1568. X    struct shared_bindata *gendata;
  1569. X    byte *dest;
  1570. X    int destlen;
  1571. X{
  1572. X    byte *srcptr, *ep;
  1573. X    register int t, n;
  1574. X
  1575. X    if (!gendata) return;
  1576. X
  1577. X    srcptr = gendata->data;
  1578. X    ep = srcptr + gendata->length;
  1579. X
  1580. X    /* Search the generic records for a tag of the desired type. */
  1581. X    while (srcptr < ep) {
  1582. X        t = *srcptr++;
  1583. X        n = *srcptr++;
  1584. X
  1585. X        if (t == tag) {
  1586. X            /* Copy whatever fits. */
  1587. X            if (n > destlen)
  1588. X                n = destlen;
  1589. X            bcopy(srcptr, dest, n);
  1590. X            return;
  1591. X        }
  1592. X        srcptr += n;
  1593. X    }
  1594. X}
  1595. X
  1596. X
  1597. X
  1598. X
  1599. X/*
  1600. X * Convert a hardware address to an ASCII string.
  1601. X */
  1602. X
  1603. Xchar *haddrtoa(haddr, htype)
  1604. X    register byte *haddr;
  1605. X    byte htype;
  1606. X{
  1607. X    static char haddrbuf[2 * MAXHADDRLEN + 1];
  1608. X    register char *bufptr;
  1609. X    register unsigned count;
  1610. X
  1611. X    bufptr = haddrbuf;
  1612. X    for (count = haddrlength(htype); count > 0; count--) {
  1613. X    sprintf(bufptr, "%02X",    (unsigned) (*haddr++ & 0xFF));
  1614. X    bufptr += 2;
  1615. X    }
  1616. X    return (haddrbuf);
  1617. X}
  1618. X
  1619. X
  1620. X
  1621. X/*
  1622. X * Return pointer to static string which gives full filesystem error message.
  1623. X */
  1624. X
  1625. Xchar *get_errmsg()
  1626. X{
  1627. X    static char errmsg[80];
  1628. X
  1629. X    if (errno < sys_nerr) {
  1630. X    return sys_errlist[errno];
  1631. X    } else {
  1632. X    sprintf(errmsg, "Error %d", errno);
  1633. X    return errmsg;
  1634. X    }
  1635. X}
  1636. X
  1637. X
  1638. X
  1639. X/*
  1640. X * This routine reports errors and such via stderr and syslog() if
  1641. X * appopriate.  It just helps avoid a lot of "#ifdef SYSLOG" constructs
  1642. X * from being scattered throughout the code.
  1643. X *
  1644. X * The syntax is identical to syslog(3), but %m is not considered special
  1645. X * for output to stderr (i.e. you'll see "%m" in the output. . .).  Also,
  1646. X * control strings should normally end with \n since newlines aren't
  1647. X * automatically generated for stderr output (whereas syslog strips out all
  1648. X * newlines and adds its own at the end).
  1649. X */
  1650. X
  1651. X/*VARARGS2*/
  1652. Xvoid report(priority, fmt, p0, p1, p2, p3, p4)
  1653. X    int priority;
  1654. X    char *fmt;
  1655. X{
  1656. X#ifdef LOG_SALERT
  1657. X    static char *levelnames[] = {
  1658. X    "unknown level: ",
  1659. X    "alert(1):      ",
  1660. X    "subalert(2):   ",
  1661. X    "emergency(3):  ",
  1662. X    "error(4):      ",
  1663. X    "critical(5):   ",
  1664. X    "warning(6):    ",
  1665. X    "notice(7):     ",
  1666. X    "information(8):",
  1667. X    "debug(9):      ",
  1668. X    "unknown level: "
  1669. X    };
  1670. X#else
  1671. X    static char *levelnames[] = {
  1672. X    "emergency(0):  ",
  1673. X    "alert(1):      ",
  1674. X    "critical(2):   ",
  1675. X    "error(3):      ",
  1676. X    "warning(4):    ",
  1677. X    "notice(5):     ",
  1678. X    "information(6):",
  1679. X    "debug(7):      ",
  1680. X    "unknown level: "
  1681. X    };
  1682. X#endif
  1683. X
  1684. X    if ((priority < 0) || (priority >= sizeof(levelnames)/sizeof(char *))) {
  1685. X    priority = sizeof(levelnames) / sizeof(char *) - 1;
  1686. X    }
  1687. X
  1688. X    /*
  1689. X     * Print the message
  1690. X     */
  1691. X    if (debug > 2) {
  1692. X    fprintf(stderr, "bootpd: %s ", levelnames[priority]);
  1693. X    fprintf(stderr, fmt, p0, p1, p2, p3, p4);
  1694. X    }
  1695. X#ifdef SYSLOG
  1696. X    syslog(priority, fmt, p0, p1, p2, p3, p4);
  1697. X#endif
  1698. X}
  1699. END_OF_FILE
  1700. if test 40573 -ne `wc -c <'bootpd.c'`; then
  1701.     echo shar: \"'bootpd.c'\" unpacked with wrong size!
  1702. fi
  1703. # end of 'bootpd.c'
  1704. fi
  1705. if test -f 'readfile.c' -a "${1}" != "-c" ; then 
  1706.   echo shar: Will not clobber existing file \"'readfile.c'\"
  1707. else
  1708. echo shar: Extracting \"'readfile.c'\" \(49372 characters\)
  1709. sed "s/^X//" >'readfile.c' <<'END_OF_FILE'
  1710. X#ifndef _BLURB_
  1711. X#define _BLURB_
  1712. X/************************************************************************
  1713. X          Copyright 1988, 1991 by Carnegie Mellon University
  1714. X
  1715. X                          All Rights Reserved
  1716. X
  1717. XPermission to use, copy, modify, and distribute this software and its
  1718. Xdocumentation for any purpose and without fee is hereby granted, provided
  1719. Xthat the above copyright notice appear in all copies and that both that
  1720. Xcopyright notice and this permission notice appear in supporting
  1721. Xdocumentation, and that the name of Carnegie Mellon University not be used
  1722. Xin advertising or publicity pertaining to distribution of the software
  1723. Xwithout specific, written prior permission.
  1724. X
  1725. XCARNEGIE MELLON UNIVERSITY DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS
  1726. XSOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS.
  1727. XIN NO EVENT SHALL CMU BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
  1728. XDAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
  1729. XPROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
  1730. XACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
  1731. XSOFTWARE.
  1732. X************************************************************************/
  1733. X#endif /* _BLURB_ */
  1734. X
  1735. X
  1736. X#ifndef lint
  1737. Xstatic char rcsid[] = "$Header: /afs/andrew.cmu.edu/netdev/src/cmu/bootp-public/RCS/readfile.c,v 1.3 1991/11/01 10:02:29 ww0n Exp ww0n $";
  1738. X#endif
  1739. X
  1740. X
  1741. X/*
  1742. X * bootpd configuration file reading code.
  1743. X *
  1744. X * The routines in this file deal with reading, interpreting, and storing
  1745. X * the information found in the bootpd configuration file (usually
  1746. X * /etc/bootptab).
  1747. X */
  1748. X
  1749. X
  1750. X#include <stdio.h>
  1751. X#ifdef SVR4
  1752. X#include <string.h>
  1753. X#else
  1754. X#include <strings.h>
  1755. X#endif
  1756. X#include <ctype.h>
  1757. X#include <sys/errno.h>
  1758. X#include <sys/types.h>
  1759. X#include <sys/stat.h>
  1760. X#include <sys/file.h>
  1761. X#include <sys/time.h>
  1762. X#include <netinet/in.h>
  1763. X#ifdef SYSLOG
  1764. X#include <syslog.h>
  1765. X#endif
  1766. X#include "bootp.h"
  1767. X#include "hash.h"
  1768. X#include "bootpd.h"
  1769. X
  1770. X#ifdef SVR4
  1771. X#define    bcopy(a,b,c)    memcpy(b,a,c)
  1772. X#define    bzero(p,l)    memset(p,0,l)
  1773. X#define    bcmp(a,b,c)    memcmp(a,b,c)
  1774. X#endif
  1775. X
  1776. X#define SUCCESS              0
  1777. X#define E_END_OF_ENTRY        (-1)
  1778. X#define E_SYNTAX_ERROR        (-2)
  1779. X#define E_UNKNOWN_SYMBOL    (-3)
  1780. X#define E_BAD_IPADDR        (-4)
  1781. X#define E_BAD_HADDR        (-5)
  1782. X#define E_BAD_SMASK        (-6)
  1783. X#define E_BAD_TIMEOFF        (-7)
  1784. X#define E_BAD_VM_COOKIE        (-8)
  1785. X#define E_BAD_HTYPE        (-9)
  1786. X#define E_BAD_BOOTSIZE           (-10)
  1787. X#define E_BAD_BOOT_SERVER      (-11)
  1788. X#define E_BAD_HOMEDIR           (-12)
  1789. X#define E_BAD_TFTPDIR           (-13)
  1790. X
  1791. X#define SYM_NULL          0
  1792. X#define SYM_BOOTFILE          1
  1793. X#define SYM_COOKIE_SERVER      2
  1794. X#define SYM_DOMAIN_SERVER      3
  1795. X#define SYM_GATEWAY          4
  1796. X#define SYM_HADDR          5
  1797. X#define SYM_HOMEDIR          6
  1798. X#define SYM_HTYPE          7
  1799. X#define SYM_IMPRESS_SERVER      8
  1800. X#define SYM_IPADDR          9
  1801. X#define SYM_LOG_SERVER         10
  1802. X#define SYM_LPR_SERVER         11
  1803. X#define SYM_NAME_SERVER         12
  1804. X#define SYM_RLP_SERVER         13
  1805. X#define SYM_SUBNET_MASK         14
  1806. X#define SYM_TIME_OFFSET         15
  1807. X#define SYM_TIME_SERVER         16
  1808. X#define SYM_VENDOR_MAGIC     17
  1809. X#define SYM_SIMILAR_ENTRY     18
  1810. X#define SYM_NAME_SWITCH         19
  1811. X#define SYM_BOOTSIZE         20
  1812. X#define SYM_BOOT_SERVER         22
  1813. X#define SYM_TFTPDIR         23
  1814. X
  1815. X#define OP_ADDITION          1    /* Operations on tags */
  1816. X#define OP_DELETION          2
  1817. X#define OP_BOOLEAN          3
  1818. X
  1819. X#define MAXINADDRS         16    /* Max size of an IP address list */
  1820. X#define MAXBUFLEN         64    /* Max temp buffer space */
  1821. X#define MAXENTRYLEN           2048    /* Max size of an entire entry */
  1822. X
  1823. XPRIVATE int process_entry();
  1824. XPRIVATE eval_symbol();
  1825. XPRIVATE boolean goodname();
  1826. XPRIVATE prs_inetaddr();
  1827. XPRIVATE int interp_byte();
  1828. X
  1829. X
  1830. X/*
  1831. X * Structure used to map a configuration-file symbol (such as "ds") to a
  1832. X * unique integer.
  1833. X */
  1834. X
  1835. Xstruct symbolmap {
  1836. X    char *symbol;
  1837. X    int symbolcode;
  1838. X};
  1839. X
  1840. X
  1841. Xstruct htypename {
  1842. X    char *name;
  1843. X    byte htype;
  1844. X};
  1845. X
  1846. X
  1847. XPRIVATE int nhosts;        /* Number of hosts (/w hw or IP address) */
  1848. XPRIVATE int nentries;        /* Total number of entries */
  1849. XPRIVATE long modtime = 0;    /* Last modification time of bootptab */
  1850. X
  1851. X
  1852. X/*
  1853. X * List of symbolic names used in the bootptab file.  The order and actual
  1854. X * values of the symbol codes (SYM_. . .) are unimportant, but they must
  1855. X * all be unique.
  1856. X */
  1857. X
  1858. XPRIVATE struct symbolmap symbol_list[] = {
  1859. X    { "bf", SYM_BOOTFILE    },
  1860. X    { "bs", SYM_BOOTSIZE    },
  1861. X    { "cs", SYM_COOKIE_SERVER    },
  1862. X    { "ds", SYM_DOMAIN_SERVER    },
  1863. X    { "gw", SYM_GATEWAY        },
  1864. X    { "ha", SYM_HADDR        },
  1865. X    { "hd", SYM_HOMEDIR        },
  1866. X    { "hn", SYM_NAME_SWITCH     },
  1867. X    { "ht", SYM_HTYPE        },
  1868. X    { "im", SYM_IMPRESS_SERVER    },
  1869. X    { "ip", SYM_IPADDR        },
  1870. X    { "lg", SYM_LOG_SERVER    },
  1871. X    { "lp", SYM_LPR_SERVER    },
  1872. X    { "ns", SYM_NAME_SERVER    },
  1873. X    { "rl", SYM_RLP_SERVER    },
  1874. X    { "sa", SYM_BOOT_SERVER    },
  1875. X    { "sm", SYM_SUBNET_MASK    },
  1876. X    { "tc", SYM_SIMILAR_ENTRY   },
  1877. X    { "td", SYM_TFTPDIR        },
  1878. X    { "to", SYM_TIME_OFFSET    },
  1879. X    { "ts", SYM_TIME_SERVER    },
  1880. X    { "vm", SYM_VENDOR_MAGIC    },
  1881. X};
  1882. X
  1883. X
  1884. X/*
  1885. X * List of symbolic names for hardware types.  Name translates into
  1886. X * hardware type code listed with it.  Names must begin with a letter
  1887. X * and must be all lowercase.  This is searched linearly, so put
  1888. X * commonly-used entries near the beginning.
  1889. X */
  1890. X
  1891. XPRIVATE struct htypename htnamemap[] = {
  1892. X    { "ethernet",   HTYPE_ETHERNET    },
  1893. X    { "ethernet3",  HTYPE_EXP_ETHERNET    },
  1894. X    { "ether",        HTYPE_ETHERNET    },
  1895. X    { "ether3",        HTYPE_EXP_ETHERNET    },
  1896. X    { "ieee802",    HTYPE_IEEE802    },
  1897. X    { "tr",        HTYPE_IEEE802    },
  1898. X    { "token-ring", HTYPE_IEEE802    },
  1899. X    { "pronet",        HTYPE_PRONET    },
  1900. X    { "chaos",        HTYPE_CHAOS        },
  1901. X    { "arcnet",        HTYPE_ARCNET    },
  1902. X    { "ax.25",        HTYPE_AX25        }
  1903. X};
  1904. X
  1905. X
  1906. X
  1907. X/*
  1908. X * Externals and forward declarations.
  1909. X */
  1910. X
  1911. Xextern char *malloc();
  1912. Xextern boolean iplookcmp();
  1913. Xextern int errno;
  1914. X
  1915. XPRIVATE char *smalloc();
  1916. XPRIVATE void fill_defaults();
  1917. XPRIVATE void del_string();
  1918. XPRIVATE void del_bindata();
  1919. XPRIVATE void del_iplist();
  1920. XPRIVATE void free_host();
  1921. XPRIVATE u_long get_u_long();
  1922. XPRIVATE void process_generic();
  1923. XPRIVATE char *get_string();
  1924. XPRIVATE struct shared_string *get_shared_string();
  1925. XPRIVATE void read_entry();
  1926. XPRIVATE boolean nullcmp();
  1927. XPRIVATE boolean nmcmp();
  1928. XPRIVATE boolean hwinscmp();
  1929. XPRIVATE void adjust();
  1930. XPRIVATE void eat_whitespace();
  1931. XPRIVATE void makelower();
  1932. XPRIVATE struct in_addr_list *get_addresses();
  1933. XPRIVATE byte *prs_haddr();
  1934. X
  1935. X
  1936. X
  1937. X
  1938. X
  1939. X/*
  1940. X * Read bootptab database file.  Avoid rereading the file if the
  1941. X * write date hasn't changed since the last time we read it.
  1942. X */
  1943. X
  1944. Xvoid readtab()
  1945. X{
  1946. X    struct host *hp;
  1947. X    FILE *fp;
  1948. X    struct stat st;
  1949. X    unsigned hashcode, buflen;
  1950. X    static char buffer[MAXENTRYLEN];
  1951. X#ifdef DEBUG
  1952. X    char timestr[26];
  1953. X#endif
  1954. X
  1955. X    /*
  1956. X     * Check the last modification time.
  1957. X     */
  1958. X    if (stat(bootptab, &st) < 0) {
  1959. X    report(LOG_ERR, "stat on \"%s\": %s\n", bootptab, get_errmsg());
  1960. X    return;
  1961. X    }
  1962. X#ifdef DEBUG
  1963. X    if (debug > 3) {
  1964. X    strcpy(timestr, ctime(&(st.st_mtime)));
  1965. X    report(LOG_INFO, "bootptab mtime is %s",
  1966. X           timestr);
  1967. X    }
  1968. X#endif
  1969. X    if (st.st_mtime == modtime && st.st_nlink) {
  1970. X    /*
  1971. X     * hasn't been modified or deleted yet.
  1972. X     */
  1973. X    return;
  1974. X    }
  1975. X    report(LOG_INFO, "reading %s\"%s\"\n",
  1976. X       (modtime != 0L) ? "new " : "",
  1977. X       bootptab);
  1978. X
  1979. X    /*
  1980. X     * Open bootptab file.
  1981. X     */
  1982. X    if ((fp = fopen(bootptab, "r")) == NULL) {
  1983. X    report(LOG_ERR, "error opening \"%s\": %s\n", bootptab, get_errmsg());
  1984. X    return;
  1985. X    }
  1986. X
  1987. X    /*
  1988. X     * Record file modification time.
  1989. X     */
  1990. X    if (fstat(fileno(fp), &st) < 0) {
  1991. X    report(LOG_ERR, "fstat: %s\n", get_errmsg());
  1992. X    fclose(fp);
  1993. X    return;
  1994. X    }
  1995. X    modtime = st.st_mtime;
  1996. X
  1997. X    /*
  1998. X     * Entirely erase all hash tables.
  1999. X     */
  2000. X    hash_Reset(hwhashtable, free_host);
  2001. X    hash_Reset(iphashtable, free_host);
  2002. X    hash_Reset(nmhashtable, free_host);
  2003. X
  2004. X    nhosts = 0;
  2005. X    nentries = 0;
  2006. X    while (TRUE) {
  2007. X    buflen = sizeof(buffer);
  2008. X    read_entry(fp, buffer, &buflen);
  2009. X    if (buflen == 0) {        /* More entries? */
  2010. X        break;
  2011. X    }
  2012. X    hp = (struct host *) smalloc(sizeof(struct host));
  2013. X
  2014. X    /*
  2015. X     * Get individual info
  2016. X     */
  2017. X    hp->flags.vm_auto = TRUE;
  2018. X    bcopy(vm_rfc1048, hp->vm_cookie, 4);
  2019. X    if (process_entry(hp, buffer) < 0) {
  2020. X        free_host(hp);
  2021. X        continue;
  2022. X    }
  2023. X
  2024. X    if ((hp->flags.htype && hp->flags.haddr) || hp->flags.iaddr) {
  2025. X        nhosts++;
  2026. X    }
  2027. X    if (hp->flags.htype && hp->flags.haddr) {
  2028. X        hashcode = hash_HashFunction(hp->haddr, haddrlength(hp->htype));
  2029. X        if (hash_Insert(hwhashtable, hashcode, hwinscmp, hp, hp) < 0) {
  2030. X        report(LOG_WARNING, "duplicate %s address: %s\n",
  2031. X            netname(hp->htype),
  2032. X            haddrtoa(hp->haddr, hp->htype));
  2033. X        free_host(hp);
  2034. X        continue;
  2035. X        }
  2036. X    }
  2037. X    if (hp->flags.iaddr) {
  2038. X        hashcode = hash_HashFunction(&(hp->iaddr.s_addr), 4);
  2039. X        if (hash_Insert(iphashtable, hashcode, nullcmp, hp, hp) < 0) {
  2040. X        report(LOG_ERR,
  2041. X            "hash_Insert() failed on IP address insertion\n");
  2042. X        }
  2043. X    }
  2044. X
  2045. X    hashcode = hash_HashFunction(hp->hostname->string,
  2046. X                     strlen(hp->hostname->string));
  2047. X    if (hash_Insert(nmhashtable, hashcode, nullcmp, hp->hostname->string, hp) < 0) {
  2048. X        report(LOG_ERR,
  2049. X           "hash_Insert() failed on insertion of hostname: \"%s\"\n",
  2050. X           hp->hostname->string);
  2051. X    }
  2052. X    nentries++;
  2053. X    }
  2054. X
  2055. Xdone:
  2056. X    fclose(fp);
  2057. X    report(LOG_INFO, "read %d entries (%d hosts) from \"%s\"\n",
  2058. X        nentries, nhosts, bootptab);
  2059. X    return;
  2060. X}
  2061. X
  2062. X
  2063. X
  2064. X/*
  2065. X * Read an entire host entry from the file pointed to by "fp" and insert it
  2066. X * into the memory pointed to by "buffer".  Leading whitespace and comments
  2067. X * starting with "#" are ignored (removed).  Backslashes (\) always quote
  2068. X * the next character except that newlines preceeded by a backslash cause
  2069. X * line-continuation onto the next line.  The entry is terminated by a
  2070. X * newline character which is not preceeded by a backslash.  Sequences
  2071. X * surrounded by double quotes are taken literally (including newlines, but
  2072. X * not backslashes).
  2073. X *
  2074. X * The "bufsiz" parameter points to an unsigned int which specifies the
  2075. X * maximum permitted buffer size.  Upon return, this value will be replaced
  2076. X * with the actual length of the entry (not including the null terminator).
  2077. X *
  2078. X * This code is a little scary. . . .  I don't like using gotos in C
  2079. X * either, but I first wrote this as an FSM diagram and gotos seemed like
  2080. X * the easiest way to implement it.  Maybe later I'll clean it up.
  2081. X */
  2082. X
  2083. XPRIVATE void read_entry(fp, buffer, bufsiz)
  2084. XFILE *fp;
  2085. Xchar *buffer;
  2086. Xunsigned *bufsiz;
  2087. X{
  2088. X    int c, length;
  2089. X
  2090. X    length = 0;
  2091. X
  2092. X    /*
  2093. X     * Eat whitespace, blank lines, and comment lines.
  2094. X     */
  2095. Xtop:
  2096. X    c = fgetc(fp);
  2097. X    if (c < 0) {
  2098. X    goto done;            /* Exit if end-of-file */
  2099. X    }
  2100. X    if (isspace(c)) {
  2101. X    goto top;            /* Skip over whitespace */
  2102. X    }
  2103. X    if (c == '#') {
  2104. X    while (TRUE) {            /* Eat comments after # */
  2105. X        c = fgetc(fp);
  2106. X        if (c < 0) {
  2107. X        goto done;        /* Exit if end-of-file */
  2108. X        }
  2109. X        if (c == '\n') {
  2110. X        goto top;        /* Try to read the next line */
  2111. X        }
  2112. X    }
  2113. X    }
  2114. X    ungetc(c, fp);    /* Other character, push it back to reprocess it */
  2115. X
  2116. X
  2117. X    /*
  2118. X     * Now we're actually reading a data entry.  Get each character and
  2119. X     * assemble it into the data buffer, processing special characters like
  2120. X     * double quotes (") and backslashes (\).
  2121. X     */
  2122. X
  2123. Xmainloop:
  2124. X    c = fgetc(fp);
  2125. X    switch (c) {
  2126. X    case EOF:        
  2127. X    case '\n':
  2128. X        goto done;            /* Exit on EOF or newline */
  2129. X    case '\\':
  2130. X        c = fgetc(fp);        /* Backslash, read a new character */
  2131. X        if (c < 0) {
  2132. X        goto done;        /* Exit on EOF */
  2133. X        }
  2134. X        *buffer++ = c;        /* Store the literal character */
  2135. X        length++;
  2136. X        if (length < *bufsiz - 1) {
  2137. X        goto mainloop;
  2138. X        } else {
  2139. X        goto done;
  2140. X        }
  2141. X    case '"':
  2142. X        *buffer++ = '"';        /* Store double-quote */
  2143. X        length++;
  2144. X        if (length >= *bufsiz - 1) {
  2145. X        goto done;
  2146. X        }
  2147. X        while (TRUE) {        /* Special quote processing loop */
  2148. X        c = fgetc(fp);
  2149. X        switch (c) {
  2150. X            case EOF:
  2151. X            goto done;        /* Exit on EOF . . . */
  2152. X            case '"':
  2153. X            *buffer++ = '"';    /* Store matching quote */
  2154. X            length++;
  2155. X            if (length < *bufsiz - 1) {
  2156. X                goto mainloop;    /* And continue main loop */
  2157. X            } else {
  2158. X                goto done;
  2159. X            }
  2160. X            case '\\':
  2161. X            if ((c = fgetc(fp)) < 0) {    /* Backslash */
  2162. X                goto done;            /* EOF. . . .*/
  2163. X            }            /* else fall through */
  2164. X            default:
  2165. X            *buffer++ = c;        /* Other character, store it */
  2166. X            length++;
  2167. X            if (length >= *bufsiz - 1) {
  2168. X                goto done;
  2169. X            }
  2170. X        }
  2171. X        }
  2172. X    case ':':
  2173. X        *buffer++ = c;        /* Store colons */
  2174. X        length++;
  2175. X        if (length >= *bufsiz - 1) {
  2176. X        goto done;
  2177. X        }
  2178. X
  2179. X        do {            /* But remove whitespace after them */
  2180. X        c = fgetc(fp);
  2181. X        if ((c < 0) || (c == '\n')) {
  2182. X            goto done;
  2183. X        }
  2184. X        } while (isspace(c));    /* Skip whitespace */
  2185. X
  2186. X        if (c == '\\') {        /* Backslash quotes next character */
  2187. X        c = fgetc(fp);
  2188. X        if (c < 0) {
  2189. X            goto done;
  2190. X        }
  2191. X        if (c == '\n') {
  2192. X            goto top;        /* Backslash-newline continuation */
  2193. X        }
  2194. X        }
  2195. X        /* fall through if "other" character */
  2196. X    default:
  2197. X        *buffer++ = c;        /* Store other characters */
  2198. X        length++;
  2199. X        if (length >= *bufsiz - 1) {
  2200. X        goto done;
  2201. X        }
  2202. X    }
  2203. X    goto mainloop;            /* Keep going */
  2204. X
  2205. Xdone:
  2206. X    *buffer = '\0';            /* Terminate string */
  2207. X    *bufsiz = length;            /* Tell the caller its length */
  2208. X}
  2209. X
  2210. X
  2211. X
  2212. X/*
  2213. X * Parse out all the various tags and parameters in the host entry pointed
  2214. X * to by "src".  Stuff all the data into the appropriate fields of the
  2215. X * host structure pointed to by "host".  If there is any problem with the
  2216. X * entry, an error message is reported via report(), no further processing
  2217. X * is done, and -1 is returned.  Successful calls return 0.
  2218. X *
  2219. X * (Some errors probably shouldn't be so completely fatal. . . .)
  2220. X */
  2221. X
  2222. XPRIVATE int process_entry(host, src)
  2223. Xstruct host *host;
  2224. Xchar *src;
  2225. X{
  2226. X    int retval;
  2227. X
  2228. X    if (!host || *src == '\0') {
  2229. X    return -1;
  2230. X    }
  2231. X    host->hostname = get_shared_string(&src);
  2232. X    if (!goodname(host->hostname->string)) {
  2233. X    report(LOG_ERR, "bad hostname: \"%s\"\n", host->hostname->string);
  2234. X    del_string(host->hostname);
  2235. X    return -1;
  2236. X    }
  2237. X    adjust(&src);
  2238. X    while (TRUE) {
  2239. X     retval = eval_symbol(&src, host);
  2240. X    switch (retval) {
  2241. X        case SUCCESS:
  2242. X        break;
  2243. X        case E_SYNTAX_ERROR:
  2244. X        report(LOG_ERR, "syntax error in entry for host \"%s\"\n",
  2245. X                host->hostname->string);
  2246. X        return -1;
  2247. X        case E_UNKNOWN_SYMBOL:
  2248. X        report(LOG_ERR, "unknown symbol in entry for host \"%s\"\n",
  2249. X                host->hostname->string);
  2250. X        return -1;
  2251. X        case E_BAD_IPADDR:
  2252. X        report(LOG_ERR, "bad IP address for host \"%s\"\n",
  2253. X                host->hostname->string);
  2254. X        return -1;
  2255. X        case E_BAD_HADDR:
  2256. X        report(LOG_ERR, "bad hardware address for host \"%s\"\n",
  2257. X                host->hostname->string);
  2258. X        return -1;
  2259. X        case E_BAD_SMASK:
  2260. X        report(LOG_ERR, "bad subnet mask for host \"%s\"\n",
  2261. X                host->hostname->string);
  2262. X        return -1;
  2263. X        case E_BAD_TIMEOFF:
  2264. X        report(LOG_ERR, "bad time offset for host \"%s\"\n",
  2265. X                host->hostname->string);
  2266. X        return -1;
  2267. X        case E_BAD_VM_COOKIE:
  2268. X        report(LOG_ERR, "bad vendor magic cookie for host \"%s\"\n",
  2269. X                host->hostname->string);
  2270. X        return -1;
  2271. X        case E_BAD_HOMEDIR:
  2272. X        report(LOG_ERR, "bad home directory for host \"%s\"\n",
  2273. X                host->hostname->string);
  2274. X        return -1;
  2275. X        case E_BAD_TFTPDIR:
  2276. X        report(LOG_ERR, "bad TFTP directory for host \"%s\"\n",
  2277. X                host->hostname->string);
  2278. X        return -1;
  2279. X        case E_BAD_BOOT_SERVER:
  2280. X        report(LOG_ERR, "bad boot server IP address for host \"%s\"\n",
  2281. X                host->hostname->string);
  2282. X        return -1;
  2283. X        case E_END_OF_ENTRY:
  2284. X        default:
  2285. X#if 0
  2286. X        /*
  2287. X         * For now, don't try to make-up a subnet mask if one
  2288. X         * wasn't specified.
  2289. X         *
  2290. X         * This algorithm is also not entirely correct.
  2291. X         */
  2292. X        if (!(hp->flags.subnet_mask)) {
  2293. X            /*
  2294. X             * Try to deduce the subnet mask from the network class
  2295. X             */
  2296. X            value = (ntohl(value) >> 30) & 0x03;
  2297. X            switch (value) {
  2298. X            case 0:
  2299. X            case 1:
  2300. X                hp->subnet_mask.s_addr = htonl(0xFF000000L);
  2301. X                break;
  2302. X            case 2:
  2303. X                hp->subnet_mask.s_addr = htonl(0xFFFF0000L);
  2304. X                break;
  2305. X            case 3:
  2306. X                hp->subnet_mask.s_addr = htonl(0xFFFFFF00L);
  2307. X                break;
  2308. X            }
  2309. X            hp->flags.subnet_mask = TRUE;
  2310. X        }
  2311. X#endif
  2312. X        /*
  2313. X         * And now we're done with this entry
  2314. X         */
  2315. X        return 0;
  2316. X    }
  2317. X    adjust(&src);
  2318. X    }
  2319. X}
  2320. X
  2321. X
  2322. X
  2323. X/*
  2324. X * Evaluate the two-character tag symbol pointed to by "symbol" and place
  2325. X * the data in the structure pointed to by "hp".  The pointer pointed to
  2326. X * by "symbol" is updated to point past the source string (but may not
  2327. X * point to the next tag entry).
  2328. X *
  2329. X * Obviously, this need a few more comments. . . .
  2330. X */
  2331. X
  2332. XPRIVATE eval_symbol(symbol, hp)
  2333. Xchar **symbol;
  2334. Xstruct host *hp;
  2335. X{
  2336. X    char tmpstr[MAXSTRINGLEN];
  2337. X    byte *tmphaddr, *ustr;
  2338. X    struct shared_string *ss;
  2339. X    struct symbolmap *symbolptr;
  2340. X    u_long value;
  2341. X    long timeoff;
  2342. X    int i, numsymbols;
  2343. X    unsigned len;
  2344. X    int optype;        /* Indicates boolean, addition, or deletion */
  2345. X
  2346. X    if ((*symbol)[0] == '\0') {
  2347. X    return E_END_OF_ENTRY;
  2348. X    }
  2349. X    if ((*symbol)[0] == ':') {
  2350. X    return SUCCESS;
  2351. X    }
  2352. X    if ((*symbol)[0] == 'T') {            /* generic symbol */
  2353. X    (*symbol)++;
  2354. X    value = get_u_long(symbol);
  2355. X    eat_whitespace(symbol);
  2356. X    if ((*symbol)[0] != '=') {
  2357. X        return E_SYNTAX_ERROR;
  2358. X    }
  2359. X    (*symbol)++;
  2360. X    if (!(hp->generic)) {
  2361. X        hp->generic = (struct shared_bindata *)
  2362. X                smalloc(sizeof(struct shared_bindata));
  2363. X    }
  2364. X    process_generic(symbol, &(hp->generic), (byte) (value & 0xFF));
  2365. X    hp->flags.generic = TRUE;
  2366. X    return SUCCESS;
  2367. X    }
  2368. X
  2369. X    eat_whitespace(symbol);
  2370. X
  2371. X    /*
  2372. X     * Determine the type of operation to be done on this symbol
  2373. X     */
  2374. X    switch ((*symbol)[2]) {
  2375. X    case '=':
  2376. X        optype = OP_ADDITION;
  2377. X        break;
  2378. X    case '@':
  2379. X        optype = OP_DELETION;
  2380. X        break;
  2381. X    case ':':
  2382. X    case '\0':
  2383. X        optype = OP_BOOLEAN;
  2384. X        break;
  2385. X    default:
  2386. X        return E_SYNTAX_ERROR;
  2387. X    }
  2388. X
  2389. X    symbolptr = symbol_list;
  2390. X    numsymbols = sizeof(symbol_list) / sizeof(struct symbolmap);
  2391. X    for (i = 0; i < numsymbols; i++) {
  2392. X    if (((symbolptr->symbol)[0] == (*symbol)[0]) &&
  2393. X       ((symbolptr->symbol)[1] == (*symbol)[1])) {
  2394. X        break;
  2395. X    }
  2396. X    symbolptr++;
  2397. X    }
  2398. X    if (i >= numsymbols) {
  2399. X    return E_UNKNOWN_SYMBOL;
  2400. X    }
  2401. X
  2402. X    /*
  2403. X     * Skip past the = or @ character (to point to the data) if this
  2404. X     * isn't a boolean operation.  For boolean operations, just skip
  2405. X     * over the two-character tag symbol (and nothing else. . . .).
  2406. X     */
  2407. X    (*symbol) += (optype == OP_BOOLEAN) ? 2 : 3;
  2408. X
  2409. X    switch (symbolptr->symbolcode) {
  2410. X    case SYM_BOOTFILE:
  2411. X        switch (optype) {
  2412. X        case OP_ADDITION:
  2413. X            if (ss = get_shared_string(symbol)) {
  2414. X            if (hp->bootfile) {
  2415. X                del_string(hp->bootfile);
  2416. X            }
  2417. X            hp->bootfile = ss;
  2418. X            hp->flags.bootfile = TRUE;
  2419. X            }
  2420. X            break;
  2421. X        case OP_DELETION:
  2422. X            if (hp->bootfile) {
  2423. X            del_string(hp->bootfile);
  2424. X            }
  2425. X            hp->bootfile = NULL;
  2426. X            hp->flags.bootfile = FALSE;
  2427. X            break;
  2428. X        case OP_BOOLEAN:
  2429. X            return E_SYNTAX_ERROR;
  2430. X        }
  2431. X        break;
  2432. X
  2433. X    case SYM_COOKIE_SERVER:
  2434. X        switch (optype) {
  2435. X        case OP_ADDITION:
  2436. X            if (hp->flags.cookie_server && hp->cookie_server) {
  2437. X            del_iplist(hp->cookie_server);
  2438. X            }
  2439. X            hp->cookie_server = get_addresses(symbol);
  2440. X            hp->flags.cookie_server = TRUE;
  2441. X            break;
  2442. X        case OP_DELETION:
  2443. X            if (hp->flags.cookie_server && hp->cookie_server) {
  2444. X            del_iplist(hp->cookie_server);
  2445. X            }
  2446. X            hp->cookie_server = NULL;
  2447. X            hp->flags.cookie_server = FALSE;
  2448. X            break;
  2449. X        case OP_BOOLEAN:
  2450. X            return E_SYNTAX_ERROR;
  2451. X        }
  2452. X        break;
  2453. X
  2454. X    case SYM_DOMAIN_SERVER:
  2455. X        switch (optype) {
  2456. X        case OP_ADDITION:
  2457. X            if (hp->flags.domain_server && hp->domain_server) {
  2458. X            del_iplist(hp->domain_server);
  2459. X            }
  2460. X            hp->domain_server = get_addresses(symbol);
  2461. X            hp->flags.domain_server = TRUE;
  2462. X            break;
  2463. X        case OP_DELETION:
  2464. X            if (hp->flags.domain_server && hp->domain_server) {
  2465. X            del_iplist(hp->domain_server);
  2466. X            }
  2467. X            hp->domain_server = NULL;
  2468. X            hp->flags.domain_server = FALSE;
  2469. X            break;
  2470. X        case OP_BOOLEAN:
  2471. X            return E_SYNTAX_ERROR;
  2472. X        }
  2473. X        break;
  2474. X
  2475. X    case SYM_GATEWAY:
  2476. X        switch (optype) {
  2477. X        case OP_ADDITION:
  2478. X            if (hp->flags.gateway && hp->gateway) {
  2479. X            del_iplist(hp->gateway);
  2480. X            }
  2481. X            hp->gateway = get_addresses(symbol);
  2482. X            hp->flags.gateway = TRUE;
  2483. X            break;
  2484. X        case OP_DELETION:
  2485. X            if (hp->flags.gateway && hp->gateway) {
  2486. X            del_iplist(hp->gateway);
  2487. X            }
  2488. X            hp->gateway = NULL;
  2489. X            hp->flags.gateway = FALSE;
  2490. X            break;
  2491. X        case OP_BOOLEAN:
  2492. X            return E_SYNTAX_ERROR;
  2493. X        }
  2494. X        break;
  2495. X
  2496. X    case SYM_HADDR:
  2497. X        switch (optype) {
  2498. X        case OP_ADDITION:
  2499. X            if (hp->flags.htype && hp->htype &&
  2500. X            (tmphaddr = prs_haddr(symbol, hp->htype))) {
  2501. X            bcopy(tmphaddr, hp->haddr, haddrlength(hp->htype));
  2502. X            hp->flags.haddr = TRUE;
  2503. X            } else {
  2504. X            return E_BAD_HADDR;
  2505. X            }
  2506. X            break;
  2507. X        case OP_DELETION:
  2508. X            hp->flags.haddr = FALSE;
  2509. X            break;
  2510. X        case OP_BOOLEAN:
  2511. X            return E_SYNTAX_ERROR;
  2512. X        }
  2513. X        break;
  2514. X
  2515. X    case SYM_HOMEDIR:
  2516. X        switch (optype) {
  2517. X        case OP_ADDITION:
  2518. X            if (ss = get_shared_string(symbol)) {
  2519. X            if ((ss->string)[0] == '/') {
  2520. X                if (hp->homedir) {
  2521. X                del_string(hp->homedir);
  2522. X                }
  2523. X                hp->homedir = ss;
  2524. X                hp->flags.homedir = TRUE;
  2525. X            } else {
  2526. X                return E_BAD_HOMEDIR;
  2527. X            }
  2528. X            } else {
  2529. X            return E_BAD_HOMEDIR;
  2530. X            }
  2531. X            break;
  2532. X        case OP_DELETION:
  2533. X            if (hp->homedir) {
  2534. X            del_string(hp->homedir);
  2535. X            }
  2536. X            hp->homedir = NULL;
  2537. X            hp->flags.homedir = FALSE;
  2538. X            break;
  2539. X        case OP_BOOLEAN:
  2540. X            return E_SYNTAX_ERROR;
  2541. X        }
  2542. X        break;
  2543. X
  2544. X    case SYM_HTYPE:
  2545. X        switch (optype) {
  2546. X        case OP_ADDITION:
  2547. X            value = 0L;            /* Assume an illegal value */
  2548. X            eat_whitespace(symbol);
  2549. X            if (isdigit(**symbol)) {
  2550. X            value = get_u_long(symbol);
  2551. X            } else {
  2552. X            len = sizeof(tmpstr);
  2553. X            (void) get_string(symbol, tmpstr, &len);
  2554. X            makelower(tmpstr);
  2555. X            numsymbols = sizeof(htnamemap) /
  2556. X                     sizeof(struct htypename);
  2557. X            for (i = 0; i < numsymbols; i++) {
  2558. X                if (!strcmp(htnamemap[i].name, tmpstr)) {
  2559. X                break;
  2560. X                }
  2561. X            }
  2562. X            if (i < numsymbols) {
  2563. X                value = htnamemap[i].htype;
  2564. X            }
  2565. X            }
  2566. X            if ((value < 0) || (value >= hwinfocnt)) {
  2567. X            return E_BAD_HTYPE;
  2568. X            }
  2569. X            hp->htype = (byte) (value & 0xFF);
  2570. X            hp->flags.htype = TRUE;
  2571. X            break;
  2572. X        case OP_DELETION:
  2573. X            hp->flags.htype = FALSE;
  2574. X            break;
  2575. X        case OP_BOOLEAN:
  2576. X            return E_SYNTAX_ERROR;
  2577. X        }
  2578. X        break;
  2579. X
  2580. X    case SYM_IMPRESS_SERVER:
  2581. X        switch (optype) {
  2582. X        case OP_ADDITION:
  2583. X            if (hp->flags.impress_server && hp->impress_server) {
  2584. X            del_iplist(hp->impress_server);
  2585. X            }
  2586. X            hp->impress_server = get_addresses(symbol);
  2587. X            hp->flags.impress_server = TRUE;
  2588. X            break;
  2589. X        case OP_DELETION:
  2590. X            if (hp->flags.impress_server && hp->impress_server) {
  2591. X            del_iplist(hp->impress_server);
  2592. X            }
  2593. X            hp->impress_server = NULL;
  2594. X            hp->flags.impress_server = FALSE;
  2595. X            break;
  2596. X        case OP_BOOLEAN:
  2597. X            return E_SYNTAX_ERROR;
  2598. X        }
  2599. X        break;
  2600. X
  2601. X    case SYM_IPADDR:
  2602. X        switch (optype) {
  2603. X        case OP_ADDITION:
  2604. X            if (prs_inetaddr(symbol, &value) < 0) {
  2605. X            return E_BAD_IPADDR;
  2606. X            } else {
  2607. X            hp->iaddr.s_addr = value;
  2608. X            hp->flags.iaddr = TRUE;
  2609. X            }
  2610. X            break;
  2611. X        case OP_DELETION:
  2612. X            hp->flags.iaddr = FALSE;
  2613. X            break;
  2614. X        case OP_BOOLEAN:
  2615. X            return E_SYNTAX_ERROR;
  2616. X        }
  2617. X        break;
  2618. X
  2619. X    case SYM_LOG_SERVER:
  2620. X        switch (optype) {
  2621. X        case OP_ADDITION:
  2622. X            if (hp->flags.log_server && hp->log_server) {
  2623. X            del_iplist(hp->log_server);
  2624. X            }
  2625. X            hp->log_server = get_addresses(symbol);
  2626. X            hp->flags.log_server = TRUE;
  2627. X            break;
  2628. X        case OP_DELETION:
  2629. X            if (hp->flags.log_server && hp->log_server) {
  2630. X            del_iplist(hp->log_server);
  2631. X            }
  2632. X            hp->log_server = NULL;
  2633. X            hp->flags.log_server = FALSE;
  2634. X            break;
  2635. X        case OP_BOOLEAN:
  2636. X            return E_SYNTAX_ERROR;
  2637. X        }
  2638. X        break;
  2639. X
  2640. X    case SYM_LPR_SERVER:
  2641. X        switch (optype) {
  2642. X        case OP_ADDITION:
  2643. X            if (hp->flags.lpr_server && hp->lpr_server) {
  2644. X            del_iplist(hp->lpr_server);
  2645. X            }
  2646. X            hp->lpr_server = get_addresses(symbol);
  2647. X            hp->flags.lpr_server = TRUE;
  2648. X            break;
  2649. X        case OP_DELETION:
  2650. X            if (hp->flags.lpr_server && hp->lpr_server) {
  2651. X            del_iplist(hp->lpr_server);
  2652. X            }
  2653. X            hp->lpr_server = NULL;
  2654. X            hp->flags.lpr_server = FALSE;
  2655. X            break;
  2656. X        case OP_BOOLEAN:
  2657. X            return E_SYNTAX_ERROR;
  2658. X        }
  2659. X        break;
  2660. X
  2661. X    case SYM_NAME_SERVER:
  2662. X        switch (optype) {
  2663. X        case OP_ADDITION:
  2664. X            if (hp->flags.name_server && hp->name_server) {
  2665. X            del_iplist(hp->name_server);
  2666. X            }
  2667. X            hp->name_server = get_addresses(symbol);
  2668. X            hp->flags.name_server = TRUE;
  2669. X            break;
  2670. X        case OP_DELETION:
  2671. X            if (hp->flags.name_server && hp->name_server) {
  2672. X            del_iplist(hp->name_server);
  2673. X            }
  2674. X            hp->name_server = NULL;
  2675. X            hp->flags.name_server = FALSE;
  2676. X            break;
  2677. X        case OP_BOOLEAN:
  2678. X            return E_SYNTAX_ERROR;
  2679. X        }
  2680. X        break;
  2681. X
  2682. X    case SYM_RLP_SERVER:
  2683. X        switch (optype) {
  2684. X        case OP_ADDITION:
  2685. X            if (hp->flags.rlp_server && hp->rlp_server) {
  2686. X            del_iplist(hp->rlp_server);
  2687. X            }
  2688. X            hp->rlp_server = get_addresses(symbol);
  2689. X            hp->flags.rlp_server = TRUE;
  2690. X            break;
  2691. X        case OP_DELETION:
  2692. X            if (hp->flags.rlp_server && hp->rlp_server) {
  2693. X            del_iplist(hp->rlp_server);
  2694. X            }
  2695. X            hp->rlp_server = NULL;
  2696. X            hp->flags.rlp_server = FALSE;
  2697. X            break;
  2698. X        case OP_BOOLEAN:
  2699. X            return E_SYNTAX_ERROR;
  2700. X        }
  2701. X        break;
  2702. X
  2703. X    case SYM_SUBNET_MASK:
  2704. X        switch (optype) {
  2705. X        case OP_ADDITION:
  2706. X            if (prs_inetaddr(symbol, &value) < 0) {
  2707. X            return E_BAD_SMASK;
  2708. X            } else {
  2709. X            hp->subnet_mask.s_addr = value;
  2710. X            hp->flags.subnet_mask = TRUE;
  2711. X            }
  2712. X            break;
  2713. X        case OP_DELETION:
  2714. X            hp->flags.subnet_mask = FALSE;
  2715. X            break;
  2716. X        case OP_BOOLEAN:
  2717. X            return E_SYNTAX_ERROR;
  2718. X        }
  2719. X        break;
  2720. X
  2721. X    case SYM_TIME_OFFSET:
  2722. X        switch (optype) {
  2723. X        case OP_ADDITION:
  2724. X            len = sizeof(tmpstr);
  2725. X            (void) get_string(symbol, tmpstr, &len);
  2726. X            if (!strncmp(tmpstr, "auto", 4)) {
  2727. X            hp->flags.timeoff_auto = TRUE;
  2728. X            hp->flags.time_offset = TRUE;
  2729. X            } else {
  2730. X            if (sscanf(tmpstr, "%ld", &timeoff) != 1) {
  2731. X                return E_BAD_TIMEOFF;
  2732. X            } else {
  2733. X                hp->time_offset = timeoff;
  2734. X                hp->flags.timeoff_auto = FALSE;
  2735. X                hp->flags.time_offset = TRUE;
  2736. X            }
  2737. X            }
  2738. X            break;
  2739. X        case OP_DELETION:
  2740. X            hp->flags.time_offset = FALSE;
  2741. X            break;
  2742. X        case OP_BOOLEAN:
  2743. X            return E_SYNTAX_ERROR;
  2744. X        }
  2745. X        break;
  2746. X
  2747. X    case SYM_TIME_SERVER:
  2748. X        switch (optype) {
  2749. X        case OP_ADDITION:
  2750. X            if (hp->flags.time_server && hp->time_server) {
  2751. X            del_iplist(hp->time_server);
  2752. X            }
  2753. X            hp->time_server = get_addresses(symbol);
  2754. X            hp->flags.time_server = TRUE;
  2755. X            break;
  2756. X        case OP_DELETION:
  2757. X            if (hp->flags.time_server && hp->time_server) {
  2758. X            del_iplist(hp->time_server);
  2759. X            }
  2760. X            hp->time_server = NULL;
  2761. X            hp->flags.time_server = FALSE;
  2762. X            break;
  2763. X        case OP_BOOLEAN:
  2764. X            return E_SYNTAX_ERROR;
  2765. X        }
  2766. X        break;
  2767. X
  2768. X    case SYM_VENDOR_MAGIC:
  2769. X        switch (optype) {
  2770. X        case OP_ADDITION:
  2771. X            if (!strncmp(*symbol, "auto", 4)) {
  2772. X            hp->flags.vm_auto = TRUE;    /* Make it auto */
  2773. X            } else if (!strncmp(*symbol, "rfc1048", 7) ||
  2774. X                   !strncmp(*symbol, "rfc1084", 7)) {
  2775. X            hp->flags.vm_auto = FALSE;    /* Make it manual */
  2776. X            bcopy(vm_rfc1048, hp->vm_cookie, 4);
  2777. X            } else if (!strncmp(*symbol, "cmu", 3)) {
  2778. X            hp->flags.vm_auto = FALSE;    /* Make it manual */
  2779. X            bcopy(vm_cmu, hp->vm_cookie, 4);
  2780. X            } else {
  2781. X            if (prs_inetaddr(symbol, &value) < 0) {
  2782. X                return E_BAD_VM_COOKIE;
  2783. X            }
  2784. X            hp->flags.vm_auto = FALSE;    /* Make it manual */
  2785. X            ustr = hp->vm_cookie;
  2786. X            bcopy((char*)&value, ustr, sizeof(long));
  2787. X            }
  2788. X            hp->flags.vendor_magic = TRUE;
  2789. X            break;
  2790. X        case OP_DELETION:
  2791. X            hp->flags.vendor_magic = FALSE;
  2792. X            break;
  2793. X        case OP_BOOLEAN:
  2794. X            hp->flags.vm_auto = TRUE;
  2795. X            hp->flags.vendor_magic = TRUE;
  2796. X            break;
  2797. X        }
  2798. X        break;
  2799. X
  2800. X    case SYM_SIMILAR_ENTRY:
  2801. X        switch (optype) {
  2802. X        case OP_ADDITION:
  2803. X            fill_defaults(hp, symbol);
  2804. X            break;
  2805. X        default:
  2806. X            return E_SYNTAX_ERROR;
  2807. X        }
  2808. X        break;
  2809. X
  2810. X    case SYM_NAME_SWITCH:
  2811. X        switch (optype) {
  2812. X        case OP_ADDITION:
  2813. X            return E_SYNTAX_ERROR;
  2814. X        case OP_DELETION:
  2815. X            hp->flags.send_name = FALSE;
  2816. X            hp->flags.name_switch = FALSE;
  2817. X            break;
  2818. X        case OP_BOOLEAN:
  2819. X            hp->flags.send_name = TRUE;
  2820. X            hp->flags.name_switch = TRUE;
  2821. X            break;
  2822. X        }
  2823. X        break;
  2824. X
  2825. X    case SYM_BOOTSIZE:
  2826. X        switch (optype) {
  2827. X        case OP_ADDITION:
  2828. X            if (!strncmp(*symbol, "auto", 4)) {
  2829. X            hp->flags.bootsize = TRUE;
  2830. X            hp->flags.bootsize_auto = TRUE;
  2831. X            } else {
  2832. X            hp->bootsize = (unsigned int) get_u_long(symbol);
  2833. X            hp->flags.bootsize = TRUE;
  2834. X            hp->flags.bootsize_auto = FALSE;
  2835. X            }
  2836. X            break;
  2837. X        case OP_DELETION:
  2838. X            hp->flags.bootsize = FALSE;
  2839. X            break;
  2840. X        case OP_BOOLEAN:
  2841. X            hp->flags.bootsize = TRUE;
  2842. X            hp->flags.bootsize_auto = TRUE;
  2843. X            break;
  2844. X        }
  2845. X        break;
  2846. X
  2847. X    case SYM_BOOT_SERVER:
  2848. X        switch (optype) {
  2849. X        case OP_ADDITION:
  2850. X            if (prs_inetaddr(symbol, &value) < 0) {
  2851. X            return E_BAD_BOOT_SERVER;
  2852. X            } else {
  2853. X            hp->bootserver.s_addr = value;
  2854. X            hp->flags.bootserver = TRUE;
  2855. X            }
  2856. X            break;
  2857. X        case OP_DELETION:
  2858. X            hp->flags.bootserver = FALSE;
  2859. X            break;
  2860. X        case OP_BOOLEAN:
  2861. X            return E_SYNTAX_ERROR;
  2862. X        }
  2863. X        break;
  2864. X
  2865. X    case SYM_TFTPDIR:
  2866. X        switch (optype) {
  2867. X        case OP_ADDITION:
  2868. X            if (ss = get_shared_string(symbol)) {
  2869. X            if ((ss->string)[0] == '/') {
  2870. X                if (hp->tftpdir) {
  2871. X                del_string(hp->tftpdir);
  2872. X                }
  2873. X                hp->tftpdir = ss;
  2874. X                hp->flags.tftpdir = TRUE;
  2875. X            } else {
  2876. X                return E_BAD_TFTPDIR;
  2877. X            }
  2878. X            } else {
  2879. X            return E_BAD_TFTPDIR;
  2880. X            }
  2881. X            break;
  2882. X        case OP_DELETION:
  2883. X            if (hp->tftpdir) {
  2884. X            del_string(hp->tftpdir);
  2885. X            }
  2886. X            hp->tftpdir = NULL;
  2887. X            hp->flags.tftpdir = FALSE;
  2888. X            break;
  2889. X        case OP_BOOLEAN:
  2890. X            return E_SYNTAX_ERROR;
  2891. X        }
  2892. X        break;
  2893. X
  2894. X    default:
  2895. X        return E_UNKNOWN_SYMBOL;
  2896. X    }
  2897. X    return SUCCESS;
  2898. X}
  2899. X
  2900. X
  2901. X
  2902. X/*
  2903. X * Read a string from the buffer indirectly pointed to through "src" and
  2904. X * move it into the buffer pointed to by "dest".  A pointer to the maximum
  2905. X * allowable length of the string (including null-terminator) is passed as
  2906. X * "length".  The actual length of the string which was read is returned in
  2907. X * the unsigned integer pointed to by "length".  This value is the same as
  2908. X * that which would be returned by applying the strlen() function on the
  2909. X * destination string (i.e the terminating null is not counted as a
  2910. X * character).  Trailing whitespace is removed from the string.  For
  2911. X * convenience, the function returns the new value of "dest".
  2912. X *
  2913. X * The string is read until the maximum number of characters, an unquoted
  2914. X * colon (:), or a null character is read.  The return string in "dest" is
  2915. X * null-terminated.
  2916. X */
  2917. X
  2918. XPRIVATE char *get_string(src, dest, length)
  2919. Xchar **src, *dest;
  2920. Xunsigned *length;
  2921. X{
  2922. X    int n, len, quoteflag;
  2923. X
  2924. X    quoteflag = FALSE;
  2925. X    n = 0;
  2926. X    len = *length - 1;
  2927. X    while ((n < len) && (**src)) {
  2928. X    if (!quoteflag && (**src == ':')) {
  2929. X         break;
  2930. X    }
  2931. X    if (**src == '"') {
  2932. X        (*src)++;
  2933. X        quoteflag = !quoteflag;
  2934. X        continue;
  2935. X    }
  2936. X    if (**src == '\\') {
  2937. X        (*src)++;
  2938. X        if (! **src) {
  2939. X        break;
  2940. X        }
  2941. X    }
  2942. X    *dest++ = *(*src)++;
  2943. X    n++;
  2944. X    }
  2945. X
  2946. X    /*
  2947. X     * Remove that troublesome trailing whitespace. . .
  2948. X     */
  2949. X    while ((n > 0) && isspace(dest[-1])) {
  2950. X    dest--;
  2951. X    n--;
  2952. X    }
  2953. X
  2954. X    *dest = '\0';
  2955. X    *length = n;
  2956. X    return dest;
  2957. X}
  2958. X
  2959. X
  2960. X
  2961. X/*
  2962. X * Read the string indirectly pointed to by "src", update the caller's
  2963. X * pointer, and return a pointer to a malloc'ed shared_string structure
  2964. X * containing the string.
  2965. X *
  2966. X * The string is read using the same rules as get_string() above.
  2967. X */
  2968. X
  2969. XPRIVATE struct shared_string *get_shared_string(src)
  2970. Xchar **src;
  2971. X{
  2972. X    char retstring[MAXSTRINGLEN];
  2973. X    struct shared_string *s;
  2974. X    unsigned length;
  2975. X
  2976. X    length = sizeof(retstring);
  2977. X    (void) get_string(src, retstring, &length);
  2978. X
  2979. X    s = (struct shared_string *) smalloc(sizeof(struct shared_string)
  2980. X                     + length);
  2981. X    s->linkcount = 1;
  2982. X    strcpy(s->string, retstring);
  2983. X
  2984. X    return s;
  2985. X}
  2986. X
  2987. X
  2988. X
  2989. X/*
  2990. X * Load RFC1048 generic information directly into a memory buffer.
  2991. X *
  2992. X * "src" indirectly points to the ASCII representation of the generic data.
  2993. X * "dest" points to a string structure which is updated to point to a new
  2994. X * string with the new data appended to the old string.  The old string is
  2995. X * freed.
  2996. X *
  2997. X * The given tag value is inserted with the new data.
  2998. X *
  2999. X * The data may be represented as either a stream of hexadecimal numbers
  3000. X * representing bytes (any or all bytes may optionally start with '0x' and
  3001. X * be separated with periods ".") or as a quoted string of ASCII
  3002. X * characters (the quotes are required).
  3003. X */
  3004. X
  3005. XPRIVATE void process_generic(src, dest, tagvalue)
  3006. Xchar **src;
  3007. Xstruct shared_bindata **dest;
  3008. Xbyte tagvalue;
  3009. X{
  3010. X    byte tmpbuf[MAXBUFLEN];
  3011. X    byte *str;
  3012. X    struct shared_bindata *bdata;
  3013. X    int newlength, oldlength;
  3014. X
  3015. X    str = tmpbuf;
  3016. X    *str++ = tagvalue;        /* Store tag value */
  3017. X    str++;            /* Skip over length field */
  3018. X    if ((*src)[0] == '"') {                /* ASCII data */
  3019. X    newlength = sizeof(tmpbuf) - 2;    /* Set maximum allowed length */
  3020. X    (void) get_string(src, str, &newlength);
  3021. X    } else {                        /* Numeric data */
  3022. X    newlength = 0;
  3023. X    while (newlength < sizeof(tmpbuf) - 2) {
  3024. X        if (interp_byte(src, str++) < 0) {
  3025. X        break;
  3026. X        } else {
  3027. X        newlength++;
  3028. X        }
  3029. X        if (**src == '.') {
  3030. X        (*src)++;
  3031. X        }
  3032. X    }    
  3033. X    }
  3034. X    tmpbuf[1] = (byte) (newlength & 0xFF);
  3035. X    oldlength = ((*dest)->length);
  3036. X    bdata = (struct shared_bindata *) smalloc(sizeof(struct shared_bindata)
  3037. X                        + oldlength + newlength + 1);
  3038. X    if (oldlength > 0) {
  3039. X    bcopy((*dest)->data, bdata->data, oldlength);
  3040. X    }
  3041. X    bcopy(tmpbuf, bdata->data + oldlength, newlength + 2);
  3042. X    bdata->length = oldlength + newlength + 2;
  3043. X    bdata->linkcount = 1;
  3044. X    if (*dest) {
  3045. X    del_bindata(*dest);
  3046. X    }
  3047. X    *dest = bdata;
  3048. X}
  3049. X
  3050. X
  3051. X
  3052. X/*
  3053. X * Verify that the given string makes sense as a hostname (according to
  3054. X * Appendix 1, page 29 of RFC882).
  3055. X *
  3056. X * Return TRUE for good names, FALSE otherwise.
  3057. X */
  3058. X
  3059. XPRIVATE boolean goodname(hostname)
  3060. Xregister char *hostname;
  3061. X{
  3062. X    do {
  3063. X    if (!isalpha(*hostname++)) {    /* First character must be a letter */
  3064. X        return FALSE;
  3065. X    }
  3066. X    while (isalnum(*hostname) || (*hostname == '-')) {
  3067. X        hostname++;            /* Alphanumeric or a hyphen */
  3068. X    }
  3069. X    if (!isalnum(hostname[-1])) {    /* Last must be alphanumeric */
  3070. X        return FALSE;
  3071. X    }
  3072. X    if (*hostname == '\0') {    /* Done? */
  3073. X        return TRUE;
  3074. X    }
  3075. X    } while (*hostname++ == '.');    /* Dot, loop for next label */
  3076. X
  3077. X    return FALSE;            /* If it's not a dot, lose */
  3078. X}
  3079. X
  3080. X
  3081. X
  3082. X/*
  3083. X * Null compare function -- always returns FALSE so an element is always
  3084. X * inserted into a hash table (i.e. there is never a collision with an
  3085. X * existing element).
  3086. X */
  3087. X
  3088. XPRIVATE boolean nullcmp(host1, host2)
  3089. Xstruct host *host1, *host2;
  3090. X{
  3091. X    return FALSE;
  3092. X}
  3093. X
  3094. X
  3095. X/*
  3096. X * Function for comparing a string with the hostname field of a host
  3097. X * structure.
  3098. X */
  3099. X
  3100. XPRIVATE boolean nmcmp(name, hp)
  3101. Xchar *name;
  3102. Xstruct host *hp;
  3103. X{
  3104. X    return !strcmp(name, hp->hostname->string);
  3105. X}
  3106. X
  3107. X
  3108. X/*
  3109. X * Compare function to determine whether two hardware addresses are
  3110. X * equivalent.  Returns TRUE if "host1" and "host2" are equivalent, FALSE
  3111. X * otherwise.
  3112. X *
  3113. X * If the hardware addresses of "host1" and "host2" are identical, but
  3114. X * they are on different IP subnets, this function returns FALSE.
  3115. X *
  3116. X * This function is used when inserting elements into the hardware address
  3117. X * hash table.
  3118. X */
  3119. X
  3120. XPRIVATE boolean hwinscmp(host1, host2)
  3121. Xstruct host *host1, *host2;
  3122. X{
  3123. X    if (host1->htype != host2->htype) {
  3124. X    return FALSE;
  3125. X    }
  3126. X    if (bcmp(host1->haddr, host2->haddr, haddrlength(host1->htype))) {
  3127. X    return FALSE;
  3128. X    }
  3129. X    if ((host1->subnet_mask.s_addr) == (host2->subnet_mask.s_addr)) {
  3130. X    if (((host1->iaddr.s_addr) & (host1->subnet_mask.s_addr)) !=
  3131. X        ((host2->iaddr.s_addr) & (host2->subnet_mask.s_addr))) {
  3132. X        return FALSE;
  3133. X    }
  3134. X    }
  3135. X    return TRUE;
  3136. X}
  3137. X
  3138. X
  3139. X
  3140. X/*
  3141. X * Process the "similar entry" symbol.
  3142. X *
  3143. X * The host specified as the value of the "tc" symbol is used as a template
  3144. X * for the current host entry.  Symbol values not explicitly set in the
  3145. X * current host entry are inferred from the template entry.
  3146. X */
  3147. X
  3148. XPRIVATE void fill_defaults(hp, src)
  3149. Xstruct host *hp;
  3150. Xchar **src;
  3151. X{
  3152. X    unsigned tlen, hashcode;
  3153. X    struct host *hp2, thp;
  3154. X    char tstring[MAXSTRINGLEN];
  3155. X
  3156. X    tlen = sizeof(tstring);
  3157. X    (void) get_string(src, tstring, &tlen);
  3158. X    if (goodname(tstring)) {
  3159. X    hashcode = hash_HashFunction(tstring, tlen);
  3160. X    hp2 = (struct host *) hash_Lookup(nmhashtable, hashcode, nmcmp,
  3161. X                      tstring);
  3162. X    } else {
  3163. X    thp.iaddr.s_addr = inet_addr(tstring);
  3164. X    hashcode = hash_HashFunction(&(thp.iaddr.s_addr), 4);
  3165. X    hp2 = (struct host *) hash_Lookup(iphashtable, hashcode, iplookcmp,
  3166. X                      &thp);
  3167. X    }
  3168. X    if (hp2 == NULL) {
  3169. X    report(LOG_ERR, "can't find tc=\"%s\"\n", tstring);
  3170. X    } else {
  3171. X    /*
  3172. X     * Assignments inside "if" conditionals are intended here.
  3173. X     */
  3174. X    if (!hp->flags.cookie_server) {
  3175. X        if (hp->flags.cookie_server = hp2->flags.cookie_server) {
  3176. X        hp->cookie_server = hp2->cookie_server;
  3177. X        (hp->cookie_server->linkcount)++;
  3178. X        }
  3179. X    }
  3180. X    if (!hp->flags.domain_server) {
  3181. X        if (hp->flags.domain_server = hp2->flags.domain_server) {
  3182. X        hp->domain_server = hp2->domain_server;
  3183. X        (hp->domain_server->linkcount)++;
  3184. X        }
  3185. X    }
  3186. X    if (!hp->flags.gateway) {
  3187. X        if (hp->flags.gateway = hp2->flags.gateway) {
  3188. X        hp->gateway = hp2->gateway;
  3189. X        (hp->gateway->linkcount)++;
  3190. X        }
  3191. X    }
  3192. X    if (!hp->flags.impress_server) {
  3193. X        if (hp->flags.impress_server = hp2->flags.impress_server) {
  3194. X        hp->impress_server = hp2->impress_server;
  3195. X        (hp->impress_server->linkcount)++;
  3196. X        }
  3197. X    }
  3198. X    if (!hp->flags.log_server) {
  3199. X        if (hp->flags.log_server = hp2->flags.log_server) {
  3200. X        hp->log_server = hp2->log_server;
  3201. X        (hp->log_server->linkcount)++;
  3202. X        }
  3203. X    }
  3204. X    if (!hp->flags.lpr_server) {
  3205. X        if (hp->flags.lpr_server = hp2->flags.lpr_server) {
  3206. X        hp->lpr_server = hp2->lpr_server;
  3207. X        (hp->lpr_server->linkcount)++;
  3208. X        }
  3209. X    }
  3210. X    if (!hp->flags.name_server) {
  3211. X        if (hp->flags.name_server = hp2->flags.name_server) {
  3212. X        hp->name_server = hp2->name_server;
  3213. X        (hp->name_server->linkcount)++;
  3214. X        }
  3215. X    }
  3216. X    if (!hp->flags.rlp_server) {
  3217. X        if (hp->flags.rlp_server = hp2->flags.rlp_server) {
  3218. X        hp->rlp_server = hp2->rlp_server;
  3219. X        (hp->rlp_server->linkcount)++;
  3220. X        }
  3221. X    }
  3222. X    if (!hp->flags.time_server) {
  3223. X        if (hp->flags.time_server = hp2->flags.time_server) {
  3224. X        hp->time_server = hp2->time_server;
  3225. X        (hp->time_server->linkcount)++;
  3226. X        }
  3227. X    }
  3228. X    if (!hp->flags.homedir) {
  3229. X        if (hp->flags.homedir = hp2->flags.homedir) {
  3230. X        hp->homedir = hp2->homedir;
  3231. X        (hp->homedir->linkcount)++;
  3232. X        }
  3233. X    }
  3234. X    if (!hp->flags.bootfile) {
  3235. X        if (hp->flags.bootfile = hp2->flags.bootfile) {
  3236. X        hp->bootfile = hp2->bootfile;
  3237. X        (hp->bootfile->linkcount)++;
  3238. X        }
  3239. X    }
  3240. X    if (!hp->flags.generic) {
  3241. X        if (hp->flags.generic = hp2->flags.generic) {
  3242. X        hp->generic = hp2->generic;
  3243. X        (hp->generic->linkcount)++;
  3244. X        }
  3245. X    }
  3246. X    if (!hp->flags.vendor_magic) {
  3247. X        hp->flags.vm_auto = hp2->flags.vm_auto;
  3248. X        if (hp->flags.vendor_magic = hp2->flags.vendor_magic) {
  3249. X        bcopy(hp2->vm_cookie, hp->vm_cookie, 4);
  3250. X        }
  3251. X    }
  3252. X    if (!hp->flags.name_switch) {
  3253. X        if (hp->flags.name_switch = hp2->flags.name_switch) {
  3254. X        hp->flags.send_name = hp2->flags.send_name;
  3255. X        }
  3256. X    }
  3257. X    if (!hp->flags.htype) {
  3258. X        if (hp->flags.htype = hp2->flags.htype) {
  3259. X        hp->htype = hp2->htype;
  3260. X        }
  3261. X    }
  3262. X    if (!hp->flags.time_offset) {
  3263. X        if (hp->flags.time_offset = hp2->flags.time_offset) {
  3264. X        hp->flags.timeoff_auto = hp2->flags.timeoff_auto;
  3265. X        hp->time_offset = hp2->time_offset;
  3266. X        }
  3267. X    }
  3268. X    if (!hp->flags.subnet_mask) {
  3269. X        if (hp->flags.subnet_mask = hp2->flags.subnet_mask) {
  3270. X        hp->subnet_mask.s_addr = hp2->subnet_mask.s_addr;
  3271. X        }
  3272. X    }
  3273. X    if (!hp->flags.bootsize) {
  3274. X        if (hp->flags.bootsize = hp2->flags.bootsize) {
  3275. X        hp->flags.bootsize_auto = hp2->flags.bootsize_auto;
  3276. X        hp->bootsize = hp2->bootsize;
  3277. X        }
  3278. X    }
  3279. X    if (!hp->flags.tftpdir) {
  3280. X        if (hp->flags.tftpdir = hp2->flags.tftpdir) {
  3281. X        hp->tftpdir = hp2->tftpdir;
  3282. X        (hp->tftpdir->linkcount)++;
  3283. X        }
  3284. X    }
  3285. X    }
  3286. X}
  3287. X
  3288. X
  3289. X
  3290. X/*
  3291. X * This function adjusts the caller's pointer to point just past the
  3292. X * first-encountered colon.  If it runs into a null character, it leaves
  3293. X * the pointer pointing to it.
  3294. X */
  3295. X
  3296. XPRIVATE void adjust(s)
  3297. Xchar **s;
  3298. X{
  3299. X    register char *t;
  3300. X
  3301. X    t = *s;
  3302. X    while (*t && (*t != ':')) {
  3303. X    t++;
  3304. X    }
  3305. X    if (*t) {
  3306. X    t++;
  3307. X    }
  3308. X    *s = t;
  3309. X}
  3310. X
  3311. X
  3312. X
  3313. X
  3314. X/*
  3315. X * This function adjusts the caller's pointer to point to the first
  3316. X * non-whitespace character.  If it runs into a null character, it leaves
  3317. X * the pointer pointing to it.
  3318. X */
  3319. X
  3320. XPRIVATE void eat_whitespace(s)
  3321. Xchar **s;
  3322. X{
  3323. X    register char *t;
  3324. X
  3325. X    t = *s;
  3326. X    while (*t && isspace(*t)) {
  3327. X    t++;
  3328. X    }
  3329. X    *s = t;
  3330. X}
  3331. X
  3332. X
  3333. X
  3334. X/*
  3335. X * This function converts the given string to all lowercase.
  3336. X */
  3337. X
  3338. XPRIVATE void makelower(s)
  3339. Xchar *s;
  3340. X{
  3341. X    while (*s) {
  3342. X    if (isupper(*s)) {
  3343. X        *s = tolower(*s);
  3344. X    }
  3345. X    s++;
  3346. X    }
  3347. X}
  3348. X
  3349. X
  3350. X
  3351. X/*
  3352. X *
  3353. X *    N O T E :
  3354. X *
  3355. X *    In many of the functions which follow, a parameter such as "src" or
  3356. X *    "symbol" is passed as a pointer to a pointer to something.  This is
  3357. X *    done for the purpose of letting the called function update the
  3358. X *    caller's copy of the parameter (i.e. to effect call-by-reference
  3359. X *    parameter passing).  The value of the actual parameter is only used
  3360. X *    to locate the real parameter of interest and then update this indirect
  3361. X *    parameter.
  3362. X *
  3363. X *    I'm sure somebody out there won't like this. . . .
  3364. X *
  3365. X *
  3366. X */
  3367. X
  3368. X
  3369. X
  3370. X/*
  3371. X * "src" points to a character pointer which points to an ASCII string of
  3372. X * whitespace-separated IP addresses.  A pointer to an in_addr_list
  3373. X * structure containing the list of addresses is returned.  NULL is
  3374. X * returned if no addresses were found at all.  The pointer pointed to by
  3375. X * "src" is updated to point to the first non-address (illegal) character.
  3376. X */
  3377. X
  3378. XPRIVATE struct in_addr_list *get_addresses(src)
  3379. Xchar **src;
  3380. X{
  3381. X    struct in_addr tmpaddrlist[MAXINADDRS];
  3382. X    struct in_addr *address1, *address2;
  3383. X    struct in_addr_list *result;
  3384. X    unsigned addrcount, totalsize;
  3385. X
  3386. X    address1 = tmpaddrlist;
  3387. X    for (addrcount = 0; addrcount < MAXINADDRS; addrcount++) {
  3388. X    while (**src && isspace(**src)) {    /* Skip whitespace */
  3389. X        (*src)++;
  3390. X    }
  3391. X    if (! **src) {                /* Quit if nothing more */
  3392. X        break;
  3393. X    }
  3394. X    if (prs_inetaddr(src, &(address1->s_addr)) < 0) {
  3395. X        break;
  3396. X    }
  3397. X    address1++;            /* Point to next address slot */
  3398. X    }
  3399. X    if (addrcount < 1) {
  3400. X    result = NULL;
  3401. X    } else {
  3402. X    totalsize = sizeof(struct in_addr_list)
  3403. X            + (addrcount - 1) * sizeof(struct in_addr);
  3404. X    result = (struct in_addr_list *) smalloc(totalsize);
  3405. X    result->linkcount = 1;
  3406. X    result->addrcount = addrcount;
  3407. X    address1 = tmpaddrlist;
  3408. X    address2 = result->addr;
  3409. X    for (; addrcount > 0; addrcount--) {
  3410. X        address2->s_addr = address1->s_addr;
  3411. X        address1++;
  3412. X        address2++;
  3413. X    }
  3414. X    }
  3415. X    return result;
  3416. X}
  3417. X
  3418. X
  3419. X
  3420. X/*
  3421. X * prs_inetaddr(src, result)
  3422. X *
  3423. X * "src" is a value-result parameter; the pointer it points to is updated
  3424. X * to point to the next data position.   "result" points to an unsigned long
  3425. X * in which an address is returned.
  3426. X *
  3427. X * This function parses the IP address string in ASCII "dot notation" pointed
  3428. X * to by (*src) and places the result (in network byte order) in the unsigned
  3429. X * long pointed to by "result".  For malformed addresses, -1 is returned,
  3430. X * (*src) points to the first illegal character, and the unsigned long pointed
  3431. X * to by "result" is unchanged.  Successful calls return 0.
  3432. X */
  3433. X
  3434. XPRIVATE prs_inetaddr(src, result)
  3435. Xchar **src;
  3436. Xu_long *result;
  3437. X{
  3438. X    register u_long value;
  3439. X    u_long parts[4], *pp = parts;
  3440. X    int n;
  3441. X
  3442. X    if (!isdigit(**src)) {
  3443. X    return -1;
  3444. X    }
  3445. Xloop:
  3446. X    value = get_u_long(src);
  3447. X    if (**src == '.') {
  3448. X    /*
  3449. X     * Internet format:
  3450. X     *    a.b.c.d
  3451. X     *    a.b.c    (with c treated as 16-bits)
  3452. X     *    a.b    (with b treated as 24 bits)
  3453. X     */
  3454. X    if (pp >= parts + 4) {
  3455. X        return (-1);
  3456. X    }
  3457. X    *pp++ = value;
  3458. X    (*src)++;
  3459. X    goto loop;
  3460. X    }
  3461. X    /*
  3462. X     * Check for trailing characters.
  3463. X     */
  3464. X    if (**src && !(isspace(**src) || (**src == ':'))) {
  3465. X    return (-1);
  3466. X    }
  3467. X    *pp++ = value;
  3468. X    /*
  3469. X     * Construct the address according to
  3470. X     * the number of parts specified.
  3471. X     */
  3472. X    n = pp - parts;
  3473. X    switch (n) {
  3474. X    case 1:                /* a -- 32 bits */
  3475. X        value = parts[0];
  3476. X        break;
  3477. X    case 2:                /* a.b -- 8.24 bits */
  3478. X        value = (parts[0] << 24) | (parts[1] & 0xFFFFFF);
  3479. X        break;
  3480. X    case 3:                /* a.b.c -- 8.8.16 bits */
  3481. X        value = (parts[0] << 24) | ((parts[1] & 0xFF) << 16) |
  3482. X            (parts[2] & 0xFFFF);
  3483. X        break;
  3484. X    case 4:                /* a.b.c.d -- 8.8.8.8 bits */
  3485. X        value = (parts[0] << 24) | ((parts[1] & 0xFF) << 16) |
  3486. X            ((parts[2] & 0xFF) << 8) | (parts[3] & 0xFF);
  3487. X        break;
  3488. X    default:
  3489. X        return (-1);
  3490. X    }
  3491. X    *result = htonl(value);
  3492. X    return (0);
  3493. X}
  3494. X
  3495. X
  3496. X
  3497. X/*
  3498. X * "src" points to a pointer which in turn points to a hexadecimal ASCII
  3499. X * string.  This string is interpreted as a hardware address and returned
  3500. X * as a pointer to the actual hardware address, represented as an array of
  3501. X * bytes.
  3502. X *
  3503. X * The ASCII string must have the proper number of digits for the specified
  3504. X * hardware type (e.g. twelve digits for a 48-bit Ethernet address).
  3505. X * Two-digit sequences (bytes) may be separated with periods (.)  and/or
  3506. X * prefixed with '0x' for readability, but this is not required.
  3507. X *
  3508. X * For bad addresses, the pointer which "src" points to is updated to point
  3509. X * to the start of the first two-digit sequence which was bad, and the
  3510. X * function returns a NULL pointer.
  3511. X */
  3512. X
  3513. XPRIVATE byte *prs_haddr(src, htype)
  3514. Xchar **src;
  3515. Xbyte htype;
  3516. X{
  3517. X    static byte haddr[MAXHADDRLEN];
  3518. X    byte *hptr;
  3519. X    unsigned hlen;
  3520. X
  3521. X    hlen = haddrlength(htype);        /* Get length of this address type */
  3522. X    hptr = haddr;
  3523. X    while  (hptr < haddr + hlen) {
  3524. X    if (**src == '.') {
  3525. X        (*src)++;
  3526. X    }
  3527. X    if (interp_byte(src, hptr++) < 0) {
  3528. X        return NULL;
  3529. X    }
  3530. X    }
  3531. X    return haddr;       
  3532. X}
  3533. X
  3534. X
  3535. X
  3536. X/*
  3537. X * "src" is a pointer to a character pointer which in turn points to a
  3538. X * hexadecimal ASCII representation of a byte.  This byte is read, the
  3539. X * character pointer is updated, and the result is deposited into the
  3540. X * byte pointed to by "retbyte".
  3541. X *
  3542. X * The usual '0x' notation is allowed but not required.  The number must be
  3543. X * a two digit hexadecimal number.  If the number is invalid, "src" and
  3544. X * "retbyte" are left untouched and -1 is returned as the function value.
  3545. X * Successful calls return 0.
  3546. X */
  3547. X
  3548. XPRIVATE int interp_byte(src, retbyte)
  3549. Xchar **src;
  3550. Xbyte *retbyte;
  3551. X{
  3552. X    int v;
  3553. X
  3554. X    if ((*src)[0] == '0' && (*src)[1] == 'x' || (*src)[1] == 'X') {
  3555. X    (*src) += 2;    /* allow 0x for hex, but don't require it */
  3556. X    }
  3557. X    if (!isxdigit((*src)[0]) || !isxdigit((*src)[1])) {
  3558. X    return -1;
  3559. X    }
  3560. X    if (sscanf(*src, "%2x", &v) != 1) {
  3561. X    return -1;
  3562. X    }
  3563. X    (*src) += 2;
  3564. X    *retbyte = (byte) (v & 0xFF);
  3565. X    return 0;
  3566. X}
  3567. X
  3568. X
  3569. X
  3570. X/*
  3571. X * The parameter "src" points to a character pointer which points to an
  3572. X * ASCII string representation of an unsigned number.  The number is
  3573. X * returned as an unsigned long and the character pointer is updated to
  3574. X * point to the first illegal character.
  3575. X */
  3576. X
  3577. XPRIVATE u_long get_u_long(src)
  3578. Xchar **src;
  3579. X{
  3580. X    register u_long value, base;
  3581. X    char c;
  3582. X
  3583. X    /*
  3584. X     * Collect number up to first illegal character.  Values are specified
  3585. X     * as for C:  0x=hex, 0=octal, other=decimal.
  3586. X     */
  3587. X    value = 0;
  3588. X    base = 10;
  3589. X    if (**src == '0') {
  3590. X    base = 8, (*src)++;
  3591. X    }
  3592. X    if (**src == 'x' || **src == 'X') {
  3593. X    base = 16, (*src)++;
  3594. X    }
  3595. X    while (c = **src) {
  3596. X    if (isdigit(c)) {
  3597. X        value = (value * base) + (c - '0');
  3598. X        (*src)++;
  3599. X        continue;
  3600. X    }
  3601. X    if (base == 16 && isxdigit(c)) {
  3602. X        value = (value << 4) + ((c & ~32) + 10 - 'A');
  3603. X        (*src)++;
  3604. X        continue;
  3605. X    }
  3606. X    break;
  3607. X    }
  3608. X    return value;
  3609. X}
  3610. X
  3611. X
  3612. X
  3613. X/*
  3614. X * Routines for deletion of data associated with the main data structure.
  3615. X */
  3616. X
  3617. X
  3618. X/*
  3619. X * Frees the entire host data structure given.  Does nothing if the passed
  3620. X * pointer is NULL.
  3621. X */
  3622. X
  3623. XPRIVATE void free_host(hostptr)
  3624. Xstruct host *hostptr;
  3625. X{
  3626. X    if (hostptr) {
  3627. X    del_iplist(hostptr->cookie_server);
  3628. X    del_iplist(hostptr->domain_server);
  3629. X    del_iplist(hostptr->gateway);
  3630. X    del_iplist(hostptr->impress_server);
  3631. X    del_iplist(hostptr->log_server);
  3632. X    del_iplist(hostptr->lpr_server);
  3633. X    del_iplist(hostptr->name_server);
  3634. X    del_iplist(hostptr->rlp_server);
  3635. X    del_iplist(hostptr->time_server);
  3636. X    del_string(hostptr->hostname);
  3637. X    del_string(hostptr->homedir);
  3638. X    del_string(hostptr->bootfile);
  3639. X    del_string(hostptr->tftpdir);
  3640. X    del_bindata(hostptr->generic);
  3641. X    free((char *) hostptr);
  3642. X    }
  3643. X}
  3644. X
  3645. X
  3646. X
  3647. X/*
  3648. X * Decrements the linkcount on the given IP address data structure.  If the
  3649. X * linkcount goes to zero, the memory associated with the data is freed.
  3650. X */
  3651. X
  3652. XPRIVATE void del_iplist(iplist)
  3653. Xstruct in_addr_list *iplist;
  3654. X{
  3655. X    if (iplist) {
  3656. X    if (! (--(iplist->linkcount))) {
  3657. X        free((char *) iplist);
  3658. X    }
  3659. X    }
  3660. X}
  3661. X
  3662. X
  3663. X
  3664. X/*
  3665. X * Decrements the linkcount on a string data structure.  If the count
  3666. X * goes to zero, the memory associated with the string is freed.  Does
  3667. X * nothing if the passed pointer is NULL.
  3668. X */
  3669. X
  3670. XPRIVATE void del_string(stringptr)
  3671. Xstruct shared_string *stringptr;
  3672. X{
  3673. X    if (stringptr) {
  3674. X    if (! (--(stringptr->linkcount))) {
  3675. X        free((char *) stringptr);
  3676. X    }
  3677. X    }
  3678. X}
  3679. X
  3680. X
  3681. X
  3682. X/*
  3683. X * Decrements the linkcount on a shared_bindata data structure.  If the
  3684. X * count goes to zero, the memory associated with the data is freed.  Does
  3685. X * nothing if the passed pointer is NULL.
  3686. X */
  3687. X
  3688. XPRIVATE void del_bindata(dataptr)
  3689. Xstruct shared_bindata *dataptr;
  3690. X{
  3691. X    if (dataptr) {
  3692. X    if (! (--(dataptr->linkcount))) {
  3693. X        free((char *) dataptr);
  3694. X    }
  3695. X    }
  3696. X}
  3697. X
  3698. X
  3699. X
  3700. X
  3701. X/* smalloc()  --  safe malloc()
  3702. X *
  3703. X * Always returns a valid pointer (if it returns at all).  The allocated
  3704. X * memory is initialized to all zeros.  If malloc() returns an error, a
  3705. X * message is printed using the report() function and the program aborts
  3706. X * with a status of 1.
  3707. X */
  3708. X
  3709. XPRIVATE char *smalloc(nbytes)
  3710. Xunsigned nbytes;
  3711. X{
  3712. X    char *retvalue;
  3713. X
  3714. X    retvalue = malloc(nbytes);
  3715. X    if (!retvalue) {
  3716. X    report(LOG_ERR, "malloc() failure -- exiting\n");
  3717. X    exit(1);    
  3718. X    }
  3719. X    bzero(retvalue, nbytes);
  3720. X    return retvalue;
  3721. X}
  3722. END_OF_FILE
  3723. if test 49372 -ne `wc -c <'readfile.c'`; then
  3724.     echo shar: \"'readfile.c'\" unpacked with wrong size!
  3725. fi
  3726. # end of 'readfile.c'
  3727. fi
  3728. echo shar: End of archive 2 \(of 2\).
  3729. cp /dev/null ark2isdone
  3730. MISSING=""
  3731. for I in 1 2 ; do
  3732.     if test ! -f ark${I}isdone ; then
  3733.     MISSING="${MISSING} ${I}"
  3734.     fi
  3735. done
  3736. if test "${MISSING}" = "" ; then
  3737.     echo You have unpacked both archives.
  3738.     rm -f ark[1-9]isdone
  3739. else
  3740.     echo You still need to unpack the following archives:
  3741.     echo "        " ${MISSING}
  3742. fi
  3743. ##  End of shell archive.
  3744. exit 0
  3745.