1. How can a program know whether it is running in superuser mode?
- Posted by JerryStory Jan 15, 2010
- 966 views
I want to do something like this.
if (program is not run in superuser mode) then puts(1, "This program needs to be run in superuser mode.\n") puts(1, "On Ubuntu, type 'sudo ' \n") end if
How can a Euphoria program running on Linux know whether it is running in superuser mode? There doesn't seem to be any way to solve this problem with command_line().
2. Re: How can a program know whether it is running in superuser mode?
- Posted by rkdavis Jan 15, 2010
- 934 views
not sure if it will work but in bash you'd check the env var EUID so perhaps you can check that.
3. Re: How can a program know whether it is running in superuser mode?
- Posted by rkdavis Jan 15, 2010
- 963 views
sorry forgot to give an example
if [[ $EUID -ne 0 ]]; then echo "This script must be run as root" 1>&2 exit 1 fi
4. Re: How can a program know whether it is running in superuser mode?
- Posted by JerryStory Jan 16, 2010
- 957 views
if [[ $EUID -ne 0 ]]; then echo "This script must be run as root" 1>&2 exit 1 fi
Thanks. But now I have difficulty putting that in system() or system_exec() in a way that works.
5. Re: How can a program know whether it is running in superuser mode?
- Posted by ChrisB (moderator) Jan 16, 2010
- 923 views
Hi
in euphoria
sequence env env = getenv("USER") puts(1, env & "\n") --and if equal(env, "ROOT") = 1 then --do stuff else --don't do stuff end if
Chris
6. Re: How can a program know whether it is running in superuser mode?
- Posted by jimcbrown (admin) Jan 16, 2010
- 933 views
Hi
in euphoria
sequence env env = getenv("USER") puts(1, env & "\n") --and if equal(env, "ROOT") = 1 then --do stuff else --don't do stuff end if
Chris
This is a BAD idea. I believe any user can set the USER variable to whatever they want.
This is actually not as dangerous as changing the USER variable won't change the permissions, so there is not really a security violation, but this method isn't very accurate to determine if you really are root or not.
On bash, UID is a read-only variable, but a simple C program could override the value of the UID environment variable as well. (But again, if done this is not a security breach.)
There is no way in Euphoria to do this. The only chance is to use dll.e and wrap the "getuid()" and "geteuid()" functions from the C library.
7. Re: How can a program know whether it is running in superuser mode?
- Posted by ChrisB (moderator) Jan 16, 2010
- 904 views
Hi
Well its not that bad - just a quick and dirty way to check that the user is root - and yes still couldn't run root programs - will do for most purposes. Unless you were really paranoid.
Chris
8. Re: How can a program know whether it is running in superuser mode?
- Posted by rkdavis Jan 16, 2010
- 917 views
Using $EUID is only a quick and dirty check. It can be fiddled with but it's not going to do anyone trying to fool your program any good as the system()/system_exec() would still fail anyway when they themselves try to do something they don't have the pivs to do
so personally i'd still use $EUID if all i wanted to do was warn the user they'd need to sudo (or run as root) and then exit out if they weren't
another quick and dirty method that is more system()/system_exec() friendly is id as you could pipe it's result into something else
however if you are going to be doing stuff like that alot i'd say jim's method would be the correct way
9. Re: How can a program know whether it is running in superuser mode?
- Posted by JerryStory Jan 16, 2010
- 926 views
Hi
in euphoria
sequence env env = getenv("USER") puts(1, env & "\n") --and if equal(env, "ROOT") = 1 then --do stuff else --don't do stuff end if
Chris
Thanks! This is exactly what I wanted. It works perfectly. (minor detail: "root" instead of "ROOT")
10. Re: How can a program know whether it is running in superuser mode?
- Posted by alanjohnoxley Jan 18, 2010
- 886 views
Some id's can have root permissions via the sudo'ers file, while not being the root id themselves.
How about trying to touch a garbage file in the /tmp folder, then chown it to root.
If thats successful, then you do have root permissions even if you are not the root user.
Normally you want to know if you have root permissions.
11. Re: How can a program know whether it is running in superuser mode?
- Posted by jimcbrown (admin) Jan 18, 2010
- 861 views
Some id's can have root permissions via the sudo'ers file, while not being the root id themselves.
No, that would require the user to use the sudo command... and when it is used, the program that sudo runs thinks it is root.
How about trying to touch a garbage file in the /tmp folder, then chown it to root.
If thats successful, then you do have root permissions even if you are not the root user.
Normally you want to know if you have root permissions.
That's not a bad idea, one can do this using system_exec() and the chown command.
It's not perfect though, as there are other possible error conditions besides not havng root permissions, but it's hard to imagine /tmp being on a read only file system or similiar.