2009-03-04 11:32:55 +08:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2007 The Android Open Source Project
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2015-03-20 06:21:08 +08:00
|
|
|
#define TRACE_TAG TRACE_ADB
|
|
|
|
|
|
|
|
#include "sysdeps.h"
|
|
|
|
#include "adb.h"
|
2009-03-04 11:32:55 +08:00
|
|
|
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <errno.h>
|
2015-03-09 12:12:08 +08:00
|
|
|
#include <stdarg.h>
|
2012-05-26 04:55:46 +08:00
|
|
|
#include <stddef.h>
|
2015-03-09 12:12:08 +08:00
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2009-03-04 11:32:55 +08:00
|
|
|
#include <string.h>
|
2009-05-26 06:17:55 +08:00
|
|
|
#include <sys/time.h>
|
2015-03-09 12:12:08 +08:00
|
|
|
#include <time.h>
|
|
|
|
|
|
|
|
#include <string>
|
2009-03-04 11:32:55 +08:00
|
|
|
|
2015-04-20 23:09:20 +08:00
|
|
|
#include <base/stringprintf.h>
|
2015-04-25 14:02:00 +08:00
|
|
|
#include <base/strings.h>
|
2015-04-20 23:09:20 +08:00
|
|
|
|
adb: Add public key authentification
Secure adb using a public key authentication, to allow USB debugging
only from authorized hosts.
When a device is connected to an unauthorized host, the adb daemon sends
the user public key to the device. A popup is shown to ask the user to
allow debugging once or permanantly from the host. The public key is
installed on the device in the later case. Other keys may be installed
at build time.
On the host, the user public/private key pair is automatically generated,
if it does not exist, when the adb daemon starts and is stored in
$HOME/.android/adb_key(.pub) or in $ANDROID_SDK_HOME on windows. If needed,
the ADB_KEYS_PATH env variable may be set to a :-separated (; under
Windows) list of private keys, e.g. company-wide or vendor keys.
On the device, vendors public keys are installed at build time in
/adb_keys. User-installed keys are stored in /data/misc/adb/adb_keys.
ADB Protocol change:
If the device needs to authenticate the host, it replies to CNXN
packets with an AUTH packet. The AUTH packet payload is a random token.
The host signs the token with one of its private keys and sends an AUTH(0)
packet. If the signature verification succeeds, the device replies with
a CNXN packet. Otherwise, it sends a new AUTH packet with a new token so
that the host can retry with another private key. Once the host has tried
all its keys, it can send an AUTH(1) packet with a public key as
payload. adbd then sends the public key to the framework (if it has been
started) for confirmation.
Change-Id: I4e84d7621da956f66ff657245901bdaefead8395
2012-04-13 03:23:49 +08:00
|
|
|
#include "adb_auth.h"
|
2015-02-25 13:26:58 +08:00
|
|
|
#include "adb_io.h"
|
2015-02-19 10:03:26 +08:00
|
|
|
#include "adb_listeners.h"
|
2015-02-25 07:51:19 +08:00
|
|
|
#include "transport.h"
|
2009-03-04 11:32:55 +08:00
|
|
|
|
2012-05-26 05:10:02 +08:00
|
|
|
#define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
|
|
|
|
|
2009-03-04 11:32:55 +08:00
|
|
|
#if !ADB_HOST
|
2013-05-24 00:54:13 +08:00
|
|
|
#include <cutils/properties.h>
|
2013-03-01 06:12:58 +08:00
|
|
|
#include <sys/capability.h>
|
2012-08-15 12:00:22 +08:00
|
|
|
#include <sys/mount.h>
|
2009-03-04 11:32:55 +08:00
|
|
|
#endif
|
|
|
|
|
2011-03-17 06:57:42 +08:00
|
|
|
ADB_MUTEX_DEFINE( D_lock );
|
2009-03-04 11:32:55 +08:00
|
|
|
|
|
|
|
int HOST = 0;
|
|
|
|
|
2012-05-26 05:10:02 +08:00
|
|
|
#if !ADB_HOST
|
2015-02-19 10:22:45 +08:00
|
|
|
const char *adb_device_banner = "device";
|
2012-05-26 05:10:02 +08:00
|
|
|
#endif
|
2009-03-04 11:32:55 +08:00
|
|
|
|
|
|
|
void fatal(const char *fmt, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
va_start(ap, fmt);
|
|
|
|
fprintf(stderr, "error: ");
|
|
|
|
vfprintf(stderr, fmt, ap);
|
|
|
|
fprintf(stderr, "\n");
|
|
|
|
va_end(ap);
|
|
|
|
exit(-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
void fatal_errno(const char *fmt, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
va_start(ap, fmt);
|
|
|
|
fprintf(stderr, "error: %s: ", strerror(errno));
|
|
|
|
vfprintf(stderr, fmt, ap);
|
|
|
|
fprintf(stderr, "\n");
|
|
|
|
va_end(ap);
|
|
|
|
exit(-1);
|
|
|
|
}
|
|
|
|
|
2015-03-09 12:12:08 +08:00
|
|
|
#if !ADB_HOST
|
|
|
|
void start_device_log(void) {
|
|
|
|
struct tm now;
|
|
|
|
time_t t;
|
|
|
|
tzset();
|
|
|
|
time(&t);
|
|
|
|
localtime_r(&t, &now);
|
|
|
|
|
2015-03-20 13:53:30 +08:00
|
|
|
char timestamp[PATH_MAX];
|
|
|
|
strftime(timestamp, sizeof(timestamp), "%Y-%m-%d-%H-%M-%S", &now);
|
|
|
|
|
2015-05-02 08:04:38 +08:00
|
|
|
std::string path = android::base::StringPrintf("/data/adb/adb-%s-%d", timestamp, getpid());
|
|
|
|
int fd = unix_open(path.c_str(), O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC, 0640);
|
2015-03-09 12:12:08 +08:00
|
|
|
if (fd == -1) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// redirect stdout and stderr to the log file
|
|
|
|
dup2(fd, STDOUT_FILENO);
|
|
|
|
dup2(fd, STDERR_FILENO);
|
|
|
|
fprintf(stderr, "--- adb starting (pid %d) ---\n", getpid());
|
|
|
|
adb_close(fd);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
int adb_trace_mask;
|
|
|
|
|
|
|
|
std::string get_trace_setting_from_env() {
|
|
|
|
const char* setting = getenv("ADB_TRACE");
|
|
|
|
if (setting == nullptr) {
|
|
|
|
setting = "";
|
|
|
|
}
|
|
|
|
|
|
|
|
return std::string(setting);
|
|
|
|
}
|
|
|
|
|
|
|
|
#if !ADB_HOST
|
|
|
|
std::string get_trace_setting_from_prop() {
|
|
|
|
char buf[PROPERTY_VALUE_MAX];
|
|
|
|
property_get("persist.adb.trace_mask", buf, "");
|
|
|
|
return std::string(buf);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
std::string get_trace_setting() {
|
|
|
|
#if ADB_HOST
|
|
|
|
return get_trace_setting_from_env();
|
|
|
|
#else
|
|
|
|
return get_trace_setting_from_prop();
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
// Split the comma/space/colum/semi-column separated list of tags from the trace
|
|
|
|
// setting and build the trace mask from it. note that '1' and 'all' are special
|
|
|
|
// cases to enable all tracing.
|
|
|
|
//
|
|
|
|
// adb's trace setting comes from the ADB_TRACE environment variable, whereas
|
|
|
|
// adbd's comes from the system property persist.adb.trace_mask.
|
|
|
|
void adb_trace_init() {
|
|
|
|
const std::string trace_setting = get_trace_setting();
|
2009-03-04 11:32:55 +08:00
|
|
|
|
|
|
|
static const struct {
|
|
|
|
const char* tag;
|
|
|
|
int flag;
|
|
|
|
} tags[] = {
|
|
|
|
{ "1", 0 },
|
|
|
|
{ "all", 0 },
|
|
|
|
{ "adb", TRACE_ADB },
|
|
|
|
{ "sockets", TRACE_SOCKETS },
|
|
|
|
{ "packets", TRACE_PACKETS },
|
|
|
|
{ "rwx", TRACE_RWX },
|
|
|
|
{ "usb", TRACE_USB },
|
|
|
|
{ "sync", TRACE_SYNC },
|
|
|
|
{ "sysdeps", TRACE_SYSDEPS },
|
|
|
|
{ "transport", TRACE_TRANSPORT },
|
|
|
|
{ "jdwp", TRACE_JDWP },
|
2011-03-17 06:57:42 +08:00
|
|
|
{ "services", TRACE_SERVICES },
|
adb: Add public key authentification
Secure adb using a public key authentication, to allow USB debugging
only from authorized hosts.
When a device is connected to an unauthorized host, the adb daemon sends
the user public key to the device. A popup is shown to ask the user to
allow debugging once or permanantly from the host. The public key is
installed on the device in the later case. Other keys may be installed
at build time.
On the host, the user public/private key pair is automatically generated,
if it does not exist, when the adb daemon starts and is stored in
$HOME/.android/adb_key(.pub) or in $ANDROID_SDK_HOME on windows. If needed,
the ADB_KEYS_PATH env variable may be set to a :-separated (; under
Windows) list of private keys, e.g. company-wide or vendor keys.
On the device, vendors public keys are installed at build time in
/adb_keys. User-installed keys are stored in /data/misc/adb/adb_keys.
ADB Protocol change:
If the device needs to authenticate the host, it replies to CNXN
packets with an AUTH packet. The AUTH packet payload is a random token.
The host signs the token with one of its private keys and sends an AUTH(0)
packet. If the signature verification succeeds, the device replies with
a CNXN packet. Otherwise, it sends a new AUTH packet with a new token so
that the host can retry with another private key. Once the host has tried
all its keys, it can send an AUTH(1) packet with a public key as
payload. adbd then sends the public key to the framework (if it has been
started) for confirmation.
Change-Id: I4e84d7621da956f66ff657245901bdaefead8395
2012-04-13 03:23:49 +08:00
|
|
|
{ "auth", TRACE_AUTH },
|
2009-03-04 11:32:55 +08:00
|
|
|
{ NULL, 0 }
|
|
|
|
};
|
|
|
|
|
2015-03-09 12:12:08 +08:00
|
|
|
if (trace_setting.empty()) {
|
|
|
|
return;
|
|
|
|
}
|
2009-03-04 11:32:55 +08:00
|
|
|
|
2015-03-09 12:12:08 +08:00
|
|
|
// Use a comma/colon/semi-colon/space separated list
|
|
|
|
const char* p = trace_setting.c_str();
|
2009-03-04 11:32:55 +08:00
|
|
|
while (*p) {
|
|
|
|
int len, tagn;
|
|
|
|
|
2015-03-09 12:12:08 +08:00
|
|
|
const char* q = strpbrk(p, " ,:;");
|
2009-03-04 11:32:55 +08:00
|
|
|
if (q == NULL) {
|
|
|
|
q = p + strlen(p);
|
|
|
|
}
|
|
|
|
len = q - p;
|
|
|
|
|
2015-03-09 12:12:08 +08:00
|
|
|
for (tagn = 0; tags[tagn].tag != NULL; tagn++) {
|
2009-03-04 11:32:55 +08:00
|
|
|
int taglen = strlen(tags[tagn].tag);
|
|
|
|
|
2015-03-09 12:12:08 +08:00
|
|
|
if (len == taglen && !memcmp(tags[tagn].tag, p, len)) {
|
2009-03-04 11:32:55 +08:00
|
|
|
int flag = tags[tagn].flag;
|
|
|
|
if (flag == 0) {
|
|
|
|
adb_trace_mask = ~0;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
adb_trace_mask |= (1 << flag);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
p = q;
|
|
|
|
if (*p)
|
|
|
|
p++;
|
|
|
|
}
|
2015-03-09 12:12:08 +08:00
|
|
|
|
|
|
|
#if !ADB_HOST
|
|
|
|
start_device_log();
|
|
|
|
#endif
|
2009-03-04 11:32:55 +08:00
|
|
|
}
|
|
|
|
|
2015-02-26 09:51:28 +08:00
|
|
|
apacket* get_apacket(void)
|
2009-03-04 11:32:55 +08:00
|
|
|
{
|
2015-02-26 09:51:28 +08:00
|
|
|
apacket* p = reinterpret_cast<apacket*>(malloc(sizeof(apacket)));
|
|
|
|
if (p == nullptr) {
|
|
|
|
fatal("failed to allocate an apacket");
|
|
|
|
}
|
|
|
|
|
2009-03-04 11:32:55 +08:00
|
|
|
memset(p, 0, sizeof(apacket) - MAX_PAYLOAD);
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
|
|
|
void put_apacket(apacket *p)
|
|
|
|
{
|
|
|
|
free(p);
|
|
|
|
}
|
|
|
|
|
adb: Add public key authentification
Secure adb using a public key authentication, to allow USB debugging
only from authorized hosts.
When a device is connected to an unauthorized host, the adb daemon sends
the user public key to the device. A popup is shown to ask the user to
allow debugging once or permanantly from the host. The public key is
installed on the device in the later case. Other keys may be installed
at build time.
On the host, the user public/private key pair is automatically generated,
if it does not exist, when the adb daemon starts and is stored in
$HOME/.android/adb_key(.pub) or in $ANDROID_SDK_HOME on windows. If needed,
the ADB_KEYS_PATH env variable may be set to a :-separated (; under
Windows) list of private keys, e.g. company-wide or vendor keys.
On the device, vendors public keys are installed at build time in
/adb_keys. User-installed keys are stored in /data/misc/adb/adb_keys.
ADB Protocol change:
If the device needs to authenticate the host, it replies to CNXN
packets with an AUTH packet. The AUTH packet payload is a random token.
The host signs the token with one of its private keys and sends an AUTH(0)
packet. If the signature verification succeeds, the device replies with
a CNXN packet. Otherwise, it sends a new AUTH packet with a new token so
that the host can retry with another private key. Once the host has tried
all its keys, it can send an AUTH(1) packet with a public key as
payload. adbd then sends the public key to the framework (if it has been
started) for confirmation.
Change-Id: I4e84d7621da956f66ff657245901bdaefead8395
2012-04-13 03:23:49 +08:00
|
|
|
void handle_online(atransport *t)
|
2009-03-04 11:32:55 +08:00
|
|
|
{
|
|
|
|
D("adb: online\n");
|
adb: Add public key authentification
Secure adb using a public key authentication, to allow USB debugging
only from authorized hosts.
When a device is connected to an unauthorized host, the adb daemon sends
the user public key to the device. A popup is shown to ask the user to
allow debugging once or permanantly from the host. The public key is
installed on the device in the later case. Other keys may be installed
at build time.
On the host, the user public/private key pair is automatically generated,
if it does not exist, when the adb daemon starts and is stored in
$HOME/.android/adb_key(.pub) or in $ANDROID_SDK_HOME on windows. If needed,
the ADB_KEYS_PATH env variable may be set to a :-separated (; under
Windows) list of private keys, e.g. company-wide or vendor keys.
On the device, vendors public keys are installed at build time in
/adb_keys. User-installed keys are stored in /data/misc/adb/adb_keys.
ADB Protocol change:
If the device needs to authenticate the host, it replies to CNXN
packets with an AUTH packet. The AUTH packet payload is a random token.
The host signs the token with one of its private keys and sends an AUTH(0)
packet. If the signature verification succeeds, the device replies with
a CNXN packet. Otherwise, it sends a new AUTH packet with a new token so
that the host can retry with another private key. Once the host has tried
all its keys, it can send an AUTH(1) packet with a public key as
payload. adbd then sends the public key to the framework (if it has been
started) for confirmation.
Change-Id: I4e84d7621da956f66ff657245901bdaefead8395
2012-04-13 03:23:49 +08:00
|
|
|
t->online = 1;
|
2009-03-04 11:32:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void handle_offline(atransport *t)
|
|
|
|
{
|
|
|
|
D("adb: offline\n");
|
|
|
|
//Close the associated usb
|
adb: Add public key authentification
Secure adb using a public key authentication, to allow USB debugging
only from authorized hosts.
When a device is connected to an unauthorized host, the adb daemon sends
the user public key to the device. A popup is shown to ask the user to
allow debugging once or permanantly from the host. The public key is
installed on the device in the later case. Other keys may be installed
at build time.
On the host, the user public/private key pair is automatically generated,
if it does not exist, when the adb daemon starts and is stored in
$HOME/.android/adb_key(.pub) or in $ANDROID_SDK_HOME on windows. If needed,
the ADB_KEYS_PATH env variable may be set to a :-separated (; under
Windows) list of private keys, e.g. company-wide or vendor keys.
On the device, vendors public keys are installed at build time in
/adb_keys. User-installed keys are stored in /data/misc/adb/adb_keys.
ADB Protocol change:
If the device needs to authenticate the host, it replies to CNXN
packets with an AUTH packet. The AUTH packet payload is a random token.
The host signs the token with one of its private keys and sends an AUTH(0)
packet. If the signature verification succeeds, the device replies with
a CNXN packet. Otherwise, it sends a new AUTH packet with a new token so
that the host can retry with another private key. Once the host has tried
all its keys, it can send an AUTH(1) packet with a public key as
payload. adbd then sends the public key to the framework (if it has been
started) for confirmation.
Change-Id: I4e84d7621da956f66ff657245901bdaefead8395
2012-04-13 03:23:49 +08:00
|
|
|
t->online = 0;
|
2009-03-04 11:32:55 +08:00
|
|
|
run_transport_disconnects(t);
|
|
|
|
}
|
|
|
|
|
adb: Add public key authentification
Secure adb using a public key authentication, to allow USB debugging
only from authorized hosts.
When a device is connected to an unauthorized host, the adb daemon sends
the user public key to the device. A popup is shown to ask the user to
allow debugging once or permanantly from the host. The public key is
installed on the device in the later case. Other keys may be installed
at build time.
On the host, the user public/private key pair is automatically generated,
if it does not exist, when the adb daemon starts and is stored in
$HOME/.android/adb_key(.pub) or in $ANDROID_SDK_HOME on windows. If needed,
the ADB_KEYS_PATH env variable may be set to a :-separated (; under
Windows) list of private keys, e.g. company-wide or vendor keys.
On the device, vendors public keys are installed at build time in
/adb_keys. User-installed keys are stored in /data/misc/adb/adb_keys.
ADB Protocol change:
If the device needs to authenticate the host, it replies to CNXN
packets with an AUTH packet. The AUTH packet payload is a random token.
The host signs the token with one of its private keys and sends an AUTH(0)
packet. If the signature verification succeeds, the device replies with
a CNXN packet. Otherwise, it sends a new AUTH packet with a new token so
that the host can retry with another private key. Once the host has tried
all its keys, it can send an AUTH(1) packet with a public key as
payload. adbd then sends the public key to the framework (if it has been
started) for confirmation.
Change-Id: I4e84d7621da956f66ff657245901bdaefead8395
2012-04-13 03:23:49 +08:00
|
|
|
#if DEBUG_PACKETS
|
2009-03-04 11:32:55 +08:00
|
|
|
#define DUMPMAX 32
|
|
|
|
void print_packet(const char *label, apacket *p)
|
|
|
|
{
|
|
|
|
char *tag;
|
|
|
|
char *x;
|
|
|
|
unsigned count;
|
|
|
|
|
|
|
|
switch(p->msg.command){
|
|
|
|
case A_SYNC: tag = "SYNC"; break;
|
|
|
|
case A_CNXN: tag = "CNXN" ; break;
|
|
|
|
case A_OPEN: tag = "OPEN"; break;
|
|
|
|
case A_OKAY: tag = "OKAY"; break;
|
|
|
|
case A_CLSE: tag = "CLSE"; break;
|
|
|
|
case A_WRTE: tag = "WRTE"; break;
|
adb: Add public key authentification
Secure adb using a public key authentication, to allow USB debugging
only from authorized hosts.
When a device is connected to an unauthorized host, the adb daemon sends
the user public key to the device. A popup is shown to ask the user to
allow debugging once or permanantly from the host. The public key is
installed on the device in the later case. Other keys may be installed
at build time.
On the host, the user public/private key pair is automatically generated,
if it does not exist, when the adb daemon starts and is stored in
$HOME/.android/adb_key(.pub) or in $ANDROID_SDK_HOME on windows. If needed,
the ADB_KEYS_PATH env variable may be set to a :-separated (; under
Windows) list of private keys, e.g. company-wide or vendor keys.
On the device, vendors public keys are installed at build time in
/adb_keys. User-installed keys are stored in /data/misc/adb/adb_keys.
ADB Protocol change:
If the device needs to authenticate the host, it replies to CNXN
packets with an AUTH packet. The AUTH packet payload is a random token.
The host signs the token with one of its private keys and sends an AUTH(0)
packet. If the signature verification succeeds, the device replies with
a CNXN packet. Otherwise, it sends a new AUTH packet with a new token so
that the host can retry with another private key. Once the host has tried
all its keys, it can send an AUTH(1) packet with a public key as
payload. adbd then sends the public key to the framework (if it has been
started) for confirmation.
Change-Id: I4e84d7621da956f66ff657245901bdaefead8395
2012-04-13 03:23:49 +08:00
|
|
|
case A_AUTH: tag = "AUTH"; break;
|
2009-03-04 11:32:55 +08:00
|
|
|
default: tag = "????"; break;
|
|
|
|
}
|
|
|
|
|
|
|
|
fprintf(stderr, "%s: %s %08x %08x %04x \"",
|
|
|
|
label, tag, p->msg.arg0, p->msg.arg1, p->msg.data_length);
|
|
|
|
count = p->msg.data_length;
|
|
|
|
x = (char*) p->data;
|
|
|
|
if(count > DUMPMAX) {
|
|
|
|
count = DUMPMAX;
|
|
|
|
tag = "\n";
|
|
|
|
} else {
|
|
|
|
tag = "\"\n";
|
|
|
|
}
|
|
|
|
while(count-- > 0){
|
|
|
|
if((*x >= ' ') && (*x < 127)) {
|
|
|
|
fputc(*x, stderr);
|
|
|
|
} else {
|
|
|
|
fputc('.', stderr);
|
|
|
|
}
|
|
|
|
x++;
|
|
|
|
}
|
adb: Add public key authentification
Secure adb using a public key authentication, to allow USB debugging
only from authorized hosts.
When a device is connected to an unauthorized host, the adb daemon sends
the user public key to the device. A popup is shown to ask the user to
allow debugging once or permanantly from the host. The public key is
installed on the device in the later case. Other keys may be installed
at build time.
On the host, the user public/private key pair is automatically generated,
if it does not exist, when the adb daemon starts and is stored in
$HOME/.android/adb_key(.pub) or in $ANDROID_SDK_HOME on windows. If needed,
the ADB_KEYS_PATH env variable may be set to a :-separated (; under
Windows) list of private keys, e.g. company-wide or vendor keys.
On the device, vendors public keys are installed at build time in
/adb_keys. User-installed keys are stored in /data/misc/adb/adb_keys.
ADB Protocol change:
If the device needs to authenticate the host, it replies to CNXN
packets with an AUTH packet. The AUTH packet payload is a random token.
The host signs the token with one of its private keys and sends an AUTH(0)
packet. If the signature verification succeeds, the device replies with
a CNXN packet. Otherwise, it sends a new AUTH packet with a new token so
that the host can retry with another private key. Once the host has tried
all its keys, it can send an AUTH(1) packet with a public key as
payload. adbd then sends the public key to the framework (if it has been
started) for confirmation.
Change-Id: I4e84d7621da956f66ff657245901bdaefead8395
2012-04-13 03:23:49 +08:00
|
|
|
fputs(tag, stderr);
|
2009-03-04 11:32:55 +08:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static void send_ready(unsigned local, unsigned remote, atransport *t)
|
|
|
|
{
|
|
|
|
D("Calling send_ready \n");
|
|
|
|
apacket *p = get_apacket();
|
|
|
|
p->msg.command = A_OKAY;
|
|
|
|
p->msg.arg0 = local;
|
|
|
|
p->msg.arg1 = remote;
|
|
|
|
send_packet(p, t);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void send_close(unsigned local, unsigned remote, atransport *t)
|
|
|
|
{
|
|
|
|
D("Calling send_close \n");
|
|
|
|
apacket *p = get_apacket();
|
|
|
|
p->msg.command = A_CLSE;
|
|
|
|
p->msg.arg0 = local;
|
|
|
|
p->msg.arg1 = remote;
|
|
|
|
send_packet(p, t);
|
|
|
|
}
|
|
|
|
|
2012-05-26 05:10:02 +08:00
|
|
|
static size_t fill_connect_data(char *buf, size_t bufsize)
|
|
|
|
{
|
|
|
|
#if ADB_HOST
|
|
|
|
return snprintf(buf, bufsize, "host::") + 1;
|
|
|
|
#else
|
|
|
|
static const char *cnxn_props[] = {
|
|
|
|
"ro.product.name",
|
|
|
|
"ro.product.model",
|
|
|
|
"ro.product.device",
|
|
|
|
};
|
|
|
|
static const int num_cnxn_props = ARRAY_SIZE(cnxn_props);
|
|
|
|
int i;
|
|
|
|
size_t remaining = bufsize;
|
|
|
|
size_t len;
|
|
|
|
|
|
|
|
len = snprintf(buf, remaining, "%s::", adb_device_banner);
|
|
|
|
remaining -= len;
|
|
|
|
buf += len;
|
|
|
|
for (i = 0; i < num_cnxn_props; i++) {
|
|
|
|
char value[PROPERTY_VALUE_MAX];
|
|
|
|
property_get(cnxn_props[i], value, "");
|
|
|
|
len = snprintf(buf, remaining, "%s=%s;", cnxn_props[i], value);
|
|
|
|
remaining -= len;
|
|
|
|
buf += len;
|
|
|
|
}
|
|
|
|
|
|
|
|
return bufsize - remaining + 1;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2015-02-19 09:47:33 +08:00
|
|
|
void send_connect(atransport *t)
|
2009-03-04 11:32:55 +08:00
|
|
|
{
|
|
|
|
D("Calling send_connect \n");
|
|
|
|
apacket *cp = get_apacket();
|
|
|
|
cp->msg.command = A_CNXN;
|
|
|
|
cp->msg.arg0 = A_VERSION;
|
|
|
|
cp->msg.arg1 = MAX_PAYLOAD;
|
2012-05-26 05:10:02 +08:00
|
|
|
cp->msg.data_length = fill_connect_data((char *)cp->data,
|
|
|
|
sizeof(cp->data));
|
2009-03-04 11:32:55 +08:00
|
|
|
send_packet(cp, t);
|
adb: Add public key authentification
Secure adb using a public key authentication, to allow USB debugging
only from authorized hosts.
When a device is connected to an unauthorized host, the adb daemon sends
the user public key to the device. A popup is shown to ask the user to
allow debugging once or permanantly from the host. The public key is
installed on the device in the later case. Other keys may be installed
at build time.
On the host, the user public/private key pair is automatically generated,
if it does not exist, when the adb daemon starts and is stored in
$HOME/.android/adb_key(.pub) or in $ANDROID_SDK_HOME on windows. If needed,
the ADB_KEYS_PATH env variable may be set to a :-separated (; under
Windows) list of private keys, e.g. company-wide or vendor keys.
On the device, vendors public keys are installed at build time in
/adb_keys. User-installed keys are stored in /data/misc/adb/adb_keys.
ADB Protocol change:
If the device needs to authenticate the host, it replies to CNXN
packets with an AUTH packet. The AUTH packet payload is a random token.
The host signs the token with one of its private keys and sends an AUTH(0)
packet. If the signature verification succeeds, the device replies with
a CNXN packet. Otherwise, it sends a new AUTH packet with a new token so
that the host can retry with another private key. Once the host has tried
all its keys, it can send an AUTH(1) packet with a public key as
payload. adbd then sends the public key to the framework (if it has been
started) for confirmation.
Change-Id: I4e84d7621da956f66ff657245901bdaefead8395
2012-04-13 03:23:49 +08:00
|
|
|
}
|
|
|
|
|
2015-04-25 14:02:00 +08:00
|
|
|
// qual_overwrite is used to overwrite a qualifier string. dst is a
|
|
|
|
// pointer to a char pointer. It is assumed that if *dst is non-NULL, it
|
|
|
|
// was malloc'ed and needs to freed. *dst will be set to a dup of src.
|
|
|
|
// TODO: switch to std::string for these atransport fields instead.
|
|
|
|
static void qual_overwrite(char** dst, const std::string& src) {
|
2012-05-26 05:10:02 +08:00
|
|
|
free(*dst);
|
2015-04-25 14:02:00 +08:00
|
|
|
*dst = strdup(src.c_str());
|
2012-05-26 05:10:02 +08:00
|
|
|
}
|
|
|
|
|
2015-04-25 14:02:00 +08:00
|
|
|
void parse_banner(const char* banner, atransport* t) {
|
2009-03-04 11:32:55 +08:00
|
|
|
D("parse_banner: %s\n", banner);
|
2015-04-25 14:02:00 +08:00
|
|
|
|
|
|
|
// The format is something like:
|
|
|
|
// "device::ro.product.name=x;ro.product.model=y;ro.product.device=z;".
|
|
|
|
std::vector<std::string> pieces = android::base::Split(banner, ":");
|
|
|
|
|
|
|
|
if (pieces.size() > 2) {
|
|
|
|
const std::string& props = pieces[2];
|
|
|
|
for (auto& prop : android::base::Split(props, ";")) {
|
|
|
|
// The list of properties was traditionally ;-terminated rather than ;-separated.
|
|
|
|
if (prop.empty()) continue;
|
|
|
|
|
|
|
|
std::vector<std::string> key_value = android::base::Split(prop, "=");
|
|
|
|
if (key_value.size() != 2) continue;
|
|
|
|
|
|
|
|
const std::string& key = key_value[0];
|
|
|
|
const std::string& value = key_value[1];
|
|
|
|
if (key == "ro.product.name") {
|
|
|
|
qual_overwrite(&t->product, value);
|
|
|
|
} else if (key == "ro.product.model") {
|
|
|
|
qual_overwrite(&t->model, value);
|
|
|
|
} else if (key == "ro.product.device") {
|
|
|
|
qual_overwrite(&t->device, value);
|
2012-05-26 05:10:02 +08:00
|
|
|
}
|
|
|
|
}
|
2009-03-04 11:32:55 +08:00
|
|
|
}
|
|
|
|
|
2015-04-25 14:02:00 +08:00
|
|
|
const std::string& type = pieces[0];
|
|
|
|
if (type == "bootloader") {
|
2009-03-04 11:32:55 +08:00
|
|
|
D("setting connection_state to CS_BOOTLOADER\n");
|
|
|
|
t->connection_state = CS_BOOTLOADER;
|
|
|
|
update_transports();
|
2015-04-25 14:02:00 +08:00
|
|
|
} else if (type == "device") {
|
2009-03-04 11:32:55 +08:00
|
|
|
D("setting connection_state to CS_DEVICE\n");
|
|
|
|
t->connection_state = CS_DEVICE;
|
|
|
|
update_transports();
|
2015-04-25 14:02:00 +08:00
|
|
|
} else if (type == "recovery") {
|
2009-03-04 11:32:55 +08:00
|
|
|
D("setting connection_state to CS_RECOVERY\n");
|
|
|
|
t->connection_state = CS_RECOVERY;
|
|
|
|
update_transports();
|
2015-04-25 14:02:00 +08:00
|
|
|
} else if (type == "sideload") {
|
2012-01-10 06:54:53 +08:00
|
|
|
D("setting connection_state to CS_SIDELOAD\n");
|
|
|
|
t->connection_state = CS_SIDELOAD;
|
|
|
|
update_transports();
|
2015-04-30 13:37:25 +08:00
|
|
|
} else {
|
|
|
|
D("setting connection_state to CS_HOST\n");
|
|
|
|
t->connection_state = CS_HOST;
|
2012-01-10 06:54:53 +08:00
|
|
|
}
|
2009-03-04 11:32:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void handle_packet(apacket *p, atransport *t)
|
|
|
|
{
|
|
|
|
asocket *s;
|
|
|
|
|
2010-06-16 21:11:28 +08:00
|
|
|
D("handle_packet() %c%c%c%c\n", ((char*) (&(p->msg.command)))[0],
|
|
|
|
((char*) (&(p->msg.command)))[1],
|
|
|
|
((char*) (&(p->msg.command)))[2],
|
|
|
|
((char*) (&(p->msg.command)))[3]);
|
2009-03-04 11:32:55 +08:00
|
|
|
print_packet("recv", p);
|
|
|
|
|
|
|
|
switch(p->msg.command){
|
|
|
|
case A_SYNC:
|
|
|
|
if(p->msg.arg0){
|
|
|
|
send_packet(p, t);
|
|
|
|
if(HOST) send_connect(t);
|
|
|
|
} else {
|
|
|
|
t->connection_state = CS_OFFLINE;
|
|
|
|
handle_offline(t);
|
|
|
|
send_packet(p, t);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
|
|
|
|
case A_CNXN: /* CONNECT(version, maxdata, "system-id-string") */
|
|
|
|
/* XXX verify version, etc */
|
|
|
|
if(t->connection_state != CS_OFFLINE) {
|
|
|
|
t->connection_state = CS_OFFLINE;
|
|
|
|
handle_offline(t);
|
|
|
|
}
|
adb: Add public key authentification
Secure adb using a public key authentication, to allow USB debugging
only from authorized hosts.
When a device is connected to an unauthorized host, the adb daemon sends
the user public key to the device. A popup is shown to ask the user to
allow debugging once or permanantly from the host. The public key is
installed on the device in the later case. Other keys may be installed
at build time.
On the host, the user public/private key pair is automatically generated,
if it does not exist, when the adb daemon starts and is stored in
$HOME/.android/adb_key(.pub) or in $ANDROID_SDK_HOME on windows. If needed,
the ADB_KEYS_PATH env variable may be set to a :-separated (; under
Windows) list of private keys, e.g. company-wide or vendor keys.
On the device, vendors public keys are installed at build time in
/adb_keys. User-installed keys are stored in /data/misc/adb/adb_keys.
ADB Protocol change:
If the device needs to authenticate the host, it replies to CNXN
packets with an AUTH packet. The AUTH packet payload is a random token.
The host signs the token with one of its private keys and sends an AUTH(0)
packet. If the signature verification succeeds, the device replies with
a CNXN packet. Otherwise, it sends a new AUTH packet with a new token so
that the host can retry with another private key. Once the host has tried
all its keys, it can send an AUTH(1) packet with a public key as
payload. adbd then sends the public key to the framework (if it has been
started) for confirmation.
Change-Id: I4e84d7621da956f66ff657245901bdaefead8395
2012-04-13 03:23:49 +08:00
|
|
|
|
2015-04-25 14:02:00 +08:00
|
|
|
parse_banner(reinterpret_cast<const char*>(p->data), t);
|
adb: Add public key authentification
Secure adb using a public key authentication, to allow USB debugging
only from authorized hosts.
When a device is connected to an unauthorized host, the adb daemon sends
the user public key to the device. A popup is shown to ask the user to
allow debugging once or permanantly from the host. The public key is
installed on the device in the later case. Other keys may be installed
at build time.
On the host, the user public/private key pair is automatically generated,
if it does not exist, when the adb daemon starts and is stored in
$HOME/.android/adb_key(.pub) or in $ANDROID_SDK_HOME on windows. If needed,
the ADB_KEYS_PATH env variable may be set to a :-separated (; under
Windows) list of private keys, e.g. company-wide or vendor keys.
On the device, vendors public keys are installed at build time in
/adb_keys. User-installed keys are stored in /data/misc/adb/adb_keys.
ADB Protocol change:
If the device needs to authenticate the host, it replies to CNXN
packets with an AUTH packet. The AUTH packet payload is a random token.
The host signs the token with one of its private keys and sends an AUTH(0)
packet. If the signature verification succeeds, the device replies with
a CNXN packet. Otherwise, it sends a new AUTH packet with a new token so
that the host can retry with another private key. Once the host has tried
all its keys, it can send an AUTH(1) packet with a public key as
payload. adbd then sends the public key to the framework (if it has been
started) for confirmation.
Change-Id: I4e84d7621da956f66ff657245901bdaefead8395
2012-04-13 03:23:49 +08:00
|
|
|
|
|
|
|
if (HOST || !auth_enabled) {
|
|
|
|
handle_online(t);
|
|
|
|
if(!HOST) send_connect(t);
|
|
|
|
} else {
|
|
|
|
send_auth_request(t);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case A_AUTH:
|
|
|
|
if (p->msg.arg0 == ADB_AUTH_TOKEN) {
|
2013-01-16 04:36:47 +08:00
|
|
|
t->connection_state = CS_UNAUTHORIZED;
|
adb: Add public key authentification
Secure adb using a public key authentication, to allow USB debugging
only from authorized hosts.
When a device is connected to an unauthorized host, the adb daemon sends
the user public key to the device. A popup is shown to ask the user to
allow debugging once or permanantly from the host. The public key is
installed on the device in the later case. Other keys may be installed
at build time.
On the host, the user public/private key pair is automatically generated,
if it does not exist, when the adb daemon starts and is stored in
$HOME/.android/adb_key(.pub) or in $ANDROID_SDK_HOME on windows. If needed,
the ADB_KEYS_PATH env variable may be set to a :-separated (; under
Windows) list of private keys, e.g. company-wide or vendor keys.
On the device, vendors public keys are installed at build time in
/adb_keys. User-installed keys are stored in /data/misc/adb/adb_keys.
ADB Protocol change:
If the device needs to authenticate the host, it replies to CNXN
packets with an AUTH packet. The AUTH packet payload is a random token.
The host signs the token with one of its private keys and sends an AUTH(0)
packet. If the signature verification succeeds, the device replies with
a CNXN packet. Otherwise, it sends a new AUTH packet with a new token so
that the host can retry with another private key. Once the host has tried
all its keys, it can send an AUTH(1) packet with a public key as
payload. adbd then sends the public key to the framework (if it has been
started) for confirmation.
Change-Id: I4e84d7621da956f66ff657245901bdaefead8395
2012-04-13 03:23:49 +08:00
|
|
|
t->key = adb_auth_nextkey(t->key);
|
|
|
|
if (t->key) {
|
|
|
|
send_auth_response(p->data, p->msg.data_length, t);
|
|
|
|
} else {
|
|
|
|
/* No more private keys to try, send the public key */
|
|
|
|
send_auth_publickey(t);
|
|
|
|
}
|
|
|
|
} else if (p->msg.arg0 == ADB_AUTH_SIGNATURE) {
|
|
|
|
if (adb_auth_verify(t->token, p->data, p->msg.data_length)) {
|
|
|
|
adb_auth_verified(t);
|
|
|
|
t->failed_auth_attempts = 0;
|
|
|
|
} else {
|
|
|
|
if (t->failed_auth_attempts++ > 10)
|
|
|
|
adb_sleep_ms(1000);
|
|
|
|
send_auth_request(t);
|
|
|
|
}
|
|
|
|
} else if (p->msg.arg0 == ADB_AUTH_RSAPUBLICKEY) {
|
|
|
|
adb_auth_confirm_key(p->data, p->msg.data_length, t);
|
|
|
|
}
|
2009-03-04 11:32:55 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case A_OPEN: /* OPEN(local-id, 0, "destination") */
|
2013-12-13 21:09:44 +08:00
|
|
|
if (t->online && p->msg.arg0 != 0 && p->msg.arg1 == 0) {
|
2009-03-04 11:32:55 +08:00
|
|
|
char *name = (char*) p->data;
|
|
|
|
name[p->msg.data_length > 0 ? p->msg.data_length - 1 : 0] = 0;
|
|
|
|
s = create_local_service_socket(name);
|
|
|
|
if(s == 0) {
|
|
|
|
send_close(0, p->msg.arg0, t);
|
|
|
|
} else {
|
|
|
|
s->peer = create_remote_socket(p->msg.arg0, t);
|
|
|
|
s->peer->peer = s;
|
|
|
|
send_ready(s->id, s->peer->id, t);
|
|
|
|
s->ready(s);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case A_OKAY: /* READY(local-id, remote-id, "") */
|
2013-12-13 21:09:44 +08:00
|
|
|
if (t->online && p->msg.arg0 != 0 && p->msg.arg1 != 0) {
|
|
|
|
if((s = find_local_socket(p->msg.arg1, 0))) {
|
2009-03-04 11:32:55 +08:00
|
|
|
if(s->peer == 0) {
|
2013-12-13 21:09:44 +08:00
|
|
|
/* On first READY message, create the connection. */
|
2009-03-04 11:32:55 +08:00
|
|
|
s->peer = create_remote_socket(p->msg.arg0, t);
|
|
|
|
s->peer->peer = s;
|
2013-12-13 21:09:44 +08:00
|
|
|
s->ready(s);
|
|
|
|
} else if (s->peer->id == p->msg.arg0) {
|
|
|
|
/* Other READY messages must use the same local-id */
|
|
|
|
s->ready(s);
|
|
|
|
} else {
|
|
|
|
D("Invalid A_OKAY(%d,%d), expected A_OKAY(%d,%d) on transport %s\n",
|
|
|
|
p->msg.arg0, p->msg.arg1, s->peer->id, p->msg.arg1, t->serial);
|
2009-03-04 11:32:55 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2013-12-13 21:09:44 +08:00
|
|
|
case A_CLSE: /* CLOSE(local-id, remote-id, "") or CLOSE(0, remote-id, "") */
|
|
|
|
if (t->online && p->msg.arg1 != 0) {
|
|
|
|
if((s = find_local_socket(p->msg.arg1, p->msg.arg0))) {
|
|
|
|
/* According to protocol.txt, p->msg.arg0 might be 0 to indicate
|
|
|
|
* a failed OPEN only. However, due to a bug in previous ADB
|
|
|
|
* versions, CLOSE(0, remote-id, "") was also used for normal
|
|
|
|
* CLOSE() operations.
|
|
|
|
*
|
|
|
|
* This is bad because it means a compromised adbd could
|
|
|
|
* send packets to close connections between the host and
|
|
|
|
* other devices. To avoid this, only allow this if the local
|
|
|
|
* socket has a peer on the same transport.
|
|
|
|
*/
|
|
|
|
if (p->msg.arg0 == 0 && s->peer && s->peer->transport != t) {
|
|
|
|
D("Invalid A_CLSE(0, %u) from transport %s, expected transport %s\n",
|
|
|
|
p->msg.arg1, t->serial, s->peer->transport->serial);
|
|
|
|
} else {
|
|
|
|
s->close(s);
|
|
|
|
}
|
2009-03-04 11:32:55 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2013-12-13 21:09:44 +08:00
|
|
|
case A_WRTE: /* WRITE(local-id, remote-id, <data>) */
|
|
|
|
if (t->online && p->msg.arg0 != 0 && p->msg.arg1 != 0) {
|
|
|
|
if((s = find_local_socket(p->msg.arg1, p->msg.arg0))) {
|
2009-03-04 11:32:55 +08:00
|
|
|
unsigned rid = p->msg.arg0;
|
|
|
|
p->len = p->msg.data_length;
|
|
|
|
|
|
|
|
if(s->enqueue(s, p) == 0) {
|
|
|
|
D("Enqueue the socket\n");
|
|
|
|
send_ready(s->id, rid, t);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
printf("handle_packet: what is %08x?!\n", p->msg.command);
|
|
|
|
}
|
|
|
|
|
|
|
|
put_apacket(p);
|
|
|
|
}
|
|
|
|
|
|
|
|
#if ADB_HOST
|
2012-12-07 10:18:12 +08:00
|
|
|
|
2010-04-19 19:21:12 +08:00
|
|
|
int launch_server(int server_port)
|
2009-03-04 11:32:55 +08:00
|
|
|
{
|
2014-11-12 01:24:11 +08:00
|
|
|
#if defined(_WIN32)
|
2009-03-04 11:32:55 +08:00
|
|
|
/* we need to start the server in the background */
|
|
|
|
/* we create a PIPE that will be used to wait for the server's "OK" */
|
|
|
|
/* message since the pipe handles must be inheritable, we use a */
|
|
|
|
/* security attribute */
|
|
|
|
HANDLE pipe_read, pipe_write;
|
2012-11-29 09:18:50 +08:00
|
|
|
HANDLE stdout_handle, stderr_handle;
|
2009-03-04 11:32:55 +08:00
|
|
|
SECURITY_ATTRIBUTES sa;
|
|
|
|
STARTUPINFO startup;
|
|
|
|
PROCESS_INFORMATION pinfo;
|
|
|
|
char program_path[ MAX_PATH ];
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
sa.nLength = sizeof(sa);
|
|
|
|
sa.lpSecurityDescriptor = NULL;
|
|
|
|
sa.bInheritHandle = TRUE;
|
|
|
|
|
|
|
|
/* create pipe, and ensure its read handle isn't inheritable */
|
|
|
|
ret = CreatePipe( &pipe_read, &pipe_write, &sa, 0 );
|
|
|
|
if (!ret) {
|
|
|
|
fprintf(stderr, "CreatePipe() failure, error %ld\n", GetLastError() );
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
SetHandleInformation( pipe_read, HANDLE_FLAG_INHERIT, 0 );
|
|
|
|
|
2012-11-29 09:18:50 +08:00
|
|
|
/* Some programs want to launch an adb command and collect its output by
|
|
|
|
* calling CreateProcess with inheritable stdout/stderr handles, then
|
|
|
|
* using read() to get its output. When this happens, the stdout/stderr
|
|
|
|
* handles passed to the adb client process will also be inheritable.
|
|
|
|
* When starting the adb server here, care must be taken to reset them
|
|
|
|
* to non-inheritable.
|
|
|
|
* Otherwise, something bad happens: even if the adb command completes,
|
|
|
|
* the calling process is stuck while read()-ing from the stdout/stderr
|
|
|
|
* descriptors, because they're connected to corresponding handles in the
|
|
|
|
* adb server process (even if the latter never uses/writes to them).
|
|
|
|
*/
|
|
|
|
stdout_handle = GetStdHandle( STD_OUTPUT_HANDLE );
|
|
|
|
stderr_handle = GetStdHandle( STD_ERROR_HANDLE );
|
|
|
|
if (stdout_handle != INVALID_HANDLE_VALUE) {
|
|
|
|
SetHandleInformation( stdout_handle, HANDLE_FLAG_INHERIT, 0 );
|
|
|
|
}
|
|
|
|
if (stderr_handle != INVALID_HANDLE_VALUE) {
|
|
|
|
SetHandleInformation( stderr_handle, HANDLE_FLAG_INHERIT, 0 );
|
|
|
|
}
|
|
|
|
|
2009-03-04 11:32:55 +08:00
|
|
|
ZeroMemory( &startup, sizeof(startup) );
|
|
|
|
startup.cb = sizeof(startup);
|
|
|
|
startup.hStdInput = GetStdHandle( STD_INPUT_HANDLE );
|
|
|
|
startup.hStdOutput = pipe_write;
|
|
|
|
startup.hStdError = GetStdHandle( STD_ERROR_HANDLE );
|
|
|
|
startup.dwFlags = STARTF_USESTDHANDLES;
|
|
|
|
|
|
|
|
ZeroMemory( &pinfo, sizeof(pinfo) );
|
|
|
|
|
|
|
|
/* get path of current program */
|
|
|
|
GetModuleFileName( NULL, program_path, sizeof(program_path) );
|
2013-11-13 16:23:37 +08:00
|
|
|
char args[64];
|
|
|
|
snprintf(args, sizeof(args), "adb -P %d fork-server server", server_port);
|
2009-03-04 11:32:55 +08:00
|
|
|
ret = CreateProcess(
|
|
|
|
program_path, /* program path */
|
2013-11-13 16:23:37 +08:00
|
|
|
args,
|
2009-03-04 11:32:55 +08:00
|
|
|
/* the fork-server argument will set the
|
|
|
|
debug = 2 in the child */
|
|
|
|
NULL, /* process handle is not inheritable */
|
|
|
|
NULL, /* thread handle is not inheritable */
|
|
|
|
TRUE, /* yes, inherit some handles */
|
|
|
|
DETACHED_PROCESS, /* the new process doesn't have a console */
|
|
|
|
NULL, /* use parent's environment block */
|
|
|
|
NULL, /* use parent's starting directory */
|
|
|
|
&startup, /* startup info, i.e. std handles */
|
|
|
|
&pinfo );
|
|
|
|
|
|
|
|
CloseHandle( pipe_write );
|
|
|
|
|
|
|
|
if (!ret) {
|
|
|
|
fprintf(stderr, "CreateProcess failure, error %ld\n", GetLastError() );
|
|
|
|
CloseHandle( pipe_read );
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
CloseHandle( pinfo.hProcess );
|
|
|
|
CloseHandle( pinfo.hThread );
|
|
|
|
|
|
|
|
/* wait for the "OK\n" message */
|
|
|
|
{
|
|
|
|
char temp[3];
|
|
|
|
DWORD count;
|
|
|
|
|
|
|
|
ret = ReadFile( pipe_read, temp, 3, &count, NULL );
|
|
|
|
CloseHandle( pipe_read );
|
|
|
|
if ( !ret ) {
|
|
|
|
fprintf(stderr, "could not read ok from ADB Server, error = %ld\n", GetLastError() );
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (count != 3 || temp[0] != 'O' || temp[1] != 'K' || temp[2] != '\n') {
|
|
|
|
fprintf(stderr, "ADB server didn't ACK\n" );
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
2014-11-12 01:24:11 +08:00
|
|
|
#else /* !defined(_WIN32) */
|
2009-03-04 11:32:55 +08:00
|
|
|
char path[PATH_MAX];
|
|
|
|
int fd[2];
|
|
|
|
|
|
|
|
// set up a pipe so the child can tell us when it is ready.
|
|
|
|
// fd[0] will be parent's end, and fd[1] will get mapped to stderr in the child.
|
|
|
|
if (pipe(fd)) {
|
|
|
|
fprintf(stderr, "pipe failed in launch_server, errno: %d\n", errno);
|
|
|
|
return -1;
|
|
|
|
}
|
2009-10-21 23:55:00 +08:00
|
|
|
get_my_path(path, PATH_MAX);
|
2009-03-04 11:32:55 +08:00
|
|
|
pid_t pid = fork();
|
|
|
|
if(pid < 0) return -1;
|
|
|
|
|
|
|
|
if (pid == 0) {
|
|
|
|
// child side of the fork
|
|
|
|
|
|
|
|
// redirect stderr to the pipe
|
|
|
|
// we use stderr instead of stdout due to stdout's buffering behavior.
|
|
|
|
adb_close(fd[0]);
|
|
|
|
dup2(fd[1], STDERR_FILENO);
|
|
|
|
adb_close(fd[1]);
|
|
|
|
|
2012-11-15 02:16:17 +08:00
|
|
|
char str_port[30];
|
|
|
|
snprintf(str_port, sizeof(str_port), "%d", server_port);
|
2009-03-04 11:32:55 +08:00
|
|
|
// child process
|
2012-11-15 02:16:17 +08:00
|
|
|
int result = execl(path, "adb", "-P", str_port, "fork-server", "server", NULL);
|
2009-03-04 11:32:55 +08:00
|
|
|
// this should not return
|
|
|
|
fprintf(stderr, "OOPS! execl returned %d, errno: %d\n", result, errno);
|
|
|
|
} else {
|
|
|
|
// parent side of the fork
|
|
|
|
|
|
|
|
char temp[3];
|
|
|
|
|
|
|
|
temp[0] = 'A'; temp[1] = 'B'; temp[2] = 'C';
|
|
|
|
// wait for the "OK\n" message
|
|
|
|
adb_close(fd[1]);
|
|
|
|
int ret = adb_read(fd[0], temp, 3);
|
2011-03-17 06:57:42 +08:00
|
|
|
int saved_errno = errno;
|
2009-03-04 11:32:55 +08:00
|
|
|
adb_close(fd[0]);
|
|
|
|
if (ret < 0) {
|
2011-03-17 06:57:42 +08:00
|
|
|
fprintf(stderr, "could not read ok from ADB Server, errno = %d\n", saved_errno);
|
2009-03-04 11:32:55 +08:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (ret != 3 || temp[0] != 'O' || temp[1] != 'K' || temp[2] != '\n') {
|
|
|
|
fprintf(stderr, "ADB server didn't ACK\n" );
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
setsid();
|
|
|
|
}
|
2014-11-12 01:24:11 +08:00
|
|
|
#endif /* !defined(_WIN32) */
|
2009-03-04 11:32:55 +08:00
|
|
|
return 0;
|
|
|
|
}
|
2014-11-12 01:24:11 +08:00
|
|
|
#endif /* ADB_HOST */
|
2009-03-04 11:32:55 +08:00
|
|
|
|
2013-03-22 04:07:42 +08:00
|
|
|
// Try to handle a network forwarding request.
|
|
|
|
// This returns 1 on success, 0 on failure, and -1 to indicate this is not
|
|
|
|
// a forwarding-related request.
|
2015-05-08 12:38:41 +08:00
|
|
|
int handle_forward_request(const char* service, TransportType type, const char* serial, int reply_fd)
|
2013-03-22 04:07:42 +08:00
|
|
|
{
|
|
|
|
if (!strcmp(service, "list-forward")) {
|
|
|
|
// Create the list of forward redirections.
|
2015-05-01 08:32:03 +08:00
|
|
|
std::string listeners = format_listeners();
|
2013-03-22 04:07:42 +08:00
|
|
|
#if ADB_HOST
|
2015-05-01 08:32:03 +08:00
|
|
|
SendOkay(reply_fd);
|
2013-03-22 04:07:42 +08:00
|
|
|
#endif
|
2015-05-01 08:32:03 +08:00
|
|
|
SendProtocolString(reply_fd, listeners);
|
2013-03-22 04:07:42 +08:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!strcmp(service, "killforward-all")) {
|
|
|
|
remove_all_listeners();
|
|
|
|
#if ADB_HOST
|
|
|
|
/* On the host: 1st OKAY is connect, 2nd OKAY is status */
|
2015-05-01 08:32:03 +08:00
|
|
|
SendOkay(reply_fd);
|
2013-03-22 04:07:42 +08:00
|
|
|
#endif
|
2015-05-01 08:32:03 +08:00
|
|
|
SendOkay(reply_fd);
|
2013-03-22 04:07:42 +08:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!strncmp(service, "forward:",8) ||
|
|
|
|
!strncmp(service, "killforward:",12)) {
|
2015-02-26 09:51:28 +08:00
|
|
|
char *local, *remote;
|
2013-03-22 04:07:42 +08:00
|
|
|
atransport *transport;
|
|
|
|
|
|
|
|
int createForward = strncmp(service, "kill", 4);
|
|
|
|
int no_rebind = 0;
|
|
|
|
|
|
|
|
local = strchr(service, ':') + 1;
|
|
|
|
|
|
|
|
// Handle forward:norebind:<local>... here
|
|
|
|
if (createForward && !strncmp(local, "norebind:", 9)) {
|
|
|
|
no_rebind = 1;
|
|
|
|
local = strchr(local, ':') + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
remote = strchr(local,';');
|
|
|
|
|
|
|
|
if (createForward) {
|
|
|
|
// Check forward: parameter format: '<local>;<remote>'
|
|
|
|
if(remote == 0) {
|
2015-05-01 08:32:03 +08:00
|
|
|
SendFail(reply_fd, "malformed forward spec");
|
2013-03-22 04:07:42 +08:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
*remote++ = 0;
|
|
|
|
if((local[0] == 0) || (remote[0] == 0) || (remote[0] == '*')) {
|
2015-05-01 08:32:03 +08:00
|
|
|
SendFail(reply_fd, "malformed forward spec");
|
2013-03-22 04:07:42 +08:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Check killforward: parameter format: '<local>'
|
|
|
|
if (local[0] == 0) {
|
2015-05-01 08:32:03 +08:00
|
|
|
SendFail(reply_fd, "malformed forward spec");
|
2013-03-22 04:07:42 +08:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-17 13:54:44 +08:00
|
|
|
std::string error_msg;
|
2015-05-06 04:10:43 +08:00
|
|
|
transport = acquire_one_transport(CS_ANY, type, serial, &error_msg);
|
2013-03-22 04:07:42 +08:00
|
|
|
if (!transport) {
|
2015-05-01 08:32:03 +08:00
|
|
|
SendFail(reply_fd, error_msg);
|
2013-03-22 04:07:42 +08:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2015-05-06 04:10:43 +08:00
|
|
|
InstallStatus r;
|
2013-03-22 04:07:42 +08:00
|
|
|
if (createForward) {
|
|
|
|
r = install_listener(local, remote, transport, no_rebind);
|
|
|
|
} else {
|
|
|
|
r = remove_listener(local, transport);
|
|
|
|
}
|
2015-04-20 23:09:20 +08:00
|
|
|
if (r == INSTALL_STATUS_OK) {
|
2013-03-22 04:07:42 +08:00
|
|
|
#if ADB_HOST
|
|
|
|
/* On the host: 1st OKAY is connect, 2nd OKAY is status */
|
2015-05-01 08:32:03 +08:00
|
|
|
SendOkay(reply_fd);
|
2013-03-22 04:07:42 +08:00
|
|
|
#endif
|
2015-05-01 08:32:03 +08:00
|
|
|
SendOkay(reply_fd);
|
2013-03-22 04:07:42 +08:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2015-04-20 23:09:20 +08:00
|
|
|
std::string message;
|
|
|
|
switch (r) {
|
|
|
|
case INSTALL_STATUS_OK: message = " "; break;
|
|
|
|
case INSTALL_STATUS_INTERNAL_ERROR: message = "internal error"; break;
|
|
|
|
case INSTALL_STATUS_CANNOT_BIND:
|
|
|
|
message = android::base::StringPrintf("cannot bind to socket: %s", strerror(errno));
|
|
|
|
break;
|
|
|
|
case INSTALL_STATUS_CANNOT_REBIND:
|
|
|
|
message = android::base::StringPrintf("cannot rebind existing socket: %s", strerror(errno));
|
|
|
|
break;
|
|
|
|
case INSTALL_STATUS_LISTENER_NOT_FOUND: message = "listener not found"; break;
|
2013-03-22 04:07:42 +08:00
|
|
|
}
|
2015-05-01 08:32:03 +08:00
|
|
|
SendFail(reply_fd, message);
|
2013-03-22 04:07:42 +08:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-05-09 07:08:57 +08:00
|
|
|
int handle_host_request(const char* service, TransportType type, const char* serial, int reply_fd, asocket *s)
|
|
|
|
{
|
|
|
|
if(!strcmp(service, "kill")) {
|
|
|
|
fprintf(stderr,"adb server killed by remote request\n");
|
2009-03-04 11:32:55 +08:00
|
|
|
fflush(stdout);
|
2015-05-01 08:32:03 +08:00
|
|
|
SendOkay(reply_fd);
|
2015-05-09 07:08:57 +08:00
|
|
|
usb_cleanup();
|
2009-03-04 11:32:55 +08:00
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
#if ADB_HOST
|
2014-09-06 06:38:15 +08:00
|
|
|
atransport *transport = NULL;
|
2009-03-04 11:32:55 +08:00
|
|
|
// "transport:" is used for switching transport with a specified serial number
|
|
|
|
// "transport-usb:" is used for switching transport to the only USB transport
|
|
|
|
// "transport-local:" is used for switching transport to the only local transport
|
|
|
|
// "transport-any:" is used for switching transport to the only transport
|
|
|
|
if (!strncmp(service, "transport", strlen("transport"))) {
|
2015-05-06 04:10:43 +08:00
|
|
|
TransportType type = kTransportAny;
|
2009-03-04 11:32:55 +08:00
|
|
|
|
|
|
|
if (!strncmp(service, "transport-usb", strlen("transport-usb"))) {
|
|
|
|
type = kTransportUsb;
|
|
|
|
} else if (!strncmp(service, "transport-local", strlen("transport-local"))) {
|
|
|
|
type = kTransportLocal;
|
|
|
|
} else if (!strncmp(service, "transport-any", strlen("transport-any"))) {
|
|
|
|
type = kTransportAny;
|
|
|
|
} else if (!strncmp(service, "transport:", strlen("transport:"))) {
|
|
|
|
service += strlen("transport:");
|
2011-07-28 01:56:14 +08:00
|
|
|
serial = service;
|
2009-03-04 11:32:55 +08:00
|
|
|
}
|
|
|
|
|
2015-04-17 13:54:44 +08:00
|
|
|
std::string error_msg = "unknown failure";
|
|
|
|
transport = acquire_one_transport(CS_ANY, type, serial, &error_msg);
|
2009-03-04 11:32:55 +08:00
|
|
|
|
|
|
|
if (transport) {
|
|
|
|
s->transport = transport;
|
2015-05-01 08:32:03 +08:00
|
|
|
SendOkay(reply_fd);
|
2009-03-04 11:32:55 +08:00
|
|
|
} else {
|
2015-05-01 08:32:03 +08:00
|
|
|
SendFail(reply_fd, error_msg);
|
2009-03-04 11:32:55 +08:00
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// return a list of all connected devices
|
2012-04-21 02:21:14 +08:00
|
|
|
if (!strncmp(service, "devices", 7)) {
|
2015-05-01 08:32:03 +08:00
|
|
|
bool long_listing = (strcmp(service+7, "-l") == 0);
|
|
|
|
if (long_listing || service[7] == 0) {
|
|
|
|
D("Getting device list...\n");
|
|
|
|
std::string device_list = list_transports(long_listing);
|
|
|
|
D("Sending device list...\n");
|
|
|
|
SendOkay(reply_fd);
|
|
|
|
SendProtocolString(reply_fd, device_list);
|
2012-04-21 02:21:14 +08:00
|
|
|
return 0;
|
|
|
|
}
|
2015-05-01 08:32:03 +08:00
|
|
|
return 1;
|
2009-03-04 11:32:55 +08:00
|
|
|
}
|
|
|
|
|
2009-10-12 11:04:18 +08:00
|
|
|
// remove TCP transport
|
|
|
|
if (!strncmp(service, "disconnect:", 11)) {
|
|
|
|
char buffer[4096];
|
|
|
|
memset(buffer, 0, sizeof(buffer));
|
2015-05-08 12:38:41 +08:00
|
|
|
const char* serial = service + 11;
|
2010-05-24 22:44:35 +08:00
|
|
|
if (serial[0] == 0) {
|
|
|
|
// disconnect from all TCP devices
|
|
|
|
unregister_all_tcp_transports();
|
2009-10-12 11:04:18 +08:00
|
|
|
} else {
|
2010-05-24 22:44:35 +08:00
|
|
|
char hostbuf[100];
|
|
|
|
// assume port 5555 if no port is specified
|
|
|
|
if (!strchr(serial, ':')) {
|
|
|
|
snprintf(hostbuf, sizeof(hostbuf) - 1, "%s:5555", serial);
|
|
|
|
serial = hostbuf;
|
|
|
|
}
|
|
|
|
atransport *t = find_transport(serial);
|
|
|
|
|
|
|
|
if (t) {
|
|
|
|
unregister_transport(t);
|
|
|
|
} else {
|
|
|
|
snprintf(buffer, sizeof(buffer), "No such device %s", serial);
|
|
|
|
}
|
2009-10-12 11:04:18 +08:00
|
|
|
}
|
|
|
|
|
2015-05-01 08:32:03 +08:00
|
|
|
SendOkay(reply_fd);
|
|
|
|
SendProtocolString(reply_fd, buffer);
|
2009-08-25 06:58:40 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-03-04 11:32:55 +08:00
|
|
|
// returns our value for ADB_SERVER_VERSION
|
|
|
|
if (!strcmp(service, "version")) {
|
2015-05-01 08:32:03 +08:00
|
|
|
SendOkay(reply_fd);
|
|
|
|
SendProtocolString(reply_fd, android::base::StringPrintf("%04x", ADB_SERVER_VERSION));
|
2009-03-04 11:32:55 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!strncmp(service,"get-serialno",strlen("get-serialno"))) {
|
2015-02-26 09:51:28 +08:00
|
|
|
const char *out = "unknown";
|
2015-05-06 04:10:43 +08:00
|
|
|
transport = acquire_one_transport(CS_ANY, type, serial, NULL);
|
2015-04-17 13:54:44 +08:00
|
|
|
if (transport && transport->serial) {
|
2009-03-04 11:32:55 +08:00
|
|
|
out = transport->serial;
|
|
|
|
}
|
2015-05-01 08:32:03 +08:00
|
|
|
SendOkay(reply_fd);
|
|
|
|
SendProtocolString(reply_fd, out);
|
2009-03-04 11:32:55 +08:00
|
|
|
return 0;
|
|
|
|
}
|
2012-04-21 02:21:14 +08:00
|
|
|
if(!strncmp(service,"get-devpath",strlen("get-devpath"))) {
|
2015-02-26 09:51:28 +08:00
|
|
|
const char *out = "unknown";
|
2015-05-06 04:10:43 +08:00
|
|
|
transport = acquire_one_transport(CS_ANY, type, serial, NULL);
|
2015-04-17 13:54:44 +08:00
|
|
|
if (transport && transport->devpath) {
|
2012-04-21 02:21:14 +08:00
|
|
|
out = transport->devpath;
|
|
|
|
}
|
2015-05-01 08:32:03 +08:00
|
|
|
SendOkay(reply_fd);
|
|
|
|
SendProtocolString(reply_fd, out);
|
2012-04-21 02:21:14 +08:00
|
|
|
return 0;
|
|
|
|
}
|
2009-03-04 11:32:55 +08:00
|
|
|
// indicates a new emulator instance has started
|
|
|
|
if (!strncmp(service,"emulator:",9)) {
|
|
|
|
int port = atoi(service+9);
|
|
|
|
local_connect(port);
|
|
|
|
/* we don't even need to send a reply */
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!strncmp(service,"get-state",strlen("get-state"))) {
|
2015-05-06 04:10:43 +08:00
|
|
|
transport = acquire_one_transport(CS_ANY, type, serial, NULL);
|
2015-05-01 08:32:03 +08:00
|
|
|
SendOkay(reply_fd);
|
|
|
|
SendProtocolString(reply_fd, transport->connection_state_name());
|
2009-03-04 11:32:55 +08:00
|
|
|
return 0;
|
|
|
|
}
|
2014-07-15 08:23:06 +08:00
|
|
|
#endif // ADB_HOST
|
|
|
|
|
2015-05-06 04:10:43 +08:00
|
|
|
int ret = handle_forward_request(service, type, serial, reply_fd);
|
2014-07-15 08:23:06 +08:00
|
|
|
if (ret >= 0)
|
|
|
|
return ret - 1;
|
2009-03-04 11:32:55 +08:00
|
|
|
return -1;
|
|
|
|
}
|