]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - usr.sbin/jail/jail.c
This commit was generated by cvs2svn to compensate for changes in r152058,
[FreeBSD/FreeBSD.git] / usr.sbin / jail / jail.c
1 /*
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  * ----------------------------------------------------------------------------
8  */
9
10 #include <sys/cdefs.h>
11 __FBSDID("$FreeBSD$");
12
13 #include <sys/param.h>
14 #include <sys/jail.h>
15
16 #include <netinet/in.h>
17 #include <arpa/inet.h>
18
19 #include <err.h>
20 #include <errno.h>
21 #include <grp.h>
22 #include <login_cap.h>
23 #include <paths.h>
24 #include <pwd.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <unistd.h>
29
30 static void     usage(void);
31 extern char     **environ;
32
33 #define GET_USER_INFO do {                                              \
34         pwd = getpwnam(username);                                       \
35         if (pwd == NULL) {                                              \
36                 if (errno)                                              \
37                         err(1, "getpwnam: %s", username);               \
38                 else                                                    \
39                         errx(1, "%s: no such user", username);          \
40         }                                                               \
41         lcap = login_getpwclass(pwd);                                   \
42         if (lcap == NULL)                                               \
43                 err(1, "getpwclass: %s", username);                     \
44         ngroups = NGROUPS;                                              \
45         if (getgrouplist(username, pwd->pw_gid, groups, &ngroups) != 0) \
46                 err(1, "getgrouplist: %s", username);                   \
47 } while (0)
48
49 int
50 main(int argc, char **argv)
51 {
52         login_cap_t *lcap = NULL;
53         struct jail j;
54         struct passwd *pwd = NULL;
55         struct in_addr in;
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;
61
62         iflag = lflag = uflag = Uflag = 0;
63         username = cleanenv = NULL;
64
65         while ((ch = getopt(argc, argv, "ilu:U:")) != -1) {
66                 switch (ch) {
67                 case 'i':
68                         iflag = 1;
69                         break;
70                 case 'u':
71                         username = optarg;
72                         uflag = 1;
73                         break;
74                 case 'U':
75                         username = optarg;
76                         Uflag = 1;
77                         break;
78                 case 'l':
79                         lflag = 1;
80                         break;
81                 default:
82                         usage();
83                 }
84         }
85         argc -= optind;
86         argv += optind;
87         if (argc < 4)
88                 usage();
89         if (uflag && Uflag)
90                 usage();
91         if (lflag && username == NULL)
92                 usage();
93         if (uflag)
94                 GET_USER_INFO;
95         if (realpath(argv[0], path) == NULL)
96                 err(1, "realpath: %s", argv[0]);
97         if (chdir(path) != 0)
98                 err(1, "chdir: %s", path);
99         memset(&j, 0, sizeof(j));
100         j.version = 0;
101         j.path = path;
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);
106         i = jail(&j);
107         if (i == -1)
108                 err(1, "jail");
109         if (iflag) {
110                 printf("%d\n", i);
111                 fflush(stdout);
112         }
113         if (username != NULL) {
114                 if (Uflag)
115                         GET_USER_INFO;
116                 if (lflag) {
117                         p = getenv("TERM");
118                         environ = &cleanenv;
119                 }
120                 if (setgroups(ngroups, groups) != 0)
121                         err(1, "setgroups");
122                 if (setgid(pwd->pw_gid) != 0)
123                         err(1, "setgid");
124                 if (setusercontext(lcap, pwd, pwd->pw_uid,
125                     LOGIN_SETALL & ~LOGIN_SETGROUP) != 0)
126                         err(1, "setusercontext");
127                 login_close(lcap);
128         }
129         if (lflag) {
130                 if (*pwd->pw_shell)
131                         shell = pwd->pw_shell;
132                 else
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);
139                 if (p)
140                         setenv("TERM", p, 1);
141         }
142         if (execv(argv[3], argv + 3) != 0)
143                 err(1, "execv: %s", argv[3]);
144         exit(0);
145 }
146
147 static void
148 usage(void)
149 {
150
151         (void)fprintf(stderr, "%s%s\n",
152              "usage: jail [-i] [-l -u username | -U username]",
153              " path hostname ip-number command ...");
154         exit(1);
155 }