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 *****************************************************************************/
25 #include <stdlib.h> /* exit() */
29 #include <sys/types.h>
33 #include <sys/socket.h>
35 #include <sys/resource.h> /* getrlimit() */
38 #include <netinet/in.h>
40 #if defined (AF_INET6) && !defined (IPV6_V6ONLY)
41 # warning Uho, your IPv6 support is broken and has been disabled. Fix your C library.
46 # define AF_LOCAL AF_UNIX
48 /* Required yet non-standard cmsg functions */
50 # define CMSG_ALIGN(len) (((len) + sizeof(intptr_t)-1) & ~(sizeof(intptr_t)-1))
53 # define CMSG_SPACE(len) (CMSG_ALIGN(sizeof(struct cmsghdr)) + CMSG_ALIGN(len))
56 # define CMSG_LEN(len) (CMSG_ALIGN(sizeof(struct cmsghdr)) + (len))
59 static inline int is_allowed_port (uint16_t port
)
62 return (port
== 80) || (port
== 443) || (port
== 554);
66 static inline int send_err (int fd
, int err
)
68 return send (fd
, &err
, sizeof (err
), 0) == sizeof (err
) ? 0 : -1;
72 * Send a file descriptor to another process
74 static int send_fd (int p
, int fd
)
79 char buf
[CMSG_SPACE (sizeof (fd
))];
86 hdr
.msg_control
= buf
;
87 hdr
.msg_controllen
= sizeof (buf
);
90 iov
.iov_len
= sizeof (val
);
92 cmsg
= CMSG_FIRSTHDR (&hdr
);
93 cmsg
->cmsg_level
= SOL_SOCKET
;
94 cmsg
->cmsg_type
= SCM_RIGHTS
;
95 cmsg
->cmsg_len
= CMSG_LEN (sizeof (fd
));
96 memcpy (CMSG_DATA (cmsg
), &fd
, sizeof (fd
));
97 hdr
.msg_controllen
= cmsg
->cmsg_len
;
99 return sendmsg (p
, &hdr
, 0) == sizeof (val
) ? 0 : -1;
104 * Background process run as root to open privileged TCP ports.
106 static void rootprocess (int fd
)
108 struct sockaddr_storage ss
;
110 while (recv (fd
, &ss
, sizeof (ss
), 0) == sizeof (ss
))
115 switch (ss
.ss_family
)
118 if (!is_allowed_port (((struct sockaddr_in
*)&ss
)->sin_port
))
120 if (send_err (fd
, EACCES
))
124 len
= sizeof (struct sockaddr_in
);
129 if (!is_allowed_port (((struct sockaddr_in6
*)&ss
)->sin6_port
))
131 if (send_err (fd
, EACCES
))
135 len
= sizeof (struct sockaddr_in6
);
140 if (send_err (fd
, EAFNOSUPPORT
))
145 sock
= socket (ss
.ss_family
, SOCK_STREAM
, IPPROTO_TCP
);
150 setsockopt (sock
, SOL_SOCKET
, SO_REUSEADDR
, &val
, sizeof (val
));
152 if (ss
.ss_family
== AF_INET6
)
153 setsockopt (sock
, IPPROTO_IPV6
, IPV6_V6ONLY
, &val
, sizeof (val
));
155 if (bind (sock
, (struct sockaddr
*)&ss
, len
) == 0)
162 send_err (fd
, errno
);
167 * - use libcap if available,
171 int main (int argc
, char *argv
[])
173 /* Support for dynamically opening RTSP, HTTP and HTTP/SSL ports */
176 if (socketpair (AF_LOCAL
, SOCK_STREAM
, 0, pair
))
179 goto error
; /* we want 0, 1 and 2 open */
189 int null
= open ("/dev/null", O_RDWR
);
199 rootprocess (pair
[1]);
208 snprintf (buf
, sizeof (buf
), "%d", pair
[0]);
209 setenv ("VLC_ROOTWRAP_SOCK", buf
, 1);
211 /* Support for real-time priorities */
214 rlim
.rlim_max
= rlim
.rlim_cur
= sched_get_priority_min (SCHED_RR
) + 24;
215 setrlimit (RLIMIT_RTPRIO
, &rlim
);
218 uid_t uid
= getuid ();
221 const char *sudo
= getenv ("SUDO_UID");
227 fprintf (stderr
, "Cannot determine unprivileged user for VLC!\n");
232 if (!setuid (0)) /* sanity check: we cannot get root back */
235 /* Yeah, the user can execute just about anything from here.
236 * But we've dropped privileges, so it does not matter. */
237 if (strlen (argv
[0]) < sizeof ("-wrapper"))
239 argv
[0][strlen (argv
[0]) - strlen ("-wrapper")] = '\0';
242 if (execvp (argv
[0], argv
))