1de6f3704SJamie Gritton /*-
2*4d846d26SWarner Losh * SPDX-License-Identifier: BSD-2-Clause
35e53a4f9SPedro F. Giffuni *
4de6f3704SJamie Gritton * Copyright (c) 2009 James Gritton.
5de6f3704SJamie Gritton * All rights reserved.
6de6f3704SJamie Gritton *
7de6f3704SJamie Gritton * Redistribution and use in source and binary forms, with or without
8de6f3704SJamie Gritton * modification, are permitted provided that the following conditions
9de6f3704SJamie Gritton * are met:
10de6f3704SJamie Gritton * 1. Redistributions of source code must retain the above copyright
11de6f3704SJamie Gritton * notice, this list of conditions and the following disclaimer.
12de6f3704SJamie Gritton * 2. Redistributions in binary form must reproduce the above copyright
13de6f3704SJamie Gritton * notice, this list of conditions and the following disclaimer in the
14de6f3704SJamie Gritton * documentation and/or other materials provided with the distribution.
15de6f3704SJamie Gritton *
16de6f3704SJamie Gritton * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17de6f3704SJamie Gritton * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18de6f3704SJamie Gritton * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19de6f3704SJamie Gritton * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20de6f3704SJamie Gritton * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21de6f3704SJamie Gritton * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22de6f3704SJamie Gritton * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23de6f3704SJamie Gritton * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24de6f3704SJamie Gritton * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25de6f3704SJamie Gritton * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26de6f3704SJamie Gritton * SUCH DAMAGE.
27de6f3704SJamie Gritton */
28de6f3704SJamie Gritton
29de6f3704SJamie Gritton #include <sys/param.h>
30de6f3704SJamie Gritton #include <sys/types.h>
31de6f3704SJamie Gritton #include <sys/jail.h>
32de6f3704SJamie Gritton #include <sys/uio.h>
33de6f3704SJamie Gritton
34de6f3704SJamie Gritton #include <errno.h>
35de6f3704SJamie Gritton #include <stdio.h>
36de6f3704SJamie Gritton #include <stdlib.h>
37de6f3704SJamie Gritton #include <string.h>
38de6f3704SJamie Gritton
39de6f3704SJamie Gritton #include "jail.h"
40de6f3704SJamie Gritton
41de6f3704SJamie Gritton
42de6f3704SJamie Gritton /*
43de6f3704SJamie Gritton * Return the JID corresponding to a jail name.
44de6f3704SJamie Gritton */
45de6f3704SJamie Gritton int
jail_getid(const char * name)46de6f3704SJamie Gritton jail_getid(const char *name)
47de6f3704SJamie Gritton {
48de6f3704SJamie Gritton char *ep;
49de6f3704SJamie Gritton int jid;
50de6f3704SJamie Gritton struct iovec jiov[4];
51de6f3704SJamie Gritton
52de6f3704SJamie Gritton jid = strtoul(name, &ep, 10);
53a9cefdddSKyle Evans if (*name && !*ep) {
54067498edSKyle Evans /*
55067498edSKyle Evans * jid == 0 is a special case; it will not appear in the
56067498edSKyle Evans * kernel's jail list, but naturally processes will be assigned
57067498edSKyle Evans * to it because it is prison 0. Trivially return this one
58067498edSKyle Evans * without a trip to the kernel, because it always exists but
59067498edSKyle Evans * the lookup won't succeed.
60067498edSKyle Evans */
61067498edSKyle Evans if (jid == 0)
62067498edSKyle Evans return jid;
63a9cefdddSKyle Evans jiov[0].iov_base = __DECONST(char *, "jid");
64a9cefdddSKyle Evans jiov[0].iov_len = sizeof("jid");
65a9cefdddSKyle Evans jiov[1].iov_base = &jid;
66a9cefdddSKyle Evans jiov[1].iov_len = sizeof(jid);
67a9cefdddSKyle Evans } else {
68d031802bSJamie Gritton jiov[0].iov_base = __DECONST(char *, "name");
69de6f3704SJamie Gritton jiov[0].iov_len = sizeof("name");
70de6f3704SJamie Gritton jiov[1].iov_len = strlen(name) + 1;
71de6f3704SJamie Gritton jiov[1].iov_base = alloca(jiov[1].iov_len);
72de6f3704SJamie Gritton strcpy(jiov[1].iov_base, name);
73a9cefdddSKyle Evans }
74d031802bSJamie Gritton jiov[2].iov_base = __DECONST(char *, "errmsg");
75de6f3704SJamie Gritton jiov[2].iov_len = sizeof("errmsg");
76de6f3704SJamie Gritton jiov[3].iov_base = jail_errmsg;
77de6f3704SJamie Gritton jiov[3].iov_len = JAIL_ERRMSGLEN;
78de6f3704SJamie Gritton jail_errmsg[0] = 0;
79de6f3704SJamie Gritton jid = jail_get(jiov, 4, 0);
80de6f3704SJamie Gritton if (jid < 0 && !jail_errmsg[0])
81de6f3704SJamie Gritton snprintf(jail_errmsg, JAIL_ERRMSGLEN, "jail_get: %s",
82de6f3704SJamie Gritton strerror(errno));
83de6f3704SJamie Gritton return jid;
84de6f3704SJamie Gritton }
85de6f3704SJamie Gritton
86de6f3704SJamie Gritton /*
87de6f3704SJamie Gritton * Return the name corresponding to a JID.
88de6f3704SJamie Gritton */
89de6f3704SJamie Gritton char *
jail_getname(int jid)90de6f3704SJamie Gritton jail_getname(int jid)
91de6f3704SJamie Gritton {
92de6f3704SJamie Gritton struct iovec jiov[6];
93de6f3704SJamie Gritton char *name;
94de6f3704SJamie Gritton char namebuf[MAXHOSTNAMELEN];
95de6f3704SJamie Gritton
96d031802bSJamie Gritton jiov[0].iov_base = __DECONST(char *, "jid");
97de6f3704SJamie Gritton jiov[0].iov_len = sizeof("jid");
98de6f3704SJamie Gritton jiov[1].iov_base = &jid;
99de6f3704SJamie Gritton jiov[1].iov_len = sizeof(jid);
100d031802bSJamie Gritton jiov[2].iov_base = __DECONST(char *, "name");
101de6f3704SJamie Gritton jiov[2].iov_len = sizeof("name");
102de6f3704SJamie Gritton jiov[3].iov_base = namebuf;
103de6f3704SJamie Gritton jiov[3].iov_len = sizeof(namebuf);
104d031802bSJamie Gritton jiov[4].iov_base = __DECONST(char *, "errmsg");
105de6f3704SJamie Gritton jiov[4].iov_len = sizeof("errmsg");
106de6f3704SJamie Gritton jiov[5].iov_base = jail_errmsg;
107de6f3704SJamie Gritton jiov[5].iov_len = JAIL_ERRMSGLEN;
108de6f3704SJamie Gritton jail_errmsg[0] = 0;
109de6f3704SJamie Gritton jid = jail_get(jiov, 6, 0);
110fba36ac4SJamie Gritton if (jid < 0) {
111fba36ac4SJamie Gritton if (!jail_errmsg[0])
112de6f3704SJamie Gritton snprintf(jail_errmsg, JAIL_ERRMSGLEN, "jail_get: %s",
113de6f3704SJamie Gritton strerror(errno));
114fba36ac4SJamie Gritton return NULL;
115fba36ac4SJamie Gritton } else {
116de6f3704SJamie Gritton name = strdup(namebuf);
117de6f3704SJamie Gritton if (name == NULL)
118de6f3704SJamie Gritton strerror_r(errno, jail_errmsg, JAIL_ERRMSGLEN);
119fba36ac4SJamie Gritton }
120de6f3704SJamie Gritton return name;
121de6f3704SJamie Gritton }
122