#!/usr/bin/env perl

# wbmc_ssh_ldifbackup, v 1.0 2002/06/28 19:54 hata

require "/opt/nec/mail/wbmc/lib/wbmc.pl";
require "/opt/nec/mail/wbmc/bin/wbmc_msh_cmd.pl";

$umsconf_file = '/opt/nec/mail/ums.conf';
$ldapsearch_cmd = '/usr/bin/ldapsearch';
$slapadd_cmd = '/usr/sbin/slapadd';
$slapd_conf = '/opt/nec/mail/emsldap/slapd.conf';
#$ldif_backup_dir = '/tmp';
$ldif_backup_dir = '/home/mail/#emsldap';
$emsldap_cmd = '/etc/rc.d/init.d/emsldap';

#&readParam();

$action = $ENV{'WBMC_LDIFBACKUP_ACTION'};
$restore_ldif = "$ldif_backup_dir/$ENV{'WBMC_LDIFRESTORE_FILE'}";

&umsconf_read;

if ($ARGV[0] eq "")
{
    system("/opt/nec/mail/msh $host /opt/nec/mail/mcmd/wbmc_ssh_ldifbackup $action");
#   system("/opt/nec/mail/msh $host /opt/nec/mail/wbmc/bin/wbmc_ssh_ldifbackup $action");
    exit;
}
else
{
    $action = $ARGV[0];
}

if ($action eq "backup") 
{
    &ldif_backup;
}
exit;

sub umsconf_read
{
  # /opt/nec/mail/ums.conf ɤ߹
  # - host, port, uri, basedn, binddn, bindpw 
  #
    open(UMSCONF_FILE, $umsconf_file);
    while(<UMSCONF_FILE>) {
        $line = $_;
        chomp($line);
        if ($line =~ /^host/) {
            ($para, $host) = split(/=/, $line);
            $host =~ s/"//g;
        }
        if ($line =~ /^port/) {
            ($para, $port) = split(/=/, $line);
            $port =~ s/"//g;
        }
        if ($line =~ /^uri/) {
            ($para, $uri) = split(/=/, $line);
            $uri =~ s/"//g;
        }
        if ($line =~ /^basedn/) {
            ($para, $basedn) = split(/basedn=/, $line);
            $basedn =~ s/"//g;
        }
        if ($line =~ /^binddn/) {
            ($para, $binddn) = split(/binddn=/, $line);
            $binddn =~ s/"//g;
        }
        if ($line =~ /^bindpw/) {
            ($para, $bindpw) = split(/bindpw=/, $line);
            $bindpw =~ s/"//g;
        }
    }
}

sub ldif_backup
{
  # ldapsearch ޥɤLDIF᡼  ե˽񤭹
  # - ldapsearch -x -LLL -D "binddn" -w "bindpw" -h "host:port" -b "basedn" '*' '+'
  #
    my $ldapsearch_opt = " -x -LLL -D '$binddn' -w $bindpw -H '$uri' -b '$basedn' '*' '+'";
    my $all_search = $ldapsearch_cmd . $ldapsearch_opt;
    my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
    my $backupdate = sprintf("%04d%02d%02d%02d%02d%02d",
                          $year + 1900, $mon + 1, $mday, $hour, $min, $sec);
    my $ldif_backupfile = "$ldif_backup_dir/$backupdate.ldif";
    my $ldif_backuptmp = "$ldif_backup_dir/$backupdate.tmp";

    if (!open("LDIF", ">$ldif_backuptmp"))
    {
        print "open($ldif_backuptmp) failed: $!";
        return;
    }
    if (!open(SRCH, "$all_search | "))
    {
        print "open($all_search) failed: $!";
        return;
    }

    while (my $line = <SRCH>)
    {
        if ($line =~ /subschemaSubentry: cn=Subschema/)
        {
            next;
        }
      # if ($line =~ /^emsmodifyTimestamp:/)
      # {
      #     $line = "emsmodifyTimestamp: 20020101000000Z\n";
      # }
        print LDIF $line;
    }

    close(SRCH);
    close(LDIF);

    system "sync";

    if (!rename($ldif_backuptmp, $ldif_backupfile))
    {
        print "rename($ldif_backuptmp) failed: $!";
    }
}

# touchޥɤǥե
sub touchFile {
    my($file_name) = @_;
    system("/bin/touch" . ' ' . $file_name);
}
