Received: by 2002:a05:6a10:c604:0:0:0:0 with SMTP id y4csp312527pxt; Fri, 6 Aug 2021 02:40:57 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwJmEG8gKbyxb77Q4kHzxvBLzblPIjwN+u3y0sbKGJQ6h95Yy1x0XyOcmDA6q+9bWlr+43a X-Received: by 2002:a92:c605:: with SMTP id p5mr265137ilm.209.1628242857516; Fri, 06 Aug 2021 02:40:57 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1628242857; cv=none; d=google.com; s=arc-20160816; b=HVWxxMVZh+TSx/2rk2G9Ip9F+mc7Dgn/V3bfeIAyYMXHg2kEucUZ0IUbtiV0Ytz7Tv /HNPwnc4OvAMM+4OxKJUIi5AFiylXNY87jnOd8Q+1+QwS+qvRHxy9ZvYXOExLDeh0j/c Pd5cuO7BpjMJB5IY2EFmzWZrEnPwg8fKtYJg9MB/SEYsBuKIrtVZ71vj02W2PVf2smhK g6zXe+Cmnrp1Wb8s+a6DTQ1NeszUmuMwD7LMKi+Jabm8D97acSC3TJTBUaker2Lria7x F2P4IH9hei35sd9NiopRokLN/CsMI8pSXt1QP0w3ULyGM5VLSyP7n0CoEZgfLBonSgwg pTmg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:from:subject:references:mime-version :message-id:in-reply-to:date:dkim-signature; bh=rpVgB1BDvoLw0qfkUaQpBy8/tFI+Wqy6J0WWDSTrs+M=; b=HkPyNdKk7m+Rl58oF+Ef2Jeq5A3laIA+By4SP53Tkg1Eo3UChzgrdNBcGqqOOfJYeN tmmtSX2s3ixVHcmiTSXLmzUVKNq31advb/TJuvdnIqbYFM1MFVGZFqhpsirD3cn/nAZR +VcI04sgAOmaD1F9rCFsotDj6SBA0iJGT6MgXbfYjAZAXho4yw9w0cbtaUGdYonyuOip 4/Rxzc5lAOVB/9A34vA3RoeCe0D5LVaxBu0toSWc4pY2IW+Rb/uwMSjE5hLq80fsZsqB 53f4ENqbXmRCeRMFcEpoOgiBtTDkRth4EjAEz9Jhq2XKYo6pCL/oHh/FiEv6ncSl1Zom Osxw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=Verbatl3; spf=pass (google.com: domain of linux-bluetooth-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-bluetooth-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id z4si7962092ioz.87.2021.08.06.02.40.24; Fri, 06 Aug 2021 02:40:57 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-bluetooth-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=Verbatl3; spf=pass (google.com: domain of linux-bluetooth-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-bluetooth-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231553AbhHFJjG (ORCPT + 99 others); Fri, 6 Aug 2021 05:39:06 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47892 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S244234AbhHFJjG (ORCPT ); Fri, 6 Aug 2021 05:39:06 -0400 Received: from mail-qt1-x849.google.com (mail-qt1-x849.google.com [IPv6:2607:f8b0:4864:20::849]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C9742C061798 for ; Fri, 6 Aug 2021 02:38:50 -0700 (PDT) Received: by mail-qt1-x849.google.com with SMTP id k2-20020ac847820000b029024ea3acef5bso4109296qtq.12 for ; Fri, 06 Aug 2021 02:38:50 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=rpVgB1BDvoLw0qfkUaQpBy8/tFI+Wqy6J0WWDSTrs+M=; b=Verbatl3PFVwgMy2cI8rbUFYb4XLBCQch8XFDIwiyeKPfPQluDLWkLpXVtI6Qj3dFb WrKHpDgOoZHV8UwsD10DlZXcpIkn2g7vVEkDvffm29FlhSt00V5yoW4+w8zjqou6mUUt LpH11iKqIotnSSGZGfq/bZOWZ1701BM1RTgHVEBl3P0520E3BcZZswmeClx4oYHucyH4 HPI4adPo/6FaY6upwwKXyOXpwlLFGxOBW4g7+fm2JE3YlZllfHIrsfMz5B9a2GNtZesH W48sSCQMly+yUg1LOadZtexgI7mC0VNt2w7H3o7sd56f50kpXzLcSUJkVYNrsq/7U2Jj uuJg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=rpVgB1BDvoLw0qfkUaQpBy8/tFI+Wqy6J0WWDSTrs+M=; b=e1uH9obT7capCkmqBPIfHdQUfFgHUYspEkgRTYtuMpXISBYLv7mo1vM6niPfXL7m+V SF+fmcu7reFyXHqx4rlYSd91Hp0JZLKjBU0pbwoW7GNxkhvUaUXHa7MB9ICS4Cu2UmB9 zGvdX2Wvl850924jOpx6K7UdNX6dBv6jRwSvMHV1J7X7FiCH3+8jj15+B7OKF2bozRZb czbh0rT4551C0Q1O/L07zIFMz5QBZ8wQbvirZlHXxmS8+B9bQKlPEXtjJLNRu4o+yQDB OwCFn2yaVXRTsQx6S2F2G+/7dzR+JFGmP8WbUO6Y44CeFmtfuneaOkyqs7qaHoYCgLPo Vbxg== X-Gm-Message-State: AOAM530zo6XiwH7GS9y7WXDPotkM8JsqDEb1p1oHZqV4/kkFRaJXoufO wWMUptU1vvqAsgzb3ECWvJjrqNPlQI/+sjTT752EgX4wnZb4GeCq7bIioSe3NpULFDEO4kML4lH eQSs6FAHJlu93ij2LGqpb6Twl/YqMdrJ+H4e9tWGc/6tv/9bCpryEULP575FeWpmo0sDzjlC/zv v+eCYFa/6yVWE= X-Received: from howardchung-p920.tpe.corp.google.com ([2401:fa00:1:10:85ab:3c41:db28:3381]) (user=howardchung job=sendgmr) by 2002:a05:6214:5182:: with SMTP id kl2mr10116716qvb.19.1628242729940; Fri, 06 Aug 2021 02:38:49 -0700 (PDT) Date: Fri, 6 Aug 2021 17:38:33 +0800 In-Reply-To: <20210806173706.v1.1.Ie7d876ad0b7defabfa86beb64e83f655f12be7ab@changeid> Message-Id: <20210806173706.v1.2.I1d1cf4fbc02b11d62f186f0db23c2c537e296a64@changeid> Mime-Version: 1.0 References: <20210806173706.v1.1.Ie7d876ad0b7defabfa86beb64e83f655f12be7ab@changeid> X-Mailer: git-send-email 2.32.0.605.g8dce9f2422-goog Subject: [PATCH v1 2/3] client: add service-allowlist command From: Howard Chung To: linux-bluetooth@vger.kernel.org, luiz.dentz@gmail.com Cc: Yun-Hao Chung , Miao-chen Chou Content-Type: text/plain; charset="UTF-8" Precedence: bulk List-ID: X-Mailing-List: linux-bluetooth@vger.kernel.org From: Yun-Hao Chung This creates a menu - admin and adds two commands to bluetoothctl - set-service-allowlist [uuid1 uuid2 ...] - get-service-allowlist Reviewed-by: Miao-chen Chou --- The following test steps were performed: 1. [bluetooth]# - menu admin 2. [bluetooth]# set-service-allowlist 1124 180A 180F 1812 1801 3. [bluetooth]# get-service-allowlist 4. Verify the printed UUIDs are the same as what we set. Makefile.tools | 4 +- client/admin_policy.c | 133 ++++++++++++++++++++++++++++++++++++++++++ client/admin_policy.h | 25 ++++++++ client/main.c | 72 +++++++++++++++++++++++ 4 files changed, 233 insertions(+), 1 deletion(-) create mode 100644 client/admin_policy.c create mode 100644 client/admin_policy.h diff --git a/Makefile.tools b/Makefile.tools index c836b5984934..1b493d4d0ed6 100644 --- a/Makefile.tools +++ b/Makefile.tools @@ -9,7 +9,9 @@ client_bluetoothctl_SOURCES = client/main.c \ client/advertising.c \ client/adv_monitor.h \ client/adv_monitor.c \ - client/gatt.h client/gatt.c + client/gatt.h client/gatt.c \ + client/admin_policy.h \ + client/admin_policy.c client_bluetoothctl_LDADD = gdbus/libgdbus-internal.la src/libshared-glib.la \ $(GLIB_LIBS) $(DBUS_LIBS) -lreadline endif diff --git a/client/admin_policy.c b/client/admin_policy.c new file mode 100644 index 000000000000..21062c4f6185 --- /dev/null +++ b/client/admin_policy.c @@ -0,0 +1,133 @@ +/* + * + * BlueZ - Bluetooth protocol stack for Linux + * + * Copyright (C) 2021 Google LLC + * + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + */ + +#ifdef HAVE_CONFIG_H +#include +#endif + +#include +#include +#include + +#include "gdbus/gdbus.h" +#include "src/shared/shell.h" + +#include "admin_policy.h" +#define _GNU_SOURCE + +static GDBusProxy *set_proxy; +static GDBusProxy *status_proxy; + +void admin_policy_set_set_proxy(GDBusProxy *proxy) +{ + set_proxy = proxy; +} + +void admin_policy_set_status_proxy(GDBusProxy *proxy) +{ + status_proxy = proxy; +} + +void admin_policy_read_service_allowlist(DBusConnection *dbus_conn) +{ + DBusMessageIter iter, subiter; + char *uuid = NULL; + + if (!status_proxy || !g_dbus_proxy_get_property(status_proxy, + "ServiceAllowList", &iter)) { + bt_shell_printf("Failed to get property\n"); + return bt_shell_noninteractive_quit(EXIT_FAILURE); + } + + if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY) { + bt_shell_printf("Unexpected return type\n"); + return bt_shell_noninteractive_quit(EXIT_FAILURE); + } + + bt_shell_printf("Service AllowedList:\n"); + dbus_message_iter_recurse(&iter, &subiter); + while (dbus_message_iter_get_arg_type(&subiter) == + DBUS_TYPE_STRING) { + dbus_message_iter_get_basic(&subiter, &uuid); + bt_shell_printf("\t%s\n", uuid); + dbus_message_iter_next(&subiter); + } + + return bt_shell_noninteractive_quit(EXIT_SUCCESS); +} + +struct uuid_list_data { + char **uuid_list; + size_t num; +}; + +static void set_service_setup(DBusMessageIter *iter, void *user_data) +{ + struct uuid_list_data *data = user_data; + DBusMessageIter arr_iter; + size_t i; + + dbus_message_iter_open_container(iter, DBUS_TYPE_ARRAY, + DBUS_TYPE_STRING_AS_STRING, + &arr_iter); + + for (i = 0; i < data->num; i++) { + dbus_message_iter_append_basic(&arr_iter, DBUS_TYPE_STRING, + &data->uuid_list[i]); + } + + dbus_message_iter_close_container(iter, &arr_iter); +} + +static void set_service_reply(DBusMessage *message, void *user_data) +{ + DBusError error; + + dbus_error_init(&error); + + if (!dbus_set_error_from_message(&error, message)) { + bt_shell_printf("Set allowed service successfully\n"); + return bt_shell_noninteractive_quit(EXIT_SUCCESS); + } + + bt_shell_printf("Failed to set service allowed list: %s\n", error.name); + dbus_error_free(&error); + return bt_shell_noninteractive_quit(EXIT_FAILURE); +} + +void admin_policy_set_service_allowlist(DBusConnection *dbus_connd, + int argc, char *argv[]) +{ + struct uuid_list_data data; + + if (!set_proxy) { + bt_shell_printf("Set proxy not ready\n"); + return bt_shell_noninteractive_quit(EXIT_FAILURE); + } + + data.uuid_list = argv; + data.num = argc; + + if (!g_dbus_proxy_method_call(set_proxy, "SetServiceAllowList", + set_service_setup, set_service_reply, + &data, NULL)) { + bt_shell_printf("Failed to call method\n"); + return bt_shell_noninteractive_quit(EXIT_FAILURE); + } +} diff --git a/client/admin_policy.h b/client/admin_policy.h new file mode 100644 index 000000000000..1c8c2152d59d --- /dev/null +++ b/client/admin_policy.h @@ -0,0 +1,25 @@ +/* + * + * BlueZ - Bluetooth protocol stack for Linux + * + * Copyright (C) 2021 Google LLC + * + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + */ + +void admin_policy_set_set_proxy(GDBusProxy *proxy); +void admin_policy_set_status_proxy(GDBusProxy *proxy); + +void admin_policy_read_service_allowlist(DBusConnection *dbus_conn); +void admin_policy_set_service_allowlist(DBusConnection *dbus_conn, + int argc, char *argv[]); diff --git a/client/main.c b/client/main.c index c1a62edb7f99..5297760c8cec 100644 --- a/client/main.c +++ b/client/main.c @@ -29,6 +29,7 @@ #include "gatt.h" #include "advertising.h" #include "adv_monitor.h" +#include "admin_policy.h" /* String display constants */ #define COLORED_NEW COLOR_GREEN "NEW" COLOR_OFF @@ -530,6 +531,23 @@ static void admon_manager_added(GDBusProxy *proxy) adv_monitor_register_app(dbus_conn); } +static void admin_policy_set_added(GDBusProxy *proxy) +{ + admin_policy_set_set_proxy(proxy); +} + +static void admin_policy_status_added(GDBusProxy *proxy) +{ + struct adapter *adapter; + + adapter = find_ctrl(ctrl_list, g_dbus_proxy_get_path(proxy)); + + if (!adapter) + return; + + admin_policy_set_status_proxy(proxy); +} + static void proxy_added(GDBusProxy *proxy, void *user_data) { const char *interface; @@ -565,6 +583,10 @@ static void proxy_added(GDBusProxy *proxy, void *user_data) } else if (!strcmp(interface, "org.bluez.AdvertisementMonitorManager1")) { admon_manager_added(proxy); + } else if (!strcmp(interface, "org.bluez.AdminPolicySet1")) { + admin_policy_set_added(proxy); + } else if (!strcmp(interface, "org.bluez.AdminPolicyStatus1")) { + admin_policy_status_added(proxy); } } @@ -621,6 +643,23 @@ static void adapter_removed(GDBusProxy *proxy) } } +static void admin_policy_set_removed(GDBusProxy *proxy) +{ + admin_policy_set_set_proxy(NULL); +} + +static void admin_policy_status_removed(GDBusProxy *proxy) +{ + struct adapter *adapter; + + adapter = find_ctrl(ctrl_list, g_dbus_proxy_get_path(proxy)); + + if (!adapter) + return; + + admin_policy_set_status_proxy(NULL); +} + static void proxy_removed(GDBusProxy *proxy, void *user_data) { const char *interface; @@ -661,6 +700,10 @@ static void proxy_removed(GDBusProxy *proxy, void *user_data) } else if (!strcmp(interface, "org.bluez.AdvertisementMonitorManager1")) { adv_monitor_remove_manager(dbus_conn); + } else if (!strcmp(interface, "org.bluez.AdminPolicySet1")) { + admin_policy_set_removed(proxy); + } else if (!strcmp(interface, "org.bluez.AdminPolicyStatus1")) { + admin_policy_status_removed(proxy); } } @@ -2824,6 +2867,22 @@ static void cmd_adv_monitor_get_supported_info(int argc, char *argv[]) adv_monitor_get_supported_info(); } +static void cmd_set_service_allowlist(int argc, char *argv[]) +{ + if (check_default_ctrl() == FALSE) + return bt_shell_noninteractive_quit(EXIT_FAILURE); + + admin_policy_set_service_allowlist(dbus_conn, argc - 1, argv + 1); +} + +static void cmd_get_service_allowlist(int argc, char *argv[]) +{ + if (check_default_ctrl() == FALSE) + return bt_shell_noninteractive_quit(EXIT_FAILURE); + + admin_policy_read_service_allowlist(dbus_conn); +} + static const struct bt_shell_menu advertise_menu = { .name = "advertise", .desc = "Advertise Options Submenu", @@ -2978,6 +3037,18 @@ static const struct bt_shell_menu gatt_menu = { { } }, }; +static const struct bt_shell_menu admin_menu = { + .name = "admin", + .desc = "Admin Policy Submenu", + .entries = { + { "set-service-allowlist", "[uuid1 uuid2 ...]", + cmd_set_service_allowlist, + "Set service allowlist"}, + { "get-service-allowlist", NULL, cmd_get_service_allowlist, + "Get service allowlist"}, + {} }, +}; + static const struct bt_shell_menu main_menu = { .name = "main", .entries = { @@ -3075,6 +3146,7 @@ int main(int argc, char *argv[]) bt_shell_add_submenu(&advertise_monitor_menu); bt_shell_add_submenu(&scan_menu); bt_shell_add_submenu(&gatt_menu); + bt_shell_add_submenu(&admin_menu); bt_shell_set_prompt(PROMPT_OFF); if (agent_option) -- 2.32.0.605.g8dce9f2422-goog