sys-admin 1.3.1 → 1.4.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -8,7 +8,7 @@ $LOAD_PATH.unshift Dir.pwd
8
8
  $LOAD_PATH.unshift Dir.pwd + "/test"
9
9
 
10
10
  if File::ALT_SEPARATOR
11
- require "tc_win32"
11
+ require "tc_windows"
12
12
  else
13
13
  require "tc_unix"
14
14
  end
@@ -6,15 +6,15 @@
6
6
  base = File.basename(Dir.pwd)
7
7
 
8
8
  if base == "test" || base =~ /sys-admin.*/
9
- require "ftools"
9
+ require "fileutils"
10
10
  Dir.chdir("..") if base == "test"
11
11
  Dir.mkdir("sys") unless File.exists?("sys")
12
- File.copy("admin.so","sys") if File.exists?("admin.so")
12
+ FileUtils.cp("ext/admin.so", "sys") if File.exists?("ext/admin.so")
13
13
  $LOAD_PATH.unshift(Dir.pwd)
14
14
  end
15
15
 
16
- require "test/unit"
17
- require "sys/admin"
16
+ require 'test/unit'
17
+ require 'sys/admin'
18
18
  include Sys
19
19
 
20
20
  class TC_Sys_Admin_Unix < Test::Unit::TestCase
@@ -26,7 +26,7 @@ class TC_Sys_Admin_Unix < Test::Unit::TestCase
26
26
  end
27
27
 
28
28
  def test_version
29
- assert_equal("1.3.1", Admin::VERSION)
29
+ assert_equal("1.4.0", Admin::VERSION)
30
30
  end
31
31
 
32
32
  def test_get_login
@@ -1,7 +1,9 @@
1
1
  ###############################################################################
2
- # tc_win32.rb
2
+ # tc_windows.rb
3
3
  #
4
- # Test suite for the Win32 version of sys-admin.
4
+ # Test suite for the Win32 version of sys-admin. Note that some of the tests
5
+ # are numbered to ensure a certain order. That way I can add test users
6
+ # before configuring or deleting them.
5
7
  ###############################################################################
6
8
  base = File.basename(Dir.pwd)
7
9
 
@@ -9,8 +11,9 @@ if base == "test" || base =~ /sys-admin.*/
9
11
  require "ftools"
10
12
  Dir.chdir("..") if base == "test"
11
13
  Dir.mkdir("sys") unless File.exists?("sys")
12
- File.copy("lib/sys/win32.rb","sys/admin.rb")
14
+ File.copy("lib/sys/admin.rb", "sys/admin.rb")
13
15
  $LOAD_PATH.unshift(Dir.pwd)
16
+ Dir.chdir("test") rescue nil
14
17
  end
15
18
 
16
19
  require "test/unit"
@@ -30,7 +33,68 @@ class TC_Sys_Admin_Win32 < Test::Unit::TestCase
30
33
  end
31
34
 
32
35
  def test_version
