1 /*****************************************************************************
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 *****************************************************************************/
24 #define _XPG4_2 /* ancilliary data on Solaris */
26 #include <stdlib.h> /* exit() */
30 #include <sys/types.h>
34 #include <sys/socket.h>
36 #include <sys/resource.h> /* getrlimit() */
39 #include <netinet/in.h>
41 #if defined (AF_INET6) && !defined (IPV6_V6ONLY)
42 # warning Uho, your IPv6 support is broken and has been disabled. Fix your C library.
47 # define AF_LOCAL AF_UNIX
49 /* Required yet non-standard cmsg functions */
51 # define CMSG_ALIGN(len) (((len) + sizeof(intptr_t)-1) & ~(sizeof(intptr_t)-1))
54 # define CMSG_SPACE(len) (CMSG_ALIGN(sizeof(struct cmsghdr)) + CMSG_ALIGN(len))
57 # define CMSG_LEN(len) (CMSG_ALIGN(sizeof(struct cmsghdr)) + (len))
60 static inline int is_allowed_port (uint16_t port
)
63 return (port
== 80) || (port
== 443) || (port
== 554);
67 static inline int send_err (int fd
, int err
)
69 return send (fd
, &err
, sizeof (err
), 0) == sizeof (err
) ? 0 : -1;
73 * Send a file descriptor to another process
75 static int send_fd (int p
, int fd
)
80 char buf
[CMSG_SPACE (sizeof (fd
))];
87 hdr
.msg_control
= buf
;
88 hdr
.msg_controllen
= sizeof (buf
);
91 iov
.iov_len
= sizeof (val
);
93 cmsg
= CMSG_FIRSTHDR (&hdr
);
94 cmsg
->cmsg_level
= SOL_SOCKET
;
95 cmsg
->cmsg_type
= SCM_RIGHTS
;
96 cmsg
->cmsg_len
= CMSG_LEN (sizeof (fd
));
97 memcpy (CMSG_DATA (cmsg
), &fd
, sizeof (fd
));
98 hdr
.msg_controllen
= cmsg
->cmsg_len
;
100 return sendmsg (p
, &hdr
, 0) == sizeof (val
) ? 0 : -1;
105 * Background process run as root to open privileged TCP ports.
107 static void rootprocess (int fd
)
112 struct sockaddr_storage ss
;
113 struct sockaddr_in sin
;
115 struct sockaddr_in6 sin6
;
119 while (recv (fd
, &addr
.ss
, sizeof (addr
.ss
), 0) == sizeof (addr
.ss
))
125 switch (addr
.sa
.sa_family
)
128 if (!is_allowed_port (addr
.sin
.sin_port
))
130 if (send_err (fd
, EACCES
))
134 len
= sizeof (struct sockaddr_in
);
140 if (!is_allowed_port (addr
.sin6
.sin6_port
))
142 if (send_err (fd
, EACCES
))
146 len
= sizeof (struct sockaddr_in6
);
152 if (send_err (fd
, EAFNOSUPPORT
))
157 sock
= socket (family
, SOCK_STREAM
, IPPROTO_TCP
);
162 setsockopt (sock
, SOL_SOCKET
, SO_REUSEADDR
, &val
, sizeof (val
));
164 if (addr
.sa
.sa_family
== AF_INET6
)
165 setsockopt (sock
, IPPROTO_IPV6
, IPV6_V6ONLY
, &val
, sizeof (val
));
167 if (bind (sock
, &addr
.sa
, len
) == 0)
174 send_err (fd
, errno
);
179 * - use libcap if available,
183 int main (int argc
, char *argv
[])
185 /* Support for dynamically opening RTSP, HTTP and HTTP/SSL ports */
188 if (socketpair (AF_LOCAL
, SOCK_STREAM
, 0, pair
))
191 goto error
; /* we want 0, 1 and 2 open */
201 int null
= open ("/dev/null", O_RDWR
);
211 rootprocess (pair
[1]);
220 snprintf (buf
, sizeof (buf
), "%d", pair
[0]);
221 setenv ("VLC_ROOTWRAP_SOCK", buf
, 1);
223 /* Support for real-time priorities */
226 rlim
.rlim_max
= rlim
.rlim_cur
= sched_get_priority_min (SCHED_RR
) + 24;
227 setrlimit (RLIMIT_RTPRIO
, &rlim
);
230 uid_t uid
= getuid ();
233 const char *sudo
= getenv ("SUDO_UID");
239 fprintf (stderr
, "Cannot determine unprivileged user for VLC!\n");
244 if (!setuid (0)) /* sanity check: we cannot get root back */
247 /* Yeah, the user can execute just about anything from here.
248 * But we've dropped privileges, so it does not matter. */
249 if (strlen (argv
[0]) < sizeof ("-wrapper"))
251 argv
[0][strlen (argv
[0]) - strlen ("-wrapper")] = '\0';
254 if (execvp (argv
[0], argv
))