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); 56*a9cefdddSKyle Evans if (*name && !*ep) { 57*a9cefdddSKyle Evans jiov[0].iov_base = __DECONST(char *, "jid"); 58*a9cefdddSKyle Evans jiov[0].iov_len = sizeof("jid"); 59*a9cefdddSKyle Evans jiov[1].iov_base = &jid; 60*a9cefdddSKyle Evans jiov[1].iov_len = sizeof(jid); 61*a9cefdddSKyle Evans } else { 62d031802bSJamie Gritton jiov[0].iov_base = __DECONST(char *, "name"); 63de6f3704SJamie Gritton jiov[0].iov_len = sizeof("name"); 64de6f3704SJamie Gritton jiov[1].iov_len = strlen(name) + 1; 65de6f3704SJamie Gritton jiov[1].iov_base = alloca(jiov[1].iov_len); 66de6f3704SJamie Gritton strcpy(jiov[1].iov_base, name); 67*a9cefdddSKyle Evans } 68d031802bSJamie Gritton jiov[2].iov_base = __DECONST(char *, "errmsg"); 69de6f3704SJamie Gritton jiov[2].iov_len = sizeof("errmsg"); 70de6f3704SJamie Gritton jiov[3].iov_base = jail_errmsg; 71de6f3704SJamie Gritton jiov[3].iov_len = JAIL_ERRMSGLEN; 72de6f3704SJamie Gritton jail_errmsg[0] = 0; 73de6f3704SJamie Gritton jid = jail_get(jiov, 4, 0); 74de6f3704SJamie Gritton if (jid < 0 && !jail_errmsg[0]) 75de6f3704SJamie Gritton snprintf(jail_errmsg, JAIL_ERRMSGLEN, "jail_get: %s", 76de6f3704SJamie Gritton strerror(errno)); 77de6f3704SJamie Gritton return jid; 78de6f3704SJamie Gritton } 79de6f3704SJamie Gritton 80de6f3704SJamie Gritton /* 81de6f3704SJamie Gritton * Return the name corresponding to a JID. 82de6f3704SJamie Gritton */ 83de6f3704SJamie Gritton char * 84de6f3704SJamie Gritton jail_getname(int jid) 85de6f3704SJamie Gritton { 86de6f3704SJamie Gritton struct iovec jiov[6]; 87de6f3704SJamie Gritton char *name; 88de6f3704SJamie Gritton char namebuf[MAXHOSTNAMELEN]; 89de6f3704SJamie Gritton 90d031802bSJamie Gritton jiov[0].iov_base = __DECONST(char *, "jid"); 91de6f3704SJamie Gritton jiov[0].iov_len = sizeof("jid"); 92de6f3704SJamie Gritton jiov[1].iov_base = &jid; 93de6f3704SJamie Gritton jiov[1].iov_len = sizeof(jid); 94d031802bSJamie Gritton jiov[2].iov_base = __DECONST(char *, "name"); 95de6f3704SJamie Gritton jiov[2].iov_len = sizeof("name"); 96de6f3704SJamie Gritton jiov[3].iov_base = namebuf; 97de6f3704SJamie Gritton jiov[3].iov_len = sizeof(namebuf); 98d031802bSJamie Gritton jiov[4].iov_base = __DECONST(char *, "errmsg"); 99de6f3704SJamie Gritton jiov[4].iov_len = sizeof("errmsg"); 100de6f3704SJamie Gritton jiov[5].iov_base = jail_errmsg; 101de6f3704SJamie Gritton jiov[5].iov_len = JAIL_ERRMSGLEN; 102de6f3704SJamie Gritton jail_errmsg[0] = 0; 103de6f3704SJamie Gritton jid = jail_get(jiov, 6, 0); 104fba36ac4SJamie Gritton if (jid < 0) { 105fba36ac4SJamie Gritton if (!jail_errmsg[0]) 106de6f3704SJamie Gritton snprintf(jail_errmsg, JAIL_ERRMSGLEN, "jail_get: %s", 107de6f3704SJamie Gritton strerror(errno)); 108fba36ac4SJamie Gritton return NULL; 109fba36ac4SJamie Gritton } else { 110de6f3704SJamie Gritton name = strdup(namebuf); 111de6f3704SJamie Gritton if (name == NULL) 112de6f3704SJamie Gritton strerror_r(errno, jail_errmsg, JAIL_ERRMSGLEN); 113fba36ac4SJamie Gritton } 114de6f3704SJamie Gritton return name; 115de6f3704SJamie Gritton } 116