This repository has been archived by the owner on Jan 16, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnm-dbus-manager.c
1654 lines (1356 loc) · 58.8 KB
/
nm-dbus-manager.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
/* SPDX-License-Identifier: GPL-2.0-or-later */
/*
* Copyright (C) 2006 - 2013 Red Hat, Inc.
* Copyright (C) 2006 - 2008 Novell, Inc.
*/
#include "src/core/nm-default-daemon.h"
#include "nm-dbus-manager.h"
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#include "c-list/src/c-list.h"
#include "libnm-glib-aux/nm-c-list.h"
#include "nm-dbus-interface.h"
#include "libnm-core-intern/nm-core-internal.h"
#include "libnm-std-aux/nm-dbus-compat.h"
#include "nm-dbus-object.h"
#include "NetworkManagerUtils.h"
#include "libnm-core-aux-intern/nm-auth-subject.h"
/* The base path for our GDBusObjectManagerServers. They do not contain
* "NetworkManager" because GDBusObjectManagerServer requires that all
* exported objects be *below* the base path, and eg the Manager object
* is the base path already.
*/
#define OBJECT_MANAGER_SERVER_BASE_PATH "/org/freedesktop"
/*****************************************************************************/
typedef struct {
CList caller_info_lst;
gulong uid;
gulong pid;
gint64 uid_checked_at;
gint64 pid_checked_at;
bool uid_valid : 1;
bool pid_valid : 1;
char sender[0];
} CallerInfo;
typedef struct {
GVariant *value;
} PropertyCacheData;
typedef struct {
CList registration_lst;
NMDBusObject *obj;
NMDBusObjectClass *klass;
guint info_idx;
guint registration_id;
PropertyCacheData property_cache[];
} RegistrationData;
/* we require that @path is the first member of NMDBusManagerData
* because _objects_by_path_hash() requires that. */
G_STATIC_ASSERT(G_STRUCT_OFFSET(struct _NMDBusObjectInternal, path) == 0);
enum {
PRIVATE_CONNECTION_NEW,
PRIVATE_CONNECTION_DISCONNECTED,
LAST_SIGNAL
};
static guint signals[LAST_SIGNAL];
typedef struct {
GHashTable *objects_by_path;
CList objects_lst_head;
CList private_servers_lst_head;
NMDBusManagerSetPropertyHandler set_property_handler;
gpointer set_property_handler_data;
GDBusConnection *main_dbus_connection;
CList caller_info_lst_head;
guint objmgr_registration_id;
bool started : 1;
bool shutting_down : 1;
} NMDBusManagerPrivate;
struct _NMDBusManager {
GObject parent;
NMDBusManagerPrivate _priv;
};
struct _NMDBusManagerClass {
GObjectClass parent;
};
G_DEFINE_TYPE(NMDBusManager, nm_dbus_manager, G_TYPE_OBJECT)
#define NM_DBUS_MANAGER_GET_PRIVATE(self) _NM_GET_PRIVATE(self, NMDBusManager, NM_IS_DBUS_MANAGER)
/*****************************************************************************/
#define _NMLOG_DOMAIN LOGD_CORE
#define _NMLOG(level, ...) __NMLOG_DEFAULT(level, _NMLOG_DOMAIN, "bus-manager", __VA_ARGS__)
NM_DEFINE_SINGLETON_GETTER(NMDBusManager, nm_dbus_manager_get, NM_TYPE_DBUS_MANAGER);
/*****************************************************************************/
static const GDBusInterfaceInfo interface_info_objmgr;
static const GDBusSignalInfo signal_info_objmgr_interfaces_added;
static const GDBusSignalInfo signal_info_objmgr_interfaces_removed;
static GVariantBuilder *_obj_collect_properties_all(NMDBusObject *obj, GVariantBuilder *builder);
/*****************************************************************************/
static guint
_objects_by_path_hash(gconstpointer user_data)
{
const char *const *p_data = user_data;
nm_assert(p_data);
nm_assert(*p_data);
nm_assert((*p_data)[0] == '/');
return nm_hash_str(*p_data);
}
static gboolean
_objects_by_path_equal(gconstpointer user_data_a, gconstpointer user_data_b)
{
const char *const *p_data_a = user_data_a;
const char *const *p_data_b = user_data_b;
nm_assert(p_data_a);
nm_assert(*p_data_a);
nm_assert((*p_data_a)[0] == '/');
nm_assert(p_data_b);
nm_assert(*p_data_b);
nm_assert((*p_data_b)[0] == '/');
return nm_streq(*p_data_a, *p_data_b);
}
/*****************************************************************************/
typedef struct {
CList private_servers_lst;
const char *tag;
GQuark detail;
char *address;
GDBusServer *server;
/* With peer bus connections, we'll get a new connection for each
* client. For each connection we create an ObjectManager for
* that connection to handle exporting our objects.
*
* Note that even for connections that don't export any objects
* we'll still create GDBusObjectManager since that's where we store
* the pointer to the GDBusConnection.
*/
CList object_mgr_lst_head;
NMDBusManager *manager;
} PrivateServer;
typedef struct {
CList object_mgr_lst;
GDBusObjectManagerServer *manager;
char *fake_sender;
} ObjectMgrData;
typedef struct {
GDBusConnection *connection;
PrivateServer *server;
gboolean remote_peer_vanished;
} CloseConnectionInfo;
/*****************************************************************************/
static void
_object_mgr_data_free(ObjectMgrData *obj_mgr_data)
{
GDBusConnection *connection;
c_list_unlink_stale(&obj_mgr_data->object_mgr_lst);
connection = g_dbus_object_manager_server_get_connection(obj_mgr_data->manager);
if (!g_dbus_connection_is_closed(connection))
g_dbus_connection_close(connection, NULL, NULL, NULL);
g_dbus_object_manager_server_set_connection(obj_mgr_data->manager, NULL);
g_object_unref(obj_mgr_data->manager);
g_object_unref(connection);
g_free(obj_mgr_data->fake_sender);
g_slice_free(ObjectMgrData, obj_mgr_data);
}
/*****************************************************************************/
static gboolean
close_connection_in_idle(gpointer user_data)
{
CloseConnectionInfo *info = user_data;
PrivateServer *server = info->server;
ObjectMgrData *obj_mgr_data, *obj_mgr_data_safe;
/* Emit this for the manager */
g_signal_emit(server->manager,
signals[PRIVATE_CONNECTION_DISCONNECTED],
server->detail,
info->connection);
/* FIXME: there's a bug (754730) in GLib for which the connection
* is marked as closed when the remote peer vanishes but its
* resources are not cleaned up. Work around it by explicitly
* closing the connection in that case. */
if (info->remote_peer_vanished)
g_dbus_connection_close(info->connection, NULL, NULL, NULL);
c_list_for_each_entry_safe (obj_mgr_data,
obj_mgr_data_safe,
&server->object_mgr_lst_head,
object_mgr_lst) {
gs_unref_object GDBusConnection *connection = NULL;
connection = g_dbus_object_manager_server_get_connection(obj_mgr_data->manager);
if (connection == info->connection) {
_object_mgr_data_free(obj_mgr_data);
break;
}
}
g_object_unref(server->manager);
g_slice_free(CloseConnectionInfo, info);
return G_SOURCE_REMOVE;
}
static void
private_server_closed_connection(GDBusConnection *conn,
gboolean remote_peer_vanished,
GError *error,
gpointer user_data)
{
PrivateServer *s = user_data;
CloseConnectionInfo *info;
/* Clean up after the connection */
_LOGD("(%s) closed connection " NM_HASH_OBFUSCATE_PTR_FMT " on private socket",
s->tag,
NM_HASH_OBFUSCATE_PTR(conn));
info = g_slice_new0(CloseConnectionInfo);
info->connection = conn;
info->server = s;
info->remote_peer_vanished = remote_peer_vanished;
g_object_ref(s->manager);
/* Delay the close of connection to ensure that D-Bus signals
* are handled */
nm_g_idle_add(close_connection_in_idle, info);
}
static gboolean
private_server_new_connection(GDBusServer *server, GDBusConnection *conn, gpointer user_data)
{
PrivateServer *s = user_data;
ObjectMgrData *obj_mgr_data;
static guint32 counter = 0;
GDBusObjectManagerServer *manager;
char *sender;
g_signal_connect(conn, "closed", G_CALLBACK(private_server_closed_connection), s);
/* Fake a sender since private connections don't have one */
sender = g_strdup_printf("x:y:%d", counter++);
manager = g_dbus_object_manager_server_new(OBJECT_MANAGER_SERVER_BASE_PATH);
g_dbus_object_manager_server_set_connection(manager, conn);
obj_mgr_data = g_slice_new(ObjectMgrData);
obj_mgr_data->manager = manager;
obj_mgr_data->fake_sender = sender;
c_list_link_tail(&s->object_mgr_lst_head, &obj_mgr_data->object_mgr_lst);
_LOGD("(%s) accepted connection " NM_HASH_OBFUSCATE_PTR_FMT " on private socket",
s->tag,
NM_HASH_OBFUSCATE_PTR(conn));
/* Emit this for the manager.
*
* It is essential to do this from the "new-connection" signal handler, as
* at that point no messages from the connection are yet processed
* (which avoids races with registering objects). */
g_signal_emit(s->manager, signals[PRIVATE_CONNECTION_NEW], s->detail, conn, manager);
return TRUE;
}
static gboolean
private_server_authorize(GDBusAuthObserver *observer,
GIOStream *stream,
GCredentials *credentials,
gpointer user_data)
{
return g_credentials_get_unix_user(credentials, NULL) == 0;
}
static gboolean
private_server_allow_mechanism(GDBusAuthObserver *observer,
const char *mechanism,
gpointer user_data)
{
return NM_IN_STRSET(mechanism, "EXTERNAL");
}
static void
private_server_free(gpointer ptr)
{
PrivateServer *s = ptr;
ObjectMgrData *obj_mgr_data, *obj_mgr_data_safe;
c_list_unlink_stale(&s->private_servers_lst);
unlink(s->address);
g_free(s->address);
c_list_for_each_entry_safe (obj_mgr_data,
obj_mgr_data_safe,
&s->object_mgr_lst_head,
object_mgr_lst)
_object_mgr_data_free(obj_mgr_data);
g_dbus_server_stop(s->server);
g_signal_handlers_disconnect_by_func(s->server, G_CALLBACK(private_server_new_connection), s);
g_object_unref(s->server);
g_slice_free(PrivateServer, s);
}
void
nm_dbus_manager_private_server_register(NMDBusManager *self, const char *path, const char *tag)
{
NMDBusManagerPrivate *priv;
PrivateServer *s;
gs_unref_object GDBusAuthObserver *auth_observer = NULL;
GDBusServer *server;
GError *error = NULL;
gs_free char *address = NULL;
gs_free char *guid = NULL;
g_return_if_fail(NM_IS_DBUS_MANAGER(self));
g_return_if_fail(path);
g_return_if_fail(tag);
priv = NM_DBUS_MANAGER_GET_PRIVATE(self);
/* Only one instance per tag; but don't warn */
c_list_for_each_entry (s, &priv->private_servers_lst_head, private_servers_lst) {
if (nm_streq0(tag, s->tag))
return;
}
unlink(path);
address = g_strdup_printf("unix:path=%s", path);
_LOGD("(%s) creating private socket %s", tag, address);
guid = g_dbus_generate_guid();
auth_observer = g_dbus_auth_observer_new();
g_signal_connect(auth_observer,
"authorize-authenticated-peer",
G_CALLBACK(private_server_authorize),
NULL);
g_signal_connect(auth_observer,
"allow-mechanism",
G_CALLBACK(private_server_allow_mechanism),
NULL);
server = g_dbus_server_new_sync(address,
G_DBUS_SERVER_FLAGS_NONE,
guid,
auth_observer,
NULL,
&error);
if (!server) {
_LOGW("(%s) failed to set up private socket %s: %s", tag, address, error->message);
g_error_free(error);
return;
}
s = g_slice_new0(PrivateServer);
s->address = g_steal_pointer(&address);
s->server = server;
g_signal_connect(server, "new-connection", G_CALLBACK(private_server_new_connection), s);
c_list_init(&s->object_mgr_lst_head);
s->manager = self;
s->detail = g_quark_from_string(tag);
s->tag = g_quark_to_string(s->detail);
c_list_link_tail(&priv->private_servers_lst_head, &s->private_servers_lst);
g_dbus_server_start(server);
}
static const char *
private_server_get_connection_owner(PrivateServer *s, GDBusConnection *connection)
{
ObjectMgrData *obj_mgr_data;
nm_assert(s);
nm_assert(G_IS_DBUS_CONNECTION(connection));
c_list_for_each_entry (obj_mgr_data, &s->object_mgr_lst_head, object_mgr_lst) {
gs_unref_object GDBusConnection *c = NULL;
c = g_dbus_object_manager_server_get_connection(obj_mgr_data->manager);
if (c == connection)
return obj_mgr_data->fake_sender;
}
return NULL;
}
static GDBusConnection *
private_server_get_connection_by_owner(PrivateServer *s, const char *owner)
{
ObjectMgrData *obj_mgr_data;
nm_assert(s);
nm_assert(owner);
c_list_for_each_entry (obj_mgr_data, &s->object_mgr_lst_head, object_mgr_lst) {
if (nm_streq(owner, obj_mgr_data->fake_sender))
return g_dbus_object_manager_server_get_connection(obj_mgr_data->manager);
}
return NULL;
}
/*****************************************************************************/
static void
_caller_info_free(CallerInfo *caller_info)
{
c_list_unlink_stale(&caller_info->caller_info_lst);
g_free(caller_info);
}
static gboolean
_bus_get_unix_pid(NMDBusManager *self, const char *sender, gulong *out_pid)
{
NMDBusManagerPrivate *priv = NM_DBUS_MANAGER_GET_PRIVATE(self);
guint32 unix_pid = G_MAXUINT32;
gs_unref_variant GVariant *ret = NULL;
if (!priv->main_dbus_connection)
return FALSE;
ret = g_dbus_connection_call_sync(priv->main_dbus_connection,
DBUS_SERVICE_DBUS,
DBUS_PATH_DBUS,
DBUS_INTERFACE_DBUS,
"GetConnectionUnixProcessID",
g_variant_new("(s)", sender),
G_VARIANT_TYPE("(u)"),
G_DBUS_CALL_FLAGS_NONE,
2000,
NULL,
NULL);
if (!ret)
return FALSE;
g_variant_get(ret, "(u)", &unix_pid);
*out_pid = (gulong) unix_pid;
return TRUE;
}
static gboolean
_bus_get_unix_user(NMDBusManager *self, const char *sender, gulong *out_user)
{
NMDBusManagerPrivate *priv = NM_DBUS_MANAGER_GET_PRIVATE(self);
guint32 unix_uid = G_MAXUINT32;
gs_unref_variant GVariant *ret = NULL;
if (!priv->main_dbus_connection)
return FALSE;
ret = g_dbus_connection_call_sync(priv->main_dbus_connection,
DBUS_SERVICE_DBUS,
DBUS_PATH_DBUS,
DBUS_INTERFACE_DBUS,
"GetConnectionUnixUser",
g_variant_new("(s)", sender),
G_VARIANT_TYPE("(u)"),
G_DBUS_CALL_FLAGS_NONE,
2000,
NULL,
NULL);
if (!ret)
return FALSE;
g_variant_get(ret, "(u)", &unix_uid);
*out_user = (gulong) unix_uid;
return TRUE;
}
static const CallerInfo *
_get_caller_info_ensure(NMDBusManager *self,
const char *sender,
gboolean ensure_uid,
gboolean ensure_pid)
{
NMDBusManagerPrivate *priv = NM_DBUS_MANAGER_GET_PRIVATE(self);
CallerInfo *caller_info;
CallerInfo *ci;
gint64 now_ns;
gsize num;
#define CALLER_INFO_MAX_AGE (NM_UTILS_NSEC_PER_SEC * 1)
/* Linear search the cache for the sender.
*
* The number of cached caller-infos is limited. Hence, it's O(1) and
* the list is reasonably short.
* Also, the entire caching assumes that we repeatedly ask for the
* same sender. That means, we expect to find the right caller info
* at the front of the list. */
num = 1;
caller_info = NULL;
c_list_for_each_entry (ci, &priv->caller_info_lst_head, caller_info_lst) {
if (nm_streq(sender, ci->sender)) {
caller_info = ci;
break;
}
num++;
}
if (caller_info)
nm_c_list_move_front(&priv->caller_info_lst_head, &caller_info->caller_info_lst);
else {
gsize l = strlen(sender) + 1;
caller_info = g_malloc(sizeof(CallerInfo) + l);
*caller_info = (CallerInfo){
.uid_checked_at = -CALLER_INFO_MAX_AGE,
.pid_checked_at = -CALLER_INFO_MAX_AGE,
};
memcpy(caller_info->sender, sender, l);
c_list_link_front(&priv->caller_info_lst_head, &caller_info->caller_info_lst);
/* only cache the last few entries. */
while (TRUE) {
nm_assert(num > 0 && num == c_list_length(&priv->caller_info_lst_head));
if (num-- <= 5)
break;
_caller_info_free(
c_list_last_entry(&priv->caller_info_lst_head, CallerInfo, caller_info_lst));
}
}
now_ns = nm_utils_get_monotonic_timestamp_nsec();
if (ensure_uid && (now_ns - caller_info->uid_checked_at) > CALLER_INFO_MAX_AGE) {
caller_info->uid_checked_at = now_ns;
if (!(caller_info->uid_valid = _bus_get_unix_user(self, sender, &caller_info->uid)))
caller_info->uid = G_MAXULONG;
}
if (ensure_pid && (now_ns - caller_info->pid_checked_at) > CALLER_INFO_MAX_AGE) {
caller_info->pid_checked_at = now_ns;
if (!(caller_info->pid_valid = _bus_get_unix_pid(self, sender, &caller_info->pid)))
caller_info->pid = G_MAXULONG;
}
return caller_info;
}
static gboolean
_get_caller_info(NMDBusManager *self,
GDBusMethodInvocation *context,
GDBusConnection *connection,
GDBusMessage *message,
const char **out_sender,
gulong *out_uid,
gulong *out_pid)
{
NMDBusManagerPrivate *priv = NM_DBUS_MANAGER_GET_PRIVATE(self);
const CallerInfo *caller_info;
const char *sender;
if (context) {
nm_assert(G_IS_DBUS_METHOD_INVOCATION(context));
connection = g_dbus_method_invocation_get_connection(context);
/* only bus connections will have a sender */
sender = g_dbus_method_invocation_get_sender(context);
} else {
nm_assert(G_IS_DBUS_MESSAGE(message));
sender = g_dbus_message_get_sender(message);
}
nm_assert(G_IS_DBUS_CONNECTION(connection));
if (!sender) {
PrivateServer *s;
/* Might be a private connection, for which we fake a sender */
c_list_for_each_entry (s, &priv->private_servers_lst_head, private_servers_lst) {
sender = private_server_get_connection_owner(s, connection);
if (sender) {
NM_SET_OUT(out_uid, 0);
NM_SET_OUT(out_sender, sender);
if (out_pid) {
GCredentials *creds;
creds = g_dbus_connection_get_peer_credentials(connection);
if (creds) {
pid_t pid;
pid = g_credentials_get_unix_pid(creds, NULL);
if (pid == -1)
*out_pid = G_MAXULONG;
else
*out_pid = pid;
} else
*out_pid = G_MAXULONG;
}
return TRUE;
}
}
NM_SET_OUT(out_sender, NULL);
NM_SET_OUT(out_uid, G_MAXULONG);
NM_SET_OUT(out_pid, G_MAXULONG);
return FALSE;
}
caller_info = _get_caller_info_ensure(self, sender, !!out_uid, !!out_pid);
NM_SET_OUT(out_sender, caller_info->sender);
NM_SET_OUT(out_uid, caller_info->uid);
NM_SET_OUT(out_pid, caller_info->pid);
if (out_uid && !caller_info->uid_valid)
return FALSE;
if (out_pid && !caller_info->pid_valid)
return FALSE;
return TRUE;
}
gboolean
nm_dbus_manager_get_caller_info(NMDBusManager *self,
GDBusMethodInvocation *context,
const char **out_sender,
gulong *out_uid,
gulong *out_pid)
{
return _get_caller_info(self, context, NULL, NULL, out_sender, out_uid, out_pid);
}
gboolean
nm_dbus_manager_get_caller_info_from_message(NMDBusManager *self,
GDBusConnection *connection,
GDBusMessage *message,
const char **out_sender,
gulong *out_uid,
gulong *out_pid)
{
return _get_caller_info(self, NULL, connection, message, out_sender, out_uid, out_pid);
}
/**
* nm_dbus_manager_ensure_uid:
*
* @self: bus manager instance
* @context: D-Bus method invocation
* @uid: a user-id
* @error_domain: error domain to return on failure
* @error_code: error code to return on failure
*
* Retrieves the uid of the D-Bus method caller and
* checks that it matches @uid, unless @uid is G_MAXULONG.
* In case of failure the function returns FALSE and finishes
* handling the D-Bus method with an error.
*
* Returns: %TRUE if the check succeeded, %FALSE otherwise
*/
gboolean
nm_dbus_manager_ensure_uid(NMDBusManager *self,
GDBusMethodInvocation *context,
gulong uid,
GQuark error_domain,
int error_code)
{
gulong caller_uid;
GError *error = NULL;
g_return_val_if_fail(NM_IS_DBUS_MANAGER(self), FALSE);
g_return_val_if_fail(G_IS_DBUS_METHOD_INVOCATION(context), FALSE);
if (!nm_dbus_manager_get_caller_info(self, context, NULL, &caller_uid, NULL)) {
error = g_error_new_literal(error_domain, error_code, "Unable to determine request UID.");
g_dbus_method_invocation_take_error(context, error);
return FALSE;
}
if (uid != G_MAXULONG && caller_uid != uid) {
error = g_error_new_literal(error_domain, error_code, "Permission denied");
g_dbus_method_invocation_take_error(context, error);
return FALSE;
}
return TRUE;
}
gboolean
nm_dbus_manager_get_unix_user(NMDBusManager *self, const char *sender, gulong *out_uid)
{
NMDBusManagerPrivate *priv = NM_DBUS_MANAGER_GET_PRIVATE(self);
const CallerInfo *caller_info;
PrivateServer *s;
g_return_val_if_fail(sender != NULL, FALSE);
g_return_val_if_fail(out_uid != NULL, FALSE);
/* Check if it's a private connection sender, which we fake */
c_list_for_each_entry (s, &priv->private_servers_lst_head, private_servers_lst) {
gs_unref_object GDBusConnection *connection = NULL;
connection = private_server_get_connection_by_owner(s, sender);
if (connection) {
*out_uid = 0;
return TRUE;
}
}
/* Otherwise, a bus connection */
caller_info = _get_caller_info_ensure(self, sender, TRUE, FALSE);
*out_uid = caller_info->uid;
if (!caller_info->uid_valid) {
_LOGW("failed to get unix user for dbus sender '%s'", sender);
return FALSE;
}
return TRUE;
}
/*****************************************************************************/
static const NMDBusInterfaceInfoExtended *
_reg_data_get_interface_info(RegistrationData *reg_data)
{
nm_assert(reg_data);
return reg_data->klass->interface_infos[reg_data->info_idx];
}
/*****************************************************************************/
static void
dbus_vtable_method_call(GDBusConnection *connection,
const char *sender,
const char *object_path,
const char *interface_name,
const char *method_name,
GVariant *parameters,
GDBusMethodInvocation *invocation,
gpointer user_data)
{
NMDBusManager *self;
NMDBusManagerPrivate *priv;
RegistrationData *reg_data = user_data;
NMDBusObject *obj = reg_data->obj;
const NMDBusInterfaceInfoExtended *interface_info = _reg_data_get_interface_info(reg_data);
const NMDBusMethodInfoExtended *method_info = NULL;
gboolean on_same_interface;
on_same_interface = nm_streq(interface_info->parent.name, interface_name);
/* handle property setter first... */
if (!on_same_interface && nm_streq(interface_name, DBUS_INTERFACE_PROPERTIES)
&& nm_streq(method_name, "Set")) {
const NMDBusPropertyInfoExtended *property_info = NULL;
const char *property_interface;
const char *property_name;
gs_unref_variant GVariant *value = NULL;
self = nm_dbus_object_get_manager(obj);
priv = NM_DBUS_MANAGER_GET_PRIVATE(self);
g_variant_get(parameters, "(&s&sv)", &property_interface, &property_name, &value);
nm_assert(nm_streq(property_interface, interface_info->parent.name));
property_info =
(const NMDBusPropertyInfoExtended *) nm_dbus_utils_interface_info_lookup_property(
&interface_info->parent,
property_name,
NULL);
if (!property_info
|| !NM_FLAGS_HAS(property_info->parent.flags, G_DBUS_PROPERTY_INFO_FLAGS_WRITABLE))
g_return_if_reached();
if (!priv->set_property_handler) {
g_dbus_method_invocation_return_error(invocation,
G_DBUS_ERROR,
G_DBUS_ERROR_AUTH_FAILED,
"Cannot authenticate setting property %s",
property_name);
return;
}
priv->set_property_handler(obj,
interface_info,
property_info,
connection,
sender,
invocation,
value,
priv->set_property_handler_data);
return;
}
if (on_same_interface) {
method_info = (const NMDBusMethodInfoExtended *) nm_dbus_utils_interface_info_lookup_method(
&interface_info->parent,
method_name);
}
if (!method_info) {
g_dbus_method_invocation_return_error(invocation,
G_DBUS_ERROR,
G_DBUS_ERROR_UNKNOWN_METHOD,
"Unknown method %s",
method_name);
return;
}
self = nm_dbus_object_get_manager(obj);
priv = NM_DBUS_MANAGER_GET_PRIVATE(self);
if (priv->shutting_down && !method_info->allow_during_shutdown) {
g_dbus_method_invocation_return_error_literal(invocation,
G_DBUS_ERROR,
G_DBUS_ERROR_FAILED,
"NetworkManager is exiting");
return;
}
method_info->handle(reg_data->obj,
interface_info,
method_info,
connection,
sender,
invocation,
parameters);
}
static GVariant *
_obj_get_property(RegistrationData *reg_data, guint property_idx, gboolean refetch)
{
const NMDBusInterfaceInfoExtended *interface_info = _reg_data_get_interface_info(reg_data);
const NMDBusPropertyInfoExtended *property_info;
GVariant *value;
property_info =
(const NMDBusPropertyInfoExtended *) (interface_info->parent.properties[property_idx]);
if (refetch)
nm_clear_g_variant(®_data->property_cache[property_idx].value);
else {
value = reg_data->property_cache[property_idx].value;
if (value)
goto out;
}
value = nm_dbus_utils_get_property(G_OBJECT(reg_data->obj),
property_info->parent.signature,
property_info->property_name);
reg_data->property_cache[property_idx].value = value;
out:
return g_variant_ref(value);
}
static GVariant *
dbus_vtable_get_property(GDBusConnection *connection,
const char *sender,
const char *object_path,
const char *interface_name,
const char *property_name,
GError **error,
gpointer user_data)
{
RegistrationData *reg_data = user_data;
const NMDBusInterfaceInfoExtended *interface_info = _reg_data_get_interface_info(reg_data);
guint property_idx;
if (!nm_dbus_utils_interface_info_lookup_property(&interface_info->parent,
property_name,
&property_idx))
g_return_val_if_reached(NULL);
return _obj_get_property(reg_data, property_idx, FALSE);
}
static const GDBusInterfaceVTable dbus_vtable = {
.method_call = dbus_vtable_method_call,
.get_property = dbus_vtable_get_property,
/* set_property is handled via method_call as well. We need to authenticate
* which requires an asynchronous handler. */
.set_property = NULL,
};
static void
_obj_register(NMDBusManager *self, NMDBusObject *obj)
{
NMDBusManagerPrivate *priv = NM_DBUS_MANAGER_GET_PRIVATE(self);
guint i, k;
guint n_klasses;
GType gtype;
NMDBusObjectClass *klasses[10];
const NMDBusInterfaceInfoExtended *const *prev_interface_infos = NULL;
GVariantBuilder builder;
nm_assert(c_list_is_empty(&obj->internal.registration_lst_head));
nm_assert(priv->main_dbus_connection);
nm_assert(priv->objmgr_registration_id != 0);
nm_assert(priv->started);
n_klasses = 0;
gtype = G_OBJECT_TYPE(obj);
while (gtype != NM_TYPE_DBUS_OBJECT) {
nm_assert(n_klasses < G_N_ELEMENTS(klasses));
klasses[n_klasses++] = g_type_class_ref(gtype);
gtype = g_type_parent(gtype);
}
for (k = n_klasses; k > 0;) {
NMDBusObjectClass *klass = NM_DBUS_OBJECT_CLASS(klasses[--k]);
if (!klass->interface_infos)
continue;
if (prev_interface_infos == klass->interface_infos) {
/* derived classes inherrit the interface-infos from the parent class.
* For convenience, we allow the subclass to leave interface-infos untouched,
* but it means we must ignore the parent's interface, because we already
* handled it.
*
* Note that the loop goes from the parent classes to child classes */
continue;
}
prev_interface_infos = klass->interface_infos;
for (i = 0; klass->interface_infos[i]; i++) {
const NMDBusInterfaceInfoExtended *interface_info = klass->interface_infos[i];
RegistrationData *reg_data;
gs_free_error GError *error = NULL;
guint registration_id;
guint prop_len = NM_PTRARRAY_LEN(interface_info->parent.properties);
reg_data = g_malloc0(sizeof(RegistrationData) + (sizeof(PropertyCacheData) * prop_len));
registration_id = g_dbus_connection_register_object(
priv->main_dbus_connection,
obj->internal.path,
NM_UNCONST_PTR(GDBusInterfaceInfo, &interface_info->parent),
&dbus_vtable,
reg_data,
NULL,
&error);
if (!registration_id) {
_LOGE("failure to register object %s: %s", obj->internal.path, error->message);
g_free(reg_data);
continue;
}
reg_data->obj = obj;
reg_data->klass = g_type_class_ref(G_TYPE_FROM_CLASS(klass));
reg_data->info_idx = i;
reg_data->registration_id = registration_id;
c_list_link_tail(&obj->internal.registration_lst_head, ®_data->registration_lst);
}
}
for (k = 0; k < n_klasses; k++)
g_type_class_unref(klasses[k]);
nm_assert(!c_list_is_empty(&obj->internal.registration_lst_head));
/* Currently, the interfaces of an object do not changed and strictly depend on the object glib type.
* We don't need more flexibility, and it simplifies the code. Hence, now emit interface-added
* signal for the new object.
*
* Warning: note that if @obj's notify signal is currently blocked via g_object_freeze_notify(),
* we might emit properties with an inconsistent (internal) state. There is no easy solution,