Chris Masone | 5e06f18 | 2010-03-23 08:29:52 -0700 | [diff] [blame] | 1 | # Copyright (c) 2010 The Chromium OS Authors. All rights reserved. |
| 2 | # Use of this source code is governed by a BSD-style license that can be |
| 3 | # found in the LICENSE file. |
| 4 | |
Sean O | e5d8fd0 | 2010-09-30 10:44:44 +0200 | [diff] [blame] | 5 | import logging, os, re, utils |
Sean O'Connor | 985a034 | 2010-12-06 17:23:44 -0800 | [diff] [blame] | 6 | from autotest_lib.client.bin import chromeos_constants, test |
Chris Masone | 5e06f18 | 2010-03-23 08:29:52 -0700 | [diff] [blame] | 7 | from autotest_lib.client.common_lib import error |
Sean O'Connor | 985a034 | 2010-12-06 17:23:44 -0800 | [diff] [blame] | 8 | |
Sean O | e5d8fd0 | 2010-09-30 10:44:44 +0200 | [diff] [blame] | 9 | |
| 10 | CRYPTOHOME_CMD = '/usr/sbin/cryptohome' |
| 11 | |
| 12 | class ChromiumOSError(error.InstallError): |
| 13 | """Generic error for ChromiumOS-specific exceptions.""" |
| 14 | pass |
| 15 | |
| 16 | |
| 17 | def __run_cmd(cmd): |
| 18 | return utils.system_output(cmd + ' 2>&1', retain_output=True, |
| 19 | ignore_status=True).strip() |
| 20 | |
| 21 | |
| 22 | def get_user_hash(user): |
| 23 | """Get the hash for the test user account.""" |
| 24 | hash_cmd = CRYPTOHOME_CMD + ' --action=obfuscate_user --user=%s' % user |
| 25 | return __run_cmd(hash_cmd) |
| 26 | |
| 27 | |
| 28 | def remove_vault(user): |
| 29 | """Remove the test user account.""" |
| 30 | logging.debug('user is %s', user) |
| 31 | user_hash = get_user_hash(user) |
| 32 | logging.debug('Removing vault for user %s - %s' % (user, user_hash)) |
| 33 | cmd = CRYPTOHOME_CMD + ' --action=remove --force --user=%s' % user |
| 34 | __run_cmd(cmd) |
| 35 | # Ensure that the user directory does not exist |
| 36 | if os.path.exists(os.path.join('/home/.shadow/', user_hash)): |
| 37 | raise ChromiumOSError('Cryptohome could not remove the test user.') |
| 38 | |
| 39 | |
| 40 | def mount_vault(user, password, create=False): |
| 41 | cmd = (CRYPTOHOME_CMD + ' --action=mount --user=%s --password=%s' % |
| 42 | (user, password)) |
| 43 | if create: |
| 44 | cmd += ' --create' |
| 45 | __run_cmd(cmd) |
| 46 | # Ensure that the user directory exists |
| 47 | user_hash = get_user_hash(user) |
| 48 | if not os.path.exists(os.path.join('/home/.shadow/', user_hash)): |
| 49 | raise ChromiumOSError('Cryptohome vault not found after mount.') |
| 50 | # Ensure that the user directory is mounted |
| 51 | if not is_mounted(allow_fail=True): |
| 52 | raise ChromiumOSError('Cryptohome created the user but did not mount.') |
| 53 | |
| 54 | |
| 55 | def test_auth(user, password): |
| 56 | cmd = (CRYPTOHOME_CMD + ' --action=test_auth --user=%s --password=%s' % |
| 57 | (user, password)) |
| 58 | return 'Authentication succeeded' in __run_cmd(cmd) |
| 59 | |
| 60 | |
| 61 | def unmount_vault(): |
| 62 | """Unmount the directory.""" |
| 63 | cmd = (CRYPTOHOME_CMD + ' --action=unmount') |
| 64 | __run_cmd(cmd) |
| 65 | # Ensure that the user directory is not mounted |
| 66 | if is_mounted(allow_fail=True): |
| 67 | raise ChromiumOSError('Cryptohome did not unmount the user.') |
| 68 | |
| 69 | |
| 70 | def __get_mount_parts(expected_mountpt=chromeos_constants.CRYPTOHOME_MOUNT_PT, |
Sourav Poddar | 574bd62 | 2010-05-26 14:22:26 +0530 | [diff] [blame] | 71 | allow_fail = False): |
Will Drewry | 81ad616 | 2010-04-01 10:26:07 -0500 | [diff] [blame] | 72 | mount_line = utils.system_output( |
| 73 | 'grep %s /proc/$(pgrep cryptohomed)/mounts' % expected_mountpt, |
Sourav Poddar | 574bd62 | 2010-05-26 14:22:26 +0530 | [diff] [blame] | 74 | ignore_status = allow_fail) |
| 75 | return mount_line.split() |
| 76 | |
| 77 | |
Frank Swiderski | 52653c3 | 2010-05-26 17:40:47 -0700 | [diff] [blame] | 78 | def is_mounted(device=chromeos_constants.CRYPTOHOME_DEVICE_REGEX, |
| 79 | expected_mountpt=chromeos_constants.CRYPTOHOME_MOUNT_PT, |
| 80 | allow_fail=False): |
| 81 | mount_line = utils.system_output( |
| 82 | 'grep %s /proc/$(pgrep cryptohomed)/mounts' % expected_mountpt, |
| 83 | ignore_status=allow_fail) |
| 84 | mount_parts = mount_line.split() |
| 85 | return len(mount_parts) > 0 and re.match(device, mount_parts[0]) |
Sourav Poddar | 574bd62 | 2010-05-26 14:22:26 +0530 | [diff] [blame] | 86 | |
| 87 | |
Frank Swiderski | 52653c3 | 2010-05-26 17:40:47 -0700 | [diff] [blame] | 88 | def is_mounted_on_tmpfs(device = chromeos_constants.CRYPTOHOME_INCOGNITO, |
Sourav Poddar | 574bd62 | 2010-05-26 14:22:26 +0530 | [diff] [blame] | 89 | expected_mountpt = |
| 90 | chromeos_constants.CRYPTOHOME_MOUNT_PT, |
| 91 | allow_fail = False): |
| 92 | mount_parts = __get_mount_parts(device, allow_fail) |
| 93 | return (len(mount_parts) > 2 and device == mount_parts[0] and |
| 94 | 'tmpfs' == mount_parts[2]) |