| 1 |
|
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 |
|
|---|
| 15 |
|
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 18 |
|
|---|
| 19 |
|
|---|
| 20 |
|
|---|
| 21 |
|
|---|
| 22 |
|
|---|
| 23 |
|
|---|
| 24 |
|
|---|
| 25 |
|
|---|
| 26 |
|
|---|
| 27 |
#ifdef HAVE_CONFIG_H |
|---|
| 28 |
# include "config.h" |
|---|
| 29 |
#endif |
|---|
| 30 |
|
|---|
| 31 |
#include <vlc_common.h> |
|---|
| 32 |
|
|---|
| 33 |
#include <ctype.h> |
|---|
| 34 |
#include <vlc_acl.h> |
|---|
| 35 |
|
|---|
| 36 |
#include <errno.h> |
|---|
| 37 |
|
|---|
| 38 |
#include <vlc_network.h> |
|---|
| 39 |
#include <vlc_charset.h> |
|---|
| 40 |
|
|---|
| 41 |
|
|---|
| 42 |
typedef struct vlc_acl_entry_t |
|---|
| 43 |
{ |
|---|
| 44 |
uint8_t host[17]; |
|---|
| 45 |
uint8_t i_bytes_match; |
|---|
| 46 |
uint8_t i_bits_mask; |
|---|
| 47 |
bool b_allow; |
|---|
| 48 |
} vlc_acl_entry_t; |
|---|
| 49 |
|
|---|
| 50 |
struct vlc_acl_t |
|---|
| 51 |
{ |
|---|
| 52 |
vlc_object_t *p_owner; |
|---|
| 53 |
unsigned i_size; |
|---|
| 54 |
vlc_acl_entry_t *p_entries; |
|---|
| 55 |
bool b_allow_default; |
|---|
| 56 |
}; |
|---|
| 57 |
|
|---|
| 58 |
static int ACL_Resolve( vlc_object_t *p_this, uint8_t *p_bytes, |
|---|
| 59 |
const char *psz_ip ) |
|---|
| 60 |
{ |
|---|
| 61 |
struct addrinfo hints, *res; |
|---|
| 62 |
int i_family; |
|---|
| 63 |
|
|---|
| 64 |
memset (&hints, 0, sizeof (hints)); |
|---|
| 65 |
hints.ai_socktype = SOCK_STREAM; |
|---|
| 66 |
hints.ai_flags = AI_NUMERICHOST; |
|---|
| 67 |
|
|---|
| 68 |
if( vlc_getaddrinfo( p_this, psz_ip, 0, &hints, &res ) ) |
|---|
| 69 |
{ |
|---|
| 70 |
msg_Err( p_this, "invalid IP address %s", psz_ip ); |
|---|
| 71 |
return -1; |
|---|
| 72 |
} |
|---|
| 73 |
|
|---|
| 74 |
p_bytes[16] = 0; |
|---|
| 75 |
|
|---|
| 76 |
i_family = res->ai_addr->sa_family; |
|---|
| 77 |
switch( i_family ) |
|---|
| 78 |
{ |
|---|
| 79 |
case AF_INET: |
|---|
| 80 |
{ |
|---|
| 81 |
struct sockaddr_in *addr; |
|---|
| 82 |
|
|---|
| 83 |
addr = (struct sockaddr_in *)res->ai_addr; |
|---|
| 84 |
memset( p_bytes, 0, 12 ); |
|---|
| 85 |
memcpy( p_bytes + 12, &addr->sin_addr, 4 ); |
|---|
| 86 |
break; |
|---|
| 87 |
} |
|---|
| 88 |
|
|---|
| 89 |
#if defined (HAVE_GETADDRINFO) || defined (WIN32) |
|---|
| 90 |
|
|---|
| 91 |
|
|---|
| 92 |
case AF_INET6: |
|---|
| 93 |
{ |
|---|
| 94 |
struct sockaddr_in6 *addr; |
|---|
| 95 |
|
|---|
| 96 |
addr = (struct sockaddr_in6 *)res->ai_addr; |
|---|
| 97 |
memcpy( p_bytes, &addr->sin6_addr, 16 ); |
|---|
| 98 |
break; |
|---|
| 99 |
} |
|---|
| 100 |
#endif |
|---|
| 101 |
|
|---|
| 102 |
default: |
|---|
| 103 |
msg_Err( p_this, "unknown address family" ); |
|---|
| 104 |
vlc_freeaddrinfo( res ); |
|---|
| 105 |
return -1; |
|---|
| 106 |
} |
|---|
| 107 |
|
|---|
| 108 |
vlc_freeaddrinfo( res ); |
|---|
| 109 |
return i_family; |
|---|
| 110 |
} |
|---|
| 111 |
|
|---|
| 112 |
|
|---|
| 113 |
|
|---|
| 114 |
|
|---|
| 115 |
|
|---|
| 116 |
|
|---|
| 117 |
|
|---|
| 118 |
|
|---|
| 119 |
|
|---|
| 120 |
|
|---|
| 121 |
|
|---|
| 122 |
|
|---|
| 123 |
int ACL_Check( vlc_acl_t *p_acl, const char *psz_ip ) |
|---|
| 124 |
{ |
|---|
| 125 |
const vlc_acl_entry_t *p_cur, *p_end; |
|---|
| 126 |
uint8_t host[17]; |
|---|
| 127 |
|
|---|
| 128 |
if( p_acl == NULL ) |
|---|
| 129 |
return -1; |
|---|
| 130 |
|
|---|
| 131 |
p_cur = p_acl->p_entries; |
|---|
| 132 |
p_end = p_cur + p_acl->i_size; |
|---|
| 133 |
|
|---|
| 134 |
if( ACL_Resolve( p_acl->p_owner, host, psz_ip ) < 0 ) |
|---|
| 135 |
return -1; |
|---|
| 136 |
|
|---|
| 137 |
while (p_cur < p_end) |
|---|
| 138 |
{ |
|---|
| 139 |
unsigned i; |
|---|
| 140 |
|
|---|
| 141 |
i = p_cur->i_bytes_match; |
|---|
| 142 |
if( (memcmp( p_cur->host, host, i ) == 0) |
|---|
| 143 |
&& (((p_cur->host[i] ^ host[i]) & p_cur->i_bits_mask) == 0) ) |
|---|
| 144 |
return !p_cur->b_allow; |
|---|
| 145 |
|
|---|
| 146 |
p_cur++; |
|---|
| 147 |
} |
|---|
| 148 |
|
|---|
| 149 |
return !p_acl->b_allow_default; |
|---|
| 150 |
} |
|---|
| 151 |
|
|---|
| 152 |
|
|---|
| 153 |
|
|---|
| 154 |
|
|---|
| 155 |
|
|---|
| 156 |
int ACL_AddNet( vlc_acl_t *p_acl, const char *psz_ip, int i_len, |
|---|
| 157 |
bool b_allow ) |
|---|
| 158 |
{ |
|---|
| 159 |
vlc_acl_entry_t *p_ent; |
|---|
| 160 |
unsigned i_size; |
|---|
| 161 |
div_t d; |
|---|
| 162 |
int i_family; |
|---|
| 163 |
|
|---|
| 164 |
i_size = p_acl->i_size; |
|---|
| 165 |
p_ent = (vlc_acl_entry_t *)realloc( p_acl->p_entries, |
|---|
| 166 |
++p_acl->i_size * sizeof( *p_ent ) ); |
|---|
| 167 |
|
|---|
| 168 |
if( p_ent == NULL ) |
|---|
| 169 |
return -1; |
|---|
| 170 |
|
|---|
| 171 |
p_acl->p_entries = p_ent; |
|---|
| 172 |
p_ent += i_size; |
|---|
| 173 |
|
|---|
| 174 |
i_family = ACL_Resolve( p_acl->p_owner, p_ent->host, psz_ip ); |
|---|
| 175 |
if( i_family < 0 ) |
|---|
| 176 |
{ |
|---|
| 177 |
|
|---|
| 178 |
|
|---|
| 179 |
|
|---|
| 180 |
|
|---|
| 181 |
p_acl->i_size--; |
|---|
| 182 |
return -1; |
|---|
| 183 |
} |
|---|
| 184 |
|
|---|
| 185 |
if( i_len >= 0 ) |
|---|
| 186 |
{ |
|---|
| 187 |
if( i_family == AF_INET ) |
|---|
| 188 |
i_len += 96; |
|---|
| 189 |
|
|---|
| 190 |
if( i_len > 128 ) |
|---|
| 191 |
i_len = 128; |
|---|
| 192 |
} |
|---|
| 193 |
else |
|---|
| 194 |
i_len = 128; |
|---|
| 195 |
|
|---|
| 196 |
d = div( i_len, 8 ); |
|---|
| 197 |
p_ent->i_bytes_match = d.quot; |
|---|
| 198 |
p_ent->i_bits_mask = 0xff << (8 - d.rem); |
|---|
| 199 |
|
|---|
| 200 |
p_ent->b_allow = b_allow; |
|---|
| 201 |
return 0; |
|---|
| 202 |
} |
|---|
| 203 |
|
|---|
| 204 |
|
|---|
| 205 |
|
|---|
| 206 |
|
|---|
| 207 |
|
|---|
| 208 |
|
|---|
| 209 |
|
|---|
| 210 |
|
|---|
| 211 |
|
|---|
| 212 |
|
|---|
| 213 |
vlc_acl_t *__ACL_Create( vlc_object_t *p_this, bool b_allow ) |
|---|
| 214 |
{ |
|---|
| 215 |
vlc_acl_t *p_acl; |
|---|
| 216 |
|
|---|
| 217 |
p_acl = (vlc_acl_t *)malloc( sizeof( *p_acl ) ); |
|---|
| 218 |
if( p_acl == NULL ) |
|---|
| 219 |
return NULL; |
|---|
| 220 |
|
|---|
| 221 |
vlc_object_yield( p_this ); |
|---|
| 222 |
p_acl->p_owner = p_this; |
|---|
| 223 |
p_acl->i_size = 0; |
|---|
| 224 |
p_acl->p_entries = NULL; |
|---|
| 225 |
p_acl->b_allow_default = b_allow; |
|---|
| 226 |
|
|---|
| 227 |
return p_acl; |
|---|
| 228 |
} |
|---|
| 229 |
|
|---|
| 230 |
|
|---|
| 231 |
|
|---|
| 232 |
|
|---|
| 233 |
|
|---|
| 234 |
|
|---|
| 235 |
|
|---|
| 236 |
|
|---|
| 237 |
|
|---|
| 238 |
|
|---|
| 239 |
vlc_acl_t *__ACL_Duplicate( vlc_object_t *p_this, const vlc_acl_t *p_acl ) |
|---|
| 240 |
{ |
|---|
| 241 |
vlc_acl_t *p_dupacl; |
|---|
| 242 |
|
|---|
| 243 |
if( p_acl == NULL ) |
|---|
| 244 |
return NULL; |
|---|
| 245 |
|
|---|
| 246 |
p_dupacl = (vlc_acl_t *)malloc( sizeof( *p_dupacl ) ); |
|---|
| 247 |
if( p_dupacl == NULL ) |
|---|
| 248 |
return NULL; |
|---|
| 249 |
|
|---|
| 250 |
if( p_acl->i_size ) |
|---|
| 251 |
{ |
|---|
| 252 |
p_dupacl->p_entries = (vlc_acl_entry_t *) |
|---|
| 253 |
malloc( p_acl->i_size * sizeof( vlc_acl_entry_t ) ); |
|---|
| 254 |
|
|---|
| 255 |
if( p_dupacl->p_entries == NULL ) |
|---|
| 256 |
{ |
|---|
| 257 |
free( p_dupacl ); |
|---|
| 258 |
return NULL; |
|---|
| 259 |
} |
|---|
| 260 |
|
|---|
| 261 |
memcpy( p_dupacl->p_entries, p_acl->p_entries, |
|---|
| 262 |
p_acl->i_size * sizeof( vlc_acl_entry_t ) ); |
|---|
| 263 |
} |
|---|
| 264 |
else |
|---|
| 265 |
p_dupacl->p_entries = NULL; |
|---|
| 266 |
|
|---|
| 267 |
vlc_object_yield( p_this ); |
|---|
| 268 |
p_dupacl->p_owner = p_this; |
|---|
| 269 |
p_dupacl->i_size = p_acl->i_size; |
|---|
| 270 |
p_dupacl->b_allow_default = p_acl->b_allow_default; |
|---|
| 271 |
|
|---|
| 272 |
return p_dupacl; |
|---|
| 273 |
} |
|---|
| 274 |
|
|---|
| 275 |
|
|---|
| 276 |
|
|---|
| 277 |
|
|---|
| 278 |
|
|---|
| 279 |
void ACL_Destroy( vlc_acl_t *p_acl ) |
|---|
| 280 |
{ |
|---|
| 281 |
if( p_acl != NULL ) |
|---|
| 282 |
{ |
|---|
| 283 |
if( p_acl->p_entries != NULL ) |
|---|
| 284 |
free( p_acl->p_entries ); |
|---|
| 285 |
|
|---|
| 286 |
vlc_object_release( p_acl->p_owner ); |
|---|
| 287 |
free( p_acl ); |
|---|
| 288 |
} |
|---|
| 289 |
} |
|---|
| 290 |
|
|---|
| 291 |
|
|---|
| 292 |
|
|---|
| 293 |
|
|---|
| 294 |
|
|---|
| 295 |
|
|---|
| 296 |
|
|---|
| 297 |
|
|---|
| 298 |
|
|---|
| 299 |
|
|---|
| 300 |
int ACL_LoadFile( vlc_acl_t *p_acl, const char *psz_path ) |
|---|
| 301 |
{ |
|---|
| 302 |
FILE *file; |
|---|
| 303 |
|
|---|
| 304 |
if( p_acl == NULL ) |
|---|
| 305 |
return -1; |
|---|
| 306 |
|
|---|
| 307 |
file = utf8_fopen( psz_path, "r" ); |
|---|
| 308 |
if( file == NULL ) |
|---|
| 309 |
return -1; |
|---|
| 310 |
|
|---|
| 311 |
msg_Dbg( p_acl->p_owner, "find .hosts in dir=%s", psz_path ); |
|---|
| 312 |
|
|---|
| 313 |
while( !feof( file ) ) |
|---|
| 314 |
{ |
|---|
| 315 |
char line[1024], *psz_ip, *ptr; |
|---|
| 316 |
|
|---|
| 317 |
if( fgets( line, sizeof( line ), file ) == NULL ) |
|---|
| 318 |
{ |
|---|
| 319 |
if( ferror( file ) ) |
|---|
| 320 |
{ |
|---|
| 321 |
msg_Err( p_acl->p_owner, "error reading %s : %m", psz_path ); |
|---|
| 322 |
goto error; |
|---|
| 323 |
} |
|---|
| 324 |
continue; |
|---|
| 325 |
} |
|---|
| 326 |
|
|---|
| 327 |
|
|---|
| 328 |
psz_ip = line; |
|---|
| 329 |
|
|---|
| 330 |
|
|---|
| 331 |
while( isspace( *psz_ip ) ) |
|---|
| 332 |
psz_ip++; |
|---|
| 333 |
|
|---|
| 334 |
if( *psz_ip == '\0' ) |
|---|
| 335 |
continue; |
|---|
| 336 |
|
|---|
| 337 |
ptr = strchr( psz_ip, '\n' ); |
|---|
| 338 |
if( ptr == NULL ) |
|---|
| 339 |
{ |
|---|
| 340 |
msg_Warn( p_acl->p_owner, "skipping overly long line in %s", |
|---|
| 341 |
psz_path); |
|---|
| 342 |
do |
|---|
| 343 |
{ |
|---|
| 344 |
if( fgets( line, sizeof( line ), file ) == NULL ) |
|---|
| 345 |
{ |
|---|
| 346 |
if( ferror( file ) ) |
|---|
| 347 |
{ |
|---|
| 348 |
msg_Err( p_acl->p_owner, "error reading %s : %m", |
|---|
| 349 |
psz_path ); |
|---|
| 350 |
} |
|---|
| 351 |
goto error; |
|---|
| 352 |
} |
|---|
| 353 |
} |
|---|
| 354 |
while( strchr( line, '\n' ) == NULL); |
|---|
| 355 |
|
|---|
| 356 |
continue; |
|---|
| 357 |
} |
|---|
| 358 |
|
|---|
| 359 |
|
|---|
| 360 |
if( *psz_ip == '#' ) |
|---|
| 361 |
continue; |
|---|
| 362 |
|
|---|
| 363 |
|
|---|
| 364 |
|
|---|
| 365 |
for( ptr = psz_ip; ( *ptr != '#' ) && !isspace( *ptr ); ptr++ ); |
|---|
| 366 |
|
|---|
| 367 |
*ptr = '\0'; |
|---|
| 368 |
|
|---|
| 369 |
msg_Dbg( p_acl->p_owner, "restricted to %s", psz_ip ); |
|---|
| 370 |
|
|---|
| 371 |
ptr = strchr( psz_ip, '/' ); |
|---|
| 372 |
if( ptr != NULL ) |
|---|
| 373 |
*ptr++ = '\0'; |
|---|
| 374 |
|
|---|
| 375 |
if( (ptr != NULL) |
|---|
| 376 |
? ACL_AddNet( p_acl, psz_ip, atoi( ptr ), true ) |
|---|
| 377 |
: ACL_AddHost( p_acl, psz_ip, true ) ) |
|---|
| 378 |
{ |
|---|
| 379 |
msg_Err( p_acl->p_owner, "cannot add ACL from %s", psz_path ); |
|---|
| 380 |
continue; |
|---|
| 381 |
} |
|---|
| 382 |
} |
|---|
| 383 |
|
|---|
| 384 |
fclose( file ); |
|---|
| 385 |
return 0; |
|---|
| 386 |
|
|---|
| 387 |
error: |
|---|
| 388 |
fclose( file ); |
|---|
| 389 |
return -1; |
|---|
| 390 |
} |
|---|
| 391 |
|
|---|