ejabberd external authentication php sqlserver


Kasaf

I have an ejabberd 15.03 server in a centOS virtual machine and I need to implement an external authentication method with php against SQL Server. The official documentation shows a php/mysql example php/mysql external authentication

Is there a way to change this example to php/sqlserver and functions like mysql_ping.

Here is an example I wrote and put it under /opt/auth/ and configure ejabberd.yml to execute as external authentication.

$auth = new JabberAuth();
$auth->dbhost = "hostIP";
$auth->dbuser = "user";
$auth->dbpass = "pass";
$auth->dbbase = "databaseName";



$auth->play(); // We simply start process !

class JabberAuth {
    var $dbhost; 
    var $dbuser; 
    var $dbpass;
    var $dbbase; 

    var $debug      = false;                      /* Debug mode */
    var $debugfile  = "/opt/auth/log/pipe-debug.log";  /* Debug output */
    var $logging    = false;                      /* Do we log requests ? */
    var $logfile    = "/opt/auth/log/pipe-log.log" ;   /* Log file ... */
    /*
     * For both debug and logging, ejabberd have to be able to write.
     */

    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()
    {


$hash=sha1($this->jabber_pass);
$query = mssql_query("SELECT [attribute] FROM [DatabaseName].[dbo].[table] where (S_USERNAME='$this->jabber_user') AND  (SUBSTRING(S_PASSWORD,1,(LEN(S_PASSWORD)-3)))='$hash'");


// Check if there were any records
if (!mssql_num_rows($query)) {
    return false;
} else {
    return true;
}
    }

    function checkuser()
    {

$query = mssql_query("SELECT [attribute] FROM [DatabaseName].[dbo].[table] where S_USERNAME='$this->jabber_user'");

// Check if there were any records
if (!mssql_num_rows($query)) {
    return false;
} else {
    return true;
}

    }

    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 = @mssql_pconnect($this->dbhost, $this->dbuser, $this->dbpass);
        echo '1';
        var_dump($this->mysock);
        @mssql_select_db($this->dbbase, $this->mysock);
        $this->logg("MsSql :: ". (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);
        }
    }
}

This didn't make any PHP errors. But in ejabberd.log I always get "extauth script exited abruptly with reason "normal""

Here is the crash report in crash.log

