#!/usr/bin/perl
# mvobjs.cgi
# ...  Services with Tarantella.
# STTA Rev. 1.3 Copyright (c) 2001-2004 Jesús Pérez Lorenzo  --- license GNU GPL 
# stta: (@#) 1.31051043346- [2004_01_29_102036]

require './stta-lib.pl';
do 'app-lib.pl';
if ( $config{'has_ldapusers'} eq "true" || $config{'is_ldapcache'} eq "true") {
    do "ldap-lib.pl";
}
&foreign_require("proc", "proc-lib.pl");

&ReadParse();
if ( !$in{objtype} ) { &redirect("index.cgi"); }
if ( $in{task} ne "move" &&  $in{task} ne "copy" ) { &redirect("index.cgi"); }
$objtype=$in{objtype};
$postform=$in{postform};
$titleform="index_mv".$objtype;
$task=$in{task};
$orgobj=$in{orgobj};
$orgobj=~ s/\r|\n/ /g;
$userdn=$in{userdn} || "";
$userou=$in{userou} || "";
$newobj=$in{newobj};
$newalias=$in{newalias};
$destgrp=$in{destgrp};
$grptype=$objtype;
if ( $objtype =~ /apps/ ) {  $grptype="grpapps" };
if ( $objtype =~ /hosts/ ) {  $grptype="grphosts" };
if ( $objtype eq "ou" || $objtype eq "users" || $objtype eq "sysusers") {  $grptype="ou" };
if ( $objtype eq "sysusers") {  $postform="mvusers" ; $titleform="index_mvusers" };

&headerstta($text{$titleform}, "", $postform,undef,1,0,"$text{'headpage_right'}"."$text{'head_logout'}",undef,undef,"$text{head_below}" ); 

if ( $objtype eq "ou" || $objectype eq "users" ) { 
     %h_objs=&list_ous();
} else { %h_objs=eval("\&list_$objtype()"); }
$listObjs=$objtype."_list";
if ( $grptype ne $objtype )  {
     if ( $grptype eq "ou" ) { 
          %h_grpobjs=&list_ous();
     } else {
          %h_grpobjs=eval("\&list_$grptype()");
     }
} else { %h_grpobjs=%h_objs }

 print "<p>\n";
 print "<table width=100% border>\n";
