]> git.sesse.net Git - vlc/blob - src/network/rootbind.c
Qt: CaptureOpenPanel: check and prefill with usual devices
[vlc] / src / network / rootbind.c
1 /*****************************************************************************
2  * rootbind.c: bind to reserved ports through the root wrapper
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 #ifdef HAVE_CONFIG_H
22 # include <config.h>
23 #endif
24
25 #define _XPG4_2 /* ancilliary data on Solaris */
26
27 #if !defined (WIN32) && !defined (SYS_BEOS)
28 # define ENABLE_ROOTWRAP 1
29 #endif
30
31 #include <stddef.h>
32 struct sockaddr;
33 int rootwrap_bind (int, int, int, const struct sockaddr *, size_t);
34
35 #include <errno.h>
36
37 #ifdef ENABLE_ROOTWRAP
38
39 #include <string.h>
40 #include <stdlib.h>
41
42 #include <sys/types.h>
43 #include <unistd.h>
44 #include <sys/socket.h>
45 #include <sys/uio.h>
46 #include <sys/un.h>
47 #include <netinet/in.h>
48 #include <pthread.h>
49
50 /* Required yet non-standard cmsg functions */
51 #ifndef CMSG_ALIGN
52 # define CMSG_ALIGN(len) (((len) + sizeof(intptr_t)-1) & ~(sizeof(intptr_t)-1))
53 #endif
54 #ifndef CMSG_SPACE
55 # define CMSG_SPACE(len) (CMSG_ALIGN(sizeof(struct cmsghdr)) + CMSG_ALIGN(len))
56 #endif
57 #ifndef CMSG_LEN
58 # define CMSG_LEN(len) (CMSG_ALIGN(sizeof(struct cmsghdr)) + (len))
59 #endif
60
61 /**
62  * Receive a file descriptor from another process
63  */
64 static int recv_fd (int p)
65 {
66     struct msghdr hdr;
67     struct iovec iov;
68     struct cmsghdr *cmsg;
69     int val, fd;
70     char buf[CMSG_SPACE (sizeof (fd))];
71
72     hdr.msg_name = NULL;
73     hdr.msg_namelen = 0;
74     hdr.msg_iov = &iov;
75     hdr.msg_iovlen = 1;
76     hdr.msg_control = buf;
77     hdr.msg_controllen = sizeof (buf);
78
79     iov.iov_base = &val;
80     iov.iov_len = sizeof (val);
81
82     if (recvmsg (p, &hdr, 0) != sizeof (val))
83         return -1;
84
85     for (cmsg = CMSG_FIRSTHDR (&hdr); cmsg != NULL;
86          cmsg = CMSG_NXTHDR (&hdr, cmsg))
87     {
88         if ((cmsg->cmsg_level == SOL_SOCKET)
89          && (cmsg->cmsg_type = SCM_RIGHTS)
90          && (cmsg->cmsg_len >= CMSG_LEN (sizeof (fd))))
91         {
92             memcpy (&fd, CMSG_DATA (cmsg), sizeof (fd));
93             return fd;
94         }
95     }
96
97     errno = val;
98     return -1;
99 }
100
101 /**
102  * Tries to obtain a bound TCP socket from the root process
103  */
104 int rootwrap_bind (int family, int socktype, int protocol,
105                    const struct sockaddr *addr, size_t alen)
106 {
107     /* can't use libvlc */
108     static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
109     struct sockaddr_storage ss;
110     int fd, sock = -1;
111
112     const char *sockenv = getenv ("VLC_ROOTWRAP_SOCK");
113     if (sockenv != NULL)
114         sock = atoi (sockenv);
115     if (sock == -1)
116     {
117         errno = EACCES;
118         return -1;
119     }
120
121     switch (family)
122     {
123         case AF_INET:
124             if (alen < sizeof (struct sockaddr_in))
125             {
126                 errno = EINVAL;
127                 return -1;
128             }
129             break;
130
131 #ifdef AF_INET6
132         case AF_INET6:
133             if (alen < sizeof (struct sockaddr_in6))
134             {
135                 errno = EINVAL;
136                 return -1;
137             }
138             break;
139 #endif
140
141         default:
142             errno = EAFNOSUPPORT;
143             return -1;
144     }
145
146     if (family != addr->sa_family)
147     {
148         errno = EAFNOSUPPORT;
149         return -1;
150     }
151
152     /* Only TCP is implemented at the moment */
153     if ((socktype != SOCK_STREAM)
154      || (protocol && (protocol != IPPROTO_TCP)))
155     {
156         errno = EACCES;
157         return -1;
158     }
159
160     memset (&ss, 0, sizeof (ss));
161     memcpy (&ss, addr, (alen > sizeof (ss)) ? sizeof (ss) : alen);
162
163     pthread_mutex_lock (&mutex);
164     if (send (sock, &ss, sizeof (ss), 0) != sizeof (ss))
165         return -1;
166
167     fd = recv_fd (sock);
168     pthread_mutex_unlock (&mutex);
169     return fd;
170 }
171
172 #else
173 int rootwrap_bind (int family, int socktype, int protocol,
174                    const struct sockaddr *addr, size_t alen)
175 {
176     (void)family;
177     (void)socktype;
178     (void)protocol;
179     (void)addr;
180     (void)alen;
181     errno = EACCES;
182     return -1;
183 }
184
185 #endif /* ENABLE_ROOTWRAP */