1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
|
/* $NetBSD: btkey.c,v 1.5 2021/08/25 22:52:25 rillig Exp $ */
/*-
* Copyright (c) 2007 Iain Hibbert
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <sys/cdefs.h>
__COPYRIGHT("@(#) Copyright (c) 2007 Iain Hibbert. All rights reserved.");
__RCSID("$NetBSD: btkey.c,v 1.5 2021/08/25 22:52:25 rillig Exp $");
#include <bluetooth.h>
#include <ctype.h>
#include <err.h>
#include <errno.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "btkey.h"
__dead static void usage(void);
static bool scan_key(const char *);
bdaddr_t laddr;
bdaddr_t raddr;
uint8_t key[HCI_KEY_SIZE];
int
main(int ac, char *av[])
{
struct hostent *he;
int ch;
bool cf, cd, lf, ld, rf, rd, wf, wd, nk;
memset(&laddr, 0, sizeof(laddr));
memset(&raddr, 0, sizeof(raddr));
memset(key, 0, sizeof(key));
cf = cd = lf = ld = rf = rd = wf = wd = nk = false;
while ((ch = getopt(ac, av, "a:cCd:k:lLrRwW")) != EOF) {
switch (ch) {
case 'a': /* remote device address */
if (!bt_aton(optarg, &raddr)) {
he = bt_gethostbyname(optarg);
if (he == NULL)
errx(EXIT_FAILURE, "%s: %s",
optarg, hstrerror(h_errno));
bdaddr_copy(&raddr, (bdaddr_t *)he->h_addr);
}
break;
case 'c': /* clear from file */
cf = true;
break;
case 'C': /* clear from device */
cd = true;
break;
case 'd': /* local device address */
if (!bt_devaddr(optarg, &laddr)
&& !bt_aton(optarg, &laddr)) {
he = bt_gethostbyname(optarg);
if (he == NULL)
errx(EXIT_FAILURE, "%s: %s",
optarg, hstrerror(h_errno));
bdaddr_copy(&laddr, (bdaddr_t *)he->h_addr);
}
break;
case 'k': /* new link key */
if (!scan_key(optarg))
errx(EXIT_FAILURE, "invalid key '%s'", optarg);
nk = true;
break;
case 'l': /* list from file */
lf = true;
break;
case 'L': /* list from device */
ld = true;
break;
case 'r': /* read from file */
rf = true;
break;
case 'R': /* read from device */
rd = true;
break;
case 'w': /* write to file */
wf = true;
break;
case 'W': /* write to device */
wd = true;
break;
default:
usage();
}
}
ac -= optind;
av += optind;
/*
* validate options
*/
if ((lf || ld) && (rf || rd || wf || wd || cf || cd || nk))
errx(EXIT_FAILURE, "list is exclusive of other options");
if (((rf && rd) || (rf && nk) || (rd && nk)) && (wf || wd))
errx(EXIT_FAILURE, "too many key sources");
if (((bdaddr_any(&laddr) || bdaddr_any(&raddr)) && !(lf || ld))
|| ((lf || ld) && (bdaddr_any(&laddr) || !bdaddr_any(&raddr)))
|| ac > 0)
usage();
/*
* do what we gotta do and be done
*/
if (!bdaddr_any(&laddr))
print_addr("device", &laddr);
if (!bdaddr_any(&raddr))
print_addr("bdaddr", &raddr);
if (lf && !list_file())
err(EXIT_FAILURE, "list file");
if (ld && !list_device())
err(EXIT_FAILURE, "list device");
if (nk)
print_key("new key", key);
if (rf) {
if (!read_file())
err(EXIT_FAILURE, "file key");
print_key("file key", key);
}
if (rd) {
if (!read_device())
err(EXIT_FAILURE, "device key");
print_key("device key", key);
}
if (wf || wd || cf || cd)
printf("\n");
if (wf) {
if (!write_file())
err(EXIT_FAILURE, "write to file");
printf("written to file\n");
}
if (wd) {
if (!write_device())
err(EXIT_FAILURE, "write to device");
printf("written to device\n");
}
if (cf) {
if (!clear_file())
err(EXIT_FAILURE, "clear from file");
printf("cleared from file\n");
}
if (cd) {
if (!clear_device())
err(EXIT_FAILURE, "clear from device");
printf("cleared from device\n");
}
exit(EXIT_SUCCESS);
}
static void
usage(void)
{
fprintf(stderr,
"Usage: %s [-cCrRwW] [-k key] -a address -d device\n"
" %s -lL -d device\n"
"\n", getprogname(), getprogname());
fprintf(stderr,
"Where:\n"
"\t-a address remote device address\n"
"\t-c clear from file\n"
"\t-C clear from device\n"
"\t-d device local device address\n"
"\t-k key user specified link_key\n"
"\t-l list file keys\n"
"\t-L list device keys\n"
"\t-r read from file\n"
"\t-R read from device\n"
"\t-w write to file\n"
"\t-W write to device\n"
"\n");
exit(EXIT_FAILURE);
}
static bool
scan_key(const char *arg)
{
static const char digits[] = "0123456789abcdef";
const char *p;
int i, j;
memset(key, 0, sizeof(key));
for (i = 0 ; i < HCI_KEY_SIZE ; i++) {
for (j = 0 ; j < 2 ; j++) {
if (*arg == '\0')
return true;
for (p = digits ;
*p != tolower((unsigned char)*arg) ;
p++)
if (*p == '\0')
return false;
arg++;
key[i] = (key[i] << 4) + (p - digits);
}
}
if (*arg != '\0')
return false;
return true;
}
void
print_key(const char *type, const uint8_t *src)
{
int i;
printf("%10s: ", type);
for (i = 0 ; i < HCI_KEY_SIZE ; i++)
printf("%2.2x", src[i]);
printf("\n");
}
void
print_addr(const char *type, const bdaddr_t *addr)
{
char name[HCI_DEVNAME_SIZE];
struct hostent *he;
printf("%10s: %s", type, bt_ntoa(addr, NULL));
if (bt_devname(name, addr))
printf(" (%s)", name);
else if ((he = bt_gethostbyaddr((const char *)addr,
sizeof(bdaddr_t), AF_BLUETOOTH)) != NULL)
printf(" (%s)", he->h_name);
printf("\n");
}
|