3 ## Example script of how you could import a smbpasswd file into an LDAP
4 ## directory using the Mozilla PerLDAP module.
6 ## writen by jerry@samba.org
8 ## ported to Net::LDAP by dkrovich@slackworks.com
12 #################################################
13 ## set these to a value appropriate for your site
16 $DN="ou=people,dc=plainjoe,dc=org";
17 $ROOTDN="cn=Manager,dc=plainjoe,dc=org";
18 # If you use perl special character in your
19 # rootpw, escape them:
20 # $rootpw = "secr\@t" instead of $rootpw = "secr@t"
25 ## end local site variables
26 #################################################
28 $ldap = Net
::LDAP
->new($LDAPSERVER) or die "Unable to connect to LDAP server $LDAPSERVER";
30 ## Bind as $ROOTDN so you can do updates
31 $mesg = $ldap->bind($ROOTDN, password
=> $rootpw);
32 $mesg->error() if $mesg->code();
34 while ( $string = <STDIN
> ) {
37 ## Get the account info from the smbpasswd file
38 @smbentry = split (/:/, $string);
40 ## Check for the existence of a system account
41 @getpwinfo = getpwnam($smbentry[0]);
43 print STDERR
"**$smbentry[0] does not have a system account... \n";
46 ## Calculate RID = uid*2 +1000
47 $rid=@getpwinfo[2]*2+1000;
49 ## check and see if account info already exists in LDAP.
50 $result = $ldap->search ( base
=> "$DN",
52 filter
=> "(uid=$smbentry[0])"
55 ## If no LDAP entry exists, create one.
56 if ( $result->count == 0 ) {
57 $new_entry = Net
::LDAP
::Entry
->new();
58 $new_entry->add( dn
=> "uid=$smbentry[0],$DN",
61 lmPassword
=> $smbentry[2],
62 ntPassword
=> $smbentry[3],
63 acctFlags
=> $smbentry[4],
65 pwdLastSet
=> hex(substr($smbentry[5],4)),
66 objectclass
=> 'sambaAccount' );
68 $result = $ldap->add( $new_entry );
69 $result->error() if $result->code();
70 print "Adding [uid=" . $smbentry[0] . "," . $DN . "]\n";
72 ## Otherwise, supplement/update the existing entry.
74 elsif ($result->count == 1)
76 # Put the search results into an entry object
77 $entry = $result->entry(0);
79 print "Updating [" . $entry->dn . "]\n";
81 ## Add the objectclass: sambaAccount attribute if it's not there
82 @values = $entry->get_value( "objectclass" );
84 foreach $item (@values) {
86 if ( "$item" eq "sambaAccount" ) {
91 ## Adding sambaAccount objectclass requires adding at least rid:
92 ## uid attribute already exists we know since we searched on it
93 $entry->add(objectclass
=> "sambaAccount",
97 ## Set the other attribute values
98 $entry->replace(rid
=> $rid,
99 lmPassword
=> $smbentry[2],
100 ntPassword
=> $smbentry[3],
101 acctFlags
=> $smbentry[4],
102 pwdLastSet
=> hex(substr($smbentry[5],4)));
104 ## Apply changes to the LDAP server
105 $updatemesg = $entry->update($ldap);
106 $updatemesg->error() if $updatemesg->code();
108 ## If we get here, the LDAP search returned more than one value
109 ## which shouldn't happen under normal circumstances.
111 print STDERR
"LDAP search returned more than one entry for $smbentry[0]... skipping!\n";