summaryrefslogtreecommitdiff
path: root/src/acl.c
blob: b7a334c9c9906cb55cd84d87c48b921e97c6e8bb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
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
395
396
397
398
399
400
401
/* tinyproxy - A fast light-weight HTTP proxy
 * Copyright (C) 2000, 2002 Robert James Kaes <rjkaes@users.sourceforge.net>
 *
 * 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 Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

/* This system handles Access Control for use of this daemon. A list of
 * domains, or IP addresses (including IP blocks) are stored in a list
 * which is then used to compare incoming connections.
 */

#include "main.h"

#include "acl.h"
#include "heap.h"
#include "log.h"
#include "network.h"
#include "sock.h"
#include "vector.h"

#include <limits.h>

/* Define how long an IPv6 address is in bytes (128 bits, 16 bytes) */
#define IPV6_LEN 16

enum acl_type {
        ACL_STRING,
        ACL_NUMERIC
};

/*
 * Hold the information about a particular access control.  We store
 * whether it's an ALLOW or DENY entry, and also whether it's a string
 * entry (like a domain name) or an IP entry.
 */
struct acl_s {
        acl_access_t access;
        enum acl_type type;
        union {
                char *string;
                struct {
                        unsigned char network[IPV6_LEN];
                        unsigned char mask[IPV6_LEN];
                } ip;
        } address;
};

/*
 * Fills in the netmask array given a numeric value.
 *
 * Returns:
 *   0 on success
 *  -1 on failure (invalid mask value)
 *
 */
static int
fill_netmask_array (char *bitmask_string, int v6,
                    unsigned char array[], size_t len)
{
        unsigned int i;
        unsigned long int mask;
        char *endptr;

        errno = 0;              /* to distinguish success/failure after call */
        mask = strtoul (bitmask_string, &endptr, 10);

        /* check for various conversion errors */
        if ((errno == ERANGE && mask == ULONG_MAX)
            || (errno != 0 && mask == 0) || (endptr == bitmask_string))
                return -1;

        if (v6 == 0) {
                /* The mask comparison is done as an IPv6 address, so
                 * convert to a longer mask in the case of IPv4
                 * addresses. */
                mask += 12 * 8;
        }

        /* check valid range for a bit mask */
        if (mask > (8 * len))
                return -1;

        /* we have a valid range to fill in the array */
        for (i = 0; i != len; ++i) {
                if (mask >= 8) {
                        array[i] = 0xff;
                        mask -= 8;
                } else if (mask > 0) {
                        array[i] = (unsigned char) (0xff << (8 - mask));
                        mask = 0;
                } else {
                        array[i] = 0;
                }
        }

        return 0;
}

/**
 * If the access list has not been set up, create it.
 */
static int init_access_list(vector_t *access_list)
{
        if (!*access_list) {
                *access_list = vector_create ();
                if (!*access_list) {
                        log_message (LOG_ERR,
                                     "Unable to allocate memory for access list");
                        return -1;
                }
        }

        return 0;
}

/*
 * Inserts a new access control into the list. The function will figure out
 * whether the location is an IP address (with optional netmask) or a
 * domain name.
 *
 * Returns:
 *    -1 on failure
 *     0 otherwise.
 */
int
insert_acl (char *location, acl_access_t access_type, vector_t *access_list)
{
        struct acl_s acl;
        int ret;
        char *p, ip_dst[IPV6_LEN];

        assert (location != NULL);

        ret = init_access_list(access_list);
        if (ret != 0) {
                return -1;
        }

        /*
         * Start populating the access control structure.
         */
        memset (&acl, 0, sizeof (struct acl_s));
        acl.access = access_type;

        /*
         * Check for a valid IP address (the simplest case) first.
         */
        if (full_inet_pton (location, ip_dst) > 0) {
                acl.type = ACL_NUMERIC;
                memcpy (acl.address.ip.network, ip_dst, IPV6_LEN);
                memset (acl.address.ip.mask, 0xff, IPV6_LEN);
        } else {
                int i;

                /*
                 * At this point we're either a hostname or an
                 * IP address with a slash.
                 */
                p = strchr (location, '/');
                if (p != NULL) {
                        char dst[sizeof(struct in6_addr)];
                        int v6;

                        /*
                         * We have a slash, so it's intended to be an
                         * IP address with mask
                         */
                        *p = '\0';
                        if (full_inet_pton (location, ip_dst) <= 0)
                                return -1;

                        acl.type = ACL_NUMERIC;

                        /* Check if the IP address before the netmask is
                         * an IPv6 address */
                        if (inet_pton(AF_INET6, location, dst) > 0)
                                v6 = 1;
                        else
                                v6 = 0;

                        if (fill_netmask_array
                            (p + 1, v6, &(acl.address.ip.mask[0]), IPV6_LEN)
                            < 0)
                                return -1;

                        for (i = 0; i < IPV6_LEN; i++)
                                acl.address.ip.network[i] = ip_dst[i] &
                                        acl.address.ip.mask[i];
                } else {
                        /* In all likelihood a string */
                        acl.type = ACL_STRING;
                        acl.address.string = safestrdup (location);
                        if (!acl.address.string)
                                return -1;
                }
        }

        ret = vector_append (*access_list, &acl, sizeof (struct acl_s));
        return ret;
}

