xref: /dragonfly/usr.sbin/bthcid/config.c (revision 86d7f5d3)
1*86d7f5d3SJohn Marino /* $NetBSD: config.c,v 1.4 2007/01/25 20:33:41 plunky Exp $ */
2*86d7f5d3SJohn Marino /* $DragonFly: src/usr.sbin/bthcid/config.c,v 1.1 2008/01/30 14:10:19 hasso Exp $ */
3*86d7f5d3SJohn Marino 
4*86d7f5d3SJohn Marino /*-
5*86d7f5d3SJohn Marino  * Copyright (c) 2006 Itronix Inc.
6*86d7f5d3SJohn Marino  * All rights reserved.
7*86d7f5d3SJohn Marino  *
8*86d7f5d3SJohn Marino  * Redistribution and use in source and binary forms, with or without
9*86d7f5d3SJohn Marino  * modification, are permitted provided that the following conditions
10*86d7f5d3SJohn Marino  * are met:
11*86d7f5d3SJohn Marino  * 1. Redistributions of source code must retain the above copyright
12*86d7f5d3SJohn Marino  *    notice, this list of conditions and the following disclaimer.
13*86d7f5d3SJohn Marino  * 2. Redistributions in binary form must reproduce the above copyright
14*86d7f5d3SJohn Marino  *    notice, this list of conditions and the following disclaimer in the
15*86d7f5d3SJohn Marino  *    documentation and/or other materials provided with the distribution.
16*86d7f5d3SJohn Marino  * 3. The name of Itronix Inc. may not be used to endorse
17*86d7f5d3SJohn Marino  *    or promote products derived from this software without specific
18*86d7f5d3SJohn Marino  *    prior written permission.
19*86d7f5d3SJohn Marino  *
20*86d7f5d3SJohn Marino  * THIS SOFTWARE IS PROVIDED BY ITRONIX INC. ``AS IS'' AND
21*86d7f5d3SJohn Marino  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22*86d7f5d3SJohn Marino  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23*86d7f5d3SJohn Marino  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL ITRONIX INC. BE LIABLE FOR ANY
24*86d7f5d3SJohn Marino  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
25*86d7f5d3SJohn Marino  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26*86d7f5d3SJohn Marino  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
27*86d7f5d3SJohn Marino  * ON ANY THEORY OF LIABILITY, WHETHER IN
28*86d7f5d3SJohn Marino  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29*86d7f5d3SJohn Marino  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30*86d7f5d3SJohn Marino  * POSSIBILITY OF SUCH DAMAGE.
31*86d7f5d3SJohn Marino  */
32*86d7f5d3SJohn Marino 
33*86d7f5d3SJohn Marino #include <sys/time.h>
34*86d7f5d3SJohn Marino #include <bluetooth.h>
35*86d7f5d3SJohn Marino #include <errno.h>
36*86d7f5d3SJohn Marino #include <fcntl.h>
37*86d7f5d3SJohn Marino #include <stdlib.h>
38*86d7f5d3SJohn Marino #include <string.h>
39*86d7f5d3SJohn Marino #include <syslog.h>
40*86d7f5d3SJohn Marino #include <unistd.h>
41*86d7f5d3SJohn Marino 
42*86d7f5d3SJohn Marino #include "bthcid.h"
43*86d7f5d3SJohn Marino 
44*86d7f5d3SJohn Marino #if 0
45*86d7f5d3SJohn Marino static const char *key_file = "/var/db/bthcid.keys";
46*86d7f5d3SJohn Marino static const char *new_key_file = "/var/db/bthcid.keys.new";
47*86d7f5d3SJohn Marino #endif
48*86d7f5d3SJohn Marino 
49*86d7f5d3SJohn Marino /*
50*86d7f5d3SJohn Marino  * Look up key in keys file. We store a dictionary for each
51*86d7f5d3SJohn Marino  * remote address, and inside that we have a data object for
52*86d7f5d3SJohn Marino  * each local address containing the key.
53*86d7f5d3SJohn Marino  */
54*86d7f5d3SJohn Marino uint8_t *
lookup_key(bdaddr_t * laddr,bdaddr_t * raddr)55*86d7f5d3SJohn Marino lookup_key(bdaddr_t *laddr, bdaddr_t *raddr)
56*86d7f5d3SJohn Marino {
57*86d7f5d3SJohn Marino 	link_key_p	key = NULL;
58*86d7f5d3SJohn Marino 
59*86d7f5d3SJohn Marino 	syslog(LOG_DEBUG, "Got Link_Key_Request event from '%s', " \
60*86d7f5d3SJohn Marino 			"remote bdaddr %s", bt_ntoa(laddr, NULL),
61*86d7f5d3SJohn Marino 			bt_ntoa(raddr, NULL));
62*86d7f5d3SJohn Marino 
63*86d7f5d3SJohn Marino 	if ((key = get_key(raddr, 0)) != NULL) {
64*86d7f5d3SJohn Marino 		syslog(LOG_DEBUG, "Found matching entry, " \
65*86d7f5d3SJohn Marino 				"remote bdaddr %s, name '%s', link key %s",
66*86d7f5d3SJohn Marino 				bt_ntoa(&key->bdaddr, NULL),
67*86d7f5d3SJohn Marino 				(key->name != NULL)? key->name : "No name",
68*86d7f5d3SJohn Marino 				(key->key != NULL)? "exists" : "doesn't exist");
69*86d7f5d3SJohn Marino 		return key->key;
70*86d7f5d3SJohn Marino 	}
71*86d7f5d3SJohn Marino 
72*86d7f5d3SJohn Marino 	syslog(LOG_DEBUG, "Could not find link key for remote bdaddr %s",
73*86d7f5d3SJohn Marino 			bt_ntoa(raddr, NULL));
74*86d7f5d3SJohn Marino 	return NULL;
75*86d7f5d3SJohn Marino }
76*86d7f5d3SJohn Marino 
77*86d7f5d3SJohn Marino /*
78*86d7f5d3SJohn Marino  * Look up pin in keys file. We store a dictionary for each
79*86d7f5d3SJohn Marino  * remote address, and inside that we have a data object for
80*86d7f5d3SJohn Marino  * each local address containing the pin.
81*86d7f5d3SJohn Marino  */
82*86d7f5d3SJohn Marino uint8_t *
lookup_pin_conf(bdaddr_t * laddr,bdaddr_t * raddr)83*86d7f5d3SJohn Marino lookup_pin_conf(bdaddr_t *laddr, bdaddr_t *raddr)
84*86d7f5d3SJohn Marino {
85*86d7f5d3SJohn Marino 	link_key_p	key = NULL;
86*86d7f5d3SJohn Marino 
87*86d7f5d3SJohn Marino 	syslog(LOG_DEBUG, "Got Link_Pin_Request event from '%s', " \
88*86d7f5d3SJohn Marino 			"remote bdaddr %s", bt_ntoa(laddr, NULL),
89*86d7f5d3SJohn Marino 			bt_ntoa(raddr, NULL));
90*86d7f5d3SJohn Marino 
91*86d7f5d3SJohn Marino 	if ((key = get_key(raddr, 0)) != NULL) {
92*86d7f5d3SJohn Marino 		syslog(LOG_DEBUG, "Found matching entry, " \
93*86d7f5d3SJohn Marino 				"remote bdaddr %s, name '%s', pin %s",
94*86d7f5d3SJohn Marino 				bt_ntoa(&key->bdaddr, NULL),
95*86d7f5d3SJohn Marino 				(key->name != NULL)? key->name : "No name",
96*86d7f5d3SJohn Marino 				(key->pin != NULL)? "exists" : "doesn't exist");
97*86d7f5d3SJohn Marino 		return key->pin;
98*86d7f5d3SJohn Marino 	}
99*86d7f5d3SJohn Marino 
100*86d7f5d3SJohn Marino 	syslog(LOG_DEBUG, "Could not find link key for remote bdaddr %s",
101*86d7f5d3SJohn Marino 			bt_ntoa(raddr, NULL));
102*86d7f5d3SJohn Marino 	return NULL;
103*86d7f5d3SJohn Marino }
104*86d7f5d3SJohn Marino 
105*86d7f5d3SJohn Marino 
106*86d7f5d3SJohn Marino void
save_key(bdaddr_t * laddr,bdaddr_t * raddr,uint8_t * key)107*86d7f5d3SJohn Marino save_key(bdaddr_t *laddr, bdaddr_t *raddr, uint8_t * key)
108*86d7f5d3SJohn Marino {
109*86d7f5d3SJohn Marino 	link_key_p	lkey = NULL;
110*86d7f5d3SJohn Marino 
111*86d7f5d3SJohn Marino 	syslog(LOG_DEBUG, "Got Link_Key_Notification event from '%s', " \
112*86d7f5d3SJohn Marino 			"remote bdaddr %s", bt_ntoa(laddr, NULL),
113*86d7f5d3SJohn Marino 			bt_ntoa(raddr, NULL));
114*86d7f5d3SJohn Marino 
115*86d7f5d3SJohn Marino 	if ((lkey = get_key(raddr, 1)) == NULL) {
116*86d7f5d3SJohn Marino 		syslog(LOG_ERR, "Could not find entry for remote bdaddr %s",
117*86d7f5d3SJohn Marino 				bt_ntoa(raddr, NULL));
118*86d7f5d3SJohn Marino 		return;
119*86d7f5d3SJohn Marino 	}
120*86d7f5d3SJohn Marino 
121*86d7f5d3SJohn Marino 	syslog(LOG_DEBUG, "Updating link key for the entry, " \
122*86d7f5d3SJohn Marino 			"remote bdaddr %s, name '%s', link key %s",
123*86d7f5d3SJohn Marino 			bt_ntoa(&lkey->bdaddr, NULL),
124*86d7f5d3SJohn Marino 			(lkey->name != NULL)? lkey->name : "No name",
125*86d7f5d3SJohn Marino 			(lkey->key != NULL)? "exists" : "doesn't exist");
126*86d7f5d3SJohn Marino 
127*86d7f5d3SJohn Marino 	if (lkey->key == NULL) {
128*86d7f5d3SJohn Marino 		lkey->key = (uint8_t *) malloc(HCI_KEY_SIZE);
129*86d7f5d3SJohn Marino 		if (lkey->key == NULL) {
130*86d7f5d3SJohn Marino 			syslog(LOG_ERR, "Could not allocate link key");
131*86d7f5d3SJohn Marino 			exit(1);
132*86d7f5d3SJohn Marino 		}
133*86d7f5d3SJohn Marino 	}
134*86d7f5d3SJohn Marino 
135*86d7f5d3SJohn Marino 	memcpy(lkey->key, key, HCI_KEY_SIZE);
136*86d7f5d3SJohn Marino 
137*86d7f5d3SJohn Marino 	dump_keys_file();
138*86d7f5d3SJohn Marino 	read_config_file();
139*86d7f5d3SJohn Marino 	read_keys_file();
140*86d7f5d3SJohn Marino 
141*86d7f5d3SJohn Marino 	return;
142*86d7f5d3SJohn Marino }
143*86d7f5d3SJohn Marino 
144