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