1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415
|
/*
*
* bluez-tools - a set of tools to manage bluetooth devices for linux
*
* Copyright (C) 2010-2011 Alexander Orlenko <zxteam@gmail.com>
*
*
* 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.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <errno.h>
#include <gio/gio.h>
#include <glib.h>
#include <string.h>
#include <stdlib.h>
#include "agent-helper.h"
gboolean agent_need_unregister;
static const gchar *_bt_agent_introspect_xml = "<node name=\"/org/blueztools\">\n\t<interface name=\"org.bluez.Agent1\">\n\t\t<method name=\"Release\">\n\t\t</method>\n\t\t<method name=\"RequestPinCode\">\n\t\t\t<arg name=\"device\" direction=\"in\" type=\"o\"/>\n\t\t\t<arg name=\"pincode\" direction=\"out\" type=\"s\"/>\n\t\t</method>\n\t\t<method name=\"DisplayPinCode\">\n\t\t\t<arg name=\"device\" direction=\"in\" type=\"o\"/>\n\t\t\t<arg name=\"pincode\" direction=\"in\" type=\"s\"/>\n\t\t</method>\n\t\t<method name=\"RequestPasskey\">\n\t\t\t<arg name=\"device\" direction=\"in\" type=\"o\"/>\n\t\t\t<arg name=\"passkey\" direction=\"out\" type=\"u\"/>\n\t\t</method>\n\t\t<method name=\"DisplayPasskey\">\n\t\t\t<arg name=\"device\" direction=\"in\" type=\"o\"/>\n\t\t\t<arg name=\"passkey\" direction=\"in\" type=\"u\"/>\n\t\t\t<arg name=\"entered\" direction=\"in\" type=\"q\"/>\n\t\t</method>\n\t\t<method name=\"RequestConfirmation\">\n\t\t\t<arg name=\"device\" direction=\"in\" type=\"o\"/>\n\t\t\t<arg name=\"passkey\" direction=\"in\" type=\"u\"/>\n\t\t</method>\n\t\t<method name=\"RequestAuthorization\">\n\t\t\t<arg name=\"device\" direction=\"in\" type=\"o\"/>\n\t\t</method>\n\t\t<method name=\"AuthorizeService\">\n\t\t\t<arg name=\"device\" direction=\"in\" type=\"o\"/>\n\t\t\t<arg name=\"uuid\" direction=\"in\" type=\"s\"/>\n\t\t</method>\n\t\t<method name=\"Cancel\">\n\t\t</method>\n\t</interface>\n</node>\n";
static guint _bt_agent_registration_id = 0;
static GHashTable *_pin_hash_table = NULL;
static gboolean _interactive = TRUE;
static GMainLoop *_mainloop = NULL;
static void _bt_agent_g_destroy_notify(gpointer data);
static void _bt_agent_method_call_func(GDBusConnection *connection, const gchar *sender, const gchar *object_path, const gchar *interface_name, const gchar *method_name, GVariant *parameters, GDBusMethodInvocation *invocation, gpointer user_data);
static const gchar *_find_device_pin(const gchar *device_path);
static void _bt_agent_method_call_func(GDBusConnection *connection, const gchar *sender, const gchar *object_path, const gchar *interface_name, const gchar *method_name, GVariant *parameters, GDBusMethodInvocation *invocation, gpointer user_data)
{
// g_print("%s%s\n", method_name, g_variant_print(parameters, FALSE));
if (g_strcmp0(method_name, "AuthorizeService") == 0)
{
GError *error = NULL;
Device *device_obj = device_new(g_variant_get_string(g_variant_get_child_value(parameters, 0), NULL));
const char *uuid = g_variant_get_string(g_variant_get_child_value(parameters, 1), NULL);
if (_interactive)
g_print("Device: %s (%s) for UUID %s\n", device_get_alias(device_obj, &error), device_get_address(device_obj, &error), uuid);
if (error)
{
g_critical("Failed to get remote device's MAC address: %s", error->message);
g_error_free(error);
g_dbus_method_invocation_return_dbus_error(invocation, "org.bluez.Error.Rejected", "Internal error occurred");
return;
}
if (device_get_paired (device_obj, &error))
{
g_dbus_method_invocation_return_value(invocation, NULL);
}
else if (error)
{
g_dbus_method_invocation_return_dbus_error(invocation, "org.bluez.Error.Rejected", "Internal error occurred");
g_error_free (error);
}
else
{
g_dbus_method_invocation_return_dbus_error(invocation, "org.bluez.Error.Rejected", "Service authorization rejected");
}
}
else if (g_strcmp0(method_name, "Cancel") == 0)
{
if (_interactive)
g_print("Request canceled\n");
// Return void
g_dbus_method_invocation_return_value(invocation, NULL);
}
else if (g_strcmp0(method_name, "DisplayPasskey") == 0)
{
GError *error = NULL;
Device *device_obj = device_new(g_variant_get_string(g_variant_get_child_value(parameters, 0), NULL));
const gchar *pin = _find_device_pin(device_get_dbus_object_path(device_obj));
if (_interactive)
g_print("Device: %s (%s)\n", device_get_alias(device_obj, &error), device_get_address(device_obj, &error));
if (error)
{
g_critical("Failed to get remote device's MAC address: %s", error->message);
g_error_free(error);
}
g_object_unref(device_obj);
if (_interactive)
{
g_print("Passkey: %u, entered: %u\n", g_variant_get_uint32(g_variant_get_child_value(parameters, 1)), g_variant_get_uint16(g_variant_get_child_value(parameters, 2)));
g_dbus_method_invocation_return_value(invocation, NULL);
return;
}
else if (pin != NULL)
{
/* OK, device found */
g_dbus_method_invocation_return_value(invocation, NULL);
return;
}
g_dbus_method_invocation_return_dbus_error(invocation, "org.bluez.Error.Rejected", "Pairing rejected");
}
else if (g_strcmp0(method_name, "DisplayPinCode") == 0)
{
GError *error = NULL;
Device *device_obj = device_new(g_variant_get_string(g_variant_get_child_value(parameters, 0), NULL));
const gchar *pin = _find_device_pin(device_get_dbus_object_path(device_obj));
const gchar *pincode = g_variant_get_string(g_variant_get_child_value(parameters, 1), NULL);
if (_interactive)
g_print("Device: %s (%s)\n", device_get_alias(device_obj, &error), device_get_address(device_obj, &error));
if (error)
{
g_critical("Failed to get remote device's MAC address: %s", error->message);
g_error_free(error);
}
g_object_unref(device_obj);
/* Try to use found PIN */
if (pin != NULL)
{
if (g_strcmp0(pin, "*") == 0 || g_strcmp0(pin, pincode) == 0)
{
if (_interactive)
g_print("Pin code confirmed\n");
g_dbus_method_invocation_return_value(invocation, NULL);
}
else
g_dbus_method_invocation_return_dbus_error(invocation, "org.bluez.Error.Rejected", "Passkey does not match");
return;
}
else if (_interactive)
{
g_print("Confirm pin code: %s (yes/no)? ", pincode);
gchar yn[4] = {0,};
errno = 0;
if (scanf("%3s", yn) == EOF && errno)
g_warning("%s\n", strerror(errno));
if(g_ascii_strcasecmp(yn, "yes") == 0 || g_ascii_strcasecmp(yn, "y") == 0)
g_dbus_method_invocation_return_value(invocation, NULL);
else
g_dbus_method_invocation_return_dbus_error(invocation, "org.bluez.Error.Rejected", "Passkey does not match");
return;
}
g_dbus_method_invocation_return_dbus_error(invocation, "org.bluez.Error.Rejected", "Pairing rejected");
}
else if (g_strcmp0(method_name, "Release") == 0)
{
agent_need_unregister = FALSE;
if(_mainloop)
if (g_main_loop_is_running(_mainloop))
g_main_loop_quit(_mainloop);
g_print("Agent released\n");
// Return void
g_dbus_method_invocation_return_value(invocation, NULL);
}
else if (g_strcmp0(method_name, "RequestAuthorization") == 0)
{
GError *error = NULL;
Device *device_obj = device_new(g_variant_get_string(g_variant_get_child_value(parameters, 0), NULL));
if (_interactive)
g_print("Device: %s (%s)\n", device_get_alias(device_obj, &error), device_get_address(device_obj, &error));
if(error)
{
g_critical("Failed to get remote device's MAC address: %s", error->message);
g_error_free(error);
}
g_object_unref(device_obj);
if (_interactive)
{
g_print("Authorize this device pairing (yes/no)? ");
gchar yn[4] = {0,};
errno = 0;
if (scanf("%3s", yn) == EOF && errno)
g_warning("%s\n", strerror(errno));
if(g_ascii_strcasecmp(yn, "yes") == 0 || g_ascii_strcasecmp(yn, "y") == 0)
g_dbus_method_invocation_return_value(invocation, NULL);
else
g_dbus_method_invocation_return_dbus_error(invocation, "org.bluez.Error.Rejected", "Pairing rejected");
return;
}
g_dbus_method_invocation_return_dbus_error(invocation, "org.bluez.Error.Rejected", "Pairing rejected");
}
else if (g_strcmp0(method_name, "RequestConfirmation") == 0)
{
GError *error = NULL;
Device *device_obj = device_new(g_variant_get_string(g_variant_get_child_value(parameters, 0), NULL));
guint32 passkey = g_variant_get_uint32(g_variant_get_child_value(parameters, 1));
const gchar *pin = _find_device_pin(device_get_dbus_object_path(device_obj));
if (_interactive)
g_print("Device: %s (%s)\n", device_get_alias(device_obj, &error), device_get_address(device_obj, &error));
if(error)
{
g_critical("Failed to get remote device's MAC address: %s", error->message);
g_error_free(error);
}
g_object_unref(device_obj);
/* Try to use found PIN */
if (pin != NULL)
{
guint32 passkey_t;
sscanf(pin, "%u", &passkey_t);
if (g_strcmp0(pin, "*") == 0 || passkey_t == passkey)
{
if (_interactive)
g_print("Passkey confirmed\n");
g_dbus_method_invocation_return_value(invocation, NULL);
}
else
g_dbus_method_invocation_return_dbus_error(invocation, "org.bluez.Error.Rejected", "Passkey does not match");
return;
}
else if (_interactive)
{
g_print("Confirm passkey: %u (yes/no)? ", passkey);
gchar yn[4] = {0,};
errno = 0;
if (scanf("%3s", yn) == EOF && errno)
g_warning("%s\n", strerror(errno));
if(g_ascii_strcasecmp(yn, "yes") == 0 || g_ascii_strcasecmp(yn, "y") == 0)
g_dbus_method_invocation_return_value(invocation, NULL);
else
g_dbus_method_invocation_return_dbus_error(invocation, "org.bluez.Error.Rejected", "Passkey does not match");
return;
}
g_dbus_method_invocation_return_dbus_error(invocation, "org.bluez.Error.Rejected", "Passkey does not match");
}
else if (g_strcmp0(method_name, "RequestPasskey") == 0)
{
GError *error = NULL;
Device *device_obj = device_new(g_variant_get_string(g_variant_get_child_value(parameters, 0), NULL));
const gchar *pin = _find_device_pin(device_get_dbus_object_path(device_obj));
guint32 ret = 0;
gboolean invoke = FALSE;
if (_interactive)
g_print("Device: %s (%s)\n", device_get_alias(device_obj, &error), device_get_address(device_obj, &error));
if(error)
{
g_critical("Failed to get remote device's MAC address: %s", error->message);
g_error_free(error);
}
g_object_unref(device_obj);
/* Try to use found PIN */
if (pin != NULL)
{
if (_interactive)
g_print("Passkey found\n");
sscanf(pin, "%u", &ret);
invoke = TRUE;
}
else if (_interactive)
{
g_print("Enter passkey: ");
errno = 0;
if (scanf("%u", &ret) == EOF && errno)
g_warning("%s\n", strerror(errno));
invoke = TRUE;
}
if (invoke)
{
g_dbus_method_invocation_return_value(invocation, g_variant_new ("(u)", ret));
}
else
{
g_dbus_method_invocation_return_dbus_error(invocation, "org.bluez.Error.Rejected", "No passkey inputted");
}
}
else if (g_strcmp0(method_name, "RequestPinCode") == 0)
{
GError *error = NULL;
Device *device_obj = device_new(g_variant_get_string(g_variant_get_child_value(parameters, 0), NULL));
const gchar *pin = _find_device_pin(device_get_dbus_object_path(device_obj));
gchar *ret = NULL;
gboolean invoke = FALSE;
if (_interactive)
g_print("Device: %s (%s)\n", device_get_alias(device_obj, &error), device_get_address(device_obj, &error));
if(error)
{
g_critical("Failed to get remote device's MAC address: %s", error->message);
g_error_free(error);
}
g_object_unref(device_obj);
/* Try to use found PIN */
if (pin != NULL)
{
if (_interactive)
g_print("Passkey found\n");
sscanf(pin, "%ms", &ret);
invoke = TRUE;
}
else if (_interactive)
{
g_print("Enter passkey: ");
errno = 0;
if (scanf("%ms", &ret) == EOF && errno)
g_warning("%s\n", strerror(errno));
invoke = TRUE;
}
if (invoke)
{
g_dbus_method_invocation_return_value(invocation, g_variant_new ("(s)", ret));
}
else
{
g_dbus_method_invocation_return_dbus_error(invocation, "org.bluez.Error.Rejected", "No passkey inputted");
}
if (ret)
free(ret);
}
}
static void _bt_agent_g_destroy_notify(gpointer data)
{
g_free(data);
}
static const gchar *_find_device_pin(const gchar *device_path)
{
if (_pin_hash_table)
{
GError *error = NULL;
Device *device = device_new(device_path);
const gchar *pin_by_addr = g_hash_table_lookup(_pin_hash_table, device_get_address(device, &error));
const gchar *pin_by_alias = g_hash_table_lookup(_pin_hash_table, device_get_alias(device, &error));
if(error)
{
g_critical("Failed to get remote device's MAC address: %s", error->message);
g_error_free(error);
}
g_object_unref(device);
const gchar *pin_all = g_hash_table_lookup(_pin_hash_table, "*");
if (pin_by_addr)
return pin_by_addr;
else if (pin_by_alias)
return pin_by_alias;
else if (pin_all)
return pin_all;
}
return NULL;
}
void register_agent_callbacks(gboolean interactive_console, GHashTable *pin_dictonary, gpointer main_loop_object, GError **error)
{
GDBusInterfaceVTable bt_agent_table;
memset(&bt_agent_table, 0x0, sizeof(bt_agent_table));
if(pin_dictonary)
_pin_hash_table = pin_dictonary;
if(main_loop_object)
_mainloop = (GMainLoop *) main_loop_object;
_interactive = interactive_console;
GDBusNodeInfo *bt_agent_node_info = g_dbus_node_info_new_for_xml(_bt_agent_introspect_xml, error);
GDBusInterfaceInfo *bt_agent_interface_info = g_dbus_node_info_lookup_interface(bt_agent_node_info, AGENT_DBUS_INTERFACE);
bt_agent_table.method_call = _bt_agent_method_call_func;
_bt_agent_registration_id = g_dbus_connection_register_object(g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, error), AGENT_PATH, bt_agent_interface_info, &bt_agent_table, NULL, _bt_agent_g_destroy_notify, error);
g_dbus_node_info_unref(bt_agent_node_info);
}
void unregister_agent_callbacks(GError **error)
{
if (_bt_agent_registration_id)
g_dbus_connection_unregister_object(g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, error), _bt_agent_registration_id);
}
|