Modify dbus policies based on default deny
[platform/core/connectivity/bluetooth-frwk.git] / bt-api / bt-common.c
index b049640..d9ab407 100644 (file)
@@ -83,6 +83,7 @@ GDBusConnection *g_bus_get_private_conn(void)
                                NULL, /* GDBusAuthObserver */
                                NULL,
                                &error);
+       g_free(address);
        if (!private_gconn) {
                if (error) {
                        BT_ERR("Unable to connect to dbus: %s", error->message);
@@ -404,6 +405,8 @@ const char *_bt_convert_error_to_string(int error)
                return "NOT_INITIALIZED";
        case BLUETOOTH_ERROR_AUTHENTICATION_REJECTED:
                return "AUTHENTICATION REJECTED";
+       case BLUETOOTH_ERROR_AUTHORIZATION_REJECTED:
+               return "AUTHORIZATION REJECTED";
        default:
                return "UNKNOWN";
        }
@@ -1301,7 +1304,7 @@ void _bt_unregister_profile(char *path)
 GDBusNodeInfo * _bt_get_gdbus_node(const gchar *xml_data)
 {
        if (bus_id == 0) {
-               char *name = g_strdup_printf("org.bt.frwk%d", getpid());
+               char *name = g_strdup_printf("org.bt.frwk.p%d", getpid());
 
                bus_id = g_bus_own_name(G_BUS_TYPE_SYSTEM,
                                                name,
@@ -1509,13 +1512,13 @@ int _bt_discover_service_uuids(char *address, char *remote_uuid)
        proxy = g_dbus_proxy_new_sync(gconn, G_DBUS_PROXY_FLAGS_NONE, NULL,
                                BT_BLUEZ_NAME, object_path, BT_PROPERTIES_INTERFACE, NULL,
                                &err);
-
        g_free(object_path);
-
-       retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
-       if (err) {
-               BT_ERR("DBus Error: [%s]", err->message);
-               g_clear_error(&err);
+       if (proxy == NULL) {
+               if (err) {
+                       BT_ERR("DBus Error: [%s]", err->message);
+                       g_clear_error(&err);
+               }
+               return BLUETOOTH_ERROR_INTERNAL;
        }
        ret = g_dbus_proxy_call_sync(proxy, "Get",
                        g_variant_new("(ss)", BT_DEVICE_INTERFACE, "UUIDs"),
@@ -1569,24 +1572,24 @@ int _bt_get_cod_by_address(char *address, bluetooth_device_class_t *dev_class)
        GVariant *value = NULL;
        GVariant *result = NULL;
        unsigned int  class = 0x00;
-       int ret = BLUETOOTH_ERROR_INTERNAL;
+       int ret = BLUETOOTH_ERROR_NONE;
 
        gconn = __get_gdbus_connection();
        retv_if(gconn == NULL, BLUETOOTH_ERROR_INTERNAL);
-               object_path = _bt_get_device_object_path(address);
 
+       object_path = _bt_get_device_object_path(address);
        retv_if(object_path == NULL, BLUETOOTH_ERROR_INTERNAL);
 
        proxy = g_dbus_proxy_new_sync(gconn, G_DBUS_PROXY_FLAGS_NONE, NULL,
                                BT_BLUEZ_NAME, object_path, BT_PROPERTIES_INTERFACE, NULL,
                                &err);
-
-       g_free(object_path);
-
-       retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
-       if (err) {
-               BT_ERR("DBus Error: [%s]", err->message);
-               g_clear_error(&err);
+       if (proxy == NULL) {
+               ret = BLUETOOTH_ERROR_INTERNAL;
+               if (err) {
+                       BT_ERR("DBus Error: [%s]", err->message);
+                       g_clear_error(&err);
+               }
+               goto done;
        }
 
        result = g_dbus_proxy_call_sync(proxy, "Get",
@@ -1606,6 +1609,7 @@ int _bt_get_cod_by_address(char *address, bluetooth_device_class_t *dev_class)
        }
 
 done:
+       g_free(object_path);
        if (proxy)
                g_object_unref(proxy);
 
@@ -2013,13 +2017,17 @@ void _bt_set_adapter_internal_status(gboolean enabled)
 
 BT_EXPORT_API int bluetooth_get_uuid_name(const char *uuid, char **name)
 {
-       int i;
+#define SHORT_UUID_COUNT       162
+#define LONG_UUID_COUNT                17
+
        int offset = 0;
        int uuid_len = 4;
-       static struct {
+       typedef struct {
                const char *uuid;
                const char *specification_name;
-       } bt_uuid_name[] = {
+       } uuid_name_s;
+       static uuid_name_s short_uuid_name[SHORT_UUID_COUNT] = {
+               // List should be sorted by UUID
                /* BT Classic Services */
                {"1101", "Serial Port Service"},
                {"1102", "LAN Access Using PPP Service"},
@@ -2191,26 +2199,33 @@ BT_EXPORT_API int bluetooth_get_uuid_name(const char *uuid, char **name)
                {"2A68", "Navigation"},
                {"2A6D", "Pressure"},
                {"2A6E", "Temperature"},
-
+       };
+       static uuid_name_s long_uuid_name[LONG_UUID_COUNT] = {
+               // List should be sorted by UUID
                /* Custom uuids */
-               {"7905F431-B5CE-4E99-A40F-4B1E122D00D0", "Apple Notification Center Service"},
-               {"9FBF120D-6301-42D9-8C58-25E699A21DBD", "Notifications Source"},
-               {"69D1D8F3-45E1-49A8-9821-9BBDFDAAD9D9", "Control Point"},
+               {"1AB7C24D-185A-45B9-90D4-F7AB1A71949A", "Samsung Health Service"},
                {"22EAC6E9-24D6-4BB5-BE44-B36ACE7C7BFB", "Data Source"},
+               {"2F7CABCE-808D-411F-9A0C-BB92BA96C102", "Entity Update"},
+               {"32D1955A-E5AA-4A96-9A49-08538DA8B8F6", "Samsung Gear Fit Manager Service"},
+               {"69D1D8F3-45E1-49A8-9821-9BBDFDAAD9D9", "Control Point"},
+               {"7905F431-B5CE-4E99-A40F-4B1E122D00D0", "Apple Notification Center Service"},
                {"89D3502B-0F36-433A-8EF4-C502AD55F8DC", "Apple Media Service"},
+               {"9A3F68E0-86CE-11E5-A309-0002A5D5C51B", "Samsung Gear Manager Service"},
                {"9B3C81D8-57B1-4A8A-B8DF-0E56F7CA51C2", "Remote Command"},
-               {"2F7CABCE-808D-411F-9A0C-BB92BA96C102", "Entity Update"},
+               {"9FBF120D-6301-42D9-8C58-25E699A21DBD", "Notifications Source"},
+               {"A49EB41E-CB06-495C-9F4F-BB80A90CDF00", "Samsung Gear Manager Service"},
+               {"ADE3D529-C784-4F63-A987-EB69F70EE816", "IoT OIC Service"},
+               {"C2051EE0-804D-4D50-A12C-15E243852100", "Notifications Source"},
+               {"C2F2CC0F-C085-4DD4-BE5A-ACA3074BBC72", "Control Point"},
                {"C6B2F38C-23AB-46D8-A6AB-A3A870BBD5D7", "Entity Attribute"},
-               {"9A3F68E0-86CE-11E5-A309-0002A5D5C51B", "Samsung Gear Manager Service"},
-               {"c2f2cc0f-c085-4dd4-be5a-aca3074bbc72", "Control Point"},
-               {"cece518b-28d7-4171-92d5-76a1e249a3b9", "Notifications Source"},
-               {"32D1955A-E5AA-4A96-9A49-08538DA8B8F6", "Samsung Gear Fit Manager Service"},
+               {"CECE518B-28D7-4171-92D5-76A1E249A3B9", "Notifications Source"},
                {"FE53FF98-B259-4337-B56A-0EC9F82C6BAD", "Control Point"},
-               {"C2051EE0-804D-4D50-A12C-15E243852100", "Notifications Source"},
-               {"1ab7c24d-185a-45b9-90d4-f7ab1a71949a", "Samsung Health Service"},
-               {"ADE3D529-C784-4F63-A987-EB69F70EE816", "IoT OIC Service"},
-               {NULL, NULL}
        };
+       const uuid_name_s *uuid_name = short_uuid_name;
+       int start = 0;
+       int end = SHORT_UUID_COUNT - 1;
+       int p;
+       int ret;
 
        if (!uuid || !name)
                return BLUETOOTH_ERROR_INVALID_PARAM;
@@ -2220,15 +2235,22 @@ BT_EXPORT_API int bluetooth_get_uuid_name(const char *uuid, char **name)
                else {
                        offset = 0;
                        uuid_len = 36;
+                       end = LONG_UUID_COUNT - 1;
+                       uuid_name = long_uuid_name;
                }
        } else if (strlen(uuid) >= 8)
                offset = 4;
 
-       for (i = 0; bt_uuid_name[i].uuid; i++) {
-               if (!g_ascii_strncasecmp(uuid + offset, bt_uuid_name[i].uuid, uuid_len)) {
-                       *name = g_strdup(bt_uuid_name[i].specification_name);
+       while (start <= end) {
+               p = start + (end - start) / 2;
+               ret = g_ascii_strncasecmp(uuid + offset, uuid_name[p].uuid, uuid_len);
+               if (ret == 0) {
+                       *name = g_strdup(uuid_name[p].specification_name);
                        return BLUETOOTH_ERROR_NONE;
-               }
+               } else if (ret < 0)
+                       end = p - 1;
+               else
+                       start = p + 1;
        }
 
        BT_INFO("Unknown uuid : %s", uuid);