#!/usr/local/bin/perl
# save_passwd.cgi
# Change a user's password

require './passwd-lib.pl';
&error_setup($text{'passwd_err'});
&ReadParse();

if ($config{'passwd_cmd'}) {
	# Call the passwd program to do the change
	@user = getpwnam($in{'user'});
	@user || &error($text{'passwd_euser'});
	&can_edit_passwd(\@user) || &error($text{'passwd_ecannot'});
	if ($access{'repeat'}) {
		$in{'new'} eq $in{'repeat'} || &error($text{'passwd_erepeat'});
		}
	&foreign_require("proc", "proc-lib.pl");
	if ($access{'old'} == 1 ||
	    $access{'old'} == 2 && $in{'user'} ne $remote_user) {
		@cmd = ( $config{'passwd_cmd'}, $user[2], $user[3] );
		}
	else {
		@cmd = ( "$config{'passwd_cmd'} '$in{'user'}'" );
		}
	&additional_log('exec', undef, $cmd[0]);
	local ($fh, $fpid) = &foreign_call("proc", "pty_process_exec", @cmd);
	while(1) {
		local $rv = &wait_for($fh, '(new|re-enter).*:',
					   '(old|current|login).*:',
					   'pick a password');
		$out .= $wait_for_input;
		sleep(1);
		if ($rv == 0) {
			syswrite($fh, $in{'new'}."\n", length($in{'new'})+1);
			}
		elsif ($rv == 1) {
			syswrite($fh, $in{'old'}."\n", length($in{'old'})+1);
			}
		elsif ($rv == 2) {
			syswrite($fh, "1\n", 2);
			}
		else {
			last;
			}
		last if (++$count > 10);
		}
	close($fh);
	waitpid($fpid, 0);
	&error(&text('passwd_ecmd', "<tt>$config{'passwd_cmd'}</tt>", "<pre>$out</pre>")) if ($? || $count > 10);
	&webmin_log("passwd", undef, $in{'user'});
	}
else {
	# Update the config files directly via the useradmin module
	&foreign_require("useradmin", "user-lib.pl");

	# Find the user
	@ulist = &foreign_call("useradmin", "list_users");
	($user) = grep { $_->{'user'} eq $in{'user'} } @ulist;
	$user || &error($text{'passwd_euser'});
	&can_edit_passwd([ $user->{'user'}, $user->{'pass'}, $user->{'uid'},
			   $user->{'gid'} ]) || &error($text{'passwd_ecannot'});

	# Validate inputs
	if ($access{'old'} == 1 ||
	    $access{'old'} == 2 && $user->{'user'} ne $remote_user) {
		crypt($in{'old'}, $user->{'pass'}) eq $user->{'pass'} ||
			&error($text{'passwd_eold'});
		}
	if ($access{'repeat'}) {
		$in{'new'} eq $in{'repeat'} || &error($text{'passwd_erepeat'});
		}
	$err = &useradmin::check_password_restrictions(
		$in{'new'}, $user->{'user'});
	&error($err) if ($err);

	# Do the change!
	$user->{'olduser'} = $user->{'user'};
	$user->{'pass'} = &useradmin::encrypt_password($in{'new'});
	$user->{'passmode'} = 3;
	$user->{'plainpass'} = $in{'new'};
	$pft = &useradmin::passfiles_type();
	if ($pft == 2 || $pft == 5) {
		$user->{'change'} = int(time() / (60*60*24));
		}
	elsif ($pft == 4) {
		$user->{'change'} = time();
		}
	&useradmin::lock_user_files();
	$ENV{'USERADMIN_USER'} = $user->{'user'};
	$ENV{'USERADMIN_UID'} = $user->{'uid'};
	$ENV{'USERADMIN_REAL'} = $user->{'real'};
	$ENV{'USERADMIN_SHELL'} = $user->{'shell'};
	$ENV{'USERADMIN_HOME'} = $user->{'home'};
	$ENV{'USERADMIN_GID'} = $user->{'gid'};
	$ENV{'USERADMIN_PASS'} = $in{'new'};
	$ENV{'USERADMIN_ACTION'} = 'MODIFY_USER';
	&useradmin::making_changes();
	&useradmin::modify_user($user, $user);
	&useradmin::made_changes();
	&useradmin::unlock_user_files();
	if ($access{'others'}) {
		&useradmin::other_modules("useradmin_modify_user", $user);
		}
	delete($user->{'plainpass'});
	delete($user->{'pass'});
	&webmin_log("passwd", undef, $user->{'user'}, $user);
	}

# Show a confirmation message
&header($text{'passwd_title'}, "");
print "<hr>\n";
print "<p>",&text('passwd_ok', "<tt>$user->{'user'}</tt>"),"<p>\n";
print "<hr>\n";
&footer("", $text{'index_return'});

