EGL: destroy the created EGLSurface on Close()
[vlc.git] / bin / rootwrap.c
blob03598a94178d9be5e96e177370d1fcde6fa905c7
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
48 #ifndef AF_LOCAL
49 # define AF_LOCAL AF_UNIX
50 #endif
51 /* Required yet non-standard cmsg functions */
52 #ifndef CMSG_ALIGN
53 # define CMSG_ALIGN(len) (((len) + sizeof(intptr_t)-1) & ~(sizeof(intptr_t)-1))
54 #endif
55 #ifndef CMSG_SPACE
56 # define CMSG_SPACE(len) (CMSG_ALIGN(sizeof(struct cmsghdr)) + CMSG_ALIGN(len))
57 #endif
58 #ifndef CMSG_LEN
59 # define CMSG_LEN(len) (CMSG_ALIGN(sizeof(struct cmsghdr)) + (len))
60 #endif
62 static inline int is_allowed_port (uint16_t port)
64 port = ntohs (port);
65 return (port == 80) || (port == 443) || (port == 554);
69 static inline int send_err (int fd, int err)
71 return send (fd, &err, sizeof (err), 0) == sizeof (err) ? 0 : -1;
74 /**
75 * Send a file descriptor to another process
77 static int send_fd (int p, int fd)
79 struct msghdr hdr;
80 struct iovec iov;
81 struct cmsghdr *cmsg;
82 char buf[CMSG_SPACE (sizeof (fd))];
83 int val = 0;
85 hdr.msg_name = NULL;
86 hdr.msg_namelen = 0;
87 hdr.msg_iov = &iov;
88 hdr.msg_iovlen = 1;
89 hdr.msg_control = buf;
90 hdr.msg_controllen = sizeof (buf);
92 iov.iov_base = &val;
93 iov.iov_len = sizeof (val);
95 cmsg = CMSG_FIRSTHDR (&hdr);
96 cmsg->cmsg_level = SOL_SOCKET;
97 cmsg->cmsg_type = SCM_RIGHTS;
98 cmsg->cmsg_len = CMSG_LEN (sizeof (fd));
99 memcpy (CMSG_DATA (cmsg), &fd, sizeof (fd));
100 hdr.msg_controllen = cmsg->cmsg_len;
102 return sendmsg (p, &hdr, 0) == sizeof (val) ? 0 : -1;
107 * Background process run as root to open privileged TCP ports.
109 static void rootprocess (int fd)
111 union
113 struct sockaddr sa;
114 struct sockaddr_storage ss;
115 struct sockaddr_in sin;
116 #ifdef AF_INET6
117 struct sockaddr_in6 sin6;
118 #endif
119 } addr;
121 while (recv (fd, &addr.ss, sizeof (addr.ss), 0) == sizeof (addr.ss))
123 unsigned len;
124 int sock;
125 int family;
127 switch (addr.sa.sa_family)
129 case AF_INET:
130 if (!is_allowed_port (addr.sin.sin_port))
132 if (send_err (fd, EACCES))
133 return;
134 continue;
136 len = sizeof (struct sockaddr_in);
137 family = PF_INET;
138 break;
140 #ifdef AF_INET6
141 case AF_INET6:
142 if (!is_allowed_port (addr.sin6.sin6_port))
144 if (send_err (fd, EACCES))
145 return;
146 continue;
148 len = sizeof (struct sockaddr_in6);
149 family = PF_INET6;
150 break;
151 #endif
153 default:
154 if (send_err (fd, EAFNOSUPPORT))
155 return;
156 continue;
159 sock = socket (family, SOCK_STREAM, IPPROTO_TCP);
160 if (sock != -1)
162 const int val = 1;
164 setsockopt (sock, SOL_SOCKET, SO_REUSEADDR, &val, sizeof (val));
165 #ifdef AF_INET6
166 if (addr.sa.sa_family == AF_INET6)
167 setsockopt (sock, IPPROTO_IPV6, IPV6_V6ONLY, &val, sizeof (val));
168 #endif
169 if (bind (sock, &addr.sa, len) == 0)
171 send_fd (fd, sock);
172 close (sock);
173 continue;
176 send_err (fd, errno);
180 /* TODO?
181 * - use libcap if available,
182 * - call chroot
185 int main (int argc, char *argv[])
187 /* Support for dynamically opening RTSP, HTTP and HTTP/SSL ports */
188 int pair[2];
190 if (socketpair (AF_LOCAL, SOCK_STREAM, 0, pair))
191 return 1;
192 if (pair[0] < 3)
193 goto error; /* we want 0, 1 and 2 open */
195 pid_t pid = fork ();
196 switch (pid)
198 case -1:
199 goto error;
201 case 0:
203 int null = open ("/dev/null", O_RDWR);
204 if (null != -1)
206 dup2 (null, 0);
207 dup2 (null, 1);
208 dup2 (null, 2);
209 close (null);
211 close (pair[0]);
212 setsid ();
213 rootprocess (pair[1]);
214 exit (0);
218 close (pair[1]);
219 pair[1] = -1;
221 char buf[21];
222 snprintf (buf, sizeof (buf), "%d", pair[0]);
223 setenv ("VLC_ROOTWRAP_SOCK", buf, 1);
225 /* Support for real-time priorities */
226 #ifdef RLIMIT_RTPRIO
227 struct rlimit rlim;
228 rlim.rlim_max = rlim.rlim_cur = sched_get_priority_min (SCHED_RR) + 24;
229 setrlimit (RLIMIT_RTPRIO, &rlim);
230 #endif
232 uid_t uid = getuid ();
233 if (uid == 0)
235 const char *sudo = getenv ("SUDO_UID");
236 if (sudo)
237 uid = atoi (sudo);
239 if (uid == 0)
241 fprintf (stderr, "Cannot determine unprivileged user for VLC!\n");
242 exit (1);
244 setuid (uid);
246 if (!setuid (0)) /* sanity check: we cannot get root back */
247 exit (1);
249 /* Yeah, the user can execute just about anything from here.
250 * But we've dropped privileges, so it does not matter. */
251 if (strlen (argv[0]) < sizeof ("-wrapper"))
252 goto error;
253 argv[0][strlen (argv[0]) - strlen ("-wrapper")] = '\0';
255 (void)argc;
256 if (execvp (argv[0], argv))
257 perror (argv[0]);
259 error:
260 close (pair[0]);
261 if (pair[1] != -1)
262 close (pair[1]);
263 return 1;