Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
edmonds committed Sep 25, 2011
0 parents commit a8a53bc
Show file tree
Hide file tree
Showing 8 changed files with 871 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.*swp
*.so.*
*.o
17 changes: 17 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
CC = gcc
CFLAGS = --std=gnu99 -fPIC -O2 -g -ggdb -Wall
LDFLAGS = -lunbound

BINS = libnss_ubdns.so.2

all: $(BINS)

UBDNS_OBJS = arpa.o domain_to_str.o lookup.o ubdns.o

libnss_ubdns.so.2: $(UBDNS_OBJS)
$(CC) -fPIC -shared -Wl,-h,libnss_ubdns.so.2 -Wl,--version-script,nss_ubdns.map -o $@ $(LDFLAGS) $^

clean:
rm -f $(BINS) $(UBDNS_OBJS)

.PHONY: all clean
89 changes: 89 additions & 0 deletions arpa.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/* arpa.c - convert IPv4 or IPv6 address to PTR qname
* based on isip4() and isip6() from unbound-host
*/

/*
* Copyright (C) 2011 Robert S. Edmonds
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
* OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/

/*
* checkconf/unbound-host.c - replacement for host that supports validation.
*
* Copyright (c) 2007, NLnet Labs. All rights reserved.
*
* This software is open source.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* Neither the name of the NLNET LABS nor the names of its contributors may
* be used to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/

#include <stdint.h>
#include <stdio.h>
#include <string.h>

void
arpa_qname_ip4(const void *addr, char **res) {
char buf[32];
snprintf(buf, sizeof(buf), "%u.%u.%u.%u.in-addr.arpa.",
(unsigned)((uint8_t *) addr)[3],
(unsigned)((uint8_t *) addr)[2],
(unsigned)((uint8_t *) addr)[1],
(unsigned)((uint8_t *) addr)[0]
);
*res = strdup(buf);
}

void
arpa_qname_ip6(const void *addr, char **res) {
const char *hex = "0123456789abcdef";
char buf[128];
char *p;
int i;

p = buf;
for (i = 15; i >= 0; i--) {
uint8_t b = ((uint8_t *) addr)[i];
*p++ = hex[ (b & 0x0f) ];
*p++ = '.';
*p++ = hex[ (b & 0xf0) >> 4 ];
*p++ = '.';
}
snprintf(buf + 16*4, sizeof(buf) - 16*4, "ip6.arpa.");
*res = strdup(buf);
}
73 changes: 73 additions & 0 deletions domain_to_str.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/* domain_to_str.c - convert wire-format DNS name to presentation format
* from wreck/wdns/msg/domain_to_str.c
*/

/*
* Copyright (c) 2009, 2010 by Internet Systems Consortium, Inc. ("ISC")
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
* OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/

#include <assert.h>
#include <stdint.h>
#include <stdio.h>

/**
* Convert a domain name to a human-readable string.
*
* \param[in] src domain name in wire format
* \param[in] src_len length of domain name in bytes
* \param[out] dst caller-allocated string buffer of size WDNS_PRESLEN_NAME
*
* \return Number of bytes read from src.
*/

