Sha256: c1db51314da8a21787bbe19948b919e10945c81a11f20e91af077cafe8bdf3bc

Contents?: true

Size: 995 Bytes

Versions: 1

Compression:

Stored size: 995 Bytes

Contents

# frozen_string_literal: true

require 'etc'
module Fusuma
  module Plugin
    module Appmatcher
      class UserSwitcher
        User = Struct.new(:username, :uid, :gid)
        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

        # Drops privileges to that of the specified user
        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

        # Execute the provided block in a child process as the specified user
        # The parent blocks until the child finishes.
        def as_user(user = @login_user)
          fork do
            drop_priv(user)
            yield(user) if block_given?
          end
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
fusuma-plugin-appmatcher-0.1.0.pre lib/fusuma/plugin/appmatcher/user_switcher.rb