aboutsummaryrefslogtreecommitdiffstats
path: root/src/faudit/dbus.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/faudit/dbus.c')
-rw-r--r--src/faudit/dbus.c131
1 files changed, 0 insertions, 131 deletions
diff --git a/src/faudit/dbus.c b/src/faudit/dbus.c
deleted file mode 100644
index 389504fb8..000000000
--- a/src/faudit/dbus.c
+++ /dev/null
@@ -1,131 +0,0 @@
1/*
2 * Copyright (C) 2014-2021 Firejail Authors
3 *
4 * This file is part of firejail project
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19*/
20#include "faudit.h"
21#include "../include/rundefs.h"
22#include <stdarg.h>
23#include <sys/socket.h>
24#include <sys/un.h>
25
26// return 0 if the connection is possible
27int check_unix(const char *sockfile) {
28 assert(sockfile);
29 int rv = -1;
30
31 // open socket
32 int sock = socket(AF_UNIX, SOCK_STREAM, 0);
33 if (sock == -1)
34 return rv;
35
36 // connect
37 struct sockaddr_un remote;
38 memset(&remote, 0, sizeof(struct sockaddr_un));
39 remote.sun_family = AF_UNIX;
40 strncpy(remote.sun_path, sockfile, sizeof(remote.sun_path) - 1);
41 int len = strlen(remote.sun_path) + sizeof(remote.sun_family);
42 if (*sockfile == '@')
43 remote.sun_path[0] = '\0';
44 if (connect(sock, (struct sockaddr *)&remote, len) == 0)
45 rv = 0;
46
47 close(sock);
48 return rv;
49}
50
51static char *test_dbus_env(char *env_var_name) {
52 // check the session bus
53 char *str = getenv(env_var_name);
54 char *found = NULL;
55 if (str) {
56 int rv = 0;
57 char *bus = strdup(str);
58 if (!bus)
59 errExit("strdup");
60 char *sockfile;
61 if ((sockfile = strstr(bus, "unix:abstract=")) != NULL) {
62 sockfile += 13;
63 *sockfile = '@';
64 char *ptr = strchr(sockfile, ',');
65 if (ptr)
66 *ptr = '\0';
67 rv = check_unix(sockfile);
68 *sockfile = '@';
69 if (rv == 0)
70 printf("MAYBE: D-Bus socket %s is available\n", sockfile);
71 else if (rv == -1)
72 printf("GOOD: cannot connect to D-Bus socket %s\n", sockfile);
73 }
74 else if ((sockfile = strstr(bus, "unix:path=")) != NULL) {
75 sockfile += 10;
76 char *ptr = strchr(sockfile, ',');
77 if (ptr)
78 *ptr = '\0';
79 rv = check_unix(sockfile);
80 if (rv == 0) {
81 if (strcmp(RUN_DBUS_USER_SOCKET, sockfile) == 0 ||
82 strcmp(RUN_DBUS_SYSTEM_SOCKET, sockfile) == 0) {
83 printf("GOOD: D-Bus filtering is active on %s\n", sockfile);
84 } else {
85 printf("MAYBE: D-Bus socket %s is available\n", sockfile);
86 }
87 }
88 else if (rv == -1)
89 printf("GOOD: cannot connect to D-Bus socket %s\n", sockfile);
90 found = strdup(sockfile);
91 if (!found)
92 errExit("strdup");
93 }
94 else if (strstr(bus, "tcp:host=") != NULL)
95 printf("UGLY: %s bus configured for TCP communication.\n", env_var_name);
96 else
97 printf("GOOD: cannot find a %s D-Bus socket\n", env_var_name);
98 free(bus);
99 }
100 else
101 printf("MAYBE: %s environment variable not configured.\n", env_var_name);
102 return found;
103}
104
105static void test_default_socket(const char *found, const char *format, ...) {
106 va_list ap;
107 va_start(ap, format);
108 char *sockfile;
109 if (vasprintf(&sockfile, format, ap) == -1)
110 errExit("vasprintf");
111 va_end(ap);
112 if (found != NULL && strcmp(found, sockfile) == 0)
113 goto end;
114 int rv = check_unix(sockfile);
115 if (rv == 0)
116 printf("MAYBE: D-Bus socket %s is available\n", sockfile);
117end:
118 free(sockfile);
119}
120
121void dbus_test(void) {
122 char *found_user = test_dbus_env("DBUS_SESSION_BUS_ADDRESS");
123 test_default_socket(found_user, "/run/user/%d/bus", (int) getuid());
124 test_default_socket(found_user, "/run/user/%d/dbus/user_bus_socket", (int) getuid());
125 if (found_user != NULL)
126 free(found_user);
127 char *found_system = test_dbus_env("DBUS_SYSTEM_BUS_ADDRESS");
128 test_default_socket(found_system, "/run/dbus/system_bus_socket");
129 if (found_system != NULL)
130 free(found_system);
131}