admin.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /*
  2. *
  3. * BlueZ - Bluetooth protocol stack for Linux
  4. *
  5. * Copyright (C) 2021 Google LLC
  6. *
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. */
  19. #ifdef HAVE_CONFIG_H
  20. #include <config.h>
  21. #endif
  22. #include <stdio.h>
  23. #include <string.h>
  24. #include <stdlib.h>
  25. #include "gdbus/gdbus.h"
  26. #include "src/shared/shell.h"
  27. #include "admin.h"
  28. #define _GNU_SOURCE
  29. static GDBusProxy *set_proxy;
  30. static GDBusProxy *status_proxy;
  31. void admin_policy_set_set_proxy(GDBusProxy *proxy)
  32. {
  33. set_proxy = proxy;
  34. }
  35. void admin_policy_set_status_proxy(GDBusProxy *proxy)
  36. {
  37. status_proxy = proxy;
  38. }
  39. void admin_policy_read_service_allowlist(DBusConnection *dbus_conn)
  40. {
  41. DBusMessageIter iter, subiter;
  42. char *uuid = NULL;
  43. if (!status_proxy || !g_dbus_proxy_get_property(status_proxy,
  44. "ServiceAllowList", &iter)) {
  45. bt_shell_printf("Failed to get property\n");
  46. return bt_shell_noninteractive_quit(EXIT_FAILURE);
  47. }
  48. if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY) {
  49. bt_shell_printf("Unexpected return type\n");
  50. return bt_shell_noninteractive_quit(EXIT_FAILURE);
  51. }
  52. bt_shell_printf("Service AllowedList:\n");
  53. dbus_message_iter_recurse(&iter, &subiter);
  54. while (dbus_message_iter_get_arg_type(&subiter) ==
  55. DBUS_TYPE_STRING) {
  56. dbus_message_iter_get_basic(&subiter, &uuid);
  57. bt_shell_printf("\t%s\n", uuid);
  58. dbus_message_iter_next(&subiter);
  59. }
  60. return bt_shell_noninteractive_quit(EXIT_SUCCESS);
  61. }
  62. struct uuid_list_data {
  63. char **uuid_list;
  64. size_t num;
  65. };
  66. static void set_service_setup(DBusMessageIter *iter, void *user_data)
  67. {
  68. struct uuid_list_data *data = user_data;
  69. DBusMessageIter arr_iter;
  70. size_t i;
  71. dbus_message_iter_open_container(iter, DBUS_TYPE_ARRAY,
  72. DBUS_TYPE_STRING_AS_STRING,
  73. &arr_iter);
  74. for (i = 0; i < data->num; i++) {
  75. dbus_message_iter_append_basic(&arr_iter, DBUS_TYPE_STRING,
  76. &data->uuid_list[i]);
  77. }
  78. dbus_message_iter_close_container(iter, &arr_iter);
  79. }
  80. static void set_service_reply(DBusMessage *message, void *user_data)
  81. {
  82. DBusError error;
  83. dbus_error_init(&error);
  84. if (!dbus_set_error_from_message(&error, message)) {
  85. bt_shell_printf("Set allowed service successfully\n");
  86. return bt_shell_noninteractive_quit(EXIT_SUCCESS);
  87. }
  88. bt_shell_printf("Failed to set service allowed list: %s\n", error.name);
  89. dbus_error_free(&error);
  90. return bt_shell_noninteractive_quit(EXIT_FAILURE);
  91. }
  92. void admin_policy_set_service_allowlist(DBusConnection *dbus_connd,
  93. int argc, char *argv[])
  94. {
  95. struct uuid_list_data data;
  96. if (!set_proxy) {
  97. bt_shell_printf("Set proxy not ready\n");
  98. return bt_shell_noninteractive_quit(EXIT_FAILURE);
  99. }
  100. data.uuid_list = argv;
  101. data.num = argc;
  102. if (!g_dbus_proxy_method_call(set_proxy, "SetServiceAllowList",
  103. set_service_setup, set_service_reply,
  104. &data, NULL)) {
  105. bt_shell_printf("Failed to call method\n");
  106. return bt_shell_noninteractive_quit(EXIT_FAILURE);
  107. }
  108. }