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>
29 #include <sys/prctl.h>
36 #include "sd-daemon.h"
37 #include "bus-internal.h"
41 #include "capability.h"
42 #include "bus-xml-policy.h"
45 static char *arg_address = NULL;
46 static char **arg_configuration = NULL;
54 static ClientContext *client_context_free(ClientContext *c) {
64 DEFINE_TRIVIAL_CLEANUP_FUNC(ClientContext*, client_context_free);
66 static int client_context_new(ClientContext **out) {
67 _cleanup_(client_context_freep) ClientContext *c = NULL;
69 c = new0(ClientContext, 1);
80 static void *run_client(void *userdata) {
81 _cleanup_(client_context_freep) ClientContext *c = userdata;
82 _cleanup_(proxy_freep) Proxy *p = NULL;
86 r = proxy_new(&p, c->fd, c->fd, arg_address);
92 /* set comm to "p$PIDu$UID" and suffix with '*' if truncated */
93 r = snprintf(comm, sizeof(comm), "p" PID_FMT "u" UID_FMT, p->local_creds.pid, p->local_creds.uid);
94 if (r >= (ssize_t)sizeof(comm))
95 comm[sizeof(comm) - 2] = '*';
96 (void) prctl(PR_SET_NAME, comm);
98 r = proxy_set_policy(p, c->policy, arg_configuration);
102 r = proxy_hello_policy(p, c->bus_uid);
112 static int loop_clients(int accept_fd, uid_t bus_uid) {
113 _cleanup_(shared_policy_freep) SharedPolicy *sp = NULL;
117 r = pthread_attr_init(&attr);
119 return log_error_errno(errno, "Cannot initialize pthread attributes: %m");
122 r = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
124 r = log_error_errno(errno, "Cannot mark pthread attributes as detached: %m");
128 r = shared_policy_new(&sp);
137 fd = accept4(accept_fd, NULL, NULL, SOCK_NONBLOCK | SOCK_CLOEXEC);
139 if (errno == EAGAIN || errno == EINTR)
142 r = log_error_errno(errno, "accept4() failed: %m");
146 r = client_context_new(&c);
155 c->bus_uid = bus_uid;
157 r = pthread_create(&tid, &attr, run_client, c);
159 log_error("Cannot spawn thread: %m");
160 client_context_free(c);
166 pthread_attr_destroy(&attr);
170 static int help(void) {
172 printf("%s [OPTIONS...]\n\n"
173 "DBus proxy server.\n\n"
174 " -h --help Show this help\n"
175 " --version Show package version\n"
176 " --configuration=PATH Configuration file or directory\n"
177 " --machine=MACHINE Connect to specified machine\n"
178 " --address=ADDRESS Connect to the bus specified by ADDRESS\n"
179 " (default: " DEFAULT_SYSTEM_BUS_ADDRESS ")\n",
180 program_invocation_short_name);
185 static int parse_argv(int argc, char *argv[]) {
194 static const struct option options[] = {
195 { "help", no_argument, NULL, 'h' },
196 { "version", no_argument, NULL, ARG_VERSION },
197 { "address", required_argument, NULL, ARG_ADDRESS },
198 { "configuration", required_argument, NULL, ARG_CONFIGURATION },
199 { "machine", required_argument, NULL, ARG_MACHINE },
208 while ((c = getopt_long(argc, argv, "h", options, NULL)) >= 0)
217 puts(PACKAGE_STRING);
218 puts(SYSTEMD_FEATURES);
222 r = free_and_strdup(&arg_address, optarg);
227 case ARG_CONFIGURATION:
228 r = strv_extend(&arg_configuration, optarg);
234 _cleanup_free_ char *e = NULL;
237 e = bus_address_escape(optarg);
242 a = strjoin("x-machine-kernel:machine=", e, ";x-machine-unix:machine=", e, NULL);
244 a = strjoin("x-machine-unix:machine=", e, NULL);
259 assert_not_reached("Unhandled option");
263 log_error("Too many arguments");
268 arg_address = strdup(DEFAULT_SYSTEM_BUS_ADDRESS);
276 int main(int argc, char *argv[]) {
281 log_set_target(LOG_TARGET_JOURNAL_OR_KMSG);
282 log_parse_environment();
287 if (geteuid() == 0) {
288 const char *user = "systemd-bus-proxy";
290 r = get_user_creds(&user, &uid, &gid, NULL, NULL);
292 log_error_errno(r, "Cannot resolve user name %s: %m", user);
296 r = drop_privileges(uid, gid, 1ULL << CAP_IPC_OWNER);
298 log_error_errno(r, "Cannot drop privileges: %m");
303 r = parse_argv(argc, argv);
307 r = sd_listen_fds(0);
309 log_error("Illegal number of file descriptors passed");
313 accept_fd = SD_LISTEN_FDS_START;
315 r = fd_nonblock(accept_fd, false);
317 log_error_errno(r, "Cannot mark accept-fd non-blocking: %m");
321 r = loop_clients(accept_fd, bus_uid);
326 "STATUS=Shutting down.");
328 strv_free(arg_configuration);
331 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;