#!/usr/bin/perl
# makeusr.cgi
# ...  Servicios de Tarantella.

 require './tarantella-lib.pl';
do "ldap-lib.pl";

&header($text{'index_addusr'}, "", "");
print "<hr><p>\n"; 
&foreign_require("proc", "proc-lib.pl");
&connect ();
&ReadParse();

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

 
 
           &connect ();
           $ldap_base=$config{ldap_dnusers};
           $filter = "(cn=*)";
           $entry = $conn->search ($ldap_base, "subtree", $filter );
           &webmin_log("makeusr.cgi", "search", "subtree", $filter);
           while ($entry) {
               my (%users);
               $users{dn} = $entry->getDN ();
               $users{cn} = $entry->{cn}[0];
               $users{displayname} = $entry->{displayname}[0];
               $users[$numusrs++] = \%users;
               if ( "$config{ldap_type}" eq "OpenLDAP" ) {
                     $users{department} = $entry->{description}[0];
               } else {
                     $users{department} = $entry->{departmentnumber}[0];
               }
               $entry = $conn->nextEntry ();
           }




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

 if ($username)  {
    $userou=$in{userou};
    $password=$in{password};
    $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};
    $entry = $conn->newEntry ();
    $entry->setDN ($dn);
    if ( "$config{ldap_type}" eq "OpenLDAP" ) {
        $entry->{objectclass} =
             ["top", "person", "organizationalperson", "inetorgperson", "uidObject"];
    } else {
        $entry->{objectclass} =
             ["top", "person", "organizationalperson", "inetorgperson"];
    }
    $entry->{userpassword} = [$password];
    if ( "$config{ldap_type}" eq "OpenLDAP" ) {
         $entry->{description} = [$organizationname];
    } else {
         $entry->{departmentnumber} = [$organizationname];
    }
    $entry->{sn} = [$username];
    $entry->{cn} = [$username];
    $entry->{uid} = [$username];
    $conn->add ($entry);
    if ($err = $conn->getErrorCode ()) {
	print "<hr>\n";
        print "<b> add ($dn): $err:".$conn->getErrorString ()."</b>\n";
	print "<hr>\n";
        &webmin_log("makeusr.cgi", "new Entry", $dn, $err);
    } 
    else {
        &webmin_log("makeusr.cgi", "new Entry", $dn, $err);
        $lin_user="add::".$username."::".$password."::".$username."::".$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('makeusr.cgi', undef, $str);
        &webmin_log($config{'ldap_log'}, undef, "write");
        &unlock_file($config{'ldap_log'});
        $| = 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";
        $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 \"$in{username}\" \\\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"; }
                print TEMPF "  --description \"$in{username}\" \n";
        close(TEMPF);

        if (&run_tta_cmd($type_cmd,$ens_obj,$temp_f)) { 
           print "<tr><p><b>".$text{'user_made'}.$username." ->  ".$in{userou}."</b></tr>\n";
           print "<tr></tr>\n";
           print "<tr><form METHOD=POST name=addusr ACTION=\"tta_modusr.cgi?username=$username&userou=$userou\" >\n"; 
           print "  <input TYPE=\"hidden\" NAME=\"postform\" VALUE=\"addusr\"\n";
           print "  <input TYPE=\"hidden\" NAME=\"username\" VALUE=\"",$username,"\"\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 "  <input TYPE=\"hidden\" NAME=\"passwdcache\" VALUE=\"",$passwdcache,"\">\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"; 
        }
	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;

