#!/usr/bin/perl
# addusr.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';
if ( $config{'has_ldapusers'} eq "true"  || $config{'is_ldapcache'} eq "true" ) {
    do "ldap-lib.pl";
}

&headerstta($text{'index_addusr'}, "", "addusr",undef,1,0,"$text{'headpage_right'}"."$text{'head_logout'}",undef,undef,"$text{head_below}" ); 
print "<hr><p>\n"; 
&foreign_require("proc", "proc-lib.pl");
&ReadParse();

 $type_cmd = $in{ttacmd};
 $tta_norun = $in{ttanorun};
 print "<p>\n";
 print "<table width=100% border>\n";
 print "<tr $tb> <td><font color='$revtext'><b>$text{'titidx_adusr'}</b></td> </tr>\n";
 print "<tr $cb> <td><table width=100%>\n";
 print "<center>\n";

 $username=$in{username};
 $administrador=false;
 $passwcache=true;
# $ntdomain=$config{'companyntdomain'};
 $ntdomain="";
 $sysuser=$in{sysuser};
 $syncuser=$in{syncuser};

 if ( $config{'has_ldapusers'} ne "true" ) { $username="" }
 if ( $config{'has_systemusers'} ne "true") { $sysuser="" }

if ($username eq "" && $sysuser eq "" )  {
         &footHere;
         exit;
}
 $userou=$in{userou};
 $organizationname="";
 $dn_ou="";
 $ens_ou="";
 %h_ou=&list_ous();
 if ( $h_ou{$userou}->{'alias'} ne $userou ) {
        $err_msg=$text{'list_err'};
        &footHere;
        exit;
 }
 $dn_ou=$h_ou{$userou}->{'obj'};
 $organizationname=$h_ou{$userou}->{'name'};
 $ens_ou=$h_ou{$userou}->{'ens'};
 $where="";
 $email="";
 if ($username)  {
    $ldap_server=1;
    &connect ();
    $password=$in{password};
    # check is ou exist in LDAP
    $dn=$config{ldap_base};
    if ( $dn_ou ne "" ) {
         $dn=$dn_ou.",".$config{ldap_base};
         $cur_entry=&sel_ldap_obj($dn,"ou=*");
         if ( !defined($cur_entry) || $cur_entry->dn ne $dn) {
              @line=split(/,/, $dn_ou,-1);
              $ouname=$line[0];
              if ( &add_ou($dn) eq 0 ) {
                   if ( $config{ldap2_server}.length gt 0 ) {
                        $ldap_server=2;
                        &connect ();
                        if ( &add_ou($dn) ne 0 ) { &err_replication() }
                   }
               } else { &html_errshow("ldap")};
         }
    }
    if ( $dn_ou eq "" ) {
         $dn = "cn=$username,".$config{ldap_base};
    } else {
         $dn = "cn=$username,$dn_ou,".$config{ldap_base};
    }
    $surname=$username;
    if ( &add_usr() eq 0 ) {
        if ( $config{ldap2_server}.length gt 0 ) {
             $ldap_server=2;
             &connect ();
             if ( &add_usr() ne 0 ) { &err_replication() }
        } 
        if ( $config{'ldap_log_name'} ne "" ) {
             $lin_user="add::".$username."::".$password."::".$username."::".$dn;
             &lock_file($config{'ldap_log_name'});
             open(FILELDAPLOG, ">>$config{'ldap_log_name'}");
             print FILELDAPLOG "$lin_user\n";
             close (FILELDAPLOG);
             &unlock_file($config{'ldap_log_name'});
             &remote_file("write",$config{ldap_log_name},"ldap_log_name");
        }
        &webmin_log($config{'ldap_log_name'}, "usr", "write",$dn);
        $str=$text{'user_made'}.$username." ->  ".$dn;
        &additional_log('addusr.cgi', "usr", $str);
    } else { &html_errshow("ldap")};
 }
        $| = 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";
        if ( $username ) {
          if ( $ens_ou eq "" ) {
            $ens_obj = "cn=$username";
          } else {
            $ens_obj = "$ens_ou/cn=$username";
          }
        } elsif ( $sysuser ) {
          if ( $ens_ou eq "" ) {
            $ens_obj = "cn=$sysuser";
          } else {
            $ens_obj = "$ens_ou/cn=$sysuser";
          }
        }
        $temp_f=$config{'tta_defaults_obj'}; 
        %h_ttaobj=&load_ttaobj();
        $user =$h_ttaobj{'user'}->{'value'};
        $ntdomain =$h_ttaobj{'ntdomain'}->{'value'};
        $bandwidth =$h_ttaobj{'bandwidth'}->{'value'};
        $cdm     =$h_ttaobj{'cdm'}->{'value'};
        $enabled =$h_ttaobj{'enabled'}->{'value'};
        $inherit =$h_ttaobj{'inherit'}->{'value'};
        $shared =$h_ttaobj{'shared'}->{'value'};
        $webtop  =$h_ttaobj{'webtop'}->{'value'};
        $conntype=$h_ttaobj{'conntype'}->{'value'};
        $temp_f = &tempname();
        open(TEMPF, ">$temp_f");
                print TEMPF " --name \"".$config{tta_base}."/$ens_obj\" \\\n";
                if ( $username ) {
                     print TEMPF "  --surname \"$in{username}\" \\\n";
                     if ( $user ne "" ) {print TEMPF "  --user \"$user\" \\\n"; }
                } elsif ( $sysuser ) {
                     print TEMPF "  --surname \"$sysuser\" \\\n";
                     print TEMPF "  --user \"$sysuser\" \\\n"; 
                }
                if ( $ntdomain ne "" ) {print TEMPF "  --ntdomain \"$ntdomain\"\\\n"; }
                if ( $bandwidth ne "" ) {print TEMPF "  --bandwidth \"$bandwidth\"\\\n"; }
                if ( $cdm ne "" ) {print TEMPF "  --cdm \"$cdm\"\\\n"; }
                if ( $enabled ne "" ) {print TEMPF "  --enabled \"$enabled\"\\\n"; }
                if ( $shared ne "" ) {print TEMPF "  --shared \"$shared\"\\\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"; }
                if ( $username ) {
                      print TEMPF "  --description \"$in{username}\" \n";
                } elsif ( $sysuser ) {
                      print TEMPF "  --description \"$sysuser\" \n";
                }
        close(TEMPF);
        if ( $tta_norun eq "true" ) { $type_cmd = "no_run $type_cmd"; };
        if (&run_tta_cmd($type_cmd,$ens_obj,$temp_f)) { 
            print "</td></tr></pre>\n";
        } else { &html_errshow("tta")};
        if ($sysuser ne "" )  {
               %h_sysusers=&list_sysusers();
               if ( $h_sysusers{$sysuser}->{name} eq $sysuser ) {
                    $err_msg="$sysuser -- $text{'list_err'}";
                    &footHere;
                    exit;
               }
               $maxsess=$config{default_maxsess};
               $lin_sysuser=$sysuser."::".$userou."::".$ens_obj."::".$maxsess."::".$email."::".$where."::";
               &out_objslist( "add", "addusr.cgi", "sysusers", "sysusers_list", $sysuser, $lin_sysuser, undef );
               if ( $syncuser eq "true" ) { &make_remote_user("add",$sysuser); }
        }
        if ( $username ) {
                print "<tr $cb><td><b>".$text{'user_made'}.$username." ->  ".$in{userou}."</b></td></tr>\n";
        } elsif ( $sysuser ) {
                print "<tr $cb><td><b>".$text{'user_made'}.$sysuser." ->  ".$in{userou}."</b></td></tr>\n";
        }
        if ( $config{'is_ldapcache'} eq "true" && $in{tta3tier_login} ) { 
             $dnldap = "default";
             $default_obj="cn=$config{ldapcache_defaultobj}";
             if ( $username ) {
                  $dn = "ou=$username";
             } else {
                  $dn = "ou=$sysuser";
             }
             if ( $dn_ou ne "" ) { $dn .= ",$dn_ou"; }
             $ldapcache_server=1;
             &connect_cache ();
             @tta3tier=&set_tta3tier($default_obj,$dn,$dnldap,"add","user");
             if ( &cache_obj(\%tta3tier) ne 0 ) { 
                  &html_errshow("ldap");
                  &footHere; exit;
             };
             if ( $config{ldapcache2_server}.length gt 0 ) {
                      $ldapcache_server=2;
                      &connect_cache ();
                      if ( &cache_obj(\%tta3tier) ne 0 ) { &err_replication() }
             }
        }
        print "<tr></tr>\n";
