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