#!/usr/bin/perl
# tta_loadous.cgi
# Loading Ous from File to ...  Servicios de Tarantella.
# STTA Rev. 1.3 Copyright (c) 2001-2002 Jesús Pérez Lorenzo  --- license GNU GPL 

require './stta-lib.pl';
do "ldap-lib.pl";
&foreign_require("proc", "proc-lib.pl");
&ReadParse();

&headerstta($text{'index_loadou'}, "", "",undef,1,1,$text{headpage_rigth} );
print "<hr><p>\n"; 
print "<p>\n";
print "<table width=100% border>\n";
print "<tr $tb> <td><b>$text{'titidx_loadou'}</b></td> </tr>\n";
print "<tr $cb> <td><table width=100%>\n";
print "<center>\n";
$file = $in{file};
if (!$file) { 
           print "<tr></tr>\n";
           print "<tr><form METHOD=POST name=loadous ACTION=\"tta_loadous.cgi\" >\n"; 
           print "<td valign=top>",&hlink("<b>$text{'f_loadfile'}</b>","floadfile"),"&nbsp;\n";
           print "<td valign=top><input TYPE=\"text\" NAME=\"file\" VALUE=\"\" cols=\"52\" size=\"52\" ALIGN=left ", &file_chooser_button("file",0,0,$config{tta_backup_dir}),"</td></tr>\n";
           print "        <tr><td valign=top><input TYPE=\"submit\" NAME=\"",$text{'f_sendreq'},"\" VALUE=\"",$text{'f_sendreq'},"\">\n";
           print "        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\n";
           print "	</form>\n"; 
           print "<tr><tr></table>\n";
           print "<tr $tb><td><b>$text{'load_data'}</b></td></tr>\n";
           print "</table><hr><p>\n";
           &footer($config{'back_return'},$text{'index'});
           print "<p><p><p>\n";
           do "footer.pl";
           exit;
} 
unless ( open DATAFILE, "$config{tta_backup_dir}/$file" || open DATAFILE, "$file") {
 if ( $file ne "" ) { print "<p><b>$text{'loadou_noexist'} $file </b></td> </tr>\n"; }
 print "<tr><tr></table></table><hr><p>\n";
 &footer($config{'back_return'},$text{'index'});
 print "<p><p><p>\n";
 do "footer.pl";
 exit;
}

$str=$file." : ".$text{'titidx_loadou'};
&additional_log('tta_loadous.cgi', "loadou", $str);
&webmin_log('tta_loadous.cgi', "loadou", "loading $file");

