ext/jail.c
branchmahlon-misc
changeset 12 5fd07a9e7e7b
parent 11 e908d309e7ec
equal deleted inserted replaced
11:e908d309e7ec 12:5fd07a9e7e7b
       
     1 /*
       
     2  *  jail.c - Ruby jparallel
       
     3  *
       
     4  *  vim: set nosta noet ts=4 sw=4:
       
     5  *
       
     6  *  $Id$
       
     7  *  
       
     8  *  Authors:
       
     9  *	* Michael Granger <ged@FaerieMUD.org>
       
    10  *	* Mahlon E. Smith <mahlon@martini.nu>
       
    11  *  
       
    12  *  Copyright (c) 2008, Michael Granger and Mahlon E. Smith
       
    13  *  All rights reserved.
       
    14  *  
       
    15  *  Redistribution and use in source and binary forms, with or without
       
    16  *  modification, are permitted provided that the following conditions are met:
       
    17  *  
       
    18  *	* Redistributions of source code must retain the above copyright notice,
       
    19  *	  this list of conditions and the following disclaimer.
       
    20  *  
       
    21  *	* Redistributions in binary form must reproduce the above copyright notice,
       
    22  *	  this list of conditions and the following disclaimer in the documentation
       
    23  *	  and/or other materials provided with the distribution.
       
    24  *  
       
    25  *	* Neither the name of the author/s, nor the names of the project's
       
    26  *	  contributors may be used to endorse or promote products derived from this
       
    27  *	  software without specific prior written permission.
       
    28  *  
       
    29  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
       
    30  *  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
       
    31  *  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
       
    32  *  DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
       
    33  *  FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
       
    34  *  DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
       
    35  *  SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
       
    36  *  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
       
    37  *  OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
       
    38  *  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
       
    39  *  
       
    40  */
       
    41 
       
    42 
       
    43 #include "jail.h"
       
    44 
       
    45 VALUE rbjail_mBSD;
       
    46 VALUE rbjail_cBSDJail;
       
    47 VALUE rbjail_cIPAddr;
       
    48 
       
    49 
       
    50 /*
       
    51  *  Debug logging function
       
    52  */
       
    53 void
       
    54 #ifdef HAVE_STDARG_PROTOTYPES
       
    55 rbjail_debug(const char *fmt, ...)
       
    56 #else
       
    57 rbjail_debug( const char *fmt, va_dcl )
       
    58 #endif
       
    59 {
       
    60 	char buf[BUFSIZ], buf2[BUFSIZ];
       
    61 	va_list args;
       
    62 
       
    63 	if ( !RTEST(ruby_debug) ) return;
       
    64 
       
    65 	snprintf( buf, BUFSIZ, "Debug>>> %s", fmt );
       
    66 
       
    67 	va_init_list( args, fmt );
       
    68 	vsnprintf( buf2, BUFSIZ, buf, args );
       
    69 	fputs( buf2, stderr );
       
    70 	fputs( "\n", stderr );
       
    71 	fflush( stderr );
       
    72 	va_end( args );
       
    73 }
       
    74 
       
    75 
       
    76 /*
       
    77  * Object validity checker. Returns the data pointer.
       
    78  */
       
    79 static struct xprison *
       
    80 rbjail_check_jail( VALUE self ) {
       
    81 	debugMsg(( "Checking a BSD::Jail object (%d).", self ));
       
    82 	Check_Type( self, T_DATA );
       
    83 
       
    84 	if ( !rb_obj_is_kind_of(self, rbjail_cBSDJail) ) {
       
    85 		rb_raise( rb_eTypeError, "wrong argument type %s (expected BSD::Jail)",
       
    86 			rb_class2name(CLASS_OF( self )) );
       
    87 	}
       
    88 
       
    89 	return DATA_PTR( self );
       
    90 }
       
    91 
       
    92 
       
    93 /*
       
    94  * Fetch the data pointer and check it for sanity.
       
    95  */
       
    96 static struct xprison *
       
    97 rbjail_get_jailptr( VALUE self ) {
       
    98 	struct xprison *ptr = rbjail_check_jail( self );
       
    99 
       
   100 	debugMsg(( "Fetching a Jail (%p).", ptr ));
       
   101 	if ( !ptr )
       
   102 		rb_raise( rb_eRuntimeError, "uninitialized Jail" );
       
   103 
       
   104 	return ptr;
       
   105 }
       
   106 
       
   107 
       
   108 /*
       
   109  * Copy memory from the given 'xp' to a ruby managed object.
       
   110  */
       
   111 static VALUE
       
   112 rbjail_alloc( VALUE class, struct xprison *xp )
       
   113 {
       
   114 	struct xprison *rbjail_xp = ALLOC( struct xprison );
       
   115 	VALUE rbjail = rb_funcall( class, rb_intern("allocate"), 0 );
       
   116 
       
   117 	// replace the null pointer obj with an xprison ptr.
       
   118 	//
       
   119 	memcpy( rbjail_xp, xp, sizeof( struct xprison ) );
       
   120 	DATA_PTR( rbjail ) = rbjail_xp;
       
   121 
       
   122 	return rbjail;
       
   123 }
       
   124 
       
   125 
       
   126 /*
       
   127  *
       
   128  */
       
   129 static VALUE
       
   130 rbjail_get_ip( VALUE self )
       
   131 {
       
   132 	struct xprison *xp = rbjail_get_jailptr( self );
       
   133 	struct in_addr in;
       
   134 	char *ip;
       
   135 
       
   136 	in.s_addr = ntohl( xp->pr_ip );
       
   137 	ip = inet_ntoa(in);
       
   138 
       
   139 	return rb_funcall( rbjail_cIPAddr, rb_intern("new"), 1, rb_str_new2(ip) );
       
   140 }
       
   141 
       
   142 
       
   143 /*
       
   144  *
       
   145  */
       
   146 static VALUE
       
   147 rbjail_get_jid( VALUE self )
       
   148 {
       
   149 	struct xprison *xp = rbjail_get_jailptr( self );
       
   150 	return INT2FIX( xp->pr_id );
       
   151 }
       
   152 
       
   153 
       
   154 /*
       
   155  *
       
   156  */
       
   157 static VALUE
       
   158 rbjail_get_host( VALUE self )
       
   159 {
       
   160 	struct xprison *xp = rbjail_get_jailptr( self );
       
   161 	return rb_str_new2( xp->pr_host );
       
   162 }
       
   163 
       
   164 
       
   165 /*
       
   166  *
       
   167  */
       
   168 static VALUE
       
   169 rbjail_get_path( VALUE self )
       
   170 {
       
   171 	struct xprison *xp = rbjail_get_jailptr( self );
       
   172 	return rb_str_new2( xp->pr_path );
       
   173 }
       
   174 
       
   175 
       
   176 /*
       
   177  * GC Free function
       
   178  */
       
   179 static void
       
   180 rbjail_gc_free( struct xprison *ptr ) {
       
   181 	if ( ptr ) {
       
   182 		xfree( ptr );
       
   183 	}
       
   184 
       
   185 	else {
       
   186 		debugMsg(( "Not freeing an uninitialized jail" ));
       
   187 	}
       
   188 }
       
   189 
       
   190 
       
   191 /*
       
   192  * Allocate a new ruby object with a NULL pointer.
       
   193  */
       
   194 static VALUE
       
   195 rbjail_s_alloc( VALUE class )
       
   196 {
       
   197 	return Data_Wrap_Struct( class, NULL, rbjail_gc_free, NULL );
       
   198 }
       
   199 
       
   200 
       
   201 /*
       
   202  * Create a new jail object.
       
   203  * Returns the +id+ of the newly created jail.
       
   204  */
       
   205 static VALUE
       
   206 rbjail_jail( int argc, VALUE *argv, VALUE self ) {
       
   207 	struct jail j;
       
   208 	struct in_addr in;
       
   209 	VALUE ip, path, host, securelevel;
       
   210 	int id;
       
   211 
       
   212 	rb_scan_args( argc, argv, "31", &ip, &path, &host, &securelevel );
       
   213 
       
   214 	if ( inet_aton( RSTRING_PTR( rb_obj_as_string(ip) ), &in ) == 0 )
       
   215 		rb_raise( rb_eArgError, "Could not make sense of ip number: %s", ip );
       
   216 
       
   217 	SafeStringValue(path);
       
   218 	SafeStringValue(host);
       
   219 
       
   220 	j.version   = 0;
       
   221 	j.path	    = RSTRING_PTR( path );
       
   222 	j.hostname  = RSTRING_PTR( host );
       
   223 	j.ip_number = ntohl( in.s_addr );
       
   224 	id = jail(&j);
       
   225 
       
   226 	if ( id == -1 ) rb_sys_fail( "jail" );
       
   227 	if ( chdir("/") == -1 ) rb_sys_fail( "chdir" );
       
   228 
       
   229 	debugMsg(( "New jail created with id: %d\n", id ));
       
   230 	return INT2FIX( id );
       
   231 }
       
   232 
       
   233 
       
   234 /*
       
   235  * Iterate over the currently instantiated jails, returning a jail
       
   236  * object that matches the given string/ip/JID -- or nil if none do. 
       
   237  * Without an argument, return an array of all JIDs. 
       
   238  */ 
       
   239 static VALUE
       
   240 rbjail_find( int argc, VALUE *argv, VALUE self )
       
   241 { 
       
   242 	struct xprison *sxp, *xp;
       
   243 	struct in_addr in;
       
   244 	size_t i, len;
       
   245 
       
   246 	VALUE arg, rbjail;
       
   247 	VALUE jails = rb_ary_new();
       
   248 	int jid = 0, compare = 0;
       
   249 	char *str = "";
       
   250 
       
   251 	rb_scan_args( argc, argv, "01", &arg );
       
   252 
       
   253 	// An argument was passed, so let's figure out what it was
       
   254 	// and try to compare it to the current jails.
       
   255 	//
       
   256 	if ( argc == 1 ) {
       
   257 		switch ( TYPE(arg) ) {
       
   258 
       
   259 			// find by JID
       
   260 			//
       
   261 			case T_FIXNUM:
       
   262 				jid = FIX2INT( arg );
       
   263 				break;
       
   264 
       
   265 				// find by IP/hostname
       
   266 				//
       
   267 			case T_OBJECT:
       
   268 			case T_DATA:
       
   269 			case T_STRING:
       
   270 				str = RSTRING_PTR( rb_obj_as_string(arg) );
       
   271 				compare = 1;
       
   272 				break;
       
   273 
       
   274 			default:
       
   275 				rb_raise( rb_eTypeError, "invalid argument to find(): %s",
       
   276 						RSTRING_PTR(rb_inspect(arg)) );
       
   277 		}
       
   278 	}
       
   279 
       
   280 	// Get the size of the xprison and allocate memory to it.
       
   281 	//
       
   282 	if ( sysctlbyname("security.jail.list", NULL, &len, NULL, 0) == -1 )
       
   283 		rb_sys_fail("sysctlbyname(): security.jail.list");
       
   284 	if ( len <= 0 ) {
       
   285 		rb_sys_fail("sysctlbyname(): unable to determine xprison size");
       
   286 		return Qnil;
       
   287 	}
       
   288 
       
   289 	sxp = xp = malloc( len );
       
   290 	if ( sxp == NULL ) {
       
   291 		rb_sys_fail("sysctlbyname(): unable to allocate memory");
       
   292 		return Qnil;
       
   293 	}
       
   294 
       
   295 	// Get and sanity check the current prison list
       
   296 	//
       
   297 	if ( sysctlbyname("security.jail.list", xp, &len, NULL, 0) == -1 ) {
       
   298 		if ( errno == ENOMEM ) free( sxp );
       
   299 		rb_sys_fail("sysctlbyname(): out of memory");
       
   300 		return Qnil;
       
   301 	}
       
   302 	if ( len < sizeof(*xp) || len % sizeof(*xp) || xp->pr_version != XPRISON_VERSION )
       
   303 		rb_fatal("Kernel and userland out of sync");
       
   304 
       
   305 	// No arguments to find() -- return an array of all JIDs
       
   306 	//
       
   307 	if ( argc == 0 ) {
       
   308 		for ( i = 0; i < len / sizeof(*xp); i++ ) {
       
   309 			rb_ary_push( jails, rbjail_alloc( self, xp ) );
       
   310 			xp++;
       
   311 		}
       
   312 
       
   313 		free( sxp );
       
   314 		return jails;
       
   315 	}
       
   316 
       
   317 	// Argument passed to find(): walk the jail list, comparing the arg
       
   318 	// with each current jail.  Return first match.
       
   319 	//
       
   320 	for ( i = 0; i < len / sizeof(*xp); i++ ) {
       
   321 		in.s_addr = ntohl(xp->pr_ip);
       
   322 		if (( compare == 0 && xp->pr_id == jid ) ||
       
   323 				( compare == 1 &&
       
   324 				  (( strcmp( str, inet_ntoa(in) ) == 0 ) ||
       
   325 				   (  strcmp( str, xp->pr_host )  == 0 ))
       
   326 				)) {
       
   327 
       
   328 			debugMsg(( "Located jail: %d", xp->pr_id ));
       
   329 			rbjail = rbjail_alloc( self, xp );
       
   330 
       
   331 			free( sxp );
       
   332 			return rbjail;
       
   333 		}
       
   334 		else {
       
   335 
       
   336 			xp++;
       
   337 		}
       
   338 	}
       
   339 
       
   340 	free( sxp );
       
   341 	return Qnil;
       
   342 }
       
   343 
       
   344 
       
   345 static void
       
   346 rbjail_do_jail_attach( int jid )
       
   347 {
       
   348 	if ( jail_attach(jid) == -1 )
       
   349 		rb_sys_fail( "jail_attach" );
       
   350 }
       
   351 
       
   352 
       
   353 /* Mostly ripped off from Ruby's process.c */
       
   354 static VALUE
       
   355 rbjail_attach_block( int jid )
       
   356 {
       
   357 	int pid;
       
   358 
       
   359 	rb_secure(2);
       
   360 
       
   361 	fflush(stdout);
       
   362 	fflush(stderr);
       
   363 
       
   364 	switch ( pid = fork() ) {
       
   365 		case 0:
       
   366 			rb_thread_atfork();
       
   367 			if ( rb_block_given_p() ) {
       
   368 				int status;
       
   369 
       
   370 				rbjail_do_jail_attach( jid );
       
   371 				rb_protect( rb_yield, Qundef, &status );
       
   372 				ruby_stop( status );
       
   373 			}
       
   374 			return Qnil;
       
   375 
       
   376 		case -1:
       
   377 			rb_sys_fail( "fork(2)" );
       
   378 			return Qnil;
       
   379 
       
   380 		default:
       
   381 			return INT2FIX( pid );
       
   382 	}
       
   383 }
       
   384 
       
   385 static VALUE
       
   386 rbjail_attach( int argc, VALUE *argv, VALUE self )
       
   387 {
       
   388 	VALUE jidnum, rval;
       
   389 	int jid;
       
   390 
       
   391 	rb_scan_args( argc, argv, "1", &jidnum );
       
   392 	jid = NUM2INT( jidnum );
       
   393 
       
   394 	if ( rb_block_given_p() ) {
       
   395 		rval = rbjail_attach_block( jid );
       
   396 	}
       
   397 
       
   398 	else {
       
   399 		rbjail_do_jail_attach( jid );
       
   400 		rval = Qtrue;
       
   401 	}
       
   402 
       
   403 	return rval;
       
   404 }
       
   405 
       
   406 
       
   407 /*
       
   408  * Ruby initializer.
       
   409  */
       
   410 void
       
   411 Init_jail( void )
       
   412 {
       
   413 	rbjail_mBSD	= rb_define_module( "BSD" );
       
   414 	rbjail_cBSDJail = rb_define_class_under( rbjail_mBSD, "Jail", rb_cObject );
       
   415 
       
   416 	rb_require("ipaddr");
       
   417 	rbjail_cIPAddr = rb_const_get( rb_cObject, rb_intern("IPAddr") );
       
   418 
       
   419 	rb_define_alloc_func( rbjail_cBSDJail, rbjail_s_alloc );
       
   420 
       
   421 	// Make the 'new' method private.
       
   422 	rb_funcall( rbjail_cBSDJail, rb_intern("private_class_method"), 1, ID2SYM(rb_intern("new")) );
       
   423 
       
   424 	// main utility functions
       
   425 	//
       
   426 	rb_define_singleton_method( rbjail_cBSDJail, "jail", rbjail_jail, -1 );
       
   427 	rb_define_singleton_method( rbjail_cBSDJail, "find", rbjail_find, -1 );
       
   428 	rb_define_method( rbjail_cBSDJail, "attach", rbjail_attach, -1 );
       
   429 	//	rb_define_alias( rbjail_cBSDJail, "list", "find" );
       
   430 	
       
   431 	// accessor functions
       
   432 	//
       
   433 	rb_define_method( rbjail_cBSDJail, "jid",  rbjail_get_jid,  0 );
       
   434 	rb_define_method( rbjail_cBSDJail, "ip",   rbjail_get_ip,   0 );
       
   435 	rb_define_method( rbjail_cBSDJail, "host", rbjail_get_host, 0 );
       
   436 	rb_define_method( rbjail_cBSDJail, "path", rbjail_get_path, 0 );
       
   437 }
       
   438 
       
   439