3 # Copyright 1996-2013 Ian Jackson <ijackson@chiark.greenend.org.uk>
4 # Copyright 1998 David Damerell <damerell@chiark.greenend.org.uk>
6 # Chancellor Masters and Scholars of the University of Cambridge
7 # Copyright 2010 Tony Finch <fanf@dotat.at>
9 # This is free software; you can redistribute it and/or modify it
10 # under the terms of the GNU General Public License as published by
11 # the Free Software Foundation; either version 3 of the License, or
12 # (at your option) any later version.
14 # This program is distributed in the hope that it will be useful, but
15 # WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 # General Public License for more details.
19 # You should have received a copy of the GNU General Public License
20 # along with userv-utils; if not, see http://www.gnu.org/licenses/.
25 print(<<END) || die "groupmanage: write usage: $!\n";
28 groupmanage <groupname> [--info]
29 groupmanage <groupname> <action> [<action> ...]
30 groupmanage <groupname> --create [<action> <action> ...]
33 --add <username> <username> ...
34 --remove <username> <username> ...
36 --manager-add <username> <username> ...
37 --manager-remove <username> <username> ...
39 --owner <username> [root only]
40 groupmanage is Copyright. It is free software, released under the GNU
41 GPL v2 or later. There is NO WARRANTY. See the GPL for details.
46 @ARGV || &usage('too few arguments');
49 exec 'userv','root','groupmanage',@ARGV;
50 &quit("unable to execute userv to gain root privilege: $!");
53 chdir("/etc") || die "groupmanage: chdir /etc: $!\n";
55 $groupname= shift(@ARGV);
56 $groupname =~ y/\n//d;
58 $groupname =~ m/^\w[-0-9A-Za-z]*$/ ||
59 &quit("first argument is invalid - must be group name");
61 @ARGV || push(@ARGV,'--info');
63 $callinguser= exists $ENV{'USERV_UID'} ? $ENV{'USERV_UID'} : $<;
65 %opt= ('user-create','0',
66 'user-create-minunameu','5',
67 'user-create-min','10000',
68 'user-create-max','19999',
69 'user-create-nameintitle','0',
70 'user-create-maxperu','5',
73 'grouplist-file','grouplist',
78 %ovalid= ('user-create','boolean',
79 'user-create-minunameu','number',
80 'user-create-min','number',
81 'user-create-max','number',
82 'user-create-nameintitle','boolean',
83 'user-create-maxperu','number',
84 'group-file','string',
86 'grouplist-file','string',
87 'name-regexp','string',
88 'name-maxlen','number',
89 'admin-group','string',
90 'finish-command','string');
93 $cov= $_ eq 'yes' ? 1 :
95 &quit("groupmanage.conf:$.: bad boolean value");
99 m/^[0-9]{1,10}$/ || &quit("groupmanage.conf:$.: bad numerical value");
106 open(GMC,"groupmanage.conf") || &quit("read groupmanage.conf: $!");
108 next if m/^\#/ || !m/\S/;
110 s/^\s*([-0-9a-z]+)\s*// || &quit("groupmanage.conf:$.: bad option format");
112 defined($opt{$co}) || &quit("groupmanage.conf:$.: unknown option $co");
114 $ovf= 'ov_'.$ovalid{$co};
120 if ($ARGV[0] eq '--info') {
121 @ARGV == 1 || &usage('no arguments allowed after --info');
131 $callinguser || return;
133 if ($opt{'user-create-minunameu'}) {
134 print(STDERR <<END) || &quit("write err re name: $!");
135 groupmanage: groups you create must be named after you ...
136 <usernamepart>-<identifier>
137 You must quote at least $opt{'user-create-minunameu'} chars of your username $createby
138 (or all of it if it is shorter).
141 if ($opt{'name-regexp'}) {
142 print(STDERR <<END) || &quit("write err re name: $!");
143 groupmanage: groups you create must match a pattern...
144 The pattern is the Perl regular expression /$opt{'name-regexp'}/.
150 if ($ARGV[0] eq '--create') {
151 $opt{'user-create'} || !$callinguser ||
152 &quit("group creation by users disabled by administrator");
153 length($groupname) <= $opt{'name-maxlen'} ||
154 &quit("group names must be $opt{'name-maxlen'} chars or fewer");
155 $!=0; (@pw= getpwuid($callinguser))
156 || &quit("cannot get your passwd entry: $!");
158 if ($opt{'user-create-minunameu'}) {
159 $groupname =~ m/^([-0-9A-Za-z]+)-([0-9a-z]+)$/ || &naming;
162 $upart eq $createby ||
163 (length($upart) >= $opt{'user-create-minunameu'} &&
164 substr($createby,0,length($upart)) eq $upart)
167 $groupname =~ m/${opt{'name-regexp'}}/ || &naming;
177 $bythisowner < $opt{'user-create-maxperu'} ||
178 &quit("you already have $bythisowner group(s)");
179 $groupfileix==-1 || &quit("group already exists, cannot create it");
180 $grouplistix==-1 || &quit("group is already in grouplist, cannot create it");
181 for ($gid= $opt{'user-create-min'};
182 $gid < $opt{'user-create-max'} && defined(getgrgid($gid));
184 $gid <= $opt{'user-create-max'} || &quit("out of gids to use, contact admin");
185 $password=''; @members=($createby);
186 $description= "${createby}'s -- user-defined, no title";
187 $owner= $createby; @managers=(); @members= ($createby);
188 $groupfileix=$#groupfile+1;
189 $grouplistix=$#grouplist+1;
190 &p("created group $groupname");
193 &p("modifying group $groupname");
196 &weare($owner) || grep(&weare($_),@managers) || !$callinguser ||
197 &quit("you may not manage $groupname");
202 if (m/^--(add|remove)$/) {
203 $action= $1; $clist= 'members'; $what= 'member';
204 } elsif (m/^--owner$/) {
205 !$callinguser || &quit("only root may change owner");
206 @ARGV || &usage("no username owner after --owner");
207 $owner= shift(@ARGV);
208 &p("owner set to $owner");
209 } elsif (m/^--manager-(add|remove)$/) {
210 $action= $1; $clist= 'managers'; $what= 'manager';
211 } elsif (m/^--clear$/) {
212 &p('cleared list of members');
213 @members=(); $action='none'; $memc++;
214 } elsif (m/^--manager-clear$/) {
215 &p('cleared list of managers');
216 @managers=(); $action='none';
217 } elsif (m/^--title$/) {
218 &weare($owner) || !$callinguser ||
219 &quit("only group's owner ($owner) may change title");
220 @ARGV || &usage("no title after --title");
221 $_= shift(@ARGV); y/\020-\176//cd; y/:\\//d;
222 if ($opt{'user-create-nameintitle'} &&
223 $gid >= $opt{'user-create-min'} && $gid <= $opt{'user-create-max'}) {
224 $_= "${owner}'s -- $_";
227 &p("title set to $description");
229 &usage("unknown option $_");
230 } elsif (m/^\w[-0-9A-Za-z]*$/) {
233 defined(getpwnam($chgu)) || &quit("username $chgu does not exist");
234 eval "\@l = \@$clist; 1" || &quit("internal error: $@");
235 $already= grep($_ eq $chgu, @l);
236 if ($action eq 'add') {
238 &p("$chgu already $what");
240 &p("added $what $chgu");
242 $memc+= ($clist eq 'members');
244 } elsif ($action eq 'remove') {
246 &p("removed $what $chgu");
247 @l= grep($_ ne $chgu, @l);
248 $memc+= ($clist eq 'members');
250 &p("$chgu is already not $what");
253 &usage("username found but no action to take for them");
255 eval "\@$clist = \@l; 1" || &quit("internal error: $@");
257 &usage("bad username or option $_");
260 &p("nb: a change to group membership only takes effect at the user's next login")
262 $groupfile[$groupfileix]=
263 "$groupname:$password:$gid:".join(',',@members)."\n";
264 $grouplist[$grouplistix]=
265 "$groupname:$description:$owner:".join(',',@managers).":$homedir\n";
266 &save($opt{'group-file'},@groupfile);
267 &save($opt{'grouplist-file'},@grouplist);
268 if ($opt{'finish-command'}) {
269 !system($opt{'finish-command'}) || &quit("finish-command: $?");
271 unlink($opt{'gtmp-file'}) || &quit("unlock group (remove gtmp): $!");
276 open(GF,"< $opt{'group-file'}") || &quit("read group: $!");
277 @groupfile=<GF>; close(GF);
279 for ($i=0; $i<=$#groupfile; $i++) {
280 $_= $groupfile[$i]; s/\n$//;
282 m/^(\w[-0-9A-Za-z]*):([^:]*):(\d+):([-0-9A-Za-z,]*)$/ ||
283 &quit("bad entry in group: $_");
285 next unless $1 eq $groupname;
286 $groupfileix<0 || &quit("duplicate entries in group");
290 @members= split(/,/,$4);
292 open(GL,"< $opt{'grouplist-file'}") || &quit("read grouplist: $!");
293 @grouplist=<GL>; close(GL);
295 for ($i=0; $i<=$#grouplist; $i++) {
296 $_= $grouplist[$i]; s/\n$//;
298 m/^(\w[-0-9A-Za-z]*):([^:]*):(\w[-0-9A-Za-z]*):([-0-9A-Za-z,]*):([^:]*)$/ ||
299 &quit("bad entry in grouplist: $_");
300 $bythisowner++ if ($create && $3 eq $createby &&
301 $gname2gid{$1} >= $opt{'user-create-min'} &&
302 $gname2gid{$1} <= $opt{'user-create-max'});
303 next unless $1 eq $groupname;
304 $grouplistix<0 || &quit("duplicate entries in grouplist");
309 @managers= split(/,/,$4);
314 $grouplistix>=0 || &quit("no entry in grouplist for $groupname");
315 $groupfileix>=0 || &quit("no entry in group for $groupname");
319 return 0 if $_[0] eq '';
320 @pw= getpwnam($_[0]);
321 return @pw && $pw[2] == $callinguser ? 1 : 0;
325 $filename= shift(@_);
326 unlink("$filename~");
327 open(DUMP,"> $filename.new") || &quit("create new $filename: $!");
328 print(DUMP @_) || &quit("write new $filename: $!");
329 close(DUMP) || &quit("close new $filename: $!");
330 link("$filename","$filename~") || &quit("create backup $filename: $!");
331 rename("$filename.new","$filename") || &quit("install new $filename: $!");
337 die "groupmanage: @_\n";
341 link($opt{'group-file'},$opt{'gtmp-file'}) || &quit("create gtmp: $!");
346 return unless $locked;
348 unlink($opt{'gtmp-file'}) || warn("unlock group file (remove gtmp): $!\n");
352 print(<<END) || &quit("write to stdout: $!\n");
355 description $description
364 print(STDOUT "$stdout_string") || &quit("write to stdout: $!\n");
369 $stdout_string.= $_[0]."\n";