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