X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~mdw/git/preload-hacks/blobdiff_plain/658c1774604911024016a572a119688031d69d77..deedf22d7c74bec2330fd0e555d1dda49e96b26e:/noip.c diff --git a/noip.c b/noip.c index b9043f0..0fab12e 100644 --- a/noip.c +++ b/noip.c @@ -89,6 +89,23 @@ typedef struct aclnode { unsigned short minport, maxport; } aclnode; +/* Implicit bind records */ +typedef struct impbind { + struct impbind *next; + int af, how; + ipaddr minaddr, maxaddr, bindaddr; +} impbind; +enum { EXPLICIT, SAME }; + +/* A type for an address range */ +typedef struct addrrange { + int type; + union { + struct { int af; ipaddr min, max; } range; + } u; +} addrrange; +enum { EMPTY, ANY, LOCAL, RANGE }; + /* Local address records */ typedef struct full_ipaddr { int af; @@ -107,6 +124,7 @@ 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; +static impbind *impbinds, **impbind_tail = &impbinds; /*----- Import the real versions of functions -----------------------------*/ @@ -161,8 +179,10 @@ static void import(void) /* Debugging */ #ifdef DEBUG # define D(body) { if (debug) { body } } +# define Dpid pid_t pid = debug ? getpid() : -1 #else # define D(body) ; +# define Dpid #endif /* Preservation of error status */ @@ -298,6 +318,23 @@ static void ipaddr_from_sockaddr(ipaddr *a, const struct sockaddr *sa) } } +/* Store the address A in SA. */ +static void ipaddr_to_sockaddr(struct sockaddr *sa, const ipaddr *a) +{ + switch (sa->sa_family) { + case AF_INET: + SIN(sa)->sin_addr = a->v4; + break; + case AF_INET6: + SIN6(sa)->sin6_addr = a->v6; + SIN6(sa)->sin6_scope_id = 0; + SIN6(sa)->sin6_flowinfo = 0; + break; + default: + abort(); + } +} + /* Copy a whole socket address about. */ static void copy_sockaddr(struct sockaddr *sa_dst, const struct sockaddr *sa_src) @@ -407,6 +444,7 @@ static char *present_sockaddr(const struct sockaddr *sa, socklen_t len, #define WANT(n_) do { if (sz < (n_)) goto nospace; } while (0) #define PUTC(c_) do { *buf++ = (c_); sz--; } while (0) + if (!sa) return ""; if (!sz) return ""; if (!len) len = family_socklen(sa->sa_family); @@ -504,22 +542,27 @@ static int parse_sockaddr(struct sockaddr *sa, const char *p) #ifdef DEBUG -/* Write to standard error a description of the ACL node A. */ -static void dump_aclnode(const aclnode *a) +static void dump_addrrange(int af, const ipaddr *min, const ipaddr *max) { char buf[ADDRBUFSZ]; const char *p; int plen; - fprintf(stderr, "noip: %c ", a->act ? '+' : '-'); - plen = common_prefix_length(a->af, &a->minaddr, &a->maxaddr); - p = inet_ntop(a->af, &a->minaddr, buf, sizeof(buf)); + plen = common_prefix_length(af, min, max); + p = inet_ntop(af, min, buf, sizeof(buf)); fprintf(stderr, strchr(p, ':') ? "[%s]" : "%s", p); if (plen < 0) { - p = inet_ntop(a->af, &a->maxaddr, buf, sizeof(buf)); + p = inet_ntop(af, &max, buf, sizeof(buf)); fprintf(stderr, strchr(p, ':') ? "-[%s]" : "-%s", p); - } else if (plen < address_width(a->af)) + } else if (plen < address_width(af)) fprintf(stderr, "/%d", plen); +} + +/* Write to standard error a description of the ACL node A. */ +static void dump_aclnode(const aclnode *a) +{ + fprintf(stderr, "noip(%d): %c ", getpid(), a->act ? '+' : '-'); + dump_addrrange(a->af, &a->minaddr, &a->maxaddr); if (a->minport != 0 || a->maxport != 0xffff) { fprintf(stderr, ":%u", (unsigned)a->minport); if (a->minport != a->maxport) @@ -536,7 +579,7 @@ static void dump_acl(const aclnode *a) dump_aclnode(a); act = a->act; } - fprintf(stderr, "noip: [default policy: %s]\n", + fprintf(stderr, "noip(%d): [default policy: %s]\n", getpid(), act == ALLOW ? "DENY" : "ALLOW"); } @@ -547,20 +590,24 @@ static int acl_allows_p(const aclnode *a, const struct sockaddr *sa) { unsigned short port = port_from_sockaddr(sa); int act = ALLOW; + Dpid; D({ char buf[ADDRBUFSZ]; - fprintf(stderr, "noip: check %s\n", + fprintf(stderr, "noip(%d): check %s\n", pid, present_sockaddr(sa, 0, buf, sizeof(buf))); }) for (; a; a = a->next) { D( dump_aclnode(a); ) - if (sockaddr_in_range_p(sa, &a->minaddr, &a->maxaddr) && + if (a->af == sa->sa_family && + 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"); ) + D( fprintf(stderr, "noip(%d): aha! %s\n", pid, + a->act ? "ALLOW" : "DENY"); ) return (a->act); } act = a->act; } - D( fprintf(stderr, "noip: nothing found: %s\n", act ? "DENY" : "ALLOW"); ) + D( fprintf(stderr, "noip(%d): nothing found: %s\n", pid, + act ? "DENY" : "ALLOW"); ) return (!act); } @@ -618,6 +665,34 @@ done: return (rc); } +/* Convert the IP address SA to a Unix-domain address SUN. Fail if the + * address seems already taken. If DESPARATEP then try cleaning up stale old + * sockets. + */ +static int encode_unused_inet_addr(struct sockaddr *sa, + struct sockaddr_un *sun, + int desperatep) +{ + address waddr; + struct sockaddr_un wsun; + int rc; + char buf[ADDRBUFSZ]; + + snprintf(sun->sun_path, sizeof(sun->sun_path), "%s/%s", sockdir, + present_sockaddr(sa, 0, buf, sizeof(buf))); + if ((rc = unix_socket_status(sun, !desperatep)) == USED) return (-1); + else if (rc == STALE) unlink(sun->sun_path); + + wildcard_address(sa->sa_family, &waddr.sa); + port_to_sockaddr(&waddr.sa, port_from_sockaddr(sa)); + snprintf(wsun.sun_path, sizeof(wsun.sun_path), "%s/%s", sockdir, + present_sockaddr(&waddr.sa, 0, buf, sizeof(buf))); + if ((rc = unix_socket_status(&wsun, !desperatep)) == USED) return (-1); + else if (rc == STALE) unlink(wsun.sun_path); + + return (0); +} + /* 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 @@ -634,7 +709,7 @@ static int encode_inet_addr(struct sockaddr_un *sun, char buf[ADDRBUFSZ]; int rc; - D( fprintf(stderr, "noip: encode %s (%s)", + D( fprintf(stderr, "noip(%d): encode %s (%s)", getpid(), present_sockaddr(sa, 0, buf, sizeof(buf)), want == WANT_EXISTING ? "EXISTING" : "FRESH"); ) sun->sun_family = AF_UNIX; @@ -654,20 +729,12 @@ static int encode_inet_addr(struct sockaddr_un *sun, copy_sockaddr(&addr.sa, sa); for (i = 0; i < 10; i++) { 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; + if (!encode_unused_inet_addr(&addr.sa, sun, 0)) goto found; } for (desperatep = 0; desperatep < 2; desperatep++) { for (i = minautoport; i <= maxautoport; 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); - case UNUSED: goto found; - } + if (!encode_unused_inet_addr(&addr.sa, sun, 0)) goto found; } } errno = EADDRINUSE; @@ -698,7 +765,7 @@ static int decode_inet_addr(struct sockaddr *sa, int af_hint, if (len > sizeof(*sun)) return (-1); ((char *)sun)[len] = 0; nn = strlen(sun->sun_path); - D( fprintf(stderr, "noip: decode `%s'", sun->sun_path); ) + D( fprintf(stderr, "noip(%d): decode `%s'", getpid(), sun->sun_path); ) if (af_hint && !sun->sun_path[0]) { wildcard_address(af_hint, sa); D( fprintf(stderr, " -- unbound socket\n"); ) @@ -785,6 +852,58 @@ static int fixup_real_ip_socket(int sk, int af_hint, int *tmp) 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) +{ + address addr; + socklen_t alen; + + 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); + *fakelen = alen; +} + +/*----- Implicit binding --------------------------------------------------*/ + +#ifdef DEBUG + +static void dump_impbind(const impbind *i) +{ + char buf[ADDRBUFSZ]; + + fprintf(stderr, "noip(%d): ", getpid()); + dump_addrrange(i->af, &i->minaddr, &i->maxaddr); + switch (i->how) { + case SAME: fprintf(stderr, " "); break; + case EXPLICIT: + fprintf(stderr, " %s", inet_ntop(i->af, &i->bindaddr, + buf, sizeof(buf))); + break; + default: abort(); + } + fputc('\n', stderr); +} + +static void dump_impbind_list(void) +{ + const impbind *i; + + for (i = impbinds; i; i = i->next) dump_impbind(i); +} + +#endif + /* 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. @@ -794,6 +913,8 @@ static int do_implicit_bind(int sk, const struct sockaddr **sa, { address addr; socklen_t mylen = sizeof(*sun); + const impbind *i; + Dpid; if (acl_allows_p(connect_real, *sa)) { if (fixup_real_ip_socket(sk, (*sa)->sa_family, 0)) return (-1); @@ -802,7 +923,20 @@ static int do_implicit_bind(int sk, const struct sockaddr **sa, if (sun->sun_family == AF_UNIX) { if (mylen < sizeof(*sun)) ((char *)sun)[mylen] = 0; if (!sun->sun_path[0]) { + D( fprintf(stderr, "noip(%d): checking impbind list...\n", pid); ) + for (i = impbinds; i; i = i->next) { + D( dump_impbind(i); ) + if ((*sa)->sa_family == i->af && + sockaddr_in_range_p(*sa, &i->minaddr, &i->maxaddr)) { + D( fprintf(stderr, "noip(%d): match!\n", pid); ) + addr.sa.sa_family = (*sa)->sa_family; + ipaddr_to_sockaddr(&addr.sa, &i->bindaddr); + goto found; + } + } + D( fprintf(stderr, "noip(%d): no match; using wildcard\n", pid); ) wildcard_address((*sa)->sa_family, &addr.sa); + found: encode_inet_addr(sun, &addr.sa, WANT_FRESH); if (real_bind(sk, SA(sun), SUN_LEN(sun))) return (-1); } @@ -814,28 +948,6 @@ static int do_implicit_bind(int sk, const struct sockaddr **sa, 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) -{ - address addr; - socklen_t alen; - - 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); - *fakelen = alen; -} - /*----- Configuration -----------------------------------------------------*/ /* Return the process owner's home directory. */ @@ -959,103 +1071,242 @@ static void parse_ports(char **pp, unsigned short *min, unsigned short *max) *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_, \ - af_, minaddr_, maxaddr_, minport_, maxport_) do { \ - aclnode *a_; \ - NEW(a_); \ - 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.. +/* Parse an address range designator starting at PP and store a + * representation of it in R. An address range designator has the form: + * + * any | local | ADDR | ADDR - ADDR | ADDR/ADDR | ADDR/INT */ -static void parse_acl_line(char **pp, aclnode ***tail) +static int parse_addrrange(char **pp, addrrange *r) { - ipaddr minaddr, maxaddr; - unsigned short minport, maxport; - int i, af, n; - int act; + char *p = *pp, *q; + int n; int del; - char *p = *pp; - char *q; + int af; - for (;;) { + SKIPSPC; + if (KWMATCHP("any")) r->type = ANY; + else if (KWMATCHP("local")) r->type = LOCAL; + else { + parse_nextaddr(&p, &q, &del); + af = guess_address_family(q); + if (inet_pton(af, q, &r->u.range.min) <= 0) goto bad; + RESCAN(del); SKIPSPC; - if (*p == '+') act = ALLOW; - else if (*p == '-') act = DENY; - else goto bad; + if (*p == '-') { + p++; + parse_nextaddr(&p, &q, &del); + if (inet_pton(af, q, &r->u.range.max) <= 0) goto bad; + RESCAN(del); + } else if (*p == '/') { + p++; + NEXTNUMBER(q, del); + n = strtoul(q, 0, 0); + r->u.range.max = r->u.range.min; + mask_address(af, &r->u.range.min, n, 0); + mask_address(af, &r->u.range.max, n, 1); + RESCAN(del); + } else + r->u.range.max = r->u.range.min; + r->type = RANGE; + r->u.range.af = af; + } + *pp = p; + return (0); - p++; - SKIPSPC; - if (KWMATCHP("any")) { - parse_ports(&p, &minport, &maxport); +bad: + return (-1); +} + +/* Call FUNC on each individual address range in R. */ +static void foreach_addrrange(const addrrange *r, + void (*func)(int af, + const ipaddr *min, + const ipaddr *max, + void *p), + void *p) +{ + ipaddr minaddr, maxaddr; + int i, af; + + switch (r->type) { + case EMPTY: + break; + case ANY: 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); + func(af, &minaddr, &maxaddr, p); } - } else if (KWMATCHP("local")) { - parse_ports(&p, &minport, &maxport); + break; + case LOCAL: 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); + func(af, &minaddr, &minaddr, p); + func(af, &maxaddr, &maxaddr, p); } for (i = 0; i < n_local_ipaddrs; i++) { - ACLNODE(*tail, act, local_ipaddrs[i].af, - local_ipaddrs[i].addr, local_ipaddrs[i].addr, - minport, maxport); + func(local_ipaddrs[i].af, + &local_ipaddrs[i].addr, &local_ipaddrs[i].addr, + p); } + break; + case RANGE: + func(r->u.range.af, &r->u.range.min, &r->u.range.max, p); + break; + default: + abort(); + } +} + +struct add_aclnode_ctx { + int act; + unsigned short minport, maxport; + aclnode ***tail; +}; + +static void add_aclnode(int af, const ipaddr *min, const ipaddr *max, + void *p) +{ + struct add_aclnode_ctx *ctx = p; + aclnode *a; + + NEW(a); + a->act = ctx->act; + a->af = af; + a->minaddr = *min; a->maxaddr = *max; + a->minport = ctx->minport; a->maxport = ctx->maxport; + **ctx->tail = a; *ctx->tail = &a->next; +} + +/* 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 +|- ADDR-RANGE 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 add_aclnode_ctx ctx; + addrrange r; + char *p = *pp; + + ctx.tail = tail; + for (;;) { + SKIPSPC; + if (*p == '+') ctx.act = ALLOW; + else if (*p == '-') ctx.act = DENY; + else goto bad; + + p++; + if (parse_addrrange(&p, &r)) goto bad; + parse_ports(&p, &ctx.minport, &ctx.maxport); + foreach_addrrange(&r, add_aclnode, &ctx); + SKIPSPC; + if (*p != ',') break; + if (*p) p++; + } + if (*p) goto bad; + *pp = p; + return; + +bad: + D( fprintf(stderr, "noip(%d): bad acl spec (ignored)\n", getpid()); ) + 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; + + if ((p = getenv(var)) != 0) { + p = q = xstrdup(p); + parse_acl_line(&q, tail); + free(p); + } +} + +struct add_impbind_ctx { + int af, how; + ipaddr addr; +}; + +static void add_impbind(int af, const ipaddr *min, const ipaddr *max, + void *p) +{ + struct add_impbind_ctx *ctx = p; + impbind *i; + + if (ctx->af && af != ctx->af) return; + NEW(i); + i->af = af; + i->how = ctx->how; + i->minaddr = *min; i->maxaddr = *max; + switch (ctx->how) { + case EXPLICIT: i->bindaddr = ctx->addr; + case SAME: break; + default: abort(); + } + *impbind_tail = i; impbind_tail = &i->next; +} + +/* Parse an implicit-bind line. An implicit-bind entry has the form + * ADDR-RANGE {ADDR | same} + */ +static void parse_impbind_line(char **pp) +{ + struct add_impbind_ctx ctx; + char *p = *pp, *q; + addrrange r; + int del; + + for (;;) { + if (parse_addrrange(&p, &r)) goto bad; + SKIPSPC; + if (KWMATCHP("same")) { + ctx.how = SAME; + ctx.af = 0; } else { + ctx.how = EXPLICIT; parse_nextaddr(&p, &q, &del); - af = guess_address_family(q); - if (inet_pton(af, q, &minaddr) <= 0) goto bad; + ctx.af = guess_address_family(q); + if (inet_pton(ctx.af, q, &ctx.addr) < 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); - } 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, af, minaddr, maxaddr, minport, maxport); } + foreach_addrrange(&r, add_impbind, &ctx); SKIPSPC; if (*p != ',') break; if (*p) p++; } + if (*p) goto bad; *pp = p; return; bad: - D( fprintf(stderr, "noip: bad acl spec (ignored)\n"); ) + D( fprintf(stderr, "noip(%d): bad implicit-bind spec (ignored)\n", + getpid()); ) return; } +/* Parse implicit-bind instructions from an environment variable VAR, + * attaching it to the list. + */ +static void parse_impbind_env(const char *var) +{ + char *p, *q; + + if ((p = getenv(var)) != 0) { + p = q = xstrdup(p); + parse_impbind_line(&q); + free(p); + } +} + /* Parse the autoports configuration directive. Syntax is MIN - MAX. */ static void parse_autoports(char **pp) { @@ -1066,30 +1317,19 @@ static void parse_autoports(char **pp) SKIPSPC; NEXTNUMBER(q, del); x = strtoul(q, 0, 0); RESCAN(del); SKIPSPC; - if (*p != '-') goto bad; p++; + if (*p != '-') goto bad; + p++; NEXTNUMBER(q, del); y = strtoul(q, 0, 0); RESCAN(del); minautoport = x; maxautoport = y; + SKIPSPC; if (*p) goto bad; *pp = p; return; bad: - D( fprintf(stderr, "bad port range (ignored)\n"); ) + D( fprintf(stderr, "noip(%d): bad port range (ignored)\n", getpid()); ) 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; - - if ((p = getenv(var)) != 0) { - p = q = xstrdup(p); - parse_acl_line(&q, tail); - free(p); - } -} - /* Read the configuration from the config file and environment. */ static void readconfig(void) { @@ -1097,9 +1337,11 @@ static void readconfig(void) char buf[1024]; size_t n; char *p, *q, *cmd; + Dpid; parse_acl_env("NOIP_REALBIND_BEFORE", &bind_tail); parse_acl_env("NOIP_REALCONNECT_BEFORE", &connect_tail); + parse_impbind_env("NOIP_IMPBIND_BEFORE"); if ((p = getenv("NOIP_AUTOPORTS")) != 0) { p = q = xstrdup(p); parse_autoports(&q); @@ -1107,11 +1349,11 @@ static void readconfig(void) } if ((p = getenv("NOIP_CONFIG")) == 0) snprintf(p = buf, sizeof(buf), "%s/.noip", home()); - D( fprintf(stderr, "noip: config file: %s\n", p); ) + D( fprintf(stderr, "noip(%d): config file: %s\n", pid, p); ) if ((fp = fopen(p, "r")) == 0) { - D( fprintf(stderr, "noip: couldn't read config: %s\n", - strerror(errno)); ) + D( fprintf(stderr, "noip(%d): couldn't read config: %s\n", + pid, strerror(errno)); ) goto done; } while (fgets(buf, sizeof(buf), fp)) { @@ -1131,70 +1373,129 @@ static void readconfig(void) parse_acl_line(&p, &bind_tail); else if (strcmp(cmd, "realconnect") == 0) parse_acl_line(&p, &connect_tail); + else if (strcmp(cmd, "impbind") == 0) + parse_impbind_line(&p); else if (strcmp(cmd, "autoports") == 0) parse_autoports(&p); else if (strcmp(cmd, "debug") == 0) debug = *p ? atoi(p) : 1; else - D( fprintf(stderr, "noip: bad config command %s\n", cmd); ) + D( fprintf(stderr, "noip(%d): bad config command %s\n", pid, cmd); ) } fclose(fp); done: parse_acl_env("NOIP_REALBIND", &bind_tail); parse_acl_env("NOIP_REALCONNECT", &connect_tail); + parse_impbind_env("NOIP_IMPBIND"); parse_acl_env("NOIP_REALBIND_AFTER", &bind_tail); parse_acl_env("NOIP_REALCONNECT_AFTER", &connect_tail); + parse_impbind_env("NOIP_IMPBIND_AFTER"); *bind_tail = 0; *connect_tail = 0; + *impbind_tail = 0; if (!sockdir) sockdir = getenv("NOIP_SOCKETDIR"); if (!sockdir) { snprintf(buf, sizeof(buf), "%s/noip-%s", tmpdir(), user()); sockdir = xstrdup(buf); } - D( fprintf(stderr, "noip: socketdir: %s\n", sockdir); - fprintf(stderr, "noip: autoports: %u-%u\n", - minautoport, maxautoport); - fprintf(stderr, "noip: realbind acl:\n"); + D( fprintf(stderr, "noip(%d): socketdir: %s\n", pid, sockdir); + fprintf(stderr, "noip(%d): autoports: %u-%u\n", + pid, minautoport, maxautoport); + fprintf(stderr, "noip(%d): realbind acl:\n", pid); dump_acl(bind_real); - fprintf(stderr, "noip: realconnect acl:\n"); - dump_acl(connect_real); ) + fprintf(stderr, "noip(%d): realconnect acl:\n", pid); + dump_acl(connect_real); + fprintf(stderr, "noip(%d): impbind list:\n", pid); + dump_impbind_list(); ) } /*----- Overridden system calls -------------------------------------------*/ +static void dump_syserr(long rc) + { fprintf(stderr, " => %ld (E%d)\n", rc, errno); } + +static void dump_sysresult(long rc) +{ + if (rc < 0) dump_syserr(rc); + else fprintf(stderr, " => %ld\n", rc); +} + +static void dump_addrresult(long rc, const struct sockaddr *sa, + socklen_t len) +{ + char addrbuf[ADDRBUFSZ]; + + if (rc < 0) dump_syserr(rc); + else { + fprintf(stderr, " => %ld [%s]\n", rc, + present_sockaddr(sa, len, addrbuf, sizeof(addrbuf))); + } +} + int socket(int pf, int ty, int proto) { + int sk; + + D( fprintf(stderr, "noip(%d): SOCKET pf=%d, type=%d, proto=%d", + getpid(), pf, ty, proto); ) + switch (pf) { default: if (!family_known_p(pf)) { + D( fprintf(stderr, " -> unknown; refuse\n"); ) errno = EAFNOSUPPORT; - return (-1); + sk = -1; } + D( fprintf(stderr, " -> inet; substitute"); ) pf = PF_UNIX; proto = 0; + break; case PF_UNIX: #ifdef PF_NETLINK case PF_NETLINK: #endif - return (real_socket(pf, ty, proto)); + D( fprintf(stderr, " -> safe; permit"); ) + break; } + sk = real_socket(pf, ty, proto); + D( dump_sysresult(sk); ) + return (sk); } int socketpair(int pf, int ty, int proto, int *sk) { - if (family_known_p(pf)) { + int rc; + + D( fprintf(stderr, "noip(%d): SOCKETPAIR pf=%d, type=%d, proto=%d", + getpid(), pf, ty, proto); ) + if (!family_known_p(pf)) + D( fprintf(stderr, " -> unknown; permit"); ) + else { + D( fprintf(stderr, " -> inet; substitute"); ) pf = PF_UNIX; proto = 0; } - return (real_socketpair(pf, ty, proto, sk)); + rc = real_socketpair(pf, ty, proto, sk); + D( if (rc < 0) dump_syserr(rc); + else fprintf(stderr, " => %d (%d, %d)\n", rc, sk[0], sk[1]); ) + return (rc); } int bind(int sk, const struct sockaddr *sa, socklen_t len) { struct sockaddr_un sun; + int rc; + Dpid; - if (family_known_p(sa->sa_family)) { + D({ char buf[ADDRBUFSZ]; + fprintf(stderr, "noip(%d): BIND sk=%d, sa[%d]=%s", pid, + sk, len, present_sockaddr(sa, len, buf, sizeof(buf))); }) + + if (!family_known_p(sa->sa_family)) + D( fprintf(stderr, " -> unknown af; pass through"); ) + else { + D( fprintf(stderr, " -> checking...\n"); ) PRESERVING_ERRNO({ if (acl_allows_p(bind_real, sa)) { if (fixup_real_ip_socket(sk, sa->sa_family, 0)) @@ -1205,21 +1506,32 @@ int bind(int sk, const struct sockaddr *sa, socklen_t len) len = SUN_LEN(&sun); } }); + D( fprintf(stderr, "noip(%d): BIND ...", pid); ) } - return (real_bind(sk, sa, len)); + rc = real_bind(sk, sa, len); + D( dump_sysresult(rc); ) + return (rc); } int connect(int sk, const struct sockaddr *sa, socklen_t len) { struct sockaddr_un sun; int rc; + Dpid; - if (!family_known_p(sa->sa_family)) + D({ char buf[ADDRBUFSZ]; + fprintf(stderr, "noip(%d): CONNECT sk=%d, sa[%d]=%s", pid, + sk, len, present_sockaddr(sa, len, buf, sizeof(buf))); }) + + if (!family_known_p(sa->sa_family)) { + D( fprintf(stderr, " -> unknown af; pass through"); ) rc = real_connect(sk, sa, len); - else { + } else { + D( fprintf(stderr, " -> checking...\n"); ) PRESERVING_ERRNO({ do_implicit_bind(sk, &sa, &len, &sun); }); + D( fprintf(stderr, "noip(%d): CONNECT ...", pid); ) rc = real_connect(sk, sa, len); if (rc < 0) { switch (errno) { @@ -1227,6 +1539,7 @@ int connect(int sk, const struct sockaddr *sa, socklen_t len) } } } + D( dump_sysresult(rc); ) return (rc); } @@ -1234,13 +1547,28 @@ ssize_t sendto(int sk, const void *buf, size_t len, int flags, const struct sockaddr *to, socklen_t tolen) { struct sockaddr_un sun; + ssize_t n; + Dpid; + + D({ char addrbuf[ADDRBUFSZ]; + fprintf(stderr, "noip(%d): SENDTO sk=%d, len=%lu, flags=%d, to[%d]=%s", + pid, sk, (unsigned long)len, flags, tolen, + present_sockaddr(to, tolen, addrbuf, sizeof(addrbuf))); }) - if (to && family_known_p(to->sa_family)) { + if (!to) + D( fprintf(stderr, " -> null address; leaving"); ) + else if (!family_known_p(to->sa_family)) + D( fprintf(stderr, " -> unknown af; pass through"); ) + else { + D( fprintf(stderr, " -> checking...\n"); ) PRESERVING_ERRNO({ do_implicit_bind(sk, &to, &tolen, &sun); }); + D( fprintf(stderr, "noip(%d): SENDTO ...", pid); ) } - return (real_sendto(sk, buf, len, flags, to, tolen)); + n = real_sendto(sk, buf, len, flags, to, tolen); + D( dump_sysresult(n); ) + return (n); } ssize_t recvfrom(int sk, void *buf, size_t len, int flags, @@ -1249,57 +1577,91 @@ ssize_t recvfrom(int sk, void *buf, size_t len, int flags, char sabuf[1024]; socklen_t mylen = sizeof(sabuf); ssize_t n; + Dpid; - if (!from) - return real_recvfrom(sk, buf, len, flags, 0, 0); - PRESERVING_ERRNO({ - n = real_recvfrom(sk, buf, len, flags, SA(sabuf), &mylen); - if (n < 0) - return (-1); - return_fake_name(SA(sabuf), mylen, from, fromlen); - }); + D( fprintf(stderr, "noip(%d): RECVFROM sk=%d, len=%lu, flags=%d", + pid, sk, (unsigned long)len, flags); ) + + if (!from) { + D( fprintf(stderr, " -> null addr; pass through"); ) + n = real_recvfrom(sk, buf, len, flags, 0, 0); + } else { + PRESERVING_ERRNO({ + n = real_recvfrom(sk, buf, len, flags, SA(sabuf), &mylen); + if (n >= 0) { + D( fprintf(stderr, " -> converting...\n"); ) + return_fake_name(SA(sabuf), mylen, from, fromlen); + D( fprintf(stderr, "noip(%d): ... RECVFROM", pid); ) + } + }); + } + D( dump_addrresult(n, from, fromlen ? *fromlen : 0); ) return (n); } ssize_t sendmsg(int sk, const struct msghdr *msg, int flags) { struct sockaddr_un sun; - const struct sockaddr *sa; + const struct sockaddr *sa = SA(msg->msg_name); struct msghdr mymsg; - - if (msg->msg_name && family_known_p(SA(msg->msg_name)->sa_family)) { + ssize_t n; + Dpid; + + D({ char addrbuf[ADDRBUFSZ]; + fprintf(stderr, "noip(%d): SENDMSG sk=%d, " + "msg_flags=%d, msg_name[%d]=%s, ...", + pid, sk, msg->msg_flags, msg->msg_namelen, + present_sockaddr(sa, msg->msg_namelen, + addrbuf, sizeof(addrbuf))); }) + + if (!sa) + D( fprintf(stderr, " -> null address; leaving"); ) + else if (!family_known_p(sa->sa_family)) + D( fprintf(stderr, " -> unknown af; pass through"); ) + else { + D( fprintf(stderr, " -> checking...\n"); ) PRESERVING_ERRNO({ - sa = SA(msg->msg_name); mymsg = *msg; do_implicit_bind(sk, &sa, &mymsg.msg_namelen, &sun); mymsg.msg_name = SA(sa); msg = &mymsg; }); + D( fprintf(stderr, "noip(%d): SENDMSG ...", pid); ) } - return (real_sendmsg(sk, msg, flags)); + n = real_sendmsg(sk, msg, flags); + D( dump_sysresult(n); ) + return (n); } ssize_t recvmsg(int sk, struct msghdr *msg, int flags) { char sabuf[1024]; - struct sockaddr *sa; - socklen_t len; + struct sockaddr *sa = SA(msg->msg_name); + socklen_t len = msg->msg_namelen; ssize_t n; + Dpid; - if (!msg->msg_name) + D( fprintf(stderr, "noip(%d): RECVMSG sk=%d msg_flags=%d, ...", + pid, sk, msg->msg_flags); ) + + if (!msg->msg_name) { + D( fprintf(stderr, " -> null addr; pass through"); ) return (real_recvmsg(sk, msg, flags)); - PRESERVING_ERRNO({ - sa = SA(msg->msg_name); - len = msg->msg_namelen; - msg->msg_name = sabuf; - msg->msg_namelen = sizeof(sabuf); - n = real_recvmsg(sk, msg, flags); - if (n < 0) - return (-1); - return_fake_name(SA(sabuf), msg->msg_namelen, sa, &len); - msg->msg_name = sa; - msg->msg_namelen = len; - }); + } else { + PRESERVING_ERRNO({ + msg->msg_name = sabuf; + msg->msg_namelen = sizeof(sabuf); + n = real_recvmsg(sk, msg, flags); + if (n >= 0) { + D( fprintf(stderr, " -> converting...\n"); ) + return_fake_name(SA(sabuf), msg->msg_namelen, sa, &len); + D( fprintf(stderr, "noip(%d): ... RECVMSG", pid); ) + } + msg->msg_name = sa; + msg->msg_namelen = len; + }); + } + D( dump_addrresult(n, sa, len); ) return (n); } @@ -1307,35 +1669,60 @@ int accept(int sk, struct sockaddr *sa, socklen_t *len) { char sabuf[1024]; socklen_t mylen = sizeof(sabuf); - int nsk = real_accept(sk, SA(sabuf), &mylen); + int nsk; + Dpid; - if (nsk < 0) - return (-1); - return_fake_name(SA(sabuf), mylen, sa, len); + D( fprintf(stderr, "noip(%d): ACCEPT sk=%d", pid, sk); ) + + nsk = real_accept(sk, SA(sabuf), &mylen); + if (nsk < 0) /* failed */; + else if (!sa) D( fprintf(stderr, " -> address not wanted"); ) + else { + D( fprintf(stderr, " -> converting...\n"); ) + return_fake_name(SA(sabuf), mylen, sa, len); + D( fprintf(stderr, "noip(%d): ... ACCEPT", pid); ) + } + D( dump_addrresult(nsk, sa, len ? *len : 0); ) return (nsk); } int getsockname(int sk, struct sockaddr *sa, socklen_t *len) { + int rc; + Dpid; + + D( fprintf(stderr, "noip(%d): GETSOCKNAME sk=%d", pid, sk); ) PRESERVING_ERRNO({ char sabuf[1024]; socklen_t mylen = sizeof(sabuf); - if (real_getsockname(sk, SA(sabuf), &mylen)) - return (-1); - return_fake_name(SA(sabuf), mylen, sa, len); + rc = real_getsockname(sk, SA(sabuf), &mylen); + if (rc >= 0) { + D( fprintf(stderr, " -> converting...\n"); ) + return_fake_name(SA(sabuf), mylen, sa, len); + D( fprintf(stderr, "noip(%d): ... GETSOCKNAME", pid); ) + } }); - return (0); + D( dump_addrresult(rc, sa, *len); ) + return (rc); } int getpeername(int sk, struct sockaddr *sa, socklen_t *len) { + int rc; + Dpid; + + D( fprintf(stderr, "noip(%d): GETPEERNAME sk=%d", pid, sk); ) PRESERVING_ERRNO({ char sabuf[1024]; socklen_t mylen = sizeof(sabuf); - if (real_getpeername(sk, SA(sabuf), &mylen)) - return (-1); - return_fake_name(SA(sabuf), mylen, sa, len); + rc = real_getpeername(sk, SA(sabuf), &mylen); + if (rc >= 0) { + D( fprintf(stderr, " -> converting...\n"); ) + return_fake_name(SA(sabuf), mylen, sa, len); + D( fprintf(stderr, "noip(%d): ... GETPEERNAME", pid); ) + } }); + D( dump_addrresult(rc, sa, *len); ) return (0); } @@ -1409,6 +1796,7 @@ static void cleanup_sockdir(void) address addr; struct sockaddr_un sun; struct stat st; + Dpid; if ((dir = opendir(sockdir)) == 0) return; sun.sun_family = AF_UNIX; @@ -1419,13 +1807,13 @@ static void cleanup_sockdir(void) 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", - sun.sun_path); ) + D( fprintf(stderr, "noip(%d): ignoring unknown socketdir entry `%s'\n", + pid, sun.sun_path); ) continue; } if (unix_socket_status(&sun, 0) == STALE) { - D( fprintf(stderr, "noip: clearing away stale socket %s\n", - d->d_name); ) + D( fprintf(stderr, "noip(%d): clearing away stale socket %s\n", + pid, d->d_name); ) unlink(sun.sun_path); } } @@ -1440,7 +1828,9 @@ static void get_local_ipaddrs(void) struct ifaddrs *ifa_head, *ifa; ipaddr a; int i; + Dpid; + D( fprintf(stderr, "noip(%d): fetching local addresses...\n", pid); ) if (getifaddrs(&ifa_head)) { perror("getifaddrs"); return; } for (n_local_ipaddrs = 0, ifa = ifa_head; n_local_ipaddrs < MAX_LOCAL_IPADDRS && ifa; @@ -1449,7 +1839,8 @@ static void get_local_ipaddrs(void) continue; ipaddr_from_sockaddr(&a, ifa->ifa_addr); D({ char buf[ADDRBUFSZ]; - fprintf(stderr, "noip: local addr %s = %s", ifa->ifa_name, + fprintf(stderr, "noip(%d): local addr %s = %s", pid, + ifa->ifa_name, inet_ntop(ifa->ifa_addr->sa_family, &a, buf, sizeof(buf))); }) for (i = 0; i < n_local_ipaddrs; i++) {