+/* -*-c-*-
+ *
+ * Make programs use Unix-domain sockets instead of IP
+ *
+ * (c) 2008 Straylight/Edgeware
+ */
+
+/*----- Licensing notice --------------------------------------------------*
+ *
+ * This file is part of the preload-hacks package.
+ *
+ * Preload-hacks are free software; you can redistribute it and/or modify
+ * them under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or (at
+ * your option) any later version.
+ *
+ * Preload-hacks are distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+ * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ * for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with preload-hacks; if not, write to the Free Software Foundation, Inc.,
+ * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ */
+
#define _GNU_SOURCE
#undef sun
#undef SUN
#define DEBUG
+/*----- Header files ------------------------------------------------------*/
+
+#include <assert.h>
#include <ctype.h>
#include <errno.h>
+#include <stdarg.h>
+#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <arpa/inet.h>
#include <netinet/tcp.h>
#include <netinet/udp.h>
-#include <net/if.h>
+#include <ifaddrs.h>
+#include <netdb.h>
+
+/*----- Data structures ---------------------------------------------------*/
+
+enum { UNUSED, STALE, USED }; /* Unix socket status values */
+enum { WANT_FRESH, WANT_EXISTING }; /* Socket address dispositions */
+enum { DENY, ALLOW }; /* ACL verdicts */
-enum { UNUSED, STALE, USED };
-enum { WANT_FRESH, WANT_EXISTING };
-enum { DENY, ALLOW };
+static int address_families[] = { AF_INET, AF_INET6, -1 };
+
+#define ADDRBUFSZ 64
+
+/* Address representations. */
+typedef union ipaddr {
+ struct in_addr v4;
+ struct in6_addr v6;
+} ipaddr;
+
+/* Convenient socket address hacking. */
+typedef union address {
+ struct sockaddr sa;
+ struct sockaddr_in sin;
+ struct sockaddr_in6 sin6;
+} address;
+
+/* Access control list nodes */
typedef struct aclnode {
struct aclnode *next;
int act;
- unsigned long minaddr, maxaddr;
+ int af;
+ ipaddr minaddr, maxaddr;
unsigned short minport, maxport;
} aclnode;
-#define MAX_LOCAL_IPADDRS 16
-static struct in_addr local_ipaddrs[MAX_LOCAL_IPADDRS];
+/* Local address records */
+typedef struct full_ipaddr {
+ int af;
+ ipaddr addr;
+} full_ipaddr;
+#define MAX_LOCAL_IPADDRS 64
+static full_ipaddr local_ipaddrs[MAX_LOCAL_IPADDRS];
static int n_local_ipaddrs;
+/* General configuration */
static uid_t uid;
static char *sockdir = 0;
static int debug = 0;
static unsigned minautoport = 16384, maxautoport = 65536;
+/* Access control lists */
static aclnode *bind_real, **bind_tail = &bind_real;
static aclnode *connect_real, **connect_tail = &connect_real;
-/* --- Import magic --- */
+/*----- Import the real versions of functions -----------------------------*/
+/* The list of functions to immport. */
#define IMPORTS(_) \
_(socket, int, (int, int, int)) \
_(socketpair, int, (int, int, int, int *)) \
_(sendto, ssize_t, (int, const void *buf, size_t, int, \
const struct sockaddr *to, socklen_t tolen)) \
_(recvfrom, ssize_t, (int, void *buf, size_t, int, \
- struct sockaddr *from, socklen_t *fromlen)) \
+ struct sockaddr *from, socklen_t *fromlen)) \
_(sendmsg, ssize_t, (int, const struct msghdr *, int)) \
_(recvmsg, ssize_t, (int, struct msghdr *, int)) \
- _(close, int, (int))
+ _(ioctl, int, (int, unsigned long, ...))
+/* Function pointers to set up. */
#define DECL(imp, ret, args) static ret (*real_##imp) args;
IMPORTS(DECL)
#undef DECL
-static void setup(void) __attribute__((constructor));
+/* Import the system calls. */
static void import(void)
{
#define IMPORT(imp, ret, args) \
#undef IMPORT
}
-/* --- Support --- */
+/*----- Utilities ---------------------------------------------------------*/
+/* Socket address casts */
#define SA(sa) ((struct sockaddr *)(sa))
#define SIN(sa) ((struct sockaddr_in *)(sa))
+#define SIN6(sa) ((struct sockaddr_in6 *)(sa))
#define SUN(sa) ((struct sockaddr_un *)(sa))
+/* Raw bytes */
#define UC(ch) ((unsigned char)(ch))
+/* Memory allocation */
#define NEW(x) ((x) = xmalloc(sizeof(*x)))
#define NEWV(x, n) ((x) = xmalloc(sizeof(*x) * (n)))
+/* Debugging */
#ifdef DEBUG
# define D(body) { if (debug) { body } }
#else
# define D(body) ;
#endif
+/* Preservation of error status */
#define PRESERVING_ERRNO(body) do { \
int _err = errno; { body } errno = _err; \
} while (0)
+/* Allocate N bytes of memory; abort on failure. */
static void *xmalloc(size_t n)
{
void *p;
return (p);
}
+/* Allocate a copy of the null-terminated string P; abort on failure. */
static char *xstrdup(const char *p)
{
size_t n = strlen(p) + 1;
return (q);
}
-static int unix_socket_status(struct sockaddr_un *sun, int quickp)
+/*----- Address-type hacking ----------------------------------------------*/
+
+/* If M is a simple mask, i.e., consists of a sequence of zero bits followed
+ * by a sequence of one bits, then return the length of the latter sequence
+ * (which may be zero); otherwise return -1.
+ */
+static int simple_mask_length(unsigned long m)
{
- struct stat st;
- FILE *fp = 0;
- size_t len, n;
- int rc;
- char buf[256];
+ int n = 0;
- if (stat(sun->sun_path, &st))
- return (errno == ENOENT ? UNUSED : USED);
- if (!S_ISSOCK(st.st_mode) || quickp)
- return (USED);
- rc = USED;
- if ((fp = fopen("/proc/net/unix", "r")) == 0)
- goto done;
- fgets(buf, sizeof(buf), fp); /* skip header */
- len = strlen(sun->sun_path);
- while (fgets(buf, sizeof(buf), fp)) {
- n = strlen(buf);
- if (n >= len + 2 && buf[n - len - 2] == ' ' && buf[n - 1] == '\n' &&
- memcmp(buf + n - len - 1, sun->sun_path, len) == 0)
- goto done;
+ while (m & 1) { n++; m >>= 1; }
+ return (m ? -1 : n);
+}
+
+/* Answer whether AF is an interesting address family. */
+static int family_known_p(int af)
+{
+ switch (af) {
+ case AF_INET:
+ case AF_INET6:
+ return (1);
+ default:
+ return (0);
+ }
+}
+
+/* Return the socket address length for address family AF. */
+static socklen_t family_socklen(int af)
+{
+ switch (af) {
+ case AF_INET: return (sizeof(struct sockaddr_in));
+ case AF_INET6: return (sizeof(struct sockaddr_in6));
+ default: abort();
+ }
+}
+
+/* Return the width of addresses of kind AF. */
+static int address_width(int af)
+{
+ switch (af) {
+ case AF_INET: return 32;
+ case AF_INET6: return 128;
+ default: abort();
+ }
+}
+
+/* If addresses A and B share a common prefix then return its length;
+ * otherwise return -1.
+ */
+static int common_prefix_length(int af, const ipaddr *a, const ipaddr *b)
+{
+ switch (af) {
+ case AF_INET: {
+ unsigned long aa = ntohl(a->v4.s_addr), bb = ntohl(b->v4.s_addr);
+ unsigned long m = aa^bb;
+ if ((aa&m) == 0 && (bb&m) == m) return (32 - simple_mask_length(m));
+ else return (-1);
+ } break;
+ case AF_INET6: {
+ const uint8_t *aa = a->v6.s6_addr, *bb = b->v6.s6_addr;
+ unsigned m;
+ unsigned n;
+ int i;
+
+ for (i = 0; i < 16 && aa[i] == bb[i]; i++);
+ n = 8*i;
+ if (i < 16) {
+ m = aa[i]^bb[i];
+ if ((aa[i]&m) != 0 || (bb[i]&m) != m) return (-1);
+ n += 8 - simple_mask_length(m);
+ for (i++; i < 16; i++)
+ if (aa[i] || bb[i] != 0xff) return (-1);
+ }
+ return (n);
+ } break;
+ default:
+ abort();
+ }
+}
+
+/* Extract the port number (in host byte-order) from SA. */
+static int port_from_sockaddr(const struct sockaddr *sa)
+{
+ switch (sa->sa_family) {
+ case AF_INET: return (ntohs(SIN(sa)->sin_port));
+ case AF_INET6: return (ntohs(SIN6(sa)->sin6_port));
+ default: abort();
+ }
+}
+
+/* Store the port number PORT (in host byte-order) in SA. */
+static void port_to_sockaddr(struct sockaddr *sa, int port)
+{
+ switch (sa->sa_family) {
+ case AF_INET: SIN(sa)->sin_port = htons(port); break;
+ case AF_INET6: SIN6(sa)->sin6_port = htons(port); break;
+ default: abort();
+ }
+}
+
+/* Extract the address part from SA and store it in A. */
+static void ipaddr_from_sockaddr(ipaddr *a, const struct sockaddr *sa)
+{
+ switch (sa->sa_family) {
+ case AF_INET: a->v4 = SIN(sa)->sin_addr; break;
+ case AF_INET6: a->v6 = SIN6(sa)->sin6_addr; break;
+ default: abort();
+ }
+}
+
+/* Copy a whole socket address about. */
+static void copy_sockaddr(struct sockaddr *sa_dst,
+ const struct sockaddr *sa_src)
+ { memcpy(sa_dst, sa_src, family_socklen(sa_src->sa_family)); }
+
+/* Answer whether two addresses are equal. */
+static int ipaddr_equal_p(int af, const ipaddr *a, const ipaddr *b)
+{
+ switch (af) {
+ case AF_INET: return (a->v4.s_addr == b->v4.s_addr);
+ case AF_INET6: return (memcmp(a->v6.s6_addr, b->v6.s6_addr, 16) == 0);
+ default: abort();
+ }
+}
+
+/* Answer whether the address part of SA is between A and B (inclusive). We
+ * assume that SA has the correct address family.
+ */
+static int sockaddr_in_range_p(const struct sockaddr *sa,
+ const ipaddr *a, const ipaddr *b)
+{
+ switch (sa->sa_family) {
+ case AF_INET: {
+ unsigned long addr = ntohl(SIN(sa)->sin_addr.s_addr);
+ return (ntohl(a->v4.s_addr) <= addr &&
+ addr <= ntohl(b->v4.s_addr));
+ } break;
+ case AF_INET6: {
+ const uint8_t *ss = SIN6(sa)->sin6_addr.s6_addr;
+ const uint8_t *aa = a->v6.s6_addr, *bb = b->v6.s6_addr;
+ int h = 1, l = 1;
+ int i;
+
+ for (i = 0; h && l && i < 16; i++, ss++, aa++, bb++) {
+ if (*ss < *aa || *bb < *ss) return (0);
+ if (*aa < *ss) l = 0;
+ if (*ss < *bb) h = 0;
+ }
+ return (1);
+ } break;
+ default:
+ abort();
+ }
+}
+
+/* Fill in SA with the appropriate wildcard address. */
+static void wildcard_address(int af, struct sockaddr *sa)
+{
+ switch (af) {
+ case AF_INET: {
+ struct sockaddr_in *sin = SIN(sa);
+ memset(sin, 0, sizeof(*sin));
+ sin->sin_family = AF_INET;
+ sin->sin_port = 0;
+ sin->sin_addr.s_addr = INADDR_ANY;
+ } break;
+ case AF_INET6: {
+ struct sockaddr_in6 *sin6 = SIN6(sa);
+ memset(sin6, 0, sizeof(*sin6));
+ sin6->sin6_family = AF_INET6;
+ sin6->sin6_port = 0;
+ sin6->sin6_addr = in6addr_any;
+ sin6->sin6_scope_id = 0;
+ sin6->sin6_flowinfo = 0;
+ } break;
+ default:
+ abort();
}
- if (ferror(fp))
- goto done;
- rc = STALE;
-done:
- if (fp) fclose(fp);
- return (rc);
}
+/* Mask the address A, forcing all but the top PLEN bits to zero or one
+ * according to HIGHP.
+ */
+static void mask_address(int af, ipaddr *a, int plen, int highp)
+{
+ switch (af) {
+ case AF_INET: {
+ unsigned long addr = ntohl(a->v4.s_addr);
+ unsigned long mask = plen ? ~0ul << (32 - plen) : 0;
+ addr &= mask;
+ if (highp) addr |= ~mask;
+ a->v4.s_addr = htonl(addr & 0xffffffff);
+ } break;
+ case AF_INET6: {
+ int i = plen/8;
+ unsigned m = (0xff << (8 - plen%8)) & 0xff;
+ unsigned s = highp ? 0xff : 0;
+ if (m) {
+ a->v6.s6_addr[i] = (a->v6.s6_addr[i] & m) | (s & ~m);
+ i++;
+ }
+ for (; i < 16; i++) a->v6.s6_addr[i] = s;
+ } break;
+ default:
+ abort();
+ }
+}
+
+/* Write a presentation form of SA to BUF, a buffer of length SZ. LEN is the
+ * address length; if it's zero, look it up based on the address family.
+ * Return a pointer to the string (which might, in an emergency, be a static
+ * string rather than your buffer).
+ */
+static char *present_sockaddr(const struct sockaddr *sa, socklen_t len,
+ char *buf, size_t sz)
+{
+#define WANT(n_) do { if (sz < (n_)) goto nospace; } while (0)
+#define PUTC(c_) do { *buf++ = (c_); sz--; } while (0)
+
+ if (!sz) return "<no-space-in-buffer>";
+ if (!len) len = family_socklen(sa->sa_family);
+
+ switch (sa->sa_family) {
+ case AF_UNIX: {
+ struct sockaddr_un *sun = SUN(sa);
+ char *p = sun->sun_path;
+ size_t n = len - offsetof(struct sockaddr_un, sun_path);
+
+ assert(n);
+ if (*p == 0) {
+ WANT(1); PUTC('@');
+ p++; n--;
+ while (n) {
+ switch (*p) {
+ case 0: WANT(2); PUTC('\\'); PUTC('0'); break;
+ case '\a': WANT(2); PUTC('\\'); PUTC('a'); break;
+ case '\n': WANT(2); PUTC('\\'); PUTC('n'); break;
+ case '\r': WANT(2); PUTC('\\'); PUTC('r'); break;
+ case '\t': WANT(2); PUTC('\\'); PUTC('t'); break;
+ case '\v': WANT(2); PUTC('\\'); PUTC('v'); break;
+ case '\\': WANT(2); PUTC('\\'); PUTC('\\'); break;
+ default:
+ if (*p > ' ' && *p <= '~')
+ { WANT(1); PUTC(*p); }
+ else {
+ WANT(4); PUTC('\\'); PUTC('x');
+ PUTC((*p >> 4)&0xf); PUTC((*p >> 0)&0xf);
+ }
+ break;
+ }
+ p++; n--;
+ }
+ } else {
+ if (*p != '/') { WANT(2); PUTC('.'); PUTC('/'); }
+ while (n && *p) { WANT(1); PUTC(*p); p++; n--; }
+ }
+ WANT(1); PUTC(0);
+ } break;
+ case AF_INET: case AF_INET6: {
+ char addrbuf[NI_MAXHOST], portbuf[NI_MAXSERV];
+ int err = getnameinfo(sa, len,
+ addrbuf, sizeof(addrbuf),
+ portbuf, sizeof(portbuf),
+ NI_NUMERICHOST | NI_NUMERICSERV);
+ assert(!err);
+ snprintf(buf, sz, strchr(addrbuf, ':') ? "[%s]:%s" : "%s:%s",
+ addrbuf, portbuf);
+ } break;
+ default:
+ snprintf(buf, sz, "<unknown-address-family %d>", sa->sa_family);
+ break;
+ }
+ return (buf);
+
+nospace:
+ buf[sz - 1] = 0;
+ return (buf);
+}
+
+/* Guess the family of a textual socket address. */
+static int guess_address_family(const char *p)
+ { return (strchr(p, ':') ? AF_INET6 : AF_INET); }
+
+/* Parse a socket address P and write the result to SA. */
+static int parse_sockaddr(struct sockaddr *sa, const char *p)
+{
+ char buf[ADDRBUFSZ];
+ char *q;
+ struct addrinfo *ai, ai_hint = { 0 };
+
+ if (strlen(p) >= sizeof(buf) - 1) return (-1);
+ strcpy(buf, p); p = buf;
+ if (*p != '[') {
+ if ((q = strchr(p, ':')) == 0) return (-1);
+ *q++ = 0;
+ } else {
+ p++;
+ if ((q = strchr(p, ']')) == 0) return (-1);
+ *q++ = 0;
+ if (*q != ':') return (-1);
+ q++;
+ }
+
+ ai_hint.ai_family = AF_UNSPEC;
+ ai_hint.ai_socktype = SOCK_DGRAM;
+ ai_hint.ai_flags = AI_NUMERICHOST | AI_NUMERICSERV;
+ if (getaddrinfo(p, q, &ai_hint, &ai)) return (-1);
+ memcpy(sa, ai->ai_addr, ai->ai_addrlen);
+ freeaddrinfo(ai);
+ return (0);
+}
+
+/*----- Access control lists ----------------------------------------------*/
+
#ifdef DEBUG
-static void dump_aclnode(aclnode *a)
+/* Write to standard error a description of the ACL node A. */
+static void dump_aclnode(const aclnode *a)
{
- char minbuf[16], maxbuf[16];
- struct in_addr amin, amax;
+ char buf[ADDRBUFSZ];
+ const char *p;
+ int plen;
- amin.s_addr = htonl(a->minaddr);
- amax.s_addr = htonl(a->maxaddr);
fprintf(stderr, "noip: %c ", a->act ? '+' : '-');
- if (a->minaddr == 0 && a->maxaddr == 0xffffffff)
- fprintf(stderr, "any");
- else {
- fprintf(stderr, "%s",
- inet_ntop(AF_INET, &amin, minbuf, sizeof(minbuf)));
- if (a->maxaddr != a->minaddr) {
- fprintf(stderr, "-%s",
- inet_ntop(AF_INET, &amax, maxbuf, sizeof(maxbuf)));
- }
- }
+ plen = common_prefix_length(a->af, &a->minaddr, &a->maxaddr);
+ p = inet_ntop(a->af, &a->minaddr, buf, sizeof(buf));
+ fprintf(stderr, strchr(p, ':') ? "[%s]" : "%s", p);
+ if (plen < 0) {
+ p = inet_ntop(a->af, &a->maxaddr, buf, sizeof(buf));
+ fprintf(stderr, strchr(p, ':') ? "-[%s]" : "-%s", p);
+ } else if (plen < address_width(a->af))
+ fprintf(stderr, "/%d", plen);
if (a->minport != 0 || a->maxport != 0xffff) {
fprintf(stderr, ":%u", (unsigned)a->minport);
if (a->minport != a->maxport)
fputc('\n', stderr);
}
+static void dump_acl(const aclnode *a)
+{
+ int act = ALLOW;
+
+ for (; a; a = a->next) {
+ dump_aclnode(a);
+ act = a->act;
+ }
+ fprintf(stderr, "noip: [default policy: %s]\n",
+ act == ALLOW ? "DENY" : "ALLOW");
+}
+
#endif
-static int acl_allows_p(aclnode *a, const struct sockaddr_in *sin)
+/* Returns nonzero if the ACL A allows the socket address SA. */
+static int acl_allows_p(const aclnode *a, const struct sockaddr *sa)
{
- unsigned long addr = ntohl(sin->sin_addr.s_addr);
- unsigned short port = ntohs(sin->sin_port);
+ unsigned short port = port_from_sockaddr(sa);
int act = ALLOW;
- D( char buf[16];
- fprintf(stderr, "noip: check %s:%u\n",
- inet_ntop(AF_INET, &sin->sin_addr, buf, sizeof(buf)),
- ntohs((unsigned)sin->sin_port)); )
+ D({ char buf[ADDRBUFSZ];
+ fprintf(stderr, "noip: check %s\n",
+ present_sockaddr(sa, 0, buf, sizeof(buf))); })
for (; a; a = a->next) {
D( dump_aclnode(a); )
- if (a->minaddr <= addr && addr <= a->maxaddr &&
+ if (sockaddr_in_range_p(sa, &a->minaddr, &a->maxaddr) &&
a->minport <= port && port <= a->maxport) {
D( fprintf(stderr, "noip: aha! %s\n", a->act ? "ALLOW" : "DENY"); )
return (a->act);
return (!act);
}
-#ifdef DEBUG
-
-static void dump_acl(aclnode *a)
-{
- int act = ALLOW;
-
- for (; a; a = a->next) {
- dump_aclnode(a);
- act = a->act;
- }
- fprintf(stderr, "noip: [default policy: %s]\n",
- act == ALLOW ? "DENY" : "ALLOW");
-}
-
-#endif
+/*----- Socket address conversion -----------------------------------------*/
+/* Return a uniformly distributed integer between MIN and MAX inclusive. */
static unsigned randrange(unsigned min, unsigned max)
{
unsigned mask, i;
/* It's so nice not to have to care about the quality of the generator
- much! */
+ * much!
+ */
max -= min;
for (mask = 1; mask < max; mask = (mask << 1) | 1)
;
return (i + min);
}
+/* Return the status of Unix-domain socket address SUN. Returns: UNUSED if
+ * the socket doesn't exist; USED if the path refers to an active socket, or
+ * isn't really a socket at all, or we can't tell without a careful search
+ * and QUICKP is set; or STALE if the file refers to a socket which isn't
+ * being used any more.
+ */
+static int unix_socket_status(struct sockaddr_un *sun, int quickp)
+{
+ struct stat st;
+ FILE *fp = 0;
+ size_t len, n;
+ int rc;
+ char buf[256];
+
+ if (stat(sun->sun_path, &st))
+ return (errno == ENOENT ? UNUSED : USED);
+ if (!S_ISSOCK(st.st_mode) || quickp)
+ return (USED);
+ rc = USED;
+ if ((fp = fopen("/proc/net/unix", "r")) == 0)
+ goto done;
+ if (!fgets(buf, sizeof(buf), fp)) goto done; /* skip header */
+ len = strlen(sun->sun_path);
+ while (fgets(buf, sizeof(buf), fp)) {
+ n = strlen(buf);
+ if (n >= len + 2 && buf[n - len - 2] == ' ' && buf[n - 1] == '\n' &&
+ memcmp(buf + n - len - 1, sun->sun_path, len) == 0)
+ goto done;
+ }
+ if (ferror(fp))
+ goto done;
+ rc = STALE;
+done:
+ if (fp) fclose(fp);
+ return (rc);
+}
+
+/* Encode the Internet address SA as a Unix-domain address SUN. If WANT is
+ * WANT_FRESH, and SA's port number is zero, then we pick an arbitrary local
+ * port. Otherwise we pick the port given. There's an unpleasant hack to
+ * find servers bound to local wildcard addresses. Returns zero on success;
+ * -1 on failure.
+ */
static int encode_inet_addr(struct sockaddr_un *sun,
- const struct sockaddr_in *sin,
+ const struct sockaddr *sa,
int want)
{
int i;
int desperatep = 0;
- char buf[INET_ADDRSTRLEN];
+ address addr;
+ char buf[ADDRBUFSZ];
int rc;
- D( fprintf(stderr, "noip: encode %s:%u (%s)",
- inet_ntop(AF_INET, &sin->sin_addr, buf, sizeof(buf)),
- (unsigned)ntohs(sin->sin_port),
+ D( fprintf(stderr, "noip: encode %s (%s)",
+ present_sockaddr(sa, 0, buf, sizeof(buf)),
want == WANT_EXISTING ? "EXISTING" : "FRESH"); )
sun->sun_family = AF_UNIX;
- if (sin->sin_port || want == WANT_EXISTING) {
- snprintf(sun->sun_path, sizeof(sun->sun_path), "%s/%s:%u", sockdir,
- inet_ntop(AF_INET, &sin->sin_addr, buf, sizeof(buf)),
- (unsigned)ntohs(sin->sin_port));
+ if (port_from_sockaddr(sa) || want == WANT_EXISTING) {
+ snprintf(sun->sun_path, sizeof(sun->sun_path), "%s/%s", sockdir,
+ present_sockaddr(sa, 0, buf, sizeof(buf)));
rc = unix_socket_status(sun, 0);
if (rc == STALE) unlink(sun->sun_path);
if (rc != USED && want == WANT_EXISTING) {
- snprintf(sun->sun_path, sizeof(sun->sun_path), "%s/0.0.0.0:%u",
- sockdir, (unsigned)ntohs(sin->sin_port));
+ wildcard_address(sa->sa_family, &addr.sa);
+ port_to_sockaddr(&addr.sa, port_from_sockaddr(sa));
+ snprintf(sun->sun_path, sizeof(sun->sun_path), "%s/%s", sockdir,
+ present_sockaddr(&addr.sa, 0, buf, sizeof(buf)));
if (unix_socket_status(sun, 0) == STALE) unlink(sun->sun_path);
}
} else {
+ copy_sockaddr(&addr.sa, sa);
for (i = 0; i < 10; i++) {
- snprintf(sun->sun_path, sizeof(sun->sun_path), "%s/%s:%u", sockdir,
- inet_ntop(AF_INET, &sin->sin_addr, buf, sizeof(buf)),
- randrange(minautoport, maxautoport));
+ port_to_sockaddr(&addr.sa, randrange(minautoport, maxautoport));
+ snprintf(sun->sun_path, sizeof(sun->sun_path), "%s/%s", sockdir,
+ present_sockaddr(&addr.sa, 0, buf, sizeof(buf)));
if (unix_socket_status(sun, 1) == UNUSED) goto found;
}
for (desperatep = 0; desperatep < 2; desperatep++) {
for (i = minautoport; i <= maxautoport; i++) {
- snprintf(sun->sun_path, sizeof(sun->sun_path), "%s/%s:%u", sockdir,
- inet_ntop(AF_INET, &sin->sin_addr, buf, sizeof(buf)),
- (unsigned)i);
+ port_to_sockaddr(&addr.sa, i);
+ snprintf(sun->sun_path, sizeof(sun->sun_path), "%s/%s", sockdir,
+ present_sockaddr(&addr.sa, 0, buf, sizeof(buf)));
rc = unix_socket_status(sun, !desperatep);
switch (rc) {
case STALE: unlink(sun->sun_path);
return (0);
}
-static int decode_inet_addr(struct sockaddr_in *sin,
+/* Decode the Unix address SUN to an Internet address SIN. If AF_HINT is
+ * nonzero, an empty address (indicative of an unbound Unix-domain socket) is
+ * translated to a wildcard Internet address of the appropriate family.
+ * Returns zero on success; -1 on failure (e.g., it wasn't one of our
+ * addresses).
+ */
+static int decode_inet_addr(struct sockaddr *sa, int af_hint,
const struct sockaddr_un *sun,
socklen_t len)
{
- char buf[INET_ADDRSTRLEN + 16];
- char *p;
- size_t n = strlen(sockdir), nn = strlen(sun->sun_path);
- struct sockaddr_in sin_mine;
- unsigned long port;
-
- if (!sin)
- sin = &sin_mine;
- if (sun->sun_family != AF_UNIX)
- return (-1);
- if (len < sizeof(sun)) ((char *)sun)[len] = 0;
- D( fprintf(stderr, "noip: decode (%d) `%s'",
- *sun->sun_path, sun->sun_path); )
- if (!sun->sun_path[0]) {
- sin->sin_family = AF_INET;
- sin->sin_addr.s_addr = INADDR_ANY;
- sin->sin_port = 0;
+ char buf[ADDRBUFSZ];
+ size_t n = strlen(sockdir), nn;
+ address addr;
+
+ if (!sa) sa = &addr.sa;
+ if (sun->sun_family != AF_UNIX) return (-1);
+ if (len > sizeof(*sun)) return (-1);
+ ((char *)sun)[len] = 0;
+ nn = strlen(sun->sun_path);
+ D( fprintf(stderr, "noip: decode `%s'", sun->sun_path); )
+ if (af_hint && !sun->sun_path[0]) {
+ wildcard_address(af_hint, sa);
D( fprintf(stderr, " -- unbound socket\n"); )
return (0);
}
D( fprintf(stderr, " -- not one of ours\n"); )
return (-1);
}
- memcpy(buf, sun->sun_path + n + 1, nn - n);
- if ((p = strchr(buf, ':')) == 0) {
- D( fprintf(stderr, " -- malformed (no port)\n"); )
- return (-1);
- }
- *p++ = 0;
- sin->sin_family = AF_INET;
- if (inet_pton(AF_INET, buf, &sin->sin_addr) <= 0) {
- D( fprintf(stderr, " -- malformed (bad address `%s')\n", buf); )
- return (-1);
- }
- port = strtoul(p, &p, 10);
- if (*p || port >= 65536) {
- D( fprintf(stderr, " -- malformed (port out of range)"); )
- return (-1);
- }
- sin->sin_port = htons(port);
- D( fprintf(stderr, " -> %s:%u\n",
- inet_ntop(AF_INET, &sin->sin_addr, buf, sizeof(buf)),
- (unsigned)port); )
+ if (parse_sockaddr(sa, sun->sun_path + n + 1)) return (-1);
+ D( fprintf(stderr, " -> %s\n",
+ present_sockaddr(sa, 0, buf, sizeof(buf))); )
return (0);
}
-static int fixup_real_ip_socket(int sk)
+/* SK is (or at least might be) a Unix-domain socket we created when an
+ * Internet socket was asked for. We've decided it should be an Internet
+ * socket after all, with family AF_HINT, so convert it. If TMP is not null,
+ * then don't replace the existing descriptor: store the new socket in *TMP
+ * and return zero.
+ */
+static int fixup_real_ip_socket(int sk, int af_hint, int *tmp)
{
int nsk;
int type;
int f, fd;
struct sockaddr_un sun;
- struct sockaddr_in sin;
+ address addr;
socklen_t len;
#define OPTS(_) \
len = sizeof(sun);
if (real_getsockname(sk, SA(&sun), &len))
return (-1);
- if (decode_inet_addr(&sin, &sun, len))
+ if (decode_inet_addr(&addr.sa, af_hint, &sun, len))
return (0); /* Not one of ours */
len = sizeof(type);
if (real_getsockopt(sk, SOL_SOCKET, SO_TYPE, &type, &len) < 0 ||
- (nsk = real_socket(PF_INET, type, 0)) < 0)
+ (nsk = real_socket(addr.sa.sa_family, type, 0)) < 0)
return (-1);
#define FIX(opt, ty) do { \
ty ov_; \
len = sizeof(ov_); \
if (real_getsockopt(sk, SOL_SOCKET, SO_##opt, &ov_, &len) < 0 || \
real_setsockopt(nsk, SOL_SOCKET, SO_##opt, &ov_, len)) { \
- real_close(nsk); \
+ close(nsk); \
return (-1); \
} \
} while (0);
OPTS(FIX)
#undef FIX
- if ((f = fcntl(sk, F_GETFL)) < 0 ||
- (fd = fcntl(sk, F_GETFD)) < 0 ||
- fcntl(nsk, F_SETFL, f) < 0 ||
- dup2(nsk, sk) < 0) {
- real_close(nsk);
- return (-1);
- }
- unlink(sun.sun_path);
- real_close(nsk);
- if (fcntl(sk, F_SETFD, fd) < 0) {
- perror("noip: fixup_real_ip_socket F_SETFD");
- abort();
+ if (tmp)
+ *tmp = nsk;
+ else {
+ if ((f = fcntl(sk, F_GETFL)) < 0 ||
+ (fd = fcntl(sk, F_GETFD)) < 0 ||
+ fcntl(nsk, F_SETFL, f) < 0 ||
+ dup2(nsk, sk) < 0) {
+ close(nsk);
+ return (-1);
+ }
+ unlink(sun.sun_path);
+ close(nsk);
+ if (fcntl(sk, F_SETFD, fd) < 0) {
+ perror("noip: fixup_real_ip_socket F_SETFD");
+ abort();
+ }
}
return (0);
}
+/* The socket SK is about to be used to communicate with the remote address
+ * SA. Assign it a local address so that getpeername(2) does something
+ * useful.
+ */
static int do_implicit_bind(int sk, const struct sockaddr **sa,
socklen_t *len, struct sockaddr_un *sun)
{
- struct sockaddr_in sin;
+ address addr;
socklen_t mylen = sizeof(*sun);
- if (acl_allows_p(connect_real, SIN(*sa))) {
- if (fixup_real_ip_socket(sk))
- return (-1);
+ if (acl_allows_p(connect_real, *sa)) {
+ if (fixup_real_ip_socket(sk, (*sa)->sa_family, 0)) return (-1);
} else {
- if (real_getsockname(sk, SA(sun), &mylen) < 0)
- return (-1);
+ if (real_getsockname(sk, SA(sun), &mylen) < 0) return (-1);
if (sun->sun_family == AF_UNIX) {
if (mylen < sizeof(*sun)) ((char *)sun)[mylen] = 0;
if (!sun->sun_path[0]) {
- sin.sin_family = AF_INET;
- sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
- sin.sin_port = 0;
- encode_inet_addr(sun, &sin, WANT_FRESH);
- if (real_bind(sk, SA(sun), SUN_LEN(sun)))
- return (-1);
+ wildcard_address((*sa)->sa_family, &addr.sa);
+ encode_inet_addr(sun, &addr.sa, WANT_FRESH);
+ if (real_bind(sk, SA(sun), SUN_LEN(sun))) return (-1);
}
- encode_inet_addr(sun, SIN(*sa), WANT_EXISTING);
+ encode_inet_addr(sun, *sa, WANT_EXISTING);
*sa = SA(sun);
*len = SUN_LEN(sun);
}
return (0);
}
+/* We found the real address SA, with length LEN; if it's a Unix-domain
+ * address corresponding to a fake socket, convert it to cover up the
+ * deception. Whatever happens, put the result at FAKE and store its length
+ * at FAKELEN.
+ */
static void return_fake_name(struct sockaddr *sa, socklen_t len,
struct sockaddr *fake, socklen_t *fakelen)
{
- struct sockaddr_in sin;
+ address addr;
socklen_t alen;
- if (sa->sa_family == AF_UNIX && !decode_inet_addr(&sin, SUN(sa), len)) {
- sa = SA(&sin);
- len = sizeof(sin);
+ if (sa->sa_family == AF_UNIX &&
+ !decode_inet_addr(&addr.sa, 0, SUN(sa), len)) {
+ sa = &addr.sa;
+ len = family_socklen(addr.sa.sa_family);
}
alen = len;
- if (len > *fakelen)
- len = *fakelen;
- if (len > 0)
- memcpy(fake, sa, len);
+ if (len > *fakelen) len = *fakelen;
+ if (len > 0) memcpy(fake, sa, len);
*fakelen = alen;
}
-/* --- Configuration --- */
+/*----- Configuration -----------------------------------------------------*/
+/* Return the process owner's home directory. */
static char *home(void)
{
char *p;
return "/notexist";
}
+/* Return a good temporary directory to use. */
static char *tmpdir(void)
{
char *p;
else return ("/tmp");
}
+/* Return the user's name, or at least something distinctive. */
static char *user(void)
{
static char buf[16];
}
}
+/* Skip P over space characters. */
#define SKIPSPC do { while (*p && isspace(UC(*p))) p++; } while (0)
+
+/* Set Q to point to the next word following P, null-terminate it, and step P
+ * past it. */
#define NEXTWORD(q) do { \
SKIPSPC; \
q = p; \
while (*p && !isspace(UC(*p))) p++; \
if (*p) *p++ = 0; \
} while (0)
-#define NEXTADDR(q, del) do { \
- SKIPSPC; \
- q = p; \
- while (*p && (*p == '.' || isdigit(UC(*p)))) p++; \
- del = *p; \
- if (*p) *p++ = 0; \
-} while (0)
+
+/* Set Q to point to the next dotted-quad address, store the ending delimiter
+ * in DEL, null-terminate it, and step P past it. */
+static void parse_nextaddr(char **pp, char **qq, int *del)
+{
+ char *p = *pp;
+
+ SKIPSPC;
+ if (*p == '[') {
+ p++; SKIPSPC;
+ *qq = p;
+ p += strcspn(p, "]");
+ if (*p) *p++ = 0;
+ *del = 0;
+ } else {
+ *qq = p;
+ while (*p && (*p == '.' || isdigit(UC(*p)))) p++;
+ *del = *p;
+ if (*p) *p++ = 0;
+ }
+ *pp = p;
+}
+
+/* Set Q to point to the next decimal number, store the ending delimiter in
+ * DEL, null-terminate it, and step P past it. */
#define NEXTNUMBER(q, del) do { \
SKIPSPC; \
q = p; \
del = *p; \
if (*p) *p++ = 0; \
} while (0)
+
+/* Push the character DEL back so we scan it again, unless it's zero
+ * (end-of-file). */
#define RESCAN(del) do { if (del) *--p = del; } while (0)
+
+/* Evaluate true if P is pointing to the word KW (and not some longer string
+ * of which KW is a prefix). */
+
#define KWMATCHP(kw) (strncmp(p, kw, sizeof(kw) - 1) == 0 && \
!isalnum(UC(p[sizeof(kw) - 1])) && \
(p += sizeof(kw) - 1))
-
+
+/* Parse a port list, starting at *PP. Port lists have the form
+ * [:LOW[-HIGH]]: if omitted, all ports are included; if HIGH is omitted,
+ * it's as if HIGH = LOW. Store LOW in *MIN, HIGH in *MAX and set *PP to the
+ * rest of the string.
+ */
static void parse_ports(char **pp, unsigned short *min, unsigned short *max)
{
char *p = *pp, *q;
*pp = p;
}
+/* Make a new ACL node. ACT is the verdict; AF is the address family;
+ * MINADDR and MAXADDR are the ranges on IP addresses; MINPORT and MAXPORT
+ * are the ranges on port numbers; TAIL is the list tail to attach the new
+ * node to.
+ */
#define ACLNODE(tail_, act_, \
- minaddr_, maxaddr_, minport_, maxport_) do { \
+ af_, minaddr_, maxaddr_, minport_, maxport_) do { \
aclnode *a_; \
NEW(a_); \
- a_->act = act_; \
- a_->minaddr = minaddr_; a_->maxaddr = maxaddr_; \
- a_->minport = minport_; a_->maxport = maxport_; \
+ a_->act = (act_); \
+ a_->af = (af_); \
+ a_->minaddr = (minaddr_); a_->maxaddr = (maxaddr_); \
+ a_->minport = (minport_); a_->maxport = (maxport_); \
*tail_ = a_; tail_ = &a_->next; \
} while (0)
+/* Parse an ACL line. *PP points to the end of the line; *TAIL points to
+ * the list tail (i.e., the final link in the list). An ACL entry has the
+ * form +|- [any | local | ADDR | ADDR - ADDR | ADDR/ADDR | ADDR/INT] PORTS
+ * where PORTS is parsed by parse_ports above; an ACL line consists of a
+ * comma-separated sequence of entries..
+ */
static void parse_acl_line(char **pp, aclnode ***tail)
{
- struct in_addr addr;
- unsigned long minaddr, maxaddr, mask;
+ ipaddr minaddr, maxaddr;
unsigned short minport, maxport;
- int i, n;
+ int i, af, n;
int act;
int del;
char *p = *pp;
p++;
SKIPSPC;
if (KWMATCHP("any")) {
- minaddr = 0;
- maxaddr = 0xffffffff;
- goto justone;
+ parse_ports(&p, &minport, &maxport);
+ for (i = 0; address_families[i] >= 0; i++) {
+ af = address_families[i];
+ memset(&minaddr, 0, sizeof(minaddr));
+ maxaddr = minaddr; mask_address(af, &maxaddr, 0, 1);
+ ACLNODE(*tail, act, af, minaddr, maxaddr, minport, maxport);
+ }
} else if (KWMATCHP("local")) {
parse_ports(&p, &minport, &maxport);
- ACLNODE(*tail, act, 0, 0, minport, maxport);
- ACLNODE(*tail, act, 0xffffffff, 0xffffffff, minport, maxport);
+ for (i = 0; address_families[i] >= 0; i++) {
+ af = address_families[i];
+ memset(&minaddr, 0, sizeof(minaddr));
+ maxaddr = minaddr; mask_address(af, &maxaddr, 0, 1);
+ ACLNODE(*tail, act, af, minaddr, minaddr, minport, maxport);
+ ACLNODE(*tail, act, af, maxaddr, maxaddr, minport, maxport);
+ }
for (i = 0; i < n_local_ipaddrs; i++) {
- minaddr = ntohl(local_ipaddrs[i].s_addr);
- ACLNODE(*tail, act, minaddr, minaddr, minport, maxport);
+ ACLNODE(*tail, act, local_ipaddrs[i].af,
+ local_ipaddrs[i].addr, local_ipaddrs[i].addr,
+ minport, maxport);
}
} else {
- if (*p == ':') {
- minaddr = 0;
- maxaddr = 0xffffffff;
- } else {
- NEXTADDR(q, del);
- if (inet_pton(AF_INET, q, &addr) <= 0) goto bad;
- minaddr = ntohl(addr.s_addr);
+ parse_nextaddr(&p, &q, &del);
+ af = guess_address_family(q);
+ if (inet_pton(af, q, &minaddr) <= 0) goto bad;
+ RESCAN(del);
+ SKIPSPC;
+ if (*p == '-') {
+ p++;
+ parse_nextaddr(&p, &q, &del);
+ if (inet_pton(af, q, &maxaddr) <= 0) goto bad;
RESCAN(del);
- SKIPSPC;
- if (*p == '-') {
- p++;
- NEXTADDR(q, del);
- if (inet_pton(AF_INET, q, &addr) <= 0) goto bad;
- RESCAN(del);
- maxaddr = ntohl(addr.s_addr);
- } else if (*p == '/') {
- p++;
- NEXTADDR(q, del);
- if (strchr(q, '.')) {
- if (inet_pton(AF_INET, q, &addr) <= 0) goto bad;
- mask = ntohl(addr.s_addr);
- } else {
- n = strtoul(q, 0, 0);
- mask = (~0ul << (32 - n)) & 0xffffffff;
- }
- RESCAN(del);
- minaddr &= mask;
- maxaddr = minaddr | (mask ^ 0xffffffff);
- } else
- maxaddr = minaddr;
- }
- justone:
+ } else if (*p == '/') {
+ p++;
+ NEXTNUMBER(q, del);
+ n = strtoul(q, 0, 0);
+ maxaddr = minaddr;
+ mask_address(af, &minaddr, n, 0);
+ mask_address(af, &maxaddr, n, 1);
+ } else
+ maxaddr = minaddr;
parse_ports(&p, &minport, &maxport);
- ACLNODE(*tail, act, minaddr, maxaddr, minport, maxport);
+ ACLNODE(*tail, act, af, minaddr, maxaddr, minport, maxport);
}
SKIPSPC;
if (*p != ',') break;
- p++;
+ if (*p) p++;
}
+ *pp = p;
return;
bad:
return;
}
+/* Parse the autoports configuration directive. Syntax is MIN - MAX. */
static void parse_autoports(char **pp)
{
char *p = *pp, *q;
if (*p != '-') goto bad; p++;
NEXTNUMBER(q, del); y = strtoul(q, 0, 0); RESCAN(del);
minautoport = x; maxautoport = y;
+ *pp = p;
return;
bad:
return;
}
+/* Parse an ACL from an environment variable VAR, attaching it to the list
+ * TAIL. */
static void parse_acl_env(const char *var, aclnode ***tail)
{
char *p, *q;
}
}
+/* Read the configuration from the config file and environment. */
static void readconfig(void)
{
FILE *fp;
dump_acl(connect_real); )
}
-/* --- Hooks --- */
+/*----- Overridden system calls -------------------------------------------*/
int socket(int pf, int ty, int proto)
{
- if (pf == PF_INET) {
- pf = PF_UNIX;
- proto = 0;
+ switch (pf) {
+ default:
+ if (!family_known_p(pf)) {
+ errno = EAFNOSUPPORT;
+ return (-1);
+ }
+ pf = PF_UNIX;
+ proto = 0;
+ case PF_UNIX:
+#ifdef PF_NETLINK
+ case PF_NETLINK:
+#endif
+ return (real_socket(pf, ty, proto));
}
- return real_socket(pf, ty, proto);
}
int socketpair(int pf, int ty, int proto, int *sk)
{
- if (pf == PF_INET) {
+ if (family_known_p(pf)) {
pf = PF_UNIX;
proto = 0;
}
{
struct sockaddr_un sun;
- if (sa->sa_family == AF_INET) {
+ if (family_known_p(sa->sa_family)) {
PRESERVING_ERRNO({
- if (acl_allows_p(bind_real, SIN(sa))) {
- if (fixup_real_ip_socket(sk))
+ if (acl_allows_p(bind_real, sa)) {
+ if (fixup_real_ip_socket(sk, sa->sa_family, 0))
return (-1);
} else {
- encode_inet_addr(&sun, SIN(sa), WANT_FRESH);
+ encode_inet_addr(&sun, sa, WANT_FRESH);
sa = SA(&sun);
len = SUN_LEN(&sun);
}
});
}
- return real_bind(sk, sa, len);
+ return (real_bind(sk, sa, len));
}
int connect(int sk, const struct sockaddr *sa, socklen_t len)
{
struct sockaddr_un sun;
+ int rc;
- if (sa->sa_family == AF_INET) {
+ if (!family_known_p(sa->sa_family))
+ rc = real_connect(sk, sa, len);
+ else {
PRESERVING_ERRNO({
do_implicit_bind(sk, &sa, &len, &sun);
});
+ rc = real_connect(sk, sa, len);
+ if (rc < 0) {
+ switch (errno) {
+ case ENOENT: errno = ECONNREFUSED; break;
+ }
+ }
}
- return real_connect(sk, sa, len);
+ return (rc);
}
ssize_t sendto(int sk, const void *buf, size_t len, int flags,
{
struct sockaddr_un sun;
- if (to && to->sa_family == AF_INET) {
+ if (to && family_known_p(to->sa_family)) {
PRESERVING_ERRNO({
do_implicit_bind(sk, &to, &tolen, &sun);
});
}
- return real_sendto(sk, buf, len, flags, to, tolen);
+ return (real_sendto(sk, buf, len, flags, to, tolen));
}
ssize_t recvfrom(int sk, void *buf, size_t len, int flags,
const struct sockaddr *sa;
struct msghdr mymsg;
- if (msg->msg_name && SA(msg->msg_name)->sa_family == AF_INET) {
+ if (msg->msg_name && family_known_p(SA(msg->msg_name)->sa_family)) {
PRESERVING_ERRNO({
sa = SA(msg->msg_name);
mymsg = *msg;
msg = &mymsg;
});
}
- return real_sendmsg(sk, msg, flags);
+ return (real_sendmsg(sk, msg, flags));
}
ssize_t recvmsg(int sk, struct msghdr *msg, int flags)
ssize_t n;
if (!msg->msg_name)
- return real_recvmsg(sk, msg, flags);
+ return (real_recvmsg(sk, msg, flags));
PRESERVING_ERRNO({
sa = SA(msg->msg_name);
len = msg->msg_namelen;
memset(p, 0, *len);
return (0);
}
- return real_getsockopt(sk, lev, opt, p, len);
+ return (real_getsockopt(sk, lev, opt, p, len));
}
int setsockopt(int sk, int lev, int opt, const void *p, socklen_t len)
case SO_DETACH_FILTER:
return (0);
}
- return real_setsockopt(sk, lev, opt, p, len);
+ return (real_setsockopt(sk, lev, opt, p, len));
}
-/* --- Initialization --- */
+int ioctl(int fd, unsigned long op, ...)
+{
+ va_list ap;
+ void *arg;
+ int sk;
+ int rc;
+
+ va_start(ap, op);
+ arg = va_arg(ap, void *);
+
+ switch (op) {
+ case SIOCGIFADDR:
+ case SIOCGIFBRDADDR:
+ case SIOCGIFDSTADDR:
+ case SIOCGIFNETMASK:
+ PRESERVING_ERRNO({
+ if (fixup_real_ip_socket(fd, AF_INET, &sk)) goto real;
+ });
+ rc = real_ioctl(sk, op, arg);
+ PRESERVING_ERRNO({ close(sk); });
+ break;
+ default:
+ real:
+ rc = real_ioctl(fd, op, arg);
+ break;
+ }
+ va_end(ap);
+ return (rc);
+}
+/*----- Initialization ----------------------------------------------------*/
+
+/* Clean up the socket directory, deleting stale sockets. */
static void cleanup_sockdir(void)
{
DIR *dir;
struct dirent *d;
- struct sockaddr_in sin;
+ address addr;
struct sockaddr_un sun;
struct stat st;
- if ((dir = opendir(sockdir)) == 0)
- return;
+ if ((dir = opendir(sockdir)) == 0) return;
sun.sun_family = AF_UNIX;
while ((d = readdir(dir)) != 0) {
if (d->d_name[0] == '.') continue;
snprintf(sun.sun_path, sizeof(sun.sun_path),
"%s/%s", sockdir, d->d_name);
- if (decode_inet_addr(&sin, &sun, SUN_LEN(&sun)) ||
+ if (decode_inet_addr(&addr.sa, 0, &sun, SUN_LEN(&sun)) ||
stat(sun.sun_path, &st) ||
!S_ISSOCK(st.st_mode)) {
D( fprintf(stderr, "noip: ignoring unknown socketdir entry `%s'\n",
closedir(dir);
}
+/* Find the addresses attached to local network interfaces, and remember them
+ * in a table.
+ */
static void get_local_ipaddrs(void)
{
- struct if_nameindex *ifn;
- struct ifreq ifr;
- int sk;
+ struct ifaddrs *ifa_head, *ifa;
+ ipaddr a;
int i;
- ifn = if_nameindex();
- if ((sk = real_socket(PF_INET, SOCK_STREAM, 00)) < 0)
- return;
- for (i = n_local_ipaddrs = 0;
- n_local_ipaddrs < MAX_LOCAL_IPADDRS &&
- ifn[i].if_name && *ifn[i].if_name;
- i++) {
- strcpy(ifr.ifr_name, ifn[i].if_name);
- if (ioctl(sk, SIOCGIFADDR, &ifr) || ifr.ifr_addr.sa_family != AF_INET)
+ if (getifaddrs(&ifa_head)) { perror("getifaddrs"); return; }
+ for (n_local_ipaddrs = 0, ifa = ifa_head;
+ n_local_ipaddrs < MAX_LOCAL_IPADDRS && ifa;
+ ifa = ifa->ifa_next) {
+ if (!ifa->ifa_addr || !family_known_p(ifa->ifa_addr->sa_family))
continue;
- local_ipaddrs[n_local_ipaddrs++] =
- SIN(&ifr.ifr_addr)->sin_addr;
- D( fprintf(stderr, "noip: local addr %s = %s\n", ifn[i].if_name,
- inet_ntoa(local_ipaddrs[n_local_ipaddrs - 1])); )
+ ipaddr_from_sockaddr(&a, ifa->ifa_addr);
+ D({ char buf[ADDRBUFSZ];
+ fprintf(stderr, "noip: local addr %s = %s", ifa->ifa_name,
+ inet_ntop(ifa->ifa_addr->sa_family, &a,
+ buf, sizeof(buf))); })
+ for (i = 0; i < n_local_ipaddrs; i++) {
+ if (ifa->ifa_addr->sa_family == local_ipaddrs[i].af &&
+ ipaddr_equal_p(local_ipaddrs[i].af, &a, &local_ipaddrs[i].addr)) {
+ D( fprintf(stderr, " (duplicate)\n"); )
+ goto skip;
+ }
+ }
+ D( fprintf(stderr, "\n"); )
+ local_ipaddrs[n_local_ipaddrs].af = ifa->ifa_addr->sa_family;
+ local_ipaddrs[n_local_ipaddrs].addr = a;
+ n_local_ipaddrs++;
+ skip:;
}
- close(sk);
+ freeifaddrs(ifa_head);
}
-static void printerr(const char *p) { write(STDERR_FILENO, p, strlen(p)); }
+/* Print the given message to standard error. Avoids stdio. */
+static void printerr(const char *p)
+ { if (write(STDERR_FILENO, p, strlen(p))) ; }
+/* Create the socket directory, being careful about permissions. */
static void create_sockdir(void)
{
struct stat st;
- if (stat(sockdir, &st)) {
+ if (lstat(sockdir, &st)) {
if (errno == ENOENT) {
if (mkdir(sockdir, 0700)) {
perror("noip: creating socketdir");
exit(127);
}
- if (!stat(sockdir, &st))
+ if (!lstat(sockdir, &st))
goto check;
}
perror("noip: checking socketdir");
}
}
+/* Initialization function. */
+static void setup(void) __attribute__((constructor));
static void setup(void)
{
PRESERVING_ERRNO({
cleanup_sockdir();
});
}
+
+/*----- That's all, folks -------------------------------------------------*/