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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
|
#-*- Mode: perl; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
#
# Copyright (C) 2000-2001 Ximian, Inc.
#
# Authors: Hans Petter Jansson <hpj@ximian.com>,
# Arturo Espinosa <arturo@ximian.com>,
# Tambet Ingo <tambet@ximian.com>.
# Grzegorz Golawski <grzegol@pld-linux.org> (PLD Support)
#
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU Library General Public License as published
# by the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Library General Public License for more details.
#
# You should have received a copy of the GNU Library General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
package Users::Groups;
# enum like for verbose group array positions
my $LOGIN = 0;
my $PASSWD = 1;
my $GID = 2;
my $USERS = 3;
# quite generic data
$group_names = "/etc/group";
# Where are the tools?
$cmd_groupdel = &Utils::File::locate_tool ("groupdel");
$cmd_groupadd = &Utils::File::locate_tool ("groupadd");
$cmd_groupmod = &Utils::File::locate_tool ("groupmod");
$cmd_delgroup = &Utils::File::locate_tool ("delgroup");
$cmd_addgroup = &Utils::File::locate_tool ("addgroup");
$cmd_gpasswd = &Utils::File::locate_tool ("gpasswd");
$cmd_pw = &Utils::File::locate_tool ("pw");
sub del_group
{
my ($group) = @_;
if ($Utils::Backend::tool{"system"} eq "FreeBSD")
{
$command = "$cmd_pw groupdel -n \'" . $$group[$LOGIN] . "\'";
}
else
{
$command = ($cmd_delgroup) ? $cmd_delgroup : $cmd_groupdel;
$command .= " \'" . $$group[$LOGIN] . "\'";
}
&Utils::File::run ($command);
}
sub add_group
{
my ($group) = @_;
my ($u, $user, $users);
$u = $$group[$USERS];
if ($Utils::Backend::tool{"system"} eq "FreeBSD")
{
$users = join (",", sort @$u);
$command = "$cmd_pw groupadd -n \'" . $$group[$LOGIN] .
"\' -g \'" . $$group[$GID] .
"\' -M \'" . $users . "\'";
&Utils::File::run ($command);
}
else
{
if ($cmd_addgroup)
{
$command = "$cmd_addgroup " .
"--gid \'" . $$group[$GID] . "\' " . $$group[$LOGIN];
}
else
{
$command = "$cmd_groupadd -g \'" . $$group[$GID] .
"\' " . $$group[$LOGIN];
}
&Utils::File::run ($command);
foreach $user (sort @$u)
{
$command = "$cmd_gpasswd -a \'" . $user .
"\' " . $$group[$LOGIN];
&Utils::File::run ($command);
}
}
}
sub change_group
{
my ($old_group, $new_group) = @_;
my (%users, %user, $users_arr, $str);
my ($n, $o, $users, $i, $j, $max_n, $max_o, $r, @tmp); # for iterations
if ($Utils::Backend::tool{"system"} eq "FreeBSD")
{
$users_arr = $$new_group[$USERS];
$str = join (",", sort @$users_arr);
$command = "$cmd_pw groupmod -n \'" . $$old_group[$LOGIN] .
"\' -g \'" . $$new_group[$GID] .
"\' -l \'" . $$new_group[$LOGIN] .
"\' -M \'" . $str . "\'";
&Utils::File::run ($command);
}
else
{
$command = "$cmd_groupmod -g \'" . $$new_group[$GID] .
"\' -n \'" . $$new_group[$LOGIN] . "\' " .
"\'" . $$old_group[$LOGIN] . "\'";
&Utils::File::run ($command);
# Let's see if the users that compose the group have changed.
if (!Utils::Util::struct_eq ($$new_group[$USERS], $$old_group[$USERS]))
{
$users{$_} |= 1 foreach (@{$$new_group[$USERS]});
$users{$_} |= 2 foreach (@{$$old_group[$USERS]});
foreach $user (keys %users)
{
$state = $users{$user};
if ($state == 2)
{
# users with state 2 are those that only appeared
# in the old group configuration, so we must delete them
$command = "$cmd_gpasswd -d \'" . $user . "\' \'" .
$$new_group[$LOGIN] . "\'";
&Utils::File::run ($command);
}
else
{
# users with state 1 are those who were added
# to the new group configuration
$command = "$cmd_gpasswd -a \'" . $user . "\' \'" .
$$new_group[$LOGIN] . "\'";
&Utils::File::run ($command);
}
}
}
}
}
sub get
{
my ($ifh, @groups, $group_last_modified);
my (@line, $copy, @a);
# Find the file.
$ifh = &Utils::File::open_read_from_names($group_names);
return unless ($ifh);
# Parse the file.
@groups = ();
while (<$ifh>)
{
chomp;
# FreeBSD allows comments in the group file. */
next if &Utils::Util::ignore_line ($_);
$_ = &Utils::XML::unquote ($_);
@line = split ':', $_, -1;
@a = split ',', pop @line;
push @line, [@a];
$copy = [@line];
push (@groups, $copy);
}
&Utils::File::close_file ($ifh);
return \@groups;
}
sub get_files
{
my @arr;
push @arr, $group_names;
return \@arr;
}
sub set
{
my ($config) = @_;
my ($old_config, %groups);
my (%config_hash, %old_config_hash);
if ($config)
{
# Make backup manually, otherwise they don't get backed up.
&Utils::File::do_backup ($group_names);
$old_config = &get ();
foreach $i (@$config)
{
$groups{$$i[$LOGIN]} |= 1;
$config_hash{$$i[$LOGIN]} = $i;
}
foreach $i (@$old_config)
{
$groups{$$i[$LOGIN]} |= 2;
$old_config_hash{$$i[$LOGIN]} = $i;
}
# Delete all groups that only appeared in the old configuration
foreach $i (sort (keys (%groups)))
{
$state = $groups{$i};
if ($state == 1)
{
# Groups with state 1 have been added to the config
&add_group ($config_hash{$i});
}
elsif ($state == 2)
{
# Groups with state 2 have been deleted from the config
&del_group ($old_config_hash{$i});
}
elsif (($state == 3) &&
(!Utils::Util::struct_eq ($config_hash{$i}, $old_config_hash{$i})))
{
&change_group ($old_config_hash{$i}, $config_hash{$i});
}
}
}
}
1;
|