Use simpler error callbacks for GDBus security hooks
[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_valist(DBusConnection *connection,
275                                 GDBusPendingReply pending, const char *name,
276                                         const char *format, va_list args)
277 {
278         GSList *list;
279
280         for (list = pending_security; list; list = list->next) {
281                 struct security_data *secdata = list->data;
282                 DBusMessage *reply;
283
284                 if (secdata->pending != pending)
285                         continue;
286
287                 pending_security = g_slist_remove(pending_security, secdata);
288
289                 reply = g_dbus_create_error_valist(secdata->message,
290                                                         name, format, args);
291                 if (reply != NULL) {
292                         dbus_connection_send(connection, reply, NULL);
293                         dbus_message_unref(reply);
294                 }
295
296                 dbus_message_unref(secdata->message);
297                 g_free(secdata);
298                 return;
299         }
300 }
301
302 void g_dbus_pending_error(DBusConnection *connection,
303                                 GDBusPendingReply pending,
304                                 const char *name, const char *format, ...)
305 {
306         va_list args;
307
308         va_start(args, format);
309
310         g_dbus_pending_error_valist(connection, pending, name, format, args);
311
312         va_end(args);
313 }
314
315 static gboolean check_privilege(DBusConnection *conn, DBusMessage *msg,
316                         const GDBusMethodTable *method, void *iface_user_data)
317 {
318         const GDBusSecurityTable *security;
319
320         for (security = security_table; security && security->function &&
321                                         security->privilege; security++) {
322                 struct security_data *secdata;
323
324                 if (security->privilege != method->privilege)
325                         continue;
326
327                 secdata = g_new(struct security_data, 1);
328                 secdata->pending = next_pending++;
329                 secdata->message = dbus_message_ref(msg);
330                 secdata->method = method;
331                 secdata->iface_user_data = iface_user_data;
332
333                 pending_security = g_slist_prepend(pending_security, secdata);
334
335                 security->function(conn, secdata->pending);
336
337                 return TRUE;
338         }
339
340         return FALSE;
341 }
342
343 static void generic_unregister(DBusConnection *connection, void *user_data)
344 {
345         struct generic_data *data = user_data;
346
347         g_free(data->introspect);
348         g_free(data);
349 }
350
351 static struct interface_data *find_interface(GSList *interfaces,
352                                                 const char *name)
353 {
354         GSList *list;
355
356         if (!name)
357                 return NULL;
358
359         for (list = interfaces; list; list = list->next) {
360                 struct interface_data *iface = list->data;
361                 if (!strcmp(name, iface->name))
362                         return iface;
363         }
364
365         return NULL;
366 }
367
368 static DBusHandlerResult generic_message(DBusConnection *connection,
369                                         DBusMessage *message, void *user_data)
370 {
371         struct generic_data *data = user_data;
372         struct interface_data *iface;
373         const GDBusMethodTable *method;
374         const char *interface;
375
376         interface = dbus_message_get_interface(message);
377
378         iface = find_interface(data->interfaces, interface);
379         if (!iface)
380                 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
381
382         for (method = iface->methods; method &&
383                         method->name && method->function; method++) {
384                 if (dbus_message_is_method_call(message, iface->name,
385                                                         method->name) == FALSE)
386                         continue;
387
388                 if (dbus_message_has_signature(message,
389                                                 method->signature) == FALSE)
390                         continue;
391
392                 if (check_privilege(connection, message, method,
393                                                 iface->user_data) == TRUE)
394                         return DBUS_HANDLER_RESULT_HANDLED;
395
396                 return process_message(connection, message, method,
397                                                         iface->user_data);
398         }
399
400         return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
401 }
402
403 static DBusObjectPathVTable generic_table = {
404         .unregister_function    = generic_unregister,
405         .message_function       = generic_message,
406 };
407
408 static void invalidate_parent_data(DBusConnection *conn, const char *child_path)
409 {
410         struct generic_data *data = NULL;
411         char *parent_path, *slash;
412
413         parent_path = g_strdup(child_path);
414         slash = strrchr(parent_path, '/');
415         if (!slash)
416                 goto done;
417
418         if (slash == parent_path && parent_path[1] != '\0')
419                 parent_path[1] = '\0';
420         else
421                 *slash = '\0';
422
423         if (!strlen(parent_path))
424                 goto done;
425
426         if (!dbus_connection_get_object_path_data(conn, parent_path,
427                                                         (void *) &data)) {
428                 invalidate_parent_data(conn, parent_path);
429                 goto done;
430         }
431
432         if (!data)
433                 goto done;
434
435         g_free(data->introspect);
436         data->introspect = NULL;
437
438 done:
439         g_free(parent_path);
440 }
441
442 static GDBusMethodTable introspect_methods[] = {
443         { "Introspect", "",     "s", introspect },
444         { }
445 };
446
447 static void add_interface(struct generic_data *data, const char *name,
448                                 const GDBusMethodTable *methods,
449                                 const GDBusSignalTable *signals,
450                                 const GDBusPropertyTable *properties,
451                                 void *user_data,
452                                 GDBusDestroyFunction destroy)
453 {
454         struct interface_data *iface;
455
456         iface = g_new0(struct interface_data, 1);
457         iface->name = g_strdup(name);
458         iface->methods = methods;
459         iface->signals = signals;
460         iface->properties = properties;
461         iface->user_data = user_data;
462         iface->destroy = destroy;
463
464         data->interfaces = g_slist_append(data->interfaces, iface);
465 }
466
467 static struct generic_data *object_path_ref(DBusConnection *connection,
468                                                         const char *path)
469 {
470         struct generic_data *data;
471
472         if (dbus_connection_get_object_path_data(connection, path,
473                                                 (void *) &data) == TRUE) {
474                 if (data != NULL) {
475                         data->refcount++;
476                         return data;
477                 }
478         }
479
480         data = g_new0(struct generic_data, 1);
481         data->refcount = 1;
482
483         data->introspect = g_strdup(DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE "<node></node>");
484
485         if (!dbus_connection_register_object_path(connection, path,
486                                                 &generic_table, data)) {
487                 g_free(data->introspect);
488                 g_free(data);
489                 return NULL;
490         }
491
492         invalidate_parent_data(connection, path);
493
494         add_interface(data, DBUS_INTERFACE_INTROSPECTABLE,
495                         introspect_methods, NULL, NULL, data, NULL);
496
497         return data;
498 }
499
500 static gboolean remove_interface(struct generic_data *data, const char *name)
501 {
502         struct interface_data *iface;
503
504         iface = find_interface(data->interfaces, name);
505         if (!iface)
506                 return FALSE;
507
508         data->interfaces = g_slist_remove(data->interfaces, iface);
509
510         if (iface->destroy)
511                 iface->destroy(iface->user_data);
512
513         g_free(iface->name);
514         g_free(iface);
515
516         return TRUE;
517 }
518
519 static void object_path_unref(DBusConnection *connection, const char *path)
520 {
521         struct generic_data *data = NULL;
522
523         if (dbus_connection_get_object_path_data(connection, path,
524                                                 (void *) &data) == FALSE)
525                 return;
526
527         if (data == NULL)
528                 return;
529
530         data->refcount--;
531
532         if (data->refcount > 0)
533                 return;
534
535         remove_interface(data, DBUS_INTERFACE_INTROSPECTABLE);
536
537         invalidate_parent_data(connection, path);
538
539         dbus_connection_unregister_object_path(connection, path);
540 }
541
542 static gboolean check_signal(DBusConnection *conn, const char *path,
543                                 const char *interface, const char *name,
544                                 const char **args)
545 {
546         struct generic_data *data = NULL;
547         struct interface_data *iface;
548         const GDBusSignalTable *signal;
549
550         *args = NULL;
551         if (!dbus_connection_get_object_path_data(conn, path,
552                                         (void *) &data) || !data) {
553                 error("dbus_connection_emit_signal: path %s isn't registered",
554                                 path);
555                 return FALSE;
556         }
557
558         iface = find_interface(data->interfaces, interface);
559         if (!iface) {
560                 error("dbus_connection_emit_signal: %s does not implement %s",
561                                 path, interface);
562                 return FALSE;
563         }
564
565         for (signal = iface->signals; signal && signal->name; signal++) {
566                 if (!strcmp(signal->name, name)) {
567                         *args = signal->signature;
568                         break;
569                 }
570         }
571
572         if (!*args) {
573                 error("No signal named %s on interface %s", name, interface);
574                 return FALSE;
575         }
576
577         return TRUE;
578 }
579
580 static dbus_bool_t emit_signal_valist(DBusConnection *conn,
581                                                 const char *path,
582                                                 const char *interface,
583                                                 const char *name,
584                                                 int first,
585                                                 va_list var_args)
586 {
587         DBusMessage *signal;
588         dbus_bool_t ret;
589         const char *signature, *args;
590
591         if (!check_signal(conn, path, interface, name, &args))
592                 return FALSE;
593
594         signal = dbus_message_new_signal(path, interface, name);
595         if (!signal) {
596                 error("Unable to allocate new %s.%s signal", interface,  name);
597                 return FALSE;
598         }
599
600         ret = dbus_message_append_args_valist(signal, first, var_args);
601         if (!ret)
602                 goto fail;
603
604         signature = dbus_message_get_signature(signal);
605         if (strcmp(args, signature) != 0) {
606                 error("%s.%s: expected signature'%s' but got '%s'",
607                                 interface, name, args, signature);
608                 ret = FALSE;
609                 goto fail;
610         }
611
612         ret = dbus_connection_send(conn, signal, NULL);
613
614 fail:
615         dbus_message_unref(signal);
616
617         return ret;
618 }
619
620 gboolean g_dbus_register_interface(DBusConnection *connection,
621                                         const char *path, const char *name,
622                                         const GDBusMethodTable *methods,
623                                         const GDBusSignalTable *signals,
624                                         const GDBusPropertyTable *properties,
625                                         void *user_data,
626                                         GDBusDestroyFunction destroy)
627 {
628         struct generic_data *data;
629
630         data = object_path_ref(connection, path);
631         if (data == NULL)
632                 return FALSE;
633
634         if (find_interface(data->interfaces, name)) {
635                 object_path_unref(connection, path);
636                 return FALSE;
637         }
638
639         add_interface(data, name, methods, signals,
640                         properties, user_data, destroy);
641
642         g_free(data->introspect);
643         data->introspect = NULL;
644
645         return TRUE;
646 }
647
648 gboolean g_dbus_unregister_interface(DBusConnection *connection,
649                                         const char *path, const char *name)
650 {
651         struct generic_data *data = NULL;
652
653         if (!path)
654                 return FALSE;
655
656         if (dbus_connection_get_object_path_data(connection, path,
657                                                 (void *) &data) == FALSE)
658                 return FALSE;
659
660         if (data == NULL)
661                 return FALSE;
662
663         if (remove_interface(data, name) == FALSE)
664                 return FALSE;
665
666         g_free(data->introspect);
667         data->introspect = NULL;
668
669         object_path_unref(connection, path);
670
671         return TRUE;
672 }
673
674 gboolean g_dbus_register_security(const GDBusSecurityTable *security)
675 {
676         if (security_table != NULL)
677                 return FALSE;
678
679         security_table = security;
680
681         return TRUE;
682 }
683
684 gboolean g_dbus_unregister_security(const GDBusSecurityTable *security)
685 {
686         security_table = NULL;
687
688         return TRUE;
689 }
690
691 DBusMessage *g_dbus_create_error_valist(DBusMessage *message, const char *name,
692                                         const char *format, va_list args)
693 {
694         char str[1024];
695
696         vsnprintf(str, sizeof(str), format, args);
697
698         return dbus_message_new_error(message, name, str);
699 }
700
701 DBusMessage *g_dbus_create_error(DBusMessage *message, const char *name,
702                                                 const char *format, ...)
703 {
704         va_list args;
705         DBusMessage *reply;
706
707         va_start(args, format);
708
709         reply = g_dbus_create_error_valist(message, name, format, args);
710
711         va_end(args);
712
713         return reply;
714 }
715
716 DBusMessage *g_dbus_create_reply_valist(DBusMessage *message,
717                                                 int type, va_list args)
718 {
719         DBusMessage *reply;
720
721         reply = dbus_message_new_method_return(message);
722         if (reply == NULL)
723                 return NULL;
724
725         if (dbus_message_append_args_valist(reply, type, args) == FALSE) {
726                 dbus_message_unref(reply);
727                 return NULL;
728         }
729
730         return reply;
731 }
732
733 DBusMessage *g_dbus_create_reply(DBusMessage *message, int type, ...)
734 {
735         va_list args;
736         DBusMessage *reply;
737
738         va_start(args, type);
739
740         reply = g_dbus_create_reply_valist(message, type, args);
741
742         va_end(args);
743
744         return reply;
745 }
746
747 gboolean g_dbus_send_message(DBusConnection *connection, DBusMessage *message)
748 {
749         dbus_bool_t result;
750
751         if (dbus_message_get_type(message) == DBUS_MESSAGE_TYPE_METHOD_CALL)
752                 dbus_message_set_no_reply(message, TRUE);
753
754         result = dbus_connection_send(connection, message, NULL);
755
756         dbus_message_unref(message);
757
758         return result;
759 }
760
761 gboolean g_dbus_send_reply_valist(DBusConnection *connection,
762                                 DBusMessage *message, int type, va_list args)
763 {
764         DBusMessage *reply;
765
766         reply = dbus_message_new_method_return(message);
767         if (reply == NULL)
768                 return FALSE;
769
770         if (dbus_message_append_args_valist(reply, type, args) == FALSE) {
771                 dbus_message_unref(reply);
772                 return FALSE;
773         }
774
775         return g_dbus_send_message(connection, reply);
776 }
777
778 gboolean g_dbus_send_reply(DBusConnection *connection,
779                                 DBusMessage *message, int type, ...)
780 {
781         va_list args;
782         gboolean result;
783
784         va_start(args, type);
785
786         result = g_dbus_send_reply_valist(connection, message, type, args);
787
788         va_end(args);
789
790         return result;
791 }
792
793 gboolean g_dbus_emit_signal(DBusConnection *connection,
794                                 const char *path, const char *interface,
795                                 const char *name, int type, ...)
796 {
797         va_list args;
798         gboolean result;
799
800         va_start(args, type);
801
802         result = emit_signal_valist(connection, path, interface,
803                                                         name, type, args);
804
805         va_end(args);
806
807         return result;
808 }
809
810 gboolean g_dbus_emit_signal_valist(DBusConnection *connection,
811                                 const char *path, const char *interface,
812                                 const char *name, int type, va_list args)
813 {
814         return emit_signal_valist(connection, path, interface,
815                                                         name, type, args);
816 }