#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <sys/stat.h>
#include <time.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <stdarg.h>
#include <signal.h>
#include <sys/wait.h>
#include <fcntl.h>
#include <pwd.h>
#include <grp.h>
#include <dirent.h>
#define CUFTPD_DEBUG(fmt, ...) cuftpd_debug(__FILE__, __LINE__, fmt, __VA_ARGS__)
#define CUFTPD_ARR_LEN(arr) (sizeof(arr)/sizeof(arr[0]))
#define CUFTPD_VER "1.0"
#define CUFTPD_DEF_SRV_PORT 21
#define CUFTPD_LISTEN_QU_LEN 8
#define CUFTPD_LINE_END "\r\n"
#define CUFTPD_OK 0
#define CUFTPD_ERR (-1)
#define CUFTPD_CHECK_LOGIN() \
do { \
if (!cuftpd_cur_user) { \
cuftpd_send_resp(ctrlfd, 530, "first please"); \
return CUFTPD_ERR; \
} \
} while(0)
struct cuftpd_cmd_struct {
char *cmd_name;
int (*cmd_handler)(int ctrlfd, char *cmd_line);
};
struct cuftpd_user_struct {
char user[128];
char pass[128];
};
/* local functions declarations */
static int cuftpd_do_user(int,char*);
static int cuftpd_do_pass(int,char*);
static int cuftpd_do_pwd(int, char*);
static int cuftpd_do_cwd(int, char*);
static int cuftpd_do_syst(int,char*);
static int cuftpd_do_list(int,char*);
static int cuftpd_do_size(int,char*);
static int cuftpd_do_dele(int,char*);
static int cuftpd_do_type(int,char*);
static int cuftpd_do_port(int,char*);
static int cuftpd_do_pasv(int,char*);
static int cuftpd_do_retr(int,char*);
static int cuftpd_do_stor(int,char*);
static int cuftpd_do_quit(int,char*);
/* global variables */
int cuftpd_debug_on;
int cuftpd_srv_port = CUFTPD_DEF_SRV_PORT;
int cuftpd_cur_child_num;
int cuftpd_quit_flag;
struct cuftpd_user_struct *cuftpd_cur_user;
int cuftpd_pasv_fd = -1;
int cuftpd_pasv_connfd = -1;
int cuftpd_port_connfd = -1;
char cuftpd_home_dir[PATH_MAX];
/*
* Currently supported ftp commands.
*/
struct cuftpd_cmd_struct cuftpd_cmds[] = {
{"USER", cuftpd_do_user},
{"PASS", },
{"PWD", cuftpd_do_pwd},
{"XPWD", cuftpd_do_pwd},
{"CWD", cuftpd_do_cwd},
{"LIST", cuftpd_do_list},
{"NLST", cuftpd_do_list},
{"SYST", cuftpd_do_syst},
{"TYPE", cuftpd_do_type},
{"SIZE", cuftpd_do_size},
{"DELE", cuftpd_do_dele},
{"RMD", cuftpd_do_dele},
{"PORT", cuftpd_do_port},
{"PASV", cuftpd_do_pasv},
{"RETR", cuftpd_do_retr},
{"STOR", cuftpd_do_stor},
{"QUIT", cuftpd_do_quit},
{ NULL, NULL}
};
/*
* Anonymous users, you can add more user/pass pairs here.
*/
struct cuftpd_user_struct cuftpd_users[] = {
{"anonymous", ""},
{"ftp", ""}
};
/*
* Ftp server's responses, and are not complete.
*/
char cuftpd_srv_resps[][256] = {
"150 Begin transfer" CUFTPD_LINE_END,
"200 OK" CUFTPD_LINE_END,
"213 %d" CUFTPD_LINE_END,
"215 UNIX Type: L8" CUFTPD_LINE_END,
"220 CuFTPD " CUFTPD_VER " Server" CUFTPD_LINE_END,
"221 Goodbye" CUFTPD_LINE_END,
"226 Transfer complete" CUFTPD_LINE_END,
"227 Entering Passive Mode (%d,%d,%d,%d,%d,%d)" CUFTPD_LINE_END,
"230 User %s logged in" CUFTPD_LINE_END,
"250 CWD command successful" CUFTPD_LINE_END,
"257 \"%s\" is current directory" CUFTPD_LINE_END,
"331 Password required for %s" CUFTPD_LINE_END,
"500 Unsupport command %s" CUFTPD_LINE_END,
"530 Login %s" CUFTPD_LINE_END,
"550 Error" CUFTPD_LINE_END
};
static void
cuftpd_debug(const char *file, int line, const char *fmt, ...)
{
va_list ap;
if (!cuftpd_debug_on)
return;
fprintf(stderr, "(%s:%d:%d)", file, line, getpid());
va_start(ap, fmt);
vfprintf(stderr, fmt, ap);
va_end(ap);
}
static void
cuftpd_usage(void)
{
printf("cuftpd " CUFTPD_VER " usage:\n");
printf("cuftpd -p port -d\n");
}
static void
cuftpd_parse_args(int argc, char *argv[])
{
int opt;
int err = 0;
while ((opt = getopt(argc, argv, "p:dh")) != -1) {
switch (opt) {
case 'p':
cuftpd_srv_port = atoi(optarg);
err = (cuftpd_srv_port < 0 || cuftpd_srv_port > 65535);
break;
case 'd':
cuftpd_debug_on = 1;
break;
default:
err = 1;
break;
}
if (err) {
cuftpd_usage();
exit(-1);
}
}
CUFTPD_DEBUG("srv port:%d\n", cuftpd_srv_port);
}
static void
cuftpd_sigchild(int signo)
{
int status;
if (signo != SIGCHLD)
return;
while (waitpid(-1, &status, WNOHANG) > 0)
cuftpd_cur_child_num--;
CUFTPD_DEBUG("caught a SIGCHLD, cuftpd_cur_child_num=%d\n", cuftpd_cur_child_num);
}
static int
cuftpd_init(void)
{
/* add all init statements here */
signal(SIGPIPE, SIG_IGN);
signal(SIGCHLD, cuftpd_sigchild);
getcwd(cuftpd_home_dir, sizeof(cuftpd_home_dir));
return CUFTPD_OK;
}
/*
* Create ftp server's listening socket.
*/
static int
cuftpd_create_srv(void)
{
int fd;
int on = 1;
int err;
struct sockaddr_in srvaddr;
if ((fd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
CUFTPD_DEBUG("socket() failed: %s\n", strerror(errno));
return fd;
}
err = setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
if (err < 0) {
CUFTPD_DEBUG("setsockopt() failed: %s\n", strerror(errno));
close(fd);
return CUFTPD_ERR;
}
memset(&srvaddr, 0, sizeof(srvaddr));
srvaddr.sin_family = AF_INET;
srvaddr.sin_port = htons(cuftpd_srv_port);
srvaddr.sin_addr.s_addr = htonl(INADDR_ANY);
err = bind(fd, (struct sockaddr*)&srvaddr, sizeof(srvaddr));
if (err < 0) {
CUFTPD_DEBUG("bind() failed: %s\n", strerror(errno));
close(fd);
return CUFTPD_ERR;
}
err = listen(fd, CUFTPD_LISTEN_QU_LEN);
if (err < 0) {
CUFTPD_DEBUG("listen() failed: %s\n", strerror(errno));
close(fd);
return CUFTPD_ERR;
}
if (cuftpd_debug_on) {
int len = sizeof(srvaddr);
getsockname(fd, (struct sockaddr*)&srvaddr, &len);
CUFTPD_DEBUG("create srv listen socket OK: %s:%d\n",
inet_ntoa(srvaddr.sin_addr), ntohs(srvaddr.sin_port));
}
return fd;
}
/*
* Map server response's number to the msg pointer.
*/
static char *
cuftpd_srv_resp_num2msg(int num)
{
int i;
char buf[8];
snprintf(buf, sizeof(buf), "%d", num);
if (strlen(buf) != 3)
return NULL;
for (i = 0; i < CUFTPD_ARR_LEN(cuftpd_srv_resps); i++)
if (strncmp(buf, cuftpd_srv_resps[i], strlen(buf)) == 0)
return cuftpd_srv_resps[i];
return NULL;
}
static int
cuftpd_send_msg(int fd, char *msg, int len)
{
int n, off = 0, left = len;
while (1) {
n = write(fd, msg + off, left);
if (n < 0) {
if (errno == EINTR)
continue;
return n;
}
if (n < left) {
left -= n;
off += n;
continue;
}
return len;
}
}
static int
cuftpd_recv_msg(int fd, char buf[], int len)
{
int n;
while (1) {
n = read(fd, buf, len);
if (n < 0) {
if (errno == EINTR)
continue;
return n;
}
return n;
}
}
static int
cuftpd_send_resp(i
- 1
- 2
- 3
前往页