Class: Fusuma::Plugin::Appmatcher::UserSwitcher

Inherits:
Object
  • Object
show all
Includes:
CustomProcess
Defined in:
lib/fusuma/plugin/appmatcher/user_switcher.rb

Overview

Drop sudo privileges

Defined Under Namespace

Classes: User

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeUserSwitcher



16
17
18
19
20
21
# File 'lib/fusuma/plugin/appmatcher/user_switcher.rb', line 16

def initialize
  username = ENV["SUDO_USER"] || Etc.getlogin
  uid = `id -u #{username}`.chomp.to_i
  gid = `id -g #{username}`.chomp.to_i
  @login_user = User.new(username, uid, gid)
end

Instance Attribute Details

#login_userObject (readonly)

Returns the value of attribute login_user.



14
15
16
# File 'lib/fusuma/plugin/appmatcher/user_switcher.rb', line 14

def 
  @login_user
end

Instance Method Details

#as_user(user = @login_user, proctitle:) ⇒ Object

Execute the provided block in a child process as the specified user The parent blocks until the child finishes.



33
34
35
36
37
38
39
40
# File 'lib/fusuma/plugin/appmatcher/user_switcher.rb', line 33

def as_user(user = @login_user, proctitle:)
  self.proctitle = "#{self.class.name.underscore}(#{user.username}) -> #{proctitle}"

  fork do
    drop_priv(user)
    yield(user) if block_given?
  end
end

#drop_priv(user) ⇒ Object

Drops privileges to that of the specified user



24
25
26
27
28
29
# File 'lib/fusuma/plugin/appmatcher/user_switcher.rb', line 24

def drop_priv(user)
  # Process.initgroups(user.username, user.gid)
  Process::Sys.setegid(user.gid)
  Process::Sys.setgid(user.gid)
  Process::Sys.setuid(user.uid)
end