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