1 /* $NetBSD: db.c,v 1.5 2019/02/03 03:19:30 mrg 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.5 2019/02/03 03:19:30 mrg 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 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_integer_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 111 db_set(prop_dictionary_t dev, bdaddr_t *laddr, bdaddr_t *raddr, const char *service) 112 { 113 prop_dictionary_t ldev, rdev; 114 prop_number_t version; 115 116 ldev = prop_dictionary_get(db, bt_ntoa(laddr, NULL)); 117 if (ldev == NULL) { 118 ldev = prop_dictionary_create(); 119 if (ldev == NULL) 120 return 0; 121 122 if (!prop_dictionary_set(db, bt_ntoa(laddr, NULL), ldev)) 123 return 0; 124 125 prop_object_release(ldev); 126 } 127 128 rdev = prop_dictionary_get(ldev, bt_ntoa(raddr, NULL)); 129 if (rdev == NULL) { 130 rdev = prop_dictionary_create(); 131 if (rdev == NULL) 132 return 0; 133 134 if (!prop_dictionary_set(ldev, bt_ntoa(raddr, NULL), rdev)) 135 return 0; 136 137 prop_object_release(rdev); 138 } 139 140 if (!prop_dictionary_set(rdev, service, dev)) 141 return 0; 142 143 if (db_flush == true) { 144 version = prop_number_create_integer(BTDEVCTL_VERSION); 145 if (version == NULL) 146 err(EXIT_FAILURE, "prop_number_create_integer"); 147 148 if (!prop_dictionary_set(db, "btdevctl-version", version)) 149 err(EXIT_FAILURE, "prop_dictionary_set"); 150 151 prop_object_release(version); 152 153 if (!prop_dictionary_externalize_to_file(db, BTDEVCTL_PLIST)) 154 warn("%s", BTDEVCTL_PLIST); 155 } 156 157 return 1; 158 } 159 160 /* 161 * update database from version 0. This was a flat file using 162 * btdevN as an index, and local-bdaddr and remote-bdaddr stored 163 * as data objects. Step through and add them to the new dictionary. 164 * We have to generate the service, but only HID, HF and HSET 165 * were supported, so thats not too difficult. 166 */ 167 static void 168 db_update0(void) 169 { 170 prop_dictionary_t old, dev; 171 prop_dictionary_keysym_t key; 172 prop_object_iterator_t iter; 173 prop_object_t obj; 174 bdaddr_t laddr, raddr; 175 const char *service; 176 177 db_flush = false; /* no write on set */ 178 old = db; 179 180 db = prop_dictionary_create(); 181 if (db == NULL) 182 err(EXIT_FAILURE, "prop_dictionary_create"); 183 184 iter = prop_dictionary_iterator(old); 185 if (iter == NULL) 186 err(EXIT_FAILURE, "prop_dictionary_iterator"); 187 188 while ((key = prop_object_iterator_next(iter)) != NULL) { 189 dev = prop_dictionary_get_keysym(old, key); 190 if (prop_object_type(dev) != PROP_TYPE_DICTIONARY) 191 errx(EXIT_FAILURE, "invalid device dictionary"); 192 193 obj = prop_dictionary_get(dev, BTDEVladdr); 194 if (prop_data_size(obj) != sizeof(laddr)) 195 errx(EXIT_FAILURE, "invalid %s", BTDEVladdr); 196 197 bdaddr_copy(&laddr, prop_data_data_nocopy(obj)); 198 prop_dictionary_remove(dev, BTDEVladdr); 199 200 obj = prop_dictionary_get(dev, BTDEVraddr); 201 if (prop_data_size(obj) != sizeof(raddr)) 202 errx(EXIT_FAILURE, "invalid %s", BTDEVraddr); 203 204 bdaddr_copy(&raddr, prop_data_data_nocopy(obj)); 205 prop_dictionary_remove(dev, BTDEVraddr); 206 207 obj = prop_dictionary_get(dev, BTDEVtype); 208 if (prop_string_equals_cstring(obj, "bthidev")) 209 service = "HID"; 210 else if (prop_string_equals_cstring(obj, "btsco")) { 211 obj = prop_dictionary_get(dev, BTSCOlisten); 212 if (prop_bool_true(obj)) 213 service = "HF"; 214 else 215 service = "HSET"; 216 } else 217 errx(EXIT_FAILURE, "invalid %s", BTDEVtype); 218 219 if (!db_set(dev, &laddr, &raddr, service)) 220 err(EXIT_FAILURE, "service store failed"); 221 } 222 223 prop_object_iterator_release(iter); 224 prop_object_release(old); 225 226 db_flush = true; /* write on set */ 227 } 228 229 /* 230 * update database from version 1. Link Mode capability was added. 231 * By default, we request authentication for HIDs, and encryption 232 * is enabled for keyboards. 233 */ 234 static void 235 db_update1(void) 236 { 237 prop_dictionary_t ldev, rdev, srv; 238 prop_object_iterator_t iter0, iter1; 239 prop_dictionary_keysym_t key; 240 prop_object_t obj; 241 bdaddr_t bdaddr; 242 243 iter0 = prop_dictionary_iterator(db); 244 if (iter0 == NULL) 245 err(EXIT_FAILURE, "prop_dictionary_iterator"); 246 247 while ((key = prop_object_iterator_next(iter0)) != NULL) { 248 ldev = prop_dictionary_get_keysym(db, key); 249 if (prop_object_type(ldev) != PROP_TYPE_DICTIONARY 250 || !bt_aton(prop_dictionary_keysym_cstring_nocopy(key), &bdaddr)) 251 continue; 252 253 iter1 = prop_dictionary_iterator(ldev); 254 if (iter1 == NULL) 255 err(EXIT_FAILURE, "prop_dictionary_iterator"); 256 257 while ((key = prop_object_iterator_next(iter1)) != NULL) { 258 rdev = prop_dictionary_get_keysym(ldev, key); 259 if (prop_object_type(rdev) != PROP_TYPE_DICTIONARY 260 || !bt_aton(prop_dictionary_keysym_cstring_nocopy(key), &bdaddr)) 261 continue; 262 263 srv = prop_dictionary_get(rdev, "HID"); 264 if (prop_object_type(srv) != PROP_TYPE_DICTIONARY) 265 continue; 266 267 obj = prop_dictionary_get(srv, BTHIDEVdescriptor); 268 if (prop_object_type(obj) != PROP_TYPE_DATA) 269 continue; 270 271 obj = prop_string_create_cstring_nocopy(hid_mode(obj)); 272 if (obj == NULL || !prop_dictionary_set(srv, BTDEVmode, obj)) 273 err(EXIT_FAILURE, "Cannot set %s", BTDEVmode); 274 275 prop_object_release(obj); 276 } 277 278 prop_object_iterator_release(iter1); 279 } 280 281 prop_object_iterator_release(iter0); 282 } 283