#!/usr/bin/perl5
#
# $Id: cat8.pl,v 1.3 1998/05/21 20:43:53 sheneman Exp $
#
##############################################################################
#
# The contents of this file are subject to the Netscape Public License Version
# 1.0 (the "License"); you may not use this file except in compliance with the
# License. You may obtain a copy of the License at http://www.mozilla.org/NPL/ 
#
# Software distributed under the License is distributed on an "AS IS" basis,
# WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
# the specific language governing rights and limitations under the License. 
#
# The Initial Developer of the Original Code is Netscape Communications
# Corporation. Portions created by Netscape are Copyright (C) 1998 Netscape
# Communications Corporation. All Rights Reserved.
#
##############################################################################
#
# cat8.pl
#
# This script attempts to generate a list of duplicates by
#   comparing full names in LDAP.
#
# AUTHOR:  Luke Sheneman
# HISTORY: 
# 
#   12/11/97 - First implementation
#
# (C)opyright 1997, Sheneman Script Factory, Inc
#

# modules that we need
use Ldapp;                      # LDAP module for Perl5, very cool!
use LdapConf;                   # LDAP Config foo
use LdapUtils;                  # Some defaults and utilities we use.

$| = 1; 			# flush all buffers immediately

# set some defaults here
$LDAP_SERVER                  = "hoth.mcom.com";
$LDAP_CERT_PATH               = "/etc/adduser_cert.db";

@ATTRIBUTES = ("uid", 
	       "manager", 
	       "employeetype", 
	       "employeenumber", 
	       "cn", 
	       "nscppersonexpdate", 
	       "userpassword",
	       "objectclass");

# the path to the current files
$LEIF_FILE = "/u/sheneman/projects/cleanup/psoftsync_verbose.txt";
$PATH = "/u/sheneman/projects/cleanup/current";

# the names of the current files
$cat8_file      = "$PATH/cat8.txt.new";


# setup some LDAP parameters for every bind this session
sub setup_ldap {
    # set some defaults for the LDAP bind
    $ld{host} = $LDAP_SERVER;

    # set the cert path
    if($LDAP_CERT_PATH eq "") {
	$ld{port} = $LdapConf::PORT;
    } else {
	$ld{port} = $LdapConf::SSL_PORT;
    }
    $ld{root} = $LdapConf::ROOT;
    $ld{cert} = "$LDAP_CERT_PATH";

    # return success
    return 0;
}

# determine if this LDAP account is a "pseudo-account"
sub is_pseudo { 

    $ispseudo = 0;
    foreach $obj (@objectclass) {
	if( ($obj eq "pseudoAccount") || ($obj eq "pseudoaccount") ) {
	    $ispseudo = 1;
	    last;
	}
    }

    return $ispseudo;
}


sub is_hidden {
    
    $ishidden = 0;
    foreach $obj (@objectclass) {
	if( ($obj eq "nscpHideThis") || ($obj eq "nscphidethis") ) {
	    $ishidden = 1;
	    last;
	}
    }

    return $ishidden;
}



sub cat8_list {

    # establish the LDAP connection here
    $ld{conn} = new Ldapp($ld{host}, $ld{port}, $ld{bind}, $ld{pswd}, $ld{cert});
    if(!$ld{conn}) { 
	print("Error:  Could not connect to LDAP server $LDAP_SERVER\n");
	return -1;
    }

    open(OUT, ">$cat8_file");

    $entry = $ld{conn}->search($LdapConf::PEOPLE_ROOT, "onelevel", "uid=*", 0, @ATTRIBUTES);
    while($entry)
    {
	# build a hash array of LDAP records
	$entries{(@{$entry->{'uid'}}[0])} = $entry;
	$entry = $ld{conn}->entry;
    }

    $counter = 0;

    # generate a list of all people with the correct attributes
    foreach $entry (@entries) {
	if($entry) {

	    $uid              = @{$entry->{'uid'}}[0];
	    $employeetype     = @{$entry->{'employeetype'}}[0];
	    $employeenumber   = @{$entry->{'employeenumber'}}[0];
	    $cn               = @{$entry->{'cn'}}[0];
	    $exp              = @{$entry->{'nscppersonexpdate'}}[0];
	    $userpassword     = @{$entry->{'userpassword'}}[0];
	    @objectclass      = @{$entry->{'objectclass'}};

	    # determine if the user is hidden and person
	    $ishidden1 = &is_hidden();
	    $ispseudo1 = &is_pseudo();

	    # search for the duplicate here
	    $dup = $ld{conn}->search($LdapConf::PEOPLE_ROOT, "onelevel", "(& (!(uid=$uid)) (cn=$cn))", 0, @ATTRIBUTES);
	    if($dup && !$ishidden1 && !$ispseudo1) {

		$uid2              = @{$dup->{'uid'}}[0];
		$employeetype2     = @{$dup->{'employeetype'}}[0];
		$employeenumber2   = @{$dup->{'employeenumber'}}[0];
		$cn2               = @{$dup->{'cn'}}[0];
		$exp2              = @{$dup->{'nscppersonexpdate'}}[0];
		$userpassword2     = @{$dup->{'userpassword'}}[0];
		@objectclass       = @{$dup->{'objectclass'}};

		# determine if the user is hidden and person
		$ishidden2 = &is_hidden();
		$ispseudo2 = &is_pseudo();

		if(!$ishidden2 && !$ispseudo2) {
		
		    print(OUT "DUPLICATE \#$counter:\n");
		    print(OUT "  USER:       [$uid], [$uid2]\n");
		    print(OUT "  CN:         [$cn], [$cn]\n");
		    print(OUT "  EMP TYPE:   [$employeetype], [$employeetype2]\n");
		    print(OUT "  EMP NUMBER: [$employeenumber], [$employeenumber2]\n");
		    print(OUT "  PASSWORD:   [$userpassword], [$userpassword2]\n");
		    print(OUT "  EXPIRATION: [$exp], [$exp2]\n");

		    $counter++;

		    print(OUT "\n\n");
		}
	    }
	}
    }

    # close the LDAP connection here
    if($ld{conn}) {
	$ld{conn}->close;
    }
    
    close(OUT);

    return 0;
}


# setup LDAP connection
&setup_ldap;

&cat8_list;

exit 0;




















