Handle hostnames with upper-case letters
[webmin.git] / passwd / change-passwd.pl
1 #!/usr/local/bin/perl
2 # change-passwd.pl
3 # Changes a user's password using the Users and Groups module. Also changes
4 # the password in other modules.
5
6 $no_acl_check++;
7 $ENV{'WEBMIN_CONFIG'} ||= "/etc/webmin";
8 $ENV{'WEBMIN_VAR'} ||= "/var/webmin";
9 if ($0 =~ /^(.*\/)[^\/]+$/) {
10         chdir($1);
11         }
12 chop($pwd = `pwd`);
13 $0 = "$pwd/change-passwd.pl";
14 do './passwd-lib.pl';
15
16 if ($ARGV[0] eq "--old" || $ARGV[0] eq "-old") {
17         $askold = 1;
18         shift(@ARGV);
19         }
20 @ARGV == 1 || &errordie("usage: change-passwd.pl [-old] <username>");
21 if (&foreign_installed("useradmin") != 1) {
22         &errordie("Users and Groups module is not supported on this OS");
23         }
24
25 # Find the user, either in local password file or LDAP
26 $user = &find_user($ARGV[0]);
27 $user || &errordie("User $ARGV[0] does not exist");
28
29 $| = 1;
30 if ($askold) {
31         # Ask for the old password
32         print "(current) UNIX password: ";
33         $old = <STDIN>;
34         $old =~ s/\r|\n//g;
35         &unix_crypt($old, $user->{'pass'}) eq $user->{'pass'} ||
36                 &errordie("Old password is incorrect");
37         }
38
39 # Ask for password
40 print "New password: ";
41 $pass = <STDIN>;
42 $pass =~ s/\r|\n//g;
43 print "Retype new password: ";
44 $again = <STDIN>;
45 $again =~ s/\r|\n//g;
46 $pass eq $again || &errordie("Passwords don't match");
47
48 # Check password sanity
49 $err = &useradmin::check_password_restrictions($pass, $ARGV[0]);
50 &errordie($err) if ($err);
51
52 # Do the change!
53 &change_password($user, $pass, 1);
54
55 # All done
56 exit(0);
57
58 sub errordie
59 {
60 print STDERR @_,"\n";
61 exit(1);
62 }
63