From c638f466a362ce453b0d75418d4851996ae9a29d Mon Sep 17 00:00:00 2001 From: Chris Lumens Date: Mon, 21 Jan 2008 17:28:11 -0500 Subject: Try to fix a problem creating users via kickstart (#428891). --- users.py | 87 +++++++++++++++++++++++++++++++++++++--------------------------- 1 file changed, 50 insertions(+), 37 deletions(-) (limited to 'users.py') diff --git a/users.py b/users.py index f71563447..9c6ba77d4 100644 --- a/users.py +++ b/users.py @@ -1,7 +1,7 @@ # # users.py: Code for creating user accounts and setting the root password # -# Copyright (C) 2006, 2007 Red Hat, Inc. All rights reserved. +# Copyright (C) 2006, 2007, 2008 Red Hat, Inc. All rights reserved. # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by @@ -27,6 +27,9 @@ import tempfile import os import os.path +import logging +log = logging.getLogger("anaconda") + def createLuserConf(instPath): """Writes a libuser.conf for instPath.""" (fd, fn) = tempfile.mkstemp(prefix="libuser.") @@ -68,53 +71,63 @@ class Users: def createUser (self, name, password=None, isCrypted=False, groups=[], homedir=None, shell=None, uid=None, lock=False, root="/mnt/sysimage"): - if self.admin.lookupUserByName(name): - return None + childpid = os.fork() - userEnt = self.admin.initUser(name) - groupEnt = self.admin.initGroup(name) + if not childpid: + os.chroot(root) + os.unsetenv("LIBUSER_CONF") - grpLst = filter(lambda grp: grp, - map(lambda name: self.admin.lookupGroupByName(name), groups)) - userEnt.set(libuser.GIDNUMBER, [groupEnt.get(libuser.GIDNUMBER)[0]] + - map(lambda grp: grp.get(libuser.GIDNUMBER)[0], grpLst)) + if self.admin.lookupUserByName(name): + os._exit(1) - if not homedir: - homedir = "/home/" + name + userEnt = self.admin.initUser(name) + groupEnt = self.admin.initGroup(name) - # Do this to make the user's home dir under the install root. - if homedir[0] != "/": - userEnt.set(libuser.HOMEDIRECTORY, root + "/" + homedir) - else: - userEnt.set(libuser.HOMEDIRECTORY, root + homedir) + grpLst = filter(lambda grp: grp, + map(lambda name: self.admin.lookupGroupByName(name), groups)) + userEnt.set(libuser.GIDNUMBER, [groupEnt.get(libuser.GIDNUMBER)[0]] + + map(lambda grp: grp.get(libuser.GIDNUMBER)[0], grpLst)) - if shell: - userEnt.set(libuser.LOGINSHELL, shell) + if not homedir: + homedir = "/home/" + name - if uid >= 0: - userEnt.set(libuser.UIDNUMBER, uid) + userEnt.set(libuser.HOMEDIRECTORY, homedir) - self.admin.addUser(userEnt) - self.admin.addGroup(groupEnt) + if shell: + userEnt.set(libuser.LOGINSHELL, shell) - if password: - if isCrypted: - self.admin.setpassUser(userEnt, password, isCrypted) - else: - self.admin.setpassUser(userEnt, cryptPassword(password, True), isCrypted) + if uid >= 0: + userEnt.set(libuser.UIDNUMBER, uid) - if lock: - self.admin.lockUser(userEnt) + self.admin.addUser(userEnt) + self.admin.addGroup(groupEnt) + + if password: + if isCrypted: + self.admin.setpassUser(userEnt, password, isCrypted) + else: + self.admin.setpassUser(userEnt, cryptPassword(password, True), isCrypted) - # Add the user to all the groups they should be part of. - for grp in grpLst: - grp.add(libuser.MEMBERNAME, name) - self.admin.modifyGroup(grp) + if lock: + self.admin.lockUser(userEnt) - # Now set the correct home directory to fix up passwd. - userEnt.set(libuser.HOMEDIRECTORY, homedir) - self.admin.modifyUser(userEnt) - return True + # Add the user to all the groups they should be part of. + for grp in grpLst: + grp.add(libuser.MEMBERNAME, name) + self.admin.modifyGroup(grp) + + os._exit(0) + + try: + (pid, status) = os.waitpid(childpid, 0) + except OSError, (num, msg): + log.critical("exception from waitpid while creating a user: %s %s" % (num, msg)) + return False + + if os.WIFEXITED(status) and (os.WEXITSTATUS(status) == 0): + return True + else: + return False def setRootPassword(self, password, isCrypted, useMD5, lock): rootUser = self.admin.lookupUserByName("root") -- cgit