1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2010 Lennart Poettering
7 Copyright 2013 Daniel Mack
8 Copyright 2014 Kay Sievers
9 Copyright 2015 David Herrmann
11 systemd is free software; you can redistribute it and/or modify it
12 under the terms of the GNU Lesser General Public License as published by
13 the Free Software Foundation; either version 2.1 of the License, or
14 (at your option) any later version.
16 systemd is distributed in the hope that it will be useful, but
17 WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 Lesser General Public License for more details.
21 You should have received a copy of the GNU Lesser General Public License
22 along with systemd; If not, see <http://www.gnu.org/licenses/>.
25 #include <sys/socket.h>
27 #include <sys/types.h>
33 #include <sys/prctl.h>
41 #include "socket-util.h"
42 #include "sd-daemon.h"
44 #include "bus-internal.h"
45 #include "bus-message.h"
50 #include "capability.h"
51 #include "bus-control.h"
52 #include "smack-util.h"
54 #include "bus-xml-policy.h"
57 #include "synthesize.h"
59 static char *arg_address = NULL;
60 static char **arg_configuration = NULL;
68 static ClientContext *client_context_free(ClientContext *c) {
78 DEFINE_TRIVIAL_CLEANUP_FUNC(ClientContext*, client_context_free);
80 static int client_context_new(ClientContext **out) {
81 _cleanup_(client_context_freep) ClientContext *c = NULL;
83 c = new0(ClientContext, 1);
94 static void *run_client(void *userdata) {
95 _cleanup_(client_context_freep) ClientContext *c = userdata;
96 _cleanup_(proxy_freep) Proxy *p = NULL;
100 r = proxy_new(&p, c->fd, c->fd, arg_address);
106 /* set comm to "p$PIDu$UID" and suffix with '*' if truncated */
107 r = snprintf(comm, sizeof(comm), "p" PID_FMT "u" UID_FMT, p->local_creds.pid, p->local_creds.uid);
108 if (r >= (ssize_t)sizeof(comm))
109 comm[sizeof(comm) - 2] = '*';
110 (void) prctl(PR_SET_NAME, comm);
112 r = proxy_set_policy(p, c->policy, arg_configuration);
116 r = proxy_hello_policy(p, c->bus_uid);
126 static int loop_clients(int accept_fd, uid_t bus_uid) {
127 _cleanup_(shared_policy_freep) SharedPolicy *sp = NULL;
131 r = pthread_attr_init(&attr);
133 return log_error_errno(errno, "Cannot initialize pthread attributes: %m");
136 r = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
138 r = log_error_errno(errno, "Cannot mark pthread attributes as detached: %m");
142 r = shared_policy_new(&sp);
151 fd = accept4(accept_fd, NULL, NULL, SOCK_NONBLOCK | SOCK_CLOEXEC);
153 if (errno == EAGAIN || errno == EINTR)
156 r = log_error_errno(errno, "accept4() failed: %m");
160 r = client_context_new(&c);
169 c->bus_uid = bus_uid;
171 r = pthread_create(&tid, &attr, run_client, c);
173 log_error("Cannot spawn thread: %m");
174 client_context_free(c);
180 pthread_attr_destroy(&attr);
184 static int help(void) {
186 printf("%s [OPTIONS...]\n\n"
187 "DBus proxy server.\n\n"
188 " -h --help Show this help\n"
189 " --version Show package version\n"
190 " --configuration=PATH Configuration file or directory\n"
191 " --machine=MACHINE Connect to specified machine\n"
192 " --address=ADDRESS Connect to the bus specified by ADDRESS\n"
193 " (default: " DEFAULT_SYSTEM_BUS_ADDRESS ")\n",
194 program_invocation_short_name);
199 static int parse_argv(int argc, char *argv[]) {
208 static const struct option options[] = {
209 { "help", no_argument, NULL, 'h' },
210 { "version", no_argument, NULL, ARG_VERSION },
211 { "address", required_argument, NULL, ARG_ADDRESS },
212 { "configuration", required_argument, NULL, ARG_CONFIGURATION },
213 { "machine", required_argument, NULL, ARG_MACHINE },
222 while ((c = getopt_long(argc, argv, "h", options, NULL)) >= 0)
231 puts(PACKAGE_STRING);
232 puts(SYSTEMD_FEATURES);
236 r = free_and_strdup(&arg_address, optarg);
241 case ARG_CONFIGURATION:
242 r = strv_extend(&arg_configuration, optarg);
248 _cleanup_free_ char *e = NULL;
251 e = bus_address_escape(optarg);
256 a = strjoin("x-machine-kernel:machine=", e, ";x-machine-unix:machine=", e, NULL);
258 a = strjoin("x-machine-unix:machine=", e, NULL);
273 assert_not_reached("Unhandled option");
277 log_error("Too many arguments");
282 arg_address = strdup(DEFAULT_SYSTEM_BUS_ADDRESS);
290 int main(int argc, char *argv[]) {
295 log_set_target(LOG_TARGET_JOURNAL_OR_KMSG);
296 log_parse_environment();
301 if (geteuid() == 0) {
302 const char *user = "systemd-bus-proxy";
304 r = get_user_creds(&user, &uid, &gid, NULL, NULL);
306 log_error_errno(r, "Cannot resolve user name %s: %m", user);
310 r = drop_privileges(uid, gid, 1ULL << CAP_IPC_OWNER);
312 log_error_errno(r, "Cannot drop privileges: %m");
317 r = parse_argv(argc, argv);
321 r = sd_listen_fds(0);
323 log_error("Illegal number of file descriptors passed");
327 accept_fd = SD_LISTEN_FDS_START;
329 r = fd_nonblock(accept_fd, false);
331 log_error_errno(r, "Cannot mark accept-fd non-blocking: %m");
335 r = loop_clients(accept_fd, bus_uid);
340 "STATUS=Shutting down.");
342 strv_free(arg_configuration);
345 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;