forked from saltstack/salt
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgroupadd.py
More file actions
219 lines (165 loc) · 5.06 KB
/
Copy pathgroupadd.py
File metadata and controls
219 lines (165 loc) · 5.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
# -*- coding: utf-8 -*-
'''
Manage groups on Linux, OpenBSD and NetBSD
'''
# Import python libs
try:
import grp
except ImportError:
pass
# Define the module's virtual name
__virtualname__ = 'group'
def __virtual__():
'''
Set the user module if the kernel is Linux or OpenBSD
'''
if __grains__['kernel'] in ('Linux', 'OpenBSD', 'NetBSD'):
return __virtualname__
return False
def add(name, gid=None, system=False):
'''
Add the specified group
CLI Example:
.. code-block:: bash
salt '*' group.add foo 3456
'''
cmd = 'groupadd '
if gid:
cmd += '-g {0} '.format(gid)
if system and __grains__['kernel'] != 'OpenBSD':
cmd += '-r '
cmd += name
ret = __salt__['cmd.run_all'](cmd)
return not ret['retcode']
def delete(name):
'''
Remove the named group
CLI Example:
.. code-block:: bash
salt '*' group.delete foo
'''
ret = __salt__['cmd.run_all']('groupdel {0}'.format(name))
return not ret['retcode']
def info(name):
'''
Return information about a group
CLI Example:
.. code-block:: bash
salt '*' group.info foo
'''
try:
grinfo = grp.getgrnam(name)
except KeyError:
return {}
else:
return _format_info(grinfo)
def _format_info(data):
'''
Return formatted information in a pretty way.
'''
return {'name': data.gr_name,
'passwd': data.gr_passwd,
'gid': data.gr_gid,
'members': data.gr_mem}
def getent(refresh=False):
'''
Return info on all groups
CLI Example:
.. code-block:: bash
salt '*' group.getent
'''
if 'group.getent' in __context__ and not refresh:
return __context__['group.getent']
ret = []
for grinfo in grp.getgrall():
ret.append(_format_info(grinfo))
__context__['group.getent'] = ret
return ret
def chgid(name, gid):
'''
Change the gid for a named group
CLI Example:
.. code-block:: bash
salt '*' group.chgid foo 4376
'''
pre_gid = __salt__['file.group_to_gid'](name)
if gid == pre_gid:
return True
cmd = 'groupmod -g {0} {1}'.format(gid, name)
__salt__['cmd.run'](cmd)
post_gid = __salt__['file.group_to_gid'](name)
if post_gid != pre_gid:
return post_gid == gid
return False
def adduser(name, username):
'''
Add a user in the group.
CLI Example:
.. code-block:: bash
salt '*' group.adduser foo bar
Verifies if a valid username 'bar' as a member of an existing group 'foo',
if not then adds it.
'''
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
def deluser(name, username):
'''
Remove a user from the group.
CLI Example:
.. code-block:: bash
salt '*' group.deluser foo bar
Removes a member user 'bar' from a group 'foo'. If group is not present
then returns True.
'''
grp_info = __salt__['group.info'](name)
try:
if username in grp_info['members']:
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
except Exception:
return True
def members(name, members_list):
'''
Replaces members of the group with a provided list.
CLI Example:
salt '*' group.members foo 'user1,user2,user3,...'
Replaces a membership list for a local group 'foo'.
foo:x:1234:user1,user2,user3,...
'''
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