[comp.lang.perl] account admin scripts

ramsey@NPIRS.Purdue.EDU (Ed Ramsey) (12/27/90)

has anyone done much with account administration/creation/deletion
scripts in perl?  I would like to find some already done (well :-),
and work from there.  Thanks.

-Ed

Ed Ramsey  NPIRS User Services Manager ramsey@npirs.purdue.edu
                   317/494-6616  FAX 317/494-0535
     "You gave your life to become the person you are right now.
        Was it worth it?"    Richard Bach, *One*

stergios@portia.Stanford.EDU (Stergios) (12/27/90)

here's what I use to add a new (or clobber an old) group to /etc/group.

sm

**********************************************************************

#!/usr/local/bin/perl

$ENV{'PATH'} = '/bin:/usr/bin:/usr/ucb/bin:/usr/etc';
$path = $ENV{'PATH'};
$ENV{'SHELL'} = '/bin/sh' if $ENV{'SHELL'} ne '';
$ENV{'IFS'} = '' if $ENV{'IFS'} ne '' ;

if ($#ARGV < 1) { &usage ; }
$ngname = shift ;
$ngmembers = join(',', @ARGV ) ;
$#ARGV = -1 ;

$file = '/etc/group' ;
$gid = -1 ;
open(GROUP, "<$file") ;

while(<GROUP>) {
($name,$star,$junkgid,$rest) = split(/:/) ;
$groupbygid{$junkgid} = $groupbyname{$name} = $_ ;
}

close (GROUP) ;


;# check if new group name already exists
;# and verify if it should be clobbered
;# if it gets clobbered use the same gid
;# as it had previously.

if ( defined $groupbyname{$ngname} ) {
print "$ngname already exists.\t$groupbyname{$ngname}" ;
print "Clobber anyway? [y/n] : " ;
$gid = (split(':',$groupbyname{$ngname}))[2] ;
$_ = <> ;
chop ;
if(/y/i) {
print "ok, good luck.\n" ;
} else {
print "exiting\n" ;
exit ( 2 ) ;
}
} else {
print "$ngname is a new group.\n" ;
}

;# search for a free gid if necessary.
;# $gid is nonzero now if the requested group
;# name was found to already exist.  If it was found
;# $gid was set to the existing group name's $gid.

if ( $gid == -1 ) {
$gid = 500 ;
 findfreegid:
while ( 1 ) {
if ( ! defined $groupbygid{$gid} ) {
print "$gid is not currently used. accept? [y/n] : " ;
$_ = <> ;
chop ;
if(/y/i) {
print "ok, good luck.\n" ;
last findfreegid ;
}
}
$gid++ ;
}
}

;# form the new group entry

$newgroupentry = join (':', ($ngname, '*', $gid, $ngmembers)) ;
$newgroupentry .= "\n" ;

print "New Group Entry ->$newgroupentry" ;
print "This look ok to enter into /etc/group? [y/n] : " ;
$_ = <> ;
if (! /y/i) {
exit ( 2) ;
}

$groupbygid{$gid} = $newgroupentry ;

$filename = "/tmp/junk.$$" ;
open (OUTPUT, ">$filename") ;
foreach (sort bynumber keys %groupbygid) {
print OUTPUT $groupbygid{$_} ;
}
close OUTPUT ;

;# /tmp and /etc are on the same partition on my system. will this
;# work if this is not the case???

rename("/etc/group", "/tmp/group.bak") ;
rename($filename, "/etc/group") ;

exit (0) ;

;#  SUBROUTINES

sub bynumber {
$a - $b ;
}

sub usage {
print 'Usage: $0 <groupname> <groupmembers>', "\n" ;
exit (2) ;
}