Allow users to chmod certain directories via sudoers file
Linux - GeneralThis Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Allow users to chmod certain directories via sudoers file
Can anyone provide an example of what I should type in the sudoers file so a specific user (example: smith) can perform a chmod on files located in /data/security/ only?
It seems when I enter smith ALL=NOPASSWD:/bin/chmod 755 /data/security/* smith can perform a chmod on any files, regardless of the directory.
First you need to think about what is a file under /data/security/ ? If it has hard links to a filename in some other location then it's debatable whether the file really qualifies. You can solve this problem by having a filesystem mounted at /data/security/ and nothing mounted below it (making the filesystem and the directory tree the same thing).
Then maybe you want to combine "chroot();chdir();chmod()" into one program so that when it is called with sudo it operates on file(s) under /data/security/ by being chrooted there and has no alternative regardless of the command line provided by the user. That's what I'd call the standard way to do it for a C programmer.
There is also a thing called "posix capabilities" containing CAP_FOWNER (see http://linux.die.net/man/7/capabilities) which looks as if it will allow chmod by users other than the owner. I suspect this is a worse option than the other.
It seems when I enter smith ALL=NOPASSWD:/bin/chmod 755 /data/security/* smith can perform a chmod on any files, regardless of the directory.
Right, because the way the command is parsed, you're allowing the chmod command. The arguments after it aren't getting shoveled along.
Quote:
Originally Posted by johnmccarthy
linosaurusroot,
Sort of new to Linux. What command should I type to mount the directory as a seperate file system? Is it somthing like mount /data/security?
Sorry, but all that is overkill, especially if you only ever want that user to run ONE COMMAND as sudo/root, and it doesn't really address things (or users), that you may want to add in the future.
The easiest thing to do would be for you to write a very small shell script with that one command in it. Make it owned by root:root ("chown root:root /path/to/scriptname"), and permissions of 700 (rwx------) ("chmod 700 /path/to/scriptname"), so that ONLY root can run or edit it. That's it.
In sudoers, restrict them to that one command, and deny ALL OTHERS, as so:
The users in the LIMITEDADMINS group will be able to run the commands listed as root, and nothing else. If the script needs an argument, just have the script read it from the command line, so the user can call it like:
Code:
sudo /path/to/scriptname someargument
...and the "someargument" will get passed to the script, which will take action on it.
If you want more users to do it in the future, just add them to the alias group. More commands? Just add another script/command (comma-separated), and you're done.
Sorry, but all that is overkill, especially if you only ever want that user to run ONE COMMAND as sudo/root .....
LIMITEDADMINS ALL=(ALL) /usr/local/bin/scriptname
He doesn't want them to run only one command. He wants them to chmod files under one directory. Once you put "chmod $1 $2" kind of thing in that script you are doomed.
He doesn't want them to run only one command. He wants them to chmod files under one directory.
...which is ONE COMMAND. The OP posted " can perform a chmod on files located in /data/security/ only?". So, since they only want to perform ONE operation on ONE location....
Quote:
Once you put "chmod $1 $2" kind of thing in that script you are doomed.
Please explain why. Yes, if you just shove that into a script it's a bad thing. If you write the script properly (that is, to check for the path name and/or hard code it in), then the user won't be able to chmod ANY files...just the ones allowed, which solves the problem. In this case, the OP very clearly states that they only want to chmod files located in /data/security. So, part of the script would be:
Code:
chmod /data/security/$1
Which will take only one argument (a file name), and chmod it. Putting in "/etc/sudoers" would then expand to "/data/security/etc/sudoers"...which is invalid, and won't work. A symbolic link from /etc/sudoers to /data/security/sudoers won't work, since the chmod doesn't change the SOURCE file, only the symlink, so they won't magically get rights to sudoers that way.
...which is ONE COMMAND. The OP posted " can perform a chmod on files located in /data/security/ only?". So, since they only want to perform ONE operation on ONE location....
We may have been interpreting this differently with
Code:
smith ALL=NOPASSWD:/bin/chmod 755 /data/security/*
meaning either "sudo /bin/chmod 755 /data/security/*" (literally one command) or "sudo /bin/chmod 755 /data/security/foo-bar-and-whatever-chosen-by-the-user" (choice of target but under that directory, possibly in a subdirectory) .
Quote:
Please explain why. Yes, if you just shove that into a script it's a bad thing. If you write the script properly (that is, to check for the path name and/or hard code it in), then the user won't be able to chmod ANY files...just the ones allowed, which solves the problem. In this case, the OP very clearly states that they only want to chmod files located in /data/security. So, part of the script would be:
Code:
chmod /data/security/$1
Which will take only one argument (a file name), and chmod it. Putting in "/etc/sudoers" would then expand to "/data/security/etc/sudoers"...which is invalid, and won't work. A symbolic link from /etc/sudoers to /data/security/sudoers won't work, since the chmod doesn't change the SOURCE file, only the symlink, so they won't magically get rights to sudoers that way.
I'll take it for granted you're testing the name and excluding parent directories (such as "/data/security/" + "../../etc/shadow" meaning "/etc/shadow").
I think you're assuming that only root has write access in /data/security/ (but we've not been told that by OP). If smith (or an accomplice or even an innocent user) writes there you have the possibility of something in that directory being a symbolic link to something outside the directory. It could even become this while the script is running (which is a race condition - what a C programmer might tackle with fchmod()).
I've just tested and chmod does follow symlinks on my system.
What about
test -f ${1/*\/} && chmod /specific/directory/here/${1/*\/}
Although, I'm not too knowledgeable on this, but I presume this wouldn't allow any links nor any directories to be modified, as well as would keep anyone from modifying anything in any sub directories.
What about
test -f ${1/*\/} && chmod /specific/directory/here/${1/*\/}
Although, I'm not too knowledgeable on this, but I presume this wouldn't allow any links nor any directories to be modified, as well as would keep anyone from modifying anything in any sub directories.
The test and chmod occur at different times so if someone replaces the file by a symlink at just the right time you've got trouble.
What you'd do in C is open() the file (using its name) then test the file descriptor you had that it was the right one and finally do fchmod() on the descriptor (you don't care about the name at this stage).
The test and chmod occur at different times so if someone replaces the file by a symlink at just the right time you've got trouble.
Ah! Read something similar to that not long ago, just took me one more time reading it (here) to finally get it.
Also, just ran a few tests and found that even using "test -f", for some odd reason, still chmod'ed the linked file.
Quote:
What you'd do in C is open() the file (using its name) then test the file descriptor you had that it was the right one and finally do fchmod() on the descriptor (you don't care about the name at this stage).
After reading and trying all that, then I would have to say writing a specific program seems the safest way to implementing something like this.
Please allow me to begin with saying that this site has renewed my faith in the community involvement whose charter is to help others when possible. I think each of your comments above are extrodinary and well thought out along with your constructive critisism many will benefit from those who view this thread. After much thought and consdieration I have decided to implemnet a simple and secure resolution. I have created a script that directs the changes I would have executed if I was at the console. This script can be executed by the general user after performing the sudo command and providing the path and respective script. This script is 700, and located in a folder which the general user can not view (thus not modify).
If I could share the points/credit for solving this problem to all those who replied I would, since each solution would work but based on my limited skill-set and security factors I have implemented TB0ne concept. Going forward I hope to improve my Linux Skills and share everything I know.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.