803fb7
From afa96dafde9d50f2b53ccf8136ead9ed79544877 Mon Sep 17 00:00:00 2001
803fb7
From: Shawn Landden <shawn@churchofgit.com>
803fb7
Date: Tue, 10 Mar 2015 04:41:59 -0700
803fb7
Subject: [PATCH] add REMOTE_ADDR and REMOTE_PORT for Accept=yes
803fb7
803fb7
Cherry-picked from: 3b1c524154c876aecebc98787975cc2943100210
803fb7
Resolves: #1341154
803fb7
---
803fb7
 TODO                                     |  2 -
803fb7
 man/systemd.socket.xml                   |  7 ++-
de8967
 src/core/service.c                       | 42 ++++++++++++-
803fb7
 src/libsystemd/sd-resolve/test-resolve.c |  2 +-
de8967
 src/shared/socket-util.c                 | 76 +++++++++++++++++-------
803fb7
 src/shared/socket-util.h                 |  4 +-
803fb7
 src/timesync/timesyncd-server.h          |  2 +-
803fb7
 7 files changed, 107 insertions(+), 28 deletions(-)
803fb7
803fb7
diff --git a/TODO b/TODO
803fb7
index d96d2bf0e..498d82c21 100644
803fb7
--- a/TODO
803fb7
+++ b/TODO
803fb7
@@ -185,8 +185,6 @@ Features:
803fb7
 * as soon as we have kdbus, and sender timestamps, revisit coalescing multiple parallel daemon reloads:
803fb7
   http://lists.freedesktop.org/archives/systemd-devel/2014-December/025862.html
803fb7
 
803fb7
-* set $REMOTE_IP (or $REMOTE_ADDR/$REMOTE_PORT) environment variable when doing per-connection socket activation. use format introduced by xinetd or CGI for this
803fb7
-
803fb7
 * the install state probably shouldn't get confused by generated units, think dbus1/kdbus compat!
803fb7
 
803fb7
 * in systemctl list-unit-files: show the install value the presets would suggest for a service in a third column
803fb7
diff --git a/man/systemd.socket.xml b/man/systemd.socket.xml
803fb7
index 2f541937f..350a95648 100644
803fb7
--- a/man/systemd.socket.xml
803fb7
+++ b/man/systemd.socket.xml
803fb7
@@ -357,7 +357,12 @@
803fb7
         daemons designed for usage with
803fb7
         <citerefentry><refentrytitle>inetd</refentrytitle><manvolnum>8</manvolnum></citerefentry>
803fb7
         to work unmodified with systemd socket
803fb7
-        activation.</para></listitem>
803fb7
+        activation.</para>
803fb7
+
803fb7
+        <para>For IPv4 and IPv6 connections the <varname>REMOTE_ADDR</varname>
803fb7
+        environment variable will contain the remote IP, and <varname>REMOTE_PORT</varname>
803fb7
+        will contain the remote port. This is the same as the format used by CGI.
803fb7
+        For SOCK_RAW the port is the IP protocol.</para></listitem>
803fb7
       </varlistentry>
803fb7
 
803fb7
       <varlistentry>
