OLD | NEW |
(Empty) | |
| 1 #!/usr/bin/env python |
| 2 # Copyright 2015 The Chromium Authors. All rights reserved. |
| 3 # Use of this source code is governed by a BSD-style license that can be |
| 4 # found in the LICENSE file. |
| 5 |
| 6 """Push host key onto Android devices using public userdebug key. |
| 7 |
| 8 If the device has the chrome-infrastructure private adb key on it, use |
| 9 ADB_VENDOR_KEYS to push the host-specific private key onto the devices that are |
| 10 not authorized. |
| 11 """ |
| 12 |
| 13 import argparse |
| 14 import os |
| 15 import subprocess |
| 16 import sys |
| 17 import tempfile |
| 18 |
| 19 ADB_KEYS_PATH = '/data/misc/adb/adb_keys' |
| 20 |
| 21 |
| 22 def GetCmdOutput(cmd, env=None): |
| 23 env = {} if not env else env |
| 24 env['HOME'] = os.environ['HOME'] |
| 25 stdout, stderr = subprocess.Popen(cmd, stdout=subprocess.PIPE, |
| 26 stderr=subprocess.PIPE, |
| 27 env=env).communicate() |
| 28 output = stdout + stderr |
| 29 return output |
| 30 |
| 31 |
| 32 def PushHostPubkey(device, adb, env, public_keys_set): |
| 33 GetCmdOutput([adb, 'devices'], env) |
| 34 GetCmdOutput([adb, '-s', device, 'root'], env) |
| 35 adb_ls_output = GetCmdOutput([adb, '-s', device, 'shell', 'ls', ADB_KEYS_PATH], |
| 36 env) |
| 37 # If adb_keys file exists on device add its contents to the public keys set. |
| 38 if adb_ls_output == ADB_KEYS_PATH: |
| 39 dev_keys = GetCmdOutput([adb, '-s', device, 'shell', 'cat', ADB_KEYS_PATH], |
| 40 env).splitlines() |
| 41 public_keys_set.update(dev_keys) |
| 42 |
| 43 with tempfile.NamedTemporaryFile() as f: |
| 44 f.write('\n'.join(public_keys_set)) |
| 45 f.flush() |
| 46 GetCmdOutput([adb, '-s', device, 'push', f.name, ADB_KEYS_PATH], env) |
| 47 |
| 48 |
| 49 def GetUnauthorizedDevices(adb): |
| 50 output = GetCmdOutput([adb, 'devices']).splitlines() |
| 51 return [line.split('\t')[0] for line in output if '\tunauthorized' in line] |
| 52 |
| 53 |
| 54 def main(argv): |
| 55 parser = argparse.ArgumentParser() |
| 56 parser.add_argument('--adb-path', help='Path to adb binary.', default='adb') |
| 57 parser.add_argument('--adb-keys-dir', |
| 58 help='Point to directory that contains adb keys.', |
| 59 default=os.path.join(os.environ['HOME'], '.android')) |
| 60 options = parser.parse_args() |
| 61 dir_contents = os.listdir(options.adb_keys_dir) |
| 62 adb_path = options.adb_path |
| 63 private_key_files = [os.path.join(options.adb_keys_dir, key) |
| 64 for key in dir_contents if key.endswith('adbkey')] |
| 65 public_key_files = [os.path.join(options.adb_keys_dir, key) |
| 66 for key in dir_contents if key.endswith('adbkey.pub')] |
| 67 public_keys_set = set() |
| 68 for public_key_file in public_key_files: |
| 69 with open(public_key_file, 'r') as f: |
| 70 public_keys_set.add(f.read().strip()) |
| 71 |
| 72 # Kill server and find unauthorized devices without ADB_VENDOR_KEYS |
| 73 GetCmdOutput([adb_path, 'kill-server'], env={}).splitlines() |
| 74 unauthorized_devices = GetUnauthorizedDevices(adb_path) |
| 75 |
| 76 # Kill server launched with ADB_VENDOR_KEYS |
| 77 GetCmdOutput([adb_path, 'kill-server']).splitlines() |
| 78 env = ({'ADB_VENDOR_KEYS': ':'.join(private_key_files)} if private_key_files |
| 79 else {}) |
| 80 for device in unauthorized_devices: |
| 81 PushHostPubkey(device, adb_path, env, public_keys_set) |
| 82 |
| 83 |
| 84 if __name__ == '__main__': |
| 85 sys.exit(main(sys.argv)) |
OLD | NEW |