File: rule.c

package info (click to toggle)
libpam-abl 0.6.0-7
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 448 kB
  • sloc: ansic: 4,757; sh: 47; makefile: 10
file content (239 lines) | stat: -rw-r--r-- 5,746 bytes parent folder | download | duplicates (4)
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
/*
 *   pam_abl - a PAM module and program for automatic blacklisting of hosts and users
 *
 *   Copyright (C) 2005-2012
 *
 *   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, see <http://www.gnu.org/licenses/>.
 */

#include "rule.h"

#include <errno.h>
#include <ctype.h>
#include <string.h>

int parse_long(const char **sp, long *lp) {
    long l = 0;

    if (!isdigit(**sp)) {
        return EINVAL;
    }

    while (isdigit(**sp)) {
        l = l * 10 + *(*sp)++ - '0';
    }

    *lp = l;
    return 0;
}

/* Parse a time specification in the form
 * <digits>[s|m|h|d]
 */
static int parse_time(const char **sp, long *tp) {
    long t;
    int err;

    if (err = parse_long(sp, &t), 0 != err) {
        return err;
    }

    /* Handle the multiplier suffix */
    switch (**sp) {
    case 'd':
        t *= 24;
    case 'h':
        t *= 60;
    case 'm':
        t *= 60;
    case 's':
        (*sp)++;
    default:
        ;
    }

    *tp = t;

    return 0;
}

int rule_parse_time(const char *p, long *t, long min) {
    int err;

    if (err = parse_time(&p, t), 0 != err) {
        *t = min;
        return err;
    }

    if (*p != '\0') {
        *t = min;
        return EINVAL;
    }

    if (*t < min) {
        *t = min;
    }

    return 0;
}

static size_t wordlen(const char *rp) {
    size_t l = 0;
    while (*rp != '\0' &&
           *rp != '/'  &&
           *rp != '|'  &&
           *rp != ':'  &&
           !isspace(*rp)) {
        rp++;
        l++;
    }
    return l;
}

static int match(log_context *log, const char *pattern, const char *target, size_t len) {
    log_debug(log, "match('%s', '%s', %d)", pattern, target, len);
    if (!pattern)
        return 0;
    return (len == strlen(pattern)) && (memcmp(pattern, target, len) == 0);
}

static int matchname(log_context *log, const char *user, const char *service,
                     const char **rp) {
    size_t l = wordlen(*rp);
    int ok;

    log_debug(log, "Check %s/%s against %s(%d)", user, service, *rp, l);

    ok = (l != 0) && ((l == 1 && **rp == '*') || match(log, user, *rp, l));
    (*rp) += l;
    if (ok) {
        log_debug(log, "Name part matches, **rp = '%c'", **rp);
    }
    if (**rp == '/') {
        (*rp)++;
        l = wordlen(*rp);
        ok &= (l != 0) && ((l == 1 && **rp == '*') || match(log, service, *rp, l));
        (*rp) += l;
    }

    log_debug(log, "%satch!", ok ? "M" : "No m");

    return ok;
}

static int matchnames(log_context *log, const char *user, const char *service,
                      const char **rp) {
    int ok = matchname(log, user, service, rp);
    while (**rp == '|') {
        (*rp)++;
        ok |= matchname(log, user, service, rp);
    }
    return ok;
}

static long howmany(log_context *log, AuthState *history, time_t now, long limit) {
    if (firstAttempt(history))
        return -1;
    long i = 0;
    AuthAttempt attempt;
    while (!nextAttempt(history, &attempt)) {
        if (difftime(now, attempt.m_time) <= (double) limit)
            ++i;
    }
    log_debug(log, "howmany(%ld) = %ld", limit, i);
    return i;
}

static int matchperiod(log_context *log, AuthState *history, time_t now, const char **rp) {
    int err;
    long count, period;

    log_debug(log, "matchperiod(%p, %u, '%s')", history, getNofAttempts(history), *rp);

    if (err = parse_long(rp, &count), 0 != err) {
        return 0;
    }
    log_debug(log, "count is %ld, **rp='%c'", count, **rp);
    if (**rp != '/') {
        return 0;
    }
    (*rp)++;
    if (err = parse_time(rp, &period), 0 != err) {
        return 0;
    }
    log_debug(log, "period is %ld, **rp='%c'", period, **rp);
    log_debug(log, "Checking %ld/%ld", count, period);
    return howmany(log, history, now, period) >= count;
}

int rule_matchperiods(log_context *log, AuthState *history, time_t now, const char **rp) {
    if (matchperiod(log, history, now, rp)) {
        return 1;
    }
    while (**rp == ',') {
        (*rp)++;
        if (matchperiod(log, history, now, rp)) {
            return 1;
        }
    }
    return 0;
}

static int check_clause(log_context *log, const char **rp,
                        const char *user, const char *service,
                        AuthState *history, time_t now) {
    int inv = 0;

    if (**rp == '!') {
        inv = 1;
        (*rp)++;
    }

    if (!(inv ^ matchnames(log, user, service, rp))) {
        return 0;
    }

    log_debug(log, "Name matched, next char is '%c'", **rp);

    /* The name part matches so now check the trigger clauses */
    if (**rp != ':') {
        return 0;
    }

    (*rp)++;
    return rule_matchperiods(log, history, now, rp);
}

BlockState rule_test(log_context *log, const char *rule,
              const char *user, const char *service,
              AuthState *history, time_t now) {
    if (!rule)
        return CLEAR;
    const char *rp = rule;

    while (*rp != '\0') {
        if (check_clause(log, &rp, user, service, history, now)) {
            return BLOCKED;
        }
        while (*rp != '\0' && !isspace(*rp)) {
            rp++;
        }
        while (isspace(*rp)) {
            rp++;
        }
    }

    return CLEAR;
}