From owner-freebsd-hackers Sun Dec 9 11:19:57 2001 Delivered-To: freebsd-hackers@freebsd.org Received: from bazooka.trit.org (bazooka.trit.org [63.198.170.138]) by hub.freebsd.org (Postfix) with ESMTP id A5F0437B405; Sun, 9 Dec 2001 11:19:48 -0800 (PST) Received: by bazooka.trit.org (Postfix, from userid 1000) id 6AB703E3A; Sun, 9 Dec 2001 19:19:48 +0000 (UTC) Received: from bazooka (localhost [127.0.0.1]) by bazooka.trit.org (Postfix) with ESMTP id 694463C12E; Sun, 9 Dec 2001 19:19:48 +0000 (UTC) To: chris@FreeBSD.ORG Cc: Igor M Podlesny , freebsd-hackers@FreeBSD.ORG Subject: Re: jail.c.patch (allowing to use hostnames when invoking jail(8)) In-Reply-To: <20011125112748.B511@holly.calldei.com>; from chris@FreeBSD.ORG on "Sun, 25 Nov 2001 11:27:48 -0600" Date: Sun, 09 Dec 2001 19:19:43 +0000 From: Dima Dorfman Message-Id: <20011209191948.6AB703E3A@bazooka.trit.org> Sender: owner-freebsd-hackers@FreeBSD.ORG Precedence: bulk List-ID: List-Archive: (Web Archive) List-Help: (List Instructions) List-Subscribe: List-Unsubscribe: X-Loop: FreeBSD.ORG Chris Costello wrote: > On Saturday, November 24, 2001, Igor M Podlesny wrote: > > i = inet_aton(argv[3], &in); > > - if (!i) > > - errx(1, "Couldn't make sense of ip-number\n"); > > + if (!i) { > > + /* check if it is resolveable */ > > + struct hostent *hp; > > + hp = gethostbyname(argv[3]); > > + if (hp == NULL) { > > + errx(1, "Couldn't make sense of the jail address\n"); > > + } > > + else { > > + char **p = hp->h_addr_list; > > + if (p[1] != NULL) { > > + errx(1, "Jail should have only one ip-address associated with!\n"); > > + } > > + else { > > + memcpy(&in.s_addr, p[0], sizeof(in.s_addr)); > > + } > > + } > > + } > > I'd rewrite the above (`i = inet_aton' all the way down) as > > hp = gethostbyname(argv[3]); > if (hp == NULL) { > errx(1, "%s: %s", argv[3], hstrerror(h_errno)); > } > in = *(struct in_addr *)hp->h_addr_list[0]; > > This makes the call to inet_aton() unnecessary (and really > shortens the code!). As discussed off-list, this is a good idea. Attached is the final patch that I plan to commit unless I hear objections. Please review. Thanks. Index: jail.8 =================================================================== RCS file: /ref/cvsf/src/usr.sbin/jail/jail.8,v retrieving revision 1.30 diff -u -r1.30 jail.8 --- jail.8 2001/09/03 15:42:10 1.30 +++ jail.8 2001/12/09 19:14:30 @@ -43,7 +43,7 @@ .Nm .Ar path .Ar hostname -.Ar ip-number +.Ar hostname .Ar command .Ar ... .Sh DESCRIPTION Index: jail.c =================================================================== RCS file: /ref/cvsf/src/usr.sbin/jail/jail.c,v retrieving revision 1.7 diff -u -r1.7 jail.c --- jail.c 2001/06/24 20:28:19 1.7 +++ jail.c 2001/12/09 19:14:11 @@ -17,6 +17,7 @@ #include #include +#include #include #include #include @@ -25,12 +26,13 @@ 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 hostname command ...\n", argv[0]); i = chdir(argv[1]); if (i) @@ -39,9 +41,11 @@ j.version = 0; j.path = argv[1]; j.hostname = argv[2]; + hp = gethostbyname(argv[3]); + if (hp == NULL) + errx(1, "gethostbyname(%s): %s", argv[3], hstrerror(h_errno)); i = inet_aton(argv[3], &in); - if (!i) - errx(1, "Couldn't make sense of ip-number\n"); + in = *(struct in_addr *)hp->h_addr; j.ip_number = ntohl(in.s_addr); i = jail(&j); if (i) To Unsubscribe: send mail to majordomo@FreeBSD.org with "unsubscribe freebsd-hackers" in the body of the message