#!/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:

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

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

my($adminuser) = "cn=admin,ou=People," . $basedn;
my($adminpasswd) = "admin";

my($query) = new CGI();

if ($query->param('user')) {
    $user = $query->param('user');

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

    $userdn = "uid=$user,ou=People,$basedn";

    print "User is $userdn" if $debug;
    print $query->br if $debug;

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

    $mesg = $ldap->bind(
                dn       => $adminuser,
                password => $adminpasswd,
    );

	if ($mesg->code) {
		print "Problem binding to ldapserver: " . $mesg->error;
	} else {
		$ldap->delete( $userdn );
		print "Deleted user $user.\n";
	}
} else {
    print $query->header(-type=>"text/html"),
        $query->start_html(-title=>'LDAP Change Password', -BGCOLOR=>"white"), "\n";

    print $query->hr, "\n",
        "<FORM METHOD='post' ACTION='/cgi-bin/ldap/deluser.pl' ENCTYPE='application/x-www-form-urlencoded'>", "\n";

    print "User: ", $query->textfield(-name=>'user', -default=>'username', -size=>'10');

    print $query->br,
        $query->submit,
        $query->end_form,
        $query->hr,
        $query->end_html;

}
