acl.c 9.8 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
/*****************************************************************************
 * acl.c:
 *****************************************************************************
 * Copyright (C) 2005 Rémi Denis-Courmont
 * $Id$
 *
 * Authors: Rémi Denis-Courmont <rem # videolan.org>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
dionoea's avatar
dionoea committed
21
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22
23
24
25
26
27
28
 *****************************************************************************/

/*****************************************************************************
 * Preamble
 *****************************************************************************/
#include <stdlib.h>
#include <string.h>
29
#include <ctype.h>
30
31
#include <vlc/vlc.h>

zorglub's avatar
zorglub committed
32
#include <vlc_acl.h>
33

34
35
#include <errno.h>

zorglub's avatar
zorglub committed
36
37
#include <vlc_network.h>
#include <vlc_charset.h>
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58

/* FIXME: rwlock on acl, but libvlc doesn't implement rwlock */
typedef struct vlc_acl_entry_t
{
    uint8_t    host[17];
    uint8_t    i_bytes_match;
    uint8_t    i_bits_mask;
    vlc_bool_t b_allow;
} vlc_acl_entry_t;

struct vlc_acl_t
{
    vlc_object_t    *p_owner;
    unsigned         i_size;
    vlc_acl_entry_t *p_entries;
    vlc_bool_t       b_allow_default;
};

static int ACL_Resolve( vlc_object_t *p_this, uint8_t *p_bytes,
                        const char *psz_ip )
{
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
59
    struct addrinfo hints, *res;
60
61
    int i_family;

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
62
    memset (&hints, 0, sizeof (hints));
63
64
65
66
67
68
69
70
    hints.ai_socktype = SOCK_STREAM; /* doesn't matter */
    hints.ai_flags = AI_NUMERICHOST;

    if( vlc_getaddrinfo( p_this, psz_ip, 0, &hints, &res ) )
    {
        msg_Err( p_this, "invalid IP address %s", psz_ip );
        return -1;
    }
71

72
73
74
75
76
77
78
79
    p_bytes[16] = 0; /* avoids overflowing when i_bytes_match = 16 */

    i_family = res->ai_addr->sa_family;
    switch( i_family )
    {
        case AF_INET:
        {
            struct sockaddr_in *addr;
80

81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
            addr = (struct sockaddr_in *)res->ai_addr;
            memset( p_bytes, 0, 12 );
            memcpy( p_bytes + 12, &addr->sin_addr, 4 );
            break;
        }

#if defined (HAVE_GETADDRINFO) || defined (WIN32)
        /* unfortunately many people define AF_INET6
           though they don't have struct sockaddr_in6 */
        case AF_INET6:
        {
            struct sockaddr_in6 *addr;

            addr = (struct sockaddr_in6 *)res->ai_addr;
            memcpy( p_bytes, &addr->sin6_addr, 16 );
            break;
        }
#endif

        default:
zorglub's avatar
zorglub committed
101
            msg_Err( p_this, "unknown address family" );
102
103
104
105
106
107
108
109
110
            vlc_freeaddrinfo( res );
            return -1;
    }

    vlc_freeaddrinfo( res );
    return i_family;
}


Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
111
112
113
114
115
116
117
118
119
/**
 * Check if a given address passes an access control list.
 *
 * @param p_acl pre-existing ACL to match the address against
 * @param psz_ip numeric IPv4/IPv6 address
 *
 * @return 0 if the first matching ACL entry is an access grant,
 * 1 if the first matching ACL entry is a denial of access,
 * -1 on error.
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
 */