$ntdomain=$config{'companyntdomain'}; 
$lnum=0;
%h_ou=&list_ous();
@listobj="";
while(<DATAFILE>) {
      if (/\S/ && !/^[#\+\-]/ ) {
              chomp($lin);
              @lin = split(/::/, $_, -1);
#             $lin[1]=~ s/"//g;
#             $is_space=chop($lin[1]);
#             if ( $is_space eq " " ) { chop($lin[1]); }
              @alias = split(/,/, $lin[0], -1);
              push(@listobj, { 'alias' => $lin[0],
                              'tree_name' => $alias[1],
                              'grpou' => $alias[0],
                              'name' => $lin[1],
                              'link' => $lin[2],
                              'maxsess' => $lin[3],
                              'descrip' => $lin[4],
                              'num' => scalar(@listobj) });
                               $idx{$lin[0]} = $listobj[$#listobj];
              }
              $lnum++;
}
%h_links=&list_links();
LINE: foreach $newobj (@listobj) {
    $ou_alias=$newobj->{alias};
    next LINE if ( $ou_alias eq "" ) ;
    $type_cmd ="new_orgunit";
    $ou_name=$newobj->{name};
    if ( $ou_name eq "" )  { $ou_name=$ou_alias; }
    $grpou=$newobj->{grpou};
    if ( $grpou eq "" || $grpou eq $ou_alias) { $grpou=$config{users_alias}; }
    $ou_treename=$newobj->{tree_name};
    if ( $ou_treename eq "" ) { $ou_treename=$ou_alias; }
    $ou_descrip=$newobj->{descrip};
    if ( $ou_descrip eq "" )  { $ou_descrip=$ou_name; }
    $ou_link=$newobj->{link};
    $ou_maxsess=$newobj->{maxsess};
    if ( $ou_maxsess lt 0 )  { $ou_maxsess=$config{default_maxsess}; }
   
    $organizationname="";
    $dn_ou="";
    $ens_ou="";
    %h_ous=&list_ous();
    if ( $h_ous{$ou_alias}->{'alias'} eq $ou_alias) {   # Already there
         $str="ERROR: ".$ou_name." ->  ".$ou_alias." -> ".$text{'list_err'};
         &webmin_log('tta_loadous.cgi', "loadou", "error: $str");
         print "<tr><p><b>$str</b></tr>\n";
         next LINE;
    }
    if ( $h_ous{$grpou}->{'alias'} ne $grpou) {     # No mother OU up in the tree
         $str="ERROR: ".$ou_name." ->  ".$grpou." -> ".$text{'list_errno'};
         &webmin_log('tta_loadous.cgi', "loadou", "error: $str");
         print "<tr><p><b>$str</b></tr>\n";
         next LINE;
    }

    $dn_ou=$h_ous{$grpou}->{'obj'};
    $organizationname=$h_ous{$grpou}->{'name'};
    $ens_ou=$h_ous{$grpou}->{'ens'};
    
    if ( $dn_ou eq "" ) {
       $dnou = "ou=$ou_treename";
       $ens = "ou=$ou_treename";
    } else {
       $dnou = "ou=$ou_treename,$dn_ou";
       $ens = "$ens_ou/ou=$ou_treename";
    }
    $ens_obj = "--name \"".$config{tta_base}."/$ens\"";
    $dn = "$dnou,".$config{ldap_base};
    $ldap_server=1;
    &connect ();
    if ( &add_ou() eq 0 ) { 
        if ( $config{ldap2_server}.length gt 0 ) {
             $ldap_server=2;
             &connect ();
             if ( &add_ou() ne 0 ) { &err_replication() }
        }
        $| = 1;
        $temp_f=$config{'tta_defaults_ou'}; 
       # Assigning and collecting values to be used in the form ...
        %h_ttaobj=&load_ttaobj();
        $cdm     =$h_ttaobj{'cdm'}->{'value'};
        $inherit =$h_ttaobj{'inherit'}->{'value'};
        $webtop  =$h_ttaobj{'webtop'}->{'value'};
        $conntype=$h_ttaobj{'conntype'}->{'value'};
        $temp_f = &tempname();
        open(TEMPF, ">$temp_f");
                print TEMPF "$ens_obj\\\n";
                if ( $cdm ne "" ) {print TEMPF "  --cdm \"$cdm\"\\\n"; }
                if ( $inherit ne "" ) {print TEMPF "  --inherit \"$inherit\"\\\n"; }
                if ( $conntype ne "" ) { print TEMPF "  --conntype \"$conntype\"\\\n"; }
                if ( $webtop ne "" ) {print TEMPF "  --webtop \"$webtop\"\\\n"; }
                print TEMPF "  --description \"$in{ouname}\" \n";
        close(TEMPF);

        if (&run_tta_cmd($type_cmd,$ens_obj,$temp_f)) { 
           $lin_ou=$ou_alias."::".$ou_name."::"."$dnou"."::"."$ens"."::".$ou_maxsess."::".$ou_descrip;
           &lock_file($config{'ou_list'});
	   open(FILEOUS, ">>$config{'ou_list'}");
           print FILEOUS "$lin_ou\n";
           close (FILEOUS);
           &unlock_file($config{'ou_list'});
           $str=$text{'user_made'}.$ou_name." ->  ".$dn;
           &additional_log('tta_loadous.cgi', "loadou", $str);
           &webmin_log($config{'ou_list'}, "loadou", "write",$lin_ou);
           if ( $ou_link ne "" && $h_links{$ou_link}->{'alias'} eq $ou_link ) {
                $temp_f = &tempname();
                open(TEMPF, ">$temp_f");
                     print TEMPF "$ens_obj\\\n";
                     print TEMPF "  --link \"$config{tta_base}/".$h_links{$ou_link}->{'ens'}."\" \n";
                close(TEMPF);
                $type_cmd="add_link";
                &run_tta_cmd($type_cmd,$ens_obj,$temp_f);
           }
           print "<tr><p><b>".$text{'user_made'}."$ou_name ->  $grpou ($ou_link)</b></tr>\n";
           print "<tr></tr>\n";
        } else {
           $str="ERROR: ".$ou_name." ->  ".$dn." -> ".$ens_obj;
           &webmin_log('tta_loadous.cgi', "loadou", "error: $str");
           print "<tr><p><b>$str</b></tr>\n";
        }
	if ( $debug lt 2 )  { unlink($temp_f); }
    }
 }
 close (DATAFILE);
 print "<tr><tr></table></table><hr><p>\n";
 &footer($config{'back_return'},$text{'index'});
 print "<p><p><p>\n";
 do "footer.pl";
 exit;
