Mladen Kajic
Mladen Kajic

Reputation: 125

How does one switch OS users in a ruby script?

I have a ruby script always initiated by the root user. The script has a certain function called fou(). This function needs to be executed as the user 'otherguy' rather than 'root'. How do i switch users mid ruby script, execute the function and then switch back to root?

NEEDS TO BE ONE PROCESS
NO CHILD PROCESSES

OS specific code is welcome as long as it will work for all/most UNIX systems.

EDIT: I'd really prefer a solution without the use of external gems, only what's built into Ruby2.0.0

Upvotes: 0

Views: 408

Answers (1)

slowjack2k
slowjack2k

Reputation: 2586

With Process::Sys.seteuid(integer) and Process::Sys.setegid you can change the effective user id and group id. Don't confound it with Process::Sys.setuid(integer) and Process::Sys.setgid(integer). I think

Process::Sys.seteuid(12345)
Process::Sys.setegid(54321)
fou()
Process::Sys.seteuid(0)
Process::Sys.setegid(0) 

is what you are looking for (with Linux).

Upvotes: 2

Related Questions