<?xml version="1.0"?>
<?xml-stylesheet type="text/css" href="http://wiki.nginx.org/skins/common/feed.css?303"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
		<id>http://wiki.nginx.org/index.php?title=ImapAuthenticateWithApachePhpScript&amp;feed=atom&amp;action=history</id>
		<title>ImapAuthenticateWithApachePhpScript - Revision history</title>
		<link rel="self" type="application/atom+xml" href="http://wiki.nginx.org/index.php?title=ImapAuthenticateWithApachePhpScript&amp;feed=atom&amp;action=history"/>
		<link rel="alternate" type="text/html" href="http://wiki.nginx.org/index.php?title=ImapAuthenticateWithApachePhpScript&amp;action=history"/>
		<updated>2013-05-18T10:11:37Z</updated>
		<subtitle>Revision history for this page on the wiki</subtitle>
		<generator>MediaWiki 1.19.0</generator>

	<entry>
		<id>http://wiki.nginx.org/index.php?title=ImapAuthenticateWithApachePhpScript&amp;diff=178&amp;oldid=prev</id>
		<title>Uidvalidity: added missing brackets</title>
		<link rel="alternate" type="text/html" href="http://wiki.nginx.org/index.php?title=ImapAuthenticateWithApachePhpScript&amp;diff=178&amp;oldid=prev"/>
				<updated>2011-10-11T17:45:46Z</updated>
		
		<summary type="html">&lt;p&gt;added missing brackets&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;== Using a php script on apache server as the auth backend ==&lt;br /&gt;
