Skip to content

Instantly share code, notes, and snippets.

@potato2003
Last active March 27, 2019 08:36
Show Gist options
  • Save potato2003/7d4023e6210ae2a9d062a7ffe3ba8230 to your computer and use it in GitHub Desktop.
Save potato2003/7d4023e6210ae2a9d062a7ffe3ba8230 to your computer and use it in GitHub Desktop.
ruby 環境で必要だったので、kazuho さんの https://gist.github.com/kazuho/6181648 を参考に書いた。(参考リンク - 2) http://d.hatena.ne.jp/hirose31/20130808/1375965331
#!/usr/bin/env ruby
# usage: setusergroups [-h|--help] username child
#
# setuidgid w. support for supplementary groups
# ruby porting of https://gist.github.com/kazuho/6181648
#
# see: http://d.hatena.ne.jp/hirose31/20130808/1375965331
# see: https://gist.github.com/kazuho/6181648
require 'etc'
will_show_help = false
will_show_help |= ARGV.size < 2
will_show_help |= ["-h", "--help"].include? ARGV[0]
if will_show_help
puts "usage: setusergroups [-h|--help] username child"
exit 1
end
username = ARGV.shift
# get uid, gid
userent = Etc.getpwnam(username) \
or fail "unknown user: #{username}"
# get supplementary groups
supp_groups = []
while e = Etc.getgrent
supp_groups << e.name if e.mem.include?(username)
end
# setuid, setgid, setgroups
Process::Sys.setgid userent.gid
Process.groups = supp_groups
Process::Sys.setuid userent.uid
# run given program
program = ARGV.shift
exec(program, *ARGV)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment