3 # Actually update a user's password by directly modifying /etc/shadow
5 $ENV{'MINISERV_INTERNAL'} || die "Can only be called by miniserv.pl";
6 require './web-lib.pl';
10 &get_miniserv_config(\%miniserv);
11 $miniserv{'passwd_mode'} == 2 || die "Password changing is not enabled!";
14 $in{'new1'} ne '' || &pass_error($text{'password_enew1'});
15 $in{'new1'} eq $in{'new2'} || &pass_error($text{'password_enew2'});
17 # Is this a Webmin user?
18 if (&foreign_check("acl")) {
19 &foreign_require("acl", "acl-lib.pl");
20 ($wuser) = grep { $_->{'name'} eq $in{'user'} } &acl::list_users();
21 if ($wuser->{'pass'} eq 'x') {
22 # A Webmin user, but using Unix authentication
25 elsif ($wuser->{'pass'} eq '*LK*' ||
26 $wuser->{'pass'} =~ /^\!/) {
27 &pass_error("Webmin users with locked accounts cannot change ".
31 if (!$in{'pam'} && !$wuser) {
32 $miniserv{'passwd_cindex'} ne '' && $miniserv{'passwd_mindex'} ne '' ||
33 die "Missing password file configuration";
37 # Update Webmin user's password
38 $enc = &acl::encrypt_password($in{'old'}, $wuser->{'pass'});
39 $enc eq $wuser->{'pass'} || &pass_error($text{'password_eold'});
40 $perr = &acl::check_password_restrictions($in{'user'}, $in{'new1'});
41 $perr && &pass_error(&text('password_enewpass', $perr));
42 $wuser->{'pass'} = &acl::encrypt_password($in{'new1'});
43 $wuser->{'temppass'} = 0;
44 &acl::modify_user($wuser->{'name'}, $wuser);
48 # Use PAM to make the change..
49 eval "use Authen::PAM;";
51 &pass_error(&text('password_emodpam', $@));
54 # Check if the old password is correct
55 $service = $miniserv{'pam'} ? $miniserv{'pam'} : "webmin";
56 $pamh = new Authen::PAM($service, $in{'user'}, \&pam_check_func);
57 $rv = $pamh->pam_authenticate();
58 $rv == PAM_SUCCESS() ||
59 &pass_error($text{'password_eold'});
62 # Change the password with PAM, in a sub-process. This is needed because
63 # the UID must be changed to properly signal to the PAM libraries that
64 # the password change is not being done by the root user.
67 @uinfo = getpwnam($in{'user'});
69 ($>, $<) = (0, $uinfo[2]);
70 $pamh = new Authen::PAM("passwd", $in{'user'}, \&pam_change_func);
71 $rv = $pamh->pam_chauthtok();
74 print TEMP ($messages || $pamh->pam_strerror($rv)),"\n";
81 chop($messages = <TEMP>);
84 $rv == PAM_SUCCESS || &pass_error(&text('password_epam', $messages));
88 # Directly update password file
90 # Read shadow file and find user
91 &lock_file($miniserv{'passwd_file'});
92 $lref = &read_file_lines($miniserv{'passwd_file'});
93 for($i=0; $i<@$lref; $i++) {
94 @line = split(/:/, $lref->[$i], -1);
95 local $u = $line[$miniserv{'passwd_uindex'}];
96 if ($u eq $in{'user'}) {
101 defined($idx) || &pass_error($text{'password_euser'});
103 # Validate old password
104 &unix_crypt($in{'old'}, $line[$miniserv{'passwd_pindex'}]) eq
105 $line[$miniserv{'passwd_pindex'}] ||
106 &pass_error($text{'password_eold'});
108 # Make sure new password meets restrictions
109 if (&foreign_check("changepass")) {
110 &foreign_require("changepass", "changepass-lib.pl");
111 $err = &changepass::check_password($in{'new1'}, $in{'user'});
112 &pass_error($err) if ($err);
114 elsif (&foreign_check("useradmin")) {
115 &foreign_require("useradmin", "user-lib.pl");
116 $err = &useradmin::check_password_restrictions(
117 $in{'new1'}, $in{'user'});
118 &pass_error($err) if ($err);
121 # Set new password and save file
122 $salt = chr(int(rand(26))+65) . chr(int(rand(26))+65);
123 $line[$miniserv{'passwd_pindex'}] = &unix_crypt($in{'new1'}, $salt);
124 $days = int(time()/(24*60*60));
125 $line[$miniserv{'passwd_cindex'}] = $days;
126 $lref->[$idx] = join(":", @line);
128 &unlock_file($miniserv{'passwd_file'});
132 &header(undef, undef, undef, undef, 1, 1);
134 print "<center><h3>",&text('password_done', "/"),"</h3></center>\n";
140 &header(undef, undef, undef, undef, 1, 1);
143 print "<center><h3>",$text{'password_err'}," : ",@_,"</h3></center>\n";
158 $ans = $in{'user'} if ($code == PAM_PROMPT_ECHO_ON());
159 $ans = $in{'old'} if ($code == PAM_PROMPT_ECHO_OFF());
161 push @res, PAM_SUCCESS();
164 push @res, PAM_SUCCESS();
177 if ($code == PAM_PROMPT_ECHO_ON()) {
178 # Assume asking for username
179 push @res, PAM_SUCCESS();
180 push @res, $in{'user'};
182 elsif ($code == PAM_PROMPT_ECHO_OFF()) {
183 # Assume asking for a password (old first, then new)
184 push @res, PAM_SUCCESS();
185 if ($msg =~ /old|current/i) {
186 push @res, $in{'old'};
189 push @res, $in{'new1'};
193 # Some message .. ignore it
194 push @res, PAM_SUCCESS();
198 push @res, PAM_SUCCESS();