1 /* $NetBSD: config.c,v 1.4 2007/01/25 20:33:41 plunky Exp $ */
4 * Copyright (c) 2006 Itronix Inc.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. The name of Itronix Inc. may not be used to endorse
16 * or promote products derived from this software without specific
17 * prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY ITRONIX INC. ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL ITRONIX INC. BE LIABLE FOR ANY
23 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
26 * ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
32 #include <sys/cdefs.h>
33 __RCSID("$NetBSD: config.c,v 1.4 2007/01/25 20:33:41 plunky Exp $");
36 #include <prop/proplib.h>
37 #include <bluetooth.h>
48 static const char *key_file
= "/var/db/bthcid.keys";
49 static const char *new_key_file
= "/var/db/bthcid.keys.new";
51 static prop_dictionary_t
54 prop_dictionary_t dict
;
59 fd
= open(key_file
, O_RDONLY
, 0);
63 len
= lseek(fd
, 0, SEEK_END
);
75 (void)lseek(fd
, 0, SEEK_SET
);
76 if (read(fd
, xml
, len
) != len
) {
82 dict
= prop_dictionary_internalize(xml
);
89 * Look up key in keys file. We store a dictionary for each
90 * remote address, and inside that we have a data object for
91 * each local address containing the key.
94 lookup_key(bdaddr_t
*laddr
, bdaddr_t
*raddr
)
96 static uint8_t key
[HCI_KEY_SIZE
];
97 prop_dictionary_t cfg
;
104 obj
= prop_dictionary_get(cfg
, bt_ntoa(laddr
, NULL
));
105 if (obj
== NULL
|| prop_object_type(obj
) != PROP_TYPE_DICTIONARY
) {
106 prop_object_release(cfg
);
110 obj
= prop_dictionary_get(obj
, bt_ntoa(raddr
, NULL
));
111 if (obj
== NULL
|| prop_object_type(obj
) != PROP_TYPE_DATA
112 || prop_data_size(obj
) != sizeof(key
)) {
113 prop_object_release(cfg
);
117 memcpy(key
, prop_data_data_nocopy(obj
), sizeof(key
));
118 prop_object_release(cfg
);
123 save_key(bdaddr_t
*laddr
, bdaddr_t
*raddr
, uint8_t *key
)
125 prop_dictionary_t cfg
, dev
;
133 cfg
= prop_dictionary_create();
135 syslog(LOG_ERR
, "prop_dictionary_create() failed: %m");
140 dev
= prop_dictionary_get(cfg
, bt_ntoa(laddr
, NULL
));
142 dev
= prop_dictionary_create();
144 syslog(LOG_ERR
, "prop_dictionary_create() failed: %m");
145 prop_object_release(cfg
);
149 if (!prop_dictionary_set(cfg
, bt_ntoa(laddr
, NULL
), dev
)) {
150 syslog(LOG_ERR
, "prop_dictionary_set() failed: %m");
151 prop_object_release(dev
);
152 prop_object_release(cfg
);
156 prop_object_release(dev
);
159 dat
= prop_data_create_data_nocopy(key
, HCI_KEY_SIZE
);
161 syslog(LOG_ERR
, "Cannot create data object: %m");
162 prop_object_release(cfg
);
166 if (!prop_dictionary_set(dev
, bt_ntoa(raddr
, NULL
), dat
)) {
167 syslog(LOG_ERR
, "prop_dictionary_set() failed: %m");
168 prop_object_release(dat
);
169 prop_object_release(cfg
);
173 prop_object_release(dat
);
175 xml
= prop_dictionary_externalize(cfg
);
177 syslog(LOG_ERR
, "prop_dictionary_externalize() failed: %m");
178 prop_object_release(cfg
);
182 prop_object_release(cfg
);
184 fd
= open(new_key_file
, O_WRONLY
|O_TRUNC
|O_CREAT
|O_EXLOCK
, 0600);
186 syslog(LOG_ERR
, "Cannot open new keyfile %s: %m", key_file
);
192 if ((size_t)write(fd
, xml
, len
) != len
) {
193 syslog(LOG_ERR
, "Write of keyfile failed: %m");
196 unlink(new_key_file
);
203 if (rename(new_key_file
, key_file
) < 0) {
204 syslog(LOG_ERR
, "rename(%s, %s) failed: %m",
205 new_key_file
, key_file
);
207 unlink(new_key_file
);