1*86d7f5d3SJohn Marino /*-
2*86d7f5d3SJohn Marino * Copyright (c) 2004 Joerg Sonnenberger <joerg@bec.de>
3*86d7f5d3SJohn Marino * Copyright (c) 2003 Mike Barcroft <mike@FreeBSD.org>
4*86d7f5d3SJohn Marino * All rights reserved.
5*86d7f5d3SJohn Marino *
6*86d7f5d3SJohn Marino * Redistribution and use in source and binary forms, with or without
7*86d7f5d3SJohn Marino * modification, are permitted provided that the following conditions
8*86d7f5d3SJohn Marino * are met:
9*86d7f5d3SJohn Marino * 1. Redistributions of source code must retain the above copyright
10*86d7f5d3SJohn Marino * notice, this list of conditions and the following disclaimer.
11*86d7f5d3SJohn Marino * 2. Redistributions in binary form must reproduce the above copyright
12*86d7f5d3SJohn Marino * notice, this list of conditions and the following disclaimer in the
13*86d7f5d3SJohn Marino * documentation and/or other materials provided with the distribution.
14*86d7f5d3SJohn Marino *
15*86d7f5d3SJohn Marino * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16*86d7f5d3SJohn Marino * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17*86d7f5d3SJohn Marino * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18*86d7f5d3SJohn Marino * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19*86d7f5d3SJohn Marino * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20*86d7f5d3SJohn Marino * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21*86d7f5d3SJohn Marino * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22*86d7f5d3SJohn Marino * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23*86d7f5d3SJohn Marino * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24*86d7f5d3SJohn Marino * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25*86d7f5d3SJohn Marino * SUCH DAMAGE.
26*86d7f5d3SJohn Marino *
27*86d7f5d3SJohn Marino * $FreeBSD: src/usr.sbin/jexec/jexec.c,v 1.2 2003/07/04 19:14:27 bmilekic Exp $
28*86d7f5d3SJohn Marino * $DragonFly: src/usr.sbin/jexec/jexec.c,v 1.2 2006/08/22 13:02:30 joerg Exp $
29*86d7f5d3SJohn Marino */
30*86d7f5d3SJohn Marino
31*86d7f5d3SJohn Marino #include <sys/param.h>
32*86d7f5d3SJohn Marino #include <sys/jail.h>
33*86d7f5d3SJohn Marino
34*86d7f5d3SJohn Marino #include <err.h>
35*86d7f5d3SJohn Marino #include <errno.h>
36*86d7f5d3SJohn Marino #include <stdio.h>
37*86d7f5d3SJohn Marino #include <stdlib.h>
38*86d7f5d3SJohn Marino #include <unistd.h>
39*86d7f5d3SJohn Marino
40*86d7f5d3SJohn Marino static int getjailid(const char *str);
41*86d7f5d3SJohn Marino static void usage(void);
42*86d7f5d3SJohn Marino
43*86d7f5d3SJohn Marino int
main(int argc,char ** argv)44*86d7f5d3SJohn Marino main(int argc, char **argv)
45*86d7f5d3SJohn Marino {
46*86d7f5d3SJohn Marino int jid;
47*86d7f5d3SJohn Marino
48*86d7f5d3SJohn Marino if (argc < 3)
49*86d7f5d3SJohn Marino usage();
50*86d7f5d3SJohn Marino jid = getjailid(argv[1]);
51*86d7f5d3SJohn Marino if (jail_attach(jid) == -1)
52*86d7f5d3SJohn Marino err(1, "jail_attach(%d) failed", jid);
53*86d7f5d3SJohn Marino if (chdir("/") == -1)
54*86d7f5d3SJohn Marino err(1, "chdir(\"/\") failed");
55*86d7f5d3SJohn Marino if (execvp(argv[2], argv + 2) == -1)
56*86d7f5d3SJohn Marino err(1, "execvp(%s) failed", argv[2]);
57*86d7f5d3SJohn Marino exit(0);
58*86d7f5d3SJohn Marino }
59*86d7f5d3SJohn Marino
60*86d7f5d3SJohn Marino static void
usage(void)61*86d7f5d3SJohn Marino usage(void)
62*86d7f5d3SJohn Marino {
63*86d7f5d3SJohn Marino fprintf(stderr, "usage: jexec jid command [...]\n");
64*86d7f5d3SJohn Marino exit(1);
65*86d7f5d3SJohn Marino }
66*86d7f5d3SJohn Marino
67*86d7f5d3SJohn Marino static int
getjailid(const char * str)68*86d7f5d3SJohn Marino getjailid(const char *str)
69*86d7f5d3SJohn Marino {
70*86d7f5d3SJohn Marino long v;
71*86d7f5d3SJohn Marino char *ep;
72*86d7f5d3SJohn Marino
73*86d7f5d3SJohn Marino errno = 0;
74*86d7f5d3SJohn Marino v = strtol(str, &ep, 10);
75*86d7f5d3SJohn Marino if (v < INT_MIN || v > INT_MAX || errno == ERANGE)
76*86d7f5d3SJohn Marino errc(1, ERANGE, "invalid jail id: %s", str);
77*86d7f5d3SJohn Marino if (ep == str || *ep != '\0')
78*86d7f5d3SJohn Marino errx(1, "cannot parse jail id: %s.", str);
79*86d7f5d3SJohn Marino
80*86d7f5d3SJohn Marino return((int)(v));
81*86d7f5d3SJohn Marino }
82