chiark / gitweb /
uopen.1: Reports wrong section in .TH line.
[preload-hacks] / noip.c
diff --git a/noip.c b/noip.c
index 735a58a7995f9033d86217b2c124508f37244036..50284e16f77aff59785c1daca0ff401f8c60ef9d 100644 (file)
--- a/noip.c
+++ b/noip.c
@@ -42,6 +42,7 @@ static int n_local_ipaddrs;
 static uid_t uid;
 static char *sockdir = 0;
 static int debug = 0;
+static unsigned minautoport = 16384, maxautoport = 65536;
 
 static aclnode *bind_real, **bind_tail = &bind_real;
 static aclnode *connect_real,  **connect_tail = &connect_real;
@@ -104,7 +105,7 @@ static void *xmalloc(size_t n)
 {
   void *p;
   if (!n) return (0);
-  if ((p = malloc(n)) == 0) { perror("malloc"); exit(1); }
+  if ((p = malloc(n)) == 0) { perror("malloc"); exit(127); }
   return (p);
 }
 
@@ -116,7 +117,7 @@ static char *xstrdup(const char *p)
   return (q);
 }
 
-static int unix_socket_status(struct sockaddr_un *sun, int quick_p)
+static int unix_socket_status(struct sockaddr_un *sun, int quickp)
 {
   struct stat st;
   FILE *fp = 0;
@@ -124,13 +125,11 @@ static int unix_socket_status(struct sockaddr_un *sun, int quick_p)
   int rc;
   char buf[256];
 
-  rc = UNUSED;
-  if (stat(sun->sun_path, &st) && errno == ENOENT)
-    goto done;
+  if (stat(sun->sun_path, &st))
+    return (errno == ENOENT ? UNUSED : USED);
+  if (!S_ISSOCK(st.st_mode) || quickp)
+    return (USED);
   rc = USED;
-  if (quick_p)
-    goto done;
-
   if ((fp = fopen("/proc/net/unix", "r")) == 0)
     goto done;
   fgets(buf, sizeof(buf), fp); /* skip header */
@@ -207,7 +206,7 @@ static int acl_allows_p(aclnode *a, const struct sockaddr_in *sin)
 static void dump_acl(aclnode *a)
 {
   int act = ALLOW;
-  
+
   for (; a; a = a->next) {
     dump_aclnode(a);
     act = a->act;
@@ -218,12 +217,25 @@ static void dump_acl(aclnode *a)
 
 #endif
 
+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! */
+  max -= min;
+  for (mask = 1; mask < max; mask = (mask << 1) | 1)
+    ;
+  do i = rand() & mask; while (i > max);
+  return (i + min);
+}
+
 static int encode_inet_addr(struct sockaddr_un *sun,
                            const struct sockaddr_in *sin,
                            int want)
 {
   int i;
-  int desperate_p = 0;
+  int desperatep = 0;
   char buf[INET_ADDRSTRLEN];
   int rc;
 
@@ -244,12 +256,18 @@ static int encode_inet_addr(struct sockaddr_un *sun,
       if (unix_socket_status(sun, 0) == STALE) unlink(sun->sun_path);
     }
   } else {
-    for (desperate_p = 0; desperate_p < 2; desperate_p++) {
-      for (i = 16384; i < 65536; i++) {
+    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));
+      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);
-       rc = unix_socket_status(sun, !desperate_p);
+       rc = unix_socket_status(sun, !desperatep);
        switch (rc) {
          case STALE: unlink(sun->sun_path);
          case UNUSED: goto found;
@@ -393,7 +411,7 @@ static int do_implicit_bind(int sk, const struct sockaddr **sa,
       if (mylen < sizeof(*sun)) ((char *)sun)[mylen] = 0;
       if (!sun->sun_path[0]) {
        sin.sin_family = AF_INET;
-       sin.sin_addr.s_addr = INADDR_LOOPBACK;
+       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)))
@@ -432,9 +450,13 @@ static char *home(void)
   char *p;
   struct passwd *pw;
 
-  if ((p = getenv("HOME")) != 0) return (p);
-  else if ((pw = getpwuid(uid)) != 0) return (pw->pw_dir);
-  else return "/notexist";
+  if (getuid() == uid &&
+      (p = getenv("HOME")) != 0)
+    return (p);
+  else if ((pw = getpwuid(uid)) != 0)
+    return (pw->pw_dir);
+  else
+    return "/notexist";
 }
 
 static char *tmpdir(void)
@@ -486,27 +508,22 @@ static char *user(void)
 #define KWMATCHP(kw) (strncmp(p, kw, sizeof(kw) - 1) == 0 &&           \
                      !isalnum(UC(p[sizeof(kw) - 1])) &&                \
                      (p += sizeof(kw) - 1))
