This commit was manufactured by cvs2svn to create branch 'SAMBA_3_0'.
[sfrench/samba-autobuild/.git] / examples / LDAP / ldapsync.pl
1 #!/usr/bin/perl -w
2
3 # LDAP to unix password sync script for samba-tng
4 # originally by Jody Haynes <Jody.Haynes@isunnetworks.com>
5 # 12/12/2000    milos@interactivesi.com
6 #               modified for use with MD5 passwords
7 # 12/16/2000    mami@arena.sci.univr.it
8 #               modified to change lmpassword and ntpassword for samba
9 # 05/01/2001    mami@arena.sci.univr.it
10 #               modified for being also a /bin/passwd replacement
11
12 $basedn = "ou=Students,dc=univr, dc=it";
13 $binddn = "uid=root,dc=univr,dc=it";
14 $scope = "sub";
15 $passwd = "mysecret";
16
17 foreach $arg (@ARGV) {
18         if ($< != 0) {
19                 die "Only root can specify parameters\n";
20         } else {
21                 if ( ($arg eq '-?') || ($arg eq '--help') ) {
22                         print "Usage: $0 [-o] [username]\n";
23                         print "  -o, --without-old-password     do not ask for old password (root only)\n";
24                         print "  -?, --help                     show this help message\n";
25                         exit (-1);
26                 } elsif ( ($arg eq '-o') || ($arg eq '--without-old-password') ) {
27                         $oldpass = 1;
28                 } elsif (substr($arg,0) ne '-')  {
29                         $user = $arg;
30                         if (!defined(getpwnam($user))) {
31                                 die "$0: Unknown user name '$user'\n";  ;
32                         }
33                 }
34         }
35 }
36
37 if (!defined($user)) {
38         $user=$ENV{"USER"};
39 }
40
41 if (!defined($oldpass)) {
42         system "stty -echo";
43         print "Old password for user $user: ";
44         chomp($oldpass=<STDIN>);
45         print "\n";
46         system "stty echo";
47
48         $ntpwd = `/usr/local/sbin/smbencrypt '$oldpass'`;
49         $lmpassword = substr($ntpwd, 0, index($ntpwd, ':')); chomp $lmpassword;
50         $ntpassword = substr($ntpwd, index($ntpwd, ':')+1); chomp $ntpassword;
51
52         # Find dn for user $user (maybe check unix password too?)
53         $dn=`ldapsearch -b '$basedn' -s '$scope' '(&(uid=$user)(lmpassword=$lmpassword)(ntpassword=$ntpassword))'|head -1`;
54         chomp $dn;
55
56         if ($dn eq '') {
57                 print "Wrong password for user $user!\n";
58                 exit (-1);
59         }
60 } else {
61         # Find dn for user $user
62         $dn=`ldapsearch -b '$basedn' -s '$scope' '(uid=$user)'|head -1`;
63         chomp $dn;
64 }
65
66 system "stty -echo";
67 print "New password for user $user: ";
68 chomp($pass=<STDIN>);
69 print "\n";
70 system "stty echo";
71
72 system "stty -echo";
73 print "Retype new password for user $user: ";
74 chomp($pass2=<STDIN>);
75 print "\n";
76 system "stty echo";
77
78 if ($pass ne $pass2) {
79         die "Wrong password!\n";
80 } else {
81 # MD5 password
82 $random = join '', ('.', '/', 0..9, 'A'..'Z', 'a'..'z')[rand 64, rand 64, rand 64, rand 64, rand 64, rand 64, rand 64, rand 64];
83 $bsalt = "\$1\$"; $esalt = "\$";
84 $modsalt = $bsalt.$random.$esalt;
85 $password = crypt($pass, $modsalt);
86
87 # LanManager and NT clear text passwords
88 $ntpwd = `/usr/local/sbin/smbencrypt '$pass'`;
89 chomp($lmpassword = substr($ntpwd, 0, index($ntpwd, ':')));
90 chomp($ntpassword = substr($ntpwd, index($ntpwd, ':')+1));
91
92 $FILE="|/usr/bin/ldapmodify -D '$binddn' -w $passwd";
93
94 open FILE or die;
95
96 print FILE <<EOF;
97 dn: $dn
98 changetype: modify
99 replace: userPassword
100 userPassword: {crypt}$password
101 -
102 changetype: modify
103 replace: lmpassword
104 lmpassword: $lmpassword
105 -
106 changetype: modify
107 replace: ntpassword
108 ntpassword: $ntpassword
109 -
110
111 EOF
112 close FILE;
113
114 }
115
116 exit 0;
117