sys-admin 1.7.0 → 1.7.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,27 +1,27 @@
1
- ###########################################################################
2
- # groups.rb
3
- #
4
- # Sample script to demonstrate some of the various group methods. Alter
5
- # as you see fit.
6
- ###########################################################################
7
- require "pp"
8
- require "sys/admin"
9
- include Sys
10
-
11
- if File::ALT_SEPARATOR
12
- pp Admin.get_group("guests")
13
- pp Admin.get_group(513)
14
- else
15
- pp Admin.get_group("admin")
16
- pp Admin.get_group(7)
17
- end
18
-
19
- sleep 3
20
-
21
- Admin.groups.each{ |g|
22
- pp g
23
- puts
24
- }
25
-
26
- # This should raise an error
27
- Admin.get_group("fofofofof")
1
+ ###########################################################################
2
+ # groups.rb
3
+ #
4
+ # Sample script to demonstrate some of the various group methods. Alter
5
+ # as you see fit.
6
+ ###########################################################################
7
+ require "pp"
8
+ require "sys/admin"
9
+ include Sys
10
+
11
+ if File::ALT_SEPARATOR
12
+ pp Admin.get_group("guests")
13
+ pp Admin.get_group(513)
14
+ else
15
+ pp Admin.get_group("admin")
16
+ pp Admin.get_group(7)
17
+ end
18
+
19
+ sleep 3
20
+
21
+ Admin.groups.each{ |g|
22
+ pp g
23
+ puts
24
+ }
25
+
26
+ # This should raise an error
27
+ Admin.get_group("fofofofof")
@@ -1,41 +1,41 @@
1
- ###########################################################################
2
- # users.rb
3
- #
4
- # Sample script to demonstrate some of the various user methods. Alter
5
- # as you see fit.
6
- ###########################################################################
7
- require "pp"
8
- require "sys/admin"
9
- include Sys
10
-
11
- =begin
12
- user = User.new do |u|
13
- u.name = "Foo"
14
- u.description = "Test account"
15
- u.password = "changeme"
16
- #u.lockout = false
17
- u.disabled = true
18
- #u.password_required = true
19
- end
20
-
21
- Admin.delete_user(u.name) rescue nil
22
- Admin.add_user(user)
23
-
24
- pp Admin.get_user("Foo")
25
-
26
- Admin.delete_user("Foo")
27
- =end
28
-
29
- user = Admin.get_login
30
-
31
- puts "User: #{user}"
32
-
33
- sleep 3
34
-
35
- Admin.users.each{ |u|
36
- pp u
37
- puts
38
- }
39
-
40
- pp Admin.get_user(user)
41
- pp Admin.get_user(501)
1
+ ###########################################################################
2
+ # users.rb
3
+ #
4
+ # Sample script to demonstrate some of the various user methods. Alter
5
+ # as you see fit.
6
+ ###########################################################################
7
+ require "pp"
8
+ require "sys/admin"
9
+ include Sys
10
+
11
+ =begin
12
+ user = User.new do |u|
13
+ u.name = "Foo"
14
+ u.description = "Test account"
15
+ u.password = "changeme"
16
+ #u.lockout = false
17
+ u.disabled = true
18
+ #u.password_required = true
19
+ end
20
+
21
+ Admin.delete_user(u.name) rescue nil
22
+ Admin.add_user(user)
23
+
24
+ pp Admin.get_user("Foo")
25
+
26
+ Admin.delete_user("Foo")
27
+ =end
28
+
29
+ user = Admin.get_login
30
+
31
+ puts "User: #{user}"
32
+
33
+ sleep 3
34
+
35
+ Admin.users.each{ |u|
36
+ pp u
37
+ puts
38
+ }
39
+
40
+ pp Admin.get_user(user)
41
+ pp Admin.get_user(501)
@@ -1,256 +1,256 @@
1
- require 'sys/admin/custom'
2
- require 'sys/admin/common'
3
- require 'rbconfig'
4
-
5
- # The BSD specific code.
6
-
7
- module Sys
8
- class Admin
9
- private
10
-
11
- # :no-doc:
12
- BUF_MAX = 65536 # Max buffer for retry
13
-
14
- # I'm making some aliases here to prevent potential conflicts
15
- attach_function :open_c, :open, [:string, :int], :int
16
- attach_function :pread_c, :pread, [:int, :pointer, :size_t, :off_t], :size_t
17
- attach_function :close_c, :close, [:int], :int
18
-
19
- attach_function :getlogin_r, [:pointer, :int], :int
20
- attach_function :getpwnam_r, [:string, :pointer, :pointer, :size_t, :pointer], :int
21
- attach_function :getpwuid_r, [:long, :pointer, :pointer, :size_t, :pointer], :int
22
- attach_function :getgrnam_r, [:string, :pointer, :pointer, :size_t, :pointer], :int
23
- attach_function :getgrgid_r, [:long, :pointer, :pointer, :size_t, :pointer], :int
24
-
25
- private_class_method :getlogin_r, :getpwnam_r, :getpwuid_r, :getgrnam_r
26
- private_class_method :getgrgid_r
27
- private_class_method :open_c, :pread_c, :close_c
28
-
29
- # struct passwd from /usr/include/pwd.h
30
- class PasswdStruct < FFI::Struct
31
- fields = [
32
- :pw_name, :string,
33
- :pw_passwd, :string,
34
- :pw_uid, :uid_t,
35
- :pw_gid, :gid_t,
36
- :pw_change, :time_t,
37
- :pw_class, :string,
38
- :pw_gecos, :string,
39
- :pw_dir, :string,
40
- :pw_shell, :string,
41
- :pw_expire, :time_t
42
- ]
43
-
44
- if RbConfig::CONFIG['host_os'] =~ /freebsd/i
45
- fields.push(:pw_fields, :int)
46
- end
47
-
48
- layout(*fields)
49
- end
50
-
51
- # struct group from /usr/include/grp.h
52
- class GroupStruct < FFI::Struct
53
- layout(
54
- :gr_name, :string,
55
- :gr_passwd, :string,
56
- :gr_gid, :uint,
57
- :gr_mem, :pointer
58
- )
59
- end
60
-
61
- # I'm blending the timeval struct in directly here
62
- class LastlogStruct < FFI::Struct
63
- layout(
64
- :ll_time, :int32,
65
- :ll_line, [:char, 32],
66
- :ll_host, [:char, 256]
67
- )
68
- end
69
-
70
- public
71
-
72
- # Returns the login for the current process.
73
- #
74
- def self.get_login
75
- buf = FFI::MemoryPointer.new(:char, 256)
76
-
77
- if getlogin_r(buf, buf.size) != 0
78
- raise Error, "getlogin_r function failed: " + strerror(FFI.errno)
79
- end
80
-
81
- buf.read_string
82
- end
83
-
84
- # Returns a User object for the given name or uid. Raises an error
85
- # if a user cannot be found.
86
- #
87
- # Examples:
88
- #
89
- # Sys::Admin.get_user('joe')
90
- # Sys::Admin.get_user(501)
91
- #
92
- def self.get_user(uid)
93
- buf = FFI::MemoryPointer.new(:char, 1024)
94
- pbuf = FFI::MemoryPointer.new(PasswdStruct)
95
- temp = PasswdStruct.new
96
-
97
- if uid.is_a?(String)
98
- if getpwnam_r(uid, temp, buf, buf.size, pbuf) != 0
99
- raise Error, "getpwnam_r function failed: " + strerror(FFI.errno)
100
- end
101
- else
102
- if getpwuid_r(uid, temp, buf, buf.size, pbuf) != 0
103
- raise Error, "getpwuid_r function failed: " + strerror(FFI.errno)
104
- end
105
- end
106
-
107
- ptr = pbuf.read_pointer
108
-
109
- if ptr.null?
110
- raise Error, "no user found for #{uid}"
111
- end
112
-
113
- pwd = PasswdStruct.new(ptr)
114
- get_user_from_struct(pwd)
115
- end
116
-
117
- # Returns a Group object for the given name or uid. Raises an error
118
- # if a group cannot be found.
119
- #
120
- # Examples:
121
- #
122
- # Sys::Admin.get_group('admin')
123
- # Sys::Admin.get_group(101)
124
- #
125
- def self.get_group(gid)
126
- size = 1024
127
- buf = FFI::MemoryPointer.new(:char, size)
128
- pbuf = FFI::MemoryPointer.new(PasswdStruct)
129
- temp = GroupStruct.new
130
-
131
- begin
132
- if gid.is_a?(String)
133
- val = getgrnam_r(gid, temp, buf, buf.size, pbuf)
134
- fun = 'getgrnam_r'
135
- else
136
- val = getgrgid_r(gid, temp, buf, buf.size, pbuf)
137
- fun = 'getgrgid_r'
138
- end
139
- raise SystemCallError.new(fun, val) if val != 0
140
- rescue Errno::ERANGE
141
- size += 1024
142
- raise if size > BUF_MAX
143
- buf = FFI::MemoryPointer.new(:char, size)
144
- retry
145
- end
146
-
147
- ptr = pbuf.read_pointer
148
-
149
- if ptr.null?
150
- raise Error, "no group found for '#{gid}'"
151
- end
152
-
153
- grp = GroupStruct.new(ptr)
154
- get_group_from_struct(grp)
155
- end
156
-
157
- # Returns an array of User objects for each user on the system.
158
- #
159
- def self.users
160
- users = []
161
-
162
- begin
163
- setpwent()
164
-
165
- until (ptr = getpwent()).null?
166
- pwd = PasswdStruct.new(ptr)
167
- users << get_user_from_struct(pwd)
168
- end
169
- ensure
170
- endpwent()
171
- end
172
-
173
- users
174
- end
175
-
176
- # Returns an array of Group objects for each user on the system.
177
- #
178
- def self.groups
179
- groups = []
180
-
181
- begin
182
- setgrent()
183
-
184
- until (ptr = getgrent()).null?
185
- grp = GroupStruct.new(ptr)
186
- groups << get_group_from_struct(grp)
187
- end
188
- ensure
189
- endgrent()
190
- end
191
-
192
- groups
193
- end
194
-
195
- private
196
-
197
- # Takes a GroupStruct and converts it to a Group object.
198
- def self.get_group_from_struct(grp)
199
- Group.new do |g|
200
- g.name = grp[:gr_name]
201
- g.passwd = grp[:gr_passwd]
202
- g.gid = grp[:gr_gid]
203
- g.members = grp[:gr_mem].read_array_of_string
204
- end
205
- end
206
-
207
- # Takes a UserStruct and converts it to a User object.
208
- def self.get_user_from_struct(pwd)
209
- user = User.new do |u|
210
- u.name = pwd[:pw_name]
211
- u.passwd = pwd[:pw_passwd]
212
- u.uid = pwd[:pw_uid]
213
- u.gid = pwd[:pw_gid]
214
- u.change = Time.at(pwd[:pw_change])
215
- u.access_class = pwd[:pw_class]
216
- u.gecos = pwd[:pw_gecos]
217
- u.dir = pwd[:pw_dir]
218
- u.shell = pwd[:pw_shell]
219
- u.expire = Time.at(pwd[:pw_expire])
220
- end
221
-
222
- log = get_lastlog_info(user.uid)
223
-
224
- if log
225
- user.login_time = Time.at(log[:ll_time])
226
- user.login_device = log[:ll_line].to_s
227
- user.login_host = log[:ll_host].to_s
228
- end
229
-
230
- user
231
- end
232
-
233
- # Get lastlog information for the given user.
234
- def self.get_lastlog_info(uid)
235
- logfile = '/var/log/lastlog'
236
- lastlog = LastlogStruct.new
237
-
238
- begin
239
- fd = open_c(logfile, File::RDONLY)
240
-
241
- if fd != -1
242
- bytes = pread_c(fd, lastlog, lastlog.size, uid * lastlog.size)
243
- if bytes < 0
244
- raise Error, "pread function failed: " + strerror(FFI.errno)
245
- end
246
- else
247
- nil # Ignore, improper permissions
248
- end
249
- ensure
250
- close_c(fd) if fd && fd >= 0
251
- end
252
-
253
- lastlog
254
- end
255
- end
256
- end
1
+ require 'sys/admin/custom'
2
+ require 'sys/admin/common'
3
+ require 'rbconfig'
4
+
5
+ # The BSD specific code.
6
+
7
+ module Sys
8
+ class Admin
9
+ private
10
+
11
+ # :no-doc:
12
+ BUF_MAX = 65536 # Max buffer for retry
13
+
14
+ # I'm making some aliases here to prevent potential conflicts
15
+ attach_function :open_c, :open, [:string, :int], :int
16
+ attach_function :pread_c, :pread, [:int, :pointer, :size_t, :off_t], :size_t
17
+ attach_function :close_c, :close, [:int], :int
18
+
19
+ attach_function :getlogin_r, [:pointer, :int], :int
20
+ attach_function :getpwnam_r, [:string, :pointer, :pointer, :size_t, :pointer], :int
21
+ attach_function :getpwuid_r, [:long, :pointer, :pointer, :size_t, :pointer], :int
22
+ attach_function :getgrnam_r, [:string, :pointer, :pointer, :size_t, :pointer], :int
23
+ attach_function :getgrgid_r, [:long, :pointer, :pointer, :size_t, :pointer], :int
24
+
25
+ private_class_method :getlogin_r, :getpwnam_r, :getpwuid_r, :getgrnam_r
26
+ private_class_method :getgrgid_r
27
+ private_class_method :open_c, :pread_c, :close_c
28
+
29
+ # struct passwd from /usr/include/pwd.h
30
+ class PasswdStruct < FFI::Struct
31
+ fields = [
32
+ :pw_name, :string,
33
+ :pw_passwd, :string,
34
+ :pw_uid, :uid_t,
35
+ :pw_gid, :gid_t,
36
+ :pw_change, :time_t,
37
+ :pw_class, :string,
38
+ :pw_gecos, :string,
39
+ :pw_dir, :string,
40
+ :pw_shell, :string,
41
+ :pw_expire, :time_t
42
+ ]
43
+
44
+ if RbConfig::CONFIG['host_os'] =~ /freebsd/i
45
+ fields.push(:pw_fields, :int)
46
+ end
47
+
48
+ layout(*fields)
49
+ end
50
+
51
+ # struct group from /usr/include/grp.h
52
+ class GroupStruct < FFI::Struct
53
+ layout(
54
+ :gr_name, :string,
55
+ :gr_passwd, :string,
56
+ :gr_gid, :uint,
57
+ :gr_mem, :pointer
58
+ )
59
+ end
60
+
61
+ # I'm blending the timeval struct in directly here
62
+ class LastlogStruct < FFI::Struct
63
+ layout(
64
+ :ll_time, :int32,
65
+ :ll_line, [:char, 32],
66
+ :ll_host, [:char, 256]
67
+ )
68
+ end
69
+
70
+ public
71
+
72
+ # Returns the login for the current process.
73
+ #
74
+ def self.get_login
75
+ buf = FFI::MemoryPointer.new(:char, 256)
76
+
77
+ if getlogin_r(buf, buf.size) != 0
78
+ raise Error, "getlogin_r function failed: " + strerror(FFI.errno)
79
+ end
80
+
81
+ buf.read_string
82
+ end
83
+
84
+ # Returns a User object for the given name or uid. Raises an error
85
+ # if a user cannot be found.
86
+ #
87
+ # Examples:
88
+ #
89
+ # Sys::Admin.get_user('joe')
90
+ # Sys::Admin.get_user(501)
91
+ #
92
+ def self.get_user(uid)
93
+ buf = FFI::MemoryPointer.new(:char, 1024)
94
+ pbuf = FFI::MemoryPointer.new(PasswdStruct)
95
+ temp = PasswdStruct.new
96
+
97
+ if uid.is_a?(String)
98
+ if getpwnam_r(uid, temp, buf, buf.size, pbuf) != 0
99
+ raise Error, "getpwnam_r function failed: " + strerror(FFI.errno)
100
+ end
101
+ else
102
+ if getpwuid_r(uid, temp, buf, buf.size, pbuf) != 0
103
+ raise Error, "getpwuid_r function failed: " + strerror(FFI.errno)
104
+ end
105
+ end
106
+
107
+ ptr = pbuf.read_pointer
108
+
109
+ if ptr.null?
110
+ raise Error, "no user found for #{uid}"
111
+ end
112
+
113
+ pwd = PasswdStruct.new(ptr)
114
+ get_user_from_struct(pwd)
115
+ end
116
+
117
+ # Returns a Group object for the given name or uid. Raises an error
118
+ # if a group cannot be found.
119
+ #
120
+ # Examples:
121
+ #
122
+ # Sys::Admin.get_group('admin')
123
+ # Sys::Admin.get_group(101)
124
+ #
125
+ def self.get_group(gid)
126
+ size = 1024
127
+ buf = FFI::MemoryPointer.new(:char, size)
128
+ pbuf = FFI::MemoryPointer.new(PasswdStruct)
129
+ temp = GroupStruct.new
130
+
131
+ begin
132
+ if gid.is_a?(String)
133
+ val = getgrnam_r(gid, temp, buf, buf.size, pbuf)
134
+ fun = 'getgrnam_r'
135
+ else
136
+ val = getgrgid_r(gid, temp, buf, buf.size, pbuf)
137
+ fun = 'getgrgid_r'
138
+ end
139
+ raise SystemCallError.new(fun, val) if val != 0
140
+ rescue Errno::ERANGE
141
+ size += 1024
142
+ raise if size > BUF_MAX
143
+ buf = FFI::MemoryPointer.new(:char, size)
144
+ retry
145
+ end
146
+
147
+ ptr = pbuf.read_pointer
148
+
149
+ if ptr.null?
150
+ raise Error, "no group found for '#{gid}'"
151
+ end
152
+
153
+ grp = GroupStruct.new(ptr)
154
+ get_group_from_struct(grp)
155
+ end
156
+
157
+ # Returns an array of User objects for each user on the system.
158
+ #
159
+ def self.users
160
+ users = []
161
+
162
+ begin
163
+ setpwent()
164
+
165
+ until (ptr = getpwent()).null?
166
+ pwd = PasswdStruct.new(ptr)
167
+ users << get_user_from_struct(pwd)
168
+ end
169
+ ensure
170
+ endpwent()
171
+ end
172
+
173
+ users
174
+ end
175
+
176
+ # Returns an array of Group objects for each user on the system.
177
+ #
178
+ def self.groups
179
+ groups = []
180
+
181
+ begin
182
+ setgrent()
183
+
184
+ until (ptr = getgrent()).null?
185
+ grp = GroupStruct.new(ptr)
186
+ groups << get_group_from_struct(grp)
187
+ end
188
+ ensure
189
+ endgrent()
190
+ end
191
+
192
+ groups
193
+ end
194
+
195
+ private
196
+
197
+ # Takes a GroupStruct and converts it to a Group object.
198
+ def self.get_group_from_struct(grp)
199
+ Group.new do |g|
200
+ g.name = grp[:gr_name]
201
+ g.passwd = grp[:gr_passwd]
202
+ g.gid = grp[:gr_gid]
203
+ g.members = grp[:gr_mem].read_array_of_string
204
+ end
205
+ end
206
+
207
+ # Takes a UserStruct and converts it to a User object.
208
+ def self.get_user_from_struct(pwd)
209
+ user = User.new do |u|
210
+ u.name = pwd[:pw_name]
211
+ u.passwd = pwd[:pw_passwd]
212
+ u.uid = pwd[:pw_uid]
213
+ u.gid = pwd[:pw_gid]
214
+ u.change = Time.at(pwd[:pw_change])
215
+ u.access_class = pwd[:pw_class]
216
+ u.gecos = pwd[:pw_gecos]
217
+ u.dir = pwd[:pw_dir]
218
+ u.shell = pwd[:pw_shell]
219
+ u.expire = Time.at(pwd[:pw_expire])
220
+ end
221
+
222
+ log = get_lastlog_info(user.uid)
223
+
224
+ if log
225
+ user.login_time = Time.at(log[:ll_time])
226
+ user.login_device = log[:ll_line].to_s
227
+ user.login_host = log[:ll_host].to_s
228
+ end
229
+
230
+ user
231
+ end
232
+
233
+ # Get lastlog information for the given user.
234
+ def self.get_lastlog_info(uid)
235
+ logfile = '/var/log/lastlog'
236
+ lastlog = LastlogStruct.new
237
+
238
+ begin
239
+ fd = open_c(logfile, File::RDONLY)
240
+
241
+ if fd != -1
242
+ bytes = pread_c(fd, lastlog, lastlog.size, uid * lastlog.size)
243
+ if bytes < 0
244
+ raise Error, "pread function failed: " + strerror(FFI.errno)
245
+ end
246
+ else
247
+ nil # Ignore, improper permissions
248
+ end
249
+ ensure
250
+ close_c(fd) if fd && fd >= 0
251
+ end
252
+
253
+ lastlog
254
+ end
255
+ end
256
+ end