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 | |
barfab@chromium.org | b6d2993 | 2012-04-11 09:46:43 +0200 | [diff] [blame] | 5 | import dbus, logging, os, re, shutil |
| 6 | |
| 7 | import common, constants |
barfab@chromium.org | 5c37463 | 2012-04-05 16:50:56 +0200 | [diff] [blame] | 8 | from autotest_lib.client.bin import utils |
Chris Masone | 5e06f18 | 2010-03-23 08:29:52 -0700 | [diff] [blame] | 9 | from autotest_lib.client.common_lib import error |
Eric Li | c4d8f4a | 2010-12-10 09:49:23 -0800 | [diff] [blame] | 10 | |
Sean O | e5d8fd0 | 2010-09-30 10:44:44 +0200 | [diff] [blame] | 11 | CRYPTOHOME_CMD = '/usr/sbin/cryptohome' |
| 12 | |
| 13 | class ChromiumOSError(error.InstallError): |
| 14 | """Generic error for ChromiumOS-specific exceptions.""" |
| 15 | pass |
| 16 | |
| 17 | |
| 18 | def __run_cmd(cmd): |
| 19 | return utils.system_output(cmd + ' 2>&1', retain_output=True, |
| 20 | ignore_status=True).strip() |
| 21 | |
| 22 | |
| 23 | def get_user_hash(user): |
barfab@chromium.org | 5c37463 | 2012-04-05 16:50:56 +0200 | [diff] [blame] | 24 | """Get the user hash for the given user.""" |
Sean O | e5d8fd0 | 2010-09-30 10:44:44 +0200 | [diff] [blame] | 25 | hash_cmd = CRYPTOHOME_CMD + ' --action=obfuscate_user --user=%s' % user |
| 26 | return __run_cmd(hash_cmd) |
| 27 | |
| 28 | |
barfab@chromium.org | 5c37463 | 2012-04-05 16:50:56 +0200 | [diff] [blame] | 29 | def user_path(user): |
| 30 | """Get the user mount point for the given user.""" |
| 31 | return utils.system_output('cryptohome-path user %s' % user) |
| 32 | |
| 33 | |
| 34 | def system_path(user): |
| 35 | """Get the system mount point for the given user.""" |
| 36 | return utils.system_output('cryptohome-path system %s' % user) |
| 37 | |
| 38 | |
Frank Farzan | d5e3631 | 2012-01-13 14:34:03 -0800 | [diff] [blame] | 39 | def get_tpm_status(): |
| 40 | """Get the TPM status. |
| 41 | |
| 42 | Returns: |
| 43 | A TPM status dictionary, for example: |
| 44 | { 'Enabled': True, |
| 45 | 'Owned': True, |
| 46 | 'Being Owned': False, |
| 47 | 'Ready': True, |
| 48 | 'Password': '' |
| 49 | } |
| 50 | """ |
| 51 | out = __run_cmd(CRYPTOHOME_CMD + ' --action=tpm_status') |
| 52 | status = {} |
| 53 | for field in ['Enabled', 'Owned', 'Being Owned', 'Ready']: |
| 54 | match = re.search('TPM %s: (true|false)' % field, out) |
| 55 | if not match: |
| 56 | raise ChromiumOSError('Invalid TPM status: "%s".' % out) |
| 57 | status[field] = match.group(1) == 'true' |
| 58 | match = re.search('TPM Password: (\w*)', out) |
| 59 | status['Password'] = '' |
| 60 | if match: |
| 61 | status['Password'] = match.group(1) |
| 62 | return status |
| 63 | |
| 64 | |
Darren Krahn | 5f880f6 | 2012-10-02 15:17:59 -0700 | [diff] [blame] | 65 | def get_tpm_attestation_status(): |
| 66 | """Get the TPM attestation status. Works similar to get_tpm_status(). |
| 67 | """ |
| 68 | out = __run_cmd(CRYPTOHOME_CMD + ' --action=tpm_attestation_status') |
| 69 | status = {} |
| 70 | for field in ['Prepared', 'Enrolled']: |
| 71 | match = re.search('Attestation %s: (true|false)' % field, out) |
| 72 | if not match: |
| 73 | raise ChromiumOSError('Invalid attestation status: "%s".' % out) |
| 74 | status[field] = match.group(1) == 'true' |
| 75 | return status |
| 76 | |
| 77 | |
Frank Farzan | d5e3631 | 2012-01-13 14:34:03 -0800 | [diff] [blame] | 78 | def take_tpm_ownership(): |
| 79 | """Take TPM owernship. |
| 80 | |
| 81 | Blocks until TPM is owned. |
| 82 | """ |
| 83 | __run_cmd(CRYPTOHOME_CMD + ' --action=tpm_take_ownership') |
| 84 | __run_cmd(CRYPTOHOME_CMD + ' --action=tpm_wait_ownership') |
| 85 | |
| 86 | |
Darren Krahn | 0e73e7f | 2012-09-05 15:35:15 -0700 | [diff] [blame] | 87 | def verify_ek(): |
| 88 | """Verify the TPM endorsement key. |
| 89 | |
| 90 | Returns true if EK is valid. |
| 91 | """ |
| 92 | cmd = CRYPTOHOME_CMD + ' --action=tpm_verify_ek' |
| 93 | return (utils.system(cmd, ignore_status=True) == 0) |
| 94 | |
| 95 | |
Sean O | e5d8fd0 | 2010-09-30 10:44:44 +0200 | [diff] [blame] | 96 | def remove_vault(user): |
barfab@chromium.org | 5c37463 | 2012-04-05 16:50:56 +0200 | [diff] [blame] | 97 | """Remove the given user's vault from the shadow directory.""" |
Sean O | e5d8fd0 | 2010-09-30 10:44:44 +0200 | [diff] [blame] | 98 | logging.debug('user is %s', user) |
| 99 | user_hash = get_user_hash(user) |
barfab@chromium.org | 5c37463 | 2012-04-05 16:50:56 +0200 | [diff] [blame] | 100 | 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] | 101 | cmd = CRYPTOHOME_CMD + ' --action=remove --force --user=%s' % user |
| 102 | __run_cmd(cmd) |
barfab@chromium.org | 5c37463 | 2012-04-05 16:50:56 +0200 | [diff] [blame] | 103 | # Ensure that the vault does not exist. |
| 104 | if os.path.exists(os.path.join(constants.SHADOW_ROOT, user_hash)): |
| 105 | raise ChromiumOSError('Cryptohome could not remove the user''s vault.') |
Sean O | e5d8fd0 | 2010-09-30 10:44:44 +0200 | [diff] [blame] | 106 | |
| 107 | |
barfab@chromium.org | cf2151e | 2012-04-04 15:39:34 +0200 | [diff] [blame] | 108 | def remove_all_vaults(): |
| 109 | """Remove any existing vaults from the shadow directory. |
| 110 | |
| 111 | This function must be run with root privileges. |
| 112 | """ |
barfab@chromium.org | 5c37463 | 2012-04-05 16:50:56 +0200 | [diff] [blame] | 113 | for item in os.listdir(constants.SHADOW_ROOT): |
| 114 | abs_item = os.path.join(constants.SHADOW_ROOT, item) |
barfab@chromium.org | cf2151e | 2012-04-04 15:39:34 +0200 | [diff] [blame] | 115 | if os.path.isdir(os.path.join(abs_item, 'vault')): |
| 116 | logging.debug('Removing vault for user with hash %s' % item) |
| 117 | shutil.rmtree(abs_item) |
| 118 | |
| 119 | |
Sean O | e5d8fd0 | 2010-09-30 10:44:44 +0200 | [diff] [blame] | 120 | def mount_vault(user, password, create=False): |
barfab@chromium.org | 5c37463 | 2012-04-05 16:50:56 +0200 | [diff] [blame] | 121 | """Mount the given user's vault.""" |
Sean O | e5d8fd0 | 2010-09-30 10:44:44 +0200 | [diff] [blame] | 122 | cmd = (CRYPTOHOME_CMD + ' --action=mount --user=%s --password=%s' % |
| 123 | (user, password)) |
| 124 | if create: |
| 125 | cmd += ' --create' |
| 126 | __run_cmd(cmd) |
barfab@chromium.org | 5c37463 | 2012-04-05 16:50:56 +0200 | [diff] [blame] | 127 | # Ensure that the vault exists in the shadow directory. |
Sean O | e5d8fd0 | 2010-09-30 10:44:44 +0200 | [diff] [blame] | 128 | user_hash = get_user_hash(user) |
barfab@chromium.org | 5c37463 | 2012-04-05 16:50:56 +0200 | [diff] [blame] | 129 | 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] | 130 | raise ChromiumOSError('Cryptohome vault not found after mount.') |
barfab@chromium.org | 5c37463 | 2012-04-05 16:50:56 +0200 | [diff] [blame] | 131 | # Ensure that the vault is mounted. |
| 132 | if not is_vault_mounted( |
| 133 | user=user, |
| 134 | device_regex=constants.CRYPTOHOME_DEV_REGEX_REGULAR_USER, |
| 135 | allow_fail=True): |
| 136 | raise ChromiumOSError('Cryptohome created a vault but did not mount.') |
Sean O | e5d8fd0 | 2010-09-30 10:44:44 +0200 | [diff] [blame] | 137 | |
| 138 | |
| 139 | def test_auth(user, password): |
| 140 | cmd = (CRYPTOHOME_CMD + ' --action=test_auth --user=%s --password=%s' % |
| 141 | (user, password)) |
Elly Jones | 630f3a9 | 2012-04-17 16:40:25 -0400 | [diff] [blame] | 142 | cmd += ' --async' |
Sean O | e5d8fd0 | 2010-09-30 10:44:44 +0200 | [diff] [blame] | 143 | return 'Authentication succeeded' in __run_cmd(cmd) |
| 144 | |
| 145 | |
Elly Jones | 686c2f4 | 2011-10-24 16:45:07 -0400 | [diff] [blame] | 146 | def unmount_vault(user=None): |
barfab@chromium.org | 5c37463 | 2012-04-05 16:50:56 +0200 | [diff] [blame] | 147 | """Unmount the given user's vault. |
| 148 | |
| 149 | Once unmounting for a specific user is supported, the user parameter will |
| 150 | name the target user. See crosbug.com/20778. |
Elly Jones | 686c2f4 | 2011-10-24 16:45:07 -0400 | [diff] [blame] | 151 | """ |
Sean O | e5d8fd0 | 2010-09-30 10:44:44 +0200 | [diff] [blame] | 152 | cmd = (CRYPTOHOME_CMD + ' --action=unmount') |
| 153 | __run_cmd(cmd) |
barfab@chromium.org | 5c37463 | 2012-04-05 16:50:56 +0200 | [diff] [blame] | 154 | # Ensure that the vault is not mounted. |
| 155 | if is_vault_mounted(allow_fail=True): |
Sean O | e5d8fd0 | 2010-09-30 10:44:44 +0200 | [diff] [blame] | 156 | raise ChromiumOSError('Cryptohome did not unmount the user.') |
| 157 | |
| 158 | |
barfab@chromium.org | 5c37463 | 2012-04-05 16:50:56 +0200 | [diff] [blame] | 159 | def __get_mount_info(mount_point, allow_fail=False): |
| 160 | """Get information about the active mount at a given mount point.""" |
Will Drewry | 81ad616 | 2010-04-01 10:26:07 -0500 | [diff] [blame] | 161 | mount_line = utils.system_output( |
barfab@chromium.org | 5c37463 | 2012-04-05 16:50:56 +0200 | [diff] [blame] | 162 | 'grep %s /proc/$(pgrep cryptohomed)/mounts' % mount_point, |
| 163 | ignore_status=allow_fail) |
Sourav Poddar | 574bd62 | 2010-05-26 14:22:26 +0530 | [diff] [blame] | 164 | return mount_line.split() |
| 165 | |
| 166 | |
barfab@chromium.org | 5c37463 | 2012-04-05 16:50:56 +0200 | [diff] [blame] | 167 | def __get_user_mount_info(user=None, allow_fail=False): |
| 168 | """Get information about the active mounts for a given user. |
| 169 | |
| 170 | Returns the active mounts at the user's user and system mount points. If no |
| 171 | user is given, the active mount at the shared mount point is returned |
| 172 | (regular users have a bind-mount at this mount point for backwards |
| 173 | compatibility; the guest user has a mount at this mount point only). |
| 174 | """ |
| 175 | if user: |
| 176 | return [__get_mount_info(mount_point=user_path(user), |
| 177 | allow_fail=allow_fail), |
| 178 | __get_mount_info(mount_point=system_path(user), |
| 179 | allow_fail=allow_fail)] |
Jim Hebert | f08f88d | 2011-04-22 10:33:49 -0700 | [diff] [blame] | 180 | else: |
barfab@chromium.org | 5c37463 | 2012-04-05 16:50:56 +0200 | [diff] [blame] | 181 | return [__get_mount_info(mount_point=constants.CRYPTOHOME_MOUNT_PT, |
| 182 | allow_fail=allow_fail)] |
Jim Hebert | f08f88d | 2011-04-22 10:33:49 -0700 | [diff] [blame] | 183 | |
| 184 | |
barfab@chromium.org | 5c37463 | 2012-04-05 16:50:56 +0200 | [diff] [blame] | 185 | def is_vault_mounted( |
| 186 | user=None, |
| 187 | device_regex=constants.CRYPTOHOME_DEV_REGEX_ANY, |
| 188 | fs_regex=constants.CRYPTOHOME_FS_REGEX_ANY, |
| 189 | allow_fail=False): |
| 190 | """Check whether a vault is mounted for the given user. |
| 191 | |
| 192 | If no user is given, the shared mount point is checked, determining whether |
| 193 | a vault is mounted for any user. |
| 194 | """ |
| 195 | user_mount_info = __get_user_mount_info(user=user, allow_fail=allow_fail) |
| 196 | for mount_info in user_mount_info: |
| 197 | if (len(mount_info) < 3 or |
| 198 | not re.match(device_regex, mount_info[0]) or |
| 199 | not re.match(fs_regex, mount_info[2])): |
| 200 | return False |
| 201 | return True |
Sourav Poddar | 574bd62 | 2010-05-26 14:22:26 +0530 | [diff] [blame] | 202 | |
| 203 | |
barfab@chromium.org | 5c37463 | 2012-04-05 16:50:56 +0200 | [diff] [blame] | 204 | def is_guest_vault_mounted(allow_fail=False): |
| 205 | """Check whether a vault backed by tmpfs is mounted for the guest user.""" |
| 206 | return is_vault_mounted( |
| 207 | user=None, |
| 208 | device_regex=constants.CRYPTOHOME_DEV_REGEX_GUEST, |
| 209 | fs_regex=constants.CRYPTOHOME_FS_REGEX_TMPFS, |
| 210 | allow_fail=allow_fail) |
| 211 | |
| 212 | |
| 213 | def get_mounted_vault_devices(user=None, allow_fail=False): |
| 214 | """Get the device(s) backing the vault mounted for the given user. |
| 215 | |
| 216 | Returns the devices mounted at the user's user and system mount points. If |
| 217 | no user is given, the device mounted at the shared mount point is returned. |
| 218 | """ |
| 219 | return [mount_info[0] |
| 220 | for mount_info |
| 221 | in __get_user_mount_info(user=user, allow_fail=allow_fail) |
| 222 | if len(mount_info)] |
Nirnimesh | 6681449 | 2011-06-27 18:00:33 -0700 | [diff] [blame] | 223 | |
| 224 | |
| 225 | def canonicalize(credential): |
barfab@chromium.org | 5c37463 | 2012-04-05 16:50:56 +0200 | [diff] [blame] | 226 | """Perform basic canonicalization of |email_address|. |
Nirnimesh | 6681449 | 2011-06-27 18:00:33 -0700 | [diff] [blame] | 227 | |
barfab@chromium.org | 5c37463 | 2012-04-05 16:50:56 +0200 | [diff] [blame] | 228 | Perform basic canonicalization of |email_address|, taking into account that |
| 229 | gmail does not consider '.' or caps inside a username to matter. It also |
| 230 | ignores everything after a '+'. For example, |
| 231 | c.masone+abc@gmail.com == cMaSone@gmail.com, per |
Nirnimesh | 6681449 | 2011-06-27 18:00:33 -0700 | [diff] [blame] | 232 | http://mail.google.com/support/bin/answer.py?hl=en&ctx=mail&answer=10313 |
| 233 | """ |
| 234 | if not credential: |
| 235 | return None |
| 236 | |
| 237 | parts = credential.split('@') |
| 238 | if len(parts) != 2: |
barfab@chromium.org | 5c37463 | 2012-04-05 16:50:56 +0200 | [diff] [blame] | 239 | raise error.TestError('Malformed email: ' + credential) |
Nirnimesh | 6681449 | 2011-06-27 18:00:33 -0700 | [diff] [blame] | 240 | |
| 241 | (name, domain) = parts |
| 242 | name = name.partition('+')[0] |
barfab@chromium.org | 5c37463 | 2012-04-05 16:50:56 +0200 | [diff] [blame] | 243 | if (domain == constants.SPECIAL_CASE_DOMAIN): |
Nirnimesh | 6681449 | 2011-06-27 18:00:33 -0700 | [diff] [blame] | 244 | name = name.replace('.', '') |
| 245 | return '@'.join([name, domain]).lower() |
Elly Jones | 686c2f4 | 2011-10-24 16:45:07 -0400 | [diff] [blame] | 246 | |
barfab@chromium.org | cf2151e | 2012-04-04 15:39:34 +0200 | [diff] [blame] | 247 | |
Elly Jones | 2f0ebba | 2011-10-27 13:43:20 -0400 | [diff] [blame] | 248 | class CryptohomeProxy: |
| 249 | def __init__(self): |
| 250 | BUSNAME = 'org.chromium.Cryptohome' |
| 251 | PATH = '/org/chromium/Cryptohome' |
| 252 | INTERFACE = 'org.chromium.CryptohomeInterface' |
| 253 | bus = dbus.SystemBus() |
| 254 | obj = bus.get_object(BUSNAME, PATH) |
| 255 | self.iface = dbus.Interface(obj, INTERFACE) |
| 256 | |
| 257 | def mount(self, user, password, create=False): |
| 258 | """Mounts a cryptohome. |
| 259 | |
| 260 | Returns True if the mount succeeds or False otherwise. |
| 261 | TODO(ellyjones): Migrate mount_vault() to use a multi-user-safe |
| 262 | heuristic, then remove this method. See <crosbug.com/20778>. |
| 263 | """ |
| 264 | return self.iface.Mount(user, password, create, False, [])[1] |
| 265 | |
| 266 | def unmount(self, user): |
| 267 | """Unmounts a cryptohome. |
| 268 | |
| 269 | Returns True if the unmount suceeds or false otherwise. |
| 270 | TODO(ellyjones): Once there's a per-user unmount method, use it. See |
| 271 | <crosbug.com/20778>. |
| 272 | """ |
Elly Jones | 5c3c2b0 | 2011-12-21 14:26:28 -0500 | [diff] [blame] | 273 | return self.iface.Unmount() |
Elly Jones | 2f0ebba | 2011-10-27 13:43:20 -0400 | [diff] [blame] | 274 | |
| 275 | def is_mounted(self, user): |
| 276 | """Tests whether a user's cryptohome is mounted.""" |
| 277 | return (utils.is_mountpoint(user_path(user)) |
| 278 | and utils.is_mountpoint(system_path(user))) |
| 279 | |
| 280 | def require_mounted(self, user): |
| 281 | """Raises a test failure if a user's cryptohome is not mounted.""" |
| 282 | utils.require_mountpoint(user_path(user)) |
| 283 | utils.require_mountpoint(system_path(user)) |
Elly Jones | 4458f44 | 2012-04-16 15:42:56 -0400 | [diff] [blame] | 284 | |
| 285 | def migrate(self, user, oldkey, newkey): |
| 286 | """Migrates the specified user's cryptohome from one key to another.""" |
| 287 | return self.iface.MigrateKey(user, oldkey, newkey) |
| 288 | |
| 289 | def remove(self, user): |
| 290 | return self.iface.Remove(user) |