📄 cuftpd.c.c
字号:
/*
* A very simple ftp server's source code for demonstration.
* It supports PASV/PORT modes and following operations:
* ls,pwd,cwd,get,put,dele.
* I have tested it using following ftp clients:
* 1. Windows XP's command line ftp client,
* 2. IE 6.0,
* 3. Redhat 9.0's ftp client,
* 4. CuteFTP 8,
* I'll introduce more functions and improve its performance
* if i have enough idle time afterwards.
* Any issues pls paste to CU.
*
* Change History:
* 11/24/2006 1.0 Initial version.
*/
#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>
#include "cuftpd.h"
/* 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", cuftpd_do_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(int fd, int num, ...)
{
char *cp = cuftpd_srv_resp_num2msg(num);
va_list ap;
char buf[BUFSIZ];
if (!cp) {
CUFTPD_DEBUG("cuftpd_srv_resp_num2msg(%d) failed\n", num);
return CUFTPD_ERR;
}
va_start(ap, num);
vsnprintf(buf, sizeof(buf), cp, ap);
va_end(ap);
CUFTPD_DEBUG("send resp:%s\n", buf);
if (cuftpd_send_msg(fd, buf, strlen(buf)) != strlen(buf)) {
CUFTPD_DEBUG("cuftpd_send_msg() failed: %s\n", strerror(errno));
return CUFTPD_ERR;
}
return CUFTPD_OK;
}
static void cuftpd_trim_lineend(char *cp)
{
if (cp && strlen(cp) > 0) {
char *cp2 = &cp[strlen(cp) - 1];
while (*cp2 == '\r' || *cp2 == '\n')
if (--cp2 < cp)
break;
cp2[1] = '\0';
}
}
static int cuftpd_get_connfd(void)
{
int fd;
if (cuftpd_pasv_fd >= 0) {
fd = accept(cuftpd_pasv_fd, NULL, NULL);
if (fd >= 0) {
close(cuftpd_pasv_fd);
cuftpd_pasv_fd = -1;
cuftpd_pasv_connfd = fd;
return fd;
}
else
CUFTPD_DEBUG("accept() failed:%s\n", strerror(errno));
}
else if (cuftpd_port_connfd >= 0)
return cuftpd_port_connfd;
return (-1);
}
static int cuftpd_close_all_fd(void)
{
if (cuftpd_pasv_fd >= 0) {
close(cuftpd_pasv_fd);
cuftpd_pasv_fd = -1;
}
if (cuftpd_pasv_connfd >= 0) {
close(cuftpd_pasv_connfd);
cuftpd_pasv_connfd = -1;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -