summaryrefslogtreecommitdiffstats
path: root/ldapcat-1.2.pl
blob: 7740eabe0dfc02799240bdf8ccb62ed07f729c64 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
#!/usr/bin/perl -w
##############################################################################
# $Id: ldapcat,v 1.2 2002/02/22 00:41:31 jheiss Exp $
##############################################################################
# Search LDAP for users or groups and print them out in standard UNIX
# format.
#
# TODO:
# - Allow user to specify options to ldapsearch, like -x
##############################################################################
# $Log: ldapcat,v $
# Revision 1.2  2002/02/22 00:41:31  jheiss
# Made Base64 decoding optional.
# Don't print out full entry for KERBEROS type passwords, just a marker.
#
# Revision 1.1  2002/02/09 03:24:50  jheiss
# Initial revision
#
##############################################################################

# Includes and such
use strict;

# Constants
# Set the following to true to have any Base64 encoded fields decoded.
# Requires that you have the MIME::Base64 module from CPAN.
my $DECODE_BASE64 = 1;    # 1 for yes, 0 for no

sub usage
{
    die "Usage: $0 {passwd|group}\n";
}

if (scalar @ARGV == 0)
{
    usage();
}

my %entries;
my $entry;
my @elements;

if ($ARGV[0] eq 'passwd')
{
    open(LS,
             "ldapsearch -LLL '(objectClass=posixAccount)' uid userPassword "
           . "uidNumber gidNumber cn homeDirectory loginShell |"
        )
      || die "Failed to run ldapsearch";

    #open(LS, "ldapsearch -x -LLL '(objectClass=posixAccount)' uid " .
    #"userPassword uidNumber gidNumber cn homeDirectory loginShell |") ||
    #die "Failed to run ldapsearch";
} elsif ($ARGV[0] eq 'group')
{
    open(LS,
             "ldapsearch -LLL '(objectClass=posixGroup)' cn userPassword "
           . "gidNumber memberUid |"
        )
      || die "Failed to run ldapsearch";

    #open(LS, "ldapsearch -x -LLL '(objectClass=posixGroup)' cn userPassword " .
    #"gidNumber memberUid |") ||
    #die "Failed to run ldapsearch";
} else
{
    usage();
}

while (<LS>)
{
    #print "line:  '$_'\n";

    if (/^dn: /)
    {
        if ($ARGV[0] eq 'passwd')
        {
            /dn: uid=(\w+),/;
            $entry = $1;
        } elsif ($ARGV[0] eq 'group')
        {
            /dn: cn=(\w+),/;
            $entry = $1;
        }

        if (!$entry)
        {
            $entry = 'bogus';
        }
    } else
    {
        chomp;

        /^(\w+):/;
        if ($1)
        {
            my $field = $1;

            #print "Field line:  $_\n";
            #print "Field:  $field\n";

            # Take off the field label
            $_ =~ s/^[[:alpha:]]+://;

            #print "Stripped:  '$_'\n";

            # Check to see if the entry was encoded
            if (/^: /)
            {
                #print "Encoded field $field\n";
                $_ =~ s/^: //;
                if ($DECODE_BASE64)
                {
                    require MIME::Base64;
                    $_ = MIME::Base64::decode_base64($_);
                }
            } else
            {
                $_ =~ s/^ //;
            }

            # The userPassword field looks like:  {type}password
            if ($field eq 'userPassword')
            {
                /^{(\w+)}(.*)/;
                my $passtype = $1;
                my $password = $2;

                #print "passtype:  $1\n";
                #print "password:  $2\n";

                if ($passtype eq 'crypt')
                {
                    $_ = $2;
                } elsif ($passtype eq 'KERBEROS')
                {
                    $_ = '*K*';
                } else
                {
                    # Let other types pass through since I don't know what
                    # else is possible
                }
            }

            # There can be multiple members in a group
            if ($field eq 'memberUid')
            {
                push(@{$entries{$entry}->{$field}}, $_);
            } else
            {
                $entries{$entry}->{$field} = $_;
            }
        }
    }
}
close(LS);

if ($ARGV[0] eq 'passwd')
{
    foreach my $user (sort keys %entries)
    {
        print "$user:";
        print $entries{$user}->{'userPassword'} . ":";

        #print "x:";  # Fake password
        print $entries{$user}->{'uidNumber'} . ":";
        print $entries{$user}->{'gidNumber'} . ":";
        print $entries{$user}->{'cn'} . ":";
        print $entries{$user}->{'homeDirectory'} . ":";
        print $entries{$user}->{'loginShell'} . "\n";
    }
} elsif ($ARGV[0] eq 'group')
{
    foreach my $group (sort keys %entries)
    {
        print "$group:";
        print $entries{$group}->{'userPassword'} . ":";

        #print "*:";  # Fake password
        print $entries{$group}->{'gidNumber'} . ":";
        print join(',', @{$entries{$group}->{'memberUid'}}) . "\n";
    }
}