0

我对 ejabberd 2.1.2 有疑问,并且external_auth 没有收到响应。我已经检查了谷歌上所有不同的帖子和这里的问题,但仍然没有找到解决方案。

在我的 /etc/ejabberd/ejabberd.cfg 我有以下条目:

{auth_method, external}.
{extauth_program, "/tmp/auth.php"}.

上面引用的 extauth_program 可以在下面找到。我可以以“ejabberd”用户身份运行 php 文件,并且文件创建/文件权限(也是日志文件)不是问题。

#!/usr/bin/php

<?
$fh  = fopen("php://stdin", 'r');
$stdout = fopen('php://stdout', 'w');
$fs = fopen("/tmp/auth-log.txt." . getmypid(), 'a');

if(!$fh){
    die("Cannot open STDIN\n");
}

$users = array('user1'=>'password1', 'user2'=>'password2');

do{
    $lenBytes = fgets($fh, 3);
    $len = unpack('n', $lenBytes);
    $len = $len[1];
    if($len<1) continue;
    $msg = fgets($fh, $len+1);
    $toks=explode(':',$msg);
    fwrite($fs, $msg . "\n");

    $method = array_shift($toks);
    fwrite($fs, "$method\n");
    $result = false;

    switch($method){
        case 'auth':
            list($username, $server, $password) = $toks;
            $password = trim($password);
            fwrite($fs, "checking user: $username and password $password\n");

            if($users[$username] == $password){
                fwrite($fs, "password match\n");
                $result = true;
            }else{
                $result = false;
            }
            break;

        case 'isuser':
            list($username, $server) = $toks;
            if(isset($users[$username])){
                $result = true;
            }else{
                $result = false;
            }
            break;

        default:
            $result = false;
    }
        $message =  @pack("nn", 2, $result);
        fwrite($stdout, $message);
        $dump = @unpack("nn", $message);
        $dump = $dump["n"];
        fwrite($fs, $dump . "\n");
        flush();
} while(true);
?>

当我启动 xjabberd 并尝试使用 'user1@example.com' 和密码:'password1' 连接时,它需要很长时间,最终失败。在日志文件中,我看到从上述 PHP 脚本生成的以下条目。当从客户端到服务器的连接发生时,这些几乎立即发生。

auth:user1:example.com:password1
auth
checking user: user1 and password password1
password match
2

在 /var/log/ejabberd/ejabberd.log 我有以下条目:

=INFO REPORT==== 2011-02-20 01:15:26 ===
I(<0.557.0>:ejabberd_c2s:587) : ({socket_state,tls,{tlssock,#Port<0.2820>,#Port<0.2844>},<0.556.0>}) Failed authentication for user1@example.com

=ERROR REPORT==== 2011-02-20 01:15:36 ===
E(<0.365.0>:extauth:80) : extauth call '["auth","user1@example.com",
                                         "example.com","password1"]' didn't receive response

=INFO REPORT==== 2011-02-20 01:15:36 ===
I(<0.559.0>:ejabberd_c2s:587) : ({socket_state,tls,{tlssock,#Port<0.2846>,#Port<0.2848>},<0.558.0>}) Failed authentication for user1@example.com@example.com

=INFO REPORT==== 2011-02-20 01:15:37 ===
I(<0.553.0>:ejabberd_listener:232) : (#Port<0.2850>) Accepted connection {{10,1,1,3},55051} -> {{10,130,11,243},5222}

=ERROR REPORT==== 2011-02-20 01:15:50 ===
E(<0.365.0>:extauth:80) : extauth call '["auth","user1","example.com",
                                         "password1"]' didn't receive response

=INFO REPORT==== 2011-02-20 01:15:50 ===
I(<0.561.0>:ejabberd_c2s:587) : ({socket_state,tls,{tlssock,#Port<0.2850>,#Port<0.2852>},<0.560.0>}) Failed authentication for user1@example.com

=INFO REPORT==== 2011-02-20 01:15:51 ===
I(<0.553.0>:ejabberd_listener:232) : (#Port<0.2854>) Accepted connection {{10,1,1,3},55052} -> {{10,130,11,243},5222}

=ERROR REPORT==== 2011-02-20 01:16:03 ===
E(<0.365.0>:extauth:80) : extauth call '["auth","user1@example.com",
                                         "example.com","password1"]' didn't receive response

=INFO REPORT==== 2011-02-20 01:16:03 ===
I(<0.563.0>:ejabberd_c2s:587) : ({socket_state,tls,{tlssock,#Port<0.2854>,#Port<0.2856>},<0.562.0>}) Failed authentication for user1@example.com@example.com

任何解决此问题的帮助将不胜感激。提前致谢!

4

2 回答 2

1

行前:

$message =  @pack("nn", 2, $result);

写:

$result = ($result) ? 1 : 0;
于 2012-03-23T19:56:31.150 回答
1

花了 1 天时间试图获得工作 ejabberd 授权。
问题基本上出现在从标准输入读取时阻塞进程。它在请求字符串的末尾一直被阻止,直到客户端由于超时而断开连接。
这是从描述符读取非块符号的解决方案:

function non_block_read($fd, &$data) {
    $read = array($fd);
    $write = array();
    $except = array();
    $result = stream_select($read, $write, $except, 0);
    if($result === false) {
                throw new Exception('stream_select failed');
        }
    if($result === 0) return false;
    $data.= stream_get_line($fd, 1);
    return true;
}

这是守护程序代码也可以使终身阅读周期。

$fh  = fopen("php://stdin", 'r');
$fhout  = fopen("php://stdout", 'w');
$pdo = new PDO('mysql:host='.$host.';dbname='.$db, $user,$pass);
if(!$fh){
    die("Cannot open STDIN\n");
}
$aStr='';
$collect=false;
do {
        if (!non_block_read($fh,$aStr)) {
                if (strlen($aStr) > 0) {
                       $toAuth = substr(trim($aStr),1);
                       checkAuth($toAuth,$pdo,$fhout);
                       $aStr='';
                }
                else sleep (1);
        }
}
while (true);

一些解释: checkAuth - 任何实现 'auth' 和 'isuser' 处理程序的函数。
sleep(1) - 逃避 CPU 负载的简单方法。
第一个符号 - 预先设置消息的服务符号,它因客户端而异,所以我只是剪掉它。
这是回复代码。回复 - 真 | 错误的值。

$message =  @pack("nn", 2, $result);
fwrite($stdout, $message);
flush();

享受。

于 2014-08-15T12:55:04.603 回答