diff -uNr bluez-utils-2.0.orig/hcid/security.c bluez-utils-2.0/hcid/security.c --- bluez-utils-2.0.orig/hcid/security.c Fri Jul 19 03:12:46 2002 +++ bluez-utils-2.0/hcid/security.c Thu Aug 8 17:09:58 2002 @@ -51,6 +51,8 @@ #include "hcid.h" #include "lib.h" +#include + static GIOChannel *io_chan[HCI_MAX_DEV]; static int pairing; @@ -95,8 +97,11 @@ int f; f = open(hcid.key_file, O_RDONLY); - if (f >= 0) + if (f >= 0) { + flock(f, LOCK_EX); key = __get_link_key(f, sba, dba); + flock(f, LOCK_UN); + } else if (errno != ENOENT) syslog(LOG_ERR, "Link key database open failed. %s(%d)", strerror(errno), errno); @@ -135,6 +140,8 @@ return; } + flock(f, LOCK_EX); + /* Check if key already exist */ exist = __get_link_key(f, &key->sba, &key->dba); @@ -149,6 +156,7 @@ if (err < 0) { syslog(LOG_ERR, "Link key database seek failed. %s(%d)", strerror(errno), errno); + flock(f, LOCK_UN); goto failed; } @@ -159,6 +167,7 @@ ba2str(&key->sba, sa); ba2str(&key->dba, da); syslog(LOG_INFO, "%s link key %s %s", exist ? "Replacing" : "Saving", sa, da); + flock(f, LOCK_UN); failed: close(f); diff -uNr bluez-utils-2.0.orig/tools/hcitool.c bluez-utils-2.0/tools/hcitool.c --- bluez-utils-2.0.orig/tools/hcitool.c Tue Jun 25 13:36:09 2002 +++ bluez-utils-2.0/tools/hcitool.c Fri Aug 9 17:21:38 2002 @@ -865,6 +865,78 @@ free(cr); } +/* Remove link key */ + +static struct option rmkey_options[] = { + {"help", 0,0, 'h'}, + {0, 0, 0, 0} +}; + +static char *rmkey_help = + "Usage:\n" + "\trmkey \n"; + +static void cmd_rmkey(int dev_id, int argc, char **argv) +{ + bdaddr_t bdaddr; + int opt, dd; + + for_each_opt(opt, rmkey_options, NULL) { + switch(opt) { + default: + printf(rmkey_help); + return; + } + } + argc -= optind; + argv += optind; + + if (argc < 1) { + printf(rmkey_help); + return; + } + + baswap(&bdaddr, strtoba(argv[0])); + + if (dev_id < 0) { + dev_id = hci_get_route(&bdaddr); + if (dev_id < 0) { + fprintf(stderr, "Device is not available.\n"); + exit(1); + } + } + + if (remove_link_key(dev_id, &bdaddr) == 0) + printf("ok\n"); + +} + +/* Display link keys */ + +static struct option keys_options[] = { + {"help", 0,0, 'h'}, + {0, 0, 0, 0} +}; + +static char *keys_help = + "Usage:\n" + "\tkeys\n"; + +static void cmd_keys(int dev_id, int argc, char **argv) +{ + int opt; + for_each_opt(opt, keys_options, NULL) { + switch(opt) { + default: + printf(keys_help); + return; + } + } + + printf("Keys:\n"); + show_link_keys(); +} + struct { char *cmd; @@ -882,6 +954,10 @@ { "dc", cmd_dc, "Disconnect from remote device" }, { "cpt", cmd_cpt, "Change connection packet type" }, { "rssi", cmd_rssi, "Display connection RSSI" }, + + { "rmkey", cmd_rmkey, "Remove link key" }, + { "keys", cmd_keys, "Show link keys" }, + { NULL, NULL, 0} };