3 # LDAP to unix password sync script for samba-tng
4 # originally by Jody Haynes <Jody.Haynes@isunnetworks.com>
5 # 2000/12/12 milos@interactivesi.com
6 # modified for use with MD5 passwords
7 # 2000/12/16 mami@arena.sci.univr.it
8 # modified to change lmpassword and ntpassword for samba
9 # 2001/01/05 mami@arena.sci.univr.it
10 # modified for being also a /bin/passwd replacement
11 # 2001/01/29 mami@arena.sci.univr.it
12 # now there are two small programs: ldapchpasswd to
13 # change password from unix and ldapsync.pl to sync
14 # from NT/2000. ldapchpasswd do not need clear password.
15 # 2001/01/31 mami@arena.sci.univr.it
16 # add server parameter to ldap commands
17 # 2001/06/20 mami@arena.sci.univr.it
18 # add pwdlastset and shadowlastchange update
20 $basedn = "ou=Students,dc=univr, dc=it";
21 $binddn = "uid=root,dc=univr,dc=it";
23 $server = "my_server";
25 foreach $arg (@ARGV) {
27 die "Only root can specify parameters\n";
29 if ( ($arg eq '-?') || ($arg eq '--help') ) {
30 print "Usage: $0 [-o] [username]\n";
31 print " -o, --without-old-password do not ask for old password (root only)\n";
32 print " -?, --help show this help message\n";
34 } elsif ( ($arg eq '-o') || ($arg eq '--without-old-password') ) {
36 } elsif (substr($arg,0) ne '-') {
38 if (!defined(getpwnam($user))) {
39 die "$0: Unknown user name '$user'\n"; ;
45 if (!defined($user)) {
54 print "LDAP password for root DN: ";
55 chomp($passwd=<STDIN
>);
58 # Find dn for user $user binding as root's dn
59 chomp($dn=`ldapsearch -h '$server' -b '$basedn' -s '$scope' -D '$binddn' -w '$passwd' '(uid=$user)'|head -1`);
60 if ( ($dn eq '') || ($passwd eq '') ) {
61 print "Wrong LDAP password for root DN!\n";
65 if (!defined($oldpass)) {
67 print "Old password for user $user: ";
68 chomp($oldpass=<STDIN
>);
73 chomp($path_to_uid=`ldapsearch -h '$server' -b '$basedn' -s '$scope' '(uid=$user)'|head -1`);
74 # Find old password for user $user binding as self
75 chomp($dn=`ldapsearch -h '$server' -b '$basedn' -s '$scope' -D '$path_to_uid' -w '$oldpass' '(uid=$user)'|head -1`);
77 if ( ($dn eq '') || ($oldpass eq '') ) {
78 print "Wrong password for user $user!\n";
85 print "New password for user $user: ";
91 print "Retype new password for user $user: ";
92 chomp($pass2=<STDIN
>);
96 if ( ($pass ne $pass2) || (length($pass)<1) ) {
97 die "Wrong password!\n";
100 $random = join '', ('.', '/', 0..9, 'A'..'Z', 'a'..'z')[rand 64, rand 64, rand 64, rand 64, rand 64, rand 64, rand 64, rand 64];
101 $bsalt = "\$1\$"; $esalt = "\$";
102 $modsalt = $bsalt.$random.$esalt;
103 $password = crypt($pass, $modsalt);
105 # LanManager and NT clear text passwords
106 $ntpwd = `/usr/local/sbin/mkntpwd '$pass'`;
107 chomp($lmpassword = substr($ntpwd, 0, index($ntpwd, ':')));
108 chomp($ntpassword = substr($ntpwd, index($ntpwd, ':')+1));
110 #$FILE="|/usr/bin/ldapmodify -h '$server' -D '$binddn' -w $passwd";
112 $FILE="|/usr/bin/ldapmodify -h '$server' -D '$dn' -w '$oldpass'";
114 $FILE="|/usr/bin/ldapmodify -h '$server' -D '$binddn' -w '$passwd'";
118 $shadowlastchange=int(time/24/3600);
119 $pwdlastset=sprintf('%x',time);
126 replace: userPassword
127 userPassword: {crypt}$password
131 lmpassword: $lmpassword
135 ntpassword: $ntpassword
138 replace: shadowlastchange
139 shadowlastchange: $shadowlastchange
143 pwdlastset: $pwdlastset