Introduce "adb keygen"
Introduce the "adb keygen" command.
Usage: adb keygen <filename>
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 a85094151253845648ee005325dcd0a254270253)
Change-Id: Ic76ffc9412171dddc879af0bbf6e20fbe1a8f057
diff --git a/adb_auth_host.c b/adb_auth_host.c
index c72fe42..4c33d14 100644
--- a/adb_auth_host.c
+++ b/adb_auth_host.c
@@ -114,18 +114,34 @@
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);
@@ -436,6 +452,11 @@
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;