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

View File

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