5

Does anyone know if it's possible to use regex capture within Apache's DirectoryMatch directive? I'd like to do something like the following:

<DirectoryMatch ^/home/www/(.*)>
    AuthType Basic
    AuthName $1
    AuthUserFile /etc/apache2/svn.passwd
    Require group $1 admin
</DirectoryMatch>

but so far I've had no success.

Specifically, I'm trying to create a group-based HTTP Auth for individual directories/vhosts on a server in Apache 2.0.

For example, Site A, pointing to /home/www/a will be available to all users in group admin and group a, site b at /home/www/b will be available to all users in group admin and group b, etc. I'd like to keep everything based on the directory name so I can easily script adding htpasswd users to the correct groups and automate this as much as possible, but other suggestions for solving the problem are certainly welcome.

Chris
  • 93
  • 7

2 Answers2

3

You could tackle the problem from a completely different angle: enable the perl module and you can include a little perl script in your httpd.conf. You could then do something like this:

<Perl>
my @groups = qw/ foo bar baz /;
foreach ( @groups ) {
    push @PerlConfig, qq| <Directory /home/www/$_> blah </Directory> |;
}
</Perl>

That way, you could even read your groups and other information from a database or by simply globbing /home/www or whatever else tickles your fancy.

innaM
  • 47,505
  • 4
  • 67
  • 87
  • 1
    This is incredibly handy, but almost "ungooglable", since every search that contains Perl and Apache ends up with tons of answers to how Apache can be configured to run cgi scripts. A reference for other struggling googlers: https://perl.apache.org/docs/2.0/api/Apache2/PerlSections.html – bjarneh Mar 11 '16 at 09:22
0

What you are trying to do looks very similar to per-user home directories. The way Apache handles these is through file system permissions and .htaccess files. I don't believe there is any way to use regex capture in the enclosed directives (AuthName, etc).

Ryan Wersal
  • 3,210
  • 1
  • 20
  • 29