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