![]() |
Check other users permissions (with proposed solution)
So, in bash, as root, I want to check if <user> has <permission> on <file|directory>.
In bash, of course there is 'test' to determine if the current <user> (EUID) has execute permissions on <file>: Code:
[ -x /pathto/file ]The best I could come up with was this: Code:
#!/bin/bashIt works, but I was wondering, is there an alternative/better way?? Thanks! - Rand |
I think it is a good approach, but there are other ways to work with: you can check the number of arguments, $# will help you. Also you do not need double check if $# is good enough you can skip the check in the function permission.
__________________________________ Happy with solution ... mark as SOLVED If someone helps you, or you approve of what's posted, click the "Add to Reputation" button, on the left of the post. |
${#@} got it, thanks!
Still wondering if there other ways to check for another users permissions on a file/dir. |
I think the best you can do is to check if that user is capable to do that. Therefore sudo -u $user <test> is the real way to check. I do not know why do you want another solution.
__________________________________ Happy with solution ... mark as SOLVED If someone helps you, or you approve of what's posted, click the "Add to Reputation" button, on the left of the post. |
It's such a common task to check (non-ACL) permissions, before performing some action.
I feel there should be some simple, direct, builtin (api-calling) compliant command to do it. Perhaps an enhancement to bash or sh-POSIX. Sudo is so heavy handed to use to perform such a simple test, which outght to be exposed at the filesystem-api or shell level. I feel using sudo for this is like driving a nail with a sledgehammer. It's certainly indirect, and also has security risks, but was the only quick solution I could find. I can think of worse alternatives, I just can't think of any better. :( |
Quote:
The correct way to do it is to simply do the action. Error handling can trivially find out why some part of the sequence could not be done; insufficient access permissions are just one possible reason. Checking beforehand is no substitute, since the permissions may change between checking and the actual access. When you realize this, it suddenly becomes obvious why there is no API for this. The only issue for programmers is that you need to have full error handling, preferably one that can gracefully fail with informative error messages. From experience, it is not hard; it becomes second nature very quickly when you always do it (always do it and do it right!). Learning how to do it was a lot of work. |
Yes, there's no substitute for good exception handling.
But, in this case I need to provide sanity checks before any action is attempted. I cannot allow the script to run halfway through and fail. Exception handling not withstanding, because even if an exception is caught, there is no 'rollback' concept. So, the intention of this script is to ensure a minimum level of sanity and verify dependencies are satisfied before cascading actions commence. Here is my simplified version, using runuser instead of sudo, not sure if it is an improvement... And still need to clean the parameters... Code:
#!/bin/bash |
Quote:
I may sound harsh, but that is not my intention. I know that the world is full of examples and designs where you first do some checks to see whether the task ahead might fail, and then just do the work assuming it cannot fail -- or if it fails, the tool fails catastrophically. That design is just wrong. A good programmer can do better. I personally prefer to use C (GNU C99 where available, C99 elsewhere), so I'm definitely not talking about exceptions either. Just robust error handling, where any function call can fail. Quote:
Let me repeat: that design is not robust. It is prone to failure. The correct solution is to do the work in a way which allows you to abort cleanly if an error occurs. I have personally not yet encountered a case where such a solution does not exist. In some cases they might be prohibitively expensive or slow, but I have not encountered such. Quote:
Code:
sudo -u "$USER" -- test -r "$FILE" && echo "$USER can read $FILE"In the general case, I would use a separate helper script. Save Code:
#!/bin/bashThe end result is that the above script can be used just like test, except with the target username added as the first parameter. Examples: Code:
test-user "$user" -d "$dir" -a -w "$dir" && echo "$user can create files in directory $dir"The script then checks using the id utility if the user exists. This check is for courtesy only: the following sudo will catch any invalid usernames anyway. (It helps catch installation problems, if you forget to add the line to sudoers.) The final line uses sudo to switch to the desired user. Remember, the script is running privileged (as root) at this point. All other parameters -- except for the user name -- are given to the explicitly executed /usr/bin/test utility as-is. The above script is therefore just as secure as test is; there is no need to try to filter or modify any of the parameters. (If I used the shell built-in test or conditional operators, it would be a different matter.) The main difference between our approaches is that you first look for something that works, then try to make it safe. I have the opposite approach; for me, security is an integral, inseparable part of my tools. They're not always safe, of course -- I err way too often --, but usually I manage to warn my users about the risks, and inform them about any deficiencies of the approach. I do not see security features as something that can be added on later. I hope you find this useful. I apologise if I sound harsh, but your approach to the issue is something that I see almost constantly causing trouble. I wish programmers did better. The fact that you are aware of the issues, and discuss them here, tells me you have the skill -- or if not the skills yet, then definitely the capability the learn them! -- to handle this stuff right. Otherwise I would not have brought it up at all. |
Quote:
|
Quote:
However, I assume at some point the OP wishes to test if a user can enter a directory, which is best tested using cd, as I showed in my post. In that case test alone is insufficient. I was being sneaky ;) |
Quote:
Quote:
Code:
~$ USER=root |
Quote:
The correct solution is of course Code:
sudo -n -u "$USER" -- bash -c "cd '$DIRSQ'" &>/dev/nullCode:
SQ="'" |
Quote:
Quote:
Code:
sudo -n -u "$USER" -- bash -c 'cd "$0"' "$DIR" &>/dev/nullQuote:
|
Quote:
Let's say I needed this sort of a tool to let my users check if they have set their file and directory accesses correctly. I prefer to use group-based access controls, so this is not that far fetched. Here is the skeleton of the script I'd use. Note: this one is intended to be used by humans interactively, not so much as a scripting tool. I'm using [ instead of [[ (as well as avoiding certain other Bashisms) because this is the sort of thing I might wish to run using dash instead, in certain situations. Also, this is untested, so it probably contains bugs. Code:
#!/bin/bashHmm.. it might be useful to add echo or printf to the tests, to let the user know the detailed result of each test? |
Hey Nominal, pan64, Ntubski,
This is great! Thank you for the thorough discussion and recommendations! I'll update my code. [OT] An aside , regarding the exception handling, et al: Consider that we in the software-engineering field further our trade. For mission and life-critical systems we are (more and more) mandated to incorporate 'formal (verification) methods' (esp. in Europe, getting better in the Americas). That is, We MUST (formally, mathematically) show that a system or subsystem, yes even each and every script, must satisfy it's logical pre-conditions and logical post-conditions, WITHOUT execution (in the case of the verification), and also DURING execution. (the pre-conditions: I was calling sanity-checking-before-operation). It's not 1970's, where formal systems and CASE tools were big and clunky, it's now, where flight systems and medical devices can kill, if we don't check that what we are about to do is safe - ... just a thought, you know what i'm sayin, and i see what your sayin, too. [/OT] |
| All times are GMT -5. The time now is 07:28 PM. |