xref: /freebsd/lib/libjail/jail_getid.c (revision 783d3ff6d7fae619db8a7990b8a6387de0c677b5)
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/param.h>
30 #include <sys/types.h>
31 #include <sys/jail.h>
32 #include <sys/uio.h>
33 
34 #include <errno.h>
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <string.h>
38 
39 #include "jail.h"
40 
41 
42 /*
43  * Return the JID corresponding to a jail name.
44  */
45 int
46 jail_getid(const char *name)
47 {
48 	char *ep;
49 	int jid;
50 	struct iovec jiov[4];
51 
52 	jid = strtoul(name, &ep, 10);
53 	if (*name && !*ep) {
54 		/*
55 		 * jid == 0 is a special case; it will not appear in the
56 		 * kernel's jail list, but naturally processes will be assigned
57 		 * to it because it is prison 0.  Trivially return this one
58 		 * without a trip to the kernel, because it always exists but
59 		 * the lookup won't succeed.
60 		 */
61 		if (jid == 0)
62 			return jid;
63 		jiov[0].iov_base = __DECONST(char *, "jid");
64 		jiov[0].iov_len = sizeof("jid");
65 		jiov[1].iov_base = &jid;
66 		jiov[1].iov_len = sizeof(jid);
67 	} else {
68 		jiov[0].iov_base = __DECONST(char *, "name");
69 		jiov[0].iov_len = sizeof("name");
70 		jiov[1].iov_len = strlen(name) + 1;
71 		jiov[1].iov_base = alloca(jiov[1].iov_len);
72 		strcpy(jiov[1].iov_base, name);
73 	}
74 	jiov[2].iov_base = __DECONST(char *, "errmsg");
75 	jiov[2].iov_len = sizeof("errmsg");
76 	jiov[3].iov_base = jail_errmsg;
77 	jiov[3].iov_len = JAIL_ERRMSGLEN;
78 	jail_errmsg[0] = 0;
79 	jid = jail_get(jiov, 4, 0);
80 	if (jid < 0 && !jail_errmsg[0])
81 		snprintf(jail_errmsg, JAIL_ERRMSGLEN, "jail_get: %s",
82 		    strerror(errno));
83 	return jid;
84 }
85 
86 /*
87  * Return the name corresponding to a JID.
88  */
89 char *
90 jail_getname(int jid)
91 {
92 	struct iovec jiov[6];
93 	char *name;
94 	char namebuf[MAXHOSTNAMELEN];
95 
96 	jiov[0].iov_base = __DECONST(char *, "jid");
97 	jiov[0].iov_len = sizeof("jid");
98 	jiov[1].iov_base = &jid;
99 	jiov[1].iov_len = sizeof(jid);
100 	jiov[2].iov_base = __DECONST(char *, "name");
101 	jiov[2].iov_len = sizeof("name");
102 	jiov[3].iov_base = namebuf;
103 	jiov[3].iov_len = sizeof(namebuf);
104 	jiov[4].iov_base = __DECONST(char *, "errmsg");
105 	jiov[4].iov_len = sizeof("errmsg");
106 	jiov[5].iov_base = jail_errmsg;
107 	jiov[5].iov_len = JAIL_ERRMSGLEN;
108 	jail_errmsg[0] = 0;
109 	jid = jail_get(jiov, 6, 0);
110 	if (jid < 0) {
111 		if (!jail_errmsg[0])
112 			snprintf(jail_errmsg, JAIL_ERRMSGLEN, "jail_get: %s",
113 			    strerror(errno));
114 		return NULL;
115 	} else {
116 		name = strdup(namebuf);
117 		if (name == NULL)
118 			strerror_r(errno, jail_errmsg, JAIL_ERRMSGLEN);
119 	}
120 	return name;
121 }
122