<?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>The Dumping Ground &#187; PHP</title>
	<atom:link href="http://ardvaark.net/category/technology/php/feed" rel="self" type="application/rss+xml" />
	<link>http://ardvaark.net</link>
	<description>And who cares?</description>
	<lastBuildDate>Fri, 30 Jul 2010 21:14:00 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Updated Mantis Basic Authentication Code</title>
		<link>http://ardvaark.net/updated-mantis-basic-authentication-code</link>
		<comments>http://ardvaark.net/updated-mantis-basic-authentication-code#comments</comments>
		<pubDate>Thu, 10 Nov 2005 17:22:36 +0000</pubDate>
		<dc:creator>brian</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false"></guid>
		<description><![CDATA[I&#8217;ve updated my post on Making Mantis with Basic Authentication Not Suck to fix an oversight I discovered in the original version. Basically, direct links into Mantis would not work, since most of the Mantis pages redirect to the login page when a user has not yet authenticated. The modification was simply to modify the [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve updated my post on <a href="/making_mantis_with_basic_authentication_not_suck.html">Making Mantis with Basic Authentication Not Suck</a> to fix an oversight I discovered in the original version.</p>
<p>Basically, direct links into Mantis would not work, since most of the Mantis pages redirect to the login page when a user has not yet authenticated.  The modification was simply to modify the login page to detect basic authentication and redirect to the previously modified login script.</p>
]]></content:encoded>
			<wfw:commentRss>http://ardvaark.net/updated-mantis-basic-authentication-code/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Making Mantis with Basic Authentication Not Suck</title>
		<link>http://ardvaark.net/making-mantis-with-basic-authentication-not-suck</link>
		<comments>http://ardvaark.net/making-mantis-with-basic-authentication-not-suck#comments</comments>
		<pubDate>Thu, 10 Nov 2005 17:17:21 +0000</pubDate>
		<dc:creator>brian</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false"></guid>
		<description><![CDATA[At work, I&#8217;ve been doing a lot of system setup and administration. While it&#8217;s definitely beyond my job description as a Software Architect, there&#8217;s nobody else than can do it right on my team &#8211; and thus it falls to me. When administrating a large number of servers, it becomes quickly apparant that a centralized [...]]]></description>
			<content:encoded><![CDATA[<p>At <a href="http://loc.gov">work</a>, I&#8217;ve been doing a lot of system setup and administration.  While it&#8217;s definitely beyond my job description as a Software Architect, there&#8217;s nobody else than can do it right on my team &#8211; and thus it falls to me.</p>
<p>When administrating a large number of servers, it becomes quickly apparant that a centralized directory of credentials is absolutely necessary.  The obvious solution is <a href="http://www.openldap.org/">OpenLDAP</a>.  After creating a directory, though, it becomes necessary to integrate the various applications into the directory so that the users may use the same credentials across all applications.  (I&#8217;m not even talking about single sign-on.  That&#8217;s a whole other kettle of fish.)</p>
<p>In some cases, it&#8217;s built right in.  For example, our Linux servers use <a href="http://www.padl.com/OSS/pam_ldap.html">pam_ldap</a> and <a href="http://www.padl.com/OSS/nss_ldap.html">nss_ldap</a> to tie in the operating system.  For <a href="http://httpd.apache.org">Apache</a>, the modules <a href="http://httpd.apache.org/docs/2.0/mod/mod_ldap.html">mod_ldap</a> and <a href="http://httpd.apache.org/docs/2.0/mod/mod_auth_ldap.html">mod_auth_ldap</a> provide the necessary integration; and some web applications, such as <a href="http://subversion.tigris.org/">Subversion</a> fall in line quite naturally.</p>
<p>Many web applications, though, support their own authentication via a web page, and those applications often do not fit in quite the way you would hope.  Two such applications we use are <a href="http://www.mediawiki.org/">MediaWiki</a> and <a href="http://mantisbt.org">MantisBT</a>.  In the case of MediaWiki, I found a <a href="http://meta.wikimedia.org/wiki/Auto_Login_via_REMOTE_USER">series of modifications</a> that pretty much worked.  I had to tweak a few things, and while I&#8217;m not going to write about them here, feel free to <a href="/user/1/contact">contact me</a> if you&#8217;d like to know what I did.</p>
<p>Mantis, though, ostensibly supports basic authentication.  The <a href="http://manual.mantisbt.org/manual.configuration.authentication.php">authentication section in the manual</a> has a specific <code>BASIC_AUTH</code> option.  The problem is that it doesn&#8217;t work quite right.</p>
<p>I am protecting my Mantis directory with an Apache authentication directive.  In the <code>.htaccess</code> in the Mantis directory, I require basic authentication, and the authenticated user must be a member of the <code>cn=Mantis</code> group in the directory.</p>
<p><code><br />
SSLRequireSSL</p>
<p>AuthType Basic<br />
AuthName "Mantis"</p>
<p><IfDefine AUTH_LDAP><br />
        AuthLDAPEnabled on<br />
        AuthLDAPUrl     ldaps://ldap_server/ou=people,dc=example,dc=com?uid?sub?objectClass=account</p>
<p>        AuthLDAPGroupAttributeIsDN on<br />
        require group cn=Mantis,ou=groups,dc=example,dc=com<br />
</IfDefine></p>
<p><IfDefine !AUTH_LDAP><br />
        order deny,allow<br />
        deny from all<br />
</IfDefine><br />
</code></p>
<p>The Mantis configuration directive to use basic authentication is set  in the <code>config_inc.php</code> file.</p>
<p><code><br />
# --- authentication settings ---<br />
$g_login_method = BASIC_AUTH;<br />
</code></p>
<p>So in this scenario, you would never be able to get to the login page without authenticating with the web server.  Mantis, rather than treating the basic auth as authoratitive, it takes the username and password received and then tries to log in using them (at least in version 0.19.3).  I&#8217;m not sure the when that would be the right thing to do, but it certainly isn&#8217;t in this case because it requires me to keep the password in the directory synchronized with the password in the Mantis database.</p>
<p>So our goal is to make Mantis ignore its own password and simply think it has successfully authenticated when <code>g_login_method</code> is set to <code>BASIC_AUTH</code>.</p>
<p>As a nice feature, the <code>core/authentication_api.php</code> file has support for auto-creation of a user if it doesn&#8217;t exist when logging in via basic authentication.  We want to keep that functionality.  However, it inserts the basic authentication password into the database.  While it really shouldn&#8217;t hurt anything, I&#8217;d like to keep it seperate for clarity.  So on line 77, I&#8217;ve modified the call to <code>user_create()</code> in the <code>auth_attempt_login()</code> to generate a random password and insert that into the database.</p>
<p><code><br />
# Modified to generate a random password.<br />
# Since the basic authentication should be authoratative, then<br />
# this password is just a dummy password, and should never be used.<br />
# -- BCV<br />
$t_cookie_string = user_create( $p_username, auth_generate_random_password() );<br />
</code></p>
<p>The next step is to make <code>auth_attempt_login()</code> succeed when basic authentication has been used.  Modifying the <code>if</code> block around the <code>auth_does_password_match()</code>, we use some short-circuiting to skip the check (and therefore always succeed) if the login method equals <code>BASIC_AUTH</code>.</p>
<p><code><br />
# Since basic auth is authoratative, and assuming that this page<br />
# cannot be viewed unless it has already succeeded, then don't<br />
# bother checking a password, and just do a valid login.<br />
# -- BCV<br />
if (BASIC_AUTH != $t_login_method &#038;&#038; !auth_does_password_match( $t_user_id, $p_password ) ) {<br />
      user_increment_failed_login_count( $t_user_id );<br />
      return false;<br />
}<br />
</code></p>
<p>The final step is to redirect to the <code>login.php</code> script at certain appropriate points.  The first is in the default page, named <code>index.php</code>.  Normally, it redirects to the login page if the user is not authenticated.  We&#8217;re going to modify that behavior when we&#8217;re using basic authentication so that it skips right to the login script instead.  That way, it will automatically attempt to log in the user.</p>
<p><code><br />
if ( auth_is_user_authenticated() ) {<br />
        print_header_redirect( 'main_page.php' );<br />
} else if (BASIC_AUTH == config_get( 'login_method')) {<br />
        print_header_redirect( 'login.php' );<br />
} else {<br />
        print_header_redirect( 'login_page.php' );<br />
}<br />
</code></p>
<p>Finally, we don&#8217;t really want them to be able to log out, since that would require a login again.  The modification I&#8217;ve chosen does a logout, and then immediately attempts to log in again.</p>
<p><code><br />
auth_logout();</p>
<p>if ( HTTP_AUTH == config_get( 'login_method' ) ) {<br />
        auth_http_set_logout_pending( true );<br />
}</p>
<p>if ( BASIC_AUTH == config_get( 'login_method' ) ) {<br />
        print_header_redirect( 'index.php' );<br />
} else {<br />
        print_header_redirect( config_get( 'logout_redirect_page' ) );<br />
}<br />
</code></p>
<p>And that&#8217;s it!  Remember, the entire premise of these modifications is that the web server authentication is fully trusted, and that access to the underlying PHP scripts cannot occur unless the user has already successfully authenticated.  If that&#8217;s no the case in your scenario, then these modifications won&#8217;t be secure.</p>
<p>Feel free to take these modifications for yourself.  I&#8217;m going to see about submitting them as a patch to the official Mantis codebase.  Oh, and if you find anything that I&#8217;ve missed, or anything that&#8217;s insecure, please <a href="/user/1/contact">let me know</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://ardvaark.net/making-mantis-with-basic-authentication-not-suck/feed</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>
