matchfile.c 3.93 KB
Newer Older
1
2
3
4
5
/* Icecast
 *
 * This program is distributed under the GNU General Public License, version 2.
 * A copy of this license is included with this source.
 *
6
 * Copyright 2015-2018, Philipp "ph3-der-loewe" Schafft <lion@lion.leolix.org>
7
8
9
10
11
12
13
14
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <stdio.h>
#include <stdlib.h>
15
#include <errno.h>
16
17
18
19
20
21
#include <string.h>
#include <time.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>

22
23
#include "common/avl/avl.h"

24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include "matchfile.h"
#include "logging.h"
#include "util.h" /* for MAX_LINE_LEN and get_line() */
#define CATMODULE "matchfile"

struct matchfile_tag {
    /* reference counter */
    size_t refcount;

    /* filename of input file */
    char *filename;

    time_t file_recheck;
    time_t file_mtime;
    avl_tree *contents;
};

static int __func_free(void *x) {
    free(x);
    return 1;
}

static int __func_compare (void *arg, void *a, void *b) {
47
    (void)arg;
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
    return strcmp(b, a);
}

static void __func_recheck(matchfile_t *file) {
    time_t now = time(NULL);
    struct stat file_stat;
    FILE *input = NULL;
    avl_tree *new_contents;
    char line[MAX_LINE_LEN];

    if (now < file->file_recheck)
        return;

    file->file_recheck = now + 10;

    if (stat(file->filename, &file_stat) < 0) {
        ICECAST_LOG_WARN("failed to check status of \"%s\": %s", file->filename, strerror(errno));
        return;
    }

    if (file_stat.st_mtime == file->file_mtime)
        return; /* common case, no update to file */

    file->file_mtime = file_stat.st_mtime;

    input = fopen(file->filename, "r");
    if (!input) {
        ICECAST_LOG_WARN("Failed to open file \"%s\": %s", file->filename, strerror(errno));
        return;
    }

    new_contents = avl_tree_new(__func_compare, NULL);

    while (get_line(input, line, MAX_LINE_LEN)) {
        char *str;

        if(!line[0] || line[0] == '#')
            continue;
        str = strdup(line);
        if (str)
            avl_insert(new_contents, str);
    }

    fclose(input);

    if (file->contents) avl_tree_free(file->contents, __func_free);
    file->contents = new_contents;
}

matchfile_t *matchfile_new(const char *filename) {
    matchfile_t *ret;

    if (!filename)
        return NULL;

    ret = calloc(1, sizeof(matchfile_t));
    if (!ret)
        return NULL;

    ret->refcount     = 1;
    ret->filename     = strdup(filename);
    ret->file_mtime   = 0;
    ret->file_recheck = 0;

    if (!ret->filename) {
        matchfile_release(ret);
        return NULL;
    }

    /* load initial database */
    __func_recheck(ret);

    return ret;
}

int          matchfile_addref(matchfile_t *file) {
    if (!file)
        return -1;

    file->refcount++;

    return 0;
}

int          matchfile_release(matchfile_t *file) {
    if (!file)
        return -1;

    file->refcount--;

    if (file->refcount)
        return 0;

    if (file->contents)
        avl_tree_free(file->contents, __func_free);
    free(file->filename);
    free(file);

    return 0;
}

/* we are not const char *key because of avl_get_by_key()... */
150
int          matchfile_match(matchfile_t *file, const char *key) {
151
152
153
154
155
156
157
158
159
160
161
162
163
    void *result;

    if (!file)
        return -1;

    /* reload database if needed */
    __func_recheck(file);

    if (!file->contents)
        return 0;

    return avl_get_by_key(file->contents, (void*)key, &result) == 0 ? 1 : 0;
}
164

165
int          matchfile_match_allow_deny(matchfile_t *allow, matchfile_t *deny, const char *key) {
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
    if (!allow && !deny)
        return 1;

    if (!key)
        return 0;

    if (matchfile_match(deny, key) > 0) {
        ICECAST_LOG_DEBUG("%s is banned", key);
        return 0;
    }

    if (matchfile_match(allow, key) > 0) {
        ICECAST_LOG_DEBUG("%s is allowed", key);
        return 1;
    } else if (allow) {
        /* we are not on allow list but there is one, so reject */
        ICECAST_LOG_DEBUG("%s is not allowed", key);
        return 0;
    }

    /* default: allow */
    return 1;
}