int ACL_Check( vlc_acl_t *p_acl, const char *psz_ip )
{
    const vlc_acl_entry_t *p_cur, *p_end;
    uint8_t host[17];

    if( p_acl == NULL )
        return -1;

    p_cur = p_acl->p_entries;
    p_end = p_cur + p_acl->i_size;

    if( ACL_Resolve( p_acl->p_owner, host, psz_ip ) < 0 )
        return -1;

    while (p_cur < p_end)
    {
        unsigned i;

        i = p_cur->i_bytes_match;
        if( (memcmp( p_cur->host, host, i ) == 0)
         && (((p_cur->host[i] ^ host[i]) & p_cur->i_bits_mask) == 0) )
            return !p_cur->b_allow;

        p_cur++;
    }

    return !p_acl->b_allow_default;
}

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
150
151
152
153
/**
 * Adds an item to an ACL.
 * Items are always matched in the same order as they are added.
 */
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
int ACL_AddNet( vlc_acl_t *p_acl, const char *psz_ip, int i_len,
                vlc_bool_t b_allow )
{
    vlc_acl_entry_t *p_ent;
    unsigned i_size;
    div_t d;
    int i_family;

    i_size = p_acl->i_size;
    p_ent = (vlc_acl_entry_t *)realloc( p_acl->p_entries,
                                        ++p_acl->i_size * sizeof( *p_ent ) );

    if( p_ent == NULL )
        return -1;

169
170
171
    p_acl->p_entries = p_ent;
    p_ent += i_size;

172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
    i_family = ACL_Resolve( p_acl->p_owner, p_ent->host, psz_ip );
    if( i_family < 0 )
    {
        /*
         * I'm lazy : memory space will be re-used in the next ACL_Add call...
         * or not.
         */
        p_acl->i_size--;
        return -1;
    }

    if( i_len >= 0 )
    {
        if( i_family == AF_INET )
            i_len += 96;

        if( i_len > 128 )
            i_len = 128;
        else
        if( i_len < 0 )
            i_len = 0;
    }
    else
        i_len = 128; /* ACL_AddHost */

    d = div( i_len, 8 );
    p_ent->i_bytes_match = d.quot;
    p_ent->i_bits_mask = 0xff << (8 - d.rem);

    p_ent->b_allow = b_allow;
    return 0;
}


Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
206
207
208
209
210
211
212
213
/**
 * Creates an empty ACL.
 *
 * @param b_allow whether to grant (VLC_TRUE) or deny (VLC_FALSE) access
 * by default (ie if none of the ACL entries matched).
 *
 * @return an ACL object. NULL in case of error.
 */
214
215
216
217
218
219
220
221
222
223
224
225
226
vlc_acl_t *__ACL_Create( vlc_object_t *p_this, vlc_bool_t b_allow )
{
    vlc_acl_t *p_acl;

    p_acl = (vlc_acl_t *)malloc( sizeof( *p_acl ) );
    if( p_acl == NULL )
        return NULL;

    vlc_object_yield( p_this );
    p_acl->p_owner = p_this;
    p_acl->i_size = 0;
    p_acl->p_entries = NULL;
    p_acl->b_allow_default = b_allow;
227

228
229
230
231
    return p_acl;
}


Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
232
233
234
235
236
237
238
239
/**
 * Perform a deep copy of an existing ACL.
 *
 * @param p_this object to attach the copy to.
 * @param p_acl ACL object to be copied.
 *
 * @return a new ACL object, or NULL on error.
 */
240
241
242
243
244
245
246
247
248
249
250
vlc_acl_t *__ACL_Duplicate( vlc_object_t *p_this, const vlc_acl_t *p_acl )
{
    vlc_acl_t *p_dupacl;

    if( p_acl == NULL )
        return NULL;

    p_dupacl = (vlc_acl_t *)malloc( sizeof( *p_dupacl ) );
    if( p_dupacl == NULL )
        return NULL;

251
    if( p_acl->i_size )
252
    {
253
254
255
256
257
258
259
260
261
262
263
264
265
266
        p_dupacl->p_entries = (vlc_acl_entry_t *)
            malloc( p_acl->i_size * sizeof( vlc_acl_entry_t ) );

        if( p_dupacl->p_entries == NULL )
        {
            free( p_dupacl );
            return NULL;
        }

        memcpy( p_dupacl->p_entries, p_acl->p_entries,
                p_acl->i_size * sizeof( vlc_acl_entry_t ) );
    }
    else
        p_dupacl->p_entries = NULL;
267
268
269
270

    vlc_object_yield( p_this );
    p_dupacl->p_owner = p_this;
    p_dupacl->i_size = p_acl->i_size;
271
272
    p_dupacl->b_allow_default = p_acl->b_allow_default;

273
274
275
276
    return p_dupacl;
}


Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
277
278
279
/**
 * Releases all resources associated with an ACL object.
 */
