#!/usr/bin/perl5
#
# $Id: cat2.pl,v 1.4 1998/05/21 20:41:25 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.
#
##############################################################################
#
# cat2.pl
#
# This script generates a list of people terminated in LDAP/UNIX
# who do not have corresponding PeopleSoft records.
#
# AUTHOR:  Luke Sheneman
# HISTORY: 
# 
#   12/09/97 - First implementation
#
# (C)opyright 1997, Sheneman Script Factory, Inc
#

# modules that we need
use Ldapp;                      # LDAP module for Perl5, very cool!
use LdapConf;                   # Some generic LDAP configuration 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
$cat2_file       = "$PATH/cat2.txt";


# 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;
}


# Sort function, to sort the entries by UID
sub byCN
{
  ($a->{cn}[0] cmp $b->{cn}[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 cat2_list {

    @results = `grep "No entry in PeopleSoft" $LEIF_FILE | cut -f4 -d" "`;

    # 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, ">$cat2_file");

    foreach $result (@results) {
	chop $result;
	chop $result;

	#print("Looking for $result in LDAP\n");

	# generate a list of people with the correct attributes
	$entry = $ld{conn}->search($LdapConf::PEOPLE_ROOT, "onelevel", "uid=$result", 0, @ATTRIBUTES);
	if($entry)
	{
	    $uid           = @{$entry->{'uid'}}[0];
	    $manager       = @{$entry->{'manager'}}[0];
	    $employeetype  = @{$entry->{'employeetype'}}[0];
	    $employeenumber= @{$entry->{'employeenumber'}}[0];
	    $cn            = @{$entry->{'cn'}}[0];
	    $exp           = @{$entry->{'nscppersonexpdate'}}[0];
	    $mailhost      = @{$entry->{'mailhost'}}[0];
	    $userpassword  = @{$entry->{'userpassword'}}[0];
	    @objectclass   = @{$entry->{'objectclass'}};

	    # see if we are a person or not (obsolete now, really)
	    $ispseudo = &is_pseudo();
		
	    if( ($userpassword =~ "TERMINATED") && !$ispseudo) {
		print(OUT "USER:         $uid\n");
		print(OUT "  FULL NAME:  $cn\n");
		print(OUT "  EMP TYPE:   $employeetype\n");
		print(OUT "  EMP NUMBER: $employeenumber\n");
		print(OUT "  MANAGER:    $manager\n");
		print(OUT "  MAILHOST:   $mailhost\n");
		print(OUT "  PASSWORD:   $userpassword\n");
		print(OUT "  EXPIRATION: $exp\n");
		print(OUT "\n\n");
	    }
	}
    }

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

    close(OUT);
	
    return 0;
}


# setup LDAP connection information
&setup_ldap;


# build a lost of people who have been terminated in LDAP but do not have
#  PeopleSoft entries, so they are still in the phonebook
&cat2_list;


exit 0;















