1 /*****************************************************************************
3 *****************************************************************************
4 * Copyright © 2005 Rémi Denis-Courmont
7 * Author: Rémi Denis-Courmont <rem # videolan.org>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
22 *****************************************************************************/
29 # define ENABLE_ROOTWRAP 1
32 #ifdef ENABLE_ROOTWRAP
34 #include <stdlib.h> /* exit() */
38 #include <sys/types.h>
41 #include <sys/socket.h>
42 #ifdef HAVE_SYS_TIME_H
46 #include <sys/resource.h> /* getrlimit() */
49 #include <pwd.h> /* getpwnam(), getpwuid() */
50 #include <grp.h> /* setgroups() */
52 #include <netinet/in.h>
55 /*#ifndef HAVE_CLEARENV
56 extern char **environ;
58 static int clearenv (void)
66 * Converts username to UID.
68 static uid_t parse_user (const char *name)
81 * Tries to find a real non-root user ID
83 static uid_t guess_user (void)
94 name = getenv ("SUDO_USER");
97 uid = parse_user (name);
98 if (uid != (uid_t)(-1))
103 name = getenv ("VLC_USER");
106 uid = parse_user (name);
107 if (uid != (uid_t)(-1))
112 uid = parse_user ("vlc");
113 if (uid != (uid_t)(-1))
121 * Returns the main GID associated with a given UID.
123 static gid_t guess_gid (uid_t uid)
134 static int is_allowed_port (uint16_t port)
138 return (port == 80) || (port == 443) || (port == 554);
142 static int send_err (int fd, int err)
144 return send (fd, &err, sizeof (err), 0) == sizeof (err) ? 0 : -1;
148 * Ugly POSIX(?) code to pass a file descriptor to another process
150 static int send_fd (int p, int fd)
154 struct cmsghdr *cmsg;
155 char buf[CMSG_SPACE (sizeof (fd))];
162 hdr.msg_control = buf;
163 hdr.msg_controllen = sizeof (buf);
166 iov.iov_len = sizeof (val);
168 cmsg = CMSG_FIRSTHDR (&hdr);
169 cmsg->cmsg_level = SOL_SOCKET;
170 cmsg->cmsg_type = SCM_RIGHTS;
171 cmsg->cmsg_len = CMSG_LEN (sizeof (fd));
172 memcpy (CMSG_DATA (cmsg), &fd, sizeof (fd));
173 hdr.msg_controllen = cmsg->cmsg_len;
175 return sendmsg (p, &hdr, 0) == sizeof (val) ? 0 : -1;
180 * Background process run as root to open privileged TCP ports.
182 static void rootprocess (int fd)
184 struct sockaddr_storage ss;
187 * - use libcap if available,
190 while (recv (fd, &ss, sizeof (ss), 0) == sizeof (ss))
195 switch (ss.ss_family)
198 if (!is_allowed_port (((struct sockaddr_in *)&ss)->sin_port))
200 if (send_err (fd, EACCES))
204 len = sizeof (struct sockaddr_in);
209 if (!is_allowed_port (((struct sockaddr_in6 *)&ss)->sin6_port))
211 if (send_err (fd, EACCES))
215 len = sizeof (struct sockaddr_in6);
220 if (send_err (fd, EAFNOSUPPORT))
225 sock = socket (ss.ss_family, SOCK_STREAM, IPPROTO_TCP);
230 setsockopt (sock, SOL_SOCKET, SO_REUSEADDR, &val, sizeof (val));
231 if (ss.ss_family == AF_INET6)
232 setsockopt (sock, IPPROTO_IPV6, IPV6_V6ONLY, &val, sizeof (val));
234 if (bind (sock, (struct sockaddr *)&ss, len) == 0)
241 send_err (fd, errno);
245 static int rootwrap_sock = -1;
246 static pid_t rootwrap_pid = -1;
248 static void close_rootwrap (void)
250 close (rootwrap_sock);
251 waitpid (rootwrap_pid, NULL, 0);
262 /* Are we running with root privileges? */
269 /* Make sure 0, 1 and 2 are opened, and only these. */
270 if (getrlimit (RLIMIT_NOFILE, &lim))
273 for (fd = 3; ((unsigned)fd) < lim.rlim_cur; fd++)
281 fputs ("Starting VLC root wrapper...", stderr);
284 fprintf (stderr, " using UID %u", (unsigned)u);
287 fprintf (stderr, ", using GID %u\n", (unsigned)g);
291 fputs ("***************************************\n"
292 "* Running VLC as root is discouraged. *\n"
293 "***************************************\n"
295 " It is potentially dangerous, "
296 "and might not even work properly.\n", stderr);
304 if (socketpair (AF_LOCAL, SOCK_STREAM, 0, pair))
306 perror ("socketpair");
310 switch (rootwrap_pid = fork ())
323 rootprocess (pair[1]);
328 rootwrap_sock = pair[0];
336 atexit (close_rootwrap);
341 * Ugly POSIX(?) code to receive a file descriptor from another process
343 static int recv_fd (int p)
347 struct cmsghdr *cmsg;
349 char buf[CMSG_SPACE (sizeof (fd))];
355 hdr.msg_control = buf;
356 hdr.msg_controllen = sizeof (buf);
359 iov.iov_len = sizeof (val);
361 if (recvmsg (p, &hdr, 0) != sizeof (val))
364 for (cmsg = CMSG_FIRSTHDR (&hdr); cmsg != NULL;
365 cmsg = CMSG_NXTHDR (&hdr, cmsg))
367 if ((cmsg->cmsg_level == SOL_SOCKET)
368 && (cmsg->cmsg_type = SCM_RIGHTS)
369 && (cmsg->cmsg_len >= CMSG_LEN (sizeof (fd))))
371 memcpy (&fd, CMSG_DATA (cmsg), sizeof (fd));
380 * Tries to obtain a bound TCP socket from the root process
382 int rootwrap_bind (int family, int socktype, int protocol,
383 const struct sockaddr *addr, size_t alen)
385 /* can't use libvlc */
386 static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
388 struct sockaddr_storage ss;
391 if (rootwrap_sock == -1)
400 if (alen < sizeof (struct sockaddr_in))
409 if (alen < sizeof (struct sockaddr_in6))
418 errno = EAFNOSUPPORT;
422 if (family != addr->sa_family)
424 errno = EAFNOSUPPORT;
428 /* Only TCP is implemented at the moment */
429 if ((socktype != SOCK_STREAM)
430 || (protocol && (protocol != IPPROTO_TCP)))
436 memset (&ss, 0, sizeof (ss));
437 memcpy (&ss, addr, alen > sizeof (ss) ? sizeof (ss) : alen);
439 pthread_mutex_lock (&mutex);
440 if (send (rootwrap_sock, &ss, sizeof (ss), 0) != sizeof (ss))
443 fd = recv_fd (rootwrap_sock);
444 pthread_mutex_unlock (&mutex);
450 val = fcntl (fd, F_GETFL, 0);
451 fcntl (fd, F_SETFL, ((val != -1) ? val : 0) | O_NONBLOCK);
466 int rootwrap_bind (int family, int socktype, int protocol,
467 const struct sockaddr *addr, size_t alen)
472 #endif /* ENABLE_ROOTWRAP */