xref: /freebsd/usr.sbin/jail/jail.c (revision c37420b0d5b3b6ef875fbf0b84a13f6f09be56d6)
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 
10 #include <sys/cdefs.h>
11 __FBSDID("$FreeBSD$");
12 
13 #include <sys/param.h>
14 #include <sys/jail.h>
15 
16 #include <netinet/in.h>
17 #include <arpa/inet.h>
18 
19 #include <err.h>
20 #include <errno.h>
21 #include <grp.h>
22 #include <login_cap.h>
23 #include <paths.h>
24 #include <pwd.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <unistd.h>
29 
30 static void	usage(void);
31 extern char	**environ;
32 
33 #define GET_USER_INFO do {						\
34 	pwd = getpwnam(username);					\
35 	if (pwd == NULL) {						\
36 		if (errno)						\
37 			err(1, "getpwnam: %s", username);		\
38 		else							\
39 			errx(1, "%s: no such user", username);		\
40 	}								\
41 	lcap = login_getpwclass(pwd);					\
42 	if (lcap == NULL)						\
43 		err(1, "getpwclass: %s", username);			\
44 	ngroups = NGROUPS;						\
45 	if (getgrouplist(username, pwd->pw_gid, groups, &ngroups) != 0)	\
46 		err(1, "getgrouplist: %s", username);			\
47 } while (0)
48 
49 int
50 main(int argc, char **argv)
51 {
52 	login_cap_t *lcap;
53 	struct jail j;
54 	struct passwd *pwd;
55 	struct in_addr in;
56 	int ch, groups[NGROUPS], i, iflag, lflag, ngroups, uflag, Uflag;
57 	char path[PATH_MAX], *username;
58 	static char *cleanenv;
59 	const char *shell, *p;
60 
61 	iflag = lflag = uflag = Uflag = 0;
62 	username = cleanenv = NULL;
63 
64 	while ((ch = getopt(argc, argv, "ilu:U:")) != -1) {
65 		switch (ch) {
66 		case 'i':
67 			iflag = 1;
68 			break;
69 		case 'u':
70 			username = optarg;
71 			uflag = 1;
72 			break;
73 		case 'U':
74 			username = optarg;
75 			Uflag = 1;
76 			break;
77 		case 'l':
78 			lflag = 1;
79 			break;
80 		default:
81 			usage();
82 		}
83 	}
84 	argc -= optind;
85 	argv += optind;
86 	if (argc < 4)
87 		usage();
88 	if (uflag && Uflag)
89 		usage();
90 	if (lflag && username == NULL)
91 		usage();
92 	if (uflag)
93 		GET_USER_INFO;
94 	if (realpath(argv[0], path) == NULL)
95 		err(1, "realpath: %s", argv[0]);
96 	if (chdir(path) != 0)
97 		err(1, "chdir: %s", path);
98 	memset(&j, 0, sizeof(j));
99 	j.version = 0;
100 	j.path = path;
101 	j.hostname = argv[1];
102 	if (inet_aton(argv[2], &in) == 0)
103 		errx(1, "Could not make sense of ip-number: %s", argv[2]);
104 	j.ip_number = ntohl(in.s_addr);
105 	i = jail(&j);
106 	if (i == -1)
107 		err(1, "jail");
108 	if (iflag) {
109 		printf("%d\n", i);
110 		fflush(stdout);
111 	}
112 	if (username != NULL) {
113 		if (Uflag)
114 			GET_USER_INFO;
115 		if (lflag) {
116 			p = getenv("TERM");
117 			environ = &cleanenv;
118 		}
119 		if (setgroups(ngroups, groups) != 0)
120 			err(1, "setgroups");
121 		if (setgid(pwd->pw_gid) != 0)
122 			err(1, "setgid");
123 		if (setusercontext(lcap, pwd, pwd->pw_uid,
124 		    LOGIN_SETALL & ~LOGIN_SETGROUP) != 0)
125 			err(1, "setusercontext");
126 		login_close(lcap);
127 	}
128 	if (lflag) {
129 		if (*pwd->pw_shell)
130 			shell = pwd->pw_shell;
131 		else
132 			shell = _PATH_BSHELL;
133 		if (chdir(pwd->pw_dir) < 0)
134 			errx(1, "no home directory");
135 		setenv("HOME", pwd->pw_dir, 1);
136 		setenv("SHELL", shell, 1);
137 		setenv("USER", pwd->pw_name, 1);
138 		if (p)
139 			setenv("TERM", p, 1);
140 	}
141 	if (execv(argv[3], argv + 3) != 0)
142 		err(1, "execv: %s", argv[3]);
143 	exit(0);
144 }
145 
146 static void
147 usage(void)
148 {
149 
150 	(void)fprintf(stderr, "%s%s\n",
151 	     "usage: jail [-i] [-l -u username | -U username]",
152 	     " path hostname ip-number command ...");
153 	exit(1);
154 }
155