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