noip.c: Hack ioctl(2) as well.
[preload-hacks] / noip.c
diff --git a/noip.c b/noip.c
index a1e141a..b9043f0 100644 (file)
--- a/noip.c
+++ b/noip.c
@@ -34,6 +34,7 @@
 #include <assert.h>
 #include <ctype.h>
 #include <errno.h>
+#include <stdarg.h>
 #include <stddef.h>
 #include <stdio.h>
 #include <stdlib.h>
@@ -125,7 +126,8 @@ static aclnode *connect_real,  **connect_tail = &connect_real;
   _(recvfrom, ssize_t, (int, void *buf, size_t, int,                   \
                        struct sockaddr *from, socklen_t *fromlen))     \
   _(sendmsg, ssize_t, (int, const struct msghdr *, int))               \
-  _(recvmsg, ssize_t, (int, struct msghdr *, int))
+  _(recvmsg, ssize_t, (int, struct msghdr *, int))                     \
+  _(ioctl, int, (int, unsigned long, ...))
 
 /* Function pointers to set up. */
 #define DECL(imp, ret, args) static ret (*real_##imp) args;
@@ -715,9 +717,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 +765,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 +796,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 +1197,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);
@@ -1361,6 +1369,36 @@ int setsockopt(int sk, int lev, int opt, const void *p, socklen_t len)
   return (real_setsockopt(sk, lev, opt, p, len));
 }
 
+int ioctl(int fd, unsigned long op, ...)
+{
+  va_list ap;
+  void *arg;
+  int sk;
+  int rc;
+
+  va_start(ap, op);
+  arg = va_arg(ap, void *);
+
+  switch (op) {
+    case SIOCGIFADDR:
+    case SIOCGIFBRDADDR:
+    case SIOCGIFDSTADDR:
+    case SIOCGIFNETMASK:
+      PRESERVING_ERRNO({
+       if (fixup_real_ip_socket(fd, AF_INET, &sk)) goto real;
+      });
+      rc = real_ioctl(sk, op, arg);
+      PRESERVING_ERRNO({ close(sk); });
+      break;
+    default:
+    real:
+      rc = real_ioctl(fd, op, arg);
+      break;
+  }
+  va_end(ap);
+  return (rc);
+}
+
 /*----- Initialization ----------------------------------------------------*/
 
 /* Clean up the socket directory, deleting stale sockets. */