codec: omxil: use switch lut for string conversion
[vlc.git] / bin / rootwrap.c
blob0258773b90751d2ed4c05a20d78ddbcc8b30938f
1 /*****************************************************************************
2 * rootwrap.c
3 *****************************************************************************
4 * Copyright © 2005-2008 Rémi Denis-Courmont
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
19 *****************************************************************************/
21 #ifdef HAVE_CONFIG_H
22 # include <config.h>
23 #endif
24 #define _XPG4_2 /* ancilliary data on Solaris */
26 #include <stdlib.h> /* exit() */
27 #include <stdio.h>
28 #include <string.h>
30 #include <sys/types.h>
31 #include <unistd.h>
32 #include <fcntl.h>
33 #include <sys/stat.h>
34 #include <sys/socket.h>
35 #include <sys/uio.h>
36 #include <sys/resource.h> /* getrlimit() */
37 #ifdef RLIMIT_RTPRIO
38 #include <sched.h>
39 #endif
40 #include <errno.h>
41 #include <netinet/in.h>
43 #if defined (AF_INET6) && !defined (IPV6_V6ONLY)
44 # warning Uho, your IPv6 support is broken and has been disabled. Fix your C library.
45 # undef AF_INET6
46 #endif
47 #ifndef AF_LOCAL
48 # define AF_LOCAL AF_UNIX
49 #endif
50 #if !defined(MSG_NOSIGNAL) && defined(SO_NOSIGPIPE)
51 # define MSG_NOSIGNAL 0
52 #endif
53 /* Required yet non-standard cmsg functions */
54 #ifndef CMSG_ALIGN
55 # define CMSG_ALIGN(len) (((len) + sizeof(intptr_t)-1) & ~(sizeof(intptr_t)-1))
56 #endif
57 #ifndef CMSG_SPACE
58 # define CMSG_SPACE(len) (CMSG_ALIGN(sizeof(struct cmsghdr)) + CMSG_ALIGN(len))
59 #endif
60 #ifndef CMSG_LEN
61 # define CMSG_LEN(len) (CMSG_ALIGN(sizeof(struct cmsghdr)) + (len))
62 #endif
64 static inline int is_allowed_port (uint16_t port)
66 port = ntohs (port);
67 return (port == 80) || (port == 443) || (port == 554);
71 static inline int send_err (int fd, int err)
73 return send(fd, &err, sizeof (err), MSG_NOSIGNAL) == sizeof (err) ? 0 : -1;
76 /**
77 * Send a file descriptor to another process
79 static int send_fd (int p, int fd)
81 struct msghdr hdr;
82 struct iovec iov;
83 struct cmsghdr *cmsg;
84 char buf[CMSG_SPACE (sizeof (fd))];
85 int val = 0;
87 hdr.msg_name = NULL;
88 hdr.msg_namelen = 0;
89 hdr.msg_iov = &iov;
90 hdr.msg_iovlen = 1;
91 hdr.msg_control = buf;
92 hdr.msg_controllen = sizeof (buf);
94 iov.iov_base = &val;
95 iov.iov_len = sizeof (val);
97 cmsg = CMSG_FIRSTHDR (&hdr);
98 cmsg->cmsg_level = SOL_SOCKET;
99 cmsg->cmsg_type = SCM_RIGHTS;
100 cmsg->cmsg_len = CMSG_LEN (sizeof (fd));
101 memcpy (CMSG_DATA (cmsg), &fd, sizeof (fd));
102 hdr.msg_controllen = cmsg->cmsg_len;
104 return sendmsg(p, &hdr, MSG_NOSIGNAL) == sizeof (val) ? 0 : -1;
109 * Background process run as root to open privileged TCP ports.
111 static void rootprocess (int fd)
113 union
115 struct sockaddr sa;
116 struct sockaddr_storage ss;
117 struct sockaddr_in sin;
118 #ifdef AF_INET6
119 struct sockaddr_in6 sin6;
120 #endif
121 } addr;
123 while (recv (fd, &addr.ss, sizeof (addr.ss), 0) == sizeof (addr.ss))
125 unsigned len;
126 int sock;
127 int family;
129 switch (addr.sa.sa_family)
131 case AF_INET:
132 if (!is_allowed_port (addr.sin.sin_port))
134 if (send_err (fd, EACCES))
135 return;
136 continue;
138 len = sizeof (struct sockaddr_in);
139 family = PF_INET;
140 break;
142 #ifdef AF_INET6
143 case AF_INET6:
144 if (!is_allowed_port (addr.sin6.sin6_port))
146 if (send_err (fd, EACCES))
147 return;
148 continue;
150 len = sizeof (struct sockaddr_in6);
151 family = PF_INET6;
152 break;
153 #endif
155 default:
156 if (send_err (fd, EAFNOSUPPORT))
157 return;
158 continue;
161 sock = socket (family, SOCK_STREAM, IPPROTO_TCP);
162 if (sock != -1)
164 const int val = 1;
166 setsockopt (sock, SOL_SOCKET, SO_REUSEADDR, &val, sizeof (val));
167 #ifdef AF_INET6
168 if (addr.sa.sa_family == AF_INET6)
169 setsockopt (sock, IPPROTO_IPV6, IPV6_V6ONLY, &val, sizeof (val));
170 #endif
171 if (bind (sock, &addr.sa, len) == 0)
173 send_fd (fd, sock);
174 close (sock);
175 continue;
177 close (sock);
179 send_err (fd, errno);
183 /* TODO?
184 * - use libcap if available,
185 * - call chroot
188 int main (int argc, char *argv[])
190 /* Support for dynamically opening RTSP, HTTP and HTTP/SSL ports */
191 int pair[2];
193 if (socketpair (AF_LOCAL, SOCK_STREAM, 0, pair))
194 return 1;
195 if (pair[0] < 3)
196 goto error; /* we want 0, 1 and 2 open */
198 #ifdef SO_NOSIGPIPE
199 setsockopt(pair[1], SOL_SOCKET, SO_NOSIGPIPE, &(int){ 1 }, sizeof (int));
200 #endif
202 pid_t pid = fork ();
203 switch (pid)
205 case -1:
206 goto error;
208 case 0:
210 int null = open ("/dev/null", O_RDWR);
211 if (null != -1)
213 dup2 (null, 0);
214 dup2 (null, 1);
215 dup2 (null, 2);
216 close (null);
218 close (pair[0]);
219 setsid ();
220 rootprocess (pair[1]);
221 exit (0);
225 close (pair[1]);
226 pair[1] = -1;
228 char buf[21];
229 snprintf (buf, sizeof (buf), "%d", pair[0]);
230 setenv ("VLC_ROOTWRAP_SOCK", buf, 1);
232 /* Support for real-time priorities */
233 #ifdef RLIMIT_RTPRIO
234 struct rlimit rlim;
235 rlim.rlim_max = rlim.rlim_cur = sched_get_priority_min (SCHED_RR) + 24;
236 setrlimit (RLIMIT_RTPRIO, &rlim);
237 #endif
239 uid_t uid = getuid ();
240 if (uid == 0)
242 const char *sudo = getenv ("SUDO_UID");
243 if (sudo)
244 uid = atoi (sudo);
246 if (uid == 0)
248 fputs("Cannot determine unprivileged user for VLC!\n", stderr);
249 exit (1);
251 setuid (uid);
253 if (!setuid (0)) /* sanity check: we cannot get root back */
254 exit (1);
256 /* Yeah, the user can execute just about anything from here.
257 * But we've dropped privileges, so it does not matter. */
258 if (strlen (argv[0]) < sizeof ("-wrapper"))
259 goto error;
260 argv[0][strlen (argv[0]) - strlen ("-wrapper")] = '\0';
262 (void)argc;
263 if (execvp (argv[0], argv))
264 perror (argv[0]);
266 error:
267 close (pair[0]);
268 if (pair[1] != -1)
269 close (pair[1]);
270 return 1;