sys-admin 1.5.6 → 1.6.0
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGES +8 -0
- data/MANIFEST +6 -3
- data/README +3 -17
- data/Rakefile +22 -51
- data/examples/example_groups.rb +27 -0
- data/examples/example_users.rb +41 -0
- data/lib/bsd/sys/admin.rb +239 -0
- data/lib/darwin/sys/admin.rb +227 -0
- data/lib/linux/sys/admin.rb +259 -0
- data/lib/sunos/sys/admin.rb +241 -0
- data/lib/sys/admin.rb +17 -0
- data/lib/sys/admin/common.rb +140 -0
- data/lib/sys/admin/custom.rb +16 -0
- data/lib/unix/sys/admin.rb +164 -0
- data/lib/windows/sys/admin.rb +971 -0
- data/sys-admin.gemspec +3 -2
- data/test/test_sys_admin.rb +3 -5
- data/test/test_sys_admin_unix.rb +96 -81
- data/test/test_sys_admin_windows.rb +16 -38
- metadata +67 -59
- data/examples/groups.rb +0 -39
- data/examples/users.rb +0 -53
- data/ext/extconf.rb +0 -72
- data/ext/sys/admin.c +0 -419
- data/ext/sys/admin.h +0 -515
metadata
CHANGED
@@ -1,58 +1,74 @@
|
|
1
|
-
--- !ruby/object:Gem::Specification
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
2
|
name: sys-admin
|
3
|
-
version: !ruby/object:Gem::Version
|
4
|
-
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.6.0
|
5
5
|
prerelease:
|
6
|
-
segments:
|
7
|
-
- 1
|
8
|
-
- 5
|
9
|
-
- 6
|
10
|
-
version: 1.5.6
|
11
6
|
platform: ruby
|
12
|
-
authors:
|
7
|
+
authors:
|
13
8
|
- Daniel J. Berger
|
14
9
|
autorequire:
|
15
10
|
bindir: bin
|
16
11
|
cert_chain: []
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
12
|
+
date: 2013-01-05 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: ffi
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: 1.1.0
|
22
|
+
type: :runtime
|
22
23
|
prerelease: false
|
23
|
-
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: 1.1.0
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: test-unit
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
24
33
|
none: false
|
25
|
-
requirements:
|
26
|
-
- -
|
27
|
-
- !ruby/object:Gem::Version
|
28
|
-
|
29
|
-
segments:
|
30
|
-
- 2
|
31
|
-
- 1
|
32
|
-
- 1
|
33
|
-
version: 2.1.1
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: 2.5.0
|
34
38
|
type: :development
|
35
|
-
|
36
|
-
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: 2.5.0
|
46
|
+
description: ! " The sys-admin library is a unified, cross platform replacement
|
47
|
+
for the\n 'etc' library that ships as part of the Ruby standard library. It\n
|
48
|
+
\ provides a common interface for all platforms, including MS Windows. In\n addition,
|
49
|
+
it provides an interface for adding, deleting and configuring\n users on MS Windows.\n"
|
37
50
|
email: djberg96@gmail.com
|
38
51
|
executables: []
|
39
|
-
|
40
|
-
|
41
|
-
- ext/extconf.rb
|
42
|
-
extra_rdoc_files:
|
52
|
+
extensions: []
|
53
|
+
extra_rdoc_files:
|
43
54
|
- CHANGES
|
44
55
|
- README
|
45
56
|
- MANIFEST
|
46
|
-
|
47
|
-
files:
|
57
|
+
files:
|
48
58
|
- CHANGES
|
49
59
|
- doc/sys-admin-unix.txt
|
50
60
|
- doc/sys-admin-windows.txt
|
51
|
-
- examples/
|
52
|
-
- examples/
|
53
|
-
-
|
54
|
-
-
|
55
|
-
-
|
61
|
+
- examples/example_groups.rb
|
62
|
+
- examples/example_users.rb
|
63
|
+
- lib/bsd/sys/admin.rb
|
64
|
+
- lib/darwin/sys/admin.rb
|
65
|
+
- lib/linux/sys/admin.rb
|
66
|
+
- lib/sunos/sys/admin.rb
|
67
|
+
- lib/sys/admin/common.rb
|
68
|
+
- lib/sys/admin/custom.rb
|
69
|
+
- lib/sys/admin.rb
|
70
|
+
- lib/unix/sys/admin.rb
|
71
|
+
- lib/windows/sys/admin.rb
|
56
72
|
- MANIFEST
|
57
73
|
- Rakefile
|
58
74
|
- README
|
@@ -61,37 +77,29 @@ files:
|
|
61
77
|
- test/test_sys_admin_unix.rb
|
62
78
|
- test/test_sys_admin_windows.rb
|
63
79
|
homepage: http://www.github.com/djberg96/sysutils
|
64
|
-
licenses:
|
80
|
+
licenses:
|
65
81
|
- Artistic 2.0
|
66
82
|
post_install_message:
|
67
83
|
rdoc_options: []
|
68
|
-
|
69
|
-
require_paths:
|
84
|
+
require_paths:
|
70
85
|
- lib
|
71
|
-
required_ruby_version: !ruby/object:Gem::Requirement
|
86
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
72
87
|
none: false
|
73
|
-
requirements:
|
74
|
-
- -
|
75
|
-
- !ruby/object:Gem::Version
|
76
|
-
|
77
|
-
|
78
|
-
- 0
|
79
|
-
version: "0"
|
80
|
-
required_rubygems_version: !ruby/object:Gem::Requirement
|
88
|
+
requirements:
|
89
|
+
- - ! '>='
|
90
|
+
- !ruby/object:Gem::Version
|
91
|
+
version: '0'
|
92
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
81
93
|
none: false
|
82
|
-
requirements:
|
83
|
-
- -
|
84
|
-
- !ruby/object:Gem::Version
|
85
|
-
|
86
|
-
segments:
|
87
|
-
- 0
|
88
|
-
version: "0"
|
94
|
+
requirements:
|
95
|
+
- - ! '>='
|
96
|
+
- !ruby/object:Gem::Version
|
97
|
+
version: '0'
|
89
98
|
requirements: []
|
90
|
-
|
91
99
|
rubyforge_project: sysutils
|
92
|
-
rubygems_version: 1.8.
|
100
|
+
rubygems_version: 1.8.24
|
93
101
|
signing_key:
|
94
102
|
specification_version: 3
|
95
103
|
summary: A unified, cross platform replacement for the "etc" library.
|
96
|
-
test_files:
|
104
|
+
test_files:
|
97
105
|
- test/test_sys_admin.rb
|
data/examples/groups.rb
DELETED
@@ -1,39 +0,0 @@
|
|
1
|
-
###########################################################################
|
2
|
-
# groups.rb
|
3
|
-
#
|
4
|
-
# Sample script to demonstrate some of the various group methods. Alter
|
5
|
-
# as you see fit.
|
6
|
-
###########################################################################
|
7
|
-
base = File.basename(Dir.pwd)
|
8
|
-
|
9
|
-
if base == "examples" || base =~ /sys-admin.*/
|
10
|
-
require "ftools"
|
11
|
-
Dir.chdir("..") if base == "examples"
|
12
|
-
Dir.mkdir("sys") unless File.exists?("sys")
|
13
|
-
if RUBY_PLATFORM.match("mswin")
|
14
|
-
File.copy("lib/sys/admin.rb", "sys/admin.rb")
|
15
|
-
else
|
16
|
-
File.copy("admin.so","sys") if File.exists?("admin.so")
|
17
|
-
end
|
18
|
-
$LOAD_PATH.unshift(Dir.pwd)
|
19
|
-
end
|
20
|
-
|
21
|
-
require "pp"
|
22
|
-
require "sys/admin"
|
23
|
-
include Sys
|
24
|
-
|
25
|
-
if PLATFORM.match("mswin")
|
26
|
-
pp Admin.get_group("guests")
|
27
|
-
pp Admin.get_group(513)
|
28
|
-
else
|
29
|
-
pp Admin.get_group("adm")
|
30
|
-
pp Admin.get_group(7)
|
31
|
-
end
|
32
|
-
|
33
|
-
Admin.groups{ |g|
|
34
|
-
pp g
|
35
|
-
puts
|
36
|
-
}
|
37
|
-
|
38
|
-
# This should raise an error
|
39
|
-
Admin.get_group("fofofofof")
|
data/examples/users.rb
DELETED
@@ -1,53 +0,0 @@
|
|
1
|
-
###########################################################################
|
2
|
-
# users.rb
|
3
|
-
#
|
4
|
-
# Sample script to demonstrate some of the various user methods. Alter
|
5
|
-
# as you see fit.
|
6
|
-
###########################################################################
|
7
|
-
base = File.basename(Dir.pwd)
|
8
|
-
|
9
|
-
if base == "examples" || base =~ /sys-admin.*/
|
10
|
-
require "ftools"
|
11
|
-
Dir.chdir("..") if base == "examples"
|
12
|
-
Dir.mkdir("sys") unless File.exists?("sys")
|
13
|
-
if RUBY_PLATFORM.match("mswin")
|
14
|
-
File.copy("lib/sys/admin.rb", "sys/admin.rb")
|
15
|
-
else
|
16
|
-
File.copy("admin.so","sys") if File.exists?("admin.so")
|
17
|
-
end
|
18
|
-
$LOAD_PATH.unshift(Dir.pwd)
|
19
|
-
end
|
20
|
-
|
21
|
-
require "pp"
|
22
|
-
require "sys/admin"
|
23
|
-
include Sys
|
24
|
-
|
25
|
-
user = User.new do |u|
|
26
|
-
u.name = "Foo"
|
27
|
-
u.description = "Test account"
|
28
|
-
u.password = "changeme"
|
29
|
-
#u.lockout = false
|
30
|
-
u.disabled = true
|
31
|
-
#u.password_required = true
|
32
|
-
end
|
33
|
-
|
34
|
-
Admin.delete_user(u.name) rescue nil
|
35
|
-
Admin.add_user(user)
|
36
|
-
|
37
|
-
#pp Admin.get_user("Foo")
|
38
|
-
|
39
|
-
#Admin.delete_user("Foo")
|
40
|
-
|
41
|
-
=begin
|
42
|
-
user = Admin.get_login
|
43
|
-
|
44
|
-
puts "User: #{user}"
|
45
|
-
|
46
|
-
Admin.users{ |u|
|
47
|
-
pp u
|
48
|
-
puts
|
49
|
-
}
|
50
|
-
|
51
|
-
pp Admin.get_user(user)
|
52
|
-
pp Admin.get_user(501)
|
53
|
-
=end
|
data/ext/extconf.rb
DELETED
@@ -1,72 +0,0 @@
|
|
1
|
-
require "mkmf"
|
2
|
-
require "rbconfig"
|
3
|
-
|
4
|
-
AIX = Config::CONFIG['host_os'] =~ /aix/i
|
5
|
-
|
6
|
-
# TODO: Just run the rake install task.
|
7
|
-
if File::ALT_SEPARATOR
|
8
|
-
STDERR.puts "Use the 'rake install' task to install on MS Windows."
|
9
|
-
STDERR.puts "Exiting. The sys-admin package was NOT installed."
|
10
|
-
exit
|
11
|
-
else
|
12
|
-
dir_config('admin')
|
13
|
-
|
14
|
-
# Some versions of AIX apparently have buggy implementations of certain
|
15
|
-
# re-entrant functions, so they're skipped for now.
|
16
|
-
|
17
|
-
have_func("getlogin_r")
|
18
|
-
have_func("getlogin")
|
19
|
-
have_func("getenv")
|
20
|
-
|
21
|
-
have_func("getpwuid_r")
|
22
|
-
have_func("getpwuid")
|
23
|
-
have_func("getpwnam_r")
|
24
|
-
have_func("getpwnam")
|
25
|
-
have_func("getpwent_r") unless AIX
|
26
|
-
have_func("getpwent")
|
27
|
-
|
28
|
-
have_func("getgrgid_r")
|
29
|
-
have_func("getgrgid")
|
30
|
-
have_func("getgrnam_r")
|
31
|
-
have_func("getgrnam")
|
32
|
-
have_func("getgrent_r") unless AIX
|
33
|
-
have_func("getgrent")
|
34
|
-
|
35
|
-
have_struct_member("struct passwd", "pw_gecos", "pwd.h")
|
36
|
-
have_struct_member("struct passwd", "pw_change", "pwd.h")
|
37
|
-
have_struct_member("struct passwd", "pw_quota", "pwd.h")
|
38
|
-
have_struct_member("struct passwd", "pw_age", "pwd.h")
|
39
|
-
have_struct_member("struct passwd", "pw_class", "pwd.h")
|
40
|
-
have_struct_member("struct passwd", "pw_comment", "pwd.h")
|
41
|
-
have_struct_member("struct passwd", "pw_expire", "pwd.h")
|
42
|
-
have_struct_member("struct passwd", "pw_passwd", "pwd.h")
|
43
|
-
|
44
|
-
have_struct_member("struct group", "gr_passwd", "grp.h")
|
45
|
-
|
46
|
-
if have_header("usersec.h") # AIX
|
47
|
-
have_func("getuserattr", "usersec.h")
|
48
|
-
else
|
49
|
-
utmp = have_header("utmp.h")
|
50
|
-
lastlog = have_header("lastlog.h")
|
51
|
-
|
52
|
-
if have_header("utmpx.h")
|
53
|
-
have_func("getlastlogx")
|
54
|
-
end
|
55
|
-
|
56
|
-
if utmp || lastlog
|
57
|
-
have_struct_member(
|
58
|
-
"struct lastlog",
|
59
|
-
"ll_time",
|
60
|
-
["utmp.h", "time.h", "lastlog.h"]
|
61
|
-
)
|
62
|
-
end
|
63
|
-
end
|
64
|
-
|
65
|
-
$CFLAGS += " -D_POSIX_PTHREAD_SEMANTICS"
|
66
|
-
|
67
|
-
if RUBY_PLATFORM =~ /linux|bsd/i
|
68
|
-
$CFLAGS += " -D_GNU_SOURCE -D_REENTRANT"
|
69
|
-
end
|
70
|
-
end
|
71
|
-
|
72
|
-
create_makefile('sys/admin', 'sys')
|
data/ext/sys/admin.c
DELETED
@@ -1,419 +0,0 @@
|
|
1
|
-
#include "ruby.h"
|
2
|
-
#include "admin.h"
|
3
|
-
|
4
|
-
#ifndef RSTRING_PTR
|
5
|
-
#define RSTRING_PTR(v) (RSTRING(v)->ptr)
|
6
|
-
#define RSTRING_LEN(v) (RSTRING(v)->len)
|
7
|
-
#endif
|
8
|
-
|
9
|
-
#ifndef RARRAY_PTR
|
10
|
-
#define RARRAY_PTR(v) (RARRAY(v)->ptr)
|
11
|
-
#define RARRAY_LEN(v) (RARRAY(v)->len)
|
12
|
-
#endif
|
13
|
-
|
14
|
-
/*
|
15
|
-
* call-seq:
|
16
|
-
* User.new
|
17
|
-
* User.new{ |user| ... }
|
18
|
-
*
|
19
|
-
* Creates and returns a User object, which encapsulates the information
|
20
|
-
* typically found within an /etc/passwd entry, i.e. a struct passwd.
|
21
|
-
*
|
22
|
-
* If a block is provided, yields the object back to the block.
|
23
|
-
*/
|
24
|
-
static VALUE user_init(VALUE self){
|
25
|
-
if(rb_block_given_p())
|
26
|
-
rb_yield(self);
|
27
|
-
|
28
|
-
return self;
|
29
|
-
}
|
30
|
-
|
31
|
-
/*
|
32
|
-
* call-seq:
|
33
|
-
* Group.new
|
34
|
-
* Group.new{ |user| ... }
|
35
|
-
*
|
36
|
-
* Creates and returns a Group object, which encapsulates the information
|
37
|
-
* typically found within an /etc/group entry, i.e. a struct group.
|
38
|
-
*
|
39
|
-
* If a block is provided, yields the object back to the block.
|
40
|
-
*/
|
41
|
-
static VALUE group_init(VALUE self){
|
42
|
-
if(rb_block_given_p())
|
43
|
-
rb_yield(self);
|
44
|
-
|
45
|
-
return self;
|
46
|
-
}
|
47
|
-
|
48
|
-
/*
|
49
|
-
* call-seq:
|
50
|
-
* Sys::Admin.get_login
|
51
|
-
*
|
52
|
-
* Returns the login for the process. If this is called from a process that
|
53
|
-
* has no controlling terminal, then it resorts to returning the "LOGNAME" or
|
54
|
-
* "USER" environment variable. If neither of those is defined, then nil
|
55
|
-
* is returned.
|
56
|
-
*
|
57
|
-
* Note that this method will _probably_ return the real user login, but may
|
58
|
-
* return the effective user login. YMMV depending on your platform and how
|
59
|
-
* the program is run.
|
60
|
-
*/
|
61
|
-
static VALUE admin_get_login(VALUE klass){
|
62
|
-
#ifdef HAVE_GETLOGIN_R
|
63
|
-
char login[_POSIX_LOGIN_NAME_MAX];
|
64
|
-
|
65
|
-
if(!getlogin_r(login, _POSIX_LOGIN_NAME_MAX))
|
66
|
-
return rb_str_new2(login);
|
67
|
-
#elif HAVE_GETLOGIN
|
68
|
-
char* login = getlogin();
|
69
|
-
|
70
|
-
if(login)
|
71
|
-
return rb_str_new2(login);
|
72
|
-
#endif
|
73
|
-
|
74
|
-
#ifdef HAVE_GETPWUID_R
|
75
|
-
uid_t uid;
|
76
|
-
char buf[USER_BUF_SIZE];
|
77
|
-
struct passwd pwd;
|
78
|
-
struct passwd* pwdbuf;
|
79
|
-
|
80
|
-
uid = getuid();
|
81
|
-
|
82
|
-
if(getpwuid_r(uid, &pwd, buf, USER_BUF_SIZE, &pwdbuf) != 0)
|
83
|
-
return rb_str_new2(pwdbuf->pw_name);
|
84
|
-
#elif HAVE_GETPWUID
|
85
|
-
uid_t uid;
|
86
|
-
struct passwd* pwd;
|
87
|
-
|
88
|
-
uid = getuid();
|
89
|
-
|
90
|
-
if((pwd = getpwuid(uid)))
|
91
|
-
return rb_str_new2(pwd->pw_name);
|
92
|
-
#endif
|
93
|
-
|
94
|
-
#ifdef HAVE_GETENV
|
95
|
-
char* user = getenv("LOGNAME");
|
96
|
-
|
97
|
-
if(user){
|
98
|
-
return rb_str_new2(user);
|
99
|
-
}
|
100
|
-
else{
|
101
|
-
user = getenv("USER");
|
102
|
-
if(user)
|
103
|
-
return rb_str_new2(user);
|
104
|
-
}
|
105
|
-
#endif
|
106
|
-
|
107
|
-
return Qnil;
|
108
|
-
}
|
109
|
-
|
110
|
-
/* call-seq:
|
111
|
-
* Admin.get_user(name)
|
112
|
-
* Admin.get_user(uid)
|
113
|
-
*
|
114
|
-
* Returns a User object for the given +name+ or +uid+. Raises an Admin::Error
|
115
|
-
* if a user cannot be found for that name or user ID.
|
116
|
-
*/
|
117
|
-
static VALUE admin_get_user(VALUE klass, VALUE v_value){
|
118
|
-
VALUE v_user;
|
119
|
-
|
120
|
-
if(FIXNUM_P(v_value))
|
121
|
-
v_user = get_user_by_num(v_value);
|
122
|
-
else
|
123
|
-
v_user = get_user_by_name(v_value);
|
124
|
-
|
125
|
-
return v_user;
|
126
|
-
}
|
127
|
-
|
128
|
-
/* call-seq:
|
129
|
-
* Admin.get_group(name)
|
130
|
-
* Admin.get_group(gid)
|
131
|
-
*
|
132
|
-
* Returns a Group object for the given +name+ or +gid+. Raises an Admin::Error
|
133
|
-
* if a group cannot be found for that name or GID.
|
134
|
-
*
|
135
|
-
*--
|
136
|
-
* Developer's Note:
|
137
|
-
*
|
138
|
-
* I generally oppose method overloading like this, but for this method, and
|
139
|
-
* for only two types, I can live with it for the added convenience it
|
140
|
-
* provides.
|
141
|
-
*/
|
142
|
-
static VALUE admin_get_group(VALUE klass, VALUE v_value){
|
143
|
-
VALUE v_group;
|
144
|
-
|
145
|
-
if(FIXNUM_P(v_value))
|
146
|
-
v_group = get_group_by_num(v_value);
|
147
|
-
else
|
148
|
-
v_group = get_group_by_name(v_value);
|
149
|
-
|
150
|
-
return v_group;
|
151
|
-
}
|
152
|
-
|
153
|
-
/*
|
154
|
-
* :no-doc:
|
155
|
-
*
|
156
|
-
* This is the main body of the Admin.groups method. It is wrapped separately
|
157
|
-
* for the sake of an rb_ensure() call.
|
158
|
-
*/
|
159
|
-
static VALUE admin_groups_body(VALUE klass){
|
160
|
-
VALUE v_array = Qnil;
|
161
|
-
|
162
|
-
if(!rb_block_given_p())
|
163
|
-
v_array = rb_ary_new();
|
164
|
-
|
165
|
-
setgrent();
|
166
|
-
|
167
|
-
#ifdef HAVE_GETGRENT_R
|
168
|
-
struct group grp;
|
169
|
-
char buf[GROUP_BUF_SIZE];
|
170
|
-
#if defined _GNU_SOURCE && !defined __sunos && !defined __SUNPRO_C && !defined __SUNPRO_CC
|
171
|
-
struct group* grp_p;
|
172
|
-
|
173
|
-
while(!getgrent_r(&grp, buf, GROUP_BUF_SIZE, &grp_p)){
|
174
|
-
if(grp_p == NULL)
|
175
|
-
break;
|
176
|
-
|
177
|
-
if(rb_block_given_p())
|
178
|
-
rb_yield(get_group(grp_p));
|
179
|
-
else
|
180
|
-
rb_ary_push(v_array, get_group(grp_p));
|
181
|
-
}
|
182
|
-
#else
|
183
|
-
while(getgrent_r(&grp, buf, GROUP_BUF_SIZE) != NULL){
|
184
|
-
if(rb_block_given_p())
|
185
|
-
rb_yield(get_group(&grp));
|
186
|
-
else
|
187
|
-
rb_ary_push(v_array, get_group(&grp));
|
188
|
-
}
|
189
|
-
#endif
|
190
|
-
#elif HAVE_GETGRENT
|
191
|
-
struct group* grp;
|
192
|
-
while((grp = getgrent()) != NULL){
|
193
|
-
if(rb_block_given_p())
|
194
|
-
rb_yield(get_group(grp));
|
195
|
-
else
|
196
|
-
rb_ary_push(v_array, get_group(grp));
|
197
|
-
}
|
198
|
-
#else
|
199
|
-
rb_raise(rb_eNotImpError, "groups method not supported on this platform");
|
200
|
-
#endif
|
201
|
-
|
202
|
-
return v_array; /* Nil or an array */
|
203
|
-
}
|
204
|
-
|
205
|
-
/* call-seq:
|
206
|
-
* Admin.groups
|
207
|
-
* Admin.groups{ |group| ... }
|
208
|
-
*
|
209
|
-
* In block form, yields a Group object for each group on the system. In
|
210
|
-
* non-block form, returns an Array of Group objects.
|
211
|
-
*/
|
212
|
-
static VALUE admin_groups(VALUE klass){
|
213
|
-
return rb_ensure(admin_groups_body, rb_ary_new3(1, klass),
|
214
|
-
admin_groups_cleanup, Qnil
|
215
|
-
);
|
216
|
-
}
|
217
|
-
|
218
|
-
/*
|
219
|
-
* :no-doc:
|
220
|
-
*
|
221
|
-
* This is the main body of the Admin.users method. It is wrapped separately
|
222
|
-
* for the sake of an rb_ensure() call.
|
223
|
-
*/
|
224
|
-
static VALUE admin_users_body(VALUE klass){
|
225
|
-
VALUE v_array = Qnil;
|
226
|
-
|
227
|
-
if(!rb_block_given_p())
|
228
|
-
v_array = rb_ary_new();
|
229
|
-
|
230
|
-
setpwent();
|
231
|
-
|
232
|
-
#ifdef HAVE_GETPWENT_R
|
233
|
-
struct passwd pwd;
|
234
|
-
char buf[USER_BUF_SIZE];
|
235
|
-
|
236
|
-
#if defined _GNU_SOURCE && !defined __sunos && !defined __SUNPRO_C && !defined __SUNPRO_CC
|
237
|
-
struct passwd* pwd_p;
|
238
|
-
|
239
|
-
while(!getpwent_r(&pwd, buf, USER_BUF_SIZE, &pwd_p)){
|
240
|
-
if(pwd_p == NULL)
|
241
|
-
break;
|
242
|
-
|
243
|
-
if(rb_block_given_p())
|
244
|
-
rb_yield(get_user(pwd_p));
|
245
|
-
else
|
246
|
-
rb_ary_push(v_array, get_user(pwd_p));
|
247
|
-
}
|
248
|
-
#else
|
249
|
-
while(getpwent_r(&pwd, buf, USER_BUF_SIZE) != NULL){
|
250
|
-
if(rb_block_given_p())
|
251
|
-
rb_yield(get_user(&pwd));
|
252
|
-
else
|
253
|
-
rb_ary_push(v_array, get_user(&pwd));
|
254
|
-
}
|
255
|
-
#endif
|
256
|
-
#elif HAVE_GETPWENT
|
257
|
-
struct passwd* pwd;
|
258
|
-
|
259
|
-
while((pwd = getpwent()) != NULL){
|
260
|
-
if(rb_block_given_p())
|
261
|
-
rb_yield(get_user(pwd));
|
262
|
-
else
|
263
|
-
rb_ary_push(v_array, get_user(pwd));
|
264
|
-
}
|
265
|
-
#else
|
266
|
-
rb_raise(rb_eNotImpError, "users method not supported on this platform");
|
267
|
-
#endif
|
268
|
-
|
269
|
-
return v_array; /* Nil or an array */
|
270
|
-
}
|
271
|
-
|
272
|
-
|
273
|
-
/* call-seq:
|
274
|
-
* Admin.users
|
275
|
-
* Admin.users{ |user| ... }
|
276
|
-
*
|
277
|
-
* In block form, yields a User object for each user on the system. In
|
278
|
-
* non-block form, returns an Array of User objects.
|
279
|
-
*/
|
280
|
-
static VALUE admin_users(VALUE klass){
|
281
|
-
return rb_ensure(admin_users_body, rb_ary_new3(1, klass),
|
282
|
-
admin_users_cleanup, Qnil
|
283
|
-
);
|
284
|
-
}
|
285
|
-
|
286
|
-
/* call-seq:
|
287
|
-
* User#groups # => ['staff', 'admin', ...]
|
288
|
-
*
|
289
|
-
* Returns an array of groups the user belongs to.
|
290
|
-
*/
|
291
|
-
static VALUE user_groups(VALUE self){
|
292
|
-
VALUE v_groups, v_group, v_users, v_group_name, v_name, v_result;
|
293
|
-
int i;
|
294
|
-
|
295
|
-
v_name = rb_funcall(self, rb_intern("name"), 0, 0);
|
296
|
-
v_result = rb_ary_new();
|
297
|
-
v_groups = admin_groups(self);
|
298
|
-
|
299
|
-
/* Iterate over each group, checking its members. If the members includes
|
300
|
-
* the user name, we have a match.
|
301
|
-
*/
|
302
|
-
if(!NIL_P(v_groups)){
|
303
|
-
for(i = 0; i < RARRAY_LEN(v_groups); i++){
|
304
|
-
v_group = RARRAY_PTR(v_groups)[i];
|
305
|
-
v_users = rb_funcall(v_group, rb_intern("members"), 0, 0);
|
306
|
-
|
307
|
-
if(RTEST(rb_funcall(v_users, rb_intern("include?"), 1, v_name))){
|
308
|
-
v_group_name = rb_funcall(v_group, rb_intern("name"), 0, 0);
|
309
|
-
rb_ary_push(v_result, v_group_name);
|
310
|
-
}
|
311
|
-
}
|
312
|
-
}
|
313
|
-
|
314
|
-
return v_result;
|
315
|
-
}
|
316
|
-
|
317
|
-
/*
|
318
|
-
* The Sys::Admin class encapsulates typical operations surrounding the query
|
319
|
-
* of user and group information.
|
320
|
-
*/
|
321
|
-
void Init_admin(){
|
322
|
-
VALUE mSys, cAdmin;
|
323
|
-
|
324
|
-
/* The Sys module is used primarily as a namespace for Sys::Admin */
|
325
|
-
mSys = rb_define_module("Sys");
|
326
|
-
|
327
|
-
/* A unified, cross platform replacement for the Etc module. */
|
328
|
-
cAdmin = rb_define_class_under(mSys, "Admin", rb_cObject);
|
329
|
-
|
330
|
-
/* Encapsulates information typically found in /etc/passwd */
|
331
|
-
cUser = rb_define_class_under(mSys, "User", rb_cObject);
|
332
|
-
|
333
|
-
/* Encapsulates information typically found in /etc/group */
|
334
|
-
cGroup = rb_define_class_under(mSys, "Group", rb_cObject);
|
335
|
-
|
336
|
-
/* Error raised if any of the Sys::Admin methods fail */
|
337
|
-
cAdminError = rb_define_class_under(cAdmin, "Error", rb_eStandardError);
|
338
|
-
|
339
|
-
/* Class Methods */
|
340
|
-
rb_define_singleton_method(cAdmin, "get_login", admin_get_login, 0);
|
341
|
-
rb_define_singleton_method(cAdmin, "get_user", admin_get_user, 1);
|
342
|
-
rb_define_singleton_method(cAdmin, "get_group", admin_get_group, 1);
|
343
|
-
rb_define_singleton_method(cAdmin, "users", admin_users, 0);
|
344
|
-
rb_define_singleton_method(cAdmin, "groups", admin_groups, 0);
|
345
|
-
|
346
|
-
/* Instance Methods */
|
347
|
-
rb_define_method(cUser, "initialize", user_init, 0);
|
348
|
-
rb_define_method(cUser, "groups", user_groups, 0);
|
349
|
-
rb_define_method(cGroup,"initialize", group_init, 0);
|
350
|
-
|
351
|
-
/* User Attributes */
|
352
|
-
|
353
|
-
/* The user name associated with the account */
|
354
|
-
rb_define_attr(cUser, "name", 1, 1);
|
355
|
-
|
356
|
-
/* The user's encrypted password. Deprecated in favor of /etc/shadow */
|
357
|
-
rb_define_attr(cUser, "passwd", 1, 1);
|
358
|
-
|
359
|
-
/* The user's user ID */
|
360
|
-
rb_define_attr(cUser, "uid", 1, 1);
|
361
|
-
|
362
|
-
/* The user's primary group ID */
|
363
|
-
rb_define_attr(cUser, "gid", 1, 1);
|
364
|
-
|
365
|
-
/* The absolute pathname of the user's home directory */
|
366
|
-
rb_define_attr(cUser, "dir", 1, 1);
|
367
|
-
|
368
|
-
/* The user's login shell */
|
369
|
-
rb_define_attr(cUser, "shell", 1, 1);
|
370
|
-
|
371
|
-
/* A comment field. Rarely used. */
|
372
|
-
rb_define_attr(cUser, "gecos", 1, 1);
|
373
|
-
|
374
|
-
/* The user's alloted amount of disk space */
|
375
|
-
rb_define_attr(cUser, "quota", 1, 1);
|
376
|
-
|
377
|
-
/* Used in the past for password aging. Deprecated in favor of /etc/shadow */
|
378
|
-
rb_define_attr(cUser, "age", 1, 1);
|
379
|
-
|
380
|
-
/* The user's access class */
|
381
|
-
rb_define_attr(cUser, "access_class", 1, 1);
|
382
|
-
|
383
|
-
/* Another comment field. Rarely used. */
|
384
|
-
rb_define_attr(cUser, "comment", 1, 1);
|
385
|
-
|
386
|
-
/* Account expiration date */
|
387
|
-
rb_define_attr(cUser, "expire", 1, 1);
|
388
|
-
|
389
|
-
/* Next date a password change will be needed */
|
390
|
-
rb_define_attr(cUser, "change", 1, 1);
|
391
|
-
|
392
|
-
/* The last time the user logged in */
|
393
|
-
rb_define_attr(cUser, "login_time", 1, 0);
|
394
|
-
|
395
|
-
/* The name of the terminal device the user last logged on with */
|
396
|
-
rb_define_attr(cUser, "login_device", 1, 0);
|
397
|
-
|
398
|
-
/* The hostname from which the user last logged in */
|
399
|
-
rb_define_attr(cUser, "login_host", 1, 0);
|
400
|
-
|
401
|
-
/* Group Attributes */
|
402
|
-
|
403
|
-
/* The name of the group */
|
404
|
-
rb_define_attr(cGroup, "name", 1, 1);
|
405
|
-
|
406
|
-
/* The group's group ID */
|
407
|
-
rb_define_attr(cGroup, "gid", 1, 1);
|
408
|
-
|
409
|
-
/* An array of users that are members of the group */
|
410
|
-
rb_define_attr(cGroup, "members", 1, 1);
|
411
|
-
|
412
|
-
/* The group password, if any. */
|
413
|
-
rb_define_attr(cGroup, "passwd", 1, 1);
|
414
|
-
|
415
|
-
/* Constants */
|
416
|
-
|
417
|
-
/* 1.5.5: The version of this library */
|
418
|
-
rb_define_const(cAdmin, "VERSION", rb_str_new2(SYS_ADMIN_VERSION));
|
419
|
-
}
|