noip.c: Placate GCC warning about `misleading' layout.
[preload-hacks] / noip.c
diff --git a/noip.c b/noip.c
index de7cf89..b10f6bb 100644 (file)
--- 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 -----------------------------*/
 
@@ -300,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)
@@ -507,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(%d):   %c ", getpid(), 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)
@@ -811,6 +851,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, " <self>"); 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.
@@ -820,6 +912,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);
@@ -828,7 +922,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);
       }
@@ -840,28 +947,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. */
@@ -985,92 +1070,214 @@ 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);
-       RESCAN(del);
-      } 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++;
@@ -1080,10 +1287,25 @@ static void parse_acl_line(char **pp, aclnode ***tail)
   return;
 
 bad:
-  D( fprintf(stderr, "noip(%d): bad acl spec (ignored)\n", getpid()); )
+  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)
 {
@@ -1094,7 +1316,8 @@ 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;
@@ -1106,19 +1329,6 @@ 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;
-
-  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)
 {
@@ -1130,6 +1340,7 @@ static void readconfig(void)
 
   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);
@@ -1161,22 +1372,27 @@ 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());
@@ -1188,7 +1404,9 @@ done:
      fprintf(stderr, "noip(%d): realbind acl:\n", pid);
      dump_acl(bind_real);
      fprintf(stderr, "noip(%d): realconnect acl:\n", pid);
-     dump_acl(connect_real); )
+     dump_acl(connect_real);
+     fprintf(stderr, "noip(%d): impbind list:\n", pid);
+     dump_impbind_list(); )
 }
 
 /*----- Overridden system calls -------------------------------------------*/