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