X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=blobdiff_plain;f=src%2Fmachine%2Fmachine-dbus.c;h=b46f0a8dac8afc811fc7b6c1b5e0e0dd5ac9c75c;hb=0a6f50c0afdfc434b492493bd9efab20cbee8623;hp=76c5dcf735ba1369480d5f9c257bdba02b3fbdf5;hpb=ee451d766a64117a41ec36dd71e61683c9d9b83c;p=elogind.git diff --git a/src/machine/machine-dbus.c b/src/machine/machine-dbus.c index 76c5dcf73..b46f0a8da 100644 --- a/src/machine/machine-dbus.c +++ b/src/machine/machine-dbus.c @@ -21,7 +21,6 @@ #include #include -#include #include #include "bus-util.h" @@ -32,7 +31,10 @@ #include "fileio.h" #include "in-addr-util.h" #include "local-addresses.h" +#include "path-util.h" +#include "bus-internal.h" #include "machine.h" +#include "machine-dbus.h" static int property_get_id( sd_bus *bus, @@ -173,6 +175,9 @@ int bus_machine_method_get_addresses(sd_bus *bus, sd_bus_message *message, void assert(message); assert(m); + if (m->class != MACHINE_CONTAINER) + return sd_bus_error_setf(error, SD_BUS_ERROR_NOT_SUPPORTED, "Requesting IP address data is only supported on container machines."); + r = readlink_malloc("/proc/self/ns/net", &us); if (r < 0) return sd_bus_error_set_errno(error, r); @@ -317,6 +322,9 @@ int bus_machine_method_get_os_release(sd_bus *bus, sd_bus_message *message, void assert(message); assert(m); + if (m->class != MACHINE_CONTAINER) + return sd_bus_error_setf(error, SD_BUS_ERROR_NOT_SUPPORTED, "Requesting OS release data is only supported on container machines."); + r = namespace_open(m->leader, NULL, &mntns_fd, NULL, &root_fd); if (r < 0) return r; @@ -391,99 +399,114 @@ int bus_machine_method_get_os_release(sd_bus *bus, sd_bus_message *message, void } int bus_machine_method_open_pty(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) { - _cleanup_close_ int pidnsfd = -1, mntnsfd = -1, rootfd = -1; _cleanup_bus_message_unref_ sd_bus_message *reply = NULL; - _cleanup_close_pair_ int pair[2] = { -1, -1 }; + _cleanup_free_ char *pty_name = NULL; _cleanup_close_ int master = -1; - union { - struct cmsghdr cmsghdr; - uint8_t buf[CMSG_SPACE(sizeof(int))]; - } control = {}; - struct msghdr mh = { - .msg_control = &control, - .msg_controllen = sizeof(control), - }; Machine *m = userdata; - _cleanup_free_ char *pty_name = NULL; - struct cmsghdr *cmsg; - siginfo_t si; - pid_t child; int r; assert(bus); assert(message); assert(m); - r = namespace_open(m->leader, &pidnsfd, &mntnsfd, NULL, &rootfd); - if (r < 0) - return r; - - if (socketpair(AF_UNIX, SOCK_DGRAM, 0, pair) < 0) - return -errno; + if (m->class != MACHINE_CONTAINER) + return sd_bus_error_setf(error, SD_BUS_ERROR_NOT_SUPPORTED, "Opening pseudo TTYs is only supported on container machines."); - child = fork(); - if (child < 0) - return -errno; + master = openpt_in_namespace(m->leader, O_RDWR|O_NOCTTY|O_CLOEXEC); + if (master < 0) + return master; - if (child == 0) { - pair[0] = safe_close(pair[0]); + r = ptsname_malloc(master, &pty_name); + if (r < 0) + return r; - r = namespace_enter(pidnsfd, mntnsfd, -1, rootfd); - if (r < 0) - _exit(EXIT_FAILURE); + r = sd_bus_message_new_method_return(message, &reply); + if (r < 0) + return r; - master = posix_openpt(O_RDWR|O_NOCTTY|O_CLOEXEC); - if (master < 0) - _exit(EXIT_FAILURE); + r = sd_bus_message_append(reply, "hs", master, pty_name); + if (r < 0) + return r; - cmsg = CMSG_FIRSTHDR(&mh); - cmsg->cmsg_level = SOL_SOCKET; - cmsg->cmsg_type = SCM_RIGHTS; - cmsg->cmsg_len = CMSG_LEN(sizeof(int)); - memcpy(CMSG_DATA(cmsg), &master, sizeof(int)); + return sd_bus_send(bus, reply, NULL); +} - mh.msg_controllen = cmsg->cmsg_len; +int bus_machine_method_open_login(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) { + _cleanup_bus_message_unref_ sd_bus_message *reply = NULL; + _cleanup_free_ char *pty_name = NULL, *getty = NULL; + _cleanup_bus_unref_ sd_bus *container_bus = NULL; + _cleanup_close_ int master = -1; + Machine *m = userdata; + const char *p; + int r; - if (sendmsg(pair[1], &mh, MSG_NOSIGNAL) < 0) - _exit(EXIT_FAILURE); + if (m->class != MACHINE_CONTAINER) + return sd_bus_error_setf(error, SD_BUS_ERROR_NOT_SUPPORTED, "Opening logins is only supported on container machines."); - _exit(EXIT_SUCCESS); - } + r = bus_verify_polkit_async( + message, + CAP_SYS_ADMIN, + "org.freedesktop.machine1.login", + false, + &m->manager->polkit_registry, + error); + if (r < 0) + return r; + if (r == 0) + return 1; /* Will call us back */ - pair[1] = safe_close(pair[1]); + master = openpt_in_namespace(m->leader, O_RDWR|O_NOCTTY|O_CLOEXEC); + if (master < 0) + return master; - r = wait_for_terminate(child, &si); + r = ptsname_malloc(master, &pty_name); 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) + p = path_startswith(pty_name, "/dev/pts/"); + if (!p) + return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "PTS name %s is invalid", pty_name); + + if (unlockpt(master) < 0) return -errno; - for (cmsg = CMSG_FIRSTHDR(&mh); cmsg; cmsg = CMSG_NXTHDR(&mh, cmsg)) - if (cmsg->cmsg_level == SOL_SOCKET && cmsg->cmsg_type == SCM_RIGHTS) { - int *fds; - unsigned n_fds; + r = sd_bus_new(&container_bus); + if (r < 0) + return r; - fds = (int*) CMSG_DATA(cmsg); - n_fds = (cmsg->cmsg_len - CMSG_LEN(0)) / sizeof(int); +#ifdef ENABLE_KDBUS + asprintf(&container_bus->address, "x-machine-kernel:pid=" PID_FMT ";x-machine-unix:pid=" PID_FMT, m->leader, m->leader); +#else + asprintf(&container_bus->address, "x-machine-kernel:pid=" PID_FMT, m->leader); +#endif + if (!container_bus->address) + return -ENOMEM; - if (n_fds != 1) { - close_many(fds, n_fds); - return -EIO; - } + container_bus->bus_client = true; + container_bus->trusted = false; + container_bus->is_system = true; - master = fds[0]; - } + r = sd_bus_start(container_bus); + if (r < 0) + return r; - if (master < 0) - return -EIO; + getty = strjoin("container-getty@", p, ".service", NULL); + if (!getty) + return -ENOMEM; - r = ptsname_malloc(master, &pty_name); + r = sd_bus_call_method( + container_bus, + "org.freedesktop.systemd1", + "/org/freedesktop/systemd1", + "org.freedesktop.systemd1.Manager", + "StartUnit", + error, NULL, + "ss", getty, "replace"); if (r < 0) return r; + container_bus = sd_bus_unref(container_bus); + r = sd_bus_message_new_method_return(message, &reply); if (r < 0) return r; @@ -513,6 +536,7 @@ const sd_bus_vtable machine_vtable[] = { SD_BUS_METHOD("GetAddresses", NULL, "a(iay)", bus_machine_method_get_addresses, SD_BUS_VTABLE_UNPRIVILEGED), SD_BUS_METHOD("GetOSRelease", NULL, "a{ss}", bus_machine_method_get_os_release, SD_BUS_VTABLE_UNPRIVILEGED), SD_BUS_METHOD("OpenPTY", NULL, "hs", bus_machine_method_open_pty, 0), + SD_BUS_METHOD("OpenLogin", NULL, "hs", bus_machine_method_open_login, SD_BUS_VTABLE_UNPRIVILEGED), SD_BUS_VTABLE_END };