sync with 2.2
[samba.git] / examples / LDAP / import_smbpasswd.pl
1 #!/usr/bin/perl
2 ##
3 ## Example script of how you could import and smbpasswd file into an LDAP
4 ## directory using the Mozilla PerLDAP module.
5 ##
6 ## writen by jerry@samba.org
7 ##
8
9 use Mozilla::LDAP::Conn;
10 use Mozilla::LDAP::Entry;
11
12 #################################################
13 ## set these to a value appropriate for your site
14 ##
15
16 $DN="ou=people,dc=plainjoe,dc=org";
17 $ROOTDN="cn=Manager,dc=plainjoe,dc=org";
18 $rootpw = "secret";
19 $LDAPSERVER="localhost";
20
21 ##
22 ## end local site variables
23 #################################################
24
25 $conn = new Mozilla::LDAP::Conn ("$LDAPSERVER", "389", $ROOTDN, $rootpw );
26 die "Unable to connect to LDAP server $LDAPSERVER" unless $conn;
27
28
29 while ( $string = <STDIN> ) {
30         chop ($string);
31
32         ## get the account information
33         @smbentry = split (/:/, $string);
34
35         ## check for the existence of the posixAccount first
36
37         ## FIXME!!  Should do a getownam() and let the NSS modules lookup the account
38         ## This way you can have a UNIX account in /etc/passwd and the smbpasswd i
39         ## entry in LDAP.
40         $result = $conn->search ("$DN", "sub", "(&(uid=$smbentry[0])(objectclass=posixAccount))");
41         if ( ! $result ) {
42                 print STDERR "uid=$smbentry[0] does not have a posixAccount entry in the directory!\n";
43                 next;
44         }
45
46         print "Updating [" . $result->getDN() . "]\n";
47
48         ## Do we need to add the 'objectclass: smbPasswordEntry' attribute?
49         if (! $result->hasValue("objectclass", "smbPasswordEntry")) {
50                 $result->addValue("objectclass", "smbPasswordEntry");
51         }
52         
53         ## Set other attribute values
54         $result->setValues ("lmPassword", $smbentry[2]);
55         $result->setValues ("ntPassword", $smbentry[3]);
56         $result->setValues ("acctFlags",  $smbentry[4]);
57         $result->setValues ("pwdLastSet", substr($smbentry[5],4));
58
59         if (! $conn->update($result)) {
60                 print "Error updating!\n";
61         }
62 }
63
64 $conn->close();
65 exit 0;