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 $
33 #include <sys/fcntl.h>
34 #include <sys/queue.h>
35 #include <bluetooth.h>
48 static void free_key (link_key_p key);
49 static int hexa2int4(char *a);
50 static int hexa2int8(char *a);
53 static LIST_HEAD(, link_key) link_keys;
54 char *config_file = "/etc/bluetooth/hcsecd.conf";
56 static link_key_p key = NULL;
63 %token <string> T_BDADDRSTRING T_HEXSTRING T_STRING
64 %token T_DEVICE T_BDADDR T_NAME T_KEY T_PIN T_NOKEY T_NOPIN T_JUNK
74 key = (link_key_p) malloc(sizeof(*key));
76 syslog(LOG_ERR, "Could not allocate new " \
81 memset(key, 0, sizeof(*key));
85 if (get_key(&key->bdaddr, 1) != NULL) {
86 syslog(LOG_ERR, "Ignoring duplicated entry " \
88 bt_ntoa(&key->bdaddr, NULL));
91 LIST_INSERT_HEAD(&link_keys, key, next);
107 bdaddr: T_BDADDR T_BDADDRSTRING
109 if (!bt_aton($2, &key->bdaddr)) {
110 syslog(LOG_ERR, "Cound not parse BD_ADDR " \
117 name: T_NAME T_STRING
119 if (key->name != NULL)
122 key->name = strdup($2);
123 if (key->name == NULL) {
124 syslog(LOG_ERR, "Could not allocate new " \
131 key: T_KEY T_HEXSTRING
135 if (key->key != NULL)
138 key->key = (uint8_t *) malloc(NG_HCI_KEY_SIZE);
139 if (key->key == NULL) {
140 syslog(LOG_ERR, "Could not allocate new " \
145 memset(key->key, 0, NG_HCI_KEY_SIZE);
147 len = strlen($2) / 2;
148 if (len > NG_HCI_KEY_SIZE)
149 len = NG_HCI_KEY_SIZE;
151 for (i = 0; i < len; i ++)
152 key->key[i] = hexa2int8((char *)($2) + 2*i);
156 if (key->key != NULL)
165 if (key->pin != NULL)
168 key->pin = strdup($2);
169 if (key->pin == NULL) {
170 syslog(LOG_ERR, "Could not allocate new " \
177 if (key->pin != NULL)
186 /* Display parser error message */
188 yyerror(char const *message)
190 syslog(LOG_ERR, "%s in line %d", message, yylineno);
193 /* Re-read config file */
195 read_config_file(void)
199 if (config_file == NULL) {
200 syslog(LOG_ERR, "Unknown config file name!");
204 if ((yyin = fopen(config_file, "r")) == NULL) {
205 syslog(LOG_ERR, "Could not open config file '%s'. %s (%d)",
206 config_file, strerror(errno), errno);
212 syslog(LOG_ERR, "Could not parse config file '%s'",config_file);
228 link_key_p key = NULL;
230 while ((key = LIST_FIRST(&link_keys)) != NULL) {
231 LIST_REMOVE(key, next);
236 /* Find link key entry in the list. Return exact or default match */
238 get_key(bdaddr_p bdaddr, int exact_match)
240 link_key_p key = NULL, defkey = NULL;
242 LIST_FOREACH(key, &link_keys, next) {
243 if (memcmp(bdaddr, &key->bdaddr, sizeof(key->bdaddr)) == 0)
247 if (memcmp(NG_HCI_BDADDR_ANY, &key->bdaddr,
248 sizeof(key->bdaddr)) == 0)
252 return ((key != NULL)? key : defkey);
260 link_key_p key = NULL;
263 LIST_FOREACH(key, &link_keys, next) {
264 if (key->key != NULL)
265 snprintf(buffer, sizeof(buffer),
266 "0x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
267 key->key[0], key->key[1], key->key[2],
268 key->key[3], key->key[4], key->key[5],
269 key->key[6], key->key[7], key->key[8],
270 key->key[9], key->key[10], key->key[11],
271 key->key[12], key->key[13], key->key[14],
279 (key->name != NULL)? key->name : "noname",
280 bt_ntoa(&key->bdaddr, NULL),
281 (key->pin != NULL)? key->pin : "nopin",
282 (key->key != NULL)? buffer : "nokey");
292 link_key_t *key = NULL;
293 char buf[HCSECD_BUFFER_SIZE], *p = NULL, *cp = NULL;
297 if ((f = fopen(HCSECD_KEYSFILE, "r")) == NULL) {
301 syslog(LOG_ERR, "Could not open keys file %s. %s (%d)\n",
302 HCSECD_KEYSFILE, strerror(errno), errno);
307 while ((p = fgets(buf, sizeof(buf), f)) != NULL) {
310 if ((cp = strpbrk(p, " ")) == NULL)
315 if (!bt_aton(p, &bdaddr))
318 if ((key = get_key(&bdaddr, 1)) == NULL)
321 if (key->key == NULL) {
322 key->key = (uint8_t *) malloc(NG_HCI_KEY_SIZE);
323 if (key->key == NULL) {
324 syslog(LOG_ERR, "Could not allocate link key");
329 memset(key->key, 0, NG_HCI_KEY_SIZE);
331 len = strlen(cp) / 2;
332 if (len > NG_HCI_KEY_SIZE)
333 len = NG_HCI_KEY_SIZE;
335 for (i = 0; i < len; i ++)
336 key->key[i] = hexa2int8(cp + 2*i);
338 syslog(LOG_DEBUG, "Restored link key for the entry, " \
339 "remote bdaddr %s, name '%s'",
340 bt_ntoa(&key->bdaddr, NULL),
341 (key->name != NULL)? key->name : "No name");
353 link_key_p key = NULL;
354 char tmp[PATH_MAX], buf[HCSECD_BUFFER_SIZE];
357 snprintf(tmp, sizeof(tmp), "%s.tmp", HCSECD_KEYSFILE);
358 if ((f = open(tmp, O_RDWR|O_CREAT|O_TRUNC|O_EXCL, 0600)) < 0) {
359 syslog(LOG_ERR, "Could not create temp keys file %s. %s (%d)\n",
360 tmp, strerror(errno), errno);
364 LIST_FOREACH(key, &link_keys, next) {
365 if (key->key == NULL)
368 snprintf(buf, sizeof(buf),
369 "%s %02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x\n",
370 bt_ntoa(&key->bdaddr, NULL),
371 key->key[0], key->key[1], key->key[2], key->key[3],
372 key->key[4], key->key[5], key->key[6], key->key[7],
373 key->key[8], key->key[9], key->key[10], key->key[11],
374 key->key[12], key->key[13], key->key[14], key->key[15]);
376 if (write(f, buf, strlen(buf)) < 0) {
377 syslog(LOG_ERR, "Could not write temp keys file. " \
378 "%s (%d)\n", strerror(errno), errno);
385 if (rename(tmp, HCSECD_KEYSFILE) < 0) {
386 syslog(LOG_ERR, "Could not rename(%s, %s). %s (%d)\n",
387 tmp, HCSECD_KEYSFILE, strerror(errno), errno);
397 free_key(link_key_p key)
399 if (key->name != NULL)
401 if (key->key != NULL)
403 if (key->pin != NULL)
406 memset(key, 0, sizeof(*key));
410 /* Convert hex ASCII to int4 */
414 if ('0' <= *a && *a <= '9')
417 if ('A' <= *a && *a <= 'F')
418 return (*a - 'A' + 0xa);
420 if ('a' <= *a && *a <= 'f')
421 return (*a - 'a' + 0xa);
423 syslog(LOG_ERR, "Invalid hex character: '%c' (%#x)", *a, *a);
427 /* Convert hex ASCII to int8 */
431 return ((hexa2int4(a) << 4) | hexa2int4(a + 1));