📄 commond_action.c
字号:
#include <stdio.h>
#include "commond_action.h"
COMMOND commond_list[]=
{
{"USER",user_validate},
{"PASS",passwd_validate},
{"SYST",syst},
{"PWD",send_pwd},
{"XPWD",send_pwd},
{"TYPE",send_type},
{"PORT",port_connection},
{"LIST",my_ls},
{"CWD",change_dir},
{"XCWD",change_dir},
{"CDUP",cdup},
{"NOOP",noop},
{"QUIT",quit},
{"HELP",help},
{"PASV",pasv_connection},
{"STOR",upload},
{"RETR",download},
{"REST",reset_file_offset},
{"DELE",del_file},
{"RNFR",ready_rename_file},
{"RNTO",rename_file},
{"MKD",make_dir},
{"XMKD",make_dir},
{"XRMD",remove_dir},
{"RMD",remove_dir},
{"SIZE",send_file_size},
{"APPE",append_data},
{NULL,NULL}
};
CLIENT client_infor={0,0,0,0,0,PORT,0,{0},ASCII,0,{0},NO,NO};
char user[50]={0};
int user_validate(char *username)
{
struct passwd *ptr=NULL;
memset(user,0,50);
memcpy(user,username,strlen(username));
ptr=getpwnam(username);
memset(commond_buffer,0,100);
memcpy(commond_buffer,"331 User name okay, need password.\r\n",strlen("331 User name okay, need password.\r\n"));
if (strcmp(username,"anonymous") == 0)
{
client_infor.is_anonymous=YES;
memset(user,0,50);
strcpy(user,"ftp");
}
if (ptr == NULL)
{
return FAIL;
}
else
{
return SCUESS;
}
}
int passwd_validate(char *passwd)
{
memset(commond_buffer,0,100);
int i=0;
struct passwd *ptr=NULL;
struct spwd *pt=NULL;
char buffer[13];
memset(buffer,0,13);
ptr=getpwnam(user);
if (ptr == NULL)
{
memcpy(commond_buffer,"530 Login incorrect.\r\n",strlen("530 Login incorrect.\r\n"));
return FAIL;
}
pt=getspnam(user);
if (client_infor.is_anonymous == YES)
{
client_infor.is_anonymous = NO;
if (strlen(passwd) > 0)
{
memcpy(commond_buffer,"230 Login successful. Have fun.\r\n",strlen("230 Login successful. Have fun.\r\n"));
if (setegid(ptr->pw_gid) == 0)
{
if (seteuid(ptr->pw_uid) == 0)
{
chdir(ptr->pw_dir);
memset(pwd,0,500);
memcpy(pwd,ptr->pw_dir,strlen(ptr->pw_dir));
return SCUESS;
}
else
{
return FAIL;
}
}
else
{
return FAIL;
}
}
else
{
memcpy(commond_buffer,"530 Login incorrect.\r\n",strlen("530 Login incorrect.\r\n"));
return FAIL;
}
}
for (i=0;i<12 ;i++ )
{
buffer[i]=*(pt->sp_pwdp+i);
}
if (strcmp(crypt(passwd,buffer),pt->sp_pwdp) == 0)
{
memcpy(commond_buffer,"230 Login successful. Have fun.\r\n",strlen("230 Login successful. Have fun.\r\n"));
if (setegid(ptr->pw_gid) == 0)
{
if (seteuid(ptr->pw_uid) == 0)
{
chdir(ptr->pw_dir);
memset(pwd,0,500);
memcpy(pwd,ptr->pw_dir,strlen(ptr->pw_dir));
}
else
{
return FAIL;
}
}
else
{
return FAIL;
}
}
else
{
memcpy(commond_buffer,"530 Login incorrect.\r\n",strlen("530 Login incorrect.\r\n"));
}
return SCUESS;
}
int syst(char *valus)
{
memset(commond_buffer,0,100);
memcpy(commond_buffer,"215 UNIX Type: L8\r\n",strlen("215 UNIX Type: L8\r\n"));
return SCUESS;
}
int send_pwd(char *valus)
{
memset(pwd,0,500);
getcwd(pwd,500);
char *place=strstr(pwd,"/var/ftp");
if (place != NULL)
{
strcpy(valus,place+strlen("/var/ftp"));
memset(pwd,0,50);
strcpy(pwd,valus);
}
memset(commond_buffer,0,100);
sprintf(commond_buffer,"257 \"%s\"\r\n",pwd);
return SCUESS;
}
int send_type(char *valus)
{
memset(commond_buffer,0,100);
switch (*valus)
{
case 'A':
memcpy(commond_buffer,"200 Switching to ASCII mode.\r\n",strlen("200 Switching to ASCII mode.\r\n"));
client_infor.trans_mode=ASCII;
break;
case 'I':
memcpy(commond_buffer,"200 Switching to Binary mode.\r\n",strlen("200 Switching to Binary mode.\r\n"));
client_infor.trans_mode=BINARY;
break;
}
return SCUESS;
}
int port_connection(char *valus)
{
char port_f[10]={0},port_l[10]={0};
char *ptr=NULL;
char *ptr1=NULL;
int i=0,p_f=0,p_l=0;
memset(client_infor.ip_addr,0,20);
ptr=find_first_of(valus,',');
for (i=0;i<3 ;i++ )
{
ptr=find_first_of(ptr+1,',');
}
copy_str(valus,client_infor.ip_addr,ptr-valus,0);
replace_of(client_infor.ip_addr,',','.');
ptr1=find_first_of(ptr+1,',');
copy_str(ptr+1,port_f,ptr1-ptr-1,0);
strcpy(port_l,ptr1+1);
p_f=atoi(port_f);
p_l=atoi(port_l);
client_infor.port_num=p_f*256+p_l;
client_infor.connect_mode=PORT;
memset(commond_buffer,0,100);
memcpy(commond_buffer,"200 PORT command successful. Consider using PASV.\r\n",strlen("200 PORT command successful. Consider using PASV.\r\n"));
return SCUESS;
}
int my_ls(char *valus)
{
struct stat c_stat;
struct dirent *tmp_file;
struct tm *create_tm;
char buffer[500];
FILE_IN file;
int len=sizeof(struct sockaddr_in);
struct sockaddr_in port_addr;
//创建新的套接字
client_infor.socket_trans_list=socket(AF_INET,SOCK_STREAM,0);
port_addr.sin_family=AF_INET;
port_addr.sin_port=htons(client_infor.port_num);
port_addr.sin_addr.s_addr=inet_addr(client_infor.ip_addr);
if(connect(client_infor.socket_trans_list,(struct sockaddr *)&port_addr,len)<0)
{
perror("connect error");
return FAIL;
}
memset(commond_buffer,0,100);
memcpy(commond_buffer,"150 Here comes the directory listing.\r\n",strlen("150 Here comes the directory listing.\r\n"));
write(client_infor.socket_control,commond_buffer,strlen(commond_buffer));
DIR *dirtmp_file = opendir(".");
if(dirtmp_file == NULL)
{
perror("OPEN DIR FAILD");
return FAIL;
}
while(1)
{
memset(&c_stat,0,sizeof(stat));
memset(&file,0,sizeof(FILE_IN));
memset(buffer,0,500);
tmp_file = readdir(dirtmp_file);
if(tmp_file == NULL)
{
break;
}
stat(tmp_file->d_name,&c_stat);
//提取文件类型
switch(c_stat.st_mode & S_IFMT)
{
case S_IFSOCK:
file.file_type='s';
break;
case S_IFLNK:
file.file_type='l';
break;
case S_IFREG:
file.file_type='-';
break;
case S_IFBLK:
file.file_type='b';
break;
case S_IFDIR:
file.file_type='d';
break;
case S_IFCHR:
file.file_type='c';
break;
case S_IFIFO:
file.file_type='p';
break;
}
// 提取文件所有者权限
if(c_stat.st_mode & S_IRUSR)
file.owner_is_read='r';
else
file.owner_is_read='-';
if(c_stat.st_mode & S_IWUSR)
file.owner_is_write='w';
else
file.owner_is_write='-';
if(c_stat.st_mode & S_IXUSR)
file.owner_is_execute='x';
else
file.owner_is_execute='-';
//提取用户组权限
if(c_stat.st_mode & S_IRGRP)
file.owner_group_is_read='r';
else
file.owner_group_is_read='-';
if(c_stat.st_mode & S_IWGRP)
file.owner_group_is_write='w';
else
file.owner_group_is_write='-';
if(c_stat.st_mode & S_IXGRP)
file.owner_group_is_execute='x';
else
file.owner_group_is_execute='-';
//提取其他用户权限
if(c_stat.st_mode & S_IROTH)
file.other_user_is_read='r';
else
file.other_user_is_read='-';
if(c_stat.st_mode & S_IWOTH)
file.other_user_is_write='w';
else
file.other_user_is_write='-';
if(c_stat.st_mode & S_IXOTH)
file.other_user_is_execute='x';
else
file.other_user_is_execute='-';
//提取文件的连接数目
file.hard_link_counts=c_stat.st_nlink;
//提取用户ID
file.user_identify_code=c_stat.st_uid;
//提取组ID
file.group_identify_code=c_stat.st_gid;
//提取文件大小
file.file_bulk=c_stat.st_size;
//提取创建时间
memset(file.create_data,0,100);
create_tm = localtime(&c_stat.st_mtime);
strftime(file.create_data,100,"%b %d %H:%M",create_tm);
//提取文件名
strcpy(file.file_name,tmp_file->d_name);
sprintf(buffer,"%c%c%c%c%c%c%c%c%c%c%5d%7d%7d%15d %s %-s\r\n",
file.file_type,
file.owner_is_read,
file.owner_is_write,
file.owner_is_execute,
file.owner_group_is_read,
file.owner_group_is_write,
file.owner_group_is_execute,
file.other_user_is_read,
file.other_user_is_write,
file.other_user_is_execute,
file.hard_link_counts,
file.user_identify_code,
file.group_identify_code,
file.file_bulk,
file.create_data,
file.file_name);
write(client_infor.socket_trans_list,buffer,strlen(buffer));
}
memset(commond_buffer,0,100);
memcpy(commond_buffer,"226 Directory send OK.\r\n",strlen("226 Directory send OK.\r\n"));
close(client_infor.socket_trans_list);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -