From nobody@FreeBSD.org  Mon Dec 10 11:27:59 2001
Return-Path: <nobody@FreeBSD.org>
Received: from freefall.freebsd.org (freefall.FreeBSD.org [216.136.204.21])
	by hub.freebsd.org (Postfix) with ESMTP id A3D3B37B41C
	for <freebsd-gnats-submit@FreeBSD.org>; Mon, 10 Dec 2001 11:27:58 -0800 (PST)
Received: (from nobody@localhost)
	by freefall.freebsd.org (8.11.6/8.11.6) id fBAJRwG16169;
	Mon, 10 Dec 2001 11:27:58 -0800 (PST)
	(envelope-from nobody)
Message-Id: <200112101927.fBAJRwG16169@freefall.freebsd.org>
Date: Mon, 10 Dec 2001 11:27:58 -0800 (PST)
From: Evan Sarmiento <evms@cs.bu.edu>
To: freebsd-gnats-submit@FreeBSD.org
Subject: [PATCH] Allows users to start jails by hostname as well as IP address
X-Send-Pr-Version: www-1.0

>Number:         32680
>Category:       bin
>Synopsis:       [jail] [patch] Allows users to start jail(8) by hostname as well as IP address
>Confidential:   no
>Severity:       non-critical
>Priority:       low
>Responsible:    bz
>State:          closed
>Quarter:        
>Keywords:       
>Date-Required:  
>Class:          sw-bug
>Submitter-Id:   current-users
>Arrival-Date:   Mon Dec 10 11:30:01 PST 2001
>Closed-Date:    Sat Nov 29 16:42:08 UTC 2008
>Last-Modified:  Sat Nov 29 16:42:08 UTC 2008
>Originator:     Evan Sarmiento
>Release:        5.0-CURRENT
>Organization:
>Environment:
FreeBSD vaio 5.0-CURRENT FreeBSD 5.0-CURRENT #14: Wed Nov 28 20:01:56 EST 2001     kaworu@teqnix.sekt7.org:/FreeBSD/FreeBSD-current/src/sys/i386/compile/ARMITAGE  i386

>Description:
When you need to start a jail, you have to specify the IP address
of the jail. This patch allows users to specify the name of the
jail instead of the Ip address if they choose to. For example..

host mrq's ip is 169.69.6.1.

jail /export/mrq mrq 169.69.6.1 /bin/sh /etc/rc
with patch:
jail /export/mrq mrq mrq /bin/sh /etc/rc

Just uses gethostbyname().
>How-To-Repeat:

>Fix:
--- jail.c.orig Sun Jun 24 16:28:19 2001
+++ jail.c      Mon Dec 10 14:21:19 2001
@@ -17,20 +17,20 @@
 #include <arpa/inet.h>

 #include <err.h>
-#include <stdio.h>
+#include <netdb.h>
 #include <stdlib.h>
