naccyde / rpms / systemd

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