Module: User

Extended by:
User
Included in:
User
Defined in:
lib/nub/user.rb

Overview

Some user related helper methods

Instance Method Summary collapse

Instance Method Details

#drop_privilegesObject

Drop root privileges to original user Only affects ruby commands not system commands Params:

returns

uid, gid of prior user



33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/nub/user.rb', line 33

def drop_privileges()
  uid = gid = nil

  if Process.uid.zero?
    uid, gid = Process.uid, Process.gid
    sudo_uid, sudo_gid = ENV['SUDO_UID'].to_i, ENV['SUDO_GID'].to_i
    Process::Sys.setegid(sudo_uid)
    Process::Sys.seteuid(sudo_gid)
  end

  return uid, gid
end

#nameObject

Get the current user taking into account sudo priviledges



65
66
67
# File 'lib/nub/user.rb', line 65

def name
  return Process.uid.zero? ? Etc.getpwuid(ENV['SUDO_UID'].to_i).name : ENV['USER']
end

#raise_privileges(uid, gid) ⇒ Object

Raise privileges if dropped earlier Only affects ruby commands not system commands Params:

uid

uid of user to assume

gid

gid of user to assume



51
52
53
54
55
56
# File 'lib/nub/user.rb', line 51

def raise_privileges(uid, gid)
  if uid and gid
    Process::Sys.seteuid(uid)
    Process::Sys.setegid(gid)
  end
end

#root?Boolean

Check if the current user has root privileges

Returns:

  • (Boolean)


60
61
62
# File 'lib/nub/user.rb', line 60

def root?
  return Process.uid.zero?
end