import2_smbpasswd.pl [plain text]
use Net::LDAP;
$DN="dc=samba,dc=my-domain,dc=com";
$ROOTDN="cn=Manager,dc=my-domain,dc=com";
$rootpw = "secret";
$LDAPSERVER="localhost";
$ldap = Net::LDAP->new($LDAPSERVER) or die "Unable to connect to LDAP server $LDAPSERVER";
$mesg = $ldap->bind($ROOTDN, password => $rootpw);
while ( $string = <STDIN> ) {
chop ($string);
@smbentry = split (/:/, $string);
@getpwinfo = getpwnam($smbentry[0]);
if (! @getpwinfo ) {
print STDERR "$smbentry[0] does not have a system account... skipping\n";
next;
}
$result = $ldap->search ( base => "$DN",
scope => "sub",
filter => "(&(|(objectclass=posixAccount)(objectclass=smbPasswordEntry))(uid=$smbentry[0]))"
);
if ( $result->count == 0 ) {
$entry = $ldap->add ( dn => "uid=$smbentry[0]\,$DN",
attrs => [
uid => $smbentry[0],
uidNumber => @getpwinfo[2],
lmPassword => $smbentry[2],
ntPassword => $smbentry[3],
acctFlags => $smbentry[4],
pwdLastSet => substr($smbentry[5],4),
objectclass => [ 'top', 'smbPasswordEntry' ]
]
);
print "Adding [uid=" . $smbentry[0] . "," . $DN . "]\n";
} elsif ($result->count == 1) {
$entry = $result->shift_entry;
print "Updating [" . $entry->dn . "]\n";
@values = $entry->get_value( "objectclass" );
$flag = 1;
foreach $item (@values) {
if ( lc($item) eq "smbpasswordentry" ) {
print $item . "\n";
$flag = 0;
}
}
if ( $flag ) {
$entry->add(objectclass => "smbPasswordEntry");
}
$entry->replace(lmPassword => $smbentry[2],
ntPassword => $smbentry[3],
acctFlags => $smbentry[4],
pwdLastSet => substr($smbentry[5],4)
);
$updatemesg = $entry->update($ldap);
if ( $updatemesg->code ) {
print "Error updating $smbentry[0]!\n";
}
} else {
print STDERR "LDAP search returned more than one entry for $smbentry[0]... skipping!\n";
next;
}
}
$ldap->unbind();
exit 0;