1/*	$NetBSD: file.c$	*/
2
3/*-
4 * Copyright (c) 2007 Iain Hibbert
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
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 the author may not be used to endorse or promote products
16 *    derived from this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
30#include <sys/cdefs.h>
31__RCSID("$NetBSD: file.c$");
32
33#include <sys/stat.h>
34#include <prop/proplib.h>
35
36#include <bluetooth.h>
37#include <stdbool.h>
38#include <string.h>
39
40#include "btkey.h"
41
42static const char *key_file = "/var/db/bthcid.keys";
43
44/*
45 * List keys file.
46 */
47bool
48list_file(void)
49{
50	prop_dictionary_t db, dev;
51	prop_object_iterator_t iter;
52	prop_dictionary_keysym_t sym;
53	prop_object_t dat;
54	bdaddr_t bdaddr;
55	bool rv = false;
56
57	db = prop_dictionary_internalize_from_file(key_file);
58	if (db == NULL)
59		return false;
60
61	dev = prop_dictionary_get(db, bt_ntoa(&laddr, NULL));
62	if (prop_object_type(dev) != PROP_TYPE_DICTIONARY)
63		goto done;
64
65	iter = prop_dictionary_iterator(dev);
66	if (iter == NULL)
67		goto done;
68
69	while ((sym = prop_object_iterator_next(iter)) != NULL) {
70		memset(&bdaddr, 0, sizeof(bdaddr));
71		bt_aton(prop_dictionary_keysym_cstring_nocopy(sym), &bdaddr);
72		if (bdaddr_any(&bdaddr))
73			continue;
74
75		dat = prop_dictionary_get_keysym(dev, sym);
76		if (prop_data_size(dat) != HCI_KEY_SIZE)
77			continue;
78
79		printf("\n");
80		print_addr("bdaddr", &bdaddr);
81		print_key("file key", prop_data_data_nocopy(dat));
82	}
83
84	prop_object_iterator_release(iter);
85	rv = true;
86
87done:
88	prop_object_release(db);
89	return rv;
90}
91
92/*
93 * Read from keys file.
94 */
95bool
96read_file(void)
97{
98	prop_dictionary_t db, dev;
99	prop_object_t dat;
100	bool rv = false;
101
102	db = prop_dictionary_internalize_from_file(key_file);
103	if (db == NULL)
104		return false;
105
106	dev = prop_dictionary_get(db, bt_ntoa(&laddr, NULL));
107	if (prop_object_type(dev) != PROP_TYPE_DICTIONARY)
108		goto done;
109
110	dat = prop_dictionary_get(dev, bt_ntoa(&raddr, NULL));
111	if (prop_data_size(dat) != HCI_KEY_SIZE)
112		goto done;
113
114	memcpy(key, prop_data_data_nocopy(dat), HCI_KEY_SIZE);
115	rv = true;
116
117done:
118	prop_object_release(db);
119	return rv;
120}
121
122/*
123 * Write to keys file.
124 */
125bool
126write_file(void)
127{
128	prop_dictionary_t db, dev;
129	prop_data_t dat;
130	mode_t mode;
131	bool rv = false;
132
133	db = prop_dictionary_internalize_from_file(key_file);
134	if (db == NULL) {
135		db = prop_dictionary_create();
136		if (db == NULL)
137			return false;
138	}
139
140	dev = prop_dictionary_get(db, bt_ntoa(&laddr, NULL));
141	if (dev == NULL) {
142		dev = prop_dictionary_create();
143		if (dev == NULL)
144			goto done;
145
146		rv = prop_dictionary_set(db, bt_ntoa(&laddr, NULL), dev);
147		prop_object_release(dev);
148		if (rv == false)
149			goto done;
150	}
151
152	dat = prop_data_create_data_nocopy(key, HCI_KEY_SIZE);
153	if (dat == NULL)
154		goto done;
155
156	rv = prop_dictionary_set(dev, bt_ntoa(&raddr, NULL), dat);
157	prop_object_release(dat);
158	if (rv == false)
159		goto done;
160
161	mode = umask(S_IRWXG | S_IRWXO);
162	rv = prop_dictionary_externalize_to_file(db, key_file);
163	umask(mode);
164
165done:
166	prop_object_release(db);
167	return rv;
168}
169
170/*
171 * Clear from keys file.
172 */
173bool
174clear_file(void)
175{
176	prop_dictionary_t db, dev;
177	prop_data_t dat;
178	mode_t mode;
179	bool rv = false;
180
181	db = prop_dictionary_internalize_from_file(key_file);
182	if (db == NULL)
183		return false;
184
185	dev = prop_dictionary_get(db, bt_ntoa(&laddr, NULL));
186	if (dev == NULL)
187		goto done;
188
189	dat = prop_dictionary_get(dev, bt_ntoa(&raddr, NULL));
190	if (prop_data_size(dat) != HCI_KEY_SIZE)
191		goto done;
192
193	prop_dictionary_remove(dev, bt_ntoa(&raddr, NULL));
194
195	mode = umask(S_IRWXG | S_IRWXO);
196	rv = prop_dictionary_externalize_to_file(db, key_file);
197	umask(mode);
198
199done:
200	prop_object_release(db);
201	return rv;
202}
203