Qt: Don't post the initial event of THEMIM, force it.
[vlc.git] / bin / rootwrap.c
blobcff9476b5d8770b0ef197ce83be47002b55e8cd2
1 /*****************************************************************************
2 * rootwrap.c
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 *****************************************************************************/
21 #if HAVE_CONFIG_H
22 # include <config.h>
23 #endif
25 #include <stdlib.h> /* exit() */
26 #include <stdio.h>
27 #include <string.h>
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>
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
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
59 static inline int is_allowed_port (uint16_t port)
61 port = ntohs (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;
71 /**
72 * Send a file descriptor to another process
74 static int send_fd (int p, int fd)
76 struct msghdr hdr;
77 struct iovec iov;
78 struct cmsghdr *cmsg;
79 char buf[CMSG_SPACE (sizeof (fd))];
80 int val = 0;
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);
89 iov.iov_base = &val;
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))
112 unsigned len;
113 int sock;
115 switch (ss.ss_family)
117 case AF_INET:
118 if (!is_allowed_port (((struct sockaddr_in *)&ss)->sin_port))
120 if (send_err (fd, EACCES))
121 return;
122 continue;
124 len = sizeof (struct sockaddr_in);
125 break;
127 #ifdef AF_INET6
128 case AF_INET6:
129 if (!is_allowed_port (((struct sockaddr_in6 *)&ss)->sin6_port))
131 if (send_err (fd, EACCES))
132 return;
133 continue;
135 len = sizeof (struct sockaddr_in6);
136 break;
137 #endif
139 default:
140 if (send_err (fd, EAFNOSUPPORT))
141 return;
142 continue;
145 sock = socket (ss.ss_family, SOCK_STREAM, IPPROTO_TCP);
146 if (sock != -1)
148 const int val = 1;
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)
157 send_fd (fd, sock);
158 close (sock);
159 continue;
162 send_err (fd, errno);
166 /* TODO?
167 * - use libcap if available,
168 * - call chroot
171 int main (int argc, char *argv[])
173 /* Support for dynamically opening RTSP, HTTP and HTTP/SSL ports */
174 int pair[2];
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 */
181 pid_t pid = fork ();
182 switch (pid)
184 case -1:
185 goto error;
187 case 0:
189 int null = open ("/dev/null", O_RDWR);
190 if (null != -1)
192 dup2 (null, 0);
193 dup2 (null, 1);
194 dup2 (null, 2);
195 close (null);
197 close (pair[0]);
198 setsid ();
199 rootprocess (pair[1]);
200 exit (0);
204 close (pair[1]);
205 pair[1] = -1;
207 char buf[21];
208 snprintf (buf, sizeof (buf), "%d", pair[0]);
209 setenv ("VLC_ROOTWRAP_SOCK", buf, 1);
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
218 uid_t uid = getuid ();
219 if (uid == 0)
221 const char *sudo = getenv ("SUDO_UID");
222 if (sudo)
223 uid = atoi (sudo);
225 if (uid == 0)
227 fprintf (stderr, "Cannot determine unprivileged user for VLC!\n");
228 exit (1);
230 setuid (uid);
232 if (!setuid (0)) /* sanity check: we cannot get root back */
233 exit (1);
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';
241 (void)argc;
242 if (execvp (argv[0], argv))
243 perror (argv[0]);
245 error:
246 close (pair[0]);
247 if (pair[1] != -1)
248 close (pair[1]);
249 return 1;