resolv.c 24 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186
  1. /* resolv.c: DNS Resolver
  2. *
  3. * Copyright (C) 1998 Kenneth Albanowski <kjahds@kjahds.com>,
  4. * The Silver Hammer Group, Ltd.
  5. *
  6. * This library is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Library General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2 of the License, or (at your option) any later version.
  10. *
  11. * 5-Oct-2000 W. Greathouse wgreathouse@smva.com
  12. * Fix memory leak and memory corruption.
  13. * -- Every name resolution resulted in
  14. * a new parse of resolv.conf and new
  15. * copy of nameservers allocated by
  16. * strdup.
  17. * -- Every name resolution resulted in
  18. * a new read of resolv.conf without
  19. * resetting index from prior read...
  20. * resulting in exceeding array bounds.
  21. *
  22. * Limit nameservers read from resolv.conf
  23. *
  24. * Add "search" domains from resolv.conf
  25. *
  26. * Some systems will return a security
  27. * signature along with query answer for
  28. * dynamic DNS entries.
  29. * -- skip/ignore this answer
  30. *
  31. * Include arpa/nameser.h for defines.
  32. *
  33. * General cleanup
  34. *
  35. */
  36. #define __FORCE_GLIBC__
  37. #include <features.h>
  38. #include <string.h>
  39. #include <stdio.h>
  40. #include <signal.h>
  41. #include <errno.h>
  42. #include <sys/socket.h>
  43. #include <sys/types.h>
  44. #include <netinet/in.h>
  45. #include <arpa/inet.h>
  46. #include <stdlib.h>
  47. #include <unistd.h>
  48. #include <cfgfile.h>
  49. #include <resolv.h>
  50. #include <netdb.h>
  51. #include <ctype.h>
  52. #include <arpa/nameser.h>
  53. #define MAX_RECURSE 5
  54. #define REPLY_TIMEOUT 10
  55. #define MAX_RETRIES 15
  56. #define MAX_SERVERS 3
  57. #define MAX_SEARCH 4
  58. #undef DEBUG
  59. #ifdef DEBUG
  60. #define DPRINTF(X,args...) fprintf(stderr, X, ##args)
  61. #else
  62. #define DPRINTF(X,args...)
  63. #endif /* DEBUG */
  64. extern int nameservers;
  65. extern char * nameserver[MAX_SERVERS];
  66. extern int searchdomains;
  67. extern char * searchdomain[MAX_SEARCH];
  68. extern struct hostent * get_hosts_byname(const char * name);
  69. extern struct hostent * get_hosts_byaddr(const char * addr, int len, int type);
  70. extern struct hostent * read_etc_hosts(const char * name, int ip);
  71. extern int resolve_address(const char * address, int nscount,
  72. char ** nsip, struct in_addr * in);
  73. extern int resolve_mailbox(const char * address, int nscount,
  74. char ** nsip, struct in_addr * in);
  75. extern int dns_lookup(const char * name, int type, int nscount,
  76. char ** nsip, unsigned char ** outpacket, struct resolv_answer * a);
  77. #ifdef L_encodeh
  78. int encode_header(struct resolv_header *h, unsigned char *dest, int maxlen)
  79. {
  80. if (maxlen < HFIXEDSZ)
  81. return -1;
  82. dest[0] = (h->id & 0xff00) >> 8;
  83. dest[1] = (h->id & 0x00ff) >> 0;
  84. dest[2] = (h->qr ? 0x80 : 0) |
  85. ((h->opcode & 0x0f) << 3) |
  86. (h->aa ? 0x04 : 0) |
  87. (h->tc ? 0x02 : 0) |
  88. (h->rd ? 0x01 : 0);
  89. dest[3] = (h->ra ? 0x80 : 0) | (h->rcode & 0x0f);
  90. dest[4] = (h->qdcount & 0xff00) >> 8;
  91. dest[5] = (h->qdcount & 0x00ff) >> 0;
  92. dest[6] = (h->ancount & 0xff00) >> 8;
  93. dest[7] = (h->ancount & 0x00ff) >> 0;
  94. dest[8] = (h->nscount & 0xff00) >> 8;
  95. dest[9] = (h->nscount & 0x00ff) >> 0;
  96. dest[10] = (h->arcount & 0xff00) >> 8;
  97. dest[11] = (h->arcount & 0x00ff) >> 0;
  98. return HFIXEDSZ;
  99. }
  100. #endif
  101. #ifdef L_decodeh
  102. int decode_header(unsigned char *data, struct resolv_header *h)
  103. {
  104. h->id = (data[0] << 8) | data[1];
  105. h->qr = (data[2] & 0x80) ? 1 : 0;
  106. h->opcode = (data[2] >> 3) & 0x0f;
  107. h->aa = (data[2] & 0x04) ? 1 : 0;
  108. h->tc = (data[2] & 0x02) ? 1 : 0;
  109. h->rd = (data[2] & 0x01) ? 1 : 0;
  110. h->ra = (data[3] & 0x80) ? 1 : 0;
  111. h->rcode = data[3] & 0x0f;
  112. h->qdcount = (data[4] << 8) | data[5];
  113. h->ancount = (data[6] << 8) | data[7];
  114. h->nscount = (data[8] << 8) | data[9];
  115. h->arcount = (data[10] << 8) | data[11];
  116. return HFIXEDSZ;
  117. }
  118. #endif
  119. #ifdef L_encoded
  120. /* Encode a dotted string into nameserver transport-level encoding.
  121. This routine is fairly dumb, and doesn't attempt to compress
  122. the data */
  123. int encode_dotted(const char *dotted, unsigned char *dest, int maxlen)
  124. {
  125. int used = 0;
  126. while (dotted && *dotted) {
  127. char *c = strchr(dotted, '.');
  128. int l = c ? c - dotted : strlen(dotted);
  129. if (l >= (maxlen - used - 1))
  130. return -1;
  131. dest[used++] = l;
  132. memcpy(dest + used, dotted, l);
  133. used += l;
  134. if (c)
  135. dotted = c + 1;
  136. else
  137. break;
  138. }
  139. if (maxlen < 1)
  140. return -1;
  141. dest[used++] = 0;
  142. return used;
  143. }
  144. #endif
  145. #ifdef L_decoded
  146. /* Decode a dotted string from nameserver transport-level encoding.
  147. This routine understands compressed data. */
  148. int decode_dotted(const unsigned char *data, int offset,
  149. char *dest, int maxlen)
  150. {
  151. int l;
  152. int measure = 1;
  153. int total = 0;
  154. int used = 0;
  155. if (!data)
  156. return -1;
  157. while ((l=data[offset++])) {
  158. if (measure)
  159. total++;
  160. if ((l & 0xc0) == (0xc0)) {
  161. if (measure)
  162. total++;
  163. /* compressed item, redirect */
  164. offset = ((l & 0x3f) << 8) | data[offset];
  165. measure = 0;
  166. continue;
  167. }
  168. if ((used + l + 1) >= maxlen)
  169. return -1;
  170. memcpy(dest + used, data + offset, l);
  171. offset += l;
  172. used += l;
  173. if (measure)
  174. total += l;
  175. if (data[offset] != 0)
  176. dest[used++] = '.';
  177. else
  178. dest[used++] = '\0';
  179. }
  180. DPRINTF("Total decode len = %d\n", total);
  181. return total;
  182. }
  183. #endif
  184. #ifdef L_lengthd
  185. int length_dotted(const unsigned char *data, int offset)
  186. {
  187. int orig_offset = offset;
  188. int l;
  189. if (!data)
  190. return -1;
  191. while ((l = data[offset++])) {
  192. if ((l & 0xc0) == (0xc0)) {
  193. offset++;
  194. break;
  195. }
  196. offset += l;
  197. }
  198. return offset - orig_offset;
  199. }
  200. #endif
  201. #ifdef L_encodeq
  202. int encode_question(struct resolv_question *q,
  203. unsigned char *dest, int maxlen)
  204. {
  205. int i;
  206. i = encode_dotted(q->dotted, dest, maxlen);
  207. if (i < 0)
  208. return i;
  209. dest += i;
  210. maxlen -= i;
  211. if (maxlen < 4)
  212. return -1;
  213. dest[0] = (q->qtype & 0xff00) >> 8;
  214. dest[1] = (q->qtype & 0x00ff) >> 0;
  215. dest[2] = (q->qclass & 0xff00) >> 8;
  216. dest[3] = (q->qclass & 0x00ff) >> 0;
  217. return i + 4;
  218. }
  219. #endif
  220. #ifdef L_decodeq
  221. int decode_question(unsigned char *message, int offset,
  222. struct resolv_question *q)
  223. {
  224. char temp[256];
  225. int i;
  226. i = decode_dotted(message, offset, temp, sizeof(temp));
  227. if (i < 0)
  228. return i;
  229. offset += i;
  230. q->dotted = strdup(temp);
  231. q->qtype = (message[offset + 0] << 8) | message[offset + 1];
  232. q->qclass = (message[offset + 2] << 8) | message[offset + 3];
  233. return i + 4;
  234. }
  235. #endif
  236. #ifdef L_lengthq
  237. int length_question(unsigned char *message, int offset)
  238. {
  239. int i;
  240. i = length_dotted(message, offset);
  241. if (i < 0)
  242. return i;
  243. return i + 4;
  244. }
  245. #endif
  246. #ifdef L_encodea
  247. int encode_answer(struct resolv_answer *a, unsigned char *dest, int maxlen)
  248. {
  249. int i;
  250. i = encode_dotted(a->dotted, dest, maxlen);
  251. if (i < 0)
  252. return i;
  253. dest += i;
  254. maxlen -= i;
  255. if (maxlen < (RRFIXEDSZ+a->rdlength))
  256. return -1;
  257. *dest++ = (a->atype & 0xff00) >> 8;
  258. *dest++ = (a->atype & 0x00ff) >> 0;
  259. *dest++ = (a->aclass & 0xff00) >> 8;
  260. *dest++ = (a->aclass & 0x00ff) >> 0;
  261. *dest++ = (a->ttl & 0xff000000) >> 24;
  262. *dest++ = (a->ttl & 0x00ff0000) >> 16;
  263. *dest++ = (a->ttl & 0x0000ff00) >> 8;
  264. *dest++ = (a->ttl & 0x000000ff) >> 0;
  265. *dest++ = (a->rdlength & 0xff00) >> 8;
  266. *dest++ = (a->rdlength & 0x00ff) >> 0;
  267. memcpy(dest, a->rdata, a->rdlength);
  268. return i + RRFIXEDSZ + a->rdlength;
  269. }
  270. #endif
  271. #ifdef L_decodea
  272. int decode_answer(unsigned char *message, int offset,
  273. struct resolv_answer *a)
  274. {
  275. char temp[256];
  276. int i;
  277. i = decode_dotted(message, offset, temp, sizeof(temp));
  278. if (i < 0)
  279. return i;
  280. message += offset + i;
  281. a->dotted = strdup(temp);
  282. a->atype = (message[0] << 8) | message[1];
  283. message += 2;
  284. a->aclass = (message[0] << 8) | message[1];
  285. message += 2;
  286. a->ttl = (message[0] << 24) |
  287. (message[1] << 16) | (message[2] << 8) | (message[3] << 0);
  288. message += 4;
  289. a->rdlength = (message[0] << 8) | message[1];
  290. message += 2;
  291. a->rdata = message;
  292. a->rdoffset = offset + i + RRFIXEDSZ;
  293. DPRINTF("i=%d,rdlength=%d\n", i, a->rdlength);
  294. return i + RRFIXEDSZ + a->rdlength;
  295. }
  296. #endif
  297. #ifdef L_encodep
  298. int encode_packet(struct resolv_header *h,
  299. struct resolv_question **q,
  300. struct resolv_answer **an,
  301. struct resolv_answer **ns,
  302. struct resolv_answer **ar,
  303. unsigned char *dest, int maxlen)
  304. {
  305. int i, total = 0;
  306. int j;
  307. i = encode_header(h, dest, maxlen);
  308. if (i < 0)
  309. return i;
  310. dest += i;
  311. maxlen -= i;
  312. total += i;
  313. for (j = 0; j < h->qdcount; j++) {
  314. i = encode_question(q[j], dest, maxlen);
  315. if (i < 0)
  316. return i;
  317. dest += i;
  318. maxlen -= i;
  319. total += i;
  320. }
  321. for (j = 0; j < h->ancount; j++) {
  322. i = encode_answer(an[j], dest, maxlen);
  323. if (i < 0)
  324. return i;
  325. dest += i;
  326. maxlen -= i;
  327. total += i;
  328. }
  329. for (j = 0; j < h->nscount; j++) {
  330. i = encode_answer(ns[j], dest, maxlen);
  331. if (i < 0)
  332. return i;
  333. dest += i;
  334. maxlen -= i;
  335. total += i;
  336. }
  337. for (j = 0; j < h->arcount; j++) {
  338. i = encode_answer(ar[j], dest, maxlen);
  339. if (i < 0)
  340. return i;
  341. dest += i;
  342. maxlen -= i;
  343. total += i;
  344. }
  345. return total;
  346. }
  347. #endif
  348. #ifdef L_decodep
  349. int decode_packet(unsigned char *data, struct resolv_header *h)
  350. {
  351. return decode_header(data, h);
  352. }
  353. #endif
  354. #ifdef L_formquery
  355. int form_query(int id, const char *name, int type, unsigned char *packet,
  356. int maxlen)
  357. {
  358. struct resolv_header h;
  359. struct resolv_question q;
  360. int i, j;
  361. memset(&h, 0, sizeof(h));
  362. h.id = id;
  363. h.qdcount = 1;
  364. q.dotted = (char *) name;
  365. q.qtype = type;
  366. q.qclass = C_IN; /* CLASS_IN */
  367. i = encode_header(&h, packet, maxlen);
  368. if (i < 0)
  369. return i;
  370. j = encode_question(&q, packet + i, maxlen - i);
  371. if (j < 0)
  372. return j;
  373. return i + j;
  374. }
  375. #endif
  376. #ifdef L_dnslookup
  377. int dns_caught_signal = 0;
  378. void dns_catch_signal(int signo)
  379. {
  380. dns_caught_signal = 1;
  381. }
  382. int dns_lookup(const char *name, int type, int nscount, char **nsip,
  383. unsigned char **outpacket, struct resolv_answer *a)
  384. {
  385. static int id = 1;
  386. int i, j, len, fd, pos;
  387. static int ns = 0;
  388. struct sockaddr_in sa;
  389. int oldalarm;
  390. __sighandler_t oldhandler;
  391. struct resolv_header h;
  392. struct resolv_question q;
  393. int retries = 0;
  394. unsigned char * packet = malloc(PACKETSZ);
  395. unsigned char * lookup = malloc(MAXDNAME);
  396. int variant = 0;
  397. fd = -1;
  398. if (!packet || !lookup || !nscount)
  399. goto fail;
  400. DPRINTF("Looking up type %d answer for '%s'\n", type, name);
  401. ns %= nscount;
  402. while (retries++ < MAX_RETRIES) {
  403. if (fd != -1)
  404. close(fd);
  405. fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
  406. if (fd == -1)
  407. goto fail;
  408. memset(packet, 0, PACKETSZ);
  409. memset(&h, 0, sizeof(h));
  410. h.id = ++id;
  411. h.qdcount = 1;
  412. h.rd = 1;
  413. DPRINTF("encoding header\n", h.rd);
  414. i = encode_header(&h, packet, PACKETSZ);
  415. if (i < 0)
  416. goto fail;
  417. strncpy(lookup,name,MAXDNAME);
  418. if (variant < searchdomains && strchr(lookup, '.') == NULL)
  419. {
  420. strncat(lookup,".", MAXDNAME);
  421. strncat(lookup,searchdomain[variant], MAXDNAME);
  422. }
  423. DPRINTF("lookup name: %s\n", lookup);
  424. q.dotted = (char *)lookup;
  425. q.qtype = type;
  426. q.qclass = C_IN; /* CLASS_IN */
  427. j = encode_question(&q, packet+i, PACKETSZ-i);
  428. if (j < 0)
  429. goto fail;
  430. len = i + j;
  431. DPRINTF("On try %d, sending query to port %d of machine %s\n",
  432. retries, NAMESERVER_PORT, nsip[ns]);
  433. sa.sin_family = AF_INET;
  434. sa.sin_port = htons(NAMESERVER_PORT);
  435. sa.sin_addr.s_addr = inet_addr(nsip[ns]);
  436. if (connect(fd, (struct sockaddr *) &sa, sizeof(sa)) == -1) {
  437. if (errno == ENETUNREACH) {
  438. /* routing error, presume not transient */
  439. goto tryall;
  440. } else
  441. /* retry */
  442. continue;
  443. }
  444. DPRINTF("Transmitting packet of length %d, id=%d, qr=%d\n",
  445. len, h.id, h.qr);
  446. send(fd, packet, len, 0);
  447. dns_caught_signal = 0;
  448. oldalarm = alarm(REPLY_TIMEOUT);
  449. oldhandler = signal(SIGALRM, dns_catch_signal);
  450. i = recv(fd, packet, PACKETSZ, 0);
  451. alarm(0);
  452. signal(SIGALRM, oldhandler);
  453. alarm(oldalarm);
  454. DPRINTF("Timeout=%d, len=%d\n", dns_caught_signal, i);
  455. if (dns_caught_signal)
  456. /* timed out, so retry send and receive,
  457. to next nameserver on queue */
  458. goto again;
  459. if (i < HFIXEDSZ)
  460. /* too short ! */
  461. goto again;
  462. decode_header(packet, &h);
  463. DPRINTF("id = %d, qr = %d\n", h.id, h.qr);
  464. if ((h.id != id) || (!h.qr))
  465. /* unsolicited */
  466. goto again;
  467. DPRINTF("Got response %s\n", "(i think)!");
  468. DPRINTF("qrcount=%d,ancount=%d,nscount=%d,arcount=%d\n",
  469. h.qdcount, h.ancount, h.nscount, h.arcount);
  470. DPRINTF("opcode=%d,aa=%d,tc=%d,rd=%d,ra=%d,rcode=%d\n",
  471. h.opcode, h.aa, h.tc, h.rd, h.ra, h.rcode);
  472. if ((h.rcode) || (h.ancount < 1)) {
  473. /* negative result, not present */
  474. goto again;
  475. }
  476. pos = HFIXEDSZ;
  477. for (j = 0; j < h.qdcount; j++) {
  478. DPRINTF("Skipping question %d at %d\n", j, pos);
  479. i = length_question(packet, pos);
  480. DPRINTF("Length of question %d is %d\n", j, i);
  481. if (i < 0)
  482. goto again;
  483. pos += i;
  484. }
  485. DPRINTF("Decoding answer at pos %d\n", pos);
  486. for (j=0;j<h.ancount;j++)
  487. {
  488. i = decode_answer(packet, pos, a);
  489. if (i<0) {
  490. DPRINTF("failed decode %d\n", i);
  491. goto again;
  492. }
  493. /* For all but T_SIG, accept first answer */
  494. if (a->atype != T_SIG)
  495. break;
  496. DPRINTF("skipping T_SIG %d\n", i);
  497. free(a->dotted);
  498. pos += i;
  499. }
  500. DPRINTF("Answer name = |%s|\n", a->dotted);
  501. DPRINTF("Answer type = |%d|\n", a->atype);
  502. close(fd);
  503. if (outpacket)
  504. *outpacket = packet;
  505. else
  506. free(packet);
  507. return (0); /* success! */
  508. tryall:
  509. /* if there are other nameservers, give them a go,
  510. otherwise return with error */
  511. variant = 0;
  512. if (retries >= nscount*(searchdomains+1))
  513. goto fail;
  514. again:
  515. /* if there are searchdomains, try them or fallback as passed */
  516. if (variant < searchdomains) {
  517. /* next search */
  518. variant++;
  519. } else {
  520. /* next server, first search */
  521. ns = (ns + 1) % nscount;
  522. variant = 0;
  523. }
  524. }
  525. fail:
  526. if (fd != -1)
  527. close(fd);
  528. if (lookup)
  529. free(lookup);
  530. if (packet)
  531. free(packet);
  532. return -1;
  533. }
  534. #endif
  535. #ifdef L_resolveaddress
  536. int resolve_address(const char *address, int nscount,
  537. char **nsip, struct in_addr *in)
  538. {
  539. unsigned char *packet;
  540. struct resolv_answer a;
  541. char temp[256];
  542. int i;
  543. int nest = 0;
  544. if (!address || !in)
  545. return -1;
  546. strncpy(temp, address, sizeof(temp));
  547. for (;;) {
  548. i = dns_lookup(temp, T_A, nscount, nsip, &packet, &a);
  549. if (i < 0)
  550. return -1;
  551. free(a.dotted);
  552. if (a.atype == T_CNAME) { /* CNAME */
  553. DPRINTF("Got a CNAME in resolve_address()\n");
  554. i = decode_dotted(packet, a.rdoffset, temp, sizeof(temp));
  555. free(packet);
  556. if (i < 0)
  557. return -1;
  558. if (++nest > MAX_RECURSE)
  559. return -1;
  560. continue;
  561. } else if (a.atype == T_A) { /* ADDRESS */
  562. free(packet);
  563. break;
  564. } else {
  565. free(packet);
  566. return -1;
  567. }
  568. }
  569. if (in)
  570. memcpy(in, a.rdata, INADDRSZ); /* IPv4 T_A */
  571. return 0;
  572. }
  573. #endif
  574. #ifdef L_resolvemailbox
  575. int resolve_mailbox(const char *address, int nscount,
  576. char **nsip, struct in_addr *in)
  577. {
  578. struct resolv_answer a;
  579. unsigned char *packet;
  580. char temp[256];
  581. int nest = 0;
  582. int i;
  583. if (!address || !in)
  584. return -1;
  585. /* look up mail exchange */
  586. i = dns_lookup(address, T_MX, nscount, nsip, &packet, &a);
  587. strncpy(temp, address, sizeof(temp));
  588. if (i >= 0) {
  589. i = decode_dotted(packet, a.rdoffset+2, temp, sizeof(temp));
  590. free(packet);
  591. }
  592. for (;;) {
  593. i = dns_lookup(temp, T_A, nscount, nsip, &packet, &a);
  594. if (i < 0)
  595. return -1;
  596. free(a.dotted);
  597. if (a.atype == T_CNAME) { /* CNAME */
  598. DPRINTF("Got a CNAME in resolve_mailbox()\n");
  599. i = decode_dotted(packet, a.rdoffset, temp, sizeof(temp));
  600. free(packet);
  601. if (i < 0)
  602. return i;
  603. if (++nest > MAX_RECURSE)
  604. return -1;
  605. continue;
  606. } else if (a.atype == T_A) { /* ADDRESS */
  607. free(packet);
  608. break;
  609. } else {
  610. free(packet);
  611. return -1;
  612. }
  613. }
  614. if (in)
  615. memcpy(in, a.rdata, INADDRSZ); /* IPv4 */
  616. return 0;
  617. }
  618. #endif
  619. #ifdef L_opennameservers
  620. int nameservers;
  621. char * nameserver[MAX_SERVERS];
  622. int searchdomains;
  623. char * searchdomain[MAX_SEARCH];
  624. /*
  625. * we currently read formats not quite the same as that on normal
  626. * unix systems, we can have a list of nameservers after the keyword.
  627. */
  628. int open_nameservers()
  629. {
  630. FILE *fp;
  631. int i;
  632. #define RESOLV_ARGS 5
  633. char szBuffer[128], *p, *argv[RESOLV_ARGS];
  634. int argc;
  635. if (nameservers > 0)
  636. return 0;
  637. if ((fp = fopen("/etc/resolv.conf", "r")) ||
  638. (fp = fopen("/etc/config/resolv.conf", "r"))) {
  639. while (fgets(szBuffer, sizeof(szBuffer), fp) != NULL) {
  640. for (p = szBuffer; *p && isspace(*p); p++)
  641. /* skip white space */;
  642. if (*p == '\0' || *p == '\n' || *p == '#') /* skip comments etc */
  643. continue;
  644. argc = 0;
  645. while (*p && argc < RESOLV_ARGS) {
  646. argv[argc++] = p;
  647. while (*p && !isspace(*p) && *p != '\n')
  648. p++;
  649. while (*p && (isspace(*p) || *p == '\n')) /* remove spaces */
  650. *p++ = '\0';
  651. }
  652. if (strcmp(argv[0], "nameserver") == 0) {
  653. for (i = 1; i < argc && nameservers < MAX_SERVERS; i++) {
  654. nameserver[nameservers++] = strdup(argv[i]);
  655. DPRINTF("adding nameserver %s\n", argv[i]);
  656. }
  657. }
  658. /* domain and search are mutually exclusive, the last one wins */
  659. if (strcmp(argv[0],"domain")==0 || strcmp(argv[0],"search")==0) {
  660. while (searchdomains > 0) {
  661. free(searchdomain[--searchdomains]);
  662. searchdomain[searchdomains] = NULL;
  663. }
  664. for (i=1; i < argc && searchdomains < MAX_SEARCH; i++) {
  665. searchdomain[searchdomains++] = strdup(argv[i]);
  666. DPRINTF("adding search %s\n", argv[i]);
  667. }
  668. }
  669. }
  670. fclose(fp);
  671. } else {
  672. DPRINTF("failed to open %s\n", "resolv.conf");
  673. }
  674. DPRINTF("nameservers = %d\n", nameservers);
  675. return 0;
  676. }
  677. #endif
  678. #ifdef L_closenameservers
  679. void close_nameservers(void)
  680. {
  681. while (nameservers > 0) {
  682. free(nameserver[--nameservers]);
  683. nameserver[nameservers] = NULL;
  684. }
  685. while (searchdomains > 0) {
  686. free(searchdomain[--searchdomains]);
  687. searchdomain[searchdomains] = NULL;
  688. }
  689. }
  690. #endif
  691. #ifdef L_resolvename
  692. const char *resolve_name(const char *name, int mailbox)
  693. {
  694. struct in_addr in;
  695. int i;
  696. /* shortcut: is it a valid IP address to begin with? */
  697. if (inet_aton(name, &in))
  698. return name;
  699. open_nameservers();
  700. DPRINTF("looking up '%s', mailbox=%d, nameservers=%d\n",
  701. name, mailbox, nameservers);
  702. if (mailbox)
  703. i = resolve_mailbox(name, nameservers, nameserver, &in);
  704. else
  705. i = resolve_address(name, nameservers, nameserver, &in);
  706. if (i < 0)
  707. return 0;
  708. DPRINTF("success = '%s'\n", inet_ntoa(in));
  709. return inet_ntoa(in);
  710. }
  711. #endif
  712. #ifdef L_gethostbyname
  713. struct hostent *gethostbyname(const char *name)
  714. {
  715. static struct hostent h;
  716. static char namebuf[256];
  717. static struct in_addr in;
  718. static struct in_addr *addr_list[2];
  719. struct hostent *hp;
  720. unsigned char *packet;
  721. struct resolv_answer a;
  722. int i;
  723. int nest = 0;
  724. open_nameservers();
  725. if (!name)
  726. return 0;
  727. if ((hp = get_hosts_byname(name))) /* do /etc/hosts first */
  728. return(hp);
  729. memset(&h, 0, sizeof(h));
  730. addr_list[0] = &in;
  731. addr_list[1] = 0;
  732. strncpy(namebuf, name, sizeof(namebuf));
  733. /* First check if this is already an address */
  734. if (inet_aton(name, &in)) {
  735. h.h_name = namebuf;
  736. h.h_addrtype = AF_INET;
  737. h.h_length = sizeof(in);
  738. h.h_addr_list = (char **) addr_list;
  739. return &h;
  740. }
  741. for (;;) {
  742. i = dns_lookup(namebuf, 1, nameservers, nameserver, &packet, &a);
  743. if (i < 0)
  744. return 0;
  745. strncpy(namebuf, a.dotted, sizeof(namebuf));
  746. free(a.dotted);
  747. if (a.atype == T_CNAME) { /* CNAME */
  748. DPRINTF("Got a CNAME in gethostbyname()\n");
  749. i = decode_dotted(packet, a.rdoffset, namebuf, sizeof(namebuf));
  750. free(packet);
  751. if (i < 0)
  752. return 0;
  753. if (++nest > MAX_RECURSE)
  754. return 0;
  755. continue;
  756. } else if (a.atype == T_A) { /* ADDRESS */
  757. memcpy(&in, a.rdata, sizeof(in));
  758. h.h_name = namebuf;
  759. h.h_addrtype = AF_INET;
  760. h.h_length = sizeof(in);
  761. h.h_addr_list = (char **) addr_list;
  762. free(packet);
  763. break;
  764. } else {
  765. free(packet);
  766. return 0;
  767. }
  768. }
  769. return &h;
  770. }
  771. #endif
  772. #ifdef L_getnetbyname
  773. struct netent * getnetbyname(const char * name)
  774. {
  775. return NULL;
  776. }
  777. #endif
  778. #ifdef L_res_init
  779. int res_init()
  780. {
  781. return(0);
  782. }
  783. #endif
  784. #ifdef L_res_query
  785. #ifndef MIN
  786. #define MIN(x, y) ((x) < (y) ? (x) : (y))
  787. #endif
  788. int res_query(const char *dname, int class, int type,
  789. unsigned char *answer, int anslen)
  790. {
  791. unsigned char * packet = 0;
  792. struct resolv_answer a;
  793. int i;
  794. open_nameservers();
  795. if (!dname || class != 1 /* CLASS_IN */)
  796. return(-1);
  797. memset((char *) &a, '\0', sizeof(a));
  798. i = dns_lookup(dname, type, nameservers, nameserver, &packet, &a);
  799. if (i < 0)
  800. return(-1);
  801. free(a.dotted);
  802. if (a.atype == type) { /* CNAME*/
  803. if (anslen && answer)
  804. memcpy(answer, a.rdata, MIN(anslen, a.rdlength));
  805. if (packet)
  806. free(packet);
  807. return(MIN(anslen, a.rdlength));
  808. }
  809. if (packet)
  810. free(packet);
  811. return 0;
  812. }
  813. #endif
  814. #ifdef L_gethostbyaddr
  815. struct hostent *gethostbyaddr(const char *addr, int len, int type)
  816. {
  817. static struct hostent h;
  818. static char namebuf[256];
  819. static struct in_addr in;
  820. static struct in_addr *addr_list[2];
  821. struct hostent *hp;
  822. unsigned char *packet;
  823. struct resolv_answer a;
  824. int i;
  825. int nest = 0;
  826. if (!addr || (len != sizeof(in)) || (type != AF_INET))
  827. return 0;
  828. if ((hp = get_hosts_byaddr(addr, len, type))) /* do /etc/hosts first */
  829. return(hp);
  830. memcpy(&in.s_addr, addr, len);
  831. open_nameservers();
  832. memset(&h, 0, sizeof(h));
  833. addr_list[0] = &in;
  834. addr_list[1] = 0;
  835. sprintf(namebuf, "%d.%d.%d.%d.in-addr.arpa",
  836. (in.s_addr >> 24) & 0xff,
  837. (in.s_addr >> 16) & 0xff,
  838. (in.s_addr >> 8) & 0xff,
  839. (in.s_addr >> 0) & 0xff);
  840. for (;;) {
  841. i = dns_lookup(namebuf, T_PTR, nameservers, nameserver, &packet, &a);
  842. if (i < 0)
  843. return 0;
  844. strncpy(namebuf, a.dotted, sizeof(namebuf));
  845. free(a.dotted);
  846. if (a.atype == T_CNAME) { /* CNAME */
  847. DPRINTF("Got a CNAME in gethostbyaddr()\n");
  848. i = decode_dotted(packet, a.rdoffset, namebuf, sizeof(namebuf));
  849. free(packet);
  850. if (i < 0)
  851. return 0;
  852. if (++nest > MAX_RECURSE)
  853. return 0;
  854. continue;
  855. } else if (a.atype == T_PTR) { /* ADDRESS */
  856. i = decode_dotted(packet, a.rdoffset, namebuf, sizeof(namebuf));
  857. free(packet);
  858. h.h_name = namebuf;
  859. h.h_addrtype = AF_INET;
  860. h.h_length = sizeof(in);
  861. h.h_addr_list = (char **) addr_list;
  862. break;
  863. } else {
  864. free(packet);
  865. return 0;
  866. }
  867. }
  868. return &h;
  869. }
  870. #endif
  871. #ifdef L_read_etc_hosts
  872. struct hostent * read_etc_hosts(const char * name, int ip)
  873. {
  874. static struct hostent h;
  875. static struct in_addr in;
  876. static struct in_addr *addr_list[2];
  877. static char line[80];
  878. FILE *fp;
  879. char *cp;
  880. #define MAX_ALIAS 5
  881. char *alias[MAX_ALIAS];
  882. int aliases, i;
  883. if ((fp = fopen("/etc/hosts", "r")) == NULL &&
  884. (fp = fopen("/etc/config/hosts", "r")) == NULL)
  885. return((struct hostent *) NULL);
  886. while (fgets(line, sizeof(line), fp)) {
  887. if ((cp = strchr(line, '#')))
  888. *cp = '\0';
  889. aliases = 0;
  890. cp = line;
  891. while (*cp) {
  892. while (*cp && isspace(*cp))
  893. *cp++ = '\0';
  894. if (!*cp)
  895. continue;
  896. if (aliases < MAX_ALIAS)
  897. alias[aliases++] = cp;
  898. while (*cp && !isspace(*cp))
  899. cp++;
  900. }
  901. if (aliases < 2)
  902. continue; /* syntax error really */
  903. if (ip) {
  904. if (strcmp(name, alias[0]) != 0)
  905. continue;
  906. } else {
  907. for (i = 1; i < aliases; i++)
  908. if (strcasecmp(name, alias[i]) == 0)
  909. break;
  910. if (i >= aliases)
  911. continue;
  912. }
  913. if (inet_aton(alias[0], &in) == 0)
  914. break; /* bad ip address */
  915. addr_list[0] = &in;
  916. addr_list[1] = 0;
  917. h.h_name = alias[1];
  918. h.h_addrtype = AF_INET;
  919. h.h_length = sizeof(in);
  920. h.h_addr_list = (char**) addr_list;
  921. fclose(fp);
  922. return(&h);
  923. }
  924. fclose(fp);
  925. return((struct hostent *) NULL);
  926. }
  927. #endif
  928. #ifdef L_get_hosts_byname
  929. struct hostent * get_hosts_byname(const char * name)
  930. {
  931. return(read_etc_hosts(name, 0));
  932. }
  933. #endif
  934. #ifdef L_get_hosts_byaddr
  935. struct hostent * get_hosts_byaddr(const char * addr, int len, int type)
  936. {
  937. char ipaddr[20];
  938. if (type != AF_INET || len != sizeof(struct in_addr))
  939. return((struct hostent *) NULL);
  940. strcpy(ipaddr, inet_ntoa(* (struct in_addr *) addr));
  941. return(read_etc_hosts(ipaddr, 1));
  942. }
  943. #endif