1194869Sjamie/*-
2194869Sjamie * Copyright (c) 2009 James Gritton.
3194869Sjamie * All rights reserved.
4194869Sjamie *
5194869Sjamie * Redistribution and use in source and binary forms, with or without
6194869Sjamie * modification, are permitted provided that the following conditions
7194869Sjamie * are met:
8194869Sjamie * 1. Redistributions of source code must retain the above copyright
9194869Sjamie *    notice, this list of conditions and the following disclaimer.
10194869Sjamie * 2. Redistributions in binary form must reproduce the above copyright
11194869Sjamie *    notice, this list of conditions and the following disclaimer in the
12194869Sjamie *    documentation and/or other materials provided with the distribution.
13194869Sjamie *
14194869Sjamie * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15194869Sjamie * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16194869Sjamie * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17194869Sjamie * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18194869Sjamie * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19194869Sjamie * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20194869Sjamie * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21194869Sjamie * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22194869Sjamie * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23194869Sjamie * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24194869Sjamie * SUCH DAMAGE.
25194869Sjamie */
26194869Sjamie
27194869Sjamie#include <sys/cdefs.h>
28194869Sjamie__FBSDID("$FreeBSD$");
29194869Sjamie
30194869Sjamie#include <sys/param.h>
31194869Sjamie#include <sys/types.h>
32194869Sjamie#include <sys/jail.h>
33194869Sjamie#include <sys/uio.h>
34194869Sjamie
35194869Sjamie#include <errno.h>
36194869Sjamie#include <stdio.h>
37194869Sjamie#include <stdlib.h>
38194869Sjamie#include <string.h>
39194869Sjamie
40194869Sjamie#include "jail.h"
41194869Sjamie
42194869Sjamie
43194869Sjamie/*
44194869Sjamie * Return the JID corresponding to a jail name.
45194869Sjamie */
46194869Sjamieint
47194869Sjamiejail_getid(const char *name)
48194869Sjamie{
49194869Sjamie	char *ep;
50194869Sjamie	int jid;
51194869Sjamie	struct iovec jiov[4];
52194869Sjamie
53194869Sjamie	jid = strtoul(name, &ep, 10);
54194869Sjamie	if (*name && !*ep)
55194869Sjamie		return jid;
56194869Sjamie	*(const void **)&jiov[0].iov_base = "name";
57194869Sjamie	jiov[0].iov_len = sizeof("name");
58194869Sjamie	jiov[1].iov_len = strlen(name) + 1;
59194869Sjamie	jiov[1].iov_base = alloca(jiov[1].iov_len);
60194869Sjamie	strcpy(jiov[1].iov_base, name);
61194869Sjamie	*(const void **)&jiov[2].iov_base = "errmsg";
62194869Sjamie	jiov[2].iov_len = sizeof("errmsg");
63194869Sjamie	jiov[3].iov_base = jail_errmsg;
64194869Sjamie	jiov[3].iov_len = JAIL_ERRMSGLEN;
65194869Sjamie	jail_errmsg[0] = 0;
66194869Sjamie	jid = jail_get(jiov, 4, 0);
67194869Sjamie	if (jid < 0 && !jail_errmsg[0])
68194869Sjamie		snprintf(jail_errmsg, JAIL_ERRMSGLEN, "jail_get: %s",
69194869Sjamie		    strerror(errno));
70194869Sjamie	return jid;
71194869Sjamie}
72194869Sjamie
73194869Sjamie/*
74194869Sjamie * Return the name corresponding to a JID.
75194869Sjamie */
76194869Sjamiechar *
77194869Sjamiejail_getname(int jid)
78194869Sjamie{
79194869Sjamie	struct iovec jiov[6];
80194869Sjamie	char *name;
81194869Sjamie	char namebuf[MAXHOSTNAMELEN];
82194869Sjamie
83194869Sjamie	*(const void **)&jiov[0].iov_base = "jid";
84194869Sjamie	jiov[0].iov_len = sizeof("jid");
85194869Sjamie	jiov[1].iov_base = &jid;
86194869Sjamie	jiov[1].iov_len = sizeof(jid);
87194869Sjamie	*(const void **)&jiov[2].iov_base = "name";
88194869Sjamie	jiov[2].iov_len = sizeof("name");
89194869Sjamie	jiov[3].iov_base = namebuf;
90194869Sjamie	jiov[3].iov_len = sizeof(namebuf);
91194869Sjamie	*(const void **)&jiov[4].iov_base = "errmsg";
92194869Sjamie	jiov[4].iov_len = sizeof("errmsg");
93194869Sjamie	jiov[5].iov_base = jail_errmsg;
94194869Sjamie	jiov[5].iov_len = JAIL_ERRMSGLEN;
95194869Sjamie	jail_errmsg[0] = 0;
96194869Sjamie	jid = jail_get(jiov, 6, 0);
97210134Sjamie	if (jid < 0) {
98210134Sjamie		if (!jail_errmsg[0])
99210134Sjamie			snprintf(jail_errmsg, JAIL_ERRMSGLEN, "jail_get: %s",
100210134Sjamie			    strerror(errno));
101210134Sjamie		return NULL;
102210134Sjamie	} else {
103210134Sjamie		name = strdup(namebuf);
104210134Sjamie		if (name == NULL)
105210134Sjamie			strerror_r(errno, jail_errmsg, JAIL_ERRMSGLEN);
106210134Sjamie	}
107194869Sjamie	return name;
108194869Sjamie}
109