Working FTP

This commit is contained in:
Erik Zandvliet 2012-08-29 11:20:31 +02:00
parent f351ca46d8
commit dc8e96634b
2 changed files with 22 additions and 13 deletions

View file

@ -1,6 +1,6 @@
#include "ftp.h" #include "ftp.h"
FTP::User::User( Socket::Connection NewConnection ) { FTP::User::User( Socket::Connection NewConnection, std::map<std::string,std::string> Credentials ) {
Conn = NewConnection; Conn = NewConnection;
USER = ""; USER = "";
PASS = ""; PASS = "";
@ -9,6 +9,7 @@ FTP::User::User( Socket::Connection NewConnection ) {
TYPE = TYPE_ASCII_NONPRINT; TYPE = TYPE_ASCII_NONPRINT;
PORT = 20; PORT = 20;
RNFR = ""; RNFR = "";
AllCredentials = Credentials;
MyDir = Filesystem::Directory( "", FTPBasePath ); MyDir = Filesystem::Directory( "", FTPBasePath );
MyDir.SetPermissions( "", Filesystem::P_LIST ); MyDir.SetPermissions( "", Filesystem::P_LIST );
@ -69,14 +70,14 @@ int FTP::User::ParseCommand( std::string Command ) {
USER = ""; USER = "";
PASS = ""; PASS = "";
if( Command == "" ) { return 501; }//Syntax error in parameters or arguments. if( Command == "" ) { return 501; }//Syntax error in parameters or arguments.
USER = ThisCmd; USER = Command;
return 331;//User name okay, need password. return 331;//User name okay, need password.
break; break;
} }
case CMD_PASS: { case CMD_PASS: {
if( USER == "" ) { return 503; }//Bad sequence of commands if( USER == "" ) { return 503; }//Bad sequence of commands
if( Command == "" ) { return 501; }//Syntax error in parameters or arguments. if( Command == "" ) { return 501; }//Syntax error in parameters or arguments.
PASS = ThisCmd; PASS = Command;
if( !LoggedIn( ) ) { if( !LoggedIn( ) ) {
USER = ""; USER = "";
PASS =""; PASS ="";
@ -89,15 +90,15 @@ int FTP::User::ParseCommand( std::string Command ) {
std::cout << "Listening on :" << MyPassivePort << "\n"; std::cout << "Listening on :" << MyPassivePort << "\n";
Socket::Connection Connected = Passive.accept(); Socket::Connection Connected = Passive.accept();
if( Connected.connected() ) { if( Connected.connected() ) {
Conn.write( "125 Data connection already open; transfer starting.\n" ); Conn.Send( "125 Data connection already open; transfer starting.\n" );
} else { } else {
Conn.write( "150 File status okay; about to open data connection.\n" ); Conn.Send( "150 File status okay; about to open data connection.\n" );
} }
while( !Connected.connected() ) { while( !Connected.connected() ) {
Connected = Passive.accept(); Connected = Passive.accept();
} }
fprintf( stderr, "Sending LIST information\n" ); fprintf( stderr, "Sending LIST information\n" );
Connected.write( MyDir.LIST( ActiveStreams ) ); Connected.Send( MyDir.LIST( ActiveStreams ) );
Connected.close( ); Connected.close( );
return 226; return 226;
break; break;
@ -136,15 +137,15 @@ int FTP::User::ParseCommand( std::string Command ) {
std::cout << "Listening on :" << MyPassivePort << "\n"; std::cout << "Listening on :" << MyPassivePort << "\n";
Socket::Connection Connected = Passive.accept(); Socket::Connection Connected = Passive.accept();
if( Connected.connected() ) { if( Connected.connected() ) {
Conn.write( "125 Data connection already open; transfer starting.\n" ); Conn.Send( "125 Data connection already open; transfer starting.\n" );
} else { } else {
Conn.write( "150 File status okay; about to open data connection.\n" ); Conn.Send( "150 File status okay; about to open data connection.\n" );
} }
while( !Connected.connected() ) { while( !Connected.connected() ) {
Connected = Passive.accept(); Connected = Passive.accept();
} }
fprintf( stderr, "Sending RETR information\n" ); fprintf( stderr, "Sending RETR information\n" );
Connected.write( MyDir.RETR( Command ) ); Connected.Send( MyDir.RETR( Command ) );
Connected.close(); Connected.close();
return 226; return 226;
break; break;
@ -156,9 +157,9 @@ int FTP::User::ParseCommand( std::string Command ) {
std::cout << "Listening on :" << MyPassivePort << "\n"; std::cout << "Listening on :" << MyPassivePort << "\n";
Socket::Connection Connected = Passive.accept(); Socket::Connection Connected = Passive.accept();
if( Connected.connected() ) { if( Connected.connected() ) {
Conn.write( "125 Data connection already open; transfer starting.\n" ); Conn.Send( "125 Data connection already open; transfer starting.\n" );
} else { } else {
Conn.write( "150 File status okay; about to open data connection.\n" ); Conn.Send( "150 File status okay; about to open data connection.\n" );
} }
while( !Connected.connected() ) { while( !Connected.connected() ) {
Connected = Passive.accept(); Connected = Passive.accept();
@ -306,7 +307,13 @@ int FTP::User::ParseCommand( std::string Command ) {
bool FTP::User::LoggedIn( ) { bool FTP::User::LoggedIn( ) {
if( USER == "" || PASS == "" ) { return false; } if( USER == "" || PASS == "" ) { return false; }
if( !AllCredentials.size() ) {
return true; return true;
}
if( ( AllCredentials.find( USER ) != AllCredentials.end() ) && AllCredentials[USER] == PASS ) {
return true;
}
return false;
} }
std::string FTP::User::NumToMsg( int MsgNum ) { std::string FTP::User::NumToMsg( int MsgNum ) {

View file

@ -1,3 +1,4 @@
#include <map>
#include <string> #include <string>
#include <cstdlib> #include <cstdlib>
#include <cstdio> #include <cstdio>
@ -54,13 +55,14 @@ namespace FTP {
class User { class User {
public: public:
User( Socket::Connection NewConnection = Socket::Connection() ); User( Socket::Connection NewConnection = Socket::Connection(), std::map<std::string,std::string> Credentials = std::map<std::string,std::string>() );
~User( ); ~User( );
int ParseCommand( std::string Command ); int ParseCommand( std::string Command );
bool LoggedIn( ); bool LoggedIn( );
std::string NumToMsg( int MsgNum ); std::string NumToMsg( int MsgNum );
Socket::Connection Conn; Socket::Connection Conn;
private: private:
std::map<std::string,std::string> AllCredentials;
std::string USER; std::string USER;
std::string PASS; std::string PASS;
Mode MODE; Mode MODE;