]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - 6/usr.sbin/jail/jail.c
Clone Kip's Xen on stable/6 tree so that I can work on improving FreeBSD/amd64
[FreeBSD/FreeBSD.git] / 6 / 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 #include <sys/sysctl.h>
16
17 #include <netinet/in.h>
18 #include <arpa/inet.h>
19
20 #include <err.h>
21 #include <errno.h>
22 #include <grp.h>
23 #include <login_cap.h>
24 #include <paths.h>
25 #include <pwd.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <unistd.h>
30
31 static void     usage(void);
32 extern char     **environ;
33
34 #define GET_USER_INFO do {                                              \
35         pwd = getpwnam(username);                                       \
36         if (pwd == NULL) {                                              \
37                 if (errno)                                              \
38                         err(1, "getpwnam: %s", username);               \
39                 else                                                    \
40                         errx(1, "%s: no such user", username);          \
41         }                                                               \
42         lcap = login_getpwclass(pwd);                                   \
43         if (lcap == NULL)                                               \
44                 err(1, "getpwclass: %s", username);                     \
45         ngroups = NGROUPS;                                              \
46         if (getgrouplist(username, pwd->pw_gid, groups, &ngroups) != 0) \
47                 err(1, "getgrouplist: %s", username);                   \
48 } while (0)
49
50 int
51 main(int argc, char **argv)
52 {
53         login_cap_t *lcap = NULL;
54         struct jail j;
55         struct passwd *pwd = NULL;
56         struct in_addr in;
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;
62         long ltmp;
63         FILE *fp;
64
65         iflag = Jflag = lflag = uflag = Uflag = 0;
66         securelevel = -1;
67         username = JidFile = cleanenv = NULL;
68         fp = NULL;
69
70         while ((ch = getopt(argc, argv, "ils:u:U:J:")) != -1) {
71                 switch (ch) {
72                 case 'i':
73                         iflag = 1;
74                         break;
75                 case 'J':
76                         JidFile = optarg;
77                         Jflag = 1;
78                         break;
79                 case 's':
80                         ltmp = strtol(optarg, &ep, 0);
81                         if (*ep || ep == optarg || ltmp > INT_MAX || !ltmp)
82                                 errx(1, "invalid securelevel: `%s'", optarg);
83                         securelevel = ltmp;
84                         break;
85                 case 'u':
86                         username = optarg;
87                         uflag = 1;
88                         break;
89                 case 'U':
90                         username = optarg;
91                         Uflag = 1;
92                         break;
93                 case 'l':
94                         lflag = 1;
95                         break;
96                 default:
97                         usage();
98                 }
99         }
100         argc -= optind;
101         argv += optind;
102         if (argc < 4)
103                 usage();
104         if (uflag && Uflag)
105                 usage();
106         if (lflag && username == NULL)
107                 usage();
108         if (uflag)
109                 GET_USER_INFO;
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));
115         j.version = 0;
116         j.path = path;
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);
121         if (Jflag) {
122                 fp = fopen(JidFile, "w");
123                 if (fp == NULL)
124                         errx(1, "Could not create JidFile: %s", JidFile);
125         }
126         i = jail(&j);
127         if (i == -1)
128                 err(1, "jail");
129         if (iflag) {
130                 printf("%d\n", i);
131                 fflush(stdout);
132         }
133         if (Jflag) {
134                 if (fp != NULL) {
135                         fprintf(fp, "%d\t%s\t%s\t%s\t%s\n",
136                             i, j.path, j.hostname, argv[2], argv[3]);
137                         (void)fclose(fp);
138                 } else {
139                         errx(1, "Could not write JidFile: %s", JidFile);
140                 }
141         }
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);
146         }
147         if (username != NULL) {
148                 if (Uflag)
149                         GET_USER_INFO;
150                 if (lflag) {
151                         p = getenv("TERM");
152                         environ = &cleanenv;
153                 }
154                 if (setgroups(ngroups, groups) != 0)
155                         err(1, "setgroups");
156                 if (setgid(pwd->pw_gid) != 0)
157                         err(1, "setgid");
158                 if (setusercontext(lcap, pwd, pwd->pw_uid,
159                     LOGIN_SETALL & ~LOGIN_SETGROUP & ~LOGIN_SETLOGIN) != 0)
160                         err(1, "setusercontext");
161                 login_close(lcap);
162         }
163         if (lflag) {
164                 if (*pwd->pw_shell)
165                         shell = pwd->pw_shell;
166                 else
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);
173                 if (p)
174                         setenv("TERM", p, 1);
175         }
176         if (execv(argv[3], argv + 3) != 0)
177                 err(1, "execv: %s", argv[3]);
178         exit(0);
179 }
180
181 static void
182 usage(void)
183 {
184
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 ...");
189         exit(1);
190 }