Allow useradd and groupadd to use alternate root

This commit is contained in:
Joseph Hall 2015-12-17 16:23:27 -07:00
parent c7d3ac58ef
commit 1eb3b9c5e4
2 changed files with 85 additions and 24 deletions

View File

@ -28,7 +28,7 @@ def __virtual__():
' only available on Linux, OpenBSD and NetBSD')
def add(name, gid=None, system=False):
def add(name, gid=None, system=False, root=None):
'''
Add the specified group
@ -45,12 +45,15 @@ def add(name, gid=None, system=False):
cmd += '-r '
cmd += name
if root is not None:
cmd.extend(('-R', root))
ret = __salt__['cmd.run_all'](cmd, python_shell=False)
return not ret['retcode']
def delete(name):
def delete(name, root=None):
'''
Remove the named group
@ -60,7 +63,12 @@ def delete(name):
salt '*' group.delete foo
'''
ret = __salt__['cmd.run_all']('groupdel {0}'.format(name), python_shell=False)
cmd = ('groupdel', name)
if root is not None:
cmd.extend(('-R', root))
ret = __salt__['cmd.run_all'](cmd, python_shell=False)
return not ret['retcode']
@ -113,7 +121,7 @@ def getent(refresh=False):
return ret
def chgid(name, gid):
def chgid(name, gid, root=None):
'''
Change the gid for a named group
@ -126,7 +134,11 @@ def chgid(name, gid):
pre_gid = __salt__['file.group_to_gid'](name)
if gid == pre_gid:
return True
cmd = 'groupmod -g {0} {1}'.format(gid, name)
cmd = ('groupmod', '-g', gid, name)
if root is not None:
cmd.extend(('-R', root))
__salt__['cmd.run'](cmd, python_shell=False)
post_gid = __salt__['file.group_to_gid'](name)
if post_gid != pre_gid:
@ -134,7 +146,7 @@ def chgid(name, gid):
return False
def adduser(name, username):
def adduser(name, username, root=None):
'''
Add a user in the group.
@ -151,18 +163,22 @@ def adduser(name, username):
if __grains__['kernel'] == 'Linux':
if on_redhat_5:
cmd = 'gpasswd -a {0} {1}'.format(username, name)
cmd = ('gpasswd', '-a', username, name)
else:
cmd = 'gpasswd --add {0} {1}'.format(username, name)
cmd = ('gpasswd', '--add', username, name)
if root is not None:
cmd.extend(('-Q', root))
else:
cmd = 'usermod -G {0} {1}'.format(name, username)
cmd = ('usermod', '-G', name, username)
if root is not None:
cmd.extend(('-R', root))
retcode = __salt__['cmd.retcode'](cmd, python_shell=False)
return not retcode
def deluser(name, username):
def deluser(name, username, root=None):
'''
Remove a user from the group.
@ -182,9 +198,11 @@ def deluser(name, username):
if username in grp_info['members']:
if __grains__['kernel'] == 'Linux':
if on_redhat_5:
cmd = 'gpasswd -d {0} {1}'.format(username, name)
cmd = ('gpasswd', '-d', username, name)
else:
cmd = 'gpasswd --del {0} {1}'.format(username, name)
cmd = ('gpasswd', '--del', username, name)
if root is not None:
cmd.extend(('-R', root))
retcode = __salt__['cmd.retcode'](cmd, python_shell=False)
elif __grains__['kernel'] == 'OpenBSD':
out = __salt__['cmd.run_stdout']('id -Gn {0}'.format(username),
@ -203,7 +221,7 @@ def deluser(name, username):
return True
def members(name, members_list):
def members(name, members_list, root=None):
'''
Replaces members of the group with a provided list.
@ -218,9 +236,11 @@ def members(name, members_list):
if __grains__['kernel'] == 'Linux':
if on_redhat_5:
cmd = 'gpasswd -M {0} {1}'.format(members_list, name)
cmd = ('gpasswd', '-M', members_list, name)
else:
cmd = 'gpasswd --members {0} {1}'.format(members_list, name)
cmd = ('gpasswd', '--members', members_list, name)
if root is not None:
cmd.extend(('-R', root))
retcode = __salt__['cmd.retcode'](cmd, python_shell=False)
elif __grains__['kernel'] == 'OpenBSD':
retcode = 1

View File

@ -61,7 +61,7 @@ def _build_gecos(gecos_dict):
gecos_dict.get('homephone', ''))
def _update_gecos(name, key, value):
def _update_gecos(name, key, value, root=None):
'''
Common code to change a user's GECOS information
'''
@ -76,6 +76,10 @@ def _update_gecos(name, key, value):
return True
gecos_data = copy.deepcopy(pre_info)
gecos_data[key] = value
if root is not None:
cmd.extend(('-R', root))
cmd = ['usermod', '-c', _build_gecos(gecos_data), name]
__salt__['cmd.run'](cmd, python_shell=False)
post_info = info(name)
@ -95,7 +99,8 @@ def add(name,
workphone='',
homephone='',
createhome=True,
loginclass=None):
loginclass=None,
root=None):
'''
Add a user to the minion
@ -175,6 +180,9 @@ def add(name,
cmd.append(name)
if root is not None:
cmd.extend(('-R', root))
ret = __salt__['cmd.run_all'](cmd, python_shell=False)
if ret['retcode'] != 0:
@ -201,7 +209,7 @@ def add(name,
return True
def delete(name, remove=False, force=False):
def delete(name, remove=False, force=False, root=None):
'''
Remove a user from the minion
@ -221,6 +229,9 @@ def delete(name, remove=False, force=False):
cmd.append(name)
if root is not None:
cmd.extend(('-R', root))
ret = __salt__['cmd.run_all'](cmd, python_shell=False)
if ret['retcode'] == 0:
@ -283,7 +294,7 @@ def chuid(name, uid):
return info(name).get('uid') == uid
def chgid(name, gid):
def chgid(name, gid, root=None):
'''
Change the default group of the user
@ -297,11 +308,15 @@ def chgid(name, gid):
if gid == pre_info['gid']:
return True
cmd = ['usermod', '-g', '{0}'.format(gid), name]
if root is not None:
cmd.extend(('-R', root))
__salt__['cmd.run'](cmd, python_shell=False)
return info(name).get('gid') == gid
def chshell(name, shell):
def chshell(name, shell, root=None):
'''
Change the default shell of the user
@ -315,11 +330,15 @@ def chshell(name, shell):
if shell == pre_info['shell']:
return True
cmd = ['usermod', '-s', shell, name]
if root is not None:
cmd.extend(('-R', root))
__salt__['cmd.run'](cmd, python_shell=False)
return info(name).get('shell') == shell
def chhome(name, home, persist=False):
def chhome(name, home, persist=False, root=None):
'''
Change the home directory of the user, pass True for persist to move files
to the new home directory if the old home directory exist.
@ -334,6 +353,10 @@ def chhome(name, home, persist=False):
if home == pre_info['home']:
return True
cmd = ['usermod', '-d', '{0}'.format(home)]
if root is not None:
cmd.extend(('-R', root))
if persist and __grains__['kernel'] != 'OpenBSD':
cmd.append('-m')
cmd.append(name)
@ -341,7 +364,7 @@ def chhome(name, home, persist=False):
return info(name).get('home') == home
def chgroups(name, groups, append=False):
def chgroups(name, groups, append=False, root=None):
'''
Change the groups to which this user belongs
@ -368,6 +391,7 @@ def chgroups(name, groups, append=False):
if ugrps == set(groups):
return True
cmd = ['usermod']
if __grains__['kernel'] != 'OpenBSD':
if append:
cmd.append('-a')
@ -376,9 +400,14 @@ def chgroups(name, groups, append=False):
cmd.append('-G')
else:
cmd.append('-S')
if __grains__['kernel'] != 'OpenBSD':
cmd.append('-G')
cmd.extend([','.join(groups), name])
if root is not None:
cmd.extend(('-R', root))
result = __salt__['cmd.run_all'](cmd, python_shell=False)
# try to fallback on gpasswd to add user to localgroups
# for old lib-pamldap support
@ -445,7 +474,7 @@ def chhomephone(name, homephone):
return _update_gecos(name, 'homephone', homephone)
def chloginclass(name, loginclass):
def chloginclass(name, loginclass, root=None):
'''
Change the default login class of the user
@ -460,9 +489,15 @@ def chloginclass(name, loginclass):
'''
if __grains__['kernel'] != 'OpenBSD':
return False
if loginclass == get_loginclass(name):
return True
cmd = ['usermod', '-L', '{0}'.format(loginclass), '{0}'.format(name)]
if root is not None:
cmd.extend(('-R', root))
__salt__['cmd.run'](cmd, python_shell=False)
return get_loginclass(name) == loginclass
@ -564,7 +599,7 @@ def list_users():
return sorted([user.pw_name for user in pwd.getpwall()])
def rename(name, new_name):
def rename(name, new_name, root=None):
'''
Change the username for a named user
@ -577,11 +612,17 @@ def rename(name, new_name):
current_info = info(name)
if not current_info:
raise CommandExecutionError('User \'{0}\' does not exist'.format(name))
new_info = info(new_name)
if new_info:
raise CommandExecutionError(
'User \'{0}\' already exists'.format(new_name)
)
cmd = ['usermod', '-l', '{0}'.format(new_name), '{0}'.format(name)]
if root is not None:
cmd.extend(('-R', root))
__salt__['cmd.run'](cmd, python_shell=False)
return info(name).get('name') == new_name