From 86c9e5f7e20a3f1712038ce642628c2e1e866434 Mon Sep 17 00:00:00 2001 From: Nick Kralevich Date: Thu, 13 Nov 2014 15:17:29 -0800 Subject: [PATCH] Introduce "adb keygen" Introduce the "adb keygen" command. Usage: adb keygen This command creates an adb public/private key pair in a user specified file. This can be used to create new adb keys, or rotate existing keys. Modify adb's key generation routines to use the HOSTNAME/LOGNAME environment variables if available. This allows someone to override the username/hostname embedded within the adb public key file if desired. Fallback to the old mechanisms if those environment variables aren't available. Bug: 18342715 Change-Id: Ibccee6088d4609aa05ad6687d3a1d8a8689d3e8a (cherry picked from commit af782b9f2ac4fb817ded80d4317a45345bb3f992) Change-Id: Ic76ffc9412171dddc879af0bbf6e20fbe1a8f057 --- adb/adb_auth.h | 1 + adb/adb_auth_host.c | 27 ++++++++++++++++++++++++--- adb/commandline.c | 9 +++++++++ 3 files changed, 34 insertions(+), 3 deletions(-) diff --git a/adb/adb_auth.h b/adb/adb_auth.h index b24c67413..54dd53731 100644 --- a/adb/adb_auth.h +++ b/adb/adb_auth.h @@ -18,6 +18,7 @@ #define __ADB_AUTH_H void adb_auth_init(void); +int adb_auth_keygen(const char* filename); void adb_auth_verified(atransport *t); void send_auth_request(atransport *t); diff --git a/adb/adb_auth_host.c b/adb/adb_auth_host.c index 8e444a8a8..dd839001b 100644 --- a/adb/adb_auth_host.c +++ b/adb/adb_auth_host.c @@ -113,18 +113,34 @@ out: static void get_user_info(char *buf, size_t len) { char hostname[1024], username[1024]; - int ret; + int ret = -1; + + if (getenv("HOSTNAME") != NULL) { + strncpy(hostname, getenv("HOSTNAME"), sizeof(hostname)); + hostname[sizeof(hostname)-1] = '\0'; + ret = 0; + } #ifndef _WIN32 - ret = gethostname(hostname, sizeof(hostname)); if (ret < 0) + ret = gethostname(hostname, sizeof(hostname)); #endif + if (ret < 0) strcpy(hostname, "unknown"); + ret = -1; + + if (getenv("LOGNAME") != NULL) { + strncpy(username, getenv("LOGNAME"), sizeof(username)); + username[sizeof(username)-1] = '\0'; + ret = 0; + } + #if !defined _WIN32 && !defined ADB_HOST_ON_TARGET - ret = getlogin_r(username, sizeof(username)); if (ret < 0) + ret = getlogin_r(username, sizeof(username)); #endif + if (ret < 0) strcpy(username, "unknown"); ret = snprintf(buf, len, " %s@%s", username, hostname); @@ -411,6 +427,11 @@ int adb_auth_get_userkey(unsigned char *data, size_t len) return ret + 1; } +int adb_auth_keygen(const char* filename) { + adb_trace_mask |= (1 << TRACE_AUTH); + return (generate_key(filename) == 0); +} + void adb_auth_init(void) { int ret; diff --git a/adb/commandline.c b/adb/commandline.c index 87baeb909..770487804 100644 --- a/adb/commandline.c +++ b/adb/commandline.c @@ -36,6 +36,7 @@ #define TRACE_TAG TRACE_ADB #include "adb.h" #include "adb_client.h" +#include "adb_auth.h" #include "file_sync_service.h" static int do_cmd(transport_type ttype, char* serial, char *cmd, ...); @@ -190,6 +191,9 @@ void help() " adb restore - restore device contents from the backup archive\n" "\n" " adb disable-verity - disable dm-verity checking on USERDEBUG builds\n" + " adb keygen - generate adb public/private key. The private key is stored in ,\n" + " and the public key is stored in .pub. Any existing files\n" + " are overwritten.\n" " adb help - show this help message\n" " adb version - show version num\n" "\n" @@ -1720,6 +1724,11 @@ top: return restore(argc, argv); } + if (!strcmp(argv[0], "keygen")) { + if (argc < 2) return usage(); + return adb_auth_keygen(argv[1]); + } + if (!strcmp(argv[0], "jdwp")) { int fd = adb_connect("jdwp"); if (fd >= 0) {