1 /* $NetBSD: btpin.c,v 1.7 2017/12/21 09:04:34 plunky 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.7 2017/12/21 09:04:34 plunky 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 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 136 strncpy((char *)rp.pin, pin, HCI_PIN_SIZE); 137 } 138 139 s = socket(PF_LOCAL, SOCK_STREAM, 0); 140 if (s == -1) 141 err(EXIT_FAILURE, "socket"); 142 143 if (connect(s, (struct sockaddr *)&un, sizeof(un)) == -1) 144 err(EXIT_FAILURE, "connect(\"%s\")", un.sun_path); 145 146 if (send(s, &rp, sizeof(rp), 0) != sizeof(rp)) 147 err(EXIT_FAILURE, "send"); 148 149 close(s); 150 151 if (pair == 0) 152 exit(EXIT_SUCCESS); 153 154 s = socket(PF_BLUETOOTH, SOCK_SEQPACKET, BTPROTO_L2CAP); 155 if (s == -1) 156 err(EXIT_FAILURE, "socket"); 157 158 ch = L2CAP_LM_AUTH; 159 if (setsockopt(s, BTPROTO_L2CAP, SO_L2CAP_LM, &ch, sizeof(ch)) == -1) 160 err(EXIT_FAILURE, "SO_L2CAP_LM"); 161 162 memset(&bt, 0, sizeof(bt)); 163 bt.bt_len = sizeof(bt); 164 bt.bt_family = AF_BLUETOOTH; 165 bdaddr_copy(&bt.bt_bdaddr, &rp.laddr); 166 if (bind(s, (struct sockaddr *)&bt, sizeof(bt)) == -1) 167 err(EXIT_FAILURE, "bind"); 168 169 fprintf(stdout, "Pairing.. "); 170 fflush(stdout); 171 172 bt.bt_psm = L2CAP_PSM_SDP; 173 bdaddr_copy(&bt.bt_bdaddr, &rp.raddr); 174 if (connect(s, (struct sockaddr *)&bt, sizeof(bt)) == -1) { 175 fprintf(stdout, "failed (%s)\n", strerror(errno)); 176 exit(EXIT_FAILURE); 177 } 178 179 close(s); 180 fprintf(stdout, "done\n"); 181 182 exit(EXIT_SUCCESS); 183 } 184 185 static void 186 usage(void) 187 { 188 189 fprintf(stderr, 190 "usage: %s [-P] [-d device] [-s socket] {-p pin | -r [-l len]} -a addr\n" 191 "", getprogname()); 192 193 exit(EXIT_FAILURE); 194 } 195