sys-admin 1.6.4 → 1.7.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- checksums.yaml.gz.sig +0 -0
- data.tar.gz.sig +0 -0
- data/CHANGES +168 -161
- data/MANIFEST +18 -18
- data/README +148 -154
- data/Rakefile +47 -47
- data/certs/djberg96_pub.pem +22 -17
- data/doc/sys-admin-unix.txt +162 -162
- data/doc/sys-admin-windows.txt +346 -346
- data/examples/example_groups.rb +27 -27
- data/examples/example_users.rb +41 -41
- data/lib/bsd/sys/admin.rb +256 -256
- data/lib/darwin/sys/admin.rb +237 -237
- data/lib/linux/sys/admin.rb +273 -273
- data/lib/sunos/sys/admin.rb +261 -261
- data/lib/sys-admin.rb +1 -1
- data/lib/sys/admin.rb +24 -24
- data/lib/sys/admin/common.rb +137 -137
- data/lib/sys/admin/custom.rb +16 -16
- data/lib/unix/sys/admin.rb +163 -163
- data/lib/windows/sys/admin.rb +997 -997
- data/sys-admin.gemspec +31 -31
- data/test/test_sys_admin.rb +19 -19
- data/test/test_sys_admin_unix.rb +260 -260
- data/test/test_sys_admin_windows.rb +337 -337
- metadata +51 -27
- metadata.gz.sig +0 -0
data/lib/sys-admin.rb
CHANGED
@@ -1 +1 @@
|
|
1
|
-
require_relative 'sys/admin'
|
1
|
+
require_relative 'sys/admin'
|
data/lib/sys/admin.rb
CHANGED
@@ -1,24 +1,24 @@
|
|
1
|
-
module Sys
|
2
|
-
class Admin
|
3
|
-
# The version of the sys-admin library.
|
4
|
-
VERSION =
|
5
|
-
end
|
6
|
-
end
|
7
|
-
|
8
|
-
# Stub to require the correct based on platform
|
9
|
-
require 'rbconfig'
|
10
|
-
|
11
|
-
case RbConfig::CONFIG['host_os']
|
12
|
-
when /linux/i
|
13
|
-
require 'linux/sys/admin'
|
14
|
-
when /sunos|solaris/i
|
15
|
-
require 'sunos/sys/admin'
|
16
|
-
when /cygwin|mingw|mswin|windows|dos/i
|
17
|
-
require 'windows/sys/admin'
|
18
|
-
when /darwin|mach/i
|
19
|
-
require 'darwin/sys/admin'
|
20
|
-
when /bsd/i
|
21
|
-
require 'bsd/sys/admin'
|
22
|
-
else
|
23
|
-
require 'unix/sys/admin'
|
24
|
-
end
|
1
|
+
module Sys
|
2
|
+
class Admin
|
3
|
+
# The version of the sys-admin library.
|
4
|
+
VERSION = '1.7.0'.freeze
|
5
|
+
end
|
6
|
+
end
|
7
|
+
|
8
|
+
# Stub to require the correct based on platform
|
9
|
+
require 'rbconfig'
|
10
|
+
|
11
|
+
case RbConfig::CONFIG['host_os']
|
12
|
+
when /linux/i
|
13
|
+
require 'linux/sys/admin'
|
14
|
+
when /sunos|solaris/i
|
15
|
+
require 'sunos/sys/admin'
|
16
|
+
when /cygwin|mingw|mswin|windows|dos/i
|
17
|
+
require 'windows/sys/admin'
|
18
|
+
when /darwin|mach/i
|
19
|
+
require 'darwin/sys/admin'
|
20
|
+
when /bsd/i
|
21
|
+
require 'bsd/sys/admin'
|
22
|
+
else
|
23
|
+
require 'unix/sys/admin'
|
24
|
+
end
|
data/lib/sys/admin/common.rb
CHANGED
@@ -1,137 +1,137 @@
|
|
1
|
-
require 'ffi'
|
2
|
-
|
3
|
-
# The Sys module serves as a namespace only.
|
4
|
-
module Sys
|
5
|
-
|
6
|
-
# The Admin class provides a unified, cross platform replacement
|
7
|
-
# for the Etc module.
|
8
|
-
class Admin
|
9
|
-
extend FFI::Library
|
10
|
-
ffi_lib FFI::Library::LIBC
|
11
|
-
|
12
|
-
private
|
13
|
-
|
14
|
-
attach_function :strerror, [:int], :string
|
15
|
-
|
16
|
-
attach_function :getlogin, [], :string
|
17
|
-
attach_function :getuid, [], :long
|
18
|
-
attach_function :getpwnam, [:string], :pointer
|
19
|
-
attach_function :getpwuid, [:long], :pointer
|
20
|
-
attach_function :getpwent, [], :pointer
|
21
|
-
attach_function :setpwent, [], :void
|
22
|
-
attach_function :endpwent, [], :void
|
23
|
-
|
24
|
-
attach_function :getgrgid, [:long], :pointer
|
25
|
-
attach_function :getgrnam, [:string], :pointer
|
26
|
-
attach_function :getgrent, [], :pointer
|
27
|
-
attach_function :endgrent, [], :void
|
28
|
-
attach_function :setgrent, [], :void
|
29
|
-
|
30
|
-
private_class_method :getlogin, :getuid, :getpwnam, :getpwuid, :getpwent
|
31
|
-
private_class_method :setpwent, :endpwent, :getgrgid, :getgrnam
|
32
|
-
private_class_method :getgrent, :endgrent, :setgrent, :strerror
|
33
|
-
|
34
|
-
public
|
35
|
-
|
36
|
-
# Error typically raised if any of the Sys::Admin methods fail.
|
37
|
-
class Error < StandardError; end
|
38
|
-
|
39
|
-
# The User class encapsulates the information found in /etc/passwd.
|
40
|
-
class User
|
41
|
-
# The user name associated with the account.
|
42
|
-
attr_accessor :name
|
43
|
-
|
44
|
-
# The user's encrypted password. Deprecated by /etc/shadow.
|
45
|
-
attr_accessor :passwd
|
46
|
-
|
47
|
-
# The user's user ID.
|
48
|
-
attr_accessor :uid
|
49
|
-
|
50
|
-
# The user's group ID.
|
51
|
-
attr_accessor :gid
|
52
|
-
|
53
|
-
# Next date a password change will be needed.
|
54
|
-
attr_accessor :change
|
55
|
-
|
56
|
-
# A comment field. Rarely used now.
|
57
|
-
attr_accessor :gecos
|
58
|
-
|
59
|
-
# The user's alloted amount of disk space.
|
60
|
-
attr_accessor :quota
|
61
|
-
|
62
|
-
# The absolute path name of the user's home directory.
|
63
|
-
attr_accessor :dir
|
64
|
-
|
65
|
-
# The user's login shell.
|
66
|
-
attr_accessor :shell
|
67
|
-
|
68
|
-
# The account's expiration date
|
69
|
-
attr_accessor :expire
|
70
|
-
|
71
|
-
# TODO: Forgot what this is.
|
72
|
-
attr_accessor :fields
|
73
|
-
|
74
|
-
# The user's access class.
|
75
|
-
attr_accessor :access_class
|
76
|
-
|
77
|
-
# Another comment field.
|
78
|
-
attr_accessor :comment
|
79
|
-
|
80
|
-
# Used in the past for password aging. Deprecated by /etc/shadow.
|
81
|
-
attr_accessor :age
|
82
|
-
|
83
|
-
# The last time the user logged in.
|
84
|
-
attr_accessor :login_time
|
85
|
-
|
86
|
-
# The host name from which the user last logged in.
|
87
|
-
attr_accessor :login_host
|
88
|
-
|
89
|
-
# The name of the terminal device the user last logged on with.
|
90
|
-
attr_accessor :login_device
|
91
|
-
|
92
|
-
# Creates and returns a User object, which encapsulates the information
|
93
|
-
# typically found within an /etc/passwd entry, i.e. a struct passwd.
|
94
|
-
#
|
95
|
-
# If a block is provided, yields the object back to the block.
|
96
|
-
#
|
97
|
-
def initialize
|
98
|
-
yield self if block_given?
|
99
|
-
end
|
100
|
-
|
101
|
-
# An array of groups to which the user belongs.
|
102
|
-
def groups
|
103
|
-
array = []
|
104
|
-
|
105
|
-
Sys::Admin.groups.each{ |grp|
|
106
|
-
array << grp.name if grp.members.include?(self.name)
|
107
|
-
}
|
108
|
-
|
109
|
-
array
|
110
|
-
end
|
111
|
-
end
|
112
|
-
|
113
|
-
# The Group class encapsulates information found in /etc/group.
|
114
|
-
class Group
|
115
|
-
# The name of the group.
|
116
|
-
attr_accessor :name
|
117
|
-
|
118
|
-
# The group's group ID.
|
119
|
-
attr_accessor :gid
|
120
|
-
|
121
|
-
# An array of members associated with the group.
|
122
|
-
attr_accessor :members
|
123
|
-
|
124
|
-
# The group password, if any.
|
125
|
-
attr_accessor :passwd
|
126
|
-
|
127
|
-
# Creates and returns a Group object, which encapsulates the information
|
128
|
-
# typically found within an /etc/group entry, i.e. a struct group.
|
129
|
-
#
|
130
|
-
# If a block is provided, yields the object back to the block.
|
131
|
-
#
|
132
|
-
def initialize
|
133
|
-
yield self if block_given?
|
134
|
-
end
|
135
|
-
end
|
136
|
-
end
|
137
|
-
end
|
1
|
+
require 'ffi'
|
2
|
+
|
3
|
+
# The Sys module serves as a namespace only.
|
4
|
+
module Sys
|
5
|
+
|
6
|
+
# The Admin class provides a unified, cross platform replacement
|
7
|
+
# for the Etc module.
|
8
|
+
class Admin
|
9
|
+
extend FFI::Library
|
10
|
+
ffi_lib FFI::Library::LIBC
|
11
|
+
|
12
|
+
private
|
13
|
+
|
14
|
+
attach_function :strerror, [:int], :string
|
15
|
+
|
16
|
+
attach_function :getlogin, [], :string
|
17
|
+
attach_function :getuid, [], :long
|
18
|
+
attach_function :getpwnam, [:string], :pointer
|
19
|
+
attach_function :getpwuid, [:long], :pointer
|
20
|
+
attach_function :getpwent, [], :pointer
|
21
|
+
attach_function :setpwent, [], :void
|
22
|
+
attach_function :endpwent, [], :void
|
23
|
+
|
24
|
+
attach_function :getgrgid, [:long], :pointer
|
25
|
+
attach_function :getgrnam, [:string], :pointer
|
26
|
+
attach_function :getgrent, [], :pointer
|
27
|
+
attach_function :endgrent, [], :void
|
28
|
+
attach_function :setgrent, [], :void
|
29
|
+
|
30
|
+
private_class_method :getlogin, :getuid, :getpwnam, :getpwuid, :getpwent
|
31
|
+
private_class_method :setpwent, :endpwent, :getgrgid, :getgrnam
|
32
|
+
private_class_method :getgrent, :endgrent, :setgrent, :strerror
|
33
|
+
|
34
|
+
public
|
35
|
+
|
36
|
+
# Error typically raised if any of the Sys::Admin methods fail.
|
37
|
+
class Error < StandardError; end
|
38
|
+
|
39
|
+
# The User class encapsulates the information found in /etc/passwd.
|
40
|
+
class User
|
41
|
+
# The user name associated with the account.
|
42
|
+
attr_accessor :name
|
43
|
+
|
44
|
+
# The user's encrypted password. Deprecated by /etc/shadow.
|
45
|
+
attr_accessor :passwd
|
46
|
+
|
47
|
+
# The user's user ID.
|
48
|
+
attr_accessor :uid
|
49
|
+
|
50
|
+
# The user's group ID.
|
51
|
+
attr_accessor :gid
|
52
|
+
|
53
|
+
# Next date a password change will be needed.
|
54
|
+
attr_accessor :change
|
55
|
+
|
56
|
+
# A comment field. Rarely used now.
|
57
|
+
attr_accessor :gecos
|
58
|
+
|
59
|
+
# The user's alloted amount of disk space.
|
60
|
+
attr_accessor :quota
|
61
|
+
|
62
|
+
# The absolute path name of the user's home directory.
|
63
|
+
attr_accessor :dir
|
64
|
+
|
65
|
+
# The user's login shell.
|
66
|
+
attr_accessor :shell
|
67
|
+
|
68
|
+
# The account's expiration date
|
69
|
+
attr_accessor :expire
|
70
|
+
|
71
|
+
# TODO: Forgot what this is.
|
72
|
+
attr_accessor :fields
|
73
|
+
|
74
|
+
# The user's access class.
|
75
|
+
attr_accessor :access_class
|
76
|
+
|
77
|
+
# Another comment field.
|
78
|
+
attr_accessor :comment
|
79
|
+
|
80
|
+
# Used in the past for password aging. Deprecated by /etc/shadow.
|
81
|
+
attr_accessor :age
|
82
|
+
|
83
|
+
# The last time the user logged in.
|
84
|
+
attr_accessor :login_time
|
85
|
+
|
86
|
+
# The host name from which the user last logged in.
|
87
|
+
attr_accessor :login_host
|
88
|
+
|
89
|
+
# The name of the terminal device the user last logged on with.
|
90
|
+
attr_accessor :login_device
|
91
|
+
|
92
|
+
# Creates and returns a User object, which encapsulates the information
|
93
|
+
# typically found within an /etc/passwd entry, i.e. a struct passwd.
|
94
|
+
#
|
95
|
+
# If a block is provided, yields the object back to the block.
|
96
|
+
#
|
97
|
+
def initialize
|
98
|
+
yield self if block_given?
|
99
|
+
end
|
100
|
+
|
101
|
+
# An array of groups to which the user belongs.
|
102
|
+
def groups
|
103
|
+
array = []
|
104
|
+
|
105
|
+
Sys::Admin.groups.each{ |grp|
|
106
|
+
array << grp.name if grp.members.include?(self.name)
|
107
|
+
}
|
108
|
+
|
109
|
+
array
|
110
|
+
end
|
111
|
+
end
|
112
|
+
|
113
|
+
# The Group class encapsulates information found in /etc/group.
|
114
|
+
class Group
|
115
|
+
# The name of the group.
|
116
|
+
attr_accessor :name
|
117
|
+
|
118
|
+
# The group's group ID.
|
119
|
+
attr_accessor :gid
|
120
|
+
|
121
|
+
# An array of members associated with the group.
|
122
|
+
attr_accessor :members
|
123
|
+
|
124
|
+
# The group password, if any.
|
125
|
+
attr_accessor :passwd
|
126
|
+
|
127
|
+
# Creates and returns a Group object, which encapsulates the information
|
128
|
+
# typically found within an /etc/group entry, i.e. a struct group.
|
129
|
+
#
|
130
|
+
# If a block is provided, yields the object back to the block.
|
131
|
+
#
|
132
|
+
def initialize
|
133
|
+
yield self if block_given?
|
134
|
+
end
|
135
|
+
end
|
136
|
+
end
|
137
|
+
end
|
data/lib/sys/admin/custom.rb
CHANGED
@@ -1,16 +1,16 @@
|
|
1
|
-
require 'ffi'
|
2
|
-
|
3
|
-
class FFI::Pointer
|
4
|
-
def read_array_of_string
|
5
|
-
elements = []
|
6
|
-
|
7
|
-
loc = self
|
8
|
-
|
9
|
-
until ((element = loc.read_pointer).null?)
|
10
|
-
elements << element.read_string
|
11
|
-
loc += FFI::Type::POINTER.size
|
12
|
-
end
|
13
|
-
|
14
|
-
elements
|
15
|
-
end
|
16
|
-
end
|
1
|
+
require 'ffi'
|
2
|
+
|
3
|
+
class FFI::Pointer
|
4
|
+
def read_array_of_string
|
5
|
+
elements = []
|
6
|
+
|
7
|
+
loc = self
|
8
|
+
|
9
|
+
until ((element = loc.read_pointer).null?)
|
10
|
+
elements << element.read_string
|
11
|
+
loc += FFI::Type::POINTER.size
|
12
|
+
end
|
13
|
+
|
14
|
+
elements
|
15
|
+
end
|
16
|
+
end
|
data/lib/unix/sys/admin.rb
CHANGED
@@ -1,163 +1,163 @@
|
|
1
|
-
require 'sys/admin/custom'
|
2
|
-
require 'sys/admin/common'
|
3
|
-
|
4
|
-
# Code used as a fallback for UNIX platforms.
|
5
|
-
|
6
|
-
module Sys
|
7
|
-
class Admin
|
8
|
-
private
|
9
|
-
|
10
|
-
class PasswdStruct < FFI::Struct
|
11
|
-
layout(
|
12
|
-
:pw_name, :string,
|
13
|
-
:pw_passwd, :string,
|
14
|
-
:pw_uid, :uint,
|
15
|
-
:pw_gid, :uint,
|
16
|
-
:pw_gecos, :string,
|
17
|
-
:pw_dir, :string,
|
18
|
-
:pw_shell, :string
|
19
|
-
)
|
20
|
-
end
|
21
|
-
|
22
|
-
class GroupStruct < FFI::Struct
|
23
|
-
layout(
|
24
|
-
:gr_name, :string,
|
25
|
-
:gr_passwd, :string,
|
26
|
-
:gr_gid, :uint,
|
27
|
-
:gr_mem, :pointer
|
28
|
-
)
|
29
|
-
end
|
30
|
-
|
31
|
-
public
|
32
|
-
|
33
|
-
# Returns the login for the current process.
|
34
|
-
#
|
35
|
-
def self.get_login
|
36
|
-
getlogin()
|
37
|
-
end
|
38
|
-
|
39
|
-
# Returns a User object for the given name or uid. Raises an error
|
40
|
-
# if a user cannot be found.
|
41
|
-
#
|
42
|
-
# Examples:
|
43
|
-
#
|
44
|
-
# Sys::Admin.get_user('joe')
|
45
|
-
# Sys::Admin.get_user(501)
|
46
|
-
#
|
47
|
-
def self.get_user(uid)
|
48
|
-
if uid.is_a?(String)
|
49
|
-
pwd = PasswdStruct.new(getpwnam(uid))
|
50
|
-
else
|
51
|
-
pwd = PasswdStruct.new(getpwuid(uid))
|
52
|
-
end
|
53
|
-
|
54
|
-
if pwd.null?
|
55
|
-
raise Error, "no user found for: #{uid}"
|
56
|
-
end
|
57
|
-
|
58
|
-
user = User.new do |u|
|
59
|
-
u.name = pwd[:pw_name]
|
60
|
-
u.passwd = pwd[:pw_passwd]
|
61
|
-
u.uid = pwd[:pw_uid]
|
62
|
-
u.gid = pwd[:pw_gid]
|
63
|
-
u.gecos = pwd[:pw_gecos]
|
64
|
-
u.dir = pwd[:pw_dir]
|
65
|
-
u.shell = pwd[:pw_shell]
|
66
|
-
end
|
67
|
-
|
68
|
-
user
|
69
|
-
end
|
70
|
-
|
71
|
-
# Returns a Group object for the given name or uid. Raises an error
|
72
|
-
# if a group cannot be found.
|
73
|
-
#
|
74
|
-
# Examples:
|
75
|
-
#
|
76
|
-
# Sys::Admin.get_group('admin')
|
77
|
-
# Sys::Admin.get_group(101)
|
78
|
-
#
|
79
|
-
def self.get_group(gid)
|
80
|
-
if gid.is_a?(String)
|
81
|
-
grp = GroupStruct.new(getgrnam(gid))
|
82
|
-
else
|
83
|
-
grp = GroupStruct.new(getgrgid(gid))
|
84
|
-
end
|
85
|
-
|
86
|
-
if grp.null?
|
87
|
-
raise Error, "no group found for: #{gid}"
|
88
|
-
end
|
89
|
-
|
90
|
-
Group.new do |g|
|
91
|
-
g.name = grp[:gr_name]
|
92
|
-
g.passwd = grp[:gr_passwd]
|
93
|
-
g.gid = grp[:gr_gid]
|
94
|
-
g.members = grp[:gr_mem].read_array_of_string
|
95
|
-
end
|
96
|
-
end
|
97
|
-
|
98
|
-
# Returns an array of User objects for each user on the system.
|
99
|
-
#
|
100
|
-
def self.users
|
101
|
-
users = []
|
102
|
-
|
103
|
-
begin
|
104
|
-
setpwent()
|
105
|
-
|
106
|
-
until (ptr = getpwent()).null?
|
107
|
-
pwd = PasswdStruct.new(ptr)
|
108
|
-
users << get_user_from_struct(pwd)
|
109
|
-
end
|
110
|
-
ensure
|
111
|
-
endpwent()
|
112
|
-
end
|
113
|
-
|
114
|
-
users
|
115
|
-
end
|
116
|
-
|
117
|
-
# Returns an array of Group objects for each user on the system.
|
118
|
-
#
|
119
|
-
def self.groups
|
120
|
-
groups = []
|
121
|
-
|
122
|
-
begin
|
123
|
-
setgrent()
|
124
|
-
|
125
|
-
until (ptr = getgrent()).null?
|
126
|
-
grp = GroupStruct.new(ptr)
|
127
|
-
groups << get_group_from_struct(grp)
|
128
|
-
end
|
129
|
-
ensure
|
130
|
-
endgrent()
|
131
|
-
end
|
132
|
-
|
133
|
-
groups
|
134
|
-
end
|
135
|
-
|
136
|
-
private
|
137
|
-
|
138
|
-
# Takes a GroupStruct and converts it to a Group object.
|
139
|
-
def self.get_group_from_struct(grp)
|
140
|
-
Group.new do |g|
|
141
|
-
g.name = grp[:gr_name]
|
142
|
-
g.passwd = grp[:gr_passwd]
|
143
|
-
g.gid = grp[:gr_gid]
|
144
|
-
g.members = grp[:gr_mem].read_array_of_string
|
145
|
-
end
|
146
|
-
end
|
147
|
-
|
148
|
-
# Takes a UserStruct and converts it to a User object.
|
149
|
-
def self.get_user_from_struct(pwd)
|
150
|
-
user = User.new do |u|
|
151
|
-
u.name = pwd[:pw_name]
|
152
|
-
u.passwd = pwd[:pw_passwd]
|
153
|
-
u.uid = pwd[:pw_uid]
|
154
|
-
u.gid = pwd[:pw_gid]
|
155
|
-
u.gecos = pwd[:pw_gecos]
|
156
|
-
u.dir = pwd[:pw_dir]
|
157
|
-
u.shell = pwd[:pw_shell]
|
158
|
-
end
|
159
|
-
|
160
|
-
user
|
161
|
-
end
|
162
|
-
end
|
163
|
-
end
|
1
|
+
require 'sys/admin/custom'
|
2
|
+
require 'sys/admin/common'
|
3
|
+
|
4
|
+
# Code used as a fallback for UNIX platforms.
|
5
|
+
|
6
|
+
module Sys
|
7
|
+
class Admin
|
8
|
+
private
|
9
|
+
|
10
|
+
class PasswdStruct < FFI::Struct
|
11
|
+
layout(
|
12
|
+
:pw_name, :string,
|
13
|
+
:pw_passwd, :string,
|
14
|
+
:pw_uid, :uint,
|
15
|
+
:pw_gid, :uint,
|
16
|
+
:pw_gecos, :string,
|
17
|
+
:pw_dir, :string,
|
18
|
+
:pw_shell, :string
|
19
|
+
)
|
20
|
+
end
|
21
|
+
|
22
|
+
class GroupStruct < FFI::Struct
|
23
|
+
layout(
|
24
|
+
:gr_name, :string,
|
25
|
+
:gr_passwd, :string,
|
26
|
+
:gr_gid, :uint,
|
27
|
+
:gr_mem, :pointer
|
28
|
+
)
|
29
|
+
end
|
30
|
+
|
31
|
+
public
|
32
|
+
|
33
|
+
# Returns the login for the current process.
|
34
|
+
#
|
35
|
+
def self.get_login
|
36
|
+
getlogin()
|
37
|
+
end
|
38
|
+
|
39
|
+
# Returns a User object for the given name or uid. Raises an error
|
40
|
+
# if a user cannot be found.
|
41
|
+
#
|
42
|
+
# Examples:
|
43
|
+
#
|
44
|
+
# Sys::Admin.get_user('joe')
|
45
|
+
# Sys::Admin.get_user(501)
|
46
|
+
#
|
47
|
+
def self.get_user(uid)
|
48
|
+
if uid.is_a?(String)
|
49
|
+
pwd = PasswdStruct.new(getpwnam(uid))
|
50
|
+
else
|
51
|
+
pwd = PasswdStruct.new(getpwuid(uid))
|
52
|
+
end
|
53
|
+
|
54
|
+
if pwd.null?
|
55
|
+
raise Error, "no user found for: #{uid}"
|
56
|
+
end
|
57
|
+
|
58
|
+
user = User.new do |u|
|
59
|
+
u.name = pwd[:pw_name]
|
60
|
+
u.passwd = pwd[:pw_passwd]
|
61
|
+
u.uid = pwd[:pw_uid]
|
62
|
+
u.gid = pwd[:pw_gid]
|
63
|
+
u.gecos = pwd[:pw_gecos]
|
64
|
+
u.dir = pwd[:pw_dir]
|
65
|
+
u.shell = pwd[:pw_shell]
|
66
|
+
end
|
67
|
+
|
68
|
+
user
|
69
|
+
end
|
70
|
+
|
71
|
+
# Returns a Group object for the given name or uid. Raises an error
|
72
|
+
# if a group cannot be found.
|
73
|
+
#
|
74
|
+
# Examples:
|
75
|
+
#
|
76
|
+
# Sys::Admin.get_group('admin')
|
77
|
+
# Sys::Admin.get_group(101)
|
78
|
+
#
|
79
|
+
def self.get_group(gid)
|
80
|
+
if gid.is_a?(String)
|
81
|
+
grp = GroupStruct.new(getgrnam(gid))
|
82
|
+
else
|
83
|
+
grp = GroupStruct.new(getgrgid(gid))
|
84
|
+
end
|
85
|
+
|
86
|
+
if grp.null?
|
87
|
+
raise Error, "no group found for: #{gid}"
|
88
|
+
end
|
89
|
+
|
90
|
+
Group.new do |g|
|
91
|
+
g.name = grp[:gr_name]
|
92
|
+
g.passwd = grp[:gr_passwd]
|
93
|
+
g.gid = grp[:gr_gid]
|
94
|
+
g.members = grp[:gr_mem].read_array_of_string
|
95
|
+
end
|
96
|
+
end
|
97
|
+
|
98
|
+
# Returns an array of User objects for each user on the system.
|
99
|
+
#
|
100
|
+
def self.users
|
101
|
+
users = []
|
102
|
+
|
103
|
+
begin
|
104
|
+
setpwent()
|
105
|
+
|
106
|
+
until (ptr = getpwent()).null?
|
107
|
+
pwd = PasswdStruct.new(ptr)
|
108
|
+
users << get_user_from_struct(pwd)
|
109
|
+
end
|
110
|
+
ensure
|
111
|
+
endpwent()
|
112
|
+
end
|
113
|
+
|
114
|
+
users
|
115
|
+
end
|
116
|
+
|
117
|
+
# Returns an array of Group objects for each user on the system.
|
118
|
+
#
|
119
|
+
def self.groups
|
120
|
+
groups = []
|
121
|
+
|
122
|
+
begin
|
123
|
+
setgrent()
|
124
|
+
|
125
|
+
until (ptr = getgrent()).null?
|
126
|
+
grp = GroupStruct.new(ptr)
|
127
|
+
groups << get_group_from_struct(grp)
|
128
|
+
end
|
129
|
+
ensure
|
130
|
+
endgrent()
|
131
|
+
end
|
132
|
+
|
133
|
+
groups
|
134
|
+
end
|
135
|
+
|
136
|
+
private
|
137
|
+
|
138
|
+
# Takes a GroupStruct and converts it to a Group object.
|
139
|
+
def self.get_group_from_struct(grp)
|
140
|
+
Group.new do |g|
|
141
|
+
g.name = grp[:gr_name]
|
142
|
+
g.passwd = grp[:gr_passwd]
|
143
|
+
g.gid = grp[:gr_gid]
|
144
|
+
g.members = grp[:gr_mem].read_array_of_string
|
145
|
+
end
|
146
|
+
end
|
147
|
+
|
148
|
+
# Takes a UserStruct and converts it to a User object.
|
149
|
+
def self.get_user_from_struct(pwd)
|
150
|
+
user = User.new do |u|
|
151
|
+
u.name = pwd[:pw_name]
|
152
|
+
u.passwd = pwd[:pw_passwd]
|
153
|
+
u.uid = pwd[:pw_uid]
|
154
|
+
u.gid = pwd[:pw_gid]
|
155
|
+
u.gecos = pwd[:pw_gecos]
|
156
|
+
u.dir = pwd[:pw_dir]
|
157
|
+
u.shell = pwd[:pw_shell]
|
158
|
+
end
|
159
|
+
|
160
|
+
user
|
161
|
+
end
|
162
|
+
end
|
163
|
+
end
|