peterc (owner)

Revisions

gist: 90347 Download_button fork
public
Public Clone URL: git://gist.github.com/90347.git
Embed All Files: show embed
runas #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#!/usr/bin/env ruby
 
# runas - Run another program under the privileges of a specified user and group.
# This is necessary because sudo demands a password, as we need it to be hands off.
# A poor man's suexec basically.
 
require 'etc'
 
user, group, cmd = ARGV
 
begin
  uid = Etc.getpwnam(user).uid
  gid = Etc.getgrnam(group).gid
 
  unless Process.euid == uid && Process.egid == gid
    Process.initgroups(user, gid)
    Process::GID.change_privilege(gid)
    Process::UID.change_privilege(uid)
  end
 
  exec cmd
rescue
  puts "Could not run as #{user}:#{group}"
  exit 1
end