xref: /freebsd/usr.sbin/jail/jail.c (revision 1b6c76a2fe091c74f08427e6c870851025a9cf67)
1 /*
2  * ----------------------------------------------------------------------------
3  * "THE BEER-WARE LICENSE" (Revision 42):
4  * <phk@FreeBSD.ORG> wrote this file.  As long as you retain this notice you
5  * can do whatever you want with this stuff. If we meet some day, and you think
6  * this stuff is worth it, you can buy me a beer in return.   Poul-Henning Kamp
7  * ----------------------------------------------------------------------------
8  *
9  * $FreeBSD$
10  *
11  */
12 
13 #include <sys/types.h>
14 #include <sys/jail.h>
15 
16 #include <netinet/in.h>
17 #include <arpa/inet.h>
18 
19 #include <err.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <unistd.h>
24 
25 int
26 main(int argc, char **argv)
27 {
28 	struct jail j;
29 	int i;
30 	struct in_addr in;
31 
32 	if (argc < 5)
33 		errx(1, "Usage: %s path hostname ip-number command ...\n",
34 		    argv[0]);
35 	i = chdir(argv[1]);
36 	if (i)
37 		err(1, "chdir %s", argv[1]);
38 	memset(&j, 0, sizeof(j));
39 	j.version = 0;
40 	j.path = argv[1];
41 	j.hostname = argv[2];
42 	i = inet_aton(argv[3], &in);
43 	if (!i)
44 		errx(1, "Couldn't make sense of ip-number\n");
45 	j.ip_number = ntohl(in.s_addr);
46 	i = jail(&j);
47 	if (i)
48 		err(1, "Imprisonment failed");
49 	i = execv(argv[4], argv + 4);
50 	if (i)
51 		err(1, "execv(%s)", argv[4]);
52 	exit (0);
53 }
54