-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
455 lines (410 loc) · 12.5 KB
/
main.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
/*
bftpd Copyright (C) 1999-2003 Max-Wilhelm Bruker
This program is is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License, version 2 of the
License as published by the Free Software Foundation.
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.
*/
#include <config.h>
#include <stdio.h>
#ifdef HAVE_SYS_TYPES_H
#include <sys/types.h>
#endif
#ifdef HAVE_SYS_STAT_H
#include <sys/stat.h>
#endif
#ifdef HAVE_SYS_SOCKET_H
#include <sys/socket.h>
#endif
#ifdef HAVE_NETINET_IN_H
#include <netinet/in.h>
#endif
#ifdef HAVE_ARPA_INET_H
#include <arpa/inet.h>
#endif
#ifdef HAVE_ASM_SOCKET_H
#include <asm/socket.h>
#endif
#include <signal.h>
#include <string.h>
#include <stdlib.h>
#include <netdb.h>
#include <unistd.h>
#include <limits.h>
#include <errno.h>
#include <fcntl.h>
#ifdef HAVE_WAIT_H
# include <wait.h>
#else
# ifdef HAVE_SYS_WAIT_H
# include <sys/wait.h>
# endif
#endif
#include "main.h"
#include "cwd.h"
#include "mystring.h"
#include "logging.h"
#include "dirlist.h"
#include "bftpdutmp.h"
#include "options.h"
#include "login.h"
#include "list.h"
int global_argc;
char **global_argv;
char **my_argv_list; // jesse
struct sockaddr_in name;
int isparent = 1;
int listensocket, sock;
FILE *passwdfile = NULL, *groupfile = NULL, *devnull;
struct sockaddr_in remotename;
char *remotehostname = NULL;
int control_timeout, data_timeout;
int alarm_type = 0;
struct bftpd_list_element *child_list;
/* Command line parameters */
char *configpath = PATH_BFTPD_CONF;
int daemonmode = 0;
void print_file(int number, char *filename)
{
FILE *phile;
char foo[MAX_STRING_LENGTH];
phile = fopen(filename, "r");
if (phile) {
while (fgets(foo, MAX_STRING_LENGTH, phile)) {
foo[strlen(foo) - 1] = '\0';
control_printf(SL_SUCCESS, "%i-%s", number, foo);
}
fclose(phile);
}
}
void end_child()
{
Force_Update_Sent_Recv_Log();
if (passwdfile)
fclose(passwdfile);
if (groupfile)
fclose(groupfile);
config_end();
bftpd_log("Quitting.\n");
bftpd_statuslog(1, 0, "quit");
bftpdutmp_end();
log_end();
login_end();
bftpd_cwd_end();
if (daemonmode) {
close(sock);
close(0);
close(1);
close(2);
}
}
/*
This function causes the program to
re-read parts of the config file.
-- Jesse
*/
void handler_sighup(int sig)
{
bftpd_log("Caught HUP signal. Re-reading config file.\n");
Reread_Config_File();
signal(sig, handler_sighup);
}
void handler_sigchld(int sig)
{
pid_t pid;
int i;
struct bftpd_childpid *childpid;
/* Get the child's return code so that the zombie dies */
/* pid = wait(NULL); */
pid = waitpid(-1, NULL, WNOHANG);
while (pid > 0)
{
for (i = 0; i < bftpd_list_count(child_list); i++) {
childpid = bftpd_list_get(child_list, i);
if ( (childpid) && (childpid->pid == pid) ) {
close(childpid->sock);
bftpd_list_del(&child_list, i);
free(childpid);
/* make sure the child is removed from the log */
bftpdutmp_remove_pid(pid);
}
}
pid = waitpid(-1, NULL, WNOHANG); /* check for more children */
}
}
void handler_sigterm(int signum)
{
bftpdutmp_end();
exit(0); /* Force normal termination so that end_child() is called */
}
void handler_sigalrm(int signum)
{
/* Log user out. -- Jesse <slicer69@hotmail.com> */
bftpdutmp_end();
// Force_Update_Sent_Recv_Log();
if (alarm_type) {
close(alarm_type);
bftpd_log("Kicked from the server due to data connection timeout.\n");
control_printf(SL_FAILURE, "421 Kicked from the server due to data connection timeout.");
exit(0);
} else {
bftpd_log("Kicked from the server due to control connection timeout.\n");
control_printf(SL_FAILURE, "421 Kicked from the server due to control connection timeout.");
exit(0);
}
}
void init_everything()
{
if (!daemonmode)
{
config_init();
hidegroups_init();
}
log_init();
bftpdutmp_init();
login_init();
Open_Send_Receive_Log();
}
int main(int argc, char **argv)
{
char str[MAX_STRING_LENGTH + 1];
static struct hostent *he;
int i = 1, port;
int retval;
unsigned long get_value;
socklen_t my_length;
char *temp_string = NULL;
my_argv_list = argv;
signal(SIGHUP, handler_sighup);
/* try both default locations for configuration file */
configpath = Find_Config_File();
while (((retval = getopt(argc, argv, "c:hvdDin"))) > -1) {
switch (retval) {
case 'h':
printf(
"Usage: %s [-h] [-v] [-i|-d|-D] [-c <filename>|-n]\n"
"-h print this help\n"
"-v display version number\n"
"-i (default) run from inetd\n"
"-d daemon mode: fork() and run in TCP listen mode\n"
"-D run in TCP listen mode, but don't pre-fork()\n"
"-c read the config file named \"filename\" instead of " PATH_BFTPD_CONF "\n"
"-n no config file, use defaults\n", argv[0]);
return 0;
case 'v': printf("Bftpd version %s\n", VERSION);
return 0;
case 'i': daemonmode = 0; break;
case 'd': daemonmode = 1; break;
case 'D': daemonmode = 2; break;
case 'c': configpath = strdup(optarg); break;
case 'n': configpath = NULL; break;
}
}
if (daemonmode) {
struct sockaddr_in myaddr, new;
if (daemonmode == 1) {
if (fork())
exit(0); /* Exit from parent process */
setsid();
if (fork())
return 0;
}
signal(SIGCHLD, handler_sigchld);
config_init();
if (chdir("/") == -1)
fprintf(stderr, "Chdir failed: %s\n", strerror(errno));
hidegroups_init();
listensocket = socket(AF_INET, SOCK_STREAM, 0);
#ifdef SO_REUSEADDR
setsockopt(listensocket, SOL_SOCKET, SO_REUSEADDR, (void *) &i,
sizeof(i));
#endif
#ifdef SO_REUSEPORT
setsockopt(listensocket, SOL_SOCKET, SO_REUSEPORT, (void *) &i,
sizeof(i));
#endif
memset((void *) &myaddr, 0, sizeof(myaddr));
myaddr.sin_family = AF_INET;
if (!((port = strtoul(config_getoption("PORT"), NULL, 10))))
port = DEFAULT_PORT;
myaddr.sin_port = htons(port);
if (!strcasecmp(config_getoption("BIND_TO_ADDR"), "any")
|| !config_getoption("BIND_TO_ADDR")[0])
myaddr.sin_addr.s_addr = INADDR_ANY;
else
myaddr.sin_addr.s_addr = inet_addr(config_getoption("BIND_TO_ADDR"));
if (bind(listensocket, (struct sockaddr *) &myaddr, sizeof(myaddr)) < 0) {
fprintf(stderr, "Bind failed: %s\n", strerror(errno));
exit(1);
}
if (listen(listensocket, 5)) {
fprintf(stderr, "Listen failed: %s\n", strerror(errno));
exit(1);
}
/* check for open stdin, stdout, stderr */
if (listensocket >= 3)
{
for (i = 0; i < 3; i++) {
close(i); /* Remove fd pointing to the console */
open("/dev/null", O_RDWR); /* Create fd pointing nowhere */
}
}
my_length = sizeof(new);
while ((sock = accept(listensocket, (struct sockaddr *) &new, &my_length))) {
pid_t pid;
/* If accept() becomes interrupted by SIGCHLD, it will return -1.
* So in order not to create a child process when that happens,
* we have to check if accept() returned an error.
*/
if (sock > 0) {
pid = fork();
if (!pid) { /* child */
close(0);
close(1);
close(2);
isparent = 0;
dup2(sock, fileno(stdin));
dup2(sock, fileno(stderr));
break;
} else { /* parent */
struct bftpd_childpid *tmp_pid = malloc(sizeof(struct bftpd_childpid));
tmp_pid->pid = pid;
tmp_pid->sock = sock;
bftpd_list_add(&child_list, tmp_pid);
}
}
}
}
/* Child only. From here on... */
devnull = fopen("/dev/null", "w");
if (! devnull)
fprintf(stderr, "Fopen failed: %s\n", strerror(errno));
global_argc = argc;
global_argv = argv;
init_everything();
atexit(end_child);
signal(SIGTERM, handler_sigterm);
signal(SIGALRM, handler_sigalrm);
/* If we do not have getpwnam() for some reason, then
we must use FILE_AUTH or exit. */
#ifdef NO_GETPWNAM
{
char *file_auth_option;
file_auth_option = config_getoption("FILE_AUTH");
if (! file_auth_option[0] )
{
bftpd_log("Exiting, becasue we have no way to authorize clients.\n");
exit(0);
}
}
#endif
get_value = strtoul(config_getoption("CONTROL_TIMEOUT"), NULL, 0);
if (get_value <= INT_MAX)
control_timeout = get_value;
else
control_timeout = 0;
if (!control_timeout)
control_timeout = CONTROL_TIMEOUT;
get_value = strtoul(config_getoption("DATA_TIMEOUT"), NULL, 0);
if (get_value <= INT_MAX)
data_timeout = get_value;
else
data_timeout = 0;
if (!data_timeout)
data_timeout = DATA_TIMEOUT;
get_value = strtoul(config_getoption("XFER_BUFSIZE"), NULL, 0);
if (get_value <= INT_MAX)
xfer_bufsize = get_value;
else
xfer_bufsize = 9;
/* handled below
if (!xfer_bufsize)
xfer_bufsize = XFER_BUFSIZE;
*/
/* do range check on xfer_bufsize, which is an int */
if ( (xfer_bufsize <= 0) || (xfer_bufsize > 1000000) )
xfer_bufsize = XFER_BUFSIZE;
/* get scripts to run pre and post write */
pre_write_script = config_getoption("PRE_WRITE_SCRIPT");
if (! pre_write_script[0])
pre_write_script = NULL;
post_write_script = config_getoption("POST_WRITE_SCRIPT");
if (! post_write_script[0])
post_write_script = NULL;
my_length = sizeof(remotename);
if (getpeername(fileno(stderr), (struct sockaddr *) &remotename, &my_length)) {
control_printf(SL_FAILURE, "421-Could not get peer IP address.\r\n421 %s.",
strerror(errno));
return 0;
}
i = 1;
#ifndef __minix
setsockopt(fileno(stdin), SOL_SOCKET, SO_OOBINLINE, (void *) &i,
sizeof(i));
#endif
setsockopt(fileno(stdin), SOL_SOCKET, SO_KEEPALIVE, (void *) &i,
sizeof(i));
/* If option is set, determine the client FQDN */
if (!strcasecmp((char *) config_getoption("RESOLVE_CLIENT_IP"), "yes")) {
if ((he = gethostbyaddr((char *) &remotename.sin_addr, sizeof(struct in_addr), AF_INET)))
{
if (he->h_name)
remotehostname = strdup(he->h_name);
else
remotehostname = strdup("");
}
else
{
temp_string = inet_ntoa(remotename.sin_addr);
if (temp_string)
remotehostname = strdup(temp_string);
else
remotehostname = strdup("");
}
}
else
{
temp_string = inet_ntoa(remotename.sin_addr);
if (temp_string)
remotehostname = strdup(temp_string);
else
remotehostname = strdup("");
}
bftpd_log("Incoming connection from %s.\n", remotehostname);
bftpd_statuslog(1, 0, "connect %s", remotehostname);
my_length = sizeof(name);
getsockname(fileno(stdin), (struct sockaddr *) &name, &my_length);
print_file(220, config_getoption("MOTD_GLOBAL"));
/* Parse hello message */
strcpy(str, (char *) config_getoption("HELLO_STRING"));
replace(str, "%v", VERSION, MAX_STRING_LENGTH);
if (strstr(str, "%h")) {
if ((he = gethostbyaddr((char *) &name.sin_addr, sizeof(struct in_addr), AF_INET)))
replace(str, "%h", he->h_name, MAX_STRING_LENGTH);
else
replace(str, "%h", (char *) inet_ntoa(name.sin_addr), MAX_STRING_LENGTH);
}
replace(str, "%i", (char *) inet_ntoa(name.sin_addr), MAX_STRING_LENGTH);
control_printf(SL_SUCCESS, "220 %s", str);
/* We might not get any data, so let's set an alarm before the
first read. -- Jesse <slicer69@hotmail.com> */
alarm(control_timeout);
/* Read lines from client and execute appropriate commands */
while (fgets(str, MAXCMD, stdin)) {
int string_length;
alarm(control_timeout);
string_length = strlen(str) - 2;
if (string_length < 0) string_length = 0;
str[string_length] = 0;
bftpd_statuslog(2, 0, "%s", str);
#ifdef DEBUG
bftpd_log("Processing command: %s\n", str);
#endif
parsecmd(str);
}
if (remotehostname) free(remotehostname);
return 0;
}