From 9491a544f622e40453265c30f24ce44a61440cc1 Mon Sep 17 00:00:00 2001 From: Claudio Takahasi Date: Thu, 1 May 2008 13:52:26 +0000 Subject: Moving authorization code to dbus-service.c --- audio/main.c | 5 +-- audio/manager.c | 10 ++--- hcid/dbus-service.c | 108 ++++++++++++++++++++++++++++++++++++++++++++++++ hcid/dbus-service.h | 5 +++ hcid/plugin.c | 116 ++-------------------------------------------------- hcid/plugin.h | 6 --- input/main.c | 5 +-- input/server.c | 7 ++-- network/main.c | 4 +- network/server.c | 6 +-- plugins/echo.c | 3 +- 11 files changed, 133 insertions(+), 142 deletions(-) diff --git a/audio/main.c b/audio/main.c index 22a5e20a..eaa04806 100644 --- a/audio/main.c +++ b/audio/main.c @@ -29,14 +29,13 @@ #include #include #include -#include #include -#include + +#include "dbus.h" #include "plugin.h" #include "dbus-service.h" -#include "dbus.h" #include "logging.h" #include "unix.h" #include "device.h" diff --git a/audio/manager.c b/audio/manager.c index 9211ccb7..f33dc963 100644 --- a/audio/manager.c +++ b/audio/manager.c @@ -49,7 +49,9 @@ #include #include "dbus-helper.h" -#include "dbus.h" +#include "glib-helper.h" + +#include "dbus-service.h" #include "logging.h" #include "textfile.h" #include "ipc.h" @@ -63,8 +65,6 @@ #include "control.h" #include "manager.h" #include "sdpd.h" -#include "plugin.h" -#include "glib-helper.h" typedef enum { HEADSET = 1 << 0, @@ -1195,7 +1195,7 @@ static void auth_cb(DBusError *derr, void *user_data) error("Access denied: %s", derr->message); if (dbus_error_has_name(derr, DBUS_ERROR_NO_REPLY)) { debug("Canceling authorization request"); - if (plugin_cancel_auth(&device->dst) < 0) + if (service_cancel_auth(&device->dst) < 0) manager_cancel_authorize(&device->dst, uuid, NULL); } @@ -1283,7 +1283,7 @@ static gboolean ag_io_cb(GIOChannel *chan, GIOCondition cond, void *data) return TRUE; } - if (plugin_req_auth(&device->src, &device->dst, uuid, auth_cb, + if (service_req_auth(&device->src, &device->dst, uuid, auth_cb, device) == 0) goto proceed; else if (!manager_authorize(&device->dst, uuid, auth_cb_old, device, diff --git a/hcid/dbus-service.c b/hcid/dbus-service.c index be41b22e..c446a6e4 100644 --- a/hcid/dbus-service.c +++ b/hcid/dbus-service.c @@ -36,6 +36,9 @@ #include #include +#include +#include +#include #include #include @@ -52,6 +55,8 @@ #include "error.h" #include "manager.h" #include "adapter.h" +#include "agent.h" +#include "device.h" #include "dbus-service.h" #include "dbus-hci.h" @@ -70,6 +75,11 @@ struct service_uuids { char **uuids; }; +struct service_auth { + service_auth_cb cb; + void *user_data; +}; + static GSList *services = NULL; static GSList *services_uuids = NULL; static GSList *removed = NULL; @@ -1196,3 +1206,101 @@ void unregister_uuids(const char *name) service_uuids_free(su); } + +static struct adapter *ba2adapter(bdaddr_t *src) +{ + DBusConnection *conn = get_dbus_connection(); + struct adapter *adapter = NULL; + char address[18], path[6]; + int dev_id; + + ba2str(src, address); + dev_id = hci_devid(address); + if (dev_id < 0) + return NULL; + + /* FIXME: id2adapter? Create a list of adapters? */ + snprintf(path, sizeof(path), "/hci%d", dev_id); + if (dbus_connection_get_object_user_data(conn, + path, (void *) &adapter) == FALSE) + return NULL; + + return adapter; +} + +static void agent_auth_cb(struct agent *agent, DBusError *derr, void *user_data) +{ + struct service_auth *auth = user_data; + + auth->cb(derr, auth->user_data); + + g_free(auth); +} +int service_req_auth(bdaddr_t *src, bdaddr_t *dst, + const char *uuid, service_auth_cb cb, void *user_data) +{ + struct service_auth *auth; + struct adapter *adapter; + struct device *device; + struct agent *agent; + char address[18]; + + adapter = ba2adapter(src); + if (!adapter) + return -EPERM; + + /* Device connected? */ + if (!g_slist_find_custom(adapter->active_conn, + dst, active_conn_find_by_bdaddr)) + return -ENOTCONN; + + ba2str(dst, address); + device = adapter_find_device(adapter, address); + if (!device) + return -EPERM; + + if (!search_service_by_uuid(uuid)) + return -EPERM; + + /* FIXME: Missing check trusted file entries */ + + agent = (device->agent ? : adapter->agent); + if (!agent) + return -EPERM; + + auth = g_try_new0(struct service_auth, 1); + if (!auth) + return -ENOMEM; + + auth->cb = cb; + auth->user_data = user_data; + + return agent_authorize(agent, device->path, uuid, agent_auth_cb, auth); +} + +int service_cancel_auth(bdaddr_t *src) +{ + struct adapter *adapter = ba2adapter(src); + struct device *device; + struct agent *agent; + char address[18]; + + if (!adapter) + return -EPERM; + + ba2str(src, address); + device = adapter_find_device(adapter, address); + if (!device) + return -EPERM; + + /* + * FIXME: Cancel fails if authorization is requested to adapter's + * agent and in the meanwhile CreatePairedDevice is called. + */ + + agent = (device->agent ? : adapter->agent); + if (!agent) + return -EPERM; + + return agent_cancel(agent); +} diff --git a/hcid/dbus-service.h b/hcid/dbus-service.h index d7661789..72a23f25 100644 --- a/hcid/dbus-service.h +++ b/hcid/dbus-service.h @@ -66,3 +66,8 @@ int service_unregister(DBusConnection *conn, struct service *service); void register_uuids(const char *name, const char **uuids); void unregister_uuids(const char *name); + +typedef void (*service_auth_cb) (DBusError *derr, void *user_data); +int service_req_auth(bdaddr_t *src, bdaddr_t *dst, + const char *uuid, service_auth_cb cb, void *user_data); +int service_cancel_auth(bdaddr_t *src); diff --git a/hcid/plugin.c b/hcid/plugin.c index 6cc54de1..2a8f4375 100644 --- a/hcid/plugin.c +++ b/hcid/plugin.c @@ -33,18 +33,12 @@ #include #include -#include -#include -#include -#include "dbus-helper.h" +#include "dbus.h" +#include "logging.h" + #include "dbus-service.h" -#include "adapter.h" -#include "dbus-hci.h" -#include "agent.h" #include "plugin.h" -#include "device.h" -#include "logging.h" static GSList *plugins = NULL; @@ -53,11 +47,6 @@ struct bluetooth_plugin { struct bluetooth_plugin_desc *desc; }; -struct plugin_auth { - plugin_auth_cb cb; - void *user_data; -}; - static gboolean add_plugin(GModule *module, struct bluetooth_plugin_desc *desc) { struct bluetooth_plugin *plugin; @@ -162,102 +151,3 @@ void plugin_cleanup(void) g_slist_free(plugins); } - -static struct adapter *ba2adapter(bdaddr_t *src) -{ - DBusConnection *conn = get_dbus_connection(); - struct adapter *adapter = NULL; - char address[18], path[6]; - int dev_id; - - ba2str(src, address); - dev_id = hci_devid(address); - if (dev_id < 0) - return NULL; - - /* FIXME: id2adapter? Create a list of adapters? */ - snprintf(path, sizeof(path), "/hci%d", dev_id); - if (dbus_connection_get_object_user_data(conn, - path, (void *) &adapter) == FALSE) - return NULL; - - return adapter; -} - -static void agent_auth_cb(struct agent *agent, DBusError *derr, void *user_data) -{ - struct plugin_auth *auth = user_data; - - auth->cb(derr, auth->user_data); - - g_free(auth); -} - -int plugin_req_auth(bdaddr_t *src, bdaddr_t *dst, - const char *uuid, plugin_auth_cb cb, void *user_data) -{ - struct plugin_auth *auth; - struct adapter *adapter; - struct device *device; - struct agent *agent; - char address[18]; - - adapter = ba2adapter(src); - if (!adapter) - return -EPERM; - - /* Device connected? */ - if (!g_slist_find_custom(adapter->active_conn, - dst, active_conn_find_by_bdaddr)) - return -ENOTCONN; - - ba2str(dst, address); - device = adapter_find_device(adapter, address); - if (!device) - return -EPERM; - - if (!search_service_by_uuid(uuid)) - return -EPERM; - - /* FIXME: Missing check trusted file entries */ - - agent = (device->agent ? : adapter->agent); - if (!agent) - return -EPERM; - - auth = g_try_new0(struct plugin_auth, 1); - if (!auth) - return -ENOMEM; - - auth->cb = cb; - auth->user_data = user_data; - - return agent_authorize(agent, device->path, uuid, agent_auth_cb, auth); -} - -int plugin_cancel_auth(bdaddr_t *src) -{ - struct adapter *adapter = ba2adapter(src); - struct device *device; - struct agent *agent; - char address[18]; - - if (!adapter) - return -EPERM; - - ba2str(src, address); - device = adapter_find_device(adapter, address); - if (!device) - return -EPERM; - - /* - * FIXME: Cancel fails if authorization is requested to adapter's - * agent and in the meanwhile CreatePairedDevice is called. - */ - - agent = (device->agent ? : adapter->agent); - if (!agent) - return -EPERM; - - return agent_cancel(agent); -} diff --git a/hcid/plugin.h b/hcid/plugin.h index 09f617e2..9248aab6 100644 --- a/hcid/plugin.h +++ b/hcid/plugin.h @@ -31,9 +31,3 @@ struct bluetooth_plugin_desc { struct bluetooth_plugin_desc bluetooth_plugin_desc = { \ name, init, exit \ }; - - -typedef void (*plugin_auth_cb) (DBusError *derr, void *user_data); -int plugin_req_auth(bdaddr_t *src, bdaddr_t *dst, - const char *uuid, plugin_auth_cb cb, void *user_data); -int plugin_cancel_auth(bdaddr_t *src); diff --git a/input/main.c b/input/main.c index dab730b7..d8236ada 100644 --- a/input/main.c +++ b/input/main.c @@ -28,12 +28,11 @@ #include #include -#include -#include + +#include "dbus.h" #include "plugin.h" #include "dbus-service.h" -#include "dbus.h" #include "manager.h" #define HID_UUID "00001124-0000-1000-8000-00805f9b34fb" diff --git a/input/server.c b/input/server.c index 6cfd0d37..ff7cdbfd 100644 --- a/input/server.c +++ b/input/server.c @@ -45,8 +45,7 @@ #include "device.h" #include "server.h" #include "storage.h" - -#include "plugin.h" +#include "dbus-service.h" static const char* HID_UUID = "00001124-0000-1000-8000-00805f9b34fb"; @@ -111,7 +110,7 @@ static void auth_callback(DBusError *derr, void *user_data) if (derr) { error("Access denied: %s", derr->message); if (dbus_error_has_name(derr, DBUS_ERROR_NO_REPLY)) - plugin_cancel_auth(&auth->dst); + service_cancel_auth(&auth->dst); input_device_close_channels(&auth->src, &auth->dst); } else @@ -133,7 +132,7 @@ static int authorize_device(bdaddr_t *src, bdaddr_t *dst) bacpy(&auth->src, src); bacpy(&auth->dst, dst); - retval = plugin_req_auth(src, dst, HID_UUID, + retval = service_req_auth(src, dst, HID_UUID, auth_callback, auth); if (retval < 0) goto fallback; diff --git a/network/main.c b/network/main.c index 576ead8e..037ee062 100644 --- a/network/main.c +++ b/network/main.c @@ -29,12 +29,10 @@ #include #include -#include -#include +#include "dbus.h" #include "plugin.h" #include "dbus-service.h" -#include "dbus.h" #include "logging.h" #include "manager.h" diff --git a/network/server.c b/network/server.c index f3e6a995..0915d9d4 100644 --- a/network/server.c +++ b/network/server.c @@ -46,10 +46,10 @@ #include "logging.h" #include "dbus.h" -#include "plugin.h" #include "error.h" #include "textfile.h" #include "dbus-helper.h" +#include "dbus-service.h" #include "sdpd.h" #define NETWORK_SERVER_INTERFACE "org.bluez.network.Server" @@ -410,7 +410,7 @@ static void req_auth_cb(DBusError *derr, void *user_data) if (dbus_error_has_name(derr, DBUS_ERROR_NO_REPLY)) { bdaddr_t dst; str2ba(setup->address, &dst); - plugin_cancel_auth(&dst); + service_cancel_auth(&dst); } val = BNEP_CONN_NOT_ALLOWED; goto done; @@ -471,7 +471,7 @@ static int authorize_connection(struct network_server *ns, const char *address) uuid = bnep_uuid(ns->id); str2ba(address, &dst); - ret_val = plugin_req_auth(&ns->src, &dst, uuid, req_auth_cb, ns); + ret_val = service_req_auth(&ns->src, &dst, uuid, req_auth_cb, ns); if (ret_val < 0) return req_auth_old(address, uuid, ns); else diff --git a/plugins/echo.c b/plugins/echo.c index 153d33e9..1aa15681 100644 --- a/plugins/echo.c +++ b/plugins/echo.c @@ -31,12 +31,11 @@ #include #include -#include -#include #include #include "dbus.h" + #include "plugin.h" #include "server.h" #include "logging.h" -- cgit