#!/usr/bin/perl # # Add user to group and preserve existing groups # # smb_addtogroup username groupname # # Michiel Broek # # This script 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. # # This script 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. # # You should have received a copy of the GNU General Public License # along with MB BBS; see the file COPYING. If not, write to the Free # Software Foundation, 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. # $username = $ARGV[0]; $groupname = $ARGV[1]; $group_exist = 0; $group_already = 0; if ($#ARGV ne 1) { print "Usage: $0 username groupname\n"; exit -1; } # Read existing groups and do some checks # open(GLIST, "/etc/group") || die "open /etc/group failed! $!\n"; while () { chop ($_); split(':', $_); # Check if the group exists if ($_[0] eq $groupname) { $group_exist = 1; } next unless $_[3]; if ( $_[3] =~ /$username(.*)/i ) { push(@groups, $_[0]); if ($_[0] eq $groupname) { $group_already = 1; } } } close (GLIST); if ($group_exist == 0) { print "group '$groupname' doesn't exist\n"; exit -1; } if ($group_already == 1) { print "Already in group '$groupname'\n"; exit -1; } # Build list of groups to set $setgroups = $groupname; foreach (@groups) { $setgroups = $setgroups.",".$_; } system ("/usr/sbin/usermod -G '$setgroups' $username");