xref: /dragonfly/usr.sbin/bthcid/client.c (revision dca3c15d)
1 /* $NetBSD: client.c,v 1.4 2006/09/29 20:06:11 plunky Exp $ */
2 /* $DragonFly: src/usr.sbin/bthcid/client.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/ioctl.h>
34 #include <sys/queue.h>
35 #include <sys/stat.h>
36 #include <sys/time.h>
37 #include <sys/un.h>
38 #include <bluetooth.h>
39 #include <errno.h>
40 #include <event.h>
41 #include <fcntl.h>
42 #include <stdlib.h>
43 #include <string.h>
44 #include <syslog.h>
45 #include <unistd.h>
46 
47 #include "bthcid.h"
48 
49 /*
50  * A client is anybody who connects to our control socket to
51  * receive PIN requests.
52  */
53 struct client {
54 	struct event		ev;
55 	int			fd;		/* client descriptor */
56 	LIST_ENTRY(client)	next;
57 };
58 
59 /*
60  * PIN cache items are made when we have sent a client pin
61  * request. The event is used to expire the item.
62  */
63 struct item {
64 	struct event	 ev;
65 	bdaddr_t	 laddr;			/* local device BDADDR */
66 	bdaddr_t	 raddr;			/* remote device BDADDR */
67 	uint8_t		 pin[HCI_PIN_SIZE];	/* PIN */
68 	int		 hci;			/* HCI socket */
69 	LIST_ENTRY(item) next;
70 };
71 
72 static struct event		control_ev;
73 
74 static LIST_HEAD(,client)	client_list;
75 static LIST_HEAD(,item)		item_list;
76 
77 static void process_control	(int, short, void *);
78 static void process_client	(int, short, void *);
79 static void process_item	(int, short, void *);
80 
81 #define PIN_REQUEST_TIMEOUT	30	/* Request is valid */
82 #define PIN_TIMEOUT		300	/* PIN is valid */
83 
84 int
85 init_control(const char *name, mode_t mode)
86 {
87 	struct sockaddr_un	un;
88 	int			ctl;
89 
90 	LIST_INIT(&client_list);
91 	LIST_INIT(&item_list);
92 
93 	if (name == NULL)
94 		return 0;
95 
96 	if (unlink(name) < 0 && errno != ENOENT)
97 		return -1;
98 
99 	ctl = socket(PF_LOCAL, SOCK_STREAM, 0);
100 	if (ctl < 0)
101 		return -1;
102 
103 	memset(&un, 0, sizeof(un));
104 	un.sun_len = sizeof(un);
105 	un.sun_family = AF_LOCAL;
106 	strlcpy(un.sun_path, name, sizeof(un.sun_path));
107 	if (bind(ctl, (struct sockaddr *)&un, sizeof(un)) < 0) {
108 		close(ctl);
109 		return -1;
110 	}
111 
112 	if (chmod(name, mode) < 0) {
113 		close(ctl);
114 		unlink(name);
115 		return -1;
116 	}
117 
118 	if (listen(ctl, 10) < 0) {
119 		close(ctl);
120 		unlink(name);
121 		return -1;
122 	}
123 
124 	event_set(&control_ev, ctl, EV_READ | EV_PERSIST, process_control, NULL);
125 	if (event_add(&control_ev, NULL) < 0) {
126 		close(ctl);
127 		unlink(name);
128 		return -1;
129 	}
130 
131 	return 0;
132 }
133 
134 /* Process control socket event */
135 static void
136 process_control(int sock, short ev __unused, void *arg __unused)
137 {
138 	struct sockaddr_un	un;
139 	socklen_t		n;
140 	int			fd;
141 	struct client		*cl;
142 
143 	n = sizeof(un);
144 	fd = accept(sock, (struct sockaddr *)&un, &n);
145 	if (fd < 0) {
146 		syslog(LOG_ERR, "Could not accept PIN client connection");
147 		return;
148 	}
149 
150 	n = 1;
151 	if (ioctl(fd, FIONBIO, &n) < 0) {
152 		syslog(LOG_ERR, "Could not set non blocking IO for client");
153 		close(fd);
154 		return;
155 	}
156 
157 	cl = malloc(sizeof(struct client));
158 	if (cl == NULL) {
159 		syslog(LOG_ERR, "Could not malloc client");
160 		close(fd);
161 		return;
162 	}
163 
164 	memset(cl, 0, sizeof(struct client));
165 	cl->fd = fd;
166 
167 	event_set(&cl->ev, fd, EV_READ | EV_PERSIST, process_client, cl);
168 	if (event_add(&cl->ev, NULL) < 0) {
169 		syslog(LOG_ERR, "Could not add client event");
170 		free(cl);
171 		close(fd);
172 		return;
173 	}
174 
175 	syslog(LOG_DEBUG, "New Client");
176 	LIST_INSERT_HEAD(&client_list, cl, next);
177 }
178 
179 /* Process client response packet */
180 static void
181 process_client(int sock, short ev __unused, void *arg)
182 {
183 	bthcid_pin_response_t	 rp;
184 	struct timeval		 tv;
185 	struct sockaddr_bt	 sa;
186 	struct client		*cl = arg;
187 	struct item		*item;
188 	int			 n;
189 
190 	n = recv(sock, &rp, sizeof(rp), 0);
191 	if (n != sizeof(rp)) {
192 		if (n != 0)
193 			syslog(LOG_ERR, "Bad Client");
194 
195 		close(sock);
196 		LIST_REMOVE(cl, next);
197 		free(cl);
198 
199 		syslog(LOG_DEBUG, "Client Closed");
200 		return;
201 	}
202 
203 	syslog(LOG_DEBUG, "Received PIN for %s", bt_ntoa(&rp.raddr, NULL));
204 
205 	LIST_FOREACH(item, &item_list, next) {
206 		if (bdaddr_same(&rp.laddr, &item->laddr) == 0
207 		    || bdaddr_same(&rp.raddr, &item->raddr) == 0)
208 			continue;
209 
210 		evtimer_del(&item->ev);
211 		if (item->hci != -1) {
212 			memset(&sa, 0, sizeof(sa));
213 			sa.bt_len = sizeof(sa);
214 			sa.bt_family = AF_BLUETOOTH;
215 			bdaddr_copy(&sa.bt_bdaddr, &item->laddr);
216 
217 			send_pin_code_reply(item->hci, &sa, &item->raddr, rp.pin);
218 			LIST_REMOVE(item, next);
219 			free(item);
220 			return;
221 		}
222 		goto newpin;
223 	}
224 
225 	item = malloc(sizeof(struct item));
226 	if (item == NULL) {
227 		syslog(LOG_ERR, "Item allocation failed");
228 		return;
229 	}
230 
231 	memset(item, 0, sizeof(struct item));
232 	bdaddr_copy(&item->laddr, &rp.laddr);
233 	bdaddr_copy(&item->raddr, &rp.raddr);
234 	evtimer_set(&item->ev, process_item, item);
235 	LIST_INSERT_HEAD(&item_list, item, next);
236 
237 newpin:
238 	syslog(LOG_DEBUG, "Caching PIN for %s", bt_ntoa(&rp.raddr, NULL));
239 
240 	memcpy(item->pin, rp.pin, HCI_PIN_SIZE);
241 	item->hci = -1;
242 
243 	tv.tv_sec = PIN_TIMEOUT;
244 	tv.tv_usec = 0;
245 
246 	if (evtimer_add(&item->ev, &tv) < 0) {
247 		syslog(LOG_ERR, "Cannot add event timer for item");
248 		LIST_REMOVE(item, next);
249 		free(item);
250 	}
251 }
252 
253 /* Send PIN request to client */
254 int
255 send_client_request(bdaddr_t *laddr, bdaddr_t *raddr, int hci)
256 {
257 	bthcid_pin_request_t	 cp;
258 	struct client		*cl;
259 	struct item		*item;
260 	int			 n = 0;
261 	struct timeval		 tv;
262 
263 	memset(&cp, 0, sizeof(cp));
264 	bdaddr_copy(&cp.laddr, laddr);
265 	bdaddr_copy(&cp.raddr, raddr);
266 	cp.time = PIN_REQUEST_TIMEOUT;
267 
268 	LIST_FOREACH(cl, &client_list, next) {
269 		if (send(cl->fd, &cp, sizeof(cp), 0) != sizeof(cp))
270 			syslog(LOG_ERR, "send PIN request failed");
271 		else
272 			n++;
273 	}
274 
275 	if (n == 0)
276 		return 0;
277 
278 	syslog(LOG_DEBUG, "Sent PIN requests to %d client%s.",
279 				n, (n == 1 ? "" : "s"));
280 
281 	item = malloc(sizeof(struct item));
282 	if (item == NULL) {
283 		syslog(LOG_ERR, "Cannot allocate PIN request item");
284 		return 0;
285 	}
286 
287 	memset(item, 0, sizeof(struct item));
288 	bdaddr_copy(&item->laddr, laddr);
289 	bdaddr_copy(&item->raddr, raddr);
290 	item->hci = hci;
291 	evtimer_set(&item->ev, process_item, item);
292 
293 	tv.tv_sec = cp.time;
294 	tv.tv_usec = 0;
295 
296 	if (evtimer_add(&item->ev, &tv) < 0) {
297 		syslog(LOG_ERR, "Cannot add request timer");
298 		free(item);
299 		return 0;
300 	}
301 
302 	LIST_INSERT_HEAD(&item_list, item, next);
303 	return 1;
304 }
305 
306 /* Process item event (by expiring it) */
307 static void
308 process_item(int fd __unused, short ev __unused, void *arg)
309 {
310 	struct item *item = arg;
311 
312 	syslog(LOG_DEBUG, "PIN for %s expired", bt_ntoa(&item->raddr, NULL));
313 	LIST_REMOVE(item, next);
314 	evtimer_del(&item->ev);
315 	free(item);
316 }
317 
318 /* lookup PIN in item cache */
319 uint8_t *
320 lookup_pin(bdaddr_t *laddr, bdaddr_t *raddr)
321 {
322 	static uint8_t pin[HCI_PIN_SIZE];
323 	struct item *item;
324 
325 	LIST_FOREACH(item, &item_list, next) {
326 		if (bdaddr_same(raddr, &item->raddr) == 0)
327 			continue;
328 
329 		if (bdaddr_same(laddr, &item->laddr) == 0
330 		    && bdaddr_any(&item->laddr) == 0)
331 			continue;
332 
333 		if (item->hci >= 0)
334 			break;
335 
336 		syslog(LOG_DEBUG, "Matched PIN from cache");
337 		memcpy(pin, item->pin, sizeof(pin));
338 
339 		LIST_REMOVE(item, next);
340 		evtimer_del(&item->ev);
341 		free(item);
342 
343 		return pin;
344 	}
345 
346 	return NULL;
347 }
348