home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / bsd_srcs / usr.sbin / sendmail / src / domain.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-04-20  |  7.7 KB  |  296 lines

  1. /*
  2.  * Copyright (c) 1986 Eric P. Allman
  3.  * Copyright (c) 1988 Regents of the University of California.
  4.  * All rights reserved.
  5.  *
  6.  * Redistribution and use in source and binary forms, with or without
  7.  * modification, are permitted provided that the following conditions
  8.  * are met:
  9.  * 1. Redistributions of source code must retain the above copyright
  10.  *    notice, this list of conditions and the following disclaimer.
  11.  * 2. Redistributions in binary form must reproduce the above copyright
  12.  *    notice, this list of conditions and the following disclaimer in the
  13.  *    documentation and/or other materials provided with the distribution.
  14.  * 3. All advertising materials mentioning features or use of this software
  15.  *    must display the following acknowledgement:
  16.  *    This product includes software developed by the University of
  17.  *    California, Berkeley and its contributors.
  18.  * 4. Neither the name of the University nor the names of its contributors
  19.  *    may be used to endorse or promote products derived from this software
  20.  *    without specific prior written permission.
  21.  *
  22.  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  23.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  24.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  25.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  26.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  27.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  28.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  29.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  30.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  31.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  32.  * SUCH DAMAGE.
  33.  */
  34.  
  35. #include "sendmail.h"
  36.  
  37. #ifndef lint
  38. #ifdef NAMED_BIND
  39. static char sccsid[] = "@(#)domain.c    5.23 (Berkeley) 3/2/91 (with name server)";
  40. #else
  41. static char sccsid[] = "@(#)domain.c    5.23 (Berkeley) 3/2/91 (without name server)";
  42. #endif
  43. #endif /* not lint */
  44.  
  45. #ifdef NAMED_BIND
  46.  
  47. #include <sys/param.h>
  48. #include <errno.h>
  49. #include <arpa/nameser.h>
  50. #include <resolv.h>
  51. #include <netdb.h>
  52.  
  53. typedef union {
  54.     HEADER qb1;
  55.     char qb2[PACKETSZ];
  56. } querybuf;
  57.  
  58. static char hostbuf[MAXMXHOSTS*PACKETSZ];
  59.  
  60. getmxrr(host, mxhosts, localhost, rcode)
  61.     char *host, **mxhosts, *localhost;
  62.     int *rcode;
  63. {
  64.     extern int h_errno;
  65.     register u_char *eom, *cp;
  66.     register int i, j, n, nmx;
  67.     register char *bp;
  68.     HEADER *hp;
  69.     querybuf answer;
  70.     int ancount, qdcount, buflen, seenlocal;
  71.     u_short pref, localpref, type, prefer[MAXMXHOSTS];
  72.  
  73.     errno = 0;
  74.     n = res_search(host, C_IN, T_MX, (char *)&answer, sizeof(answer));
  75.     if (n < 0)
  76.     {
  77.         if (tTd(8, 1))
  78.             printf("getmxrr: res_search failed (errno=%d, h_errno=%d)\n",
  79.                 errno, h_errno);
  80.         switch (h_errno)
  81.         {
  82.           case NO_DATA:
  83.           case NO_RECOVERY:
  84.             /* no MX data on this host */
  85.             goto punt;
  86.  
  87.           case HOST_NOT_FOUND:
  88.             /* the host just doesn't exist */
  89.             *rcode = EX_NOHOST;
  90.             break;
  91.  
  92.           case TRY_AGAIN:
  93.             /* couldn't connect to the name server */
  94.             if (!UseNameServer && errno == ECONNREFUSED)
  95.                 goto punt;
  96.  
  97.             /* it might come up later; better queue it up */
  98.             *rcode = EX_TEMPFAIL;
  99.             break;
  100.         }
  101.  
  102.         /* irreconcilable differences */
  103.         return (-1);
  104.     }
  105.  
  106.     /* find first satisfactory answer */
  107.     hp = (HEADER *)&answer;
  108.     cp = (u_char *)&answer + sizeof(HEADER);
  109.     eom = (u_char *)&answer + n;
  110.     for (qdcount = ntohs(hp->qdcount); qdcount--; cp += n + QFIXEDSZ)
  111.         if ((n = __dn_skipname(cp, eom)) < 0)
  112.             goto punt;
  113.     nmx = 0;
  114.     seenlocal = 0;
  115.     buflen = sizeof(hostbuf);
  116.     bp = hostbuf;
  117.     ancount = ntohs(hp->ancount);
  118.     while (--ancount >= 0 && cp < eom && nmx < MAXMXHOSTS) {
  119.         if ((n = dn_expand((u_char *)&answer,
  120.             eom, cp, (u_char *)bp, buflen)) < 0)
  121.             break;
  122.         cp += n;
  123.         GETSHORT(type, cp);
  124.          cp += sizeof(u_short) + sizeof(u_long);
  125.         GETSHORT(n, cp);
  126.         if (type != T_MX)  {
  127.             if (tTd(8, 1) || _res.options & RES_DEBUG)
  128.                 printf("unexpected answer type %d, size %d\n",
  129.                     type, n);
  130.             cp += n;
  131.             continue;
  132.         }
  133.         GETSHORT(pref, cp);
  134.         if ((n = dn_expand((u_char *)&answer,
  135.             eom, cp, (u_char *)bp, buflen)) < 0)
  136.             break;
  137.         cp += n;
  138.         if (!strcasecmp(bp, localhost)) {
  139.             if (seenlocal == 0 || pref < localpref)
  140.                 localpref = pref;
  141.             seenlocal = 1;
  142.             continue;
  143.         }
  144.         prefer[nmx] = pref;
  145.         mxhosts[nmx++] = bp;
  146.         n = strlen(bp) + 1;
  147.         bp += n;
  148.         buflen -= n;
  149.     }
  150.     if (nmx == 0) {
  151. punt:        mxhosts[0] = strcpy(hostbuf, host);
  152.         return(1);
  153.     }
  154.  
  155.     /* sort the records */
  156.     for (i = 0; i < nmx; i++) {
  157.         for (j = i + 1; j < nmx; j++) {
  158.             if (prefer[i] > prefer[j] ||
  159.                 (prefer[i] == prefer[j] && rand() % 1 == 0)) {
  160.                 register int temp;
  161.                 register char *temp1;
  162.  
  163.                 temp = prefer[i];
  164.                 prefer[i] = prefer[j];
  165.                 prefer[j] = temp;
  166.                 temp1 = mxhosts[i];
  167.                 mxhosts[i] = mxhosts[j];
  168.                 mxhosts[j] = temp1;
  169.             }
  170.         }
  171.         if (seenlocal && prefer[i] >= localpref) {
  172.             /*
  173.              * truncate higher pref part of list; if we're
  174.              * the best choice left, we should have realized
  175.              * awhile ago that this was a local delivery.
  176.              */
  177.             if (i == 0) {
  178.                 *rcode = EX_CONFIG;
  179.                 return(-1);
  180.             }
  181.             nmx = i;
  182.             break;
  183.         }
  184.     }
  185.     return(nmx);
  186. }
  187.  
  188. getcanonname(host, hbsize)
  189.     char *host;
  190.     int hbsize;
  191. {
  192.     extern int h_errno;
  193.     register u_char *eom, *cp;
  194.     register int n; 
  195.     HEADER *hp;
  196.     querybuf answer;
  197.     u_short type;
  198.     int first, ancount, qdcount, loopcnt;
  199.     char nbuf[PACKETSZ];
  200.  
  201.     loopcnt = 0;
  202. loop:
  203.     /*
  204.      * Use query type of ANY if possible (NO_WILDCARD_MX), which will
  205.      * find types CNAME, A, and MX, and will cause all existing records
  206.      * to be cached by our local server.  If there is (might be) a
  207.      * wildcard MX record in the local domain or its parents that are
  208.      * searched, we can't use ANY; it would cause fully-qualified names
  209.      * to match as names in a local domain.
  210.      */
  211. # ifdef NO_WILDCARD_MX
  212.     n = res_search(host, C_IN, T_ANY, (char *)&answer, sizeof(answer));
  213. # else
  214.     n = res_search(host, C_IN, T_CNAME, (char *)&answer, sizeof(answer));
  215. # endif
  216.     if (n < 0) {
  217.         if (tTd(8, 1))
  218.             printf("getcanonname:  res_search failed (errno=%d, h_errno=%d)\n",
  219.                 errno, h_errno);
  220.         return;
  221.     }
  222.  
  223.     /* find first satisfactory answer */
  224.     hp = (HEADER *)&answer;
  225.     ancount = ntohs(hp->ancount);
  226.  
  227.     /* we don't care about errors here, only if we got an answer */
  228.     if (ancount == 0) {
  229.         if (tTd(8, 1))
  230.             printf("rcode = %d, ancount=%d\n", hp->rcode, ancount);
  231.         return;
  232.     }
  233.     cp = (u_char *)&answer + sizeof(HEADER);
  234.     eom = (u_char *)&answer + n;
  235.     for (qdcount = ntohs(hp->qdcount); qdcount--; cp += n + QFIXEDSZ)
  236.         if ((n = __dn_skipname(cp, eom)) < 0)
  237.             return;
  238.  
  239.     /*
  240.      * just in case someone puts a CNAME record after another record,
  241.      * check all records for CNAME; otherwise, just take the first
  242.      * name found.
  243.      */
  244.     for (first = 1; --ancount >= 0 && cp < eom; cp += n) {
  245.         if ((n = dn_expand((u_char *)&answer,
  246.             eom, cp, (u_char *)nbuf, sizeof(nbuf))) < 0)
  247.             break;
  248.         if (first) {            /* XXX */
  249.             (void)strncpy(host, nbuf, hbsize);
  250.             host[hbsize - 1] = '\0';
  251.             first = 0;
  252.         }
  253.         cp += n;
  254.         GETSHORT(type, cp);
  255.          cp += sizeof(u_short) + sizeof(u_long);
  256.         GETSHORT(n, cp);
  257.         if (type == T_CNAME)  {
  258.             /*
  259.              * assume that only one cname will be found.  More
  260.              * than one is undefined.  Copy so that if dn_expand
  261.              * fails, `host' is still okay.
  262.              */
  263.             if ((n = dn_expand((u_char *)&answer,
  264.                 eom, cp, (u_char *)nbuf, sizeof(nbuf))) < 0)
  265.                 break;
  266.             (void)strncpy(host, nbuf, hbsize); /* XXX */
  267.             host[hbsize - 1] = '\0';
  268.             if (++loopcnt > 8)    /* never be more than 1 */
  269.                 return;
  270.             goto loop;
  271.         }
  272.     }
  273. }
  274.  
  275. #else /* not NAMED_BIND */
  276.  
  277. #include <netdb.h>
  278.  
  279. getcanonname(host, hbsize)
  280.     char *host;
  281.     int hbsize;
  282. {
  283.     struct hostent *hp;
  284.  
  285.     hp = gethostbyname(host);
  286.     if (hp == NULL)
  287.         return;
  288.  
  289.     if (strlen(hp->h_name) >= hbsize)
  290.         return;
  291.  
  292.     (void) strcpy(host, hp->h_name);
  293. }
  294.  
  295. #endif /* not NAMED_BIND */
  296.