Rev 1572 | Blame | Compare with Previous | Last modification | View Log | RSS feed
use Getopt::Long;use constant OK => 0; # User or group exists and are validuse constant NOUSER => 1; # user $opt_user does not existuse constant NOGROUP => 2; # group $opt_group does not existuse constant PRIMARYGROUP => 3; # Primary group of user $opt_user is not $opt_groupuse constant SECONDARYGROUP => 4; # user $opt_user is not a member of one of the elements in $opt_Groupsuse constant HOMEDIR => 5; # user $opt_user home dir is not $opt_homeuse constant INVALIDPARAMETERS => 6; # GetOpt Errorsuse constant MISSINGPARAMETERS => 7; # user or group options missingour ( $opt_user, $opt_group, @opt_Groups, $opt_home );my $retval;Getopt::Long::Configure("no_ignore_case");if ( ! GetOptions( "user=s" => \$opt_user,"group=s" => \$opt_group,"Groups=s" => \@opt_Groups,"home=s" => \$opt_home ) ){print "Error processing Options\n";$retval = INVALIDPARAMETERS;}elsif ( ! defined($opt_user) && ! defined($opt_group) ){print "Missing user and/or group arguments\n";$retval = MISSINGPARAMETERS;}# else if no user but we have a group then the group becomes the check so check to see if group existselsif ( ! defined($opt_user) && defined($opt_group) ){print "Checking group $opt_group ... ";$retval = ( getgrnam($opt_group) ) ? OK : NOGROUP;print (($retval == OK) ? "OK\n" : "ERROR: Does not exist\n");}#else user is defined with or with out group, does not matterelse{print "Checking user $opt_user ... ";my @usrdet = getpwnam($opt_user);if ( $#usrdet > -1 ){$retval = OK;if ( defined($opt_group) && getgrgid($usrdet[3]) ne $opt_group ){print "ERROR: Primary Group not $opt_group\n";$retval = PRIMARYGROUP;}my @secgrps = map { split /,/ } @opt_Groups;for ( my $i = 0; $retval == OK && $i <= $#secgrps; $i++ ){if ( !grep { $_ eq $opt_user } split(' ', (getgrnam($secgrps[$i]))[3] ) ){print "ERROR: Not in Secondary group $secgrps[$i]\n";$retval = SECONDARYGROUP;}}if ( $retval == OK && defined($opt_home) && $usrdet[7] ne $opt_home ){print "ERROR: Home Dir not $opt_home\n";$retval = HOMEDIR;}print "OK\n" if ( $retval == OK );}else{print "ERROR: Does not exist\n";$retval = NOUSER;}}exit($retval);