diff options
-rw-r--r-- | hcid/hcid.h | 1 | ||||
-rw-r--r-- | hcid/security.c | 8 | ||||
-rw-r--r-- | hcid/storage.c | 70 |
3 files changed, 77 insertions, 2 deletions
diff --git a/hcid/hcid.h b/hcid/hcid.h index 9c96d91a..9010f1d3 100644 --- a/hcid/hcid.h +++ b/hcid/hcid.h @@ -113,3 +113,4 @@ gboolean hcid_dbus_init(void); int write_device_name(const bdaddr_t *local, const bdaddr_t *peer, const char *name); int write_link_key(const bdaddr_t *local, const bdaddr_t *peer, const unsigned char *key, const int type); int read_link_key(const bdaddr_t *local, const bdaddr_t *peer, unsigned char *key); +int read_pin_code(const bdaddr_t *local, const bdaddr_t *peer, char *pin); diff --git a/hcid/security.c b/hcid/security.c index e66e030e..1c4d988f 100644 --- a/hcid/security.c +++ b/hcid/security.c @@ -144,6 +144,7 @@ static void link_key_request(int dev, bdaddr_t *sba, bdaddr_t *dba) } } +#if 0 static void save_link_key(struct link_key *key) { struct link_key *exist; @@ -185,6 +186,7 @@ static void save_link_key(struct link_key *key) failed: close(f); } +#endif static void link_key_notify(int dev, bdaddr_t *sba, void *ptr) { @@ -202,14 +204,16 @@ static void link_key_notify(int dev, bdaddr_t *sba, void *ptr) key.type = evt->key_type; key.time = time(0); +#if 0 save_link_key(&key); +#endif write_link_key(sba, dba, evt->link_key, evt->key_type); } /* PIN code handling */ -int read_pin_code(void) +static int read_default_pin_code(void) { char buf[17]; FILE *f; @@ -542,7 +546,7 @@ void stop_security_manager(int hdev) void init_security_data(void) { /* Set local PIN code */ - if (read_pin_code() < 0) { + if (read_default_pin_code() < 0) { strcpy(hcid.pin_code, "BlueZ"); hcid.pin_len = 5; } diff --git a/hcid/storage.c b/hcid/storage.c index 78624231..da34b416 100644 --- a/hcid/storage.c +++ b/hcid/storage.c @@ -205,7 +205,9 @@ int write_device_name(const bdaddr_t *local, const bdaddr_t *peer, const char *n while (sscanf(ptr, "%17s %[^\n]\n%n", addr, str, &pos) != EOF) { str2ba(addr, &bdaddr); str[sizeof(str) - 1] = '\0'; + list = list_add(list, &bdaddr, str, sizeof(str)); + memset(str, 0, sizeof(str)); ptr += pos; if (ptr - buf >= st.st_size) @@ -283,7 +285,9 @@ int write_link_key(const bdaddr_t *local, const bdaddr_t *peer, const unsigned c while (sscanf(ptr, "%17s %[^\n]\n%n", addr, str, &pos) != EOF) { str2ba(addr, &bdaddr); str[sizeof(str) - 1] = '\0'; + list = list_add(list, &bdaddr, str, sizeof(str)); + memset(str, 0, sizeof(str)); ptr += pos; if (ptr - buf >= st.st_size) @@ -358,8 +362,10 @@ int read_link_key(const bdaddr_t *local, const bdaddr_t *peer, unsigned char *ke ptr = buf; + memset(str, 0, sizeof(str)); while (sscanf(ptr, "%17s %[^\n]\n%n", addr, str, &pos) != EOF) { str2ba(addr, &bdaddr); + str[sizeof(str) - 1] = '\0'; if (!bacmp(&bdaddr, peer)) { memset(tmp, 0, sizeof(tmp)); @@ -371,7 +377,71 @@ int read_link_key(const bdaddr_t *local, const bdaddr_t *peer, unsigned char *ke break; } + memset(str, 0, sizeof(str)); ptr += pos; + if (ptr - buf >= st.st_size) + break; + }; + } + +unlock: + flock(fd, LOCK_UN); + +close: + close(fd); + return err; +} + +int read_pin_code(const bdaddr_t *local, const bdaddr_t *peer, char *pin) +{ + char filename[PATH_MAX + 1], addr[18], str[17], *buf, *ptr; + bdaddr_t bdaddr; + struct stat st; + int fd, pos, err = -ENOENT; + + ba2str(local, addr); + snprintf(filename, PATH_MAX, "%s/%s/pincodes", DEVPATH, addr); + + fd = open(filename, O_RDONLY); + if (fd < 0) + return -errno; + + if (flock(fd, LOCK_SH) < 0) { + err = -errno; + goto close; + } + + if (fstat(fd, &st) < 0) { + err = -errno; + goto unlock; + } + + buf = malloc(st.st_size); + if (!buf) { + err = -ENOMEM; + goto unlock; + } + + if (st.st_size > 0) { + read(fd, buf, st.st_size); + + ptr = buf; + + memset(str, 0, sizeof(str)); + while (sscanf(ptr, "%17s %[^\n]\n%n", addr, str, &pos) != EOF) { + str2ba(addr, &bdaddr); + str[sizeof(str) - 1] = '\0'; + + if (!bacmp(&bdaddr, peer)) { + strncpy(pin, str, 16); + err = 0; + break; + } + + memset(str, 0, sizeof(str)); + ptr += pos; + if (ptr - buf >= st.st_size) + break; }; } |