config.c revision 1.1 1 1.1 gdamore /* $NetBSD: config.c,v 1.1 2006/06/19 15:44:56 gdamore Exp $ */
2 1.1 gdamore
3 1.1 gdamore /*-
4 1.1 gdamore * Copyright (c) 2006 Itronix Inc.
5 1.1 gdamore * All rights reserved.
6 1.1 gdamore *
7 1.1 gdamore * Redistribution and use in source and binary forms, with or without
8 1.1 gdamore * modification, are permitted provided that the following conditions
9 1.1 gdamore * are met:
10 1.1 gdamore * 1. Redistributions of source code must retain the above copyright
11 1.1 gdamore * notice, this list of conditions and the following disclaimer.
12 1.1 gdamore * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 gdamore * notice, this list of conditions and the following disclaimer in the
14 1.1 gdamore * documentation and/or other materials provided with the distribution.
15 1.1 gdamore * 3. The name of Itronix Inc. may not be used to endorse
16 1.1 gdamore * or promote products derived from this software without specific
17 1.1 gdamore * prior written permission.
18 1.1 gdamore *
19 1.1 gdamore * THIS SOFTWARE IS PROVIDED BY ITRONIX INC. ``AS IS'' AND
20 1.1 gdamore * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.1 gdamore * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.1 gdamore * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL ITRONIX INC. BE LIABLE FOR ANY
23 1.1 gdamore * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24 1.1 gdamore * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25 1.1 gdamore * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
26 1.1 gdamore * ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.1 gdamore * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.1 gdamore * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.1 gdamore * POSSIBILITY OF SUCH DAMAGE.
30 1.1 gdamore */
31 1.1 gdamore
32 1.1 gdamore #include <sys/cdefs.h>
33 1.1 gdamore __RCSID("$NetBSD: config.c,v 1.1 2006/06/19 15:44:56 gdamore Exp $");
34 1.1 gdamore
35 1.1 gdamore #include <sys/time.h>
36 1.1 gdamore #include <bluetooth.h>
37 1.1 gdamore #include <errno.h>
38 1.1 gdamore #include <event.h>
39 1.1 gdamore #include <fcntl.h>
40 1.1 gdamore #include <string.h>
41 1.1 gdamore #include <syslog.h>
42 1.1 gdamore #include <unistd.h>
43 1.1 gdamore
44 1.1 gdamore #include "bthcid.h"
45 1.1 gdamore
46 1.1 gdamore static bt_cfgentry_t *cfg = NULL;
47 1.1 gdamore
48 1.1 gdamore uint8_t *
49 1.1 gdamore lookup_key(bdaddr_t *laddr, bdaddr_t *raddr)
50 1.1 gdamore {
51 1.1 gdamore bt_handle_t handle;
52 1.1 gdamore
53 1.1 gdamore if (cfg != NULL) {
54 1.1 gdamore bt_freeconfig(cfg);
55 1.1 gdamore cfg = NULL;
56 1.1 gdamore }
57 1.1 gdamore
58 1.1 gdamore handle = bt_openconfig(key_file);
59 1.1 gdamore if (handle == NULL)
60 1.1 gdamore return NULL;
61 1.1 gdamore
62 1.1 gdamore cfg = bt_getconfig(handle, raddr);
63 1.1 gdamore bt_closeconfig(handle);
64 1.1 gdamore
65 1.1 gdamore if (cfg == NULL) {
66 1.1 gdamore handle = bt_openconfig(config_file);
67 1.1 gdamore if (handle == NULL)
68 1.1 gdamore return NULL;
69 1.1 gdamore
70 1.1 gdamore cfg = bt_getconfig(handle, raddr);
71 1.1 gdamore bt_closeconfig(handle);
72 1.1 gdamore }
73 1.1 gdamore
74 1.1 gdamore if (cfg != NULL)
75 1.1 gdamore return cfg->key;
76 1.1 gdamore
77 1.1 gdamore return NULL;
78 1.1 gdamore }
79 1.1 gdamore
80 1.1 gdamore uint8_t *
81 1.1 gdamore lookup_pin(bdaddr_t *laddr, bdaddr_t *raddr)
82 1.1 gdamore {
83 1.1 gdamore bt_handle_t handle;
84 1.1 gdamore
85 1.1 gdamore if (cfg != NULL) {
86 1.1 gdamore bt_freeconfig(cfg);
87 1.1 gdamore cfg = NULL;
88 1.1 gdamore }
89 1.1 gdamore
90 1.1 gdamore handle = bt_openconfig(config_file);
91 1.1 gdamore if (handle != NULL) {
92 1.1 gdamore cfg = bt_getconfig(handle, raddr);
93 1.1 gdamore bt_closeconfig(handle);
94 1.1 gdamore
95 1.1 gdamore if (cfg != NULL && cfg->pin != NULL)
96 1.1 gdamore return cfg->pin;
97 1.1 gdamore }
98 1.1 gdamore
99 1.1 gdamore return lookup_item(laddr, raddr);
100 1.1 gdamore }
101 1.1 gdamore
102 1.1 gdamore void
103 1.1 gdamore save_key(bdaddr_t *laddr, bdaddr_t *raddr, uint8_t *key)
104 1.1 gdamore {
105 1.1 gdamore static const char hex[] = "0123456789abcdef";
106 1.1 gdamore char buffer[100], keybuf[HCI_KEY_SIZE * 2 + 1];
107 1.1 gdamore int fd, n, i;
108 1.1 gdamore
109 1.1 gdamore fd = open(key_file, O_WRONLY|O_APPEND|O_CREAT|O_EXLOCK, 0600);
110 1.1 gdamore if (fd < 0) {
111 1.1 gdamore syslog(LOG_ERR, "Cannot open keyfile %s. %s (%d)",
112 1.1 gdamore key_file, strerror(errno), errno);
113 1.1 gdamore
114 1.1 gdamore return;
115 1.1 gdamore }
116 1.1 gdamore
117 1.1 gdamore for (n = i = 0 ; i < HCI_KEY_SIZE ; i++) {
118 1.1 gdamore keybuf[n++] = hex[(key[i] >> 4) & 0xf];
119 1.1 gdamore keybuf[n++] = hex[key[i] & 0xf];
120 1.1 gdamore }
121 1.1 gdamore keybuf[n] = '\0';
122 1.1 gdamore
123 1.1 gdamore n = snprintf(buffer, sizeof(buffer),
124 1.1 gdamore "\ndevice {\n"
125 1.1 gdamore "\tbdaddr %s;\n"
126 1.1 gdamore "\tkey 0x%s;\n"
127 1.1 gdamore "}\n",
128 1.1 gdamore bt_ntoa(raddr, NULL),
129 1.1 gdamore keybuf);
130 1.1 gdamore
131 1.1 gdamore write(fd, buffer, (size_t)n);
132 1.1 gdamore close(fd);
133 1.1 gdamore }
134