Add support for GDBus security handlers
[platform/upstream/connman.git] / gdbus / object.c
1 /*
2  *
3  *  D-Bus helper library
4  *
5  *  Copyright (C) 2004-2010  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 struct generic_data {
41         unsigned int refcount;
42         GSList *interfaces;
43         char *introspect;
44 };
45
46 struct interface_data {
47         char *name;
48         const GDBusMethodTable *methods;
49         const GDBusSignalTable *signals;
50         const GDBusPropertyTable *properties;
51         void *user_data;
52         GDBusDestroyFunction destroy;
53 };
54
55 struct security_data {
56         GDBusPendingReply pending;
57         DBusMessage *message;
58         const GDBusMethodTable *method;
59         void *iface_user_data;
60 };
61
62 static void print_arguments(GString *gstr, const char *sig,
63                                                 const char *direction)
64 {
65         int i;
66
67         for (i = 0; sig[i]; i++) {
68                 char type[32];
69                 int struct_level, dict_level;
70                 unsigned int len;
71                 gboolean complete;
72
73                 complete = FALSE;
74                 struct_level = dict_level = 0;
75                 memset(type, 0, sizeof(type));
76
77                 /* Gather enough data to have a single complete type */
78                 for (len = 0; len < (sizeof(type) - 1) && sig[i]; len++, i++) {
79                         switch (sig[i]){
80                         case '(':
81                                 struct_level++;
82                                 break;
83                         case ')':
84                                 struct_level--;
85                                 if (struct_level <= 0 && dict_level <= 0)
86                                         complete = TRUE;
87                                 break;
88                         case '{':
89                                 dict_level++;
90                                 break;
91                         case '}':
92                                 dict_level--;
93                                 if (struct_level <= 0 && dict_level <= 0)
94                                         complete = TRUE;
95                                 break;
96                         case 'a':
97                                 break;
98                         default:
99                                 if (struct_level <= 0 && dict_level <= 0)
100                                         complete = TRUE;
101                                 break;
102                         }
103
104                         type[len] = sig[i];
105
106                         if (complete)
107                                 break;
108                 }
109
110
111                 if (direction)
112                         g_string_append_printf(gstr,
113                                         "\t\t\t<arg type=\"%s\" direction=\"%s\"/>\n",
114                                         type, direction);
115                 else
116                         g_string_append_printf(gstr,
117                                         "\t\t\t<arg type=\"%s\"/>\n",
118                                         type);
119         }
120 }
121
122 static void generate_interface_xml(GString *gstr, struct interface_data *iface)
123 {
124         const GDBusMethodTable *method;
125         const GDBusSignalTable *signal;
126
127         for (method = iface->methods; method && method->name; method++) {
128                 if (!strlen(method->signature) && !strlen(method->reply))
129                         g_string_append_printf(gstr, "\t\t<method name=\"%s\"/>\n",
130                                                                 method->name);
131                 else {
132                         g_string_append_printf(gstr, "\t\t<method name=\"%s\">\n",
133                                                                 method->name);
134                         print_arguments(gstr, method->signature, "in");
135                         print_arguments(gstr, method->reply, "out");
136                         g_string_append_printf(gstr, "\t\t</method>\n");
137                 }
138         }
139
140         for (signal = iface->signals; signal && signal->name; signal++) {
141                 if (!strlen(signal->signature))
142                         g_string_append_printf(gstr, "\t\t<signal name=\"%s\"/>\n",
143                                                                 signal->name);
144                 else {
145                         g_string_append_printf(gstr, "\t\t<signal name=\"%s\">\n",
146                                                                 signal->name);
147                         print_arguments(gstr, signal->signature, NULL);
148                         g_string_append_printf(gstr, "\t\t</signal>\n");
149                 }
150         }
151 }
152
153 static void generate_introspection_xml(DBusConnection *conn,
154                                 struct generic_data *data, const char *path)
155 {
156         GSList *list;
157         GString *gstr;
158         char **children;
159         int i;
160
161         g_free(data->introspect);
162
163         gstr = g_string_new(DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE);
164
165         g_string_append_printf(gstr, "<node name=\"%s\">\n", path);
166
167         for (list = data->interfaces; list; list = list->next) {
168                 struct interface_data *iface = list->data;
169
170                 g_string_append_printf(gstr, "\t<interface name=\"%s\">\n",
171                                                                 iface->name);
172
173                 generate_interface_xml(gstr, iface);
174
175                 g_string_append_printf(gstr, "\t</interface>\n");
176         }
177
178         if (!dbus_connection_list_registered(conn, path, &children))
179                 goto done;
180
181         for (i = 0; children[i]; i++)
182                 g_string_append_printf(gstr, "\t<node name=\"%s\"/>\n",
183                                                                 children[i]);
184
185         dbus_free_string_array(children);
186
187 done:
188         g_string_append_printf(gstr, "</node>\n");
189
190         data->introspect = g_string_free(gstr, FALSE);
191 }
192
193 static DBusMessage *introspect(DBusConnection *connection,
194                                 DBusMessage *message, void *user_data)
195 {
196         struct generic_data *data = user_data;
197         DBusMessage *reply;
198
199         if (!dbus_message_has_signature(message, DBUS_TYPE_INVALID_AS_STRING)) {
200                 error("Unexpected signature to introspect call");
201                 return NULL;
202         }
203
204         if (!data->introspect)
205                 generate_introspection_xml(connection, data,
206                                                 dbus_message_get_path(message));
207
208         reply = dbus_message_new_method_return(message);
209         if (!reply)
210                 return NULL;
211
212         dbus_message_append_args(reply, DBUS_TYPE_STRING, &data->introspect,
213                                         DBUS_TYPE_INVALID);
214
215         return reply;
216 }
217
218 static DBusHandlerResult process_message(DBusConnection *connection,
219                         DBusMessage *message, const GDBusMethodTable *method,
220                                                         void *iface_user_data)
221 {
222         DBusMessage *reply;
223
224         reply = method->function(connection, message, iface_user_data);
225
226         if (method->flags & G_DBUS_METHOD_FLAG_NOREPLY) {
227                 if (reply != NULL)
228                         dbus_message_unref(reply);
229                 return DBUS_HANDLER_RESULT_HANDLED;
230         }
231
232         if (method->flags & G_DBUS_METHOD_FLAG_ASYNC) {
233                 if (reply == NULL)
234                         return DBUS_HANDLER_RESULT_HANDLED;
235         }
236
237         if (reply == NULL)
238                 return DBUS_HANDLER_RESULT_NEED_MEMORY;
239
240         dbus_connection_send(connection, reply, NULL);
241         dbus_message_unref(reply);
242
243         return DBUS_HANDLER_RESULT_HANDLED;
244 }
245
246 static GDBusPendingReply next_pending = 1;
247 static GSList *pending_security = NULL;
248
249 static const GDBusSecurityTable *security_table = NULL;
250
251 void g_dbus_pending_success(DBusConnection *connection,
252                                         GDBusPendingReply pending)
253 {
254         GSList *list;
255
256         for (list = pending_security; list; list = list->next) {
257                 struct security_data *secdata = list->data;
258                 DBusHandlerResult result;
259
260                 if (secdata->pending != pending)
261                         continue;
262
263                 pending_security = g_slist_remove(pending_security, secdata);
264
265                 result = process_message(connection, secdata->message,
266                                 secdata->method, secdata->iface_user_data);
267
268                 dbus_message_unref(secdata->message);
269                 g_free(secdata);
270                 return;
271         }
272 }
273
274 void g_dbus_pending_error(DBusConnection *connection,
275                                 GDBusPendingReply pending, DBusMessage *error)
276 {
277         GSList *list;
278
279         for (list = pending_security; list; list = list->next) {
280                 struct security_data *secdata = list->data;
281
282                 if (secdata->pending != pending)
283                         continue;
284
285                 pending_security = g_slist_remove(pending_security, secdata);
286
287                 if (error != NULL) {
288                         dbus_connection_send(connection, error, NULL);
289                         dbus_message_unref(error);
290                 }
291
292                 dbus_message_unref(secdata->message);
293                 g_free(secdata);
294                 return;
295         }
296 }
297
298 static gboolean check_privilege(DBusConnection *conn, DBusMessage *msg,
299                         const GDBusMethodTable *method, void *iface_user_data)
300 {
301         const GDBusSecurityTable *security;
302
303         for (security = security_table; security && security->function &&
304                                         security->privilege; security++) {
305                 struct security_data *secdata;
306
307                 if (security->privilege != method->privilege)
308                         continue;
309
310                 secdata = g_new(struct security_data, 1);
311                 secdata->pending = next_pending++;
312                 secdata->message = dbus_message_ref(msg);
313                 secdata->method = method;
314                 secdata->iface_user_data = iface_user_data;
315
316                 pending_security = g_slist_prepend(pending_security, secdata);
317
318                 security->function(conn, secdata->message, secdata->pending);
319
320                 return TRUE;
321         }
322
323         return FALSE;
324 }
325
326 static void generic_unregister(DBusConnection *connection, void *user_data)
327 {
328         struct generic_data *data = user_data;
329
330         g_free(data->introspect);
331         g_free(data);
332 }
333
334 static struct interface_data *find_interface(GSList *interfaces,
335                                                 const char *name)
336 {
337         GSList *list;
338
339         if (!name)
340                 return NULL;
341
342         for (list = interfaces; list; list = list->next) {
343                 struct interface_data *iface = list->data;
344                 if (!strcmp(name, iface->name))
345                         return iface;
346         }
347
348         return NULL;
349 }
350
351 static DBusHandlerResult generic_message(DBusConnection *connection,
352                                         DBusMessage *message, void *user_data)
353 {
354         struct generic_data *data = user_data;
355         struct interface_data *iface;
356         const GDBusMethodTable *method;
357         const char *interface;
358
359         interface = dbus_message_get_interface(message);
360
361         iface = find_interface(data->interfaces, interface);
362         if (!iface)
363                 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
364
365         for (method = iface->methods; method &&
366                         method->name && method->function; method++) {
367                 if (dbus_message_is_method_call(message, iface->name,
368                                                         method->name) == FALSE)
369                         continue;
370
371                 if (dbus_message_has_signature(message,
372                                                 method->signature) == FALSE)
373                         continue;
374
375                 if (check_privilege(connection, message, method,
376                                                 iface->user_data) == TRUE)
377                         return DBUS_HANDLER_RESULT_HANDLED;
378
379                 return process_message(connection, message, method,
380                                                         iface->user_data);
381         }
382
383         return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
384 }
385
386 static DBusObjectPathVTable generic_table = {
387         .unregister_function    = generic_unregister,
388         .message_function       = generic_message,
389 };
390
391 static void invalidate_parent_data(DBusConnection *conn, const char *child_path)
392 {
393         struct generic_data *data = NULL;
394         char *parent_path, *slash;
395
396         parent_path = g_strdup(child_path);
397         slash = strrchr(parent_path, '/');
398         if (!slash)
399                 goto done;
400
401         if (slash == parent_path && parent_path[1] != '\0')
402                 parent_path[1] = '\0';
403         else
404                 *slash = '\0';
405
406         if (!strlen(parent_path))
407                 goto done;
408
409         if (!dbus_connection_get_object_path_data(conn, parent_path,
410                                                         (void *) &data)) {
411                 invalidate_parent_data(conn, parent_path);
412                 goto done;
413         }
414
415         if (!data)
416                 goto done;
417
418         g_free(data->introspect);
419         data->introspect = NULL;
420
421 done:
422         g_free(parent_path);
423 }
424
425 static GDBusMethodTable introspect_methods[] = {
426         { "Introspect", "",     "s", introspect },
427         { }
428 };
429
430 static void add_interface(struct generic_data *data, const char *name,
431                                 const GDBusMethodTable *methods,
432                                 const GDBusSignalTable *signals,
433                                 const GDBusPropertyTable *properties,
434                                 void *user_data,
435                                 GDBusDestroyFunction destroy)
436 {
437         struct interface_data *iface;
438
439         iface = g_new0(struct interface_data, 1);
440         iface->name = g_strdup(name);
441         iface->methods = methods;
442         iface->signals = signals;
443         iface->properties = properties;
444         iface->user_data = user_data;
445         iface->destroy = destroy;
446
447         data->interfaces = g_slist_append(data->interfaces, iface);
448 }
449
450 static struct generic_data *object_path_ref(DBusConnection *connection,
451                                                         const char *path)
452 {
453         struct generic_data *data;
454
455         if (dbus_connection_get_object_path_data(connection, path,
456                                                 (void *) &data) == TRUE) {
457                 if (data != NULL) {
458                         data->refcount++;
459                         return data;
460                 }
461         }
462
463         data = g_new0(struct generic_data, 1);
464         data->refcount = 1;
465
466         data->introspect = g_strdup(DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE "<node></node>");
467
468         if (!dbus_connection_register_object_path(connection, path,
469                                                 &generic_table, data)) {
470                 g_free(data->introspect);
471                 g_free(data);
472                 return NULL;
473         }
474
475         invalidate_parent_data(connection, path);
476
477         add_interface(data, DBUS_INTERFACE_INTROSPECTABLE,
478                         introspect_methods, NULL, NULL, data, NULL);
479
480         return data;
481 }
482
483 static gboolean remove_interface(struct generic_data *data, const char *name)
484 {
485         struct interface_data *iface;
486
487         iface = find_interface(data->interfaces, name);
488         if (!iface)
489                 return FALSE;
490
491         data->interfaces = g_slist_remove(data->interfaces, iface);
492
493         if (iface->destroy)
494                 iface->destroy(iface->user_data);
495
496         g_free(iface->name);
497         g_free(iface);
498
499         return TRUE;
500 }
501
502 static void object_path_unref(DBusConnection *connection, const char *path)
503 {
504         struct generic_data *data = NULL;
505
506         if (dbus_connection_get_object_path_data(connection, path,
507                                                 (void *) &data) == FALSE)
508                 return;
509
510         if (data == NULL)
511                 return;
512
513         data->refcount--;
514
515         if (data->refcount > 0)
516                 return;
517
518         remove_interface(data, DBUS_INTERFACE_INTROSPECTABLE);
519
520         invalidate_parent_data(connection, path);
521
522         dbus_connection_unregister_object_path(connection, path);
523 }
524
525 static gboolean check_signal(DBusConnection *conn, const char *path,
526                                 const char *interface, const char *name,
527                                 const char **args)
528 {
529         struct generic_data *data = NULL;
530         struct interface_data *iface;
531         const GDBusSignalTable *signal;
532
533         *args = NULL;
534         if (!dbus_connection_get_object_path_data(conn, path,
535                                         (void *) &data) || !data) {
536                 error("dbus_connection_emit_signal: path %s isn't registered",
537                                 path);
538                 return FALSE;
539         }
540
541         iface = find_interface(data->interfaces, interface);
542         if (!iface) {
543                 error("dbus_connection_emit_signal: %s does not implement %s",
544                                 path, interface);
545                 return FALSE;
546         }
547
548         for (signal = iface->signals; signal && signal->name; signal++) {
549                 if (!strcmp(signal->name, name)) {
550                         *args = signal->signature;
551                         break;
552                 }
553         }
554
555         if (!*args) {
556                 error("No signal named %s on interface %s", name, interface);
557                 return FALSE;
558         }
559
560         return TRUE;
561 }
562
563 static dbus_bool_t emit_signal_valist(DBusConnection *conn,
564                                                 const char *path,
565                                                 const char *interface,
566                                                 const char *name,
567                                                 int first,
568                                                 va_list var_args)
569 {
570         DBusMessage *signal;
571         dbus_bool_t ret;
572         const char *signature, *args;
573
574         if (!check_signal(conn, path, interface, name, &args))
575                 return FALSE;
576
577         signal = dbus_message_new_signal(path, interface, name);
578         if (!signal) {
579                 error("Unable to allocate new %s.%s signal", interface,  name);
580                 return FALSE;
581         }
582
583         ret = dbus_message_append_args_valist(signal, first, var_args);
584         if (!ret)
585                 goto fail;
586
587         signature = dbus_message_get_signature(signal);
588         if (strcmp(args, signature) != 0) {
589                 error("%s.%s: expected signature'%s' but got '%s'",
590                                 interface, name, args, signature);
591                 ret = FALSE;
592                 goto fail;
593         }
594
595         ret = dbus_connection_send(conn, signal, NULL);
596
597 fail:
598         dbus_message_unref(signal);
599
600         return ret;
601 }
602
603 gboolean g_dbus_register_interface(DBusConnection *connection,
604                                         const char *path, const char *name,
605                                         const GDBusMethodTable *methods,
606                                         const GDBusSignalTable *signals,
607                                         const GDBusPropertyTable *properties,
608                                         void *user_data,
609                                         GDBusDestroyFunction destroy)
610 {
611         struct generic_data *data;
612
613         data = object_path_ref(connection, path);
614         if (data == NULL)
615                 return FALSE;
616
617         if (find_interface(data->interfaces, name)) {
618                 object_path_unref(connection, path);
619                 return FALSE;
620         }
621
622         add_interface(data, name, methods, signals,
623                         properties, user_data, destroy);
624
625         g_free(data->introspect);
626         data->introspect = NULL;
627
628         return TRUE;
629 }
630
631 gboolean g_dbus_unregister_interface(DBusConnection *connection,
632                                         const char *path, const char *name)
633 {
634         struct generic_data *data = NULL;
635
636         if (!path)
637                 return FALSE;
638
639         if (dbus_connection_get_object_path_data(connection, path,
640                                                 (void *) &data) == FALSE)
641                 return FALSE;
642
643         if (data == NULL)
644                 return FALSE;
645
646         if (remove_interface(data, name) == FALSE)
647                 return FALSE;
648
649         g_free(data->introspect);
650         data->introspect = NULL;
651
652         object_path_unref(connection, path);
653
654         return TRUE;
655 }
656
657 gboolean g_dbus_register_security(const GDBusSecurityTable *security)
658 {
659         if (security_table != NULL)
660                 return FALSE;
661
662         security_table = security;
663
664         return TRUE;
665 }
666
667 gboolean g_dbus_unregister_security(const GDBusSecurityTable *security)
668 {
669         security_table = NULL;
670
671         return TRUE;
672 }
673
674 DBusMessage *g_dbus_create_error_valist(DBusMessage *message, const char *name,
675                                         const char *format, va_list args)
676 {
677         char str[1024];
678
679         vsnprintf(str, sizeof(str), format, args);
680
681         return dbus_message_new_error(message, name, str);
682 }
683
684 DBusMessage *g_dbus_create_error(DBusMessage *message, const char *name,
685                                                 const char *format, ...)
686 {
687         va_list args;
688         DBusMessage *reply;
689
690         va_start(args, format);
691
692         reply = g_dbus_create_error_valist(message, name, format, args);
693
694         va_end(args);
695
696         return reply;
697 }
698
699 DBusMessage *g_dbus_create_reply_valist(DBusMessage *message,
700                                                 int type, va_list args)
701 {
702         DBusMessage *reply;
703
704         reply = dbus_message_new_method_return(message);
705         if (reply == NULL)
706                 return NULL;
707
708         if (dbus_message_append_args_valist(reply, type, args) == FALSE) {
709                 dbus_message_unref(reply);
710                 return NULL;
711         }
712
713         return reply;
714 }
715
716 DBusMessage *g_dbus_create_reply(DBusMessage *message, int type, ...)
717 {
718         va_list args;
719         DBusMessage *reply;
720
721         va_start(args, type);
722
723         reply = g_dbus_create_reply_valist(message, type, args);
724
725         va_end(args);
726
727         return reply;
728 }
729
730 gboolean g_dbus_send_message(DBusConnection *connection, DBusMessage *message)
731 {
732         dbus_bool_t result;
733
734         if (dbus_message_get_type(message) == DBUS_MESSAGE_TYPE_METHOD_CALL)
735                 dbus_message_set_no_reply(message, TRUE);
736
737         result = dbus_connection_send(connection, message, NULL);
738
739         dbus_message_unref(message);
740
741         return result;
742 }
743
744 gboolean g_dbus_send_reply_valist(DBusConnection *connection,
745                                 DBusMessage *message, int type, va_list args)
746 {
747         DBusMessage *reply;
748
749         reply = dbus_message_new_method_return(message);
750         if (reply == NULL)
751                 return FALSE;
752
753         if (dbus_message_append_args_valist(reply, type, args) == FALSE) {
754                 dbus_message_unref(reply);
755                 return FALSE;
756         }
757
758         return g_dbus_send_message(connection, reply);
759 }
760
761 gboolean g_dbus_send_reply(DBusConnection *connection,
762                                 DBusMessage *message, int type, ...)
763 {
764         va_list args;
765         gboolean result;
766
767         va_start(args, type);
768
769         result = g_dbus_send_reply_valist(connection, message, type, args);
770
771         va_end(args);
772
773         return result;
774 }
775
776 gboolean g_dbus_emit_signal(DBusConnection *connection,
777                                 const char *path, const char *interface,
778                                 const char *name, int type, ...)
779 {
780         va_list args;
781         gboolean result;
782
783         va_start(args, type);
784
785         result = emit_signal_valist(connection, path, interface,
786                                                         name, type, args);
787
788         va_end(args);
789
790         return result;
791 }
792
793 gboolean g_dbus_emit_signal_valist(DBusConnection *connection,
794                                 const char *path, const char *interface,
795                                 const char *name, int type, va_list args)
796 {
797         return emit_signal_valist(connection, path, interface,
798                                                         name, type, args);
799 }