adding files from 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 ## ported to Net::LDAP by dkrovich@slackworks.com
9
10 use Net::LDAP;
11
12 ######################################################
13 ## Set these values to whatever you need for your site
14 ##
15
16 $DN="dc=samba,dc=my-domain,dc=com";
17 $ROOTDN="cn=Manager,dc=my-domain,dc=com";
18 $rootpw = "secret";
19 $LDAPSERVER="localhost";
20
21 ##
22 ## end local site variables
23 ######################################################
24
25 $ldap = Net::LDAP->new($LDAPSERVER) or die "Unable to connect to LDAP server $LDAPSERVER";
26
27 print "##\n";
28 print "## Autogenerated smbpasswd file via ldapsearch\n";
29 print "## from $LDAPSERVER ($DN)\n";
30 print "##\n";
31
32 ## scheck for the existence of the posixAccount first
33 $result = $ldap->search ( base => "$DN",
34                           scope => "sub",
35                           filter => "(objectclass=sambaAccount)"
36                         );
37
38
39
40 ## loop over the entries we found
41 while ( $entry = $result->shift_entry() ) {
42
43         @uid         = $entry->get_value("uid");
44         @uidNumber   = $entry->get_value("uidNumber");
45         @lm_pw       = $entry->get_value("lmpassword");
46         @nt_pw       = $entry->get_value("ntpassword");
47         @acct        = $entry->get_value("acctFlags");
48         @pwdLastSet  = $entry->get_value("pwdLastSet");
49
50         if (($#uid+1) && ($#uidNumber+1)) {
51
52                 $lm_pw[0]      = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" if (! ($#lm_pw+1));
53                 $nt_pw[0]      = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" if (! ($#nt_pw+1));
54                 $acct[0]       = "[DU         ]" if (! ($#acct+1));
55                 $pwdLastSet[0] = "FFFFFFFF" if (! ($#pwdLastSet+1));
56
57                 print "$uid[0]:$uidNumber[0]:$lm_pw[0]:$nt_pw[0]:$acct[0]:LCT-$pwdLastSet[0]\n";
58         }
59
60 }
61
62 $ldap->unbind();
63 exit 0;
64