| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 290
 291
 292
 293
 294
 295
 296
 297
 298
 299
 300
 301
 302
 303
 304
 305
 306
 307
 308
 309
 310
 311
 312
 313
 314
 315
 316
 317
 318
 319
 320
 321
 322
 323
 324
 325
 326
 327
 328
 329
 330
 331
 332
 333
 334
 335
 336
 337
 338
 339
 340
 341
 342
 343
 344
 345
 346
 347
 
 | /*
 * btscanner - Displays the output of Bluetooth scans
 * Copyright (C) 2003 Pentest Limited
 * 
 * Written 2003 by Tim Hurman <timh at pentest.co.uk>
 * 
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation;
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY
 * RIGHTS.  IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) AND AUTHOR(S) BE LIABLE
 * FOR ANY CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY
 * DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
 * AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 * 
 * ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS,
 * COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS SOFTWARE
 * IS DISCLAIMED.
 */
/*
 * btscanner.c: keep a bluetooth card scanning the network to find devices
 * that are discoverable. Heavily based on hcitool.c
 */
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#ifdef HAVE_SYS_TYPES_H
#include <sys/types.h>
#endif
#include <stdlib.h>
#include <pthread.h>
#include <unistd.h>
#include <stdio.h>
#include <signal.h>
#include <errno.h>
#include <getopt.h>
#include <ncurses.h>
#ifdef HAVE_SYS_IOCTL_H
#include <sys/ioctl.h>
#endif
#ifdef HAVE_SYSLOG_H
#include <sys/syslog.h>
#endif
#ifdef HAVE_STRING_H
#include <string.h>
#endif
#ifdef HAVE_SYS_SOCKET_H
#include <sys/socket.h>
#endif
#ifdef HAVE_NETINET_IN_H
#include <netinet/in.h>
#endif
#include <bluetooth/bluetooth.h>
#include <bluetooth/hci.h>
#include <bluetooth/hci_lib.h>
#include <bluetooth/sdp.h>
#include <bluetooth/sdp_lib.h>
#include <main.h>
#include <cfg.h>
#include <screen.h>
#include <misc.h>
#include <scan.h>
#include <log.h>
#include <ll.h>
#include <oui.h>
/* globals */
hcicfg_t *hcihead = NULL;
char bts_run = 1;
char bts_run_scan = 1;
int bts_reset=1;
/* signal handlers */
/* handle a signal */
static RETSIGTYPE handlesig (int sig)
{
	sig=sig;
	bts_run = 0;
	bts_run_scan = 0;
}
/* just interrupt */
static RETSIGTYPE dointerrupt(int sig)
{
	/* do nothing, just interrupt */
	sig=sig;
}
/* usage */
void usage (char *pname) {
	fprintf(stderr, "Usage: %s [options]\n", pname);
	fprintf(stderr, "options\n\t--help\tDisplay help\n");
	fprintf(stderr, "\t--cfg=<file>\tUse <file> as the config file\n");
	fprintf(stderr, "\t--no-reset\tDo not reset the Bluetooth adapter before scanning\n");
}
/* command line options */
static struct option main_options[] = {
	{"help", 0,0, 'h'},
	{"cfg", 1,0, 'c'},
	{"no-reset", 0, &bts_reset, 0},
	{0, 0, 0, 0}
};
/* free the hci device list */
void hci_devices_free(hcicfg_t *h)
{
	if (h->next) hci_devices_free(h->next);
	free(h);
}
/* append to the hci list */
void  hci_devices_append(hcicfg_t *h, hcicfg_t *n)
{
	if (!hcihead) {
		hcihead = n;
		return;
	}
	if (h->next)
		hci_devices_append(h->next, n);
	else
		h->next = n;
	return;
}
/* get the max num of devices */
int get_max_devices(int s)
{
	struct hci_dev_list_req *dl = NULL;
	struct hci_dev_req *dr = NULL;
	struct hci_dev_info di;
	int i, ret, up;
	hcicfg_t *hci;
	ret = 1;
	dl = (struct hci_dev_list_req*)malloc
	  (HCI_MAX_DEV * sizeof(struct hci_dev_req) + sizeof(uint16_t));
	if(!dl) {
		applog(LOG_ERR,
		  "%s::malloc(): %s", __FUNCTION__, strerror(errno));
		goto get_max_devices_leave;
	}
	memset(dl, 0, HCI_MAX_DEV * sizeof(struct hci_dev_req) + sizeof(uint16_t));
	dl->dev_num = HCI_MAX_DEV;
	dr = dl->dev_req;
	/* get the device list */
	if( ioctl(s, HCIGETDEVLIST, (void*)dl) ) {
		applog(LOG_ERR,
		  "%s::ioctl(): %s", __FUNCTION__, strerror(errno));
		goto get_max_devices_leave;
	}
	ret = 0;
	/* enumerate the devices */
	for(up=i=0; i< dl->dev_num; i++, dr++) {
		di.dev_id = dr->dev_id;
		/* get the device info */
		if( ioctl(s, HCIGETDEVINFO, (void*)&di) )
			continue;
		/* make a new hci device */
		hci = (hcicfg_t*)malloc(sizeof(hcicfg_t));
		if (!hci) {
			ret = 1;
			applog(LOG_ERR,
			  "%s::malloc(): %s", __FUNCTION__, strerror(errno));
			break;
		}
		memset(hci, 0, sizeof(hcicfg_t));
		hci->id = di.dev_id;
		bacpy(&(hci->bdaddr), &(di.bdaddr));
		/* is the device enabled? */
		if (hci_test_bit(HCI_UP, &dr->dev_opt)) {
			hci->enabled = HCI_ENABLED;
			up++;
		} else {
			hci->enabled = HCI_DISABLED;
		}
		/* save */
		hci_devices_append(hcihead, hci);
	}
	if (0 == up) {
		applog(LOG_ERR,
		  "%s(): no valid HCI devices were found", __FUNCTION__);
		ret = 1;
	}
	get_max_devices_leave:
	if(dl) free(dl);
	return ret;
}
#define MAX_DEVICES 128
/* main */
int main (int argc, char **argv)
{
	int opt, ctl;
	struct sigaction act;
	sigset_t sset;
	char *cfg_filename = NULL;
/*
	printf("gdb ./btscanner %d\n", getpid());
	sleep(5);
*/
	/* get the args */
	opterr = 0;
	while ((opt=getopt_long(argc, argv, "+hc:r", main_options, NULL)) != -1)
		switch(opt) {
		case 'c':
			cfg_filename = optarg;
			break;
		case 'h':
			opterr++;
			break;
		}
	if (opterr) {
		usage(argv[0]);
		exit(0);
	}
	/* intialise the config options */
	if(cfg_parsefile(cfg_filename)) {
		fprintf(stderr, "Config file validation error\n");
		exit(1);
	}
	/* startup the logging */
	if(log_init()) {
		fprintf(stderr, "Logging failed to initialise\n");
		exit(1);
	}
	/* initialise the signals */
	memset (&sset, 0, sizeof(sset));
	sigfillset(&sset);
	sigdelset(&sset, SIGKILL);
	sigdelset(&sset, SIGSTOP);
	sigdelset(&sset, SIGTERM);
	sigdelset(&sset, SIGINT);
	sigdelset(&sset, SIGSEGV);
	sigdelset(&sset, SIGWINCH); /* need for wresize */
	if (-1 == sigprocmask(SIG_SETMASK, &sset, NULL)) {
		applog(LOG_ERR,
		  "%s::sigprocmask(): %s", __FUNCTION__, strerror(errno));
		exit(0);
	}
	if (0 != pthread_sigmask(SIG_SETMASK, &sset, NULL)) {
		applog(LOG_ERR,
		  "%s::sigprocmask(): %s", __FUNCTION__, strerror(errno));
		exit(0);
	}
	/* initialise the signal handler */
	memset(&act, 0, sizeof(act));
	act.sa_handler = handlesig;
	sigfillset(&(act.sa_mask));
	sigdelset(&(act.sa_mask), SIGKILL);
	sigdelset(&(act.sa_mask), SIGSTOP);
	if (-1 == sigaction(SIGTERM, &act, NULL)) {
		applog(LOG_ERR,
		  "%s::sigaction(): %s", __FUNCTION__, strerror(errno));
		exit(0);
	}
	if (-1 == sigaction(SIGINT, &act, NULL)) {
		applog(LOG_ERR,
		  "%s::sigaction(): %s", __FUNCTION__, strerror(errno));
		exit(0);
	}
	act.sa_handler = dointerrupt;
	if (-1 == sigaction(SIGUSR1, &act, NULL)) {
		applog(LOG_ERR,
		  "%s::sigaction(): %s", __FUNCTION__, strerror(errno));
		exit(0);
	}
	/* initialise the oui db */
	if (ouidb_init(cfg_oui_filename()))
		exit(0);
	/* initialise the linked list functions */
	ll_init();
	/* firstly open a raw socket */
	if ((ctl = socket(AF_BLUETOOTH, SOCK_RAW, BTPROTO_HCI)) < 0) {
		applog(LOG_ERR,
		  "%s::socket(): %s", __FUNCTION__, strerror(errno));
		exit(0);
    }
	/* get a list of available hci devices */
	hcihead = NULL;
	if(get_max_devices(ctl)) {
		fprintf(stderr, "No Bluetooth devices available\n");
		goto btscanner_leave;
	}
	/* start the screen runner */
	screen_on();
	/* run the main screen */
	screen_run();
	/* goodbye */
	screen_off();
	btscanner_leave:
	if (hcihead) hci_devices_free(hcihead);
	if (ctl != -1) {
		shutdown(ctl, 2);
		close(ctl);
	}
	/* tidy up */
	ouidb_close();
	ll_free();
	log_close();
	cfg_cleanup();
	exit(0);
}
 |