如何让用户从网络浏览器更改 Linux 密码?

如何让用户从网络浏览器更改 Linux 密码?

我不确定这是 stackoverflow 问题还是 serverfault,但情况如下:

我有一个 Ubuntu 10.04 文件服务器(Samba/FTP/HTTP),我希望能够让用户使用他们的网络浏览器更改服务器密码。

我以前使用 PHP 和一堆 exec 编写过类似的脚本,但我认为这并不安全,因为查看服务器上的进程列表的人可以监听它。

是否有某种插件(PHP 或 Python 或其他)可以轻松做到这一点?

我宁愿不使用像 webmin/usermin 这样的程序,因为它太过分了。

答案1

web-chpass可以通过 PAM 更改密码。

答案2

我更喜欢使用 2 个独立的进程。一个进程通过将特殊文件放入特殊文件夹来发出请求。第二个进程,cron 作业循环遍历文件夹并完成密码更改请求。

调用脚本需要做的就是将文件放入文件夹中(前提是它有这样做的权限。

请参阅此链接以获取更多详细信息并获取脚本:http://sylnsr.blogspot.com/2012/09/keep-unix-password-in-sync-with.html

答案3

如果您将 Samba 作为 PDC 运行,则可以使用它来允许用户使用 ctrl+alt+delete 更改密码。如果这个答案不适合您,我很抱歉,但我还不能发表评论等...

    unix password sync = Yes

    passwd program = /usr/bin/php -f /my_folder/my_own_script.php %u
    passwd chat = "password:" %n\n "changed"
    passwd chat debug = yes

或系统程序:

passwd program = /usr/bin/passwd %u
passwd chat = *New*UNIX*password* %n\n *ReType*new*UNIX*password* %n\n  *passwd:*all*authentication*tokens*updated*successfully*

搜索“密码同步”,它位于文档中间,更好地解释了它是如何工作的: http://www.samba.org/samba/docs/using_samba/ch09.html#samba2-CHP-9-SECT-4.3

答案4

在网上搜索了几个小时后,我还是没能找到一个非常好的选择,所以我实现了这个 hack。它使用本文使用 PHP 更改密码。

我也在使用PECL:PAM 包增加一点验证。

此页面位于安全的 HTTPS 文件夹(通过 .htaccess 自动重定向)

<?php

$messages = array();

function change_password ($user, $currpwd, $newpwd) {

    // Open a handle to expect in write mode
    $p = popen('/usr/bin/expect','w');

    // Log conversation for verification
    $log = '/tmp/passwd_' . md5($user . time());
    $cmd .= "log_file -a \"$log\"; ";

    // Spawn a shell as $user
    $cmd .= "spawn /bin/su $user; ";
    $cmd .= "expect \"Password:\"; ";
    $cmd .= "send \"$currpwd\\r\"; ";
    $cmd .= "expect \"$user@\"; ";

    // Change the unix password
    $cmd .= "send \"/usr/bin/passwd\\r\"; ";
    $cmd .= "expect \"(current) UNIX password:\"; ";
    $cmd .= "send \"$currpwd\\r\"; ";
    $cmd .= "expect \"Enter new UNIX password:\"; ";
    $cmd .= "send \"$newpwd\\r\"; ";
    $cmd .= "expect \"Retype new UNIX password:\"; ";
    $cmd .= "send \"$newpwd\\r\"; ";
    $cmd .= "expect \"passwd: password updated successfully\"; ";

    // Commit the command to expect & close
    fwrite($p, $cmd); pclose ($p);

    // Read & delete the log
    $fp = fopen($log,r);
    $output = fread($fp, 2048);
    fclose($fp); unlink($log);
    $output = explode("\n",$output);

    return (trim($output[count($output)-2]) == 'passwd: password updated successfully') ? true : false;
}

function process_post() {

    if ((!isset($_SERVER['HTTP_REFERER'])) 
        || (strpos($_SERVER['HTTP_REFERER'], $_SERVER['SCRIPT_NAME']) === FALSE)) {

        echo "GO AWAY!";
        exit();
        return FALSE;

    }

    global $messages;

    $username           = trim($_POST['username']);
    $password_current   = trim($_POST['password_current']);
    $password_new       = trim($_POST['password_new']);
    $password_confirm   = trim($_POST['password_confirm']);

    // Check for blanks
    if ($username == '' || $password_current == '' || $password_new == '' || $password_confirm == '') {
        array_push(&$messages, "ERROR: You cannot leave any field empty.");
        return FALSE;
    }

    // Check username
    if (!ctype_alnum($username)) {
        array_push(&$messages, "ERROR: You've entered an invalid username.");
        return FALSE;
    }

    // Check to see if new password is correctly typed
    if ($password_new != $password_confirm) {       
        array_push(&$messages, "ERROR: New Password and Confirmation do not match.");
        return FALSE;
    }

    // Check if current password is valid (not really neccessary)
    if (!pam_auth($username, $password_current, &$error, FALSE)) {
        if (trim($error) == "Permission denied (in pam_authenticate)")
            array_push(&$messages, "ERROR: You've username/password was not accepted.");    
        else
            array_push(&$messages, "ERROR: " . $error);
        return FALSE;
    }

    if (change_password ($username, $password_current, $password_new))
        array_push(&$messages, "Password Successfully Changed");
    else 
        array_push(&$messages, "ERROR: Password change failed.");

}

if ($_SERVER['REQUEST_METHOD'] == 'POST') process_post();


?><html>
<head>


<title>Passwords</title>

<style type="text/css">

body {
    font-family: Verdana, Arial, sans-serif;
    font-size: 12px;
}

label {
    width: 150px;
    display: block;
    float: left;
}

input {
    float: left;
}

br {
    clear: both;
}

.message {
    font-size: 11px;
    font-weight: bold;
}

.error {
    color:#C00;
}


</style>

</head>


<body>

<h2>Change Passwords</h2>

<form action="<?= $_SERVER['SCRIPT_NAME'] ?>" method="post">

<fieldset>

<? if (count($messages) != 0) { 

    foreach ($messages as $message) { ?>

<p class="message<?= ((strpos($message, 'ERROR:') === FALSE) ? '' : ' error') ?>"><?= $message ?></p>

<? } } ?>

<label>Username: </label>
<input type="text" name="username" /><br />

<label>Current Password:</label>
<input type="password" name="password_current" /><br />

<label>New Password:</label>
<input type="password" name="password_new" /><br />

<label>Confirm Password:</label>
<input type="password" name="password_confirm" /><br />

<input type="reset" value="Reset" /> <input type="submit" value="Submit" />

</fieldset>


</form>


</body>
</html>

我也有这个问题/答案发布在https://stackoverflow.com/questions/3032785/php-pam-to-change-user-password/3067974#3067974

相关内容