1 /*****************************************************************************
2 * rootbind.c: bind to reserved ports through the root wrapper
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 *****************************************************************************/
25 #if !defined (WIN32) && !defined (SYS_BEOS)
26 # define ENABLE_ROOTWRAP 1
31 int rootwrap_bind (int, int, int, const struct sockaddr *, size_t);
35 #ifdef ENABLE_ROOTWRAP
39 #include <sys/types.h>
41 #include <sys/socket.h>
44 #include <netinet/in.h>
48 * Receive a file descriptor from another process
50 static int recv_fd (int p)
56 char buf[CMSG_SPACE (sizeof (fd))];
62 hdr.msg_control = buf;
63 hdr.msg_controllen = sizeof (buf);
66 iov.iov_len = sizeof (val);
68 if (recvmsg (p, &hdr, 0) != sizeof (val))
71 for (cmsg = CMSG_FIRSTHDR (&hdr); cmsg != NULL;
72 cmsg = CMSG_NXTHDR (&hdr, cmsg))
74 if ((cmsg->cmsg_level == SOL_SOCKET)
75 && (cmsg->cmsg_type = SCM_RIGHTS)
76 && (cmsg->cmsg_len >= CMSG_LEN (sizeof (fd))))
78 memcpy (&fd, CMSG_DATA (cmsg), sizeof (fd));
88 * Tries to obtain a bound TCP socket from the root process
90 int rootwrap_bind (int family, int socktype, int protocol,
91 const struct sockaddr *addr, size_t alen)
93 /* can't use libvlc */
94 static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
95 struct sockaddr_storage ss;
98 const char *sockenv = getenv ("VLC_ROOTWRAP_SOCK");
110 if (alen < sizeof (struct sockaddr_in))
119 if (alen < sizeof (struct sockaddr_in6))
128 errno = EAFNOSUPPORT;
132 if (family != addr->sa_family)
134 errno = EAFNOSUPPORT;
138 /* Only TCP is implemented at the moment */
139 if ((socktype != SOCK_STREAM)
140 || (protocol && (protocol != IPPROTO_TCP)))
146 memset (&ss, 0, sizeof (ss));
147 memcpy (&ss, addr, alen > sizeof (ss) ? sizeof (ss) : alen);
149 pthread_mutex_lock (&mutex);
150 if (send (sock, &ss, sizeof (ss), 0) != sizeof (ss))
154 pthread_mutex_unlock (&mutex);
159 int rootwrap_bind (int family, int socktype, int protocol,
160 const struct sockaddr *addr, size_t alen)
171 #endif /* ENABLE_ROOTWRAP */