2015-04-01 17:44:12 =崩溃报告====崩溃器:初始调用:ejabberd_http:init / 2 pid:<0.8779.0> registered_name:[]异常错误:错误的参数:[{extauth,call_port, 2,[{{file,“ src / extauth.erl”},{line,99}]},{ejabberd_auth_external,check_password_extauth,3,[{file,“ src / ejabberd_auth_external.erl”},{line,182}]} ,{ejabberd_auth_external,check_password_external_cache,3,[{file,“ src / ejabberd_auth_external.erl”},{line,244}]},{ejabberd_auth,check_password_loop,2,[{file,“ src / ejabberd_auth.erl”}, line,158}]},{ejabberd_auth,check_password,3,[{file,“ src / ejabberd_auth.erl”},{line,106}]},{ejabberd_web_admin,get_auth_account,5,[{file,“ src / ejabberd_web_admin .erl“},{line,266}]},{ejabberd_web_admin,process,2,[{file,” src / ejabberd_web_admin.erl“},{line,221}]},{ejabberd_http,process,5,[{文件,“ src / ejabberd_http.erl”},{line,359}]}]祖先:[ejabberd_http_sup,ejabberd_sup,<0.37.0>] message: [] link: [<0.327.0>, #Port<0.12042>] dictionary: [{random_seed, {2036, 6729, 29501}}] trap_exit: error status: running heap_size: 2586 stack_size: 27 Reduction: 1244 neighbor: 2015-04-01 17:44:12 = SUPERVISOR REPORT ==== supervisor: {local, ejabberd_http_sup} context: child_terminated reason: badarg perpetrator: [{pid, <0.8779.0>}, {name, undefined}, {mfargs, {ejabberd_http, start_link, undefined}}, {restart_type, temporary}, {shutdown, 1000}, {child_type, worker}] 27 reduce: 1244 neighbors: 2015-04-01 17:44:12 = SUPERVISOR REPORT ==== supervisor: {local, ejabberd_http_sup} context: child_terminated reason: badarg offender: [{pid, <0.8779.0>}}, {name, undefined}, {mfargs, {ejabberd_http, start_link, undefined define }}, {restart_type, temporary}, {shutdown, 1000}, {child_type, worker}] 27 reduce: 1244 neighbors: 2015-04-01 17:44:12 = SUPERVISOR REPORT ==== supervisor: { local, ejabberd_http_sup} context: child_terminated reason: badarg perpetrator: [{pid, <0.8779.0>}}, {name, undefined}, {mfargs, {ejabberd_http, start_link, undefined}}, {restart_type, temporary}, {shutdown, 1000}, {child_type, worker}]

It appears to be a misconfiguration. This is what I added to ejabberd.yml

auth_method: external
extauth_program: "php -f /etc/ejabberd-15.03/auth_script.php"
extauth_cache: 600
extauth_instances: 3
Kasaf

I think the latest version of ejabberd doesn't support SQL server. So I finally turned to openfire which supports many databases.

Related


ejabberd external authentication php sqlserver

Kasaf I have an ejabberd 15.03 server in a centOS virtual machine and I need to implement an external authentication method with php against SQL Server. The official documentation shows a php/mysql example php/mysql external authentication Is there a way to ch

ejabberd external authentication php sqlserver

Kasaf I have an ejabberd 15.03 server in a centOS virtual machine and I need to implement an external authentication method with php against SQL Server. The official documentation shows a php/mysql example php/mysql external authentication Is there a way to ch

ejabberd external authentication php sqlserver

Kasaf I have an ejabberd 15.03 server in a centOS virtual machine and I need to implement an external authentication method with php against SQL Server. The official documentation shows a php/mysql example php/mysql external authentication Is there a way to ch

External authentication module for ejabberd on Windows

David Comer How to get ejabberd to run external authentication scripts on Windows? So far I have modified the file C:\ Program Files \ ejabberd-15.06 \ lib \ ejabberd-15.06 \ priv \ cfg \ ejabberd.yml Comment out the existing auth_method directive and add the

External authentication in ejabberd using Java

Venkas I am working on a task to implement external authentication in ejabberd using Java. I've searched the internet for examples and found examples in PHP, Perl, Python, but nothing in Java. I know the configuration that needs to be done in the 'ejabberd.cfg

External authentication module for ejabberd on Windows

David Comer How to get ejabberd to run external authentication scripts on Windows? So far I have modified the file C:\ Program Files \ ejabberd-15.06 \ lib \ ejabberd-15.06 \ priv \ cfg \ ejabberd.yml Comment out the existing auth_method directive and add the

External authentication module for ejabberd on Windows

David Comer How to get ejabberd to run external authentication scripts on Windows? So far I have modified the file C:\ Program Files \ ejabberd-15.06 \ lib \ ejabberd-15.06 \ priv \ cfg \ ejabberd.yml Comment out the existing auth_method directive and add the

JWT authentication in ejabberd

AD95 I have followed this documentation ejabberd authentication to configure the ejabberd service to use jwt tokens to authenticate users . In the ejabberd.yml file, I have configured the authentication mechanism as follows `auth_method: [jwt, sql] jwt_key: /

Joomla 3 authentication to access external application in PHP

frivolous I found this very useful script to check authentication in Joomla. Joomla 3 external authentication script <?php /** * Joomla! External authentication script * * @author vdespa * Version 1.0 * * Code adapted from /index.php * * @package Jo

Joomla 3 authentication to access external application in PHP

frivolous I found this very useful script to check authentication in Joomla. Joomla 3 external authentication script <?php /** * Joomla! External authentication script * * @author vdespa * Version 1.0 * * Code adapted from /index.php * * @package Jo

Joomla 3 authentication to access external application in PHP

frivolous I found this very useful script to check authentication in Joomla. Joomla 3 external authentication script <?php /** * Joomla! External authentication script * * @author vdespa * Version 1.0 * * Code adapted from /index.php * * @package Jo

Pre-authentication steps in ejabberd

Shubham 1164 I'm working on a whatsapp-like project and using ejabberd as a backend server for Android/IOS clients. I want to do something like step 1 for whatsapp. The client sends the mobile number to the server. 2. The server sends an OTP to the client and

SQLServer Reference External Query

Clyde Symonette Is it possible to reference an outer query from an inner query in the context of a join? The "where(sid.ItemID = i.itemID)" of the inner query is giving me an error. I thought I was doing this before, so I searched all the stored procs, but app

SQLServer Reference External Query

Clyde Symonette Is it possible to reference an outer query from an inner query in the context of a join? The "where(sid.ItemID = i.itemID)" of the inner query is giving me an error. I thought I was doing this before, so I searched all the stored procs, but app

SQLServer Reference External Query

Clyde Symonette Is it possible to reference an outer query from an inner query in the context of a join? The "where(sid.ItemID = i.itemID)" of the inner query is giving me an error. I thought I was doing this before, so I searched all the stored procs, but app

Xmpp ejabberd custom authentication with Node.js

slow harry I want to use XMPP ejabberd server with node js authentication, but didn't find any documentation or tutorial about it, where can I find examples? Jorge Guntanis If you want to authenticate with a database then it's easier to use auth_odbc, if you s

External authentication not redirecting to external site

Brendan Green A strange thing is happening here. I have an ASP.NET MVC5 website set up and running with a local account via ASP.NET Identity. I'm now trying to enable external authentication, but something strange is happening. I am sure I have followed the co

External authentication not redirecting to external site

Brendan Green A strange thing is happening here. I have an ASP.NET MVC5 website set up and running with a local account via ASP.NET Identity. I'm now trying to enable external authentication, but something strange is happening. I am sure I have followed the co

Couchdb external authentication

compartment I am developing a series of utility applications, each of which can be used on desktop, mobile and web. After some research I decided to use pouchdb on the client and couchdb on the server to provide offline sync. Users will be able to create an ac

Couchdb external authentication

compartment I am developing a series of utility applications, each of which can be used on desktop, mobile and web. After some research I decided to use pouchdb on the client and couchdb on the server to provide offline sync. Users will be able to create an ac

Couchdb external authentication

compartment I am developing a series of utility applications, each of which can be used on desktop, mobile and web. After some research I decided to use pouchdb on the client and couchdb on the server to provide offline sync. Users will be able to create an ac

External authentication via LDAP

Ajay I am using LDAP for external authentication. I have to authenticate a user via LDAP and allow that user to access many application servers. I would like to know how to map all ldap users with users in the database (Users in the ldap server Example:ldap://

Couchdb external authentication

compartment I am developing a series of utility applications, each of which can be used on desktop, mobile and web. After some research I decided to use pouchdb on the client and couchdb on the server to provide offline sync. Users will be able to create an ac

Couchdb external authentication

compartment I am developing a series of utility applications, each of which can be used on desktop, mobile and web. After some research I decided to use pouchdb on the client and couchdb on the server to provide offline sync. Users will be able to create an ac