#!/usr/bin/perl
# tta_loadusrs.cgi
# Loading Users from File to ...  Servicios de Tarantella.

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

&header($text{'index_loadusr'}, "", "");
print "<hr><p>\n"; 
print "<p>\n";
print "<table width=100% border>\n";
print "<tr $tb> <td><b>$text{'titidx_loadusr'}</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=loadusrs ACTION=\"tta_loadusrs.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=\"32\" size=\"12\" ALIGN=left</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"; 
} 
unless ( open DATAFILE, $file ) {
 print "<p><b>$text{'loadusr_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;
}
 $type_cmd = "new_person";

$str=$file." : ".$text{'titidx_loadusr'};
&additional_log('tta_loadusrs.cgi', undef, $str);
&webmin_log('tta_loadusrs.cgi', undef, "loading $file");

$ntdomain=$config{'companyntdomain'}; 
$lnum=0;
@listobj="";
while(<DATAFILE>) {
      if (/\S/ && !/^[#\+\-]/ && $lnum gt 0 ) {
              chomp($lin);
              @lin = split(/::/, $_, -1);
#             $lin[1]=~ s/"//g;
#             $is_space=chop($lin[1]);
#             if ( $is_space eq " " ) { chop($lin[1]); }
              push(@listobj, { 'department' => $lin[0],
                              'surname' => $lin[1],
                              'usernumber' => $lin[2],
                              'password' => $lin[3],
                              'email' => $lin[4],
                              'where' => $lin[5],
                              'num' => scalar(@listobj) });
                               $idx{$lin[0]} = $listobj[$#listobj];
              }
              $lnum++;
}
LINE: foreach $newobj (@listobj) {
    $username=$newobj->{usernumber};
    next LINE if ( $username eq "" ) ;
    $surname=$newobj->{surname};
    $password=$newobj->{password};
    if ( "$password" eq "" ) { $password=$username; }
    $email=$newobj->{email};
    $userou=$newobj->{department};
    $where=$newobj->{where};
    if ( "$where" eq "" ) { $where=$surname; }
    $organizationname="";
    $dn_ou="";
    $ens_ou="";
    foreach $ou (&list_ous()) {
         if ( $ou->{'alias'} eq $userou ) {
            $dn_ou=$ou->{obj};
            $ens_ou=$ou->{ens};
            $organizationname=$ou->{name};
            last;
         }
    }
    $dn = "cn=$username,$dn_ou,".$config{ldap_base};

    $ldap_server=1;
    &connect ();
    if ( &add_usr() eq 0 ) { 
        if ( $config{ldap2_server}.length gt 0 ) {
             $ldap_server=2;
             &connect ();
             if ( &add_usr() ne 0 ) { &err_replication() }
        }
        $| = 1;
        $ens_obj = "--name \"".$config{tta_base}."/$ens_ou/cn=$username\"";
        $temp_f=$config{'tta_defaults_obj'}; 
        foreach $i (&load_ttaobj()) {
            if ( $i->{'property'} eq "user" ) { $user=$i->{'value'} };
            if ( $i->{'property'} eq "ntdomain" ) { $ntdomain=$i->{'value'} };
            if ( $i->{'property'} eq "bandwidth" ) { $bandwidth=$i->{'value'} };
            if ( $i->{'property'} eq "cdm" ) { $cdm=$i->{'value'} };
            if ( $i->{'property'} eq "enabled" ) { $enabled=$i->{'value'} };
            if ( $i->{'property'} eq "inherit" ) { $inherit=$i->{'value'} };
            if ( $i->{'property'} eq "shared" ) { $shared=$i->{'value'} };
            if ( $i->{'property'} eq "webtop" ) { $webtop=$i->{'value'} };
            if ( $i->{'property'} eq "conntype" ) { $conntype=$i->{'value'} };
        }
        $temp_f = &tempname();
        open(TEMPF, ">$temp_f");
                print TEMPF "$ens_obj\\\n";
                print TEMPF "  --surname \"$surname\" \\\n";
                if ( $user ne "" ) {print TEMPF "  --user \"$user\" \\\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 ( $email ne "" ) {print TEMPF "  --email  \"$email\"\\\n";}
                print TEMPF "  --description \"$where\" \n";
        close(TEMPF);

        if (&run_tta_cmd($type_cmd,$ens_obj,$temp_f)) { 
            $lin_user="add::".$username."::".$password."::".$dn;
            &lock_file($config{'ldap_log'});
            open(FILELDAPLOG, ">>$config{'ldap_log'}");
                 print FILELDAPLOG "$lin_user\n";
            close(FILELDAPLOG);
            $str=$text{'user_made'}.$username." ->  ".$dn;
            &additional_log('tta_loadusrs.cgi', undef, $str);
            &webmin_log($config{'ldap_log'}, undef, "write");
            &unlock_file($config{'ldap_log'});
            print "<tr><p><b>".$text{'user_made'}."$username ->  $userou</b></tr>\n";
            print "<tr></tr>\n";
        } else {
            $str="ERROR: ".$username." ->  ".$dn." -> ".$ens_obj;
            &additional_log('tta_loadusrs.cgi', undef, $str);
            &webmin_log('tta_loadusrs.cgi', undef, "error: $username");
        }
	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;