size_t
domain_to_str(const uint8_t *src, size_t src_len, char *dst) {
size_t bytes_read = 0;
size_t bytes_remaining = src_len;
uint8_t oclen;

assert(src != NULL);

oclen = *src;
while (bytes_remaining > 0 && oclen != 0) {
src++;
bytes_remaining--;

bytes_read += oclen + 1 /* length octet */;

while (oclen-- && bytes_remaining > 0) {
uint8_t c = *src++;
bytes_remaining--;

if (c == '.') {
*dst++ = '\\';
*dst++ = c;
} else if (c >= '!' && c <= '~') {
*dst++ = c;
} else {
snprintf(dst, 5, "\\%.3d", c);
dst += 4;
}
}
*dst++ = '.';
oclen = *src;
}
if (bytes_read == 0)
*dst++ = '.';
bytes_read++;

*dst = '\0';
return (bytes_read);
}
181 changes: 181 additions & 0 deletions lookup.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,181 @@
/*
* Copyright (C) 2011 Robert S. Edmonds
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
* OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/

#include <assert.h>
#include <ctype.h>
#include <stdbool.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>

#include <unbound.h>

#include "ubdns.h"

static struct ub_ctx *ctx = NULL;

static void __attribute__((constructor))
ubdns_init(void) {
int ret = 0;

ctx = ub_ctx_create();
if (ctx != NULL) {
int ret;

ret = ub_ctx_resolvconf(ctx, "/etc/ubdns/resolv.conf");
if (ret != 0)
goto out;

ret = ub_ctx_add_ta_file(ctx, "/var/lib/unbound/root.key");
if (ret != 0)
goto out;
}

out:
if (ret != 0) {
if (ctx != NULL)
ub_ctx_delete(ctx);
ctx = NULL;
}
}

static void __attribute__((destructor))
ubdns_finish(void) {
ub_ctx_delete(ctx);
ctx = NULL;
}

static bool
ubdns_check_result(struct ub_result *res) {
if (res->havedata == 0)
return (false);
if (res->bogus)
return (false);

return (true);
}

static int
ubdns_add_result(struct address **_list, unsigned *_n_list, struct ub_result *res, int af) {
struct address *list = *_list;
unsigned n_list = *_n_list;
int i;

if (!ubdns_check_result(res))
return (0);

for (i = 0; res->data[i] != NULL; i++) {
if (res->len[i] == PROTO_ADDRESS_SIZE(af)) {
list = realloc(list, (n_list + 1) * sizeof(struct address));
if (!list)
return (-1);

list[n_list].family = af;
list[n_list].scope = 0;
memcpy(list[n_list].address, res->data[i], PROTO_ADDRESS_SIZE(af));
n_list += 1;
}
}

*_list = list;
*_n_list = n_list;
return (0);
}

int
ubdns_lookup_forward(const char *hn, int af, struct address **_list, unsigned *_n_list) {
struct address *list = NULL;
unsigned n_list = 0;
int r = 1;
int ret;

struct ub_result *res;

if (ctx == NULL)
goto err;

if (af == AF_INET || af == AF_UNSPEC) {
ret = ub_resolve(ctx, (char *) hn, 1 /*A*/, 1 /*IN*/, &res);
if (ret != 0)
goto err;

ret = ubdns_add_result(&list, &n_list, res, AF_INET);
if (ret != 0)
goto err;

ub_resolve_free(res);
}

if (af == AF_INET6 || af == AF_UNSPEC) {
ret = ub_resolve(ctx, (char *) hn, 28 /*A*/, 1 /*IN*/, &res);
if (ret != 0)
goto err;

ret = ubdns_add_result(&list, &n_list, res, AF_INET6);
if (ret != 0)
goto err;

ub_resolve_free(res);
}

finish:
if (r < 0) {
free(list);
} else {
qsort(list, n_list, sizeof(struct address), address_compare);

*_list = list;
*_n_list = n_list;
}

return r;
err:
r = 0;
goto finish;
}

char *
ubdns_lookup_reverse(const void *addr, int af) {
struct ub_result *res;
char *qname = NULL;
int ret;

if (ctx == NULL)
return (NULL);

if (af == AF_INET) {
arpa_qname_ip4(addr, &qname);
} else if (af == AF_INET6) {
arpa_qname_ip6(addr, &qname);
} else {
return (NULL);
}

ret = ub_resolve(ctx, qname, 12 /*PTR*/, 1 /*IN*/, &res);

if (ret == 0 &&
ubdns_check_result(res) &&
res->data[0] != NULL)
{
char name[1025];
domain_to_str((const uint8_t *) res->data[0], res->len[0], name);
return (strdup(name));
}

return (NULL);
}
11 changes: 11 additions & 0 deletions nss_ubdns.map
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
EXPORTED {
global:
_nss_ubdns_gethostbyaddr2_r;
_nss_ubdns_gethostbyaddr_r;
_nss_ubdns_gethostbyname2_r;
_nss_ubdns_gethostbyname3_r;
_nss_ubdns_gethostbyname4_r;
_nss_ubdns_gethostbyname_r;
local:
*;
};
Loading

0 comments on commit a8a53bc

Please sign in to comment.