]> git.sesse.net Git - vlc/blob - src/network/rootbind.c
libvlc_MetaRequest: increment item i_preparse_depth
[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 it
7  * under the terms of the GNU Lesser General Public License as published by
8  * the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * 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 (__OS2__)
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 #if defined(__OS2__) && !defined(ALIGN)
62 /* CMSG_NXTHDR requires this */
63 # define ALIGN(p) _ALIGN(p)
64 #endif
65
66 /**
67  * Receive a file descriptor from another process
68  */
69 static int recv_fd (int p)
70 {
71     struct msghdr hdr;
72     struct iovec iov;
73     struct cmsghdr *cmsg;
74     int val, fd;
75     char buf[CMSG_SPACE (sizeof (fd))];
76
77     hdr.msg_name = NULL;
78     hdr.msg_namelen = 0;
79     hdr.msg_iov = &iov;
80     hdr.msg_iovlen = 1;
81     hdr.msg_control = buf;
82     hdr.msg_controllen = sizeof (buf);
83
84     iov.iov_base = &val;
85     iov.iov_len = sizeof (val);
86
87     if (recvmsg (p, &hdr, 0) != sizeof (val))
88         return -1;
89
90     for (cmsg = CMSG_FIRSTHDR (&hdr); cmsg != NULL;
91          cmsg = CMSG_NXTHDR (&hdr, cmsg))
92     {
93         if ((cmsg->cmsg_level == SOL_SOCKET)
94          && (cmsg->cmsg_type == SCM_RIGHTS)
95          && (cmsg->cmsg_len >= CMSG_LEN (sizeof (fd))))
96         {
97             memcpy (&fd, CMSG_DATA (cmsg), sizeof (fd));
98             return fd;
99         }
100     }
101
102     errno = val;
103     return -1;
104 }
105
106 /**
107  * Tries to obtain a bound TCP socket from the root process
108  */
109 int rootwrap_bind (int family, int socktype, int protocol,
110                    const struct sockaddr *addr, size_t alen)
111 {
112     /* can't use libvlc */
113     static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
114     struct sockaddr_storage ss;
115     int fd, sock = -1;
116
117     const char *sockenv = getenv ("VLC_ROOTWRAP_SOCK");
118     if (sockenv != NULL)
119         sock = atoi (sockenv);
120     if (sock == -1)
121     {
122         errno = EACCES;
123         return -1;
124     }
125
126     switch (family)
127     {
128         case AF_INET:
129             if (alen < sizeof (struct sockaddr_in))
130             {
131                 errno = EINVAL;
132                 return -1;
133             }
134             break;
135
136 #ifdef AF_INET6
137         case AF_INET6:
138             if (alen < sizeof (struct sockaddr_in6))
139             {
140                 errno = EINVAL;
141                 return -1;
142             }
143             break;
144 #endif
145
146         default:
147             errno = EAFNOSUPPORT;
148             return -1;
149     }
150
151     if (family != addr->sa_family)
152     {
153         errno = EAFNOSUPPORT;
154         return -1;
155     }
156
157     /* Only TCP is implemented at the moment */
158     if ((socktype != SOCK_STREAM)
159      || (protocol && (protocol != IPPROTO_TCP)))
160     {
161         errno = EACCES;
162         return -1;
163     }
164
165     memset (&ss, 0, sizeof (ss));
166     memcpy (&ss, addr, (alen > sizeof (ss)) ? sizeof (ss) : alen);
167
168     pthread_mutex_lock (&mutex);
169     if (send (sock, &ss, sizeof (ss), 0) != sizeof (ss))
170     {
171         pthread_mutex_unlock (&mutex);
172         return -1;
173     }
174
175     fd = recv_fd (sock);
176     pthread_mutex_unlock (&mutex);
177     return fd;
178 }
179
180 #else
181 int rootwrap_bind (int family, int socktype, int protocol,
182                    const struct sockaddr *addr, size_t alen)
183 {
184     (void)family;
185     (void)socktype;
186     (void)protocol;
187     (void)addr;
188     (void)alen;
189     errno = EACCES;
190     return -1;
191 }
192
193 #endif /* ENABLE_ROOTWRAP */