blob: 6635116dd2a7313555fcd7eb4085e38024c664f0 [file] [log] [blame]
Chris Masone5e06f182010-03-23 08:29:52 -07001# 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 Lif7b81922011-03-04 14:39:35 -08005import logging, os, re
Eric Lic4d8f4a2010-12-10 09:49:23 -08006import common
7import constants as chromeos_constants
Eric Lif7b81922011-03-04 14:39:35 -08008from autotest_lib.client.bin import test, utils
Chris Masone5e06f182010-03-23 08:29:52 -07009from autotest_lib.client.common_lib import error
Eric Lic4d8f4a2010-12-10 09:49:23 -080010
Sean Oe5d8fd02010-09-30 10:44:44 +020011
12CRYPTOHOME_CMD = '/usr/sbin/cryptohome'
13
14class ChromiumOSError(error.InstallError):
15 """Generic error for ChromiumOS-specific exceptions."""
16 pass
17
18
19def __run_cmd(cmd):
20 return utils.system_output(cmd + ' 2>&1', retain_output=True,
21 ignore_status=True).strip()
22
23
24def 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
30def 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
42def 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
57def 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
63def 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
72def __get_mount_parts(expected_mountpt=chromeos_constants.CRYPTOHOME_MOUNT_PT,
Sourav Poddar574bd622010-05-26 14:22:26 +053073 allow_fail = False):
Will Drewry81ad6162010-04-01 10:26:07 -050074 mount_line = utils.system_output(
75 'grep %s /proc/$(pgrep cryptohomed)/mounts' % expected_mountpt,
Sourav Poddar574bd622010-05-26 14:22:26 +053076 ignore_status = allow_fail)
77 return mount_line.split()
78
79
Frank Swiderski52653c32010-05-26 17:40:47 -070080def 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 Poddar574bd622010-05-26 14:22:26 +053088
89
Frank Swiderski52653c32010-05-26 17:40:47 -070090def is_mounted_on_tmpfs(device = chromeos_constants.CRYPTOHOME_INCOGNITO,
Sourav Poddar574bd622010-05-26 14:22:26 +053091 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])