xref: /freebsd/usr.sbin/jexec/jexec.c (revision 195ebc7e9e4b129de810833791a19dfb4349d6a9)
1 /*-
2  * Copyright (c) 2003 Mike Barcroft <mike@FreeBSD.org>
3  * Copyright (c) 2008 Bjoern A. Zeeb <bz@FreeBSD.org>
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  *
27  * $FreeBSD$
28  */
29 
30 #include <sys/param.h>
31 #include <sys/jail.h>
32 #include <sys/socket.h>
33 #include <sys/sysctl.h>
34 #include <sys/uio.h>
35 
36 #include <arpa/inet.h>
37 #include <netinet/in.h>
38 
39 #include <err.h>
40 #include <errno.h>
41 #include <limits.h>
42 #include <login_cap.h>
43 #include <stdio.h>
44 #include <stdlib.h>
45 #include <string.h>
46 #include <pwd.h>
47 #include <unistd.h>
48 
49 static void	usage(void);
50 
51 #define GET_USER_INFO do {						\
52 	pwd = getpwnam(username);					\
53 	if (pwd == NULL) {						\
54 		if (errno)						\
55 			err(1, "getpwnam: %s", username);		\
56 		else							\
57 			errx(1, "%s: no such user", username);		\
58 	}								\
59 	lcap = login_getpwclass(pwd);					\
60 	if (lcap == NULL)						\
61 		err(1, "getpwclass: %s", username);			\
62 	ngroups = NGROUPS;						\
63 	if (getgrouplist(username, pwd->pw_gid, groups, &ngroups) != 0)	\
64 		err(1, "getgrouplist: %s", username);			\
65 } while (0)
66 
67 int
68 main(int argc, char *argv[])
69 {
70 	struct iovec params[2];
71 	int jid;
72 	login_cap_t *lcap = NULL;
73 	struct passwd *pwd = NULL;
74 	gid_t groups[NGROUPS];
75 	int ch, ngroups, uflag, Uflag;
76 	char *ep, *username;
77 	ch = uflag = Uflag = 0;
78 	username = NULL;
79 
80 	while ((ch = getopt(argc, argv, "nu:U:")) != -1) {
81 		switch (ch) {
82 		case 'n':
83 			/* Specified name, now unused */
84 			break;
85 		case 'u':
86 			username = optarg;
87 			uflag = 1;
88 			break;
89 		case 'U':
90 			username = optarg;
91 			Uflag = 1;
92 			break;
93 		default:
94 			usage();
95 		}
96 	}
97 	argc -= optind;
98 	argv += optind;
99 	if (argc < 2)
100 		usage();
101 	if (uflag && Uflag)
102 		usage();
103 	if (uflag)
104 		GET_USER_INFO;
105 	jid = strtoul(argv[0], &ep, 10);
106 	if (!*argv[0] || *ep) {
107 		*(const void **)&params[0].iov_base = "name";
108 		params[0].iov_len = sizeof("name");
109 		params[1].iov_base = argv[0];
110 		params[1].iov_len = strlen(argv[0]) + 1;
111 		jid = jail_get(params, 2, 0);
112 		if (jid < 0)
113 			errx(1, "Unknown jail: %s", argv[0]);
114 	}
115 	if (jail_attach(jid) == -1)
116 		err(1, "jail_attach(): %d", jid);
117 	if (chdir("/") == -1)
118 		err(1, "chdir(): /");
119 	if (username != NULL) {
120 		if (Uflag)
121 			GET_USER_INFO;
122 		if (setgroups(ngroups, groups) != 0)
123 			err(1, "setgroups");
124 		if (setgid(pwd->pw_gid) != 0)
125 			err(1, "setgid");
126 		if (setusercontext(lcap, pwd, pwd->pw_uid,
127 		    LOGIN_SETALL & ~LOGIN_SETGROUP & ~LOGIN_SETLOGIN) != 0)
128 			err(1, "setusercontext");
129 		login_close(lcap);
130 	}
131 	if (execvp(argv[1], argv + 1) == -1)
132 		err(1, "execvp(): %s", argv[1]);
133 	exit(0);
134 }
135 
136 static void
137 usage(void)
138 {
139 
140 	fprintf(stderr, "%s\n",
141 		"usage: jexec [-u username | -U username] jail command ...");
142 	exit(1);
143 }
144