xref: /freebsd/lib/libjail/jail_getid.c (revision 5405b282e1f319b6f3597bb77f68be903e7f248c)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
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 		jiov[0].iov_base = __DECONST(char *, "jid");
58 		jiov[0].iov_len = sizeof("jid");
59 		jiov[1].iov_base = &jid;
60 		jiov[1].iov_len = sizeof(jid);
61 	} else {
62 		jiov[0].iov_base = __DECONST(char *, "name");
63 		jiov[0].iov_len = sizeof("name");
64 		jiov[1].iov_len = strlen(name) + 1;
65 		jiov[1].iov_base = alloca(jiov[1].iov_len);
66 		strcpy(jiov[1].iov_base, name);
67 	}
68 	jiov[2].iov_base = __DECONST(char *, "errmsg");
69 	jiov[2].iov_len = sizeof("errmsg");
70 	jiov[3].iov_base = jail_errmsg;
71 	jiov[3].iov_len = JAIL_ERRMSGLEN;
72 	jail_errmsg[0] = 0;
73 	jid = jail_get(jiov, 4, 0);
74 	if (jid < 0 && !jail_errmsg[0])
75 		snprintf(jail_errmsg, JAIL_ERRMSGLEN, "jail_get: %s",
76 		    strerror(errno));
77 	return jid;
78 }
79 
80 /*
81  * Return the name corresponding to a JID.
82  */
83 char *
84 jail_getname(int jid)
85 {
86 	struct iovec jiov[6];
87 	char *name;
88 	char namebuf[MAXHOSTNAMELEN];
89 
90 	jiov[0].iov_base = __DECONST(char *, "jid");
91 	jiov[0].iov_len = sizeof("jid");
92 	jiov[1].iov_base = &jid;
93 	jiov[1].iov_len = sizeof(jid);
94 	jiov[2].iov_base = __DECONST(char *, "name");
95 	jiov[2].iov_len = sizeof("name");
96 	jiov[3].iov_base = namebuf;
97 	jiov[3].iov_len = sizeof(namebuf);
98 	jiov[4].iov_base = __DECONST(char *, "errmsg");
99 	jiov[4].iov_len = sizeof("errmsg");
100 	jiov[5].iov_base = jail_errmsg;
101 	jiov[5].iov_len = JAIL_ERRMSGLEN;
102 	jail_errmsg[0] = 0;
103 	jid = jail_get(jiov, 6, 0);
104 	if (jid < 0) {
105 		if (!jail_errmsg[0])
106 			snprintf(jail_errmsg, JAIL_ERRMSGLEN, "jail_get: %s",
107 			    strerror(errno));
108 		return NULL;
109 	} else {
110 		name = strdup(namebuf);
111 		if (name == NULL)
112 			strerror_r(errno, jail_errmsg, JAIL_ERRMSGLEN);
113 	}
114 	return name;
115 }
116