xref: /netbsd-src/usr.bin/btpin/btpin.c (revision 706110a22668fd8934cb2f868bc932cfb5446b6c)
1*706110a2Schristos /*	$NetBSD: btpin.c,v 1.8 2019/10/12 17:50:56 christos Exp $	*/
2a5c89047Sgdamore 
3a5c89047Sgdamore /*-
4a5c89047Sgdamore  * Copyright (c) 2006 Itronix Inc.
5a5c89047Sgdamore  * All rights reserved.
6a5c89047Sgdamore  *
7a5c89047Sgdamore  * Written by Iain Hibbert for Itronix Inc.
8a5c89047Sgdamore  *
9a5c89047Sgdamore  * Redistribution and use in source and binary forms, with or without
10a5c89047Sgdamore  * modification, are permitted provided that the following conditions
11a5c89047Sgdamore  * are met:
12a5c89047Sgdamore  * 1. Redistributions of source code must retain the above copyright
13a5c89047Sgdamore  *    notice, this list of conditions and the following disclaimer.
14a5c89047Sgdamore  * 2. Redistributions in binary form must reproduce the above copyright
15a5c89047Sgdamore  *    notice, this list of conditions and the following disclaimer in the
16a5c89047Sgdamore  *    documentation and/or other materials provided with the distribution.
17a5c89047Sgdamore  * 3. The name of Itronix Inc. may not be used to endorse
18a5c89047Sgdamore  *    or promote products derived from this software without specific
19a5c89047Sgdamore  *    prior written permission.
20a5c89047Sgdamore  *
21a5c89047Sgdamore  * THIS SOFTWARE IS PROVIDED BY ITRONIX INC. ``AS IS'' AND
22a5c89047Sgdamore  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
23a5c89047Sgdamore  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24a5c89047Sgdamore  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL ITRONIX INC. BE LIABLE FOR ANY
25a5c89047Sgdamore  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
26a5c89047Sgdamore  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27a5c89047Sgdamore  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
28a5c89047Sgdamore  * ON ANY THEORY OF LIABILITY, WHETHER IN
29a5c89047Sgdamore  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30a5c89047Sgdamore  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31a5c89047Sgdamore  * POSSIBILITY OF SUCH DAMAGE.
32a5c89047Sgdamore  */
33a5c89047Sgdamore 
34a5c89047Sgdamore #include <sys/cdefs.h>
3598e5374cSlukem __COPYRIGHT("@(#) Copyright (c) 2006 Itronix, Inc.  All rights reserved.");
36*706110a2Schristos __RCSID("$NetBSD: btpin.c,v 1.8 2019/10/12 17:50:56 christos Exp $");
37a5c89047Sgdamore 
38a5c89047Sgdamore #include <sys/types.h>
39a5c89047Sgdamore #include <sys/un.h>
40a5c89047Sgdamore #include <bluetooth.h>
41a5c89047Sgdamore #include <err.h>
42bbb0e93aSplunky #include <errno.h>
43a5c89047Sgdamore #include <stdlib.h>
44a5c89047Sgdamore #include <string.h>
45a5c89047Sgdamore #include <time.h>
46a5c89047Sgdamore #include <unistd.h>
47a5c89047Sgdamore 
48a7463334Sjoerg __dead static void usage(void);
49a5c89047Sgdamore 
50a5c89047Sgdamore int
main(int ac,char * av[])51a5c89047Sgdamore main(int ac, char *av[])
52a5c89047Sgdamore {
530b73a6ecSplunky 	bthcid_pin_response_t rp;
54a5c89047Sgdamore 	struct sockaddr_un un;
55bbb0e93aSplunky 	struct sockaddr_bt bt;
56a5c89047Sgdamore 	char *pin = NULL;
57bbb0e93aSplunky 	int ch, s, len, pair;
58a5c89047Sgdamore 
59a5c89047Sgdamore 	memset(&rp, 0, sizeof(rp));
60a5c89047Sgdamore 	len = -1;
61bbb0e93aSplunky 	pair = 0;
62a5c89047Sgdamore 
63a5c89047Sgdamore 	memset(&un, 0, sizeof(un));
64a5c89047Sgdamore 	un.sun_len = sizeof(un);
65a5c89047Sgdamore 	un.sun_family = AF_LOCAL;
66a5c89047Sgdamore 	strlcpy(un.sun_path, BTHCID_SOCKET_NAME, sizeof(un.sun_path));
67a5c89047Sgdamore 
68bbb0e93aSplunky 	while ((ch = getopt(ac, av, "a:d:l:Pp:rs:")) != -1) {
69a5c89047Sgdamore 		switch (ch) {
70a5c89047Sgdamore 		case 'a':
71a5c89047Sgdamore 			if (!bt_aton(optarg, &rp.raddr)) {
72a5c89047Sgdamore 				struct hostent  *he = NULL;
73a5c89047Sgdamore 
74a5c89047Sgdamore 				if ((he = bt_gethostbyname(optarg)) == NULL)
75a5c89047Sgdamore 					errx(EXIT_FAILURE, "%s: %s", optarg,
76a5c89047Sgdamore 							hstrerror(h_errno));
77a5c89047Sgdamore 
78a5c89047Sgdamore 				bdaddr_copy(&rp.raddr, (bdaddr_t *)he->h_addr);
79a5c89047Sgdamore 			}
80a5c89047Sgdamore 			break;
81a5c89047Sgdamore 
82a5c89047Sgdamore 		case 'd':
83a5c89047Sgdamore 			if (!bt_devaddr(optarg, &rp.laddr))
84a5c89047Sgdamore 				err(EXIT_FAILURE, "%s", optarg);
85a5c89047Sgdamore 
86a5c89047Sgdamore 			break;
87a5c89047Sgdamore 
88a5c89047Sgdamore 		case 'l':
89a5c89047Sgdamore 			len = atoi(optarg);
90a5c89047Sgdamore 			if (len < 1 || len > HCI_PIN_SIZE)
91a5c89047Sgdamore 				errx(EXIT_FAILURE, "Invalid PIN length");
92a5c89047Sgdamore 
93a5c89047Sgdamore 			break;
94a5c89047Sgdamore 
95bbb0e93aSplunky 		case 'P':
96bbb0e93aSplunky 			pair++;
97bbb0e93aSplunky 			break;
98bbb0e93aSplunky 
99a5c89047Sgdamore 		case 'p':
100a5c89047Sgdamore 			pin = optarg;
101a5c89047Sgdamore 			break;
102a5c89047Sgdamore 
103a5c89047Sgdamore 		case 'r':
104a5c89047Sgdamore 			if (len == -1)
105a5c89047Sgdamore 				len = 4;
106a5c89047Sgdamore 
107a5c89047Sgdamore 			break;
108a5c89047Sgdamore 
109a5c89047Sgdamore 		case 's':
110a5c89047Sgdamore 			strlcpy(un.sun_path, optarg, sizeof(un.sun_path));
111a5c89047Sgdamore 			break;
112a5c89047Sgdamore 
113a5c89047Sgdamore 		default:
114a5c89047Sgdamore 			usage();
115a5c89047Sgdamore 		}
116a5c89047Sgdamore 	}
117a5c89047Sgdamore 
118a5c89047Sgdamore 	if (bdaddr_any(&rp.raddr))
119a5c89047Sgdamore 		usage();
120a5c89047Sgdamore 
121a5c89047Sgdamore 	if (pin == NULL) {
122a5c89047Sgdamore 		if (len == -1)
123a5c89047Sgdamore 			usage();
124a5c89047Sgdamore 
125a5c89047Sgdamore 		srandom(time(NULL));
126a5c89047Sgdamore 
127a5c89047Sgdamore 		pin = (char *)rp.pin;
128a5c89047Sgdamore 		while (len-- > 0)
129a5c89047Sgdamore 			*pin++ = '0' + (random() % 10);
130a5c89047Sgdamore 
131a5c89047Sgdamore 		printf("PIN: %.*s\n", HCI_PIN_SIZE, rp.pin);
132a5c89047Sgdamore 	} else {
133a5c89047Sgdamore 		if (len != -1)
134a5c89047Sgdamore 			usage();
135*706110a2Schristos 		len = strlen(pin);
136*706110a2Schristos 		if (len > HCI_PIN_SIZE)
137*706110a2Schristos 			len = HCI_PIN_SIZE;
138a5c89047Sgdamore 
139*706110a2Schristos 		memcpy(rp.pin, pin, len);
140a5c89047Sgdamore 	}
141a5c89047Sgdamore 
142a5c89047Sgdamore 	s = socket(PF_LOCAL, SOCK_STREAM, 0);
143666f6884Splunky 	if (s == -1)
144a5c89047Sgdamore 		err(EXIT_FAILURE, "socket");
145a5c89047Sgdamore 
146666f6884Splunky 	if (connect(s, (struct sockaddr *)&un, sizeof(un)) == -1)
147a5c89047Sgdamore 		err(EXIT_FAILURE, "connect(\"%s\")", un.sun_path);
148a5c89047Sgdamore 
149a5c89047Sgdamore 	if (send(s, &rp, sizeof(rp), 0) != sizeof(rp))
150a5c89047Sgdamore 		err(EXIT_FAILURE, "send");
151a5c89047Sgdamore 
152a5c89047Sgdamore 	close(s);
153bbb0e93aSplunky 
154bbb0e93aSplunky 	if (pair == 0)
155bbb0e93aSplunky 		exit(EXIT_SUCCESS);
156bbb0e93aSplunky 
157bbb0e93aSplunky 	s = socket(PF_BLUETOOTH, SOCK_SEQPACKET, BTPROTO_L2CAP);
158666f6884Splunky 	if (s == -1)
159bbb0e93aSplunky 		err(EXIT_FAILURE, "socket");
160bbb0e93aSplunky 
161bbb0e93aSplunky 	ch = L2CAP_LM_AUTH;
162666f6884Splunky 	if (setsockopt(s, BTPROTO_L2CAP, SO_L2CAP_LM, &ch, sizeof(ch)) == -1)
163bbb0e93aSplunky 		err(EXIT_FAILURE, "SO_L2CAP_LM");
164bbb0e93aSplunky 
165bbb0e93aSplunky 	memset(&bt, 0, sizeof(bt));
166bbb0e93aSplunky 	bt.bt_len = sizeof(bt);
167bbb0e93aSplunky 	bt.bt_family = AF_BLUETOOTH;
168bbb0e93aSplunky 	bdaddr_copy(&bt.bt_bdaddr, &rp.laddr);
169666f6884Splunky 	if (bind(s, (struct sockaddr *)&bt, sizeof(bt)) == -1)
170bbb0e93aSplunky 		err(EXIT_FAILURE, "bind");
171bbb0e93aSplunky 
172bbb0e93aSplunky 	fprintf(stdout, "Pairing.. ");
173bbb0e93aSplunky 	fflush(stdout);
174bbb0e93aSplunky 
175bbb0e93aSplunky 	bt.bt_psm = L2CAP_PSM_SDP;
176bbb0e93aSplunky 	bdaddr_copy(&bt.bt_bdaddr, &rp.raddr);
177666f6884Splunky 	if (connect(s, (struct sockaddr *)&bt, sizeof(bt)) == -1) {
178bbb0e93aSplunky 		fprintf(stdout, "failed (%s)\n", strerror(errno));
179bbb0e93aSplunky 		exit(EXIT_FAILURE);
180bbb0e93aSplunky 	}
181bbb0e93aSplunky 
182bbb0e93aSplunky 	close(s);
183bbb0e93aSplunky 	fprintf(stdout, "done\n");
184bbb0e93aSplunky 
185a5c89047Sgdamore 	exit(EXIT_SUCCESS);
186a5c89047Sgdamore }
187a5c89047Sgdamore 
188a7463334Sjoerg static void
usage(void)189a5c89047Sgdamore usage(void)
190a5c89047Sgdamore {
191a5c89047Sgdamore 
192a5c89047Sgdamore 	fprintf(stderr,
193bbb0e93aSplunky 		"usage: %s [-P] [-d device] [-s socket] {-p pin | -r [-l len]} -a addr\n"
194a5c89047Sgdamore 		"", getprogname());
195a5c89047Sgdamore 
196a5c89047Sgdamore 	exit(EXIT_FAILURE);
197a5c89047Sgdamore }
198