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/sysctl.h>
17 #include <netinet/in.h>
18 #include <arpa/inet.h>
23 #include <login_cap.h>
31 static void usage(void);
32 extern char **environ;
34 #define GET_USER_INFO do { \
35 pwd = getpwnam(username); \
38 err(1, "getpwnam: %s", username); \
40 errx(1, "%s: no such user", username); \
42 lcap = login_getpwclass(pwd); \
44 err(1, "getpwclass: %s", username); \
46 if (getgrouplist(username, pwd->pw_gid, groups, &ngroups) != 0) \
47 err(1, "getgrouplist: %s", username); \
51 main(int argc, char **argv)
53 login_cap_t *lcap = NULL;
55 struct passwd *pwd = NULL;
57 gid_t groups[NGROUPS];
58 int ch, i, iflag, Jflag, lflag, ngroups, securelevel, uflag, Uflag;
59 char path[PATH_MAX], *ep, *username, *JidFile;
60 static char *cleanenv;
61 const char *shell, *p = NULL;
65 iflag = Jflag = lflag = uflag = Uflag = 0;
67 username = JidFile = cleanenv = NULL;
70 while ((ch = getopt(argc, argv, "ils:u:U:J:")) != -1) {
80 ltmp = strtol(optarg, &ep, 0);
81 if (*ep || ep == optarg || ltmp > INT_MAX || !ltmp)
82 errx(1, "invalid securelevel: `%s'", optarg);
106 if (lflag && username == NULL)
110 if (realpath(argv[0], path) == NULL)
111 err(1, "realpath: %s", argv[0]);
112 if (chdir(path) != 0)
113 err(1, "chdir: %s", path);
114 memset(&j, 0, sizeof(j));
117 j.hostname = argv[1];
118 if (inet_aton(argv[2], &in) == 0)
119 errx(1, "Could not make sense of ip-number: %s", argv[2]);
120 j.ip_number = ntohl(in.s_addr);
122 fp = fopen(JidFile, "w");
124 errx(1, "Could not create JidFile: %s", JidFile);
135 fprintf(fp, "%d\t%s\t%s\t%s\t%s\n",
136 i, j.path, j.hostname, argv[2], argv[3]);
139 errx(1, "Could not write JidFile: %s", JidFile);
142 if (securelevel > 0) {
143 if (sysctlbyname("kern.securelevel", NULL, 0, &securelevel,
144 sizeof(securelevel)))
145 err(1, "Can not set securelevel to %d", securelevel);
147 if (username != NULL) {
154 if (setgroups(ngroups, groups) != 0)
156 if (setgid(pwd->pw_gid) != 0)
158 if (setusercontext(lcap, pwd, pwd->pw_uid,
159 LOGIN_SETALL & ~LOGIN_SETGROUP & ~LOGIN_SETLOGIN) != 0)
160 err(1, "setusercontext");
165 shell = pwd->pw_shell;
167 shell = _PATH_BSHELL;
168 if (chdir(pwd->pw_dir) < 0)
169 errx(1, "no home directory");
170 setenv("HOME", pwd->pw_dir, 1);
171 setenv("SHELL", shell, 1);
172 setenv("USER", pwd->pw_name, 1);
174 setenv("TERM", p, 1);
176 if (execv(argv[3], argv + 3) != 0)
177 err(1, "execv: %s", argv[3]);
185 (void)fprintf(stderr, "%s%s%s\n",
186 "usage: jail [-i] [-J jid_file] [-s securelevel] [-l -u ",
187 "username | -U username]",
188 " path hostname ip-number command ...");