Radish alpha
H
HardenedBSD Package Manager
Radicle
Git (anonymous pull)
Log in to clone via SSH
Add the ability to setup the nameserver via pkg.conf or env
Baptiste Daroussin committed 13 years ago
commit 28ee589fb0670b443682cc641129671066ce4bec
parent 89063cd0066bce868cc2340625a727d38888b1fe
4 files changed +59 -0
modified libpkg/dns_utils.c
@@ -31,6 +31,8 @@
#include <string.h>
#include <netinet/in.h>
#include <resolv.h>
+
#include <netdb.h>
+

#include "private/utils.h"

typedef union {
@@ -132,3 +134,46 @@ dns_getsrvinfo(const char *zone)

	return (first);
}
+

+
int
+
set_nameserver(const char *nsname) {
+
	struct __res_state res;
+
	union res_sockaddr_union u[MAXNS];
+
	struct addrinfo *answer = NULL;
+
	struct addrinfo *cur = NULL;
+
	struct addrinfo hint;
+
	int nscount = 0;
+

+
	memset(u, 0, sizeof(u));
+
	memset(&hint, 0, sizeof(hint));
+
	hint.ai_socktype = SOCK_DGRAM;
+

+
	if (res_ninit(&res) == -1)
+
		return (-1);
+

+
	if (getaddrinfo(nsname, NULL, &hint, &answer) == 0) {
+
		for (cur = answer; cur != NULL; cur = cur->ai_next) {
+
			if (nscount == MAXNS)
+
				break;
+
			switch (cur->ai_addr->sa_family) {
+
			case AF_INET6:
+
				u[nscount].sin6 = *(struct sockaddr_in6*)(void *)cur->ai_addr;
+
				u[nscount++].sin6.sin6_port = htons(53);
+
				break;
+
			case AF_INET:
+
				u[nscount].sin = *(struct sockaddr_in*)(void *)cur->ai_addr;
+
				u[nscount++].sin.sin_port = htons(53);
+
				break;
+
			}
+
		}
+
		if (nscount != 0)
+
			res_setservers(&res, u, nscount);
+
		freeaddrinfo(answer);
+
	}
+
	if (nscount == 0)
+
		return (-1);
+

+
	_res = res;
+

+
	return (0);
+
}
modified libpkg/pkg.h.in
@@ -314,6 +314,7 @@ typedef enum _pkg_config_key {
	PKG_CONFIG_REPO_AUTOUPDATE,
	PKG_CONFIG_HTTP_PROXY,
	PKG_CONFIG_FTP_PROXY,
+
	PKG_CONFIG_NAMESERVER,
} pkg_config_key;

typedef enum {
modified libpkg/pkg_config.c
@@ -206,6 +206,11 @@ static struct config_entry c[] = {
		"FTP_PROXY",
		NULL,
	},
+
	[PKG_CONFIG_NAMESERVER] = {
+
		PKG_CONFIG_STRING,
+
		"NAMESERVER",
+
		NULL,
+
	},
};

static bool parsed = false;
@@ -559,6 +564,7 @@ pkg_init(const char *path)
	const char *val = NULL;
	const char *errstr = NULL;
	const char *proxy = NULL;
+
	const char *nsname = NULL;
	struct pkg_config *conf;

	pkg_get_myarch(myabi, BUFSIZ);
@@ -672,6 +678,11 @@ pkg_init(const char *path)
	if (proxy != NULL)
		setenv("FTP_PROXY", proxy, 1);

+
	/* bypass resolv.conf with specified NAMESERVER if any */
+
	pkg_config_string(PKG_CONFIG_NAMESERVER, &nsname);
+
	if (nsname != NULL)
+
		set_nameserver(nsname);
+

	return (EPKG_OK);
}

modified libpkg/private/utils.h
@@ -83,4 +83,6 @@ bool is_hardlink(struct hardlinks *hl, struct stat *st);
struct dns_srvinfo *
	dns_getsrvinfo(const char *zone);

+
int set_nameserver(const char *nsname);
+

#endif