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 * ----------------------------------------------------------------------------
10 #include <sys/cdefs.h>
11 __FBSDID("$FreeBSD$");
13 #include <sys/param.h>
15 #include <sys/queue.h>
16 #include <sys/socket.h>
17 #include <sys/sysctl.h>
18 #include <sys/types.h>
20 #include <netinet/in.h>
21 #include <arpa/inet.h>
27 #include <login_cap.h>
36 static void usage(void);
37 static int add_addresses(struct addrinfo *);
38 static struct in_addr *copy_addr4(void);
40 static struct in6_addr *copy_addr6(void);
43 extern char **environ;
46 STAILQ_ENTRY(addr4entry) addr4entries;
51 STAILQ_ENTRY(addr6entry) addr6entries;
57 STAILQ_HEAD(addr4head, addr4entry) addr4 = STAILQ_HEAD_INITIALIZER(addr4);
58 STAILQ_HEAD(addr6head, addr6entry) addr6 = STAILQ_HEAD_INITIALIZER(addr6);
60 #define GET_USER_INFO do { \
61 pwd = getpwnam(username); \
64 err(1, "getpwnam: %s", username); \
66 errx(1, "%s: no such user", username); \
68 lcap = login_getpwclass(pwd); \
70 err(1, "getpwclass: %s", username); \
72 if (getgrouplist(username, pwd->pw_gid, groups, &ngroups) != 0) \
73 err(1, "getgrouplist: %s", username); \
77 main(int argc, char **argv)
79 login_cap_t *lcap = NULL;
81 struct passwd *pwd = NULL;
82 gid_t groups[NGROUPS];
83 int ch, error, i, ngroups, securelevel;
84 int hflag, iflag, Jflag, lflag, uflag, Uflag;
85 char path[PATH_MAX], *jailname, *ep, *username, *JidFile, *ip;
86 static char *cleanenv;
87 const char *shell, *p = NULL;
90 struct addrinfo hints, *res0;
92 hflag = iflag = Jflag = lflag = uflag = Uflag = 0;
94 jailname = username = JidFile = cleanenv = NULL;
97 while ((ch = getopt(argc, argv, "hiln:s:u:U:J:")) != -1) {
113 ltmp = strtol(optarg, &ep, 0);
114 if (*ep || ep == optarg || ltmp > INT_MAX || !ltmp)
115 errx(1, "invalid securelevel: `%s'", optarg);
139 if (lflag && username == NULL)
143 if (realpath(argv[0], path) == NULL)
144 err(1, "realpath: %s", argv[0]);
145 if (chdir(path) != 0)
146 err(1, "chdir: %s", path);
147 /* Initialize struct jail. */
148 memset(&j, 0, sizeof(j));
149 j.version = JAIL_API_VERSION;
151 j.hostname = argv[1];
152 if (jailname != NULL)
153 j.jailname = jailname;
155 /* Handle IP addresses. If requested resolve hostname too. */
156 bzero(&hints, sizeof(struct addrinfo));
157 hints.ai_protocol = IPPROTO_TCP;
158 hints.ai_socktype = SOCK_STREAM;
159 if (JAIL_API_VERSION < 2)
160 hints.ai_family = PF_INET;
162 hints.ai_family = PF_UNSPEC;
163 /* Handle hostname. */
165 error = getaddrinfo(j.hostname, NULL, &hints, &res0);
167 errx(1, "failed to handle hostname: %s",
168 gai_strerror(error));
169 error = add_addresses(res0);
172 errx(1, "failed to add addresses.");
174 /* Handle IP addresses. */
175 hints.ai_flags = AI_NUMERICHOST;
176 ip = strtok(argv[2], ",");
178 error = getaddrinfo(ip, NULL, &hints, &res0);
180 errx(1, "failed to handle ip: %s", gai_strerror(error));
181 error = add_addresses(res0);
184 errx(1, "failed to add addresses.");
185 ip = strtok(NULL, ",");
187 /* Count IP addresses and add them to struct jail. */
188 if (!STAILQ_EMPTY(&addr4)) {
189 j.ip4s = STAILQ_FIRST(&addr4)->count;
190 j.ip4 = copy_addr4();
191 if (j.ip4s > 0 && j.ip4 == NULL)
192 errx(1, "copy_addr4()");
195 if (!STAILQ_EMPTY(&addr6)) {
196 j.ip6s = STAILQ_FIRST(&addr6)->count;
197 j.ip6 = copy_addr6();
198 if (j.ip6s > 0 && j.ip6 == NULL)
199 errx(1, "copy_addr6()");
204 fp = fopen(JidFile, "w");
206 errx(1, "Could not create JidFile: %s", JidFile);
210 err(1, "syscall failed with");
217 fprintf(fp, "%d\t%s\t%s\t%s\t%s\n",
218 i, j.path, j.hostname, argv[2], argv[3]);
221 errx(1, "Could not write JidFile: %s", JidFile);
224 if (securelevel > 0) {
225 if (sysctlbyname("kern.securelevel", NULL, 0, &securelevel,
226 sizeof(securelevel)))
227 err(1, "Can not set securelevel to %d", securelevel);
229 if (username != NULL) {
236 if (setgroups(ngroups, groups) != 0)
238 if (setgid(pwd->pw_gid) != 0)
240 if (setusercontext(lcap, pwd, pwd->pw_uid,
241 LOGIN_SETALL & ~LOGIN_SETGROUP & ~LOGIN_SETLOGIN) != 0)
242 err(1, "setusercontext");
247 shell = pwd->pw_shell;
249 shell = _PATH_BSHELL;
250 if (chdir(pwd->pw_dir) < 0)
251 errx(1, "no home directory");
252 setenv("HOME", pwd->pw_dir, 1);
253 setenv("SHELL", shell, 1);
254 setenv("USER", pwd->pw_name, 1);
256 setenv("TERM", p, 1);
258 if (execv(argv[3], argv + 3) != 0)
259 err(1, "execv: %s", argv[3]);
267 (void)fprintf(stderr, "%s%s%s\n",
268 "usage: jail [-hi] [-n jailname] [-J jid_file] ",
269 "[-s securelevel] [-l -u username | -U username] ",
270 "path hostname [ip[,..]] command ...");
275 add_addresses(struct addrinfo *res0)
278 struct addrinfo *res;
279 struct addr4entry *a4p;
280 struct sockaddr_in *sai;
282 struct addr6entry *a6p;
283 struct sockaddr_in6 *sai6;
288 for (res = res0; res && error == 0; res = res->ai_next) {
289 switch (res->ai_family) {
291 sai = (struct sockaddr_in *)(void *)res->ai_addr;
292 STAILQ_FOREACH(a4p, &addr4, addr4entries) {
293 if (bcmp(&sai->sin_addr, &a4p->ip4,
294 sizeof(struct in_addr)) == 0) {
295 err(1, "Ignoring duplicate IPv4 address.");
299 a4p = (struct addr4entry *) malloc(
300 sizeof(struct addr4entry));
305 bzero(a4p, sizeof(struct addr4entry));
306 bcopy(&sai->sin_addr, &a4p->ip4,
307 sizeof(struct in_addr));
308 if (!STAILQ_EMPTY(&addr4))
309 count = STAILQ_FIRST(&addr4)->count;
312 STAILQ_INSERT_TAIL(&addr4, a4p, addr4entries);
313 STAILQ_FIRST(&addr4)->count = count + 1;
317 sai6 = (struct sockaddr_in6 *)(void *)res->ai_addr;
318 STAILQ_FOREACH(a6p, &addr6, addr6entries) {
319 if (bcmp(&sai6->sin6_addr, &a6p->ip6,
320 sizeof(struct in6_addr)) == 0) {
321 err(1, "Ignoring duplicate IPv6 address.");
325 a6p = (struct addr6entry *) malloc(
326 sizeof(struct addr6entry));
331 bzero(a6p, sizeof(struct addr6entry));
332 bcopy(&sai6->sin6_addr, &a6p->ip6,
333 sizeof(struct in6_addr));
334 if (!STAILQ_EMPTY(&addr6))
335 count = STAILQ_FIRST(&addr6)->count;
338 STAILQ_INSERT_TAIL(&addr6, a6p, addr6entries);
339 STAILQ_FIRST(&addr6)->count = count + 1;
343 err(1, "Address family %d not supported. Ignoring.\n",
352 static struct in_addr *
356 struct in_addr *ip4s, *p, ia;
357 struct addr4entry *a4p;
359 if (STAILQ_EMPTY(&addr4))
362 len = STAILQ_FIRST(&addr4)->count * sizeof(struct in_addr);
364 ip4s = p = (struct in_addr *)malloc(len);
370 while (!STAILQ_EMPTY(&addr4)) {
371 a4p = STAILQ_FIRST(&addr4);
372 STAILQ_REMOVE_HEAD(&addr4, addr4entries);
373 ia.s_addr = a4p->ip4.s_addr;
374 bcopy(&ia, p, sizeof(struct in_addr));
383 static struct in6_addr *
387 struct in6_addr *ip6s, *p;
388 struct addr6entry *a6p;
390 if (STAILQ_EMPTY(&addr6))
393 len = STAILQ_FIRST(&addr6)->count * sizeof(struct in6_addr);
395 ip6s = p = (struct in6_addr *)malloc(len);
401 while (!STAILQ_EMPTY(&addr6)) {
402 a6p = STAILQ_FIRST(&addr6);
403 STAILQ_REMOVE_HEAD(&addr6, addr6entries);
404 bcopy(&a6p->ip6, p, sizeof(struct in6_addr));