&lt;br /&gt;
Start with the configuration from NginxImapProxyExample. For detail information about different configuration parameters, see the NginxMailCoreModule page.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
# Your Proxy server for pop/imap is running on 192.168.1.1&lt;br /&gt;
# You have 2 backend pop/imap servers: 192.168.1.22 and 192.168.1.33&lt;br /&gt;
# You have a webserver that you will use for the authentication and redirection logic 192.168.1.44.&lt;br /&gt;
# The authentication script is /mail/auth.php&lt;br /&gt;
&lt;br /&gt;
nginx.conf&lt;br /&gt;
&lt;br /&gt;
&amp;lt;geshi lang=&amp;quot;nginx&amp;quot;&amp;gt;&lt;br /&gt;
user  nobody;&lt;br /&gt;
worker_processes  1;&lt;br /&gt;
error_log  logs/error.log  info;&lt;br /&gt;
pid        logs/nginx.pid;&lt;br /&gt;
&lt;br /&gt;
events {&lt;br /&gt;
  worker_connections  1024;&lt;br /&gt;
  multi_accept on;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
mail {&lt;br /&gt;
  auth_http  192.168.1.44:80/mail/auth.php;&lt;br /&gt;
  pop3_capabilities  &amp;quot;TOP&amp;quot;  &amp;quot;USER&amp;quot;;&lt;br /&gt;
  imap_capabilities  &amp;quot;IMAP4rev1&amp;quot;  &amp;quot;UIDPLUS&amp;quot;;&lt;br /&gt;
&lt;br /&gt;
  server {&lt;br /&gt;
    listen     110;&lt;br /&gt;
    protocol   pop3;&lt;br /&gt;
    proxy      on;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  server {&lt;br /&gt;
    listen     143;&lt;br /&gt;
    protocol   imap;&lt;br /&gt;
    proxy      on;&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/geshi&amp;gt;&lt;br /&gt;
&lt;br /&gt;
/mail/auth.php&lt;br /&gt;
&lt;br /&gt;
&amp;lt;geshi lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;?php&lt;br /&gt;
/*&lt;br /&gt;
Nginx sends headers as&lt;br /&gt;
Auth-User: somuser&lt;br /&gt;
Auth-Pass: somepass&lt;br /&gt;
On my php app server these are seen as&lt;br /&gt;
HTTP_AUTH_USER and HTTP_AUTH_PASS&lt;br /&gt;
*/&lt;br /&gt;
if (!isset($_SERVER[&amp;quot;HTTP_AUTH_USER&amp;quot;] ) || !isset($_SERVER[&amp;quot;HTTP_AUTH_PASS&amp;quot;] )){&lt;br /&gt;
  fail();&lt;br /&gt;
}&lt;br /&gt;
$username=$_SERVER[&amp;quot;HTTP_AUTH_USER&amp;quot;] ;&lt;br /&gt;
$userpass=$_SERVER[&amp;quot;HTTP_AUTH_PASS&amp;quot;] ;&lt;br /&gt;
$protocol=$_SERVER[&amp;quot;HTTP_AUTH_PROTOCOL&amp;quot;] ;&lt;br /&gt;
// default backend port&lt;br /&gt;
$backend_port=110;&lt;br /&gt;
if ($protocol==&amp;quot;imap&amp;quot;) {&lt;br /&gt;
  $backend_port=143;&lt;br /&gt;
}&lt;br /&gt;
if ($protocol==&amp;quot;smtp&amp;quot;) {&lt;br /&gt;
  $backend_port=25;&lt;br /&gt;
}&lt;br /&gt;
// nginx likes ip address so if your&lt;br /&gt;
// application gives back hostname, convert it to ip address here&lt;br /&gt;
$backend_ip[&amp;quot;mailhost01&amp;quot;] =&amp;quot;192.168.1.22&amp;quot;;&lt;br /&gt;
$backend_ip[&amp;quot;mailhost02&amp;quot;] =&amp;quot;192.168.1.33&amp;quot;;&lt;br /&gt;
// Authenticate the user or fail&lt;br /&gt;
if (!authuser($username,$userpass)){&lt;br /&gt;
  fail();&lt;br /&gt;
  exit;&lt;br /&gt;
}&lt;br /&gt;
// Get the server for this user if we have reached so far&lt;br /&gt;
$userserver=getmailserver($username);&lt;br /&gt;
// Get the ip address of the server&lt;br /&gt;
// We are assuming that you backend returns hostname&lt;br /&gt;
// We try to get the ip else return what we got back&lt;br /&gt;
$server_ip=(isset($backend_ip[$userserver]))?$backend_ip[$userserver] :$userserver;&lt;br /&gt;
// Pass!&lt;br /&gt;
pass($server_ip, $backend_port);&lt;br /&gt;
&lt;br /&gt;
//END&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
function authuser($user,$pass){&lt;br /&gt;
  // put your logic here to authen the user to any backend&lt;br /&gt;
  // you want (datbase, ldap, etc)&lt;br /&gt;
  // for example, we will just return true;&lt;br /&gt;
  return true;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
function getmailserver($user){&lt;br /&gt;
  // put the logic here to get the mailserver&lt;br /&gt;
  // backend for the user. You can get this from&lt;br /&gt;
  // some database or ldap etc&lt;br /&gt;
  // dummy logic, all users that start with a,c,f and g get mailhost01&lt;br /&gt;
  // the others get mailhost02&lt;br /&gt;
  if in_array(substr($user,0,1), array(&amp;quot;a&amp;quot;, &amp;quot;c&amp;quot;, &amp;quot;f&amp;quot;, &amp;quot;g&amp;quot;)){&lt;br /&gt;
    return &amp;quot;mailhost01&amp;quot;;&lt;br /&gt;
  } else {&lt;br /&gt;
    return &amp;quot;mailhost02&amp;quot;;&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
function fail(){&lt;br /&gt;
  header(&amp;quot;Auth-Status: Invalid login or password&amp;quot;);&lt;br /&gt;
  exit;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
function pass($server,$port){&lt;br /&gt;
  header(&amp;quot;Auth-Status: OK&amp;quot;);&lt;br /&gt;
  header(&amp;quot;Auth-Server: $server&amp;quot;);&lt;br /&gt;
  header(&amp;quot;Auth-Port: $port&amp;quot;);&lt;br /&gt;
  exit;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/geshi&amp;gt;&lt;/div&gt;</summary>
		<author><name>Uidvalidity</name></author>	</entry>

	</feed>