Add the DragonFly cvs id and perform general cleanups on cvs/rcs/sccs ids. Most
[dragonfly.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  * $FreeBSD: src/usr.sbin/jail/jail.c,v 1.5.2.2 2003/05/08 13:04:24 maxim Exp $
10  * $DragonFly: src/usr.sbin/jail/jail.c,v 1.2 2003/06/17 04:29:55 dillon Exp $
11  * 
12  */
13
14 #include <sys/param.h>
15 #include <sys/jail.h>
16
17 #include <netinet/in.h>
18 #include <arpa/inet.h>
19
20 #include <err.h>
21 #include <grp.h>
22 #include <login_cap.h>
23 #include <pwd.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <unistd.h>
28
29 static void     usage(void);
30
31 int
32 main(int argc, char **argv)
33 {
34         login_cap_t *lcap;
35         struct jail j;
36         struct passwd *pwd;
37         struct in_addr in;
38         int ch, groups[NGROUPS], ngroups;
39         char *username;
40
41         username = NULL;
42
43         while ((ch = getopt(argc, argv, "u:")) != -1)
44                 switch (ch) {
45                 case 'u':
46                         username = optarg;
47                         break;
48                 default:
49                         usage();
50                         break;
51                 }
52         argc -= optind;
53         argv += optind;
54         if (argc < 4)
55                 usage();
56
57         if (username != NULL) {
58                 pwd = getpwnam(username);
59                 if (pwd == NULL)
60                         err(1, "getpwnam: %s", username);
61                 lcap = login_getpwclass(pwd);
62                 if (lcap == NULL)
63                         err(1, "getpwclass: %s", username);
64                 ngroups = NGROUPS;
65                 if (getgrouplist(username, pwd->pw_gid, groups, &ngroups) != 0)
66                         err(1, "getgrouplist: %s", username);
67         }
68         if (chdir(argv[0]) != 0)
69                 err(1, "chdir: %s", argv[0]);
70         memset(&j, 0, sizeof(j));
71         j.version = 0;
72         j.path = argv[0];
73         j.hostname = argv[1];
74         if (inet_aton(argv[2], &in) == 0)
75                 errx(1, "Could not make sense of ip-number: %s", argv[2]);
76         j.ip_number = ntohl(in.s_addr);
77         if (jail(&j) != 0)
78                 err(1, "jail");
79         if (username != NULL) {
80                 if (setgroups(ngroups, groups) != 0)
81                         err(1, "setgroups");
82                 if (setgid(pwd->pw_gid) != 0)
83                         err(1, "setgid");
84                 if (setusercontext(lcap, pwd, pwd->pw_uid,
85                     LOGIN_SETALL & ~LOGIN_SETGROUP) != 0)
86                         err(1, "setusercontext");
87                 login_close(lcap);
88         }
89         if (execv(argv[3], argv + 3) != 0)
90                 err(1, "execv: %s", argv[3]);
91         exit (0);
92 }
93
94 static void
95 usage(void)
96 {
97
98         (void)fprintf(stderr, "%s\n",
99             "Usage: jail [-u username] path hostname ip-number command ...");
100         exit(1);
101 }