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 $ */
5 * Copyright (c) 2006 Itronix Inc.
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.
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.
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.
34 #include <bluetooth.h>
46 static const char *key_file
= "/var/db/bthcid.keys";
47 static const char *new_key_file
= "/var/db/bthcid.keys.new";
51 * Look up key in keys file. We store a dictionary for each
52 * remote address, and inside that we have a data object for
53 * each local address containing the key.
56 lookup_key(bdaddr_t
*laddr
, bdaddr_t
*raddr
)
58 link_key_p key
= NULL
;
60 syslog(LOG_DEBUG
, "Got Link_Key_Request event from '%s', " \
61 "remote bdaddr %s", bt_ntoa(laddr
, NULL
),
62 bt_ntoa(raddr
, NULL
));
64 if ((key
= get_key(raddr
, 0)) != NULL
) {
65 syslog(LOG_DEBUG
, "Found matching entry, " \
66 "remote bdaddr %s, name '%s', link key %s",
67 bt_ntoa(&key
->bdaddr
, NULL
),
68 (key
->name
!= NULL
)? key
->name
: "No name",
69 (key
->key
!= NULL
)? "exists" : "doesn't exist");
73 syslog(LOG_DEBUG
, "Could not find link key for remote bdaddr %s",
74 bt_ntoa(raddr
, NULL
));
79 * Look up pin in keys file. We store a dictionary for each
80 * remote address, and inside that we have a data object for
81 * each local address containing the pin.
84 lookup_pin_conf(bdaddr_t
*laddr
, bdaddr_t
*raddr
)
86 link_key_p key
= NULL
;
88 syslog(LOG_DEBUG
, "Got Link_Pin_Request event from '%s', " \
89 "remote bdaddr %s", bt_ntoa(laddr
, NULL
),
90 bt_ntoa(raddr
, NULL
));
92 if ((key
= get_key(raddr
, 0)) != NULL
) {
93 syslog(LOG_DEBUG
, "Found matching entry, " \
94 "remote bdaddr %s, name '%s', pin %s",
95 bt_ntoa(&key
->bdaddr
, NULL
),
96 (key
->name
!= NULL
)? key
->name
: "No name",
97 (key
->pin
!= NULL
)? "exists" : "doesn't exist");
101 syslog(LOG_DEBUG
, "Could not find link key for remote bdaddr %s",
102 bt_ntoa(raddr
, NULL
));
108 save_key(bdaddr_t
*laddr
, bdaddr_t
*raddr
, uint8_t * key
)
110 link_key_p lkey
= NULL
;
112 syslog(LOG_DEBUG
, "Got Link_Key_Notification event from '%s', " \
113 "remote bdaddr %s", bt_ntoa(laddr
, NULL
),
114 bt_ntoa(raddr
, NULL
));
116 if ((lkey
= get_key(raddr
, 1)) == NULL
) {
117 syslog(LOG_ERR
, "Could not find entry for remote bdaddr %s",
118 bt_ntoa(raddr
, NULL
));
122 syslog(LOG_DEBUG
, "Updating link key for the entry, " \
123 "remote bdaddr %s, name '%s', link key %s",
124 bt_ntoa(&lkey
->bdaddr
, NULL
),
125 (lkey
->name
!= NULL
)? lkey
->name
: "No name",
126 (lkey
->key
!= NULL
)? "exists" : "doesn't exist");
128 if (lkey
->key
== NULL
) {
129 lkey
->key
= (uint8_t *) malloc(HCI_KEY_SIZE
);
130 if (lkey
->key
== NULL
) {
131 syslog(LOG_ERR
, "Could not allocate link key");
136 memcpy(lkey
->key
, key
, HCI_KEY_SIZE
);