Handle hostnames with upper-case letters
[webmin.git] / passwd / save_passwd.cgi
1 #!/usr/local/bin/perl
2 # save_passwd.cgi
3 # Change a user's password
4
5 require './passwd-lib.pl';
6 &error_setup($text{'passwd_err'});
7 &ReadParse();
8
9 if ($config{'passwd_cmd'}) {
10         # Call the passwd program to do the change
11         @user = getpwnam($in{'user'});
12         @user || &error($text{'passwd_euser'});
13         &can_edit_passwd(\@user) || &error($text{'passwd_ecannot'});
14         if ($access{'repeat'}) {
15                 $in{'new'} eq $in{'repeat'} || &error($text{'passwd_erepeat'});
16                 }
17         &foreign_require("proc", "proc-lib.pl");
18         if ($access{'old'} == 1 ||
19             $access{'old'} == 2 && $in{'user'} ne $remote_user) {
20                 @cmd = ( $config{'passwd_cmd'}, $user[2], $user[3] );
21                 }
22         else {
23                 @cmd = ( "$config{'passwd_cmd'} '$in{'user'}'" );
24                 }
25         &additional_log('exec', undef, $cmd[0]);
26         local ($fh, $fpid) = &foreign_call("proc", "pty_process_exec", @cmd);
27         while(1) {
28                 local $rv = &wait_for($fh, '(new|re-enter).*:',
29                                            '(old|current|login).*:',
30                                            'pick a password');
31                 $out .= $wait_for_input;
32                 sleep(1);
33                 if ($rv == 0) {
34                         syswrite($fh, $in{'new'}."\n", length($in{'new'})+1);
35                         }
36                 elsif ($rv == 1) {
37                         syswrite($fh, $in{'old'}."\n", length($in{'old'})+1);
38                         }
39                 elsif ($rv == 2) {
40                         syswrite($fh, "1\n", 2);
41                         }
42                 else {
43                         last;
44                         }
45                 last if (++$count > 10);
46                 }
47         close($fh);
48         waitpid($fpid, 0);
49         &error(&text('passwd_ecmd', "<tt>$config{'passwd_cmd'}</tt>", "<pre>$out</pre>")) if ($? || $count > 10);
50         &webmin_log("passwd", undef, $in{'user'});
51         }
52 else {
53         # Update the config files directly via the useradmin module
54         &foreign_require("useradmin", "user-lib.pl");
55
56         # Find the user, either in local password file or LDAP
57         $user = &find_user($in{'user'});
58
59         if ($user) {
60                 # Validate inputs
61                 if ($access{'old'} == 1 ||
62                     $access{'old'} == 2 && $user->{'user'} ne $remote_user) {
63                         &useradmin::validate_password(
64                             $in{'old'}, $user->{'pass'}) ||
65                                 &error($text{'passwd_eold'});
66                         }
67                 if ($access{'repeat'}) {
68                         $in{'new'} eq $in{'repeat'} || &error($text{'passwd_erepeat'});
69                         }
70                 $err = &useradmin::check_password_restrictions(
71                         $in{'new'}, $in{'user'});
72                 &error($err) if ($err);
73
74                 &can_edit_passwd([ $user->{'user'}, $user->{'pass'},
75                                    $user->{'uid'}, $user->{'gid'} ]) ||
76                         &error($text{'passwd_ecannot'});
77
78                 # Actually do the change
79                 &change_password($user, $in{'new'}, 
80                         $access{'others'} == 1 ||
81                         $access{'others'} == 2 && $in{'others'});
82                 }
83         else {
84                 &error($text{'passwd_euser'});
85                 }
86         delete($user->{'plainpass'});
87         delete($user->{'pass'});
88         &webmin_log("passwd", undef, $user->{'user'}, $user);
89         }
90
91 # Show a confirmation message
92 &ui_print_header(undef, $text{'passwd_title'}, "");
93 if (($user->{'user'} eq $remote_user || $user->{'user'} eq $base_remote_user) &&
94     !$main::session_id) {
95         print "<p>",&text('passwd_ok', "<tt>$user->{'user'}</tt>"),"<p>\n";
96         }
97 else {
98         print "<p>",&text('passwd_ok2', "<tt>$user->{'user'}</tt>"),"<p>\n";
99         }
100 &ui_print_footer($in{'one'} ? ( "/", $text{'index'} )
101                             : ( "", $text{'index_return'} ));
102