dnresolve.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742
  1. #include <u.h>
  2. #include <libc.h>
  3. #include "dns.h"
  4. #include "ip.h"
  5. enum
  6. {
  7. Maxdest= 24, /* maximum destinations for a request message */
  8. Maxtrans= 3, /* maximum transmissions to a server */
  9. };
  10. static int netquery(DN*, int, RR*, Request*, int);
  11. static RR* dnresolve1(char*, int, int, Request*, int, int);
  12. char *LOG = "dns";
  13. /*
  14. * lookup 'type' info for domain name 'name'. If it doesn't exist, try
  15. * looking it up as a canonical name.
  16. */
  17. RR*
  18. dnresolve(char *name, int class, int type, Request *req, RR **cn, int depth, int recurse, int rooted, int *status)
  19. {
  20. RR *rp, *nrp, *drp;
  21. DN *dp;
  22. int loops;
  23. char nname[Domlen];
  24. if(status)
  25. *status = 0;
  26. /*
  27. * hack for systems that don't have resolve search
  28. * lists. Just look up the simple name in the database.
  29. */
  30. if(!rooted && strchr(name, '.') == 0){
  31. rp = nil;
  32. drp = domainlist(class);
  33. for(nrp = drp; nrp != nil; nrp = nrp->next){
  34. snprint(nname, sizeof(nname), "%s.%s", name, nrp->ptr->name);
  35. rp = dnresolve(nname, class, type, req,cn, depth, recurse, rooted, status);
  36. if(rp != nil)
  37. break;
  38. }
  39. if(drp != nil)
  40. rrfree(drp);
  41. return rp;
  42. }
  43. /*
  44. * try the name directly
  45. */
  46. rp = dnresolve1(name, class, type, req, depth, recurse);
  47. if(rp)
  48. return randomize(rp);
  49. /* try it as a canonical name if we weren't told the name didn't exist */
  50. dp = dnlookup(name, class, 0);
  51. if(type != Tptr && dp->nonexistent != Rname){
  52. for(loops=0; rp == nil && loops < 32; loops++){
  53. rp = dnresolve1(name, class, Tcname, req, depth, recurse);
  54. if(rp == nil)
  55. break;
  56. if(rp->negative){
  57. rrfreelist(rp);
  58. rp = nil;
  59. break;
  60. }
  61. name = rp->host->name;
  62. if(cn)
  63. rrcat(cn, rp);
  64. else
  65. rrfreelist(rp);
  66. rp = dnresolve1(name, class, type, req, depth, recurse);
  67. }
  68. }
  69. /* distinction between not found and not good */
  70. if(rp == 0 && status != 0 && dp->nonexistent != 0)
  71. *status = dp->nonexistent;
  72. return randomize(rp);
  73. }
  74. static RR*
  75. dnresolve1(char *name, int class, int type, Request *req, int depth, int recurse)
  76. {
  77. DN *dp, *nsdp;
  78. RR *rp, *nsrp, *dbnsrp;
  79. char *cp;
  80. if(debug)
  81. syslog(0, LOG, "dnresolve1 %s %d %d", name, type, class);
  82. /* only class Cin implemented so far */
  83. if(class != Cin)
  84. return 0;
  85. dp = dnlookup(name, class, 1);
  86. /*
  87. * Try the cache first
  88. */
  89. rp = rrlookup(dp, type, OKneg);
  90. if(rp){
  91. if(rp->db){
  92. /* unauthenticated db entries are hints */
  93. if(rp->auth)
  94. return rp;
  95. } else {
  96. /* cached entry must still be valid */
  97. if(rp->ttl > now){
  98. /* but Tall entries are special */
  99. if(type != Tall || rp->query == Tall)
  100. return rp;
  101. }
  102. }
  103. }
  104. rrfreelist(rp);
  105. /*
  106. * try the cache for a canonical name. if found punt
  107. * since we'll find it during the canonical name search
  108. * in dnresolve().
  109. */
  110. if(type != Tcname){
  111. rp = rrlookup(dp, Tcname, NOneg);
  112. rrfreelist(rp);
  113. if(rp)
  114. return 0;
  115. }
  116. /*
  117. * if we're running as just a resolver, go to our
  118. * designated name servers
  119. */
  120. if(resolver){
  121. nsrp = randomize(getdnsservers(class));
  122. if(nsrp != nil) {
  123. if(netquery(dp, type, nsrp, req, depth+1)){
  124. rrfreelist(nsrp);
  125. return rrlookup(dp, type, OKneg);
  126. }
  127. rrfreelist(nsrp);
  128. }
  129. }
  130. /*
  131. * walk up the domain name looking for
  132. * a name server for the domain.
  133. */
  134. for(cp = name; cp; cp = walkup(cp)){
  135. /*
  136. * if this is a local (served by us) domain,
  137. * return answer
  138. */
  139. dbnsrp = randomize(dblookup(cp, class, Tns, 0, 0));
  140. if(dbnsrp && dbnsrp->local){
  141. rp = dblookup(name, class, type, 1, dbnsrp->ttl);
  142. rrfreelist(dbnsrp);
  143. return rp;
  144. }
  145. /*
  146. * if recursion isn't set, just accept local
  147. * entries
  148. */
  149. if(recurse == Dontrecurse){
  150. if(dbnsrp)
  151. rrfreelist(dbnsrp);
  152. continue;
  153. }
  154. /* look for ns in cache */
  155. nsdp = dnlookup(cp, class, 0);
  156. nsrp = nil;
  157. if(nsdp)
  158. nsrp = randomize(rrlookup(nsdp, Tns, NOneg));
  159. /* if the entry timed out, ignore it */
  160. if(nsrp && nsrp->ttl < now){
  161. rrfreelist(nsrp);
  162. nsrp = nil;
  163. }
  164. if(nsrp){
  165. rrfreelist(dbnsrp);
  166. /* try the name servers found in cache */
  167. if(netquery(dp, type, nsrp, req, depth+1)){
  168. rrfreelist(nsrp);
  169. return rrlookup(dp, type, OKneg);
  170. }
  171. rrfreelist(nsrp);
  172. continue;
  173. }
  174. /* use ns from db */
  175. if(dbnsrp){
  176. /* try the name servers found in db */
  177. if(netquery(dp, type, dbnsrp, req, depth+1)){
  178. /* we got an answer */
  179. rrfreelist(dbnsrp);
  180. return rrlookup(dp, type, NOneg);
  181. }
  182. rrfreelist(dbnsrp);
  183. }
  184. }
  185. /* settle for a non-authoritative answer */
  186. rp = rrlookup(dp, type, OKneg);
  187. if(rp)
  188. return rp;
  189. /* noone answered. try the database, we might have a chance. */
  190. return dblookup(name, class, type, 0, 0);
  191. }
  192. /*
  193. * walk a domain name one element to the right. return a pointer to that element.
  194. * in other words, return a pointer to the parent domain name.
  195. */
  196. char*
  197. walkup(char *name)
  198. {
  199. char *cp;
  200. cp = strchr(name, '.');
  201. if(cp)
  202. return cp+1;
  203. else if(*name)
  204. return "";
  205. else
  206. return 0;
  207. }
  208. /*
  209. * Get a udpport for requests and replies. Put the port
  210. * into "headers" mode.
  211. */
  212. static char *hmsg = "headers";
  213. static int
  214. udpport(void)
  215. {
  216. int fd, ctl;
  217. char ds[64];
  218. char adir[64];
  219. /* get a udp port */
  220. snprint(ds, sizeof(ds), "%s/udp!*!0", mntpt);
  221. ctl = announce(ds, adir);
  222. if(ctl < 0){
  223. /* warning("can't get udp port"); */
  224. return -1;
  225. }
  226. /* turn on header style interface */
  227. if(write(ctl, hmsg, strlen(hmsg)) , 0){
  228. close(ctl);
  229. warning(hmsg);
  230. return -1;
  231. }
  232. /* grab the data file */
  233. snprint(ds, sizeof(ds), "%s/data", adir);
  234. fd = open(ds, ORDWR);
  235. close(ctl);
  236. if(fd < 0){
  237. warning("can't open udp port: %r");
  238. return -1;
  239. }
  240. return fd;
  241. }
  242. static int
  243. mkreq(DN *dp, int type, uchar *buf, ushort reqno)
  244. {
  245. DNSmsg m;
  246. int len;
  247. Udphdr *uh = (Udphdr*)buf;
  248. /* stuff port number into output buffer */
  249. memset(uh, 0, sizeof(*uh));
  250. hnputs(uh->rport, 53);
  251. /* make request and convert it to output format */
  252. memset(&m, 0, sizeof(m));
  253. m.flags = Frecurse;
  254. // m.flags = resolver ? Frecurse : 0;
  255. m.id = reqno;
  256. m.qd = rralloc(type);
  257. m.qd->owner = dp;
  258. m.qd->type = type;
  259. len = convDNS2M(&m, &buf[Udphdrsize], Maxudp);
  260. if(len < 0)
  261. abort(); /* "can't convert" */;
  262. rrfree(m.qd);
  263. return len;
  264. }
  265. /* for alarms in readreply */
  266. static void
  267. ding(void *x, char *msg)
  268. {
  269. USED(x);
  270. if(strcmp(msg, "alarm") == 0)
  271. noted(NCONT);
  272. else
  273. noted(NDFLT);
  274. }
  275. static void
  276. freeanswers(DNSmsg *mp)
  277. {
  278. rrfreelist(mp->qd);
  279. rrfreelist(mp->an);
  280. rrfreelist(mp->ns);
  281. rrfreelist(mp->ar);
  282. }
  283. /*
  284. * read replies to a request. ignore any of the wrong type. wait at most 5 seconds.
  285. */
  286. static int
  287. readreply(int fd, DN *dp, int type, ushort req,
  288. uchar *ibuf, DNSmsg *mp, ulong endtime, Request *reqp)
  289. {
  290. char *err;
  291. int len;
  292. ulong now;
  293. RR *rp;
  294. notify(ding);
  295. for(; ; freeanswers(mp)){
  296. now = time(0);
  297. if(now >= endtime)
  298. return -1; /* timed out */
  299. /* timed read */
  300. alarm((endtime - now) * 1000);
  301. len = read(fd, ibuf, Udphdrsize+Maxudpin);
  302. alarm(0);
  303. len -= Udphdrsize;
  304. if(len < 0)
  305. return -1; /* timed out */
  306. /* convert into internal format */
  307. memset(mp, 0, sizeof(*mp));
  308. err = convM2DNS(&ibuf[Udphdrsize], len, mp);
  309. if(err){
  310. syslog(0, LOG, "input err %s: %I", err, ibuf);
  311. continue;
  312. }
  313. if(debug)
  314. logreply(reqp->id, ibuf, mp);
  315. /* answering the right question? */
  316. if(mp->id != req){
  317. syslog(0, LOG, "%d: id %d instead of %d: %I", reqp->id,
  318. mp->id, req, ibuf);
  319. continue;
  320. }
  321. if(mp->qd == 0){
  322. syslog(0, LOG, "%d: no question RR: %I", reqp->id, ibuf);
  323. continue;
  324. }
  325. if(mp->qd->owner != dp){
  326. syslog(0, LOG, "%d: owner %s instead of %s: %I", reqp->id,
  327. mp->qd->owner->name, dp->name, ibuf);
  328. continue;
  329. }
  330. if(mp->qd->type != type){
  331. syslog(0, LOG, "%d: type %d instead of %d: %I", reqp->id,
  332. mp->qd->type, type, ibuf);
  333. continue;
  334. }
  335. /* remember what request this is in answer to */
  336. for(rp = mp->an; rp; rp = rp->next)
  337. rp->query = type;
  338. return 0;
  339. }
  340. return 0; /* never reached */
  341. }
  342. /*
  343. * return non-0 if first list includes second list
  344. */
  345. int
  346. contains(RR *rp1, RR *rp2)
  347. {
  348. RR *trp1, *trp2;
  349. for(trp2 = rp2; trp2; trp2 = trp2->next){
  350. for(trp1 = rp1; trp1; trp1 = trp1->next){
  351. if(trp1->type == trp2->type)
  352. if(trp1->host == trp2->host)
  353. if(trp1->owner == trp2->owner)
  354. break;
  355. }
  356. if(trp1 == 0)
  357. return 0;
  358. }
  359. return 1;
  360. }
  361. typedef struct Dest Dest;
  362. struct Dest
  363. {
  364. uchar a[IPaddrlen]; /* ip address */
  365. DN *s; /* name server */
  366. int nx; /* number of transmissions */
  367. int code;
  368. };
  369. /*
  370. * Get next server address
  371. */
  372. static int
  373. serveraddrs(RR *nsrp, Dest *dest, int nd, int depth, Request *reqp)
  374. {
  375. RR *rp, *arp, *trp;
  376. Dest *cur;
  377. if(nd >= Maxdest)
  378. return 0;
  379. /*
  380. * look for a server whose address we already know.
  381. * if we find one, mark it so we ignore this on
  382. * subsequent passes.
  383. */
  384. arp = 0;
  385. for(rp = nsrp; rp; rp = rp->next){
  386. if(rp->marker)
  387. continue;
  388. arp = rrlookup(rp->host, Ta, NOneg);
  389. if(arp){
  390. rp->marker = 1;
  391. break;
  392. }
  393. arp = dblookup(rp->host->name, Cin, Ta, 0, 0);
  394. if(arp){
  395. rp->marker = 1;
  396. break;
  397. }
  398. }
  399. /*
  400. * if the cache and database lookup didn't find any new
  401. * server addresses, try resolving one via the network.
  402. * Mark any we try to resolve so we don't try a second time.
  403. */
  404. if(arp == 0){
  405. for(rp = nsrp; rp; rp = rp->next){
  406. if(rp->marker)
  407. continue;
  408. rp->marker = 1;
  409. /*
  410. * avoid loops looking up a server under itself
  411. */
  412. if(subsume(rp->owner->name, rp->host->name))
  413. continue;
  414. arp = dnresolve(rp->host->name, Cin, Ta, reqp, 0, depth+1, Recurse, 1, 0);
  415. rrfreelist(rrremneg(&arp));
  416. if(arp)
  417. break;
  418. }
  419. }
  420. /* use any addresses that we found */
  421. for(trp = arp; trp; trp = trp->next){
  422. if(nd >= Maxdest)
  423. break;
  424. cur = &dest[nd++];
  425. parseip(cur->a, trp->ip->name);
  426. cur->nx = 0;
  427. cur->s = trp->owner;
  428. cur->code = Rtimeout;
  429. }
  430. rrfreelist(arp);
  431. return nd;
  432. }
  433. /*
  434. * cache negative responses
  435. */
  436. static void
  437. cacheneg(DN *dp, int type, int rcode, RR *soarr)
  438. {
  439. RR *rp;
  440. DN *soaowner;
  441. ulong ttl;
  442. /* no cache time specified, don' make anything up */
  443. if(soarr != nil){
  444. if(soarr->next != nil){
  445. rrfreelist(soarr->next);
  446. soarr->next = nil;
  447. }
  448. soaowner = soarr->owner;
  449. } else
  450. soaowner = nil;
  451. /* the attach can cause soarr to be freed so mine it now */
  452. if(soarr != nil && soarr->soa != nil)
  453. ttl = soarr->soa->minttl+now;
  454. else
  455. ttl = 5*Min;
  456. /* add soa and negative RR to the database */
  457. rrattach(soarr, 1);
  458. rp = rralloc(type);
  459. rp->owner = dp;
  460. rp->negative = 1;
  461. rp->negsoaowner = soaowner;
  462. rp->negrcode = rcode;
  463. rp->ttl = ttl;
  464. rrattach(rp, 1);
  465. }
  466. /*
  467. * query name servers. If the name server returns a pointer to another
  468. * name server, recurse.
  469. */
  470. static int
  471. netquery1(int fd, DN *dp, int type, RR *nsrp, Request *reqp, int depth, uchar *ibuf, uchar *obuf)
  472. {
  473. int ndest, j, len, replywaits, rv;
  474. ushort req;
  475. RR *tp, *soarr;
  476. Dest *p, *l, *np;
  477. DN *ndp;
  478. Dest dest[Maxdest];
  479. DNSmsg m;
  480. ulong endtime;
  481. /* pack request into a message */
  482. req = rand();
  483. len = mkreq(dp, type, obuf, req);
  484. /* no server addresses yet */
  485. l = dest;
  486. /*
  487. * transmit requests and wait for answers.
  488. * at most Maxtrans attempts to each address.
  489. * each cycle send one more message than the previous.
  490. */
  491. for(ndest = 1; ndest < Maxdest; ndest++){
  492. p = dest;
  493. endtime = time(0);
  494. if(endtime >= reqp->aborttime)
  495. break;
  496. /* get a server address if we need one */
  497. if(ndest > l - p){
  498. j = serveraddrs(nsrp, dest, l - p, depth, reqp);
  499. l = &dest[j];
  500. }
  501. /* no servers, punt */
  502. if(l == dest)
  503. break;
  504. /* send to first 'ndest' destinations */
  505. j = 0;
  506. for(; p < &dest[ndest] && p < l; p++){
  507. /* skip destinations we've finished with */
  508. if(p->nx >= Maxtrans)
  509. continue;
  510. j++;
  511. /* exponential backoff of requests */
  512. if((1<<p->nx) > ndest)
  513. continue;
  514. memmove(obuf, p->a, sizeof(p->a));
  515. if(debug)
  516. logsend(reqp->id, depth, obuf, p->s->name,
  517. dp->name, type);
  518. if(write(fd, obuf, len + Udphdrsize) < 0)
  519. warning("sending udp msg %r");
  520. p->nx++;
  521. }
  522. if(j == 0)
  523. break; /* no destinations left */
  524. /* wait up to 5 seconds for replies */
  525. endtime = time(0) + 5;
  526. if(endtime > reqp->aborttime)
  527. endtime = reqp->aborttime;
  528. for(replywaits = 0; replywaits < ndest; replywaits++){
  529. if(readreply(fd, dp, type, req, ibuf, &m, endtime, reqp) < 0)
  530. break; /* timed out */
  531. /* find responder */
  532. for(p = dest; p < l; p++)
  533. if(memcmp(p->a, ibuf, sizeof(p->a)) == 0)
  534. break;
  535. /* remove all addrs of responding server from list */
  536. for(np = dest; np < l; np++)
  537. if(np->s == p->s)
  538. p->nx = Maxtrans;
  539. /* ignore any error replies */
  540. if((m.flags & Rmask) == Rserver){
  541. rrfreelist(m.qd);
  542. rrfreelist(m.an);
  543. rrfreelist(m.ar);
  544. rrfreelist(m.ns);
  545. if(p != l)
  546. p->code = Rserver;
  547. continue;
  548. }
  549. /* ignore any bad delegations */
  550. if(m.ns && baddelegation(m.ns, nsrp, ibuf)){
  551. rrfreelist(m.ns);
  552. m.ns = nil;
  553. if(m.an == nil){
  554. rrfreelist(m.qd);
  555. rrfreelist(m.ar);
  556. if(p != l)
  557. p->code = Rserver;
  558. continue;
  559. }
  560. }
  561. /* remove any soa's from the authority section */
  562. soarr = rrremtype(&m.ns, Tsoa);
  563. /* incorporate answers */
  564. if(m.an)
  565. rrattach(m.an, (m.flags & Fauth) ? 1 : 0);
  566. if(m.ar)
  567. rrattach(m.ar, 0);
  568. if(m.ns){
  569. ndp = m.ns->owner;
  570. rrattach(m.ns, 0);
  571. } else
  572. ndp = 0;
  573. /* free the question */
  574. if(m.qd)
  575. rrfreelist(m.qd);
  576. /*
  577. * Any reply from an authoritative server,
  578. * or a positive reply terminates the search
  579. */
  580. if(m.an != nil || (m.flags & Fauth)){
  581. if(m.an == nil && (m.flags & Rmask) == Rname)
  582. dp->nonexistent = Rname;
  583. else
  584. dp->nonexistent = 0;
  585. /*
  586. * cache any negative responses, free soarr
  587. */
  588. if((m.flags & Fauth) && m.an == nil)
  589. cacheneg(dp, type, (m.flags & Rmask), soarr);
  590. else
  591. rrfreelist(soarr);
  592. return 1;
  593. }
  594. rrfreelist(soarr);
  595. /*
  596. * if we've been given better name servers
  597. * recurse
  598. */
  599. if(m.ns){
  600. tp = rrlookup(ndp, Tns, NOneg);
  601. if(!contains(nsrp, tp)){
  602. rv = netquery(dp, type, tp, reqp, depth+1);
  603. rrfreelist(tp);
  604. return rv;
  605. } else
  606. rrfreelist(tp);
  607. }
  608. }
  609. }
  610. /* if all servers returned failure, propogate it */
  611. dp->nonexistent = Rserver;
  612. for(p = dest; p < l; p++)
  613. if(p->code != Rserver)
  614. dp->nonexistent = 0;
  615. return 0;
  616. }
  617. static int
  618. netquery(DN *dp, int type, RR *nsrp, Request *reqp, int depth)
  619. {
  620. uchar *obuf;
  621. uchar *ibuf;
  622. RR *rp;
  623. int fd, rv;
  624. if(depth > 12)
  625. return 0;
  626. /* use alloced buffers rather than ones from the stack */
  627. ibuf = emalloc(Maxudpin+Udphdrsize);
  628. obuf = emalloc(Maxudp+Udphdrsize);
  629. slave(reqp);
  630. /* prepare server RR's for incremental lookup */
  631. for(rp = nsrp; rp; rp = rp->next)
  632. rp->marker = 0;
  633. fd = udpport();
  634. if(fd < 0)
  635. return 0;
  636. rv = netquery1(fd, dp, type, nsrp, reqp, depth, ibuf, obuf);
  637. close(fd);
  638. free(ibuf);
  639. free(obuf);
  640. return rv;
  641. }