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