4 * Copyright (c) 2007 Iain Hibbert
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 the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission.
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.
30 #include <sys/cdefs.h>
31 __RCSID("$NetBSD: file.c$");
34 #include <prop/proplib.h>
36 #include <bluetooth.h>
42 static const char *key_file
= "/var/db/bthcid.keys";
50 prop_dictionary_t db
, dev
;
51 prop_object_iterator_t iter
;
52 prop_dictionary_keysym_t sym
;
57 db
= prop_dictionary_internalize_from_file(key_file
);
61 dev
= prop_dictionary_get(db
, bt_ntoa(&laddr
, NULL
));
62 if (prop_object_type(dev
) != PROP_TYPE_DICTIONARY
)
65 iter
= prop_dictionary_iterator(dev
);
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
))
75 dat
= prop_dictionary_get_keysym(dev
, sym
);
76 if (prop_data_size(dat
) != HCI_KEY_SIZE
)
80 print_addr("bdaddr", &bdaddr
);
81 print_key("file key", prop_data_data_nocopy(dat
));
84 prop_object_iterator_release(iter
);
88 prop_object_release(db
);
93 * Read from keys file.
98 prop_dictionary_t db
, dev
;
102 db
= prop_dictionary_internalize_from_file(key_file
);
106 dev
= prop_dictionary_get(db
, bt_ntoa(&laddr
, NULL
));
107 if (prop_object_type(dev
) != PROP_TYPE_DICTIONARY
)
110 dat
= prop_dictionary_get(dev
, bt_ntoa(&raddr
, NULL
));
111 if (prop_data_size(dat
) != HCI_KEY_SIZE
)
114 memcpy(key
, prop_data_data_nocopy(dat
), HCI_KEY_SIZE
);
118 prop_object_release(db
);
123 * Write to keys file.
128 prop_dictionary_t db
, dev
;
133 db
= prop_dictionary_internalize_from_file(key_file
);
135 db
= prop_dictionary_create();
140 dev
= prop_dictionary_get(db
, bt_ntoa(&laddr
, NULL
));
142 dev
= prop_dictionary_create();
146 rv
= prop_dictionary_set(db
, bt_ntoa(&laddr
, NULL
), dev
);
147 prop_object_release(dev
);
152 dat
= prop_data_create_data_nocopy(key
, HCI_KEY_SIZE
);
156 rv
= prop_dictionary_set(dev
, bt_ntoa(&raddr
, NULL
), dat
);
157 prop_object_release(dat
);
161 mode
= umask(S_IRWXG
| S_IRWXO
);
162 rv
= prop_dictionary_externalize_to_file(db
, key_file
);
166 prop_object_release(db
);
171 * Clear from keys file.
176 prop_dictionary_t db
, dev
;
181 db
= prop_dictionary_internalize_from_file(key_file
);
185 dev
= prop_dictionary_get(db
, bt_ntoa(&laddr
, NULL
));
189 dat
= prop_dictionary_get(dev
, bt_ntoa(&raddr
, NULL
));
190 if (prop_data_size(dat
) != HCI_KEY_SIZE
)
193 prop_dictionary_remove(dev
, bt_ntoa(&raddr
, NULL
));
195 mode
= umask(S_IRWXG
| S_IRWXO
);
196 rv
= prop_dictionary_externalize_to_file(db
, key_file
);
200 prop_object_release(db
);