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