]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - lib/libjail/jail_getid.c
That's EN-19:17.ipfw. It's not an SA.
[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                 /*
56                  * jid == 0 is a special case; it will not appear in the
57                  * kernel's jail list, but naturally processes will be assigned
58                  * to it because it is prison 0.  Trivially return this one
59                  * without a trip to the kernel, because it always exists but
60                  * the lookup won't succeed.
61                  */
62                 if (jid == 0)
63                         return jid;
64                 jiov[0].iov_base = __DECONST(char *, "jid");
65                 jiov[0].iov_len = sizeof("jid");
66                 jiov[1].iov_base = &jid;
67                 jiov[1].iov_len = sizeof(jid);
68         } else {
69                 jiov[0].iov_base = __DECONST(char *, "name");
70                 jiov[0].iov_len = sizeof("name");
71                 jiov[1].iov_len = strlen(name) + 1;
72                 jiov[1].iov_base = alloca(jiov[1].iov_len);
73                 strcpy(jiov[1].iov_base, name);
74         }
75         jiov[2].iov_base = __DECONST(char *, "errmsg");
76         jiov[2].iov_len = sizeof("errmsg");
77         jiov[3].iov_base = jail_errmsg;
78         jiov[3].iov_len = JAIL_ERRMSGLEN;
79         jail_errmsg[0] = 0;
80         jid = jail_get(jiov, 4, 0);
81         if (jid < 0 && !jail_errmsg[0])
82                 snprintf(jail_errmsg, JAIL_ERRMSGLEN, "jail_get: %s",
83                     strerror(errno));
84         return jid;
85 }
86
87 /*
88  * Return the name corresponding to a JID.
89  */
90 char *
91 jail_getname(int jid)
92 {
93         struct iovec jiov[6];
94         char *name;
95         char namebuf[MAXHOSTNAMELEN];
96
97         jiov[0].iov_base = __DECONST(char *, "jid");
98         jiov[0].iov_len = sizeof("jid");
99         jiov[1].iov_base = &jid;
100         jiov[1].iov_len = sizeof(jid);
101         jiov[2].iov_base = __DECONST(char *, "name");
102         jiov[2].iov_len = sizeof("name");
103         jiov[3].iov_base = namebuf;
104         jiov[3].iov_len = sizeof(namebuf);
105         jiov[4].iov_base = __DECONST(char *, "errmsg");
106         jiov[4].iov_len = sizeof("errmsg");
107         jiov[5].iov_base = jail_errmsg;
108         jiov[5].iov_len = JAIL_ERRMSGLEN;
109         jail_errmsg[0] = 0;
110         jid = jail_get(jiov, 6, 0);
111         if (jid < 0) {
112                 if (!jail_errmsg[0])
113                         snprintf(jail_errmsg, JAIL_ERRMSGLEN, "jail_get: %s",
114                             strerror(errno));
115                 return NULL;
116         } else {
117                 name = strdup(namebuf);
118                 if (name == NULL)
119                         strerror_r(errno, jail_errmsg, JAIL_ERRMSGLEN);
120         }
121         return name;
122 }