noip.c (fixup_real_ip_socket): Support for temporary fixups.
[preload-hacks] / noip.c
diff --git a/noip.c b/noip.c
index cf61a02..2a09c16 100644 (file)
--- a/noip.c
+++ b/noip.c
@@ -503,7 +503,7 @@ 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(aclnode *a)
+static void dump_aclnode(const aclnode *a)
 {
   char buf[ADDRBUFSZ];
   const char *p;
@@ -526,7 +526,7 @@ static void dump_aclnode(aclnode *a)
   fputc('\n', stderr);
 }
 
-static void dump_acl(aclnode *a)
+static void dump_acl(const aclnode *a)
 {
   int act = ALLOW;
 
@@ -541,7 +541,7 @@ static void dump_acl(aclnode *a)
 #endif
 
 /* Returns nonzero if the ACL A allows the socket address SA. */
-static int acl_allows_p(aclnode *a, const struct sockaddr *sa)
+static int acl_allows_p(const aclnode *a, const struct sockaddr *sa)
 {
   unsigned short port = port_from_sockaddr(sa);
   int act = ALLOW;
@@ -715,9 +715,11 @@ static int decode_inet_addr(struct sockaddr *sa, int af_hint,
 
 /* 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.
+ * 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)
+static int fixup_real_ip_socket(int sk, int af_hint, int *tmp)
 {
   int nsk;
   int type;
@@ -761,18 +763,22 @@ static int fixup_real_ip_socket(int sk, int af_hint)
 } 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) {
+  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);
-    return (-1);
-  }
-  unlink(sun.sun_path);
-  close(nsk);
-  if (fcntl(sk, F_SETFD, fd) < 0) {
-    perror("noip: fixup_real_ip_socket F_SETFD");
-    abort();
+    if (fcntl(sk, F_SETFD, fd) < 0) {
+      perror("noip: fixup_real_ip_socket F_SETFD");
+      abort();
+    }
   }
   return (0);
 }
@@ -788,7 +794,7 @@ static int do_implicit_bind(int sk, const struct sockaddr **sa,
   socklen_t mylen = sizeof(*sun);
 
   if (acl_allows_p(connect_real, *sa)) {
-    if (fixup_real_ip_socket(sk, (*sa)->sa_family)) return (-1);
+    if (fixup_real_ip_socket(sk, (*sa)->sa_family, 0)) return (-1);
   } else {
     if (real_getsockname(sk, SA(sun), &mylen) < 0) return (-1);
     if (sun->sun_family == AF_UNIX) {
@@ -1038,8 +1044,9 @@ static void parse_acl_line(char **pp, aclnode ***tail)
     }
     SKIPSPC;
     if (*p != ',') break;
-    p++;
+    if (*p) p++;
   }
+  *pp = p;
   return;
 
 bad:
@@ -1060,6 +1067,7 @@ static void parse_autoports(char **pp)
   if (*p != '-') goto bad; p++;
   NEXTNUMBER(q, del); y = strtoul(q, 0, 0); RESCAN(del);
   minautoport = x; maxautoport = y;
+  *pp = p;
   return;
 
 bad:
@@ -1173,7 +1181,7 @@ int socket(int pf, int ty, int 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;
   }
@@ -1187,7 +1195,7 @@ int bind(int sk, const struct sockaddr *sa, socklen_t len)
   if (family_known_p(sa->sa_family)) {
     PRESERVING_ERRNO({
       if (acl_allows_p(bind_real, sa)) {
-       if (fixup_real_ip_socket(sk, sa->sa_family))
+       if (fixup_real_ip_socket(sk, sa->sa_family, 0))
          return (-1);
       } else {
        encode_inet_addr(&sun, sa, WANT_FRESH);
@@ -1225,7 +1233,7 @@ 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);
     });
@@ -1257,7 +1265,7 @@ ssize_t sendmsg(int sk, const struct msghdr *msg, 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;