Handle hostnames with upper-case letters
[webmin.git] / changepass.pl
1 #!/usr/local/bin/perl
2 # changepass.pl
3 # Script for the user to change their webmin password
4
5 # Check command line arguments
6 usage() if (@ARGV != 3);
7 ($config, $user, $pass) = @ARGV;
8 if (!-d $config) {
9         print STDERR "The config directory $config does not exist\n";
10         exit 2;
11         }
12 if (!open(CONF, "$config/miniserv.conf")) {
13         print STDERR "Failed to open $config/miniserv.conf : $!\n";
14         print STDERR "Maybe $config is not the Webmin config directory.\n";
15         exit 3;
16         }
17 while(<CONF>) {
18         if (/^([^=]+)=(\S+)/) { $config{$1} = $2; }
19         }
20 close(CONF);
21
22 # Update the users file
23 if (!open(USERS, $config{'userfile'})) {
24         print STDERR "Failed to open Webmin users file $config{'userfile'} : $!\n";
25         exit 4;
26         }
27 while(<USERS>) {
28         s/\r|\n//g;
29         local @user = split(/:/, $_);
30         if (@user) {
31                 $users{$user[0]} = \@user;
32                 push(@users, $user[0]);
33                 }
34         }
35 close(USERS);
36 if (!defined($users{$user})) {
37         print STDERR "The Webmin user $user does not exist\n";
38         print STDERR "The users on your system are: ",join(" ", @users),"\n";
39         exit 5;
40         }
41 $salt = substr(time(), 0, 2);
42 $users{$user}->[1] = crypt($pass, $salt);
43 if (!open(USERS, "> $config{'userfile'}")) {
44         print STDERR "Failed to open Webmin users file $config{'userfile'} : $!\n";
45         exit 6;
46         }
47 foreach $v (values %users) {
48         print USERS join(":", @$v),"\n";
49         }
50 close(USERS);
51 print "Updated password of Webmin user $user\n";
52
53 # Send a signal to have miniserv reload it's config
54 if (open(PID, $config{'pidfile'})) {
55         $pid = <PID>;
56         $pid =~ s/\r|\n//;
57         close(PID);
58         if (!$pid) {
59                 print STDERR "Webmin is not running - cannot refresh configuration\n";
60                 }
61         elsif (!kill('USR1', $pid)) {
62                 print STDERR "Failed to signal process $pid - cannot refresh configuration\n";
63                 }
64         }
65 else {
66         print STDERR "Webmin is not running - cannot refresh configuration\n";
67         }
68
69 sub usage
70 {
71 print STDERR <<EOF;
72 usage: changepass.pl <config-dir> <login> <password>
73
74 This program allows you to change the password of a user in the Webmin
75 password file. For example, to change the password of the admin user
76 to foo, you would run:
77         changepass.pl /etc/webmin admin foo
78 This assumes that /etc/webmin is the Webmin configuration directory.
79 EOF
80 exit 1;
81 }
82