-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathflatpak-proxy.c
3243 lines (2734 loc) · 99.8 KB
/
flatpak-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
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* 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 <[email protected]>
*/
#include "config.h"
#include <unistd.h>
#include <string.h>
#include "flatpak-proxy.h"
#include <gio/gunixsocketaddress.h>
#include <gio/gunixconnection.h>
#include <gio/gunixfdmessage.h>
#if !GLIB_CHECK_VERSION(2, 58, 0)
# define G_SOURCE_FUNC(f) ((GSourceFunc) (void (*)(void)) (f))
#endif
/**
* The proxy listens to a unix domain socket, and for each new
* connection it opens up a new connection to a specified dbus bus
* address (typically the session bus) and forwards data between the
* two. During the authentication phase all data is forwarded as
* received, and additionally for the first 1 byte zero we also send
* the proxy credentials to the bus.
*
* Once the connection is authenticated there are two modes, filtered
* and unfiltered. In the unfiltered mode we just send all messages on
* as we receive, but in the in the filtering mode we apply a policy,
* which is similar to the policy supported by kdbus.
*
* The policy for the filtering consists of a mapping from well-known
* names to a policy that is either SEE, TALK or OWN. The default
* initial policy is that the the user is only allowed to TALK to the
* bus itself (org.freedesktop.DBus, or no destination specified), and
* TALK to its own unique id. All other clients are invisible. The
* well-known names can be specified exactly, or as a arg0namespace
* wildcards like "org.foo.*" which matches "org.foo", "org.foo.bar",
* and "org.foo.bar.gazonk", but not "org.foobar".
*
* Polices are specified for well-known names, but they also affect
* the owner of that name, so that the policy for a unique id is the
* superset of the polices for all the names it owns. Due to technical
* reasons the policy for a unique name is "sticky", in that we keep
* the highest policy granted by a once-owned name even when the client
* releases that name. This is impossible to avoid in a race-free way
* in a proxy. But this is rarely a problem in practice, as clients
* rarely release names and stay on the bus.
*
* Here is a description of the policy levels:
* (all policy levels also imply the ones before it)
*
* SEE:
* The name/id is visible in the ListNames reply
* The name/id is visible in the ListActivatableNames reply
* You can call GetNameOwner on the name
* You can call NameHasOwner on the name
* You see NameOwnerChanged signals on the name
* You see NameOwnerChanged signals on the id when the client disconnects
* You can call the GetXXX methods on the name/id to get e.g. the peer pid
* You get AccessDenied rather than NameHasNoOwner when sending messages to the name/id
*
* TALK:
* You can send any method calls and signals to the name/id
* You will receive broadcast signals from the name/id (if you have a match rule for them)
* You can call StartServiceByName on the name
*
* OWN:
* You are allowed to call RequestName/ReleaseName/ListQueuedOwners on the name.
*
* Additionally, there can be more detailed filters installed that
* limits what messages you can send to and receive broadcasts from.
* However, if you can *ever* call or recieve broadcasts from a name (even if
* filtered to some subset of paths/interfaces) its visibility is considered
* to be as TALK.
*
* The policy applies only to outgoing signals and method calls and
* incoming broadcast. All replies (errors or method returns) are
* allowed once for an outstanding method call, and never
* otherwise.
*
* Every peer on the bus is considered priviledged, and we thus trust
* it and don't apply any filtering (except broadcasts). So we rely on
* similar proxies to be running for all untrusted clients. Any such
* priviledged peer is allowed to send method call or unicast signal
* messages to the proxied client. Once another peer
* sends you a message the unique id of that peer is now made visible
* (policy SEE) to the proxied client, allowing the client to track
* caller lifetimes via NameOwnerChanged signals.
*
* Differences to kdbus custom endpoint policies:
*
* * The proxy will return the credentials (like pid) of the proxy,
* not the real client.
*
* * Policy is not dropped when a peer releases a name.
*
* * Peers that call you become visible (SEE) (and get signals for
* NameOwnerChange disconnect) In kdbus currently custom endpoints
* never get NameOwnerChange signals for unique ids, but this is
* problematic as it disallows a services to track lifetimes of its
* clients.
*
* Mode of operation
*
* Once authenticated we receive incoming messages one at a time,
* and then we demarshal the message headers to make routing decisions.
* This means we trust the bus to do message format validation, etc.
* (because we don't parse the body). Also we assume that the bus verifies
* reply_serials, i.e. that a reply can only be sent once and by the real
* recipient of an previously sent method call.
*
* Serial numbers larger than MAX_CLIENT_SERIAL reserved for messages created by the
* proxy itself (fake messages). This limits the possible values of serials
* available to the client to the value of MAX_CLIENT_SERIAL. Versions
* older than 0.1.6 required monotonically increasing serials instead. This
* mechanism was dropped since it caused regular issues with multiple D-Bus
* clients.
*
* In order to track the ownership of the allowed names we hijack the
* connection after the initial Hello message, sending AddMatch,
* ListNames and GetNameOwner messages to get a proper view of who
* owns the names atm. Then we listen to NameOwnerChanged events for
* further updates. This causes some serials abow MAX_CLIENT_SERIAL to be
* used for "fake messages".
*
* After that the filter is strictly passive, in that we never
* construct our own requests. For each message received from the
* client we look up the type and the destination policy and make a
* decision to either pass it on as is, rewrite it before passing on
* (for instance ListName replies), drop it completely, or return a
* made-up reply/error to the sender.
*
* When returning a made-up reply we replace the actual message with a
* Ping request to the bus with the same serial and replace the resulting
* reply with the made up reply (with the serial from the Ping reply).
* This means we keep the strict message ordering and serial numbers of
* the bus.
*
* Policy is applied to unique ids in the following cases:
* * During startup we call AddWatch for signals on all policy names
* and wildcards (using arg0namespace) so that we get NameOwnerChanged
* events which we use to update the unique id policies.
* * During startup we create synthetic GetNameOwner requests for all
* normal policy names, and if there are wildcarded names we create a
* synthetic ListNames request and use the results of that to do further
* GetNameOwner for the existing names matching the wildcards. When we get
* replies for the GetNameOwner requests the unique id policy is updated.
* * When we get a method call from a unique id, it gets SEE
* * When we get a reply to the initial Hello request we give
* our own assigned unique id policy TALK.
*
* There is also a mode called "sloppy-names" where you automatically get
* SEE access to all the unique names on the bus. This is used only for
* the a11y bus.
*
* All messages sent to the bus itself are fully demarshalled
* and handled on a per-method basis:
*
* Hello, AddMatch, RemoveMatch, GetId: Always allowed
* ListNames, ListActivatableNames: Always allowed, but response filtered
* UpdateActivationEnvironment, BecomeMonitor: Always denied
* RequestName, ReleaseName, ListQueuedOwners: Only allowed if arg0 is a name with policy OWN
* NameHasOwner, GetNameOwner: Only pass on if arg0 is a name with policy SEE, otherwise return synthetic reply
* StartServiceByName: Only allowed if policy TALK on arg0
* GetConnectionUnixProcessID, GetConnectionCredentials,
* GetAdtAuditSessionData, GetConnectionSELinuxSecurityContext,
* GetConnectionUnixUser: Allowed if policy SEE on arg0
*
* For unknown methods, we return a synthetic error.
*/
typedef struct FlatpakProxyClient FlatpakProxyClient;
#define FIND_AUTH_END_CONTINUE -1
#define FIND_AUTH_END_ABORT -2
#define AUTH_LINE_SENTINEL "\r\n"
#define AUTH_BEGIN "BEGIN"
// Use a relatively hight number since there are not a lot of fake requests we need to do
#define MAX_CLIENT_SERIAL (G_MAXUINT32 - 65536)
typedef enum {
/* The client has not sent BEGIN yet */
AUTH_WAITING_FOR_BEGIN,
/* The client sent BEGIN, but the server has not yet responded to the auth
messages that the client sent before */
AUTH_WAITING_FOR_BACKLOG,
/* Authentication is fully complete */
AUTH_COMPLETE,
} AuthState;
typedef enum {
EXPECTED_REPLY_NONE,
EXPECTED_REPLY_NORMAL,
EXPECTED_REPLY_HELLO,
EXPECTED_REPLY_FILTER,
EXPECTED_REPLY_FAKE_GET_NAME_OWNER,
EXPECTED_REPLY_FAKE_LIST_NAMES,
EXPECTED_REPLY_LIST_NAMES,
EXPECTED_REPLY_REWRITE,
} ExpectedReplyType;
typedef struct
{
/* During write and message parsing this is the size of the valid data in the buffer.
During reads this is the capacity of the buffer. */
gsize size;
/* Offset to the first writable position (the buffer is full when pos ==
* size) */
gsize pos;
/* Offset to the first byte that hasn't been sent yet */
gsize sent;
int refcount;
gboolean send_credentials;
GList *control_messages;
guchar data[16];
/* data continues here */
} Buffer;
typedef struct
{
Buffer *buffer;
gboolean big_endian;
guchar type;
guchar flags;
guint32 length;
guint32 serial;
const char *path;
const char *interface;
const char *member;
const char *error_name;
const char *destination;
const char *sender;
const char *signature;
gboolean has_reply_serial;
guint32 reply_serial;
guint32 unix_fds;
} Header;
typedef enum {
FILTER_TYPE_CALL = 1 << 0,
FILTER_TYPE_BROADCAST = 1 << 1,
} FilterTypeMask;
#define FILTER_TYPE_ALL (FILTER_TYPE_CALL | FILTER_TYPE_BROADCAST)
typedef struct
{
char *name;
gboolean name_is_subtree;
FlatpakPolicy policy;
/* More detailed filter */
FilterTypeMask types;
char *path;
gboolean path_is_subtree;
char *interface;
char *member;
} Filter;
static void header_free (Header *header);
G_DEFINE_AUTOPTR_CLEANUP_FUNC (Header, header_free)
typedef struct
{
gboolean got_first_byte; /* always true on bus side */
gboolean closed; /* always true on bus side */
FlatpakProxyClient *client;
GSocketConnection *connection;
GSource *in_source;
GSource *out_source;
GBytes *extra_input_data;
Buffer *current_read_buffer;
Buffer header_buffer;
GList *buffers; /* to be sent */
GList *control_messages;
GHashTable *expected_replies;
} ProxySide;
struct FlatpakProxyClient
{
GObject parent;
FlatpakProxy *proxy;
AuthState auth_state;
gsize auth_requests;
gsize auth_replies;
GByteArray *auth_buffer;
ProxySide client_side;
ProxySide bus_side;
/* Filtering data: */
guint32 hello_serial;
guint32 last_fake_serial;
GHashTable *rewrite_reply;
GHashTable *get_owner_reply;
GHashTable *unique_id_policy;
GHashTable *unique_id_owned_names;
};
typedef struct
{
GObjectClass parent_class;
} FlatpakProxyClientClass;
struct FlatpakProxy
{
GSocketService parent;
gboolean log_messages;
GList *clients;
char *socket_path;
char *dbus_address;
gboolean filter;
gboolean sloppy_names;
GHashTable *filters;
};
typedef struct
{
GSocketServiceClass parent_class;
} FlatpakProxyClass;
enum {
PROP_0,
PROP_DBUS_ADDRESS,
PROP_SOCKET_PATH
};
#define FLATPAK_TYPE_PROXY flatpak_proxy_get_type ()
#define FLATPAK_PROXY(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), FLATPAK_TYPE_PROXY, FlatpakProxy))
#define FLATPAK_IS_PROXY(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), FLATPAK_TYPE_PROXY))
#define FLATPAK_TYPE_PROXY_CLIENT flatpak_proxy_client_get_type ()
#define FLATPAK_PROXY_CLIENT(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), FLATPAK_TYPE_PROXY_CLIENT, FlatpakProxyClient))
#define FLATPAK_IS_PROXY_CLIENT(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), FLATPAK_TYPE_PROXY_CLIENT))
GType flatpak_proxy_client_get_type (void);
G_DEFINE_TYPE (FlatpakProxy, flatpak_proxy, G_TYPE_SOCKET_SERVICE)
G_DEFINE_TYPE (FlatpakProxyClient, flatpak_proxy_client, G_TYPE_OBJECT)
static void start_reading (ProxySide *side);
static void stop_reading (ProxySide *side);
static void
string_list_free (GList *filters)
{
g_list_free_full (filters, (GDestroyNotify) g_free);
}
static void
buffer_unref (Buffer *buffer)
{
g_assert (buffer->refcount > 0);
buffer->refcount--;
if (buffer->refcount == 0)
{
g_list_free_full (buffer->control_messages, g_object_unref);
g_free (buffer);
}
}
static Buffer *
buffer_ref (Buffer *buffer)
{
g_assert (buffer->refcount > 0);
buffer->refcount++;
return buffer;
}
static void
free_side (ProxySide *side)
{
g_clear_object (&side->connection);
g_clear_pointer (&side->extra_input_data, g_bytes_unref);
g_list_free_full (side->buffers, (GDestroyNotify) buffer_unref);
g_list_free_full (side->control_messages, (GDestroyNotify) g_object_unref);
if (side->in_source)
g_source_destroy (side->in_source);
if (side->out_source)
g_source_destroy (side->out_source);
g_hash_table_destroy (side->expected_replies);
}
static void
flatpak_proxy_client_finalize (GObject *object)
{
FlatpakProxyClient *client = FLATPAK_PROXY_CLIENT (object);
client->proxy->clients = g_list_remove (client->proxy->clients, client);
g_clear_object (&client->proxy);
g_byte_array_free (client->auth_buffer, TRUE);
g_hash_table_destroy (client->rewrite_reply);
g_hash_table_destroy (client->get_owner_reply);
g_hash_table_destroy (client->unique_id_policy);
g_hash_table_destroy (client->unique_id_owned_names);
free_side (&client->client_side);
free_side (&client->bus_side);
G_OBJECT_CLASS (flatpak_proxy_client_parent_class)->finalize (object);
}
static void
flatpak_proxy_client_class_init (FlatpakProxyClientClass *klass)
{
GObjectClass *object_class = G_OBJECT_CLASS (klass);
object_class->finalize = flatpak_proxy_client_finalize;
}
static void
init_side (FlatpakProxyClient *client, ProxySide *side)
{
side->got_first_byte = (side == &client->bus_side);
side->client = client;
side->header_buffer.size = 16;
side->header_buffer.pos = 0;
side->current_read_buffer = &side->header_buffer;
side->expected_replies = g_hash_table_new (g_direct_hash, g_direct_equal);
}
static void
flatpak_proxy_client_init (FlatpakProxyClient *client)
{
init_side (client, &client->client_side);
init_side (client, &client->bus_side);
client->last_fake_serial = MAX_CLIENT_SERIAL;
client->auth_buffer = g_byte_array_new ();
client->rewrite_reply = g_hash_table_new_full (g_direct_hash, g_direct_equal, NULL, g_object_unref);
client->get_owner_reply = g_hash_table_new_full (g_direct_hash, g_direct_equal, NULL, g_free);
client->unique_id_policy = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
client->unique_id_owned_names = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, (GDestroyNotify) string_list_free);
}
static FlatpakProxyClient *
flatpak_proxy_client_new (FlatpakProxy *proxy, GSocketConnection *connection)
{
FlatpakProxyClient *client;
g_socket_set_blocking (g_socket_connection_get_socket (connection), FALSE);
client = g_object_new (FLATPAK_TYPE_PROXY_CLIENT, NULL);
client->proxy = g_object_ref (proxy);
client->client_side.connection = g_object_ref (connection);
proxy->clients = g_list_prepend (proxy->clients, client);
return client;
}
void
flatpak_proxy_set_filter (FlatpakProxy *proxy,
gboolean filter)
{
proxy->filter = filter;
}
void
flatpak_proxy_set_sloppy_names (FlatpakProxy *proxy,
gboolean sloppy_names)
{
proxy->sloppy_names = sloppy_names;
}
void
flatpak_proxy_set_log_messages (FlatpakProxy *proxy,
gboolean log)
{
proxy->log_messages = log;
}
static void
filter_free (Filter *filter)
{
g_free (filter->name);
g_free (filter->path);
g_free (filter->interface);
g_free (filter->member);
g_free (filter);
}
static void
filter_list_free (GList *filters)
{
g_list_free_full (filters, (GDestroyNotify) filter_free);
}
static Filter *
filter_new (const char *name,
gboolean name_is_subtree,
FlatpakPolicy policy)
{
Filter *filter = g_new0 (Filter, 1);
filter->name = g_strdup (name);
filter->name_is_subtree = name_is_subtree;
filter->policy = policy;
filter->types = FILTER_TYPE_ALL;
return filter;
}
// rules are of the form [*|org.the.interface.[method|*]]|[@/obj/path[/*]]
static Filter *
filter_new_from_rule (const char *name,
gboolean name_is_subtree,
FilterTypeMask types,
const char *rule)
{
Filter *filter;
const char *obj_path_start = NULL;
const char *method_end = NULL;
filter = filter_new (name, name_is_subtree, FLATPAK_POLICY_TALK);
filter->types = types;
obj_path_start = strchr (rule, '@');
if (obj_path_start && obj_path_start[1] != 0)
{
filter->path = g_strdup (obj_path_start + 1);
if (g_str_has_suffix (filter->path, "/*"))
{
filter->path_is_subtree = TRUE;
filter->path[strlen (filter->path) - 2] = 0;
}
}
if (obj_path_start != NULL)
method_end = obj_path_start;
else
method_end = rule + strlen (rule);
if (method_end != rule)
{
if (rule[0] == '*')
{
/* Both interface and method wildcarded */
}
else
{
filter->interface = g_strndup (rule, method_end - rule);
char *dot = strrchr (filter->interface, '.');
if (dot != NULL)
{
*dot = 0;
if (strcmp (dot + 1, "*") != 0)
filter->member = g_strdup (dot + 1);
}
}
}
return filter;
}
static gboolean
filter_matches (Filter *filter,
FilterTypeMask type,
const char *path,
const char *interface,
const char *member)
{
if (filter->policy < FLATPAK_POLICY_TALK ||
(filter->types & type) == 0)
return FALSE;
if (filter->path)
{
if (path == NULL)
return FALSE;
if (filter->path_is_subtree)
{
gsize filter_path_len = strlen (filter->path);
if (strncmp (path, filter->path, filter_path_len) != 0 ||
(path[filter_path_len] != 0 && path[filter_path_len] != '/'))
return FALSE;
}
else if (strcmp (filter->path, path) != 0)
return FALSE;
}
if (filter->interface && g_strcmp0 (filter->interface, interface) != 0)
return FALSE;
if (filter->member && g_strcmp0 (filter->member, member) != 0)
return FALSE;
return TRUE;
}
static gboolean
any_filter_matches (GList *filters,
FilterTypeMask type,
const char *path,
const char *interface,
const char *member)
{
GList *l;
for (l = filters; l != NULL; l = l->next)
{
Filter *filter = l->data;
if (filter_matches (filter, type, path, interface, member))
return TRUE;
}
return FALSE;
}
static void
flatpak_proxy_add_filter (FlatpakProxy *proxy,
Filter *filter)
{
GList *filters, *new_filters;
if (g_hash_table_lookup_extended (proxy->filters,
filter->name,
NULL, (void **) &filters))
{
new_filters = g_list_append (filters, filter);
g_assert (new_filters == filters);
}
else
{
filters = g_list_append (NULL, filter);
g_hash_table_insert (proxy->filters, g_strdup (filter->name), filters);
}
}
void
flatpak_proxy_add_policy (FlatpakProxy *proxy,
const char *name,
gboolean name_is_subtree,
FlatpakPolicy policy)
{
Filter *filter = filter_new (name, name_is_subtree, policy);
flatpak_proxy_add_filter (proxy, filter);
}
void
flatpak_proxy_add_call_rule (FlatpakProxy *proxy,
const char *name,
gboolean name_is_subtree,
const char *rule)
{
Filter *filter = filter_new_from_rule (name, name_is_subtree, FILTER_TYPE_CALL, rule);
flatpak_proxy_add_filter (proxy, filter);
}
void
flatpak_proxy_add_broadcast_rule (FlatpakProxy *proxy,
const char *name,
gboolean name_is_subtree,
const char *rule)
{
Filter *filter = filter_new_from_rule (name, name_is_subtree, FILTER_TYPE_BROADCAST, rule);
flatpak_proxy_add_filter (proxy, filter);
}
static void
flatpak_proxy_finalize (GObject *object)
{
FlatpakProxy *proxy = FLATPAK_PROXY (object);
if (g_socket_service_is_active (G_SOCKET_SERVICE (proxy)))
unlink (proxy->socket_path);
g_assert (proxy->clients == NULL);
g_hash_table_destroy (proxy->filters);
g_free (proxy->socket_path);
g_free (proxy->dbus_address);
G_OBJECT_CLASS (flatpak_proxy_parent_class)->finalize (object);
}
static void
flatpak_proxy_set_property (GObject *object,
guint prop_id,
const GValue *value,
GParamSpec *pspec)
{
FlatpakProxy *proxy = FLATPAK_PROXY (object);
switch (prop_id)
{
case PROP_DBUS_ADDRESS:
proxy->dbus_address = g_value_dup_string (value);
break;
case PROP_SOCKET_PATH:
proxy->socket_path = g_value_dup_string (value);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
}
}
static void
flatpak_proxy_get_property (GObject *object,
guint prop_id,
GValue *value,
GParamSpec *pspec)
{
FlatpakProxy *proxy = FLATPAK_PROXY (object);
switch (prop_id)
{
case PROP_DBUS_ADDRESS:
g_value_set_string (value, proxy->dbus_address);
break;
case PROP_SOCKET_PATH:
g_value_set_string (value, proxy->socket_path);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
}
}
/* Buffer contains a default size of data that is 16 bytes, so that
it can be used on the stack for reading the header. However we
also support passing in sizes smaller that 16, which will allocate
a smaller object than the full Buffer object. This is safe as we
respect the size member, however there is no way for GCC to know this,
so we silence it manually.
*/
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Warray-bounds"
static Buffer *
buffer_new (gsize size, Buffer *old)
{
Buffer *buffer = g_malloc0 (sizeof (Buffer) + size - 16);
buffer->control_messages = NULL;
buffer->size = size;
buffer->refcount = 1;
if (old)
{
buffer->pos = old->pos;
buffer->sent = old->sent;
/* Takes ownership of any old control messages */
buffer->control_messages = old->control_messages;
old->control_messages = NULL;
g_assert (size >= old->size);
memcpy (buffer->data, old->data, old->size);
}
return buffer;
}
#pragma GCC diagnostic pop
static ProxySide *
get_other_side (ProxySide *side)
{
FlatpakProxyClient *client = side->client;
if (side == &client->client_side)
return &client->bus_side;
return &client->client_side;
}
static void
side_closed (ProxySide *side)
{
GSocket *socket, *other_socket;
ProxySide *other_side = get_other_side (side);
if (side->closed)
return;
socket = g_socket_connection_get_socket (side->connection);
g_socket_close (socket, NULL);
side->closed = TRUE;
other_socket = g_socket_connection_get_socket (other_side->connection);
if (!other_side->closed && other_side->buffers == NULL)
{
g_socket_close (other_socket, NULL);
other_side->closed = TRUE;
}
if (other_side->closed)
{
g_object_unref (side->client);
}
else
{
GError *error = NULL;
if (!g_socket_shutdown (other_socket, TRUE, FALSE, &error))
{
g_warning ("Unable to shutdown read side: %s", error->message);
g_error_free (error);
}
}
}
static gboolean
buffer_read (ProxySide *side,
Buffer *buffer,
GSocket *socket)
{
FlatpakProxyClient *client = side->client;
gsize received = 0;
GInputVector v;
GError *error = NULL;
GSocketControlMessage **messages;
int num_messages, i;
if (client->auth_state == AUTH_WAITING_FOR_BACKLOG &&
side == &client->client_side)
return FALSE;
if (side->extra_input_data && client->auth_state == AUTH_COMPLETE)
{
gsize extra_size;
const guchar *extra_bytes = g_bytes_get_data (side->extra_input_data, &extra_size);
g_assert (buffer->size >= buffer->pos);
received = MIN (extra_size, buffer->size - buffer->pos);
memcpy (&buffer->data[buffer->pos], extra_bytes, received);
if (received < extra_size)
{
side->extra_input_data =
g_bytes_new_with_free_func (extra_bytes + received,
extra_size - received,
(GDestroyNotify) g_bytes_unref,
side->extra_input_data);
}
else
{
g_clear_pointer (&side->extra_input_data, g_bytes_unref);
}
}
else if (!side->extra_input_data)
{
gssize res;
int flags = 0;
v.buffer = &buffer->data[buffer->pos];
v.size = buffer->size - buffer->pos;
res = g_socket_receive_message (socket, NULL, &v, 1,
&messages,
&num_messages,
&flags, NULL, &error);
if (res < 0 && g_error_matches (error, G_IO_ERROR, G_IO_ERROR_WOULD_BLOCK))
{
g_error_free (error);
return FALSE;
}
if (res <= 0)
{
if (res != 0)
{
g_debug ("Error reading from socket: %s", error->message);
g_error_free (error);
}
side_closed (side);
return FALSE;
}
/* We now know res is strictly positive */
received = (gsize) res;
for (i = 0; i < num_messages; i++)
buffer->control_messages = g_list_append (buffer->control_messages, messages[i]);
g_free (messages);
}
buffer->pos += received;
return TRUE;
}
static gboolean
buffer_write (ProxySide *side,
Buffer *buffer,
GSocket *socket)
{
gssize res;
GOutputVector v;
GError *error = NULL;
GSocketControlMessage **messages = NULL;
int i, n_messages;
GList *l;
if (buffer->send_credentials &&
G_IS_UNIX_CONNECTION (side->connection))
{
g_assert (buffer->size == 1);
if (!g_unix_connection_send_credentials (G_UNIX_CONNECTION (side->connection),
NULL,
&error))
{
if (g_error_matches (error, G_IO_ERROR, G_IO_ERROR_WOULD_BLOCK))
{
g_error_free (error);
return FALSE;
}
g_warning ("Error writing credentials to socket: %s", error->message);
g_error_free (error);
side_closed (side);
return FALSE;
}
buffer->sent = 1;
return TRUE;
}
n_messages = g_list_length (buffer->control_messages);
messages = g_new (GSocketControlMessage *, n_messages);
for (l = buffer->control_messages, i = 0; l != NULL; l = l->next, i++)
messages[i] = l->data;
v.buffer = &buffer->data[buffer->sent];
v.size = buffer->pos - buffer->sent;
res = g_socket_send_message (socket, NULL, &v, 1,
messages, n_messages,
G_SOCKET_MSG_NONE, NULL, &error);
g_free (messages);
if (res < 0 && g_error_matches (error, G_IO_ERROR, G_IO_ERROR_WOULD_BLOCK))
{
g_error_free (error);
return FALSE;
}
if (res <= 0)
{
if (res < 0)
{
g_warning ("Error writing credentials to socket: %s", error->message);
g_error_free (error);
}
side_closed (side);
return FALSE;
}