Skip to content

Instantly share code, notes, and snippets.

@jatkins
Last active December 11, 2015 07:29
Show Gist options
  • Save jatkins/4567126 to your computer and use it in GitHub Desktop.
Save jatkins/4567126 to your computer and use it in GitHub Desktop.
A quick and dirty perl script I wrote about 4 years ago to batch load new users into the LDAP system while I worked in the Wise office of NASA DEVELOP. It does contain some super "secret" host names and passwords, but the LDAP (nor any part) of my system exists any more.
#!/usr/bin/perl
use DateTime;
use Net::LDAP;
$DN="ou=People,dc=wisedevelop,dc=org";
$HOST="mike.wisedevelop.org";
sub generate_random_string
{
my $length_of_randomstring=shift;# the length of
# the random string to generate
my @chars=('a'..'z','A'..'Z','0'..'9','_');
my $random_string;
foreach (1..$length_of_randomstring)
{
# rand @chars will generate a random
# number between 0 and scalar @chars
$random_string.=$chars[rand @chars];
}
return $random_string;
}
die "Supply Valid CSV File as Argument!\n"
unless (defined($ARGV[0]) && -e $ARGV[0]);
open(DAT, $ARGV[0]);
@data=<DAT>;
close(DAT);
open(UID, ".lastuid");
@uid=<UID>;
$uidNumber=@uid[0];
close(UID);
chomp($uidNumber);
$ldap = Net::LDAP->new( $HOST ) or die "$@";
$mesg = $ldap->bind('cn=admin,dc=wisedevelop,dc=org', password => 'CatwRBkM2JX3');
open(UNDO, '>undo.ldif');
foreach $user (@data) {
chomp($user);
($f_name,$m_inital,$l_name,$phone,$email,$gidNumber,$expire)=split(/,/,$user);
($year,$month,$day)=split(/\//,$expire);
$uid=substr($f_name, 0, 1) . $l_name;
$uid =~ tr/[A-Z]/[a-z]/;
$name = $f_name . " " . $m_inital . ". " . $l_name;
$initals = substr($f_name, 0, 1) . $m_inital . substr($l_name, 0, 1);
$date = DateTime->new( year=>$year, month=>$month, day=>$day );
$epoch = $date->epoch;
$passwd=&generate_random_string(11);
$result = $ldap->add( "uid=$uid,$DN",
attr => [
'objectclass' => ['userAccount',
'posixAccount',
'shadowAccount'
],
'uid' => $uid,
'sn' => $l_name,
'givenName' => $f_name,
'cn' => $name,
'displayName' => $name,
'uidNumber' => $uidNumber,
'gidNumber' => $gidNumber,
'gecos' => $name,
'loginShell' => '/bin/fasle',
'homeDirectory' => "/home/$uid",
'shadowExpire' => $epoch,
'shadowFlag' => '0',
'shadowWarning' => '7',
'shadowMin' => '8',
'shadowMax' => '999999',
'shadowLastChange' => '10877',
'mail' => $email,
'l' => 'Wise',
'mobile' => $phone,
'initials' => $initals,
'manager' => 'cn=jatkins,ou=People,dc=wisedevelop,dc=org'
]
);
$result->code && warn "failed to add $uid: ", $result->error ;
system("(echo $passwd; echo $passwd) | smbpasswd -as $uid");
print "\n\n$name - $uid | $passwd - $uidNumber\n\n\n";
print UNDO "dn: uid=$uid,$DN\n";
print UNDO "changetype: delete\n";
print UNDO "\n\n";
open(LDIF, ">.updateSID");
print LDIF "dn: uid=$uid,$DN\n";
print LDIF "changetype: modify\n";
print LDIF "add: sambaPrimaryGroupSID\n";
print LDIF "sambaPrimaryGroupSID: S-1-5-21-639762182-2773858593-970662190-513\n";
close(LDIF);
system("ldapmodify -D cn=admin,dc=wisedevelop,dc=org -w CatwRBkM2JX3 -f .updateSID -x -h mike.wisedevelop.org");
system("mkdir /home/$uid");
system("cp /etc/skel/.[A-Za-z0-9]* /home/$uid/");
system("cp -r /etc/skel/* /home/$uid/");
system("chwon -R $uid:intern /home/$uid");
$uidNumber=$uidNumber+1;
}
close(UNDO);
system("echo \"$uidNumber\" > .lastuid");
$mesg = $ldap->unbind;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment