X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=blobdiff_plain;f=src%2Fbus-proxyd%2Fbus-proxyd.c;h=d612816851291e02b2388460a72631474fe9d800;hb=27cc6f166bdebc0e698fb692993b801db2618866;hp=3cf35f4081d503887610e64145976f5001e7617a;hpb=a80a3a7543975d1268692937d9d1884c6ee6e301;p=elogind.git diff --git a/src/bus-proxyd/bus-proxyd.c b/src/bus-proxyd/bus-proxyd.c index 3cf35f408..d61281685 100644 --- a/src/bus-proxyd/bus-proxyd.c +++ b/src/bus-proxyd/bus-proxyd.c @@ -23,13 +23,9 @@ ***/ #include -#include -#include -#include #include #include #include -#include #include #include #include @@ -37,24 +33,14 @@ #include "log.h" #include "util.h" -#include "hashmap.h" -#include "socket-util.h" #include "sd-daemon.h" -#include "sd-bus.h" #include "bus-internal.h" -#include "bus-message.h" -#include "bus-util.h" #include "build.h" #include "strv.h" #include "def.h" #include "capability.h" -#include "bus-control.h" -#include "smack-util.h" -#include "set.h" #include "bus-xml-policy.h" -#include "driver.h" #include "proxy.h" -#include "synthesize.h" static char *arg_address = NULL; static char **arg_configuration = NULL; @@ -62,13 +48,14 @@ static char **arg_configuration = NULL; typedef struct { int fd; SharedPolicy *policy; + uid_t bus_uid; } ClientContext; static ClientContext *client_context_free(ClientContext *c) { if (!c) return NULL; - close(c->fd); + safe_close(c->fd); free(c); return NULL; @@ -81,7 +68,7 @@ static int client_context_new(ClientContext **out) { c = new0(ClientContext, 1); if (!c) - return log_oom(); + return -ENOMEM; c->fd = -1; @@ -100,6 +87,8 @@ static void *run_client(void *userdata) { if (r < 0) goto exit; + c->fd = -1; + /* set comm to "p$PIDu$UID" and suffix with '*' if truncated */ r = snprintf(comm, sizeof(comm), "p" PID_FMT "u" UID_FMT, p->local_creds.pid, p->local_creds.uid); if (r >= (ssize_t)sizeof(comm)) @@ -110,7 +99,7 @@ static void *run_client(void *userdata) { if (r < 0) goto exit; - r = proxy_hello_policy(p, getuid()); + r = proxy_hello_policy(p, c->bus_uid); if (r < 0) goto exit; @@ -120,26 +109,25 @@ exit: return NULL; } -static int loop_clients(int accept_fd) { +static int loop_clients(int accept_fd, uid_t bus_uid) { _cleanup_(shared_policy_freep) SharedPolicy *sp = NULL; pthread_attr_t attr; int r; r = pthread_attr_init(&attr); if (r < 0) { - r = log_error_errno(errno, "Cannot initialize pthread attributes: %m"); - goto exit; + return log_error_errno(errno, "Cannot initialize pthread attributes: %m"); } r = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); if (r < 0) { r = log_error_errno(errno, "Cannot mark pthread attributes as detached: %m"); - goto exit_attr; + goto finish; } r = shared_policy_new(&sp); if (r < 0) - goto exit_attr; + goto finish; for (;;) { ClientContext *c; @@ -152,7 +140,7 @@ static int loop_clients(int accept_fd) { continue; r = log_error_errno(errno, "accept4() failed: %m"); - break; + goto finish; } r = client_context_new(&c); @@ -164,6 +152,7 @@ static int loop_clients(int accept_fd) { c->fd = fd; c->policy = sp; + c->bus_uid = bus_uid; r = pthread_create(&tid, &attr, run_client, c); if (r < 0) { @@ -173,9 +162,8 @@ static int loop_clients(int accept_fd) { } } -exit_attr: +finish: pthread_attr_destroy(&attr); -exit: return r; } @@ -230,17 +218,11 @@ static int parse_argv(int argc, char *argv[]) { puts(SYSTEMD_FEATURES); return 0; - case ARG_ADDRESS: { - char *a; - - a = strdup(optarg); - if (!a) + case ARG_ADDRESS: + r = free_and_strdup(&arg_address, optarg); + if (r < 0) return log_oom(); - - free(arg_address); - arg_address = a; break; - } case ARG_CONFIGURATION: r = strv_extend(&arg_configuration, optarg); @@ -292,16 +274,19 @@ static int parse_argv(int argc, char *argv[]) { } int main(int argc, char *argv[]) { - const char *user = "systemd-bus-proxy"; int r, accept_fd; - uid_t uid; + uid_t uid, bus_uid; gid_t gid; log_set_target(LOG_TARGET_JOURNAL_OR_KMSG); log_parse_environment(); log_open(); + bus_uid = getuid(); + if (geteuid() == 0) { + const char *user = "systemd-bus-proxy"; + r = get_user_creds(&user, &uid, &gid, NULL, NULL); if (r < 0) { log_error_errno(r, "Cannot resolve user name %s: %m", user); @@ -326,13 +311,14 @@ int main(int argc, char *argv[]) { } accept_fd = SD_LISTEN_FDS_START; + r = fd_nonblock(accept_fd, false); if (r < 0) { log_error_errno(r, "Cannot mark accept-fd non-blocking: %m"); goto finish; } - r = loop_clients(accept_fd); + r = loop_clients(accept_fd, bus_uid); finish: sd_notify(false,