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)
52 login_cap_t *lcap = NULL;
54 struct passwd *pwd = NULL;
56 gid_t groups[NGROUPS];
57 int ch, i, iflag, lflag, ngroups, uflag, Uflag;
58 char path[PATH_MAX], *username;
59 static char *cleanenv;
60 const char *shell, *p = NULL;
62 iflag = lflag = uflag = Uflag = 0;
63 username = cleanenv = NULL;
65 while ((ch = getopt(argc, argv, "ilu:U:")) != -1) {
91 if (lflag && username == NULL)
95 if (realpath(argv[0], path) == NULL)
96 err(1, "realpath: %s", argv[0]);
98 err(1, "chdir: %s", path);
99 memset(&j, 0, sizeof(j));
102 j.hostname = argv[1];
103 if (inet_aton(argv[2], &in) == 0)
104 errx(1, "Could not make sense of ip-number: %s", argv[2]);
105 j.ip_number = ntohl(in.s_addr);
113 if (username != NULL) {
120 if (setgroups(ngroups, groups) != 0)
122 if (setgid(pwd->pw_gid) != 0)
124 if (setusercontext(lcap, pwd, pwd->pw_uid,
125 LOGIN_SETALL & ~LOGIN_SETGROUP) != 0)
126 err(1, "setusercontext");
131 shell = pwd->pw_shell;
133 shell = _PATH_BSHELL;
134 if (chdir(pwd->pw_dir) < 0)
135 errx(1, "no home directory");
136 setenv("HOME", pwd->pw_dir, 1);
137 setenv("SHELL", shell, 1);
138 setenv("USER", pwd->pw_name, 1);
140 setenv("TERM", p, 1);
142 if (execv(argv[3], argv + 3) != 0)
143 err(1, "execv: %s", argv[3]);
151 (void)fprintf(stderr, "%s%s\n",
152 "usage: jail [-i] [-l -u username | -U username]",
153 " path hostname ip-number command ...");