xref: /netbsd-src/usr.sbin/btdevctl/db.c (revision bcd1eb151dd460c33b0aaeef288c719648c890e3)
1*bcd1eb15Sthorpej /*	$NetBSD: db.c,v 1.6 2020/06/07 00:12:00 thorpej Exp $	*/
24f1cbddcSplunky 
34f1cbddcSplunky /*-
44f1cbddcSplunky  * Copyright (c) 2006 Itronix Inc.
54f1cbddcSplunky  * All rights reserved.
64f1cbddcSplunky  *
74f1cbddcSplunky  * Written by Iain Hibbert for Itronix Inc.
84f1cbddcSplunky  *
94f1cbddcSplunky  * Redistribution and use in source and binary forms, with or without
104f1cbddcSplunky  * modification, are permitted provided that the following conditions
114f1cbddcSplunky  * are met:
124f1cbddcSplunky  * 1. Redistributions of source code must retain the above copyright
134f1cbddcSplunky  *    notice, this list of conditions and the following disclaimer.
144f1cbddcSplunky  * 2. Redistributions in binary form must reproduce the above copyright
154f1cbddcSplunky  *    notice, this list of conditions and the following disclaimer in the
164f1cbddcSplunky  *    documentation and/or other materials provided with the distribution.
174f1cbddcSplunky  * 3. The name of Itronix Inc. may not be used to endorse
184f1cbddcSplunky  *    or promote products derived from this software without specific
194f1cbddcSplunky  *    prior written permission.
204f1cbddcSplunky  *
214f1cbddcSplunky  * THIS SOFTWARE IS PROVIDED BY ITRONIX INC. ``AS IS'' AND
224f1cbddcSplunky  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
234f1cbddcSplunky  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
244f1cbddcSplunky  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL ITRONIX INC. BE LIABLE FOR ANY
254f1cbddcSplunky  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
264f1cbddcSplunky  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
274f1cbddcSplunky  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
284f1cbddcSplunky  * ON ANY THEORY OF LIABILITY, WHETHER IN
294f1cbddcSplunky  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
304f1cbddcSplunky  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
314f1cbddcSplunky  * POSSIBILITY OF SUCH DAMAGE.
324f1cbddcSplunky  */
334f1cbddcSplunky 
344f1cbddcSplunky #include <sys/cdefs.h>
35*bcd1eb15Sthorpej __RCSID("$NetBSD: db.c,v 1.6 2020/06/07 00:12:00 thorpej Exp $");
364f1cbddcSplunky 
374f1cbddcSplunky #include <bluetooth.h>
384f1cbddcSplunky #include <err.h>
394f1cbddcSplunky #include <stdlib.h>
405e809e89Spavel #include <stdbool.h>
414f1cbddcSplunky 
424f1cbddcSplunky #include <prop/proplib.h>
434f1cbddcSplunky 
444f1cbddcSplunky #include <dev/bluetooth/btdev.h>
45f5db72e7Splunky #include <dev/bluetooth/bthidev.h>
464f1cbddcSplunky #include <dev/bluetooth/btsco.h>
474f1cbddcSplunky 
484f1cbddcSplunky #include "btdevctl.h"
494f1cbddcSplunky 
504f1cbddcSplunky #define BTDEVCTL_PLIST		"/var/db/btdevctl.plist"
51f5db72e7Splunky #define BTDEVCTL_VERSION	2
524f1cbddcSplunky 
534f1cbddcSplunky static prop_dictionary_t db = NULL;
545e809e89Spavel static bool db_flush = true;		/* write db on set */
554f1cbddcSplunky 
564f1cbddcSplunky static void db_update0(void);
57f5db72e7Splunky static void db_update1(void);
584f1cbddcSplunky 
594f1cbddcSplunky /*
604f1cbddcSplunky  * lookup laddr/raddr/service in database and return dictionary
614f1cbddcSplunky  */
624f1cbddcSplunky prop_dictionary_t
db_get(bdaddr_t * laddr,bdaddr_t * raddr,const char * service)634f1cbddcSplunky db_get(bdaddr_t *laddr, bdaddr_t *raddr, const char *service)
644f1cbddcSplunky {
654f1cbddcSplunky 	prop_dictionary_t ldev, rdev, dev;
664f1cbddcSplunky 	prop_object_t obj;
674f1cbddcSplunky 
684f1cbddcSplunky 	if (db == NULL) {
694f1cbddcSplunky 		db = prop_dictionary_internalize_from_file(BTDEVCTL_PLIST);
704f1cbddcSplunky 		if (db == NULL) {
714f1cbddcSplunky 			db = prop_dictionary_create();
724f1cbddcSplunky 			if (db == NULL)
734f1cbddcSplunky 				err(EXIT_FAILURE, "prop_dictionary_create");
744f1cbddcSplunky 
754f1cbddcSplunky 			return NULL;
764f1cbddcSplunky 		} else {
774f1cbddcSplunky 			obj = prop_dictionary_get(db, "btdevctl-version");
78*bcd1eb15Sthorpej 			switch(prop_number_signed_value(obj)) {
79f5db72e7Splunky 			case 0: db_update0();
80fbffadb9Smrg 				/* FALLTHROUGH */
81f5db72e7Splunky 			case 1: db_update1();
82fbffadb9Smrg 				/* FALLTHROUGH */
834f1cbddcSplunky 			case BTDEVCTL_VERSION:
844f1cbddcSplunky 				break;
854f1cbddcSplunky 
864f1cbddcSplunky 			default:
874f1cbddcSplunky 				errx(EXIT_FAILURE, "unknown btdevctl-version");
884f1cbddcSplunky 			}
894f1cbddcSplunky 		}
904f1cbddcSplunky 	}
914f1cbddcSplunky 
924f1cbddcSplunky 	ldev = prop_dictionary_get(db, bt_ntoa(laddr, NULL));
934f1cbddcSplunky 	if (prop_object_type(ldev) != PROP_TYPE_DICTIONARY)
944f1cbddcSplunky 		return NULL;
954f1cbddcSplunky 
964f1cbddcSplunky 	rdev = prop_dictionary_get(ldev, bt_ntoa(raddr, NULL));
974f1cbddcSplunky 	if (prop_object_type(rdev) != PROP_TYPE_DICTIONARY)
984f1cbddcSplunky 		return NULL;
994f1cbddcSplunky 
1004f1cbddcSplunky 	dev = prop_dictionary_get(rdev, service);
1014f1cbddcSplunky 	if (prop_object_type(dev) != PROP_TYPE_DICTIONARY)
1024f1cbddcSplunky 		return NULL;
1034f1cbddcSplunky 
1044f1cbddcSplunky 	return dev;
1054f1cbddcSplunky }
1064f1cbddcSplunky 
1074f1cbddcSplunky /*
1084f1cbddcSplunky  * store dictionary in database at laddr/raddr/service
1094f1cbddcSplunky  */
1104f1cbddcSplunky int
db_set(prop_dictionary_t dev,bdaddr_t * laddr,bdaddr_t * raddr,const char * service)1114f1cbddcSplunky db_set(prop_dictionary_t dev, bdaddr_t *laddr, bdaddr_t *raddr, const char *service)
1124f1cbddcSplunky {
1134f1cbddcSplunky 	prop_dictionary_t ldev, rdev;
1144f1cbddcSplunky 
1154f1cbddcSplunky 	ldev = prop_dictionary_get(db, bt_ntoa(laddr, NULL));
1164f1cbddcSplunky 	if (ldev == NULL) {
1174f1cbddcSplunky 		ldev = prop_dictionary_create();
1184f1cbddcSplunky 		if (ldev == NULL)
1194f1cbddcSplunky 			return 0;
1204f1cbddcSplunky 
1214f1cbddcSplunky 		if (!prop_dictionary_set(db, bt_ntoa(laddr, NULL), ldev))
1224f1cbddcSplunky 			return 0;
1234f1cbddcSplunky 
1244f1cbddcSplunky 		prop_object_release(ldev);
1254f1cbddcSplunky 	}
1264f1cbddcSplunky 
1274f1cbddcSplunky 	rdev = prop_dictionary_get(ldev, bt_ntoa(raddr, NULL));
1284f1cbddcSplunky 	if (rdev == NULL) {
1294f1cbddcSplunky 		rdev = prop_dictionary_create();
1304f1cbddcSplunky 		if (rdev == NULL)
1314f1cbddcSplunky 			return 0;
1324f1cbddcSplunky 
1334f1cbddcSplunky 		if (!prop_dictionary_set(ldev, bt_ntoa(raddr, NULL), rdev))
1344f1cbddcSplunky 			return 0;
1354f1cbddcSplunky 
1364f1cbddcSplunky 		prop_object_release(rdev);
1374f1cbddcSplunky 	}
1384f1cbddcSplunky 
1394f1cbddcSplunky 	if (!prop_dictionary_set(rdev, service, dev))
1404f1cbddcSplunky 		return 0;
1414f1cbddcSplunky 
1425e809e89Spavel 	if (db_flush == true) {
143*bcd1eb15Sthorpej 		if (!prop_dictionary_set_int(db, "btdevctl-version",
144*bcd1eb15Sthorpej 					     BTDEVCTL_VERSION))
145*bcd1eb15Sthorpej 			err(EXIT_FAILURE, "prop_dictionary_set_int");
1464f1cbddcSplunky 
1474f1cbddcSplunky 		if (!prop_dictionary_externalize_to_file(db, BTDEVCTL_PLIST))
1484f1cbddcSplunky 			warn("%s", BTDEVCTL_PLIST);
1494f1cbddcSplunky 	}
1504f1cbddcSplunky 
1514f1cbddcSplunky 	return 1;
1524f1cbddcSplunky }
1534f1cbddcSplunky 
1544f1cbddcSplunky /*
1554f1cbddcSplunky  * update database from version 0. This was a flat file using
1564f1cbddcSplunky  * btdevN as an index, and local-bdaddr and remote-bdaddr stored
1574f1cbddcSplunky  * as data objects. Step through and add them to the new dictionary.
1584f1cbddcSplunky  * We have to generate the service, but only HID, HF and HSET
1594f1cbddcSplunky  * were supported, so thats not too difficult.
1604f1cbddcSplunky  */
1614f1cbddcSplunky static void
db_update0(void)1624f1cbddcSplunky db_update0(void)
1634f1cbddcSplunky {
1644f1cbddcSplunky 	prop_dictionary_t old, dev;
1654f1cbddcSplunky 	prop_dictionary_keysym_t key;
1664f1cbddcSplunky 	prop_object_iterator_t iter;
1674f1cbddcSplunky 	prop_object_t obj;
1684f1cbddcSplunky 	bdaddr_t laddr, raddr;
16951bb5141Splunky 	const char *service;
1704f1cbddcSplunky 
1715e809e89Spavel 	db_flush = false;	/* no write on set */
1724f1cbddcSplunky 	old = db;
1734f1cbddcSplunky 
1744f1cbddcSplunky 	db = prop_dictionary_create();
1754f1cbddcSplunky 	if (db == NULL)
1764f1cbddcSplunky 		err(EXIT_FAILURE, "prop_dictionary_create");
1774f1cbddcSplunky 
1784f1cbddcSplunky 	iter = prop_dictionary_iterator(old);
1794f1cbddcSplunky 	if (iter == NULL)
1804f1cbddcSplunky 		err(EXIT_FAILURE, "prop_dictionary_iterator");
1814f1cbddcSplunky 
1824f1cbddcSplunky 	while ((key = prop_object_iterator_next(iter)) != NULL) {
1834f1cbddcSplunky 		dev = prop_dictionary_get_keysym(old, key);
1844f1cbddcSplunky 		if (prop_object_type(dev) != PROP_TYPE_DICTIONARY)
1854f1cbddcSplunky 			errx(EXIT_FAILURE, "invalid device dictionary");
1864f1cbddcSplunky 
1874f1cbddcSplunky 		obj = prop_dictionary_get(dev, BTDEVladdr);
1884f1cbddcSplunky 		if (prop_data_size(obj) != sizeof(laddr))
1894f1cbddcSplunky 			errx(EXIT_FAILURE, "invalid %s", BTDEVladdr);
1904f1cbddcSplunky 
191*bcd1eb15Sthorpej 		bdaddr_copy(&laddr, prop_data_value(obj));
1924f1cbddcSplunky 		prop_dictionary_remove(dev, BTDEVladdr);
1934f1cbddcSplunky 
1944f1cbddcSplunky 		obj = prop_dictionary_get(dev, BTDEVraddr);
1954f1cbddcSplunky 		if (prop_data_size(obj) != sizeof(raddr))
1964f1cbddcSplunky 			errx(EXIT_FAILURE, "invalid %s", BTDEVraddr);
1974f1cbddcSplunky 
198*bcd1eb15Sthorpej 		bdaddr_copy(&raddr, prop_data_value(obj));
1994f1cbddcSplunky 		prop_dictionary_remove(dev, BTDEVraddr);
2004f1cbddcSplunky 
2014f1cbddcSplunky 		obj = prop_dictionary_get(dev, BTDEVtype);
202*bcd1eb15Sthorpej 		if (prop_string_equals_string(obj, "bthidev"))
2034f1cbddcSplunky 			service = "HID";
204*bcd1eb15Sthorpej 		else if (prop_string_equals_string(obj, "btsco")) {
2054f1cbddcSplunky 			obj = prop_dictionary_get(dev, BTSCOlisten);
2064f1cbddcSplunky 			if (prop_bool_true(obj))
2074f1cbddcSplunky 				service = "HF";
2084f1cbddcSplunky 			else
2094f1cbddcSplunky 				service = "HSET";
2104f1cbddcSplunky 		} else
2114f1cbddcSplunky 			errx(EXIT_FAILURE, "invalid %s", BTDEVtype);
2124f1cbddcSplunky 
2134f1cbddcSplunky 		if (!db_set(dev, &laddr, &raddr, service))
2144f1cbddcSplunky 			err(EXIT_FAILURE, "service store failed");
2154f1cbddcSplunky 	}
2164f1cbddcSplunky 
2174f1cbddcSplunky 	prop_object_iterator_release(iter);
2184f1cbddcSplunky 	prop_object_release(old);
2194f1cbddcSplunky 
2205e809e89Spavel 	db_flush = true;	/* write on set */
2214f1cbddcSplunky }
222f5db72e7Splunky 
223f5db72e7Splunky /*
224f5db72e7Splunky  * update database from version 1. Link Mode capability was added.
225f5db72e7Splunky  * By default, we request authentication for HIDs, and encryption
226f5db72e7Splunky  * is enabled for keyboards.
227f5db72e7Splunky  */
228f5db72e7Splunky static void
db_update1(void)229f5db72e7Splunky db_update1(void)
230f5db72e7Splunky {
231f5db72e7Splunky 	prop_dictionary_t ldev, rdev, srv;
232f5db72e7Splunky 	prop_object_iterator_t iter0, iter1;
233f5db72e7Splunky 	prop_dictionary_keysym_t key;
234f5db72e7Splunky 	prop_object_t obj;
235f5db72e7Splunky 	bdaddr_t bdaddr;
236f5db72e7Splunky 
237f5db72e7Splunky 	iter0 = prop_dictionary_iterator(db);
238f5db72e7Splunky 	if (iter0 == NULL)
239f5db72e7Splunky 		err(EXIT_FAILURE, "prop_dictionary_iterator");
240f5db72e7Splunky 
241f5db72e7Splunky 	while ((key = prop_object_iterator_next(iter0)) != NULL) {
242f5db72e7Splunky 		ldev = prop_dictionary_get_keysym(db, key);
243f5db72e7Splunky 		if (prop_object_type(ldev) != PROP_TYPE_DICTIONARY
244*bcd1eb15Sthorpej 		    || !bt_aton(prop_dictionary_keysym_value(key), &bdaddr))
245f5db72e7Splunky 			continue;
246f5db72e7Splunky 
247f5db72e7Splunky 		iter1 = prop_dictionary_iterator(ldev);
248f5db72e7Splunky 		if (iter1 == NULL)
249f5db72e7Splunky 			err(EXIT_FAILURE, "prop_dictionary_iterator");
250f5db72e7Splunky 
251f5db72e7Splunky 		while ((key = prop_object_iterator_next(iter1)) != NULL) {
252f5db72e7Splunky 			rdev = prop_dictionary_get_keysym(ldev, key);
253f5db72e7Splunky 			if (prop_object_type(rdev) != PROP_TYPE_DICTIONARY
254*bcd1eb15Sthorpej 			    || !bt_aton(prop_dictionary_keysym_value(key), &bdaddr))
255f5db72e7Splunky 				continue;
256f5db72e7Splunky 
257f5db72e7Splunky 			srv = prop_dictionary_get(rdev, "HID");
258f5db72e7Splunky 			if (prop_object_type(srv) != PROP_TYPE_DICTIONARY)
259f5db72e7Splunky 				continue;
260f5db72e7Splunky 
261f5db72e7Splunky 			obj = prop_dictionary_get(srv, BTHIDEVdescriptor);
262f5db72e7Splunky 			if (prop_object_type(obj) != PROP_TYPE_DATA)
263f5db72e7Splunky 				continue;
264f5db72e7Splunky 
265*bcd1eb15Sthorpej 			if (!prop_dictionary_set_string_nocopy(srv, BTDEVmode,
266*bcd1eb15Sthorpej 							       hid_mode(obj)))
267f5db72e7Splunky 				err(EXIT_FAILURE, "Cannot set %s", BTDEVmode);
268f5db72e7Splunky 		}
269f5db72e7Splunky 
270f5db72e7Splunky 		prop_object_iterator_release(iter1);
271f5db72e7Splunky 	}
272f5db72e7Splunky 
273f5db72e7Splunky 	prop_object_iterator_release(iter0);
274f5db72e7Splunky }
275