#!/usr/bin/perl
#
# Copyright (c) 2003 VA Linux Systems Japan, K.K.
#
# LICENSE NOTICE
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
#   notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
#   notice, this list of conditions and the following disclaimer in the
#   documentation and/or other materials provided with the distribution.
# 3. Neither the name of the company nor the names of its contributors
#   may be used to endorse or promote products derived from this software
#   without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
# SUCH DAMAGE.

# This product includes software developed by Luke Howard.

#
# Usage:
#   ultramigration [-h <host>] [-b <basedn>] [-D <rootdn>] [-w <rootpw>] 
#
# need ldapdiff and migrationtools
#

use File::Temp qw/ tempfile /;
use Getopt::Std;
eval q{
  use UltraPossum::Conf;
};
my $ultrapossum = 1 if ! $@;

sub error
{
  print STDERR <<EOF;
Error: $_[0]
Usage:
 $0 [-h <host>] [-b <basedn>] [-D <rootdn>] [-w <rootpw>] 
EOF
  exit 1;
}

my ( $tmpfh, $tmp ) = tempfile();
my ( $conffh, $conf ) = tempfile();
my ( $addfh, $add ) = tempfile();
my ( $modfh, $mod ) = tempfile();

close( $addfh );
close( $modfh );

my ( $master, $suffix, $rootdn, $rootpw );

if( $ultrapossum ) {
  my $ul = UltraPossum::Conf->new;
  $master = $ul->{LDAPMASTER};
  $suffix = $ul->{SUFFIX};
  $rootdn = $ul->{ROOTDN};
  $rootpw = $ul->{ROOTPW};
}

getopt('hbDw');
                                                                           
$master = $opt_h if $opt_h;
$suffix = $opt_b if $opt_b;
$rootdn = $opt_D if $opt_D;
$rootpw = $opt_w if $opt_w;

$master or die error("can't determine host");
$suffix or die error("can't determine basedn"); 
$rootdn or die error("can't determine rootdn");
$rootpw or die error("can't determine rootpw");

print STDERR "master: $master, suffix: $suffix, rootdn: $rootdn\n";

print $conffh <<EOF;
[global]
ldaphost: $master
ldapport: 389
rootdn: $rootdn
rootpw: $rootpw
modfile: $mod
addfile: $add
onlineupdate: no
offlineupdate: yes
onlineerrfatal: yes
plugin: no
#pluginfile: /usr/local/src/ldapdiff-0.7.0/plugins/ldapskeleton.so
pluginfile: no
pluginfunction: ldifskeleton
iconv: yes
ldifcharset: ISO-8859-1
ldapcharset: UTF-8
schemacheck: yes
schemabase: cn=subschema
schemafilter: objectClass=*
schemaattribute: attributetypes
schemahack: yes

[ou]
basedn: $suffix
filter: ou
group: objectClass=organizationalUnit
ignore: none
multi: none
noequality: none
mapalias: none=none
deleteentry: no
deleteattribute: no
profilebasedn: none

[passwd]
basedn: ou=People,$suffix
filter: uid
group: objectClass=posixAccount
ignore: none
multi: none
noequality: none
mapalias: none=none
deleteentry: no
deleteattribute: no
basedn: none
profilebasedn: none

[group]
basedn: ou=Group,$suffix
filter: cn
group: objectClass=posixGroup
ignore: none
multi: none
noequality: none
mapalias: none=none
deleteentry: no
deleteattribute: no
basedn: none
profilebasedn: none

EOF

close( $conffh );

print $tmpfh <<EOF;
dn: ou=People,$suffix
objectClass: organizationalUnit
ou: People

dn: ou=Group,$suffix
objectClass: organizationalUnit
ou: Group

EOF
close( $tmpfh );

sub modify
{
  system( "cat $add | ldapadd -x -h $master -D $rootdn -w $rootpw" ) == 0 or die;
  system( "cat $mod | ldapmodify -x -h $master -D $rootdn -w $rootpw" ) == 0 or die;
}

system( "ldapdiff -f $tmp -p ou -c $conf" ) == 0 or die "ldapdiff error\n";
modify();

chdir "/usr/share/migrationtools";
$ENV{LDAP_BASEDN} = $suffix;

system( "./migrate_passwd.pl /etc/passwd > $tmp ") == 0 or die;
system( "ldapdiff -f $tmp -p passwd -c $conf" ) == 0 or die;
modify();

system( "./migrate_group.pl /etc/group > $tmp" ) == 0 or die;
system( "ldapdiff -f $tmp -p group -c $conf") == 0 or die;
modify();

