gdbus: Only export ObjectManager interface on root path
[platform/upstream/connman.git] / gdbus / object.c
1 /*
2  *
3  *  D-Bus helper library
4  *
5  *  Copyright (C) 2004-2011  Marcel Holtmann <marcel@holtmann.org>
6  *
7  *
8  *  This program is free software; you can redistribute it and/or modify
9  *  it under the terms of the GNU General Public License as published by
10  *  the Free Software Foundation; either version 2 of the License, or
11  *  (at your option) any later version.
12  *
13  *  This program is distributed in the hope that it will be useful,
14  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  *  GNU General Public License for more details.
17  *
18  *  You should have received a copy of the GNU General Public License
19  *  along with this program; if not, write to the Free Software
20  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
21  *
22  */
23
24 #ifdef HAVE_CONFIG_H
25 #include <config.h>
26 #endif
27
28 #include <stdio.h>
29 #include <string.h>
30
31 #include <glib.h>
32 #include <dbus/dbus.h>
33
34 #include "gdbus.h"
35
36 #define info(fmt...)
37 #define error(fmt...)
38 #define debug(fmt...)
39
40 #define DBUS_INTERFACE_OBJECT_MANAGER "org.freedesktop.DBus.ObjectManager"
41
42 struct generic_data {
43         unsigned int refcount;
44         DBusConnection *conn;
45         char *path;
46         GSList *interfaces;
47         GSList *objects;
48         GSList *added;
49         GSList *removed;
50         guint process_id;
51         char *introspect;
52         struct generic_data *parent;
53 };
54
55 struct interface_data {
56         char *name;
57         const GDBusMethodTable *methods;
58         const GDBusSignalTable *signals;
59         const GDBusPropertyTable *properties;
60         void *user_data;
61         GDBusDestroyFunction destroy;
62 };
63
64 struct security_data {
65         GDBusPendingReply pending;
66         DBusMessage *message;
67         const GDBusMethodTable *method;
68         void *iface_user_data;
69 };
70
71 struct property_data {
72         GDBusPendingPropertySet id;
73         DBusMessage *message;
74 };
75
76 static gboolean process_changes(gpointer user_data);
77
78 static void print_arguments(GString *gstr, const GDBusArgInfo *args,
79                                                 const char *direction)
80 {
81         for (; args && args->name; args++) {
82                 g_string_append_printf(gstr,
83                                         "\t\t\t<arg name=\"%s\" type=\"%s\"",
84                                         args->name, args->signature);
85
86                 if (direction)
87                         g_string_append_printf(gstr,
88                                         " direction=\"%s\"/>\n", direction);
89                 else
90                         g_string_append_printf(gstr, "/>\n");
91
92         }
93 }
94
95 #define G_DBUS_ANNOTATE(prefix_, name_, value_)                         \
96         prefix_ "<annotation name=\"org.freedesktop.DBus." name_ "\" "  \
97         "value=\"" value_ "\"/>\n"
98
99 #define G_DBUS_ANNOTATE_DEPRECATED(prefix_) \
100         G_DBUS_ANNOTATE(prefix_, "Deprecated", "true")
101
102 #define G_DBUS_ANNOTATE_NOREPLY(prefix_) \
103         G_DBUS_ANNOTATE(prefix_, "Method.NoReply", "true")
104
105 static void generate_interface_xml(GString *gstr, struct interface_data *iface)
106 {
107         const GDBusMethodTable *method;
108         const GDBusSignalTable *signal;
109         const GDBusPropertyTable *property;
110
111         for (method = iface->methods; method && method->name; method++) {
112                 gboolean deprecated = method->flags &
113                                                 G_DBUS_METHOD_FLAG_DEPRECATED;
114                 gboolean noreply = method->flags &
115                                                 G_DBUS_METHOD_FLAG_NOREPLY;
116
117                 if (!deprecated && !noreply &&
118                                 !(method->in_args && method->in_args->name) &&
119                                 !(method->out_args && method->out_args->name))
120                         g_string_append_printf(gstr,
121                                                 "\t\t<method name=\"%s\"/>\n",
122                                                 method->name);
123                 else {
124                         g_string_append_printf(gstr,
125                                                 "\t\t<method name=\"%s\">\n",
126                                                 method->name);
127                         print_arguments(gstr, method->in_args, "in");
128                         print_arguments(gstr, method->out_args, "out");
129
130                         if (deprecated)
131                                 g_string_append_printf(gstr,
132                                         G_DBUS_ANNOTATE_DEPRECATED("\t\t\t"));
133                         if (noreply)
134                                 g_string_append_printf(gstr,
135                                         G_DBUS_ANNOTATE_NOREPLY("\t\t\t"));
136
137                         g_string_append_printf(gstr, "\t\t</method>\n");
138                 }
139         }
140
141         for (signal = iface->signals; signal && signal->name; signal++) {
142                 gboolean deprecated = signal->flags &
143                                                 G_DBUS_SIGNAL_FLAG_DEPRECATED;
144
145                 if (!deprecated && !(signal->args && signal->args->name))
146                         g_string_append_printf(gstr,
147                                                 "\t\t<signal name=\"%s\"/>\n",
148                                                 signal->name);
149                 else {
150                         g_string_append_printf(gstr,
151                                                 "\t\t<signal name=\"%s\">\n",
152                                                 signal->name);
153                         print_arguments(gstr, signal->args, NULL);
154
155                         if (deprecated)
156                                 g_string_append_printf(gstr,
157                                         G_DBUS_ANNOTATE_DEPRECATED("\t\t\t"));
158
159                         g_string_append_printf(gstr, "\t\t</signal>\n");
160                 }
161         }
162
163         for (property = iface->properties; property && property->name;
164                                                                 property++) {
165                 gboolean deprecated = property->flags &
166                                         G_DBUS_PROPERTY_FLAG_DEPRECATED;
167
168                 g_string_append_printf(gstr, "\t\t<property name=\"%s\""
169                                         " type=\"%s\" access=\"%s%s\"",
170                                         property->name, property->type,
171                                         property->get ? "read" : "",
172                                         property->set ? "write" : "");
173
174                 if (!deprecated)
175                         g_string_append_printf(gstr, "/>\n");
176                 else
177                         g_string_append_printf(gstr,
178                                 G_DBUS_ANNOTATE_DEPRECATED(">\n\t\t\t"));
179         }
180 }
181
182 static void generate_introspection_xml(DBusConnection *conn,
183                                 struct generic_data *data, const char *path)
184 {
185         GSList *list;
186         GString *gstr;
187         char **children;
188         int i;
189
190         g_free(data->introspect);
191
192         gstr = g_string_new(DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE);
193
194         g_string_append_printf(gstr, "<node>\n");
195
196         for (list = data->interfaces; list; list = list->next) {
197                 struct interface_data *iface = list->data;
198
199                 g_string_append_printf(gstr, "\t<interface name=\"%s\">\n",
200                                                                 iface->name);
201
202                 generate_interface_xml(gstr, iface);
203
204                 g_string_append_printf(gstr, "\t</interface>\n");
205         }
206
207         if (!dbus_connection_list_registered(conn, path, &children))
208                 goto done;
209
210         for (i = 0; children[i]; i++)
211                 g_string_append_printf(gstr, "\t<node name=\"%s\"/>\n",
212                                                                 children[i]);
213
214         dbus_free_string_array(children);
215
216 done:
217         g_string_append_printf(gstr, "</node>\n");
218
219         data->introspect = g_string_free(gstr, FALSE);
220 }
221
222 static DBusMessage *introspect(DBusConnection *connection,
223                                 DBusMessage *message, void *user_data)
224 {
225         struct generic_data *data = user_data;
226         DBusMessage *reply;
227
228         if (data->introspect == NULL)
229                 generate_introspection_xml(connection, data,
230                                                 dbus_message_get_path(message));
231
232         reply = dbus_message_new_method_return(message);
233         if (reply == NULL)
234                 return NULL;
235
236         dbus_message_append_args(reply, DBUS_TYPE_STRING, &data->introspect,
237                                         DBUS_TYPE_INVALID);
238
239         return reply;
240 }
241
242 static DBusHandlerResult process_message(DBusConnection *connection,
243                         DBusMessage *message, const GDBusMethodTable *method,
244                                                         void *iface_user_data)
245 {
246         DBusMessage *reply;
247
248         reply = method->function(connection, message, iface_user_data);
249
250         if (method->flags & G_DBUS_METHOD_FLAG_NOREPLY) {
251                 if (reply != NULL)
252                         dbus_message_unref(reply);
253                 return DBUS_HANDLER_RESULT_HANDLED;
254         }
255
256         if (method->flags & G_DBUS_METHOD_FLAG_ASYNC) {
257                 if (reply == NULL)
258                         return DBUS_HANDLER_RESULT_HANDLED;
259         }
260
261         if (reply == NULL)
262                 return DBUS_HANDLER_RESULT_NEED_MEMORY;
263
264         dbus_connection_send(connection, reply, NULL);
265         dbus_message_unref(reply);
266
267         return DBUS_HANDLER_RESULT_HANDLED;
268 }
269
270 static GDBusPendingReply next_pending = 1;
271 static GSList *pending_security = NULL;
272
273 static const GDBusSecurityTable *security_table = NULL;
274
275 void g_dbus_pending_success(DBusConnection *connection,
276                                         GDBusPendingReply pending)
277 {
278         GSList *list;
279
280         for (list = pending_security; list; list = list->next) {
281                 struct security_data *secdata = list->data;
282
283                 if (secdata->pending != pending)
284                         continue;
285
286                 pending_security = g_slist_remove(pending_security, secdata);
287
288                 process_message(connection, secdata->message,
289                                 secdata->method, secdata->iface_user_data);
290
291                 dbus_message_unref(secdata->message);
292                 g_free(secdata);
293                 return;
294         }
295 }
296
297 void g_dbus_pending_error_valist(DBusConnection *connection,
298                                 GDBusPendingReply pending, const char *name,
299                                         const char *format, va_list args)
300 {
301         GSList *list;
302
303         for (list = pending_security; list; list = list->next) {
304                 struct security_data *secdata = list->data;
305                 DBusMessage *reply;
306
307                 if (secdata->pending != pending)
308                         continue;
309
310                 pending_security = g_slist_remove(pending_security, secdata);
311
312                 reply = g_dbus_create_error_valist(secdata->message,
313                                                         name, format, args);
314                 if (reply != NULL) {
315                         dbus_connection_send(connection, reply, NULL);
316                         dbus_message_unref(reply);
317                 }
318
319                 dbus_message_unref(secdata->message);
320                 g_free(secdata);
321                 return;
322         }
323 }
324
325 void g_dbus_pending_error(DBusConnection *connection,
326                                 GDBusPendingReply pending,
327                                 const char *name, const char *format, ...)
328 {
329         va_list args;
330
331         va_start(args, format);
332
333         g_dbus_pending_error_valist(connection, pending, name, format, args);
334
335         va_end(args);
336 }
337
338 int polkit_check_authorization(DBusConnection *conn,
339                                 const char *action, gboolean interaction,
340                                 void (*function) (dbus_bool_t authorized,
341                                                         void *user_data),
342                                                 void *user_data, int timeout);
343
344 struct builtin_security_data {
345         DBusConnection *conn;
346         GDBusPendingReply pending;
347 };
348
349 static void builtin_security_result(dbus_bool_t authorized, void *user_data)
350 {
351         struct builtin_security_data *data = user_data;
352
353         if (authorized == TRUE)
354                 g_dbus_pending_success(data->conn, data->pending);
355         else
356                 g_dbus_pending_error(data->conn, data->pending,
357                                                 DBUS_ERROR_AUTH_FAILED, NULL);
358
359         g_free(data);
360 }
361
362 static void builtin_security_function(DBusConnection *conn,
363                                                 const char *action,
364                                                 gboolean interaction,
365                                                 GDBusPendingReply pending)
366 {
367         struct builtin_security_data *data;
368
369         data = g_new0(struct builtin_security_data, 1);
370         data->conn = conn;
371         data->pending = pending;
372
373         if (polkit_check_authorization(conn, action, interaction,
374                                 builtin_security_result, data, 30000) < 0)
375                 g_dbus_pending_error(conn, pending, NULL, NULL);
376 }
377
378 static gboolean check_privilege(DBusConnection *conn, DBusMessage *msg,
379                         const GDBusMethodTable *method, void *iface_user_data)
380 {
381         const GDBusSecurityTable *security;
382
383         for (security = security_table; security && security->privilege;
384                                                                 security++) {
385                 struct security_data *secdata;
386                 gboolean interaction;
387
388                 if (security->privilege != method->privilege)
389                         continue;
390
391                 secdata = g_new(struct security_data, 1);
392                 secdata->pending = next_pending++;
393                 secdata->message = dbus_message_ref(msg);
394                 secdata->method = method;
395                 secdata->iface_user_data = iface_user_data;
396
397                 pending_security = g_slist_prepend(pending_security, secdata);
398
399                 if (security->flags & G_DBUS_SECURITY_FLAG_ALLOW_INTERACTION)
400                         interaction = TRUE;
401                 else
402                         interaction = FALSE;
403
404                 if (!(security->flags & G_DBUS_SECURITY_FLAG_BUILTIN) &&
405                                                         security->function)
406                         security->function(conn, security->action,
407                                                 interaction, secdata->pending);
408                 else
409                         builtin_security_function(conn, security->action,
410                                                 interaction, secdata->pending);
411
412                 return TRUE;
413         }
414
415         return FALSE;
416 }
417
418 static GDBusPendingPropertySet next_pending_property = 1;
419 static GSList *pending_property_set;
420
421 static struct property_data *remove_pending_property_data(
422                                                 GDBusPendingPropertySet id)
423 {
424         struct property_data *propdata;
425         GSList *l;
426
427         for (l = pending_property_set; l != NULL; l = l->next) {
428                 propdata = l->data;
429                 if (propdata->id != id)
430                         continue;
431         }
432
433         if (l == NULL)
434                 return NULL;
435
436         pending_property_set = g_slist_delete_link(pending_property_set, l);
437
438         return propdata;
439 }
440
441 void g_dbus_pending_property_success(DBusConnection *connection,
442                                                 GDBusPendingPropertySet id)
443 {
444         struct property_data *propdata;
445
446         propdata = remove_pending_property_data(id);
447         if (propdata == NULL)
448                 return;
449
450         g_dbus_send_reply(connection, propdata->message, DBUS_TYPE_INVALID);
451         dbus_message_unref(propdata->message);
452         g_free(propdata);
453 }
454
455 void g_dbus_pending_property_error_valist(DBusConnection *connection,
456                                         GDBusPendingReply id, const char *name,
457                                         const char *format, va_list args)
458 {
459         struct property_data *propdata;
460         DBusMessage *reply;
461
462         propdata = remove_pending_property_data(id);
463         if (propdata == NULL)
464                 return;
465
466         reply = g_dbus_create_error_valist(propdata->message, name, format,
467                                                                         args);
468         if (reply != NULL) {
469                 dbus_connection_send(connection, reply, NULL);
470                 dbus_message_unref(reply);
471         }
472
473         dbus_message_unref(propdata->message);
474         g_free(propdata);
475 }
476
477 void g_dbus_pending_property_error(DBusConnection *connection,
478                                         GDBusPendingReply id, const char *name,
479                                         const char *format, ...)
480 {
481         va_list args;
482
483         va_start(args, format);
484
485         g_dbus_pending_property_error_valist(connection, id, name, format,
486                                                                         args);
487
488         va_end(args);
489 }
490
491 static void reset_parent(gpointer data, gpointer user_data)
492 {
493         struct generic_data *child = data;
494         struct generic_data *parent = user_data;
495
496         child->parent = parent;
497 }
498
499 static void append_properties(struct interface_data *data,
500                                                         DBusMessageIter *iter)
501 {
502         DBusMessageIter dict;
503
504         dbus_message_iter_open_container(iter, DBUS_TYPE_ARRAY,
505                                 DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
506                                 DBUS_TYPE_STRING_AS_STRING
507                                 DBUS_TYPE_VARIANT_AS_STRING
508                                 DBUS_DICT_ENTRY_END_CHAR_AS_STRING, &dict);
509
510         /* TODO: list properties */
511
512         dbus_message_iter_close_container(iter, &dict);
513 }
514
515 static void append_interface(gpointer data, gpointer user_data)
516 {
517         struct interface_data *iface = data;
518         DBusMessageIter *array = user_data;
519         DBusMessageIter entry;
520
521         dbus_message_iter_open_container(array, DBUS_TYPE_DICT_ENTRY, NULL,
522                                                                 &entry);
523         dbus_message_iter_append_basic(&entry, DBUS_TYPE_STRING, &iface->name);
524         append_properties(data, &entry);
525         dbus_message_iter_close_container(array, &entry);
526 }
527
528 static void emit_interfaces_added(struct generic_data *data)
529 {
530         DBusMessage *signal;
531         DBusMessageIter iter, array;
532         struct generic_data *parent = data->parent;
533
534         if (parent == NULL)
535                 return;
536
537         /* Find root data */
538         while (parent->parent)
539                 parent = parent->parent;
540
541         signal = dbus_message_new_signal(parent->path,
542                                         DBUS_INTERFACE_OBJECT_MANAGER,
543                                         "InterfacesAdded");
544         if (signal == NULL)
545                 return;
546
547         dbus_message_iter_init_append(signal, &iter);
548         dbus_message_iter_append_basic(&iter, DBUS_TYPE_OBJECT_PATH,
549                                                                 &data->path);
550
551         dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY,
552                                 DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
553                                 DBUS_TYPE_STRING_AS_STRING
554                                 DBUS_TYPE_ARRAY_AS_STRING
555                                 DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
556                                 DBUS_TYPE_STRING_AS_STRING
557                                 DBUS_TYPE_VARIANT_AS_STRING
558                                 DBUS_DICT_ENTRY_END_CHAR_AS_STRING
559                                 DBUS_DICT_ENTRY_END_CHAR_AS_STRING, &array);
560
561         g_slist_foreach(data->added, append_interface, &array);
562         g_slist_free(data->added);
563         data->added = NULL;
564
565         dbus_message_iter_close_container(&iter, &array);
566
567         g_dbus_send_message(data->conn, signal);
568 }
569
570 static struct interface_data *find_interface(GSList *interfaces,
571                                                 const char *name)
572 {
573         GSList *list;
574
575         if (name == NULL)
576                 return NULL;
577
578         for (list = interfaces; list; list = list->next) {
579                 struct interface_data *iface = list->data;
580                 if (!strcmp(name, iface->name))
581                         return iface;
582         }
583
584         return NULL;
585 }
586
587 static gboolean g_dbus_args_have_signature(const GDBusArgInfo *args,
588                                                         DBusMessage *message)
589 {
590         const char *sig = dbus_message_get_signature(message);
591         const char *p = NULL;
592
593         for (; args && args->signature && *sig; args++) {
594                 p = args->signature;
595
596                 for (; *sig && *p; sig++, p++) {
597                         if (*p != *sig)
598                                 return FALSE;
599                 }
600         }
601
602         if (*sig || (p && *p) || (args && args->signature))
603                 return FALSE;
604
605         return TRUE;
606 }
607
608 static gboolean remove_interface(struct generic_data *data, const char *name)
609 {
610         struct interface_data *iface;
611
612         iface = find_interface(data->interfaces, name);
613         if (iface == NULL)
614                 return FALSE;
615
616         data->interfaces = g_slist_remove(data->interfaces, iface);
617
618         if (iface->destroy) {
619                 iface->destroy(iface->user_data);
620                 iface->user_data = NULL;
621         }
622
623         if (data->parent == NULL) {
624                 g_free(iface->name);
625                 g_free(iface);
626                 return TRUE;
627         }
628
629         data->removed = g_slist_prepend(data->removed, iface->name);
630         g_free(iface);
631
632         if (data->process_id > 0)
633                 return TRUE;
634
635         data->process_id = g_idle_add(process_changes, data);
636
637         return TRUE;
638 }
639
640 static struct generic_data *invalidate_parent_data(DBusConnection *conn,
641                                                 const char *child_path)
642 {
643         struct generic_data *data = NULL, *child = NULL, *parent = NULL;
644         char *parent_path, *slash;
645
646         parent_path = g_strdup(child_path);
647         slash = strrchr(parent_path, '/');
648         if (slash == NULL)
649                 goto done;
650
651         if (slash == parent_path && parent_path[1] != '\0')
652                 parent_path[1] = '\0';
653         else
654                 *slash = '\0';
655
656         if (!strlen(parent_path))
657                 goto done;
658
659         if (dbus_connection_get_object_path_data(conn, parent_path,
660                                                         (void *) &data) == FALSE) {
661                 goto done;
662         }
663
664         parent = invalidate_parent_data(conn, parent_path);
665
666         if (data == NULL) {
667                 data = parent;
668                 if (data == NULL)
669                         goto done;
670         }
671
672         g_free(data->introspect);
673         data->introspect = NULL;
674
675         if (!dbus_connection_get_object_path_data(conn, child_path,
676                                                         (void *) &child))
677                 goto done;
678
679         if (child == NULL || g_slist_find(data->objects, child) != NULL)
680                 goto done;
681
682         data->objects = g_slist_prepend(data->objects, child);
683         child->parent = data;
684
685 done:
686         g_free(parent_path);
687         return data;
688 }
689
690 static inline const GDBusPropertyTable *find_property(const GDBusPropertyTable *properties,
691                                                         const char *name)
692 {
693         const GDBusPropertyTable *p;
694
695         for (p = properties; p && p->name; p++) {
696                 if (strcmp(name, p->name) == 0)
697                         return p;
698         }
699
700         return NULL;
701 }
702
703 static DBusMessage *properties_get(DBusConnection *connection,
704                                         DBusMessage *message, void *user_data)
705 {
706         struct generic_data *data = user_data;
707         struct interface_data *iface;
708         const GDBusPropertyTable *property;
709         const char *interface, *name;
710         DBusMessageIter iter, value;
711         DBusMessage *reply;
712
713         if (!dbus_message_get_args(message, NULL,
714                                         DBUS_TYPE_STRING, &interface,
715                                         DBUS_TYPE_STRING, &name,
716                                         DBUS_TYPE_INVALID))
717                 return NULL;
718
719         iface = find_interface(data->interfaces, interface);
720         if (iface == NULL)
721                 return g_dbus_create_error(message, DBUS_ERROR_INVALID_ARGS,
722                                 "No such interface '%s'", interface);
723
724         property = find_property(iface->properties, name);
725         if (property == NULL)
726                 return g_dbus_create_error(message, DBUS_ERROR_INVALID_ARGS,
727                                 "No such property '%s'", name);
728
729         if (property->exists != NULL &&
730                         !property->exists(property, iface->user_data))
731                 return g_dbus_create_error(message, DBUS_ERROR_INVALID_ARGS,
732                                         "No such property '%s'", name);
733
734         if (property->get == NULL)
735                 return g_dbus_create_error(message, DBUS_ERROR_INVALID_ARGS,
736                                 "Property '%s' is not readable", name);
737
738         reply = dbus_message_new_method_return(message);
739         if (reply == NULL)
740                 return NULL;
741
742         dbus_message_iter_init_append(reply, &iter);
743         dbus_message_iter_open_container(&iter, DBUS_TYPE_VARIANT,
744                                                 property->type, &value);
745
746         if (!property->get(property, &value, iface->user_data)) {
747                 dbus_message_unref(reply);
748                 return NULL;
749         }
750
751         dbus_message_iter_close_container(&iter, &value);
752
753         return reply;
754 }
755
756 static DBusMessage *properties_get_all(DBusConnection *connection,
757                                         DBusMessage *message, void *user_data)
758 {
759         struct generic_data *data = user_data;
760         struct interface_data *iface;
761         const GDBusPropertyTable *p;
762         const char *interface;
763         DBusMessageIter iter, dict;
764         DBusMessage *reply;
765
766         if (!dbus_message_get_args(message, NULL,
767                                         DBUS_TYPE_STRING, &interface,
768                                         DBUS_TYPE_INVALID))
769                 return NULL;
770
771         iface = find_interface(data->interfaces, interface);
772         if (iface == NULL)
773                 return g_dbus_create_error(message, DBUS_ERROR_INVALID_ARGS,
774                                         "No such interface '%s'", interface);
775
776         reply = dbus_message_new_method_return(message);
777         if (reply == NULL)
778                 return NULL;
779
780         dbus_message_iter_init_append(reply, &iter);
781         dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY,
782                         DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
783                         DBUS_TYPE_STRING_AS_STRING DBUS_TYPE_VARIANT_AS_STRING
784                         DBUS_DICT_ENTRY_END_CHAR_AS_STRING, &dict);
785
786         for (p = iface->properties; p && p->name; p++) {
787                 DBusMessageIter entry, value;
788
789                 if (p->get == NULL)
790                         continue;
791
792                 if (p->exists != NULL && !p->exists(p, iface->user_data))
793                         continue;
794
795                 dbus_message_iter_open_container(&dict, DBUS_TYPE_DICT_ENTRY,
796                                                                 NULL, &entry);
797                 dbus_message_iter_append_basic(&entry, DBUS_TYPE_STRING,
798                                                                 p->name);
799                 dbus_message_iter_open_container(&entry, DBUS_TYPE_VARIANT,
800                                                         p->type, &value);
801
802                 if (!p->get(p, &value, iface->user_data)) {
803                         dbus_message_unref(reply);
804                         return NULL;
805                 }
806
807                 dbus_message_iter_close_container(&entry, &value);
808                 dbus_message_iter_close_container(&dict, &entry);
809         }
810
811         dbus_message_iter_close_container(&iter, &dict);
812
813         return reply;
814 }
815
816 static DBusMessage *properties_set(DBusConnection *connection,
817                                         DBusMessage *message, void *user_data)
818 {
819         struct generic_data *data = user_data;
820         DBusMessageIter iter, sub;
821         struct interface_data *iface;
822         const GDBusPropertyTable *property;
823         const char *name, *interface;
824         struct property_data *propdata;
825
826         if (!dbus_message_iter_init(message, &iter))
827                 return g_dbus_create_error(message, DBUS_ERROR_INVALID_ARGS,
828                                                         "No arguments given");
829
830         if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_STRING)
831                 return g_dbus_create_error(message, DBUS_ERROR_INVALID_ARGS,
832                                         "Invalid argument type: '%c'",
833                                         dbus_message_iter_get_arg_type(&iter));
834
835         dbus_message_iter_get_basic(&iter, &name);
836         dbus_message_iter_next(&iter);
837
838         if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_STRING)
839                 return g_dbus_create_error(message, DBUS_ERROR_INVALID_ARGS,
840                                         "Invalid argument type: '%c'",
841                                         dbus_message_iter_get_arg_type(&iter));
842
843         dbus_message_iter_get_basic(&iter, &interface);
844         dbus_message_iter_next(&iter);
845
846         if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT)
847                 return g_dbus_create_error(message, DBUS_ERROR_INVALID_ARGS,
848                                         "Invalid argument type: '%c'",
849                                         dbus_message_iter_get_arg_type(&iter));
850
851         dbus_message_iter_recurse(&iter, &sub);
852
853         iface = find_interface(data->interfaces, interface);
854         if (iface == NULL)
855                 return g_dbus_create_error(message, DBUS_ERROR_INVALID_ARGS,
856                                         "No such interface '%s'", interface);
857
858         property = find_property(iface->properties, name);
859         if (property == NULL)
860                 return g_dbus_create_error(message,
861                                                 DBUS_ERROR_UNKNOWN_PROPERTY,
862                                                 "No such property '%s'", name);
863
864         if (property->set == NULL)
865                 return g_dbus_create_error(message,
866                                         DBUS_ERROR_PROPERTY_READ_ONLY,
867                                         "Property '%s' is not writable", name);
868
869         if (property->exists != NULL &&
870                         !property->exists(property, iface->user_data))
871                 return g_dbus_create_error(message,
872                                                 DBUS_ERROR_UNKNOWN_PROPERTY,
873                                                 "No such property '%s'", name);
874
875         propdata = g_new(struct property_data, 1);
876         propdata->id = next_pending_property++;
877         propdata->message = dbus_message_ref(message);
878
879         property->set(property, &sub, propdata->id, iface->user_data);
880
881         return NULL;
882 }
883
884 static const GDBusMethodTable properties_methods[] = {
885         { GDBUS_METHOD("Get",
886                         GDBUS_ARGS({ "interface", "s" }, { "name", "s" }),
887                         GDBUS_ARGS({ "value", "v" }),
888                         properties_get) },
889         { GDBUS_ASYNC_METHOD("Set", NULL,
890                         GDBUS_ARGS({ "interface", "s" }, { "name", "s" },
891                                                         { "value", "v" }),
892                         properties_set) },
893         { GDBUS_METHOD("GetAll",
894                         GDBUS_ARGS({ "interface", "s" }),
895                         GDBUS_ARGS({ "properties", "a{sv}" }),
896                         properties_get_all) },
897         { }
898 };
899
900 static const GDBusSignalTable properties_signals[] = {
901         { GDBUS_SIGNAL("PropertiesChanged",
902                         GDBUS_ARGS({ "interface", "s" },
903                                         { "changed_properties", "a{sv}" },
904                                         { "invalidated_properties", "as"})) },
905         { }
906 };
907
908 static void append_name(gpointer data, gpointer user_data)
909 {
910         char *name = data;
911         DBusMessageIter *iter = user_data;
912
913         dbus_message_iter_append_basic(iter, DBUS_TYPE_STRING, &name);
914 }
915
916 static void emit_interfaces_removed(struct generic_data *data)
917 {
918         DBusMessage *signal;
919         DBusMessageIter iter, array;
920         struct generic_data *parent = data->parent;
921
922         if (parent == NULL)
923                 return;
924
925         /* Find root data */
926         while (parent->parent)
927                 parent = parent->parent;
928
929         signal = dbus_message_new_signal(parent->path,
930                                         DBUS_INTERFACE_OBJECT_MANAGER,
931                                         "InterfacesRemoved");
932         if (signal == NULL)
933                 return;
934
935         dbus_message_iter_init_append(signal, &iter);
936         dbus_message_iter_append_basic(&iter, DBUS_TYPE_OBJECT_PATH,
937                                                                 &data->path);
938         dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY,
939                                         DBUS_TYPE_STRING_AS_STRING, &array);
940
941         g_slist_foreach(data->removed, append_name, &array);
942         g_slist_free_full(data->removed, g_free);
943         data->removed = NULL;
944
945         dbus_message_iter_close_container(&iter, &array);
946
947         g_dbus_send_message(data->conn, signal);
948 }
949
950 static gboolean process_changes(gpointer user_data)
951 {
952         struct generic_data *data = user_data;
953
954         data->process_id = 0;
955
956         if (data->added != NULL)
957                 emit_interfaces_added(data);
958
959         if (data->removed != NULL)
960                 emit_interfaces_removed(data);
961
962         return FALSE;
963 }
964
965 static void generic_unregister(DBusConnection *connection, void *user_data)
966 {
967         struct generic_data *data = user_data;
968         struct generic_data *parent = data->parent;
969
970         if (parent != NULL)
971                 parent->objects = g_slist_remove(parent->objects, data);
972
973         if (data->process_id > 0) {
974                 g_source_remove(data->process_id);
975                 if (data->removed != NULL)
976                         emit_interfaces_removed(data);
977         }
978
979         g_slist_foreach(data->objects, reset_parent, data->parent);
980         g_slist_free(data->objects);
981
982         dbus_connection_unref(data->conn);
983         g_free(data->introspect);
984         g_free(data->path);
985         g_free(data);
986 }
987
988 static DBusHandlerResult generic_message(DBusConnection *connection,
989                                         DBusMessage *message, void *user_data)
990 {
991         struct generic_data *data = user_data;
992         struct interface_data *iface;
993         const GDBusMethodTable *method;
994         const char *interface;
995
996         interface = dbus_message_get_interface(message);
997
998         iface = find_interface(data->interfaces, interface);
999         if (iface == NULL)
1000                 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1001
1002         for (method = iface->methods; method &&
1003                         method->name && method->function; method++) {
1004                 if (dbus_message_is_method_call(message, iface->name,
1005                                                         method->name) == FALSE)
1006                         continue;
1007
1008                 if (g_dbus_args_have_signature(method->in_args,
1009                                                         message) == FALSE)
1010                         continue;
1011
1012                 if (check_privilege(connection, message, method,
1013                                                 iface->user_data) == TRUE)
1014                         return DBUS_HANDLER_RESULT_HANDLED;
1015
1016                 return process_message(connection, message, method,
1017                                                         iface->user_data);
1018         }
1019
1020         return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1021 }
1022
1023 static DBusObjectPathVTable generic_table = {
1024         .unregister_function    = generic_unregister,
1025         .message_function       = generic_message,
1026 };
1027
1028 static const GDBusMethodTable introspect_methods[] = {
1029         { GDBUS_METHOD("Introspect", NULL,
1030                         GDBUS_ARGS({ "xml", "s" }), introspect) },
1031         { }
1032 };
1033
1034 static void append_interfaces(struct generic_data *data, DBusMessageIter *iter)
1035 {
1036         DBusMessageIter array;
1037
1038         dbus_message_iter_open_container(iter, DBUS_TYPE_ARRAY,
1039                                 DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
1040                                 DBUS_TYPE_STRING_AS_STRING
1041                                 DBUS_TYPE_ARRAY_AS_STRING
1042                                 DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
1043                                 DBUS_TYPE_STRING_AS_STRING
1044                                 DBUS_TYPE_VARIANT_AS_STRING
1045                                 DBUS_DICT_ENTRY_END_CHAR_AS_STRING
1046                                 DBUS_DICT_ENTRY_END_CHAR_AS_STRING, &array);
1047
1048         g_slist_foreach(data->interfaces, append_interface, &array);
1049
1050         dbus_message_iter_close_container(iter, &array);
1051 }
1052
1053 static void append_object(gpointer data, gpointer user_data)
1054 {
1055         struct generic_data *child = data;
1056         DBusMessageIter *array = user_data;
1057         DBusMessageIter entry;
1058
1059         dbus_message_iter_open_container(array, DBUS_TYPE_DICT_ENTRY, NULL,
1060                                                                 &entry);
1061         dbus_message_iter_append_basic(&entry, DBUS_TYPE_OBJECT_PATH,
1062                                                                 &child->path);
1063         append_interfaces(child, &entry);
1064         dbus_message_iter_close_container(array, &entry);
1065
1066         g_slist_foreach(child->objects, append_object, user_data);
1067 }
1068
1069 static DBusMessage *get_objects(DBusConnection *connection,
1070                                 DBusMessage *message, void *user_data)
1071 {
1072         struct generic_data *data = user_data;
1073         DBusMessage *reply;
1074         DBusMessageIter iter;
1075         DBusMessageIter array;
1076
1077         reply = dbus_message_new_method_return(message);
1078         if (reply == NULL)
1079                 return NULL;
1080
1081         dbus_message_iter_init_append(reply, &iter);
1082
1083         dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY,
1084                                         DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
1085                                         DBUS_TYPE_OBJECT_PATH_AS_STRING
1086                                         DBUS_TYPE_ARRAY_AS_STRING
1087                                         DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
1088                                         DBUS_TYPE_STRING_AS_STRING
1089                                         DBUS_TYPE_ARRAY_AS_STRING
1090                                         DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
1091                                         DBUS_TYPE_STRING_AS_STRING
1092                                         DBUS_TYPE_VARIANT_AS_STRING
1093                                         DBUS_DICT_ENTRY_END_CHAR_AS_STRING
1094                                         DBUS_DICT_ENTRY_END_CHAR_AS_STRING
1095                                         DBUS_DICT_ENTRY_END_CHAR_AS_STRING,
1096                                         &array);
1097
1098         g_slist_foreach(data->objects, append_object, &array);
1099
1100         dbus_message_iter_close_container(&iter, &array);
1101
1102         return reply;
1103 }
1104
1105 static const GDBusMethodTable manager_methods[] = {
1106         { GDBUS_METHOD("GetManagedObjects", NULL,
1107                 GDBUS_ARGS({ "objects", "a{oa{sa{sv}}}" }), get_objects) },
1108         { }
1109 };
1110
1111 static const GDBusSignalTable manager_signals[] = {
1112         { GDBUS_SIGNAL("InterfacesAdded",
1113                 GDBUS_ARGS({ "object", "o" },
1114                                 { "interfaces", "a{sa{sv}}" })) },
1115         { GDBUS_SIGNAL("InterfacesRemoved",
1116                 GDBUS_ARGS({ "object", "o" }, { "interfaces", "as" })) },
1117         { }
1118 };
1119
1120 static void add_interface(struct generic_data *data,
1121                                 const char *name,
1122                                 const GDBusMethodTable *methods,
1123                                 const GDBusSignalTable *signals,
1124                                 const GDBusPropertyTable *properties,
1125                                 void *user_data,
1126                                 GDBusDestroyFunction destroy)
1127 {
1128         struct interface_data *iface;
1129
1130         iface = g_new0(struct interface_data, 1);
1131         iface->name = g_strdup(name);
1132         iface->methods = methods;
1133         iface->signals = signals;
1134         iface->properties = properties;
1135         iface->user_data = user_data;
1136         iface->destroy = destroy;
1137
1138         data->interfaces = g_slist_append(data->interfaces, iface);
1139         if (data->parent == NULL)
1140                 return;
1141
1142         data->added = g_slist_append(data->added, iface);
1143         if (data->process_id > 0)
1144                 return;
1145
1146         data->process_id = g_idle_add(process_changes, data);
1147 }
1148
1149 static struct generic_data *object_path_ref(DBusConnection *connection,
1150                                                         const char *path)
1151 {
1152         struct generic_data *data;
1153
1154         if (dbus_connection_get_object_path_data(connection, path,
1155                                                 (void *) &data) == TRUE) {
1156                 if (data != NULL) {
1157                         data->refcount++;
1158                         return data;
1159                 }
1160         }
1161
1162         data = g_new0(struct generic_data, 1);
1163         data->conn = dbus_connection_ref(connection);
1164         data->path = g_strdup(path);
1165         data->refcount = 1;
1166
1167         data->introspect = g_strdup(DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE "<node></node>");
1168
1169         if (!dbus_connection_register_object_path(connection, path,
1170                                                 &generic_table, data)) {
1171                 g_free(data->introspect);
1172                 g_free(data);
1173                 return NULL;
1174         }
1175
1176         invalidate_parent_data(connection, path);
1177
1178         add_interface(data, DBUS_INTERFACE_INTROSPECTABLE, introspect_methods,
1179                                                 NULL, NULL, data, NULL);
1180
1181         /* Only root path export ObjectManager interface */
1182         if (data->parent == NULL)
1183                 add_interface(data, DBUS_INTERFACE_OBJECT_MANAGER,
1184                                         manager_methods, manager_signals,
1185                                         NULL, data, NULL);
1186
1187         add_interface(data, DBUS_INTERFACE_PROPERTIES, properties_methods,
1188                                         properties_signals, NULL, data, NULL);
1189
1190         return data;
1191 }
1192
1193 static void object_path_unref(DBusConnection *connection, const char *path)
1194 {
1195         struct generic_data *data = NULL;
1196
1197         if (dbus_connection_get_object_path_data(connection, path,
1198                                                 (void *) &data) == FALSE)
1199                 return;
1200
1201         if (data == NULL)
1202                 return;
1203
1204         data->refcount--;
1205
1206         if (data->refcount > 0)
1207                 return;
1208
1209         remove_interface(data, DBUS_INTERFACE_INTROSPECTABLE);
1210         remove_interface(data, DBUS_INTERFACE_PROPERTIES);
1211         remove_interface(data, DBUS_INTERFACE_OBJECT_MANAGER);
1212
1213         invalidate_parent_data(data->conn, data->path);
1214
1215         dbus_connection_unregister_object_path(data->conn, data->path);
1216 }
1217
1218 static gboolean check_signal(DBusConnection *conn, const char *path,
1219                                 const char *interface, const char *name,
1220                                 const GDBusArgInfo **args)
1221 {
1222         struct generic_data *data = NULL;
1223         struct interface_data *iface;
1224         const GDBusSignalTable *signal;
1225
1226         *args = NULL;
1227         if (!dbus_connection_get_object_path_data(conn, path,
1228                                         (void *) &data) || data == NULL) {
1229                 error("dbus_connection_emit_signal: path %s isn't registered",
1230                                 path);
1231                 return FALSE;
1232         }
1233
1234         iface = find_interface(data->interfaces, interface);
1235         if (iface == NULL) {
1236                 error("dbus_connection_emit_signal: %s does not implement %s",
1237                                 path, interface);
1238                 return FALSE;
1239         }
1240
1241         for (signal = iface->signals; signal && signal->name; signal++) {
1242                 if (!strcmp(signal->name, name)) {
1243                         *args = signal->args;
1244                         return TRUE;
1245                 }
1246         }
1247
1248         error("No signal named %s on interface %s", name, interface);
1249         return FALSE;
1250 }
1251
1252 static dbus_bool_t emit_signal_valist(DBusConnection *conn,
1253                                                 const char *path,
1254                                                 const char *interface,
1255                                                 const char *name,
1256                                                 int first,
1257                                                 va_list var_args)
1258 {
1259         DBusMessage *signal;
1260         dbus_bool_t ret;
1261         const GDBusArgInfo *args;
1262
1263         if (!check_signal(conn, path, interface, name, &args))
1264                 return FALSE;
1265
1266         signal = dbus_message_new_signal(path, interface, name);
1267         if (signal == NULL) {
1268                 error("Unable to allocate new %s.%s signal", interface,  name);
1269                 return FALSE;
1270         }
1271
1272         ret = dbus_message_append_args_valist(signal, first, var_args);
1273         if (!ret)
1274                 goto fail;
1275
1276         if (g_dbus_args_have_signature(args, signal) == FALSE) {
1277                 error("%s.%s: got unexpected signature '%s'", interface, name,
1278                                         dbus_message_get_signature(signal));
1279                 ret = FALSE;
1280                 goto fail;
1281         }
1282
1283         ret = dbus_connection_send(conn, signal, NULL);
1284
1285 fail:
1286         dbus_message_unref(signal);
1287
1288         return ret;
1289 }
1290
1291 gboolean g_dbus_register_interface(DBusConnection *connection,
1292                                         const char *path, const char *name,
1293                                         const GDBusMethodTable *methods,
1294                                         const GDBusSignalTable *signals,
1295                                         const GDBusPropertyTable *properties,
1296                                         void *user_data,
1297                                         GDBusDestroyFunction destroy)
1298 {
1299         struct generic_data *data;
1300
1301         data = object_path_ref(connection, path);
1302         if (data == NULL)
1303                 return FALSE;
1304
1305         if (find_interface(data->interfaces, name)) {
1306                 object_path_unref(connection, path);
1307                 return FALSE;
1308         }
1309
1310         add_interface(data, name, methods, signals, properties, user_data,
1311                                                                 destroy);
1312
1313         g_free(data->introspect);
1314         data->introspect = NULL;
1315
1316         return TRUE;
1317 }
1318
1319 gboolean g_dbus_unregister_interface(DBusConnection *connection,
1320                                         const char *path, const char *name)
1321 {
1322         struct generic_data *data = NULL;
1323
1324         if (path == NULL)
1325                 return FALSE;
1326
1327         if (dbus_connection_get_object_path_data(connection, path,
1328                                                 (void *) &data) == FALSE)
1329                 return FALSE;
1330
1331         if (data == NULL)
1332                 return FALSE;
1333
1334         if (remove_interface(data, name) == FALSE)
1335                 return FALSE;
1336
1337         g_free(data->introspect);
1338         data->introspect = NULL;
1339
1340         object_path_unref(connection, data->path);
1341
1342         return TRUE;
1343 }
1344
1345 gboolean g_dbus_register_security(const GDBusSecurityTable *security)
1346 {
1347         if (security_table != NULL)
1348                 return FALSE;
1349
1350         security_table = security;
1351
1352         return TRUE;
1353 }
1354
1355 gboolean g_dbus_unregister_security(const GDBusSecurityTable *security)
1356 {
1357         security_table = NULL;
1358
1359         return TRUE;
1360 }
1361
1362 DBusMessage *g_dbus_create_error_valist(DBusMessage *message, const char *name,
1363                                         const char *format, va_list args)
1364 {
1365         char str[1024];
1366
1367         vsnprintf(str, sizeof(str), format, args);
1368
1369         return dbus_message_new_error(message, name, str);
1370 }
1371
1372 DBusMessage *g_dbus_create_error(DBusMessage *message, const char *name,
1373                                                 const char *format, ...)
1374 {
1375         va_list args;
1376         DBusMessage *reply;
1377
1378         va_start(args, format);
1379
1380         reply = g_dbus_create_error_valist(message, name, format, args);
1381
1382         va_end(args);
1383
1384         return reply;
1385 }
1386
1387 DBusMessage *g_dbus_create_reply_valist(DBusMessage *message,
1388                                                 int type, va_list args)
1389 {
1390         DBusMessage *reply;
1391
1392         reply = dbus_message_new_method_return(message);
1393         if (reply == NULL)
1394                 return NULL;
1395
1396         if (dbus_message_append_args_valist(reply, type, args) == FALSE) {
1397                 dbus_message_unref(reply);
1398                 return NULL;
1399         }
1400
1401         return reply;
1402 }
1403
1404 DBusMessage *g_dbus_create_reply(DBusMessage *message, int type, ...)
1405 {
1406         va_list args;
1407         DBusMessage *reply;
1408
1409         va_start(args, type);
1410
1411         reply = g_dbus_create_reply_valist(message, type, args);
1412
1413         va_end(args);
1414
1415         return reply;
1416 }
1417
1418 gboolean g_dbus_send_message(DBusConnection *connection, DBusMessage *message)
1419 {
1420         dbus_bool_t result;
1421
1422         if (dbus_message_get_type(message) == DBUS_MESSAGE_TYPE_METHOD_CALL)
1423                 dbus_message_set_no_reply(message, TRUE);
1424
1425         result = dbus_connection_send(connection, message, NULL);
1426
1427         dbus_message_unref(message);
1428
1429         return result;
1430 }
1431
1432 gboolean g_dbus_send_reply_valist(DBusConnection *connection,
1433                                 DBusMessage *message, int type, va_list args)
1434 {
1435         DBusMessage *reply;
1436
1437         reply = dbus_message_new_method_return(message);
1438         if (reply == NULL)
1439                 return FALSE;
1440
1441         if (dbus_message_append_args_valist(reply, type, args) == FALSE) {
1442                 dbus_message_unref(reply);
1443                 return FALSE;
1444         }
1445
1446         return g_dbus_send_message(connection, reply);
1447 }
1448
1449 gboolean g_dbus_send_reply(DBusConnection *connection,
1450                                 DBusMessage *message, int type, ...)
1451 {
1452         va_list args;
1453         gboolean result;
1454
1455         va_start(args, type);
1456
1457         result = g_dbus_send_reply_valist(connection, message, type, args);
1458
1459         va_end(args);
1460
1461         return result;
1462 }
1463
1464 gboolean g_dbus_emit_signal(DBusConnection *connection,
1465                                 const char *path, const char *interface,
1466                                 const char *name, int type, ...)
1467 {
1468         va_list args;
1469         gboolean result;
1470
1471         va_start(args, type);
1472
1473         result = emit_signal_valist(connection, path, interface,
1474                                                         name, type, args);
1475
1476         va_end(args);
1477
1478         return result;
1479 }
1480
1481 gboolean g_dbus_emit_signal_valist(DBusConnection *connection,
1482                                 const char *path, const char *interface,
1483                                 const char *name, int type, va_list args)
1484 {
1485         return emit_signal_valist(connection, path, interface,
1486                                                         name, type, args);
1487 }