Examining PerLDAP
by Troy Neeriemer
 


Listing One
unless ($cgi->param()) {
    html_display_login();
} elsif ($cgi->param("exit") eq "Exit") {
    html_display_login();
} elsif ($cgi->param("mode") eq "login") {
    process_login();
} elsif ($cgi->param("mode") eq "submit") {
    process_submit();
}

Listing Two
sub ldap_get_user_info {
    my $user = $_[0]; # The user ID
    my $bind_dn;
    my $cn;
    my $anon_conn = new Mozilla::LDAP::Conn($ldap_host, 
            $ldap_port, "", "", "") || die "Can't connect to $ldap_host.\n";
    my $entry = $anon_conn->search($search_base, "sub", "(uid=$user)");

    if (! $entry) {
        return;
    } else {
        my $i = 0;
        while($entry) {
            $i++;
           $bind_dn = $entry->getDN();
            if ($entry->exists("cn")) {
                $cn = $entry->{"cn"}[0];
            } else {
                return;
            }
            $entry = $anon_conn->nextEntry();
        }
        if ($i > 1) {
            return;
        }
    }   
    $anon_conn->close();
    return ($bind_dn, $cn);
}


Listing Three
sub ldap_get_mail_groups {
    my $bind_dn = $_[0];  # The user's bind DN
    my $password = $_[1];  # The user's password
    my $mgref = $_[2];  # A reference to an array of Entry objects
    my $conn = new Mozilla::LDAP::Conn($ldap_host, $ldap_port, $bind_dn, 
                                     $password, "");
    my $entry;
    my $cn;
    if (! $conn) {
        html_display_error( "Couldn't connect to the directory server.");
        exit(0);
    } else {
        $entry = $conn->search($search_base, "sub", "objectclass=mailGroup");
        if (! $entry) {
            html_display_error("There aren't any mailing lists on this server.");
            exit(0);
        }
        while($entry) {
            $cn = $entry->{"cn"}[0];
            # add the entry to the array unless it's the Postmaster group
            if ($cn ne "Postmaster") {
                push(@$mgref,$entry);
            }
            $entry = $conn->nextEntry();
        }
        $conn->close();
    }
}


Listing Four
foreach (@mailGroup){
    $groupName = $_->{"cn"}[0];
    $description = $_->{"description"}[0];
    my $isMember = 0;
    my $ct = $_->size("uniquemember");
    for ($i = 0; $i < $ct ; $i++) {
        if ($bind_dn eq $_->{"uniquemember"}[$i]) {
            $isMember = 1;
            last;
        }
    }
    html_row($isMember, $groupName, $ct, $description);
}


Listing Five
sub ldap_add_to_group {
    my $bind_dn = $_[0];  # The user's bind DN
    my $password = $_[1]; # The user's password
    my $mg_cn = $_[2]; # The CN of the Group
    my $conn = new Mozilla::LDAP::Conn($ldap_host, $ldap_port, $bind_dn,              
                                      $password, "");
if (!$conn) {
        html_display_error( "Couldn't connect to the directory server.");
        exit(0);
    } else {
        my $entry = $conn->search($search_base, "sub", "(cn=$mg_cn)");
        if (!$entry) {
            html_display_error( "Couldn't find $mg_cn.");
            exit(0);
        }
        $entry->addValue("uniquemember", $bind_dn);
        $conn->update($entry);
        $conn->close();

    }
}


2


