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