#!/usr/bin/perl5
#
# $Id: cat1.pl,v 1.4 1998/05/21 20:40:52 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.
#
##############################################################################
#
# nscp_exp_exceptions.pl
#
# This script generates a list of Netscape Employees who 
#   are already expired in PeopleSoft, but have active 
#   LDAP/MAIL accounts.
#
# 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;                   # 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", "mailhost", "userpassword", "objectclass");

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

# the names of the current files
$nscp_expired_file      = "$PATH/nscp_expired.txt";
$ic_expired_file        = "$PATH/ic_expired.txt";
$vendors_expired_file   = "$PATH/vendors_expired.txt";
$interim_expired_file   = "$PATH/interim_expired.txt";
$oem_expired_file       = "$PATH/oem_expired.txt";
$unknown_expired_file   = "$PATH/unknown_expired.txt";

$nscp_two_file          = "$PATH/nscp_two.txt";
$ic_two_file            = "$PATH/ic_two.txt";
$vendors_two_file       = "$PATH/vendors_two.txt";
$interim_two_file       = "$PATH/interim_two.txt";
$oem_two_file           = "$PATH/oem_two.txt";
$unknown_two_file       = "$PATH/unknown_two.txt";


# get the auth info here
sub get_auth_info {

    $admin_username = "sheneman";
    $admin_password = "nimos.";

    # return success
    return 0;
}


# 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{bind} = "uid=$admin_username,$LdapConf::PEOPLE_ROOT";
    $ld{pswd} = $admin_password;
    $ld{cert} = "$LDAP_CERT_PATH";

    # return success
    return 0;
}



# this was used in the original terminate script
sub gmt {

    ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = gmtime(time);

    if (length($mday) < 2) {$mday = "0" . $mday};

    $mon = $mon + 1;
    if (length($mon)  < 2) {$mon = "0" . $mon};

    # year-2000 safe!
    $year = 1900 + $year;

    $timestring = $year . $mon . $mday;

    return $timestring;
}


# this was used in the original terminate script
sub gmt2 {

    ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = gmtime(time+3600*24*14);

    if (length($mday) < 2) {$mday = "0" . $mday};

    $mon = $mon + 1;
    if (length($mon)  < 2) {$mon = "0" . $mon};

    # dolphin safe!
    $year = 1900 + $year;

    $timestring = $year . $mon . $mday;

    return $timestring;
}


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


# print out everybody who has expired in PeopleSoft but has a mailbox
sub expired_list {

    # get the current time
    $current_date  = &gmt;
    $two_week_date = &gmt2;

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

    # generate a list of people with the correct attributes
    $entry = $ld{conn}->search($LdapConf::PEOPLE_ROOT, "onelevel", "uid=*", 0, @ATTRIBUTES);
    while($entry)
    {
	push(@entries, $entry);
	$entry = $ld{conn}->entry;
    }

    # open the current report files here
    open(NSCP,    ">$nscp_expired_file");
    open(IC,      ">$ic_expired_file");
    open(VENDORS, ">$vendors_expired_file");
    open(INTERIM, ">$interim_expired_file");
    open(OEM,     ">$oem_expired_file");
    open(UNKNOWN, ">$unknown_expired_file");

    open(NSCP2,    ">$nscp_two_file");
    open(IC2,      ">$ic_two_file");
    open(VENDORS2, ">$vendors_two_file");
    open(INTERIM2, ">$interim_two_file");
    open(OEM2,     ">$oem_two_file");
    open(UNKNOWN2, ">$unknown_two_file");


    foreach $entry (sort byCN @entries) {
	
	# get the necessary fields here
	$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 (this should be obsoleted, really)
	$ispseudo = &is_pseudo();

	# if they have expired...
	if( $exp && ($exp < $current_date) ) {
	    
	    # we don't need to show users who have expired if they are
	    #   already terminated or if they are pseudo accounts
	    if( (!($userpassword =~ "TERMINATED")) && !$ispseudo) {

		# determine how the next lines will be outputed
		if   ($employeetype eq "Employee")   { $OUT = NSCP; }
		elsif($employeetype eq "Vendor")     { $OUT = VENDORS; }
		elsif($employeetype eq "Interim")    { $OUT = INTERIM; }
		elsif($employeetype eq "OEM Partner"){ $OUT = OEM; }
		elsif($employeetype eq "Contractor") { $OUT = IC; }
		else { $OUT = UNKNOWN; };

		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");
	    }

	} elsif(($exp > $current_date) && ($exp < $two_week_date)) {  # user will expire in the next two weeks

	    # we don't need to show users who will
	    #   expire soon but are already terminated in LDAP
	    #   or are pseudo accounts 
	    if( (!($userpassword =~ "TERMINATED")) && !$ispseudo) {

		# determine how the next lines will be outputed
		if($employeetype eq "Employee") { $OUT = NSCP2; }
		elsif($employeetype eq "Vendor") { $OUT = VENDORS2; }
		elsif($employeetype eq "Interim") { $OUT = INTERIM2; }
		elsif($employeetype eq "OEM") { $OUT = OEM2; }
		elsif($employeetype eq "Contractor") { $OUT = IC2; }
		else{ $OUT=UNKNOWN2; }

		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 current report files here
    close(NSCP);
    close(IC);
    close(VENDORS);
    close(INTERIM);
    close(OEM);
    close(UNKNOWN);

    close(NSCP2);
    close(IC2);
    close(VENDORS2);
    close(OEM2);
    close(INTERIM2);

    close(UNKNOWN2);

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

    return 0;
}

# setup the bind foo
&get_auth_info;

# setup LDAP connection information
&setup_ldap;

# build a list of people who have already expired, but apparently have valid
#  mail folders
&expired_list;


exit 0;








