58 lines
1007 B
Crystal
58 lines
1007 B
Crystal
|
|
lib LibC
|
|
struct Passwd
|
|
pw_name : Char*
|
|
pw_passwd : Char*
|
|
pw_uid : UInt
|
|
pw_gid : UInt
|
|
pw_gecos : Char*
|
|
pw_dir : UInt
|
|
pw_shell : Char*
|
|
end
|
|
|
|
fun setuid(Int32) : Int32
|
|
fun setgid(Int32) : Int32
|
|
fun getpwnam(Char*) : Passwd*
|
|
end
|
|
|
|
module System
|
|
def self.become_user(user_name : String)
|
|
pointer = LibC.getpwnam user_name.to_unsafe
|
|
|
|
if pointer.null?
|
|
return nil
|
|
end
|
|
|
|
passwd = pointer.value
|
|
|
|
# FIXME: Probably should get some errno magic right now.
|
|
if 0 != LibC.setuid passwd.pw_uid
|
|
raise Exception.new "setuid failed"
|
|
end
|
|
|
|
if 0 != LibC.setgid passwd.pw_gid
|
|
raise Exception.new "setuid failed"
|
|
end
|
|
|
|
passwd
|
|
end
|
|
end
|
|
#def get_uid_gid(user_name : String)
|
|
# pointer = LibC.getpwnam user_name.to_unsafe
|
|
#
|
|
# if pointer.null?
|
|
# return nil
|
|
# end
|
|
#
|
|
# passwd = pointer.value
|
|
#
|
|
# {passwd.pw_uid, passwd.pw_gid}
|
|
#end
|
|
|
|
#uid, gid = get_uid_gid("http").not_nil!
|
|
#LibC.setuid uid
|
|
#LibC.setgid gid
|
|
|
|
#puts Process.run "whoami", output: Process::Redirect::Inherit
|
|
|