1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
| #!/usr/bin/perl
use Crypt::PasswdMD5;
use Digest::MD5 qw(md5 md5_hex md5_base64);
use Net::LDAP;
use Net::LDAP::Util qw/ldap_error_text/;
###################################### SETTINGS #################################
my $LDAPServer = "ton serveur;
my $LDAPRootDN = "cn=admin,dc=domaine,dc=fr";
my $LDAPRootPassword = "mot de passe";
my $LDAPVersion = 3;
my $LDAPBaseDN = 'dc=domaine,dc=fr';
my $LDAPFilter = '(&(objectClass=shadowAccount)(objectClass=sambaSamAccount))';
my @LDAPAttrs = ['uidNumber'];
my $template_ldif="template.ldif";
my $liste_password="maliste.csv";
my $ExportFile = "import_adduser.ldif";
####################################### DONT TOUCHE AFTER ########################
if($#ARGV < 2)
{
print "\tUsage: perl adduser.pl <PRENOM> <NOM> <DOMAINE>\n";
print "\te.g: perl adduser.pl marine leblanc tondomaine\n";
exit 0;
}
else {
open(TEMPLATE,$template_ldif);
my $template="";
while(<TEMPLATE>) {
# chomp($_);
$template.=$_;
}
$template.="\n";
close(TEMPLATE);
#### LDAP GET uidNumber ####
$ldap = Net::LDAP->new ( $LDAPServer ) or die "$@";
$mesg = $ldap->bind ( $LDAPRootDN, password => $LDAPRootPassword, version => $LDAPVersion );
$mesg->code && die $mesg->error;
my $mesg = $ldap->search(base => $LDAPBaseDN, scope => 'sub' , filter => $LDAPFilter);
$mesg->code && die $mesg->error;
my $Count= $mesg->count - 1;
if($Count>0) {
my $entry=$mesg->entry($Count);
my $uidNumber=$entry->get_value('uidNumber') + 1;
#### GET LOGIN / PASSWORD / EMAIL
my $firstname=$ARGV[0];
my $lastname=$ARGV[1];
my $domain=$ARGV[2];
my $login=$firstname . "." . $lastname;
my $clear_password=`pwgen -n 8 1`;
lc(chomp($clear_password));
my $email=$firstname . "." . $lastname . "\@tondomaine";
#### ADD TO liste_password
open(MALISTE,">>$liste_password");
print MALISTE $login . ":" .$clear_password . ":" . $email . ":" . $uidNumber . "\n";
close(MALISTE);
#### COMPLETE TEMPLATE
my $temp_template=$template;
$firstname = uc($firstname);
$lastname = uc($lastname);
$temp_template=~ s/XXX_FIRSTNAME_XXX/$firstname/g;
$temp_template=~ s/XXX_LASTNAME_XXX/$lastname/g;
$temp_template=~ s/XXX_DOMAIN_XXX/$domain/g;
$temp_template=~ s/XXX_USER_XXX/$login/g;
$temp_template=~ s/XXX_MAIL_XXX/$email/g;
$temp_template=~ s/XXX_UID_XXX/$uidNumber/g;
$encrypted_password = md5_base64($clear_password) . "==";
$temp_template=~ s/XXX_PASSWORD_XXX/$encrypted_password/g;
$ntpasswd = `mkntpwd -N $clear_password`;
$lmpasswd = `mkntpwd -L $clear_password`;
chomp($ntpasswd);
chomp($lmpasswd);
$temp_template =~ s/XXX_NTPASSWD_XXX/$ntpasswd/g;
$temp_template =~ s/XXX_LMPASSWD_XXX/$lmpasswd/g;
$sambarid = $uidNumber*2+1000;
$temp_template =~ s/XXX_SAMBARID_XXX/$sambarid/g;
$temp_template =~ s/XXX_PPTPPASSWORD_XXX/$clear_password/g;
#### WRITE LDIF
open(FINAL,">$ExportFile");
print FINAL $temp_template;
close(FINAL);
### INSERT LDIF TO LDAP
`ldapadd -x -h $LDAPServer -v -D $LDAPRootDN -w $LDAPRootPassword -f $ExportFile`;
### PRINT PASSWORD AND USER
print "Login: $login\n";
print "Password: $clear_password\n";
print "Email: $email\n";
}
$ldap->unbind();
} |