From abae84e9f86b4e2714eeedd6c9da7b87ad1ec23f Mon Sep 17 00:00:00 2001 From: Antoine Jacoutot Date: Wed, 27 Aug 2014 12:23:20 +0200 Subject: [PATCH] groupadd: fix and implement missing functions for OpenBSD gpasswd is only available on Linux and groupadd(8) does not support `-r' on OpenBSD. Add OpenBSD support for deluser() and memebers(). --- salt/modules/groupadd.py | 48 +++++++++++++++++++++++++++++++++------- 1 file changed, 40 insertions(+), 8 deletions(-) diff --git a/salt/modules/groupadd.py b/salt/modules/groupadd.py index 6f1263f256..43d1b467d4 100644 --- a/salt/modules/groupadd.py +++ b/salt/modules/groupadd.py @@ -1,6 +1,6 @@ # -*- coding: utf-8 -*- ''' -Manage groups on Linux and OpenBSD +Manage groups on Linux, OpenBSD and NetBSD ''' # Import python libs @@ -35,7 +35,7 @@ def add(name, gid=None, system=False): cmd = 'groupadd ' if gid: cmd += '-g {0} '.format(gid) - if system: + if system and __grains__['kernel'] != 'OpenBSD': cmd += '-r ' cmd += name @@ -141,8 +141,13 @@ def adduser(name, username): Verifies if a valid username 'bar' as a member of an existing group 'foo', if not then adds it. ''' - retcode = __salt__['cmd.retcode']('gpasswd --add {0} {1}'.format(username, - name)) + if __grains__['kernel'] == 'Linux': + retcode = __salt__['cmd.retcode']('gpasswd --add {0} {1}'.format( + username,name)) + else: + retcode = __salt__['cmd.retcode']('usermod -G {0} {1}'.format( + name,username)) + return not retcode @@ -163,8 +168,17 @@ def deluser(name, username): try: if username in grp_info['members']: print username - retcode = __salt__['cmd.retcode']('gpasswd --del {0} {1}'.format( - username, name)) + if __grains__['kernel'] == 'Linux': + retcode = __salt__['cmd.retcode']('gpasswd --del {0} {1}' + .format(username, name)) + elif __grains__['kernel'] == 'OpenBSD': + cmd = 'usermod -S ' + out = __salt__['cmd.run_stdout']('id -Gn {0}'.format(username)) + for group in out.split(" "): + if group != format(name): + cmd += '{0},'.format(group) + retcode = __salt__['cmd.retcode']('{0} {1}'.format( + cmd, username)) return not retcode else: return True @@ -183,6 +197,24 @@ def members(name, members_list): Replaces a membership list for a local group 'foo'. foo:x:1234:user1,user2,user3,... ''' - retcode = __salt__['cmd.retcode']('gpasswd --members {0} {1}'.format( - members_list, name)) + if __grains__['kernel'] == 'Linux': + retcode = __salt__['cmd.retcode']('gpasswd --members {0} {1}'.format( + members_list, name)) + elif __grains__['kernel'] == 'OpenBSD': + retcode = 1 + grp_info = __salt__['group.info'](name) + if grp_info and name in grp_info['name']: + __salt__['cmd.run']('groupdel {0}'.format(name)) + __salt__['cmd.run']('groupadd -g {0} {1}'.format( + grp_info['gid'], name)) + for user in members_list.split(","): + if user: + retcode = __salt__['cmd.retcode']( + 'usermod -G {0} {1}'.format(name,user)) + if not retcode == 0: + break + # provided list is '': users previously deleted from group + else: + retcode = 0 + return not retcode