1 /*        $NetBSD: client.c,v 1.6 2024/09/07 13:57:25 mlelstv Exp $   */
2 
3 /*-
4  * Copyright (c) 2006 Itronix Inc.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. The name of Itronix Inc. may not be used to endorse
16  *    or promote products derived from this software without specific
17  *    prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY ITRONIX INC. ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL ITRONIX INC. BE LIABLE FOR ANY
23  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
26  * ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 #include <sys/cdefs.h>
33 __RCSID("$NetBSD: client.c,v 1.6 2024/09/07 13:57:25 mlelstv Exp $");
34 
35 #include <sys/ioctl.h>
36 #include <sys/queue.h>
37 #include <sys/time.h>
38 #include <sys/stat.h>
39 #include <sys/un.h>
40 #include <bluetooth.h>
41 #include <errno.h>
42 #include <event.h>
43 #include <fcntl.h>
44 #include <stdlib.h>
45 #include <string.h>
46 #include <syslog.h>
47 #include <unistd.h>
48 
49 #include "bthcid.h"
50 
51 /*
52  * A client is anybody who connects to our control socket to
53  * receive PIN requests.
54  */
55 struct client {
56           struct event                  ev;
57           int                           fd;                 /* client descriptor */
58           LIST_ENTRY(client)  next;
59 };
60 
61 /*
62  * PIN cache items are made when we have sent a client pin
63  * request. The event is used to expire the item.
64  */
65 struct item {
66           struct event         ev;
67           bdaddr_t   laddr;                       /* local device BDADDR */
68           bdaddr_t   raddr;                       /* remote device BDADDR */
69           uint8_t              pin[HCI_PIN_SIZE]; /* PIN */
70           int                  hci;                         /* HCI socket */
71           LIST_ENTRY(item) next;
72 };
73 
74 static struct event           control_ev;
75 
76 static LIST_HEAD(,client)     client_list;
77 static LIST_HEAD(,item)                 item_list;
78 
79 static void process_control   (int, short, void *);
80 static void process_client    (int, short, void *);
81 static void process_item      (int, short, void *);
82 
83 #define PIN_REQUEST_TIMEOUT   30        /* Request is valid */
84 #define PIN_TIMEOUT           300       /* PIN is valid */
85 
86 int
init_control(const char * name,mode_t mode)87 init_control(const char *name, mode_t mode)
88 {
89           struct sockaddr_un  un;
90           int                           ctl;
91 
92           LIST_INIT(&client_list);
93           LIST_INIT(&item_list);
94 
95           if (name == NULL)
96                     return 0;
97 
98           if (unlink(name) < 0 && errno != ENOENT)
99                     return -1;
100 
101           ctl = socket(PF_LOCAL, SOCK_STREAM, 0);
102           if (ctl < 0)
103                     return -1;
104 
105           memset(&un, 0, sizeof(un));
106           un.sun_len = sizeof(un);
107           un.sun_family = AF_LOCAL;
108           strlcpy(un.sun_path, name, sizeof(un.sun_path));
109           if (bind(ctl, (struct sockaddr *)&un, sizeof(un)) < 0) {
110                     close(ctl);
111                     return -1;
112           }
113 
114           if (chmod(name, mode) < 0) {
115                     close(ctl);
116                     unlink(name);
117                     return -1;
118           }
119 
120           if (listen(ctl, 10) < 0) {
121                     close(ctl);
122                     unlink(name);
123                     return -1;
124           }
125 
126           event_set(&control_ev, ctl, EV_READ | EV_PERSIST, process_control, NULL);
127           if (event_add(&control_ev, NULL) < 0) {
128                     close(ctl);
129                     unlink(name);
130                     return -1;
131           }
132 
133           return 0;
134 }
135 
136 /* Process control socket event */
137 static void
process_control(int sock,short ev,void * arg)138 process_control(int sock, short ev, void *arg)
139 {
140           struct sockaddr_un  un;
141           socklen_t           n;
142           int                           fd;
143           struct client                 *cl;
144 
145           n = sizeof(un);
146           fd = accept(sock, (struct sockaddr *)&un, &n);
147           if (fd < 0) {
148                     syslog(LOG_ERR, "Could not accept PIN client connection");
149                     return;
150           }
151 
152           n = 1;
153           if (ioctl(fd, FIONBIO, &n) < 0) {
154                     syslog(LOG_ERR, "Could not set non blocking IO for client");
155                     close(fd);
156                     return;
157           }
158 
159           cl = malloc(sizeof(struct client));
160           if (cl == NULL) {
161                     syslog(LOG_ERR, "Could not malloc client");
162                     close(fd);
163                     return;
164           }
165 
166           memset(cl, 0, sizeof(struct client));
167           cl->fd = fd;
168 
169           event_set(&cl->ev, fd, EV_READ | EV_PERSIST, process_client, cl);
170           if (event_add(&cl->ev, NULL) < 0) {
171                     syslog(LOG_ERR, "Could not add client event");
172                     free(cl);
173                     close(fd);
174                     return;
175           }
176 
177           syslog(LOG_DEBUG, "New Client");
178           LIST_INSERT_HEAD(&client_list, cl, next);
179 }
180 
181 /* Process client response packet */
182 static void
process_client(int sock,short ev,void * arg)183 process_client(int sock, short ev, void *arg)
184 {
185           bthcid_pin_response_t          rp;
186           struct timeval                 tv;
187           struct sockaddr_bt   sa;
188           struct client                 *cl = arg;
189           struct item                   *item;
190           int                            n;
191 
192           n = recv(sock, &rp, sizeof(rp), 0);
193           if (n != sizeof(rp)) {
194                     if (n != 0)
195                               syslog(LOG_ERR, "Bad Client");
196 
197                     close(sock);
198                     LIST_REMOVE(cl, next);
199                     free(cl);
200 
201                     syslog(LOG_DEBUG, "Client Closed");
202                     return;
203           }
204 
205           syslog(LOG_DEBUG, "Received PIN for %s", bt_ntoa(&rp.raddr, NULL));
206 
207           LIST_FOREACH(item, &item_list, next) {
208                     if (bdaddr_same(&rp.laddr, &item->laddr) == 0
209                         || bdaddr_same(&rp.raddr, &item->raddr) == 0)
210                               continue;
211 
212                     evtimer_del(&item->ev);
213                     if (item->hci != -1) {
214                               memset(&sa, 0, sizeof(sa));
215                               sa.bt_len = sizeof(sa);
216                               sa.bt_family = AF_BLUETOOTH;
217                               bdaddr_copy(&sa.bt_bdaddr, &item->laddr);
218 
219                               send_pin_code_reply(item->hci, &sa, &item->raddr, rp.pin);
220                               LIST_REMOVE(item, next);
221                               free(item);
222                               return;
223                     }
224                     goto newpin;
225           }
226 
227           item = malloc(sizeof(struct item));
228           if (item == NULL) {
229                     syslog(LOG_ERR, "Item allocation failed");
230                     return;
231           }
232 
233           memset(item, 0, sizeof(struct item));
234           bdaddr_copy(&item->laddr, &rp.laddr);
235           bdaddr_copy(&item->raddr, &rp.raddr);
236           evtimer_set(&item->ev, process_item, item);
237           LIST_INSERT_HEAD(&item_list, item, next);
238 
239 newpin:
240           syslog(LOG_DEBUG, "Caching PIN for %s", bt_ntoa(&rp.raddr, NULL));
241 
242           memcpy(item->pin, rp.pin, HCI_PIN_SIZE);
243           item->hci = -1;
244 
245           tv.tv_sec = PIN_TIMEOUT;
246           tv.tv_usec = 0;
247 
248           if (evtimer_add(&item->ev, &tv) < 0) {
249                     syslog(LOG_ERR, "Cannot add event timer for item");
250                     LIST_REMOVE(item, next);
251                     free(item);
252           }
253 }
254 
255 /* Send PIN request to client */
256 int
send_client_request(bdaddr_t * laddr,bdaddr_t * raddr,int hci)257 send_client_request(bdaddr_t *laddr, bdaddr_t *raddr, int hci)
258 {
259           bthcid_pin_request_t           cp;
260           struct client                 *cl;
261           struct item                   *item;
262           int                            n = 0;
263           struct timeval                 tv;
264 
265           memset(&cp, 0, sizeof(cp));
266           bdaddr_copy(&cp.laddr, laddr);
267           bdaddr_copy(&cp.raddr, raddr);
268           cp.time = PIN_REQUEST_TIMEOUT;
269 
270           LIST_FOREACH(cl, &client_list, next) {
271                     if (send(cl->fd, &cp, sizeof(cp), MSG_NOSIGNAL) != sizeof(cp))
272                               syslog(LOG_ERR, "send PIN request failed");
273                     else
274                               n++;
275           }
276 
277           if (n == 0)
278                     return 0;
279 
280           syslog(LOG_DEBUG, "Sent PIN requests to %d client%s.",
281                                         n, (n == 1 ? "" : "s"));
282 
283           item = malloc(sizeof(struct item));
284           if (item == NULL) {
285                     syslog(LOG_ERR, "Cannot allocate PIN request item");
286                     return 0;
287           }
288 
289           memset(item, 0, sizeof(struct item));
290           bdaddr_copy(&item->laddr, laddr);
291           bdaddr_copy(&item->raddr, raddr);
292           item->hci = hci;
293           evtimer_set(&item->ev, process_item, item);
294 
295           tv.tv_sec = cp.time;
296           tv.tv_usec = 0;
297 
298           if (evtimer_add(&item->ev, &tv) < 0) {
299                     syslog(LOG_ERR, "Cannot add request timer");
300                     free(item);
301                     return 0;
302           }
303 
304           LIST_INSERT_HEAD(&item_list, item, next);
305           return 1;
306 }
307 
308 /* Process item event (by expiring it) */
309 static void
process_item(int fd,short ev,void * arg)310 process_item(int fd, short ev, void *arg)
311 {
312           struct item *item = arg;
313 
314           syslog(LOG_DEBUG, "PIN for %s expired", bt_ntoa(&item->raddr, NULL));
315           LIST_REMOVE(item, next);
316           evtimer_del(&item->ev);
317           free(item);
318 }
319 
320 /* lookup PIN in item cache */
321 uint8_t *
lookup_pin(bdaddr_t * laddr,bdaddr_t * raddr)322 lookup_pin(bdaddr_t *laddr, bdaddr_t *raddr)
323 {
324           static uint8_t pin[HCI_PIN_SIZE];
325           struct item *item;
326 
327           LIST_FOREACH(item, &item_list, next) {
328                     if (bdaddr_same(raddr, &item->raddr) == 0)
329                               continue;
330 
331                     if (bdaddr_same(laddr, &item->laddr) == 0
332                         && bdaddr_any(&item->laddr) == 0)
333                               continue;
334 
335                     if (item->hci >= 0)
336                               break;
337 
338                     syslog(LOG_DEBUG, "Matched PIN from cache");
339                     memcpy(pin, item->pin, sizeof(pin));
340 
341                     LIST_REMOVE(item, next);
342                     evtimer_del(&item->ev);
343                     free(item);
344 
345                     return pin;
346           }
347 
348           return NULL;
349 }
350