chiark / gitweb /
uopen.1: Reports wrong section in .TH line.
[preload-hacks] / noip.c
diff --git a/noip.c b/noip.c
index e674bedbf32ac091e4dc0046938f9e5ee87ec702..50284e16f77aff59785c1daca0ff401f8c60ef9d 100644 (file)
--- a/noip.c
+++ b/noip.c
@@ -127,7 +127,7 @@ static int unix_socket_status(struct sockaddr_un *sun, int quickp)
 
   if (stat(sun->sun_path, &st))
     return (errno == ENOENT ? UNUSED : USED);
-  if (!S_ISSOCK(st.st_mode) || !quickp)
+  if (!S_ISSOCK(st.st_mode) || quickp)
     return (USED);
   rc = USED;
   if ((fp = fopen("/proc/net/unix", "r")) == 0)
@@ -206,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;
@@ -411,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)))
@@ -508,7 +508,7 @@ 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;
@@ -522,7 +522,7 @@ static void parse_ports(char **pp, unsigned short *min, unsigned short *max)
     NEXTNUMBER(q, del); *min = strtoul(q, 0, 0); RESCAN(del);
     SKIPSPC;
     if (*p == '-')
-      { NEXTNUMBER(q, del); *max = strtoul(q, 0, 0); RESCAN(del); }
+      { p++; NEXTNUMBER(q, del); *max = strtoul(q, 0, 0); RESCAN(del); }
     else
       *max = *min;
   }
@@ -654,10 +654,6 @@ static void readconfig(void)
   size_t n;
   char *p, *q, *cmd;
 
-  if ((p = getenv("NOIP_CONFIG")) == 0)
-    snprintf(p = buf, sizeof(buf), "%s/.noip", home());
-  D( fprintf(stderr, "noip: config file: %s\n", p); )
-
   parse_acl_env("NOIP_REALBIND_BEFORE", &bind_tail);
   parse_acl_env("NOIP_REALCONNECT_BEFORE", &connect_tail);
   if ((p = getenv("NOIP_AUTOPORTS")) != 0) {
@@ -665,8 +661,15 @@ static void readconfig(void)
     parse_autoports(&q);
     free(p);
   }
-  if ((fp = fopen(p, "r")) == 0)
+  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) {
+    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;
@@ -738,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))
@@ -756,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,
@@ -916,14 +928,18 @@ static void cleanup_sockdir(void)
 
   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))
+       !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); )