You can't do what you state in a useful way, but there's undoubtedly something that's close enough and that will do what you really want.
Even if you arranged to create the directory, the www-data
user would still not be able to access /some-path/subdirectory
, because the subdirectory can only be accessed through the parent directory. (There are ways around this, but none that I recommend. You can have a process that can access both /some-path
and /some-path/subdirectory
change to /some-path/subdirectory
, then drop privileges; the resulting process will still be able to access its current directory (but not through its absolute path). You can bind-mount the directory in another location, but if you're going to do that you might as well create the directory elsewhere.)
Arrange for these directories to be located under a directory that www-data
can at least access (x
permission bit). If the problem is that the directory must belong to another user and another group, set an access control list on the directory (setfacl -m user:www-data:x /some-path
) — see How to restrict to run commands in specific directory through SUDOERS? for more information.
If the www-data
user cannot write to /some-path
, you'll still need elevated privileges to create the directory. You'll need to do at least two things, perhaps three:
- create the subdirectory as a user with sufficient privileges;
- if necessary, change the ownership of the subdirectory;
- if necessary, change the permissions of the subdirectory.
If the subdirectory must belong to the www-data
user, you can create it as a group who can write to /some-path
. If necessary, set an ACL that allows some-group
to write to /some-path
: setfacl -m group:some-group:rwx /some-path
. Then give www-data
the right to execute the mkdir
command with sudo. Run visudo
and add the following rule:
www-data ALL = ( : some-group) /bin/mkdir /some-path/[0-9A-Z_a-z]*, !/bin/mkdir /some/path/[!-0-9A-Z_a-z]
This allows www-data
to run sudo -g some-group mkdir /some-path/foo-bar
to create subdirectories in /some-path
.
If the subdirectory must belong to another user who can write to some-path
, run the mkdir
command as that user. You might be able to arrange for the directory to have the correct permissions and ownership at creation time. For the sudoers
file:
www-data ALL = (some-user : some-group) /bin/mkdir -m 775 /some-path/[0-9A-Z_a-z]*, !/bin/mkdir /some/path/[!-0-9A-Z_a-z]
Run sudo -u some-user -g some-group mkdir -m 775 /some-path/foo-bar
to create a group-writable directory belonging to some-user:some-group
under /some-path
.
x
permission it will allow the user to traverse the parent folder (I.e. pass through it to access its folder) but not list its contents.