def main(argv): cros_build_lib.AssertInsideChroot() options = GetOptions(argv) if options.action == ACTION_GET_ENTRY: db = user_db.UserDB(options.sysroot) if options.database == USER_DB: print(db.GetUserEntry(options.name, skip_lock=options.nolock)) else: print(db.GetGroupEntry(options.name, skip_lock=options.nolock)) return 0 overlays = sysroot_lib.Sysroot(options.sysroot).GetStandardField( sysroot_lib.STANDARD_FIELD_PORTDIR_OVERLAY).split() # TODO(wiley) This process could be optimized to avoid reparsing these # overlay databases each time. account_db = accounts_lib.AccountDatabase() for overlay_path in overlays: database_path = os.path.join(overlay_path, ACCOUNT_DB_FILENAME) if os.path.exists(database_path): account_db.AddAccountsFromDatabase(database_path) installed_users = user_db.UserDB(options.sysroot) if options.action == ACTION_INSTALL_USER: account_db.InstallUser(options.name, installed_users, uid=options.uid, shell=options.shell, homedir=options.home, primary_group=options.primary_group) homedir = account_db.users[options.name].home homedir_path = os.path.join(options.sysroot, homedir) if homedir != '/dev/null' and not os.path.exists(homedir_path): osutils.SafeMakedirs(homedir_path, sudo=True) uid = account_db.users[options.name].uid cros_build_lib.sudo_run( ['chown', '%d:%d' % (uid, uid), homedir_path], print_cmd=False) elif options.action == ACTION_INSTALL_GROUP: account_db.InstallGroup(options.name, installed_users, gid=options.gid) else: cros_build_lib.Die('Unsupported account type: %s' % options.account_type)
def testCanAddGroup(self): """Test that we can correctly add a group to a database.""" new_group = user_db.Group(group='foo', password='******', gid=1000, users=[]) self.assertFalse(self._user_db.GroupExists(new_group.group)) self._user_db.AddGroup(new_group) self.assertTrue(self._user_db.GroupExists(new_group.group)) # New instances should just see the new group. new_db = user_db.UserDB(self.tempdir) self.assertTrue(new_db.GroupExists(new_group.group))
def testCanAddUser(self): """Test that we can correctly add a user to a database.""" new_user = user_db.User(user='******', password='******', uid=1000, gid=1000, gecos='test', home='/dev/null', shell='/bin/false') self.assertFalse(self._user_db.UserExists(new_user.user)) self._user_db.AddUser(new_user) self.assertTrue(self._user_db.UserExists(new_user.user)) # New instances should just see the new user. new_db = user_db.UserDB(self.tempdir) self.assertTrue(new_db.UserExists(new_user.user))
def testToleratesMalformedLines(self): """Check that skip over invalid lines in databases.""" bad_user_contents = '\n'.join([ 'no colon on this line', '::::::', 'root:x:not a uid:0:root:/root:/bin/bash', 'root:x:0:not a gid:root:/root:/bin/bash', 'root:x:0:0:root:/root', 'root:x:0:0:root:/root:/bin/bash:', 'bar:x:1:1:bar user:/home/bar:/bin/sh' ]) bad_group_contents = '\n'.join([ 'no colon on this line', ':::', 'root:x:not a gid:', 'root:x:0', 'root:x:0::', 'bar:x:1:' ]) self._SetupDatabases(bad_user_contents, bad_group_contents) db = user_db.UserDB(self.tempdir) self.assertTrue(db.UserExists('bar')) self.assertTrue(db.GroupExists('bar')) self.assertFalse(db.UserExists('root')) self.assertFalse(db.GroupExists('root'))
def setUp(self): """Set up a test environment.""" self._SetupDatabases(MOCK_PASSWD_CONTENTS, MOCK_GROUP_CONTENTS) self._user_db = user_db.UserDB(self.tempdir) self.PatchObject(os, 'getuid', return_value=0)
def __init__(self, sysroot): self._sysroot = sysroot self._user_db = user_db.UserDB(sysroot)