-
Notifications
You must be signed in to change notification settings - Fork 0
/
options.c
558 lines (507 loc) · 16.9 KB
/
options.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
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <grp.h>
#include <unistd.h>
#include <limits.h>
#include "options.h"
#include "mystring.h"
#include "main.h"
#include "login.h"
#include "logging.h"
struct global config_global;
struct group_of_users *config_groups;
struct user *config_users;
/*
Returns NULL on error. May return
emtpy string "" for empty or
commented lines.
*/
char *config_read_line(FILE *configfile)
{
static char str[MAX_STRING_LENGTH];
char *pound_sign;
char *first_quote, *second_quote;
char *s = str;
if (!fgets(str, MAX_STRING_LENGTH, configfile))
return NULL;
/*
while ((strchr(s, '#') > strchr(s, '"')) && strchr(s, '"')) {
s = strchr(strchr(s, '"') + 1, '"');
if (!s) { // This means there is only one " in the string, which is a syntax error.
str[0] = 0; // So we empty the string in order not to confuse the parser.
return str;
}
}
*/
pound_sign = strchr(str, '#');
if (pound_sign)
pound_sign[0] = '\0';
first_quote = strchr(str, '"');
/* if there are two quotes, cut the string off
after the second quote */
if (first_quote)
{
second_quote = strchr(&( first_quote[1]), '"' );
if (second_quote)
second_quote[1] = '\0';
/* there is just one quote, return error */
else
str[0] = '\0';
}
s = str;
while ((s[0] == ' ') || (s[0] == '\t'))
s++;
return s;
}
void create_options(FILE *configfile, struct bftpd_option **options, struct directory **directories)
{
char *str;
struct bftpd_option *opt = NULL;
struct directory *dir = NULL;
str = config_read_line(configfile);
if (! str) /* no data from the file */
return;
while (!strchr(str, '}')) {
if (str[0] != '\n') {
if ((strstr(str, "directory")) && (strchr(str, '{')) && (directories)) {
char *tmp;
if (dir) {
dir = dir->next = malloc(sizeof(struct directory));
} else {
*directories = dir = malloc(sizeof(struct directory));
}
/* avoid memory bug */
if (! dir) return;
/* tmp = strchr(str, '"') + 1; */
tmp = strchr(str, '"');
if (tmp)
{
tmp++;
*strchr(tmp, '"') = 0;
dir->path = strdup(tmp);
}
if (! dir->path)
dir->path = "";
create_options(configfile, &(dir->options), NULL);
} else {
if (opt) {
opt = opt->next = calloc(1, sizeof(struct bftpd_option));
} else {
*options = opt = calloc(1, sizeof(struct bftpd_option));
}
/* bail out on memory error */
if (! opt)
return;
opt->name = (char *) malloc( strlen(str) + 2 );
/* opt->value = (char *) malloc(strlen(str)); */
opt->value = (char *) malloc( strlen(str) + 256);
sscanf(str, "%[^=]=\"%[^\n\"]", opt->name, opt->value);
}
}
str = config_read_line(configfile);
if (! str) /* avoid segfault */
return;
}
}
void expand_groups()
{
char foo[USERLEN + 1];
struct passwd *temp;
struct group_of_users *grp;
struct group *grpinfo;
struct list_of_struct_passwd *endp = NULL;
struct list_of_struct_group *endg = NULL;
uid_t uid;
int i;
if ((grp = config_groups)) {
do {
strcat(grp->temp_members, ",");
while (strchr(grp->temp_members, ',')) {
sscanf(grp->temp_members, "%[^,]", foo);
cutto(grp->temp_members, strlen(foo) + 1);
if (foo[0] == '@') {
if (sscanf(foo + 1, "%i", &uid)) {
if (!((grpinfo = getgrgid(uid))))
continue;
} else
if (!((grpinfo = getgrnam(foo + 1))))
continue;
if (grp->groups)
endg = endg->next = malloc(sizeof(struct list_of_struct_group));
else
grp->groups = endg = malloc(sizeof(struct list_of_struct_group));
if (! endg) return; /* bail out on erro */
endg->grp.gr_name = strdup(grpinfo->gr_name);
endg->grp.gr_passwd = strdup(grpinfo->gr_passwd);
endg->grp.gr_gid = grpinfo->gr_gid;
for (i = 0; grpinfo->gr_mem[i]; i++);
endg->grp.gr_mem = malloc((i + 1) * sizeof(char *));
for (i = 0; grpinfo->gr_mem[i]; i++)
endg->grp.gr_mem[i] = strdup(grpinfo->gr_mem[i]);
endg->grp.gr_mem[i] = NULL;
}
if (sscanf(foo, "%i", &uid)) {
if (!((temp = getpwuid(uid))))
continue;
} else
if (!((temp = getpwnam(foo))))
continue;
if (grp->users)
endp = endp->next = malloc(sizeof(struct list_of_struct_passwd));
else
grp->users = endp = malloc(sizeof(struct list_of_struct_passwd));
if (! endp) return; /* bail out on error */
/* This is ugly, but you can't just use memcpy()! */
endp->pwd.pw_name = strdup(temp->pw_name);
endp->pwd.pw_passwd = strdup(temp->pw_passwd);
endp->pwd.pw_uid = temp->pw_uid;
endp->pwd.pw_gid = temp->pw_gid;
endp->pwd.pw_gecos = strdup(temp->pw_gecos);
endp->pwd.pw_dir = strdup(temp->pw_dir);
endp->pwd.pw_shell = strdup(temp->pw_shell);
}
free(grp->temp_members);
} while ((grp = grp->next));
}
}
void config_init()
{
FILE *configfile;
char *str;
struct group_of_users *grp = NULL;
struct user *usr = NULL;
config_global.options = NULL;
config_global.directories = NULL;
if (!configpath)
return;
configfile = fopen(configpath, "r");
if (!configfile) {
control_printf(SL_FAILURE, "421 Unable to open configuration file.");
exit(1);
}
while ((str = config_read_line(configfile))) {
if (strchr(str, '{'))
{
/* replace all unwanted spaces */
while ( strstr(str, " {") )
replace(str, " {", "{", MAX_STRING_LENGTH);
while ( strstr(str, "{ ") )
replace(str, "{ ", "{", MAX_STRING_LENGTH);
while ( strstr(str, " }") )
replace(str, " }", "}", MAX_STRING_LENGTH);
while ( strstr(str, "} ") )
replace(str, "} ", "}", MAX_STRING_LENGTH);
if (!strcasecmp(str, "global{\n")) {
create_options(configfile, &(config_global.options), &(config_global.directories));
} else if (strstr(str, "user ") == str) {
if (usr) {
usr = usr->next = calloc(1, sizeof(struct user));
} else {
config_users = usr = calloc(1, sizeof(struct user));
}
/* avoid memory errors */
if (! usr)
{
fclose(configfile);
control_printf(SL_FAILURE, "421 Memory error while reading config file.");
return;
}
usr->name = strdup(str + 5);
if (! usr->name )
{
fclose(configfile);
control_printf(SL_FAILURE, "421 Memory error while handling config file.");
exit(1);
}
*strchr(usr->name, '{') = 0;
create_options(configfile, &(usr->options), &(usr->directories));
} else if (strstr(str, "group ") == str) {
if (grp) {
grp = grp->next = malloc(sizeof(struct group_of_users));
} else {
config_groups = grp = malloc(sizeof(struct group_of_users));
}
if (! grp)
{
fclose(configfile);
control_printf(SL_FAILURE, "421 Memory error while handling config file.");
exit(1);
}
cutto(str, 6);
*strchr(str, '{') = 0;
grp->users = NULL;
grp->next = NULL;
grp->temp_members = strdup(str);
create_options(configfile, &(grp->options), &(grp->directories));
}
}
}
fclose(configfile);
}
char *getoption(struct bftpd_option *opt, char *name)
{
if (! opt)
return NULL;
if (! name)
return NULL;
do
{
if (opt->name) /* avoid segfault */
{
if (!strcasecmp(opt->name, name))
return opt->value;
}
} while ((opt = opt->next));
return NULL;
}
char *getoption_directories(struct directory *dir, char *name) {
char curpath[MAX_STRING_LENGTH], *bar;
struct directory *longest = NULL;
if(!dir)
return NULL;
memset(curpath, '\0', MAX_STRING_LENGTH);
getcwd(curpath, sizeof(curpath) - 1);
strcat(curpath, "/");
do {
bar = malloc(strlen(dir->path) + 2);
if (! bar)
return NULL;
strcpy(bar, dir->path);
strcat(bar, "/");
if (!strncmp(curpath, bar, strlen(bar))) {
if (longest) {
if ((strlen(bar) > strlen(longest->path) + 1)
&& (getoption(dir->options, name)))
longest = dir;
} else {
if (getoption(dir->options, name))
longest = dir;
}
}
free(bar);
} while ((dir = dir->next));
if (longest)
return getoption(longest->options, name);
return NULL;
}
char user_is_in_group(struct group_of_users *grp) {
struct list_of_struct_group *grplist = grp->groups;
struct list_of_struct_passwd *pwdlist = grp->users;
int i;
if (pwdlist) {
do {
if (!strcmp(user, pwdlist->pwd.pw_name))
return 1;
} while ((pwdlist = pwdlist->next));
}
if (grplist) {
do {
if (userinfo.pw_gid == grplist->grp.gr_gid)
return 1;
if (grplist->grp.gr_mem) // avoid segfault
{
for (i = 0; grplist->grp.gr_mem[i]; i++)
if (!strcmp(grplist->grp.gr_mem[i], user))
return 1;
}
} while ((grplist = grplist->next));
}
return 0;
}
char *getoption_group(char *name)
{
char *result;
struct group_of_users *grp;
if ((grp = config_groups)) {
do {
if (user_is_in_group(grp) && grp->options) {
if ((result = getoption_directories(grp->directories, name)))
return result;
if ((result = getoption(grp->options, name)))
return result;
}
} while ((grp = grp->next));
}
return NULL;
}
char *getoption_user(char *name)
{
char *result;
struct user *usr;
if ((usr = config_users)) {
do {
if (!strcmp(user, usr->name)) {
if ((result = getoption_directories(usr->directories, name)))
return result;
if ((result = getoption(usr->options, name)))
return result;
}
} while ((usr = usr->next));
}
return NULL;
}
char *getoption_global(char *name)
{
char *result;
if ((result = getoption_directories(config_global.directories, name)))
return result;
if (config_global.options) {
if ((result = getoption(config_global.options, name)))
return result;
}
return NULL;
}
/* returns null string on falure or pointer to value */
char *config_getoption(char *name)
{
static char empty = 0;
char *foo;
if (userinfo_set) {
if ((foo = getoption_user(name)))
return foo;
if ((foo = getoption_group(name)))
return foo;
}
if ((foo = getoption_global(name)))
return foo;
else
return ∅
}
void config_end()
{
/* Needn't do anything ATM */
}
/*
This function attempts to find a option, stored
in memory, by the given name. It searches
global options first, then group options and,
finally, individual user options. If a value
is found for the option, the function returns
a pointer to the value of the option. If no match
is found, an empty string is returned.
-- Jesse
*/
char *config_getoption_reread(char *find_me)
{
char *return_value;
static char empty_string = 0;
return_value = getoption(config_global.options, find_me);
if (return_value)
return return_value;
return &empty_string;
}
/*
This function opens the config file and
tries to reset some of the option values
in memory.
-- Jesse
*/
void Reread_Config_File()
{
char *line; // line in config file
char *config_value; // value stored in memory
char *new_value;
FILE *config_file;
/* int xfer_delay; */
int section = 0; // where are we in the config file
unsigned long get_value;
// open config file
config_file = fopen(configpath, "r");
if (! config_file)
return;
/* read a line from the config file */
line = config_read_line(config_file);
while ( line )
{
if ( strchr(line, '{') )
section++;
/* look for reconized option name */
if ( strstr(line, "HELLO_STRING") )
config_value = config_getoption_reread("HELLO_STRING");
else if ( strstr(line, "QUIT_MSG") )
config_value = config_getoption_reread("QUIT_MSG");
else if ( strstr(line, "XFERBUFSIZE") )
config_value = config_getoption_reread("XFERBUFSIZE");
else if ( strstr(line, "DATA_TIMEOUT") )
config_value = config_getoption_reread("DATA_TIMEOUT");
else if ( strstr(line, "CONTROL_TIMEOUT") )
config_value = config_getoption_reread("CONTROL_TIMEOUT");
else if ( strstr(line, "USERLIMIT_GLOBAL") )
config_value = config_getoption_reread("USERLIMIT_GLOBAL");
else if ( strstr(line, "USERLIMIT_SINGLEUSER") )
config_value = config_getoption_reread("USERLIMIT_SINGLEUSER");
else if ( strstr(line, "USERLIMIT_HOST") )
config_value = config_getoption_reread("USERLIMIT_HOST");
else if ( strstr(line, "DENY_LOGIN") )
config_value = config_getoption_reread("DENY_LOGIN");
else if ( strstr(line, "XFER_DELAY") )
config_value = config_getoption_reread("XFER_DELAY");
else if ( strstr(line, "GZ_UPLOAD") )
config_value = config_getoption_reread("GZ_UPLOAD");
else
config_value = NULL;
/* get new value from input */
new_value = strchr(line, '"') ;
if (new_value)
{
char *temp;
new_value++; // go to first character after quote
temp = strchr(new_value, '"');
if (temp)
temp[0] = '\0'; // null terminal string
}
/* set value of option */
if ( (config_value) && (new_value) && (section == 1) )
{
// make sure it will fit.
if ( strlen(new_value) < 256)
strcpy(config_value, new_value);
}
line = config_read_line(config_file);
} /* while not end of file */
fclose(config_file);
/* reset numeric values */
get_value = strtoul( config_getoption("XFER_BUFSIZE"), NULL, 0 );
if (get_value <= INT_MAX)
xfer_bufsize = get_value;
else
xfer_bufsize = XFER_BUFSIZE;
if (! xfer_bufsize )
xfer_bufsize = XFER_BUFSIZE;
control_timeout = atoi( config_getoption("CONTROL_TIMEOUT") );
if (! control_timeout)
control_timeout = CONTROL_TIMEOUT;
data_timeout = atoi( config_getoption("DATA_TIMEOUT") );
if (! data_timeout)
data_timeout = DATA_TIMEOUT;
/* xfer_delay = atoi( config_getoption("XFER_DELAY") ); */
}
/* end of re-read config file */
/*
This function tries to find a valid configuration file in
either /etc/bftpd.conf or PREFIX/etc/bftpd.conf.
If neither is found, the former is returned.
*/
char *Find_Config_File()
{
FILE *config_file;
/* the default is to use PATH_BFTPD_CONF */
config_file = fopen(PATH_BFTPD_CONF, "r");
if (config_file)
{
fclose(config_file);
return PATH_BFTPD_CONF;
}
/* this path can only be tried is Bftpd is passed a prefix */
#ifdef PATH_BFTPD_CONF_WITH_PREFIX
config_file = fopen(PATH_BFTPD_CONF_WITH_PREFIX, "r");
if (config_file)
{
fclose(config_file);
return PATH_BFTPD_CONF_WITH_PREFIX;
}
#endif
/* nothing was found, fall back on default */
return PATH_BFTPD_CONF;
}