[systemd-commits] 7 commits - Makefile.am README TODO network/80-container-host0.network network/80-container-ve.network src/libsystemd src/machine src/shared src/timesync units/systemd-timesyncd.service.in

Lennart Poettering lennart at kemper.freedesktop.org
Sun May 18 04:55:27 PDT 2014


 Makefile.am                           |    4 
 README                                |    5 
 TODO                                  |    5 
 network/80-container-host0.network    |    7 +
 network/80-container-ve.network       |   13 ++
 src/libsystemd/sd-bus/bus-container.c |    8 -
 src/machine/machine-dbus.c            |  217 +++++++++++++++++++++++++++++++++-
 src/machine/machine.h                 |    4 
 src/machine/machinectl.c              |   75 ++++++++++-
 src/machine/machined-dbus.c           |   45 +++----
 src/shared/bus-errors.h               |    1 
 src/shared/capability.c               |    3 
 src/shared/logs-show.c                |    4 
 src/shared/util.c                     |  103 ++++++++++------
 src/shared/util.h                     |    4 
 src/timesync/timesyncd.c              |   91 +++++++++++++-
 src/timesync/timesyncd.h              |    3 
 units/systemd-timesyncd.service.in    |    5 
 18 files changed, 508 insertions(+), 89 deletions(-)

New commits:
commit 8891f695c71bd4c266d827c9aaedbbbbaf79d3eb
Author: Lennart Poettering <lennart at poettering.net>
Date:   Sun May 18 20:49:14 2014 +0900

    update TODO

diff --git a/TODO b/TODO
index c91404e..ac26ad5 100644
--- a/TODO
+++ b/TODO
@@ -30,6 +30,10 @@ External:
 
 Features:
 
+* machined: make sure we can restart machined without losing machines
+* machined/machinectl: sort IP addresses we return by scope and protocol
+* machined: write NSS module for looking up IP addresses for machines
+
 * timer units: actually add extra delays to timer units with high AccuracySec values, don't start them already when we are awake...
 
 * timesyncd:

commit 878cd7e95ca303f9851d227a22d2022bd49944b0
Author: Lennart Poettering <lennart at poettering.net>
Date:   Sun May 18 20:48:53 2014 +0900

    machined: add logic to query IP addresses of containers

