]> git.sesse.net Git - vlc/blob - src/network/acl.c
utf8_* -> vlc_* (sed roxxors)
[vlc] / src / network / acl.c
1 /*****************************************************************************
2  * acl.c:
3  *****************************************************************************
4  * Copyright © 2005-2007 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 #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 <vlc_network.h>
37 #include <vlc_fs.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     bool 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     bool       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  * Check if a given address passes an access control list.
113  *
114  * @param p_acl pre-existing ACL to match the address against
115  * @param psz_ip numeric IPv4/IPv6 address
116  *
117  * @return 0 if the first matching ACL entry is an access grant,
118  * 1 if the first matching ACL entry is a denial of access,
119  * -1 on error.
120  */
121 int ACL_Check( vlc_acl_t *p_acl, const char *psz_ip )
122 {
123     const vlc_acl_entry_t *p_cur, *p_end;
124     uint8_t host[17];
125
126     if( p_acl == NULL )
127         return -1;
128
129     p_cur = p_acl->p_entries;
130     p_end = p_cur + p_acl->i_size;
131
132     if( ACL_Resolve( p_acl->p_owner, host, psz_ip ) < 0 )
133         return -1;
134
135     while (p_cur < p_end)
136     {
137         unsigned i;
138
139         i = p_cur->i_bytes_match;
140         if( (memcmp( p_cur->host, host, i ) == 0)
141          && (((p_cur->host[i] ^ host[i]) & p_cur->i_bits_mask) == 0) )
142             return !p_cur->b_allow;
143
144         p_cur++;
145     }
146
147     return !p_acl->b_allow_default;
148 }
149
150 /**
151  * Adds an item to an ACL.
152  * Items are always matched in the same order as they are added.
153  */
154 int ACL_AddNet( vlc_acl_t *p_acl, const char *psz_ip, int i_len,
155                 bool b_allow )
156 {
157     vlc_acl_entry_t *p_ent;
158     unsigned i_size;
159     div_t d;
160     int i_family;
161
162     i_size = p_acl->i_size;
163     p_ent = (vlc_acl_entry_t *)realloc( p_acl->p_entries,
164                                         ++p_acl->i_size * sizeof( *p_ent ) );
165
166     if( p_ent == NULL )
167         return -1;
168
169     p_acl->p_entries = p_ent;
170     p_ent += i_size;
171
172     i_family = ACL_Resolve( p_acl->p_owner, p_ent->host, psz_ip );
173     if( i_family < 0 )
174     {
175         /*
176          * I'm lazy : memory space will be re-used in the next ACL_Add call...
177          * or not.
178          */
179         p_acl->i_size--;
180         return -1;
181     }
182
183     if( i_len >= 0 )
184     {
185         if( i_family == AF_INET )
186             i_len += 96;
187
188         if( i_len > 128 )
189             i_len = 128;
190     }
191     else
192         i_len = 128; /* ACL_AddHost */
193
194     d = div( i_len, 8 );
195     p_ent->i_bytes_match = d.quot;
196     p_ent->i_bits_mask = 0xff << (8 - d.rem);
197
198     p_ent->b_allow = b_allow;
199     return 0;
200 }
201
202 #undef ACL_Create
203 /**
204  * Creates an empty ACL.
205  *
206  * @param b_allow whether to grant (true) or deny (false) access
207  * by default (ie if none of the ACL entries matched).
208  *
209  * @return an ACL object. NULL in case of error.
210  */
211 vlc_acl_t *ACL_Create( vlc_object_t *p_this, bool b_allow )
212 {
213     vlc_acl_t *p_acl;
214
215     p_acl = (vlc_acl_t *)malloc( sizeof( *p_acl ) );
216     if( p_acl == NULL )
217         return NULL;
218
219     vlc_object_hold( p_this );
220     p_acl->p_owner = p_this;
221     p_acl->i_size = 0;
222     p_acl->p_entries = NULL;
223     p_acl->b_allow_default = b_allow;
224
225     return p_acl;
226 }
227
228 #undef ACL_Duplicate
229 /**
230  * Perform a deep copy of an existing ACL.
231  *
232  * @param p_this object to attach the copy to.
233  * @param p_acl ACL object to be copied.
234  *
235  * @return a new ACL object, or NULL on error.
236  */
237 vlc_acl_t *ACL_Duplicate( vlc_object_t *p_this, const vlc_acl_t *p_acl )
238 {
239     vlc_acl_t *p_dupacl;
240
241     if( p_acl == NULL )
242         return NULL;
243
244     p_dupacl = (vlc_acl_t *)malloc( sizeof( *p_dupacl ) );
245     if( p_dupacl == NULL )
246         return NULL;
247
248     if( p_acl->i_size )
249     {
250         p_dupacl->p_entries = (vlc_acl_entry_t *)
251             malloc( p_acl->i_size * sizeof( vlc_acl_entry_t ) );
252
253         if( p_dupacl->p_entries == NULL )
254         {
255             free( p_dupacl );
256             return NULL;
257         }
258
259         memcpy( p_dupacl->p_entries, p_acl->p_entries,
260                 p_acl->i_size * sizeof( vlc_acl_entry_t ) );
261     }
262     else
263         p_dupacl->p_entries = NULL;
264
265     vlc_object_hold( p_this );
266     p_dupacl->p_owner = p_this;
267     p_dupacl->i_size = p_acl->i_size;
268     p_dupacl->b_allow_default = p_acl->b_allow_default;
269
270     return p_dupacl;
271 }
272
273
274 /**
275  * Releases all resources associated with an ACL object.
276  */
277 void ACL_Destroy( vlc_acl_t *p_acl )
278 {
279     if( p_acl != NULL )
280     {
281         free( p_acl->p_entries );
282         vlc_object_release( p_acl->p_owner );
283         free( p_acl );
284     }
285 }
286
287
288 /**
289  * Reads ACL entries from a file.
290  *
291  * @param p_acl ACL object in which to insert parsed entries.
292  * @param psz_patch filename from which to parse entries.
293  *
294  * @return 0 on success, -1 on error.
295  */
296 int ACL_LoadFile( vlc_acl_t *p_acl, const char *psz_path )
297 {
298     FILE *file;
299
300     if( p_acl == NULL )
301         return -1;
302
303     file = vlc_fopen( psz_path, "r" );
304     if( file == NULL )
305         return -1;
306
307     msg_Dbg( p_acl->p_owner, "find .hosts in dir=%s", psz_path );
308
309     while( !feof( file ) )
310     {
311         char line[1024], *psz_ip, *ptr;
312
313         if( fgets( line, sizeof( line ), file ) == NULL )
314         {
315             if( ferror( file ) )
316             {
317                 msg_Err( p_acl->p_owner, "error reading %s : %m", psz_path );
318                 goto error;
319             }
320             continue;
321         }
322
323         /* fgets() is cool : never overflow, always nul-terminate */
324         psz_ip = line;
325
326         /* skips blanks - cannot overflow given '\0' is not space */
327         while( isspace( *psz_ip ) )
328             psz_ip++;
329
330         if( *psz_ip == '\0' ) /* empty/blank line */
331             continue;
332
333         ptr = strchr( psz_ip, '\n' );
334         if( ptr == NULL && !feof(file) )
335         {
336             msg_Warn( p_acl->p_owner, "skipping overly long line in %s",
337                       psz_path);
338             do
339             {
340                 if( fgets( line, sizeof( line ), file ) == NULL )
341                 {
342                      if( ferror( file ) )
343                      {
344                          msg_Err( p_acl->p_owner, "error reading %s : %m",
345                                   psz_path );
346                      }
347                      goto error;
348                 }
349             }
350             while( strchr( line, '\n' ) == NULL);
351
352             continue; /* skip unusable line */
353         }
354
355         /* look for first space, CR, LF, etc. or comment character */
356         for( ptr = psz_ip; ( *ptr!='#' ) && !isspace( *ptr ) && *ptr; ++ptr );
357
358         *ptr = '\0';
359
360         /* skip lines without usable information */
361         if( ptr == psz_ip )
362             continue;
363
364         msg_Dbg( p_acl->p_owner, "restricted to %s", psz_ip );
365
366         ptr = strchr( psz_ip, '/' );
367         if( ptr != NULL )
368             *ptr++ = '\0'; /* separate address from mask length */
369
370         if( (ptr != NULL)
371             ? ACL_AddNet( p_acl, psz_ip, atoi( ptr ), true )
372             : ACL_AddHost( p_acl, psz_ip, true ) )
373         {
374             msg_Err( p_acl->p_owner, "cannot add ACL from %s", psz_path );
375             continue;
376         }
377     }
378
379     fclose( file );
380     return 0;
381
382 error:
383     fclose( file );
384     return -1;
385 }
386