update mnetftpd to do logging

This commit is contained in:
Andrew Pamment 2018-05-29 14:39:54 +10:00
parent a85762d71f
commit 8b8552dcac
2 changed files with 35 additions and 12 deletions

View File

@ -7,6 +7,7 @@
#include <sys/stat.h> #include <sys/stat.h>
#include <sys/select.h> #include <sys/select.h>
#include <netinet/in.h> #include <netinet/in.h>
#include <stdarg.h>
#include <arpa/inet.h> #include <arpa/inet.h>
#include <netdb.h> #include <netdb.h>
#include <unistd.h> #include <unistd.h>
@ -24,12 +25,41 @@ static int client_count = 0;
static struct user_t **users; static struct user_t **users;
static int user_count = 0; static int user_count = 0;
char *logfile = NULL;
struct dllist { struct dllist {
char *data; char *data;
struct dllist *prev; struct dllist *prev;
struct dllist *next; struct dllist *next;
}; };
void dolog(char *fmt, ...) {
char buffer[PATH_MAX];
struct tm time_now;
time_t timen;
FILE *logfptr;
int mypid = getpid();
if (logfile == NULL) return;
timen = time(NULL);
localtime_r(&timen, &time_now);
logfptr = fopen(logfile, "a");
if (!logfptr) {
return;
}
va_list ap;
va_start(ap, fmt);
vsnprintf(buffer, PATH_MAX, fmt, ap);
va_end(ap);
fprintf(logfptr, "%04d-%02d-%02d %02d:%02d:%02d [%d] %s\n", time_now.tm_year + 1900, time_now.tm_mon + 1, time_now.tm_mday, time_now.tm_hour, time_now.tm_min, time_now.tm_sec, mypid, buffer);
fclose(logfptr);
}
void sigchld_handler(int s) void sigchld_handler(int s)
{ {
// waitpid() might overwrite errno, so we save and restore it: // waitpid() might overwrite errno, so we save and restore it:
@ -159,6 +189,8 @@ static int handler(void* user, const char* section, const char* name, const char
} else { } else {
cfg->ipv6 = 0; cfg->ipv6 = 0;
} }
} else if (strcasecmp(name, "log file") == 0) {
logfile = strdup(value);
} }
} else { } else {
newuser = NULL; newuser = NULL;
@ -321,7 +353,6 @@ void handle_STOR(struct ftpserver *cfg, struct ftpclient *client, char *path) {
} }
} }
fprintf(stderr, "FULL PATH %s\n", fullpath);
snprintf(fullpathpart, PATH_MAX, "%s.part", fullpath); snprintf(fullpathpart, PATH_MAX, "%s.part", fullpath);
if (stat(fullpathpart, &s) != 0 && stat(fullpath, &s) != 0) { if (stat(fullpathpart, &s) != 0 && stat(fullpath, &s) != 0) {
@ -479,7 +510,6 @@ void handle_PASV(struct ftpserver *cfg, struct ftpclient *client) {
socklen_t file_sock_len = sizeof(struct sockaddr_in); socklen_t file_sock_len = sizeof(struct sockaddr_in);
getsockname(client->data_srv_socket, (struct sockaddr*) &file_addr, &file_sock_len); getsockname(client->data_srv_socket, (struct sockaddr*) &file_addr, &file_sock_len);
fprintf(stderr, "%s\n", client->hostip);
ipcpy = strdup(client->hostip); ipcpy = strdup(client->hostip);
ipptr = strtok(ipcpy, "."); ipptr = strtok(ipcpy, ".");
@ -507,10 +537,6 @@ void handle_DELE(struct ftpserver *cfg, struct ftpclient *client, char *file) {
newpath = (char *)malloc(PATH_MAX); newpath = (char *)malloc(PATH_MAX);
parse_path(client, file, &newpath); parse_path(client, file, &newpath);
fprintf(stderr, "NEWPATH %s\n", newpath);
if (newpath[0] == '/') { if (newpath[0] == '/') {
if (strncmp(newpath, "/in", 3) == 0) { if (strncmp(newpath, "/in", 3) == 0) {
snprintf(fullpath, PATH_MAX, "%s/%s", client->current_path, &newpath[4]); snprintf(fullpath, PATH_MAX, "%s/%s", client->current_path, &newpath[4]);
@ -551,9 +577,6 @@ void handle_RETR(struct ftpserver *cfg, struct ftpclient *client, char *file) {
newpath = (char *)malloc(PATH_MAX); newpath = (char *)malloc(PATH_MAX);
parse_path(client, file, &newpath); parse_path(client, file, &newpath);
fprintf(stderr, "NEWPATH %s\n", newpath);
if (newpath[0] == '/') { if (newpath[0] == '/') {
if (strncmp(newpath, "/in", 3) == 0) { if (strncmp(newpath, "/in", 3) == 0) {
@ -570,7 +593,6 @@ void handle_RETR(struct ftpserver *cfg, struct ftpclient *client, char *file) {
snprintf(fullpath, PATH_MAX, "%s/%s", client->current_path, newpath); snprintf(fullpath, PATH_MAX, "%s/%s", client->current_path, newpath);
} }
fprintf(stderr, "FULL PATH %s\n", fullpath);
free(newpath); free(newpath);
struct stat s; struct stat s;
pid_t pid = fork(); pid_t pid = fork();
@ -822,8 +844,6 @@ void handle_CWD(struct ftpserver *cfg, struct ftpclient *client, char *dir) {
newpath = (char *)malloc(1024); newpath = (char *)malloc(1024);
parse_path(client, dir, &newpath); parse_path(client, dir, &newpath);
fprintf(stderr, "%s\n", newpath);
if (strcasecmp(newpath, "/in") == 0) { if (strcasecmp(newpath, "/in") == 0) {
client->current_path = client->user->indir; client->current_path = client->user->indir;
send_msg(client, "250 Okay.\r\n"); send_msg(client, "250 Okay.\r\n");
@ -921,8 +941,10 @@ int handle_client(struct ftpserver *cfg, struct ftpclient *client, char *buf, in
if (strcmp(cmd, "PASS") == 0) { if (strcmp(cmd, "PASS") == 0) {
fprintf(stderr, "command: %s, argument: ****\n", cmd); fprintf(stderr, "command: %s, argument: ****\n", cmd);
dolog("command: %s, argument: ****", cmd);
} else { } else {
fprintf(stderr, "command: %s, argument: %s\n", cmd, argument); fprintf(stderr, "command: %s, argument: %s\n", cmd, argument);
dolog("command: %s, argument: %s", cmd, argument);
} }
if (strcmp(cmd, "USER") == 0) { if (strcmp(cmd, "USER") == 0) {

View File

@ -4,6 +4,7 @@ Port = 2121
Min Passive Port = 60000 Min Passive Port = 60000
Max Passive Port = 65000 Max Passive Port = 65000
Enable IPv6 = true Enable IPv6 = true
Log File = /Path/to/logfile.txt
[andrew] [andrew]
password = secret password = secret