Subversion Repositories DevTools

Rev

Rev 1572 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1572 gchristi 1
 
2
use Getopt::Long;
3
 
1574 gchristi 4
use constant    OK                  => 0;    # User or group exists and are valid
5
use constant    NOUSER              => 1;    # user $opt_user does not exist
6
use constant    NOGROUP             => 2;    # group $opt_group does not exist
7
use constant    PRIMARYGROUP        => 3;    # Primary group of user $opt_user is not $opt_group
8
use constant    SECONDARYGROUP      => 4;    # user $opt_user is not a member of one of the elements in $opt_Groups
9
use constant    HOMEDIR             => 5;    # user $opt_user home dir is not $opt_home
10
use constant    INVALIDPARAMETERS   => 6;    # GetOpt Errors
11
use constant    MISSINGPARAMETERS   => 7;    # user or group options missing
12
 
1572 gchristi 13
our ( $opt_user, $opt_group, @opt_Groups, $opt_home );
14
my $retval;
15
 
16
Getopt::Long::Configure("no_ignore_case");
17
 
18
if ( ! GetOptions( "user=s"    => \$opt_user, 
19
                   "group=s"   => \$opt_group,
20
                   "Groups=s"  => \@opt_Groups,
21
                   "home=s"    => \$opt_home ) )
22
{
23
    print "Error processing Options\n";
24
    $retval = INVALIDPARAMETERS;
25
}
26
elsif ( ! defined($opt_user) && ! defined($opt_group) )
27
{
28
    print "Missing user and/or group arguments\n";
29
    $retval = MISSINGPARAMETERS;
30
}
31
# else if no user but we have a group then the group becomes the check so check to see if group exists
32
elsif ( ! defined($opt_user) && defined($opt_group) )
33
{
34
    print "Checking group $opt_group ... ";
35
    $retval = ( getgrnam($opt_group) ) ? OK : NOGROUP;
36
    print (($retval == OK) ? "OK\n" : "ERROR: Does not exist\n");
37
}
38
#else user is defined with or with out group, does not matter
39
else
40
{
41
    print "Checking user $opt_user ... ";
42
 
43
    my @usrdet = getpwnam($opt_user);
44
 
45
    if ( $#usrdet > -1 )
46
    {
47
        $retval = OK;
48
 
49
        if ( defined($opt_group) && getgrgid($usrdet[3]) ne $opt_group )
50
        {
51
            print "ERROR: Primary Group not $opt_group\n";
52
            $retval = PRIMARYGROUP;
53
        }
54
 
55
        my @secgrps = map { split /,/ } @opt_Groups;
56
        for ( my $i = 0; $retval == OK && $i <= $#secgrps; $i++ )
57
        {
58
            if ( !grep { $_ eq $opt_user } split(' ', (getgrnam($secgrps[$i]))[3] ) )
59
            {
60
                print "ERROR: Not in Secondary group $secgrps[$i]\n";
61
                $retval = SECONDARYGROUP;
62
            }
63
        }
64
 
65
        if ( $retval == OK && defined($opt_home) && $usrdet[7] ne $opt_home )
66
        {
67
            print "ERROR: Home Dir not $opt_home\n";
68
            $retval = HOMEDIR;
69
        }
70
 
71
        print "OK\n" if ( $retval == OK );
72
    }
73
    else
74
    {
75
        print "ERROR: Does not exist\n";
76
        $retval =  NOUSER;
77
    }
78
}
79
 
80
exit($retval);