Handle hostnames with upper-case letters
[webmin.git] / cluster-passwd / change-passwd.pl
1 #!/usr/local/bin/perl
2 # change-passwd.pl
3 # Changes a user's password on all cluster servers. 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 './cluster-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("cluster-useradmin") != 1) {
22         &errordie("Cluster Users and Groups module is not available");
23         }
24
25 # Find the user
26 @hosts = &cluster_useradmin::list_useradmin_hosts();
27 @ulist = &get_all_users(\@hosts);
28 ($user) = grep { $_->{'user'} eq $ARGV[0] } @ulist;
29 $user || &errordie("User $ARGV[0] does not exist");
30
31 $| = 1;
32 if ($askold) {
33         # Ask for the old password
34         print "(current) UNIX password: ";
35         $old = <STDIN>;
36         $old =~ s/\r|\n//g;
37         &unix_crypt($old, $user->{'pass'}) eq $user->{'pass'} ||
38                 &errordie("Old password is incorrect");
39         }
40
41 # Ask for password
42 print "New password: ";
43 $pass = <STDIN>;
44 $pass =~ s/\r|\n//g;
45 print "Retype new password: ";
46 $again = <STDIN>;
47 $again =~ s/\r|\n//g;
48 $pass eq $again || &errordie("Passwords don't match");
49
50 # Check password sanity
51 $err = &useradmin::check_password_restrictions($pass, $ARGV[0]);
52 &errordie($err) if ($err);
53
54 # Do it on all servers
55 &modify_on_hosts(\@hosts, $user->{'user'}, $pass, 1, \&print_func);
56
57 # All done
58 exit(0);
59
60 sub errordie
61 {
62 print STDERR @_,"\n";
63 exit(1);
64 }
65
66 # print_func(mode, message)
67 sub print_func
68 {
69 if ($_[0] == -1) {
70         print "$_[1]\n\n";
71         $indent = "    ";
72         }
73 elsif ($_[0] == -2) {
74         print "$indent$_[1]\n";
75         }
76 elsif ($_[0] == -3) {
77         print "$indent$_[1]\n\n";
78         }
79 elsif ($_[0] == -4) {
80         $indent = "";
81         print "\n";
82         }
83 elsif ($_[0] > 0) {
84         print "$indent$_[1]\n\n";
85         $indent = "";
86         }
87 }