forked from flatpak/xdg-dbus-proxy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dbus-proxy.c
419 lines (352 loc) · 10.8 KB
/
dbus-proxy.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
/*
* Copyright © 2015 Red Hat, Inc
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
*
* Authors:
* Alexander Larsson <alexl@redhat.com>
*/
#include "config.h"
#include <string.h>
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <errno.h>
#include "flatpak-proxy.h"
static const char *argv0;
static GList *proxies;
static int sync_fd = -1;
static void usage (int ecode, FILE *out) G_GNUC_NORETURN;
static void
usage (int ecode, FILE *out)
{
fprintf (out, "usage: %s [OPTIONS...] [ADDRESS PATH [OPTIONS...] ...]\n\n", argv0);
fprintf (out,
"Options:\n"
" --help Print this help\n"
" --version Print version\n"
" --fd=FD Stop when FD is closed\n"
" --args=FD Read arguments from FD\n\n"
"Proxy Options:\n"
" --filter Enable filtering\n"
" --log Turn on logging\n"
" --sloppy-names Report name changes for unique names\n"
" --see=NAME Set 'see' policy for NAME\n"
" --talk=NAME Set 'talk' policy for NAME\n"
" --own=NAME Set 'own' policy for NAME\n"
" --call=NAME=RULE Set RULE for calls on NAME\n"
" --broadcast=NAME=RULE Set RULE for broadcasts from NAME\n"
);
exit (ecode);
}
static GBytes *
fd_readall_bytes (int fd,
GError **error)
{
const guint maxreadlen = 4096;
struct stat stbuf;
gsize buf_allocated;
g_autofree guint8* buf = NULL;
gsize buf_size = 0;
if (TEMP_FAILURE_RETRY (fstat (fd, &stbuf)) != 0)
{
int errsv = errno;
g_set_error_literal (error,
G_IO_ERROR,
g_io_error_from_errno (errsv),
g_strerror (errsv));
return NULL;
}
if (S_ISREG (stbuf.st_mode) && stbuf.st_size > 0)
buf_allocated = stbuf.st_size;
else
buf_allocated = 16;
buf = g_malloc (buf_allocated);
while (TRUE)
{
gsize readlen = MIN (buf_allocated - buf_size, maxreadlen);
gssize bytes_read;
do
bytes_read = read (fd, buf + buf_size, readlen);
while (G_UNLIKELY (bytes_read == -1 && errno == EINTR));
if (G_UNLIKELY (bytes_read == -1))
{
int errsv = errno;
g_set_error_literal (error,
G_IO_ERROR,
g_io_error_from_errno (errsv),
g_strerror (errsv));
return NULL;
}
if (bytes_read == 0)
break;
buf_size += bytes_read;
if (buf_allocated - buf_size < maxreadlen)
buf = g_realloc (buf, buf_allocated *= 2);
}
return g_bytes_new_take (g_steal_pointer (&buf), buf_size);
}
static void
add_args (GBytes *bytes,
GPtrArray *args,
int pos)
{
gsize data_len, remainder_len;
const guchar *data = g_bytes_get_data (bytes, &data_len);
guchar *s;
const guchar *remainder;
remainder = data;
remainder_len = data_len;
s = memchr (remainder, 0, remainder_len);
while (s)
{
gsize len = s - remainder;
char *arg = g_strndup ((char *) remainder, len);
g_ptr_array_insert (args, pos++, arg);
remainder = s + 1;
remainder_len -= len + 1;
s = memchr (remainder, 0, remainder_len);
}
if (remainder_len)
{
char *arg = g_strndup ((char *) remainder, remainder_len);
g_ptr_array_insert (args, pos++, arg);
}
}
static gboolean
parse_generic_args (GPtrArray *args, int *args_i)
{
const char *arg = g_ptr_array_index (args, *args_i);
if (strcmp (arg, "--help") == 0)
{
usage (EXIT_SUCCESS, stdout);
}
else if (strcmp (arg, "--version") == 0)
{
g_print ("%s %s\n", PACKAGE_NAME, PACKAGE_VERSION);
exit (EXIT_SUCCESS);
}
else if (g_str_has_prefix (arg, "--fd="))
{
const char *fd_s = arg + strlen ("--fd=");
char *endptr;
int fd;
fd = strtol (fd_s, &endptr, 10);
if (fd < 0 || endptr == fd_s || *endptr != 0)
{
g_printerr ("Invalid fd %s\n", fd_s);
return FALSE;
}
sync_fd = fd;
*args_i += 1;
return TRUE;
}
else if (g_str_has_prefix (arg, "--args="))
{
const char *fd_s = arg + strlen ("--args=");
char *endptr;
int fd;
g_autoptr(GBytes) data = NULL;
g_autoptr(GError) error = NULL;
fd = strtol (fd_s, &endptr, 10);
if (fd < 0 || endptr == fd_s || *endptr != 0)
{
g_printerr ("Invalid --args fd %s\n", fd_s);
return FALSE;
}
data = fd_readall_bytes (fd, &error);
if (data == NULL)
{
g_printerr ("Failed to load --args: %s\n", error->message);
return FALSE;
}
*args_i += 1;
add_args (data, args, *args_i);
return TRUE;
}
else
{
g_printerr ("Unknown argument %s\n", arg);
return FALSE;
}
}
static gboolean
start_proxy (GPtrArray *args, int *args_i)
{
g_autoptr(FlatpakProxy) proxy = NULL;
g_autoptr(GError) error = NULL;
const char *bus_address, *socket_path;
const char *arg;
if (*args_i >= args->len || ((char *) g_ptr_array_index (args, *args_i))[0] == '-')
{
g_printerr ("No bus address given\n");
return FALSE;
}
bus_address = g_ptr_array_index (args, *args_i);
*args_i += 1;
if (*args_i >= args->len || ((char *) g_ptr_array_index (args, *args_i))[0] == '-')
{
g_printerr ("No socket path given\n");
return FALSE;
}
socket_path = g_ptr_array_index (args, *args_i);
*args_i += 1;
proxy = flatpak_proxy_new (bus_address, socket_path);
while (*args_i < args->len)
{
arg = g_ptr_array_index (args, *args_i);
if (arg[0] != '-')
break;
if (g_str_has_prefix (arg, "--see=") ||
g_str_has_prefix (arg, "--talk=") ||
g_str_has_prefix (arg, "--own="))
{
FlatpakPolicy policy = FLATPAK_POLICY_SEE;
g_autofree char *name = g_strdup (strchr (arg, '=') + 1);
gboolean wildcard = FALSE;
if (arg[2] == 't')
policy = FLATPAK_POLICY_TALK;
else if (arg[2] == 'o')
policy = FLATPAK_POLICY_OWN;
if (g_str_has_suffix (name, ".*"))
{
name[strlen (name) - 2] = 0;
wildcard = TRUE;
}
if (name[0] == ':' || !g_dbus_is_name (name))
{
g_printerr ("'%s' is not a valid dbus name\n", name);
return FALSE;
}
flatpak_proxy_add_policy (proxy, name, wildcard, policy);
*args_i += 1;
}
else if (g_str_has_prefix (arg, "--call=") ||
g_str_has_prefix (arg, "--broadcast="))
{
g_autofree char *rest = g_strdup (strchr (arg, '=') + 1);
char *name = rest;
char *rule;
char *name_end = strchr (rest, '=');
gboolean wildcard = FALSE;
if (name_end == NULL)
{
g_printerr ("'%s' is not a valid name + rule\n", rest);
return FALSE;
}
*name_end = 0;
rule = name_end + 1;
if (g_str_has_suffix (name, ".*"))
{
name[strlen (name) - 2] = 0;
wildcard = TRUE;
}
if (g_str_has_prefix (arg, "--call="))
flatpak_proxy_add_call_rule (proxy, name, wildcard, rule);
else
flatpak_proxy_add_broadcast_rule (proxy, name, wildcard, rule);
*args_i += 1;
}
else if (g_str_equal (arg, "--log"))
{
flatpak_proxy_set_log_messages (proxy, TRUE);
*args_i += 1;
}
else if (g_str_equal (arg, "--filter"))
{
flatpak_proxy_set_filter (proxy, TRUE);
*args_i += 1;
}
else if (g_str_equal (arg, "--sloppy-names"))
{
/* This means we're reporting the name changes for all unique names,
which is needed for the a11y bus */
flatpak_proxy_set_sloppy_names (proxy, TRUE);
*args_i += 1;
}
else
{
if (!parse_generic_args (args, args_i))
return FALSE;
}
}
if (!flatpak_proxy_start (proxy, &error))
{
g_printerr ("Failed to start proxy for %s: %s\n", bus_address, error->message);
return FALSE;
}
proxies = g_list_prepend (proxies, g_object_ref (proxy));
return TRUE;
}
static gboolean
sync_closed_cb (GIOChannel *source,
GIOCondition condition,
gpointer data)
{
GList *l;
for (l = proxies; l != NULL; l = l->next)
flatpak_proxy_stop (FLATPAK_PROXY (l->data));
exit (0);
return TRUE;
}
int
main (int argc, const char *argv[])
{
GMainLoop *service_loop;
int i, args_i;
g_autoptr(GPtrArray) args = g_ptr_array_new_with_free_func (g_free);
argv0 = argv[0];
if (argc == 1)
usage (EXIT_FAILURE, stderr);
for (i = 1; i < argc; i++)
g_ptr_array_add (args, g_strdup ((char *) argv[i]));
args_i = 0;
while (args_i < args->len)
{
const char *arg = g_ptr_array_index (args, args_i);
if (arg[0] == '-')
{
if (!parse_generic_args (args, &args_i))
return EXIT_FAILURE;
}
else
{
if (!start_proxy (args, &args_i))
return EXIT_FAILURE;
}
}
if (proxies == NULL)
{
g_printerr ("No proxies specified\n");
return EXIT_FAILURE;
}
if (sync_fd >= 0)
{
ssize_t written;
GIOChannel *sync_channel;
written = write (sync_fd, "x", 1);
if (written != 1)
g_warning ("Can't write to sync socket");
sync_channel = g_io_channel_unix_new (sync_fd);
g_io_add_watch (sync_channel, G_IO_ERR | G_IO_HUP,
sync_closed_cb, NULL);
}
service_loop = g_main_loop_new (NULL, FALSE);
g_main_loop_run (service_loop);
g_main_loop_unref (service_loop);
return EXIT_SUCCESS;
}