1de6f3704SJamie Gritton /*- 2de6f3704SJamie Gritton * Copyright (c) 2009 James Gritton. 3de6f3704SJamie Gritton * All rights reserved. 4de6f3704SJamie Gritton * 5de6f3704SJamie Gritton * Redistribution and use in source and binary forms, with or without 6de6f3704SJamie Gritton * modification, are permitted provided that the following conditions 7de6f3704SJamie Gritton * are met: 8de6f3704SJamie Gritton * 1. Redistributions of source code must retain the above copyright 9de6f3704SJamie Gritton * notice, this list of conditions and the following disclaimer. 10de6f3704SJamie Gritton * 2. Redistributions in binary form must reproduce the above copyright 11de6f3704SJamie Gritton * notice, this list of conditions and the following disclaimer in the 12de6f3704SJamie Gritton * documentation and/or other materials provided with the distribution. 13de6f3704SJamie Gritton * 14de6f3704SJamie Gritton * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15de6f3704SJamie Gritton * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16de6f3704SJamie Gritton * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17de6f3704SJamie Gritton * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18de6f3704SJamie Gritton * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19de6f3704SJamie Gritton * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20de6f3704SJamie Gritton * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21de6f3704SJamie Gritton * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22de6f3704SJamie Gritton * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23de6f3704SJamie Gritton * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24de6f3704SJamie Gritton * SUCH DAMAGE. 25de6f3704SJamie Gritton */ 26de6f3704SJamie Gritton 27de6f3704SJamie Gritton #include <sys/cdefs.h> 28de6f3704SJamie Gritton __FBSDID("$FreeBSD$"); 29de6f3704SJamie Gritton 30de6f3704SJamie Gritton #include <sys/param.h> 31de6f3704SJamie Gritton #include <sys/types.h> 32de6f3704SJamie Gritton #include <sys/jail.h> 33de6f3704SJamie Gritton #include <sys/uio.h> 34de6f3704SJamie Gritton 35de6f3704SJamie Gritton #include <errno.h> 36de6f3704SJamie Gritton #include <stdio.h> 37de6f3704SJamie Gritton #include <stdlib.h> 38de6f3704SJamie Gritton #include <string.h> 39de6f3704SJamie Gritton 40de6f3704SJamie Gritton #include "jail.h" 41de6f3704SJamie Gritton 42de6f3704SJamie Gritton 43de6f3704SJamie Gritton /* 44de6f3704SJamie Gritton * Return the JID corresponding to a jail name. 45de6f3704SJamie Gritton */ 46de6f3704SJamie Gritton int 47de6f3704SJamie Gritton jail_getid(const char *name) 48de6f3704SJamie Gritton { 49de6f3704SJamie Gritton char *ep; 50de6f3704SJamie Gritton int jid; 51de6f3704SJamie Gritton struct iovec jiov[4]; 52de6f3704SJamie Gritton 53de6f3704SJamie Gritton jid = strtoul(name, &ep, 10); 54de6f3704SJamie Gritton if (*name && !*ep) 55de6f3704SJamie Gritton return jid; 56de6f3704SJamie Gritton *(const void **)&jiov[0].iov_base = "name"; 57de6f3704SJamie Gritton jiov[0].iov_len = sizeof("name"); 58de6f3704SJamie Gritton jiov[1].iov_len = strlen(name) + 1; 59de6f3704SJamie Gritton jiov[1].iov_base = alloca(jiov[1].iov_len); 60de6f3704SJamie Gritton strcpy(jiov[1].iov_base, name); 61de6f3704SJamie Gritton *(const void **)&jiov[2].iov_base = "errmsg"; 62de6f3704SJamie Gritton jiov[2].iov_len = sizeof("errmsg"); 63de6f3704SJamie Gritton jiov[3].iov_base = jail_errmsg; 64de6f3704SJamie Gritton jiov[3].iov_len = JAIL_ERRMSGLEN; 65de6f3704SJamie Gritton jail_errmsg[0] = 0; 66de6f3704SJamie Gritton jid = jail_get(jiov, 4, 0); 67de6f3704SJamie Gritton if (jid < 0 && !jail_errmsg[0]) 68de6f3704SJamie Gritton snprintf(jail_errmsg, JAIL_ERRMSGLEN, "jail_get: %s", 69de6f3704SJamie Gritton strerror(errno)); 70de6f3704SJamie Gritton return jid; 71de6f3704SJamie Gritton } 72de6f3704SJamie Gritton 73de6f3704SJamie Gritton /* 74de6f3704SJamie Gritton * Return the name corresponding to a JID. 75de6f3704SJamie Gritton */ 76de6f3704SJamie Gritton char * 77de6f3704SJamie Gritton jail_getname(int jid) 78de6f3704SJamie Gritton { 79de6f3704SJamie Gritton struct iovec jiov[6]; 80de6f3704SJamie Gritton char *name; 81de6f3704SJamie Gritton char namebuf[MAXHOSTNAMELEN]; 82de6f3704SJamie Gritton 83de6f3704SJamie Gritton *(const void **)&jiov[0].iov_base = "jid"; 84de6f3704SJamie Gritton jiov[0].iov_len = sizeof("jid"); 85de6f3704SJamie Gritton jiov[1].iov_base = &jid; 86de6f3704SJamie Gritton jiov[1].iov_len = sizeof(jid); 87de6f3704SJamie Gritton *(const void **)&jiov[2].iov_base = "name"; 88de6f3704SJamie Gritton jiov[2].iov_len = sizeof("name"); 89de6f3704SJamie Gritton jiov[3].iov_base = namebuf; 90de6f3704SJamie Gritton jiov[3].iov_len = sizeof(namebuf); 91de6f3704SJamie Gritton *(const void **)&jiov[4].iov_base = "errmsg"; 92de6f3704SJamie Gritton jiov[4].iov_len = sizeof("errmsg"); 93de6f3704SJamie Gritton jiov[5].iov_base = jail_errmsg; 94de6f3704SJamie Gritton jiov[5].iov_len = JAIL_ERRMSGLEN; 95de6f3704SJamie Gritton jail_errmsg[0] = 0; 96de6f3704SJamie Gritton jid = jail_get(jiov, 6, 0); 97*fba36ac4SJamie Gritton if (jid < 0) { 98*fba36ac4SJamie Gritton if (!jail_errmsg[0]) 99de6f3704SJamie Gritton snprintf(jail_errmsg, JAIL_ERRMSGLEN, "jail_get: %s", 100de6f3704SJamie Gritton strerror(errno)); 101*fba36ac4SJamie Gritton return NULL; 102*fba36ac4SJamie Gritton } else { 103de6f3704SJamie Gritton name = strdup(namebuf); 104de6f3704SJamie Gritton if (name == NULL) 105de6f3704SJamie Gritton strerror_r(errno, jail_errmsg, JAIL_ERRMSGLEN); 106*fba36ac4SJamie Gritton } 107de6f3704SJamie Gritton return name; 108de6f3704SJamie Gritton } 109