无法使用ejabberd-17.04执行外部身份验证

时间:2018-03-06 05:51:06

标签: ejabberd

我尝试使用ejabberd 17.04执行外部身份验证。但是我无法实现这一点,在ejabberd.log中我看到以下消息

@extauth:loop:141 extauth脚本突然出现原因'正常'

这是我的/opt/conf/ejabberd.yml外部身份验证的代码段:

auth_method: [external]
extauth_program: "php /opt/ejabberd-17.04/AuthExt.php"
extauth_instances: 3
auth_use_cache: false

AuthExt.php的代码片段如下:

#!/usr/bin/php
<?php
error_reporting(0);
$auth = new JabberAuth();
$auth->dbhost = "";
$auth->dbuser = "";
$auth->dbpass = "";
$auth->dbbase = "";
$auth->play(); // We simply start process !

class JabberAuth {
    var $dbhost; /* MySQL server */
    var $dbuser; /* MySQL user */
    var $dbpass; /* MySQL password */
    var $dbbase; 
    var $debug      = false;                      /* Debug mode */
    var $debugfile  = "/var/log/pipe-debug.log";  /* Debug output */
    var $logging    = false;                      /* Do we log requests ? */
    var $logfile    = "/var/log/pipe-log.log" ;   /* Log file ... */
    var $jabber_user;   /* This is the jabber user passed to the script. filled by $this->command() */
    var $jabber_pass;   /* This is the jabber user password passed to the script. filled by $this->command() */
    var $jabber_server; /* This is the jabber server passed to the script. filled by $this->command(). Useful for VirtualHosts */
    var $jid;           /* Simply the JID, if you need it, you have to fill. */
    var $data;          /* This is what SM component send to us. */

    var $dateformat = "M d H:i:s"; /* Check date() for string format. */
    var $command; /* This is the command sent ... */
    var $mysock;  /* MySQL connection ressource */
    var $stdin;   /* stdin file pointer */
    var $stdout;  /* stdout file pointer */

    function JabberAuth()
    {
        @define_syslog_variables();
        @openlog("pipe-auth", LOG_NDELAY, LOG_SYSLOG);

        if($this->debug) {
            @error_reporting(E_ALL);
            @ini_set("log_errors", "1");
            @ini_set("error_log", $this->debugfile);
        }
        $this->logg("Starting pipe-auth ..."); // We notice that it's starting ...
        $this->openstd();
    }

    function stop()
    {
        $this->logg("Shutting down ..."); // Sorry, have to go ...
        closelog();
        $this->closestd(); // Simply close files
        exit(0); // and exit cleanly
    }

    function openstd()
    {
        $this->stdout = @fopen("php://stdout", "w"); // We open STDOUT so we can read
        $this->stdin  = @fopen("php://stdin", "r"); // and STDIN so we can talk !
    }

    function readstdin()
    {
        $l      = @fgets($this->stdin, 3); // We take the length of string
        $length = @unpack("n", $l); // ejabberd give us something to play with ...
        $len    = $length["1"]; // and we now know how long to read.
        if($len > 0) { // if not, we'll fill logfile ... and disk full is just funny once
            $this->logg("Reading $len bytes ... "); // We notice ...
            $data   = @fgets($this->stdin, $len+1);
            // $data = iconv("UTF-8", "ISO-8859-15", $data); // To be tested, not sure if still needed.
            $this->data = $data; // We set what we got.
            $this->logg("IN: ".$data);
        }
    }

    function closestd()
    {
        @fclose($this->stdin); // We close everything ...
        @fclose($this->stdout);
    }

    function out($message)
    {
        @fwrite($this->stdout, $message); // We reply ...
        $dump = @unpack("nn", $message);
        $dump = $dump["n"];
        $this->logg("OUT: ". $dump);
    }

    function myalive()
    {
        if(!is_resource($this->mysock) || !@mysql_ping($this->mysock)) { // check if we have a MySQL connection and if it's valid.
            $this->mysql(); // We try to reconnect if MySQL gone away ...
            return @mysql_ping($this->mysock); // we simply try again, to be sure ...
        } else {
            return true; // so good !
        }
    }

