| 1 |
|
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 |
|
|---|
| 15 |
|
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 18 |
|
|---|
| 19 |
|
|---|
| 20 |
|
|---|
| 21 |
#if HAVE_CONFIG_H |
|---|
| 22 |
# include <config.h> |
|---|
| 23 |
#endif |
|---|
| 24 |
|
|---|
| 25 |
#if !defined (WIN32) && !defined (SYS_BEOS) |
|---|
| 26 |
# define ENABLE_ROOTWRAP 1 |
|---|
| 27 |
#endif |
|---|
| 28 |
|
|---|
| 29 |
#include <stddef.h> |
|---|
| 30 |
struct sockaddr; |
|---|
| 31 |
int rootwrap_bind (int, int, int, const struct sockaddr *, size_t); |
|---|
| 32 |
|
|---|
| 33 |
#include <errno.h> |
|---|
| 34 |
|
|---|
| 35 |
#ifdef ENABLE_ROOTWRAP |
|---|
| 36 |
|
|---|
| 37 |
#include <string.h> |
|---|
| 38 |
#include <stdlib.h> |
|---|
| 39 |
|
|---|
| 40 |
#include <sys/types.h> |
|---|
| 41 |
#include <unistd.h> |
|---|
| 42 |
#include <sys/socket.h> |
|---|
| 43 |
#include <sys/uio.h> |
|---|
| 44 |
#include <sys/un.h> |
|---|
| 45 |
#include <netinet/in.h> |
|---|
| 46 |
#include <pthread.h> |
|---|
| 47 |
|
|---|
| 48 |
|
|---|
| 49 |
|
|---|
| 50 |
|
|---|
| 51 |
static int recv_fd (int p) |
|---|
| 52 |
{ |
|---|
| 53 |
struct msghdr hdr; |
|---|
| 54 |
struct iovec iov; |
|---|
| 55 |
struct cmsghdr *cmsg; |
|---|
| 56 |
int val, fd; |
|---|
| 57 |
char buf[CMSG_SPACE (sizeof (fd))]; |
|---|
| 58 |
|
|---|
| 59 |
hdr.msg_name = NULL; |
|---|
| 60 |
hdr.msg_namelen = 0; |
|---|
| 61 |
hdr.msg_iov = &iov; |
|---|
| 62 |
hdr.msg_iovlen = 1; |
|---|
| 63 |
hdr.msg_control = buf; |
|---|
| 64 |
hdr.msg_controllen = sizeof (buf); |
|---|
| 65 |
|
|---|
| 66 |
iov.iov_base = &val; |
|---|
| 67 |
iov.iov_len = sizeof (val); |
|---|
| 68 |
|
|---|
| 69 |
if (recvmsg (p, &hdr, 0) != sizeof (val)) |
|---|
| 70 |
return -1; |
|---|
| 71 |
|
|---|
| 72 |
for (cmsg = CMSG_FIRSTHDR (&hdr); cmsg != NULL; |
|---|
| 73 |
cmsg = CMSG_NXTHDR (&hdr, cmsg)) |
|---|
| 74 |
{ |
|---|
| 75 |
if ((cmsg->cmsg_level == SOL_SOCKET) |
|---|
| 76 |
&& (cmsg->cmsg_type = SCM_RIGHTS) |
|---|
| 77 |
&& (cmsg->cmsg_len >= CMSG_LEN (sizeof (fd)))) |
|---|
| 78 |
{ |
|---|
| 79 |
memcpy (&fd, CMSG_DATA (cmsg), sizeof (fd)); |
|---|
| 80 |
return fd; |
|---|
| 81 |
} |
|---|
| 82 |
} |
|---|
| 83 |
|
|---|
| 84 |
errno = val; |
|---|
| 85 |
return -1; |
|---|
| 86 |
} |
|---|
| 87 |
|
|---|
| 88 |
|
|---|
| 89 |
|
|---|
| 90 |
|
|---|
| 91 |
int rootwrap_bind (int family, int socktype, int protocol, |
|---|
| 92 |
const struct sockaddr *addr, size_t alen) |
|---|
| 93 |
{ |
|---|
| 94 |
|
|---|
| 95 |
static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; |
|---|
| 96 |
struct sockaddr_storage ss; |
|---|
| 97 |
int fd, sock = -1; |
|---|
| 98 |
|
|---|
| 99 |
const char *sockenv = getenv ("VLC_ROOTWRAP_SOCK"); |
|---|
| 100 |
if (sockenv != NULL) |
|---|
| 101 |
sock = atoi (sockenv); |
|---|
| 102 |
if (sock == -1) |
|---|
| 103 |
{ |
|---|
| 104 |
errno = EACCES; |
|---|
| 105 |
return -1; |
|---|
| 106 |
} |
|---|
| 107 |
|
|---|
| 108 |
switch (family) |
|---|
| 109 |
{ |
|---|
| 110 |
case AF_INET: |
|---|
| 111 |
if (alen < sizeof (struct sockaddr_in)) |
|---|
| 112 |
{ |
|---|
| 113 |
errno = EINVAL; |
|---|
| 114 |
return -1; |
|---|
| 115 |
} |
|---|
| 116 |
break; |
|---|
| 117 |
|
|---|
| 118 |
#ifdef AF_INET6 |
|---|
| 119 |
case AF_INET6: |
|---|
| 120 |
if (alen < sizeof (struct sockaddr_in6)) |
|---|
| 121 |
{ |
|---|
| 122 |
errno = EINVAL; |
|---|
| 123 |
return -1; |
|---|
| 124 |
} |
|---|
| 125 |
break; |
|---|
| 126 |
#endif |
|---|
| 127 |
|
|---|
| 128 |
default: |
|---|
| 129 |
errno = EAFNOSUPPORT; |
|---|
| 130 |
return -1; |
|---|
| 131 |
} |
|---|
| 132 |
|
|---|
| 133 |
if (family != addr->sa_family) |
|---|
| 134 |
{ |
|---|
| 135 |
errno = EAFNOSUPPORT; |
|---|
| 136 |
return -1; |
|---|
| 137 |
} |
|---|
| 138 |
|
|---|
| 139 |
|
|---|
| 140 |
if ((socktype != SOCK_STREAM) |
|---|
| 141 |
|| (protocol && (protocol != IPPROTO_TCP))) |
|---|
| 142 |
{ |
|---|
| 143 |
errno = EACCES; |
|---|
| 144 |
return -1; |
|---|
| 145 |
} |
|---|
| 146 |
|
|---|
| 147 |
memset (&ss, 0, sizeof (ss)); |
|---|
| 148 |
memcpy (&ss, addr, (alen > sizeof (ss)) ? sizeof (ss) : alen); |
|---|
| 149 |
|
|---|
| 150 |
pthread_mutex_lock (&mutex); |
|---|
| 151 |
if (send (sock, &ss, sizeof (ss), 0) != sizeof (ss)) |
|---|
| 152 |
return -1; |
|---|
| 153 |
|
|---|
| 154 |
fd = recv_fd (sock); |
|---|
| 155 |
pthread_mutex_unlock (&mutex); |
|---|
| 156 |
return fd; |
|---|
| 157 |
} |
|---|
| 158 |
|
|---|
| 159 |
#else |
|---|
| 160 |
int rootwrap_bind (int family, int socktype, int protocol, |
|---|
| 161 |
const struct sockaddr *addr, size_t alen) |
|---|
| 162 |
{ |
|---|
| 163 |
(void)family; |
|---|
| 164 |
(void)socktype; |
|---|
| 165 |
(void)protocol; |
|---|
| 166 |
(void)addr; |
|---|
| 167 |
(void)alen; |
|---|
| 168 |
errno = EACCES; |
|---|
| 169 |
return -1; |
|---|
| 170 |
} |
|---|
| 171 |
|
|---|
| 172 |
#endif |
|---|