0
|
1 |
/*
|
|
2 |
* dict.c - Ruby LinkParser - Dict Class
|
|
3 |
* $Id$
|
|
4 |
*
|
|
5 |
* Authors:
|
|
6 |
* * Michael Granger <ged@FaerieMUD.org>
|
|
7 |
*
|
|
8 |
* Copyright (c) 2006 The FaerieMUD Consortium.
|
|
9 |
*
|
|
10 |
* This work is licensed under the Creative Commons Attribution License. To
|
|
11 |
* view a copy of this license, visit
|
|
12 |
* http://creativecommons.org/licenses/by/1.0 or send a letter to Creative
|
|
13 |
* Commons, 559 Nathan Abbott Way, Stanford, California 94305, USA.
|
|
14 |
*
|
|
15 |
*/
|
|
16 |
|
|
17 |
#include <ruby.h>
|
|
18 |
#include <intern.h>
|
|
19 |
|
|
20 |
#include <stdio.h>
|
|
21 |
#include <sys/param.h>
|
|
22 |
#include <sys/jail.h>
|
|
23 |
#include <sys/types.h>
|
|
24 |
#include <unistd.h>
|
|
25 |
|
|
26 |
|
|
27 |
VALUE rbjail_mBSD;
|
|
28 |
VALUE rbjail_cBSDJail;
|
|
29 |
|
|
30 |
|
|
31 |
/*
|
2
|
32 |
* Debug logging function
|
|
33 |
*/
|
|
34 |
void
|
|
35 |
#ifdef HAVE_STDARG_PROTOTYPES
|
|
36 |
rlink_debug(const char *fmt, ...)
|
|
37 |
#else
|
|
38 |
rlink_debug( const char *fmt, va_dcl )
|
|
39 |
#endif
|
|
40 |
{
|
|
41 |
char buf[BUFSIZ], buf2[BUFSIZ];
|
|
42 |
va_list args;
|
|
43 |
|
|
44 |
if ( !RTEST(ruby_debug) ) return;
|
|
45 |
|
|
46 |
snprintf( buf, BUFSIZ, "Jail Debug>>> %s", fmt );
|
|
47 |
|
|
48 |
va_init_list( args, fmt );
|
|
49 |
vsnprintf( buf2, BUFSIZ, buf, args );
|
|
50 |
fputs( buf2, stderr );
|
|
51 |
fputs( "\n", stderr );
|
|
52 |
fflush( stderr );
|
|
53 |
va_end( args );
|
|
54 |
}
|
|
55 |
|
|
56 |
|
|
57 |
/*
|
0
|
58 |
struct jail {
|
|
59 |
u_int32_t version;
|
|
60 |
char *path;
|
|
61 |
char *hostname;
|
|
62 |
u_int32_t ip_number;
|
|
63 |
};
|
|
64 |
*/
|
|
65 |
|
|
66 |
/*
|
|
67 |
* Allocation function
|
|
68 |
*/
|
|
69 |
static jail *
|
2
|
70 |
rbjail_jail_alloc() {
|
0
|
71 |
jail *ptr = ALLOC( jail );
|
|
72 |
|
|
73 |
ptr->version = 0;
|
|
74 |
ptr->path = NULL;
|
|
75 |
ptr->hostname = NULL;
|
|
76 |
ptr->ip_number = 0;
|
|
77 |
|
|
78 |
debugMsg(( "Initialized a jail pointer <%p>", ptr ));
|
|
79 |
return ptr;
|
|
80 |
}
|
|
81 |
|
|
82 |
|
|
83 |
/*
|
|
84 |
* GC Free function
|
|
85 |
*/
|
|
86 |
static void
|
2
|
87 |
rbjail_gc_free( jail *ptr ) {
|
0
|
88 |
if ( ptr ) {
|
2
|
89 |
if ( ptr->path ) xfree( ptr->path );
|
|
90 |
if ( ptr->hostname ) xfree( ptr->hostname );
|
|
91 |
|
0
|
92 |
ptr->path = NULL;
|
|
93 |
ptr->hostname = NULL;
|
2
|
94 |
|
0
|
95 |
xfree( ptr );
|
|
96 |
}
|
|
97 |
|
|
98 |
else {
|
2
|
99 |
debugMsg(( "Not freeing an uninitialized jail" ));
|
0
|
100 |
}
|
|
101 |
}
|
|
102 |
|
|
103 |
|
|
104 |
/*
|
|
105 |
* Object validity checker. Returns the data pointer.
|
|
106 |
*/
|
2
|
107 |
static jail *
|
|
108 |
rbjail_check_jail( VALUE self ) {
|
|
109 |
debugMsg(( "Checking a BSD::Jail object (%d).", self ));
|
0
|
110 |
Check_Type( self, T_DATA );
|
|
111 |
|
2
|
112 |
if ( !rb_obj_is_kind_of(self, rbjail_cBSDJail) ) {
|
|
113 |
rb_raise( rb_eTypeError, "wrong argument type %s (expected BSD::Jail)",
|
0
|
114 |
rb_class2name(CLASS_OF( self )) );
|
|
115 |
}
|
|
116 |
|
|
117 |
return DATA_PTR( self );
|
|
118 |
}
|
|
119 |
|
|
120 |
|
|
121 |
/*
|
|
122 |
* Fetch the data pointer and check it for sanity.
|
|
123 |
*/
|
2
|
124 |
static jail *
|
|
125 |
rbjail_get_jail( VALUE self ) {
|
|
126 |
jail *ptr = check_sentence( self );
|
0
|
127 |
|
2
|
128 |
debugMsg(( "Fetching a Jail (%p).", ptr ));
|
0
|
129 |
if ( !ptr )
|
|
130 |
rb_raise( rb_eRuntimeError, "uninitialized Sentence" );
|
|
131 |
|
|
132 |
return ptr;
|
|
133 |
}
|
|
134 |
|
|
135 |
|
2
|
136 |
/* --------------------------------------------------------------
|
|
137 |
* Jail utility functions
|
|
138 |
* -------------------------------------------------------------- */
|
|
139 |
|
0
|
140 |
/*
|
2
|
141 |
* Try to jail_attach() to the specified +jid+, raising an exception if it fails.
|
0
|
142 |
*/
|
|
143 |
static void
|
|
144 |
rbjail_do_jail_attach( int jid )
|
|
145 |
{
|
|
146 |
if ( jail_attach(jid) == -1 )
|
|
147 |
rb_sys_fail( "jail_attach" );
|
|
148 |
}
|
|
149 |
|
2
|
150 |
|
|
151 |
/*
|
|
152 |
* Fork + Block function for rbjail_attach(). Mostly stolen from Ruby's process.c.
|
|
153 |
*/
|
0
|
154 |
static VALUE
|
|
155 |
rbjail_attach_block( int jid )
|
|
156 |
{
|
|
157 |
int pid;
|
|
158 |
|
|
159 |
rb_secure(2);
|
|
160 |
|
2
|
161 |
fflush( stdout );
|
|
162 |
fflush( stderr );
|
0
|
163 |
|
|
164 |
switch ( pid = fork() ) {
|
|
165 |
case 0:
|
|
166 |
rb_thread_atfork();
|
|
167 |
if ( rb_block_given_p() ) {
|
|
168 |
int status;
|
|
169 |
|
|
170 |
rbjail_do_jail_attach( jid );
|
|
171 |
rb_protect( rb_yield, Qundef, &status );
|
|
172 |
ruby_stop( status );
|
|
173 |
}
|
|
174 |
return Qnil;
|
|
175 |
|
|
176 |
case -1:
|
|
177 |
rb_sys_fail( "fork(2)" );
|
|
178 |
return Qnil;
|
|
179 |
|
|
180 |
default:
|
|
181 |
return INT2FIX( pid );
|
|
182 |
}
|
|
183 |
}
|
|
184 |
|
2
|
185 |
|
|
186 |
/* --------------------------------------------------------------
|
|
187 |
* Class methods
|
|
188 |
* -------------------------------------------------------------- */
|
|
189 |
|
|
190 |
/*
|
|
191 |
* call-seq:
|
|
192 |
* BSD::Jail.allocate -> bsdjail
|
|
193 |
*
|
|
194 |
* Allocate a new BSD::Jail object.
|
|
195 |
*
|
|
196 |
*/
|
0
|
197 |
static VALUE
|
2
|
198 |
rbjail_jail_s_allocate( VALUE klass ) {
|
|
199 |
return Data_Wrap_Struct( klass, 0, rbjail_gc_free, 0 );
|
|
200 |
}
|
|
201 |
|
|
202 |
|
|
203 |
/*
|
|
204 |
* call-seq:
|
|
205 |
* BSD::Jail.list -> array
|
|
206 |
*
|
|
207 |
* Return an Array of all the running jails on the host.
|
|
208 |
*
|
|
209 |
*/
|
|
210 |
static VALUE
|
|
211 |
rbjail_jail_s_list( VALUE klass ) {
|
|
212 |
VALUE rval = rb_ary_new();
|
|
213 |
struct xprison *xp;
|
|
214 |
struct in_addr in;
|
|
215 |
size_t i, len;
|
|
216 |
|
|
217 |
if ( sysctlbyname("jail.list", NULL, &len, NULL, 0) == -1 )
|
|
218 |
rb_sys_fail( "sysctlbyname(): jail.list" );
|
|
219 |
|
|
220 |
xp = ALLOCA_N( xprison, 1 );
|
|
221 |
|
|
222 |
if ( sysctlbyname("jail.list", xp, &len, NULL, 0) == -1 ) {
|
|
223 |
rb_sys_fail( "sysctlbyname(): jail.list" );
|
|
224 |
}
|
|
225 |
|
|
226 |
if ( len < sizeof(*xp) || len % sizeof(*xp) ||
|
|
227 |
xp->pr_version != KINFO_PRISON_VERSION )
|
|
228 |
rb_fatal( "Kernel and userland out of sync" );
|
|
229 |
|
|
230 |
len /= sizeof( *xp );
|
|
231 |
for ( i = 0; i < len; i++ ) {
|
|
232 |
VALUE jail, args[3];
|
|
233 |
|
|
234 |
/* Hostname */
|
|
235 |
args[0] = rb_str_new2( xp->pr_host );
|
|
236 |
|
|
237 |
/* IP */
|
|
238 |
in.s_addr = ntohl( xp->pr_ip );
|
|
239 |
args[1] = rb_str_new2( inet_ntoa(in) );
|
|
240 |
|
|
241 |
/* Path */
|
|
242 |
args[2] = rb_str_new2( xp->pr_path );
|
|
243 |
|
|
244 |
jail = rb_class_new_instance( 3, args, klass );
|
|
245 |
rb_ary_push( rval, jail );
|
|
246 |
|
|
247 |
xp++;
|
|
248 |
}
|
|
249 |
|
|
250 |
return rval;
|
|
251 |
}
|
|
252 |
|
|
253 |
|
|
254 |
|
|
255 |
|
|
256 |
/* --------------------------------------------------------------
|
|
257 |
* Instance methods
|
|
258 |
* -------------------------------------------------------------- */
|
|
259 |
|
|
260 |
/*
|
|
261 |
* call-seq:
|
|
262 |
* BSD::Jail.new( hostname, ip, path ) -> new_jail
|
|
263 |
*
|
|
264 |
* Create a new jail for the given +hostname+, +ip+, and +path+.
|
|
265 |
*/
|
|
266 |
static VALUE
|
|
267 |
rbjail_jail_initialize( VALUE self, VALUE hostname, VALUE ip, VALUE path ) {
|
|
268 |
if ( !rbjail_check_jail(self) ) {
|
|
269 |
struct jail *ptr = rbjail_jail_alloc();
|
|
270 |
char *pathstr = NULL, *hostnamestr = NULL;
|
|
271 |
|
|
272 |
Check_Type( hostname, T_STRING );
|
|
273 |
Check_Type( ip, T_STRING );
|
|
274 |
|
|
275 |
pathstr = ALLOC_N( char, RSTRING(path)->len );
|
|
276 |
strncpy( pathstr, RSTRING(path)->ptr, RSTRING(path)->len );
|
|
277 |
|
|
278 |
hostnamestr = ALLOC_N( char, RSTRING(hostname)->len );
|
|
279 |
strncpy( hostnamestr, RSTRING(hostname)->ptr, RSTRING(hostname)->len );
|
|
280 |
|
|
281 |
/*
|
|
282 |
struct jail {
|
|
283 |
u_int32_t version;
|
|
284 |
char *path;
|
|
285 |
char *hostname;
|
|
286 |
u_int32_t ip_number;
|
|
287 |
};
|
|
288 |
*/
|
|
289 |
ptr->version = 0; /* Per the manpage's recommendation */
|
|
290 |
ptr->ip_number = inet_addr( StringValuePtr(ip) );
|
|
291 |
ptr->path = path;
|
|
292 |
ptr->hostname = hostname;
|
|
293 |
}
|
|
294 |
}
|
|
295 |
|
|
296 |
|
|
297 |
/*
|
|
298 |
* call-seq:
|
|
299 |
* jail.attach -> true or false
|
|
300 |
* jail.attach { block } -> pid
|
|
301 |
*
|
|
302 |
* Attach to the given jail. In the non-block form, attach the current process to the
|
|
303 |
* jail and return +true+ if it succeeds. This is a one-way operation, and requires root
|
|
304 |
* privileges.
|
|
305 |
*
|
|
306 |
* In the block form, the process will be forked, and the block will be attached to the jail and
|
|
307 |
* run by the child. The parent process will receive the process ID of the child.
|
|
308 |
*/
|
|
309 |
static VALUE
|
|
310 |
rbjail_attach( int argc, VALUE *argv, VALUE self ) {
|
0
|
311 |
VALUE jidnum, rval;
|
|
312 |
int jid;
|
|
313 |
|
|
314 |
rb_scan_args( argc, argv, "1", &jidnum );
|
|
315 |
jid = NUM2INT( jidnum );
|
|
316 |
|
|
317 |
if ( rb_block_given_p() ) {
|
|
318 |
rval = rbjail_attach_block( jid );
|
|
319 |
}
|
|
320 |
|
|
321 |
else {
|
|
322 |
rbjail_do_jail_attach( jid );
|
|
323 |
rval = Qtrue;
|
|
324 |
}
|
|
325 |
|
|
326 |
return rval;
|
|
327 |
}
|
|
328 |
|
|
329 |
|
2
|
330 |
/*
|
|
331 |
* I can't remember how the hell you document a class, but that will go here.
|
|
332 |
*/
|
0
|
333 |
void
|
2
|
334 |
Init_bsdjail( void ) {
|
0
|
335 |
rbjail_mBSD = rb_define_module( "BSD" );
|
|
336 |
rbjail_cBSDJail = rb_define_class_under( rbjail_mBSD, "Jail" );
|
|
337 |
|
2
|
338 |
/* Class methods */
|
|
339 |
rb_define_alloc_function( rbjail_cBSDJail, rbjail_jail_s_allocate );
|
|
340 |
rb_define_singleton_method( rbjail_cBSDJail, "jail", rbjail_jail_s_jail, 0 );
|
|
341 |
rb_define_singleton_method( rbjail_cBSDJail, "list", rbjail_jail_s_list, 0 );
|
0
|
342 |
|
2
|
343 |
/* Instance methods */
|
|
344 |
rb_define_method( rbjail_cBSDJail, "initialize", rbjail_jail_initialize, 3 );
|
|
345 |
rb_define_method( rbjail_cBSDJail, "attach", rbjail_jail_attach, -1 );
|
|
346 |
rb_define_method( rbjail_cBSDJail, "hostname", rbjail_jail_hostname, 0 );
|
|
347 |
rb_define_method( rbjail_cBSDJail, "path", rbjail_jail_path, 0 );
|
|
348 |
rb_define_method( rbjail_cBSDJail, "ip", rbjail_jail_ip, 0 );
|
|
349 |
rb_define_method( rbjail_cBSDJail, "jid", rbjail_jail_jid, 0 );
|
0
|
350 |
|
|
351 |
}
|
|
352 |
|