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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, 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));
198 if (ss.ss_family == AF_INET6)
199 setsockopt (sock, IPPROTO_IPV6, IPV6_V6ONLY, &val, sizeof (val));
201 if (bind (sock, (struct sockaddr *)&ss, len) == 0)
208 send_err (fd, errno);
212 static int rootwrap_sock = -1;
213 static pid_t rootwrap_pid = -1;
215 static void close_rootwrap (void)
217 close (rootwrap_sock);
218 waitpid (rootwrap_pid, NULL, 0);
229 /* Are we running with root privileges? */
236 /* Make sure 0, 1 and 2 are opened, and only these. */
237 if (getrlimit (RLIMIT_NOFILE, &lim))
240 for (fd = 3; ((unsigned)fd) < lim.rlim_cur; fd++)
248 fputs ("Starting VLC root wrapper...", stderr);
252 return; /* Should we rather print an error and exit ? */
255 fprintf (stderr, " using UID %u (%s)\n", (unsigned)u, pw->pw_name);
258 fputs ("***************************************\n"
259 "* Running VLC as root is discouraged. *\n"
260 "***************************************\n"
262 " It is potentially dangerous, "
263 "and might not even work properly.\n", stderr);
268 initgroups (pw->pw_name, pw->pw_gid);
271 if (socketpair (AF_LOCAL, SOCK_STREAM, 0, pair))
273 perror ("socketpair");
277 switch (rootwrap_pid = fork ())
290 rootprocess (pair[1]);
295 rootwrap_sock = pair[0];
303 atexit (close_rootwrap);
308 * Ugly POSIX(?) code to receive a file descriptor from another process
310 static int recv_fd (int p)
314 struct cmsghdr *cmsg;
316 char buf[CMSG_SPACE (sizeof (fd))];
322 hdr.msg_control = buf;
323 hdr.msg_controllen = sizeof (buf);
326 iov.iov_len = sizeof (val);
328 if (recvmsg (p, &hdr, 0) != sizeof (val))
331 for (cmsg = CMSG_FIRSTHDR (&hdr); cmsg != NULL;
332 cmsg = CMSG_NXTHDR (&hdr, cmsg))
334 if ((cmsg->cmsg_level == SOL_SOCKET)
335 && (cmsg->cmsg_type = SCM_RIGHTS)
336 && (cmsg->cmsg_len >= CMSG_LEN (sizeof (fd))))
338 memcpy (&fd, CMSG_DATA (cmsg), sizeof (fd));
347 * Tries to obtain a bound TCP socket from the root process
349 int rootwrap_bind (int family, int socktype, int protocol,
350 const struct sockaddr *addr, size_t alen)
352 /* can't use libvlc */
353 static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
355 struct sockaddr_storage ss;
358 if (rootwrap_sock == -1)
367 if (alen < sizeof (struct sockaddr_in))
376 if (alen < sizeof (struct sockaddr_in6))
385 errno = EAFNOSUPPORT;
389 if (family != addr->sa_family)
391 errno = EAFNOSUPPORT;
395 /* Only TCP is implemented at the moment */
396 if ((socktype != SOCK_STREAM)
397 || (protocol && (protocol != IPPROTO_TCP)))
403 memset (&ss, 0, sizeof (ss));
404 memcpy (&ss, addr, alen > sizeof (ss) ? sizeof (ss) : alen);
406 pthread_mutex_lock (&mutex);
407 if (send (rootwrap_sock, &ss, sizeof (ss), 0) != sizeof (ss))
410 fd = recv_fd (rootwrap_sock);
411 pthread_mutex_unlock (&mutex);
417 val = fcntl (fd, F_GETFL, 0);
418 fcntl (fd, F_SETFL, ((val != -1) ? val : 0) | O_NONBLOCK);
433 int rootwrap_bind (int family, int socktype, int protocol,
434 const struct sockaddr *addr, size_t alen)
439 #endif /* ENABLE_ROOTWRAP */