-  
+
 static void parse_ports(char **pp, unsigned short *min, unsigned short *max)
 {
   char *p = *pp, *q;
   int del;
 
   SKIPSPC;
-  if (*p != ':') {
-    *min = 0;
-    *max = 0xffff;
-  } else {
+  if (*p != ':')
+    { *min = 0; *max = 0xffff; }
+  else {
     p++;
-    NEXTNUMBER(q, del);
-    *min = atoi(q);
-    RESCAN(del);
+    NEXTNUMBER(q, del); *min = strtoul(q, 0, 0); RESCAN(del);
     SKIPSPC;
-    if (*p == '-') {
-      NEXTNUMBER(q, del);
-      *max = atoi(q);
-      RESCAN(del);
-    } else
+    if (*p == '-')
+      { p++; NEXTNUMBER(q, del); *max = strtoul(q, 0, 0); RESCAN(del); }
+    else
       *max = *min;
   }
   *pp = p;
@@ -533,60 +550,65 @@ static void parse_acl_line(char **pp, aclnode ***tail)
   char *p = *pp;
   char *q;
 
-  SKIPSPC;
-  if (*p == '+') act = ALLOW;
-  else if (*p == '-') act = DENY;
-  else goto bad;
+  for (;;) {
+    SKIPSPC;
+    if (*p == '+') act = ALLOW;
+    else if (*p == '-') act = DENY;
+    else goto bad;
 
-  p++;
-  SKIPSPC;
-  if (KWMATCHP("any")) {
-    minaddr = 0;
-    maxaddr = 0xffffffff;
-    goto justone;
-  } 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; i < n_local_ipaddrs; i++) {
-      minaddr = ntohl(local_ipaddrs[i].s_addr);
-      ACLNODE(*tail, act, minaddr, minaddr, minport, maxport);
-    }
-  } else {
-    if (*p == ':') {
+    p++;
+    SKIPSPC;
+    if (KWMATCHP("any")) {
       minaddr = 0;
       maxaddr = 0xffffffff;
+      goto justone;
+    } 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; i < n_local_ipaddrs; i++) {
+       minaddr = ntohl(local_ipaddrs[i].s_addr);
+       ACLNODE(*tail, act, minaddr, minaddr, minport, maxport);
+      }
     } else {
-      NEXTADDR(q, del);
-      if (inet_pton(AF_INET, q, &addr) <= 0) goto bad;
-      minaddr = ntohl(addr.s_addr);
-      RESCAN(del);
-      SKIPSPC;
-      if (*p == '-') {
-       p++;
+      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);
        RESCAN(del);
-       maxaddr = ntohl(addr.s_addr);
-      } else if (*p == '/') {
-       p++;
-       NEXTADDR(q, del);
-       if (strchr(q, '.')) {
+       SKIPSPC;
+       if (*p == '-') {
+         p++;
+         NEXTADDR(q, del);
          if (inet_pton(AF_INET, q, &addr) <= 0) goto bad;
-         mask = ntohl(addr.s_addr);
-       } else {
-         n = atoi(q);
-         mask = (~0ul << (32 - n)) & 0xffffffff;
-       }
-       RESCAN(del);
-       minaddr &= mask;
-       maxaddr = minaddr | (mask ^ 0xffffffff);
-      } else
-       maxaddr = minaddr;
+         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:
+      parse_ports(&p, &minport, &maxport);
+      ACLNODE(*tail, act, minaddr, maxaddr, minport, maxport);
     }
-  justone:
-    parse_ports(&p, &minport, &maxport);
-    ACLNODE(*tail, act, minaddr, maxaddr, minport, maxport);
+    SKIPSPC;
+    if (*p != ',') break;
+    p++;
   }
   return;
 
@@ -595,19 +617,59 @@ bad:
   return;
 }
 
+static void parse_autoports(char **pp)
+{
+  char *p = *pp, *q;
+  unsigned x, y;
+  int del;
+
+  SKIPSPC;
+  NEXTNUMBER(q, del); x = strtoul(q, 0, 0); RESCAN(del);
+  SKIPSPC;
+  if (*p != '-') goto bad; p++;
+  NEXTNUMBER(q, del); y = strtoul(q, 0, 0); RESCAN(del);
+  minautoport = x; maxautoport = y;
+  return;
+
+bad:
+  D( fprintf(stderr, "bad port range (ignored)\n"); )
+  return;
+}
+
+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);
+  }
+}
+
 static void readconfig(void)
 {
   FILE *fp;
   char buf[1024];
   size_t n;
-  char *p, *cmd;
-
+  char *p, *q, *cmd;
+
+  parse_acl_env("NOIP_REALBIND_BEFORE", &bind_tail);
+  parse_acl_env("NOIP_REALCONNECT_BEFORE", &connect_tail);
+  if ((p = getenv("NOIP_AUTOPORTS")) != 0) {
+    p = q = xstrdup(p);
+    parse_autoports(&q);
+    free(p);
+  }
   if ((p = getenv("NOIP_CONFIG")) == 0)
     snprintf(p = buf, sizeof(buf), "%s/.noip", home());
   D( fprintf(stderr, "noip: config file: %s\n", p); )
-  if ((fp = fopen(p, "r")) == 0)
-    goto done;
 
+  if ((fp = fopen(p, "r")) == 0) {
+    D( fprintf(stderr, "noip: couldn't read config: %s\n",
+              strerror(errno)); )
+    goto done;
+  }
   while (fgets(buf, sizeof(buf), fp)) {
     n = strlen(buf);
     p = buf;
@@ -625,6 +687,8 @@ 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, "autoports") == 0)
+      parse_autoports(&p);
     else if (strcmp(cmd, "debug") == 0)
       debug = *p ? atoi(p) : 1;
     else
