public inbox for linux-bluetooth@vger.kernel.org
 help / color / mirror / Atom feed
* [Bluez-devel] [PATCH] randomizer for passkey-agent
@ 2007-10-23 13:07 eGore
  2007-10-23 14:30 ` eGore
  2007-10-25 22:52 ` Marcel Holtmann
  0 siblings, 2 replies; 6+ messages in thread
From: eGore @ 2007-10-23 13:07 UTC (permalink / raw)
  To: BlueZ development

[-- Attachment #1: Type: text/plain, Size: 361 bytes --]

Hi list,

I wanted a passkey-agent that does not always use the same passkey so it can run in daemon mode and provide passkeys. This is an attempt to implement this functionality.

Please review and commit.

Thanks in advance,
  Christoph Brill
-- 
Psssst! Schon vom neuen GMX MultiMessenger gehört?
Der kanns mit allen: http://www.gmx.net/de/go/multimessenger

[-- Attachment #2: bluez-passkey-random.patch --]
[-- Type: text/x-patch, Size: 4190 bytes --]

--- daemon/passkey-agent.c.org	2007-01-25 16:09:25.000000000 +0100
+++ daemon/passkey-agent.c	2007-10-23 15:02:48.000000000 +0200
@@ -32,6 +32,7 @@
 #include <signal.h>
 #include <getopt.h>
 #include <string.h>
+#include <time.h>
 
 #include <dbus/dbus.h>
 
@@ -39,6 +40,9 @@
 
 static char *passkey = NULL;
 static char *address = NULL;
+int use_random = 0;
+int use_default = 0;
+int key_length = 4;
 
 static volatile sig_atomic_t __io_canceled = 0;
 static volatile sig_atomic_t __io_terminated = 0;
@@ -48,6 +52,16 @@
 	__io_canceled = 1;
 }
 
+static char * next_key() {
+	int i;
+	char * key = malloc((key_length + 1) * sizeof(char));
+	for (i = 0; i < key_length; i++) {
+		key[i] = 48+(rand() %10);
+	}
+	key[key_length] = '\0';
+	return key;
+}
+
 static DBusHandlerResult agent_filter(DBusConnection *conn,
 						DBusMessage *msg, void *data)
 {
@@ -78,6 +92,12 @@
 	const char *path, *address;
 	dbus_bool_t numeric;
 
+	if (use_random) {
+		if (passkey)
+			free(passkey);
+		passkey = next_key();
+	}
+
 	if (!passkey)
 		return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
 
@@ -150,7 +170,7 @@
 	if (!dbus_message_get_args(msg, NULL,
 			DBUS_TYPE_STRING, &path, DBUS_TYPE_STRING, &address,
 							DBUS_TYPE_INVALID)) {
-		fprintf(stderr, "Invalid arguments for passkey Confirm method");
+		fprintf(stderr, "Invalid arguments for passkey Cancel method");
 		return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
 	}
 
@@ -224,7 +244,7 @@
 };
 
 static int register_agent(DBusConnection *conn, const char *agent_path,
-				const char *remote_address, int use_default)
+				const char *remote_address)
 {
 	DBusMessage *msg, *reply;
 	DBusError err;
@@ -280,7 +300,7 @@
 }
 
 static int unregister_agent(DBusConnection *conn, const char *agent_path,
-				const char *remote_address, int use_default)
+				const char *remote_address)
 {
 	DBusMessage *msg, *reply;
 	DBusError err;
@@ -334,17 +354,23 @@
 
 static void usage(void)
 {
+#ifdef HAVE_CONFIG_H
 	printf("Bluetooth passkey agent ver %s\n\n", VERSION);
+#else
+	printf("Bluetooth passkey agent\n\n");
+#endif
 
 	printf("Usage:\n"
-		"\tpasskey-agent [--default] [--path agent-path] <passkey> [address]\n"
+		"\tpasskey-agent [--default] [--path agent-path] [--random] [--length keylength] <passkey> [address]\n"
 		"\n");
 }
 
 static struct option main_options[] = {
-	{ "default",	0, 0, 'd' },
-	{ "path",	1, 0, 'p' },
-	{ "help",	0, 0, 'h' },
+	{ "default", no_argument,       &use_default, 1   },
+	{ "path",    required_argument, 0,            'p' },
+	{ "help",    no_argument,       0,            'h' },
+	{ "random",  no_argument,       &use_random,  1   },
+	{ "length",  required_argument, 0,            'l' },
 	{ 0, 0, 0, 0 }
 };
 
@@ -353,12 +379,14 @@
 	struct sigaction sa;
 	DBusConnection *conn;
 	char match_string[128], default_path[128], *agent_path = NULL;
-	int opt, use_default = 0;
+	int opt = 0;
+
+	srand(time(NULL));
 
 	snprintf(default_path, sizeof(default_path),
 				"/org/bluez/passkey_agent_%d", getpid());
 
-	while ((opt = getopt_long(argc, argv, "+dp:h", main_options, NULL)) != EOF) {
+	while ((opt = getopt_long(argc, argv, "+dp:hrl:", main_options, NULL)) != EOF) {
 		switch(opt) {
 		case 'd':
 			use_default = 1;
@@ -373,8 +401,15 @@
 		case 'h':
 			usage();
 			exit(0);
+		case 'r':
+			use_random = 1;
+			break;
+		case 'l':
+			key_length = atoi(optarg);
+			use_random = 1;
+			break;
 		default:
-			exit(1);
+			abort ();
 		}
 	}
 
@@ -382,12 +417,17 @@
 	argv += optind;
 	optind = 0;
 
-	if (argc < 1) {
+	if (argc < 1 && !use_random) {
 		usage();
 		exit(1);
 	}
 
-	passkey = strdup(argv[0]);
+	if (!use_random) {
+		passkey = strdup(argv[0]);
+	} else {
+		passkey = next_key();
+	}
+
 	address = (argc > 1) ? strdup(argv[1]) : NULL;
 
 	if (!use_default && !address) {
@@ -404,7 +444,7 @@
 		exit(1);
 	}
 
-	if (register_agent(conn, agent_path, address, use_default) < 0) {
+	if (register_agent(conn, agent_path, address) < 0) {
 		dbus_connection_unref(conn);
 		exit(1);
 	}
@@ -430,7 +470,7 @@
 	}
 
 	if (!__io_terminated)
-		unregister_agent(conn, agent_path, address, use_default);
+		unregister_agent(conn, agent_path, address);
 
 	if (passkey)
 		free(passkey);

[-- Attachment #3: Type: text/plain, Size: 314 bytes --]

-------------------------------------------------------------------------
This SF.net email is sponsored by: Splunk Inc.
Still grepping through log files to find problems?  Stop.
Now Search log events and configuration files using AJAX and a browser.
Download your FREE copy of Splunk now >> http://get.splunk.com/

[-- Attachment #4: Type: text/plain, Size: 164 bytes --]

_______________________________________________
Bluez-devel mailing list
Bluez-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/bluez-devel

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2007-10-27 10:31 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2007-10-23 13:07 [Bluez-devel] [PATCH] randomizer for passkey-agent eGore
2007-10-23 14:30 ` eGore
2007-10-25 22:52 ` Marcel Holtmann
2007-10-26 18:07   ` Christoph Brill
2007-10-26 19:14     ` Brad Midgley
2007-10-27 10:31     ` Marcel Holtmann

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox