static DBusMessage *try_acquire(DBusConnection *conn, DBusMessage *msg, void *data) { struct media_transport *transport = data; struct media_owner *owner; struct media_request *req; guint id; if (transport->owner != NULL) return btd_error_not_authorized(msg); if (transport->state >= TRANSPORT_STATE_REQUESTING) return btd_error_not_authorized(msg); if (transport->state != TRANSPORT_STATE_PENDING) return btd_error_not_available(msg); owner = media_owner_create(msg); id = transport->resume(transport, owner); if (id == 0) { media_owner_free(owner); return btd_error_not_authorized(msg); } req = media_request_create(msg, id); media_owner_add(owner, req); media_transport_set_owner(transport, owner); return NULL; }
static DBusMessage *release(DBusConnection *conn, DBusMessage *msg, void *data) { struct media_transport *transport = data; struct media_owner *owner; const char *accesstype, *sender; struct media_request *req; if (!dbus_message_get_args(msg, NULL, DBUS_TYPE_STRING, &accesstype, DBUS_TYPE_INVALID)) return NULL; sender = dbus_message_get_sender(msg); owner = media_transport_find_owner(transport, sender); if (owner == NULL) return btd_error_not_authorized(msg); if (g_strcmp0(owner->accesstype, accesstype) == 0) { guint id; /* Not the last owner, no need to suspend */ if (g_slist_length(transport->owners) != 1) { media_transport_remove(transport, owner); return g_dbus_create_reply(msg, DBUS_TYPE_INVALID); } if (owner->pending) { const char *member; member = dbus_message_get_member(owner->pending->msg); /* Cancel Acquire request if that exist */ if (g_str_equal(member, "Acquire")) media_owner_remove(owner); else return btd_error_in_progress(msg); } id = transport->suspend(transport, owner); if (id == 0) { media_transport_remove(transport, owner); return g_dbus_create_reply(msg, DBUS_TYPE_INVALID); } req = media_request_create(msg, id); media_owner_add(owner, req); return NULL; } else if (g_strstr_len(owner->accesstype, -1, accesstype) != NULL) { media_transport_release(transport, accesstype); g_strdelimit(owner->accesstype, accesstype, ' '); } else return btd_error_not_authorized(msg); return g_dbus_create_reply(msg, DBUS_TYPE_INVALID); }
static DBusMessage *create_gatt_dbus_error(DBusMessage *msg, uint8_t att_ecode) { switch (att_ecode) { case BT_ATT_ERROR_READ_NOT_PERMITTED: return btd_error_not_permitted(msg, "Read not permitted"); case BT_ATT_ERROR_WRITE_NOT_PERMITTED: return btd_error_not_permitted(msg, "Write not permitted"); case BT_ATT_ERROR_AUTHENTICATION: case BT_ATT_ERROR_INSUFFICIENT_ENCRYPTION: case BT_ATT_ERROR_INSUFFICIENT_ENCRYPTION_KEY_SIZE: return btd_error_not_permitted(msg, "Not paired"); case BT_ATT_ERROR_INVALID_OFFSET: return btd_error_invalid_args_str(msg, "Invalid offset"); case BT_ATT_ERROR_INVALID_ATTRIBUTE_VALUE_LEN: return btd_error_invalid_args_str(msg, "Invalid Length"); case BT_ATT_ERROR_AUTHORIZATION: return btd_error_not_authorized(msg); case BT_ATT_ERROR_REQUEST_NOT_SUPPORTED: return btd_error_not_supported(msg); case 0: return btd_error_failed(msg, "Operation failed"); default: return g_dbus_create_error(msg, ERROR_INTERFACE, "Operation failed with ATT error: 0x%02x", att_ecode); } return NULL; }
static DBusMessage *unregister_watcher(DBusConnection *conn, DBusMessage *msg, void *data) { const char *sender = dbus_message_get_sender(msg); struct gatt_service *gatt = data; struct watcher *watcher, *match; GSList *l; char *path; if (!dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &path, DBUS_TYPE_INVALID)) return btd_error_invalid_args(msg); match = g_new0(struct watcher, 1); match->name = g_strdup(sender); match->path = g_strdup(path); l = g_slist_find_custom(gatt->watchers, match, watcher_cmp); INFO("unregistering watcher %s",match->path); watcher_free(match); if (!l) return btd_error_not_authorized(msg); watcher = l->data; g_dbus_remove_watch(conn, watcher->id); gatt->watchers = g_slist_remove(gatt->watchers, watcher); watcher_free(watcher); if (gatt->watchers == NULL && gatt->attioid) { btd_device_remove_attio_callback(gatt->dev, gatt->attioid); gatt->attioid = 0; } return dbus_message_new_method_return(msg); }
static DBusMessage *port_disconnect(DBusConnection *conn, DBusMessage *msg, void *user_data) { struct serial_device *device = user_data; struct serial_port *port; const char *dev, *owner, *caller; if (dbus_message_get_args(msg, NULL, DBUS_TYPE_STRING, &dev, DBUS_TYPE_INVALID) == FALSE) return NULL; port = find_port(device->ports, dev); if (!port) return btd_error_does_not_exist(msg); if (!port->listener_id) return btd_error_not_connected(msg); owner = dbus_message_get_sender(port->msg); caller = dbus_message_get_sender(msg); if (!g_str_equal(owner, caller)) return btd_error_not_authorized(msg); port_release(port); g_dbus_remove_watch(conn, port->listener_id); port->listener_id = 0; return g_dbus_create_reply(msg, DBUS_TYPE_INVALID); }
static DBusMessage *cancel_authorization(DBusConnection *conn, DBusMessage *msg, void *data) { DBusMessage *reply; struct service_adapter *serv_adapter = data; struct pending_auth *auth; const gchar *sender; sender = dbus_message_get_sender(msg); auth = find_pending_by_sender(serv_adapter, sender); if (auth == NULL) return btd_error_does_not_exist(msg); btd_cancel_authorization(auth->id); reply = btd_error_not_authorized(auth->msg); dbus_message_unref(auth->msg); g_dbus_send_message(btd_get_dbus_connection(), reply); serv_adapter->pending_list = g_slist_remove(serv_adapter->pending_list, auth); g_free(auth); auth = next_pending(serv_adapter); if (auth == NULL) goto done; auth->id = btd_request_authorization(get_address(serv_adapter), &auth->dst, auth->uuid, auth_cb, serv_adapter); done: return dbus_message_new_method_return(msg); }
static DBusMessage *unregister_agent(DBusConnection *conn, DBusMessage *msg, void *data) { struct audio_device *device = data; struct gateway *gw = device->gateway; const char *path; if (!gw->agent) goto done; if (strcmp(gw->agent->name, dbus_message_get_sender(msg)) != 0) return btd_error_not_authorized(msg); if (!dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &path, DBUS_TYPE_INVALID)) return btd_error_invalid_args(msg); if (strcmp(gw->agent->path, path) != 0) return btd_error_does_not_exist(msg); g_dbus_remove_watch(conn, gw->agent->watch); agent_free(gw->agent); gw->agent = NULL; done: return dbus_message_new_method_return(msg); }
static DBusMessage *connection_cancel(DBusConnection *conn, DBusMessage *msg, void *data) { struct network_conn *nc = data; const char *owner = dbus_message_get_sender(nc->msg); const char *caller = dbus_message_get_sender(msg); if (!g_str_equal(owner, caller)) return btd_error_not_authorized(msg); connection_destroy(conn, nc); return g_dbus_create_reply(msg, DBUS_TYPE_INVALID); }
static DBusMessage *acquire(DBusConnection *conn, DBusMessage *msg, void *data) { struct media_transport *transport = data; struct media_owner *owner; struct media_request *req; const char *accesstype, *sender; guint id; if (!dbus_message_get_args(msg, NULL, DBUS_TYPE_STRING, &accesstype, DBUS_TYPE_INVALID)) return NULL; sender = dbus_message_get_sender(msg); owner = media_transport_find_owner(transport, sender); if (owner != NULL) return btd_error_not_authorized(msg); if (media_transport_acquire(transport, accesstype) == FALSE) return btd_error_not_authorized(msg); owner = media_owner_create(conn, msg, accesstype); id = transport->resume(transport, owner); if (id == 0) { media_transport_release(transport, accesstype); media_owner_free(owner); return btd_error_not_authorized(msg); } req = media_request_create(msg, id); media_owner_add(owner, req); media_transport_add(transport, owner); return NULL; }
static void auth_cb(DBusError *derr, void *user_data) { struct service_adapter *serv_adapter = user_data; DBusMessage *reply; struct pending_auth *auth; bdaddr_t src; auth = next_pending(serv_adapter); if (auth == NULL) { info("Authorization cancelled: Client exited"); return; } if (derr) { error("Access denied: %s", derr->message); reply = btd_error_not_authorized(auth->msg); dbus_message_unref(auth->msg); g_dbus_send_message(auth->conn, reply); goto done; } g_dbus_send_reply(auth->conn, auth->msg, DBUS_TYPE_INVALID); done: dbus_connection_unref(auth->conn); serv_adapter->pending_list = g_slist_remove(serv_adapter->pending_list, auth); g_free(auth); auth = next_pending(serv_adapter); if (auth == NULL) return; if (serv_adapter->adapter) adapter_get_address(serv_adapter->adapter, &src); else bacpy(&src, BDADDR_ANY); btd_request_authorization(&src, &auth->dst, auth->uuid, auth_cb, serv_adapter); }
static DBusMessage *release(DBusConnection *conn, DBusMessage *msg, void *data) { struct media_transport *transport = data; struct media_owner *owner = transport->owner; const char *sender; struct media_request *req; guint id; sender = dbus_message_get_sender(msg); if (owner == NULL || g_strcmp0(owner->name, sender) != 0) return btd_error_not_authorized(msg); if (owner->pending) { const char *member; member = dbus_message_get_member(owner->pending->msg); /* Cancel Acquire request if that exist */ if (g_str_equal(member, "Acquire")) media_owner_remove(owner); else return btd_error_in_progress(msg); } transport_set_state(transport, TRANSPORT_STATE_SUSPENDING); id = transport->suspend(transport, owner); if (id == 0) { media_transport_remove_owner(transport); return g_dbus_create_reply(msg, DBUS_TYPE_INVALID); } req = media_request_create(msg, id); media_owner_add(owner, req); return NULL; }
static DBusMessage *request_authorization(DBusConnection *conn, DBusMessage *msg, void *data) { struct record_data *user_record; struct service_adapter *serv_adapter = data; sdp_record_t *record; sdp_list_t *services; const char *sender; dbus_uint32_t handle; const char *address; struct pending_auth *auth; char uuid_str[MAX_LEN_UUID_STR]; uuid_t *uuid, *uuid128; bdaddr_t src; if (dbus_message_get_args(msg, NULL, DBUS_TYPE_STRING, &address, DBUS_TYPE_UINT32, &handle, DBUS_TYPE_INVALID) == FALSE) return NULL; sender = dbus_message_get_sender(msg); if (find_pending_by_sender(serv_adapter, sender)) return btd_error_does_not_exist(msg); user_record = find_record(serv_adapter, handle, sender); if (!user_record) { user_record = find_record(serv_adapter_any, handle, sender); if (!user_record) return btd_error_not_authorized(msg); } record = sdp_record_find(user_record->handle); if (record == NULL) return btd_error_not_authorized(msg); if (sdp_get_service_classes(record, &services) < 0) { sdp_record_free(record); return btd_error_not_authorized(msg); } if (services == NULL) return btd_error_not_authorized(msg); uuid = services->data; uuid128 = sdp_uuid_to_uuid128(uuid); sdp_list_free(services, bt_free); if (sdp_uuid2strn(uuid128, uuid_str, MAX_LEN_UUID_STR) < 0) { bt_free(uuid128); return btd_error_not_authorized(msg); } bt_free(uuid128); auth = g_new0(struct pending_auth, 1); auth->msg = dbus_message_ref(msg); auth->conn = dbus_connection_ref(connection); auth->sender = user_record->sender; memcpy(auth->uuid, uuid_str, MAX_LEN_UUID_STR); str2ba(address, &auth->dst); serv_adapter->pending_list = g_slist_append(serv_adapter->pending_list, auth); auth = next_pending(serv_adapter); if (auth == NULL) return btd_error_does_not_exist(msg); if (serv_adapter->adapter) adapter_get_address(serv_adapter->adapter, &src); else bacpy(&src, BDADDR_ANY); if (btd_request_authorization(&src, &auth->dst, auth->uuid, auth_cb, serv_adapter) < 0) { serv_adapter->pending_list = g_slist_remove(serv_adapter->pending_list, auth); g_free(auth); return btd_error_not_authorized(msg); } return NULL; }