Home | History | Annotate | Line # | Download | only in btpin
btpin.c revision 1.5
      1 /*	$NetBSD: btpin.c,v 1.5 2009/05/16 07:18:42 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.5 2009/05/16 07:18:42 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 int  main(int, char *[]);
     49 void usage(void);
     50 
     51 int
     52 main(int ac, char *av[])
     53 {
     54 	bthcid_pin_response_t rp;
     55 	struct sockaddr_un un;
     56 	struct sockaddr_bt bt;
     57 	char *pin = NULL;
     58 	int ch, s, len, pair;
     59 
     60 	memset(&rp, 0, sizeof(rp));
     61 	len = -1;
     62 	pair = 0;
     63 
     64 	memset(&un, 0, sizeof(un));
     65 	un.sun_len = sizeof(un);
     66 	un.sun_family = AF_LOCAL;
     67 	strlcpy(un.sun_path, BTHCID_SOCKET_NAME, sizeof(un.sun_path));
     68 
     69 	while ((ch = getopt(ac, av, "a:d:l:Pp:rs:")) != -1) {
     70 		switch (ch) {
     71 		case 'a':
     72 			if (!bt_aton(optarg, &rp.raddr)) {
     73 				struct hostent  *he = NULL;
     74 
     75 				if ((he = bt_gethostbyname(optarg)) == NULL)
     76 					errx(EXIT_FAILURE, "%s: %s", optarg,
     77 							hstrerror(h_errno));
     78 
     79 				bdaddr_copy(&rp.raddr, (bdaddr_t *)he->h_addr);
     80 			}
     81 			break;
     82 
     83 		case 'd':
     84 			if (!bt_devaddr(optarg, &rp.laddr))
     85 				err(EXIT_FAILURE, "%s", optarg);
     86 
     87 			break;
     88 
     89 		case 'l':
     90 			len = atoi(optarg);
     91 			if (len < 1 || len > HCI_PIN_SIZE)
     92 				errx(EXIT_FAILURE, "Invalid PIN length");
     93 
     94 			break;
     95 
     96 		case 'P':
     97 			pair++;
     98 			break;
     99 
    100 		case 'p':
    101 			pin = optarg;
    102 			break;
    103 
    104 		case 'r':
    105 			if (len == -1)
    106 				len = 4;
    107 
    108 			break;
    109 
    110 		case 's':
    111 			strlcpy(un.sun_path, optarg, sizeof(un.sun_path));
    112 			break;
    113 
    114 		default:
    115 			usage();
    116 		}
    117 	}
    118 
    119 	if (bdaddr_any(&rp.raddr))
    120 		usage();
    121 
    122 	if (pin == NULL) {
    123 		if (len == -1)
    124 			usage();
    125 
    126 		srandom(time(NULL));
    127 
    128 		pin = (char *)rp.pin;
    129 		while (len-- > 0)
    130 			*pin++ = '0' + (random() % 10);
    131 
    132 		printf("PIN: %.*s\n", HCI_PIN_SIZE, rp.pin);
    133 	} else {
    134 		if (len != -1)
    135 			usage();
    136 
    137 		strncpy((char *)rp.pin, pin, HCI_PIN_SIZE);
    138 	}
    139 
    140 	s = socket(PF_LOCAL, SOCK_STREAM, 0);
    141 	if (s < 0)
    142 		err(EXIT_FAILURE, "socket");
    143 
    144 	if (connect(s, (struct sockaddr *)&un, sizeof(un)) < 0)
    145 		err(EXIT_FAILURE, "connect(\"%s\")", un.sun_path);
    146 
    147 	if (send(s, &rp, sizeof(rp), 0) != sizeof(rp))
    148 		err(EXIT_FAILURE, "send");
    149 
    150 	close(s);
    151 
    152 	if (pair == 0)
    153 		exit(EXIT_SUCCESS);
    154 
    155 	s = socket(PF_BLUETOOTH, SOCK_SEQPACKET, BTPROTO_L2CAP);
    156 	if (s < 0)
    157 		err(EXIT_FAILURE, "socket");
    158 
    159 	ch = L2CAP_LM_AUTH;
    160 	if (setsockopt(s, BTPROTO_L2CAP, SO_L2CAP_LM, &ch, sizeof(ch)) < 0)
    161 		err(EXIT_FAILURE, "SO_L2CAP_LM");
    162 
    163 	memset(&bt, 0, sizeof(bt));
    164 	bt.bt_len = sizeof(bt);
    165 	bt.bt_family = AF_BLUETOOTH;
    166 	bdaddr_copy(&bt.bt_bdaddr, &rp.laddr);
    167 	if (bind(s, (struct sockaddr *)&bt, sizeof(bt)) < 0)
    168 		err(EXIT_FAILURE, "bind");
    169 
    170 	fprintf(stdout, "Pairing.. ");
    171 	fflush(stdout);
    172 
    173 	bt.bt_psm = L2CAP_PSM_SDP;
    174 	bdaddr_copy(&bt.bt_bdaddr, &rp.raddr);
    175 	if (connect(s, (struct sockaddr *)&bt, sizeof(bt)) < 0) {
    176 		fprintf(stdout, "failed (%s)\n", strerror(errno));
    177 		exit(EXIT_FAILURE);
    178 	}
    179 
    180 	close(s);
    181 	fprintf(stdout, "done\n");
    182 
    183 	exit(EXIT_SUCCESS);
    184 }
    185 
    186 void
    187 usage(void)
    188 {
    189 
    190 	fprintf(stderr,
    191 		"usage: %s [-P] [-d device] [-s socket] {-p pin | -r [-l len]} -a addr\n"
    192 		"", getprogname());
    193 
    194 	exit(EXIT_FAILURE);
    195 }
    196