Add support for GDBus security action and flags
[framework/connectivity/connman.git] / gdbus / object.c
index 2823054..725c0bb 100644 (file)
@@ -2,7 +2,7 @@
  *
  *  D-Bus helper library
  *
- *  Copyright (C) 2004-2009  Marcel Holtmann <marcel@holtmann.org>
+ *  Copyright (C) 2004-2010  Marcel Holtmann <marcel@holtmann.org>
  *
  *
  *  This program is free software; you can redistribute it and/or modify
@@ -45,13 +45,20 @@ struct generic_data {
 
 struct interface_data {
        char *name;
-       GDBusMethodTable *methods;
-       GDBusSignalTable *signals;
-       GDBusPropertyTable *properties;
+       const GDBusMethodTable *methods;
+       const GDBusSignalTable *signals;
+       const GDBusPropertyTable *properties;
        void *user_data;
        GDBusDestroyFunction destroy;
 };
 
+struct security_data {
+       GDBusPendingReply pending;
+       DBusMessage *message;
+       const GDBusMethodTable *method;
+       void *iface_user_data;
+};
+
 static void print_arguments(GString *gstr, const char *sig,
                                                const char *direction)
 {
@@ -114,8 +121,8 @@ static void print_arguments(GString *gstr, const char *sig,
 
 static void generate_interface_xml(GString *gstr, struct interface_data *iface)
 {
-       GDBusMethodTable *method;
-       GDBusSignalTable *signal;
+       const GDBusMethodTable *method;
+       const GDBusSignalTable *signal;
 
        for (method = iface->methods; method && method->name; method++) {
                if (!strlen(method->signature) && !strlen(method->reply))
@@ -183,14 +190,15 @@ done:
        data->introspect = g_string_free(gstr, FALSE);
 }
 
-static DBusHandlerResult introspect(DBusConnection *connection,
-                               DBusMessage *message, struct generic_data *data)
+static DBusMessage *introspect(DBusConnection *connection,
+                               DBusMessage *message, void *user_data)
 {
+       struct generic_data *data = user_data;
        DBusMessage *reply;
 
        if (!dbus_message_has_signature(message, DBUS_TYPE_INVALID_AS_STRING)) {
                error("Unexpected signature to introspect call");
-               return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
+               return NULL;
        }
 
        if (!data->introspect)
@@ -199,18 +207,147 @@ static DBusHandlerResult introspect(DBusConnection *connection,
 
        reply = dbus_message_new_method_return(message);
        if (!reply)
-               return DBUS_HANDLER_RESULT_NEED_MEMORY;
+               return NULL;
 
        dbus_message_append_args(reply, DBUS_TYPE_STRING, &data->introspect,
                                        DBUS_TYPE_INVALID);
 
-       dbus_connection_send(connection, reply, NULL);
+       return reply;
+}
+
+static DBusHandlerResult process_message(DBusConnection *connection,
+                       DBusMessage *message, const GDBusMethodTable *method,
+                                                       void *iface_user_data)
+{
+       DBusMessage *reply;
+
+       reply = method->function(connection, message, iface_user_data);
+
+       if (method->flags & G_DBUS_METHOD_FLAG_NOREPLY) {
+               if (reply != NULL)
+                       dbus_message_unref(reply);
+               return DBUS_HANDLER_RESULT_HANDLED;
+       }
+
+       if (method->flags & G_DBUS_METHOD_FLAG_ASYNC) {
+               if (reply == NULL)
+                       return DBUS_HANDLER_RESULT_HANDLED;
+       }
 
+       if (reply == NULL)
+               return DBUS_HANDLER_RESULT_NEED_MEMORY;
+
+       dbus_connection_send(connection, reply, NULL);
        dbus_message_unref(reply);
 
        return DBUS_HANDLER_RESULT_HANDLED;
 }
 
+static GDBusPendingReply next_pending = 1;
+static GSList *pending_security = NULL;
+
+static const GDBusSecurityTable *security_table = NULL;
+
+void g_dbus_pending_success(DBusConnection *connection,
+                                       GDBusPendingReply pending)
+{
+       GSList *list;
+
+        for (list = pending_security; list; list = list->next) {
+               struct security_data *secdata = list->data;
+               DBusHandlerResult result;
+
+               if (secdata->pending != pending)
+                       continue;
+
+               pending_security = g_slist_remove(pending_security, secdata);
+
+               result = process_message(connection, secdata->message,
+                               secdata->method, secdata->iface_user_data);
+
+               dbus_message_unref(secdata->message);
+               g_free(secdata);
+               return;
+        }
+}
+
+void g_dbus_pending_error_valist(DBusConnection *connection,
+                               GDBusPendingReply pending, const char *name,
+                                       const char *format, va_list args)
+{
+       GSList *list;
+
+        for (list = pending_security; list; list = list->next) {
+               struct security_data *secdata = list->data;
+               DBusMessage *reply;
+
+               if (secdata->pending != pending)
+                       continue;
+
+               pending_security = g_slist_remove(pending_security, secdata);
+
+               reply = g_dbus_create_error_valist(secdata->message,
+                                                       name, format, args);
+               if (reply != NULL) {
+                       dbus_connection_send(connection, reply, NULL);
+                       dbus_message_unref(reply);
+               }
+
+               dbus_message_unref(secdata->message);
+               g_free(secdata);
+               return;
+        }
+}
+
+void g_dbus_pending_error(DBusConnection *connection,
+                               GDBusPendingReply pending,
+                               const char *name, const char *format, ...)
+{
+       va_list args;
+
+       va_start(args, format);
+
+       g_dbus_pending_error_valist(connection, pending, name, format, args);
+
+       va_end(args);
+}
+
+static gboolean check_privilege(DBusConnection *conn, DBusMessage *msg,
+                       const GDBusMethodTable *method, void *iface_user_data)
+{
+       const GDBusSecurityTable *security;
+
+       for (security = security_table; security && security->privilege;
+                                                               security++) {
+               struct security_data *secdata;
+               gboolean interaction;
+
+               if (security->privilege != method->privilege)
+                       continue;
+
+               secdata = g_new(struct security_data, 1);
+               secdata->pending = next_pending++;
+               secdata->message = dbus_message_ref(msg);
+               secdata->method = method;
+               secdata->iface_user_data = iface_user_data;
+
+               pending_security = g_slist_prepend(pending_security, secdata);
+
+               if (security->flags & G_DBUS_SECURITY_FLAG_ALLOW_INTERACTION)
+                       interaction = TRUE;
+               else
+                       interaction = FALSE;
+
+               if (security->function)
+                       security->function(conn, security->action,
+                                               interaction, secdata->pending);
+
+               return TRUE;
+       }
+
+       return FALSE;
+}
+
 static void generic_unregister(DBusConnection *connection, void *user_data)
 {
        struct generic_data *data = user_data;
@@ -241,14 +378,9 @@ static DBusHandlerResult generic_message(DBusConnection *connection,
 {
        struct generic_data *data = user_data;
        struct interface_data *iface;
-       GDBusMethodTable *method;
+       const GDBusMethodTable *method;
        const char *interface;
 
-       if (dbus_message_is_method_call(message,
-                                       DBUS_INTERFACE_INTROSPECTABLE,
-                                                               "Introspect"))
-               return introspect(connection, message, data);
-
        interface = dbus_message_get_interface(message);
 
        iface = find_interface(data->interfaces, interface);
@@ -257,8 +389,6 @@ static DBusHandlerResult generic_message(DBusConnection *connection,
 
        for (method = iface->methods; method &&
                        method->name && method->function; method++) {
-               DBusMessage *reply;
-
                if (dbus_message_is_method_call(message, iface->name,
                                                        method->name) == FALSE)
                        continue;
@@ -267,26 +397,12 @@ static DBusHandlerResult generic_message(DBusConnection *connection,
                                                method->signature) == FALSE)
                        continue;
 
-               reply = method->function(connection, message, iface->user_data);
-
-               if (method->flags & G_DBUS_METHOD_FLAG_NOREPLY) {
-                       if (reply != NULL)
-                               dbus_message_unref(reply);
+               if (check_privilege(connection, message, method,
+                                               iface->user_data) == TRUE)
                        return DBUS_HANDLER_RESULT_HANDLED;
-               }
-
-               if (method->flags & G_DBUS_METHOD_FLAG_ASYNC) {
-                       if (reply == NULL)
-                               return DBUS_HANDLER_RESULT_HANDLED;
-               }
 
-               if (reply == NULL)
-                       return DBUS_HANDLER_RESULT_NEED_MEMORY;
-
-               dbus_connection_send(connection, reply, NULL);
-               dbus_message_unref(reply);
-
-               return DBUS_HANDLER_RESULT_HANDLED;
+               return process_message(connection, message, method,
+                                                       iface->user_data);
        }
 
        return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
@@ -307,13 +423,19 @@ static void invalidate_parent_data(DBusConnection *conn, const char *child_path)
        if (!slash)
                goto done;
 
-       *slash = '\0';
+       if (slash == parent_path && parent_path[1] != '\0')
+               parent_path[1] = '\0';
+       else
+               *slash = '\0';
+
        if (!strlen(parent_path))
                goto done;
 
        if (!dbus_connection_get_object_path_data(conn, parent_path,
-                                                       (void *) &data))
+                                                       (void *) &data)) {
+               invalidate_parent_data(conn, parent_path);
                goto done;
+       }
 
        if (!data)
                goto done;
@@ -325,6 +447,31 @@ done:
        g_free(parent_path);
 }
 
+static GDBusMethodTable introspect_methods[] = {
+       { "Introspect", "",     "s", introspect },
+       { }
+};
+
+static void add_interface(struct generic_data *data, const char *name,
+                               const GDBusMethodTable *methods,
+                               const GDBusSignalTable *signals,
+                               const GDBusPropertyTable *properties,
+                               void *user_data,
+                               GDBusDestroyFunction destroy)
+{
+       struct interface_data *iface;
+
+       iface = g_new0(struct interface_data, 1);
+       iface->name = g_strdup(name);
+       iface->methods = methods;
+       iface->signals = signals;
+       iface->properties = properties;
+       iface->user_data = user_data;
+       iface->destroy = destroy;
+
+       data->interfaces = g_slist_append(data->interfaces, iface);
+}
+
 static struct generic_data *object_path_ref(DBusConnection *connection,
                                                        const char *path)
 {
@@ -339,11 +486,10 @@ static struct generic_data *object_path_ref(DBusConnection *connection,
        }
 
        data = g_new0(struct generic_data, 1);
+       data->refcount = 1;
 
        data->introspect = g_strdup(DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE "<node></node>");
 
-       data->refcount = 1;
-
        if (!dbus_connection_register_object_path(connection, path,
                                                &generic_table, data)) {
                g_free(data->introspect);
@@ -353,9 +499,31 @@ static struct generic_data *object_path_ref(DBusConnection *connection,
 
        invalidate_parent_data(connection, path);
 
+       add_interface(data, DBUS_INTERFACE_INTROSPECTABLE,
+                       introspect_methods, NULL, NULL, data, NULL);
+
        return data;
 }
 
+static gboolean remove_interface(struct generic_data *data, const char *name)
+{
+       struct interface_data *iface;
+
+       iface = find_interface(data->interfaces, name);
+       if (!iface)
+               return FALSE;
+
+       data->interfaces = g_slist_remove(data->interfaces, iface);
+
+       if (iface->destroy)
+               iface->destroy(iface->user_data);
+
+       g_free(iface->name);
+       g_free(iface);
+
+       return TRUE;
+}
+
 static void object_path_unref(DBusConnection *connection, const char *path)
 {
        struct generic_data *data = NULL;
@@ -372,6 +540,8 @@ static void object_path_unref(DBusConnection *connection, const char *path)
        if (data->refcount > 0)
                return;
 
+       remove_interface(data, DBUS_INTERFACE_INTROSPECTABLE);
+
        invalidate_parent_data(connection, path);
 
        dbus_connection_unregister_object_path(connection, path);
@@ -383,7 +553,7 @@ static gboolean check_signal(DBusConnection *conn, const char *path,
 {
        struct generic_data *data = NULL;
        struct interface_data *iface;
-       GDBusSignalTable *signal;
+       const GDBusSignalTable *signal;
 
        *args = NULL;
        if (!dbus_connection_get_object_path_data(conn, path,
@@ -457,32 +627,25 @@ fail:
 
 gboolean g_dbus_register_interface(DBusConnection *connection,
                                        const char *path, const char *name,
-                                       GDBusMethodTable *methods,
-                                       GDBusSignalTable *signals,
-                                       GDBusPropertyTable *properties,
+                                       const GDBusMethodTable *methods,
+                                       const GDBusSignalTable *signals,
+                                       const GDBusPropertyTable *properties,
                                        void *user_data,
                                        GDBusDestroyFunction destroy)
 {
        struct generic_data *data;
-       struct interface_data *iface;
 
        data = object_path_ref(connection, path);
        if (data == NULL)
                return FALSE;
 
-       if (find_interface(data->interfaces, name))
+       if (find_interface(data->interfaces, name)) {
+               object_path_unref(connection, path);
                return FALSE;
+       }
 
-       iface = g_new0(struct interface_data, 1);
-
-       iface->name = g_strdup(name);
-       iface->methods = methods;
-       iface->signals = signals;
-       iface->properties = properties;
-       iface->user_data = user_data;
-       iface->destroy = destroy;
-
-       data->interfaces = g_slist_append(data->interfaces, iface);
+       add_interface(data, name, methods, signals,
+                       properties, user_data, destroy);
 
        g_free(data->introspect);
        data->introspect = NULL;
@@ -494,7 +657,6 @@ gboolean g_dbus_unregister_interface(DBusConnection *connection,
                                        const char *path, const char *name)
 {
        struct generic_data *data = NULL;
-       struct interface_data *iface;
 
        if (!path)
                return FALSE;
@@ -506,18 +668,9 @@ gboolean g_dbus_unregister_interface(DBusConnection *connection,
        if (data == NULL)
                return FALSE;
 
-       iface = find_interface(data->interfaces, name);
-       if (!iface)
+       if (remove_interface(data, name) == FALSE)
                return FALSE;
 
-       data->interfaces = g_slist_remove(data->interfaces, iface);
-
-       if (iface->destroy)
-               iface->destroy(iface->user_data);
-
-       g_free(iface->name);
-       g_free(iface);
-
        g_free(data->introspect);
        data->introspect = NULL;
 
@@ -526,6 +679,23 @@ gboolean g_dbus_unregister_interface(DBusConnection *connection,
        return TRUE;
 }
 
+gboolean g_dbus_register_security(const GDBusSecurityTable *security)
+{
+       if (security_table != NULL)
+               return FALSE;
+
+       security_table = security;
+
+       return TRUE;
+}
+
+gboolean g_dbus_unregister_security(const GDBusSecurityTable *security)
+{
+       security_table = NULL;
+
+       return TRUE;
+}
+
 DBusMessage *g_dbus_create_error_valist(DBusMessage *message, const char *name,
                                        const char *format, va_list args)
 {