Merge branch 'vendor/GCC50'
[dragonfly.git] / usr.sbin / bthcid / config.c
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 <fcntl.h>
37 #include <stdlib.h>
38 #include <string.h>
39 #include <syslog.h>
40 #include <unistd.h>
41
42 #include "bthcid.h"
43
44 #if 0
45 static const char *key_file = "/var/db/bthcid.keys";
46 static const char *new_key_file = "/var/db/bthcid.keys.new";
47 #endif
48
49 /*
50  * Look up key in keys file. We store a dictionary for each
51  * remote address, and inside that we have a data object for
52  * each local address containing the key.
53  */
54 uint8_t *
55 lookup_key(bdaddr_t *laddr, bdaddr_t *raddr)
56 {
57         link_key_p      key = NULL;
58
59         syslog(LOG_DEBUG, "Got Link_Key_Request event from '%s', " \
60                         "remote bdaddr %s", bt_ntoa(laddr, NULL),
61                         bt_ntoa(raddr, NULL));
62
63         if ((key = get_key(raddr, 0)) != NULL) {
64                 syslog(LOG_DEBUG, "Found matching entry, " \
65                                 "remote bdaddr %s, name '%s', link key %s",
66                                 bt_ntoa(&key->bdaddr, NULL),
67                                 (key->name != NULL)? key->name : "No name",
68                                 (key->key != NULL)? "exists" : "doesn't exist");
69                 return key->key;
70         }
71
72         syslog(LOG_DEBUG, "Could not find link key for remote bdaddr %s",
73                         bt_ntoa(raddr, NULL));
74         return NULL;
75 }
76
77 /*
78  * Look up pin in keys file. We store a dictionary for each
79  * remote address, and inside that we have a data object for
80  * each local address containing the pin.
81  */
82 uint8_t *
83 lookup_pin_conf(bdaddr_t *laddr, bdaddr_t *raddr)
84 {
85         link_key_p      key = NULL;
86
87         syslog(LOG_DEBUG, "Got Link_Pin_Request event from '%s', " \
88                         "remote bdaddr %s", bt_ntoa(laddr, NULL),
89                         bt_ntoa(raddr, NULL));
90
91         if ((key = get_key(raddr, 0)) != NULL) {
92                 syslog(LOG_DEBUG, "Found matching entry, " \
93                                 "remote bdaddr %s, name '%s', pin %s",
94                                 bt_ntoa(&key->bdaddr, NULL),
95                                 (key->name != NULL)? key->name : "No name",
96                                 (key->pin != NULL)? "exists" : "doesn't exist");
97                 return key->pin;
98         }
99
100         syslog(LOG_DEBUG, "Could not find link key for remote bdaddr %s",
101                         bt_ntoa(raddr, NULL));
102         return NULL;
103 }
104
105
106 void
107 save_key(bdaddr_t *laddr, bdaddr_t *raddr, uint8_t * key)
108 {
109         link_key_p      lkey = NULL;
110
111         syslog(LOG_DEBUG, "Got Link_Key_Notification event from '%s', " \
112                         "remote bdaddr %s", bt_ntoa(laddr, NULL),
113                         bt_ntoa(raddr, NULL));
114
115         if ((lkey = get_key(raddr, 1)) == NULL) {
116                 syslog(LOG_ERR, "Could not find entry for remote bdaddr %s",
117                                 bt_ntoa(raddr, NULL));
118                 return;
119         }
120         
121         syslog(LOG_DEBUG, "Updating link key for the entry, " \
122                         "remote bdaddr %s, name '%s', link key %s",
123                         bt_ntoa(&lkey->bdaddr, NULL),
124                         (lkey->name != NULL)? lkey->name : "No name",
125                         (lkey->key != NULL)? "exists" : "doesn't exist");
126
127         if (lkey->key == NULL) {
128                 lkey->key = (uint8_t *) malloc(HCI_KEY_SIZE);
129                 if (lkey->key == NULL) {
130                         syslog(LOG_ERR, "Could not allocate link key");
131                         exit(1);
132                 }
133         }
134
135         memcpy(lkey->key, key, HCI_KEY_SIZE);
136
137         dump_keys_file();
138         read_config_file();
139         read_keys_file();
140         
141         return;
142 }
143