dnresolve.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765
  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. }
  343. /*
  344. * return non-0 if first list includes second list
  345. */
  346. int
  347. contains(RR *rp1, RR *rp2)
  348. {
  349. RR *trp1, *trp2;
  350. for(trp2 = rp2; trp2; trp2 = trp2->next){
  351. for(trp1 = rp1; trp1; trp1 = trp1->next){
  352. if(trp1->type == trp2->type)
  353. if(trp1->host == trp2->host)
  354. if(trp1->owner == trp2->owner)
  355. break;
  356. }
  357. if(trp1 == 0)
  358. return 0;
  359. }
  360. return 1;
  361. }
  362. typedef struct Dest Dest;
  363. struct Dest
  364. {
  365. uchar a[IPaddrlen]; /* ip address */
  366. DN *s; /* name server */
  367. int nx; /* number of transmissions */
  368. int code;
  369. };
  370. /*
  371. * return multicast version if any
  372. */
  373. int
  374. ipisbm(uchar *ip)
  375. {
  376. if(isv4(ip)){
  377. if(ip[IPv4off] >= 0xe0 && ip[IPv4off] < 0xf0)
  378. return 4;
  379. if(ipcmp(ip, IPv4bcast) == 0)
  380. return 4;
  381. } else {
  382. if(ip[0] == 0xff)
  383. return 6;
  384. }
  385. return 0;
  386. }
  387. /*
  388. * Get next server address
  389. */
  390. static int
  391. serveraddrs(RR *nsrp, Dest *dest, int nd, int depth, Request *reqp)
  392. {
  393. RR *rp, *arp, *trp;
  394. Dest *cur;
  395. if(nd >= Maxdest)
  396. return 0;
  397. /*
  398. * look for a server whose address we already know.
  399. * if we find one, mark it so we ignore this on
  400. * subsequent passes.
  401. */
  402. arp = 0;
  403. for(rp = nsrp; rp; rp = rp->next){
  404. assert(rp->magic == RRmagic);
  405. if(rp->marker)
  406. continue;
  407. arp = rrlookup(rp->host, Ta, NOneg);
  408. if(arp){
  409. rp->marker = 1;
  410. break;
  411. }
  412. arp = dblookup(rp->host->name, Cin, Ta, 0, 0);
  413. if(arp){
  414. rp->marker = 1;
  415. break;
  416. }
  417. }
  418. /*
  419. * if the cache and database lookup didn't find any new
  420. * server addresses, try resolving one via the network.
  421. * Mark any we try to resolve so we don't try a second time.
  422. */
  423. if(arp == 0){
  424. for(rp = nsrp; rp; rp = rp->next){
  425. if(rp->marker)
  426. continue;
  427. rp->marker = 1;
  428. /*
  429. * avoid loops looking up a server under itself
  430. */
  431. if(subsume(rp->owner->name, rp->host->name))
  432. continue;
  433. arp = dnresolve(rp->host->name, Cin, Ta, reqp, 0, depth+1, Recurse, 1, 0);
  434. rrfreelist(rrremneg(&arp));
  435. if(arp)
  436. break;
  437. }
  438. }
  439. /* use any addresses that we found */
  440. for(trp = arp; trp; trp = trp->next){
  441. if(nd >= Maxdest)
  442. break;
  443. cur = &dest[nd];
  444. parseip(cur->a, trp->ip->name);
  445. if(ipisbm(cur->a))
  446. continue;
  447. cur->nx = 0;
  448. cur->s = trp->owner;
  449. cur->code = Rtimeout;
  450. nd++;
  451. }
  452. rrfreelist(arp);
  453. return nd;
  454. }
  455. /*
  456. * cache negative responses
  457. */
  458. static void
  459. cacheneg(DN *dp, int type, int rcode, RR *soarr)
  460. {
  461. RR *rp;
  462. DN *soaowner;
  463. ulong ttl;
  464. /* no cache time specified, don' make anything up */
  465. if(soarr != nil){
  466. if(soarr->next != nil){
  467. rrfreelist(soarr->next);
  468. soarr->next = nil;
  469. }
  470. soaowner = soarr->owner;
  471. } else
  472. soaowner = nil;
  473. /* the attach can cause soarr to be freed so mine it now */
  474. if(soarr != nil && soarr->soa != nil)
  475. ttl = soarr->soa->minttl+now;
  476. else
  477. ttl = 5*Min;
  478. /* add soa and negative RR to the database */
  479. rrattach(soarr, 1);
  480. rp = rralloc(type);
  481. rp->owner = dp;
  482. rp->negative = 1;
  483. rp->negsoaowner = soaowner;
  484. rp->negrcode = rcode;
  485. rp->ttl = ttl;
  486. rrattach(rp, 1);
  487. }
  488. /*
  489. * query name servers. If the name server returns a pointer to another
  490. * name server, recurse.
  491. */
  492. static int
  493. netquery1(int fd, DN *dp, int type, RR *nsrp, Request *reqp, int depth, uchar *ibuf, uchar *obuf)
  494. {
  495. int ndest, j, len, replywaits, rv;
  496. ushort req;
  497. RR *tp, *soarr;
  498. Dest *p, *l, *np;
  499. DN *ndp;
  500. Dest dest[Maxdest];
  501. DNSmsg m;
  502. ulong endtime;
  503. /* pack request into a message */
  504. req = rand();
  505. len = mkreq(dp, type, obuf, Frecurse|Oquery, req);
  506. /* no server addresses yet */
  507. l = dest;
  508. /*
  509. * transmit requests and wait for answers.
  510. * at most Maxtrans attempts to each address.
  511. * each cycle send one more message than the previous.
  512. */
  513. for(ndest = 1; ndest < Maxdest; ndest++){
  514. p = dest;
  515. endtime = time(0);
  516. if(endtime >= reqp->aborttime)
  517. break;
  518. /* get a server address if we need one */
  519. if(ndest > l - p){
  520. j = serveraddrs(nsrp, dest, l - p, depth, reqp);
  521. l = &dest[j];
  522. }
  523. /* no servers, punt */
  524. if(l == dest)
  525. break;
  526. /* send to first 'ndest' destinations */
  527. j = 0;
  528. for(; p < &dest[ndest] && p < l; p++){
  529. /* skip destinations we've finished with */
  530. if(p->nx >= Maxtrans)
  531. continue;
  532. j++;
  533. /* exponential backoff of requests */
  534. if((1<<p->nx) > ndest)
  535. continue;
  536. memmove(obuf, p->a, sizeof(p->a));
  537. if(debug)
  538. logsend(reqp->id, depth, obuf, p->s->name,
  539. dp->name, type);
  540. if(write(fd, obuf, len + OUdphdrsize) < 0)
  541. warning("sending udp msg %r");
  542. p->nx++;
  543. }
  544. if(j == 0)
  545. break; /* no destinations left */
  546. /* wait up to 5 seconds for replies */
  547. endtime = time(0) + 5;
  548. if(endtime > reqp->aborttime)
  549. endtime = reqp->aborttime;
  550. for(replywaits = 0; replywaits < ndest; replywaits++){
  551. if(readreply(fd, dp, type, req, ibuf, &m, endtime, reqp) < 0)
  552. break; /* timed out */
  553. /* find responder */
  554. for(p = dest; p < l; p++)
  555. if(memcmp(p->a, ibuf, sizeof(p->a)) == 0)
  556. break;
  557. /* remove all addrs of responding server from list */
  558. for(np = dest; np < l; np++)
  559. if(np->s == p->s)
  560. p->nx = Maxtrans;
  561. /* ignore any error replies */
  562. if((m.flags & Rmask) == Rserver){
  563. rrfreelist(m.qd);
  564. rrfreelist(m.an);
  565. rrfreelist(m.ar);
  566. rrfreelist(m.ns);
  567. if(p != l)
  568. p->code = Rserver;
  569. continue;
  570. }
  571. /* ignore any bad delegations */
  572. if(m.ns && baddelegation(m.ns, nsrp, ibuf)){
  573. rrfreelist(m.ns);
  574. m.ns = nil;
  575. if(m.an == nil){
  576. rrfreelist(m.qd);
  577. rrfreelist(m.ar);
  578. if(p != l)
  579. p->code = Rserver;
  580. continue;
  581. }
  582. }
  583. /* remove any soa's from the authority section */
  584. soarr = rrremtype(&m.ns, Tsoa);
  585. /* incorporate answers */
  586. if(m.an)
  587. rrattach(m.an, (m.flags & Fauth) ? 1 : 0);
  588. if(m.ar)
  589. rrattach(m.ar, 0);
  590. if(m.ns){
  591. ndp = m.ns->owner;
  592. rrattach(m.ns, 0);
  593. } else
  594. ndp = 0;
  595. /* free the question */
  596. if(m.qd)
  597. rrfreelist(m.qd);
  598. /*
  599. * Any reply from an authoritative server,
  600. * or a positive reply terminates the search
  601. */
  602. if(m.an != nil || (m.flags & Fauth)){
  603. if(m.an == nil && (m.flags & Rmask) == Rname)
  604. dp->nonexistent = Rname;
  605. else
  606. dp->nonexistent = 0;
  607. /*
  608. * cache any negative responses, free soarr
  609. */
  610. if((m.flags & Fauth) && m.an == nil)
  611. cacheneg(dp, type, (m.flags & Rmask), soarr);
  612. else
  613. rrfreelist(soarr);
  614. return 1;
  615. }
  616. rrfreelist(soarr);
  617. /*
  618. * if we've been given better name servers
  619. * recurse
  620. */
  621. if(m.ns){
  622. tp = rrlookup(ndp, Tns, NOneg);
  623. if(!contains(nsrp, tp)){
  624. rv = netquery(dp, type, tp, reqp, depth+1);
  625. rrfreelist(tp);
  626. return rv;
  627. } else
  628. rrfreelist(tp);
  629. }
  630. }
  631. }
  632. /* if all servers returned failure, propogate it */
  633. dp->nonexistent = Rserver;
  634. for(p = dest; p < l; p++)
  635. if(p->code != Rserver)
  636. dp->nonexistent = 0;
  637. return 0;
  638. }
  639. static int
  640. netquery(DN *dp, int type, RR *nsrp, Request *reqp, int depth)
  641. {
  642. uchar *obuf;
  643. uchar *ibuf;
  644. RR *rp;
  645. int fd, rv;
  646. if(depth > 12)
  647. return 0;
  648. /* use alloced buffers rather than ones from the stack */
  649. ibuf = emalloc(Maxudpin+OUdphdrsize);
  650. obuf = emalloc(Maxudp+OUdphdrsize);
  651. slave(reqp);
  652. /* prepare server RR's for incremental lookup */
  653. for(rp = nsrp; rp; rp = rp->next)
  654. rp->marker = 0;
  655. fd = udpport();
  656. if(fd < 0)
  657. return 0;
  658. rv = netquery1(fd, dp, type, nsrp, reqp, depth, ibuf, obuf);
  659. close(fd);
  660. free(ibuf);
  661. free(obuf);
  662. return rv;
  663. }