]> git.sesse.net Git - vlc/blob - src/stream_output/acl.c
isblank() is not an ANSI function,it should really be tested for
[vlc] / src / stream_output / acl.c
1 /*****************************************************************************
2  * acl.c:
3  *****************************************************************************
4  * Copyright (C) 2005 Rémi Denis-Courmont
5  * $Id$
6  *
7  * Authors: Rémi Denis-Courmont <rem # videolan.org>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
22  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #include <stdlib.h>
28 #include <string.h>
29 #include <vlc/vlc.h>
30
31 #include "vlc_acl.h"
32
33 #if defined( WIN32 ) || defined( UNDER_CE )
34 #   include <winsock2.h>
35 #   include <ws2tcpip.h>
36 #else
37 #   include <sys/socket.h>
38 #   include <netinet/in.h>
39 #   include <netdb.h>
40 #endif
41
42 #include "network.h"
43
44 /* FIXME: rwlock on acl, but libvlc doesn't implement rwlock */
45 /* FIXME: move to src/stream_output/whatever */
46 typedef struct vlc_acl_entry_t
47 {
48     uint8_t    host[17];
49     uint8_t    i_bytes_match;
50     uint8_t    i_bits_mask;
51     vlc_bool_t b_allow;
52 } vlc_acl_entry_t;
53
54 struct vlc_acl_t
55 {
56     vlc_object_t    *p_owner;
57     unsigned         i_size;
58     vlc_acl_entry_t *p_entries;
59     vlc_bool_t       b_allow_default;
60 };
61
62 static int ACL_Resolve( vlc_object_t *p_this, uint8_t *p_bytes,
63                         const char *psz_ip )
64 {
65     struct addrinfo hints = { }, *res;
66     int i_family;
67
68     hints.ai_socktype = SOCK_STREAM; /* doesn't matter */
69     hints.ai_flags = AI_NUMERICHOST;
70
71     if( vlc_getaddrinfo( p_this, psz_ip, 0, &hints, &res ) )
72     {
73         msg_Err( p_this, "invalid IP address %s", psz_ip );
74         return -1;
75     }
76     
77     p_bytes[16] = 0; /* avoids overflowing when i_bytes_match = 16 */
78
79     i_family = res->ai_addr->sa_family;
80     switch( i_family )
81     {
82         case AF_INET:
83         {
84             struct sockaddr_in *addr;
85                 
86             addr = (struct sockaddr_in *)res->ai_addr;
87             memset( p_bytes, 0, 12 );
88             memcpy( p_bytes + 12, &addr->sin_addr, 4 );
89             break;
90         }
91
92 #if defined (HAVE_GETADDRINFO) || defined (WIN32)
93         /* unfortunately many people define AF_INET6
94            though they don't have struct sockaddr_in6 */
95         case AF_INET6:
96         {
97             struct sockaddr_in6 *addr;
98
99             addr = (struct sockaddr_in6 *)res->ai_addr;
100             memcpy( p_bytes, &addr->sin6_addr, 16 );
101             break;
102         }
103 #endif
104
105         default:
106             msg_Err( p_this, "IMPOSSIBLE: unknown address family!" );
107             vlc_freeaddrinfo( res );
108             return -1;
109     }
110
111     vlc_freeaddrinfo( res );
112     return i_family;
113 }
114
115
116 /*
117  * Returns 0 if allowed, 1 if not, -1 on error.
118  */
119 int ACL_Check( vlc_acl_t *p_acl, const char *psz_ip )
120 {
121     const vlc_acl_entry_t *p_cur, *p_end;
122     uint8_t host[17];
123
124     if( p_acl == NULL )
125         return -1;
126
127     p_cur = p_acl->p_entries;
128     p_end = p_cur + p_acl->i_size;
129
130     if( ACL_Resolve( p_acl->p_owner, host, psz_ip ) < 0 )
131         return -1;
132
133     while (p_cur < p_end)
134     {
135         unsigned i;
136
137         i = p_cur->i_bytes_match;
138         if( (memcmp( p_cur->host, host, i ) == 0)
139          && (((p_cur->host[i] ^ host[i]) & p_cur->i_bits_mask) == 0) )
140             return !p_cur->b_allow;
141
142         p_cur++;
143     }
144
145     return !p_acl->b_allow_default;
146 }
147
148 int ACL_AddNet( vlc_acl_t *p_acl, const char *psz_ip, int i_len,
149                 vlc_bool_t b_allow )
150 {
151     vlc_acl_entry_t *p_ent;
152     unsigned i_size;
153     div_t d;
154     int i_family;
155
156     i_size = p_acl->i_size;
157     p_ent = (vlc_acl_entry_t *)realloc( p_acl->p_entries,
158                                         ++p_acl->i_size * sizeof( *p_ent ) );
159
160     if( p_ent == NULL )
161         return -1;
162
163     i_family = ACL_Resolve( p_acl->p_owner, p_ent->host, psz_ip );
164     if( i_family < 0 )
165     {
166         /*
167          * I'm lazy : memory space will be re-used in the next ACL_Add call...
168          * or not.
169          */
170         p_acl->i_size--;
171         return -1;
172     }
173
174     if( i_len >= 0 )
175     {
176         if( i_family == AF_INET )
177             i_len += 96;
178
179         p_acl->p_entries = p_ent;
180         p_ent += i_size;
181
182         if( i_len > 128 )
183             i_len = 128;
184         else
185         if( i_len < 0 )
186             i_len = 0;
187     }
188     else
189         i_len = 128; /* ACL_AddHost */
190
191     d = div( i_len, 8 );
192     p_ent->i_bytes_match = d.quot;
193     p_ent->i_bits_mask = 0xff << (8 - d.rem);
194
195     p_ent->b_allow = b_allow;
196     return 0;
197 }
198
199
200 vlc_acl_t *__ACL_Create( vlc_object_t *p_this, vlc_bool_t b_allow )
201 {
202     vlc_acl_t *p_acl;
203
204     p_acl = (vlc_acl_t *)malloc( sizeof( *p_acl ) );
205     if( p_acl == NULL )
206         return NULL;
207
208     vlc_object_yield( p_this );
209     p_acl->p_owner = p_this;
210     p_acl->i_size = 0;
211     p_acl->p_entries = NULL;
212     p_acl->b_allow_default = b_allow;
213     
214     return p_acl;
215 }
216
217
218 vlc_acl_t *__ACL_Duplicate( vlc_object_t *p_this, const vlc_acl_t *p_acl )
219 {
220     vlc_acl_t *p_dupacl;
221
222     if( p_acl == NULL )
223         return NULL;
224
225     p_dupacl = (vlc_acl_t *)malloc( sizeof( *p_dupacl ) );
226     if( p_dupacl == NULL )
227         return NULL;
228
229     p_dupacl->p_entries = (vlc_acl_entry_t *)
230         malloc( p_acl->i_size * sizeof( vlc_acl_entry_t ) );
231     if( p_dupacl->p_entries == NULL )
232     {
233         free( p_dupacl );
234         return NULL;
235     }   
236
237     vlc_object_yield( p_this );
238     p_dupacl->p_owner = p_this;
239     p_dupacl->i_size = p_acl->i_size;
240     memcpy( p_dupacl->p_entries, p_acl->p_entries,
241             p_dupacl->i_size * sizeof( vlc_acl_entry_t ) );
242     
243     return p_dupacl;
244 }
245
246
247 void ACL_Destroy( vlc_acl_t *p_acl )
248 {
249     if( p_acl != NULL )
250     {
251         if( p_acl->p_entries != NULL )
252             free( p_acl->p_entries );
253
254         vlc_object_release( p_acl->p_owner );
255         free( p_acl );
256     }
257 }
258
259 #ifndef isblank 
260 #   define isblank(c) ((c) == ' ' || (c) == '\t')
261 #endif
262
263 int ACL_LoadFile( vlc_acl_t *p_acl, const char *psz_path )
264 {
265     FILE *file;
266     
267     if( p_acl == NULL )
268         return -1;
269
270     file = fopen( psz_path, "r" );
271     if( file == NULL )
272         return -1;
273
274     msg_Dbg( p_acl->p_owner, "find .hosts in dir=%s", psz_path );
275
276     while( !feof( file ) )
277     {
278         char line[1024], *psz_ip, *ptr;
279
280         if( fgets( line, sizeof( line ) - 1, file ) == NULL )
281         {
282             if( ferror( file ) )
283             {
284                 msg_Err( p_acl->p_owner, "Error reading %s : %s\n", psz_path,
285                         strerror( errno ) );
286                 goto error;
287             }
288             continue;
289         }
290
291         psz_ip = line;
292
293         /* skips blanks */
294         while( isblank( *psz_ip ) )
295             psz_ip++;
296
297         ptr = strchr( psz_ip, '\n' );
298         if( ptr == NULL )
299         {
300             msg_Warn( p_acl->p_owner, "Skipping overly long line in %s\n",
301                       psz_path);
302             do
303             {
304                 fgets( line, sizeof( line ) - 1, file );
305                 if( ferror( file ) || feof( file ) )
306                 {
307                     msg_Err( p_acl->p_owner, "Error reading %s : %s\n",
308                              psz_path, strerror( errno ) );
309                     goto error;
310                 }
311             }
312             while( strchr( line, '\n' ) == NULL);
313
314             continue; /* skip unusable line */
315         }
316
317         /* skips comment-only line */
318         if( *psz_ip == '#' )
319             continue;
320
321         /* looks for first space, CR, LF, etc. or end-of-line comment */
322         /* (there is at least a linefeed) */
323         for( ptr = psz_ip; ( *ptr != '#' ) && !isspace( *ptr ); ptr++ );
324
325         *ptr = '\0';
326
327         msg_Dbg( p_acl->p_owner, "restricted to %s", psz_ip );
328
329         ptr = strchr( psz_ip, '/' );
330         if( ptr != NULL )
331             *ptr++ = '\0'; /* separate address from mask length */
332
333         if( (ptr != NULL)
334             ? ACL_AddNet( p_acl, psz_ip, atoi( ptr ), VLC_TRUE ) 
335             : ACL_AddHost( p_acl, psz_ip, VLC_TRUE ) )
336         {
337             msg_Err( p_acl->p_owner, "cannot add ACL from %s", psz_path );
338             goto error;
339         }
340     }
341
342     fclose( file );
343     return 0;
344
345 error:
346     fclose( file );
347     return -1;
348 }
349