    function play()
    {
        do {
            $this->readstdin(); // get data
            $length = strlen($this->data); // compute data length
            if($length > 0 ) { // for debug mainly ...
                $this->logg("GO: ".$this->data);
                $this->logg("data length is : ".$length);
            }
            $ret = $this->command(); // play with data !
            $this->logg("RE: " . $ret); // this is what WE send.
            $this->out($ret); // send what we reply.
            $this->data = NULL; // more clean. ...
        } while (true);
    }

    function command()
    {
        $data = $this->splitcomm(); // This is an array, where each node is part of what SM sent to us :
        // 0 => the command,
        // and the others are arguments .. e.g. : user, server, password ...

        if($this->myalive()) { // Check we can play with MySQL
            if(strlen($data[0]) > 0 ) {
                $this->logg("Command was : ".$data[0]);
            }
            switch($data[0]) {
                case "isuser": // this is the "isuser" command, used to check for user existance
                        $this->jabber_user = $data[1];
                        $parms = $data[1];  // only for logging purpose
                        $return = $this->checkuser();
                    break;

                case "auth": // check login, password
                        $this->jabber_user = $data[1];
                        $this->jabber_pass = $data[3];
                        $parms = $data[1].":".$data[2].":".md5($data[3]); // only for logging purpose
                        $return = $this->checkpass();
                    break;

                case "setpass":
                        $return = false; // We do not want jabber to be able to change password
                    break;

                default:
                        $this->stop(); // if it's not something known, we have to leave.
                        // never had a problem with this using ejabberd, but might lead to problem ?
                    break;
            }

            $return = ($return) ? 1 : 0;

            if(strlen($data[0]) > 0 && strlen($parms) > 0) {
                $this->logg("Command : ".$data[0].":".$parms." ==> ".$return." ");
            }
            return @pack("nn", 2, $return);
        } else {
            // $this->prevenir(); // Maybe useful to tell somewhere there's a problem ...
            return @pack("nn", 2, 0); // it's so bad.
        }
    }

    function checkpass()
    {
        /*
         * Put here your code to check password
         * $this->jabber_user
         * $this->jabber_pass
         * $this->jabber_server
         */
        if(GOOD) {
            return true;
        } else {
            return false;
        }
    }

    function checkuser()
    {
        /*
         * Put here your code to check user
         * $this->jabber_user
         * $this->jabber_pass
         * $this->jabber_server
         */
        if(GOOD) {
            return true;
        } else {
            return false;
        }
    }

    function splitcomm() // simply split command and arugments into an array.
    {
        return explode(":", $this->data);
    }

    function mysql() // "MySQL abstraction", this opens a permanent MySQL connection, and fill the ressource
    {
        $this->mysock = @mysql_pconnect($this->dbhost, $this->dbuser, $this->dbpass);
        @mysql_select_db($this->dbbase, $this->mysock);
        $this->logg("MySQL :: ". (is_resource($this->mysock) ? "Connecté" : "Déconnecté"));
    }

    function logg($message) // pretty simple, using syslog.
    // some says it doesn't work ? perhaps, but AFAIR, it was working.
    {
        if($this->logging) {
            @syslog(LOG_INFO, $message);
        }
    }
}

?>

任何人都可以指出我使用ejabberd执行外部身份验证的正确方法。

谢谢,

1 个答案:

答案 0 :(得分:0)

如果您使用的是比5.4更新的php版本,那么您应该在脚本中删除此行,因为它已被弃用。 import csv, itertools from math import floor filename = 'denton_housing.csv' denton_housing = {} def create_nested_housing_dict(): with open(filename, 'r', encoding='utf8', newline='') as f: for row in csv.DictReader(f, delimiter=','): if row['year'] not in denton_housing: #UPDATE denton_housing[row['year']] = {} try: denton_housing[row['year']][row['title_field']] = int(row['value']) except KeyError: denton_housing[row['year']] = {} return denton_housing data = create_nested_housing_dict() for key, value in data.items(): print(key + ' ' + "{0:.0f}%".format(int(value['Vacant Housing Units'])) / \ int(value['Total Housing Units']) * 100 + key + ' ' + \ "{0:.0f}%".format(int(value['Occupied Housing Units'])) / \ int(value['Total Housing Units']) * 100)

请参阅此链接了解更多详情。 https://www.ejabberd.im/check_mysql_php