#!/usr/bin/perl
# addlink.cgi
# ...  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";

&headerstta($text{'index_addlink'}, "", "addlink",undef,1,1,$text{headpage_rigth} ); 
print "<hr><p>\n";
&foreign_require("proc", "proc-lib.pl");
#$ldapcache_server=1;
#&connect_cache();
&ReadParse();
 
 print "<table width=100% border>\n";
 print "<tr $tb> <td><b>$text{'titidx_adlink'} $linkname </b></td> </tr>\n";
 print "<tr $cb> <td><table width=100%>\n";
 print "<center>\n";

 $type_cmd = $in{ttacmd};
 $linkname=$in{linkname};
 $grp_links=$in{grplinks};
 $obj_descrip=$in{descrip};
 $aliaslink=$in{aliaslink};
 $maxsess=$in{maxsess};

 if (!$linkname)  { &footHere; exit }  ;
    $alias=$aliaslink;
    %h_links=&list_links();
    if ( $h_links{$aliaslink}->{'alias'} eq $aliaslink ) {
         $err_msg="$aliaslink: ".$text{'list_err'};
         &footHere;
         exit;
    }
    $grp_name="";
    $grp_descrip="";
    $dn_grplink="";
    $ens_grplink="";
    if ( $h_links{$grp_links}->{'alias'} eq $grp_links ) {
         $dn_grplink=$h_links{$grp_links}->{obj};
         $ens_grplink=$h_links{$grp_links}->{ens};
         $grp_descrip=$h_links{$grp_links}->{descrip};
         $grp_name=$h_links{$grp_links}->{name};
    } else {
         $err_msg="$grp_links: ".$text{'list_errno'};
         &footHere;
         exit;
    }
    if ( $dn_grplink eq "" ) {
         $dn = "ou=$linkname";
         $ens= "cn=$linkname";
    } else {
         $dn = "ou=$linkname,$dn_grplink";
         $ens= "$ens_grplink/cn=$linkname";
    }
    $ens_obj = "--name \"".$config{tta_base}."/$ens\"";
    $dn_org=$dn;
    $| = 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");
                print TEMPF "$ens_obj\n";
    close(TEMPF);
    $dnldap = "default";
    $dn_org = $dn;
    $default_obj="cn=$config{ldapcache_defaultobj}";
    $ens_obj = "$ens,".$config{tta_base};
#    $ldapcache_server=1;
#    &connect_cache ();
#    @tta3tier=&set_tta3tier($default_obj,$dn,$dnldap,"add","link");
#    if ( &cache_obj(\%tta3tier) ne 0 ) { &footHere; exit }  ;
#    if ( $config{ldapcache2_server}.length gt 0 ) {
#             $ldapcache_server=2;
#             &connect_cache ();
#             if ( &cache_obj(\%tta3tier) ne 0 ) { &err_replication() }
#    }
    &lock_file($config{'link_list'});
    $lin_link=$aliaslink."::".$linkname."::"."$dn_org"."::"."$ens"."::".$maxsess."::".$obj_descrip."::";
    open(FILELINKS, ">>$config{'link_list'}");
           print FILELINKS "$lin_link\n";
    close (FILELINKS);
    &unlock_file($config{'link_list'});
    $str=$text{'user_made'}.$linkname." ->  ".$grp_name;
    &additional_log('addlinks.cgi', "link", $str);
    &webmin_log($config{'link_list'}, "link", "write",$lin_link);
    if ( &run_tta_cmd($type_cmd,$ens_obj,$temp_f) ) {
	print "";
    } else { &html_errshow("tta")};
    if ( $debug lt 2 )  { unlink($temp_f); }
    print "<tr><p><b>".$text{'user_made'}.$linkname." ->  ".$grp_name."</b></tr>\n";
    print "<tr></tr>\n";
 &footHere;
 exit;

