diff options
-rw-r--r-- | hcid/hcid.h | 1 | ||||
-rw-r--r-- | hcid/security.c | 19 | ||||
-rw-r--r-- | hcid/storage.c | 61 |
3 files changed, 72 insertions, 9 deletions
diff --git a/hcid/hcid.h b/hcid/hcid.h index 9010f1d3..36cba71f 100644 --- a/hcid/hcid.h +++ b/hcid/hcid.h @@ -111,6 +111,7 @@ gboolean hcid_dbus_init(void); #endif int write_device_name(const bdaddr_t *local, const bdaddr_t *peer, const char *name); +int read_device_name(const bdaddr_t *local, const bdaddr_t *peer, 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 1c4d988f..3cb7cde9 100644 --- a/hcid/security.c +++ b/hcid/security.c @@ -248,11 +248,11 @@ static int read_default_pin_code(void) ERR - No PIN available */ -static void call_pin_helper(int dev, struct hci_conn_info *ci) +static void call_pin_helper(int dev, bdaddr_t *sba, struct hci_conn_info *ci) { pin_code_reply_cp pr; struct sigaction sa; - char addr[18], str[255], *pin, name[20]; + char addr[18], str[255], *pin, name[249]; FILE *pipe; int ret, len; @@ -273,12 +273,13 @@ static void call_pin_helper(int dev, struct hci_conn_info *ci) goto reject; } - name[0] = 0; + memset(name, 0, sizeof(name)); + read_device_name(sba, &ci->bdaddr, name); //hci_remote_name(dev, &ci->bdaddr, sizeof(name), name, 0); ba2str(&ci->bdaddr, addr); - sprintf(str, "%s %s %s \'%s\'", hcid.pin_helper, - ci->out ? "out" : "in", addr, name); + snprintf(str, sizeof(str), "%s %s %s \'%s\'", hcid.pin_helper, + ci->out ? "out" : "in", addr, name); setenv("PATH", "/bin:/usr/bin:/usr/local/bin", 1); @@ -325,7 +326,7 @@ reject: exit(0); } -static void request_pin(int dev, struct hci_conn_info *ci) +static void request_pin(int dev, bdaddr_t *sba, struct hci_conn_info *ci) { #ifdef ENABLE_DBUS if (hcid.dbus_pin_helper) { @@ -333,7 +334,7 @@ static void request_pin(int dev, struct hci_conn_info *ci) return; } #endif - call_pin_helper(dev, ci); + call_pin_helper(dev, sba, ci); } static void pin_code_request(int dev, bdaddr_t *sba, bdaddr_t *dba) @@ -382,11 +383,11 @@ static void pin_code_request(int dev, bdaddr_t *sba, bdaddr_t *dba) /* Outgoing connection */ /* Let PIN helper handle that */ - request_pin(dev, ci); + request_pin(dev, sba, ci); } } else { /* Let PIN helper handle that */ - request_pin(dev, ci); + request_pin(dev, sba, ci); } free(cr); return; diff --git a/hcid/storage.c b/hcid/storage.c index da34b416..31f7eb62 100644 --- a/hcid/storage.c +++ b/hcid/storage.c @@ -242,6 +242,67 @@ close: return err; } +int read_device_name(const bdaddr_t *local, const bdaddr_t *peer, char *name) +{ + char filename[PATH_MAX + 1], addr[18], str[249], *buf, *ptr; + bdaddr_t bdaddr; + struct stat st; + int fd, pos, err = -ENOENT; + + ba2str(local, addr); + snprintf(filename, PATH_MAX, "%s/%s/names", 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)) { + snprintf(name, 249, "%s", str); + err = 0; + 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 write_link_key(const bdaddr_t *local, const bdaddr_t *peer, const unsigned char *key, const int type) { struct list *temp, *list = NULL; |