In the previous article, we discussed RFC 959, which defines how an FTP server should work. We also walked through some of the commands that are used in this protocol. In this article, we will be creating an example FTP server in Delphi.
The Implementation of an FTP Server - Authenticating Users (Page 6 of 6 )
This event authenticates a user that is trying to use the FTP server. It is advisable to use a database to authenticate a particular user. In the code below, I used "myusername" and "mypassword" as the username and password:
procedure TForm1.IdFTPServer1UserLogin(ASender:TIdFTPServerContext;
const AUsername, APassword: String; var AAuthenticated:Boolean);
begin
if (AUsername = edit1.Text) and (APassword = edit2.Text) then
begin
AAuthenticated:=True
end
else
begin
AAuthenticated := False;
end;
end;
procedure TForm1.IdFTPServer1StoreFile(ASender:TIdFTPServerContext;
const AFileName: String; AAppend: Boolean; var VStream:TStream);
begin
if not Aappend then
VStream := TFileStream.Create(AFileName,fmCreate)
else
VStream := TFileStream.Create(AFileName,fmOpenWrite)
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
idftpserver1.DefaultPort:=strtoint(edit3.text);
idftpserver1.Active:=true;
showmessage('Connected');
end;
procedure TForm1.IdFTPServer1AfterUserLogin(ASender:TIdFTPServerContext);
begin
//set home dir here
homedir:='c:';
end;
This event is optional. It enables you to show a message when a exception occurs:
procedure TForm1.IdFTPServer1Exception(AContext: TIdContext;
AException: Exception);
begin
showmessage(AException.Message);
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
idftpserver1.Active:=false;
close;
end;
The server status event simply displays the status of the server when there is a status change:
procedure TForm1.IdFTPServer1Stat(ASender:TIdFTPServerContext;
AStatusInfo: TStrings);
var
i:integer;
begin
for i:= 0 to astatusinfo.Count-1 do
begin
memo1.Lines.Add(astatusinfo.Strings[i]);
end;
end;
This event gets fired when the server writes data to a client. I've included it mainly for debugging purposes. It will record all communications between the server and a client:
procedure TForm1.IdFTPServer1Execute(AContext: TIdContext);
begin
logfile1.DoLogWriteString(AContext.Connection.IOHandler.AllData);
end;
That is all there is to implementing a working FTP server. As you will no doubt notice, I have not implemented all of the commands, but the ones I implemented cover all that is required for an FTP server to function well.
Conclusion
The best way to test this application is with a graphical FTP client such as Dreamweaver instead of telnet. For some reason the server application refuses to execute the LIST command when telnet is used. And as I've stated many times before, you cannot do anything with an FTP server that cannot show you the files in a given directory. I've notified the Indy developers about this problem, and hopefully they will work on it.
DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.