sys-admin 1.4.0 → 1.4.1
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGES +6 -0
- data/MANIFEST +1 -0
- data/README +2 -9
- data/Rakefile +53 -0
- data/ext/admin.c +67 -13
- data/ext/admin.h +17 -6
- data/test/tc_unix.rb +3 -12
- data/test/tc_windows.rb +3 -12
- metadata +6 -5
data/CHANGES
CHANGED
@@ -1,3 +1,9 @@
|
|
1
|
+
== 1.4.1 - 21-Mar-2007
|
2
|
+
* Bug fix for OS X. Thanks go to an anonymous user for the spot.
|
3
|
+
* Added a Rakefile. Building, testing and installing should now use the
|
4
|
+
Rake tasks (for non-gem installs).
|
5
|
+
* Much more inline documentation, especially for User and Group attributes.
|
6
|
+
|
1
7
|
== 1.4.0 - 20-Jan-2007
|
2
8
|
* Added the following methods: add_local_user, config_local_user,
|
3
9
|
delete_local_user, add_global_group, config_global_group, and
|
data/MANIFEST
CHANGED
data/README
CHANGED
@@ -3,15 +3,8 @@
|
|
3
3
|
Etc module.
|
4
4
|
|
5
5
|
== Installation
|
6
|
-
|
7
|
-
|
8
|
-
ruby install.rb
|
9
|
-
|
10
|
-
=== Unix
|
11
|
-
ruby extconf.rb (in the 'ext' directory)
|
12
|
-
make
|
13
|
-
ruby tc_admin.rb # optional (in the 'test' directory)
|
14
|
-
make install
|
6
|
+
rake test (optional)
|
7
|
+
rake install
|
15
8
|
|
16
9
|
== Synopsis
|
17
10
|
require 'sys/admin'
|
data/Rakefile
ADDED
@@ -0,0 +1,53 @@
|
|
1
|
+
require 'rake'
|
2
|
+
require 'rake/clean'
|
3
|
+
require 'rake/testtask'
|
4
|
+
|
5
|
+
desc "Clean the build files for the sys-admin source for UNIX systems"
|
6
|
+
task :clean do
|
7
|
+
Dir.chdir('ext') do
|
8
|
+
unless RUBY_PLATFORM.match('mswin')
|
9
|
+
FileUtils.rm_rf('sys') if File.exists?('sys')
|
10
|
+
build_file = 'admin.' + Config::CONFIG['DLEXT']
|
11
|
+
sh 'make distclean' if File.exists?(build_file)
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
desc "Build the sys-admin package on UNIX systems (but don't install it)"
|
17
|
+
task :build => [:clean] do
|
18
|
+
Dir.chdir('ext') do
|
19
|
+
unless RUBY_PLATFORM.match('mswin')
|
20
|
+
ruby 'extconf.rb'
|
21
|
+
sh 'make'
|
22
|
+
build_file = 'admin.' + Config::CONFIG['DLEXT']
|
23
|
+
Dir.mkdir('sys') unless File.exists?('sys')
|
24
|
+
FileUtils.cp(build_file, 'sys')
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
if RUBY_PLATFORM.match('mswin')
|
30
|
+
desc "Install the sys-admin package"
|
31
|
+
task :install do
|
32
|
+
sh 'ruby install.rb'
|
33
|
+
end
|
34
|
+
else
|
35
|
+
desc "Install the sys-admin package"
|
36
|
+
task :install => [:build] do
|
37
|
+
Dir.chdir('ext') do
|
38
|
+
sh 'make install'
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
desc "Run the test suite"
|
44
|
+
Rake::TestTask.new("test") do |t|
|
45
|
+
if RUBY_PLATFORM.match('mswin')
|
46
|
+
t.libs << 'lib'
|
47
|
+
else
|
48
|
+
task :test => :build
|
49
|
+
t.libs << 'ext'
|
50
|
+
t.libs.delete('lib')
|
51
|
+
end
|
52
|
+
t.test_files = FileList['test/tc_admin.rb']
|
53
|
+
end
|
data/ext/admin.c
CHANGED
@@ -6,8 +6,10 @@
|
|
6
6
|
* User.new
|
7
7
|
* User.new{ |user| ... }
|
8
8
|
*
|
9
|
-
* Creates and returns a User object
|
10
|
-
*
|
9
|
+
* Creates and returns a User object, which encapsulates the information
|
10
|
+
* typically found within an /etc/passwd entry, i.e. a struct passwd.
|
11
|
+
*
|
12
|
+
* If a block is provided, yields the object back to the block.
|
11
13
|
*/
|
12
14
|
static VALUE user_init(VALUE self){
|
13
15
|
if(rb_block_given_p())
|
@@ -21,8 +23,10 @@ static VALUE user_init(VALUE self){
|
|
21
23
|
* Group.new
|
22
24
|
* Group.new{ |user| ... }
|
23
25
|
*
|
24
|
-
* Creates and returns a Group object
|
25
|
-
*
|
26
|
+
* Creates and returns a Group object, which encapsulates the information
|
27
|
+
* typically found within an /etc/group entry, i.e. a struct group.
|
28
|
+
*
|
29
|
+
* If a block is provided, yields the object back to the block.
|
26
30
|
*/
|
27
31
|
static VALUE group_init(VALUE self){
|
28
32
|
if(rb_block_given_p())
|
@@ -43,8 +47,8 @@ static VALUE group_init(VALUE self){
|
|
43
47
|
* Developer's Note:
|
44
48
|
*
|
45
49
|
* Uses the (POSIX) reentrant version of getlogin_r() if supported. Otherwise
|
46
|
-
* it resorts to the standard getlogin function.
|
47
|
-
* cuserid().
|
50
|
+
* it resorts to the standard getlogin() function. If that fails, it resorts
|
51
|
+
* to cuserid(). If that fails, it resorts to getenv("USER"). If that fails,
|
48
52
|
* then nil is returned.
|
49
53
|
*/
|
50
54
|
static VALUE admin_get_login(VALUE klass){
|
@@ -77,7 +81,7 @@ static VALUE admin_get_login(VALUE klass){
|
|
77
81
|
* Admin.get_user(name)
|
78
82
|
* Admin.get_user(uid)
|
79
83
|
*
|
80
|
-
* Returns a User object for the given +name+ or +uid+.
|
84
|
+
* Returns a User object for the given +name+ or +uid+. Raises an AdminError
|
81
85
|
* if a user cannot be found for that name or user ID.
|
82
86
|
*/
|
83
87
|
static VALUE admin_get_user(VALUE klass, VALUE v_value){
|
@@ -95,7 +99,7 @@ static VALUE admin_get_user(VALUE klass, VALUE v_value){
|
|
95
99
|
* Admin.get_group(name)
|
96
100
|
* Admin.get_group(gid)
|
97
101
|
*
|
98
|
-
* Returns a Group object for the given +name+ or +gid+.
|
102
|
+
* Returns a Group object for the given +name+ or +gid+. Raises an AdminError
|
99
103
|
* if a group cannot be found for that name or GID.
|
100
104
|
*
|
101
105
|
*--
|
@@ -120,7 +124,7 @@ static VALUE admin_get_group(VALUE klass, VALUE v_value){
|
|
120
124
|
* Admin.groups
|
121
125
|
* Admin.groups{ |group| ... }
|
122
126
|
*
|
123
|
-
* In block form, yields a Group object for each group on the system.
|
127
|
+
* In block form, yields a Group object for each group on the system. In
|
124
128
|
* non-block form, returns an Array of Group objects.
|
125
129
|
*/
|
126
130
|
static VALUE admin_groups(VALUE klass){
|
@@ -171,7 +175,7 @@ static VALUE admin_groups(VALUE klass){
|
|
171
175
|
* Admin.users
|
172
176
|
* Admin.users{ |user| ... }
|
173
177
|
*
|
174
|
-
* In block form, yields a User object for each user on the system.
|
178
|
+
* In block form, yields a User object for each user on the system. In
|
175
179
|
* non-block form, returns an Array of User objects.
|
176
180
|
*/
|
177
181
|
static VALUE admin_users(VALUE klass){
|
@@ -226,11 +230,19 @@ static VALUE admin_users(VALUE klass){
|
|
226
230
|
void Init_admin(){
|
227
231
|
VALUE mSys, cAdmin;
|
228
232
|
|
229
|
-
/*
|
230
|
-
mSys
|
233
|
+
/* The Sys module is used primarily as a namespace for Sys::Admin */
|
234
|
+
mSys = rb_define_module("Sys");
|
235
|
+
|
236
|
+
/* A unified, cross platform replacement for the Etc module. */
|
231
237
|
cAdmin = rb_define_class_under(mSys, "Admin", rb_cObject);
|
232
|
-
|
238
|
+
|
239
|
+
/* Encapsulates information typically found in /etc/passwd */
|
240
|
+
cUser = rb_define_class_under(mSys, "User", rb_cObject);
|
241
|
+
|
242
|
+
/* Encapsulates information typically found in /etc/group */
|
233
243
|
cGroup = rb_define_class_under(mSys, "Group", rb_cObject);
|
244
|
+
|
245
|
+
/* Error raised if any of the Sys::Admin methods fail */
|
234
246
|
cAdminError = rb_define_class_under(mSys, "AdminError", rb_eStandardError);
|
235
247
|
|
236
248
|
/* Class Methods */
|
@@ -245,29 +257,71 @@ void Init_admin(){
|
|
245
257
|
rb_define_method(cGroup,"initialize", group_init, 0);
|
246
258
|
|
247
259
|
/* User Attributes */
|
260
|
+
|
261
|
+
/* The user name associated with the account */
|
248
262
|
rb_define_attr(cUser, "name", 1, 1);
|
263
|
+
|
264
|
+
/* The user's encrypted password. Deprecated in favor of /etc/shadow */
|
249
265
|
rb_define_attr(cUser, "passwd", 1, 1);
|
266
|
+
|
267
|
+
/* The user's user ID */
|
250
268
|
rb_define_attr(cUser, "uid", 1, 1);
|
269
|
+
|
270
|
+
/* The user's primary group ID */
|
251
271
|
rb_define_attr(cUser, "gid", 1, 1);
|
272
|
+
|
273
|
+
/* The absolute pathname of the user's home directory */
|
252
274
|
rb_define_attr(cUser, "dir", 1, 1);
|
275
|
+
|
276
|
+
/* The user's login shell */
|
253
277
|
rb_define_attr(cUser, "shell", 1, 1);
|
278
|
+
|
279
|
+
/* A comment field. Rarely used. */
|
254
280
|
rb_define_attr(cUser, "gecos", 1, 1);
|
281
|
+
|
282
|
+
/* The user's alloted amount of disk space */
|
255
283
|
rb_define_attr(cUser, "quota", 1, 1);
|
284
|
+
|
285
|
+
/* Used in the past for password aging. Deprecated in favor of /etc/shadow */
|
256
286
|
rb_define_attr(cUser, "age", 1, 1);
|
287
|
+
|
288
|
+
/* The user's access class */
|
257
289
|
rb_define_attr(cUser, "class", 1, 1);
|
290
|
+
|
291
|
+
/* Another comment field. Rarely used. */
|
258
292
|
rb_define_attr(cUser, "comment", 1, 1);
|
293
|
+
|
294
|
+
/* Account expiration date */
|
259
295
|
rb_define_attr(cUser, "expire", 1, 1);
|
296
|
+
|
297
|
+
/* Next data a password change will be needed */
|
260
298
|
rb_define_attr(cUser, "change", 1, 1);
|
299
|
+
|
300
|
+
/* The last time the user logged in */
|
261
301
|
rb_define_attr(cUser, "login_time", 1, 0);
|
302
|
+
|
303
|
+
/* The name of the terminal device the user last logged on with */
|
262
304
|
rb_define_attr(cUser, "login_device", 1, 0);
|
305
|
+
|
306
|
+
/* The hostname from which the user last logged in */
|
263
307
|
rb_define_attr(cUser, "login_host", 1, 0);
|
264
308
|
|
265
309
|
/* Group Attributes */
|
310
|
+
|
311
|
+
/* The name of the group */
|
266
312
|
rb_define_attr(cGroup, "name", 1, 1);
|
313
|
+
|
314
|
+
/* The group's group ID */
|
267
315
|
rb_define_attr(cGroup, "gid", 1, 1);
|
316
|
+
|
317
|
+
/* An array of users that are members of the group */
|
268
318
|
rb_define_attr(cGroup, "members", 1, 1);
|
319
|
+
|
320
|
+
/* The group password, if any. */
|
269
321
|
rb_define_attr(cGroup, "passwd", 1, 1);
|
270
322
|
|
271
323
|
/* Constants */
|
324
|
+
|
325
|
+
/* 1.4.1: The version of this package */
|
272
326
|
rb_define_const(cAdmin, "VERSION", rb_str_new2(SYS_ADMIN_VERSION));
|
273
327
|
}
|
data/ext/admin.h
CHANGED
@@ -8,7 +8,7 @@
|
|
8
8
|
#include <errno.h>
|
9
9
|
#include <string.h>
|
10
10
|
|
11
|
-
#define SYS_ADMIN_VERSION "1.4.
|
11
|
+
#define SYS_ADMIN_VERSION "1.4.1"
|
12
12
|
|
13
13
|
#ifdef HAVE_LASTLOG_H
|
14
14
|
#include <lastlog.h>
|
@@ -20,17 +20,27 @@
|
|
20
20
|
#define _POSIX_LOGIN_NAME_MAX 9
|
21
21
|
#endif
|
22
22
|
|
23
|
+
/* OS X returns -1 on this for some reason, so check for it */
|
24
|
+
#if defined(__MACH__) || defined(__APPLE__)
|
25
|
+
#define USER_BUF_SIZE 1024
|
26
|
+
#else
|
23
27
|
#ifdef _SC_GETPW_R_SIZE_MAX
|
24
28
|
#define USER_BUF_SIZE (sysconf(_SC_GETPW_R_SIZE_MAX))
|
25
29
|
#else
|
26
30
|
#define USER_BUF_SIZE 1024
|
27
31
|
#endif
|
32
|
+
#endif
|
28
33
|
|
34
|
+
/* OS X returns -1 on this for some reason, so check for it */
|
35
|
+
#if defined(__MACH__) || defined(__APPLE__)
|
36
|
+
#define GROUP_BUF_SIZE 7296
|
37
|
+
#else
|
29
38
|
#ifdef _SC_GETGR_R_SIZE_MAX
|
30
39
|
#define GROUP_BUF_SIZE (sysconf(_SC_GETGR_R_SIZE_MAX))
|
31
40
|
#else
|
32
41
|
#define GROUP_BUF_SIZE 7296
|
33
42
|
#endif
|
43
|
+
#endif
|
34
44
|
|
35
45
|
#ifndef _PATH_LASTLOG
|
36
46
|
#define _PATH_LASTLOG "/var/adm/lastlog"
|
@@ -126,7 +136,7 @@ static VALUE get_group_by_num(VALUE v_gid){
|
|
126
136
|
struct group* grpbuf;
|
127
137
|
|
128
138
|
if(getgrgid_r(gid, &grp, buf, sizeof(buf), &grpbuf) != 0)
|
129
|
-
rb_raise(cAdminError, "%s", strerror(errno));
|
139
|
+
rb_raise(cAdminError, "getgrgid_r() failed: %s", strerror(errno));
|
130
140
|
|
131
141
|
if(!grpbuf)
|
132
142
|
rb_raise(cAdminError, "no group found for group ID: %i", gid);
|
@@ -172,7 +182,7 @@ static VALUE get_group_by_name(VALUE v_name){
|
|
172
182
|
|
173
183
|
v_group = get_group(grp);
|
174
184
|
#else
|
175
|
-
rb_raise(rb_eNotImpError,"get_group is not supported on this platform");
|
185
|
+
rb_raise(rb_eNotImpError, "get_group is not supported on this platform");
|
176
186
|
#endif
|
177
187
|
|
178
188
|
return v_group;
|
@@ -332,11 +342,12 @@ void get_user_from_value(VALUE v_user, struct passwd* pwd){
|
|
332
342
|
static VALUE get_group(struct group* g){
|
333
343
|
VALUE v_group = rb_funcall(cGroup,rb_intern("new"),0,0);
|
334
344
|
VALUE v_array = rb_ary_new();
|
345
|
+
char **my_gr_mem = g->gr_mem;
|
335
346
|
|
336
347
|
/* Return the members as an Array of Strings */
|
337
|
-
while(*
|
338
|
-
rb_ary_push(v_array, rb_str_new2(*
|
339
|
-
|
348
|
+
while(*my_gr_mem){
|
349
|
+
rb_ary_push(v_array, rb_str_new2(*my_gr_mem));
|
350
|
+
my_gr_mem++;
|
340
351
|
}
|
341
352
|
|
342
353
|
rb_iv_set(v_group, "@name", rb_str_new2(g->gr_name));
|
data/test/tc_unix.rb
CHANGED
@@ -1,18 +1,9 @@
|
|
1
1
|
###############################################################################
|
2
2
|
# tc_unix.rb
|
3
3
|
#
|
4
|
-
# Test suite for the Unix version of sys-admin.
|
4
|
+
# Test suite for the Unix version of sys-admin. This test should be run
|
5
|
+
# via the 'rake test' task.
|
5
6
|
###############################################################################
|
6
|
-
base = File.basename(Dir.pwd)
|
7
|
-
|
8
|
-
if base == "test" || base =~ /sys-admin.*/
|
9
|
-
require "fileutils"
|
10
|
-
Dir.chdir("..") if base == "test"
|
11
|
-
Dir.mkdir("sys") unless File.exists?("sys")
|
12
|
-
FileUtils.cp("ext/admin.so", "sys") if File.exists?("ext/admin.so")
|
13
|
-
$LOAD_PATH.unshift(Dir.pwd)
|
14
|
-
end
|
15
|
-
|
16
7
|
require 'test/unit'
|
17
8
|
require 'sys/admin'
|
18
9
|
include Sys
|
@@ -26,7 +17,7 @@ class TC_Sys_Admin_Unix < Test::Unit::TestCase
|
|
26
17
|
end
|
27
18
|
|
28
19
|
def test_version
|
29
|
-
assert_equal("1.4.
|
20
|
+
assert_equal("1.4.1", Admin::VERSION)
|
30
21
|
end
|
31
22
|
|
32
23
|
def test_get_login
|
data/test/tc_windows.rb
CHANGED
@@ -4,18 +4,9 @@
|
|
4
4
|
# Test suite for the Win32 version of sys-admin. Note that some of the tests
|
5
5
|
# are numbered to ensure a certain order. That way I can add test users
|
6
6
|
# before configuring or deleting them.
|
7
|
+
#
|
8
|
+
# It is assumed that this test will be run via the 'rake test' task.
|
7
9
|
###############################################################################
|
8
|
-
base = File.basename(Dir.pwd)
|
9
|
-
|
10
|
-
if base == "test" || base =~ /sys-admin.*/
|
11
|
-
require "ftools"
|
12
|
-
Dir.chdir("..") if base == "test"
|
13
|
-
Dir.mkdir("sys") unless File.exists?("sys")
|
14
|
-
File.copy("lib/sys/admin.rb", "sys/admin.rb")
|
15
|
-
$LOAD_PATH.unshift(Dir.pwd)
|
16
|
-
Dir.chdir("test") rescue nil
|
17
|
-
end
|
18
|
-
|
19
10
|
require "test/unit"
|
20
11
|
require "sys/admin"
|
21
12
|
require "socket"
|
@@ -33,7 +24,7 @@ class TC_Sys_Admin_Win32 < Test::Unit::TestCase
|
|
33
24
|
end
|
34
25
|
|
35
26
|
def test_version
|
36
|
-
assert_equal("1.4.
|
27
|
+
assert_equal("1.4.1", Admin::VERSION)
|
37
28
|
end
|
38
29
|
|
39
30
|
def test_01_add_local_user
|
metadata
CHANGED
@@ -1,10 +1,10 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
|
-
rubygems_version: 0.9.
|
2
|
+
rubygems_version: 0.9.2
|
3
3
|
specification_version: 1
|
4
4
|
name: sys-admin
|
5
5
|
version: !ruby/object:Gem::Version
|
6
|
-
version: 1.4.
|
7
|
-
date: 2007-
|
6
|
+
version: 1.4.1
|
7
|
+
date: 2007-03-22 00:00:00 -06:00
|
8
8
|
summary: A unified, cross platform replacement for the "etc" package.
|
9
9
|
require_paths:
|
10
10
|
- lib
|
@@ -31,12 +31,13 @@ authors:
|
|
31
31
|
files:
|
32
32
|
- examples/groups.rb
|
33
33
|
- examples/users.rb
|
34
|
+
- test/tc_admin.rb
|
34
35
|
- test/tc_unix.rb
|
35
36
|
- test/tc_windows.rb
|
36
|
-
- test/tc_admin.rb
|
37
|
-
- README
|
38
37
|
- CHANGES
|
39
38
|
- MANIFEST
|
39
|
+
- README
|
40
|
+
- Rakefile
|
40
41
|
- ext/extconf.rb
|
41
42
|
- ext/admin.c
|
42
43
|
- ext/admin.h
|