5 * Copyright (c) 2001-2002 Maksim Yevmenkin <m_evmenkin@yahoo.com>
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * $Id: parser.y,v 1.5 2003/06/07 21:22:30 max Exp $
30 * $FreeBSD: src/usr.sbin/bluetooth/hcsecd/parser.y,v 1.4 2004/09/14 20:04:33 emax Exp $
31 * $DragonFly: src/usr.sbin/bthcid/parser.y,v 1.1 2008/01/30 14:10:19 hasso Exp $
34 #include <sys/fcntl.h>
35 #include <sys/queue.h>
36 #include <bluetooth.h>
49 static void free_key (link_key_p key);
50 static int hexa2int4(char *a);
51 static int hexa2int8(char *a);
54 static LIST_HEAD(, link_key) link_keys;
55 char *config_file = "/etc/bluetooth/bthcid.conf";
57 static link_key_p key = NULL;
64 %token <string> T_BDADDRSTRING T_HEXSTRING T_STRING
65 %token T_DEVICE T_BDADDR T_NAME T_KEY T_PIN T_NOKEY T_NOPIN T_JUNK
75 key = (link_key_p) malloc(sizeof(*key));
77 syslog(LOG_ERR, "Could not allocate new " \
82 memset(key, 0, sizeof(*key));
86 if (get_key(&key->bdaddr, 1) != NULL) {
87 syslog(LOG_ERR, "Ignoring duplicated entry " \
89 bt_ntoa(&key->bdaddr, NULL));
92 LIST_INSERT_HEAD(&link_keys, key, next);
108 bdaddr: T_BDADDR T_BDADDRSTRING
110 if (!bt_aton($2, &key->bdaddr)) {
111 syslog(LOG_ERR, "Could not parse BD_ADDR " \
118 name: T_NAME T_STRING
120 if (key->name != NULL)
123 key->name = strdup($2);
124 if (key->name == NULL) {
125 syslog(LOG_ERR, "Could not allocate new " \
132 key: T_KEY T_HEXSTRING
136 if (key->key != NULL)
139 key->key = (uint8_t *) malloc(HCI_KEY_SIZE);
140 if (key->key == NULL) {
141 syslog(LOG_ERR, "Could not allocate new " \
146 memset(key->key, 0, HCI_KEY_SIZE);
148 len = strlen($2) / 2;
149 if (len > HCI_KEY_SIZE)
152 for (i = 0; i < len; i ++)
153 key->key[i] = hexa2int8((char *)($2) + 2*i);
157 if (key->key != NULL)
166 if (key->pin != NULL)
169 key->pin = strdup($2);
170 if (key->pin == NULL) {
171 syslog(LOG_ERR, "Could not allocate new " \
178 if (key->pin != NULL)
187 /* Display parser error message */
189 yyerror(char const *message)
191 syslog(LOG_ERR, "%s in line %d", message, yylineno);
194 /* Re-read config file */
196 read_config_file(void)
200 if (config_file == NULL) {
201 syslog(LOG_ERR, "Unknown config file name!");
205 if ((yyin = fopen(config_file, "r")) == NULL) {
206 syslog(LOG_ERR, "Could not open config file '%s'. %s (%d)",
207 config_file, strerror(errno), errno);
213 syslog(LOG_ERR, "Could not parse config file '%s'",config_file);
229 link_key_p key = NULL;
231 while ((key = LIST_FIRST(&link_keys)) != NULL) {
232 LIST_REMOVE(key, next);
237 /* Find link key entry in the list. Return exact or default match */
239 get_key(bdaddr_p bdaddr, int exact_match)
241 link_key_p key = NULL, defkey = NULL;
243 LIST_FOREACH(key, &link_keys, next) {
244 if (memcmp(bdaddr, &key->bdaddr, sizeof(key->bdaddr)) == 0)
248 if (memcmp(BDADDR_ANY, &key->bdaddr,
249 sizeof(key->bdaddr)) == 0)
253 return ((key != NULL)? key : defkey);
261 link_key_p key = NULL;
264 LIST_FOREACH(key, &link_keys, next) {
265 if (key->key != NULL)
266 snprintf(buffer, sizeof(buffer),
267 "0x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
268 key->key[0], key->key[1], key->key[2],
269 key->key[3], key->key[4], key->key[5],
270 key->key[6], key->key[7], key->key[8],
271 key->key[9], key->key[10], key->key[11],
272 key->key[12], key->key[13], key->key[14],
280 (key->name != NULL)? key->name : "noname",
281 bt_ntoa(&key->bdaddr, NULL),
282 (key->pin != NULL)? key->pin : "nopin",
283 (key->key != NULL)? buffer : "nokey");
293 link_key_t *key = NULL;
294 char buf[BTHCID_BUFFER_SIZE], *p = NULL, *cp = NULL;
298 if ((f = fopen(BTHCID_KEYSFILE, "r")) == NULL) {
302 syslog(LOG_ERR, "Could not open keys file %s. %s (%d)\n",
303 BTHCID_KEYSFILE, strerror(errno), errno);
308 while ((p = fgets(buf, sizeof(buf), f)) != NULL) {
311 if ((cp = strpbrk(p, " ")) == NULL)
316 if (!bt_aton(p, &bdaddr))
319 if ((key = get_key(&bdaddr, 1)) == NULL)
322 if (key->key == NULL) {
323 key->key = (uint8_t *) malloc(HCI_KEY_SIZE);
324 if (key->key == NULL) {
325 syslog(LOG_ERR, "Could not allocate link key");
330 memset(key->key, 0, HCI_KEY_SIZE);
332 len = strlen(cp) / 2;
333 if (len > HCI_KEY_SIZE)
336 for (i = 0; i < len; i ++)
337 key->key[i] = hexa2int8(cp + 2*i);
339 syslog(LOG_DEBUG, "Restored link key for the entry, " \
340 "remote bdaddr %s, name '%s'",
341 bt_ntoa(&key->bdaddr, NULL),
342 (key->name != NULL)? key->name : "No name");
354 link_key_p key = NULL;
355 char tmp[PATH_MAX], buf[BTHCID_BUFFER_SIZE];
358 snprintf(tmp, sizeof(tmp), "%s.tmp", BTHCID_KEYSFILE);
359 if ((f = open(tmp, O_RDWR|O_CREAT|O_TRUNC|O_EXCL, 0600)) < 0) {
360 syslog(LOG_ERR, "Could not create temp keys file %s. %s (%d)\n",
361 tmp, strerror(errno), errno);
365 LIST_FOREACH(key, &link_keys, next) {
366 if (key->key == NULL)
369 snprintf(buf, sizeof(buf),
370 "%s %02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x\n",
371 bt_ntoa(&key->bdaddr, NULL),
372 key->key[0], key->key[1], key->key[2], key->key[3],
373 key->key[4], key->key[5], key->key[6], key->key[7],
374 key->key[8], key->key[9], key->key[10], key->key[11],
375 key->key[12], key->key[13], key->key[14], key->key[15]);
377 if (write(f, buf, strlen(buf)) < 0) {
378 syslog(LOG_ERR, "Could not write temp keys file. " \
379 "%s (%d)\n", strerror(errno), errno);
386 if (rename(tmp, BTHCID_KEYSFILE) < 0) {
387 syslog(LOG_ERR, "Could not rename(%s, %s). %s (%d)\n",
388 tmp, BTHCID_KEYSFILE, strerror(errno), errno);
398 free_key(link_key_p key)
400 if (key->name != NULL)
402 if (key->key != NULL)
404 if (key->pin != NULL)
407 memset(key, 0, sizeof(*key));
411 /* Convert hex ASCII to int4 */
415 if ('0' <= *a && *a <= '9')
418 if ('A' <= *a && *a <= 'F')
419 return (*a - 'A' + 0xa);
421 if ('a' <= *a && *a <= 'f')
422 return (*a - 'a' + 0xa);
424 syslog(LOG_ERR, "Invalid hex character: '%c' (%#x)", *a, *a);
428 /* Convert hex ASCII to int8 */
432 return ((hexa2int4(a) << 4) | hexa2int4(a + 1));