#!/usr/bin/perl
# tta_loadusrs.cgi
# Loading Users from File to ...  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 "ldap-lib.pl";
&foreign_require("proc", "proc-lib.pl");
&ReadParse();
$syncuser=$in{syncuser};

&headerstta($text{'index_loadusr'}, "", "loadusr",undef,1,0,"$text{'headpage_right'}"."$text{'head_logout'}",undef,undef,"$text{head_below}" );
print "<hr><p>\n"; 
print "<p>\n";
print "<table width=100% border>\n";
print "<tr $tb> <td><font color='$revtext'><b>$text{'titidx_loadusr'}</b></td> </tr>\n";
print "<tr $cb> <td><table align=left>\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=\"52\" size=\"52\" ALIGN=left ", &file_chooser_button("file",0,0,$config{tta_backup_dir})," ($config{tta_backup_dir})</td>\n";
            print "<td>&nbsp;&nbsp;&nbsp;&nbsp&nbsp;",&hlink("<b>$text{'sync_user'}</b>","fsyncuser"),"&nbsp;";
    print "<input type=\"checkbox\" name=\"syncuser\" value=\"true\" >&nbsp;</td></tr>";
           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><font color='$revtext'><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{'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', "loadusr", $str);
&webmin_log('tta_loadusrs.cgi', "loadusr", "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]); }
              push(@listobj, { 'department' => $lin[0],
                              'surname' => $lin[1],
                              'usernumber' => $lin[2],
                              'password' => $lin[3],
                              'email' => $lin[4],
                              'where' => $lin[5],
                              'usertype' => $lin[6],
                              '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};
    $usertype=$newobj->{usertype};
    if ( "$where" eq "" ) { $where=$surname; }
    $organizationname="";
    $dn_ou="";
    $ens_ou="";
    if ( $h_ou{$userou}->{'alias'} eq $userou ) {
            $dn_ou=$h_ou{$userou}->{obj};
            $ens_ou=$h_ou{$userou}->{ens};
            $organizationname=$h_ou{$userou}->{name};
    }
    if ( $usertype eq "ldap" ) {
        $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() }
            }
        } else { next };
     } elsif ( $usertype eq "system" ) {

     } else { next };
     if ( $ens_ou eq "" ) {
           $ens_obj = "--name \"".$config{tta_base}."/cn=$username\"";
     } else {
           $ens_obj = "--name \"".$config{tta_base}."/$ens_ou/cn=$username\"";
     } 
     $| = 1;
     $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 "$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_name'});
         open(FILELDAPLOG, ">>$config{'ldap_log_name'}");
              print FILELDAPLOG "$lin_user\n";
         close(FILELDAPLOG);
         $str=$text{'user_made'}.$username." ->  ".$dn;
         &additional_log('tta_loadusrs.cgi', "loadusr", $str);
         &webmin_log($config{'ldap_log_name'}, "loadusr", "write");
         &unlock_file($config{'ldap_log_name'});
         if ( $usertype eq "system" ) {
              %h_sysusers=&list_sysusers();
              if ( $h_sysusers{$sysuser}->{name} ne $username ) {
                 $maxsess=$config{default_maxsess};
                 $lin_sysuser=$sysuser."::".$userou."::".$ens_obj."::".$maxsess."::".$email."::".$where."::";
                 &lock_file($config{'sysusers_list'});
                 open(FILESYSUSER, ">>$config{'sysusers_list'}");
                       print FILESYSUSER "$lin_sysuser\n";
                 close (FILESYSUSER);
                 &unlock_file($config{'sysusers_list'});
                 $str=$text{'user_made'}.$sysuser." ->  ".$organizationname;
                 &additional_log('addusr.cgi', "sysuser", $str);
                 &webmin_log($config{'sysusers_list'},"sysuser","write",$lin_sysuser);
                 &remote_file("write",$config{sysusers_list},"sysusers_list");
                 if ( $syncuser eq "true" ) { &make_remote_user("add",$sysuser); }
              }
         }
         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', "loadusr", $str);
         &webmin_log('tta_loadusrs.cgi', "loadusr", "error: $username");
     }
     if ( $debug lt 2 )  { unlink($temp_f); }
 }
 close (DATAFILE);
 print "<tr><td><center>".&text('viewlog_action',$stta_sid)."</td></tr>"; 
 print "<tr><tr></table></table><hr><p>\n";
 &footer($config{'back_return'},$text{'index'});
 print "<p><p><p>\n";
 do "footer.pl";
 exit;

