#!/usr/bin/perl
#
-# Copyright (C)1995-9 Ian Jackson <ijackson@chiark.greenend.org.uk>
-# Copyright (C) 1999, 2003
-# Chancellor Masters and Scholars of the University of Cambridge
-#
-# Hacked by Ben Harris <bjh21@cam.ac.uk> in 1999 and 2003 for Unix
-# Support's own nefarious purposes.
+# Copyright 1996-2013 Ian Jackson <ijackson@chiark.greenend.org.uk>
+# Copyright 1998 David Damerell <damerell@chiark.greenend.org.uk>
+# Copyright 1999,2003
+# Chancellor Masters and Scholars of the University of Cambridge
+# Copyright 2010 Tony Finch <fanf@dotat.at>
#
# This is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by
-# the Free Software Foundation; either version 2, or (at your option)
-# any later version.
+# the Free Software Foundation; either version 3 of the License, or
+# (at your option) any later version.
#
-# It 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 General Public License for more details.
+# 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
+# General Public License for more details.
#
-# $Id$
+# You should have received a copy of the GNU General Public License
+# along with userv-utils; if not, see http://www.gnu.org/licenses/.
sub usage {
&unlock;
@ARGV || push(@ARGV,'--info');
$callinguser= exists $ENV{'USERV_UID'} ? $ENV{'USERV_UID'} : $<;
-$callingname = exists $ENV{'USERV_USER'} ? $ENV{'USERV_USER'} : getpwuid($<);
%opt= ('user-create','0',
'user-create-minunameu','5',
'gtmp-file','gtmp',
'grouplist-file','grouplist',
'name-regexp','',
+ 'name-maxlen','8',
'admin-group','',
'finish-command','');
%ovalid= ('user-create','boolean',
'gtmp-file','string',
'grouplist-file','string',
'name-regexp','string',
+ 'name-maxlen','number',
'admin-group','string',
'finish-command','string');
if ($ARGV[0] eq '--create') {
$opt{'user-create'} || !$callinguser ||
- ($opt{'admin-group'} &&
- (getgrnam($opt{'admin-group'}))[3] =~ /(^| )$callingname( |$)/) ||
&quit("group creation by users disabled by administrator");
- length($groupname) <= 8 || &quit("group names must be 8 chars or fewer");
+ length($groupname) <= $opt{'name-maxlen'} ||
+ &quit("group names must be $opt{'name-maxlen'} chars or fewer");
$!=0; (@pw= getpwuid($callinguser))
|| &quit("cannot get your passwd entry: $!");
$createby= $pw[0];
}
&weare($owner) || grep(&weare($_),@managers) || !$callinguser ||
- ($opt{'admin-group'} &&
- (getgrnam($opt{'admin-group'}))[3] =~ /(^| )$callingname( |$)/) ||
&quit("you may not manage $groupname");
$action= 'none';
} elsif (m/^\w[-0-9A-Za-z]*$/) {
y/\n//d;
$chgu=$_;
- getpwnam($chgu) || &quit("username $chgu does not exist");
+ defined(getpwnam($chgu)) || &quit("username $chgu does not exist");
eval "\@l = \@$clist; 1" || &quit("internal error: $@");
$already= grep($_ eq $chgu, @l);
if ($action eq 'add') {
&save($opt{'group-file'},@groupfile);
&save($opt{'grouplist-file'},@grouplist);
if ($opt{'finish-command'}) {
- !system($opt{'finish-command'}) || &quit("finish-command: $!");
+ !system($opt{'finish-command'}) || &quit("finish-command: $?");
}
unlink($opt{'gtmp-file'}) || &quit("unlock group (remove gtmp): $!");
&p_out;
}
sub lock {
- # NFS-safe Locking per Linux open(2)
- my($hostname) = `hostname`;
- chomp($hostname);
- my($hitching_post) = "$opt{'gtmp-file'}.$hostname.$$";
- open(LOCK, ">$hitching_post") || die "$hitching_post: $!";
- close(LOCK);
- link($hitching_post, $opt{'gtmp-file'});
- if ((stat($hitching_post))[3] != 2) {
- close(OUT);
- unlink($hitching_post);
- &quit("group file locked -- giving up...");
- }
- unlink($hitching_post);
-# link($opt{'group-file'},$opt{'gtmp-file'}) || &quit("create gtmp: $!");
+ link($opt{'group-file'},$opt{'gtmp-file'}) || &quit("create gtmp: $!");
$locked++;
}