masquerade 0.1.0 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/CHANGELOG.rdoc ADDED
@@ -0,0 +1,10 @@
1
+ = Changelog
2
+
3
+ == Release 0.2.0 - April 13, 2011
4
+
5
+ * NEW: Support for uid and gid as valid :user and :group values
6
+ * NEW: "as" method returns the result of the code block
7
+
8
+ == Release 0.1.0 - April 13, 2011
9
+
10
+ * Initial release
data/Manifest CHANGED
@@ -1,3 +1,4 @@
1
+ CHANGELOG.rdoc
1
2
  Manifest
2
3
  README.rdoc
3
4
  Rakefile
data/README.rdoc CHANGED
@@ -13,17 +13,32 @@ To run a block of code as user "roger" and group "developer":
13
13
  puts "Hello world"
14
14
  end
15
15
 
16
+ Alternatively, you can use the uid and gid directly:
17
+ Masquerade.as :user => 509, :group => 1009 do
18
+ puts "Hello world"
19
+ end
20
+
16
21
  The block of code accepts the user hash as a paramter. So you can do things like:
17
22
  Masquerade.as :user => "roger", :group => "developer" do |user_info|
18
- puts "Hello world #user_info[:user]"
23
+ puts "Hello #{user_info[:user]}"
24
+ end
25
+
26
+ Both :user and :group params are optional and you can choose to give one of the two or both:
27
+ Masquerade.as :user => "roger" do |user_info|
28
+ puts "Hello #{user_info[:user]}"
19
29
  end
20
30
 
21
- Both :user and :group params are optional and you can choose to give one or the two or both.
22
-
23
31
  === Exceptions thrown
24
32
  ==== Masquerade::PermissionsError
25
33
 
26
34
  If the user running the script does not have the privileges to masquerade as the given user.
27
35
 
28
36
  ==== Masquerade::BadUserOrGroupError
29
- If the given user or group does not exist
37
+ If the given user or group does not exist
38
+
39
+ === Return value
40
+
41
+ Returns the result of the block call. So you can do:
42
+ response = Masquerade.as :user => "roger", :group => "developer" do |user_info|
43
+ "Hello world #{user_info[:user]}"
44
+ end
data/Rakefile CHANGED
@@ -2,7 +2,7 @@ require "rubygems"
2
2
  require "rake"
3
3
  require "echoe"
4
4
 
5
- Echoe.new("masquerade", "0.1.0") do |p|
5
+ Echoe.new("masquerade", "0.2.0") do |p|
6
6
  p.description = "A user/group impersonator"
7
7
  p.summary = "Run a block of code as another user/group."
8
8
  p.url = "http://rubygems.org/gems/masquerade"
data/lib/masquerade.rb CHANGED
@@ -15,13 +15,21 @@ To run a block of code as user "roger" and group "developer":
15
15
  puts "Hello world"
16
16
  end
17
17
 
18
+ Alternatively, you can use the uid and gid directly:
19
+ Masquerade.as :user => 509, :group => 1009 do
20
+ puts "Hello world"
21
+ end
22
+
18
23
  The block of code accepts the user hash as a paramter. So you can do things like:
19
24
  Masquerade.as :user => "roger", :group => "developer" do |user_info|
20
- puts "Hello world #user_info[:user]"
25
+ puts "Hello #{user_info[:user]}"
26
+ end
27
+
28
+ Both :user and :group params are optional and you can choose to give one of the two or both:
29
+ Masquerade.as :user => "roger" do |user_info|
30
+ puts "Hello #{user_info[:user]}"
21
31
  end
22
32
 
23
- Both :user and :group params are optional and you can choose to give one or the two or both.
24
-
25
33
  === Exceptions thrown
26
34
  ==== Masquerade::PermissionsError
27
35
 
@@ -29,31 +37,53 @@ If the user running the script does not have the privileges to masquerade as the
29
37
 
30
38
  ==== Masquerade::BadUserOrGroupError
31
39
  If the given user or group does not exist
