home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume27 / bootp-2.2.B / part02 / readfile.c < prev   
Encoding:
C/C++ Source or Header  |  1993-10-11  |  48.2 KB  |  2,013 lines

  1. #ifndef _BLURB_
  2. #define _BLURB_
  3. /************************************************************************
  4.           Copyright 1988, 1991 by Carnegie Mellon University
  5.  
  6.                           All Rights Reserved
  7.  
  8. Permission to use, copy, modify, and distribute this software and its
  9. documentation for any purpose and without fee is hereby granted, provided
  10. that the above copyright notice appear in all copies and that both that
  11. copyright notice and this permission notice appear in supporting
  12. documentation, and that the name of Carnegie Mellon University not be used
  13. in advertising or publicity pertaining to distribution of the software
  14. without specific, written prior permission.
  15.  
  16. CARNEGIE MELLON UNIVERSITY DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS
  17. SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS.
  18. IN NO EVENT SHALL CMU BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
  19. DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
  20. PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
  21. ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
  22. SOFTWARE.
  23. ************************************************************************/
  24. #endif /* _BLURB_ */
  25.  
  26.  
  27. #ifndef lint
  28. static 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 $";
  29. #endif
  30.  
  31.  
  32. /*
  33.  * bootpd configuration file reading code.
  34.  *
  35.  * The routines in this file deal with reading, interpreting, and storing
  36.  * the information found in the bootpd configuration file (usually
  37.  * /etc/bootptab).
  38.  */
  39.  
  40.  
  41. #include <stdio.h>
  42. #ifdef SVR4
  43. #include <string.h>
  44. #else
  45. #include <strings.h>
  46. #endif
  47. #include <ctype.h>
  48. #include <sys/errno.h>
  49. #include <sys/types.h>
  50. #include <sys/stat.h>
  51. #include <sys/file.h>
  52. #include <sys/time.h>
  53. #include <netinet/in.h>
  54. #ifdef SYSLOG
  55. #include <syslog.h>
  56. #endif
  57. #include "bootp.h"
  58. #include "hash.h"
  59. #include "bootpd.h"
  60.  
  61. #ifdef SVR4
  62. #define    bcopy(a,b,c)    memcpy(b,a,c)
  63. #define    bzero(p,l)    memset(p,0,l)
  64. #define    bcmp(a,b,c)    memcmp(a,b,c)
  65. #endif
  66.  
  67. #define SUCCESS              0
  68. #define E_END_OF_ENTRY        (-1)
  69. #define E_SYNTAX_ERROR        (-2)
  70. #define E_UNKNOWN_SYMBOL    (-3)
  71. #define E_BAD_IPADDR        (-4)
  72. #define E_BAD_HADDR        (-5)
  73. #define E_BAD_SMASK        (-6)
  74. #define E_BAD_TIMEOFF        (-7)
  75. #define E_BAD_VM_COOKIE        (-8)
  76. #define E_BAD_HTYPE        (-9)
  77. #define E_BAD_BOOTSIZE           (-10)
  78. #define E_BAD_BOOT_SERVER      (-11)
  79. #define E_BAD_HOMEDIR           (-12)
  80. #define E_BAD_TFTPDIR           (-13)
  81.  
  82. #define SYM_NULL          0
  83. #define SYM_BOOTFILE          1
  84. #define SYM_COOKIE_SERVER      2
  85. #define SYM_DOMAIN_SERVER      3
  86. #define SYM_GATEWAY          4
  87. #define SYM_HADDR          5
  88. #define SYM_HOMEDIR          6
  89. #define SYM_HTYPE          7
  90. #define SYM_IMPRESS_SERVER      8
  91. #define SYM_IPADDR          9
  92. #define SYM_LOG_SERVER         10
  93. #define SYM_LPR_SERVER         11
  94. #define SYM_NAME_SERVER         12
  95. #define SYM_RLP_SERVER         13
  96. #define SYM_SUBNET_MASK         14
  97. #define SYM_TIME_OFFSET         15
  98. #define SYM_TIME_SERVER         16
  99. #define SYM_VENDOR_MAGIC     17
  100. #define SYM_SIMILAR_ENTRY     18
  101. #define SYM_NAME_SWITCH         19
  102. #define SYM_BOOTSIZE         20
  103. #define SYM_BOOT_SERVER         22
  104. #define SYM_TFTPDIR         23
  105.  
  106. #define OP_ADDITION          1    /* Operations on tags */
  107. #define OP_DELETION          2
  108. #define OP_BOOLEAN          3
  109.  
  110. #define MAXINADDRS         16    /* Max size of an IP address list */
  111. #define MAXBUFLEN         64    /* Max temp buffer space */
  112. #define MAXENTRYLEN           2048    /* Max size of an entire entry */
  113.  
  114. PRIVATE int process_entry();
  115. PRIVATE eval_symbol();
  116. PRIVATE boolean goodname();
  117. PRIVATE prs_inetaddr();
  118. PRIVATE int interp_byte();
  119.  
  120.  
  121. /*
  122.  * Structure used to map a configuration-file symbol (such as "ds") to a
  123.  * unique integer.
  124.  */
  125.  
  126. struct symbolmap {
  127.     char *symbol;
  128.     int symbolcode;
  129. };
  130.  
  131.  
  132. struct htypename {
  133.     char *name;
  134.     byte htype;
  135. };
  136.  
  137.  
  138. PRIVATE int nhosts;        /* Number of hosts (/w hw or IP address) */
  139. PRIVATE int nentries;        /* Total number of entries */
  140. PRIVATE long modtime = 0;    /* Last modification time of bootptab */
  141.  
  142.  
  143. /*
  144.  * List of symbolic names used in the bootptab file.  The order and actual
  145.  * values of the symbol codes (SYM_. . .) are unimportant, but they must
  146.  * all be unique.
  147.  */
  148.  
  149. PRIVATE struct symbolmap symbol_list[] = {
  150.     { "bf", SYM_BOOTFILE    },
  151.     { "bs", SYM_BOOTSIZE    },
  152.     { "cs", SYM_COOKIE_SERVER    },
  153.     { "ds", SYM_DOMAIN_SERVER    },
  154.     { "gw", SYM_GATEWAY        },
  155.     { "ha", SYM_HADDR        },
  156.     { "hd", SYM_HOMEDIR        },
  157.     { "hn", SYM_NAME_SWITCH     },
  158.     { "ht", SYM_HTYPE        },
  159.     { "im", SYM_IMPRESS_SERVER    },
  160.     { "ip", SYM_IPADDR        },
  161.     { "lg", SYM_LOG_SERVER    },
  162.     { "lp", SYM_LPR_SERVER    },
  163.     { "ns", SYM_NAME_SERVER    },
  164.     { "rl", SYM_RLP_SERVER    },
  165.     { "sa", SYM_BOOT_SERVER    },
  166.     { "sm", SYM_SUBNET_MASK    },
  167.     { "tc", SYM_SIMILAR_ENTRY   },
  168.     { "td", SYM_TFTPDIR        },
  169.     { "to", SYM_TIME_OFFSET    },
  170.     { "ts", SYM_TIME_SERVER    },
  171.     { "vm", SYM_VENDOR_MAGIC    },
  172. };
  173.  
  174.  
  175. /*
  176.  * List of symbolic names for hardware types.  Name translates into
  177.  * hardware type code listed with it.  Names must begin with a letter
  178.  * and must be all lowercase.  This is searched linearly, so put
  179.  * commonly-used entries near the beginning.
  180.  */
  181.  
  182. PRIVATE struct htypename htnamemap[] = {
  183.     { "ethernet",   HTYPE_ETHERNET    },
  184.     { "ethernet3",  HTYPE_EXP_ETHERNET    },
  185.     { "ether",        HTYPE_ETHERNET    },
  186.     { "ether3",        HTYPE_EXP_ETHERNET    },
  187.     { "ieee802",    HTYPE_IEEE802    },
  188.     { "tr",        HTYPE_IEEE802    },
  189.     { "token-ring", HTYPE_IEEE802    },
  190.     { "pronet",        HTYPE_PRONET    },
  191.     { "chaos",        HTYPE_CHAOS        },
  192.     { "arcnet",        HTYPE_ARCNET    },
  193.     { "ax.25",        HTYPE_AX25        }
  194. };
  195.  
  196.  
  197.  
  198. /*
  199.  * Externals and forward declarations.
  200.  */
  201.  
  202. extern char *malloc();
  203. extern boolean iplookcmp();
  204. extern int errno;
  205.  
  206. PRIVATE char *smalloc();
  207. PRIVATE void fill_defaults();
  208. PRIVATE void del_string();
  209. PRIVATE void del_bindata();
  210. PRIVATE void del_iplist();
  211. PRIVATE void free_host();
  212. PRIVATE u_long get_u_long();
  213. PRIVATE void process_generic();
  214. PRIVATE char *get_string();
  215. PRIVATE struct shared_string *get_shared_string();
  216. PRIVATE void read_entry();
  217. PRIVATE boolean nullcmp();
  218. PRIVATE boolean nmcmp();
  219. PRIVATE boolean hwinscmp();
  220. PRIVATE void adjust();
  221. PRIVATE void eat_whitespace();
  222. PRIVATE void makelower();
  223. PRIVATE struct in_addr_list *get_addresses();
  224. PRIVATE byte *prs_haddr();
  225.  
  226.  
  227.  
  228.  
  229.  
  230. /*
  231.  * Read bootptab database file.  Avoid rereading the file if the
  232.  * write date hasn't changed since the last time we read it.
  233.  */
  234.  
  235. void readtab()
  236. {
  237.     struct host *hp;
  238.     FILE *fp;
  239.     struct stat st;
  240.     unsigned hashcode, buflen;
  241.     static char buffer[MAXENTRYLEN];
  242. #ifdef DEBUG
  243.     char timestr[26];
  244. #endif
  245.  
  246.     /*
  247.      * Check the last modification time.
  248.      */
  249.     if (stat(bootptab, &st) < 0) {
  250.     report(LOG_ERR, "stat on \"%s\": %s\n", bootptab, get_errmsg());
  251.     return;
  252.     }
  253. #ifdef DEBUG
  254.     if (debug > 3) {
  255.     strcpy(timestr, ctime(&(st.st_mtime)));
  256.     report(LOG_INFO, "bootptab mtime is %s",
  257.            timestr);
  258.     }
  259. #endif
  260.     if (st.st_mtime == modtime && st.st_nlink) {
  261.     /*
  262.      * hasn't been modified or deleted yet.
  263.      */
  264.     return;
  265.     }
  266.     report(LOG_INFO, "reading %s\"%s\"\n",
  267.        (modtime != 0L) ? "new " : "",
  268.        bootptab);
  269.  
  270.     /*
  271.      * Open bootptab file.
  272.      */
  273.     if ((fp = fopen(bootptab, "r")) == NULL) {
  274.     report(LOG_ERR, "error opening \"%s\": %s\n", bootptab, get_errmsg());
  275.     return;
  276.     }
  277.  
  278.     /*
  279.      * Record file modification time.
  280.      */
  281.     if (fstat(fileno(fp), &st) < 0) {
  282.     report(LOG_ERR, "fstat: %s\n", get_errmsg());
  283.     fclose(fp);
  284.     return;
  285.     }
  286.     modtime = st.st_mtime;
  287.  
  288.     /*
  289.      * Entirely erase all hash tables.
  290.      */
  291.     hash_Reset(hwhashtable, free_host);
  292.     hash_Reset(iphashtable, free_host);
  293.     hash_Reset(nmhashtable, free_host);
  294.  
  295.     nhosts = 0;
  296.     nentries = 0;
  297.     while (TRUE) {
  298.     buflen = sizeof(buffer);
  299.     read_entry(fp, buffer, &buflen);
  300.     if (buflen == 0) {        /* More entries? */
  301.         break;
  302.     }
  303.     hp = (struct host *) smalloc(sizeof(struct host));
  304.  
  305.     /*
  306.      * Get individual info
  307.      */
  308.     hp->flags.vm_auto = TRUE;
  309.     bcopy(vm_rfc1048, hp->vm_cookie, 4);
  310.     if (process_entry(hp, buffer) < 0) {
  311.         free_host(hp);
  312.         continue;
  313.     }
  314.  
  315.     if ((hp->flags.htype && hp->flags.haddr) || hp->flags.iaddr) {
  316.         nhosts++;
  317.     }
  318.     if (hp->flags.htype && hp->flags.haddr) {
  319.         hashcode = hash_HashFunction(hp->haddr, haddrlength(hp->htype));
  320.         if (hash_Insert(hwhashtable, hashcode, hwinscmp, hp, hp) < 0) {
  321.         report(LOG_WARNING, "duplicate %s address: %s\n",
  322.             netname(hp->htype),
  323.             haddrtoa(hp->haddr, hp->htype));
  324.         free_host(hp);
  325.         continue;
  326.         }
  327.     }
  328.     if (hp->flags.iaddr) {
  329.         hashcode = hash_HashFunction(&(hp->iaddr.s_addr), 4);
  330.         if (hash_Insert(iphashtable, hashcode, nullcmp, hp, hp) < 0) {
  331.         report(LOG_ERR,
  332.             "hash_Insert() failed on IP address insertion\n");
  333.         }
  334.     }
  335.  
  336.     hashcode = hash_HashFunction(hp->hostname->string,
  337.                      strlen(hp->hostname->string));
  338.     if (hash_Insert(nmhashtable, hashcode, nullcmp, hp->hostname->string, hp) < 0) {
  339.         report(LOG_ERR,
  340.            "hash_Insert() failed on insertion of hostname: \"%s\"\n",
  341.            hp->hostname->string);
  342.     }
  343.     nentries++;
  344.     }
  345.  
  346. done:
  347.     fclose(fp);
  348.     report(LOG_INFO, "read %d entries (%d hosts) from \"%s\"\n",
  349.         nentries, nhosts, bootptab);
  350.     return;
  351. }
  352.  
  353.  
  354.  
  355. /*
  356.  * Read an entire host entry from the file pointed to by "fp" and insert it
  357.  * into the memory pointed to by "buffer".  Leading whitespace and comments
  358.  * starting with "#" are ignored (removed).  Backslashes (\) always quote
  359.  * the next character except that newlines preceeded by a backslash cause
  360.  * line-continuation onto the next line.  The entry is terminated by a
  361.  * newline character which is not preceeded by a backslash.  Sequences
  362.  * surrounded by double quotes are taken literally (including newlines, but
  363.  * not backslashes).
  364.  *
  365.  * The "bufsiz" parameter points to an unsigned int which specifies the
  366.  * maximum permitted buffer size.  Upon return, this value will be replaced
  367.  * with the actual length of the entry (not including the null terminator).
  368.  *
  369.  * This code is a little scary. . . .  I don't like using gotos in C
  370.  * either, but I first wrote this as an FSM diagram and gotos seemed like
  371.  * the easiest way to implement it.  Maybe later I'll clean it up.
  372.  */
  373.  
  374. PRIVATE void read_entry(fp, buffer, bufsiz)
  375. FILE *fp;
  376. char *buffer;
  377. unsigned *bufsiz;
  378. {
  379.     int c, length;
  380.  
  381.     length = 0;
  382.  
  383.     /*
  384.      * Eat whitespace, blank lines, and comment lines.
  385.      */
  386. top:
  387.     c = fgetc(fp);
  388.     if (c < 0) {
  389.     goto done;            /* Exit if end-of-file */
  390.     }
  391.     if (isspace(c)) {
  392.     goto top;            /* Skip over whitespace */
  393.     }
  394.     if (c == '#') {
  395.     while (TRUE) {            /* Eat comments after # */
  396.         c = fgetc(fp);
  397.         if (c < 0) {
  398.         goto done;        /* Exit if end-of-file */
  399.         }
  400.         if (c == '\n') {
  401.         goto top;        /* Try to read the next line */
  402.         }
  403.     }
  404.     }
  405.     ungetc(c, fp);    /* Other character, push it back to reprocess it */
  406.  
  407.  
  408.     /*
  409.      * Now we're actually reading a data entry.  Get each character and
  410.      * assemble it into the data buffer, processing special characters like
  411.      * double quotes (") and backslashes (\).
  412.      */
  413.  
  414. mainloop:
  415.     c = fgetc(fp);
  416.     switch (c) {
  417.     case EOF:        
  418.     case '\n':
  419.         goto done;            /* Exit on EOF or newline */
  420.     case '\\':
  421.         c = fgetc(fp);        /* Backslash, read a new character */
  422.         if (c < 0) {
  423.         goto done;        /* Exit on EOF */
  424.         }
  425.         *buffer++ = c;        /* Store the literal character */
  426.         length++;
  427.         if (length < *bufsiz - 1) {
  428.         goto mainloop;
  429.         } else {
  430.         goto done;
  431.         }
  432.     case '"':
  433.         *buffer++ = '"';        /* Store double-quote */
  434.         length++;
  435.         if (length >= *bufsiz - 1) {
  436.         goto done;
  437.         }
  438.         while (TRUE) {        /* Special quote processing loop */
  439.         c = fgetc(fp);
  440.         switch (c) {
  441.             case EOF:
  442.             goto done;        /* Exit on EOF . . . */
  443.             case '"':
  444.             *buffer++ = '"';    /* Store matching quote */
  445.             length++;
  446.             if (length < *bufsiz - 1) {
  447.                 goto mainloop;    /* And continue main loop */
  448.             } else {
  449.                 goto done;
  450.             }
  451.             case '\\':
  452.             if ((c = fgetc(fp)) < 0) {    /* Backslash */
  453.                 goto done;            /* EOF. . . .*/
  454.             }            /* else fall through */
  455.             default:
  456.             *buffer++ = c;        /* Other character, store it */
  457.             length++;
  458.             if (length >= *bufsiz - 1) {
  459.                 goto done;
  460.             }
  461.         }
  462.         }
  463.     case ':':
  464.         *buffer++ = c;        /* Store colons */
  465.         length++;
  466.         if (length >= *bufsiz - 1) {
  467.         goto done;
  468.         }
  469.  
  470.         do {            /* But remove whitespace after them */
  471.         c = fgetc(fp);
  472.         if ((c < 0) || (c == '\n')) {
  473.             goto done;
  474.         }
  475.         } while (isspace(c));    /* Skip whitespace */
  476.  
  477.         if (c == '\\') {        /* Backslash quotes next character */
  478.         c = fgetc(fp);
  479.         if (c < 0) {
  480.             goto done;
  481.         }
  482.         if (c == '\n') {
  483.             goto top;        /* Backslash-newline continuation */
  484.         }
  485.         }
  486.         /* fall through if "other" character */
  487.     default:
  488.         *buffer++ = c;        /* Store other characters */
  489.         length++;
  490.         if (length >= *bufsiz - 1) {
  491.         goto done;
  492.         }
  493.     }
  494.     goto mainloop;            /* Keep going */
  495.  
  496. done:
  497.     *buffer = '\0';            /* Terminate string */
  498.     *bufsiz = length;            /* Tell the caller its length */
  499. }
  500.  
  501.  
  502.  
  503. /*
  504.  * Parse out all the various tags and parameters in the host entry pointed
  505.  * to by "src".  Stuff all the data into the appropriate fields of the
  506.  * host structure pointed to by "host".  If there is any problem with the
  507.  * entry, an error message is reported via report(), no further processing
  508.  * is done, and -1 is returned.  Successful calls return 0.
  509.  *
  510.  * (Some errors probably shouldn't be so completely fatal. . . .)
  511.  */
  512.  
  513. PRIVATE int process_entry(host, src)
  514. struct host *host;
  515. char *src;
  516. {
  517.     int retval;
  518.  
  519.     if (!host || *src == '\0') {
  520.     return -1;
  521.     }
  522.     host->hostname = get_shared_string(&src);
  523.     if (!goodname(host->hostname->string)) {
  524.     report(LOG_ERR, "bad hostname: \"%s\"\n", host->hostname->string);
  525.     del_string(host->hostname);
  526.     return -1;
  527.     }
  528.     adjust(&src);
  529.     while (TRUE) {
  530.      retval = eval_symbol(&src, host);
  531.     switch (retval) {
  532.         case SUCCESS:
  533.         break;
  534.         case E_SYNTAX_ERROR:
  535.         report(LOG_ERR, "syntax error in entry for host \"%s\"\n",
  536.                 host->hostname->string);
  537.         return -1;
  538.         case E_UNKNOWN_SYMBOL:
  539.         report(LOG_ERR, "unknown symbol in entry for host \"%s\"\n",
  540.                 host->hostname->string);
  541.         return -1;
  542.         case E_BAD_IPADDR:
  543.         report(LOG_ERR, "bad IP address for host \"%s\"\n",
  544.                 host->hostname->string);
  545.         return -1;
  546.         case E_BAD_HADDR:
  547.         report(LOG_ERR, "bad hardware address for host \"%s\"\n",
  548.                 host->hostname->string);
  549.         return -1;
  550.         case E_BAD_SMASK:
  551.         report(LOG_ERR, "bad subnet mask for host \"%s\"\n",
  552.                 host->hostname->string);
  553.         return -1;
  554.         case E_BAD_TIMEOFF:
  555.         report(LOG_ERR, "bad time offset for host \"%s\"\n",
  556.                 host->hostname->string);
  557.         return -1;
  558.         case E_BAD_VM_COOKIE:
  559.         report(LOG_ERR, "bad vendor magic cookie for host \"%s\"\n",
  560.                 host->hostname->string);
  561.         return -1;
  562.         case E_BAD_HOMEDIR:
  563.         report(LOG_ERR, "bad home directory for host \"%s\"\n",
  564.                 host->hostname->string);
  565.         return -1;
  566.         case E_BAD_TFTPDIR:
  567.         report(LOG_ERR, "bad TFTP directory for host \"%s\"\n",
  568.                 host->hostname->string);
  569.         return -1;
  570.         case E_BAD_BOOT_SERVER:
  571.         report(LOG_ERR, "bad boot server IP address for host \"%s\"\n",
  572.                 host->hostname->string);
  573.         return -1;
  574.         case E_END_OF_ENTRY:
  575.         default:
  576. #if 0
  577.         /*
  578.          * For now, don't try to make-up a subnet mask if one
  579.          * wasn't specified.
  580.          *
  581.          * This algorithm is also not entirely correct.
  582.          */
  583.         if (!(hp->flags.subnet_mask)) {
  584.             /*
  585.              * Try to deduce the subnet mask from the network class
  586.              */
  587.             value = (ntohl(value) >> 30) & 0x03;
  588.             switch (value) {
  589.             case 0:
  590.             case 1:
  591.                 hp->subnet_mask.s_addr = htonl(0xFF000000L);
  592.                 break;
  593.             case 2:
  594.                 hp->subnet_mask.s_addr = htonl(0xFFFF0000L);
  595.                 break;
  596.             case 3:
  597.                 hp->subnet_mask.s_addr = htonl(0xFFFFFF00L);
  598.                 break;
  599.             }
  600.             hp->flags.subnet_mask = TRUE;
  601.         }
  602. #endif
  603.         /*
  604.          * And now we're done with this entry
  605.          */
  606.         return 0;
  607.     }
  608.     adjust(&src);
  609.     }
  610. }
  611.  
  612.  
  613.  
  614. /*
  615.  * Evaluate the two-character tag symbol pointed to by "symbol" and place
  616.  * the data in the structure pointed to by "hp".  The pointer pointed to
  617.  * by "symbol" is updated to point past the source string (but may not
  618.  * point to the next tag entry).
  619.  *
  620.  * Obviously, this need a few more comments. . . .
  621.  */
  622.  
  623. PRIVATE eval_symbol(symbol, hp)
  624. char **symbol;
  625. struct host *hp;
  626. {
  627.     char tmpstr[MAXSTRINGLEN];
  628.     byte *tmphaddr, *ustr;
  629.     struct shared_string *ss;
  630.     struct symbolmap *symbolptr;
  631.     u_long value;
  632.     long timeoff;
  633.     int i, numsymbols;
  634.     unsigned len;
  635.     int optype;        /* Indicates boolean, addition, or deletion */
  636.  
  637.     if ((*symbol)[0] == '\0') {
  638.     return E_END_OF_ENTRY;
  639.     }
  640.     if ((*symbol)[0] == ':') {
  641.     return SUCCESS;
  642.     }
  643.     if ((*symbol)[0] == 'T') {            /* generic symbol */
  644.     (*symbol)++;
  645.     value = get_u_long(symbol);
  646.     eat_whitespace(symbol);
  647.     if ((*symbol)[0] != '=') {
  648.         return E_SYNTAX_ERROR;
  649.     }
  650.     (*symbol)++;
  651.     if (!(hp->generic)) {
  652.         hp->generic = (struct shared_bindata *)
  653.                 smalloc(sizeof(struct shared_bindata));
  654.     }
  655.     process_generic(symbol, &(hp->generic), (byte) (value & 0xFF));
  656.     hp->flags.generic = TRUE;
  657.     return SUCCESS;
  658.     }
  659.  
  660.     eat_whitespace(symbol);
  661.  
  662.     /*
  663.      * Determine the type of operation to be done on this symbol
  664.      */
  665.     switch ((*symbol)[2]) {
  666.     case '=':
  667.         optype = OP_ADDITION;
  668.         break;
  669.     case '@':
  670.         optype = OP_DELETION;
  671.         break;
  672.     case ':':
  673.     case '\0':
  674.         optype = OP_BOOLEAN;
  675.         break;
  676.     default:
  677.         return E_SYNTAX_ERROR;
  678.     }
  679.  
  680.     symbolptr = symbol_list;
  681.     numsymbols = sizeof(symbol_list) / sizeof(struct symbolmap);
  682.     for (i = 0; i < numsymbols; i++) {
  683.     if (((symbolptr->symbol)[0] == (*symbol)[0]) &&
  684.        ((symbolptr->symbol)[1] == (*symbol)[1])) {
  685.         break;
  686.     }
  687.     symbolptr++;
  688.     }
  689.     if (i >= numsymbols) {
  690.     return E_UNKNOWN_SYMBOL;
  691.     }
  692.  
  693.     /*
  694.      * Skip past the = or @ character (to point to the data) if this
  695.      * isn't a boolean operation.  For boolean operations, just skip
  696.      * over the two-character tag symbol (and nothing else. . . .).
  697.      */
  698.     (*symbol) += (optype == OP_BOOLEAN) ? 2 : 3;
  699.  
  700.     switch (symbolptr->symbolcode) {
  701.     case SYM_BOOTFILE:
  702.         switch (optype) {
  703.         case OP_ADDITION:
  704.             if (ss = get_shared_string(symbol)) {
  705.             if (hp->bootfile) {
  706.                 del_string(hp->bootfile);
  707.             }
  708.             hp->bootfile = ss;
  709.             hp->flags.bootfile = TRUE;
  710.             }
  711.             break;
  712.         case OP_DELETION:
  713.             if (hp->bootfile) {
  714.             del_string(hp->bootfile);
  715.             }
  716.             hp->bootfile = NULL;
  717.             hp->flags.bootfile = FALSE;
  718.             break;
  719.         case OP_BOOLEAN:
  720.             return E_SYNTAX_ERROR;
  721.         }
  722.         break;
  723.  
  724.     case SYM_COOKIE_SERVER:
  725.         switch (optype) {
  726.         case OP_ADDITION:
  727.             if (hp->flags.cookie_server && hp->cookie_server) {
  728.             del_iplist(hp->cookie_server);
  729.             }
  730.             hp->cookie_server = get_addresses(symbol);
  731.             hp->flags.cookie_server = TRUE;
  732.             break;
  733.         case OP_DELETION:
  734.             if (hp->flags.cookie_server && hp->cookie_server) {
  735.             del_iplist(hp->cookie_server);
  736.             }
  737.             hp->cookie_server = NULL;
  738.             hp->flags.cookie_server = FALSE;
  739.             break;
  740.         case OP_BOOLEAN:
  741.             return E_SYNTAX_ERROR;
  742.         }
  743.         break;
  744.  
  745.     case SYM_DOMAIN_SERVER:
  746.         switch (optype) {
  747.         case OP_ADDITION:
  748.             if (hp->flags.domain_server && hp->domain_server) {
  749.             del_iplist(hp->domain_server);
  750.             }
  751.             hp->domain_server = get_addresses(symbol);
  752.             hp->flags.domain_server = TRUE;
  753.             break;
  754.         case OP_DELETION:
  755.             if (hp->flags.domain_server && hp->domain_server) {
  756.             del_iplist(hp->domain_server);
  757.             }
  758.             hp->domain_server = NULL;
  759.             hp->flags.domain_server = FALSE;
  760.             break;
  761.         case OP_BOOLEAN:
  762.             return E_SYNTAX_ERROR;
  763.         }
  764.         break;
  765.  
  766.     case SYM_GATEWAY:
  767.         switch (optype) {
  768.         case OP_ADDITION:
  769.             if (hp->flags.gateway && hp->gateway) {
  770.             del_iplist(hp->gateway);
  771.             }
  772.             hp->gateway = get_addresses(symbol);
  773.             hp->flags.gateway = TRUE;
  774.             break;
  775.         case OP_DELETION:
  776.             if (hp->flags.gateway && hp->gateway) {
  777.             del_iplist(hp->gateway);
  778.             }
  779.             hp->gateway = NULL;
  780.             hp->flags.gateway = FALSE;
  781.             break;
  782.         case OP_BOOLEAN:
  783.             return E_SYNTAX_ERROR;
  784.         }
  785.         break;
  786.  
  787.     case SYM_HADDR:
  788.         switch (optype) {
  789.         case OP_ADDITION:
  790.             if (hp->flags.htype && hp->htype &&
  791.             (tmphaddr = prs_haddr(symbol, hp->htype))) {
  792.             bcopy(tmphaddr, hp->haddr, haddrlength(hp->htype));
  793.             hp->flags.haddr = TRUE;
  794.             } else {
  795.             return E_BAD_HADDR;
  796.             }
  797.             break;
  798.         case OP_DELETION:
  799.             hp->flags.haddr = FALSE;
  800.             break;
  801.         case OP_BOOLEAN:
  802.             return E_SYNTAX_ERROR;
  803.         }
  804.         break;
  805.  
  806.     case SYM_HOMEDIR:
  807.         switch (optype) {
  808.         case OP_ADDITION:
  809.             if (ss = get_shared_string(symbol)) {
  810.             if ((ss->string)[0] == '/') {
  811.                 if (hp->homedir) {
  812.                 del_string(hp->homedir);
  813.                 }
  814.                 hp->homedir = ss;
  815.                 hp->flags.homedir = TRUE;
  816.             } else {
  817.                 return E_BAD_HOMEDIR;
  818.             }
  819.             } else {
  820.             return E_BAD_HOMEDIR;
  821.             }
  822.             break;
  823.         case OP_DELETION:
  824.             if (hp->homedir) {
  825.             del_string(hp->homedir);
  826.             }
  827.             hp->homedir = NULL;
  828.             hp->flags.homedir = FALSE;
  829.             break;
  830.         case OP_BOOLEAN:
  831.             return E_SYNTAX_ERROR;
  832.         }
  833.         break;
  834.  
  835.     case SYM_HTYPE:
  836.         switch (optype) {
  837.         case OP_ADDITION:
  838.             value = 0L;            /* Assume an illegal value */
  839.             eat_whitespace(symbol);
  840.             if (isdigit(**symbol)) {
  841.             value = get_u_long(symbol);
  842.             } else {
  843.             len = sizeof(tmpstr);
  844.             (void) get_string(symbol, tmpstr, &len);
  845.             makelower(tmpstr);
  846.             numsymbols = sizeof(htnamemap) /
  847.                      sizeof(struct htypename);
  848.             for (i = 0; i < numsymbols; i++) {
  849.                 if (!strcmp(htnamemap[i].name, tmpstr)) {
  850.                 break;
  851.                 }
  852.             }
  853.             if (i < numsymbols) {
  854.                 value = htnamemap[i].htype;
  855.             }
  856.             }
  857.             if ((value < 0) || (value >= hwinfocnt)) {
  858.             return E_BAD_HTYPE;
  859.             }
  860.             hp->htype = (byte) (value & 0xFF);
  861.             hp->flags.htype = TRUE;
  862.             break;
  863.         case OP_DELETION:
  864.             hp->flags.htype = FALSE;
  865.             break;
  866.         case OP_BOOLEAN:
  867.             return E_SYNTAX_ERROR;
  868.         }
  869.         break;
  870.  
  871.     case SYM_IMPRESS_SERVER:
  872.         switch (optype) {
  873.         case OP_ADDITION:
  874.             if (hp->flags.impress_server && hp->impress_server) {
  875.             del_iplist(hp->impress_server);
  876.             }
  877.             hp->impress_server = get_addresses(symbol);
  878.             hp->flags.impress_server = TRUE;
  879.             break;
  880.         case OP_DELETION:
  881.             if (hp->flags.impress_server && hp->impress_server) {
  882.             del_iplist(hp->impress_server);
  883.             }
  884.             hp->impress_server = NULL;
  885.             hp->flags.impress_server = FALSE;
  886.             break;
  887.         case OP_BOOLEAN:
  888.             return E_SYNTAX_ERROR;
  889.         }
  890.         break;
  891.  
  892.     case SYM_IPADDR:
  893.         switch (optype) {
  894.         case OP_ADDITION:
  895.             if (prs_inetaddr(symbol, &value) < 0) {
  896.             return E_BAD_IPADDR;
  897.             } else {
  898.             hp->iaddr.s_addr = value;
  899.             hp->flags.iaddr = TRUE;
  900.             }
  901.             break;
  902.         case OP_DELETION:
  903.             hp->flags.iaddr = FALSE;
  904.             break;
  905.         case OP_BOOLEAN:
  906.             return E_SYNTAX_ERROR;
  907.         }
  908.         break;
  909.  
  910.     case SYM_LOG_SERVER:
  911.         switch (optype) {
  912.         case OP_ADDITION:
  913.             if (hp->flags.log_server && hp->log_server) {
  914.             del_iplist(hp->log_server);
  915.             }
  916.             hp->log_server = get_addresses(symbol);
  917.             hp->flags.log_server = TRUE;
  918.             break;
  919.         case OP_DELETION:
  920.             if (hp->flags.log_server && hp->log_server) {
  921.             del_iplist(hp->log_server);
  922.             }
  923.             hp->log_server = NULL;
  924.             hp->flags.log_server = FALSE;
  925.             break;
  926.         case OP_BOOLEAN:
  927.             return E_SYNTAX_ERROR;
  928.         }
  929.         break;
  930.  
  931.     case SYM_LPR_SERVER:
  932.         switch (optype) {
  933.         case OP_ADDITION:
  934.             if (hp->flags.lpr_server && hp->lpr_server) {
  935.             del_iplist(hp->lpr_server);
  936.             }
  937.             hp->lpr_server = get_addresses(symbol);
  938.             hp->flags.lpr_server = TRUE;
  939.             break;
  940.         case OP_DELETION:
  941.             if (hp->flags.lpr_server && hp->lpr_server) {
  942.             del_iplist(hp->lpr_server);
  943.             }
  944.             hp->lpr_server = NULL;
  945.             hp->flags.lpr_server = FALSE;
  946.             break;
  947.         case OP_BOOLEAN:
  948.             return E_SYNTAX_ERROR;
  949.         }
  950.         break;
  951.  
  952.     case SYM_NAME_SERVER:
  953.         switch (optype) {
  954.         case OP_ADDITION:
  955.             if (hp->flags.name_server && hp->name_server) {
  956.             del_iplist(hp->name_server);
  957.             }
  958.             hp->name_server = get_addresses(symbol);
  959.             hp->flags.name_server = TRUE;
  960.             break;
  961.         case OP_DELETION:
  962.             if (hp->flags.name_server && hp->name_server) {
  963.             del_iplist(hp->name_server);
  964.             }
  965.             hp->name_server = NULL;
  966.             hp->flags.name_server = FALSE;
  967.             break;
  968.         case OP_BOOLEAN:
  969.             return E_SYNTAX_ERROR;
  970.         }
  971.         break;
  972.  
  973.     case SYM_RLP_SERVER:
  974.         switch (optype) {
  975.         case OP_ADDITION:
  976.             if (hp->flags.rlp_server && hp->rlp_server) {
  977.             del_iplist(hp->rlp_server);
  978.             }
  979.             hp->rlp_server = get_addresses(symbol);
  980.             hp->flags.rlp_server = TRUE;
  981.             break;
  982.         case OP_DELETION:
  983.             if (hp->flags.rlp_server && hp->rlp_server) {
  984.             del_iplist(hp->rlp_server);
  985.             }
  986.             hp->rlp_server = NULL;
  987.             hp->flags.rlp_server = FALSE;
  988.             break;
  989.         case OP_BOOLEAN:
  990.             return E_SYNTAX_ERROR;
  991.         }
  992.         break;
  993.  
  994.     case SYM_SUBNET_MASK:
  995.         switch (optype) {
  996.         case OP_ADDITION:
  997.             if (prs_inetaddr(symbol, &value) < 0) {
  998.             return E_BAD_SMASK;
  999.             } else {
  1000.             hp->subnet_mask.s_addr = value;
  1001.             hp->flags.subnet_mask = TRUE;
  1002.             }
  1003.             break;
  1004.         case OP_DELETION:
  1005.             hp->flags.subnet_mask = FALSE;
  1006.             break;
  1007.         case OP_BOOLEAN:
  1008.             return E_SYNTAX_ERROR;
  1009.         }
  1010.         break;
  1011.  
  1012.     case SYM_TIME_OFFSET:
  1013.         switch (optype) {
  1014.         case OP_ADDITION:
  1015.             len = sizeof(tmpstr);
  1016.             (void) get_string(symbol, tmpstr, &len);
  1017.             if (!strncmp(tmpstr, "auto", 4)) {
  1018.             hp->flags.timeoff_auto = TRUE;
  1019.             hp->flags.time_offset = TRUE;
  1020.             } else {
  1021.             if (sscanf(tmpstr, "%ld", &timeoff) != 1) {
  1022.                 return E_BAD_TIMEOFF;
  1023.             } else {
  1024.                 hp->time_offset = timeoff;
  1025.                 hp->flags.timeoff_auto = FALSE;
  1026.                 hp->flags.time_offset = TRUE;
  1027.             }
  1028.             }
  1029.             break;
  1030.         case OP_DELETION:
  1031.             hp->flags.time_offset = FALSE;
  1032.             break;
  1033.         case OP_BOOLEAN:
  1034.             return E_SYNTAX_ERROR;
  1035.         }
  1036.         break;
  1037.  
  1038.     case SYM_TIME_SERVER:
  1039.         switch (optype) {
  1040.         case OP_ADDITION:
  1041.             if (hp->flags.time_server && hp->time_server) {
  1042.             del_iplist(hp->time_server);
  1043.             }
  1044.             hp->time_server = get_addresses(symbol);
  1045.             hp->flags.time_server = TRUE;
  1046.             break;
  1047.         case OP_DELETION:
  1048.             if (hp->flags.time_server && hp->time_server) {
  1049.             del_iplist(hp->time_server);
  1050.             }
  1051.             hp->time_server = NULL;
  1052.             hp->flags.time_server = FALSE;
  1053.             break;
  1054.         case OP_BOOLEAN:
  1055.             return E_SYNTAX_ERROR;
  1056.         }
  1057.         break;
  1058.  
  1059.     case SYM_VENDOR_MAGIC:
  1060.         switch (optype) {
  1061.         case OP_ADDITION:
  1062.             if (!strncmp(*symbol, "auto", 4)) {
  1063.             hp->flags.vm_auto = TRUE;    /* Make it auto */
  1064.             } else if (!strncmp(*symbol, "rfc1048", 7) ||
  1065.                    !strncmp(*symbol, "rfc1084", 7)) {
  1066.             hp->flags.vm_auto = FALSE;    /* Make it manual */
  1067.             bcopy(vm_rfc1048, hp->vm_cookie, 4);
  1068.             } else if (!strncmp(*symbol, "cmu", 3)) {
  1069.             hp->flags.vm_auto = FALSE;    /* Make it manual */
  1070.             bcopy(vm_cmu, hp->vm_cookie, 4);
  1071.             } else {
  1072.             if (prs_inetaddr(symbol, &value) < 0) {
  1073.                 return E_BAD_VM_COOKIE;
  1074.             }
  1075.             hp->flags.vm_auto = FALSE;    /* Make it manual */
  1076.             ustr = hp->vm_cookie;
  1077.             bcopy((char*)&value, ustr, sizeof(long));
  1078.             }
  1079.             hp->flags.vendor_magic = TRUE;
  1080.             break;
  1081.         case OP_DELETION:
  1082.             hp->flags.vendor_magic = FALSE;
  1083.             break;
  1084.         case OP_BOOLEAN:
  1085.             hp->flags.vm_auto = TRUE;
  1086.             hp->flags.vendor_magic = TRUE;
  1087.             break;
  1088.         }
  1089.         break;
  1090.  
  1091.     case SYM_SIMILAR_ENTRY:
  1092.         switch (optype) {
  1093.         case OP_ADDITION:
  1094.             fill_defaults(hp, symbol);
  1095.             break;
  1096.         default:
  1097.             return E_SYNTAX_ERROR;
  1098.         }
  1099.         break;
  1100.  
  1101.     case SYM_NAME_SWITCH:
  1102.         switch (optype) {
  1103.         case OP_ADDITION:
  1104.             return E_SYNTAX_ERROR;
  1105.         case OP_DELETION:
  1106.             hp->flags.send_name = FALSE;
  1107.             hp->flags.name_switch = FALSE;
  1108.             break;
  1109.         case OP_BOOLEAN:
  1110.             hp->flags.send_name = TRUE;
  1111.             hp->flags.name_switch = TRUE;
  1112.             break;
  1113.         }
  1114.         break;
  1115.  
  1116.     case SYM_BOOTSIZE:
  1117.         switch (optype) {
  1118.         case OP_ADDITION:
  1119.             if (!strncmp(*symbol, "auto", 4)) {
  1120.             hp->flags.bootsize = TRUE;
  1121.             hp->flags.bootsize_auto = TRUE;
  1122.             } else {
  1123.             hp->bootsize = (unsigned int) get_u_long(symbol);
  1124.             hp->flags.bootsize = TRUE;
  1125.             hp->flags.bootsize_auto = FALSE;
  1126.             }
  1127.             break;
  1128.         case OP_DELETION:
  1129.             hp->flags.bootsize = FALSE;
  1130.             break;
  1131.         case OP_BOOLEAN:
  1132.             hp->flags.bootsize = TRUE;
  1133.             hp->flags.bootsize_auto = TRUE;
  1134.             break;
  1135.         }
  1136.         break;
  1137.  
  1138.     case SYM_BOOT_SERVER:
  1139.         switch (optype) {
  1140.         case OP_ADDITION:
  1141.             if (prs_inetaddr(symbol, &value) < 0) {
  1142.             return E_BAD_BOOT_SERVER;
  1143.             } else {
  1144.             hp->bootserver.s_addr = value;
  1145.             hp->flags.bootserver = TRUE;
  1146.             }
  1147.             break;
  1148.         case OP_DELETION:
  1149.             hp->flags.bootserver = FALSE;
  1150.             break;
  1151.         case OP_BOOLEAN:
  1152.             return E_SYNTAX_ERROR;
  1153.         }
  1154.         break;
  1155.  
  1156.     case SYM_TFTPDIR:
  1157.         switch (optype) {
  1158.         case OP_ADDITION:
  1159.             if (ss = get_shared_string(symbol)) {
  1160.             if ((ss->string)[0] == '/') {
  1161.                 if (hp->tftpdir) {
  1162.                 del_string(hp->tftpdir);
  1163.                 }
  1164.                 hp->tftpdir = ss;
  1165.                 hp->flags.tftpdir = TRUE;
  1166.             } else {
  1167.                 return E_BAD_TFTPDIR;
  1168.             }
  1169.             } else {
  1170.             return E_BAD_TFTPDIR;
  1171.             }
  1172.             break;
  1173.         case OP_DELETION:
  1174.             if (hp->tftpdir) {
  1175.             del_string(hp->tftpdir);
  1176.             }
  1177.             hp->tftpdir = NULL;
  1178.             hp->flags.tftpdir = FALSE;
  1179.             break;
  1180.         case OP_BOOLEAN:
  1181.             return E_SYNTAX_ERROR;
  1182.         }
  1183.         break;
  1184.  
  1185.     default:
  1186.         return E_UNKNOWN_SYMBOL;
  1187.     }
  1188.     return SUCCESS;
  1189. }
  1190.  
  1191.  
  1192.  
  1193. /*
  1194.  * Read a string from the buffer indirectly pointed to through "src" and
  1195.  * move it into the buffer pointed to by "dest".  A pointer to the maximum
  1196.  * allowable length of the string (including null-terminator) is passed as
  1197.  * "length".  The actual length of the string which was read is returned in
  1198.  * the unsigned integer pointed to by "length".  This value is the same as
  1199.  * that which would be returned by applying the strlen() function on the
  1200.  * destination string (i.e the terminating null is not counted as a
  1201.  * character).  Trailing whitespace is removed from the string.  For
  1202.  * convenience, the function returns the new value of "dest".
  1203.  *
  1204.  * The string is read until the maximum number of characters, an unquoted
  1205.  * colon (:), or a null character is read.  The return string in "dest" is
  1206.  * null-terminated.
  1207.  */
  1208.  
  1209. PRIVATE char *get_string(src, dest, length)
  1210. char **src, *dest;
  1211. unsigned *length;
  1212. {
  1213.     int n, len, quoteflag;
  1214.  
  1215.     quoteflag = FALSE;
  1216.     n = 0;
  1217.     len = *length - 1;
  1218.     while ((n < len) && (**src)) {
  1219.     if (!quoteflag && (**src == ':')) {
  1220.          break;
  1221.     }
  1222.     if (**src == '"') {
  1223.         (*src)++;
  1224.         quoteflag = !quoteflag;
  1225.         continue;
  1226.     }
  1227.     if (**src == '\\') {
  1228.         (*src)++;
  1229.         if (! **src) {
  1230.         break;
  1231.         }
  1232.     }
  1233.     *dest++ = *(*src)++;
  1234.     n++;
  1235.     }
  1236.  
  1237.     /*
  1238.      * Remove that troublesome trailing whitespace. . .
  1239.      */
  1240.     while ((n > 0) && isspace(dest[-1])) {
  1241.     dest--;
  1242.     n--;
  1243.     }
  1244.  
  1245.     *dest = '\0';
  1246.     *length = n;
  1247.     return dest;
  1248. }
  1249.  
  1250.  
  1251.  
  1252. /*
  1253.  * Read the string indirectly pointed to by "src", update the caller's
  1254.  * pointer, and return a pointer to a malloc'ed shared_string structure
  1255.  * containing the string.
  1256.  *
  1257.  * The string is read using the same rules as get_string() above.
  1258.  */
  1259.  
  1260. PRIVATE struct shared_string *get_shared_string(src)
  1261. char **src;
  1262. {
  1263.     char retstring[MAXSTRINGLEN];
  1264.     struct shared_string *s;
  1265.     unsigned length;
  1266.  
  1267.     length = sizeof(retstring);
  1268.     (void) get_string(src, retstring, &length);
  1269.  
  1270.     s = (struct shared_string *) smalloc(sizeof(struct shared_string)
  1271.                      + length);
  1272.     s->linkcount = 1;
  1273.     strcpy(s->string, retstring);
  1274.  
  1275.     return s;
  1276. }
  1277.  
  1278.  
  1279.  
  1280. /*
  1281.  * Load RFC1048 generic information directly into a memory buffer.
  1282.  *
  1283.  * "src" indirectly points to the ASCII representation of the generic data.
  1284.  * "dest" points to a string structure which is updated to point to a new
  1285.  * string with the new data appended to the old string.  The old string is
  1286.  * freed.
  1287.  *
  1288.  * The given tag value is inserted with the new data.
  1289.  *
  1290.  * The data may be represented as either a stream of hexadecimal numbers
  1291.  * representing bytes (any or all bytes may optionally start with '0x' and
  1292.  * be separated with periods ".") or as a quoted string of ASCII
  1293.  * characters (the quotes are required).
  1294.  */
  1295.  
  1296. PRIVATE void process_generic(src, dest, tagvalue)
  1297. char **src;
  1298. struct shared_bindata **dest;
  1299. byte tagvalue;
  1300. {
  1301.     byte tmpbuf[MAXBUFLEN];
  1302.     byte *str;
  1303.     struct shared_bindata *bdata;
  1304.     int newlength, oldlength;
  1305.  
  1306.     str = tmpbuf;
  1307.     *str++ = tagvalue;        /* Store tag value */
  1308.     str++;            /* Skip over length field */
  1309.     if ((*src)[0] == '"') {                /* ASCII data */
  1310.     newlength = sizeof(tmpbuf) - 2;    /* Set maximum allowed length */
  1311.     (void) get_string(src, str, &newlength);
  1312.     } else {                        /* Numeric data */
  1313.     newlength = 0;
  1314.     while (newlength < sizeof(tmpbuf) - 2) {
  1315.         if (interp_byte(src, str++) < 0) {
  1316.         break;
  1317.         } else {
  1318.         newlength++;
  1319.         }
  1320.         if (**src == '.') {
  1321.         (*src)++;
  1322.         }
  1323.     }    
  1324.     }
  1325.     tmpbuf[1] = (byte) (newlength & 0xFF);
  1326.     oldlength = ((*dest)->length);
  1327.     bdata = (struct shared_bindata *) smalloc(sizeof(struct shared_bindata)
  1328.                         + oldlength + newlength + 1);
  1329.     if (oldlength > 0) {
  1330.     bcopy((*dest)->data, bdata->data, oldlength);
  1331.     }
  1332.     bcopy(tmpbuf, bdata->data + oldlength, newlength + 2);
  1333.     bdata->length = oldlength + newlength + 2;
  1334.     bdata->linkcount = 1;
  1335.     if (*dest) {
  1336.     del_bindata(*dest);
  1337.     }
  1338.     *dest = bdata;
  1339. }
  1340.  
  1341.  
  1342.  
  1343. /*
  1344.  * Verify that the given string makes sense as a hostname (according to
  1345.  * Appendix 1, page 29 of RFC882).
  1346.  *
  1347.  * Return TRUE for good names, FALSE otherwise.
  1348.  */
  1349.  
  1350. PRIVATE boolean goodname(hostname)
  1351. register char *hostname;
  1352. {
  1353.     do {
  1354.     if (!isalpha(*hostname++)) {    /* First character must be a letter */
  1355.         return FALSE;
  1356.     }
  1357.     while (isalnum(*hostname) || (*hostname == '-')) {
  1358.         hostname++;            /* Alphanumeric or a hyphen */
  1359.     }
  1360.     if (!isalnum(hostname[-1])) {    /* Last must be alphanumeric */
  1361.         return FALSE;
  1362.     }
  1363.     if (*hostname == '\0') {    /* Done? */
  1364.         return TRUE;
  1365.     }
  1366.     } while (*hostname++ == '.');    /* Dot, loop for next label */
  1367.  
  1368.     return FALSE;            /* If it's not a dot, lose */
  1369. }
  1370.  
  1371.  
  1372.  
  1373. /*
  1374.  * Null compare function -- always returns FALSE so an element is always
  1375.  * inserted into a hash table (i.e. there is never a collision with an
  1376.  * existing element).
  1377.  */
  1378.  
  1379. PRIVATE boolean nullcmp(host1, host2)
  1380. struct host *host1, *host2;
  1381. {
  1382.     return FALSE;
  1383. }
  1384.  
  1385.  
  1386. /*
  1387.  * Function for comparing a string with the hostname field of a host
  1388.  * structure.
  1389.  */
  1390.  
  1391. PRIVATE boolean nmcmp(name, hp)
  1392. char *name;
  1393. struct host *hp;
  1394. {
  1395.     return !strcmp(name, hp->hostname->string);
  1396. }
  1397.  
  1398.  
  1399. /*
  1400.  * Compare function to determine whether two hardware addresses are
  1401.  * equivalent.  Returns TRUE if "host1" and "host2" are equivalent, FALSE
  1402.  * otherwise.
  1403.  *
  1404.  * If the hardware addresses of "host1" and "host2" are identical, but
  1405.  * they are on different IP subnets, this function returns FALSE.
  1406.  *
  1407.  * This function is used when inserting elements into the hardware address
  1408.  * hash table.
  1409.  */
  1410.  
  1411. PRIVATE boolean hwinscmp(host1, host2)
  1412. struct host *host1, *host2;
  1413. {
  1414.     if (host1->htype != host2->htype) {
  1415.     return FALSE;
  1416.     }
  1417.     if (bcmp(host1->haddr, host2->haddr, haddrlength(host1->htype))) {
  1418.     return FALSE;
  1419.     }
  1420.     if ((host1->subnet_mask.s_addr) == (host2->subnet_mask.s_addr)) {
  1421.     if (((host1->iaddr.s_addr) & (host1->subnet_mask.s_addr)) !=
  1422.         ((host2->iaddr.s_addr) & (host2->subnet_mask.s_addr))) {
  1423.         return FALSE;
  1424.     }
  1425.     }
  1426.     return TRUE;
  1427. }
  1428.  
  1429.  
  1430.  
  1431. /*
  1432.  * Process the "similar entry" symbol.
  1433.  *
  1434.  * The host specified as the value of the "tc" symbol is used as a template
  1435.  * for the current host entry.  Symbol values not explicitly set in the
  1436.  * current host entry are inferred from the template entry.
  1437.  */
  1438.  
  1439. PRIVATE void fill_defaults(hp, src)
  1440. struct host *hp;
  1441. char **src;
  1442. {
  1443.     unsigned tlen, hashcode;
  1444.     struct host *hp2, thp;
  1445.     char tstring[MAXSTRINGLEN];
  1446.  
  1447.     tlen = sizeof(tstring);
  1448.     (void) get_string(src, tstring, &tlen);
  1449.     if (goodname(tstring)) {
  1450.     hashcode = hash_HashFunction(tstring, tlen);
  1451.     hp2 = (struct host *) hash_Lookup(nmhashtable, hashcode, nmcmp,
  1452.                       tstring);
  1453.     } else {
  1454.     thp.iaddr.s_addr = inet_addr(tstring);
  1455.     hashcode = hash_HashFunction(&(thp.iaddr.s_addr), 4);
  1456.     hp2 = (struct host *) hash_Lookup(iphashtable, hashcode, iplookcmp,
  1457.                       &thp);
  1458.     }
  1459.     if (hp2 == NULL) {
  1460.     report(LOG_ERR, "can't find tc=\"%s\"\n", tstring);
  1461.     } else {
  1462.     /*
  1463.      * Assignments inside "if" conditionals are intended here.
  1464.      */
  1465.     if (!hp->flags.cookie_server) {
  1466.         if (hp->flags.cookie_server = hp2->flags.cookie_server) {
  1467.         hp->cookie_server = hp2->cookie_server;
  1468.         (hp->cookie_server->linkcount)++;
  1469.         }
  1470.     }
  1471.     if (!hp->flags.domain_server) {
  1472.         if (hp->flags.domain_server = hp2->flags.domain_server) {
  1473.         hp->domain_server = hp2->domain_server;
  1474.         (hp->domain_server->linkcount)++;
  1475.         }
  1476.     }
  1477.     if (!hp->flags.gateway) {
  1478.         if (hp->flags.gateway = hp2->flags.gateway) {
  1479.         hp->gateway = hp2->gateway;
  1480.         (hp->gateway->linkcount)++;
  1481.         }
  1482.     }
  1483.     if (!hp->flags.impress_server) {
  1484.         if (hp->flags.impress_server = hp2->flags.impress_server) {
  1485.         hp->impress_server = hp2->impress_server;
  1486.         (hp->impress_server->linkcount)++;
  1487.         }
  1488.     }
  1489.     if (!hp->flags.log_server) {
  1490.         if (hp->flags.log_server = hp2->flags.log_server) {
  1491.         hp->log_server = hp2->log_server;
  1492.         (hp->log_server->linkcount)++;
  1493.         }
  1494.     }
  1495.     if (!hp->flags.lpr_server) {
  1496.         if (hp->flags.lpr_server = hp2->flags.lpr_server) {
  1497.         hp->lpr_server = hp2->lpr_server;
  1498.         (hp->lpr_server->linkcount)++;
  1499.         }
  1500.     }
  1501.     if (!hp->flags.name_server) {
  1502.         if (hp->flags.name_server = hp2->flags.name_server) {
  1503.         hp->name_server = hp2->name_server;
  1504.         (hp->name_server->linkcount)++;
  1505.         }
  1506.     }
  1507.     if (!hp->flags.rlp_server) {
  1508.         if (hp->flags.rlp_server = hp2->flags.rlp_server) {
  1509.         hp->rlp_server = hp2->rlp_server;
  1510.         (hp->rlp_server->linkcount)++;
  1511.         }
  1512.     }
  1513.     if (!hp->flags.time_server) {
  1514.         if (hp->flags.time_server = hp2->flags.time_server) {
  1515.         hp->time_server = hp2->time_server;
  1516.         (hp->time_server->linkcount)++;
  1517.         }
  1518.     }
  1519.     if (!hp->flags.homedir) {
  1520.         if (hp->flags.homedir = hp2->flags.homedir) {
  1521.         hp->homedir = hp2->homedir;
  1522.         (hp->homedir->linkcount)++;
  1523.         }
  1524.     }
  1525.     if (!hp->flags.bootfile) {
  1526.         if (hp->flags.bootfile = hp2->flags.bootfile) {
  1527.         hp->bootfile = hp2->bootfile;
  1528.         (hp->bootfile->linkcount)++;
  1529.         }
  1530.     }
  1531.     if (!hp->flags.generic) {
  1532.         if (hp->flags.generic = hp2->flags.generic) {
  1533.         hp->generic = hp2->generic;
  1534.         (hp->generic->linkcount)++;
  1535.         }
  1536.     }
  1537.     if (!hp->flags.vendor_magic) {
  1538.         hp->flags.vm_auto = hp2->flags.vm_auto;
  1539.         if (hp->flags.vendor_magic = hp2->flags.vendor_magic) {
  1540.         bcopy(hp2->vm_cookie, hp->vm_cookie, 4);
  1541.         }
  1542.     }
  1543.     if (!hp->flags.name_switch) {
  1544.         if (hp->flags.name_switch = hp2->flags.name_switch) {
  1545.         hp->flags.send_name = hp2->flags.send_name;
  1546.         }
  1547.     }
  1548.     if (!hp->flags.htype) {
  1549.         if (hp->flags.htype = hp2->flags.htype) {
  1550.         hp->htype = hp2->htype;
  1551.         }
  1552.     }
  1553.     if (!hp->flags.time_offset) {
  1554.         if (hp->flags.time_offset = hp2->flags.time_offset) {
  1555.         hp->flags.timeoff_auto = hp2->flags.timeoff_auto;
  1556.         hp->time_offset = hp2->time_offset;
  1557.         }
  1558.     }
  1559.     if (!hp->flags.subnet_mask) {
  1560.         if (hp->flags.subnet_mask = hp2->flags.subnet_mask) {
  1561.         hp->subnet_mask.s_addr = hp2->subnet_mask.s_addr;
  1562.         }
  1563.     }
  1564.     if (!hp->flags.bootsize) {
  1565.         if (hp->flags.bootsize = hp2->flags.bootsize) {
  1566.         hp->flags.bootsize_auto = hp2->flags.bootsize_auto;
  1567.         hp->bootsize = hp2->bootsize;
  1568.         }
  1569.     }
  1570.     if (!hp->flags.tftpdir) {
  1571.         if (hp->flags.tftpdir = hp2->flags.tftpdir) {
  1572.         hp->tftpdir = hp2->tftpdir;
  1573.         (hp->tftpdir->linkcount)++;
  1574.         }
  1575.     }
  1576.     }
  1577. }
  1578.  
  1579.  
  1580.  
  1581. /*
  1582.  * This function adjusts the caller's pointer to point just past the
  1583.  * first-encountered colon.  If it runs into a null character, it leaves
  1584.  * the pointer pointing to it.
  1585.  */
  1586.  
  1587. PRIVATE void adjust(s)
  1588. char **s;
  1589. {
  1590.     register char *t;
  1591.  
  1592.     t = *s;
  1593.     while (*t && (*t != ':')) {
  1594.     t++;
  1595.     }
  1596.     if (*t) {
  1597.     t++;
  1598.     }
  1599.     *s = t;
  1600. }
  1601.  
  1602.  
  1603.  
  1604.  
  1605. /*
  1606.  * This function adjusts the caller's pointer to point to the first
  1607.  * non-whitespace character.  If it runs into a null character, it leaves
  1608.  * the pointer pointing to it.
  1609.  */
  1610.  
  1611. PRIVATE void eat_whitespace(s)
  1612. char **s;
  1613. {
  1614.     register char *t;
  1615.  
  1616.     t = *s;
  1617.     while (*t && isspace(*t)) {
  1618.     t++;
  1619.     }
  1620.     *s = t;
  1621. }
  1622.  
  1623.  
  1624.  
  1625. /*
  1626.  * This function converts the given string to all lowercase.
  1627.  */
  1628.  
  1629. PRIVATE void makelower(s)
  1630. char *s;
  1631. {
  1632.     while (*s) {
  1633.     if (isupper(*s)) {
  1634.         *s = tolower(*s);
  1635.     }
  1636.     s++;
  1637.     }
  1638. }
  1639.  
  1640.  
  1641.  
  1642. /*
  1643.  *
  1644.  *    N O T E :
  1645.  *
  1646.  *    In many of the functions which follow, a parameter such as "src" or
  1647.  *    "symbol" is passed as a pointer to a pointer to something.  This is
  1648.  *    done for the purpose of letting the called function update the
  1649.  *    caller's copy of the parameter (i.e. to effect call-by-reference
  1650.  *    parameter passing).  The value of the actual parameter is only used
  1651.  *    to locate the real parameter of interest and then update this indirect
  1652.  *    parameter.
  1653.  *
  1654.  *    I'm sure somebody out there won't like this. . . .
  1655.  *
  1656.  *
  1657.  */
  1658.  
  1659.  
  1660.  
  1661. /*
  1662.  * "src" points to a character pointer which points to an ASCII string of
  1663.  * whitespace-separated IP addresses.  A pointer to an in_addr_list
  1664.  * structure containing the list of addresses is returned.  NULL is
  1665.  * returned if no addresses were found at all.  The pointer pointed to by
  1666.  * "src" is updated to point to the first non-address (illegal) character.
  1667.  */
  1668.  
  1669. PRIVATE struct in_addr_list *get_addresses(src)
  1670. char **src;
  1671. {
  1672.     struct in_addr tmpaddrlist[MAXINADDRS];
  1673.     struct in_addr *address1, *address2;
  1674.     struct in_addr_list *result;
  1675.     unsigned addrcount, totalsize;
  1676.  
  1677.     address1 = tmpaddrlist;
  1678.     for (addrcount = 0; addrcount < MAXINADDRS; addrcount++) {
  1679.     while (**src && isspace(**src)) {    /* Skip whitespace */
  1680.         (*src)++;
  1681.     }
  1682.     if (! **src) {                /* Quit if nothing more */
  1683.         break;
  1684.     }
  1685.     if (prs_inetaddr(src, &(address1->s_addr)) < 0) {
  1686.         break;
  1687.     }
  1688.     address1++;            /* Point to next address slot */
  1689.     }
  1690.     if (addrcount < 1) {
  1691.     result = NULL;
  1692.     } else {
  1693.     totalsize = sizeof(struct in_addr_list)
  1694.             + (addrcount - 1) * sizeof(struct in_addr);
  1695.     result = (struct in_addr_list *) smalloc(totalsize);
  1696.     result->linkcount = 1;
  1697.     result->addrcount = addrcount;
  1698.     address1 = tmpaddrlist;
  1699.     address2 = result->addr;
  1700.     for (; addrcount > 0; addrcount--) {
  1701.         address2->s_addr = address1->s_addr;
  1702.         address1++;
  1703.         address2++;
  1704.     }
  1705.     }
  1706.     return result;
  1707. }
  1708.  
  1709.  
  1710.  
  1711. /*
  1712.  * prs_inetaddr(src, result)
  1713.  *
  1714.  * "src" is a value-result parameter; the pointer it points to is updated
  1715.  * to point to the next data position.   "result" points to an unsigned long
  1716.  * in which an address is returned.
  1717.  *
  1718.  * This function parses the IP address string in ASCII "dot notation" pointed
  1719.  * to by (*src) and places the result (in network byte order) in the unsigned
  1720.  * long pointed to by "result".  For malformed addresses, -1 is returned,
  1721.  * (*src) points to the first illegal character, and the unsigned long pointed
  1722.  * to by "result" is unchanged.  Successful calls return 0.
  1723.  */
  1724.  
  1725. PRIVATE prs_inetaddr(src, result)
  1726. char **src;
  1727. u_long *result;
  1728. {
  1729.     register u_long value;
  1730.     u_long parts[4], *pp = parts;
  1731.     int n;
  1732.  
  1733.     if (!isdigit(**src)) {
  1734.     return -1;
  1735.     }
  1736. loop:
  1737.     value = get_u_long(src);
  1738.     if (**src == '.') {
  1739.     /*
  1740.      * Internet format:
  1741.      *    a.b.c.d
  1742.      *    a.b.c    (with c treated as 16-bits)
  1743.      *    a.b    (with b treated as 24 bits)
  1744.      */
  1745.     if (pp >= parts + 4) {
  1746.         return (-1);
  1747.     }
  1748.     *pp++ = value;
  1749.     (*src)++;
  1750.     goto loop;
  1751.     }
  1752.     /*
  1753.      * Check for trailing characters.
  1754.      */
  1755.     if (**src && !(isspace(**src) || (**src == ':'))) {
  1756.     return (-1);
  1757.     }
  1758.     *pp++ = value;
  1759.     /*
  1760.      * Construct the address according to
  1761.      * the number of parts specified.
  1762.      */
  1763.     n = pp - parts;
  1764.     switch (n) {
  1765.     case 1:                /* a -- 32 bits */
  1766.         value = parts[0];
  1767.         break;
  1768.     case 2:                /* a.b -- 8.24 bits */
  1769.         value = (parts[0] << 24) | (parts[1] & 0xFFFFFF);
  1770.         break;
  1771.     case 3:                /* a.b.c -- 8.8.16 bits */
  1772.         value = (parts[0] << 24) | ((parts[1] & 0xFF) << 16) |
  1773.             (parts[2] & 0xFFFF);
  1774.         break;
  1775.     case 4:                /* a.b.c.d -- 8.8.8.8 bits */
  1776.         value = (parts[0] << 24) | ((parts[1] & 0xFF) << 16) |
  1777.             ((parts[2] & 0xFF) << 8) | (parts[3] & 0xFF);
  1778.         break;
  1779.     default:
  1780.         return (-1);
  1781.     }
  1782.     *result = htonl(value);
  1783.     return (0);
  1784. }
  1785.  
  1786.  
  1787.  
  1788. /*
  1789.  * "src" points to a pointer which in turn points to a hexadecimal ASCII
  1790.  * string.  This string is interpreted as a hardware address and returned
  1791.  * as a pointer to the actual hardware address, represented as an array of
  1792.  * bytes.
  1793.  *
  1794.  * The ASCII string must have the proper number of digits for the specified
  1795.  * hardware type (e.g. twelve digits for a 48-bit Ethernet address).
  1796.  * Two-digit sequences (bytes) may be separated with periods (.)  and/or
  1797.  * prefixed with '0x' for readability, but this is not required.
  1798.  *
  1799.  * For bad addresses, the pointer which "src" points to is updated to point
  1800.  * to the start of the first two-digit sequence which was bad, and the
  1801.  * function returns a NULL pointer.
  1802.  */
  1803.  
  1804. PRIVATE byte *prs_haddr(src, htype)
  1805. char **src;
  1806. byte htype;
  1807. {
  1808.     static byte haddr[MAXHADDRLEN];
  1809.     byte *hptr;
  1810.     unsigned hlen;
  1811.  
  1812.     hlen = haddrlength(htype);        /* Get length of this address type */
  1813.     hptr = haddr;
  1814.     while  (hptr < haddr + hlen) {
  1815.     if (**src == '.') {
  1816.         (*src)++;
  1817.     }
  1818.     if (interp_byte(src, hptr++) < 0) {
  1819.         return NULL;
  1820.     }
  1821.     }
  1822.     return haddr;       
  1823. }
  1824.  
  1825.  
  1826.  
  1827. /*
  1828.  * "src" is a pointer to a character pointer which in turn points to a
  1829.  * hexadecimal ASCII representation of a byte.  This byte is read, the
  1830.  * character pointer is updated, and the result is deposited into the
  1831.  * byte pointed to by "retbyte".
  1832.  *
  1833.  * The usual '0x' notation is allowed but not required.  The number must be
  1834.  * a two digit hexadecimal number.  If the number is invalid, "src" and
  1835.  * "retbyte" are left untouched and -1 is returned as the function value.
  1836.  * Successful calls return 0.
  1837.  */
  1838.  
  1839. PRIVATE int interp_byte(src, retbyte)
  1840. char **src;
  1841. byte *retbyte;
  1842. {
  1843.     int v;
  1844.  
  1845.     if ((*src)[0] == '0' && (*src)[1] == 'x' || (*src)[1] == 'X') {
  1846.     (*src) += 2;    /* allow 0x for hex, but don't require it */
  1847.     }
  1848.     if (!isxdigit((*src)[0]) || !isxdigit((*src)[1])) {
  1849.     return -1;
  1850.     }
  1851.     if (sscanf(*src, "%2x", &v) != 1) {
  1852.     return -1;
  1853.     }
  1854.     (*src) += 2;
  1855.     *retbyte = (byte) (v & 0xFF);
  1856.     return 0;
  1857. }
  1858.  
  1859.  
  1860.  
  1861. /*
  1862.  * The parameter "src" points to a character pointer which points to an
  1863.  * ASCII string representation of an unsigned number.  The number is
  1864.  * returned as an unsigned long and the character pointer is updated to
  1865.  * point to the first illegal character.
  1866.  */
  1867.  
  1868. PRIVATE u_long get_u_long(src)
  1869. char **src;
  1870. {
  1871.     register u_long value, base;
  1872.     char c;
  1873.  
  1874.     /*
  1875.      * Collect number up to first illegal character.  Values are specified
  1876.      * as for C:  0x=hex, 0=octal, other=decimal.
  1877.      */
  1878.     value = 0;
  1879.     base = 10;
  1880.     if (**src == '0') {
  1881.     base = 8, (*src)++;
  1882.     }
  1883.     if (**src == 'x' || **src == 'X') {
  1884.     base = 16, (*src)++;
  1885.     }
  1886.     while (c = **src) {
  1887.     if (isdigit(c)) {
  1888.         value = (value * base) + (c - '0');
  1889.         (*src)++;
  1890.         continue;
  1891.     }
  1892.     if (base == 16 && isxdigit(c)) {
  1893.         value = (value << 4) + ((c & ~32) + 10 - 'A');
  1894.         (*src)++;
  1895.         continue;
  1896.     }
  1897.     break;
  1898.     }
  1899.     return value;
  1900. }
  1901.  
  1902.  
  1903.  
  1904. /*
  1905.  * Routines for deletion of data associated with the main data structure.
  1906.  */
  1907.  
  1908.  
  1909. /*
  1910.  * Frees the entire host data structure given.  Does nothing if the passed
  1911.  * pointer is NULL.
  1912.  */
  1913.  
  1914. PRIVATE void free_host(hostptr)
  1915. struct host *hostptr;
  1916. {
  1917.     if (hostptr) {
  1918.     del_iplist(hostptr->cookie_server);
  1919.     del_iplist(hostptr->domain_server);
  1920.     del_iplist(hostptr->gateway);
  1921.     del_iplist(hostptr->impress_server);
  1922.     del_iplist(hostptr->log_server);
  1923.     del_iplist(hostptr->lpr_server);
  1924.     del_iplist(hostptr->name_server);
  1925.     del_iplist(hostptr->rlp_server);
  1926.     del_iplist(hostptr->time_server);
  1927.     del_string(hostptr->hostname);
  1928.     del_string(hostptr->homedir);
  1929.     del_string(hostptr->bootfile);
  1930.     del_string(hostptr->tftpdir);
  1931.     del_bindata(hostptr->generic);
  1932.     free((char *) hostptr);
  1933.     }
  1934. }
  1935.  
  1936.  
  1937.  
  1938. /*
  1939.  * Decrements the linkcount on the given IP address data structure.  If the
  1940.  * linkcount goes to zero, the memory associated with the data is freed.
  1941.  */
  1942.  
  1943. PRIVATE void del_iplist(iplist)
  1944. struct in_addr_list *iplist;
  1945. {
  1946.     if (iplist) {
  1947.     if (! (--(iplist->linkcount))) {
  1948.         free((char *) iplist);
  1949.     }
  1950.     }
  1951. }
  1952.  
  1953.  
  1954.  
  1955. /*
  1956.  * Decrements the linkcount on a string data structure.  If the count
  1957.  * goes to zero, the memory associated with the string is freed.  Does
  1958.  * nothing if the passed pointer is NULL.
  1959.  */
  1960.  
  1961. PRIVATE void del_string(stringptr)
  1962. struct shared_string *stringptr;
  1963. {
  1964.     if (stringptr) {
  1965.     if (! (--(stringptr->linkcount))) {
  1966.         free((char *) stringptr);
  1967.     }
  1968.     }
  1969. }
  1970.  
  1971.  
  1972.  
  1973. /*
  1974.  * Decrements the linkcount on a shared_bindata data structure.  If the
  1975.  * count goes to zero, the memory associated with the data is freed.  Does
  1976.  * nothing if the passed pointer is NULL.
  1977.  */
  1978.  
  1979. PRIVATE void del_bindata(dataptr)
  1980. struct shared_bindata *dataptr;
  1981. {
  1982.     if (dataptr) {
  1983.     if (! (--(dataptr->linkcount))) {
  1984.         free((char *) dataptr);
  1985.     }
  1986.     }
  1987. }
  1988.  
  1989.  
  1990.  
  1991.  
  1992. /* smalloc()  --  safe malloc()
  1993.  *
  1994.  * Always returns a valid pointer (if it returns at all).  The allocated
  1995.  * memory is initialized to all zeros.  If malloc() returns an error, a
  1996.  * message is printed using the report() function and the program aborts
  1997.  * with a status of 1.
  1998.  */
  1999.  
  2000. PRIVATE char *smalloc(nbytes)
  2001. unsigned nbytes;
  2002. {
  2003.     char *retvalue;
  2004.  
  2005.     retvalue = malloc(nbytes);
  2006.     if (!retvalue) {
  2007.     report(LOG_ERR, "malloc() failure -- exiting\n");
  2008.     exit(1);    
  2009.     }
  2010.     bzero(retvalue, nbytes);
  2011.     return retvalue;
  2012. }
  2013.