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