#!/usr/bin/perl -w

#----------------------------------------------------------------------
# copyright (C) 1999, 2000, 2001 e-smith, inc.
#		
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#		
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.	See the
# GNU General Public License for more details.
#		
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
# 
# Technical support for this program is available from e-smith, inc.
# For details, please visit our web site at www.e-smith.com or
# call us on 1 888 ESMITH 1 (US/Canada toll free) or +1 613 564 8000
#----------------------------------------------------------------------

package esmith;

use strict;
use Errno;
use esmith::config;
use esmith::util;
use esmith::db;

#------------------------------------------------------------
# Function getShadowPassword
# retrieves specified users shadow password from shadow file
#------------------------------------------------------------
sub getShadowPassword ($)
{
    my ($username) = @_;
    
    #initialize name and passwd variables
    my $name = ""; 
    my $passwd = "";

    open(SFILE, "/etc/shadow") || die("could not open shadow file");

    while (! eof(SFILE) && $name ne $username) {
      ($name, $passwd) = split (/:/,<SFILE>,3);
    } 
    close(SFILE);
    
    if ($name eq $username) {
    return $passwd;
    }
    else {
      return ""; 
    }
}



my %conf;
tie %conf, 'esmith::config';

my %accounts;
tie %accounts, 'esmith::config', '/home/e-smith/accounts';

my $status = db_get_prop(\%conf, 'ldap', 'status');

#------------------------------------------------------------
# Only process templates if ldap service is enabled 
#------------------------------------------------------------

