#!/usr/bin/perl5
#
# $Id: cat7.pl,v 1.3 1998/05/21 20:43:20 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.
#
##############################################################################
#
# cat7.pl
#
# This script attempts to generate a list of people who have an active
#   LDAP account and a PeopleSoft account, but are missing information such
#   as manager, phone number, Admins, Organizational units, and business 
#   categories.
#
# AUTHOR:  Luke Sheneman
# HISTORY: 
# 
#   12/09/97 - First implementation
#   03/11/98 - Major performance enhancements
#
# (C)opyright 1997, Sheneman Script Factory, Inc
#

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

$| = 1; 			# flush all buffers immeiately

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

@ATTRIBUTES = ("uid", 
	       "manager", 
	       "employeetype", 
	       "employeenumber", 
	       "cn", 
	       "nscppersonexpdate", 
	       "userpassword", 
	       "secretary", 
	       "manager", 
	       "ou", 
	       "businesscategory",
	       "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
$nscp_file      = "$PATH/nscp_cat7.txt";
$ic_file        = "$PATH/ic_cat7.txt";
$vendors_file   = "$PATH/vendors_cat7.txt";
$interim_file   = "$PATH/interim_cat7.txt";
$oem_file       = "$PATH/oem_cat7.txt";
$unknown_file   = "$PATH/unknown_cat7.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;
}


# do a linear search through the exceptions
sub check_in_peoplesoft {

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

    $found = 1;   # start by assuming they are in PeopleSoft
    foreach $i (@results) {
	chop $i;  chop $i;

	# print("Comparing [$i] and [$uid]\n");
	if($i eq $uid) {
	    $found = 0;  # if we find them in Leif's output, they are not in PeopleSoft
	    last;
	}
    }

    return $found;
}


# determine if the user's manager exists in LDAP
sub check_manager {

    # strip out the manager UID
    ($man, $fluff) = split(",", $manager);
    ($fluff, $man) = split("=", $man);

    return(!($uids{$man}));
}


sub check_secretary {

    #print("In check_secretary: $secretary\n");

    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 cat7_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(NSCP_OUT,    ">$nscp_file");
    open(IC_OUT,      ">$ic_file");
    open(VENDORS_OUT, ">$vendors_file");
    open(INTERIM_OUT, ">$interim_file");
    open(OEM_OUT,     ">$oem_file");
    open(UNKNOWN_OUT, ">$unknown_file");

    # prime the files here
    print(NSCP_OUT "\n");
    print(IC_OUT "\n");
    print(VENDORS_OUT "\n");
    print(INTERIM_OUT "\n");
    print(OEM_OUT "\n");
    print(UNKNOWN_OUT "\n");

    $entry = $ld{conn}->search($LdapConf::PEOPLE_ROOT, "onelevel", "uid=*", 0, @ATTRIBUTES);
    while($entry)
    {
	push(@entries, $entry);
	$uids{(@{$entry->{'uid'}}[0])}++;
	$entry = $ld{conn}->entry;
    }

    # generate a list of all people with the correct attributes
    foreach $entry (@entries) {
	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];
	    $manager          = @{$entry->{'manager'}}[0];
	    $ou               = @{$entry->{'ou'}}[0];
	    $businesscategory = @{$entry->{'businesscategory'}}[0];
	    $secretary        = @{$entry->{'secretary'}}[0];
	    @objectclass      = @{$entry->{'objectclass'}};

	    # see if we are hidden
	    $ishidden = &is_hidden();
	    $ispseudo = &is_pseudo();
	    
	    # determine how the next lines will be outputed
	    if   ($employeetype eq "Employee")   { $OUT = NSCP_OUT; }
	    elsif($employeetype eq "Vendor")     { $OUT = VENDORS_OUT; }
	    elsif($employeetype eq "Interim")    { $OUT = INTERIM_OUT; }
	    elsif($employeetype eq "OEM Partner"){ $OUT = OEM_OUT; }
	    elsif($employeetype eq "Contractor") { $OUT = IC_OUT; }
	    else { $OUT = UNKNOWN_OUT; }

	    # initialize some flags
	    $manager_flag = $secretary_flag = 0;

	    # check for manager problems
	    if($manager) {
		$manager_flag = &check_manager;
	    }
	    
	    # FIX THIS LATER TO CHECK SECRETARIES
	    # check for secretary problems
	    #if($secretary) {
	    #    $secretary_flag = &check_secretary;
	    #}
	    $secretary = 1;

	    # only print the record if there is a problem
	    if( (!$ou               || 
	         !$businesscategory || 
	         !$manager          || 
	         !$secretary        ||
	         $manager_flag      ||
	         $secretary_flag)   &&  !$ishidden && !$ispseudo) {

		# print header to this group
		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 "  PROBLEM(S):\n");

		# print the exceptions here
		if(!$ou)                   { print($OUT "     Missing Organizational Unit\n"); }
		if(!$businesscategory)     { print($OUT "     Missing Business Category\n"); }
		if(!$manager)              { print($OUT "     Missing Manager\n"); }
		if($manager_flag)          { print($OUT "     Incorrect Manager: $manager\n"); }
		if(!$secretary)            { print($OUT "     Missing Secretary\n"); }
		if($secretary_flag)        { print($OUT "     Incorrect Secretary: $secretary\n"); }

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

    #print("Done.\n");

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

    close(NSCP_OUT);
    close(IC_OUT);
    close(VENDORS_OUT);
    close(INTERIM_OUT);
    close(OEM_OUT);
    close(UNKNOWN_OUT);

    return 0;
}


# setup LDAP connection information
&setup_ldap;

# look for workers with missing or incorrect information in LDAP
&cat7_list;

exit 0;




















