Have a challenging question for you. Have a linux box. Need to create directory where users would be able to create files, but remove/modify only files created by them. Simple enough to have sticky bit set and thats it. But then we want particular admin user to be able to remove files from this directory and not being root user. How to do that? NFS4_ACLs are possible there. But I'm sure they won't help. Ideas? Users: user1:uploaders user2:uploaders admin1:admins <--- should be able to manage files in group dir
sgid on dir makes it possible to protect files from being edited by other users, but nothing stops user from deleting other users' files. Thats the problem
UPDATE 1:
The question was for FS permissions and nfs4_acls just because the users would be working with files over sftp. So that sudo and other scripted ways are not possible. Possible is to use LD_PRELOAD for sftp-server and override the unlink syscall or something like that. So it falls in to openssh and sftp-server.
UPDATE 2:
The users are chrooted to the directory in question by openssh and the directory should be root:root owned for it to work. All the files are put in this directory without any structure (app specific). The admin is actually not the only user to manage uploaded files but rather a group of admin users.
I'd be inclined to solve with with
sudo
rather than with ACLs. (There's no explicit mention of NFS in the question so I'm going to assume thatroot_squash
isn't an issue.)Start with your directory having permissions 1777 (sticky plus all read/write) as you suggested.
Create this script with a filename such as
/usr/local/bin/rmd
. Amend the definition ofTARGET
so that it is the absolute path to the target directoryAdd the following entry to the
sudoers
file (usevisudo
to edit this file). Change theadmin
to be the user with administrative privileges to delete files in the target directory.Since we know that
rmd
is in/usr/local/bin
it would be possible to re-exec
the script if it didn't have sufficient privileges, and so avoid the administrative user having to remember to usesudo
, but I've omitted that for now. Let me know if you want this adjustment to the script.Usage example
Bindfs is one possible solution. I named my power admin user
nradmin
and here is the example:Every file and directory in the mounted target is owned by
nradmin
. The-p ud+rwx
makes every directory to be with permissions "rwx" for the directory owner. Sincenradmin
is the owner of all directories and it has full owner permissions in them, it can successfully delete any file in them, even recursively.The alternative approach would be that you code a limited
chroot()
implementation of/bin/rm
and execute it asroot
. Achroot()
can be escaped by a process ran byroot
but only if you give this process the freedom to execute whatever it wants. A simple C binary which first makeschdir()
&chroot()
to the/uploads
directory, and then only callsunlink()
orrmdir()
should be secure. But this requires lots of coding like recursive delete of directories, a command-line option like-f
to ignore non-existing files, etc.One simple solution appears to be this. Consider the administrative user to be
admin
, and that our special directory is to be/tmp/special
.Any user can create/edit/delete their own files in
/tmp/special
. The useradmin
can delete any file (albeit with warnings fromrm
).NB If a user creates a directory in
/tmp/special
, the administrative user cannot remove it. That may be a showstopper for this solution, but as your question only mentioned files and not directories I felt it was worth offering.The full scope of this problem is unclear as we do not know what's the usecase. However using SELinux labels one get achieve what you ask for. SELinux gives you some fine-grained controls over who does what and where. If number of users is "limited" and "known" - having specific contexts/labels associated with each one of them is not a big issue, then it is a matter of writing a bit of policy to code-in your requirements.
Hmm... how about chrooting to
/special_folders_root/special_folder/./
to avoid problems with root-owned chrooted directories? See vsftpd's documentation (for example) for explanation about extraneous dot in the path.Unsure, if following will be useful, but: We have samba share with subdirs. Network MFUs putting the scanned documents inside specific subdirs (MFU01 --> /share/001/, MFU15--> /share/015/ etc). Users (from windows) may alter or remove the files in any of these subdirs, but can not remove the subdirs. I made that using windows-style ACLs, but I know nothing about NFS ACLs
NB! Not for bounty, but for assistance.
How about this approach:
Keep original uploaded files in a separate directory, per user. This covers governance and delete permissions.
In
$share
, everything is a link to the original files, and owner/admin ACLs are in place.In the end, every item in
$share
is a link back. Anything that isn't a link gets moved to the (uploader) owner's folder.