if (defined $status && $status eq "enabled" )
{
    #--------------------------------------------------
    # Remove LDAP database and import the new files.
    #--------------------------------------------------

    system ("/bin/rm -f /var/lib/ldap/*");
    my $pid = open(LDAP, "|-");
    my $program = "/usr/sbin/slapadd";
    $SIG{ALRM} = sub { die "whoops, $program pipe broke" };
    unless (defined $pid)
    {
	die "Fork failed: $!";
    }
    unless ($pid)
    {
	# Become 'ldap' to run the slapadd program
	$) = getgrnam('ldap');
	$> = getpwnam('ldap');
	# Child process
    	exec($program)
	    || die "Could not exec $program: $!";
	# NOTREACHED
    }

    #------------------------------------------------------------
    # Create LDIF import file and rebuild LDAP database.
    #------------------------------------------------------------

    print LDAP "dn: " .
	esmith::util::ldapBase (db_get (\%conf, 'DomainName')) . "\n";
    print LDAP "objectClass : organization\n";

    my $key;

    foreach $key (keys %accounts)
    {
	if (db_get_type (\%accounts, $key) eq 'user')
	{
	    print LDAP "\n";
	    print LDAP "dn: uid=$key," .
		esmith::util::ldapBase ($conf {'DomainName'}) . "\n";
	    print LDAP "uid: $key\n";
	    print LDAP "objectClass: posixaccount\n";
	    print LDAP "objectClass: posixgroup\n";
            print LDAP "loginShell: /bin/bash\n";
            print LDAP "userpassword: {crypt}" . getShadowPassword($key) . "\n";
            
            print LDAP "homeDirectory: /home/e-smith/" . $key . "/files\n";

	    if (db_get_prop (\%accounts, $key, 'Uid') !~ /^\s*$/)
	    {
		print LDAP "uidNumber: " .
		    db_get_prop (\%accounts, $key, 'Uid') . "\n";
	    }

	    if (db_get_prop (\%accounts, $key, 'Gid') !~ /^\s*$/)
	    {
		print LDAP "gidNumber: " .
		    db_get_prop (\%accounts, $key, 'Gid') . "\n";
	    }

	    my $name = db_get_prop (\%accounts, $key, 'FirstName') . " "
		. db_get_prop (\%accounts, $key, 'LastName');
	    if ($name !~ /^\s*$/)
	    {
		print LDAP "cn: $name\n";
	    }

	    if (db_get_prop (\%accounts, $key, 'FirstName') !~ /^\s*$/)
	    {
		print LDAP "givenName: " .
		    db_get_prop (\%accounts, $key, 'FirstName') . "\n";
	    }

	    if (db_get_prop (\%accounts, $key, 'LastName') !~ /^\s*$/)
	    {
		print LDAP "sn: " .
		    db_get_prop (\%accounts, $key, 'LastName') . "\n";
	    }

	    my $mail = $key . "\@" . db_get (\%conf, 'DomainName');
	    if ($mail !~ /^\s*$/)
	    {
		print LDAP "mail: $mail\n";
	    }

	    if (db_get_prop (\%accounts, $key, 'Phone') !~ /^\s*$/)
	    {
		print LDAP "telephoneNumber: " .
		    db_get_prop (\%accounts, $key, 'Phone') . "\n";
	    }

	    if (db_get_prop (\%accounts, $key, 'Company') !~ /^\s*$/)
	    {
		print LDAP "o: " .
		    db_get_prop (\%accounts, $key, 'Company') . "\n";
	    }

	    if (db_get_prop (\%accounts, $key, 'Dept') !~ /^\s*$/)
	    {
		print LDAP "ou: " .
		    db_get_prop (\%accounts, $key, 'Dept') . "\n";
	    }

	    if (db_get_prop (\%accounts, $key, 'City') !~ /^\s*$/)
	    {
		print LDAP "l: " .
		    db_get_prop (\%accounts, $key, 'City') . "\n";
	    }

	    if (db_get_prop (\%accounts, $key, 'Street') !~ /^\s*$/)
	    {
		print LDAP "street: " .
		    db_get_prop (\%accounts, $key, 'Street') . "\n";
	    }
	}

	elsif (db_get_type (\%accounts, $key) eq 'group')
	{
	    print LDAP "\n";
	    print LDAP "dn: uid=$key," .
		esmith::util::ldapBase (db_get (\%conf, 'DomainName')) . "\n";
	    print LDAP "objectClass: posixgroup\n";
	    print LDAP "uid: " . $key . "\n";
	    print LDAP "cn: " . $key . "\n";

            # ****************************************************
            #   Process group members
            # ****************************************************
            my $gmembers = db_get_prop(\%accounts, $key, 'Members');
            my $member;
	    if ($gmembers !~ /^\s*$/)
	    {
               foreach $member (split (/,/,$gmembers))
               {
                 print LDAP "memberUid: " . $member . "\n";
              }
            } 
            my $gidNumber = db_get_prop (\%accounts, $key, 'Gid');
	    if ($gidNumber !~ /^\s*$/)
	    {
		print LDAP "gidNumber: $gidNumber\n";
	    }
	    
            my $name = db_get_prop (\%accounts, $key, 'Description');
	    if ($name !~ /^\s*$/)
	    {
		print LDAP "description: $name\n";
	    }

	    my $mail = $key . "\@" . db_get (\%conf, 'DomainName');
	    if ($mail !~ /^\s*$/)
	    {
		print LDAP "mail: $mail\n";
	    }

	    if (db_get_prop (\%conf, 'ldap', 'defaultPhoneNumber') !~ /^\s*$/)
	    {
		print LDAP "telephoneNumber: " .
		    db_get_prop (\%conf, 'ldap', 'defaultPhoneNumber') . "\n";
	    }

	    if (db_get_prop (\%conf, 'ldap', 'defaultCompany') !~ /^\s*$/)
	    {
		print LDAP "o: " .
		    db_get_prop (\%conf, 'ldap', 'defaultCompany') . "\n";
	    }

	    if (db_get_prop (\%conf, 'ldap', 'defaultDepartment') !~ /^\s*$/)
	    {
		print LDAP "ou: " .
		    db_get_prop (\%conf, 'ldap', 'defaultDepartment') . "\n";
	    }

	    if (db_get_prop (\%conf, 'ldap', 'defaultCity') !~ /^\s*$/)
	    {
		print LDAP "l: " .
		    db_get_prop (\%conf, 'ldap', 'defaultCity') . "\n";
	    }

	    if (db_get_prop (\%conf, 'ldap', 'defaultStreet') !~ /^\s*$/)
	    {
		print LDAP "street: " .
		    db_get_prop (\%conf, 'ldap', 'defaultStreet') . "\n";
	    }
	}
    }

    close LDAP || die "$program exited $?";

}

exit (0);