33
- assert_equal("1.3.1", Admin::VERSION)
36
+ assert_equal("1.4.0", Admin::VERSION)
37
+ end
38
+
39
+ def test_01_add_local_user
40
+ assert_respond_to(Admin, :add_local_user)
41
+ assert_nothing_raised{ Admin.add_local_user("foo") }
42
+ end
43
+
44
+ def test_add_global_user
45
+ assert_respond_to(Admin, :add_global_user)
46
+ end
47
+
48
+ def test_02_config_local_user
49
+ assert_respond_to(Admin, :config_local_user)
50
+ assert_nothing_raised{
51
+ Admin.config_local_user("foo",{
52
+ :description => "delete me",
53
+ :fullname => "fubar"
54
+ })
55
+ }
56
+ end
57
+
58
+ def test_config_global_user
59
+ assert_respond_to(Admin, :config_global_user)
60
+ end
61
+
62
+ def test_03_delete_local_user
63
+ assert_respond_to(Admin, :delete_local_user)
64
+ assert_nothing_raised{ Admin.delete_local_user("foo") }
65
+ end
66
+
67
+ def test_delete_global_user
68
+ assert_respond_to(Admin, :delete_global_user)
69
+ end
70
+
71
+ def test_01_add_local_group
72
+ assert_respond_to(Admin, :add_local_group)
73
+ assert_nothing_raised{ Admin.add_local_group("bar") }
74
+ end
75
+
76
+ def test_add_global_group
77
+ assert_respond_to(Admin, :add_global_group)
78
+ end
79
+
80
+ def test_02_config_local_group
81
+ assert_respond_to(Admin, :config_local_group)
82
+ assert_nothing_raised{
83
+ Admin.config_local_group("bar",{:description=>"delete me"})
84
+ }
85
+ end
86
+
87
+ def test_config_global_group
88
+ assert_respond_to(Admin, :config_global_group)
89
+ end
90
+
91
+ def test_03_delete_local_group
92
+ assert_respond_to(Admin, :delete_local_group)
93
+ assert_nothing_raised{ Admin.delete_local_group("bar") }
94
+ end
95
+
96
+ def test_delete_global_group
97
+ assert_respond_to(Admin, :delete_global_group)
34
98
  end
35
99
 
36
100
  def test_get_login
metadata CHANGED
@@ -3,8 +3,8 @@ rubygems_version: 0.9.0
3
3
  specification_version: 1
4
4
  name: sys-admin
5
5
  version: !ruby/object:Gem::Version
6
- version: 1.3.1
7
- date: 2006-08-01 00:00:00 -06:00
6
+ version: 1.4.0
7
+ date: 2007-01-20 00:00:00 -07:00
8
8
  summary: A unified, cross platform replacement for the "etc" package.
9
9
  require_paths:
10
10
  - lib
@@ -31,15 +31,15 @@ authors:
31
31
  files:
32
32
  - examples/groups.rb
33
33
  - examples/users.rb
34
- - lib/sys/unix.c
35
- - lib/sys/unix.h
36
- - lib/sys/win32.orig
37
34
  - test/tc_unix.rb
38
- - test/tc_win32.rb
35
+ - test/tc_windows.rb
39
36
  - test/tc_admin.rb
40
37
  - README
41
38
  - CHANGES
42
39
  - MANIFEST
40
+ - ext/extconf.rb
41
+ - ext/admin.c
42
+ - ext/admin.h
43
43
  test_files:
44
44
  - test/tc_admin.rb
45
45
  rdoc_options: []
@@ -47,10 +47,12 @@ rdoc_options: []
47
47
  extra_rdoc_files:
48
48
  - CHANGES
49
49
  - README
50
+ - MANIFEST
51
+ - ext/admin.c
50
52
  executables: []
51
53
 
52
54
  extensions:
53
- - extconf.rb
55
+ - ext/extconf.rb
54
56
  requirements: []
55
57
 
56
58
  dependencies: []
