Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex in Directory apache directive with part of the path inside the directive

I am setting a WebDAV server for my lab using Apache.

Setting the directives of the various users directory in the virtual host configuration works:

<Directory /home/userfoo>
  DAV On
  AuthName "webdav"
  AuthType Basic
  AuthBasicProvider external
  AuthExternal pwauth
  Require user userfoo
  AssignUserID userfoo userfoo
  Options +Indexes
  AllowOverride all
</Directory>

<Directory /home/davtest>
  DAV On
  AuthName "webdav"
  AuthType Basic
  AuthBasicProvider external
  AuthExternal pwauth
  Require user davtest
  AssignUserID davtest davtest
  Options +Indexes
  AllowOverride all
</Directory>

[...]    

Aside using mod_macro (that would still need me to put each user individually) is there a way to use RegEx where part of the path in the Directory directive is re-used inside the directive? Something like:

<Directory ~ "/home/*+">
  DAV On
  AuthName "webdav"
  AuthType Basic
  AuthBasicProvider external
  AuthExternal pwauth
  Require user $1     
  AssignUserID $1 $1     
  Options +Indexes
  AllowOverride all
</Directory>
like image 436
Antonello Avatar asked Oct 25 '25 05:10

Antonello


2 Answers

Instead of Directive directive, you can use Apache's DirectoryMatch directive that gives you ability to match a regular expression and use capture groups:

<DirectoryMatch "^/home/(?<USERNAME>[^/]+)">
    DAV On
    AuthName "webdav"
    AuthType Basic
    AuthBasicProvider external
    AuthExternal pwauth
    Require user %{env:MATCH_USERNAME}
    AssignUserID %{env:MATCH_USERNAME} %{env:MATCH_USERNAME}
    Options +Indexes
    AllowOverride all
</DirectoryMatch>
like image 178
anubhava Avatar answered Oct 27 '25 22:10

anubhava


In my case capture groups didn't work. Another flexible way to achieve this is by using the mod_perl module and choose the logic you want.

For example, in my case all "normal" users are under the users group, and I had installed the members utility, so I done just:


<Perl>
     my $group = "users";
     my $output = `members $group`;
     chomp($output);
     my @members = split(/\s+/, $output);
     foreach ( @members ) {
       push @PerlConfig, qq|
<Directory /home/$_ >
      DAV On
      AuthName "webdav"
      AuthType Basic
      AuthBasicProvider external
      AuthExternal pwauth
      Require user $_
      AssignUserID $_ $_
      Options +Indexes
      AllowOverride all
</Directory> |;
}
</Perl>

Note that the perl script runs only once when apache loads the configuration, not for each request, so when you add a new user you need to reload the apache configuration, e.g. with service apache2 reload. If you use adduser you can do that automatically in /usr/local/sbin/adduser.local.

like image 35
Antonello Avatar answered Oct 28 '25 00:10

Antonello



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!