PWLib
1.10.10
|
00001 /* 00002 * ftp.h 00003 * 00004 * File Transfer Protocol Server/Client channel classes 00005 * As per RFC 959 and RFC 1123 00006 * 00007 * Portable Windows Library 00008 * 00009 * Copyright (c) 1993-2002 Equivalence Pty. Ltd. 00010 * 00011 * The contents of this file are subject to the Mozilla Public License 00012 * Version 1.0 (the "License"); you may not use this file except in 00013 * compliance with the License. You may obtain a copy of the License at 00014 * http://www.mozilla.org/MPL/ 00015 * 00016 * Software distributed under the License is distributed on an "AS IS" 00017 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See 00018 * the License for the specific language governing rights and limitations 00019 * under the License. 00020 * 00021 * The Original Code is Portable Windows Library. 00022 * 00023 * The Initial Developer of the Original Code is Equivalence Pty. Ltd. 00024 * 00025 * Contributor(s): ______________________________________. 00026 * 00027 * $Log: ftp.h,v $ 00028 * Revision 1.18 2005/11/30 12:47:37 csoutheren 00029 * Removed tabs, reformatted some code, and changed tags for Doxygen 00030 * 00031 * Revision 1.17 2004/11/11 07:34:50 csoutheren 00032 * Added #include <ptlib.h> 00033 * 00034 * Revision 1.16 2003/09/17 05:43:49 csoutheren 00035 * Removed recursive includes 00036 * 00037 * Revision 1.15 2002/11/06 22:47:23 robertj 00038 * Fixed header comment (copyright etc) 00039 * 00040 * Revision 1.14 2002/09/16 01:08:59 robertj 00041 * Added #define so can select if #pragma interface/implementation is used on 00042 * platform basis (eg MacOS) rather than compiler, thanks Robert Monaghan. 00043 * 00044 * Revision 1.13 2001/09/10 00:28:21 robertj 00045 * Fixed extra CR in comments. 00046 * 00047 * Revision 1.12 2000/06/21 01:01:21 robertj 00048 * AIX port, thanks Wolfgang Platzer (wolfgang.platzer@infonova.at). 00049 * 00050 * Revision 1.11 1999/03/09 08:01:46 robertj 00051 * Changed comments for doc++ support (more to come). 00052 * 00053 * Revision 1.10 1999/02/16 08:07:10 robertj 00054 * MSVC 6.0 compatibility changes. 00055 * 00056 * Revision 1.9 1998/11/30 02:50:45 robertj 00057 * New directory structure 00058 * 00059 * Revision 1.8 1998/09/23 06:19:26 robertj 00060 * Added open source copyright license. 00061 * 00062 * Revision 1.7 1996/10/26 01:39:41 robertj 00063 * Added check for security breach using 3 way FTP transfer or use of privileged PORT. 00064 * 00065 * Revision 1.6 1996/09/14 13:09:08 robertj 00066 * Major upgrade: 00067 * rearranged sockets to help support IPX. 00068 * added indirect channel class and moved all protocols to descend from it, 00069 * separating the protocol from the low level byte transport. 00070 * 00071 * Revision 1.5 1996/05/23 09:56:24 robertj 00072 * Changed FTP so can do passive/active mode on all data transfers. 00073 * 00074 * Revision 1.4 1996/03/31 08:45:57 robertj 00075 * Added QUIT command sent on FTP socket close. 00076 * 00077 * Revision 1.3 1996/03/26 00:50:28 robertj 00078 * FTP Client Implementation. 00079 * 00080 * Revision 1.2 1996/03/18 13:33:10 robertj 00081 * Fixed incompatibilities to GNU compiler where PINDEX != int. 00082 * 00083 * Revision 1.1 1996/03/04 12:14:18 robertj 00084 * Initial revision 00085 * 00086 */ 00087 00088 #ifndef _PFTPSOCKET 00089 #define _PFTPSOCKET 00090 00091 #ifdef P_USE_PRAGMA 00092 #pragma interface 00093 #endif 00094 00095 #include <ptclib/inetprot.h> 00096 #include <ptlib/sockets.h> 00097 00098 00102 class PFTP : public PInternetProtocol 00103 { 00104 PCLASSINFO(PFTP, PInternetProtocol); 00105 public: 00107 enum Commands { 00108 USER, PASS, ACCT, CWD, CDUP, SMNT, QUIT, REIN, PORT, PASV, TYPE, 00109 STRU, MODE, RETR, STOR, STOU, APPE, ALLO, REST, RNFR, RNTO, ABOR, 00110 DELE, RMD, MKD, PWD, LIST, NLST, SITE, SYST, STATcmd, HELP, NOOP, 00111 NumCommands 00112 }; 00113 00115 enum RepresentationType { 00116 ASCII, 00117 EBCDIC, 00118 Image 00119 }; 00120 00122 enum DataChannelType { 00123 NormalPort, 00124 Passive 00125 }; 00126 00128 enum NameTypes { 00129 ShortNames, 00130 DetailedNames 00131 }; 00132 00136 BOOL SendPORT( 00137 const PIPSocket::Address & addr, 00138 WORD port 00139 ); 00140 00141 00142 protected: 00144 PFTP(); 00145 }; 00146 00147 00151 class PFTPClient : public PFTP 00152 { 00153 PCLASSINFO(PFTPClient, PFTP); 00154 public: 00156 PFTPClient(); 00157 00159 ~PFTPClient(); 00160 00161 00169 virtual BOOL Close(); 00170 00172 00180 BOOL LogIn( 00181 const PString & username, 00182 const PString & password 00183 ); 00184 00190 PString GetSystemType(); 00191 00197 BOOL SetType( 00198 RepresentationType type 00199 ); 00200 00206 BOOL ChangeDirectory( 00207 const PString & dirPath 00208 ); 00209 00215 PString GetCurrentDirectory(); 00216 00223 PStringArray GetDirectoryNames( 00224 NameTypes type = ShortNames, 00225 DataChannelType channel = Passive 00226 ); 00233 PStringArray GetDirectoryNames( 00234 const PString & path, 00235 NameTypes type = ShortNames, 00236 DataChannelType channel = Passive 00237 ); 00238 00244 PString GetFileStatus( 00245 const PString & path, 00246 DataChannelType channel = Passive 00247 ); 00248 00257 PTCPSocket * GetFile( 00258 const PString & filename, 00259 DataChannelType channel = NormalPort 00260 ); 00261 00270 PTCPSocket * PutFile( 00271 const PString & filename, 00272 DataChannelType channel = NormalPort 00273 ); 00274 00276 00277 protected: 00279 virtual BOOL OnOpen(); 00280 00281 PTCPSocket * NormalClientTransfer( 00282 Commands cmd, 00283 const PString & args 00284 ); 00285 PTCPSocket * PassiveClientTransfer( 00286 Commands cmd, 00287 const PString & args 00288 ); 00289 00291 WORD remotePort; 00292 }; 00293 00294 00298 class PFTPServer : public PFTP 00299 { 00300 PCLASSINFO(PFTPServer, PFTP); 00301 public: 00302 enum { MaxIllegalPasswords = 3 }; 00303 00305 PFTPServer(); 00306 PFTPServer( 00307 const PString & readyString 00308 ); 00309 00311 ~PFTPServer(); 00312 00313 00314 // New functions for class 00319 virtual PString GetHelloString(const PString & user) const; 00320 00322 virtual PString GetGoodbyeString(const PString & user) const; 00323 00325 virtual PString GetSystemTypeString() const; 00326 00328 BOOL GetAllowThirdPartyPort() const { return thirdPartyPort; } 00329 00331 void SetAllowThirdPartyPort(BOOL state) { thirdPartyPort = state; } 00332 00340 BOOL ProcessCommand(); 00341 00349 virtual BOOL DispatchCommand( 00350 PINDEX code, 00351 const PString & args 00352 ); 00353 00354 00361 virtual BOOL CheckLoginRequired( 00362 PINDEX cmd 00363 ); 00364 00373 virtual BOOL AuthoriseUser( 00374 const PString & user, 00375 const PString & password, 00376 BOOL & replied 00377 ); 00378 00385 virtual BOOL OnUnknown( 00386 const PCaselessString & command 00387 ); 00388 00395 virtual void OnError( 00396 PINDEX errorCode, 00397 PINDEX cmdNum, 00398 const char * msg 00399 ); 00400 00402 virtual void OnSyntaxError( 00403 PINDEX cmdNum 00404 ); 00405 00407 virtual void OnNotImplemented( 00408 PINDEX cmdNum 00409 ); 00410 00412 virtual void OnCommandSuccessful( 00413 PINDEX cmdNum 00414 ); 00415 00416 00417 // the following commands must be implemented by all servers 00418 // and can be performed without logging in 00419 virtual BOOL OnUSER(const PCaselessString & args); 00420 virtual BOOL OnPASS(const PCaselessString & args); // officially optional, but should be done 00421 virtual BOOL OnQUIT(const PCaselessString & args); 00422 virtual BOOL OnPORT(const PCaselessString & args); 00423 virtual BOOL OnSTRU(const PCaselessString & args); 00424 virtual BOOL OnMODE(const PCaselessString & args); 00425 virtual BOOL OnTYPE(const PCaselessString & args); 00426 virtual BOOL OnNOOP(const PCaselessString & args); 00427 virtual BOOL OnSYST(const PCaselessString & args); 00428 virtual BOOL OnSTAT(const PCaselessString & args); 00429 00430 // the following commands must be implemented by all servers 00431 // and cannot be performed without logging in 00432 virtual BOOL OnRETR(const PCaselessString & args); 00433 virtual BOOL OnSTOR(const PCaselessString & args); 00434 virtual BOOL OnACCT(const PCaselessString & args); 00435 virtual BOOL OnAPPE(const PCaselessString & args); 00436 virtual BOOL OnRNFR(const PCaselessString & args); 00437 virtual BOOL OnRNTO(const PCaselessString & args); 00438 virtual BOOL OnDELE(const PCaselessString & args); 00439 virtual BOOL OnCWD(const PCaselessString & args); 00440 virtual BOOL OnCDUP(const PCaselessString & args); 00441 virtual BOOL OnRMD(const PCaselessString & args); 00442 virtual BOOL OnMKD(const PCaselessString & args); 00443 virtual BOOL OnPWD(const PCaselessString & args); 00444 virtual BOOL OnLIST(const PCaselessString & args); 00445 virtual BOOL OnNLST(const PCaselessString & args); 00446 virtual BOOL OnPASV(const PCaselessString & args); 00447 00448 // the following commands are optional and can be performed without 00449 // logging in 00450 virtual BOOL OnHELP(const PCaselessString & args); 00451 virtual BOOL OnSITE(const PCaselessString & args); 00452 virtual BOOL OnABOR(const PCaselessString & args); 00453 00454 // the following commands are optional and cannot be performed 00455 // without logging in 00456 virtual BOOL OnSMNT(const PCaselessString & args); 00457 virtual BOOL OnREIN(const PCaselessString & args); 00458 virtual BOOL OnSTOU(const PCaselessString & args); 00459 virtual BOOL OnALLO(const PCaselessString & args); 00460 virtual BOOL OnREST(const PCaselessString & args); 00461 00462 00464 void SendToClient( 00465 const PFilePath & filename 00466 ); 00467 00468 00469 protected: 00471 BOOL OnOpen(); 00472 void Construct(); 00473 00474 PString readyString; 00475 BOOL thirdPartyPort; 00476 00477 enum { 00478 NotConnected, 00479 NeedUser, 00480 NeedPassword, 00481 Connected, 00482 ClientConnect 00483 } state; 00484 00485 PIPSocket::Address remoteHost; 00486 WORD remotePort; 00487 00488 PTCPSocket * passiveSocket; 00489 00490 char type; 00491 char structure; 00492 char mode; 00493 PString userName; 00494 int illegalPasswordCount; 00495 }; 00496 00497 00498 #endif 00499 00500 00501 // End of File ///////////////////////////////////////////////////////////////