forked from apalmblad/ruby-shadow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshadow.c
291 lines (243 loc) · 6.49 KB
/
shadow.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
/*
* shadow.c
*
* Ruby extention module for using Linux shadow password.
*
* Copyright (C) 1998-1999 by Takaaki.Tateishi(ttate@jaist.ac.jp)
* License: Free for any use with your own risk!
* Modified at: <1999/8/19 06:48:18 by ttate>
*/
#include <shadow.h>
#include "ruby.h"
#ifdef RUBY19
#include <ruby/io.h>
#else
#include "rubyio.h"
#endif
#ifdef RUBY19
#define file_ptr(x) (x)->stdio_file
#else
#define file_ptr(x) (x)->f
#endif
static VALUE rb_mShadow;
static VALUE rb_mPasswd;
static VALUE rb_sPasswdEntry;
static VALUE rb_mGroup;
static VALUE rb_sGroupEntry;
static VALUE rb_eFileLock;
static VALUE
rb_shadow_setspent(VALUE self)
{
setspent();
return Qnil;
};
static VALUE
rb_shadow_endspent(VALUE self)
{
endspent();
return Qnil;
};
static VALUE
rb_shadow_sgetspent(VALUE self, VALUE str)
{
struct spwd *entry;
VALUE result;
if( TYPE(str) != T_STRING )
rb_raise(rb_eException,"argument must be a string.");
entry = sgetspent(StringValuePtr(str));
if( entry == NULL )
return Qnil;
result = rb_struct_new(rb_sPasswdEntry,
rb_tainted_str_new2(entry->sp_namp),
rb_tainted_str_new2(entry->sp_pwdp),
INT2FIX(entry->sp_lstchg),
INT2FIX(entry->sp_min),
INT2FIX(entry->sp_max),
INT2FIX(entry->sp_warn),
INT2FIX(entry->sp_inact),
INT2FIX(entry->sp_expire),
INT2FIX(entry->sp_flag),
NULL);
free(entry);
return result;
};
static VALUE
rb_shadow_fgetspent(VALUE self, VALUE file)
{
struct spwd *entry;
VALUE result;
if( TYPE(file) != T_FILE )
rb_raise(rb_eTypeError,"argument must be a File.");
entry = fgetspent( file_ptr( (RFILE(file)->fptr) ) );
if( entry == NULL )
return Qnil;
result = rb_struct_new(rb_sPasswdEntry,
rb_tainted_str_new2(entry->sp_namp),
rb_tainted_str_new2(entry->sp_pwdp),
INT2FIX(entry->sp_lstchg),
INT2FIX(entry->sp_min),
INT2FIX(entry->sp_max),
INT2FIX(entry->sp_warn),
INT2FIX(entry->sp_inact),
INT2FIX(entry->sp_expire),
INT2FIX(entry->sp_flag),
NULL);
return result;
};
static VALUE
rb_shadow_getspent(VALUE self)
{
struct spwd *entry;
VALUE result;
entry = getspent();
if( entry == NULL )
return Qnil;
result = rb_struct_new(rb_sPasswdEntry,
rb_tainted_str_new2(entry->sp_namp),
rb_tainted_str_new2(entry->sp_pwdp),
INT2FIX(entry->sp_lstchg),
INT2FIX(entry->sp_min),
INT2FIX(entry->sp_max),
INT2FIX(entry->sp_warn),
INT2FIX(entry->sp_inact),
INT2FIX(entry->sp_expire),
INT2FIX(entry->sp_flag),
NULL);
return result;
};
static VALUE
rb_shadow_getspnam(VALUE self, VALUE name)
{
struct spwd *entry;
VALUE result;
if( TYPE(name) != T_STRING )
rb_raise(rb_eException,"argument must be a string.");
entry = getspnam(StringValuePtr(name));
if( entry == NULL )
return Qnil;
result = rb_struct_new(rb_sPasswdEntry,
rb_tainted_str_new2(entry->sp_namp),
rb_tainted_str_new2(entry->sp_pwdp),
INT2FIX(entry->sp_lstchg),
INT2FIX(entry->sp_min),
INT2FIX(entry->sp_max),
INT2FIX(entry->sp_warn),
INT2FIX(entry->sp_inact),
INT2FIX(entry->sp_expire),
INT2FIX(entry->sp_flag),
NULL);
return result;
};
static VALUE
rb_shadow_putspent(VALUE self, VALUE entry, VALUE file)
{
struct spwd centry;
FILE* cfile;
VALUE val[9];
int i;
int result;
for(i=0; i<=8; i++)
val[i] = RSTRUCT_PTR( entry )[i]; //val[i] = RSTRUCT(entry)->ptr[i];
cfile = file_pr( RFILE(file)->fptr );
centry.sp_namp = StringValuePtr(val[0]);
centry.sp_pwdp = StringValuePtr(val[1]);
centry.sp_lstchg = FIX2INT(val[2]);
centry.sp_min = FIX2INT(val[3]);
centry.sp_max = FIX2INT(val[4]);
centry.sp_warn = FIX2INT(val[5]);
centry.sp_inact = FIX2INT(val[6]);
centry.sp_expire = FIX2INT(val[7]);
centry.sp_flag = FIX2INT(val[8]);
result = putspent(¢ry,cfile);
if( result == -1 )
rb_raise(rb_eStandardError,"can't change password");
return Qtrue;
};
static VALUE
rb_shadow_lckpwdf(VALUE self)
{
int result;
result = lckpwdf();
if( result == -1 )
rb_raise(rb_eFileLock,"password file was locked");
else
return Qtrue;
};
static int in_lock;
static VALUE
rb_shadow_lock(VALUE self)
{
int result;
if( rb_iterator_p() ){
result = lckpwdf();
if( result == -1 ){
rb_raise(rb_eFileLock,"password file was locked");
}
else{
in_lock++;
rb_yield(Qnil);
in_lock--;
ulckpwdf();
};
return Qtrue;
}
else{
return rb_shadow_lckpwdf(self);
};
};
static VALUE
rb_shadow_ulckpwdf(VALUE self)
{
if( in_lock ){
rb_raise(rb_eFileLock,"you call unlock method in lock iterator.");
};
ulckpwdf();
return Qtrue;
};
static VALUE
rb_shadow_unlock(VALUE self)
{
return rb_shadow_ulckpwdf(self);
};
static VALUE
rb_shadow_lock_p(VALUE self)
{
int result;
result = lckpwdf();
if( result == -1 ){
return Qtrue;
}
else{
ulckpwdf();
return Qfalse;
};
};
void
Init_shadow()
{
rb_sPasswdEntry = rb_struct_define("PasswdEntry",
"sp_namp","sp_pwdp","sp_lstchg",
"sp_min","sp_max","sp_warn",
"sp_inact","sp_expire","sp_flag", NULL);
rb_sGroupEntry = rb_struct_define("GroupEntry",
"sg_name","sg_passwd",
"sg_adm","sg_mem",NULL);
rb_mShadow = rb_define_module("Shadow");
rb_eFileLock = rb_define_class_under(rb_mShadow,"FileLock",rb_eException);
rb_mPasswd = rb_define_module_under(rb_mShadow,"Passwd");
rb_define_const(rb_mPasswd,"Entry",rb_sPasswdEntry);
rb_mGroup = rb_define_module_under(rb_mShadow,"Group");
rb_define_const(rb_mGroup,"Entry",rb_sGroupEntry);
rb_define_module_function(rb_mPasswd,"setspent",rb_shadow_setspent,0);
rb_define_module_function(rb_mPasswd,"endspent",rb_shadow_endspent,0);
rb_define_module_function(rb_mPasswd,"sgetspent",rb_shadow_sgetspent,1);
rb_define_module_function(rb_mPasswd,"fgetspent",rb_shadow_fgetspent,1);
rb_define_module_function(rb_mPasswd,"getspent",rb_shadow_getspent,0);
rb_define_module_function(rb_mPasswd,"getspnam",rb_shadow_getspnam,1);
rb_define_module_function(rb_mPasswd,"putspent",rb_shadow_putspent,2);
rb_define_module_function(rb_mPasswd,"lckpwdf",rb_shadow_lckpwdf,0);
rb_define_module_function(rb_mPasswd,"lock",rb_shadow_lock,0);
rb_define_module_function(rb_mPasswd,"ulckpwdf",rb_shadow_ulckpwdf,0);
rb_define_module_function(rb_mPasswd,"unlock",rb_shadow_unlock,0);
rb_define_module_function(rb_mPasswd,"lock?",rb_shadow_lock_p,0);
};