<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Extensible Development &#187; apache</title>
	<atom:link href="http://blog.itwarlocks.com/tag/apache/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.itwarlocks.com</link>
	<description>Profession blog about Software Engineering, Web, *nix, Processes, Tools and more.</description>
	<lastBuildDate>Mon, 29 Mar 2010 12:20:30 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=abc</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Update: HTTPS Virtual Hosts on Ubuntu 8.04 LTS Server</title>
		<link>http://blog.itwarlocks.com/2009/07/16/update-https-virtual-hosts-on-ubuntu-8-04-lts-server/</link>
		<comments>http://blog.itwarlocks.com/2009/07/16/update-https-virtual-hosts-on-ubuntu-8-04-lts-server/#comments</comments>
		<pubDate>Thu, 16 Jul 2009 13:59:28 +0000</pubDate>
		<dc:creator><span property="dc:creator" resource="http://blog.itwarlocks.com/2009/07/16/update-https-virtual-hosts-on-ubuntu-8-04-lts-server/">Jeffrey Ridout</span></dc:creator>
				<category><![CDATA[Architecture]]></category>
		<category><![CDATA[apache]]></category>
		<category><![CDATA[gnutls]]></category>
		<category><![CDATA[http]]></category>
		<category><![CDATA[https]]></category>
		<category><![CDATA[mod_gnutls]]></category>
		<category><![CDATA[mod_ssl]]></category>
		<category><![CDATA[openssl]]></category>
		<category><![CDATA[ssl]]></category>
		<category><![CDATA[TLS]]></category>
		<category><![CDATA[ubuntu]]></category>
		<category><![CDATA[Virtual Hosting]]></category>

		<guid isPermaLink="false">http://blog.itwarlocks.com/?p=219</guid>
		<description><![CDATA[Original article: 
The gnutls and gcrypt packages were updated for Ubuntu 8.04, but they are still not right for mod_gnutls.

libgnutls-dev (2.0.4-1ubuntu2.5)
libgnutls13 (2.0.4-1ubuntu2.5)
gnutls-bin (2.0.4-1ubuntu2.5)
libgcrypt11-dev (1.2.4-2ubuntu7)
libgcrypt11 (1.2.4-2ubuntu7)

]]></description>
			<content:encoded><![CDATA[<div class="messagebox"><strong>Original article:</strong> <a href="http://blog.itwarlocks.com/2009/06/18/https-virtual-hosts-on-ubuntu-8-04-lts-server/" target="_self">HTTPS Virtual Hosts on Ubuntu 8.04 LTS Server</a></div>
<p>The gnutls and gcrypt packages were updated for Ubuntu 8.04, but they are still not right for mod_gnutls.</p>
<ul>
<li>libgnutls-dev (2.0.4-1ubuntu2.5)</li>
<li>libgnutls13 (2.0.4-1ubuntu2.5)</li>
<li>gnutls-bin (2.0.4-1ubuntu2.5)</li>
<li>libgcrypt11-dev (1.2.4-2ubuntu7)</li>
<li>libgcrypt11 (1.2.4-2ubuntu7)</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://blog.itwarlocks.com/2009/07/16/update-https-virtual-hosts-on-ubuntu-8-04-lts-server/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>HTTPS Virtual Hosts on Ubuntu 8.04 LTS Server</title>
		<link>http://blog.itwarlocks.com/2009/06/18/https-virtual-hosts-on-ubuntu-8-04-lts-server/</link>
		<comments>http://blog.itwarlocks.com/2009/06/18/https-virtual-hosts-on-ubuntu-8-04-lts-server/#comments</comments>
		<pubDate>Thu, 18 Jun 2009 14:54:10 +0000</pubDate>
		<dc:creator><span property="dc:creator" resource="http://blog.itwarlocks.com/2009/06/18/https-virtual-hosts-on-ubuntu-8-04-lts-server/">Jeffrey Ridout</span></dc:creator>
				<category><![CDATA[Architecture]]></category>
		<category><![CDATA[apache]]></category>
		<category><![CDATA[gnutls]]></category>
		<category><![CDATA[http]]></category>
		<category><![CDATA[https]]></category>
		<category><![CDATA[mod_gnutls]]></category>
		<category><![CDATA[mod_ssl]]></category>
		<category><![CDATA[openssl]]></category>
		<category><![CDATA[ssl]]></category>
		<category><![CDATA[TLS]]></category>
		<category><![CDATA[ubuntu]]></category>
		<category><![CDATA[Virtual Hosting]]></category>

		<guid isPermaLink="false">http://blog.itwarlocks.com/?p=167</guid>
		<description><![CDATA[

Updated

2009-07-16: 

Introduction
Normally having multiple virtual hosts on Apache with HTTPS is not possible. HTTPS is short for HTTP/SSL which means HTTP encapsulated by SSL (Secure Socket Layer). This means the HTTP traffic sent to the Apache webserver is encrypted using SSL.
The VirtualHost definitions in the Apache configuration are used to specify the SSL options. To [...]]]></description>
			<content:encoded><![CDATA[<div class="warning">
<div class="boxcaption">
<h3>Updated</h3>
</div>
<div class="box">2009-07-16: <a href="http://blog.itwarlocks.com/2009/07/16/update-https-virtual-hosts-on-ubuntu-8-04-lts-server/" target="_self">Update: HTTPS Virtual Hosts on Ubuntu 8.04 LTS Server</a></div>
</div>
<h3>Introduction</h3>
<p>Normally having multiple <a title="VHosts documentation on apache.org" href="http://httpd.apache.org/docs/2.2/vhosts/">virtual hosts</a> on Apache with HTTPS is not possible. HTTPS is short for HTTP/SSL which means HTTP encapsulated by SSL (Secure Socket Layer). This means the HTTP traffic sent to the Apache webserver is encrypted using SSL.</p>
<p>The VirtualHost definitions in the Apache configuration are used to specify the SSL options. To know what VirtualHost Apache should use, Apache sneak previews the HTTP header to look for the &#8220;HOST&#8221; field. With HTTPS the HTTP header can only be previewed after the SSL connection has been established, so technically Apache cannot know what VirtualHost definition to use to set up the SSL connection.</p>
<p>Apache could use the correct SSL options if it knew what VirtualHost definition it should use and luckily that is possible. SSL supports several encryption protocols, the newest being <a title="TLS on Wikipedia" href="http://en.wikipedia.org/wiki/Transport_Layer_Security" target="_blank">TLS (Transport Layer Security)</a>. Technically this is a replacement for SSL and would result in HTTP/TLS instead of HTTP/SSL. TLS has a feature called <a title="SNI on Wikipedia" href="http://en.wikipedia.org/wiki/Server_Name_Indication">SNI (Server Name Indication)</a> which is the equivalent of HTTP&#8217;s &#8220;HOST&#8221; field.</p>
<p>There are a few problems with SNI though.The standard SSL/TLS module that comes with Apache is mod_ssl, which is based on <a title="OpenSSL homepage." href="http://www.openssl.org/" target="_blank">OpenSSL</a>. Unfortunately OpenSSL does not support SNI in it&#8217;s TLS implementation yet. It has been added to <a title="Virtual Hosting, SSL and SNI" href="http://weblogs.mozillazine.org/gerv/archives/2007/08/virtual_hosting_ssl_and_sni.html" target="_blank">OpenSSL 0.99 and backported to 0.98</a>, but <a title="mod_ssl documentation on apache.org" href="http://httpd.apache.org/docs/2.2/mod/mod_ssl.html" target="_blank">mod_ssl</a> doesn&#8217;t support it. <a title="GnuTLS homepage." href="http://www.gnu.org/software/gnutls/" target="_blank">GnuTLS</a> is an alternative to OpenSSL and it does support the SNI feature in TLS. A GnuTLS based module for Apache is <a title="mod_gnutls homepage." href="http://www.outoforder.cc/projects/apache/mod_gnutls/" target="_blank">mod_gnutls</a>, but this is not available as a Ubuntu binary. Internet Explorer 7 only supports SNI on Windows Vista, thus causing the same problem as SSL on Windows XP.<br />
<span id="more-167"></span></p>
<h3>Adding mod_gnutls to Apache</h3>
<p>There is no package for mod_gnutls available for Ubuntu so it has to be built from source. mod_gnutls has a few prerequisites that make it tricky to build on Ubuntu:</p>
<ul>
<li>GnuTLS (&gt;= 2.4.0)
<ul>
<li>libgcrypt (&gt;= 1.4.0)</li>
<li>libtasn1 (included in GnuTLS source package)</li>
<li>libgpgerror (&gt;= 1.4.0)</li>
</ul>
</li>
</ul>
<p>The libgcrypt version available for Ubuntu 8.04 is 1.4.1, but unfortunately that doesn&#8217;t seem to work. that means we&#8217;ll need to build libgcrypt from source too. The standard libgcrypt11 and libgcrypt11-dev packages are hard to remove and the lib-gpgerror that comes with it is fine, so you might want to leave the old libgcrypt in place and just overwrite them. (The standard libgcrypt is installed in /lib)</p>
<div>
<div class="wp-caption">Listing 1; Get, compile and install libgcrypt</div>
<pre class="brush: shell">
~% mkdir gnutls
~% cd gnutls
~/gnutls% sudo apt-get install libgcrypt11 libgcrypt-dev
~/gnutls% sudo rm /lib/libgcrypt*
~/gnutls% wget &quot;ftp://ftp.gnupg.org/gcrypt/libgcrypt/libgcrypt-1.4.4.tar.gz&quot;
~/gnutls% tar -xvzf libgcrypt-1.4.4.tar.gz
~/gnutls% cd libgcrypt-1.4.4
~/gnutls/libgcrypt-1.4.4% ./configure --prefix=/usr
~/gnutls/libgcrypt-1.4.4% make
~/gnutls/libgcrypt-1.4.4% sudo make install
~/gnutls/libgcrypt-1.4.4% sudo ldconfig
~/gnutls/libgcrypt-1.4.4% cd ..
~/gnutls% rm -Rf libgcrypt-1.4.4
</pre>
</div>
<p>It should be no problem to build GnuTLS now.</p>
<pre class="brush: shell">
~/gnutls% wget &quot;ftp://ftp.gnupg.org/gcrypt/gnutls/gnutls-2.8.1.tar.bz2&quot;
~/gnutls% tar -xvjf gnutls-2.8.1
~/gnutls% cd gnutls-2.8.1
~/gnutls/gnutls-2.8.1% ./configure --prefix=/usr
~/gnutls/gnutls-2.8.1% make
~/gnutls/gnutls-2.8.1% sudo make install
~/gnutls/gnutls-2.8.1% sudo ldconfig
~/gnutls/gnutls-2.8.1% cd ..
~/gnutls% rm -Rf gnutls-2.8.1
</pre>
<p>With all the prerequisites in place it&#8217;s time to build mod_gnutls. The configure probably will not be able to find Apache&#8217;s apxs2, so find that first.</p>
<pre class="brush: shell">
~/gnutls% which apxs2
/usr/bin/apxs2
~/gnutls% wget &quot;http://www.outoforder.cc/downloads/mod_gnutls/mod_gnutls-0.5.5.tar.bz2&quot;
~/gnutls% tar -xvjf mod_gnutls-0.5.5.tar.bz2
~/gnutls% cd mod_gnutls-0.5.5
~/gnutls/mod_gnutls-0.5.5% ./configure --prefix=/usr --with-apxs=/usr/bin/apxs2
~/gnutls/mod_gnutls-0.5.5% make
~/gnutls/mod_gnutls-0.5.5% sudo make install
~/gnutls/mod_gnutls-0.5.5% cd ..
~/gnutls% rm -Rf mod_gnutls-0.5.5
</pre>
<p>Depending on your Apache configuration practice, the module might be loaded differently. Here&#8217;s my way.</p>
<div>
<div class="wp-caption">/usr/etc/apache2/mods-available/gnutls.load</div>
<pre class="brush: text">
LoadModule gnutls_module /usr/lib/apache2/modules/mod_gnutls.so
</pre>
</div>
<div>
<div class="wp-caption">/usr/etc/apache2/mods-available/gnutls.conf</div>
<pre class="brush: text">
GnuTLSCache dbm /var/cache/apache2/gnutls_cache
</pre>
</div>
<div>
<pre class="brush: shell">
~/gnutls% cd /etc/apache2/mods-enabled
/etc/apache2/mods-enabled% sudo ln -s ../mods-available/gnutls.load
/etc/apache2/mods-enabled% sudo ln -s ../mods-available/gnutls.conf
/etc/apache2/mods-enabled% sudo touch /var/cache/apache2/gnutls_cache
/etc/apache2/mods-enabled% sudo chown www-data:www-data /var/cache/apache2/gnutls_cache
</pre>
</div>
<p>That should be it, your mod_gnutls should be functional after restarting Apache.</p>
<p>Creating certificates for GnuTLS</p>
<p>Creating certificates is quite easy, as explained in the articles below. For GnuTLS to accept the certificates however, no MD5 may be used since it&#8217;s considered unsecure. Using SHA256 or SHA512 instead is much more secure. The examples below use OpenSSL, but</p>
<div>
<div class="wp-caption">openssl.cnf</div>
<pre class="brush: text">
...
[ CA_default ]
...
default_md				= sha256
...
</pre>
</div>
<p><a title="Setting up OpenSSL to Create Certificates" href="http://www.flatmtn.com/article/setting-openssl-create-certificates" target="_blank">Setting up OpenSSL to Create Certificates</a><br />
<a title="Setting up SSL Certificates on Apache" href="http://www.flatmtn.com/article/setting-ssl-certificates-apache" target="_blank">Setting up SSL Certificates on Apache</a><br />
<a title="Creating PKCS12 Certificates" href="http://www.flatmtn.com/article/creating-pkcs12-certificates" target="_blank">Creating PKCS12 Certificates</a></p>
<h3>Creating name based Virtual Hosts with HTTPS</h3>
<p>With mod_gnutls in place and the certificates ready name based Virtual Hosts can be added to Apache.</p>
<div>
<div class="wp-caption">/etc/apache2/sites-available/https-vhost1.conf</div>
<pre class="brush: text">
&lt;VirtualHost *:443&gt;
    ServerAdmin admin@example.org
    ServerName vhost1.example.org:443
    DocumentRoot /var/vhosts/vhost1.example.org/public_html

    # Configure TLS
    GnuTLSEnable On
    GnuTLSPriorities NORMAL
    GnuTLSCertificateFile /var/vhosts/vhost1.example.org/ssl.crt/vhost1.example.org-cert.pem
    GnuTLSKeyFile /var/vhosts/vhost1.example.org/ssl.key/vhost1.example.org-key.pem
&lt;/VirtualHost&gt;
</pre>
</div>
<div>
<div class="wp-caption">/etc/apache2/sites-available/https-vhost2.conf</div>
<pre class="brush: text">
&lt;VirtualHost *:443&gt;
    ServerAdmin admin@example.org
    ServerName vhost2.example.org:443
    DocumentRoot /var/vhosts/vhost2.example.org/public_html

    # Configure TLS
    GnuTLSEnable On
    GnuTLSPriorities NORMAL
    GnuTLSCertificateFile /var/vhosts/vhost2.example.org/ssl.crt/vhost2.example.org-cert.pem
    GnuTLSKeyFile /var/vhosts/vhost2.example.org/ssl.key/vhost2.example.org-key.pem
&lt;/VirtualHost&gt;
</pre>
</div>
<p>Access vhost1.example.ogr and vhost2.example.org and examine the certificates, unless you used the same certificate files, they should be different.</p>
<h3>Conclusion</h3>
<p>Until OpenSSL and mod_ssl start supporting TLS&#8217; SNI and everyone stops using IE7 (please do!), or until mod_gnutls becomes available for Ubuntu through apt, this procedure should keep your Virtual Hosts secure.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.itwarlocks.com/2009/06/18/https-virtual-hosts-on-ubuntu-8-04-lts-server/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>PHP based authentication for mod_dav</title>
		<link>http://blog.itwarlocks.com/2009/04/27/php-based-authentication-for-mod_dav/</link>
		<comments>http://blog.itwarlocks.com/2009/04/27/php-based-authentication-for-mod_dav/#comments</comments>
		<pubDate>Mon, 27 Apr 2009 12:24:41 +0000</pubDate>
		<dc:creator><span property="dc:creator" resource="http://blog.itwarlocks.com/2009/04/27/php-based-authentication-for-mod_dav/">Jeffrey Ridout</span></dc:creator>
				<category><![CDATA[Software Engineering]]></category>
		<category><![CDATA[apache]]></category>
		<category><![CDATA[authentication]]></category>
		<category><![CDATA[mod_auth]]></category>
		<category><![CDATA[mod_auth_script]]></category>
		<category><![CDATA[mod_dav]]></category>
		<category><![CDATA[mod_dav_fs]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[WebDAV]]></category>

		<guid isPermaLink="false">http://blog.itwarlocks.com/?p=43</guid>
		<description><![CDATA[To be able to create a Document Management System in Word using WebDAV on Apache webserver, I did some research on mod_dav, an Apache module to provide WebDAV support. It appears that the authaurisation part of mod_dav is limited to "allow all" / "deny all", which did not quite suite my needs. The solution needed more fine-grained authorisation on a per-directory level. Another problem was that the authentication and authorisation data was in a database.]]></description>
			<content:encoded><![CDATA[<h3>Introduction</h3>
<p>To be able to create a <a href="http://blog.itwarlocks.com/2009/04/24/document-management-system-in-word-using-webdav/" target="_self">Document Management System in Word using WebDAV</a> on <a title="Apache webserver official website." href="http://httpd.apache.org/" target="_blank">Apache webserver</a>, I did some research on <a title="Mod_dav's official website." href="http://www.webdav.org/mod_dav/" target="_blank">mod_dav</a>, an Apache <a title="Official Apache webserver module registry." href="http://modules.apache.org/" target="_blank">module</a> to provide <a id="aptureLink_vPPLLegkod" title="Information on WebDAV on Wikipedia.org" href="http://en.wikipedia.org/wiki/WebDAV">WebDAV</a> support. It appears that the authaurisation part of mod_dav is limited to &#8220;allow all&#8221; / &#8220;deny all&#8221;, which did not quite suite my needs. The solution needed more fine-grained authorisation on a per-directory level. Another problem was that the authentication and authorisation data was in a database.<span id="more-43"></span></p>
<h3>Issues</h3>
<ol>
<li>Mod_dav does not have a fine-grained authorisation system on a per-directory level.</li>
<li>Directory structure is known but unstable.</li>
<li>Authorisation data is held in a database and requires complex SQL.</li>
</ol>
<h3>Solution</h3>
<h4>Fine-grained authorisation</h4>
<p>I started by looking at <a title="Mod_auth's official website." href="http://httpd.apache.org/docs/2.0/mod/mod_auth.html" target="_blank">mod_auth</a> and <a title="Mod_auth_dbd's official website." href="http://httpd.apache.org/docs/2.2/mod/mod_authn_dbd.html" target="_blank">mod_auth_dbd</a> (in combination with <a title="Mod_dbd's official website." href="http://httpd.apache.org/docs/2.2/mod/mod_dbd.html" target="_blank">mod_dbd</a>), but the SQL useable by mod_dbd only received the username and password as parameters, not the requested <a id="aptureLink_c3qbx5wU5Q" title="Information on URI on Wikipedia.org" href="http://en.wikipedia.org/wiki/Uniform%20Resource%20Identifier">URI</a>. What mod_auth module could possibly be used? The solution to this was quite unexpected: <a title="Mod_auth_script's official website." href="http://mod-auth-script.sourceforge.net/" target="_blank">mod_auth_script</a>.</p>
<p>Mod_auth_script allows CGI or <a title="PHP's official website." href="http://www.php.net" target="_blank">PHP</a> scripts to handle the authorisation. By looking at the <a title="PHP.net official documentation on $_SERVER predefined variables." href="http://www.php.net/manual/en/reserved.variables.server.php" target="_blank">REQUEST_URI</a> the script can easily decide to simulate authorisation by invalidating the authorisation.</p>
<p>I created a PHP script that at first would log the REQUEST_URI and allow access for any username and password. All that is left now is to handle authorisation based on the directory structure and fetch the real authorisation from the database.</p>
<div>
<div class="wp-caption" style="text-align: left">Listing 1: Apache config</div>
<pre class="brush: xml">
&lt;Location /dav&gt;
	Options			+Indexes
	Dav				On
	AuthType		Basic
	AuthName		&quot;PHP based authentication for mod_dav&quot;
	AuthScriptFile	/any_full_path_your_apache_can_get_to/.auth.php
	Require			valid-user
&lt;/Location&gt;
</pre>
</div>
<div>
<div class="wp-caption" style="text-align: left">Listing 2: .auth.php (version 0.0.1)</div>
<pre class="brush: php">
&lt;?php
/**
 * PHP based authentication for mod_dav
 * @author Jeffrey Ridout
 * @link http://blog.itwarlocks.com
 * @license http://creativecommons.org/licenses/by-sa/3.0
 * @date 2009-04-27
 * @version 0.0.1
 */

/**
 * $_SERVER[&#039;REQUEST_URI&#039;] contains the location accessed by the WebDAV client.
 * Using parse_url enables easy access to all URL parts.
 */
$requestURI = parse_url($_SERVER[&#039;REQUEST_URI&#039;]);

/**
 * Accessing some pages directly, like a page containing phpinfo(), will result in several
 * calls to .auth.php. These do not need to be logged.
 */
if ($requestURI[&#039;path&#039;] != $_SERVER[&#039;SCRIPT_NAME&#039;]) {
	$logFile = fopen(&#039;auth.log&#039;, &#039;a&#039;);
	fwrite($logFile, &quot;${_SERVER[&#039;PHP_AUTH_USER&#039;]}:${_SERVER[&#039;PHP_AUTH_PW&#039;]}@${requestURI[&#039;path&#039;]}\n&quot;);
	fclose($logFile);
}

/**
 * At this stage we always allow access.
 * mod_auth_script catches custom headers to define the result to mod_auth.
 * auth-script:allow|deny|prompt Defines the authentication result.
 */
header(&#039;auth-script:allow&#039;);

/* EOF */
?&gt;
</pre>
</div>
<h4>Directory structure</h4>
<p>The sub-directories are named according to a strict rule, but they are unknown to exist at design-time. Therefore the script needs to analyse the destination location and extract the key to be used for authorisation.<sup>1</sup></p>
<div>
<div class="wp-caption" style="text-align: left">Listing 3: Directory structure</div>
<pre class="brush: text">
/&lt;Day&gt;/&lt;Month&gt;/&lt;Year&gt;/&lt;MemberID&gt;
	&lt;Day&gt;:		00 - 31
	&lt;Month&gt;:		00 - 12
	&lt;Year&gt;:		00 - 99
	&lt;MemberID&gt;:	DDMMYY#####
</pre>
</div>
<div id="attachment_77" class="wp-caption alignnone" style="width: 118px"><a title="Directory structure" rel="lightbox-43" href="http://blog.itwarlocks.com/wp-content/uploads/2009/04/directory_structure.png"><img class="size-medium wp-image-77" title="Directory structure" src="http://blog.itwarlocks.com/wp-content/uploads/2009/04/directory_structure-108x300.png" alt="Directory structure" width="108" height="300" /></a><p class="wp-caption-text">Directory structure</p></div>
<p>This directory structure allows for a quite straight forward check to get the MemberID.<sup>2</sup></p>
<div>
<div class="wp-caption" style="text-align: left">Listing 3: .auth.php (version 0.0.2)</div>
<pre class="brush: php">
&lt;?php
/**
 * PHP based authentication for mod_dav
 * @author Jeffrey Ridout
 * @link http://blog.itwarlocks.com
 * @license http://creativecommons.org/licenses/by-sa/3.0
 * @date 2009-04-27
 * @version 0.0.2
 */

/**
 * $_SERVER[&#039;REQUEST_URI&#039;] contains the location accessed by the WebDAV client.
 * Using parse_url enables easy access to all URL parts.
 */
$requestURI = parse_url($_SERVER[&#039;REQUEST_URI&#039;]);

/**
 * Accessing some pages directly, like a page containing phpinfo(), will result in several
 * calls to .auth.php. These do not need to be logged.
 */
if ($requestURI[&#039;path&#039;] != $_SERVER[&#039;SCRIPT_NAME&#039;]) {
	$logFile = fopen(&#039;auth.log&#039;, &#039;a&#039;);
	fwrite($logFile, &quot;${_SERVER[&#039;PHP_AUTH_USER&#039;]}:${_SERVER[&#039;PHP_AUTH_PW&#039;]}@${requestURI[&#039;path&#039;]}\n&quot;);
	fclose($logFile);
}

/**
 * At stage 2 we can simulate a real authorisation lookup by using a simple array.
 * mod_auth_script catches custom headers to define the result to mod_auth.
 * auth-script:allow|deny|prompt Defines the authentication result.
 */
$blocked = array (
	&#039;01010154321&#039;,
	&#039;31037912345&#039;
);

/**
 * The directory name to search for might be in the path or destination filename.
 * .../blocked-dir
 * or
 * .../blocked-dir/
 * or
 * .../blocked-dir/...
 */
$path = dirname($requestURI[&#039;path&#039;]);
$dir = basename($path);
$match = basename($rURL[&#039;path&#039;]);
if (preg_match(&#039;#$\d{11}^#&#039;, $dir)) {
	$match = $dir;
}
if (!in_array($match, $blocked)) {
	header(&#039;auth-script:allow&#039;);
} else {
	/* Simulate authorisation by denying access.
	 * Normal authentication would return auth-script:prompt
	 */
	header(&#039;auth-script:deny&#039;);
}

/* EOF */
?&gt;
</pre>
</div>
<h4>Authorisation data</h4>
<p>The data that determines the authorisation can come from different sources; files, LDAP, RADIUS, database, &#8230;</p>
<p>In my case the authorisation result was retrieved from complex SQL using a stored procedure in a Sybase Database.</p>
<p>Whatever you use as your source of authentication/authorisation, remember that request can be asynchrounous and parallel. For a file base method that means never locking the file and opening it as &#8220;read-only&#8221;, for an external connection based method, it means using persistent connections.</p>
<h3>Conclusion</h3>
<p>Just because mod-dav and mod_auth don&#8217;t support your exact needs, doesn&#8217;t mean there is no solution. Using mod_auth_script to create your own tailored flexible solution works perfectly. As an added bonus this adds a new processing layer to PHP by allowing PHP to be executed at a much earlier stage. Using a PHP script to authorise also allows authorisation of non-existing destination URLs or destinations which access is based on complex business logic.</p>
<p><a href="/license/#cc-by-sa"><img class="size-full wp-image-95 alignleft" style="margin-right: 1.5em" title="Creative Commons: Attribution Share Alike" src="http://blog.itwarlocks.com/wp-content/uploads/2009/04/cc-by-sa_88x31.png" alt="Creative Commons: Attribution Share Alike" width="88" height="31" /></a>All code in this post is licensed under Creative Commons Attribution Share Alike.</p>
<ol class="footnotes"><li id="footnote_0_43" class="footnote">You may notice that year only has 2 digits. so no, it&#8217;s not Y2K safe&#8230;</li><li id="footnote_1_43" class="footnote">MemberID in this case is the Norwegian version of a social security number.</li></ol>]]></content:encoded>
			<wfw:commentRss>http://blog.itwarlocks.com/2009/04/27/php-based-authentication-for-mod_dav/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Minified using disk
Page Caching using disk (enhanced) (user agent is rejected)

Served from: blog.itwarlocks.com @ 2010-09-05 05:32:31 -->