/*
 * This function is called whenever a "string" access control is found in
 * the ACL.  From here we do both a text based string comparison, along with
 * a reverse name lookup comparison of the IP addresses.
 *
 * Return: 0 if host is denied
 *         1 if host is allowed
 *        -1 if no tests match, so skip
 */
static int
acl_string_processing (struct acl_s *acl,
                       const char *ip_address, const char *string_address)
{
        int match;
        struct addrinfo hints, *res, *ressave;
        size_t test_length, match_length;
        char ipbuf[512];

        assert (acl && acl->type == ACL_STRING);
        assert (ip_address && strlen (ip_address) > 0);
        assert (string_address && strlen (string_address) > 0);

        /*
         * If the first character of the ACL string is a period, we need to
         * do a string based test only; otherwise, we can do a reverse
         * lookup test as well.
         */
        if (acl->address.string[0] != '.') {
                memset (&hints, 0, sizeof (struct addrinfo));
                hints.ai_family = AF_UNSPEC;
                hints.ai_socktype = SOCK_STREAM;
                if (getaddrinfo (acl->address.string, NULL, &hints, &res) != 0)
                        goto STRING_TEST;

                ressave = res;

                match = FALSE;
                do {
                        get_ip_string (res->ai_addr, ipbuf, sizeof (ipbuf));
                        if (strcmp (ip_address, ipbuf) == 0) {
                                match = TRUE;
                                break;
                        }
                } while ((res = res->ai_next) != NULL);

                freeaddrinfo (ressave);

                if (match) {
                        if (acl->access == ACL_DENY)
                                return 0;
                        else
                                return 1;
                }
        }

STRING_TEST:
        test_length = strlen (string_address);
        match_length = strlen (acl->address.string);

        /*
         * If the string length is shorter than AC string, return a -1 so
         * that the "driver" will skip onto the next control in the list.
         */
        if (test_length < match_length)
                return -1;

        if (strcasecmp
            (string_address + (test_length - match_length),
             acl->address.string) == 0) {
                if (acl->access == ACL_DENY)
                        return 0;
                else
                        return 1;
        }

        /* Indicate that no tests succeeded, so skip to next control. */
        return -1;
}

/*
 * Compare the supplied numeric IP address with the supplied ACL structure.
 *
 * Return:
 *   1  IP address is allowed
 *   0  IP address is denied
 *  -1  neither allowed nor denied.
 */
static int check_numeric_acl (const struct acl_s *acl, const char *ip)
{
        uint8_t addr[IPV6_LEN], x, y;
        int i;

        assert (acl && acl->type == ACL_NUMERIC);
        assert (ip && strlen (ip) > 0);

        if (full_inet_pton (ip, &addr) <= 0)
                return -1;

        for (i = 0; i != IPV6_LEN; ++i) {
                x = addr[i] & acl->address.ip.mask[i];
                y = acl->address.ip.network[i];

                /* If x and y don't match, the IP addresses don't match */
                if (x != y)
                        return -1;
        }

        /* The addresses match, return the permission */
        return (acl->access == ACL_ALLOW);
}

/*
 * Checks whether a connection is allowed.
 *
 * Returns:
 *     1 if allowed
 *     0 if denied
 */
int check_acl (const char *ip, const char *host, vector_t access_list)
{
        struct acl_s *acl;
        int perm = 0;
        size_t i;

        assert (ip != NULL);
        assert (host != NULL);

        /*
         * If there is no access list allow everything.
         */
        if (!access_list)
                return 1;

        for (i = 0; i != (size_t) vector_length (access_list); ++i) {
                acl = (struct acl_s *) vector_getentry (access_list, i, NULL);
                switch (acl->type) {
                case ACL_STRING:
                        perm = acl_string_processing (acl, ip, host);
                        break;

                case ACL_NUMERIC:
                        if (ip[0] == '\0')
                                continue;
                        perm = check_numeric_acl (acl, ip);
                        break;
                }

                /*
                 * Check the return value too see if the IP address is
                 * allowed or denied.
                 */
                if (perm == 0)
                        break;
                else if (perm == 1)
                        return perm;
        }

        /*
         * Deny all connections by default.
         */
        log_message (LOG_NOTICE, "Unauthorized connection from \"%s\" [%s].",
                     host, ip);
        return 0;
}

void flush_access_list (vector_t access_list)
{
        struct acl_s *acl;
        size_t i;

        if (!access_list) {
                return;
        }

        /*
         * We need to free allocated data hanging off the acl entries
         * before we can free the acl entries themselves.
         * A hierarchical memory system would be great...
         */
        for (i = 0; i != (size_t) vector_length (access_list); ++i) {
                acl = (struct acl_s *) vector_getentry (access_list, i, NULL);
                if (acl->type == ACL_STRING) {
                        safefree (acl->address.string);
                }
        }

        vector_delete (access_list);
}