No empty .Rs/.Re
[netbsd-mini2440.git] / usr.bin / btkey / file.c
blobeb1fe99bad9b7541e074142c25bc007fae1ef491
1 /* $NetBSD: file.c$ */
3 /*-
4 * Copyright (c) 2007 Iain Hibbert
5 * All rights reserved.
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.
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$");
33 #include <sys/stat.h>
34 #include <prop/proplib.h>
36 #include <bluetooth.h>
37 #include <stdbool.h>
38 #include <string.h>
40 #include "btkey.h"
42 static const char *key_file = "/var/db/bthcid.keys";
45 * List keys file.
47 bool
48 list_file(void)
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;
57 db = prop_dictionary_internalize_from_file(key_file);
58 if (db == NULL)
59 return false;
61 dev = prop_dictionary_get(db, bt_ntoa(&laddr, NULL));
62 if (prop_object_type(dev) != PROP_TYPE_DICTIONARY)
63 goto done;
65 iter = prop_dictionary_iterator(dev);
66 if (iter == NULL)
67 goto done;
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;
75 dat = prop_dictionary_get_keysym(dev, sym);
76 if (prop_data_size(dat) != HCI_KEY_SIZE)
77 continue;
79 printf("\n");
80 print_addr("bdaddr", &bdaddr);
81 print_key("file key", prop_data_data_nocopy(dat));
84 prop_object_iterator_release(iter);
85 rv = true;
87 done:
88 prop_object_release(db);
89 return rv;
93 * Read from keys file.
95 bool
96 read_file(void)
98 prop_dictionary_t db, dev;
99 prop_object_t dat;
100 bool rv = false;
102 db = prop_dictionary_internalize_from_file(key_file);
103 if (db == NULL)
104 return false;
106 dev = prop_dictionary_get(db, bt_ntoa(&laddr, NULL));
107 if (prop_object_type(dev) != PROP_TYPE_DICTIONARY)
108 goto done;
110 dat = prop_dictionary_get(dev, bt_ntoa(&raddr, NULL));
111 if (prop_data_size(dat) != HCI_KEY_SIZE)
112 goto done;
114 memcpy(key, prop_data_data_nocopy(dat), HCI_KEY_SIZE);
115 rv = true;
117 done:
118 prop_object_release(db);
119 return rv;
123 * Write to keys file.
125 bool
126 write_file(void)
128 prop_dictionary_t db, dev;
129 prop_data_t dat;
130 mode_t mode;
131 bool rv = false;
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;
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;
146 rv = prop_dictionary_set(db, bt_ntoa(&laddr, NULL), dev);
147 prop_object_release(dev);
148 if (rv == false)
149 goto done;
152 dat = prop_data_create_data_nocopy(key, HCI_KEY_SIZE);
153 if (dat == NULL)
154 goto done;
156 rv = prop_dictionary_set(dev, bt_ntoa(&raddr, NULL), dat);
157 prop_object_release(dat);
158 if (rv == false)
159 goto done;
161 mode = umask(S_IRWXG | S_IRWXO);
162 rv = prop_dictionary_externalize_to_file(db, key_file);
163 umask(mode);
165 done:
166 prop_object_release(db);
167 return rv;
171 * Clear from keys file.
173 bool
174 clear_file(void)
176 prop_dictionary_t db, dev;
177 prop_data_t dat;
178 mode_t mode;
179 bool rv = false;
181 db = prop_dictionary_internalize_from_file(key_file);
182 if (db == NULL)
183 return false;
185 dev = prop_dictionary_get(db, bt_ntoa(&laddr, NULL));
186 if (dev == NULL)
187 goto done;
189 dat = prop_dictionary_get(dev, bt_ntoa(&raddr, NULL));
190 if (prop_data_size(dat) != HCI_KEY_SIZE)
191 goto done;
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);
197 umask(mode);
199 done:
200 prop_object_release(db);
201 return rv;