-#include <string.h>
 #include <unistd.h>

 int
 main(int argc, char **argv)
 {
+       struct hostent *hp;
        struct jail j;
        int i;
        struct in_addr in;

        if (argc < 5)
-               errx(1, "Usage: %s path hostname ip-number command ...\n",
+               errx(1, "Usage: %s path hostname address command ...\n",
                    argv[0]);
        i = chdir(argv[1]);
        if (i)
@@ -40,14 +40,26 @@
        j.path = argv[1];
        j.hostname = argv[2];
        i = inet_aton(argv[3], &in);
-       if (!i)
-               errx(1, "Couldn't make sense of ip-number\n");
+
+       if (!i) {
+         hp = gethostbyname(argv[3]);
+
+         if (hp == NULL)
+          errx(1, "gethostbyname(%s): %s (and) inet_aton(%s): Could not
+           make sense of ip-number", argv[3], hstrerror(h_errno), argv[3] );
+
+         else if (hp)
+          in = *(struct in_addr *)hp->h_addr;
+
+        }
+
        j.ip_number = ntohl(in.s_addr);
        i = jail(&j);
        if (i)
-               err(1, "Imprisonment failed");
+         err(1, "Imprisonment failed");
        i = execv(argv[4], argv + 4);
        if (i)
-               err(1, "execv(%s)", argv[4]);
+         err(1, "execv(%s)", argv[4]);
+
        exit (0);
 }

>Release-Note:
>Audit-Trail:

From: Will Andrews <will@csociety.org>
To: Evan Sarmiento <evms@cs.bu.edu>
Cc: freebsd-gnats-submit@FreeBSD.org
Subject: Re: misc/32680: [PATCH] Allows users to start jails by hostname as well as IP address
Date: Wed, 12 Dec 2001 17:27:42 -0500

 On Mon, Dec 10, 2001 at 11:27:58AM -0800, Evan Sarmiento wrote:
 > --- jail.c.orig Sun Jun 24 16:28:19 2001
 > +++ jail.c      Mon Dec 10 14:21:19 2001
 [...]
 > @@ -40,14 +40,26 @@
 [...]
 > -       if (!i)
 > -               errx(1, "Couldn't make sense of ip-number\n");
 > +
 > +       if (!i) {
 > +         hp = gethostbyname(argv[3]);
 > +
 > +         if (hp == NULL)
 > +          errx(1, "gethostbyname(%s): %s (and) inet_aton(%s): Could not
 > +           make sense of ip-number", argv[3], hstrerror(h_errno), argv[3] );
 > +
 > +         else if (hp)
 > +          in = *(struct in_addr *)hp->h_addr;
 > +
 > +        }
 > +
 
 style(9) says:
 
      Indentation is an 8 character tab.  Second level indents are four spaces.
      If you have to wrap a long statement, put the operator at the end of the
      line.
 
 > -               err(1, "Imprisonment failed");
 > +         err(1, "Imprisonment failed");
 >         i = execv(argv[4], argv + 4);
 >         if (i)
 > -               err(1, "execv(%s)", argv[4]);
 > +         err(1, "execv(%s)", argv[4]);
 > +
 >         exit (0);
 
 Whitespace changes.
 
 -- 
 wca

From: Evan Sarmiento <evms@cs.bu.edu>
To: Will Andrews <will@csociety.org>,
	FreeBSD GNATS DB <FreeBSD-gnats-submit@freebsd.org>
Cc:  
Subject: Re: misc/32680: [PATCH] Allows users to start jails by hostname as well as IP address
Date: Thu, 13 Dec 2001 06:55:44 -0500 (EST)

 Hello,
 
 Attached is the new, stylized (9) jail.c patch:
 
 --- jail.c	Thu Dec 13 06:50:45 2001
 +++ jail.new	Thu Dec 13 06:52:24 2001
 @@ -17,20 +17,20 @@
  #include <arpa/inet.h>
  
  #include <err.h>
 -#include <stdio.h>
 +#include <netdb.h>
  #include <stdlib.h>
 -#include <string.h>
  #include <unistd.h>
  
  int
  main(int argc, char **argv)
  {
 +	struct hostent *hp;
  	struct jail j;
  	int i;
  	struct in_addr in;
  
  	if (argc < 5) 
 -		errx(1, "Usage: %s path hostname ip-number command ...\n",
 +		errx(1, "Usage: %s path hostname address command ...\n",
  		    argv[0]);
  	i = chdir(argv[1]);
  	if (i)
 @@ -40,12 +40,22 @@
  	j.path = argv[1];
  	j.hostname = argv[2];
  	i = inet_aton(argv[3], &in);
 -	if (!i)
 -		errx(1, "Couldn't make sense of ip-number\n");
 +
 +	if (i == NULL) {
 +	        hp = gethostbyname(argv[3]);
 +
 +	        if (hp == NULL)
 +	            errx(1, "gethostbyname(%s): %s (and) inet_aton(%s): Could not make sense of either IP number or hostname.",
 +                         argv[3], hstrerror(h_errno), argv[3] );
 +
 +	        else if (hp)
 +	                in = *(struct in_addr *)hp->h_addr;
 +        }
 +	
  	j.ip_number = ntohl(in.s_addr);
  	i = jail(&j);
  	if (i)
 -		err(1, "Imprisonment failed");
 +	        err(1, "Imprisonment failed");
  	i = execv(argv[4], argv + 4);
  	if (i)
  	        err(1, "execv(%s)", argv[4]);
 

From: Will Andrews <will@csociety.org>
To: Evan Sarmiento <evms@cs.bu.edu>
Cc: Will Andrews <will@csociety.org>,
	FreeBSD GNATS DB <FreeBSD-gnats-submit@freebsd.org>
Subject: Re: misc/32680: [PATCH] Allows users to start jails by hostname as well as IP address
Date: Thu, 13 Dec 2001 07:17:11 -0500

 On Thu, Dec 13, 2001 at 06:55:44AM -0500, Evan Sarmiento wrote:
 > -		err(1, "Imprisonment failed");
 > +	        err(1, "Imprisonment failed");
 
 Whitespace change (1 tab was converted -- leave them alone ;).
 
 -- 
 wca

From: Evan Sarmiento <evms@cs.bu.edu>
To: freebsd-gnats-submit@freebsd.org
Cc:  
Subject: Re: misc/32680: [PATCH] Allows users to start jails by hostname as well as IP address
Date: Thu, 13 Dec 2001 13:32:49 -0500 (EST)

 Finally, I fixed it, and it should all be fine now:
 
 --- jail.c	Thu Dec 13 11:24:04 2001
 +++ jail.new	Thu Dec 13 11:25:43 2001
 @@ -17,20 +17,20 @@
  #include <arpa/inet.h>
  
  #include <err.h>
 -#include <stdio.h>
 +#include <netdb.h>
  #include <stdlib.h>
 -#include <string.h>
  #include <unistd.h>
  
  int
  main(int argc, char **argv)
  {
 +	struct hostent *hp;
  	struct jail j;
  	int i;
  	struct in_addr in;
  
  	if (argc < 5) 
 -		errx(1, "Usage: %s path hostname ip-number command ...\n",
 +		errx(1, "Usage: %s path hostname address command ...\n",
  		    argv[0]);
  	i = chdir(argv[1]);
  	if (i)
 @@ -40,8 +40,18 @@
  	j.path = argv[1];
  	j.hostname = argv[2];
  	i = inet_aton(argv[3], &in);
 -	if (!i)
 -		errx(1, "Couldn't make sense of ip-number\n");
 +
 +	if (i == NULL) {
 +	        hp = gethostbyname(argv[3]);
 +
 +	        if (hp == NULL)
 +	            errx(1, "gethostbyname(%s): %s (and) inet_aton(%s): Could not make sense of either IP number or hostname.",
 +                         argv[3], hstrerror(h_errno), argv[3] );
 +
 +	        else if (hp)
 +	                in = *(struct in_addr *)hp->h_addr;
 +        }
 +	
  	j.ip_number = ntohl(in.s_addr);
  	i = jail(&j);
  	if (i)
 
Responsible-Changed-From-To: freebsd-bugs->freebsd-jail 
Responsible-Changed-By: linimon 
Responsible-Changed-When: Fri Jan 25 22:05:28 UTC 2008 
Responsible-Changed-Why:  
Reassign to appropriate mailing list. 

http://www.freebsd.org/cgi/query-pr.cgi?pr=32680 
Responsible-Changed-From-To: freebsd-jail->bz 
Responsible-Changed-By: bz 
Responsible-Changed-When: Fri Jan 25 22:18:06 UTC 2008 
Responsible-Changed-Why:  
Take. I have similar functionality for multi-IPv4/v6 jails already in my tree. 

For single-IP jails there is the problem with this patch, if the 
host resolves to multiple IP addresses as the jail might be started on 
a random one. 

http://www.freebsd.org/cgi/query-pr.cgi?pr=32680 
State-Changed-From-To: open->patched 
State-Changed-By: bz 
State-Changed-When: Sat Nov 29 16:33:11 UTC 2008 
State-Changed-Why:  
The jail update comitted today to HEAD added a -h option 
to the jail(8) command to resolve the given hostname to IPs 
and use those for jails. 

http://www.freebsd.org/cgi/query-pr.cgi?pr=32680 
State-Changed-From-To: patched->closed 
State-Changed-By: bz 
State-Changed-When: Sat Nov 29 16:41:10 UTC 2008 
State-Changed-Why:  
Submitters mail bounces "User unknown" and if anything 
will be done with this; it'll be MFCed with the entire jail 
work to 7-STABLE. 

http://www.freebsd.org/cgi/query-pr.cgi?pr=32680 
>Unformatted:
