xref: /dragonfly/usr.sbin/bthcid/parser.y (revision 6a3cbbc2)
1 %{
2 /*
3  * parser.y
4  *
5  * Copyright (c) 2001-2002 Maksim Yevmenkin <m_evmenkin@yahoo.com>
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
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.
16  *
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
27  * SUCH DAMAGE.
28  *
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  */
32 
33 #include <sys/fcntl.h>
34 #include <sys/queue.h>
35 #include <bluetooth.h>
36 #include <errno.h>
37 #include <limits.h>
38 #include <stdio.h>
39 #include <stdarg.h>
40 #include <stdlib.h>
41 #include <string.h>
42 #include <syslog.h>
43 #include <unistd.h>
44 #include "bthcid.h"
45 
46 	int	yylex    (void);
47 
48 static	void	free_key (link_key_p key);
49 static	int	hexa2int4(char *a);
50 static	int	hexa2int8(char *a);
51 
52 extern	void			 yyerror(const char *);
53 extern	int			 yylineno;
54 extern	FILE			*yyin;
55 
56 static	LIST_HEAD(, link_key)	 link_keys;
57 
58 const	char			*config_file = "/etc/bluetooth/bthcid.conf";
59 static	link_key_p		 key = NULL;
60 %}
61 
62 %union {
63 	char	*string;
64 }
65 
66 %token <string> T_BDADDRSTRING T_HEXSTRING T_STRING
67 %token T_DEVICE T_BDADDR T_NAME T_KEY T_PIN T_NOKEY T_NOPIN T_JUNK
68 
69 %%
70 
71 config:		line
72 		| config line
73 		;
74 
75 line:		T_DEVICE
76 			{
77 			key = (link_key_p) malloc(sizeof(*key));
78 			if (key == NULL) {
79 				syslog(LOG_ERR, "Could not allocate new " \
80 						"config entry");
81 				exit(1);
82 			}
83 
84 			memset(key, 0, sizeof(*key));
85 			}
86 		'{' options '}'
87 			{
88 			if (get_key(&key->bdaddr, 1) != NULL) {
89 				syslog(LOG_ERR, "Ignoring duplicated entry " \
90 						"for bdaddr %s",
91 						bt_ntoa(&key->bdaddr, NULL));
92 				free_key(key);
93 			} else
94 				LIST_INSERT_HEAD(&link_keys, key, next);
95 
96 			key = NULL;
97 			}
98 		;
99 
100 options:	option ';'
101 		| options option ';'
102 		;
103 
104 option:		bdaddr
105 		| name
106 		| key
107 		| pin
108 		;
109 
110 bdaddr:		T_BDADDR T_BDADDRSTRING
111 			{
112 			if (!bt_aton($2, &key->bdaddr)) {
113 				syslog(LOG_ERR, "Could not parse BD_ADDR " \
114 						"'%s'", $2);
115 				exit(1);
116 			}
117 			}
118 		;
119 
120 name:		T_NAME T_STRING
121 			{
122 			if (key->name != NULL)
123 				free(key->name);
124 
125 			key->name = strdup($2);
126 			if (key->name == NULL) {
127 				syslog(LOG_ERR, "Could not allocate new " \
128 						"device name");
129 				exit(1);
130 			}
131 			}
132 		;
133 
134 key:		T_KEY T_HEXSTRING
135 			{
136 			int	i, len;
137 
138 			if (key->key != NULL)
139 				free(key->key);
140 
141 			key->key = (uint8_t *) malloc(HCI_KEY_SIZE);
142 			if (key->key == NULL) {
143 				syslog(LOG_ERR, "Could not allocate new " \
144 						"link key");
145 				exit(1);
146 			}
147 
148 			memset(key->key, 0, HCI_KEY_SIZE);
149 
150 			len = strlen($2) / 2;
151 			if (len > HCI_KEY_SIZE)
152 				len = HCI_KEY_SIZE;
153 
154 			for (i = 0; i < len; i ++)
155 				key->key[i] = hexa2int8((char *)($2) + 2*i);
156 			}
157 		| T_KEY T_NOKEY
158 			{
159 			if (key->key != NULL)
160 				free(key->key);
161 
162 			key->key = NULL;
163 			}
164 		;
165 
166 pin:		T_PIN T_STRING
167 			{
168 			if (key->pin != NULL)
169 				free(key->pin);
170 
171 			key->pin = strdup($2);
172 			if (key->pin == NULL) {
173 				syslog(LOG_ERR, "Could not allocate new " \
174 						"PIN code");
175 				exit(1);
176 			}
177 			}
178 		| T_PIN T_NOPIN
179 			{
180 			if (key->pin != NULL)
181 				free(key->pin);
182 
183 			key->pin = NULL;
184 			}
185 		;
186 
187 %%
188 
189 /* Display parser error message */
190 void
191 yyerror(char const *message)
192 {
193 	syslog(LOG_ERR, "%s in line %d", message, yylineno);
194 }
195 
196 /* Re-read config file */
197 void
198 read_config_file(void)
199 {
200 	if (config_file == NULL) {
201 		syslog(LOG_ERR, "Unknown config file name!");
202 		exit(1);
203 	}
204 
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);
208 		exit(1);
209 	}
210 
211 	clean_config();
212 	if (yyparse() < 0) {
213 		syslog(LOG_ERR, "Could not parse config file '%s'",config_file);
214 		exit(1);
215 	}
216 
217 	fclose(yyin);
218 	yyin = NULL;
219 
220 #ifdef __config_debug__
221 	dump_config();
222 #endif
223 }
224 
225 /* Clean config */
226 void
227 clean_config(void)
228 {
229 	link_key_p	lkey = NULL;
230 
231 	while ((lkey = LIST_FIRST(&link_keys)) != NULL) {
232 		LIST_REMOVE(lkey, next);
233 		free_key(lkey);
234 	}
235 }
236 
237 /* Find link key entry in the list. Return exact or default match */
238 link_key_p
239 get_key(bdaddr_p bdaddr, int exact_match)
240 {
241 	link_key_p	lkey = NULL, defkey = NULL;
242 
243 	LIST_FOREACH(lkey, &link_keys, next) {
244 		if (memcmp(bdaddr, &lkey->bdaddr, sizeof(lkey->bdaddr)) == 0)
245 			break;
246 
247 		if (!exact_match)
248 			if (memcmp(BDADDR_ANY, &lkey->bdaddr,
249 					sizeof(lkey->bdaddr)) == 0)
250 				defkey = lkey;
251 	}
252 
253 	return ((lkey != NULL)? lkey : defkey);
254 }
255 
256 #ifdef __config_debug__
257 /* Dump config */
258 void
259 dump_config(void)
260 {
261 	link_key_p	lkey = NULL;
262 	char		buffer[64];
263 
264 	LIST_FOREACH(lkey, &link_keys, next) {
265 		if (lkey->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 				lkey->key[0], lkey->key[1], lkey->key[2],
269 				lkey->key[3], lkey->key[4], lkey->key[5],
270 				lkey->key[6], lkey->key[7], lkey->key[8],
271 				lkey->key[9], lkey->key[10], lkey->key[11],
272 				lkey->key[12], lkey->key[13], lkey->key[14],
273 				lkey->key[15]);
274 
275 		syslog(LOG_DEBUG,
276 "device %s " \
277 "bdaddr %s " \
278 "pin %s " \
279 "key %s",
280 			(lkey->name != NULL)? lkey->name : "noname",
281 			bt_ntoa(&lkey->bdaddr, NULL),
282 			(lkey->pin != NULL)? lkey->pin : "nopin",
283 			(lkey->key != NULL)? buffer : "nokey");
284 	}
285 }
286 #endif
287 
288 /* Read keys file */
289 int
290 read_keys_file(void)
291 {
292 	FILE		*f = NULL;
293 	link_key_t	*lkey = NULL;
294 	char		 buf[BTHCID_BUFFER_SIZE], *p = NULL, *cp = NULL;
295 	bdaddr_t	 bdaddr;
296 	int		 i, len;
297 
298 	if ((f = fopen(BTHCID_KEYSFILE, "r")) == NULL) {
299 		if (errno == ENOENT)
300 			return (0);
301 
302 		syslog(LOG_ERR, "Could not open keys file %s. %s (%d)\n",
303 				BTHCID_KEYSFILE, strerror(errno), errno);
304 
305 		return (-1);
306 	}
307 
308 	while ((p = fgets(buf, sizeof(buf), f)) != NULL) {
309 		if (*p == '#')
310 			continue;
311 		if ((cp = strpbrk(p, " ")) == NULL)
312 			continue;
313 
314 		*cp++ = '\0';
315 
316 		if (!bt_aton(p, &bdaddr))
317 			continue;
318 
319 		if ((lkey = get_key(&bdaddr, 1)) == NULL)
320 			continue;
321 
322 		if (lkey->key == NULL) {
323 			lkey->key = (uint8_t *) malloc(HCI_KEY_SIZE);
324 			if (lkey->key == NULL) {
325 				syslog(LOG_ERR, "Could not allocate link key");
326 				exit(1);
327 			}
328 		}
329 
330 		memset(lkey->key, 0, HCI_KEY_SIZE);
331 
332 		len = strlen(cp) / 2;
333 		if (len > HCI_KEY_SIZE)
334 			len = HCI_KEY_SIZE;
335 
336 		for (i = 0; i < len; i ++)
337 			lkey->key[i] = hexa2int8(cp + 2*i);
338 
339 		syslog(LOG_DEBUG, "Restored link key for the entry, " \
340 				"remote bdaddr %s, name '%s'",
341 				bt_ntoa(&lkey->bdaddr, NULL),
342 				(lkey->name != NULL)? lkey->name : "No name");
343 	}
344 
345 	fclose(f);
346 
347 	return (0);
348 }
349 
350 /* Dump keys file */
351 int
352 dump_keys_file(void)
353 {
354 	link_key_p	lkey = NULL;
355 	char		tmp[PATH_MAX], buf[BTHCID_BUFFER_SIZE];
356 	int		f;
357 
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);
362 		return (-1);
363 	}
364 
365 	LIST_FOREACH(lkey, &link_keys, next) {
366 		if (lkey->key == NULL)
367 			continue;
368 
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(&lkey->bdaddr, NULL),
372 			lkey->key[0],  lkey->key[1],  lkey->key[2],  lkey->key[3],
373 			lkey->key[4],  lkey->key[5],  lkey->key[6],  lkey->key[7],
374 			lkey->key[8],  lkey->key[9],  lkey->key[10], lkey->key[11],
375 			lkey->key[12], lkey->key[13], lkey->key[14], lkey->key[15]);
376 
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);
380 			break;
381 		}
382 	}
383 
384 	close(f);
385 
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);
389 		unlink(tmp);
390 		return (-1);
391 	}
392 
393 	return (0);
394 }
395 
396 /* Free key entry */
397 static void
398 free_key(link_key_p lkey)
399 {
400 	if (lkey->name != NULL)
401 		free(lkey->name);
402 	if (lkey->key != NULL)
403 		free(lkey->key);
404 	if (lkey->pin != NULL)
405 		free(lkey->pin);
406 
407 	memset(lkey, 0, sizeof(*lkey));
408 	free(lkey);
409 }
410 
411 /* Convert hex ASCII to int4 */
412 static int
413 hexa2int4(char *a)
414 {
415 	if ('0' <= *a && *a <= '9')
416 		return (*a - '0');
417 
418 	if ('A' <= *a && *a <= 'F')
419 		return (*a - 'A' + 0xa);
420 
421 	if ('a' <= *a && *a <= 'f')
422 		return (*a - 'a' + 0xa);
423 
424 	syslog(LOG_ERR, "Invalid hex character: '%c' (%#x)", *a, *a);
425 	exit(1);
426 }
427 
428 /* Convert hex ASCII to int8 */
429 static int
430 hexa2int8(char *a)
431 {
432 	return ((hexa2int4(a) << 4) | hexa2int4(a + 1));
433 }
434 
435