xref: /netbsd/usr.sbin/btdevctl/db.c (revision d6b3a92f)
1 /*	$NetBSD: db.c,v 1.6 2020/06/07 00:12:00 thorpej Exp $	*/
2 
3 /*-
4  * Copyright (c) 2006 Itronix Inc.
5  * All rights reserved.
6  *
7  * Written by Iain Hibbert for Itronix Inc.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  * 3. The name of Itronix Inc. may not be used to endorse
18  *    or promote products derived from this software without specific
19  *    prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY ITRONIX INC. ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
23  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL ITRONIX INC. BE LIABLE FOR ANY
25  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
26  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
28  * ON ANY THEORY OF LIABILITY, WHETHER IN
29  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31  * POSSIBILITY OF SUCH DAMAGE.
32  */
33 
34 #include <sys/cdefs.h>
35 __RCSID("$NetBSD: db.c,v 1.6 2020/06/07 00:12:00 thorpej Exp $");
36 
37 #include <bluetooth.h>
38 #include <err.h>
39 #include <stdlib.h>
40 #include <stdbool.h>
41 
42 #include <prop/proplib.h>
43 
44 #include <dev/bluetooth/btdev.h>
45 #include <dev/bluetooth/bthidev.h>
46 #include <dev/bluetooth/btsco.h>
47 
48 #include "btdevctl.h"
49 
50 #define BTDEVCTL_PLIST		"/var/db/btdevctl.plist"
51 #define BTDEVCTL_VERSION	2
52 
53 static prop_dictionary_t db = NULL;
54 static bool db_flush = true;		/* write db on set */
55 
56 static void db_update0(void);
57 static void db_update1(void);
58 
59 /*
60  * lookup laddr/raddr/service in database and return dictionary
61  */
62 prop_dictionary_t
db_get(bdaddr_t * laddr,bdaddr_t * raddr,const char * service)63 db_get(bdaddr_t *laddr, bdaddr_t *raddr, const char *service)
64 {
65 	prop_dictionary_t ldev, rdev, dev;
66 	prop_object_t obj;
67 
68 	if (db == NULL) {
69 		db = prop_dictionary_internalize_from_file(BTDEVCTL_PLIST);
70 		if (db == NULL) {
71 			db = prop_dictionary_create();
72 			if (db == NULL)
73 				err(EXIT_FAILURE, "prop_dictionary_create");
74 
75 			return NULL;
76 		} else {
77 			obj = prop_dictionary_get(db, "btdevctl-version");
78 			switch(prop_number_signed_value(obj)) {
79 			case 0: db_update0();
80 				/* FALLTHROUGH */
81 			case 1: db_update1();
82 				/* FALLTHROUGH */
83 			case BTDEVCTL_VERSION:
84 				break;
85 
86 			default:
87 				errx(EXIT_FAILURE, "unknown btdevctl-version");
88 			}
89 		}
90 	}
91 
92 	ldev = prop_dictionary_get(db, bt_ntoa(laddr, NULL));
93 	if (prop_object_type(ldev) != PROP_TYPE_DICTIONARY)
94 		return NULL;
95 
96 	rdev = prop_dictionary_get(ldev, bt_ntoa(raddr, NULL));
97 	if (prop_object_type(rdev) != PROP_TYPE_DICTIONARY)
98 		return NULL;
99 
100 	dev = prop_dictionary_get(rdev, service);
101 	if (prop_object_type(dev) != PROP_TYPE_DICTIONARY)
102 		return NULL;
103 
104 	return dev;
105 }
106 
107 /*
108  * store dictionary in database at laddr/raddr/service
109  */
110 int
db_set(prop_dictionary_t dev,bdaddr_t * laddr,bdaddr_t * raddr,const char * service)111 db_set(prop_dictionary_t dev, bdaddr_t *laddr, bdaddr_t *raddr, const char *service)
112 {
113 	prop_dictionary_t ldev, rdev;
114 
115 	ldev = prop_dictionary_get(db, bt_ntoa(laddr, NULL));
116 	if (ldev == NULL) {
117 		ldev = prop_dictionary_create();
118 		if (ldev == NULL)
119 			return 0;
120 
121 		if (!prop_dictionary_set(db, bt_ntoa(laddr, NULL), ldev))
122 			return 0;
123 
124 		prop_object_release(ldev);
125 	}
126 
127 	rdev = prop_dictionary_get(ldev, bt_ntoa(raddr, NULL));
128 	if (rdev == NULL) {
129 		rdev = prop_dictionary_create();
130 		if (rdev == NULL)
131 			return 0;
132 
133 		if (!prop_dictionary_set(ldev, bt_ntoa(raddr, NULL), rdev))
134 			return 0;
135 
136 		prop_object_release(rdev);
137 	}
138 
139 	if (!prop_dictionary_set(rdev, service, dev))
140 		return 0;
141 
142 	if (db_flush == true) {
143 		if (!prop_dictionary_set_int(db, "btdevctl-version",
144 					     BTDEVCTL_VERSION))
145 			err(EXIT_FAILURE, "prop_dictionary_set_int");
146 
147 		if (!prop_dictionary_externalize_to_file(db, BTDEVCTL_PLIST))
148 			warn("%s", BTDEVCTL_PLIST);
149 	}
150 
151 	return 1;
152 }
153 
154 /*
155  * update database from version 0. This was a flat file using
156  * btdevN as an index, and local-bdaddr and remote-bdaddr stored
157  * as data objects. Step through and add them to the new dictionary.
158  * We have to generate the service, but only HID, HF and HSET
159  * were supported, so thats not too difficult.
160  */
161 static void
db_update0(void)162 db_update0(void)
163 {
164 	prop_dictionary_t old, dev;
165 	prop_dictionary_keysym_t key;
166 	prop_object_iterator_t iter;
167 	prop_object_t obj;
168 	bdaddr_t laddr, raddr;
169 	const char *service;
170 
171 	db_flush = false;	/* no write on set */
172 	old = db;
173 
174 	db = prop_dictionary_create();
175 	if (db == NULL)
176 		err(EXIT_FAILURE, "prop_dictionary_create");
177 
178 	iter = prop_dictionary_iterator(old);
179 	if (iter == NULL)
180 		err(EXIT_FAILURE, "prop_dictionary_iterator");
181 
182 	while ((key = prop_object_iterator_next(iter)) != NULL) {
183 		dev = prop_dictionary_get_keysym(old, key);
184 		if (prop_object_type(dev) != PROP_TYPE_DICTIONARY)
185 			errx(EXIT_FAILURE, "invalid device dictionary");
186 
187 		obj = prop_dictionary_get(dev, BTDEVladdr);
188 		if (prop_data_size(obj) != sizeof(laddr))
189 			errx(EXIT_FAILURE, "invalid %s", BTDEVladdr);
190 
191 		bdaddr_copy(&laddr, prop_data_value(obj));
192 		prop_dictionary_remove(dev, BTDEVladdr);
193 
194 		obj = prop_dictionary_get(dev, BTDEVraddr);
195 		if (prop_data_size(obj) != sizeof(raddr))
196 			errx(EXIT_FAILURE, "invalid %s", BTDEVraddr);
197 
198 		bdaddr_copy(&raddr, prop_data_value(obj));
199 		prop_dictionary_remove(dev, BTDEVraddr);
200 
201 		obj = prop_dictionary_get(dev, BTDEVtype);
202 		if (prop_string_equals_string(obj, "bthidev"))
203 			service = "HID";
204 		else if (prop_string_equals_string(obj, "btsco")) {
205 			obj = prop_dictionary_get(dev, BTSCOlisten);
206 			if (prop_bool_true(obj))
207 				service = "HF";
208 			else
209 				service = "HSET";
210 		} else
211 			errx(EXIT_FAILURE, "invalid %s", BTDEVtype);
212 
213 		if (!db_set(dev, &laddr, &raddr, service))
214 			err(EXIT_FAILURE, "service store failed");
215 	}
216 
217 	prop_object_iterator_release(iter);
218 	prop_object_release(old);
219 
220 	db_flush = true;	/* write on set */
221 }
222 
223 /*
224  * update database from version 1. Link Mode capability was added.
225  * By default, we request authentication for HIDs, and encryption
226  * is enabled for keyboards.
227  */
228 static void
db_update1(void)229 db_update1(void)
230 {
231 	prop_dictionary_t ldev, rdev, srv;
232 	prop_object_iterator_t iter0, iter1;
233 	prop_dictionary_keysym_t key;
234 	prop_object_t obj;
235 	bdaddr_t bdaddr;
236 
237 	iter0 = prop_dictionary_iterator(db);
238 	if (iter0 == NULL)
239 		err(EXIT_FAILURE, "prop_dictionary_iterator");
240 
241 	while ((key = prop_object_iterator_next(iter0)) != NULL) {
242 		ldev = prop_dictionary_get_keysym(db, key);
243 		if (prop_object_type(ldev) != PROP_TYPE_DICTIONARY
244 		    || !bt_aton(prop_dictionary_keysym_value(key), &bdaddr))
245 			continue;
246 
247 		iter1 = prop_dictionary_iterator(ldev);
248 		if (iter1 == NULL)
249 			err(EXIT_FAILURE, "prop_dictionary_iterator");
250 
251 		while ((key = prop_object_iterator_next(iter1)) != NULL) {
252 			rdev = prop_dictionary_get_keysym(ldev, key);
253 			if (prop_object_type(rdev) != PROP_TYPE_DICTIONARY
254 			    || !bt_aton(prop_dictionary_keysym_value(key), &bdaddr))
255 				continue;
256 
257 			srv = prop_dictionary_get(rdev, "HID");
258 			if (prop_object_type(srv) != PROP_TYPE_DICTIONARY)
259 				continue;
260 
261 			obj = prop_dictionary_get(srv, BTHIDEVdescriptor);
262 			if (prop_object_type(obj) != PROP_TYPE_DATA)
263 				continue;
264 
265 			if (!prop_dictionary_set_string_nocopy(srv, BTDEVmode,
266 							       hid_mode(obj)))
267 				err(EXIT_FAILURE, "Cannot set %s", BTDEVmode);
268 		}
269 
270 		prop_object_iterator_release(iter1);
271 	}
272 
273 	prop_object_iterator_release(iter0);
274 }
275