forked from jedisct1/pure-ftpd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlog_puredb.c
409 lines (380 loc) · 11.1 KB
/
log_puredb.c
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
402
403
404
405
406
407
408
409
#include <config.h>
#ifdef WITH_PUREDB
#include "ftpd.h"
#include "messages.h"
#include "log_puredb.h"
#include "pure-pw.h"
#include "../puredb/src/puredb_read.h"
#include "utils.h"
#ifdef HAVE_LIBSODIUM
# include <sodium.h>
#endif
#ifdef WITH_DMALLOC
# include <dmalloc.h>
#endif
static char *pdb_filename;
void pw_puredb_parse(const char * const file)
{
if (file == NULL || *file == 0) {
die(421, LOG_ERR, MSG_NO_VIRTUAL_FILE);
}
if ((pdb_filename = strdup(file)) == NULL) {
die_mem();
}
}
void pw_puredb_exit(void)
{
free(pdb_filename);
}
/*
* The difference between this strtok() and the libc's one is that
* this one doesn't skip empty fields, and takes a char instead of a
* string as a delimiter.
* This strtok2() variant leaves zeroes.
*/
static char *my_strtok2(char *str, const char delim)
{
static char *s;
static char save;
if (str != NULL) {
if (*str == 0) {
return NULL;
}
s = str;
scan:
while (*s != 0 && *s != delim) {
s++;
}
save = *s;
*s = 0;
return str;
}
if (s == NULL || save == 0) {
return NULL;
}
s++;
str = s;
goto scan;
}
/* Check whether an IP address matches a pattern. 1 = match 0 = nomatch */
static int access_ip_match(const struct sockaddr_storage * const sa,
char * pattern)
{
unsigned int ip0, ip1, ip2, ip3;
unsigned int netbits;
unsigned long ip;
unsigned long mask;
unsigned long saip;
const unsigned char *saip_raw;
char *comapoint;
if (*pattern == 0) {
return 1;
}
if (STORAGE_FAMILY(*sa) != AF_INET) {
return 0; /* TODO: IPv6 */
}
do {
if ((comapoint = strchr(pattern, ',')) != NULL) {
*comapoint = 0;
}
if (sscanf(pattern, "%u.%u.%u.%u/%u", /* IPv4 */
&ip0, &ip1, &ip2, &ip3, &netbits) == 5) {
ipcheck:
if (STORAGE_FAMILY(*sa) != AF_INET || netbits == 0U) {
return -1;
}
ip =
((unsigned long) ip0 << 24) |
((unsigned long) ip1 << 16) |
((unsigned long) ip2 << 8) |
(unsigned long) ip3;
ipcheck_ipdone:
mask = ~((0x80000000 >> (netbits - 1U)) - 1U);
saip_raw = (const unsigned char *) &(STORAGE_SIN_ADDR(*sa));
saip =
((unsigned long) saip_raw[0] << 24) |
((unsigned long) saip_raw[1] << 16) |
((unsigned long) saip_raw[2] << 8) |
(unsigned long) saip_raw[3];
if ((ip & mask) == (saip & mask)) {
return 1;
}
} else if (sscanf(pattern, "%u.%u.%u.%u",
&ip0, &ip1, &ip2, &ip3) == 4) {
netbits = 32U;
goto ipcheck;
} else {
struct addrinfo hints, *res;
int on;
memset(&hints, 0, sizeof hints);
hints.ai_family = AF_INET;
hints.ai_addr = NULL;
if ((on = getaddrinfo(pattern, NULL, &hints, &res)) != 0) {
logfile(LOG_WARNING, "puredb: [%s] => [%d]", pattern, on);
} else if (res->ai_family != AF_INET) {
freeaddrinfo(res);
} else {
const unsigned char * const ip_raw =
(const unsigned char *) &
(((const struct sockaddr_in *) (void *)
(res->ai_addr))->sin_addr.s_addr);
ip =
((unsigned long) ip_raw[0] << 24) |
((unsigned long) ip_raw[1] << 16) |
((unsigned long) ip_raw[2] << 8) |
(unsigned long) ip_raw[3];
netbits = 32U;
freeaddrinfo(res);
goto ipcheck_ipdone;
}
}
if (comapoint == NULL) {
break;
}
*comapoint = ',';
pattern = comapoint + 1;
} while (*pattern != 0);
return 0;
}
/* IP check. 0 = ok, -1 = denied */
static int access_ip_check(const struct sockaddr_storage * const sa,
char * const allow, char * const deny)
{
if (sa == NULL) {
return 0;
}
if (*allow == 0) {
if (*deny == 0) {
return 0;
}
if (access_ip_match(sa, deny) != 0) {
return -1;
}
return 0;
}
if (*deny == 0) {
if (access_ip_match(sa, allow) != 0) {
return 0;
}
return -1;
}
if (access_ip_match(sa, allow) != 0 && access_ip_match(sa, deny) == 0) {
return 0;
}
return -1;
}
static int time_restrictions_check(const char * const restrictions)
{
const struct tm *tm;
time_t now_t;
unsigned int time_begin, time_end;
unsigned int now;
if (*restrictions == 0) {
return 0;
}
if (sscanf(restrictions, "%u-%u", &time_begin, &time_end) != 2 ||
(now_t = time(NULL)) == (time_t) -1 ||
(tm = localtime(&now_t)) == NULL) {
return 0;
}
now = (unsigned int) tm->tm_hour * 100U + (unsigned int) tm->tm_min;
if (time_begin <= time_end) {
if (time_begin <= now && now <= time_end) {
return 0;
}
return -1;
}
if (now >= time_begin || now <= time_end) {
return 0;
}
return -1;
}
static int pw_puredb_parseline(char *line, const char * const pwd,
const struct sockaddr_storage * const sa,
const struct sockaddr_storage * const peer,
AuthResult * const result)
{
char *allow_local_ip, *deny_local_ip;
char *allow_remote_ip, *deny_remote_ip;
const char *time_restrictions;
if ((line = my_strtok2(line, *PW_LINE_SEP)) == NULL || *line == 0) { /* pwd */
return -1;
}
{
const char *crypted;
int ret = -1;
#ifdef HAVE_LIBSODIUM
# ifdef crypto_pwhash_STRPREFIX
if (strncmp(crypto_pwhash_STRPREFIX, line,
(sizeof crypto_pwhash_STRPREFIX) - 1U) == 0) {
ret = crypto_pwhash_str_verify(line, pwd, strlen(pwd));
if (ret != 0) {
return -1;
}
} else
# endif
if (line[0] == '$' && line[1] == '7' && line[2] == '$') {
ret = crypto_pwhash_scryptsalsa208sha256_str_verify
(line, pwd, strlen(pwd));
if (ret != 0) {
return -1;
}
} else
#endif
{
ret = - ((crypted = (const char *) crypt(pwd, line)) == NULL ||
pure_strcmp(line, crypted) != 0);
if (ret != 0) {
return -1;
}
}
}
if ((line = my_strtok2(NULL, *PW_LINE_SEP)) == NULL || *line == 0) { /* uid */
return -1;
}
result->uid = (uid_t) strtoul(line, NULL, 10);
if ((line = my_strtok2(NULL, *PW_LINE_SEP)) == NULL || *line == 0) { /* gid */
return -1;
}
result->gid = (gid_t) strtoul(line, NULL, 10);
#ifndef ACCEPT_ROOT_VIRTUAL_USERS
if (result->uid <= (uid_t) 0 || result->gid <= (gid_t) 0) {
return -1;
}
#endif
if (my_strtok2(NULL, *PW_LINE_SEP) == NULL) { /* gecos */
return -1;
}
if ((line = my_strtok2(NULL, *PW_LINE_SEP)) == NULL || *line == 0) { /* home */
return -1;
}
if ((result->dir = strdup(line)) == NULL || *result->dir != '/') {
return -1;
}
if ((line = my_strtok2(NULL, *PW_LINE_SEP)) == NULL) { /* bw_ul */
return 0;
}
#ifdef THROTTLING
if (*line != 0) {
result->throttling_ul_changed = 1;
result->throttling_bandwidth_ul = strtoul(line, NULL, 10);
}
#endif
if ((line = my_strtok2(NULL, *PW_LINE_SEP)) == NULL) { /* bw_dl */
return 0;
}
#ifdef THROTTLING
if (*line != 0) {
result->throttling_dl_changed = 1;
result->throttling_bandwidth_dl = strtoul(line, NULL, 10);
}
#endif
if ((line = my_strtok2(NULL, *PW_LINE_SEP)) == NULL) { /* ratio up */
return 0;
}
#ifdef RATIOS
if (*line != 0) {
result->ratio_upload = (unsigned int) strtoul(line, NULL, 10);
if (result->ratio_upload > 0U) {
result->ratio_ul_changed = 1;
}
}
#endif
if ((line = my_strtok2(NULL, *PW_LINE_SEP)) == NULL) { /* ratio down */
return 0;
}
#ifdef RATIOS
if (*line != 0) {
result->ratio_download = (unsigned int) strtoul(line, NULL, 10);
if (result->ratio_download > 0U) {
result->ratio_dl_changed = 1;
}
}
#endif
if ((line = my_strtok2(NULL, *PW_LINE_SEP)) == NULL) { /* max cnx */
return 0;
}
#ifdef PER_USER_LIMITS
if (*line != 0) {
result->per_user_max = (unsigned int) strtoull(line, NULL, 10);
}
#endif
if ((line = my_strtok2(NULL, *PW_LINE_SEP)) == NULL) { /* files quota */
return 0;
}
#ifdef QUOTAS
if (*line != 0) {
result->quota_files_changed = 1;
result->user_quota_files = strtoull(line, NULL, 10);
}
#endif
if ((line = my_strtok2(NULL, *PW_LINE_SEP)) == NULL) { /* size quota */
return 0;
}
#ifdef QUOTAS
if (*line != 0) {
result->quota_size_changed = 1;
result->user_quota_size = strtoull(line, NULL, 10);
}
#endif
if ((line = my_strtok2(NULL, *PW_LINE_SEP)) == NULL) { /* allowed local ip */
return 0;
}
allow_local_ip = line;
if ((line = my_strtok2(NULL, *PW_LINE_SEP)) == NULL) { /* denied local ip */
return 0;
}
deny_local_ip = line;
if ((line = my_strtok2(NULL, *PW_LINE_SEP)) == NULL) { /* allowed remote ip */
return 0;
}
allow_remote_ip = line;
if ((line = my_strtok2(NULL, *PW_LINE_SEP)) == NULL) { /* denied remote ip */
return 0;
}
deny_remote_ip = line;
if (access_ip_check(sa, allow_local_ip, deny_local_ip) != 0 ||
access_ip_check(peer, allow_remote_ip, deny_remote_ip) != 0) {
return -1;
}
if ((line = my_strtok2(NULL, *PW_LINE_SEP)) == NULL) { /* time restrictions */
return 0;
}
time_restrictions = line;
if (time_restrictions_check(time_restrictions) != 0) {
return -1;
}
return 0;
}
void pw_puredb_check(AuthResult * const result,
const char *account, const char *password,
const struct sockaddr_storage * const sa,
const struct sockaddr_storage * const peer)
{
char *line = NULL;
PureDB db;
off_t retpos;
size_t retlen;
result->auth_ok = 0;
(void) sa;
(void) peer;
if (puredb_open(&db, pdb_filename) != 0) {
die(421, LOG_ERR, MSG_PDB_BROKEN);
}
if (puredb_find_s(&db, account, &retpos, &retlen) != 0) {
goto bye;
}
if ((line = puredb_read(&db, retpos, retlen)) == NULL) {
goto bye;
}
result->auth_ok--;
if (pw_puredb_parseline(line, password, sa, peer, result) != 0) {
goto bye;
}
result->slow_tilde_expansion = 1;
result->auth_ok = -result->auth_ok;
bye:
puredb_read_free(line);
puredb_close(&db);
}
#endif