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 *****************************************************************************/
28 #if defined (HAVE_GETEUID) && !defined (SYS_BEOS)
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 * Tries to find a real non-root user to use
68 static struct passwd *guess_user (void)
77 if ((pw = getpwuid (uid)) != NULL)
81 name = getenv ("SUDO_USER");
83 if ((pw = getpwnam (name)) != NULL)
87 name = getenv ("VLC_USER");
89 if ((pw = getpwnam (name)) != NULL)
93 if ((pw = getpwnam ("vlc")) != NULL)
100 static int is_allowed_port (uint16_t port)
104 return (port == 80) || (port == 443) || (port == 554);
108 static int send_err (int fd, int err)
110 return send (fd, &err, sizeof (err), 0) == sizeof (err) ? 0 : -1;
114 * Ugly POSIX(?) code to pass a file descriptor to another process
116 static int send_fd (int p, int fd)
120 struct cmsghdr *cmsg;
121 char buf[CMSG_SPACE (sizeof (fd))];
128 hdr.msg_control = buf;
129 hdr.msg_controllen = sizeof (buf);
132 iov.iov_len = sizeof (val);
134 cmsg = CMSG_FIRSTHDR (&hdr);
135 cmsg->cmsg_level = SOL_SOCKET;
136 cmsg->cmsg_type = SCM_RIGHTS;
137 cmsg->cmsg_len = CMSG_LEN (sizeof (fd));
138 memcpy (CMSG_DATA (cmsg), &fd, sizeof (fd));
139 hdr.msg_controllen = cmsg->cmsg_len;
141 return sendmsg (p, &hdr, 0) == sizeof (val) ? 0 : -1;
146 * Background process run as root to open privileged TCP ports.
148 static void rootprocess (int fd)
150 struct sockaddr_storage ss;
153 * - use libcap if available,
156 while (recv (fd, &ss, sizeof (ss), 0) == sizeof (ss))
161 switch (ss.ss_family)
164 if (!is_allowed_port (((struct sockaddr_in *)&ss)->sin_port))
166 if (send_err (fd, EACCES))
170 len = sizeof (struct sockaddr_in);
175 if (!is_allowed_port (((struct sockaddr_in6 *)&ss)->sin6_port))
177 if (send_err (fd, EACCES))
181 len = sizeof (struct sockaddr_in6);
186 if (send_err (fd, EAFNOSUPPORT))
191 sock = socket (ss.ss_family, SOCK_STREAM, IPPROTO_TCP);
196 setsockopt (sock, SOL_SOCKET, SO_REUSEADDR, &val, sizeof (val));
197 if (ss.ss_family == AF_INET6)
198 setsockopt (sock, IPPROTO_IPV6, IPV6_V6ONLY, &val, sizeof (val));
200 if (bind (sock, (struct sockaddr *)&ss, len) == 0)
207 send_err (fd, errno);
211 static int rootwrap_sock = -1;
212 static pid_t rootwrap_pid = -1;
214 static void close_rootwrap (void)
216 close (rootwrap_sock);
217 waitpid (rootwrap_pid, NULL, 0);
228 /* Are we running with root privileges? */
235 /* Make sure 0, 1 and 2 are opened, and only these. */
236 if (getrlimit (RLIMIT_NOFILE, &lim))
239 for (fd = 3; ((unsigned)fd) < lim.rlim_cur; fd++)
247 fputs ("Starting VLC root wrapper...", stderr);
251 return; /* Should we rather print an error and exit ? */
254 fprintf (stderr, " using UID %u (%s)\n", (unsigned)u, pw->pw_name);
257 fputs ("***************************************\n"
258 "* Running VLC as root is discouraged. *\n"
259 "***************************************\n"
261 " It is potentially dangerous, "
262 "and might not even work properly.\n", stderr);
267 initgroups (pw->pw_name, pw->pw_gid);
270 if (socketpair (AF_LOCAL, SOCK_STREAM, 0, pair))
272 perror ("socketpair");
276 switch (rootwrap_pid = fork ())
289 rootprocess (pair[1]);
294 rootwrap_sock = pair[0];
302 atexit (close_rootwrap);
307 * Ugly POSIX(?) code to receive a file descriptor from another process
309 static int recv_fd (int p)
313 struct cmsghdr *cmsg;
315 char buf[CMSG_SPACE (sizeof (fd))];
321 hdr.msg_control = buf;
322 hdr.msg_controllen = sizeof (buf);
325 iov.iov_len = sizeof (val);
327 if (recvmsg (p, &hdr, 0) != sizeof (val))
330 for (cmsg = CMSG_FIRSTHDR (&hdr); cmsg != NULL;
331 cmsg = CMSG_NXTHDR (&hdr, cmsg))
333 if ((cmsg->cmsg_level == SOL_SOCKET)
334 && (cmsg->cmsg_type = SCM_RIGHTS)
335 && (cmsg->cmsg_len >= CMSG_LEN (sizeof (fd))))
337 memcpy (&fd, CMSG_DATA (cmsg), sizeof (fd));
346 * Tries to obtain a bound TCP socket from the root process
348 int rootwrap_bind (int family, int socktype, int protocol,
349 const struct sockaddr *addr, size_t alen)
351 /* can't use libvlc */
352 static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
354 struct sockaddr_storage ss;
357 if (rootwrap_sock == -1)
366 if (alen < sizeof (struct sockaddr_in))
375 if (alen < sizeof (struct sockaddr_in6))
384 errno = EAFNOSUPPORT;
388 if (family != addr->sa_family)
390 errno = EAFNOSUPPORT;
394 /* Only TCP is implemented at the moment */
395 if ((socktype != SOCK_STREAM)
396 || (protocol && (protocol != IPPROTO_TCP)))
402 memset (&ss, 0, sizeof (ss));
403 memcpy (&ss, addr, alen > sizeof (ss) ? sizeof (ss) : alen);
405 pthread_mutex_lock (&mutex);
406 if (send (rootwrap_sock, &ss, sizeof (ss), 0) != sizeof (ss))
409 fd = recv_fd (rootwrap_sock);
410 pthread_mutex_unlock (&mutex);
416 val = fcntl (fd, F_GETFL, 0);
417 fcntl (fd, F_SETFL, ((val != -1) ? val : 0) | O_NONBLOCK);
432 int rootwrap_bind (int family, int socktype, int protocol,
433 const struct sockaddr *addr, size_t alen)
438 #endif /* ENABLE_ROOTWRAP */