📄 ftpserv.c
字号:
/* Internet FTP Server
*/
#include <stdio.h>
#include <ctype.h>
#include <time.h>
#ifdef __TURBOC__
#include <io.h>
#include <dir.h>
#endif
#include "global.h"
#include "mbuf.h"
#include "proc.h"
#include "socket.h"
#include "dirutil.h"
#include "commands.h"
#include "files.h"
#include "ftp.h"
#include "ftpserv.h"
#include "md5.h"
static void ftpserv(int s,void *unused,void *p);
static int pport(struct sockaddr_in *sock,char *arg);
static void ftplogin(struct ftpserv *ftp,char *pass);
static int sendit(struct ftpserv *ftp,char *command,char *file);
static int recvit(struct ftpserv *ftp,char *command,char *file);
/* Command table */
static char *commands[] = {
"user",
"acct",
"pass",
"type",
"list",
"cwd",
"dele",
"name",
"quit",
"retr",
"stor",
"port",
"nlst",
"pwd",
"xpwd", /* For compatibility with 4.2BSD */
"mkd ",
"xmkd", /* For compatibility with 4.2BSD */
"xrmd", /* For compatibility with 4.2BSD */
"rmd ",
"stru",
"mode",
"syst",
"xmd5",
"xcwd",
NULL
};
/* Response messages */
static char banner[] = "220 %s FTP version %s ready at %s\n";
static char badcmd[] = "500 Unknown command '%s'\n";
static char binwarn[] = "100 Warning: type is ASCII and %s appears to be binary\n";
static char unsupp[] = "500 Unsupported command or option\n";
static char givepass[] = "331 Enter PASS command\n";
static char logged[] = "230 Logged in\n";
static char typeok[] = "200 Type %s OK\n";
static char only8[] = "501 Only logical bytesize 8 supported\n";
static char deleok[] = "250 File deleted\n";
static char mkdok[] = "200 MKD ok\n";
static char delefail[] = "550 Delete failed: %s\n";
static char pwdmsg[] = "257 \"%s\" is current directory\n";
static char badtype[] = "501 Unknown type \"%s\"\n";
static char badport[] = "501 Bad port syntax\n";
static char unimp[] = "502 Command not yet implemented\n";
static char bye[] = "221 Goodbye!\n";
static char nodir[] = "553 Can't read directory \"%s\": %s\n";
static char cantopen[] = "550 Can't read file \"%s\": %s\n";
static char sending[] = "150 Opening data connection for %s %s\n";
static char cantmake[] = "553 Can't create \"%s\": %s\n";
static char writerr[] = "552 Write error: %s\n";
static char portok[] = "200 Port command okay\n";
static char rxok[] = "226 File received OK\n";
static char txok[] = "226 File sent OK\n";
static char noperm[] = "550 Permission denied\n";
static char noconn[] = "425 Data connection reset\n";
static char lowmem[] = "421 System overloaded, try again later\n";
static char notlog[] = "530 Please log in with USER and PASS\n";
static char userfirst[] = "503 Login with USER first.\n";
static char okay[] = "200 Ok\n";
static char syst[] = "215 %s Type: L%d Version: %s\n";
/* Start up FTP service */
int
ftpstart(argc,argv,p)
int argc;
char *argv[];
void *p;
{
uint16 port;
if(argc < 2)
port = IPPORT_FTP;
else
port = atoi(argv[1]);
return start_tcp(port,"FTP Server",ftpserv,2048);
}
static void
ftpserv(s,n,p)
int s; /* Socket with user connection */
void *n;
void *p;
{
struct ftpserv ftp;
char **cmdp,buf[512],*arg,*cp,*cp1,*file,*mode;
long t;
int i;
struct sockaddr_in socket;
memset(&ftp,0,sizeof(ftp)); /* Start with clear slate */
ftp.control = fdopen(s,"r+t");
sockowner(fileno(ftp.control),Curproc); /* We own it now */
setvbuf(ftp.control,NULL,_IOLBF,BUFSIZ);
if(availmem() != 0){
fprintf(ftp.control,lowmem);
fclose(ftp.control);
return;
}
fclose(stdin);
stdin = fdup(ftp.control);
fclose(stdout);
stdout = fdup(ftp.control);
/* Set default data port */
i = SOCKSIZE;
getpeername(fileno(ftp.control),(struct sockaddr *)&socket,&i);
socket.sin_port = IPPORT_FTPD;
ASSIGN(ftp.port,socket);
logmsg(fileno(ftp.control),"open FTP");
time(&t);
cp = ctime(&t);
if((cp1 = strchr(cp,'\n')) != NULL)
*cp1 = '\0';
fprintf(ftp.control,banner,Hostname,Version,cp);
loop: fflush(ftp.control);
if((fgets(buf,sizeof(buf),ftp.control)) == NULL){
/* He closed on us */
goto finish;
}
if(strlen(buf) == 0){
/* Can't be a legal FTP command */
fprintf(ftp.control,badcmd,buf);
goto loop;
}
rip(buf);
/* Translate first word to lower case */
for(cp = buf;*cp != ' ' && *cp != '\0';cp++)
*cp = tolower(*cp);
/* Find command in table; if not present, return syntax error */
for(cmdp = commands;*cmdp != NULL;cmdp++)
if(strncmp(*cmdp,buf,strlen(*cmdp)) == 0)
break;
if(*cmdp == NULL){
fprintf(ftp.control,badcmd,buf);
goto loop;
}
/* Allow only USER, PASS and QUIT before logging in */
if(ftp.cd == NULL || ftp.path == NULL){
switch(cmdp-commands){
case USER_CMD:
case PASS_CMD:
case QUIT_CMD:
break;
default:
fprintf(ftp.control,notlog);
goto loop;
}
}
arg = &buf[strlen(*cmdp)];
while(*arg == ' ')
arg++;
/* Execute specific command */
switch(cmdp-commands){
case USER_CMD:
free(ftp.username);
ftp.username = strdup(arg);
fprintf(ftp.control,givepass);
break;
case TYPE_CMD:
switch(arg[0]){
case 'A':
case 'a': /* Ascii */
ftp.type = ASCII_TYPE;
fprintf(ftp.control,typeok,arg);
break;
case 'l':
case 'L':
while(*arg != ' ' && *arg != '\0')
arg++;
if(*arg == '\0' || *++arg != '8'){
fprintf(ftp.control,only8);
break;
}
ftp.type = LOGICAL_TYPE;
ftp.logbsize = 8;
fprintf(ftp.control,typeok,arg);
break;
case 'B':
case 'b': /* Binary */
case 'I':
case 'i': /* Image */
ftp.type = IMAGE_TYPE;
fprintf(ftp.control,typeok,arg);
break;
default: /* Invalid */
fprintf(ftp.control,badtype,arg);
break;
}
break;
case QUIT_CMD:
fprintf(ftp.control,bye);
goto finish;
case RETR_CMD:
file = pathname(ftp.cd,arg);
switch(ftp.type){
case IMAGE_TYPE:
case LOGICAL_TYPE:
mode = READ_BINARY;
break;
case ASCII_TYPE:
mode = READ_TEXT;
break;
}
if(!permcheck(ftp.path,ftp.perms,RETR_CMD,file)){
fprintf(ftp.control,noperm);
} else if((ftp.fp = fopen(file,mode)) == NULL){
fprintf(ftp.control,cantopen,file,sys_errlist[errno]);
} else {
logmsg(fileno(ftp.control),"RETR %s",file);
if(ftp.type == ASCII_TYPE && isbinary(ftp.fp)){
fprintf(ftp.control,binwarn,file);
}
sendit(&ftp,"RETR",file);
}
FREE(file);
break;
case STOR_CMD:
file = pathname(ftp.cd,arg);
switch(ftp.type){
case IMAGE_TYPE:
case LOGICAL_TYPE:
mode = WRITE_BINARY;
break;
case ASCII_TYPE:
mode = WRITE_TEXT;
break;
}
if(!permcheck(ftp.path,ftp.perms,STOR_CMD,file)){
fprintf(ftp.control,noperm);
} else if((ftp.fp = fopen(file,mode)) == NULL){
fprintf(ftp.control,cantmake,file,sys_errlist[errno]);
} else {
logmsg(fileno(ftp.control),"STOR %s",file);
recvit(&ftp,"STOR",file);
}
FREE(file);
break;
case PORT_CMD:
if(pport(&ftp.port,arg) == -1){
fprintf(ftp.control,badport);
} else {
fprintf(ftp.control,portok);
}
break;
#ifndef CPM
case LIST_CMD:
file = pathname(ftp.cd,arg);
if(!permcheck(ftp.path,ftp.perms,RETR_CMD,file)){
fprintf(ftp.control,noperm);
} else if((ftp.fp = dir(file,1)) == NULL){
fprintf(ftp.control,nodir,file,sys_errlist[errno]);
} else {
sendit(&ftp,"LIST",file);
}
FREE(file);
break;
case NLST_CMD:
file = pathname(ftp.cd,arg);
if(!permcheck(ftp.path,ftp.perms,RETR_CMD,file)){
fprintf(ftp.control,noperm);
} else if((ftp.fp = dir(file,0)) == NULL){
fprintf(ftp.control,nodir,file,sys_errlist[errno]);
} else {
sendit(&ftp,"NLST",file);
}
FREE(file);
break;
case XCWD_CMD:
case CWD_CMD:
file = pathname(ftp.cd,arg);
if(!permcheck(ftp.path,ftp.perms,RETR_CMD,file)){
fprintf(ftp.control,noperm);
FREE(file);
#ifdef MSDOS
/* Don'tcha just LOVE %%$#@!! MS-DOS? */
} else if(strcmp(file,"/") == 0 || access(file,0) == 0){
#else
} else if(access(file,0) == 0){ /* See if it exists */
#endif
/* Succeeded, record in control block */
free(ftp.cd);
ftp.cd = file;
fprintf(ftp.control,pwdmsg,file);
} else {
/* Failed, don't change anything */
fprintf(ftp.control,nodir,file,sys_errlist[errno]);
FREE(file);
}
break;
case XPWD_CMD:
case PWD_CMD:
fprintf(ftp.control,pwdmsg,ftp.cd);
break;
#else
case LIST_CMD:
case NLST_CMD:
case CWD_CMD:
case XCWD_CMD:
case XPWD_CMD:
case PWD_CMD:
#endif
case ACCT_CMD:
fprintf(ftp.control,unimp);
break;
case DELE_CMD:
file = pathname(ftp.cd,arg);
if(!permcheck(ftp.path,ftp.perms,DELE_CMD,file)){
fprintf(ftp.control,noperm);
} else if(unlink(file) == 0){
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -