1. How can a program know whether it is running in superuser mode?

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().

new topic     » topic index » view message » categorize

2. Re: How can a program know whether it is running in superuser mode?

not sure if it will work but in bash you'd check the env var EUID so perhaps you can check that.

new topic     » goto parent     » topic index » view message » categorize

3. Re: How can a program know whether it is running in superuser mode?

sorry forgot to give an example

if [[ $EUID -ne 0 ]]; then 
   echo "This script must be run as root" 1>&2 
   exit 1 
fi 

new topic     » goto parent     » topic index » view message » categorize

4. Re: How can a program know whether it is running in superuser mode?

rkdavis said...

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.

new topic     » goto parent     » topic index » view message » categorize

5. Re: How can a program know whether it is running in superuser mode?

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

new topic     » goto parent     » topic index » view message » categorize

6. Re: How can a program know whether it is running in superuser mode?

ChrisB said...

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.

new topic     » goto parent     » topic index » view message » categorize

7. Re: How can a program know whether it is running in superuser mode?

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

new topic     » goto parent     » topic index » view message » categorize

8. Re: How can a program know whether it is running in superuser mode?

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

new topic     » goto parent     » topic index » view message » categorize

9. Re: How can a program know whether it is running in superuser mode?

ChrisB said...

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")

new topic     » goto parent     » topic index » view message » categorize

10. Re: How can a program know whether it is running in superuser mode?

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.

new topic     » goto parent     » topic index » view message » categorize

11. Re: How can a program know whether it is running in superuser mode?

alanjohnoxley said...

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.

alanjohnoxley said...

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.

new topic     » goto parent     » topic index » view message » categorize

Search



Quick Links

User menu

Not signed in.

Misc Menu