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