280
281
282
283
284
285
286
287
288
289
290
291
void ACL_Destroy( vlc_acl_t *p_acl )
{
    if( p_acl != NULL )
    {
        if( p_acl->p_entries != NULL )
            free( p_acl->p_entries );

        vlc_object_release( p_acl->p_owner );
        free( p_acl );
    }
}

292
293
294
#ifndef isblank 
#   define isblank(c) ((c) == ' ' || (c) == '\t')
#endif
295

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
296
297
298
299
300
301
302
303
/**
 * Reads ACL entries from a file.
 *
 * @param p_acl ACL object in which to insert parsed entries.
 * @param psz_patch filename from which to parse entries.
 *
 * @return 0 on success, -1 on error.
 */
304
305
306
int ACL_LoadFile( vlc_acl_t *p_acl, const char *psz_path )
{
    FILE *file;
307

308
309
310
    if( p_acl == NULL )
        return -1;

311
    file = utf8_fopen( psz_path, "r" );
312
313
314
315
316
317
318
319
320
    if( file == NULL )
        return -1;

    msg_Dbg( p_acl->p_owner, "find .hosts in dir=%s", psz_path );

    while( !feof( file ) )
    {
        char line[1024], *psz_ip, *ptr;

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
321
        if( fgets( line, sizeof( line ), file ) == NULL )
322
323
324
        {
            if( ferror( file ) )
            {
zorglub's avatar
zorglub committed
325
                msg_Err( p_acl->p_owner, "error reading %s : %s\n", psz_path,
326
327
328
329
330
331
                        strerror( errno ) );
                goto error;
            }
            continue;
        }

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
332
        /* fgets() is cool : never overflow, always nul-terminate */
333
334
        psz_ip = line;

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
335
        /* skips blanks - cannot overflow given '\0' is not space */
336
        while( isspace( *psz_ip ) )
337
338
            psz_ip++;

339
340
341
        if( *psz_ip == '\0' ) /* empty/blank line */
            continue;

342
343
344
        ptr = strchr( psz_ip, '\n' );
        if( ptr == NULL )
        {
zorglub's avatar
zorglub committed
345
            msg_Warn( p_acl->p_owner, "skipping overly long line in %s\n",
346
347
348
                      psz_path);
            do
            {
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
349
                fgets( line, sizeof( line ), file );
350
351
                if( ferror( file ) || feof( file ) )
                {
zorglub's avatar
zorglub committed
352
                    msg_Err( p_acl->p_owner, "error reading %s : %s\n",
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
                             psz_path, strerror( errno ) );
                    goto error;
                }
            }
            while( strchr( line, '\n' ) == NULL);

            continue; /* skip unusable line */
        }

        /* skips comment-only line */
        if( *psz_ip == '#' )
            continue;

        /* looks for first space, CR, LF, etc. or end-of-line comment */
        /* (there is at least a linefeed) */
        for( ptr = psz_ip; ( *ptr != '#' ) && !isspace( *ptr ); ptr++ );

        *ptr = '\0';

        msg_Dbg( p_acl->p_owner, "restricted to %s", psz_ip );

        ptr = strchr( psz_ip, '/' );
        if( ptr != NULL )
            *ptr++ = '\0'; /* separate address from mask length */

        if( (ptr != NULL)
            ? ACL_AddNet( p_acl, psz_ip, atoi( ptr ), VLC_TRUE ) 
            : ACL_AddHost( p_acl, psz_ip, VLC_TRUE ) )
        {
            msg_Err( p_acl->p_owner, "cannot add ACL from %s", psz_path );
            goto error;
        }
    }

    fclose( file );
    return 0;

error:
    fclose( file );
    return -1;
}