mnet ftpd and ftpc

This commit is contained in:
Andrew Pamment
2018-05-26 21:03:27 +10:00
parent cb0a0b0999
commit 067aebc794
3 changed files with 285 additions and 0 deletions

View File

@@ -698,6 +698,69 @@ void handle_LIST(struct ftpserver *cfg, struct ftpclient *client) {
}
}
void handle_NLST(struct ftpserver *cfg, struct ftpclient *client) {
DIR *dirp;
struct dirent *dp;
char newpath[PATH_MAX];
char linebuffer[PATH_MAX];
struct stat s;
pid_t pid = fork();
if (pid > 0) {
// nothing
client->data_socket = -1;
memset(client->data_ip, 0, INET6_ADDRSTRLEN);
client->data_srv_socket = -1;
} else if (pid == 0) {
if (client->current_path == NULL) {
if (!open_tcp_connection(cfg, client)) {
send_msg(client, "425 TCP connection cannot be established.\r\n");
exit(0);
}
send_msg(client, "150 Data connection accepted; transfer starting.\r\n");
if (stat(client->user->indir, &s) == 0) {
snprintf(linebuffer, PATH_MAX, "in\r\n");
}
send_data(client, linebuffer, strlen(linebuffer));
if (stat(client->user->outdir, &s) == 0) {
snprintf(linebuffer, PATH_MAX, "out\r\n");
}
send_data(client, linebuffer, strlen(linebuffer));
close_tcp_connection(client);
send_msg(client, "226 Transfer ok.\r\n");
exit(0);
} else {
dirp = opendir(client->current_path);
if (!dirp) {
send_msg(client, "451 Could not read directory.\r\n");
exit(0);
}
if (!open_tcp_connection(cfg, client)) {
send_msg(client, "425 TCP connection cannot be established.\r\n");
closedir(dirp);
exit(0);
}
send_msg(client, "150 Data connection accepted; transfer starting.\r\n");
while ((dp = readdir(dirp)) != NULL) {
snprintf(newpath, PATH_MAX, "%s/%s", client->current_path, dp->d_name);
if (stat(newpath, &s) == 0) {
snprintf(linebuffer, PATH_MAX, "%s\r\n", dp->d_name);
send_data(client, linebuffer, strlen(linebuffer));
}
}
closedir(dirp);
close_tcp_connection(client);
send_msg(client, "226 Transfer ok.\r\n");
exit(0);
}
} else {
send_msg(client, "451 Could not read directory.\r\n");
}
}
void handle_PORT(struct ftpserver *cfg, struct ftpclient *client, char *arg) {
if (client->data_socket > 0) {
close(client->data_socket);
@@ -891,6 +954,9 @@ int handle_client(struct ftpserver *cfg, struct ftpclient *client, char *buf, in
if (strcmp(cmd, "PORT") == 0) {
handle_PORT(cfg, client, argument);
} else
if (strcmp(cmd, "NLST") == 0) {
handle_NLST(cfg, client);
} else
if (strcmp(cmd, "LIST") == 0) {
handle_LIST(cfg, client);
} else