803fb7
diff --git a/src/core/service.c b/src/core/service.c
803fb7
index ae5e61000..c76713b1c 100644
803fb7
--- a/src/core/service.c
803fb7
+++ b/src/core/service.c
803fb7
@@ -1094,7 +1094,7 @@ static int service_spawn(
803fb7
         if (r < 0)
803fb7
                 goto fail;
803fb7
 
803fb7
-        our_env = new0(char*, 4);
803fb7
+        our_env = new0(char*, 6);
803fb7
         if (!our_env) {
803fb7
                 r = -ENOMEM;
803fb7
                 goto fail;
803fb7
@@ -1118,6 +1118,46 @@ static int service_spawn(
803fb7
                         goto fail;
803fb7
                 }
803fb7
 
803fb7
+        if (UNIT_DEREF(s->accept_socket)) {
803fb7
+                union sockaddr_union sa;
803fb7
+                socklen_t salen = sizeof(sa);
803fb7
+
803fb7
+                r = getpeername(s->socket_fd, &sa.sa, &salen);
803fb7
+                if (r < 0) {
803fb7
+                        r = -errno;
803fb7
+                        goto fail;
803fb7
+                }
803fb7
+
803fb7
+                if (IN_SET(sa.sa.sa_family, AF_INET, AF_INET6)) {
803fb7
+                        _cleanup_free_ char *addr = NULL;
803fb7
+                        char *t;
803fb7
+                        int port;
803fb7
+
803fb7
+                        r = sockaddr_pretty(&sa.sa, salen, true, false, &addr);
803fb7
+                        if (r < 0)
803fb7
+                                goto fail;
803fb7
+
803fb7
+                        t = strappend("REMOTE_ADDR=", addr);
803fb7
+                        if (!t) {
803fb7
+                                r = -ENOMEM;
803fb7
+                                goto fail;
803fb7
+                        }
803fb7
+                        our_env[n_env++] = t;
803fb7
+
803fb7
+                        port = sockaddr_port(&sa.sa);
803fb7
+                        if (port < 0) {
803fb7
+                                r = port;
803fb7
+                                goto fail;
803fb7
+                        }
803fb7
+
803fb7
+                        if (asprintf(&t, "REMOTE_PORT=%u", port) < 0) {
803fb7
+                                r = -ENOMEM;
803fb7
+                                goto fail;
803fb7
+                        }
803fb7
+                        our_env[n_env++] = t;
803fb7
+                }
803fb7
+        }
803fb7
+
803fb7
         final_env = strv_env_merge(2, UNIT(s)->manager->environment, our_env, NULL);
803fb7
         if (!final_env) {
803fb7
                 r = -ENOMEM;
803fb7
diff --git a/src/libsystemd/sd-resolve/test-resolve.c b/src/libsystemd/sd-resolve/test-resolve.c
803fb7
index d08e1b5a0..a14b6de19 100644
803fb7
--- a/src/libsystemd/sd-resolve/test-resolve.c
803fb7
+++ b/src/libsystemd/sd-resolve/test-resolve.c
803fb7
@@ -49,7 +49,7 @@ static int getaddrinfo_handler(sd_resolve_query *q, int ret, const struct addrin
803fb7
         for (i = ai; i; i = i->ai_next) {
803fb7
                 _cleanup_free_ char *addr = NULL;
803fb7
 
803fb7
-                assert_se(sockaddr_pretty(i->ai_addr, i->ai_addrlen, false, &addr) == 0);
803fb7
+                assert_se(sockaddr_pretty(i->ai_addr, i->ai_addrlen, false, true, &addr) == 0);
803fb7
                 puts(addr);
803fb7
         }
803fb7
 
803fb7
diff --git a/src/shared/socket-util.c b/src/shared/socket-util.c
803fb7
index 407d0afee..a21251014 100644
803fb7
--- a/src/shared/socket-util.c
803fb7
+++ b/src/shared/socket-util.c
803fb7
@@ -302,7 +302,7 @@ int socket_address_print(const SocketAddress *a, char **ret) {
803fb7
                 return 0;
803fb7
         }
803fb7
 
803fb7
-        return sockaddr_pretty(&a->sockaddr.sa, a->size, false, ret);
803fb7
+        return sockaddr_pretty(&a->sockaddr.sa, a->size, false, true, ret);
803fb7
 }
803fb7
 
803fb7
 bool socket_address_can_accept(const SocketAddress *a) {
803fb7
@@ -471,7 +471,20 @@ bool socket_address_matches_fd(const SocketAddress *a, int fd) {
803fb7
         return socket_address_equal(a, &b);
803fb7
 }
803fb7
 
803fb7
-int sockaddr_pretty(const struct sockaddr *_sa, socklen_t salen, bool translate_ipv6, char **ret) {
803fb7
+int sockaddr_port(const struct sockaddr *_sa) {
803fb7
+        union sockaddr_union *sa = (union sockaddr_union*) _sa;
803fb7
+
803fb7
+        assert(sa);
803fb7
+
803fb7
+        if (!IN_SET(sa->sa.sa_family, AF_INET, AF_INET6))
803fb7
+                return -EAFNOSUPPORT;
803fb7
+
803fb7
+        return ntohs(sa->sa.sa_family == AF_INET6 ?
803fb7
+                       sa->in6.sin6_port :
803fb7
+                       sa->in.sin_port);
803fb7
+}
803fb7
+
803fb7
+int sockaddr_pretty(const struct sockaddr *_sa, socklen_t salen, bool translate_ipv6, bool include_port, char **ret) {
803fb7
         union sockaddr_union *sa = (union sockaddr_union*) _sa;
803fb7
         char *p;
803fb7
 
803fb7
@@ -485,11 +498,18 @@ int sockaddr_pretty(const struct sockaddr *_sa, socklen_t salen, bool translate_
803fb7
 
803fb7
                 a = ntohl(sa->in.sin_addr.s_addr);
803fb7
 
803fb7
-                if (asprintf(&p,
803fb7
-                             "%u.%u.%u.%u:%u",
803fb7
-                             a >> 24, (a >> 16) & 0xFF, (a >> 8) & 0xFF, a & 0xFF,
803fb7
-                             ntohs(sa->in.sin_port)) < 0)
803fb7
-                        return -ENOMEM;
803fb7
+                if (include_port) {
803fb7
+                        if (asprintf(&p,
803fb7
+                                     "%u.%u.%u.%u:%u",
803fb7
+                                     a >> 24, (a >> 16) & 0xFF, (a >> 8) & 0xFF, a & 0xFF,
803fb7
+                                     ntohs(sa->in.sin_port)) < 0)
803fb7
+                                return -ENOMEM;
803fb7
+                } else {
803fb7
+                        if (asprintf(&p,
803fb7
+                                     "%u.%u.%u.%u",
803fb7
+                                     a >> 24, (a >> 16) & 0xFF, (a >> 8) & 0xFF, a & 0xFF) < 0)
803fb7
+                                return -ENOMEM;
803fb7
+                }
803fb7
 
803fb7
                 break;
803fb7
         }
803fb7
@@ -501,20 +521,34 @@ int sockaddr_pretty(const struct sockaddr *_sa, socklen_t salen, bool translate_
803fb7
 
803fb7
                 if (translate_ipv6 && memcmp(&sa->in6.sin6_addr, ipv4_prefix, sizeof(ipv4_prefix)) == 0) {
803fb7
                         const uint8_t *a = sa->in6.sin6_addr.s6_addr+12;
803fb7
-
803fb7
-                        if (asprintf(&p,
803fb7
-                                     "%u.%u.%u.%u:%u",
803fb7
-                                     a[0], a[1], a[2], a[3],
803fb7
-                                     ntohs(sa->in6.sin6_port)) < 0)
803fb7
-                                return -ENOMEM;
803fb7
+                        if (include_port) {
803fb7
+                                if (asprintf(&p,
803fb7
+                                             "%u.%u.%u.%u:%u",
803fb7
+                                             a[0], a[1], a[2], a[3],
803fb7
+                                             ntohs(sa->in6.sin6_port)) < 0)
803fb7
+                                        return -ENOMEM;
803fb7
+                        } else {
803fb7
+                                if (asprintf(&p,
803fb7
+                                             "%u.%u.%u.%u",
803fb7
+                                             a[0], a[1], a[2], a[3]) < 0)
803fb7
+                                        return -ENOMEM;
803fb7
+                        }
803fb7
                 } else {
803fb7
                         char a[INET6_ADDRSTRLEN];
803fb7
 
803fb7
-                        if (asprintf(&p,
803fb7
-                                     "[%s]:%u",
803fb7
-                                     inet_ntop(AF_INET6, &sa->in6.sin6_addr, a, sizeof(a)),
803fb7
-                                     ntohs(sa->in6.sin6_port)) < 0)
803fb7
-                                return -ENOMEM;
803fb7
+                        inet_ntop(AF_INET6, &sa->in6.sin6_addr, a, sizeof(a));
803fb7
+
803fb7
+                        if (include_port) {
803fb7
+                                if (asprintf(&p,
803fb7
+                                             "[%s]:%u",
803fb7
+                                             a,
803fb7
+                                             ntohs(sa->in6.sin6_port)) < 0)
803fb7
+                                        return -ENOMEM;
803fb7
+                        } else {
803fb7
+                                p = strdup(a);
803fb7
+                                if (!p)
803fb7
+                                        return -ENOMEM;
803fb7
+                        }
803fb7
                 }
803fb7
 
803fb7
                 break;
803fb7
@@ -589,7 +623,7 @@ int getpeername_pretty(int fd, char **ret) {
803fb7
         /* For remote sockets we translate IPv6 addresses back to IPv4
803fb7
          * if applicable, since that's nicer. */
803fb7
 
803fb7
-        return sockaddr_pretty(&sa.sa, salen, true, ret);
803fb7
+        return sockaddr_pretty(&sa.sa, salen, true, true, ret);
803fb7
 }
803fb7
 
803fb7
 int getsockname_pretty(int fd, char **ret) {
803fb7
@@ -607,7 +641,7 @@ int getsockname_pretty(int fd, char **ret) {
803fb7
          * listening sockets where the difference between IPv4 and
803fb7
          * IPv6 matters. */
803fb7
 
803fb7
-        return sockaddr_pretty(&sa.sa, salen, false, ret);
803fb7
+        return sockaddr_pretty(&sa.sa, salen, false, true, ret);
803fb7
 }
803fb7
 
803fb7
 int socknameinfo_pretty(union sockaddr_union *sa, socklen_t salen, char **_ret) {
803fb7
@@ -621,7 +655,7 @@ int socknameinfo_pretty(union sockaddr_union *sa, socklen_t salen, char **_ret)
803fb7
         if (r != 0) {
803fb7
                 int saved_errno = errno;
803fb7
 
803fb7
-                r = sockaddr_pretty(&sa->sa, salen, true, &ret;;
803fb7
+                r = sockaddr_pretty(&sa->sa, salen, true, true, &ret;;
803fb7
                 if (r < 0)
803fb7
                         return log_error_errno(r, "sockadd_pretty() failed: %m");
803fb7
 
803fb7
diff --git a/src/shared/socket-util.h b/src/shared/socket-util.h
803fb7
index 07d0aff72..6bfb677fb 100644
803fb7
--- a/src/shared/socket-util.h
803fb7
+++ b/src/shared/socket-util.h
803fb7
@@ -98,7 +98,9 @@ const char* socket_address_get_path(const SocketAddress *a);
803fb7
 
803fb7
 bool socket_ipv6_is_supported(void);
803fb7
 
803fb7
-int sockaddr_pretty(const struct sockaddr *_sa, socklen_t salen, bool translate_ipv6, char **ret);
803fb7
+int sockaddr_port(const struct sockaddr *_sa) _pure_;
803fb7
+
803fb7
+int sockaddr_pretty(const struct sockaddr *_sa, socklen_t salen, bool translate_ipv6, bool include_port, char **ret);
803fb7
 int getpeername_pretty(int fd, char **ret);
803fb7
 int getsockname_pretty(int fd, char **ret);
803fb7
 
803fb7
diff --git a/src/timesync/timesyncd-server.h b/src/timesync/timesyncd-server.h
803fb7
index 243b44a0e..18c44445e 100644
803fb7
--- a/src/timesync/timesyncd-server.h
803fb7
+++ b/src/timesync/timesyncd-server.h
803fb7
@@ -59,7 +59,7 @@ struct ServerName {
803fb7
 int server_address_new(ServerName *n, ServerAddress **ret, const union sockaddr_union *sockaddr, socklen_t socklen);
803fb7
 ServerAddress* server_address_free(ServerAddress *a);
803fb7
 static inline int server_address_pretty(ServerAddress *a, char **pretty) {
803fb7
-        return sockaddr_pretty(&a->sockaddr.sa, a->socklen, true, pretty);
803fb7
+        return sockaddr_pretty(&a->sockaddr.sa, a->socklen, true, true, pretty);
803fb7
 }
803fb7
 
803fb7
 int server_name_new(Manager *m, ServerName **ret, ServerType type,const char *string);