@@ -633,13 +697,20 @@ static void readconfig(void)
   fclose(fp);
 
 done:
+  parse_acl_env("NOIP_REALBIND", &bind_tail);
+  parse_acl_env("NOIP_REALCONNECT", &connect_tail);
+  parse_acl_env("NOIP_REALBIND_AFTER", &bind_tail);
+  parse_acl_env("NOIP_REALCONNECT_AFTER", &connect_tail);
   *bind_tail = 0;
   *connect_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: sockdir: %s\n", sockdir);
+  D( fprintf(stderr, "noip: socketdir: %s\n", sockdir);
+     fprintf(stderr, "noip: autoports: %u-%u\n",
+            minautoport, maxautoport);
      fprintf(stderr, "noip: realbind acl:\n");
      dump_acl(bind_real);
      fprintf(stderr, "noip: realconnect acl:\n");
@@ -670,7 +741,7 @@ int bind(int sk, const struct sockaddr *sa, socklen_t len)
 {
   struct sockaddr_un sun;
 
-  if (sa->sa_family == AF_INET) { 
+  if (sa->sa_family == AF_INET) {
     PRESERVING_ERRNO({
       if (acl_allows_p(bind_real, SIN(sa))) {
        if (fixup_real_ip_socket(sk))
@@ -688,13 +759,22 @@ int bind(int sk, const struct sockaddr *sa, socklen_t len)
 int connect(int sk, const struct sockaddr *sa, socklen_t len)
 {
   struct sockaddr_un sun;
+  int fixup_p = 0;
+  int rc;
 
   if (sa->sa_family == AF_INET) {
     PRESERVING_ERRNO({
       do_implicit_bind(sk, &sa, &len, &sun);
+      fixup_p = 1;
     });
   }
-  return real_connect(sk, sa, len);
+  rc = real_connect(sk, sa, len);
+  if (rc < 0) {
+    switch (errno) {
+      case ENOENT:     errno = ECONNREFUSED;   break;
+    }
+  }
+  return rc;
 }
 
 ssize_t sendto(int sk, const void *buf, size_t len, int flags,
@@ -842,14 +922,24 @@ static void cleanup_sockdir(void)
 {
   DIR *dir;
   struct dirent *d;
+  struct sockaddr_in sin;
   struct sockaddr_un sun;
+  struct stat st;
 
   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)) ||
+       stat(sun.sun_path, &st) ||
+       !S_ISSOCK(st.st_mode)) {
+      D( fprintf(stderr, "noip: ignoring unknown socketdir entry `%s'\n",
+                sun.sun_path); )
+      continue;
+    }
     if (unix_socket_status(&sun, 0) == STALE) {
       D( fprintf(stderr, "noip: clearing away stale socket %s\n",
                 d->d_name); )
@@ -884,18 +974,51 @@ static void get_local_ipaddrs(void)
   close(sk);
 }
 
+static void printerr(const char *p) { write(STDERR_FILENO, p, strlen(p)); }
+
+static void create_sockdir(void)
+{
+  struct stat st;
+
+  if (stat(sockdir, &st)) {
+    if (errno == ENOENT) {
+      if (mkdir(sockdir, 0700)) {
+       perror("noip: creating socketdir");
+       exit(127);
+      }
+      if (!stat(sockdir, &st))
+       goto check;
+    }
+    perror("noip: checking socketdir");
+    exit(127);
+  }
+check:
+  if (!S_ISDIR(st.st_mode)) {
+    printerr("noip: bad socketdir: not a directory\n");
+    exit(127);
+  }
+  if (st.st_uid != uid) {
+    printerr("noip: bad socketdir: not owner\n");
+    exit(127);
+  }
+  if (st.st_mode & 077) {
+    printerr("noip: bad socketdir: not private\n");
+    exit(127);
+  }
+}
+
 static void setup(void)
 {
   PRESERVING_ERRNO({
     char *p;
 
     import();
-    uid = getuid();
+    uid = geteuid();
     if ((p = getenv("NOIP_DEBUG")) && atoi(p))
       debug = 1;
     get_local_ipaddrs();
     readconfig();
-    mkdir(sockdir, 0700);
+    create_sockdir();
     cleanup_sockdir();
   });
 }