Return-Path: Message-ID: <871y7kf6y3.wl@maple.sm.sony.co.jp> From: Takashi Sasai To: "Maksim (Max) Krasnyanskiy" Cc: David LIBAULT , bluez-devel@lists.sourceforge.net Subject: Re: [Bluez-devel] Bluetooth device name database In-Reply-To: <5.1.0.14.2.20020919094030.07a2c388@mail1.qualcomm.com> References: <5.1.0.14.2.20020917171532.077f29d0@mail1.qualcomm.com> <5.1.0.14.2.20020913 090624.067c0238@mail1.qualcomm.com> <3D622E3300006FCF@serveur.inventel.fr> < 5.1.0.14.2.20020919094030.07a2c388@mail1.qualcomm.com> MIME-Version: 1.0 (generated by SEMI 1.14.3 - "Ushinoya") Sender: bluez-devel-admin@lists.sourceforge.net Errors-To: bluez-devel-admin@lists.sourceforge.net List-Help: List-Post: List-Subscribe: , List-Id: List-Unsubscribe: , List-Archive: Date: Tue, 24 Sep 2002 11:38:12 +0900 Content-Type: multipart/mixed; boundary="=-9ZxHX2dexhbeeqwP1jQV" --=-9ZxHX2dexhbeeqwP1jQV Content-Type: text/plain; CHARSET=US-ASCII Hi Max and David, > >libbluetooth ? Do you mean that libbluetooth should provide a way to read > >from the name database ? In that case, it should also provide methods to > >access the link_key database (to remove a subscription), doesn't it ? > Something like that. > > >An other question : if an application (link key managment software) wants to > >modify the link_key file, and hcid receives a connection at the same time... > >Is there some kind of lock so that the link key file is always consistent ? > Currently no. But writes to a file are almost guarantied to be atomic. I wrote such a code before. How about this pache? Regards, Takashi --=-9ZxHX2dexhbeeqwP1jQV Content-Disposition: attachment; filename="bluez-libs-2.0-rmkey.patch" Content-Transfer-Encoding: quoted-printable Content-Type: application/octet-stream; TYPE=patch diff -uNr bluez-libs-2.0.orig/include/hci_lib.h bluez-libs-2.0/include/hci_= lib.h --- bluez-libs-2.0.orig/include/hci_lib.h Mon Aug 26 08:55:00 2002 +++ bluez-libs-2.0/include/hci_lib.h Thu Sep 5 11:10:41 2002 @@ -161,6 +161,10 @@ return (f->opcode =3D=3D opcode); } =20 +/* for HCId */ +int remove_link_key(int dev_id, bdaddr_t *dba); +int show_link_keys(void); + #ifdef __cplusplus } #endif diff -uNr bluez-libs-2.0.orig/src/hci.c bluez-libs-2.0/src/hci.c --- bluez-libs-2.0.orig/src/hci.c Mon Aug 26 08:55:00 2002 +++ bluez-libs-2.0/src/hci.c Thu Sep 5 11:12:44 2002 @@ -47,6 +47,8 @@ #include #include =20 +#include + typedef struct { char *str; unsigned int val; } hci_map; @@ -1000,3 +1002,164 @@ return -1; return 0; } + +/* for HCId */ + +#define HCID_KEY_FILE "/etc/bluetooth/link_key" + +struct link_key { + bdaddr_t sba; + bdaddr_t dba; + uint8_t key[16]; + uint8_t type; + time_t time; +}; + +/* Read exactly len bytes */ +static inline int read_n(int fd, void *buf, int len) +{ + register int t =3D 0, w; + + while (len > 0) { + if ((w =3D read(fd, buf, len)) < 0) { + if (errno =3D=3D EINTR || errno =3D=3D EAGAIN) + continue; + return -1; + } + if (!w) + return 0; + len -=3D w; + buf +=3D w; + t +=3D w; + } + + return t; +} + +/* Write exactly len bytes */ +static inline int write_n(int fd, void *buf, int len) +{ + register int t =3D 0, w; + + while (len > 0) { + if ((w =3D write(fd, buf, len)) < 0) { + if (errno =3D=3D EINTR || errno =3D=3D EAGAIN) + continue; + return -1; + } + if (!w) + return 0; + len -=3D w; + buf +=3D w; + t +=3D w; + } + + return t; +} + +int remove_link_key(int dev_id, bdaddr_t *dba) +{ + char sa[40], da[40]; + bdaddr_t sba; + static struct link_key k; + struct link_key *key =3D NULL; + struct link_key *keydb =3D NULL; + int i, r, f, exist, err, size, keynum; + + if ((dev_id < 0) && (dev_id =3D hci_get_route(dba)) < 0) { + return -1; + } + + if (hci_devba(dev_id, &sba) < 0) { + return -1; + } + + ba2str(&sba, sa); ba2str(dba, da); + + f =3D open(HCID_KEY_FILE, O_RDWR, 0); =20 + if (f < 0) { + syslog(LOG_ERR, "Link key database open failed. %s(%d)", + strerror(errno), errno); + return -1; + } + + flock(f, LOCK_EX); + err =3D 0; + exist =3D 0; + + size =3D lseek(f, 0, SEEK_END); + if ((size > 0) && (size%sizeof(k) =3D=3D 0)) { + keydb =3D (struct link_key *)malloc(size); + if (keydb =3D=3D NULL) { + err =3D -1; + goto done; + } + keynum =3D size/sizeof(k); + } else { + syslog(LOG_INFO, "%s link key %s %s", exist ? "Remove" : "Not found",=20 + sa, da); + err =3D -1; + goto done; + } + + key =3D keydb;=20 + lseek(f, 0, SEEK_SET); + while ((r =3D read_n(f, &k, sizeof(k)))) { + if (!bacmp(&k.sba, &sba) && !bacmp(&k.dba, dba)) { + exist =3D 1; + } else { + *key =3D k; key++; + } + } + + if (exist) { + key =3D keydb; + lseek(f, 0, SEEK_SET); + ftruncate(f, 0); + lseek(f, 0, SEEK_SET); + for (i =3D 0; i < keynum-1; i++) { + if ((write_n(f, key, sizeof(*key)) < 0)) { + syslog(LOG_ERR, "Link key database write failed. %s(%d)", + strerror(errno), errno); + err =3D -1; + goto done; + } + key++; + } + } + =20 + ba2str(&sba, sa); ba2str(dba, da); + syslog(LOG_INFO, "%s link key %s %s", exist ? "Remove" : "Not found",=20 + sa, da); + +done: + free(keydb); + flock(f, LOCK_UN); + close(f); + return err; +} + +int show_link_keys() +{ + char sa[40], da[40]; + static struct link_key k; + int r, f; + + f =3D open(HCID_KEY_FILE, O_RDONLY, 0); =20 + if (f < 0) { + syslog(LOG_ERR, "Link key database open failed. %s(%d)", + strerror(errno), errno); + return -1; + } + + flock(f, LOCK_EX); + + while ((r =3D read_n(f, &k, sizeof(k)))) { + ba2str(&k.sba, sa); ba2str(&k.dba, da); + printf("\t%s - %s\n", sa, da); + } + + flock(f, LOCK_UN); + close(f); + return 0; +} --=-9ZxHX2dexhbeeqwP1jQV Content-Disposition: attachment; filename="bluez-utils-2.0-rmkey.patch" Content-Transfer-Encoding: quoted-printable Content-Type: application/octet-stream; TYPE=patch diff -uNr bluez-utils-2.0.orig/hcid/security.c bluez-utils-2.0/hcid/securit= y.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" =20 +#include + static GIOChannel *io_chan[HCI_MAX_DEV]; =20 static int pairing; @@ -95,8 +97,11 @@ int f; =20 f =3D open(hcid.key_file, O_RDONLY); - if (f >=3D 0) + if (f >=3D 0) { + flock(f, LOCK_EX); key =3D __get_link_key(f, sba, dba); + flock(f, LOCK_UN); + } else if (errno !=3D ENOENT) syslog(LOG_ERR, "Link key database open failed. %s(%d)", strerror(errno), errno); @@ -135,6 +140,8 @@ return; } =20 + flock(f, LOCK_EX); + /* Check if key already exist */ exist =3D __get_link_key(f, &key->sba, &key->dba); =20 @@ -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; } =09 @@ -159,6 +167,7 @@ =20 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); =20 failed: close(f); diff -uNr bluez-utils-2.0.orig/tools/hcitool.c bluez-utils-2.0/tools/hcitoo= l.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); } =20 +/* Remove link key */ + +static struct option rmkey_options[] =3D { + {"help", 0,0, 'h'}, + {0, 0, 0, 0} +}; + +static char *rmkey_help =3D + "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 -=3D optind; + argv +=3D optind; + + if (argc < 1) { + printf(rmkey_help); + return; + } + + baswap(&bdaddr, strtoba(argv[0])); + + if (dev_id < 0) { + dev_id =3D hci_get_route(&bdaddr); + if (dev_id < 0) { + fprintf(stderr, "Device is not available.\n"); + exit(1); + } + } + + if (remove_link_key(dev_id, &bdaddr) =3D=3D 0) + printf("ok\n"); + +} + +/* Display link keys */ + +static struct option keys_options[] =3D { + {"help", 0,0, 'h'}, + {0, 0, 0, 0} +}; + +static char *keys_help =3D=20 + "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(); +} + =20 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} }; =20 --=-9ZxHX2dexhbeeqwP1jQV--