# this change has to be done as IE was unable to get some hidden values ????
        print "<tr><form METHOD=POST name=addusr ACTION=\"tta_modusr.cgi?postform=addusr&username=$username&userou=$userou&sysuser=$sysuser&syncuser=$syncuser\" >\n"; 
#        print "  <input TYPE=\"hidden\" NAME=\"postform\" VALUE=\"addusr\"\n";
#        print "  <input TYPE=\"hidden\" NAME=\"username\" VALUE=\"",$username,"\"\n";
#        print "  <input TYPE=\"hidden\" NAME=\"sysuser\" VALUE=\"",$sysuser,"\"\n";
#        print "  <input TYPE=\"hidden\" NAME=\"syncuser\" VALUE=\"",$syncuser,"\"\n";
#        print "  <input TYPE=\"hidden\" NAME=\"userou\" VALUE=\"",$userou,"\">\n";
        print "  <input TYPE=\"hidden\" NAME=\"administrador\" VALUE=\"",$administrador,"\">\n";
        print "  <input TYPE=\"hidden\" NAME=\"dn\" VALUE=\"",$dn,"\">\n";
        print "        <tr><td valign=top><input TYPE=\"submit\" NAME=\"",$text{'f_modatt'},"\" VALUE=\"",$text{'f_modatt'},"\">\n";
        print "        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\n";
        print "	</form>\n"; 
	if ( $debug lt 2 )  { unlink($temp_f); }
 print "<tr><tr></table></table><hr><p>\n";
 &footer($config{'back_return'},$text{'index'});
 print "<p><p><p>\n";
 do "footer.pl";
 exit;

