home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / ip / dns / resolv+2.1.1 / gethostnamadr.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-05-13  |  29.2 KB  |  1,145 lines

  1. /*
  2.  * Copyright (c) 1985, 1988 Regents of the University of California.
  3.  * All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms are permitted
  6.  * provided that the above copyright notice and this paragraph are
  7.  * duplicated in all such forms and that any documentation,
  8.  * advertising materials, and other materials related to such
  9.  * distribution and use acknowledge that the software was developed
  10.  * by the University of California, Berkeley.  The name of the
  11.  * University may not be used to endorse or promote products derived
  12.  * from this software without specific prior written permission.
  13.  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
  14.  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
  15.  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  16.  */
  17.  
  18. #if defined(LIBC_SCCS) && !defined(lint)
  19. static char sccsid[] = "@(#)gethostnamadr.c    6.39 (Berkeley) 1/4/90";
  20. #endif /* LIBC_SCCS and not lint */
  21.  
  22. #include <sys/param.h>
  23. #include <sys/socket.h>
  24. #include <netinet/in.h>
  25. #include <ctype.h>
  26. #include <netdb.h>
  27. #include <stdio.h>
  28. #include <errno.h>
  29. #include <arpa/inet.h>
  30. #include <arpa/nameser.h>
  31. #include <resolv.h>
  32. #include <strings.h>
  33. #include <syslog.h>
  34.  
  35. #include <net/if.h>             /* for struct ifconf */
  36. #include <sys/sockio.h>         /* for SIOCGIFCONF */
  37.  
  38. #define    MAXALIASES    35
  39. #define    MAXADDRS    35
  40. #define MAXTRIMDOMAINS  4
  41.  
  42. #define _PATH_HOSTCONF    "/etc/host.conf"
  43. #define HOSTDB        "/etc/hosts"
  44.  
  45. #define SERVICE_NONE    0
  46. #define SERVICE_BIND    1
  47. #define SERVICE_HOSTS    2
  48. #define SERVICE_NIS    3
  49. #define SERVICE_MAX    3
  50.  
  51. #define CMD_ORDER    "order"
  52. #define CMD_TRIMDOMAIN    "trim"
  53. #define CMD_HMA        "multi"
  54. #define CMD_SPOOF    "nospoof"
  55. #define CMD_SPOOFALERT    "alert"
  56. #define CMD_REORDER    "reorder"
  57. #define CMD_ON        "on"
  58. #define CMD_OFF        "off"
  59. #define CMD_WARN    "warn"
  60. #define CMD_NOWARN    "warn off"
  61.  
  62. #define ORD_BIND    "bind"
  63. #define ORD_HOSTS    "hosts"
  64. #define ORD_NIS        "nis"
  65.  
  66. #define ENV_HOSTCONF    "RESOLV_HOST_CONF"
  67. #define ENV_SERVORDER    "RESOLV_SERV_ORDER"
  68. #define ENV_SPOOF    "RESOLV_SPOOF_CHECK"
  69. #define ENV_TRIM_OVERR    "RESOLV_OVERRIDE_TRIM_DOMAINS"
  70. #define ENV_TRIM_ADD    "RESOLV_ADD_TRIM_DOMAINS"
  71. #define ENV_HMA        "RESOLV_MULTI"
  72. #define ENV_REORDER    "RESOLV_REORDER"
  73.  
  74. #define TOKEN_SEPARATORS " ,;:"
  75.  
  76. static int service_order[SERVICE_MAX + 1];
  77. static int service_done = 0;
  78.  
  79. static char *h_addr_ptrs[MAXADDRS + 1];
  80.  
  81. static struct hostent host;
  82. static char *host_aliases[MAXALIASES];
  83. static char hostbuf[BUFSIZ+1];
  84. static struct in_addr host_addr;
  85. static FILE *hostf = NULL;
  86. static char hostaddr[MAXADDRS];
  87. static char *host_addrs[2];
  88. static int stayopen = 0;
  89. static int hosts_multiple_addrs = 0;
  90. static int spoof = 0;
  91. static int spoofalert = 0;
  92. static int reorder = 0;
  93. static char *trimdomain[MAXTRIMDOMAINS];
  94. static char trimdomainbuf[BUFSIZ];
  95. static int numtrimdomains = 0;
  96. char *strpbrk();
  97. extern char *strstr(), *strtok(), *getenv();
  98. static struct hostent *_getnishost();
  99.  
  100. #if PACKETSZ > 1024
  101. #define    MAXPACKET    PACKETSZ
  102. #else
  103. #define    MAXPACKET    1024
  104. #endif
  105.  
  106. typedef union {
  107.     HEADER hdr;
  108.     u_char buf[MAXPACKET];
  109. } querybuf;
  110.  
  111. typedef union {
  112.     long al;
  113.     char ac;
  114. } align;
  115.  
  116.  
  117. #ifdef SHLIB
  118. extern int h_errno;
  119. #else
  120. int h_errno;
  121. #endif
  122. extern errno;
  123.  
  124. static void
  125. dotrimdomain(c)
  126. char *c;
  127. {
  128.     /* assume c points to the start of a host name; trim off any 
  129.        domain name matching any of the trimdomains */
  130.     int d,l1,l2;
  131.     
  132.     for(d=0;d<numtrimdomains;d++){
  133.         l1=strlen(trimdomain[d]);
  134.         l2=strlen(c);
  135.         if(l2>l1 && !strcasecmp(c+l2-l1,trimdomain[d]))
  136.             *(c+(strlen(c)-l1))='\0';
  137.     }
  138. }
  139.  
  140. static struct hostent *
  141. trim_domains(h)
  142. struct hostent *h;
  143. {
  144.     if(numtrimdomains){
  145.         int i;
  146.         dotrimdomain(h->h_name);
  147.         for(i=0;h->h_aliases[i];i++)
  148.             dotrimdomain(h->h_aliases[i]);
  149.     }
  150.     return(h);
  151. }
  152.  
  153. #define bad_config_format(cmd) \
  154.     fprintf(stderr, "resolv+: %s: \"%s\" command incorrectly formatted.\n", hostconf, cmd);
  155.  
  156. /* static void debug_breakout(x)
  157. long x;
  158. {
  159.     register int a, b, c, d;
  160.  
  161.     d = x & 255; x >>= 8;
  162.     c = x & 255; x >>= 8;
  163.     b = x & 255; x >>= 8;
  164.     a = x & 255; x >>= 8;
  165.     fprintf(stderr, "resolv+: %d.%d.%d.%d", a,b,c,d );
  166.  
  167.     return;
  168. } */
  169.  
  170. /* reorder_addrs -- by Tom Limoncelli
  171.     Optimize order of an address list.
  172.  
  173.     gethostbyaddr() usually returns a list of addresses in some
  174.     arbitrary order.  Most programs use the first one and throw the
  175.     rest away.  This routine attempts to find a "best" address and
  176.     swap it into the first position in the list.  "Best" is defined
  177.     as "an address that is on a local subnet".  The search ends after
  178.     one "best" address is found.  If no "best" address is found,
  179.     nothing is changed.
  180.  
  181.     On first execution, a table is built of interfaces, netmasks,
  182.     and mask'ed addresses.  This is to speed up future queries but
  183.     may require you to reboot after changing internet addresses.
  184.     (doesn't everyone reboot after changing internet addresses?)
  185.  
  186.     This routine should not be called if gethostbyaddr() is about
  187.     to return only one address.
  188.  
  189. */
  190.  
  191. /* Hal Stern (June 1992) of Sun claimed that more than 4 ethernets in a
  192. Sun 4/690 would not work.  This variable is set to 10 to accomodate our
  193. version of reality */
  194. #define MAXINTERFACES (10)
  195.  
  196. static void reorder_addrs(h)
  197. struct hostent *h;
  198. {
  199.     static struct {
  200.         char iname[16];
  201.         u_long address, netmask;
  202.         } itab[MAXINTERFACES], *itp;
  203.     static numitab = -1;    /* number of used entries in itab */
  204.     register struct in_addr **r; /* pointer to entry in address list */
  205.     struct in_addr tmp; /* pointer to entry in address list */
  206.     register int cnt;
  207.  
  208.     /***    itab[] contains masked addresses and netmask of each interface.
  209.             numitab is -1 : table is empty.
  210.             numitab is 0  : should never happen.
  211.             numitab is 1,2,3,... :  number of valid entries in the table.
  212.     ***/
  213.     if (!numitab) return;    /* no entries in table */
  214.     if (numitab==-1) { /* build the table */
  215.         int fd, err;
  216.         struct ifconf ifs;
  217.         struct ifreq ifbuf[MAXINTERFACES], *p;
  218.         register struct sockaddr_in *q;
  219.         u_long address, netmask;
  220.         int endp;
  221.  
  222.         /* open a socket */
  223.         fd = socket(PF_INET, SOCK_STREAM, 0);
  224.         if (fd==-1) return;
  225.  
  226.         /**** get information about the first MAXINTERFACES interfaces ****/
  227.         /* set up the ifconf structure */
  228.         ifs.ifc_len = MAXINTERFACES * sizeof(struct ifreq);
  229.         ifs.ifc_buf = (caddr_t) ifbuf;
  230.         /* get a list of interfaces */
  231.         err = ioctl(fd, SIOCGIFCONF, &ifs);
  232.         if (err == -1) return;
  233.  
  234.         /**** cycle through each interface & get netmask & address ****/
  235.         endp = ifs.ifc_len / sizeof(struct ifreq);
  236.         itp = itab;
  237.         for (p = ifs.ifc_req; endp; p++, endp--) {
  238.             strcpy( itp->iname , p->ifr_name); /* copy interface name */
  239.  
  240.             err = ioctl(fd, SIOCGIFNETMASK, p); /* get netmask */
  241.             if (err == -1) continue;    /* error? skip this interface */
  242.             q = (struct sockaddr_in *) &(p->ifr_addr);
  243.             if (q->sin_family == AF_INET) netmask=q->sin_addr.s_addr;
  244.             else continue;    /* not internet protocol? skip this interface */
  245.  
  246.             err = ioctl(fd, SIOCGIFADDR, p); /* get address */
  247.             if (err == -1) continue;    /* error? skip this interface */
  248.             q = (struct sockaddr_in *) &(p->ifr_addr);
  249.             if (q->sin_family == AF_INET) address=q->sin_addr.s_addr;
  250.             else continue;    /* not internet protocol? skip this interface */
  251.  
  252.             /* store the masked address and netmask in the table */
  253.             address = address & netmask;    /* pre-mask the address */
  254.             if (!address) continue;    /* funny address? skip this interface */
  255.             itp->address = address;
  256.             itp->netmask = netmask;
  257.  
  258.             if (numitab == -1) numitab = 0;     /* first time through */
  259.             itp++; numitab++;
  260.         }
  261.         /**** clean up ****/
  262.         close(fd);
  263.         /**** if we still don't have a table, leave */
  264.         if (!numitab) return;
  265.     }
  266.  
  267.     /**** loop through table for each (address,interface) combo ****/
  268.     for ( r = (struct in_addr **) (h->h_addr_list); *r; r++) {    /* loop through the addresses */
  269.         for ( itp = itab, cnt=numitab; cnt; itp++,cnt--) {    /* loop though the interfaces */
  270.             if (( (*r)->s_addr & itp->netmask) == itp->address) {    /* compare */
  271.                 /* We found a match.  Swap it into [0] */
  272.                 bcopy( ((struct in_addr **) (h->h_addr_list))[0],    &tmp, sizeof(tmp));
  273.                 bcopy( (*r),    ((struct in_addr **) (h->h_addr_list))[0], sizeof(tmp));
  274.                 bcopy( &tmp,                                      (*r), sizeof(tmp));
  275.  
  276.                 return;    /* found one, don't need to continue */
  277.             }
  278.         } /* interfaces */
  279.     } /* addresses */
  280. }
  281.  
  282. static void
  283. init_services()
  284. {
  285.     char *cp, *dp, buf[BUFSIZ];
  286.     register int cc = 0;
  287.     FILE *fd;
  288.     char *tdp = trimdomainbuf;
  289.     char *hostconf;
  290.  
  291.     if(NULL==(hostconf=getenv(ENV_HOSTCONF))){
  292.         hostconf=_PATH_HOSTCONF;
  293.     }
  294.     if ((fd = (FILE *)fopen(hostconf, "r")) == NULL) {
  295.                 /* make some assumptions */
  296.         service_order[0] = SERVICE_BIND;
  297.         service_order[1] = SERVICE_NONE;
  298.     } else {
  299.         while (fgets(buf, BUFSIZ, fd) != NULL) {
  300.             if ((cp = rindex(buf, '\n')) != NULL)
  301.                 *cp = '\0';
  302.             if (buf[0] == '#')
  303.                 continue;
  304.  
  305. #define checkbuf(b, cmd) (!strncasecmp(b, cmd, strlen(cmd)))
  306.  
  307.             if (checkbuf(buf, CMD_ORDER)) {
  308.                 cp = strpbrk(buf, " \t");
  309.                 if (!cp) 
  310.                                 {
  311.                                     bad_config_format(CMD_ORDER);
  312.                                 } else 
  313.                                 {
  314.                                     do {
  315.                     while (*cp == ' ' || *cp == '\t')
  316.                         cp++;
  317.                     dp = strpbrk(cp, TOKEN_SEPARATORS);
  318.                     if (dp) *dp = '\0';
  319.                     if (checkbuf(cp, ORD_BIND))
  320.                         service_order[cc++] = SERVICE_BIND;
  321.                     else if (checkbuf(cp, ORD_HOSTS))
  322.                         service_order[cc++] = SERVICE_HOSTS;
  323.                     else if (checkbuf(cp, ORD_NIS))
  324.                         service_order[cc++] = SERVICE_NIS;
  325.                     else
  326.                                         {
  327.                                             bad_config_format(CMD_ORDER);
  328.                                             fprintf(stderr, "resolv+: \"%s\" is an invalid keyword\n", cp);
  329.                                             fprintf(stderr, "resolv+: valid keywords are: %s, %s and %s\n",
  330.                                                    ORD_BIND, ORD_HOSTS, ORD_NIS);
  331.                                         }
  332.                                         
  333.                     if (dp) cp = ++dp;
  334.                                     } while (dp != NULL);
  335.                                     if (cc == 0) 
  336.                                     {
  337.                                         bad_config_format(CMD_ORDER);
  338.                                         fprintf(stderr, "resolv+: search order not specified or unrecognized keyword, host resolution will fail.\n");
  339.                                     }
  340.                                 }
  341.                                         
  342.             } else if (checkbuf(buf, CMD_HMA)) {
  343.                             if ( cp = strpbrk(buf, " \t") ) 
  344.                             {
  345.                 while (*cp == ' ' || *cp == '\t') cp++;
  346.                 if (checkbuf(cp, CMD_ON))
  347.                     hosts_multiple_addrs = 1;
  348.                             } else
  349.                                 bad_config_format(CMD_HMA);
  350.                                 
  351.             } else if (checkbuf(buf, CMD_SPOOF)) {
  352.                             if ( cp = strpbrk(buf, " \t") ) 
  353.                             {
  354.                 while (*cp == ' ' || *cp == '\t') cp++;
  355.                 if (checkbuf(cp, CMD_ON))
  356.                     spoof = 1;
  357.                             } else
  358.                                 bad_config_format(CMD_SPOOF);
  359.  
  360.             } else if (checkbuf(buf, CMD_SPOOFALERT)) {
  361.                             if ( cp = strpbrk(buf, " \t") ) 
  362.                             {
  363.                 while (*cp == ' ' || *cp == '\t') cp++;
  364.                 if (checkbuf(cp, CMD_ON))
  365.                     spoofalert = 1;
  366.                             } else
  367.                                 bad_config_format(CMD_SPOOFALERT);
  368.                             
  369.             } else if (checkbuf(buf, CMD_REORDER)) {
  370.                 if (cp = strpbrk(buf, " \t")) {
  371.                 while (*cp == ' ' || *cp == '\t') cp++;
  372.                 if (checkbuf(cp, CMD_ON))
  373.                     reorder = 1;
  374.                 } else
  375.                     bad_config_format(CMD_REORDER);
  376.  
  377.             } else if (checkbuf(buf, CMD_TRIMDOMAIN)) {
  378.                 if(numtrimdomains<MAXTRIMDOMAINS){
  379.                                     if ( cp = strpbrk(buf, " \t") ) 
  380.                                     {
  381.                     while (*cp == ' ' || *cp == '\t') cp++;
  382.                                         if (cp) 
  383.                                         {
  384.                                             (void) strcpy(tdp,cp);    
  385.                                             trimdomain[numtrimdomains++]=tdp;
  386.                                             tdp += strlen(cp)+1;
  387.                                         } else
  388.                                             bad_config_format(CMD_TRIMDOMAIN);
  389.                                     } else
  390.                                         bad_config_format(CMD_TRIMDOMAIN);
  391.                 }
  392.             }
  393.         }
  394.         
  395.         service_order[cc] = SERVICE_NONE;
  396.     }
  397.     fclose(fd);
  398.     /* override service_order if environment variable */
  399.     if(NULL!=(cp=getenv(ENV_SERVORDER))){
  400.         cc=0;
  401.         if(NULL!=(cp=strtok(cp, TOKEN_SEPARATORS))){
  402.             do{
  403.                 if(checkbuf(cp, ORD_BIND))
  404.                     service_order[cc++] = SERVICE_BIND;
  405.                 else if (checkbuf(cp, ORD_HOSTS))
  406.                     service_order[cc++] = SERVICE_HOSTS;
  407.                 else if (checkbuf(cp, ORD_NIS))
  408.                     service_order[cc++] = SERVICE_NIS;
  409.             } while(cp=strtok(NULL, TOKEN_SEPARATORS));
  410.         service_order[cc] = SERVICE_NONE;
  411.         }
  412.     }
  413.     /* override spoof if environment variable */
  414.     if(NULL!=(cp=getenv(ENV_SPOOF))){
  415.         if(checkbuf(cp, CMD_WARN)){
  416.             spoof=1;
  417.             spoofalert=1;
  418.         } else if (checkbuf(cp, CMD_OFF)){
  419.             spoof=0;
  420.             spoofalert=0;
  421.         } else if (checkbuf(cp, CMD_NOWARN)){
  422.             spoof=1;
  423.             spoofalert=0;
  424.         } else {
  425.             spoof=1;
  426.         }
  427.     }
  428.  
  429.     /* override hma if environment variable */
  430.     if(NULL!=(cp=getenv(ENV_HMA)))
  431.         if(checkbuf(cp, CMD_ON))
  432.             hosts_multiple_addrs=1;
  433.         else
  434.             hosts_multiple_addrs=0;
  435.  
  436.     /* override reorder if environment variable */
  437.     if ((cp = getenv(ENV_REORDER)) != NULL)
  438.         if (checkbuf(cp, CMD_ON))
  439.             reorder = 1;
  440.         else
  441.             reorder = 0;
  442.  
  443.     /* add trimdomains from environment variable */
  444.     if(NULL!=(cp=getenv(ENV_TRIM_ADD))){
  445.         if(NULL!=(cp=strtok(cp, TOKEN_SEPARATORS))){
  446.             do{
  447.                 if(numtrimdomains<MAXTRIMDOMAINS){
  448.                     (void)strcpy(tdp, cp);
  449.                     trimdomain[numtrimdomains++]=tdp;
  450.                     tdp += strlen(cp)+1;
  451.                 }
  452.             } while(cp=strtok(NULL, TOKEN_SEPARATORS));
  453.         }
  454.     }
  455.  
  456.     /* override trimdomains from environment variable */
  457.     if(NULL!=(cp=getenv(ENV_TRIM_OVERR))){
  458.         numtrimdomains=0;
  459.         tdp=trimdomainbuf;
  460.         if(NULL!=(cp=strtok(cp, TOKEN_SEPARATORS))){
  461.             do{
  462.                 if(numtrimdomains<MAXTRIMDOMAINS){
  463.                     (void)strcpy(tdp, cp);
  464.                     trimdomain[numtrimdomains++]=tdp;
  465.                     tdp += strlen(cp)+1;
  466.                 }
  467.             } while(cp=strtok(NULL, TOKEN_SEPARATORS));
  468.         }
  469.     }
  470.     
  471.     service_done = 1;
  472. }
  473.  
  474. static struct hostent *
  475. getanswer(answer, anslen, iquery)
  476.     querybuf *answer;
  477.     int anslen;
  478.     int iquery;
  479. {
  480.     register HEADER *hp;
  481.     register u_char *cp;
  482.     register int n;
  483.     u_char *eom;
  484.     char *bp, **ap;
  485.     int type, class, buflen, ancount, qdcount;
  486.     int haveanswer, getclass = C_ANY;
  487.     char **hap;
  488.  
  489.     eom = answer->buf + anslen;
  490.     /*
  491.      * find first satisfactory answer
  492.      */
  493.     hp = &answer->hdr;
  494.     ancount = ntohs(hp->ancount);
  495.     qdcount = ntohs(hp->qdcount);
  496.     bp = hostbuf;
  497.     buflen = sizeof(hostbuf);
  498.     cp = answer->buf + sizeof(HEADER);
  499.     if (qdcount) {
  500.         if (iquery) {
  501.             if ((n = dn_expand((char *)answer->buf, eom,
  502.                  cp, bp, buflen)) < 0) {
  503.                 h_errno = NO_RECOVERY;
  504.                 return ((struct hostent *) NULL);
  505.             }
  506.             cp += n + QFIXEDSZ;
  507.             host.h_name = bp;
  508.             n = strlen(bp) + 1;
  509.             bp += n;
  510.             buflen -= n;
  511.         } else
  512.             cp += dn_skipname(cp, eom) + QFIXEDSZ;
  513.         while (--qdcount > 0)
  514.             cp += dn_skipname(cp, eom) + QFIXEDSZ;
  515.     } else if (iquery) {
  516.         if (hp->aa)
  517.             h_errno = HOST_NOT_FOUND;
  518.         else
  519.             h_errno = TRY_AGAIN;
  520.         return ((struct hostent *) NULL);
  521.     }
  522.     ap = host_aliases;
  523.     *ap = NULL;
  524.     host.h_aliases = host_aliases;
  525.     hap = h_addr_ptrs;
  526.     *hap = NULL;
  527. #if BSD >= 43 || defined(h_addr)    /* new-style hostent structure */
  528.     host.h_addr_list = h_addr_ptrs;
  529. #endif
  530.     haveanswer = 0;
  531.     while (--ancount >= 0 && cp < eom) {
  532.         if ((n = dn_expand((char *)answer->buf, eom, cp, bp, buflen)) < 0)
  533.             break;
  534.         cp += n;
  535.         type = _getshort(cp);
  536.          cp += sizeof(u_short);
  537.         class = _getshort(cp);
  538.          cp += sizeof(u_short) + sizeof(u_long);
  539.         n = _getshort(cp);
  540.         cp += sizeof(u_short);
  541.         if (type == T_CNAME) {
  542.             cp += n;
  543.             if (ap >= &host_aliases[MAXALIASES-1])
  544.                 continue;
  545.             *ap++ = bp;
  546.             n = strlen(bp) + 1;
  547.             bp += n;
  548.             buflen -= n;
  549.             continue;
  550.         }
  551.         if (iquery && type == T_PTR) {
  552.             if ((n = dn_expand((char *)answer->buf, eom,
  553.                 cp, bp, buflen)) < 0) {
  554.                 cp += n;
  555.                 continue;
  556.             }
  557.             cp += n;
  558.             host.h_name = bp;
  559.             return(&host);
  560.         }
  561.         if (iquery || type != T_A)  {
  562. #ifdef DEBUG
  563.             if (_res.options & RES_DEBUG)
  564.                 printf("unexpected answer type %d, size %d\n",
  565.                     type, n);
  566. #endif
  567.             cp += n;
  568.             continue;
  569.         }
  570.         if (haveanswer) {
  571.             if (n != host.h_length) {
  572.                 cp += n;
  573.                 continue;
  574.             }
  575.             if (class != getclass) {
  576.                 cp += n;
  577.                 continue;
  578.             }
  579.         } else {
  580.             host.h_length = n;
  581.             getclass = class;
  582.             host.h_addrtype = (class == C_IN) ? AF_INET : AF_UNSPEC;
  583.             if (!iquery) {
  584.                 host.h_name = bp;
  585.                 bp += strlen(bp) + 1;
  586.             }
  587.         }
  588.  
  589.         bp += sizeof(align) - ((u_long)bp % sizeof(align));
  590.  
  591.         if (bp + n >= &hostbuf[sizeof(hostbuf)]) {
  592. #ifdef DEBUG
  593.             if (_res.options & RES_DEBUG)
  594.                 printf("size (%d) too big\n", n);
  595. #endif
  596.             break;
  597.         }
  598.         bcopy((char *)cp, *hap++ = bp, n);
  599.         bp +=n;
  600.         cp += n;
  601.         haveanswer++;
  602.     }
  603.     if (haveanswer) {
  604.         *ap = NULL;
  605. #if BSD >= 43 || defined(h_addr)    /* new-style hostent structure */
  606.         *hap = NULL;
  607. #else
  608.         host.h_addr = h_addr_ptrs[0];
  609. #endif
  610.         return (&host);
  611.     } else {
  612.         h_errno = TRY_AGAIN;
  613.         return ((struct hostent *) NULL);
  614.     }
  615. }
  616.  
  617. struct hostent *
  618. gethostbyname(name)
  619.     char *name;
  620. {
  621.     querybuf buf;
  622.     register char *cp;
  623.     register int cc;
  624.     int n;
  625.     struct hostent *hp;
  626.     extern struct hostent *_gethtbyname();
  627.  
  628.     /*
  629.      * disallow names consisting only of digits/dots, unless
  630.      * they end in a dot.
  631.      */
  632.     if (isdigit(name[0]))
  633.         for (cp = name;; ++cp) {
  634.             if (!*cp) {
  635.                 if (*--cp == '.')
  636.                     break;
  637.                 /*
  638.                  * All-numeric, no dot at the end.
  639.                  * Fake up a hostent as if we'd actually
  640.                  * done a lookup.  What if someone types
  641.                  * 255.255.255.255?  The test below will
  642.                  * succeed spuriously... ???
  643.                  */
  644.                 if ((host_addr.s_addr = inet_addr(name)) == -1) {
  645.                     h_errno = HOST_NOT_FOUND;
  646.                     return((struct hostent *) NULL);
  647.                 }
  648.                 host.h_name = name;
  649.                 host.h_aliases = host_aliases;
  650.                 host_aliases[0] = NULL;
  651.                 host.h_addrtype = AF_INET;
  652.                 host.h_length = sizeof(u_long);
  653.                 h_addr_ptrs[0] = (char *)&host_addr;
  654.                 h_addr_ptrs[1] = (char *)0;
  655. #if BSD >= 43 || defined(h_addr)    /* new-style hostent structure */
  656.                 host.h_addr_list = h_addr_ptrs;
  657. #else
  658.                 host.h_addr = h_addr_ptrs[0];
  659. #endif
  660.                 return (&host);
  661.             }
  662.             if (!isdigit(*cp) && *cp != '.') 
  663.                 break;
  664.         }
  665.  
  666.     if (!service_done)
  667.         init_services();
  668.  
  669.     for (cc = 0; service_order[cc] != SERVICE_NONE &&
  670.          cc <= SERVICE_MAX; cc++) {
  671.         switch (service_order[cc]) {
  672.         case SERVICE_BIND:
  673.             if ((n = res_search(name, C_IN, T_A,
  674.                         buf.buf, sizeof(buf))) < 0) {
  675. #ifdef DEBUG
  676.                 if (_res.options & RES_DEBUG)
  677.                     printf("res_search failed\n");
  678. #endif
  679.                 break;
  680.             }
  681.             hp = getanswer(&buf, n, 0);
  682.             if (h_addr_ptrs[1] && reorder)
  683.                 reorder_addrs(hp);
  684.             if (hp) 
  685.                 return trim_domains(hp);
  686.             break;
  687.         case SERVICE_HOSTS:
  688.             hp = _gethtbyname(name);
  689.             if (h_addr_ptrs[1] && reorder)
  690.                 reorder_addrs(hp);
  691.             if (hp)
  692.                 return hp;
  693.             h_errno = HOST_NOT_FOUND;
  694.             break;
  695. #ifdef NIS
  696.         case SERVICE_NIS:
  697.             hp = _getnishost(name, "hosts.byname");
  698.             if (h_addr_ptrs[1] && reorder)
  699.                 reorder_addrs(hp);
  700.             if (hp)
  701.                 return hp;
  702.             h_errno = HOST_NOT_FOUND;
  703.             break;
  704. #endif /* NIS */
  705.         }
  706.     }
  707.     return ((struct hostent *) NULL);
  708. }
  709.  
  710. struct hostent *
  711. gethostbyaddr(addr, len, type)
  712.     char *addr;
  713.     int len, type;
  714. {
  715.     int n;
  716.     querybuf buf;
  717.     register int cc;
  718.     register struct hostent *hp;
  719.     char qbuf[MAXDNAME];
  720.     extern struct hostent *_gethtbyaddr();
  721.     
  722.     if (type != AF_INET)
  723.         return ((struct hostent *) NULL);
  724.  
  725.     if (!service_done)
  726.       init_services();
  727.  
  728.     cc = 0;
  729.     while (service_order[cc] != SERVICE_NONE) {
  730.             switch (service_order[cc]) {
  731.         case SERVICE_BIND:
  732.             (void)sprintf(qbuf, "%u.%u.%u.%u.in-addr.arpa",
  733.                       ((unsigned)addr[3] & 0xff),
  734.                       ((unsigned)addr[2] & 0xff),
  735.                       ((unsigned)addr[1] & 0xff),
  736.                       ((unsigned)addr[0] & 0xff));
  737.             n = res_query(qbuf, C_IN, T_PTR, (char *)&buf,
  738.                       sizeof(buf));
  739.             if (n < 0) {
  740. #ifdef DEBUG
  741.                 if (_res.options & RES_DEBUG)
  742.                     printf("res_query failed\n");
  743. #endif
  744.                 break;
  745.             }
  746.             hp = getanswer(&buf, n, 1);
  747.             if (hp) {
  748.                 if(spoof){
  749.                     /* Spoofing check code by
  750.                      * Caspar Dik <casper@fwi.uva.nl> 
  751.                      */
  752.                     char nambuf[MAXDNAME+1];
  753.                     int ntd, namelen = strlen(hp->h_name);
  754.                     char **addrs;
  755.                     
  756.                     if (namelen >= MAXDNAME)
  757.                         return (struct hostent *)NULL;
  758.                     (void) strcpy(nambuf,hp->h_name);
  759.                     nambuf[namelen] = '.';
  760.                     nambuf[namelen+1] = '\0';
  761.  
  762.                     /* 
  763.                      * turn off domain trimming,
  764.                      * call gethostbyname(), then turn    
  765.                      * it back on, if applicable. This
  766.                      * prevents domain trimming from
  767.                      * making the name comparison fail.
  768.                      */
  769.                     ntd=numtrimdomains; 
  770.                     numtrimdomains=0;
  771.                     hp=gethostbyname(nambuf);
  772.                     numtrimdomains=ntd;
  773.                     nambuf[namelen] = 0;
  774.                     /*
  775.                     * the name must exist and the name 
  776.                     * returned by gethostbyaddr must be 
  777.                     * the canonical name and therefore 
  778.                     * identical to the name returned by 
  779.                     * gethostbyname()
  780.                     */
  781.                     if (!hp || strcmp(nambuf, hp->h_name)){
  782.                         h_errno = HOST_NOT_FOUND;
  783.                         return (struct hostent *)NULL;
  784.                     }
  785.                     /*
  786.                     * now check the addresses
  787.                     */
  788. #if defined(h_addr) || BSD >= 43
  789.                     for (addrs = hp->h_addr_list; 
  790.                         *addrs; addrs++){
  791.                         if (!bcmp(addrs[0], addr, len))
  792.                             return trim_domains(hp);
  793.                     }
  794. #else
  795.                     if (!bcmp(hp->h_addr, addr, len)))
  796.                         return trim_domains(hp);
  797. #endif
  798.                     /* We've been spoofed */
  799.                     h_errno = HOST_NOT_FOUND;
  800.                     if(spoofalert){
  801.                         openlog("resolv", LOG_PID,
  802.                             LOG_AUTH);
  803.                         syslog(LOG_NOTICE,
  804.                             "gethostbyaddr: %s != %u.%u.%u.%u, possible spoof attempt",
  805.                             hp->h_name,
  806.                             ((unsigned)addr[0]&0xff),
  807.                             ((unsigned)addr[1]&0xff),
  808.                             ((unsigned)addr[2]&0xff),
  809.                             ((unsigned)addr[3]&0xff));
  810.                     } 
  811.                     return (struct hostent *)NULL;
  812.                 }
  813.                 hp->h_addrtype = type;
  814.                 hp->h_length = len;
  815.                 h_addr_ptrs[0] = (char *)&host_addr;
  816.                 h_addr_ptrs[1] = (char *)0;
  817.                 host_addr = *(struct in_addr *)addr;
  818. #if BSD < 43 && !defined(h_addr)    /* new-style hostent structure */
  819.                 hp->h_addr = h_addr_ptrs[0];
  820. #endif
  821.                 return trim_domains(hp);
  822.             }
  823.             h_errno = HOST_NOT_FOUND;
  824.             break;
  825.         case SERVICE_HOSTS:
  826.             hp = _gethtbyaddr(addr, len, type);
  827.             if (hp) 
  828.                 return hp;
  829.             h_errno = HOST_NOT_FOUND;
  830.             break;
  831. #ifdef NIS
  832.         case SERVICE_NIS:
  833.             (void)sprintf(qbuf, "%u.%u.%u.%u",
  834.                       ((unsigned)addr[0] & 0xff),
  835.                       ((unsigned)addr[1] & 0xff),
  836.                       ((unsigned)addr[2] & 0xff),
  837.                       ((unsigned)addr[3] & 0xff));
  838.             hp = _getnishost(qbuf, "hosts.byaddr");
  839.             if (hp)
  840.                 return hp;
  841.             h_errno = HOST_NOT_FOUND;
  842.             break;
  843. #endif /* NIS */
  844.         }
  845.         cc++;
  846.     }
  847.     return ((struct hostent *)NULL);
  848. }
  849.  
  850. _sethtent(f)
  851.     int f;
  852. {
  853.     if (hostf == NULL)
  854.         hostf = fopen(HOSTDB, "r" );
  855.     else
  856.         rewind(hostf);
  857.     stayopen |= f;
  858. }
  859.  
  860. _endhtent()
  861. {
  862.     if (hostf && !stayopen) {
  863.         (void) fclose(hostf);
  864.         hostf = NULL;
  865.     }
  866. }
  867.  
  868. struct hostent *
  869. _gethtent()
  870. {
  871.     char *p;
  872.     register char *cp, **q;
  873.  
  874.     if (hostf == NULL && (hostf = fopen(HOSTDB, "r" )) == NULL)
  875.         return (NULL);
  876. again:
  877.     if ((p = fgets(hostbuf, BUFSIZ, hostf)) == NULL)
  878.         return (NULL);
  879.     if (*p == '#')
  880.         goto again;
  881.     cp = strpbrk(p, "#\n");
  882.     if (cp == NULL)
  883.         goto again;
  884.     *cp = '\0';
  885.     cp = strpbrk(p, " \t");
  886.     if (cp == NULL)
  887.         goto again;
  888.     *cp++ = '\0';
  889.     /* THIS STUFF IS INTERNET SPECIFIC */
  890. #if BSD >= 43 || defined(h_addr)    /* new-style hostent structure */
  891.     host.h_addr_list = host_addrs;
  892. #endif
  893.     host.h_addr = hostaddr;
  894.     *((u_long *)host.h_addr) = inet_addr(p);
  895.     host.h_length = sizeof (u_long);
  896.     host.h_addrtype = AF_INET;
  897.     while (*cp == ' ' || *cp == '\t')
  898.         cp++;
  899.     host.h_name = cp;
  900.     q = host.h_aliases = host_aliases;
  901.     cp = strpbrk(cp, " \t");
  902.     if (cp != NULL) 
  903.         *cp++ = '\0';
  904.     while (cp && *cp) {
  905.         if (*cp == ' ' || *cp == '\t') {
  906.             cp++;
  907.             continue;
  908.         }
  909.         if (q < &host_aliases[MAXALIASES - 1])
  910.             *q++ = cp;
  911.         cp = strpbrk(cp, " \t");
  912.         if (cp != NULL)
  913.             *cp++ = '\0';
  914.     }
  915.     *q = NULL;
  916.     return (&host);
  917. }
  918.  
  919. /* if hosts_multiple_addrs set, then gethtbyname behaves as follows:
  920.  *  - for hosts with multiple addresses, return all addresses, such that
  921.  *  the first address is most likely to be one on the same net as the
  922.  *  host we're running on, if one exists. 
  923.  *  - like the dns version of gethostsbyname, the alias field is empty
  924.  *  unless the name being looked up is an alias itself, at which point the
  925.  *  alias field contains that name, and the name field contains the primary
  926.  *  name of the host. Unlike dns, however, this behavior will still take place
  927.  *  even if the alias applies only to one of the interfaces. 
  928.  *  - determining a "local" address to put first is dependant on the netmask 
  929.  *  being such that the least significant network bit is more significant 
  930.  *  than any host bit. Only strange netmasks will violate this. 
  931.  *  - we assume addresses fit into u_longs. That's quite internet specific.
  932.  *  - if the host we're running on is not in the host file, the address 
  933.  *  shuffling will not take place.
  934.  *                     - John DiMarco <jdd@cdf.toronto.edu>
  935.  */ 
  936. struct hostent *
  937. _gethtbyname(name)
  938.     char *name;
  939. {
  940.     register struct hostent *p;
  941.     register char **cp;
  942.     char **hap, **lhap, *bp, *lbp;
  943.     int htbuflen, locbuflen;
  944.     int found=0, localfound=0;
  945.     char localname[MAXHOSTNAMELEN];
  946.  
  947.     static char htbuf[BUFSIZ+1]; /* buffer for host addresses */
  948.     static char locbuf[BUFSIZ+1]; /* buffer for local hosts's addresses */
  949.     static char *ht_addr_ptrs[MAXADDRS+1];
  950.     static char *loc_addr_ptrs[MAXADDRS+1];
  951.     static struct hostent ht;
  952.     static char *aliases[MAXALIASES];
  953.     static char namebuf[MAXHOSTNAMELEN];
  954.     
  955.     hap = ht_addr_ptrs;
  956.     lhap = loc_addr_ptrs;
  957.     *hap = NULL;
  958.     *lhap = NULL;
  959.     bp=htbuf;
  960.     lbp=locbuf;
  961.     htbuflen = sizeof(htbuf);
  962.     locbuflen = sizeof(locbuf);
  963.  
  964.     aliases[0]=NULL;
  965.     aliases[1]=NULL;
  966.     (void) strcpy(namebuf, name);
  967.  
  968.     (void)gethostname(localname, sizeof(localname));
  969.  
  970.     _sethtent(0);
  971.     while (p = _gethtent()) {
  972.         if (strcasecmp(p->h_name, name) == 0) 
  973.             found++;
  974.         else 
  975.             for (cp = p->h_aliases; *cp != 0; cp++)
  976.                 if (strcasecmp(*cp, name) == 0){ 
  977.                     found++;
  978.                     aliases[0]=name;
  979.                     (void) strcpy(namebuf, p->h_name);
  980.                 }
  981.         if (strcasecmp(p->h_name, localname) == 0)
  982.             localfound++;
  983.         else
  984.             for (cp=p->h_aliases; *cp != 0; cp++)
  985.                 if (strcasecmp(*cp, localname) == 0)
  986.                     localfound++;
  987.  
  988.         if(found){
  989.             int n;
  990.  
  991.             if(!hosts_multiple_addrs){
  992.                 /* original behaviour requested */
  993.                 _endhtent();
  994.                 return(p);
  995.             }
  996.             n = p->h_length;
  997.  
  998.             ht.h_addrtype = p->h_addrtype;
  999.             ht.h_length = p->h_length;
  1000.  
  1001.             if(n<=htbuflen){
  1002.                 /* add the found address to the list */
  1003.                 bcopy(p->h_addr_list[0], bp, n);
  1004.                 *hap++=bp;
  1005.                 *hap=NULL;
  1006.                 bp+=n;
  1007.                 htbuflen-=n;
  1008.             }
  1009.             found=0;
  1010.         }
  1011.         if(localfound){
  1012.             int n = p->h_length;
  1013.             if(n<=locbuflen){
  1014.                 /* add the found local address to the list */
  1015.                 bcopy(p->h_addr_list[0], lbp, n);
  1016.                 *lhap++=lbp;
  1017.                 *lhap=NULL;
  1018.                 lbp+=n;
  1019.                 locbuflen-=n;
  1020.             }
  1021.             localfound=0;
  1022.         }
  1023.     }
  1024.     _endhtent();
  1025.  
  1026.     if(NULL==ht_addr_ptrs[0]){
  1027.         return((struct hostent *)NULL);
  1028.     }
  1029.  
  1030.     ht.h_aliases = aliases; 
  1031.     ht.h_name = namebuf;
  1032.  
  1033.     /* shuffle addresses around to ensure one on same net as local host 
  1034.        is first, if exists */
  1035.     {
  1036.         /* "best" address is assumed to be the one with the greatest
  1037.            number of leftmost bits matching any of the addresses of
  1038.            the local host. This assumes a netmask in which all net
  1039.            bits precede host bits. Usually but not always a fair 
  1040.            assumption. */
  1041.  
  1042.         /* portability alert: assumption: iaddr fits in u_long.
  1043.            This is really internet specific. */
  1044.         int i,j, best=0;
  1045.         u_long bestval = (u_long)~0;
  1046.         
  1047.         for(i=0;loc_addr_ptrs[i];i++){
  1048.             for(j=0;ht_addr_ptrs[j];j++){
  1049.                 u_long t, l, h;
  1050.                 /* assert(sizeof(u_long)>=ht.h_length); */
  1051.                 bcopy(loc_addr_ptrs[i], (char *)&t,
  1052.                     ht.h_length);
  1053.                 l=ntohl(t);
  1054.                 bcopy(ht_addr_ptrs[j], (char *)&t, 
  1055.                     ht.h_length);
  1056.                 t=l^h;
  1057.  
  1058.                 if(t<bestval){
  1059.                     best=j;
  1060.                     bestval=t;
  1061.                 }
  1062.             }
  1063.         }
  1064.         if(best){
  1065.             char *tmp;
  1066.  
  1067.             /* swap first and best address */
  1068.             tmp=ht_addr_ptrs[0];
  1069.             ht_addr_ptrs[0]=ht_addr_ptrs[best];
  1070.             ht_addr_ptrs[best]=tmp;
  1071.         }
  1072.     }
  1073.     
  1074.     ht.h_addr_list = ht_addr_ptrs;
  1075.     return (&ht);
  1076. }
  1077.  
  1078. #ifdef NIS
  1079. static struct hostent *
  1080. _getnishost(name, map)
  1081.     char *name, *map;
  1082. {
  1083.     register char *cp, *dp, **q;
  1084.     char *result;
  1085.     int resultlen;
  1086.     static struct hostent h;
  1087.     static char *domain = (char *)NULL;
  1088.  
  1089.     if (domain == (char *)NULL)
  1090.         if (yp_get_default_domain (&domain))
  1091.             return ((struct hostent *)NULL);
  1092.  
  1093.     if (yp_match(domain, map, name, strlen(name), &result, &resultlen))
  1094.         return ((struct hostent *)NULL);
  1095.  
  1096.     if (cp = index(result, '\n'))
  1097.         *cp = '\0';
  1098.  
  1099.     cp = strpbrk(result, " \t");
  1100.     *cp++ = '\0';
  1101. #if BSD >= 43 || defined(h_addr)    /* new-style hostent structure */
  1102.     h.h_addr_list = host_addrs;
  1103. #endif
  1104.     h.h_addr = hostaddr;
  1105.     *((u_long *)h.h_addr) = inet_addr(result);
  1106.     h.h_length = sizeof(u_long);
  1107.     h.h_addrtype = AF_INET;
  1108.     while (*cp == ' ' || *cp == '\t')
  1109.         cp++;
  1110.     h.h_name = cp;
  1111.     q = h.h_aliases = host_aliases;
  1112.     cp = strpbrk(cp, " \t");
  1113.     if (cp != NULL)
  1114.         *cp++ = '\0';
  1115.     while (cp && *cp) {
  1116.         if (*cp == ' ' || *cp == '\t') {
  1117.             cp++;
  1118.             continue;
  1119.         }
  1120.         if (q < &host_aliases[MAXALIASES - 1])
  1121.             *q++ = cp;
  1122.         cp = strpbrk(cp, " \t");
  1123.         if (cp != NULL)
  1124.             *cp++ = '\0';
  1125.     }
  1126.     *q = NULL;
  1127.     return (&h);
  1128. }
  1129. #endif /* NIS */
  1130.  
  1131. struct hostent *
  1132. _gethtbyaddr(addr, len, type)
  1133.     char *addr;
  1134.     int len, type;
  1135. {
  1136.     register struct hostent *p;
  1137.  
  1138.     _sethtent(0);
  1139.     while (p = _gethtent())
  1140.         if (p->h_addrtype == type && !bcmp(p->h_addr, addr, len))
  1141.             break;
  1142.     _endhtent();
  1143.     return (p);
  1144. }
  1145.