Frank Farzan | d5e3631 | 2012-01-13 14:34:03 -0800 | [diff] [blame] | 1 | # Copyright (c) 2012 The Chromium OS Authors. All rights reserved. |
Chris Masone | 5e06f18 | 2010-03-23 08:29:52 -0700 | [diff] [blame] | 2 | # Use of this source code is governed by a BSD-style license that can be |
| 3 | # found in the LICENSE file. |
| 4 | |
Alexis Savery | ccb16be | 2017-02-01 16:23:15 -0800 | [diff] [blame^] | 5 | import dbus, gobject, logging, os, random, re, shutil, string, time |
Will Drewry | 9e44079 | 2013-12-11 17:18:35 -0600 | [diff] [blame] | 6 | from dbus.mainloop.glib import DBusGMainLoop |
barfab@chromium.org | b6d2993 | 2012-04-11 09:46:43 +0200 | [diff] [blame] | 7 | |
Hsinyu Chao | e0b08e6 | 2015-08-11 10:50:37 +0000 | [diff] [blame] | 8 | import common, constants |
barfab@chromium.org | 5c37463 | 2012-04-05 16:50:56 +0200 | [diff] [blame] | 9 | from autotest_lib.client.bin import utils |
Chris Masone | 5e06f18 | 2010-03-23 08:29:52 -0700 | [diff] [blame] | 10 | from autotest_lib.client.common_lib import error |
Will Drewry | 9e44079 | 2013-12-11 17:18:35 -0600 | [diff] [blame] | 11 | from autotest_lib.client.cros.cros_disks import DBusClient |
Eric Li | c4d8f4a | 2010-12-10 09:49:23 -0800 | [diff] [blame] | 12 | |
Sean O | e5d8fd0 | 2010-09-30 10:44:44 +0200 | [diff] [blame] | 13 | CRYPTOHOME_CMD = '/usr/sbin/cryptohome' |
Elly Fong-Jones | 6cb26ad | 2013-05-21 12:09:23 -0400 | [diff] [blame] | 14 | GUEST_USER_NAME = '$guest' |
Kris Rambish | bb5258c | 2014-12-16 16:51:17 -0800 | [diff] [blame] | 15 | UNAVAILABLE_ACTION = 'Unknown action or no action given.' |
Alexis Savery | ccb16be | 2017-02-01 16:23:15 -0800 | [diff] [blame^] | 16 | MOUNT_RETRY_COUNT = 20 |
Sean O | e5d8fd0 | 2010-09-30 10:44:44 +0200 | [diff] [blame] | 17 | |
Chris Masone | 5d010aa | 2013-05-06 14:38:42 -0700 | [diff] [blame] | 18 | class ChromiumOSError(error.TestError): |
Sean O | e5d8fd0 | 2010-09-30 10:44:44 +0200 | [diff] [blame] | 19 | """Generic error for ChromiumOS-specific exceptions.""" |
| 20 | pass |
| 21 | |
Sean O | e5d8fd0 | 2010-09-30 10:44:44 +0200 | [diff] [blame] | 22 | def __run_cmd(cmd): |
| 23 | return utils.system_output(cmd + ' 2>&1', retain_output=True, |
| 24 | ignore_status=True).strip() |
| 25 | |
Sean O | e5d8fd0 | 2010-09-30 10:44:44 +0200 | [diff] [blame] | 26 | def get_user_hash(user): |
barfab@chromium.org | 5c37463 | 2012-04-05 16:50:56 +0200 | [diff] [blame] | 27 | """Get the user hash for the given user.""" |
Elly Fong-Jones | 6cb26ad | 2013-05-21 12:09:23 -0400 | [diff] [blame] | 28 | return utils.system_output(['cryptohome', '--action=obfuscate_user', |
| 29 | '--user=%s' % user]) |
Sean O | e5d8fd0 | 2010-09-30 10:44:44 +0200 | [diff] [blame] | 30 | |
| 31 | |
barfab@chromium.org | 5c37463 | 2012-04-05 16:50:56 +0200 | [diff] [blame] | 32 | def user_path(user): |
| 33 | """Get the user mount point for the given user.""" |
Elly Fong-Jones | 6cb26ad | 2013-05-21 12:09:23 -0400 | [diff] [blame] | 34 | return utils.system_output(['cryptohome-path', 'user', user]) |
barfab@chromium.org | 5c37463 | 2012-04-05 16:50:56 +0200 | [diff] [blame] | 35 | |
| 36 | |
| 37 | def system_path(user): |
| 38 | """Get the system mount point for the given user.""" |
Elly Fong-Jones | 6cb26ad | 2013-05-21 12:09:23 -0400 | [diff] [blame] | 39 | return utils.system_output(['cryptohome-path', 'system', user]) |
barfab@chromium.org | 5c37463 | 2012-04-05 16:50:56 +0200 | [diff] [blame] | 40 | |
| 41 | |
Chris Masone | 5d010aa | 2013-05-06 14:38:42 -0700 | [diff] [blame] | 42 | def ensure_clean_cryptohome_for(user, password=None): |
| 43 | """Ensure a fresh cryptohome exists for user. |
| 44 | |
| 45 | @param user: user who needs a shiny new cryptohome. |
| 46 | @param password: if unset, a random password will be used. |
| 47 | """ |
| 48 | if not password: |
| 49 | password = ''.join(random.sample(string.ascii_lowercase, 6)) |
| 50 | remove_vault(user) |
| 51 | mount_vault(user, password, create=True) |
| 52 | |
| 53 | |
Frank Farzan | d5e3631 | 2012-01-13 14:34:03 -0800 | [diff] [blame] | 54 | def get_tpm_status(): |
| 55 | """Get the TPM status. |
| 56 | |
| 57 | Returns: |
| 58 | A TPM status dictionary, for example: |
| 59 | { 'Enabled': True, |
| 60 | 'Owned': True, |
| 61 | 'Being Owned': False, |
| 62 | 'Ready': True, |
| 63 | 'Password': '' |
| 64 | } |
| 65 | """ |
| 66 | out = __run_cmd(CRYPTOHOME_CMD + ' --action=tpm_status') |
| 67 | status = {} |
| 68 | for field in ['Enabled', 'Owned', 'Being Owned', 'Ready']: |
| 69 | match = re.search('TPM %s: (true|false)' % field, out) |
| 70 | if not match: |
| 71 | raise ChromiumOSError('Invalid TPM status: "%s".' % out) |
| 72 | status[field] = match.group(1) == 'true' |
| 73 | match = re.search('TPM Password: (\w*)', out) |
| 74 | status['Password'] = '' |
| 75 | if match: |
| 76 | status['Password'] = match.group(1) |
| 77 | return status |
| 78 | |
| 79 | |
Kris Rambish | 82ee1c0 | 2014-12-10 17:02:39 -0800 | [diff] [blame] | 80 | def get_tpm_more_status(): |
| 81 | """Get more of the TPM status. |
| 82 | |
| 83 | Returns: |
| 84 | A TPM more status dictionary, for example: |
| 85 | { 'dictionary_attack_lockout_in_effect': False, |
| 86 | 'attestation_prepared': False, |
| 87 | 'boot_lockbox_finalized': False, |
| 88 | 'enabled': True, |
| 89 | 'owned': True, |
Kris Rambish | be13259 | 2014-12-17 14:26:06 -0800 | [diff] [blame] | 90 | 'owner_password': '' |
Kris Rambish | 82ee1c0 | 2014-12-10 17:02:39 -0800 | [diff] [blame] | 91 | 'dictionary_attack_counter': 0, |
| 92 | 'dictionary_attack_lockout_seconds_remaining': 0, |
| 93 | 'dictionary_attack_threshold': 10, |
| 94 | 'attestation_enrolled': False, |
| 95 | 'initialized': True, |
| 96 | 'verified_boot_measured': False, |
| 97 | 'install_lockbox_finalized': True |
| 98 | } |
Kris Rambish | bb5258c | 2014-12-16 16:51:17 -0800 | [diff] [blame] | 99 | An empty dictionary is returned if the command is not supported. |
Kris Rambish | 82ee1c0 | 2014-12-10 17:02:39 -0800 | [diff] [blame] | 100 | """ |
Kris Rambish | 82ee1c0 | 2014-12-10 17:02:39 -0800 | [diff] [blame] | 101 | status = {} |
Kris Rambish | bb5258c | 2014-12-16 16:51:17 -0800 | [diff] [blame] | 102 | out = __run_cmd(CRYPTOHOME_CMD + ' --action=tpm_more_status | grep :') |
| 103 | if out.startswith(UNAVAILABLE_ACTION): |
| 104 | # --action=tpm_more_status only exists >= 41. |
| 105 | logging.info('Method not supported!') |
| 106 | return status |
Kris Rambish | 82ee1c0 | 2014-12-10 17:02:39 -0800 | [diff] [blame] | 107 | for line in out.splitlines(): |
| 108 | items = line.strip().split(':') |
| 109 | if items[1].strip() == 'false': |
| 110 | value = False |
| 111 | elif items[1].strip() == 'true': |
| 112 | value = True |
Kris Rambish | be13259 | 2014-12-17 14:26:06 -0800 | [diff] [blame] | 113 | elif items[1].strip().isdigit(): |
Kris Rambish | 82ee1c0 | 2014-12-10 17:02:39 -0800 | [diff] [blame] | 114 | value = int(items[1].strip()) |
Kris Rambish | be13259 | 2014-12-17 14:26:06 -0800 | [diff] [blame] | 115 | else: |
| 116 | value = items[1].strip(' "') |
Kris Rambish | 82ee1c0 | 2014-12-10 17:02:39 -0800 | [diff] [blame] | 117 | status[items[0]] = value |
| 118 | return status |
| 119 | |
| 120 | |
| 121 | def is_tpm_lockout_in_effect(): |
| 122 | """Returns true if the TPM lockout is in effect; false otherwise.""" |
| 123 | status = get_tpm_more_status() |
Christopher Wiley | 94fd6b3 | 2014-12-13 18:52:03 -0800 | [diff] [blame] | 124 | return status.get('dictionary_attack_lockout_in_effect', None) |
Kris Rambish | 82ee1c0 | 2014-12-10 17:02:39 -0800 | [diff] [blame] | 125 | |
| 126 | |
David Pursell | 2a2ef34 | 2014-10-17 10:34:56 -0700 | [diff] [blame] | 127 | def get_login_status(): |
| 128 | """Query the login status |
| 129 | |
| 130 | Returns: |
| 131 | A login status dictionary containing: |
| 132 | { 'owner_user_exists': True|False, |
| 133 | 'boot_lockbox_finalized': True|False |
| 134 | } |
| 135 | """ |
| 136 | out = __run_cmd(CRYPTOHOME_CMD + ' --action=get_login_status') |
| 137 | status = {} |
| 138 | for field in ['owner_user_exists', 'boot_lockbox_finalized']: |
| 139 | match = re.search('%s: (true|false)' % field, out) |
| 140 | if not match: |
| 141 | raise ChromiumOSError('Invalid login status: "%s".' % out) |
| 142 | status[field] = match.group(1) == 'true' |
| 143 | return status |
| 144 | |
| 145 | |
Darren Krahn | 5f880f6 | 2012-10-02 15:17:59 -0700 | [diff] [blame] | 146 | def get_tpm_attestation_status(): |
| 147 | """Get the TPM attestation status. Works similar to get_tpm_status(). |
| 148 | """ |
| 149 | out = __run_cmd(CRYPTOHOME_CMD + ' --action=tpm_attestation_status') |
| 150 | status = {} |
| 151 | for field in ['Prepared', 'Enrolled']: |
| 152 | match = re.search('Attestation %s: (true|false)' % field, out) |
| 153 | if not match: |
| 154 | raise ChromiumOSError('Invalid attestation status: "%s".' % out) |
| 155 | status[field] = match.group(1) == 'true' |
| 156 | return status |
| 157 | |
| 158 | |
Frank Farzan | d5e3631 | 2012-01-13 14:34:03 -0800 | [diff] [blame] | 159 | def take_tpm_ownership(): |
| 160 | """Take TPM owernship. |
| 161 | |
| 162 | Blocks until TPM is owned. |
| 163 | """ |
| 164 | __run_cmd(CRYPTOHOME_CMD + ' --action=tpm_take_ownership') |
| 165 | __run_cmd(CRYPTOHOME_CMD + ' --action=tpm_wait_ownership') |
| 166 | |
| 167 | |
Darren Krahn | 0e73e7f | 2012-09-05 15:35:15 -0700 | [diff] [blame] | 168 | def verify_ek(): |
| 169 | """Verify the TPM endorsement key. |
| 170 | |
| 171 | Returns true if EK is valid. |
| 172 | """ |
| 173 | cmd = CRYPTOHOME_CMD + ' --action=tpm_verify_ek' |
| 174 | return (utils.system(cmd, ignore_status=True) == 0) |
| 175 | |
| 176 | |
Sean O | e5d8fd0 | 2010-09-30 10:44:44 +0200 | [diff] [blame] | 177 | def remove_vault(user): |
barfab@chromium.org | 5c37463 | 2012-04-05 16:50:56 +0200 | [diff] [blame] | 178 | """Remove the given user's vault from the shadow directory.""" |
Sean O | e5d8fd0 | 2010-09-30 10:44:44 +0200 | [diff] [blame] | 179 | logging.debug('user is %s', user) |
| 180 | user_hash = get_user_hash(user) |
barfab@chromium.org | 5c37463 | 2012-04-05 16:50:56 +0200 | [diff] [blame] | 181 | logging.debug('Removing vault for user %s with hash %s' % (user, user_hash)) |
Sean O | e5d8fd0 | 2010-09-30 10:44:44 +0200 | [diff] [blame] | 182 | cmd = CRYPTOHOME_CMD + ' --action=remove --force --user=%s' % user |
| 183 | __run_cmd(cmd) |
barfab@chromium.org | 5c37463 | 2012-04-05 16:50:56 +0200 | [diff] [blame] | 184 | # Ensure that the vault does not exist. |
| 185 | if os.path.exists(os.path.join(constants.SHADOW_ROOT, user_hash)): |
Darren Krahn | e6c44b9 | 2014-03-31 12:11:08 -0700 | [diff] [blame] | 186 | raise ChromiumOSError('Cryptohome could not remove the user\'s vault.') |
Sean O | e5d8fd0 | 2010-09-30 10:44:44 +0200 | [diff] [blame] | 187 | |
| 188 | |
barfab@chromium.org | cf2151e | 2012-04-04 15:39:34 +0200 | [diff] [blame] | 189 | def remove_all_vaults(): |
| 190 | """Remove any existing vaults from the shadow directory. |
| 191 | |
| 192 | This function must be run with root privileges. |
| 193 | """ |
barfab@chromium.org | 5c37463 | 2012-04-05 16:50:56 +0200 | [diff] [blame] | 194 | for item in os.listdir(constants.SHADOW_ROOT): |
| 195 | abs_item = os.path.join(constants.SHADOW_ROOT, item) |
barfab@chromium.org | cf2151e | 2012-04-04 15:39:34 +0200 | [diff] [blame] | 196 | if os.path.isdir(os.path.join(abs_item, 'vault')): |
| 197 | logging.debug('Removing vault for user with hash %s' % item) |
| 198 | shutil.rmtree(abs_item) |
| 199 | |
| 200 | |
Sean O | e5d8fd0 | 2010-09-30 10:44:44 +0200 | [diff] [blame] | 201 | def mount_vault(user, password, create=False): |
barfab@chromium.org | 5c37463 | 2012-04-05 16:50:56 +0200 | [diff] [blame] | 202 | """Mount the given user's vault.""" |
Elly Fong-Jones | 6cb26ad | 2013-05-21 12:09:23 -0400 | [diff] [blame] | 203 | args = [CRYPTOHOME_CMD, '--action=mount', '--user=%s' % user, |
Chris Masone | 3543e51 | 2013-11-04 13:09:30 -0800 | [diff] [blame] | 204 | '--password=%s' % password, '--async'] |
Sean O | e5d8fd0 | 2010-09-30 10:44:44 +0200 | [diff] [blame] | 205 | if create: |
Elly Fong-Jones | 6cb26ad | 2013-05-21 12:09:23 -0400 | [diff] [blame] | 206 | args.append('--create') |
Chris Masone | 3543e51 | 2013-11-04 13:09:30 -0800 | [diff] [blame] | 207 | logging.info(__run_cmd(' '.join(args))) |
barfab@chromium.org | 5c37463 | 2012-04-05 16:50:56 +0200 | [diff] [blame] | 208 | # Ensure that the vault exists in the shadow directory. |
Sean O | e5d8fd0 | 2010-09-30 10:44:44 +0200 | [diff] [blame] | 209 | user_hash = get_user_hash(user) |
barfab@chromium.org | 5c37463 | 2012-04-05 16:50:56 +0200 | [diff] [blame] | 210 | if not os.path.exists(os.path.join(constants.SHADOW_ROOT, user_hash)): |
Alexis Savery | ccb16be | 2017-02-01 16:23:15 -0800 | [diff] [blame^] | 211 | retry = 0 |
| 212 | mounted = False |
| 213 | while retry < MOUNT_RETRY_COUNT and not mounted: |
| 214 | time.sleep(1) |
| 215 | logging.info("Retry " + str(retry + 1)) |
| 216 | __run_cmd(' '.join(args)) |
| 217 | # TODO: Remove this additional call to get_user_hash(user) when |
| 218 | # crbug.com/690994 is fixed |
| 219 | user_hash = get_user_hash(user) |
| 220 | if os.path.exists(os.path.join(constants.SHADOW_ROOT, user_hash)): |
| 221 | mounted = True |
| 222 | retry += 1 |
| 223 | if not mounted: |
| 224 | raise ChromiumOSError('Cryptohome vault not found after mount.') |
barfab@chromium.org | 5c37463 | 2012-04-05 16:50:56 +0200 | [diff] [blame] | 225 | # Ensure that the vault is mounted. |
| 226 | if not is_vault_mounted( |
| 227 | user=user, |
| 228 | device_regex=constants.CRYPTOHOME_DEV_REGEX_REGULAR_USER, |
| 229 | allow_fail=True): |
| 230 | raise ChromiumOSError('Cryptohome created a vault but did not mount.') |
Sean O | e5d8fd0 | 2010-09-30 10:44:44 +0200 | [diff] [blame] | 231 | |
| 232 | |
Chris Masone | 5d010aa | 2013-05-06 14:38:42 -0700 | [diff] [blame] | 233 | def mount_guest(): |
| 234 | """Mount the given user's vault.""" |
Chris Masone | 3543e51 | 2013-11-04 13:09:30 -0800 | [diff] [blame] | 235 | args = [CRYPTOHOME_CMD, '--action=mount_guest', '--async'] |
| 236 | logging.info(__run_cmd(' '.join(args))) |
Chris Masone | 5d010aa | 2013-05-06 14:38:42 -0700 | [diff] [blame] | 237 | # Ensure that the guest tmpfs is mounted. |
| 238 | if not is_guest_vault_mounted(allow_fail=True): |
| 239 | raise ChromiumOSError('Cryptohome did not mount tmpfs.') |
| 240 | |
| 241 | |
Sean O | e5d8fd0 | 2010-09-30 10:44:44 +0200 | [diff] [blame] | 242 | def test_auth(user, password): |
Elly Fong-Jones | 6cb26ad | 2013-05-21 12:09:23 -0400 | [diff] [blame] | 243 | cmd = [CRYPTOHOME_CMD, '--action=test_auth', '--user=%s' % user, |
| 244 | '--password=%s' % password, '--async'] |
| 245 | return 'Authentication succeeded' in utils.system_output(cmd) |
Sean O | e5d8fd0 | 2010-09-30 10:44:44 +0200 | [diff] [blame] | 246 | |
| 247 | |
Elly Fong-Jones | 6cb26ad | 2013-05-21 12:09:23 -0400 | [diff] [blame] | 248 | def unmount_vault(user): |
barfab@chromium.org | 5c37463 | 2012-04-05 16:50:56 +0200 | [diff] [blame] | 249 | """Unmount the given user's vault. |
| 250 | |
| 251 | Once unmounting for a specific user is supported, the user parameter will |
| 252 | name the target user. See crosbug.com/20778. |
Elly Jones | 686c2f4 | 2011-10-24 16:45:07 -0400 | [diff] [blame] | 253 | """ |
Chris Masone | 3543e51 | 2013-11-04 13:09:30 -0800 | [diff] [blame] | 254 | __run_cmd(CRYPTOHOME_CMD + ' --action=unmount') |
barfab@chromium.org | 5c37463 | 2012-04-05 16:50:56 +0200 | [diff] [blame] | 255 | # Ensure that the vault is not mounted. |
Elly Fong-Jones | 6cb26ad | 2013-05-21 12:09:23 -0400 | [diff] [blame] | 256 | if is_vault_mounted(user, allow_fail=True): |
Sean O | e5d8fd0 | 2010-09-30 10:44:44 +0200 | [diff] [blame] | 257 | raise ChromiumOSError('Cryptohome did not unmount the user.') |
| 258 | |
| 259 | |
barfab@chromium.org | 5c37463 | 2012-04-05 16:50:56 +0200 | [diff] [blame] | 260 | def __get_mount_info(mount_point, allow_fail=False): |
| 261 | """Get information about the active mount at a given mount point.""" |
beeps | 569f867 | 2013-08-07 10:18:51 -0700 | [diff] [blame] | 262 | cryptohomed_path = '/proc/$(pgrep cryptohomed)/mounts' |
| 263 | try: |
Daniel Erat | 2ec3279 | 2017-01-31 18:26:59 -0700 | [diff] [blame] | 264 | logging.debug("Active cryptohome mounts:\n" + |
| 265 | utils.system_output('cat %s' % cryptohomed_path)) |
beeps | 569f867 | 2013-08-07 10:18:51 -0700 | [diff] [blame] | 266 | mount_line = utils.system_output( |
| 267 | 'grep %s %s' % (mount_point, cryptohomed_path), |
| 268 | ignore_status=allow_fail) |
| 269 | except Exception as e: |
| 270 | logging.error(e) |
| 271 | raise ChromiumOSError('Could not get info about cryptohome vault ' |
| 272 | 'through %s. See logs for complete mount-point.' |
| 273 | % os.path.dirname(str(mount_point))) |
Sourav Poddar | 574bd62 | 2010-05-26 14:22:26 +0530 | [diff] [blame] | 274 | return mount_line.split() |
| 275 | |
| 276 | |
Elly Fong-Jones | 6cb26ad | 2013-05-21 12:09:23 -0400 | [diff] [blame] | 277 | def __get_user_mount_info(user, allow_fail=False): |
barfab@chromium.org | 5c37463 | 2012-04-05 16:50:56 +0200 | [diff] [blame] | 278 | """Get information about the active mounts for a given user. |
| 279 | |
| 280 | Returns the active mounts at the user's user and system mount points. If no |
| 281 | user is given, the active mount at the shared mount point is returned |
| 282 | (regular users have a bind-mount at this mount point for backwards |
| 283 | compatibility; the guest user has a mount at this mount point only). |
| 284 | """ |
Elly Fong-Jones | 6cb26ad | 2013-05-21 12:09:23 -0400 | [diff] [blame] | 285 | return [__get_mount_info(mount_point=user_path(user), |
| 286 | allow_fail=allow_fail), |
| 287 | __get_mount_info(mount_point=system_path(user), |
| 288 | allow_fail=allow_fail)] |
Jim Hebert | f08f88d | 2011-04-22 10:33:49 -0700 | [diff] [blame] | 289 | |
barfab@chromium.org | 5c37463 | 2012-04-05 16:50:56 +0200 | [diff] [blame] | 290 | def is_vault_mounted( |
Elly Fong-Jones | 6cb26ad | 2013-05-21 12:09:23 -0400 | [diff] [blame] | 291 | user, |
barfab@chromium.org | 5c37463 | 2012-04-05 16:50:56 +0200 | [diff] [blame] | 292 | device_regex=constants.CRYPTOHOME_DEV_REGEX_ANY, |
| 293 | fs_regex=constants.CRYPTOHOME_FS_REGEX_ANY, |
| 294 | allow_fail=False): |
| 295 | """Check whether a vault is mounted for the given user. |
| 296 | |
| 297 | If no user is given, the shared mount point is checked, determining whether |
| 298 | a vault is mounted for any user. |
| 299 | """ |
| 300 | user_mount_info = __get_user_mount_info(user=user, allow_fail=allow_fail) |
| 301 | for mount_info in user_mount_info: |
| 302 | if (len(mount_info) < 3 or |
| 303 | not re.match(device_regex, mount_info[0]) or |
| 304 | not re.match(fs_regex, mount_info[2])): |
| 305 | return False |
| 306 | return True |
Sourav Poddar | 574bd62 | 2010-05-26 14:22:26 +0530 | [diff] [blame] | 307 | |
| 308 | |
barfab@chromium.org | 5c37463 | 2012-04-05 16:50:56 +0200 | [diff] [blame] | 309 | def is_guest_vault_mounted(allow_fail=False): |
| 310 | """Check whether a vault backed by tmpfs is mounted for the guest user.""" |
| 311 | return is_vault_mounted( |
Elly Fong-Jones | 6cb26ad | 2013-05-21 12:09:23 -0400 | [diff] [blame] | 312 | user=GUEST_USER_NAME, |
barfab@chromium.org | 5c37463 | 2012-04-05 16:50:56 +0200 | [diff] [blame] | 313 | device_regex=constants.CRYPTOHOME_DEV_REGEX_GUEST, |
| 314 | fs_regex=constants.CRYPTOHOME_FS_REGEX_TMPFS, |
| 315 | allow_fail=allow_fail) |
| 316 | |
| 317 | |
Kazuhiro Inaba | a3bf645 | 2017-02-08 11:41:50 +0900 | [diff] [blame] | 318 | def get_mounted_vault_path(user, allow_fail=False): |
| 319 | """Get the path where the decrypted data for the user is located.""" |
| 320 | return os.path.join(constants.SHADOW_ROOT, get_user_hash(user), 'mount') |
Nirnimesh | 6681449 | 2011-06-27 18:00:33 -0700 | [diff] [blame] | 321 | |
| 322 | |
| 323 | def canonicalize(credential): |
barfab@chromium.org | 5c37463 | 2012-04-05 16:50:56 +0200 | [diff] [blame] | 324 | """Perform basic canonicalization of |email_address|. |
Nirnimesh | 6681449 | 2011-06-27 18:00:33 -0700 | [diff] [blame] | 325 | |
barfab@chromium.org | 5c37463 | 2012-04-05 16:50:56 +0200 | [diff] [blame] | 326 | Perform basic canonicalization of |email_address|, taking into account that |
| 327 | gmail does not consider '.' or caps inside a username to matter. It also |
| 328 | ignores everything after a '+'. For example, |
| 329 | c.masone+abc@gmail.com == cMaSone@gmail.com, per |
Nirnimesh | 6681449 | 2011-06-27 18:00:33 -0700 | [diff] [blame] | 330 | http://mail.google.com/support/bin/answer.py?hl=en&ctx=mail&answer=10313 |
| 331 | """ |
| 332 | if not credential: |
| 333 | return None |
| 334 | |
| 335 | parts = credential.split('@') |
| 336 | if len(parts) != 2: |
barfab@chromium.org | 5c37463 | 2012-04-05 16:50:56 +0200 | [diff] [blame] | 337 | raise error.TestError('Malformed email: ' + credential) |
Nirnimesh | 6681449 | 2011-06-27 18:00:33 -0700 | [diff] [blame] | 338 | |
| 339 | (name, domain) = parts |
| 340 | name = name.partition('+')[0] |
barfab@chromium.org | 5c37463 | 2012-04-05 16:50:56 +0200 | [diff] [blame] | 341 | if (domain == constants.SPECIAL_CASE_DOMAIN): |
Nirnimesh | 6681449 | 2011-06-27 18:00:33 -0700 | [diff] [blame] | 342 | name = name.replace('.', '') |
| 343 | return '@'.join([name, domain]).lower() |
Elly Jones | 686c2f4 | 2011-10-24 16:45:07 -0400 | [diff] [blame] | 344 | |
barfab@chromium.org | cf2151e | 2012-04-04 15:39:34 +0200 | [diff] [blame] | 345 | |
Will Drewry | d2fed97 | 2013-12-05 16:35:51 -0600 | [diff] [blame] | 346 | def crash_cryptohomed(): |
Will Drewry | dc2b0dd | 2013-12-10 16:41:04 -0600 | [diff] [blame] | 347 | # Try to kill cryptohomed so we get something to work with. |
| 348 | pid = __run_cmd('pgrep cryptohomed') |
| 349 | try: |
Will Drewry | 9e44079 | 2013-12-11 17:18:35 -0600 | [diff] [blame] | 350 | pid = int(pid) |
Will Drewry | dc2b0dd | 2013-12-10 16:41:04 -0600 | [diff] [blame] | 351 | except ValueError, e: # empty or invalid string |
Will Drewry | 9e44079 | 2013-12-11 17:18:35 -0600 | [diff] [blame] | 352 | raise error.TestError('Cryptohomed was not running') |
Will Drewry | dc2b0dd | 2013-12-10 16:41:04 -0600 | [diff] [blame] | 353 | utils.system('kill -ABRT %d' % pid) |
| 354 | # CONT just in case cryptohomed had a spurious STOP. |
| 355 | utils.system('kill -CONT %d' % pid) |
| 356 | utils.poll_for_condition( |
| 357 | lambda: utils.system('ps -p %d' % pid, |
| 358 | ignore_status=True) != 0, |
Will Drewry | 934d153 | 2014-01-30 16:23:17 -0600 | [diff] [blame] | 359 | timeout=180, |
Will Drewry | dc2b0dd | 2013-12-10 16:41:04 -0600 | [diff] [blame] | 360 | exception=error.TestError( |
| 361 | 'Timeout waiting for cryptohomed to coredump')) |
| 362 | |
Will Drewry | d2fed97 | 2013-12-05 16:35:51 -0600 | [diff] [blame] | 363 | |
Will Drewry | 9e44079 | 2013-12-11 17:18:35 -0600 | [diff] [blame] | 364 | class CryptohomeProxy(DBusClient): |
| 365 | """A DBus proxy client for testing the Cryptohome DBus server. |
| 366 | """ |
| 367 | CRYPTOHOME_BUS_NAME = 'org.chromium.Cryptohome' |
| 368 | CRYPTOHOME_OBJECT_PATH = '/org/chromium/Cryptohome' |
| 369 | CRYPTOHOME_INTERFACE = 'org.chromium.CryptohomeInterface' |
| 370 | ASYNC_CALL_STATUS_SIGNAL = 'AsyncCallStatus' |
| 371 | ASYNC_CALL_STATUS_SIGNAL_ARGUMENTS = ( |
| 372 | 'async_id', 'return_status', 'return_code' |
| 373 | ) |
| 374 | DBUS_PROPERTIES_INTERFACE = 'org.freedesktop.DBus.Properties' |
| 375 | |
Chris Masone | 19e305e | 2014-03-14 15:13:46 -0700 | [diff] [blame] | 376 | |
Chris Masone | 64170f8 | 2014-03-14 15:47:05 -0700 | [diff] [blame] | 377 | def __init__(self, bus_loop=None): |
Will Drewry | 9e44079 | 2013-12-11 17:18:35 -0600 | [diff] [blame] | 378 | self.main_loop = gobject.MainLoop() |
Will Drewry | 78db9dc | 2014-04-01 16:34:23 -0500 | [diff] [blame] | 379 | if bus_loop is None: |
Chris Masone | 64170f8 | 2014-03-14 15:47:05 -0700 | [diff] [blame] | 380 | bus_loop = DBusGMainLoop(set_as_default=True) |
Will Drewry | 9e44079 | 2013-12-11 17:18:35 -0600 | [diff] [blame] | 381 | self.bus = dbus.SystemBus(mainloop=bus_loop) |
| 382 | super(CryptohomeProxy, self).__init__(self.main_loop, self.bus, |
| 383 | self.CRYPTOHOME_BUS_NAME, |
| 384 | self.CRYPTOHOME_OBJECT_PATH) |
| 385 | self.iface = dbus.Interface(self.proxy_object, |
| 386 | self.CRYPTOHOME_INTERFACE) |
| 387 | self.properties = dbus.Interface(self.proxy_object, |
| 388 | self.DBUS_PROPERTIES_INTERFACE) |
| 389 | self.handle_signal(self.CRYPTOHOME_INTERFACE, |
| 390 | self.ASYNC_CALL_STATUS_SIGNAL, |
| 391 | self.ASYNC_CALL_STATUS_SIGNAL_ARGUMENTS) |
Elly Jones | 2f0ebba | 2011-10-27 13:43:20 -0400 | [diff] [blame] | 392 | |
Chris Masone | 19e305e | 2014-03-14 15:13:46 -0700 | [diff] [blame] | 393 | |
Will Drewry | d2fed97 | 2013-12-05 16:35:51 -0600 | [diff] [blame] | 394 | # Wrap all proxied calls to catch cryptohomed failures. |
| 395 | def __call(self, method, *args): |
| 396 | try: |
Chris Masone | f59d9df | 2014-03-14 12:05:32 -0700 | [diff] [blame] | 397 | return method(*args, timeout=180) |
Will Drewry | d2fed97 | 2013-12-05 16:35:51 -0600 | [diff] [blame] | 398 | except dbus.exceptions.DBusException, e: |
| 399 | if e.get_dbus_name() == 'org.freedesktop.DBus.Error.NoReply': |
| 400 | logging.error('Cryptohome is not responding. Sending ABRT') |
| 401 | crash_cryptohomed() |
| 402 | raise ChromiumOSError('cryptohomed aborted. Check crashes!') |
| 403 | raise e |
| 404 | |
Chris Masone | 19e305e | 2014-03-14 15:13:46 -0700 | [diff] [blame] | 405 | |
Will Drewry | 9e44079 | 2013-12-11 17:18:35 -0600 | [diff] [blame] | 406 | def __wait_for_specific_signal(self, signal, data): |
| 407 | """Wait for the |signal| with matching |data| |
| 408 | Returns the resulting dict on success or {} on error. |
| 409 | """ |
Will Drewry | c4de5ff | 2014-02-03 13:26:57 -0600 | [diff] [blame] | 410 | # Do not bubble up the timeout here, just return {}. |
| 411 | result = {} |
| 412 | try: |
| 413 | result = self.wait_for_signal(signal) |
| 414 | except utils.TimeoutError: |
| 415 | return {} |
Will Drewry | 9e44079 | 2013-12-11 17:18:35 -0600 | [diff] [blame] | 416 | for k in data.keys(): |
| 417 | if not result.has_key(k) or result[k] != data[k]: |
| 418 | return {} |
| 419 | return result |
| 420 | |
Chris Masone | 19e305e | 2014-03-14 15:13:46 -0700 | [diff] [blame] | 421 | |
Will Drewry | 9e44079 | 2013-12-11 17:18:35 -0600 | [diff] [blame] | 422 | # Perform a data-less async call. |
| 423 | # TODO(wad) Add __async_data_call. |
| 424 | def __async_call(self, method, *args): |
Will Drewry | fef135a | 2014-05-23 16:02:14 -0500 | [diff] [blame] | 425 | # Clear out any superfluous async call signals. |
| 426 | self.clear_signal_content(self.ASYNC_CALL_STATUS_SIGNAL) |
Will Drewry | 9e44079 | 2013-12-11 17:18:35 -0600 | [diff] [blame] | 427 | out = self.__call(method, *args) |
| 428 | logging.debug('Issued call ' + str(method) + |
| 429 | ' with async_id ' + str(out)) |
| 430 | result = {} |
| 431 | try: |
Will Drewry | 934d153 | 2014-01-30 16:23:17 -0600 | [diff] [blame] | 432 | # __wait_for_specific_signal has a 10s timeout |
Will Drewry | 9e44079 | 2013-12-11 17:18:35 -0600 | [diff] [blame] | 433 | result = utils.poll_for_condition( |
| 434 | lambda: self.__wait_for_specific_signal( |
| 435 | self.ASYNC_CALL_STATUS_SIGNAL, {'async_id' : out}), |
Will Drewry | 934d153 | 2014-01-30 16:23:17 -0600 | [diff] [blame] | 436 | timeout=180, |
Will Drewry | 9e44079 | 2013-12-11 17:18:35 -0600 | [diff] [blame] | 437 | desc='matching %s signal' % self.ASYNC_CALL_STATUS_SIGNAL) |
| 438 | except utils.TimeoutError, e: |
| 439 | logging.error('Cryptohome timed out. Sending ABRT.') |
| 440 | crash_cryptohomed() |
| 441 | raise ChromiumOSError('cryptohomed aborted. Check crashes!') |
| 442 | return result |
| 443 | |
Chris Masone | 19e305e | 2014-03-14 15:13:46 -0700 | [diff] [blame] | 444 | |
Will Drewry | 9e44079 | 2013-12-11 17:18:35 -0600 | [diff] [blame] | 445 | def mount(self, user, password, create=False, async=True): |
Elly Jones | 2f0ebba | 2011-10-27 13:43:20 -0400 | [diff] [blame] | 446 | """Mounts a cryptohome. |
| 447 | |
| 448 | Returns True if the mount succeeds or False otherwise. |
| 449 | TODO(ellyjones): Migrate mount_vault() to use a multi-user-safe |
| 450 | heuristic, then remove this method. See <crosbug.com/20778>. |
| 451 | """ |
Will Drewry | 9e44079 | 2013-12-11 17:18:35 -0600 | [diff] [blame] | 452 | if async: |
| 453 | return self.__async_call(self.iface.AsyncMount, user, password, |
| 454 | create, False, [])['return_status'] |
Will Drewry | d2fed97 | 2013-12-05 16:35:51 -0600 | [diff] [blame] | 455 | out = self.__call(self.iface.Mount, user, password, create, False, []) |
Will Drewry | 9e44079 | 2013-12-11 17:18:35 -0600 | [diff] [blame] | 456 | # Sync returns (return code, return status) |
| 457 | return out[1] if len(out) > 1 else False |
Elly Jones | 2f0ebba | 2011-10-27 13:43:20 -0400 | [diff] [blame] | 458 | |
Chris Masone | 19e305e | 2014-03-14 15:13:46 -0700 | [diff] [blame] | 459 | |
Elly Jones | 2f0ebba | 2011-10-27 13:43:20 -0400 | [diff] [blame] | 460 | def unmount(self, user): |
| 461 | """Unmounts a cryptohome. |
| 462 | |
| 463 | Returns True if the unmount suceeds or false otherwise. |
| 464 | TODO(ellyjones): Once there's a per-user unmount method, use it. See |
| 465 | <crosbug.com/20778>. |
| 466 | """ |
Will Drewry | d2fed97 | 2013-12-05 16:35:51 -0600 | [diff] [blame] | 467 | return self.__call(self.iface.Unmount) |
Elly Jones | 2f0ebba | 2011-10-27 13:43:20 -0400 | [diff] [blame] | 468 | |
Chris Masone | 19e305e | 2014-03-14 15:13:46 -0700 | [diff] [blame] | 469 | |
Elly Jones | 2f0ebba | 2011-10-27 13:43:20 -0400 | [diff] [blame] | 470 | def is_mounted(self, user): |
| 471 | """Tests whether a user's cryptohome is mounted.""" |
| 472 | return (utils.is_mountpoint(user_path(user)) |
| 473 | and utils.is_mountpoint(system_path(user))) |
| 474 | |
Chris Masone | 19e305e | 2014-03-14 15:13:46 -0700 | [diff] [blame] | 475 | |
Elly Jones | 2f0ebba | 2011-10-27 13:43:20 -0400 | [diff] [blame] | 476 | def require_mounted(self, user): |
| 477 | """Raises a test failure if a user's cryptohome is not mounted.""" |
| 478 | utils.require_mountpoint(user_path(user)) |
| 479 | utils.require_mountpoint(system_path(user)) |
Elly Jones | 4458f44 | 2012-04-16 15:42:56 -0400 | [diff] [blame] | 480 | |
Chris Masone | 19e305e | 2014-03-14 15:13:46 -0700 | [diff] [blame] | 481 | |
Will Drewry | 9e44079 | 2013-12-11 17:18:35 -0600 | [diff] [blame] | 482 | def migrate(self, user, oldkey, newkey, async=True): |
Elly Jones | 4458f44 | 2012-04-16 15:42:56 -0400 | [diff] [blame] | 483 | """Migrates the specified user's cryptohome from one key to another.""" |
Will Drewry | 9e44079 | 2013-12-11 17:18:35 -0600 | [diff] [blame] | 484 | if async: |
| 485 | return self.__async_call(self.iface.AsyncMigrateKey, |
| 486 | user, oldkey, newkey)['return_status'] |
Will Drewry | d2fed97 | 2013-12-05 16:35:51 -0600 | [diff] [blame] | 487 | return self.__call(self.iface.MigrateKey, user, oldkey, newkey) |
Elly Jones | 4458f44 | 2012-04-16 15:42:56 -0400 | [diff] [blame] | 488 | |
Chris Masone | 19e305e | 2014-03-14 15:13:46 -0700 | [diff] [blame] | 489 | |
Will Drewry | 9e44079 | 2013-12-11 17:18:35 -0600 | [diff] [blame] | 490 | def remove(self, user, async=True): |
| 491 | if async: |
| 492 | return self.__async_call(self.iface.AsyncRemove, |
| 493 | user)['return_status'] |
Will Drewry | d2fed97 | 2013-12-05 16:35:51 -0600 | [diff] [blame] | 494 | return self.__call(self.iface.Remove, user) |
Chris Masone | 19e305e | 2014-03-14 15:13:46 -0700 | [diff] [blame] | 495 | |
| 496 | |
| 497 | def ensure_clean_cryptohome_for(self, user, password=None): |
| 498 | """Ensure a fresh cryptohome exists for user. |
| 499 | |
| 500 | @param user: user who needs a shiny new cryptohome. |
| 501 | @param password: if unset, a random password will be used. |
| 502 | """ |
| 503 | if not password: |
| 504 | password = ''.join(random.sample(string.ascii_lowercase, 6)) |
| 505 | self.remove(user) |
| 506 | self.mount(user, password, create=True) |