diff --git a/src/libsystemd/sd-bus/bus-container.c b/src/libsystemd/sd-bus/bus-container.c
index 6bd7ad6..dd4bf15 100644
--- a/src/libsystemd/sd-bus/bus-container.c
+++ b/src/libsystemd/sd-bus/bus-container.c
@@ -42,7 +42,7 @@ int bus_container_connect_socket(sd_bus *b) {
         if (r < 0)
                 return r;
 
-        r = namespace_open(leader, &pidnsfd, &mntnsfd, &rootfd);
+        r = namespace_open(leader, &pidnsfd, &mntnsfd, NULL, &rootfd);
         if (r < 0)
                 return r;
 
@@ -61,7 +61,7 @@ int bus_container_connect_socket(sd_bus *b) {
         if (child == 0) {
                 pid_t grandchild;
 
-                r = namespace_enter(pidnsfd, mntnsfd, rootfd);
+                r = namespace_enter(pidnsfd, mntnsfd, -1, rootfd);
                 if (r < 0)
                         _exit(255);
 
@@ -140,7 +140,7 @@ int bus_container_connect_kernel(sd_bus *b) {
         if (r < 0)
                 return r;
 
-        r = namespace_open(leader, &pidnsfd, &mntnsfd, &rootfd);
+        r = namespace_open(leader, &pidnsfd, &mntnsfd, NULL, &rootfd);
         if (r < 0)
                 return r;
 
@@ -156,7 +156,7 @@ int bus_container_connect_kernel(sd_bus *b) {
 
                 pair[0] = safe_close(pair[0]);
 
-                r = namespace_enter(pidnsfd, mntnsfd, rootfd);
+                r = namespace_enter(pidnsfd, mntnsfd, -1, rootfd);
                 if (r < 0)
                         _exit(EXIT_FAILURE);
 
diff --git a/src/machine/machine-dbus.c b/src/machine/machine-dbus.c
index 83ef82d..c9c3de0 100644
--- a/src/machine/machine-dbus.c
+++ b/src/machine/machine-dbus.c
@@ -22,11 +22,15 @@
 #include <errno.h>
 #include <string.h>
 #include <sys/capability.h>
+#include <arpa/inet.h>
 
+#include "sd-rtnl.h"
 #include "bus-util.h"
 #include "bus-label.h"
 #include "strv.h"
 #include "machine.h"
+#include "rtnl-util.h"
+#include "bus-errors.h"
 
 static int property_get_id(
                 sd_bus *bus,
@@ -79,7 +83,7 @@ static int property_get_state(
 
 static BUS_DEFINE_PROPERTY_GET_ENUM(property_get_class, machine_class, MachineClass);
 
-static int method_terminate(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
+int bus_machine_method_terminate(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
         Machine *m = userdata;
         int r;
 
@@ -94,7 +98,7 @@ static int method_terminate(sd_bus *bus, sd_bus_message *message, void *userdata
         return sd_bus_reply_method_return(message, NULL);
 }
 
-static int method_kill(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
+int bus_machine_method_kill(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
         Machine *m = userdata;
         const char *swho;
         int32_t signo;
@@ -127,6 +131,208 @@ static int method_kill(sd_bus *bus, sd_bus_message *message, void *userdata, sd_
         return sd_bus_reply_method_return(message, NULL);
 }
 
+int bus_machine_method_get_addresses(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
+        _cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
+        _cleanup_close_pair_ int pair[2] = { -1, -1 };
+        _cleanup_free_ char *us = NULL, *them = NULL;
+        _cleanup_close_ int netns_fd = -1;
+        Machine *m = userdata;
+        const char *p;
+        siginfo_t si;
+        pid_t child;
+        int r;
+
+        assert(bus);
+        assert(message);
+        assert(m);
+
+        r = readlink_malloc("/proc/self/ns/net", &us);
+        if (r < 0)
+                return sd_bus_error_set_errno(error, r);
+
+        p = procfs_file_alloca(m->leader, "ns/net");
+        r = readlink_malloc(p, &them);
+        if (r < 0)
+                return sd_bus_error_set_errno(error, r);
+
+        if (streq(us, them))
+                return sd_bus_error_setf(error, BUS_ERROR_NO_PRIVATE_NETWORKING, "Machine %s does not use private networking", m->name);
+
+        r = namespace_open(m->leader, NULL, NULL, &netns_fd, NULL);
+        if (r < 0)
+                return sd_bus_error_set_errno(error, r);
+
+        if (socketpair(AF_UNIX, SOCK_SEQPACKET, 0, pair) < 0)
+                return sd_bus_error_set_errno(error, -errno);
+
+        child = fork();
+        if (child < 0)
+                return sd_bus_error_set_errno(error, -errno);
+
+        if (child == 0) {
+                _cleanup_rtnl_message_unref_ sd_rtnl_message *req = NULL, *resp = NULL;
+                _cleanup_rtnl_unref_ sd_rtnl *rtnl = NULL;
+                sd_rtnl_message *addr;
+
+                pair[0] = safe_close(pair[0]);
+
+                r = namespace_enter(-1, -1, netns_fd, -1);
+                if (r < 0)
+                        _exit(EXIT_FAILURE);
+
+                r = sd_rtnl_open(&rtnl, 0);
+                if (r < 0)
+                        _exit(EXIT_FAILURE);
+
+                r = sd_rtnl_message_new_addr(rtnl, &req, RTM_GETADDR, 0, AF_UNSPEC);
+                if (r < 0)
+                        _exit(EXIT_FAILURE);
+
+                r = sd_rtnl_message_request_dump(req, true);
+                if (r < 0)
+                        _exit(EXIT_FAILURE);
+
+                r = sd_rtnl_call(rtnl, req, 0, &resp);
+                if (r < 0)
+                        _exit(EXIT_FAILURE);
+
+                for (addr = resp; addr; addr = sd_rtnl_message_next(addr)) {
+                        uint16_t type;
+                        unsigned char family;
+                        union {
+                                struct in_addr in;
+                                struct in6_addr in6;
+                        } in_addr;
+                        struct iovec iov[2];
+
+                        r = sd_rtnl_message_get_type(addr, &type);
+                        if (r < 0)
+                                _exit(EXIT_FAILURE);
+
+                        if (type != RTM_NEWADDR)
+                                continue;
+
+                        r = sd_rtnl_message_addr_get_family(addr, &family);
+                        if (r < 0)
+                                _exit(EXIT_FAILURE);
+
+                        iov[0] = (struct iovec) { .iov_base = &family, .iov_len = sizeof(family) };
+
+                        switch (family) {
+
+                        case AF_INET:
+
+                                r = sd_rtnl_message_read_in_addr(addr, IFA_LOCAL, &in_addr.in);
+                                if (r < 0)
+                                        _exit(EXIT_FAILURE);
+
+                                if (in_addr.in.s_addr == htobe32(INADDR_LOOPBACK))
+                                        continue;
+
+                                iov[1] = (struct iovec) { .iov_base = &in_addr.in, .iov_len = sizeof(in_addr.in) };
+                                break;
+
+                        case AF_INET6:
+
+                                r = sd_rtnl_message_read_in6_addr(addr, IFA_ADDRESS, &in_addr.in6);
+                                if (r < 0)
+                                        _exit(EXIT_FAILURE);
+
+                                if (IN6_IS_ADDR_LOOPBACK(&in_addr.in6))
+                                        continue;
+
+                                iov[1] = (struct iovec) { .iov_base = &in_addr.in6, .iov_len = sizeof(in_addr.in6) };
+                                break;
+
+                        default:
+                                continue;
+                        }
+
+                        r = writev(pair[1], iov, 2);
+                        if (r < 0)
+                                _exit(EXIT_FAILURE);
+                }
+
+                _exit(EXIT_SUCCESS);
+        }
+
+        pair[1] = safe_close(pair[1]);
+
+        r = sd_bus_message_new_method_return(message, &reply);
+        if (r < 0)
+                return sd_bus_error_set_errno(error, r);
+
+        r = sd_bus_message_open_container(reply, 'a', "(yay)");
+        if (r < 0)
+                return sd_bus_error_set_errno(error, r);
+
+        for (;;) {
+                unsigned char family;
+                ssize_t n;
+                union {
+                        struct in_addr in;
+                        struct in6_addr in6;
+                } in_addr;
+                struct iovec iov[2];
+                struct msghdr mh = {
+                        .msg_iov = iov,
+                        .msg_iovlen = 2,
+                };
+
+                iov[0] = (struct iovec) { .iov_base = &family, .iov_len = sizeof(family) };
+                iov[1] = (struct iovec) { .iov_base = &in_addr, .iov_len = sizeof(in_addr) };
+
+                n = recvmsg(pair[0], &mh, 0);
+                if (n < 0)
+                        return sd_bus_error_set_errno(error, -errno);
+                if ((size_t) n < sizeof(family))
+                        break;
+
+                r = sd_bus_message_open_container(reply, 'r', "yay");
+                if (r < 0)
+                        return sd_bus_error_set_errno(error, r);
+
+                r = sd_bus_message_append(reply, "y", family);
+                if (r < 0)
+                        return sd_bus_error_set_errno(error, r);
+
+                switch (family) {
+
+                case AF_INET:
+                        if (n != sizeof(struct in_addr) + sizeof(family))
+                                return sd_bus_error_set_errno(error, EIO);
+
+                        r = sd_bus_message_append_array(reply, 'y', &in_addr.in, sizeof(in_addr.in));
+                        break;
+
+                case AF_INET6:
+                        if (n != sizeof(struct in6_addr) + sizeof(family))
+                                return sd_bus_error_set_errno(error, EIO);
+
+                        r = sd_bus_message_append_array(reply, 'y', &in_addr.in6, sizeof(in_addr.in6));
+                        break;
+                }
+                if (r < 0)
+                        return sd_bus_error_set_errno(error, r);
+
+                r = sd_bus_message_close_container(reply);
+                if (r < 0)
+                        return sd_bus_error_set_errno(error, r);
+        }
+
+        r = wait_for_terminate(child, &si);
+        if (r < 0)
+                return sd_bus_error_set_errno(error, r);
+        if (si.si_code != CLD_EXITED || si.si_status != EXIT_SUCCESS)
+                return sd_bus_error_set_errno(error, EIO);
+
+        r = sd_bus_message_close_container(reply);
+        if (r < 0)
+                return sd_bus_error_set_errno(error, r);
+
+        return sd_bus_send(bus, reply, NULL);
+}
+
 const sd_bus_vtable machine_vtable[] = {
         SD_BUS_VTABLE_START(0),
         SD_BUS_PROPERTY("Name", "s", NULL, offsetof(Machine, name), SD_BUS_VTABLE_PROPERTY_CONST),
@@ -139,9 +345,10 @@ const sd_bus_vtable machine_vtable[] = {
         SD_BUS_PROPERTY("Class", "s", property_get_class, offsetof(Machine, class), SD_BUS_VTABLE_PROPERTY_CONST),
         SD_BUS_PROPERTY("RootDirectory", "s", NULL, offsetof(Machine, root_directory), SD_BUS_VTABLE_PROPERTY_CONST),
         SD_BUS_PROPERTY("State", "s", property_get_state, 0, 0),
-        SD_BUS_METHOD("Terminate", NULL, NULL, method_terminate, SD_BUS_VTABLE_CAPABILITY(CAP_KILL)),
-        SD_BUS_METHOD("Kill", "si", NULL, method_kill, SD_BUS_VTABLE_CAPABILITY(CAP_KILL)),
-        SD_BUS_VTABLE_END
+        SD_BUS_METHOD("Terminate", NULL, NULL, bus_machine_method_terminate, SD_BUS_VTABLE_CAPABILITY(CAP_KILL)),
+        SD_BUS_METHOD("Kill", "si", NULL, bus_machine_method_kill, SD_BUS_VTABLE_CAPABILITY(CAP_KILL)),
+        SD_BUS_METHOD("GetAddresses", NULL, "a(yay)", bus_machine_method_get_addresses, SD_BUS_VTABLE_UNPRIVILEGED),
+         SD_BUS_VTABLE_END
 };
 
 int machine_object_find(sd_bus *bus, const char *path, const char *interface, void *userdata, void **found, sd_bus_error *error) {
diff --git a/src/machine/machine.h b/src/machine/machine.h
index f4aefc5..a894a46 100644
--- a/src/machine/machine.h
+++ b/src/machine/machine.h
@@ -96,6 +96,10 @@ char *machine_bus_path(Machine *s);
 int machine_object_find(sd_bus *bus, const char *path, const char *interface, void *userdata, void **found, sd_bus_error *error);
 int machine_node_enumerator(sd_bus *bus, const char *path, void *userdata, char ***nodes, sd_bus_error *error);
 
+int bus_machine_method_terminate(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error);
+int bus_machine_method_kill(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error);
+int bus_machine_method_get_addresses(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error);
+
 int machine_send_signal(Machine *m, bool new_machine);
 int machine_send_create_reply(Machine *m, sd_bus_error *error);
 
diff --git a/src/machine/machinectl.c b/src/machine/machinectl.c
index 6337aa2..c2bf7e5 100644
--- a/src/machine/machinectl.c
+++ b/src/machine/machinectl.c
@@ -27,6 +27,8 @@
 #include <pwd.h>
 #include <locale.h>
 #include <fcntl.h>
+#include <netinet/in.h>
+#include <arpa/inet.h>
 
 #include "sd-bus.h"
 #include "log.h"
@@ -165,6 +167,63 @@ static int show_unit_cgroup(sd_bus *bus, const char *unit, pid_t leader) {
         return 0;
 }
 
+static int print_addresses(sd_bus *bus, const char *name, const char *prefix, const char *prefix2) {
+        _cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
+        int r;
+
+        assert(bus);
+        assert(name);
+        assert(prefix);
+        assert(prefix2);
+
+        r = sd_bus_call_method(bus,
+                               "org.freedesktop.machine1",
+                               "/org/freedesktop/machine1",
+                               "org.freedesktop.machine1.Manager",
+                               "GetMachineAddresses",
+                               NULL,
+                               &reply,
+                               "s", name);
+        if (r < 0)
+                return r;
+
+        r = sd_bus_message_enter_container(reply, 'a', "(yay)");
+        if (r < 0)
+                return bus_log_parse_error(r);
+
+        while ((r = sd_bus_message_enter_container(reply, 'r', "yay")) > 0) {
+                unsigned char family;
+                const void *a;
+                size_t sz;
+                char buffer[MAX(INET6_ADDRSTRLEN, INET_ADDRSTRLEN)];
+
+                r = sd_bus_message_read(reply, "y", &family);
+                if (r < 0)
+                        return bus_log_parse_error(r);
+
+                r = sd_bus_message_read_array(reply, 'y', &a, &sz);
+                if (r < 0)
+                        return bus_log_parse_error(r);
+
+                printf("%s%s\n", prefix, inet_ntop(family, a, buffer, sizeof(buffer)));
+
+                r = sd_bus_message_exit_container(reply);
+                if (r < 0)
+                        return bus_log_parse_error(r);
+
+                if (prefix != prefix2)
+                        prefix = prefix2;
+        }
+        if (r < 0)
+                return bus_log_parse_error(r);
+
+        r = sd_bus_message_exit_container(reply);
+        if (r < 0)
+                return bus_log_parse_error(r);
+
+        return 0;
+}
+
 typedef struct MachineStatusInfo {
         char *name;
         sd_id128_t id;
@@ -221,6 +280,10 @@ static void print_machine_status_info(sd_bus *bus, MachineStatusInfo *i) {
         if (i->root_directory)
                 printf("\t    Root: %s\n", i->root_directory);
 
+        print_addresses(bus, i->name,
+                       "\t Address: ",
+                       "\t          ");
+
         if (i->unit) {
                 printf("\t    Unit: %s\n", i->unit);
                 show_unit_cgroup(bus, i->unit, i->leader);
@@ -427,7 +490,7 @@ static int openpt_in_namespace(pid_t pid, int flags) {
         pid_t child;
         siginfo_t si;
 
-        r = namespace_open(pid, &pidnsfd, &mntnsfd, &rootfd);
+        r = namespace_open(pid, &pidnsfd, &mntnsfd, NULL, &rootfd);
         if (r < 0)
                 return r;
 
@@ -441,7 +504,7 @@ static int openpt_in_namespace(pid_t pid, int flags) {
         if (child == 0) {
                 pair[0] = safe_close(pair[0]);
 
-                r = namespace_enter(pidnsfd, mntnsfd, rootfd);
+                r = namespace_enter(pidnsfd, mntnsfd, -1, rootfd);
                 if (r < 0)
                         _exit(EXIT_FAILURE);
 
@@ -466,10 +529,10 @@ static int openpt_in_namespace(pid_t pid, int flags) {
         pair[1] = safe_close(pair[1]);
 
         r = wait_for_terminate(child, &si);
-        if (r < 0 || si.si_code != CLD_EXITED || si.si_status != EXIT_SUCCESS) {
-
-                return r < 0 ? r : -EIO;
-        }
+        if (r < 0)
+                return r;
+        if (si.si_code != CLD_EXITED || si.si_status != EXIT_SUCCESS)
+                return -EIO;
 
         if (recvmsg(pair[0], &mh, MSG_NOSIGNAL|MSG_CMSG_CLOEXEC) < 0)
                 return -errno;
diff --git a/src/machine/machined-dbus.c b/src/machine/machined-dbus.c
index a32d7f5..0078a27 100644
--- a/src/machine/machined-dbus.c
+++ b/src/machine/machined-dbus.c
@@ -27,7 +27,6 @@
 
 #include "sd-id128.h"
 #include "sd-messages.h"
-
 #include "strv.h"
 #include "mkdir.h"
 #include "path-util.h"
@@ -343,50 +342,49 @@ static int method_terminate_machine(sd_bus *bus, sd_bus_message *message, void *
         if (!machine)
                 return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_MACHINE, "No machine '%s' known", name);
 
-        r = machine_stop(machine);
-        if (r < 0)
-                return sd_bus_error_set_errno(error, r);
-
-        return sd_bus_reply_method_return(message, NULL);
+        return bus_machine_method_terminate(bus, message, machine, error);
 }
 
 static int method_kill_machine(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
         Manager *m = userdata;
         Machine *machine;
         const char *name;
-        const char *swho;
-        int32_t signo;
-        KillWho who;
         int r;
 
         assert(bus);
         assert(message);
         assert(m);
 
-        r = sd_bus_message_read(message, "ssi", &name, &swho, &signo);
+        r = sd_bus_message_read(message, "s", &name);
         if (r < 0)
                 return sd_bus_error_set_errno(error, r);
 
-        if (isempty(swho))
-                who = KILL_ALL;
-        else {
-                who = kill_who_from_string(swho);
-                if (who < 0)
-                        return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid kill parameter '%s'", swho);
-        }
-
-        if (signo <= 0 || signo >= _NSIG)
-                return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid signal %i", signo);
-
         machine = hashmap_get(m->machines, name);
         if (!machine)
                 return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_MACHINE, "No machine '%s' known", name);
 
-        r = machine_kill(machine, who, signo);
+        return bus_machine_method_kill(bus, message, machine, error);
+}
+
+static int method_get_machine_addresses(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
+        Manager *m = userdata;
+        Machine *machine;
+        const char *name;
+        int r;
+
+        assert(bus);
+        assert(message);
+        assert(m);
+
+        r = sd_bus_message_read(message, "s", &name);
         if (r < 0)
                 return sd_bus_error_set_errno(error, r);
 
-        return sd_bus_reply_method_return(message, NULL);
+        machine = hashmap_get(m->machines, name);
+        if (!machine)
+                return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_MACHINE, "No machine '%s' known", name);
+
+        return bus_machine_method_get_addresses(bus, message, machine, error);
 }
 
 const sd_bus_vtable manager_vtable[] = {
@@ -398,6 +396,7 @@ const sd_bus_vtable manager_vtable[] = {
         SD_BUS_METHOD("RegisterMachine", "sayssus", "o", method_register_machine, 0),
         SD_BUS_METHOD("KillMachine", "ssi", NULL, method_kill_machine, SD_BUS_VTABLE_CAPABILITY(CAP_KILL)),
         SD_BUS_METHOD("TerminateMachine", "s", NULL, method_terminate_machine, SD_BUS_VTABLE_CAPABILITY(CAP_KILL)),
+        SD_BUS_METHOD("GetMachineAddresses", "s", "a(yay)", method_get_machine_addresses, SD_BUS_VTABLE_UNPRIVILEGED),
         SD_BUS_SIGNAL("MachineNew", "so", 0),
         SD_BUS_SIGNAL("MachineRemoved", "so", 0),
         SD_BUS_VTABLE_END
diff --git a/src/shared/bus-errors.h b/src/shared/bus-errors.h
index 5637935..45e1293 100644
--- a/src/shared/bus-errors.h
+++ b/src/shared/bus-errors.h
@@ -42,6 +42,7 @@
 #define BUS_ERROR_NO_SUCH_MACHINE "org.freedesktop.machine1.NoSuchMachine"
 #define BUS_ERROR_NO_MACHINE_FOR_PID "org.freedesktop.machine1.NoMachineForPID"
 #define BUS_ERROR_MACHINE_EXISTS "org.freedesktop.machine1.MachineExists"
+#define BUS_ERROR_NO_PRIVATE_NETWORKING "org.freedesktop.machine1.NoPrivateNetworking"
 
 #define BUS_ERROR_NO_SUCH_SESSION "org.freedesktop.login1.NoSuchSession"
 #define BUS_ERROR_NO_SESSION_FOR_PID "org.freedesktop.login1.NoSessionForPID"
diff --git a/src/shared/logs-show.c b/src/shared/logs-show.c
index 72c9ad2..c3578ac 100644
--- a/src/shared/logs-show.c
+++ b/src/shared/logs-show.c
@@ -1158,7 +1158,7 @@ static int get_boot_id_for_machine(const char *machine, sd_id128_t *boot_id) {
         if (r < 0)
                 return r;
 
-        r = namespace_open(pid, &pidnsfd, &mntnsfd, &rootfd);
+        r = namespace_open(pid, &pidnsfd, &mntnsfd, NULL, &rootfd);
         if (r < 0)
                 return r;
 
@@ -1174,7 +1174,7 @@ static int get_boot_id_for_machine(const char *machine, sd_id128_t *boot_id) {
 
                 pair[0] = safe_close(pair[0]);
 
-                r = namespace_enter(pidnsfd, mntnsfd, rootfd);
+                r = namespace_enter(pidnsfd, mntnsfd, -1, rootfd);
                 if (r < 0)
                         _exit(EXIT_FAILURE);
 
diff --git a/src/shared/util.c b/src/shared/util.c
index 020c1da..0cc51e0 100644
--- a/src/shared/util.c
+++ b/src/shared/util.c
@@ -6091,60 +6091,93 @@ int container_get_leader(const char *machine, pid_t *pid) {
         return 0;
 }
 
-int namespace_open(pid_t pid, int *pidns_fd, int *mntns_fd, int *root_fd) {
-        _cleanup_close_ int pidnsfd = -1, mntnsfd = -1;
-        const char *pidns, *mntns, *root;
+int namespace_open(pid_t pid, int *pidns_fd, int *mntns_fd, int *netns_fd, int *root_fd) {
+        _cleanup_close_ int pidnsfd = -1, mntnsfd = -1, netnsfd = -1;
         int rfd;
 
         assert(pid >= 0);
-        assert(pidns_fd);
-        assert(mntns_fd);
-        assert(root_fd);
 
-        mntns = procfs_file_alloca(pid, "ns/mnt");
-        mntnsfd = open(mntns, O_RDONLY|O_NOCTTY|O_CLOEXEC);
-        if (mntnsfd < 0)
-                return -errno;
+        if (mntns_fd) {
+                const char *mntns;
 
-        pidns = procfs_file_alloca(pid, "ns/pid");
-        pidnsfd = open(pidns, O_RDONLY|O_NOCTTY|O_CLOEXEC);
-        if (pidnsfd < 0)
-                return -errno;
+                mntns = procfs_file_alloca(pid, "ns/mnt");
+                mntnsfd = open(mntns, O_RDONLY|O_NOCTTY|O_CLOEXEC);
+                if (mntnsfd < 0)
+                        return -errno;
+        }
 
-        root = procfs_file_alloca(pid, "root");
-        rfd = open(root, O_RDONLY|O_NOCTTY|O_CLOEXEC|O_DIRECTORY);
-        if (rfd < 0)
-                return -errno;
+        if (pidns_fd) {
+                const char *pidns;
+
+                pidns = procfs_file_alloca(pid, "ns/pid");
+                pidnsfd = open(pidns, O_RDONLY|O_NOCTTY|O_CLOEXEC);
+                if (pidnsfd < 0)
+                        return -errno;
+        }
+
+        if (netns_fd) {
+                const char *netns;
+
+                netns = procfs_file_alloca(pid, "ns/net");
+                netnsfd = open(netns, O_RDONLY|O_NOCTTY|O_CLOEXEC);
+                if (netnsfd < 0)
+                        return -errno;
+        }
+
+        if (root_fd) {
+                const char *root;
+
+                root = procfs_file_alloca(pid, "root");
+                rfd = open(root, O_RDONLY|O_NOCTTY|O_CLOEXEC|O_DIRECTORY);
+                if (rfd < 0)
+                        return -errno;
+        }
+
+        if (pidns_fd)
+                *pidns_fd = pidnsfd;
 
-        *pidns_fd = pidnsfd;
-        *mntns_fd = mntnsfd;
-        *root_fd = rfd;
-        pidnsfd = -1;
-        mntnsfd = -1;
+        if (mntns_fd)
+                *mntns_fd = mntnsfd;
+
+        if (netns_fd)
+                *netns_fd = netnsfd;
+
+        if (root_fd)
+                *root_fd = rfd;
+
+        pidnsfd = mntnsfd = netnsfd = -1;
 
         return 0;
 }
 
-int namespace_enter(int pidns_fd, int mntns_fd, int root_fd) {
-        assert(pidns_fd >= 0);
-        assert(mntns_fd >= 0);
-        assert(root_fd >= 0);
+int namespace_enter(int pidns_fd, int mntns_fd, int netns_fd, int root_fd) {
 
-        if (setns(pidns_fd, CLONE_NEWPID) < 0)
-                return -errno;
+        if (pidns_fd >= 0)
+                if (setns(pidns_fd, CLONE_NEWPID) < 0)
+                        return -errno;
 
-        if (setns(mntns_fd, CLONE_NEWNS) < 0)
-                return -errno;
+        if (mntns_fd >= 0)
+                if (setns(mntns_fd, CLONE_NEWNS) < 0)
+                        return -errno;
 
-        if (fchdir(root_fd) < 0)
-                return -errno;
+        if (netns_fd >= 0)
+                if (setns(netns_fd, CLONE_NEWNET) < 0)
+                        return -errno;
 
-        if (chroot(".") < 0)
-                return -errno;
+        if (root_fd >= 0) {
+                if (fchdir(root_fd) < 0)
+                        return -errno;
+
+                if (chroot(".") < 0)
+                        return -errno;
+        }
 
         if (setresgid(0, 0, 0) < 0)
                 return -errno;
 
+        if (setgroups(0, NULL) < 0)
+                return -errno;
+
         if (setresuid(0, 0, 0) < 0)
                 return -errno;
 
diff --git a/src/shared/util.h b/src/shared/util.h
index f2ce4f0..7a7d15c 100644
--- a/src/shared/util.h
+++ b/src/shared/util.h
@@ -910,8 +910,8 @@ int parse_proc_cmdline(int (*parse_word)(const char *key, const char *value));
 
 int container_get_leader(const char *machine, pid_t *pid);
 
-int namespace_open(pid_t pid, int *pidns_fd, int *mntns_fd, int *root_fd);
-int namespace_enter(int pidns_fd, int mntns_fd, int root_fd);
+int namespace_open(pid_t pid, int *pidns_fd, int *mntns_fd, int *netns_fd, int *root_fd);
+int namespace_enter(int pidns_fd, int mntns_fd, int netns_fd, int root_fd);
 
 bool pid_is_alive(pid_t pid);
 bool pid_is_unwaited(pid_t pid);

commit 4eaea66423ca58dfd7cfd1099ed902d7c81d8622
Author: Lennart Poettering <lennart at poettering.net>
Date:   Sat May 17 21:32:13 2014 +0200

    network: always take possession of host side of nspawn veth tunnels and do IPv4LL on them

diff --git a/Makefile.am b/Makefile.am
index 6e0a59d..6e01188 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -2641,7 +2641,8 @@ INSTALL_DIRS += \
 
 dist_network_DATA = \
 	network/99-default.link \
-	network/80-container-host0.network
+	network/80-container-host0.network \
+	network/80-container-ve.network
 
 dist_udevrules_DATA += \
 	rules/42-usb-hid-pm.rules \
diff --git a/network/80-container-host0.network b/network/80-container-host0.network
index 5d327b4..8d9293f 100644
--- a/network/80-container-host0.network
+++ b/network/80-container-host0.network
@@ -1,3 +1,10 @@
+#  This file is part of systemd.
+#
+#  systemd is free software; you can redistribute it and/or modify it
+#  under the terms of the GNU Lesser General Public License as published by
+#  the Free Software Foundation; either version 2.1 of the License, or
+#  (at your option) any later version.
+
 [Match]
 Virtualization=container
 Name=host0
diff --git a/network/80-container-ve.network b/network/80-container-ve.network
new file mode 100644
index 0000000..f0c02b0
--- /dev/null
+++ b/network/80-container-ve.network
@@ -0,0 +1,13 @@
+#  This file is part of systemd.
+#
+#  systemd is free software; you can redistribute it and/or modify it
+#  under the terms of the GNU Lesser General Public License as published by
+#  the Free Software Foundation; either version 2.1 of the License, or
+#  (at your option) any later version.
+
+[Match]
+Name=ve-*
+Driver=veth
+
+[Network]
+IPv4LL=yes

commit a46fe318fccbbb3a7d629f95fc8fdbf4c33ed119
Author: Lennart Poettering <lennart at poettering.net>
Date:   Sat May 17 21:13:53 2014 +0200

    update TODO

diff --git a/TODO b/TODO
index 015faf6..c91404e 100644
--- a/TODO
+++ b/TODO
@@ -35,7 +35,6 @@ Features:
 * timesyncd:
   - hookup with networkd: NTP servers from dhcp
   - hookup with networkd: listen to online/offline state
-  - run as unpriviliged user
 
 * a way for container managers to turn off getty starting via $container_headless= or so...
 

commit f864fd1b4b4546244dfb59bb59196598742fa29c
Author: Lennart Poettering <lennart at poettering.net>
Date:   Sat May 17 21:13:10 2014 +0200

    timesyncd: make use of floating event sources for signal handling

diff --git a/src/timesync/timesyncd.c b/src/timesync/timesyncd.c
index 2184336..19e6d67 100644
--- a/src/timesync/timesyncd.c
+++ b/src/timesync/timesyncd.c
@@ -978,8 +978,8 @@ static int manager_new(Manager **ret) {
 
         sd_event_set_watchdog(m->event, true);
 
-        sd_event_add_signal(m->event, &m->sigterm, SIGTERM, NULL,  NULL);
-        sd_event_add_signal(m->event, &m->sigint, SIGINT, NULL, NULL);
+        sd_event_add_signal(m->event, NULL, SIGTERM, NULL,  NULL);
+        sd_event_add_signal(m->event, NULL, SIGINT, NULL, NULL);
 
         r = sd_resolve_default(&m->resolve);
         if (r < 0)
@@ -1006,9 +1006,6 @@ static void manager_free(Manager *m) {
         manager_disconnect(m);
         manager_flush_names(m);
 
-        sd_event_source_unref(m->sigint);
-        sd_event_source_unref(m->sigterm);
-
         sd_event_source_unref(m->event_retry);
 
         sd_event_source_unref(m->network_event_source);
diff --git a/src/timesync/timesyncd.h b/src/timesync/timesyncd.h
index 370b966..471d4b0 100644
--- a/src/timesync/timesyncd.h
+++ b/src/timesync/timesyncd.h
@@ -92,9 +92,6 @@ struct Manager {
 
         /* Retry connections */
         sd_event_source *event_retry;
-
-        /* Handle SIGINT/SIGTERM */
-        sd_event_source *sigterm, *sigint;
 };
 
 const struct ConfigPerfItem* timesyncd_gperf_lookup(const char *key, unsigned length);

commit 9f7115498bac670013f6b8923f2e12366fbd13a8
Author: Lennart Poettering <lennart at poettering.net>
Date:   Sat May 17 21:11:00 2014 +0200

    timesyncd: enable watchdog support

diff --git a/src/timesync/timesyncd.c b/src/timesync/timesyncd.c
index 33c04df..2184336 100644
--- a/src/timesync/timesyncd.c
+++ b/src/timesync/timesyncd.c
@@ -976,6 +976,8 @@ static int manager_new(Manager **ret) {
         if (r < 0)
                 return r;
 
+        sd_event_set_watchdog(m->event, true);
+
         sd_event_add_signal(m->event, &m->sigterm, SIGTERM, NULL,  NULL);
         sd_event_add_signal(m->event, &m->sigint, SIGINT, NULL, NULL);
 
diff --git a/units/systemd-timesyncd.service.in b/units/systemd-timesyncd.service.in
index 158438e..1d1f486 100644
--- a/units/systemd-timesyncd.service.in
+++ b/units/systemd-timesyncd.service.in
@@ -19,6 +19,7 @@ ExecStart=@rootlibexecdir@/systemd-timesyncd
 CapabilityBoundingSet=CAP_SYS_TIME CAP_SETUID CAP_SETGID CAP_SETPCAP
 PrivateTmp=yes
 PrivateDevices=yes
+WatchdogSec=1min
 
 [Install]
 WantedBy=multi-user.target

commit a349eb10d3c3a31cd47198cbf08e4f0dfaffef1d
Author: Lennart Poettering <lennart at poettering.net>
Date:   Sat May 17 20:33:47 2014 +0200

    timesyncd: run timesyncd as unpriviliged user "systemd-timesync" (but still with CAP_SYS_TIME)

diff --git a/Makefile.am b/Makefile.am
index f6c195b..6e0a59d 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -4066,6 +4066,7 @@ systemd_timesyncd_LDADD = \
 	libsystemd-shared.la \
 	libsystemd-resolve.la \
 	libsystemd-network.la \
+	libsystemd-capability.la \
 	-lm
 
 rootlibexec_PROGRAMS += \
diff --git a/README b/README
index 4358cd7..4e2f996 100644
--- a/README
+++ b/README
@@ -183,6 +183,11 @@ USERS AND GROUPS:
         exist. During execution this network facing service will drop
         privileges and assume this uid/gid for security reasons.
 
+        The NTP daemon requires the "systemd-timesync" system user and
+        group to exist. During execution this network facing service
+        will drop priviliges (with the exception of CAP_SYS_TIME) and
+        assume this uid/gid for security reasons.
+
 WARNINGS:
         systemd will warn you during boot if /etc/mtab is not a
         symlink to /proc/mounts. Please ensure that /etc/mtab is a
diff --git a/src/shared/capability.c b/src/shared/capability.c
index f8ee1c6..b49c514 100644
--- a/src/shared/capability.c
+++ b/src/shared/capability.c
@@ -156,7 +156,8 @@ int capability_bounding_set_drop(uint64_t drop, bool right_now) {
         r = 0;
 
 finish:
-        cap_set_proc(after_cap);
+        if (cap_set_proc(after_cap) < 0)
+                return -errno;
 
         return r;
 }
diff --git a/src/timesync/timesyncd.c b/src/timesync/timesyncd.c
index aefbd27..33c04df 100644
--- a/src/timesync/timesyncd.c
+++ b/src/timesync/timesyncd.c
@@ -33,6 +33,9 @@
 #include <sys/timex.h>
 #include <sys/socket.h>
 #include <resolv.h>
+#include <sys/prctl.h>
+#include <sys/types.h>
+#include <grp.h>
 
 #include "missing.h"
 #include "util.h"
@@ -49,6 +52,7 @@
 #include "sd-network.h"
 #include "event-util.h"
 #include "network-util.h"
+#include "capability.h"
 #include "timesyncd.h"
 
 #define TIME_T_MAX (time_t)((1UL << ((sizeof(time_t) << 3) - 1)) - 1)
@@ -1138,6 +1142,80 @@ static int manager_network_monitor_listen(Manager *m) {
         return 0;
 }
 
+static int drop_priviliges(void) {
+        static const cap_value_t bits[] = {
+                CAP_SYS_TIME,
+        };
+
+        _cleanup_cap_free_ cap_t d = NULL;
+        const char *name = "systemd-timesync";
+        uid_t uid;
+        gid_t gid;
+        int r;
+
+        /* Unfortunately we cannot leave privilige dropping to PID 1
+         * here, since we want to run as user but want to keep te
+         * CAP_SYS_TIME capability. Since file capabilities have been
+         * introduced this cannot be done across exec() anymore,
+         * unless our binary has the capability configured in the file
+         * system, which we want to avoid. */
+
+        r = get_user_creds(&name, &uid, &gid, NULL, NULL);
+        if (r < 0) {
+                log_error("Cannot resolve user name %s: %s", name, strerror(-r));
+                return r;
+        }
+
+        if (setresgid(gid, gid, gid) < 0) {
+                log_error("Failed change group ID: %m");
+                return -errno;
+        }
+
+        if (setgroups(0, NULL) < 0) {
+                log_error("Failed to drop auxiliary groups list: %m");
+                return -errno;
+        }
+
+        if (prctl(PR_SET_KEEPCAPS, 1) < 0) {
+                log_error("Failed to enable keep capabilities flag: %m");
+                return -errno;
+        }
+
+        r = setresuid(uid, uid, uid);
+        if (r < 0) {
+                log_error("Failed change user ID: %m");
+                return -errno;
+        }
+
+        if (prctl(PR_SET_KEEPCAPS, 0) < 0) {
+                log_error("Failed to disable keep capabilities flag: %m");
+                return -errno;
+        }
+
+        r = capability_bounding_set_drop(~(1ULL << CAP_SYS_TIME), true);
+        if (r < 0) {
+                log_error("Failed to drop capabilities: %s", strerror(-r));
+                return r;
+        }
+
+        d = cap_init();
+        if (!d)
+                return log_oom();
+
+        if (cap_set_flag(d, CAP_EFFECTIVE, ELEMENTSOF(bits), bits, CAP_SET) < 0 ||
+            cap_set_flag(d, CAP_PERMITTED, ELEMENTSOF(bits), bits, CAP_SET) < 0) {
+                log_error("Failed to enable capabilities bits: %m");
+                return -errno;
+        }
+
+        if (cap_set_proc(d) < 0) {
+                log_error("Failed to increase capabilities: %m");
+                return -errno;
+        }
+
+        return 0;
+}
+
 int main(int argc, char *argv[]) {
         _cleanup_manager_free_ Manager *m = NULL;
         int r;
@@ -1154,6 +1232,10 @@ int main(int argc, char *argv[]) {
 
         umask(0022);
 
+        r = drop_priviliges();
+        if (r < 0)
+                goto out;
+
         assert_se(sigprocmask_many(SIG_BLOCK, SIGTERM, SIGINT, -1) == 0);
 
         r = manager_new(&m);
diff --git a/units/systemd-timesyncd.service.in b/units/systemd-timesyncd.service.in
index e279d1b..158438e 100644
--- a/units/systemd-timesyncd.service.in
+++ b/units/systemd-timesyncd.service.in
@@ -16,7 +16,9 @@ Type=notify
 Restart=always
 RestartSec=0
 ExecStart=@rootlibexecdir@/systemd-timesyncd
-CapabilityBoundingSet=CAP_SYS_TIME
+CapabilityBoundingSet=CAP_SYS_TIME CAP_SETUID CAP_SETGID CAP_SETPCAP
+PrivateTmp=yes
+PrivateDevices=yes
 
 [Install]
 WantedBy=multi-user.target



More information about the systemd-commits mailing list