print "<tr $tb> <td><font color='$revtext'><b>".eval("\$text{'titidx_$postform'}")."</b></td> </tr>\n" ;
 print "<tr $cb> <td><table width=100%>\n";
 print "<center>\n";

 $dn_grpobj="";
 $name_grpobj="";
 $ens_grpobj="";
 if ( $h_grpobjs{$destgrp}->{'alias'} ne $destgrp ) {
        $err_msg="$destgrp: ".$text{'list_errno'};
        &footHere;
        exit;
 }
 $dn_grpobj=$h_grpobjs{$destgrp}->{'obj'};
 $name_grpobj=$h_grpobjs{$destgrp}->{'name'};
 $ens_grpobj=$h_grpobjs{$destgrp}->{'ens'};
            $ens_org= ""; 
            $cn_org="";
            if ( $dn_obj ne "" ) {
                 $ens_org= $ens_obj;
                 @line=split(/\//,$ens_org);
                 $cn_org=$line[$#line];
            }
 $dn_grporg = "";
 $ens_grporg= "";
 if ( $dn_grpobj ne "" ) {
      $dn_grporg= $dn_grpobj;
      $ens_grporg= $ens_grpobj;
 }
 $nitems=0;
 foreach $item (split(/ /, $orgobj)) {
            next if ( $item eq "" );
            $dn_obj="";
            $name_obj="";
            $ens_obj="";
            next if ( $userdn eq "-" );
            if ( $userdn ne "" ) { 
                 $dn_obj=$userdn;
                 $dn_obj=~ s/,$config{ldap_base}//g;
                 $name_obj=$item;
                 $ens_obj=$h_grpobjs{$userou}->{'ens'}."/cn=$item";
            } else {
                 if ( $h_objs{$item}->{'alias'} ne $item ) {
                        $errmsg="$orgobj: ".$text{'list_errno'};
                        print "<tr><td> $errmsg </td></tr>";
                        next;
                 }
                 $dn_obj=$h_objs{$item}->{'obj'};
                 $name_obj=$h_objs{$item}->{'name'};
                 $ens_obj=$h_objs{$item}->{'ens'};
            }
            $nitems++;
            $ens_org= ""; 
            $cn_org="";
            if ( $dn_obj ne "" ) {
                 $ens_org= $ens_obj;
                 @line=split(/\//,$ens_org);
                 $cn_org=$line[$#line];
            }
            if ( $userdn ne "" ) { 
                 $newdn_obj = "cn=$name_obj";
            } else {
                 $newdn_obj = "ou=$name_obj";
            }
            if ( $dn_grporg ne "" ) { $newdn_obj .= ",$dn_grporg"; }
            if ( $newobj ne "" ) {
                 $cn_org= "cn=$newobj"; 
                 if ( $nitems gt 1 ) { $cn_org .=$nitems; }
                 if ( $objtype eq "ou" ||  $objtype eq "grpapps" ) { $cn_org= "ou=$newobj"; }
                 $newdn_obj = "ou=$newobj";
                 if ( $nitems gt 1 ) { $newdn_obj .=$nitems; }
                 $newdn_obj .= ",$dn_grporg";
            }
            $newens_org = "";
            if ( $ens_grporg ne "" ) { $newens_org = $ens_grporg; }
            if ( $cn_org ne "" ) { 
                 if ( $newens_org ne "" ) { $newens_org .= "/"; }
                 $newens_org .= $cn_org; 
            }
            $newens_obj = "\"".$config{tta_base}."/$newens_org"."\"";
            $ens_obj = "\"".$config{tta_base};
            if ( $ens_org ne "" ) { $ens_obj .= "/".$ens_org; }
            $ens_obj .= "\"";
            # First try to make TTA changes 
            if ( $task eq  "copy" ) {
                 # loading object attributes ...
                 $type_cmd="attributes";
                 if ( !&run_tta_cmd($type_cmd,$ens_obj) ) {
                      $errmsg="$orgobj: ($ens_obj) - ".$text{'tta_noobject'};
                      print "<tr><td> $errmsg </td></tr>";
                      next;
                 }
                 %h_ttaobj=&load_ttaobj();
            }
            $| = 1;
            $temp = &tempname();
            open(TEMP, ">$temp");
            print TEMP "#!/bin/sh\n";
            print TEMP $env;
            print TEMP "export $export\n" if ($export);
            print TEMP "$str\n";
            $temp_f = &tempname();
            open(TEMPF, ">$temp_f");
            if ( $task eq  "move" ) {
                 $type_cmd="rename";
                 print TEMPF "  --name $ens_obj\\\n"; 
                 print TEMPF "  --newname $newens_obj\\\n"; 
            } elsif ( $task eq  "copy" ) {
                 print TEMPF "  --name $newens_obj\\\n"; 
                 if ( $h_ttaobj{objectclass}{value} =~ /ttacharacterapplication/ ) {
                      $tta_ObjType="charapp";
                 } elsif ( $h_ttaobj{objectclass}{value} =~ /ttawindowsapplication/ ) {
                      $tta_ObjType="windowsapp";
                 } elsif ( $h_ttaobj{objectclass}{value} =~ /ttaxapplication/ ) {
                      $tta_ObjType="xapp";
                 } elsif ( $h_ttaobj{objectclass}{value} =~ /scottaauxhost/ ) {
                      $tta_ObjType="host";
                 } elsif ( $h_ttaobj{objectclass}{value} =~ /scottagroupofnames/ ) {
                      $tta_ObjType="group";
                 } elsif ( $h_ttaobj{objectclass}{value} =~ /container/ ) {
                      $tta_ObjType="container";
                 } elsif ( $h_ttaobj{objectclass}{value} =~ /organizationalunit/ ) {
                      $tta_ObjType="orgunit";
                 } elsif ( $h_ttaobj{objectclass}{value} =~ /person/ ) {
                      $tta_ObjType="person";
                 }
                 if ( $tta_ObjType =~ /app$/ ) { 
                      @appdef=&get_tta_appdef($tta_ObjType);
                      %h_appdef=();
                      foreach $appDefData (@appdef) {
                              $attrName=$appDefData->{att};
                              if ( $attrName eq "appserv" ) {
                                   $hosts_list ="";
                                   foreach $h (split(/,/, $h_ttaobj{$attrName}{value})) {
                                       if ( $hosts_list ne "" ) { $hosts_list .= '" "' }
                                       $hosts_list .=$h;
                                   }
                                   $h_ttaobj{$attrName}{value}=$hosts_list;
                              }
                              $h_appdef{$attrName}->{'att'} = $appDefData->{att};
                      }
                 }
                 $type_cmd="new_".$tta_ObjType;
                 foreach $attr (keys (%h_ttaobj)) {
                       next if ( $h_ttaobj{$attr}{property} eq "Name" );
                       next if ( $h_ttaobj{$attr}{property} eq "objectclass" );
                       next if ( $h_ttaobj{$attr}{property} eq "scottaWebtopContents" );
                       next if ( $h_ttaobj{$attr}{property} eq "Organizational Unit" );
                       if ( $tta_ObjType =~ /app$/ ) { 
                            $attrName=$h_ttaobj{$attr}{property};
                            next if ($h_appdef{$attrName}->{att} ne $attrName );
                       }
                       print TEMPF "  --".$h_ttaobj{$attr}{property}.
                                   " \"$h_ttaobj{$attr}{value}\"\\\n"; 
                 }
            }
            print TEMPF "\n";
            close(TEMPF);
            if (&run_tta_cmd($type_cmd,$ens_obj,$temp_f)) { 
                print "</pre>\n";
            } else { &html_errshow("tta")};
#            system("cat $temp_f >> /opt/save/ttamvobjs.scr");
	    if ( $debug lt 2 )  { unlink($temp_f); }
            if ( $objtype eq "ou" || $grptype eq "ou" ) {
                 if ( $config{'has_ldapusers'} eq "true" ) { 
                      # LDAP changes ....
                      $ldap_server=$in{ldapserver} || "1";
                      &connect();
                      &mvcp_ldap_obj($task,$dn_obj,$newdn_obj,$objtype);
                      if ( $config{ldap2_server}.length gt 0 ) {
                           $ldap_server=2;
                           &connect();
                           if ( &mvcp_ldap_obj($task,$dn_obj,$newdn_obj,$objtype) ne 0 ) { &err_replication() }
                      }
                }
               if ( $config{'has_systemusers'} ne "true" && $userdn eq "-" ) {
                    $sysuser="";
               }
            } 
            if ( $config{'is_ldapcache'} eq "true" ) {
                     # LDAPCACHE changes ....
                     $ldapcache_server=$in{ldapcacheserver} || "1";
                     &connect_cache();
                     if ( &mvcp_cache_obj($task,$dn_obj,$newdn_obj,$objtype) eq 0 ) { 
                          if ( $config{ldapcache2_server}.length gt 0 ) {
                               $ldapcache_server=2;
                               &connect_cache ();
                               if ( &mvcp_cache_obj($task,$dn_obj,$newdn_obj,$objtype) ne 0 ) { &err_replication() }
                          }
                     }
            }
            # STTA changes ....
            if ( $objtype ne "users" ) {
               if ( $task eq "copy" && $newalias eq "" ) { $newalias="copy_$item"; }
               $lin_obj=$item."::".$destgrp."::".$newdn_obj."::".$newens_org."::".$newalias;
#               if ( $task eq "copy" && $nitems gt 1 ) { $lin_obj .=$nitems; }
               $lin_obj .="::";
               &out_objslist( "modify", "mvobjs.cgi", $objtype, $listObjs, $item, $lin_obj, \%h_objs );
               &webmin_log($h_objs{$item}->{'ens'},"$objtype","$task",$lin_obj);
            }
            print "<tr><td valign=top> ".$text{$task}."  <b>".$h_objs{$item}->{name}."</b> --><b> $name_grpobj </b> (<i>$cn_org</i>) </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;

