1de6f3704SJamie Gritton /*- 25e53a4f9SPedro F. Giffuni * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 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/cdefs.h> 30de6f3704SJamie Gritton __FBSDID("$FreeBSD$"); 31de6f3704SJamie Gritton 32de6f3704SJamie Gritton #include <sys/param.h> 33de6f3704SJamie Gritton #include <sys/types.h> 34de6f3704SJamie Gritton #include <sys/jail.h> 35de6f3704SJamie Gritton #include <sys/uio.h> 36de6f3704SJamie Gritton 37de6f3704SJamie Gritton #include <errno.h> 38de6f3704SJamie Gritton #include <stdio.h> 39de6f3704SJamie Gritton #include <stdlib.h> 40de6f3704SJamie Gritton #include <string.h> 41de6f3704SJamie Gritton 42de6f3704SJamie Gritton #include "jail.h" 43de6f3704SJamie Gritton 44de6f3704SJamie Gritton 45de6f3704SJamie Gritton /* 46de6f3704SJamie Gritton * Return the JID corresponding to a jail name. 47de6f3704SJamie Gritton */ 48de6f3704SJamie Gritton int 49de6f3704SJamie Gritton jail_getid(const char *name) 50de6f3704SJamie Gritton { 51de6f3704SJamie Gritton char *ep; 52de6f3704SJamie Gritton int jid; 53de6f3704SJamie Gritton struct iovec jiov[4]; 54de6f3704SJamie Gritton 55de6f3704SJamie Gritton jid = strtoul(name, &ep, 10); 56a9cefdddSKyle Evans if (*name && !*ep) { 57*067498edSKyle Evans /* 58*067498edSKyle Evans * jid == 0 is a special case; it will not appear in the 59*067498edSKyle Evans * kernel's jail list, but naturally processes will be assigned 60*067498edSKyle Evans * to it because it is prison 0. Trivially return this one 61*067498edSKyle Evans * without a trip to the kernel, because it always exists but 62*067498edSKyle Evans * the lookup won't succeed. 63*067498edSKyle Evans */ 64*067498edSKyle Evans if (jid == 0) 65*067498edSKyle Evans return jid; 66a9cefdddSKyle Evans jiov[0].iov_base = __DECONST(char *, "jid"); 67a9cefdddSKyle Evans jiov[0].iov_len = sizeof("jid"); 68a9cefdddSKyle Evans jiov[1].iov_base = &jid; 69a9cefdddSKyle Evans jiov[1].iov_len = sizeof(jid); 70a9cefdddSKyle Evans } else { 71d031802bSJamie Gritton jiov[0].iov_base = __DECONST(char *, "name"); 72de6f3704SJamie Gritton jiov[0].iov_len = sizeof("name"); 73de6f3704SJamie Gritton jiov[1].iov_len = strlen(name) + 1; 74de6f3704SJamie Gritton jiov[1].iov_base = alloca(jiov[1].iov_len); 75de6f3704SJamie Gritton strcpy(jiov[1].iov_base, name); 76a9cefdddSKyle Evans } 77d031802bSJamie Gritton jiov[2].iov_base = __DECONST(char *, "errmsg"); 78de6f3704SJamie Gritton jiov[2].iov_len = sizeof("errmsg"); 79de6f3704SJamie Gritton jiov[3].iov_base = jail_errmsg; 80de6f3704SJamie Gritton jiov[3].iov_len = JAIL_ERRMSGLEN; 81de6f3704SJamie Gritton jail_errmsg[0] = 0; 82de6f3704SJamie Gritton jid = jail_get(jiov, 4, 0); 83de6f3704SJamie Gritton if (jid < 0 && !jail_errmsg[0]) 84de6f3704SJamie Gritton snprintf(jail_errmsg, JAIL_ERRMSGLEN, "jail_get: %s", 85de6f3704SJamie Gritton strerror(errno)); 86de6f3704SJamie Gritton return jid; 87de6f3704SJamie Gritton } 88de6f3704SJamie Gritton 89de6f3704SJamie Gritton /* 90de6f3704SJamie Gritton * Return the name corresponding to a JID. 91de6f3704SJamie Gritton */ 92de6f3704SJamie Gritton char * 93de6f3704SJamie Gritton jail_getname(int jid) 94de6f3704SJamie Gritton { 95de6f3704SJamie Gritton struct iovec jiov[6]; 96de6f3704SJamie Gritton char *name; 97de6f3704SJamie Gritton char namebuf[MAXHOSTNAMELEN]; 98de6f3704SJamie Gritton 99d031802bSJamie Gritton jiov[0].iov_base = __DECONST(char *, "jid"); 100de6f3704SJamie Gritton jiov[0].iov_len = sizeof("jid"); 101de6f3704SJamie Gritton jiov[1].iov_base = &jid; 102de6f3704SJamie Gritton jiov[1].iov_len = sizeof(jid); 103d031802bSJamie Gritton jiov[2].iov_base = __DECONST(char *, "name"); 104de6f3704SJamie Gritton jiov[2].iov_len = sizeof("name"); 105de6f3704SJamie Gritton jiov[3].iov_base = namebuf; 106de6f3704SJamie Gritton jiov[3].iov_len = sizeof(namebuf); 107d031802bSJamie Gritton jiov[4].iov_base = __DECONST(char *, "errmsg"); 108de6f3704SJamie Gritton jiov[4].iov_len = sizeof("errmsg"); 109de6f3704SJamie Gritton jiov[5].iov_base = jail_errmsg; 110de6f3704SJamie Gritton jiov[5].iov_len = JAIL_ERRMSGLEN; 111de6f3704SJamie Gritton jail_errmsg[0] = 0; 112de6f3704SJamie Gritton jid = jail_get(jiov, 6, 0); 113fba36ac4SJamie Gritton if (jid < 0) { 114fba36ac4SJamie Gritton if (!jail_errmsg[0]) 115de6f3704SJamie Gritton snprintf(jail_errmsg, JAIL_ERRMSGLEN, "jail_get: %s", 116de6f3704SJamie Gritton strerror(errno)); 117fba36ac4SJamie Gritton return NULL; 118fba36ac4SJamie Gritton } else { 119de6f3704SJamie Gritton name = strdup(namebuf); 120de6f3704SJamie Gritton if (name == NULL) 121de6f3704SJamie Gritton strerror_r(errno, jail_errmsg, JAIL_ERRMSGLEN); 122fba36ac4SJamie Gritton } 123de6f3704SJamie Gritton return name; 124de6f3704SJamie Gritton } 125