{"id":5977,"date":"2016-06-23T08:08:11","date_gmt":"2016-06-23T00:08:11","guid":{"rendered":"http:\/\/rmohan.com\/?p=5977"},"modified":"2016-06-23T08:08:11","modified_gmt":"2016-06-23T00:08:11","slug":"access-authorization-in-apache-2-4","status":"publish","type":"post","link":"https:\/\/mohan.sg\/?p=5977","title":{"rendered":"Access authorization in Apache 2.4"},"content":{"rendered":"<p>In Apache 2.4 the authorization configuration setup has changed from previous versions. Satisfy, Order, Deny and Allow have all been deprecated and replaced with new <a href=\"http:\/\/httpd.apache.org\/docs\/2.4\/mod\/mod_authz_core.html#require\" target=\"_blank\">Require<\/a> directives.<\/p>\n<p>Below we&#8217;ve compiled some examples to guide you through the transition.<\/p>\n<p><b>If you are upgrading a server using the legacy authorization directives you can make them work quickly by enabling (it should be activated by default) <tt>mod_access_compat<\/tt> in Apache:<\/b><\/p>\n<p><code>sudo a2enmod access_compat<\/code><\/p>\n<h2 id=\"section_0\">1. Apache Documentation<\/h2>\n<p>The documentation from Apache: <a href=\"http:\/\/httpd.apache.org\/docs\/current\/upgrading.html\" target=\"_blank\">Upgrading to 2.4 from 2.2<\/a> provides the following basic examples. The old configuration settings are on the left, and the new ones for Apache 2.4 on the right:<\/p>\n<h4>All requests are denied:<\/h4>\n<p><code class=\"left12\"><b>Order<\/b> deny,allow <b>Deny<\/b> from all<\/code><code class=\"final right12\"><b>Require<\/b> all denied<\/code><\/p>\n<div class=\"clear\"><\/div>\n<h4>All requests are allowed:<\/h4>\n<p><code class=\"left12\"><b>Order<\/b> allow,deny <b>Allow<\/b> from all<\/code><code class=\"final right12\"><b>Require<\/b> all granted<\/code><\/p>\n<div class=\"clear\"><\/div>\n<h4>Only hosts in the example.org domain are allowed access:<\/h4>\n<p><code class=\"left12\"><b>Order<\/b> Deny,Allow <b>Deny<\/b> from all <b>Allow<\/b> from <i>example.org<\/i><\/code><code class=\"final right12\"><b>Require<\/b> host example.org<\/code><\/p>\n<div class=\"clear\"><\/div>\n<p>But this only scratches the surface of what&#8217;s now available.<\/p>\n<h2 id=\"section_1\">2. RequireAll and RequireAny<\/h2>\n<p>The most interesting new features are the <tt>RequireAll<\/tt>, <tt>RequireAny<\/tt> and <tt>RequireNone<\/tt> authorization containers. They promise to be both more powerful and more human-readable than the old syntax.<\/p>\n<p><b>By default all Require directives are handled as though contained within a <tt>&lt;RequireAny&gt;<\/tt>container directive. In other words, if any of the specified authorization methods succeed, then authorization is granted.<\/b><\/p>\n<p>Here is real world example where a website limits access by requiring a Basic Authentication login for certain directories:<\/p>\n<p><code class=\"left12\">AuthType Basic AuthName \"Password Protected\" AuthUserFile &lt;path_to_your_htpasswd_file&gt; <b>SetEnvIf<\/b> REQUEST_URI \"^\/(admin|secure)\/\" PROTECTED <b>Deny<\/b> from all <b>Satisfy<\/b> any <b>Allow<\/b> from env=!PROTECTED <b>Require<\/b> valid-user<\/code><code class=\"right12\"><b>SetEnvIf<\/b> REQUEST_URI \"^\/(admin|secure)\/\" PROTECTED &lt;RequireAny&gt; &lt;RequireAll&gt; <b>Require not<\/b> env PROTECTED <b>Require<\/b> all granted &lt;\/RequireAll&gt; &lt;RequireAll&gt; AuthType Basic AuthName \"Password Protected\" AuthUserFile &lt;path_to_your_htpasswd_file&gt; <b>Require<\/b> valid-user &lt;\/RequireAll&gt; &lt;\/RequireAny&gt;<\/code><\/p>\n<div class=\"clear\"><\/div>\n<p>In both cases we set an environmental variable <tt>PROTECTED<\/tt> when the request is for a file in the <tt>\/admin\/<\/tt>or <tt>\/secure\/<\/tt> directories. The syntax for this part hasn&#8217;t changed. If this variable is set, then a password will be required for access.<\/p>\n<p>While the old syntax works, it&#8217;s not immediately clear how it works. Basically to get access the request has to meet (Satisfy) either the <tt>Allow<\/tt> or the <tt>Require<\/tt> directive.<\/p>\n<p>In the new syntax this is more explicit. The request needs to pass at least one (<tt>RequireAny<\/tt>) of the two<tt>RequireAll<\/tt> container rulesets. The first container grants all users access to non-PROTECTED directories, while the second container requires a valid login.<\/p>\n<p>You can keep nesting containers until all possible options are covered.<\/p>\n<p>But seeing as the outer <tt>&lt;RequireAny&gt;<\/tt> is already implied, we should be able to remove it. Actually we can remove quite a bit now that we know what we&#8217;re doing:<\/p>\n<p><code class=\"final\">AuthType Basic AuthName \"Password Protected\" AuthUserFile &lt;path_to_your_htpasswd_file&gt; <b>SetEnvIf<\/b> REQUEST_URI \"^\/(admin|secure)\/\" PROTECTED &lt;RequireAll&gt; <b>Require not<\/b> env PROTECTED &lt;\/RequireAll&gt; <b>Require<\/b> valid-user <\/code><\/p>\n<p>Note that any <tt>Require not<\/tt> directives must always be enclosed in a <tt>RequireAll<\/tt> directive. Otherwise you will see an alert logged:<\/p>\n<p><code>[core:alert] <i>...<\/i> negative Require directive has no effect in &lt;RequireAny&gt; directive<\/code><\/p>\n<p><b>See further down the page a version of this example that does away with the ENV variable entirely by using an <tt>expr<\/tt> condition.<\/b><\/p>\n<h2 id=\"section_2\">3. Require authorization providers<\/h2>\n<p>The Require directive comes with a number of build-in authorization providers, including some already demonstrated above. Different modules provide different methods.<\/p>\n<p>The following are provided by the <a href=\"http:\/\/httpd.apache.org\/docs\/current\/mod\/mod_authz_core.html\" target=\"_blank\">mod_authz_core<\/a> module:<\/p>\n<h4>all<\/h4>\n<p>Replaces <tt>Allow from all<\/tt> and <tt>Deny from all<\/tt> in the old syntax:<\/p>\n<p><code><b>Require<\/b> all granted<\/code><code><b>Require<\/b> all denied<\/code><\/p>\n<h4>env<\/h4>\n<p><code><b>Require<\/b> env <i>safe_zone<\/i><\/code><code>&lt;RequireAll&gt; <b>Require not<\/b> env <i>PROTECTED<\/i> &lt;\/RequireAll&gt;<\/code><\/p>\n<h4>method<\/h4>\n<p>This example allows only GET and HEAD requests unless you are logged in:<\/p>\n<p><code>&lt;RequireAny&gt; <b>Require<\/b> method GET HEAD <b>Require<\/b> valid-user &lt;\/RequireAny&gt;<\/code><\/p>\n<p>The <tt>&lt;RequireAny&gt;<\/tt> container is not necessary here, but included for clarity:<\/p>\n<h4>expr<\/h4>\n<p><code><b>Require<\/b> expr %{HTTP_USER_AGENT} != 'BadBot'<\/code><\/p>\n<p>The following options are provided by the <a href=\"http:\/\/httpd.apache.org\/docs\/current\/mod\/mod_authz_host.html\" target=\"_blank&quot;\">mod_authz_host<\/a> module:<\/p>\n<h4>local<\/h4>\n<p><code><b>Require<\/b> local<\/code><\/p>\n<h4>ip<\/h4>\n<p><code>&lt;RequireAll&gt; <b>Require<\/b> ip <i>192.168.1.0\/24<\/i> <b>Require not<\/b> ip <i>192.168.1.104<\/i> &lt;\/RequireAll&gt;<\/code><code><b>Require<\/b> ip 2001:db8:1:1::\/64<\/code><\/p>\n<h4>host<\/h4>\n<p><code>&lt;RequireAll&gt; <b>Require<\/b> host <i>example.org<\/i> <b>Require not<\/b> host <i>blocked.example.org<\/i> &lt;\/RequireAll&gt;<\/code><\/p>\n<h2 id=\"section_3\">4. Working with expressions<\/h2>\n<p>After a bit of messing about we were able to further simplify the previous example by removing the<tt>SetEnvIf<\/tt> clause and replacing it with a <tt>Require expr<\/tt> regular expression condition.<\/p>\n<p><code class=\"left12\">AuthType Basic AuthName \"Password Protected\" AuthUserFile &lt;path_to_your_htpasswd_file&gt; <b>SetEnvIf<\/b> REQUEST_URI \"^\/(admin|secure)\/\" PROTECTED &lt;RequireAll&gt; <b>Require not<\/b> env PROTECTED &lt;\/RequireAll&gt; <b>Require<\/b> valid-user<\/code><code class=\"final right12\">AuthType Basic AuthName \"Password Protected\" AuthUserFile &lt;path_to_your_htpasswd_file&gt; <b>Require<\/b> expr %{REQUEST_URI} !~ m#^\/(admin|secure)\/# <b>Require<\/b> valid-user <\/code><\/p>\n<div class=\"clear\"><\/div>\n<p>The tricky part was working out how to include the forward slash <tt>\/<\/tt> in the regular expression. The solution is instead of the default format which doesn&#8217;t allow a forward slash in the match:<\/p>\n<p><code><b>Require<\/b> expr %{REQUEST_URI} !~ \/<i>expr<\/i>\/<\/code><\/p>\n<p>To use the alternative syntax:<\/p>\n<p><code><b>Require<\/b> expr %{REQUEST_URI} !~ m#<i>expr<\/i>#<\/code><\/p>\n<p>For details on other SERVER variables and comparison operators that can be used see the link under References below.<\/p>\n<h2 id=\"section_4\">5. Granting local access<\/h2>\n<p>Another real world example is granting access only to the local network.<\/p>\n<p>In this case we&#8217;re defining the local network as the server itself (localhost), plus the 192.168.1.* subnet covering 192.168.1.0 &#8211; 192.168.1.255.<\/p>\n<p><code class=\"left12\">&lt;Directory \"\/path\/to\/your\/website\"&gt; Options FollowSymlinks AllowOverride None Order allow,deny Allow from 127.0.0.0\/8 192.168.1 ::1 &lt;\/Directory&gt;<\/code><code class=\"final right12\">&lt;Directory \"\/path\/to\/your\/website\"&gt; Options FollowSymlinks AllowOverride None <b>Require<\/b> local <b>Require<\/b> ip 192.168.1 &lt;\/Directory&gt;<\/code><\/p>\n<div class=\"clear\"><\/div>\n<p>The <tt>local<\/tt> Require&#8217;ment matches requests from the local host over IPv4 or IPv6 (so including 127.0.0.1\/8 and ::1). We wrap this, along with <tt>Require ip 192.168.1<\/tt>, in a <tt>RequireAny<\/tt> authorization container because we want to accept connections that match either condition.<\/p>\n<p>We could also write 192.168.1.0\/24 instead of just 192.168.1, but they have the same effect.<\/p>\n<p>If you want to also allow connections from outside the local network, but requiring authentication, the configuration becomes:<\/p>\n<p><code class=\"final\">&lt;Directory \"\/path\/to\/your\/website\"&gt; Options FollowSymlinks AllowOverride None <b>Require<\/b> local <b>Require<\/b> ip 192.168.1 <b>Require<\/b> valid-user &lt;\/Directory&gt;<\/code><\/p>\n<p>So we&#8217;re now granting access from localhost and the local network without authentication, plus from all other locations, but then requiring authentication.<\/p>\n<p>You can make this more secure by restricting outside access to only recognised locations:<\/p>\n<p><code class=\"final\">&lt;Directory \"\/path\/to\/your\/website\"&gt; Options FollowSymlinks AllowOverride None <b>Require<\/b> local <b>Require<\/b> ip 192.168.1 &lt;RequireAll&gt; <b>Require<\/b> host example.org <b>Require not<\/b> host badhost.example.org <b>Require<\/b> valid-user &lt;\/RequireAll&gt; &lt;\/Directory&gt;<\/code><\/p>\n<p>Now an external connection can only come from *.example.org and only in conjunction with a valid login. To specify more than one domain or ip address in addition to example.org they will need to be wrapped in yet another container:<\/p>\n<p><code class=\"final\">&lt;Directory \"\/path\/to\/your\/website\"&gt; Options FollowSymlinks AllowOverride None <b>Require<\/b> local <b>Require<\/b> ip 192.168.1 &lt;RequireAll&gt; &lt;RequireAny&gt; <b>Require<\/b> host example.org example.com <b>Require<\/b> ip 8.8.8.8 &lt;\/RequireAny&gt; <b>Require not<\/b> host badhost.example.org <b>Require<\/b> valid-user &lt;\/RequireAll&gt; &lt;\/Directory&gt;<\/code><\/p>\n<p>For those getting confused, <tt>RequireAll<\/tt> means that all the requirements in that container need to be met, while <tt>RequireAny<\/tt> means that only one or more of the contained requirements needs to be met:<\/p>\n<p><code><b>Require<\/b> (local) OR (ip 192.168.1) OR [ [ (host example.org) OR (host example.com) OR (ip 8.8.8.8) ] AND (NOT host badhost.example.com) AND (valid-user) ]<\/code><\/p>\n<h2 id=\"section_5\">6. Public file in Private directory<\/h2>\n<p>Thank you to Alfredo for this question &#8211; how to have a password-protected directory (or website) but allow access to a specific file.<\/p>\n<p>If you have a directory <tt>~\/private\/<\/tt> then you can make the entire directory secure by adding an<tt>.htaccess<\/tt> file <tt>~\/private\/.htaccess<\/tt> with:<\/p>\n<p><code>AuthType Basic AuthName \"Password Required\" AuthUserFile <i>\/path\/to\/<\/i>.htpasswd <b>Require<\/b> valid-user<\/code><\/p>\n<p>But what if there is a file <tt>~\/private\/public.html<\/tt> that you want to make globally accessible? This wasn&#8217;t possible in earlier versions of Apache, but can be done now quite simply:<\/p>\n<p><code class=\"final\">AuthType Basic AuthName \"Password Required\" AuthUserFile <i>\/path\/to\/<\/i>.htpasswd <b>Require<\/b> expr %{REQUEST_URI} = \"\/private\/public\\.html\" <b>Require<\/b> valid-user<\/code><\/p>\n<p>How does it work? Remember that there is an explicit <tt>&lt;RequireAny&gt;<\/tt> wrapped around the two <tt>Require<\/tt>statements, so it reads as: <i>either<\/i> the request is for the file <tt>public.html<\/tt> <i>or<\/i> require a password.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In Apache 2.4 the authorization configuration setup has changed from previous versions. Satisfy, Order, Deny and Allow have all been deprecated and replaced with new Require directives.<\/p>\n<p>Below we&#8217;ve compiled some examples to guide you through the transition.<\/p>\n<p>If you are upgrading a server using the legacy authorization directives you can make them work quickly [&#8230;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[12],"tags":[],"_links":{"self":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/5977"}],"collection":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=5977"}],"version-history":[{"count":1,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/5977\/revisions"}],"predecessor-version":[{"id":5978,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/5977\/revisions\/5978"}],"wp:attachment":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=5977"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=5977"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=5977"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}