Initial checkin of Webmin
[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 restart miniserv, if running
54 if (open(PID, $config{'pidfile'})) {
55         <PID> =~ /(\d+)/; $pid = $1;
56         close(PID);
57         kill('HUP', $pid);
58         }
59
60 sub usage
61 {
62 print STDERR <<EOF;
63 usage: changepass.pl <config-dir> <login> <password>
64
65 This program allows you to change the password of a user in the Webmin
66 password file. For example, to change the password of the admin user
67 to foo, you would run:
68         changepass.pl /etc/webmin admin foo
69 This assumes that /etc/webmin is the Webmin configuration directory.
70 EOF
71 exit 1;
72 }
73