Regex for Apache Directory directive

I am trying to set up a DAV access to our computational server…

If I name individual Directory directives it 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>

    [...]

I believe I can use RegEx in the directive, but how?
Something like this (that doesn’t work):

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

Any help ?

I think there’s an error in your regex: * is a quantifier, not a “wildcard” symbol. You might want to change the expression to ^/home/.+$ or even ^/home/[a-z]+$.

Thank you, you are right. But how can I save the part after “/home/” to use inside the directive (if it is possible) ?

It looks possible with DirectoryMatch:

From 2.4.8 onwards, named groups and backreferences are captured and written to the environment with the corresponding name prefixed with “MATCH_” and in upper case. This allows elements of paths to be referenced from within expressions and modules like mod_rewrite. In order to prevent confusion, numbered (unnamed) backreferences are ignored. Use named groups instead.

So in your case (added optional trailing / too):

<DirectoryMatch "^/home/(?<username>[a-z]+)/?$">
    Require user %{env:MATCH_USERNAME}
</DirectoryMatch>
1 Like

Thank you.. this seems promising, but it also seems that Require got somehow interpreted before the placeholder is expanded:

# apachectl configtest
AH00543: apache2: bad user name %{env:MATCH_USERNAME}

(Apache 2.4.52)

I solved it with mod_perl:

<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>