Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

About this user

Peter Cooperx http://www.petercooper.co.uk/

« Newer Snippets
Older Snippets »
Showing 1-1 of 1 total  RSS 

runas - Run a program under a specific user / group (poor man's suexec)

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


Usage example: ./runas username groupname "sleep 10"
« Newer Snippets
Older Snippets »
Showing 1-1 of 1 total  RSS