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