#!/usr/bin/perl
# Author: Brad Marshall (bmarshal@pisoftware.com)
# Date: 20010803

# This script is released under the GNU GPL.  See http://www.gnu.org/copyleft/gpl.html
# for full licensing details.

# TODO:
# Check if user actually exists
# Better error reporting - ldap server unavailable etc

use CGI;
use CGI::Carp qw(fatalsToBrowser carpout);
use Net::LDAP; 

my($debug) = 0;
my($basedn) = "dc=gumby";
my($ldaphost) = "eta";

my($query) = new CGI();

print $query->header(-type=>"text/html"),
	$query->start_html(-title=>'LDAP List Users', -BGCOLOR=>"white"), "\n";

print $query->hr,"\n"; 

# Username, home directory, passwd, first name, last name, primary group, login shell, 

#dn: uid=bmarshal,ou=People,dc=pisoftware,dc=com
#uid: bmarshal
#cn: Brad Marshall
#objectclass: account
#objectclass: posixAccount
#objectclass: top
#loginshell: /bin/bash
#uidnumber: 500
#gidnumber: 120
#homedirectory: /mnt/home/bmarshal
#gecos: Brad Marshall,,,,
#mail: bmarshal@pisoftware.com
#httppassword: {crypt}soYX7fAuuVIX6
#modifytimestamp: 20010727070251Z
#modifiersname: cn=Manager, dc=pisoftware, dc=com
#userpassword:: e2NyeXB0fTEwenhJUFpPMnhmNE0=

my($ldap) = Net::LDAP->new($ldaphost) or die "Can't bind to $ldaphost: $!\n";

$bind = $ldap->bind;

if ($bind->code) {
    print "Can't bind to $ldaphost: " . $mesg->error;
} else {
    $mesg = $ldap->search( base => $basedn,
                           filter => '(objectclass=posixAccount)');
    if ($mesg->code) {
        print "Error doing search: $mesg->error";
    } else {
        print "<TABLE>\n";
        $done_table = 0;
        foreach my $entry ($mesg->all_entries) {
            if (! $done_table) {
                print "     <TR>\n";
                # Print out the headings
                foreach my $attr ($entry->attributes) {
                    if ($attr ne "objectClass") {
                        print "<TD>$attr</TD>\n";
                    }
                }
                print "     </TR>\n";
                $done_table = 1;
            }

            # Print out the values
            print "<TR>\n";
            foreach my $attr ($entry->attributes) {
                if ($attr ne "objectClass") {
                    print "<TD>" . $entry->get_value($attr) . "</TD>\n";
                }
            }
            print "</TR>\n";
        }
        print "</TABLE>\n";
    }
}

$query->hr,
$query->end_html;

