1 /* $NetBSD: config.c,v 1.4 2007/01/25 20:33:41 plunky Exp $ */ 2 /* $DragonFly: src/usr.sbin/bthcid/config.c,v 1.1 2008/01/30 14:10:19 hasso Exp $ */ 3 4 /*- 5 * Copyright (c) 2006 Itronix Inc. 6 * All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 3. The name of Itronix Inc. may not be used to endorse 17 * or promote products derived from this software without specific 18 * prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY ITRONIX INC. ``AS IS'' AND 21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL ITRONIX INC. BE LIABLE FOR ANY 24 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 25 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 26 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 27 * ON ANY THEORY OF LIABILITY, WHETHER IN 28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 30 * POSSIBILITY OF SUCH DAMAGE. 31 */ 32 33 #include <sys/time.h> 34 #include <bluetooth.h> 35 #include <errno.h> 36 #include <event.h> 37 #include <fcntl.h> 38 #include <stdlib.h> 39 #include <string.h> 40 #include <syslog.h> 41 #include <unistd.h> 42 43 #include "bthcid.h" 44 45 #if 0 46 static const char *key_file = "/var/db/bthcid.keys"; 47 static const char *new_key_file = "/var/db/bthcid.keys.new"; 48 #endif 49 50 /* 51 * Look up key in keys file. We store a dictionary for each 52 * remote address, and inside that we have a data object for 53 * each local address containing the key. 54 */ 55 uint8_t * 56 lookup_key(bdaddr_t *laddr, bdaddr_t *raddr) 57 { 58 link_key_p key = NULL; 59 60 syslog(LOG_DEBUG, "Got Link_Key_Request event from '%s', " \ 61 "remote bdaddr %s", bt_ntoa(laddr, NULL), 62 bt_ntoa(raddr, NULL)); 63 64 if ((key = get_key(raddr, 0)) != NULL) { 65 syslog(LOG_DEBUG, "Found matching entry, " \ 66 "remote bdaddr %s, name '%s', link key %s", 67 bt_ntoa(&key->bdaddr, NULL), 68 (key->name != NULL)? key->name : "No name", 69 (key->key != NULL)? "exists" : "doesn't exist"); 70 return key->key; 71 } 72 73 syslog(LOG_DEBUG, "Could not find link key for remote bdaddr %s", 74 bt_ntoa(raddr, NULL)); 75 return NULL; 76 } 77 78 /* 79 * Look up pin in keys file. We store a dictionary for each 80 * remote address, and inside that we have a data object for 81 * each local address containing the pin. 82 */ 83 uint8_t * 84 lookup_pin_conf(bdaddr_t *laddr, bdaddr_t *raddr) 85 { 86 link_key_p key = NULL; 87 88 syslog(LOG_DEBUG, "Got Link_Pin_Request event from '%s', " \ 89 "remote bdaddr %s", bt_ntoa(laddr, NULL), 90 bt_ntoa(raddr, NULL)); 91 92 if ((key = get_key(raddr, 0)) != NULL) { 93 syslog(LOG_DEBUG, "Found matching entry, " \ 94 "remote bdaddr %s, name '%s', pin %s", 95 bt_ntoa(&key->bdaddr, NULL), 96 (key->name != NULL)? key->name : "No name", 97 (key->pin != NULL)? "exists" : "doesn't exist"); 98 return key->pin; 99 } 100 101 syslog(LOG_DEBUG, "Could not find link key for remote bdaddr %s", 102 bt_ntoa(raddr, NULL)); 103 return NULL; 104 } 105 106 107 void 108 save_key(bdaddr_t *laddr, bdaddr_t *raddr, uint8_t * key) 109 { 110 link_key_p lkey = NULL; 111 112 syslog(LOG_DEBUG, "Got Link_Key_Notification event from '%s', " \ 113 "remote bdaddr %s", bt_ntoa(laddr, NULL), 114 bt_ntoa(raddr, NULL)); 115 116 if ((lkey = get_key(raddr, 1)) == NULL) { 117 syslog(LOG_ERR, "Could not find entry for remote bdaddr %s", 118 bt_ntoa(raddr, NULL)); 119 return; 120 } 121 122 syslog(LOG_DEBUG, "Updating link key for the entry, " \ 123 "remote bdaddr %s, name '%s', link key %s", 124 bt_ntoa(&lkey->bdaddr, NULL), 125 (lkey->name != NULL)? lkey->name : "No name", 126 (lkey->key != NULL)? "exists" : "doesn't exist"); 127 128 if (lkey->key == NULL) { 129 lkey->key = (uint8_t *) malloc(HCI_KEY_SIZE); 130 if (lkey->key == NULL) { 131 syslog(LOG_ERR, "Could not allocate link key"); 132 exit(1); 133 } 134 } 135 136 memcpy(lkey->key, key, HCI_KEY_SIZE); 137 138 dump_keys_file(); 139 read_config_file(); 140 read_keys_file(); 141 142 return; 143 } 144 145