@@ -1,411 +0,0 @@
1
- #include <limits.h>
2
- #include <stdlib.h>
3
- #include <stdio.h>
4
- #include <unistd.h>
5
- #include <pwd.h>
6
- #include <grp.h>
7
- #include <fcntl.h>
8
- #include <errno.h>
9
- #include <string.h>
10
-
11
- #define SYS_ADMIN_VERSION "1.3.1"
12
-
13
- #ifdef HAVE_LASTLOG_H
14
- #include <lastlog.h>
15
- #else
16
- #include <utmp.h>
17
- #endif
18
-
19
- #ifndef _POSIX_LOGIN_NAME_MAX
20
- #define _POSIX_LOGIN_NAME_MAX 9
21
- #endif
22
-
23
- #ifdef _SC_GETPW_R_SIZE_MAX
24
- #define USER_BUF_SIZE (sysconf(_SC_GETPW_R_SIZE_MAX))
25
- #else
26
- #define USER_BUF_SIZE 1024
27
- #endif
28
-
29
- #ifdef _SC_GETGR_R_SIZE_MAX
30
- #define GROUP_BUF_SIZE (sysconf(_SC_GETGR_R_SIZE_MAX))
31
- #else
32
- #define GROUP_BUF_SIZE 7296
33
- #endif
34
-
35
- #ifndef _PATH_LASTLOG
36
- #define _PATH_LASTLOG "/var/adm/lastlog"
37
- #endif
38
-
39
- /* Function prototypes */
40
- static VALUE get_user(struct passwd* p);
41
- static VALUE get_group(struct group* g);
42
- int get_lastlog_info(const char* n, VALUE rbVal);
43
-
44
- VALUE cUser, cGroup, cAdminError;
45
-
46
- /*
47
- * :no-doc:
48
- *
49
- * Helper function that returns a User object based on user ID.
50
- */
51
- static VALUE get_user_by_num(VALUE rbUID){
52
- VALUE rbUser;
53
- uid_t uid = NUM2INT(rbUID);
54
-
55
- #ifdef HAVE_GETPWUID_R
56
- char buf[USER_BUF_SIZE];
57
- struct passwd pwd;
58
- struct passwd* pwdbuf;
59
-
60
- if(getpwuid_r(uid, &pwd, buf, sizeof(buf), &pwdbuf) != 0)
61
- rb_raise(cAdminError, "%s", strerror(errno));
62
-
63
- if(!pwdbuf)
64
- rb_raise(cAdminError, "no user found for %i:", uid);
65
-
66
- rbUser = get_user(pwdbuf);
67
- #elif HAVE_GETPWUID
68
- struct passwd* pwd;
69
- if( (pwd = getpwuid(uid)) == NULL)
70
- rb_raise(cAdminError, "no user found for: %i", uid);
71
-
72
- rbUser = get_user(pwd);
73
- #else
74
- rb_raise(rb_eNotImpError, "getting user by user ID not supported");
75
- #endif
76
-
77
- return rbUser;
78
- }
79
-
80
- /*
81
- * :no-doc:
82
- *
83
- * Helper function that returns a User object based on name.
84
- */
85
- static VALUE get_user_by_name(VALUE rbName){
86
- VALUE rbUser;
87
- SafeStringValue(rbName);
88
-
89
- #ifdef HAVE_GETPWNAM_R
90
- char buf[USER_BUF_SIZE];
91
- struct passwd pwd;
92
- struct passwd* pwdbuf;
93
-
94
- if(getpwnam_r(RSTRING(rbName)->ptr, &pwd, buf, sizeof(buf), &pwdbuf) != 0)
95
- rb_raise(cAdminError, "%s", strerror(errno));
96
-
97
- if(!pwdbuf)
98
- rb_raise(cAdminError, "no user found for %s", StringValuePtr(rbName));
99
-
100
- rbUser = get_user(pwdbuf);
101
- #elif HAVE_GETPWNAM
102
- struct passwd* pwd;
103
- if( (pwd = getpwnam(RSTRING(rbName)->ptr)) == NULL)
104
- rb_raise(cAdminError, "no user found for %s", StringValuePtr(rbName));
105
-
106
- rbUser = get_user(pwd);
107
- #else
108
- rb_raise(rb_eNotImpError, "getting user by name not supported");
109
- #endif
110
-
111
- return rbUser;
112
- }
113
-
114
- /*
115
- * :no-doc:
116
- *
117
- * Helper function that returns a Group object based on group ID.
118
- */
119
- static VALUE get_group_by_num(VALUE rbGID){
120
- VALUE rbGroup;
121
- gid_t gid = NUM2INT(rbGID);
122
-
123
- #ifdef HAVE_GETGRGID_R
124
- char buf[GROUP_BUF_SIZE];
125
- struct group grp;
126
- struct group* grpbuf;
127
-
128
- if(getgrgid_r(gid, &grp, buf, sizeof(buf), &grpbuf) != 0)
129
- rb_raise(cAdminError, "%s", strerror(errno));
130
-
131
- if(!grpbuf)
132
- rb_raise(cAdminError, "no group found for group ID: %i", gid);
133
-
134
- rbGroup = get_group(grpbuf);
135
- #elif HAVE_GETGRGID
136
- struct group* grp;
137
- if( (grp = getgrgid(gid)) == NULL)
138
- rb_raise(cAdminError, "no group found for group ID: %i", gid);
139
-
140
- rbGroup = get_group(grp);
141
- #else
142
- rb_raise(rb_eNotImpError, "getting group by group ID not supported");
143
- #endif
144
-
145
- return rbGroup;
146
- }
147
-
148
- /*
149
- * :no-doc:
150
- *
151
- * Helper function that returns a Group object based on group name.
152
- */
153
- static VALUE get_group_by_name(VALUE rbName){
154
- VALUE rbGroup = Qnil;
155
- SafeStringValue(rbName);
156
- #ifdef HAVE_GETGRNAM_R
157
- char buf[GROUP_BUF_SIZE];
158
- struct group grp;
159
- struct group* grpbuf;
160
-
161
- if(getgrnam_r(RSTRING(rbName)->ptr, &grp, buf, sizeof(buf), &grpbuf) != 0)
162
- rb_raise(cAdminError, "%s", strerror(errno));
163
-
164
- if(!grpbuf)
165
- rb_raise(cAdminError, "no group found for: %s", StringValuePtr(rbName));
166
-
167
- rbGroup = get_group(grpbuf);
168
- #elif HAVE_GETGRNAM
169
- struct group* grp
170
- if((grp = getgrnam(RSTRING(rbName)->ptr)) == NULL)
171
- rb_raise(cAdminError, "no group found for: %s", StringValuePtr(rbName));
172
-
173
- rbGroup = get_group(grp);
174
- #else
175
- rb_raise(rb_eNotImpError,"get_group is not supported on this platform");
176
- #endif
177
-
178
- return rbGroup;
179
- }
180
-
181
- /*
182
- * :no-doc:
183
- *
184
- * Helper function that turns a struct passwd into a User object.
185
- */
186
- static VALUE get_user(struct passwd* p){
187
- VALUE rbUser = rb_funcall(cUser,rb_intern("new"),0,0);
188
-
189
- rb_iv_set(rbUser, "@name", rb_str_new2(p->pw_name));
190
- rb_iv_set(rbUser, "@uid", INT2FIX(p->pw_uid));
191
- rb_iv_set(rbUser, "@gid", INT2FIX(p->pw_gid));
192
- rb_iv_set(rbUser, "@dir", rb_str_new2(p->pw_dir));
193
- rb_iv_set(rbUser, "@shell", rb_str_new2(p->pw_shell));
194
-
195
- #ifdef HAVE_ST_PW_PASSWD
196
- rb_iv_set(rbUser, "@passwd", rb_str_new2(p->pw_passwd));
197
- #endif
198
-
199
- #ifdef HAVE_ST_PW_AGE
200
- rb_iv_set(rbUser, "@age", INT2FIX(p->pw_age));
201
- #endif
202
-
203
- #ifdef HAVE_ST_PW_COMMENT
204
- rb_iv_set(rbUser, "@comment", rb_str_new2(p->pw_comment));
205
- #endif
206
-
207
- #ifdef HAVE_ST_PW_GECOS
208
- rb_iv_set(rbUser, "@gecos", rb_str_new2(p->pw_gecos));
209
- #endif
210
-
211
- #ifdef HAVE_ST_PW_QUOTA
212
- rb_iv_set(rbUser, "@quota", INT2FIX(p->pw_quota));
213
- #endif
214
-
215
- #ifdef HAVE_ST_PW_CLASS
216
- rb_iv_set(rbUser, "@class", rb_str_new2(p->pw_class));
217
- #endif
218
-
219
- #ifdef HAVE_ST_PW_EXPIRE
220
- rb_iv_set(rbUser, "@expire", INT2FIX(p->pw_expire));
221
- #endif
222
-
223
- /* Get the lastlog info for the given user */
224
- get_lastlog_info(p->pw_name, rbUser);
225
-
226
- return rbUser;
227
- }
228
-
229
- /*
230
- * :no-doc:
231
- *
232
- * Helper function that turns a User object into a struct passwd.
233
- */
234
- void get_user_from_value(VALUE rbUser, struct passwd* pwd){
235
-
236
- VALUE rbName = rb_iv_get(rbUser, "@name");
237
- VALUE rbUID = rb_iv_get(rbUser, "@uid");
238
- VALUE rbGID = rb_iv_get(rbUser, "@gid");
239
- VALUE rbDir = rb_iv_get(rbUser, "@dir");
240
- VALUE rbShell = rb_iv_get(rbUser, "@shell");
241
-
242
- if(NIL_P(rbName))
243
- rb_raise(cAdminError, "user name cannot be nil");
244
-
245
- if(!NIL_P(rbUID))
246
- pwd->pw_uid = NUM2INT(rbUID);
247
-
248
- if(!NIL_P(rbGID))
249
- pwd->pw_gid = NUM2INT(rbGID);
250
-
251
- if(!NIL_P(rbDir)){
252
- SafeStringValue(rbDir);
253
- pwd->pw_dir = StringValuePtr(rbDir);
254
- }
255
-
256
- if(!NIL_P(rbShell)){
257
- SafeStringValue(rbShell);
258
- pwd->pw_shell = StringValuePtr(rbShell);
259
- }
260
-
261
- #ifdef HAVE_ST_PW_PASSWD
262
- VALUE rbPasswd = rb_iv_get(rbUser, "@passwd");
263
- if(!NIL_P(rbPasswd)){
264
- SafeStringValue(rbPasswd);
265
- pwd->pw_passwd = StringValuePtr(rbPasswd);
266
- }
267
- #endif
268
-
269
- #ifdef HAVE_ST_PW_AGE
270
- VALUE rbAge = rb_iv_get(rbUser, "@age");
271
- if(!NIL_P(rbAge))
272
- pwd->pw_age = (char*)NUM2INT(rbAge);
273
- #endif
274
-
275
- #ifdef HAVE_ST_PW_COMMENT
276
- VALUE rbComment = rb_iv_get(rbUser, "@comment");
277
- if(!NIL_P(rbComment)){
278
- SafeStringValue(rbComment);
279
- pwd->pw_comment = StringValuePtr(rbComment);
280
- }
281
- #endif
282
-
283
- #ifdef HAVE_ST_PW_GECOS
284
- VALUE rbGecos = rb_iv_get(rbUser, "@gecos");
285
- if(!NIL_P(rbGecos)){
286
- SafeStringValue(rbGecos);
287
- pwd->pw_gecos = StringValuePtr(rbGecos);
288
- }
289
- #endif
290
-
291
- #ifdef HAVE_ST_PW_QUOTA
292
- VALUE rbQuota = rb_iv_get(rbUser, "@quota");
293
- if(!NIL_P(rbQuota))
294
- pwd->pw_quota = NUM2INT(rbQuota);
295
- #endif
296
-
297
- #ifdef HAVE_ST_PW_CLASS
298
- VALUE rbClass = rb_iv_get(rbUser, "@class");
299
- if(!NIL_P(rbClass)){
300
- SafeStringValue(rbClass);
301
- pwd->pw_class = StringValuePtr(rbClass);
302
- }
303
- #endif
304
-
305
- #ifdef HAVE_ST_PW_EXPIRE
306
- VALUE rbExpire = rb_iv_get(rbUser, "@expire");
307
- if(!NIL_P(rbExpire))
308
- pwd->pw_expire = NUM2INT(rbExpire);
309
- #endif
310
-
311
- }
312
-
313
- /*
314
- * :no-doc:
315
- *
316
- * Helper function that turns a struct grp into a Group object.
317
- */
318
- static VALUE get_group(struct group* g){
319
- VALUE rbGroup = rb_funcall(cGroup,rb_intern("new"),0,0);
320
- VALUE rbArray = rb_ary_new();
321
-
322
- /* Return the members as an Array of Strings */
323
- while(*g->gr_mem){
324
- rb_ary_push(rbArray, rb_str_new2(*g->gr_mem));
325
- g->gr_mem++;
326
- }
327
-
328
- rb_iv_set(rbGroup, "@name", rb_str_new2(g->gr_name));
329
- rb_iv_set(rbGroup, "@gid", INT2FIX(g->gr_gid));
330
- rb_iv_set(rbGroup, "@members", rbArray);
331
- #ifdef HAVE_ST_GR_PASSWD
332
- rb_iv_set(rbGroup, "@passwd", rb_str_new2(g->gr_passwd));
333
- #endif
334
-
335
- return rbGroup;
336
- }
337
-
338
- /*
339
- * :no-doc:
340
- *
341
- * Helper function that turns a Group object into a struct group.
342
- */
343
- void get_group_from_value(VALUE rbGroup, struct group* grp){
344
- char** members = malloc(sizeof(char*));
345
- VALUE rbName = rb_iv_get(rbGroup, "@name");
346
- VALUE rbGID = rb_iv_get(rbGroup, "@gid");
347
- VALUE rbMem = rb_iv_get(rbGroup, "@members");
348
- VALUE rbPasswd = rb_iv_get(rbGroup, "@passwd");
349
- int i = 0;
350
-
351
- if(NIL_P(rbName))
352
- rb_raise(cAdminError, "group name must be set");
353
-
354
- SafeStringValue(rbName);
355
- grp->gr_name = StringValuePtr(rbName);
356
-
357
- if(!NIL_P(rbGID))
358
- grp->gr_gid = NUM2INT(rbGID);
359
-
360
- if(!NIL_P(rbMem)){
361
- VALUE rbVal;
362
- while((rbVal = rb_ary_shift(rbMem)) != Qnil){
363
- members[i] = StringValuePtr(rbVal);
364
- i++;
365
- }
366
- members[i] = '\0';
367
- grp->gr_mem = members;
368
- }
369
-
370
- #ifdef HAVE_ST_GR_PASSWD
371
- if(!NIL_P(rbPasswd)){
372
- SafeStringValue(rbPasswd);
373
- grp->gr_passwd = StringValuePtr(rbPasswd);
374
- }
375
- #endif
376
-
377
- free(members);
378
- }
379
-
380
- /*
381
- * :no-doc:
382
- *
383
- * Helper function that gets lastlog information for the User object.
384
- */
385
- int get_lastlog_info(const char* name, VALUE rbUser){
386
- int fd;
387
- struct lastlog log;
388
- struct passwd* pwd;
389
- int ll_size = sizeof(struct lastlog);
390
-
391
- /* The lastlog information is not necessarily readable by all users, so
392
- * ignore open() errors if they occur.
393
- */
394
- if((fd = open(_PATH_LASTLOG, O_RDONLY)) == -1)
395
- return -1;
396
-
397
- if((pwd = getpwnam(name)) == NULL)
398
- rb_sys_fail(0);
399
-
400
- pread(fd, &log, ll_size, pwd->pw_uid * ll_size);
401
- close(fd);
402
-
403
- #ifdef HAVE_ST_LL_TIME
404
- if(log.ll_time != 0)
405
- rb_iv_set(rbUser, "@login_time", rb_time_new(log.ll_time, 0));
406
- #endif
407
- rb_iv_set(rbUser, "@login_device", rb_str_new2(log.ll_line));
408
- rb_iv_set(rbUser, "@login_host", rb_str_new2(log.ll_host));
409
-
410
- return 0;
411
- }