40
+
41
+ === Return value
42
+
43
+ Returns the result of the block call. So you can do:
44
+ response = Masquerade.as :user => "roger", :group => "developer" do |user_info|
45
+ "Hello world #{user_info[:user]}"
46
+ end
32
47
  =end
33
48
  module Masquerade
34
49
  def self.as(who, &block)
35
50
  begin
36
51
  current_uid = Process.uid
37
52
  current_gid = Process.gid
38
- # get user info by name
39
53
  if who.include?(:user)
40
- user_struct = Etc.getpwnam(who[:user])
54
+ user_struct = nil
55
+ if who[:user].kind_of? String
56
+ # get user info by name
57
+ user_struct = Etc.getpwnam(who[:user])
58
+ elsif who[:user].kind_of? Numeric
59
+ # get user info by uid
60
+ user_struct = Etc.getpwuid(who[:user])
61
+ end
41
62
  # set the uid of the current process to that of the chosen user
42
63
  Process.uid = user_struct.uid
43
64
  end
44
65
  if who.include?(:group)
45
- group_struct = Etc.getgrnam(who[:group])
66
+ group_struct = nil
67
+ if who[:group].kind_of? String
68
+ # get group info by name
69
+ group_struct = Etc.getgrnam(who[:group])
70
+ elsif who[:group].kind_of? Numeric
71
+ # get group info by gid
72
+ group_struct = Etc.getgrgid(who[:group])
73
+ end
74
+ # set the gid of the current process to that of the chosen group
46
75
  Process.gid = group_struct.gid
47
76
  end
48
77
  # run the block
49
- block.call(who)
78
+ response = block.call(who)
50
79
  # restore process uid and gid
51
80
  Process.uid = current_uid
52
81
  Process.gid = current_gid
82
+ return response
53
83
  rescue Errno::EPERM
54
84
  raise PermissionsError, "You do not have permissions to impersonate this user or group"
55
85
  rescue ArgumentError
56
- raise BadUserOrGroupError, "The user or group name does not exist"
86
+ raise BadUserOrGroupError, "The user or group does not exist"
57
87
  end
58
88
  end
59
89
 
data/masquerade.gemspec CHANGED
@@ -2,15 +2,15 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = %q{masquerade}
5
- s.version = "0.1.0"
5
+ s.version = "0.2.0"
6
6
 
7
7
  s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
8
8
  s.authors = ["Nitesh Goel"]
9
9
  s.date = %q{2011-04-14}
10
10
  s.description = %q{A user/group impersonator}
11
11
  s.email = %q{nitesh@wikinvest.com}
12
- s.extra_rdoc_files = ["README.rdoc", "lib/masquerade.rb"]
13
- s.files = ["Manifest", "README.rdoc", "Rakefile", "lib/masquerade.rb", "masquerade.gemspec"]
12
+ s.extra_rdoc_files = ["CHANGELOG.rdoc", "README.rdoc", "lib/masquerade.rb"]
13
+ s.files = ["CHANGELOG.rdoc", "Manifest", "README.rdoc", "Rakefile", "lib/masquerade.rb", "masquerade.gemspec"]
14
14
  s.homepage = %q{http://rubygems.org/gems/masquerade}
15
15
  s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Masquerade", "--main", "README.rdoc"]
16
16
  s.require_paths = ["lib"]
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: masquerade
3
3
  version: !ruby/object:Gem::Version
4
- hash: 27
4
+ hash: 23
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
- - 1
8
+ - 2
9
9
  - 0
10
- version: 0.1.0
10
+ version: 0.2.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Nitesh Goel
@@ -25,9 +25,11 @@ executables: []
25
25
  extensions: []
26
26
 
27
27
  extra_rdoc_files:
28
+ - CHANGELOG.rdoc
28
29
  - README.rdoc
29
30
  - lib/masquerade.rb
30
31
  files:
32
+ - CHANGELOG.rdoc
31
33
  - Manifest
32
34
  - README.rdoc
33
35
  - Rakefile