This commit was manufactured by cvs2svn to create branch 'SAMBA_3_0'.
[samba.git] / examples / LDAP / export_smbpasswd.pl
1 #!/usr/bin/perl
2 ##
3 ## Example script to export ldap entries into an smbpasswd file format
4 ## 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 values to whatever you need 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
26 $conn = new Mozilla::LDAP::Conn ("$LDAPSERVER", "389", $ROOTDN, $rootpw );
27 die "Unable to connect to LDAP server $LDAPSERVER" unless $conn;
28
29 print "##\n";
30 print "## Autogenerated smbpasswd file via ldapsearch\n";
31 print "## from $LDAPSERVER ($DN)\n";
32 print "##\n";
33
34 ## scheck for the existence of the posixAccount first
35 $result = $conn->search ("$DN", "sub", "(objectclass=smbPasswordEntry)");
36         
37         
38 ## loop over the entries we found
39 while ($result) {
40         
41         @uid         = $result->getValue("uid"); 
42         @uidNumber   = $result->getValue("uidNumber"); 
43         @lm_pw       = $result->getValue("lmpassword"); 
44         @nt_pw       = $result->getValue("ntpassword"); 
45         @acct        = $result->getValue("acctFlags"); 
46         @pwdLastSet  = $result->getValue("pwdLastSet"); 
47         
48         if (($#uid+1) && ($#uidNumber+1)) {
49         
50                 $lm_pw[0]      = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" if (! ($#lm_pw+1));
51                 $nt_pw[0]      = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" if (! ($#nt_pw+1));
52                 $acct[0]       = "[DU         ]" if (! ($#acct+1));
53                 $pwdLastSet[0] = "FFFFFFFF" if (! ($#pwdLastSet+1));
54                 
55                 print "$uid[0]:$uidNumber[0]:$lm_pw[0]:$nt_pw[0]:$acct[0]:LCT-$pwdLastSet[0]\n";
56         }
57         
58         $result = $conn->nextEntry();           
59         
60 }
61
62 $conn->close();
63 exit 0;