demux: adaptive: add inheritable wrapper deleter
[vlc.git] / bin / rootwrap.c
blobf582a736c78ecce1f57f45553a1db9370372f0b1
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 setsockopt (sock, SOL_SOCKET, SO_REUSEADDR, &(int){ 1 }, sizeof (int));
165 #ifdef AF_INET6
166 if (addr.sa.sa_family == AF_INET6)
167 setsockopt (sock, IPPROTO_IPV6, IPV6_V6ONLY, &(int){ 1 }, sizeof (int));
168 #endif
169 if (bind (sock, &addr.sa, len) == 0)
171 send_fd (fd, sock);
172 close (sock);
173 continue;
175 close (sock);
177 send_err (fd, errno);
181 /* TODO?
182 * - use libcap if available,
183 * - call chroot
186 int main (int argc, char *argv[])
188 /* Support for dynamically opening RTSP, HTTP and HTTP/SSL ports */
189 int pair[2];
191 if (socketpair (AF_LOCAL, SOCK_STREAM, 0, pair))
192 return 1;
193 if (pair[0] < 3)
194 goto error; /* we want 0, 1 and 2 open */
196 #ifdef SO_NOSIGPIPE
197 setsockopt(pair[1], SOL_SOCKET, SO_NOSIGPIPE, &(int){ 1 }, sizeof (int));
198 #endif
200 pid_t pid = fork ();
201 switch (pid)
203 case -1:
204 goto error;
206 case 0:
208 int null = open ("/dev/null", O_RDWR);
209 if (null != -1)
211 dup2 (null, 0);
212 dup2 (null, 1);
213 dup2 (null, 2);
214 close (null);
216 close (pair[0]);
217 setsid ();
218 rootprocess (pair[1]);
219 exit (0);
223 close (pair[1]);
224 pair[1] = -1;
226 char buf[21];
227 snprintf (buf, sizeof (buf), "%d", pair[0]);
228 setenv ("VLC_ROOTWRAP_SOCK", buf, 1);
230 /* Support for real-time priorities */
231 #ifdef RLIMIT_RTPRIO
232 struct rlimit rlim;
233 rlim.rlim_max = rlim.rlim_cur = sched_get_priority_min (SCHED_RR) + 24;
234 setrlimit (RLIMIT_RTPRIO, &rlim);
235 #endif
237 uid_t uid = getuid ();
238 if (uid == 0)
240 const char *sudo = getenv ("SUDO_UID");
241 if (sudo)
242 uid = atoi (sudo);
244 if (uid == 0)
246 fputs("Cannot determine unprivileged user for VLC!\n", stderr);
247 exit (1);
249 setuid (uid);
251 if (!setuid (0)) /* sanity check: we cannot get root back */
252 exit (1);
254 /* Yeah, the user can execute just about anything from here.
255 * But we've dropped privileges, so it does not matter. */
256 if (strlen (argv[0]) < sizeof ("-wrapper"))
257 goto error;
258 argv[0][strlen (argv[0]) - strlen ("-wrapper")] = '\0';
260 (void)argc;
261 if (execvp (argv[0], argv))
262 perror (argv[0]);
264 error:
265 close (pair[0]);
266 if (pair[1] != -1)
267 close (pair[1]);
268 return 1;