dnresolve.c 15 KB

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