noip.c (fixup_real_ip_socket): Support for temporary fixups.
authorMark Wooding <mdw@distorted.org.uk>
Mon, 2 May 2016 12:26:10 +0000 (13:26 +0100)
committerMark Wooding <mdw@distorted.org.uk>
Mon, 2 May 2016 23:12:08 +0000 (00:12 +0100)
Add a new argument to `fixup_real_ip_socket' where it can return a
temporary fixed-up copy of the input socket.  Sort out the call sites to
pass a null pointer (requesting the previous behaviour).

noip.c

diff --git a/noip.c b/noip.c
index a1e141a..2a09c16 100644 (file)
--- a/noip.c
+++ b/noip.c
@@ -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) {
@@ -1189,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);