Welcome! Log In Create A New Profile


Problems with PHP authentication imap/smtp proxy

April 13, 2014 03:25PM
I managed to write my php auth script but still having problems authenticating.
Also this is what I see in the logs:
[error] 22014#0: *3234 recv() failed (111: Connection refused) while in http auth state, client: back.end.ip server:, login: "user@domain.com"

Also do I call this script with the following auth_http line? I never see anything listening on 9000. Where is this 9000 coming from? I just see everyone using it:



$db = new PDO('mysql:host=back.end.ip;dbname=server;charset=utf8', 'user', 'password');
$db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);

if (!isset($_SERVER["HTTP_AUTH_USER"] ) || !isset($_SERVER["HTTP_AUTH_PASS"] )) {
$username = $_SERVER["HTTP_AUTH_USER"] ;
$userpass = $_SERVER["HTTP_AUTH_PASS"] ;
$protocol = $_SERVER["HTTP_AUTH_PROTOCOL"] ;

$backend_port = "";
if ($protocol == "imap") {
$backend_port = 993;
if ($protocol == "smtp") {
$backend_port = 25;
// nginx likes ip address so if your
// application gives back hostname, convert it to ip address here
$backend_ip = "back.end.ip";
// Authenticate the user or fail
if (!authuser($username,$userpass)) {
// Get the server for this user if we have reached so far
$userserver = getmailserver($username);

// Get the ip address of the server
// We are assuming that your backend returns hostname
// We try to get the ip else return what we got back

$server_ip = (isset($backend_ip[$userserver]))?$backend_ip[$userserver] :$userserver;
// Pass!
pass($server_ip, $backend_port);


function authuser($user,$pass) {
global $db;
$stmt = $db->prepare("SELECT password FROM users WHERE email=:email LIMIT 1");
$dbpass = $stmt->fetchColumn();
return ($dbpass === $pass);

function getmailserver($user) {
return $backend_ip;

function fail(){
header("Auth-Status: Invalid login or password");

function pass($server,$port) {
header("Auth-Status: OK");
header("Auth-Server: $server");
header("Auth-Port: $port");

nginx.conf (my http section is fine as I use it for my backend apache)

mail {
server_name mx1.domain.com;
#auth_http unix:/path/socket:/cgi-bin/auth;

proxy on;

ssl_prefer_server_ciphers on;
ssl_protocols TLSv1 SSLv3;
ssl_ciphers HIGH:!ADH:!MD5:@STRENGTH;
ssl_session_cache shared:TLSSL:16m;
ssl_session_timeout 10m;
ssl_certificate ssl/ug-mail.crt;
ssl_certificate_key ssl/private/ug-mail.key;

imap_capabilities "IMAP4rev1 UIDPLUS";
smtp_capabilities "PIPELINING 8BITMIME DSN";

# smtp_auth plain login;
# imap_auth plain login;

server {
listen 25;
protocol smtp;
timeout 120000;

server {
listen 8825;
protocol smtp;
starttls on;

server {
listen 993;
protocol imap;
ssl on;
Subject Author Posted

Problems with PHP authentication imap/smtp proxy

cybermass April 13, 2014 03:25PM

Re: Problems with PHP authentication imap/smtp proxy

Maxim Dounin April 14, 2014 07:08AM

Re: Problems with PHP authentication imap/smtp proxy

Shawn Za April 14, 2014 01:48PM

Sorry, only registered users may post in this forum.

Click here to login

Online Users

Guests: 84
Record Number of Users: 8 on April 13, 2023
Record Number of Guests: 421 on December 02, 2018
Powered by nginx      Powered by FreeBSD      PHP Powered      Powered by MariaDB      ipv6 ready