2016-11-25 17:18:40 +01:00
|
|
|
/*
|
|
|
|
* This file is part of the UCB release of Plan 9. It is subject to the license
|
|
|
|
* terms in the LICENSE file found in the top-level directory of this
|
|
|
|
* distribution and at http://akaros.cs.berkeley.edu/files/Plan9License. No
|
|
|
|
* part of the UCB release of Plan 9, including this file, may be copied,
|
|
|
|
* modified, propagated, or distributed except according to the terms contained
|
|
|
|
* in the LICENSE file.
|
|
|
|
*/
|
2018-01-06 01:08:25 +01:00
|
|
|
/* Portions of this file are Copyright (C) 2015-2018 Giacomo Tesio <giacomo@tesio.it>
|
|
|
|
* See /doc/license/gpl-2.0.txt for details about the licensing.
|
|
|
|
*/
|
2016-11-25 17:18:40 +01:00
|
|
|
|
|
|
|
#include <u.h>
|
2017-04-19 23:33:14 +02:00
|
|
|
#include <lib9.h>
|
2016-11-25 17:18:40 +01:00
|
|
|
#include <bio.h>
|
|
|
|
#include <ndb.h>
|
|
|
|
#include "ndbhf.h"
|
|
|
|
|
|
|
|
static void nstrcpy(char*, char*, int);
|
|
|
|
static void mkptrname(char*, char*, int);
|
|
|
|
static Ndbtuple *doquery(int, char *dn, char *type);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* search for a tuple that has the given 'attr=val' and also 'rattr=x'.
|
|
|
|
* copy 'x' into 'buf' and return the whole tuple.
|
|
|
|
*
|
|
|
|
* return 0 if not found.
|
|
|
|
*/
|
|
|
|
Ndbtuple*
|
|
|
|
dnsquery(char *net, char *val, char *type)
|
|
|
|
{
|
|
|
|
char rip[128];
|
|
|
|
char *p;
|
|
|
|
Ndbtuple *t;
|
|
|
|
int fd;
|
|
|
|
|
|
|
|
/* if the address is V4 or V6 null address, give up early */
|
|
|
|
if(strcmp(val, "::") == 0 || strcmp(val, "0.0.0.0") == 0)
|
|
|
|
return nil;
|
|
|
|
|
|
|
|
if(net == nil)
|
|
|
|
net = "/net";
|
|
|
|
snprint(rip, sizeof(rip), "%s/dns", net);
|
2019-11-26 02:25:23 +01:00
|
|
|
fd = sys_open(rip, ORDWR);
|
2016-11-25 17:18:40 +01:00
|
|
|
if(fd < 0){
|
|
|
|
if(strcmp(net, "/net") == 0)
|
|
|
|
snprint(rip, sizeof(rip), "/srv/dns");
|
|
|
|
else {
|
|
|
|
snprint(rip, sizeof(rip), "/srv/dns%s", net);
|
|
|
|
p = strrchr(rip, '/');
|
|
|
|
*p = '_';
|
|
|
|
}
|
2019-11-26 02:25:23 +01:00
|
|
|
fd = sys_open(rip, ORDWR);
|
2016-11-25 17:18:40 +01:00
|
|
|
if(fd < 0)
|
|
|
|
return nil;
|
2019-11-26 02:25:23 +01:00
|
|
|
if(sys_mount(fd, -1, net, MBEFORE, "", '9') < 0){
|
|
|
|
sys_close(fd);
|
2016-11-25 17:18:40 +01:00
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
/* fd is now closed */
|
|
|
|
snprint(rip, sizeof(rip), "%s/dns", net);
|
2019-11-26 02:25:23 +01:00
|
|
|
fd = sys_open(rip, ORDWR);
|
2016-11-25 17:18:40 +01:00
|
|
|
if(fd < 0)
|
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* zero out the error string */
|
|
|
|
werrstr("");
|
|
|
|
|
|
|
|
/* if this is a reverse lookup, first lookup the domain name */
|
|
|
|
if(strcmp(type, "ptr") == 0){
|
|
|
|
mkptrname(val, rip, sizeof rip);
|
|
|
|
t = doquery(fd, rip, "ptr");
|
|
|
|
} else
|
|
|
|
t = doquery(fd, val, type);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* TODO: make fd static and keep it open to reduce 9P traffic
|
|
|
|
* walking to /net*^/dns. Must be prepared to re-open it on error.
|
|
|
|
*/
|
2019-11-26 02:25:23 +01:00
|
|
|
sys_close(fd);
|
2016-11-25 17:18:40 +01:00
|
|
|
ndbsetmalloctag(t, getcallerpc());
|
|
|
|
return t;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* convert address into a reverse lookup address
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
mkptrname(char *ip, char *rip, int rlen)
|
|
|
|
{
|
|
|
|
char buf[128];
|
|
|
|
char *p, *np;
|
|
|
|
int len;
|
|
|
|
|
|
|
|
if(cistrstr(ip, "in-addr.arpa") || cistrstr(ip, "ip6.arpa")){
|
|
|
|
nstrcpy(rip, ip, rlen);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
nstrcpy(buf, ip, sizeof buf);
|
|
|
|
for(p = buf; *p; p++)
|
|
|
|
;
|
|
|
|
*p = '.';
|
|
|
|
np = rip;
|
|
|
|
len = 0;
|
|
|
|
while(p >= buf){
|
|
|
|
len++;
|
|
|
|
p--;
|
|
|
|
if(*p == '.'){
|
|
|
|
memmove(np, p+1, len);
|
|
|
|
np += len;
|
|
|
|
len = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
memmove(np, p+1, len);
|
|
|
|
np += len;
|
|
|
|
strcpy(np, "in-addr.arpa");
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
nstrcpy(char *to, char *from, int len)
|
|
|
|
{
|
|
|
|
strncpy(to, from, len);
|
|
|
|
to[len-1] = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static Ndbtuple*
|
|
|
|
doquery(int fd, char *dn, char *type)
|
|
|
|
{
|
|
|
|
char buf[1024];
|
|
|
|
int n;
|
|
|
|
Ndbtuple *t, *first, *last;
|
|
|
|
|
2019-11-26 02:25:23 +01:00
|
|
|
sys_seek(fd, 0, 0);
|
2016-11-25 17:18:40 +01:00
|
|
|
snprint(buf, sizeof(buf), "!%s %s", dn, type);
|
2019-11-26 02:25:23 +01:00
|
|
|
if(jehanne_write(fd, buf, strlen(buf)) < 0)
|
2016-11-25 17:18:40 +01:00
|
|
|
return nil;
|
|
|
|
|
2019-11-26 02:25:23 +01:00
|
|
|
sys_seek(fd, 0, 0);
|
2016-11-25 17:18:40 +01:00
|
|
|
|
|
|
|
first = last = nil;
|
|
|
|
|
|
|
|
for(;;){
|
2019-11-26 02:25:23 +01:00
|
|
|
n = jehanne_read(fd, buf, sizeof(buf)-2);
|
2016-11-25 17:18:40 +01:00
|
|
|
if(n <= 0)
|
|
|
|
break;
|
|
|
|
if(buf[n-1] != '\n')
|
|
|
|
buf[n++] = '\n'; /* ndbparsline needs a trailing new line */
|
|
|
|
buf[n] = 0;
|
|
|
|
|
|
|
|
/* check for the error condition */
|
|
|
|
if(buf[0] == '!'){
|
|
|
|
werrstr("%s", buf+1);
|
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
|
|
|
|
t = _ndbparseline(buf);
|
|
|
|
if(t != nil){
|
|
|
|
if(first)
|
|
|
|
last->entry = t;
|
|
|
|
else
|
|
|
|
first = t;
|
|
|
|
last = t;
|
|
|
|
|
|
|
|
while(last->entry)
|
|
|
|
last = last->entry;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ndbsetmalloctag(first, getcallerpc());
|
|
|
|
return first;
|
|
|
|
}
|