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, Jflag, lflag, ngroups, uflag, Uflag;
58 char path[PATH_MAX], *username, *JidFile;
59 static char *cleanenv;
60 const char *shell, *p = NULL;
63 iflag = Jflag = lflag = uflag = Uflag = 0;
64 username = JidFile = cleanenv = NULL;
67 while ((ch = getopt(argc, argv, "ilu:U:J:")) != -1) {
97 if (lflag && username == NULL)
101 if (realpath(argv[0], path) == NULL)
102 err(1, "realpath: %s", argv[0]);
103 if (chdir(path) != 0)
104 err(1, "chdir: %s", path);
105 memset(&j, 0, sizeof(j));
108 j.hostname = argv[1];
109 if (inet_aton(argv[2], &in) == 0)
110 errx(1, "Could not make sense of ip-number: %s", argv[2]);
111 j.ip_number = ntohl(in.s_addr);
113 fp = fopen(JidFile, "w");
115 errx(1, "Could not create JidFile: %s", JidFile);
126 fprintf(fp, "%d\t%s\t%s\t%s\t%s\n",
127 i, j.path, j.hostname, argv[2], argv[3]);
130 errx(1, "Could not write JidFile: %s", JidFile);
133 if (username != NULL) {
140 if (setgroups(ngroups, groups) != 0)
142 if (setgid(pwd->pw_gid) != 0)
144 if (setusercontext(lcap, pwd, pwd->pw_uid,
145 LOGIN_SETALL & ~LOGIN_SETGROUP) != 0)
146 err(1, "setusercontext");
151 shell = pwd->pw_shell;
153 shell = _PATH_BSHELL;
154 if (chdir(pwd->pw_dir) < 0)
155 errx(1, "no home directory");
156 setenv("HOME", pwd->pw_dir, 1);
157 setenv("SHELL", shell, 1);
158 setenv("USER", pwd->pw_name, 1);
160 setenv("TERM", p, 1);
162 if (execv(argv[3], argv + 3) != 0)
163 err(1, "execv: %s", argv[3]);
171 (void)fprintf(stderr, "%s%s\n",
172 "usage: jail [-i] [-J jid_file] [-l -u username | -U username]",
173 " path hostname ip-number command ...");