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