chiark / gitweb /
Move bus path definitions to def.h
[elogind.git] / src / bus-proxyd / bus-proxyd.c
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3 /***
4   This file is part of systemd.
5
6   Copyright 2010 Lennart Poettering
7
8   systemd is free software; you can redistribute it and/or modify it
9   under the terms of the GNU Lesser General Public License as published by
10   the Free Software Foundation; either version 2.1 of the License, or
11   (at your option) any later version.
12
13   systemd is distributed in the hope that it will be useful, but
14   WITHOUT ANY WARRANTY; without even the implied warranty of
15   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16   Lesser General Public License for more details.
17
18   You should have received a copy of the GNU Lesser General Public License
19   along with systemd; If not, see <http://www.gnu.org/licenses/>.
20 ***/
21
22 #include <sys/socket.h>
23 #include <sys/un.h>
24 #include <sys/types.h>
25 #include <fcntl.h>
26 #include <unistd.h>
27 #include <string.h>
28 #include <errno.h>
29 #include <sys/poll.h>
30 #include <stddef.h>
31 #include <getopt.h>
32
33 #include "log.h"
34 #include "util.h"
35 #include "socket-util.h"
36 #include "sd-daemon.h"
37 #include "sd-bus.h"
38 #include "bus-internal.h"
39 #include "bus-message.h"
40 #include "bus-util.h"
41 #include "build.h"
42 #include "strv.h"
43 #include "def.h"
44
45 static const char *arg_address = DEFAULT_SYSTEM_BUS_PATH;
46 static char *arg_command_line_buffer = NULL;
47
48 static int help(void) {
49
50         printf("%s [OPTIONS...]\n\n"
51                "Connect STDIO or a socket to a given bus address.\n\n"
52                "  -h --help              Show this help\n"
53                "     --version           Show package version\n"
54                "     --address=ADDRESS   Connect to the bus specified by ADDRESS\n"
55                "                         (default: " DEFAULT_SYSTEM_BUS_PATH ")\n",
56                program_invocation_short_name);
57
58         return 0;
59 }
60
61 static int parse_argv(int argc, char *argv[]) {
62
63         enum {
64                 ARG_VERSION = 0x100,
65                 ARG_ADDRESS,
66         };
67
68         static const struct option options[] = {
69                 { "help",       no_argument,       NULL, 'h'            },
70                 { "version",    no_argument,       NULL, ARG_VERSION    },
71                 { "address",    required_argument, NULL, ARG_ADDRESS    },
72                 { NULL,         0,                 NULL, 0              }
73         };
74
75         int c;
76
77         assert(argc >= 0);
78         assert(argv);
79
80         while ((c = getopt_long(argc, argv, "h", options, NULL)) >= 0) {
81
82                 switch (c) {
83
84                 case 'h':
85                         help();
86                         return 0;
87
88                 case ARG_VERSION:
89                         puts(PACKAGE_STRING);
90                         puts(SYSTEMD_FEATURES);
91                         return 0;
92
93                 case ARG_ADDRESS:
94                         arg_address = optarg;
95                         break;
96
97                 case '?':
98                         return -EINVAL;
99
100                 default:
101                         assert_not_reached("Unhandled option");
102                 }
103         }
104
105         /* If the first command line argument is only "x" characters
106          * we'll write who we are talking to into it, so that "ps" is
107          * explanatory */
108         arg_command_line_buffer = argv[optind];
109         if (argc > optind + 1 ||
110             (arg_command_line_buffer && arg_command_line_buffer[strspn(arg_command_line_buffer, "x")] != 0)) {
111                 log_error("Too many arguments");
112                 return -EINVAL;
113         }
114
115         return 1;
116 }
117
118 static int rename_service(sd_bus *a, sd_bus *b) {
119         _cleanup_bus_creds_unref_ sd_bus_creds *creds = NULL;
120         _cleanup_free_ char *p = NULL, *name = NULL;
121         const char *comm;
122         char **cmdline;
123         uid_t uid;
124         pid_t pid;
125         int r;
126
127         assert(a);
128         assert(b);
129
130         r = sd_bus_get_peer_creds(b, SD_BUS_CREDS_UID|SD_BUS_CREDS_PID|SD_BUS_CREDS_CMDLINE|SD_BUS_CREDS_COMM, &creds);
131         if (r < 0)
132                 return r;
133
134         r = sd_bus_creds_get_uid(creds, &uid);
135         if (r < 0)
136                 return r;
137
138         r = sd_bus_creds_get_pid(creds, &pid);
139         if (r < 0)
140                 return r;
141
142         r = sd_bus_creds_get_cmdline(creds, &cmdline);
143         if (r < 0)
144                 return r;
145
146         r = sd_bus_creds_get_comm(creds, &comm);
147         if (r < 0)
148                 return r;
149
150         name = uid_to_name(uid);
151         if (!name)
152                 return -ENOMEM;
153
154         p = strv_join(cmdline, " ");
155         if (!p)
156                 return -ENOMEM;
157
158         /* The status string gets the full command line ... */
159         sd_notifyf(false,
160                    "STATUS=Processing requests from client PID %lu (%s); UID %lu (%s)",
161                    (unsigned long) pid, p,
162                    (unsigned long) uid, name);
163
164         /* ... and the argv line only the short comm */
165         if (arg_command_line_buffer) {
166                 size_t m, w;
167
168                 m = strlen(arg_command_line_buffer);
169                 w = snprintf(arg_command_line_buffer, m,
170                              "[PID %lu/%s; UID %lu/%s]",
171                              (unsigned long) pid, comm,
172                              (unsigned long) uid, name);
173
174                 if (m > w)
175                         memset(arg_command_line_buffer + w, 0, m - w);
176         }
177
178         log_debug("Running on behalf of PID %lu (%s), UID %lu (%s), %s",
179                   (unsigned long) pid, p,
180                   (unsigned long) uid, name,
181                   a->unique_name);
182                 ;
183         return 0;
184 }
185
186 static int synthesize_name_acquired(sd_bus *a, sd_bus *b, sd_bus_message *m) {
187         _cleanup_bus_message_unref_ sd_bus_message *n = NULL;
188         const char *name, *old_owner, *new_owner;
189         int r;
190
191         assert(a);
192         assert(b);
193         assert(m);
194
195         /* If we get NameOwnerChanged for our own name, we need to
196          * synthesize NameLost/NameAcquired, since socket clients need
197          * that, even though it is obsoleted on kdbus */
198
199         if (!a->is_kernel)
200                 return 0;
201
202         if (!sd_bus_message_is_signal(m, "org.freedesktop.DBus", "NameOwnerChanged") ||
203             !streq_ptr(m->path, "/org/freedesktop/DBus") ||
204             !streq_ptr(m->sender, "org.freedesktop.DBus"))
205                 return 0;
206
207         r = sd_bus_message_read(m, "sss", &name, &old_owner, &new_owner);
208         if (r < 0)
209                 return r;
210
211         r = sd_bus_message_rewind(m, true);
212         if (r < 0)
213                 return r;
214
215         if (streq(old_owner, a->unique_name)) {
216
217                 r = sd_bus_message_new_signal(
218                                 b,
219                                 "/org/freedesktop/DBus",
220                                 "org.freedesktop.DBus",
221                                 "NameLost",
222                                 &n);
223
224         } else if (streq(new_owner, a->unique_name)) {
225
226                 r = sd_bus_message_new_signal(
227                                 b,
228                                 "/org/freedesktop/DBus",
229                                 "org.freedesktop.DBus",
230                                 "NameAcquired",
231                                 &n);
232         } else
233                 return 0;
234
235         if (r < 0)
236                 return r;
237
238         r = sd_bus_message_append(n, "s", name);
239         if (r < 0)
240                 return r;
241
242         r = bus_message_append_sender(n, "org.freedesktop.DBus");
243         if (r < 0)
244                 return r;
245
246         r = bus_seal_synthetic_message(b, n);
247         if (r < 0)
248                 return r;
249
250         return sd_bus_send(b, n, NULL);
251 }
252
253 static int process_policy(sd_bus *a, sd_bus *b, sd_bus_message *m) {
254         _cleanup_bus_message_unref_ sd_bus_message *n = NULL;
255         int r;
256
257         assert(a);
258         assert(b);
259         assert(m);
260
261         if (!sd_bus_message_is_method_call(m, "org.freedesktop.DBus.Properties", "GetAll"))
262                 return 0;
263
264         if (!streq_ptr(m->path, "/org/gnome/DisplayManager/Slave"))
265                 return 0;
266
267         r = sd_bus_message_new_method_errorf(m, &n, SD_BUS_ERROR_ACCESS_DENIED, "gdm, you are stupid");
268         if (r < 0)
269                 return r;
270
271         r = bus_message_append_sender(n, "org.freedesktop.DBus");
272         if (r < 0) {
273                 log_error("Failed to append sender to gdm reply: %s", strerror(-r));
274                 return r;
275         }
276
277         r = bus_seal_synthetic_message(b, n);
278         if (r < 0) {
279                 log_error("Failed to seal gdm reply: %s", strerror(-r));
280                 return r;
281         }
282
283         r = sd_bus_send(b, n, NULL);
284         if (r < 0) {
285                 log_error("Failed to send gdm reply: %s", strerror(-r));
286                 return r;
287         }
288
289         return 1;
290 }
291
292 static int process_hello(sd_bus *a, sd_bus *b, sd_bus_message *m, bool *got_hello) {
293         _cleanup_bus_message_unref_ sd_bus_message *n = NULL;
294         bool is_hello;
295         int r;
296
297         assert(a);
298         assert(b);
299         assert(m);
300         assert(got_hello);
301
302         /* As reaction to hello we need to respond with two messages:
303          * the callback reply and the NameAcquired for the unique
304          * name, since hello is otherwise obsolete on kdbus. */
305
306         is_hello =
307                 sd_bus_message_is_method_call(m, "org.freedesktop.DBus", "Hello") &&
308                 streq_ptr(m->destination, "org.freedesktop.DBus");
309
310         if (!is_hello) {
311
312                 if (*got_hello)
313                         return 0;
314
315                 log_error("First packet isn't hello (it's %s.%s), aborting.", m->interface, m->member);
316                 return -EIO;
317         }
318
319         if (*got_hello) {
320                 log_error("Got duplicate hello, aborting.");
321                 return -EIO;
322         }
323
324         *got_hello = true;
325
326         if (!a->is_kernel)
327                 return 0;
328
329         r = sd_bus_message_new_method_return(m, &n);
330         if (r < 0) {
331                 log_error("Failed to generate HELLO reply: %s", strerror(-r));
332                 return r;
333         }
334
335         r = sd_bus_message_append(n, "s", a->unique_name);
336         if (r < 0) {
337                 log_error("Failed to append unique name to HELLO reply: %s", strerror(-r));
338                 return r;
339         }
340
341         r = bus_message_append_sender(n, "org.freedesktop.DBus");
342         if (r < 0) {
343                 log_error("Failed to append sender to HELLO reply: %s", strerror(-r));
344                 return r;
345         }
346
347         r = bus_seal_synthetic_message(b, n);
348         if (r < 0) {
349                 log_error("Failed to seal HELLO reply: %s", strerror(-r));
350                 return r;
351         }
352
353         r = sd_bus_send(b, n, NULL);
354         if (r < 0) {
355                 log_error("Failed to send HELLO reply: %s", strerror(-r));
356                 return r;
357         }
358
359         n = sd_bus_message_unref(n);
360         r = sd_bus_message_new_signal(
361                         b,
362                         "/org/freedesktop/DBus",
363                         "org.freedesktop.DBus",
364                         "NameAcquired",
365                         &n);
366         if (r < 0) {
367                 log_error("Failed to allocate initial NameAcquired message: %s", strerror(-r));
368                 return r;
369         }
370
371         r = sd_bus_message_append(n, "s", a->unique_name);
372         if (r < 0) {
373                 log_error("Failed to append unique name to NameAcquired message: %s", strerror(-r));
374                 return r;
375         }
376
377         r = bus_message_append_sender(n, "org.freedesktop.DBus");
378         if (r < 0) {
379                 log_error("Failed to append sender to NameAcquired message: %s", strerror(-r));
380                 return r;
381         }
382
383         r = bus_seal_synthetic_message(b, n);
384         if (r < 0) {
385                 log_error("Failed to seal NameAcquired message: %s", strerror(-r));
386                 return r;
387         }
388
389         r = sd_bus_send(b, n, NULL);
390         if (r < 0) {
391                 log_error("Failed to send NameAcquired message: %s", strerror(-r));
392                 return r;
393         }
394
395         return 1;
396 }
397
398 int main(int argc, char *argv[]) {
399
400         _cleanup_bus_unref_ sd_bus *a = NULL, *b = NULL;
401         sd_id128_t server_id;
402         int r, in_fd, out_fd;
403         bool got_hello = false;
404         bool is_unix;
405         struct ucred ucred = {};
406         _cleanup_free_ char *peersec = NULL;
407
408         log_set_target(LOG_TARGET_JOURNAL_OR_KMSG);
409         log_parse_environment();
410         log_open();
411
412         r = parse_argv(argc, argv);
413         if (r <= 0)
414                 goto finish;
415
416         r = sd_listen_fds(0);
417         if (r == 0) {
418                 in_fd = STDIN_FILENO;
419                 out_fd = STDOUT_FILENO;
420         } else if (r == 1) {
421                 in_fd = SD_LISTEN_FDS_START;
422                 out_fd = SD_LISTEN_FDS_START;
423         } else {
424                 log_error("Illegal number of file descriptors passed");
425                 goto finish;
426         }
427
428         is_unix =
429                 sd_is_socket(in_fd, AF_UNIX, 0, 0) > 0 &&
430                 sd_is_socket(out_fd, AF_UNIX, 0, 0) > 0;
431
432         if (is_unix) {
433                 getpeercred(in_fd, &ucred);
434                 getpeersec(in_fd, &peersec);
435         }
436
437         r = sd_bus_new(&a);
438         if (r < 0) {
439                 log_error("Failed to allocate bus: %s", strerror(-r));
440                 goto finish;
441         }
442
443         r = sd_bus_set_address(a, arg_address);
444         if (r < 0) {
445                 log_error("Failed to set address to connect to: %s", strerror(-r));
446                 goto finish;
447         }
448
449         r = sd_bus_negotiate_fds(a, is_unix);
450         if (r < 0) {
451                 log_error("Failed to set FD negotiation: %s", strerror(-r));
452                 goto finish;
453         }
454
455         if (ucred.pid > 0) {
456                 a->fake_creds.pid = ucred.pid;
457                 a->fake_creds.uid = ucred.uid;
458                 a->fake_creds.gid = ucred.gid;
459                 a->fake_creds_valid = true;
460         }
461
462         if (peersec) {
463                 a->fake_label = peersec;
464                 peersec = NULL;
465         }
466
467         a->manual_peer_interface = true;
468
469         r = sd_bus_start(a);
470         if (r < 0) {
471                 log_error("Failed to start bus client: %s", strerror(-r));
472                 goto finish;
473         }
474
475         r = sd_bus_get_server_id(a, &server_id);
476         if (r < 0) {
477                 log_error("Failed to get server ID: %s", strerror(-r));
478                 goto finish;
479         }
480
481         r = sd_bus_new(&b);
482         if (r < 0) {
483                 log_error("Failed to allocate bus: %s", strerror(-r));
484                 goto finish;
485         }
486
487         r = sd_bus_set_fd(b, in_fd, out_fd);
488         if (r < 0) {
489                 log_error("Failed to set fds: %s", strerror(-r));
490                 goto finish;
491         }
492
493         r = sd_bus_set_server(b, 1, server_id);
494         if (r < 0) {
495                 log_error("Failed to set server mode: %s", strerror(-r));
496                 goto finish;
497         }
498
499         r = sd_bus_negotiate_fds(b, is_unix);
500         if (r < 0) {
501                 log_error("Failed to set FD negotiation: %s", strerror(-r));
502                 goto finish;
503         }
504
505         r = sd_bus_set_anonymous(b, true);
506         if (r < 0) {
507                 log_error("Failed to set anonymous authentication: %s", strerror(-r));
508                 goto finish;
509         }
510
511         b->manual_peer_interface = true;
512
513         r = sd_bus_start(b);
514         if (r < 0) {
515                 log_error("Failed to start bus client: %s", strerror(-r));
516                 goto finish;
517         }
518
519         r = rename_service(a, b);
520         if (r < 0)
521                 log_debug("Failed to rename process: %s", strerror(-r));
522
523         if (a->is_kernel) {
524                 _cleanup_free_ char *match = NULL;
525                 const char *unique;
526
527                 r = sd_bus_get_unique_name(a, &unique);
528                 if (r < 0) {
529                         log_error("Failed to get unique name: %s", strerror(-r));
530                         goto finish;
531                 }
532
533                 match = strjoin("type='signal',"
534                                 "sender='org.freedesktop.DBus',"
535                                 "path='/org/freedesktop/DBus',"
536                                 "interface='org.freedesktop.DBus',"
537                                 "member='NameOwnerChanged',"
538                                 "arg1='",
539                                 unique,
540                                 "'",
541                                 NULL);
542                 if (!match) {
543                         log_oom();
544                         goto finish;
545                 }
546
547                 r = sd_bus_add_match(a, match, NULL, NULL);
548                 if (r < 0) {
549                         log_error("Failed to add match for NameLost: %s", strerror(-r));
550                         goto finish;
551                 }
552
553                 free(match);
554                 match = strjoin("type='signal',"
555                                 "sender='org.freedesktop.DBus',"
556                                 "path='/org/freedesktop/DBus',"
557                                 "interface='org.freedesktop.DBus',"
558                                 "member='NameOwnerChanged',"
559                                 "arg2='",
560                                 unique,
561                                 "'",
562                                 NULL);
563                 if (!match) {
564                         log_oom();
565                         goto finish;
566                 }
567
568                 r = sd_bus_add_match(a, match, NULL, NULL);
569                 if (r < 0) {
570                         log_error("Failed to add match for NameAcquired: %s", strerror(-r));
571                         goto finish;
572                 }
573         }
574
575         for (;;) {
576                 _cleanup_bus_message_unref_ sd_bus_message *m = NULL;
577                 int events_a, events_b, fd;
578                 uint64_t timeout_a, timeout_b, t;
579                 struct timespec _ts, *ts;
580                 struct pollfd *pollfd;
581                 int k;
582
583                 if (got_hello) {
584                         r = sd_bus_process(a, &m);
585                         if (r < 0) {
586                                 /* treat 'connection reset by peer' as clean exit condition */
587                                 if (r == -ECONNRESET)
588                                         r = 0;
589                                 else
590                                         log_error("Failed to process bus a: %s", strerror(-r));
591
592                                 goto finish;
593                         }
594
595                         if (m) {
596                                 /* We officially got EOF, let's quit */
597                                 if (sd_bus_message_is_signal(m, "org.freedesktop.DBus.Local", "Disconnected")) {
598                                         r = 0;
599                                         goto finish;
600                                 }
601
602                                 k = synthesize_name_acquired(a, b, m);
603                                 if (k < 0) {
604                                         r = k;
605                                         log_error("Failed to synthesize message: %s", strerror(-r));
606                                         goto finish;
607                                 }
608
609                                 k = sd_bus_send(b, m, NULL);
610                                 if (k < 0) {
611                                         r = k;
612                                         log_error("Failed to send message: %s", strerror(-r));
613                                         goto finish;
614                                 }
615                         }
616
617                         if (r > 0)
618                                 continue;
619                 }
620
621                 r = sd_bus_process(b, &m);
622                 if (r < 0) {
623                         /* treat 'connection reset by peer' as clean exit condition */
624                         if (r == -ECONNRESET)
625                                 r = 0;
626                         else
627                                 log_error("Failed to process bus b: %s", strerror(-r));
628
629                         goto finish;
630                 }
631
632                 if (m) {
633                         /* We officially got EOF, let's quit */
634                         if (sd_bus_message_is_signal(m, "org.freedesktop.DBus.Local", "Disconnected")) {
635                                 r = 0;
636                                 goto finish;
637                         }
638
639                         k = process_hello(a, b, m, &got_hello);
640                         if (k < 0) {
641                                 r = k;
642                                 goto finish;
643                         }
644
645                         if (k > 0)
646                                 r = k;
647                         else {
648                                 k = process_policy(a, b, m);
649                                 if (k < 0) {
650                                         r = k;
651                                         goto finish;
652                                 }
653
654                                 k = sd_bus_send(a, m, NULL);
655                                 if (k < 0) {
656                                         r = k;
657                                         log_error("Failed to send message: %s", strerror(-r));
658                                         goto finish;
659                                 }
660                         }
661                 }
662
663                 if (r > 0)
664                         continue;
665
666                 fd = sd_bus_get_fd(a);
667                 if (fd < 0) {
668                         log_error("Failed to get fd: %s", strerror(-r));
669                         goto finish;
670                 }
671
672                 events_a = sd_bus_get_events(a);
673                 if (events_a < 0) {
674                         log_error("Failed to get events mask: %s", strerror(-r));
675                         goto finish;
676                 }
677
678                 r = sd_bus_get_timeout(a, &timeout_a);
679                 if (r < 0) {
680                         log_error("Failed to get timeout: %s", strerror(-r));
681                         goto finish;
682                 }
683
684                 events_b = sd_bus_get_events(b);
685                 if (events_b < 0) {
686                         log_error("Failed to get events mask: %s", strerror(-r));
687                         goto finish;
688                 }
689
690                 r = sd_bus_get_timeout(b, &timeout_b);
691                 if (r < 0) {
692                         log_error("Failed to get timeout: %s", strerror(-r));
693                         goto finish;
694                 }
695
696                 t = timeout_a;
697                 if (t == (uint64_t) -1 || (timeout_b != (uint64_t) -1 && timeout_b < timeout_a))
698                         t = timeout_b;
699
700                 if (t == (uint64_t) -1)
701                         ts = NULL;
702                 else {
703                         usec_t nw;
704
705                         nw = now(CLOCK_MONOTONIC);
706                         if (t > nw)
707                                 t -= nw;
708                         else
709                                 t = 0;
710
711                         ts = timespec_store(&_ts, t);
712                 }
713
714                 pollfd = (struct pollfd[3]) {
715                         {.fd = fd,     .events = events_a,           },
716                         {.fd = in_fd,  .events = events_b & POLLIN,  },
717                         {.fd = out_fd, .events = events_b & POLLOUT, }
718                 };
719
720                 r = ppoll(pollfd, 3, ts, NULL);
721                 if (r < 0) {
722                         log_error("ppoll() failed: %m");
723                         goto finish;
724                 }
725         }
726
727         r = 0;
728
729 finish:
730         sd_bus_flush(a);
731         sd_bus_flush(b);
732
733         return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
734 }