]> git.sesse.net Git - vlc/blob - bin/rootwrap.c
Revert "Call XInitThreads early enough"
[vlc] / bin / rootwrap.c
1 /*****************************************************************************
2  * rootwrap.c
3  *****************************************************************************
4  * Copyright © 2005-2008 Rémi Denis-Courmont
5  *
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.
10  *
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.
15  *
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  *****************************************************************************/
20
21 #if HAVE_CONFIG_H
22 # include <config.h>
23 #endif
24
25 #include <stdlib.h> /* exit() */
26 #include <stdio.h>
27 #include <string.h>
28
29 #include <sys/types.h>
30 #include <unistd.h>
31 #include <fcntl.h>
32 #include <sys/stat.h>
33 #include <sys/socket.h>
34 #include <sys/uio.h>
35 #include <sys/resource.h> /* getrlimit() */
36 #include <sched.h>
37 #include <errno.h>
38 #include <netinet/in.h>
39
40 #if defined (AF_INET6) && !defined (IPV6_V6ONLY)
41 # warning Uho, your IPv6 support is broken and has been disabled. Fix your C library.
42 # undef AF_INET6
43 #endif
44
45 #ifndef AF_LOCAL
46 # define AF_LOCAL AF_UNIX
47 #endif
48 /* Required yet non-standard cmsg functions */
49 #ifndef CMSG_ALIGN
50 # define CMSG_ALIGN(len) (((len) + sizeof(intptr_t)-1) & ~(sizeof(intptr_t)-1))
51 #endif
52 #ifndef CMSG_SPACE
53 # define CMSG_SPACE(len) (CMSG_ALIGN(sizeof(struct cmsghdr)) + CMSG_ALIGN(len))
54 #endif
55 #ifndef CMSG_LEN
56 # define CMSG_LEN(len) (CMSG_ALIGN(sizeof(struct cmsghdr)) + (len))
57 #endif
58
59 static inline int is_allowed_port (uint16_t port)
60 {
61     port = ntohs (port);
62     return (port == 80) || (port == 443) || (port == 554);
63 }
64
65
66 static inline int send_err (int fd, int err)
67 {
68     return send (fd, &err, sizeof (err), 0) == sizeof (err) ? 0 : -1;
69 }
70
71 /**
72  * Send a file descriptor to another process
73  */
74 static int send_fd (int p, int fd)
75 {
76     struct msghdr hdr;
77     struct iovec iov;
78     struct cmsghdr *cmsg;
79     char buf[CMSG_SPACE (sizeof (fd))];
80     int val = 0;
81
82     hdr.msg_name = NULL;
83     hdr.msg_namelen = 0;
84     hdr.msg_iov = &iov;
85     hdr.msg_iovlen = 1;
86     hdr.msg_control = buf;
87     hdr.msg_controllen = sizeof (buf);
88
89     iov.iov_base = &val;
90     iov.iov_len = sizeof (val);
91
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;
98
99     return sendmsg (p, &hdr, 0) == sizeof (val) ? 0 : -1;
100 }
101
102
103 /**
104  * Background process run as root to open privileged TCP ports.
105  */
106 static void rootprocess (int fd)
107 {
108     struct sockaddr_storage ss;
109
110     while (recv (fd, &ss, sizeof (ss), 0) == sizeof (ss))
111     {
112         unsigned len;
113         int sock;
114
115         switch (ss.ss_family)
116         {
117             case AF_INET:
118                 if (!is_allowed_port (((struct sockaddr_in *)&ss)->sin_port))
119                 {
120                     if (send_err (fd, EACCES))
121                         return;
122                     continue;
123                 }
124                 len = sizeof (struct sockaddr_in);
125                 break;
126
127 #ifdef AF_INET6
128             case AF_INET6:
129                 if (!is_allowed_port (((struct sockaddr_in6 *)&ss)->sin6_port))
130                 {
131                     if (send_err (fd, EACCES))
132                         return;
133                     continue;
134                 }
135                 len = sizeof (struct sockaddr_in6);
136                 break;
137 #endif
138
139             default:
140                 if (send_err (fd, EAFNOSUPPORT))
141                     return;
142                 continue;
143         }
144
145         sock = socket (ss.ss_family, SOCK_STREAM, IPPROTO_TCP);
146         if (sock != -1)
147         {
148             const int val = 1;
149
150             setsockopt (sock, SOL_SOCKET, SO_REUSEADDR, &val, sizeof (val));
151 #ifdef AF_INET6
152             if (ss.ss_family == AF_INET6)
153                 setsockopt (sock, IPPROTO_IPV6, IPV6_V6ONLY, &val, sizeof (val));
154 #endif
155             if (bind (sock, (struct sockaddr *)&ss, len) == 0)
156             {
157                 send_fd (fd, sock);
158                 close (sock);
159                 continue;
160             }
161         }
162         send_err (fd, errno);
163     }
164 }
165
166 /* TODO?
167  *  - use libcap if available,
168  *  - call chroot
169  */
170
171 int main (int argc, char *argv[])
172 {
173     /* Support for dynamically opening RTSP, HTTP and HTTP/SSL ports */
174     int pair[2];
175
176     if (socketpair (AF_LOCAL, SOCK_STREAM, 0, pair))
177         return 1;
178     if (pair[0] < 3)
179         goto error; /* we want 0, 1 and 2 open */
180
181     pid_t pid = fork ();
182     switch (pid)
183     {
184         case -1:
185             goto error;
186
187         case 0:
188         {
189             int null = open ("/dev/null", O_RDWR);
190             if (null != -1)
191             {
192                 dup2 (null, 0);
193                 dup2 (null, 1);
194                 dup2 (null, 2);
195                 close (null);
196             }
197             close (pair[0]);
198             setsid ();
199             rootprocess (pair[1]);
200             exit (0);
201         }
202     }
203
204     close (pair[1]);
205     pair[1] = -1;
206
207     char buf[21];
208     snprintf (buf, sizeof (buf), "%d", pair[0]);
209     setenv ("VLC_ROOTWRAP_SOCK", buf, 1);
210
211     /* Support for real-time priorities */
212 #ifdef RLIMIT_RTPRIO
213     struct rlimit rlim;
214     rlim.rlim_max = rlim.rlim_cur = sched_get_priority_min (SCHED_RR) + 24;
215     setrlimit (RLIMIT_RTPRIO, &rlim);
216 #endif
217
218     uid_t uid = getuid ();
219     if (uid == 0)
220     {
221         const char *sudo = getenv ("SUDO_UID");
222         if (sudo)
223             uid = atoi (sudo);
224     }
225     if (uid == 0)
226     {
227         fprintf (stderr, "Cannot determine unprivileged user for VLC!\n");
228         exit (1);
229     }
230     setuid (uid);
231
232     if (!setuid (0)) /* sanity check: we cannot get root back */
233         exit (1);
234
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"))
238         goto error;
239     argv[0][strlen (argv[0]) - strlen ("-wrapper")] = '\0';
240
241     (void)argc;
242     if (execvp (argv[0], argv))
243         perror (argv[0]);
244
245 error:
246     close (pair[0]);
247     if (pair[1] != -1)
248         close (pair[1]);
249     return 1;
250 }