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>
16 #include <netinet/in.h>
17 #include <arpa/inet.h>
22 #include <login_cap.h>
30 static void usage(void);
31 extern char **environ;
33 #define GET_USER_INFO do { \
34 pwd = getpwnam(username); \
37 err(1, "getpwnam: %s", username); \
39 errx(1, "%s: no such user", username); \
41 lcap = login_getpwclass(pwd); \
43 err(1, "getpwclass: %s", username); \
45 if (getgrouplist(username, pwd->pw_gid, groups, &ngroups) != 0) \
46 err(1, "getgrouplist: %s", username); \
50 main(int argc, char **argv)
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;
61 iflag = lflag = uflag = Uflag = 0;
62 username = cleanenv = NULL;
64 while ((ch = getopt(argc, argv, "ilu:U:")) != -1) {
90 if (lflag && username == NULL)
94 if (realpath(argv[0], path) == NULL)
95 err(1, "realpath: %s", argv[0]);
97 err(1, "chdir: %s", path);
98 memset(&j, 0, sizeof(j));
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);
112 if (username != NULL) {
119 if (setgroups(ngroups, groups) != 0)
121 if (setgid(pwd->pw_gid) != 0)
123 if (setusercontext(lcap, pwd, pwd->pw_uid,
124 LOGIN_SETALL & ~LOGIN_SETGROUP) != 0)
125 err(1, "setusercontext");
130 shell = pwd->pw_shell;
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);
139 setenv("TERM", p, 1);
141 if (execv(argv[3], argv + 3) != 0)
142 err(1, "execv: %s", argv[3]);
150 (void)fprintf(stderr, "%s%s\n",
151 "usage: jail [-i] [-l -u username | -U username]",
152 " path hostname ip-number command ...");