| 1 |
{ |
| 2 |
Double commander |
| 3 |
------------------------------------------------------------------------- |
| 4 |
Wfx plugin for working with File Transfer Protocol |
| 5 |
|
| 6 |
Copyright (C) 2009-2023 Alexander Koblov ([email protected]) |
| 7 |
|
| 8 |
This library is free software; you can redistribute it and/or |
| 9 |
modify it under the terms of the GNU Lesser General Public |
| 10 |
License as published by the Free Software Foundation; either |
| 11 |
version 2.1 of the License, or (at your option) any later version. |
| 12 |
|
| 13 |
This library is distributed in the hope that it will be useful, |
| 14 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 16 |
Lesser General Public License for more details. |
| 17 |
|
| 18 |
You should have received a copy of the GNU Lesser General Public |
| 19 |
License along with this library; if not, write to the Free Software |
| 20 |
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 21 |
} |
| 22 |
|
| 23 |
unit FtpFunc; |
| 24 |
|
| 25 |
{$mode delphi}{$H+} |
| 26 |
{$include calling.inc} |
| 27 |
|
| 28 |
interface |
| 29 |
|
| 30 |
uses |
| 31 |
SysUtils, Classes, |
| 32 |
WfxPlugin, Extension; |
| 33 |
|
| 34 |
const |
| 35 |
cAddConnection = '<Add connection>'; |
| 36 |
cQuickConnection = '<Quick connection>'; |
| 37 |
|
| 38 |
type |
| 39 |
|
| 40 |
{ TConnection } |
| 41 |
|
| 42 |
TConnection = class |
| 43 |
public |
| 44 |
ConnectionName, Path, Host: AnsiString; |
| 45 |
Port: AnsiString; |
| 46 |
UserName: AnsiString; |
| 47 |
Password: AnsiString; |
| 48 |
MasterPassword: Boolean; |
| 49 |
CachedPassword: AnsiString; |
| 50 |
Proxy: String; |
| 51 |
PassiveMode: Boolean; |
| 52 |
CopySCP: Boolean; |
| 53 |
OnlySCP: Boolean; |
| 54 |
AutoTLS: Boolean; |
| 55 |
FullSSL: Boolean; |
| 56 |
OpenSSH: Boolean; |
| 57 |
AgentSSH: Boolean; |
| 58 |
UseAllocate: Boolean; |
| 59 |
Encoding: AnsiString; |
| 60 |
Fingerprint: AnsiString; |
| 61 |
InitCommands: AnsiString; |
| 62 |
ShowHiddenItems: Boolean; |
| 63 |
PasswordChanged: Boolean; |
| 64 |
KeepAliveTransfer: Boolean; |
| 65 |
PublicKey, PrivateKey: String; |
| 66 |
public |
| 67 |
procedure Assign(Connection: TConnection); |
| 68 |
end; |
| 69 |
|
| 70 |
function FsInitW(PluginNr: Integer; pProgressProc: TProgressProcW; |
| 71 |
pLogProc: TLogProcW; pRequestProc: TRequestProcW): Integer; dcpcall; export; |
| 72 |
|
| 73 |
function FsFindFirstW(Path: PWideChar; var FindData: TWin32FindDataW): THandle; dcpcall; export; |
| 74 |
function FsFindNextW(Hdl: THandle; var FindData: TWin32FindDataW): BOOL; dcpcall; export; |
| 75 |
function FsFindClose(Hdl: THandle): Integer; dcpcall; export; |
| 76 |
|
| 77 |
function FsExecuteFileW(MainWin: THandle; RemoteName, Verb: PWideChar): Integer; dcpcall; export; |
| 78 |
function FsRenMovFileW(OldName, NewName: PWideChar; Move, OverWrite: BOOL; |
| 79 |
RemoteInfo: pRemoteInfo): Integer; dcpcall; export; |
| 80 |
function FsGetFileW(RemoteName, LocalName: PWideChar; CopyFlags: Integer; |
| 81 |
RemoteInfo: pRemoteInfo): Integer; dcpcall; export; |
| 82 |
function FsPutFileW(LocalName, RemoteName: PWideChar; CopyFlags: Integer) |
| 83 |
: Integer; dcpcall; export; |
| 84 |
function FsDeleteFileW(RemoteName: PWideChar): BOOL; dcpcall; export; |
| 85 |
|
| 86 |
function FsMkDirW(RemoteDir: PWideChar): BOOL; dcpcall; export; |
| 87 |
function FsRemoveDirW(RemoteName: PWideChar): BOOL; dcpcall; export; |
| 88 |
|
| 89 |
function FsSetTimeW(RemoteName: PWideChar; CreationTime, LastAccessTime, |
| 90 |
LastWriteTime: PFileTime): BOOL; dcpcall; export; |
| 91 |
|
| 92 |
function FsDisconnectW(DisconnectRoot: PWideChar): BOOL; dcpcall; export; |
| 93 |
|
| 94 |
procedure FsSetCryptCallbackW(pCryptProc: TCryptProcW; CryptoNr, Flags: Integer); dcpcall; export; |
| 95 |
procedure FsGetDefRootName(DefRootName: PAnsiChar; MaxLen: Integer); dcpcall; export; |
| 96 |
procedure FsSetDefaultParams(dps: pFsDefaultParamStruct); dcpcall; export; |
| 97 |
procedure FsStatusInfoW(RemoteDir: PWideChar; InfoStartEnd, InfoOperation: Integer); dcpcall; export; |
| 98 |
function FsGetBackgroundFlags: Integer; dcpcall; export; |
| 99 |
{ Network API } |
| 100 |
{ |
| 101 |
procedure FsNetworkGetSupportedProtocols(Protocols: PAnsiChar; MaxLen: LongInt); dcpcall; export; |
| 102 |
function FsNetworkGetConnection(Index: LongInt; Connection: PAnsiChar; MaxLen: LongInt): LongBool; dcpcall; export; |
| 103 |
function FsNetworkManageConnection(MainWin: HWND; Connection: PAnsiChar; Action: LongInt; MaxLen: LongInt): LongBool; dcpcall; export; |
| 104 |
function FsNetworkOpenConnection(Connection: PAnsiChar; RootDir, RemotePath: PAnsiChar; MaxLen: LongInt): LongBool; dcpcall; export; |
| 105 |
} |
| 106 |
{ Extension API } |
| 107 |
procedure ExtensionInitialize(StartupInfo: PExtensionStartupInfo); dcpcall; export; |
| 108 |
|
| 109 |
function ReadPassword(ConnectionName: AnsiString; out Password: AnsiString): Boolean; |
| 110 |
function DeletePassword(ConnectionName: AnsiString): Boolean; |
| 111 |
|
| 112 |
var |
| 113 |
gStartupInfo: TExtensionStartupInfo; |
| 114 |
|
| 115 |
var |
| 116 |
LogProc: TLogProcW; |
| 117 |
CryptProc: TCryptProcW; |
| 118 |
PluginNumber: Integer; |
| 119 |
CryptoNumber: Integer; |
| 120 |
RequestProc: TRequestProcW; |
| 121 |
ProgressProc: TProgressProcW; |
| 122 |
|
| 123 |
implementation |
| 124 |
|
| 125 |
uses |
| 126 |
IniFiles, StrUtils, FtpAdv, FtpUtils, FtpConfDlg, syncobjs, LazFileUtils, |
| 127 |
LazUTF8, DCClassesUtf8, DCConvertEncoding, SftpSend, ScpSend, FtpProxy, |
| 128 |
FtpPropDlg, DCFileAttributes; |
| 129 |
|
| 130 |
var |
| 131 |
DefaultIniName: String; |
| 132 |
TcpKeepAlive: Boolean = True; |
| 133 |
ActiveConnectionList, ConnectionList: TStringList; |
| 134 |
IniFile: TIniFile; |
| 135 |
ListLock: TCriticalSection; |
| 136 |
|
| 137 |
threadvar |
| 138 |
ThreadCon: TFtpSendEx; |
| 139 |
|
| 140 |
const |
| 141 |
FS_COPYFLAGS_FORCE = FS_COPYFLAGS_OVERWRITE or FS_COPYFLAGS_RESUME; |
| 142 |
RootList: array [0 .. 1] of AnsiString = (cAddConnection, cQuickConnection); |
| 143 |
|
| 144 |
type |
| 145 |
TListRec = record |
| 146 |
Path: AnsiString; |
| 147 |
Index: Integer; |
| 148 |
FtpSend: TFTPSendEx; |
| 149 |
FtpList: TFTPListEx; |
| 150 |
end; |
| 151 |
PListRec = ^TListRec; |
| 152 |
|
| 153 |
procedure ReadConnectionList; |
| 154 |
var |
| 155 |
I, Count: Integer; |
| 156 |
sIndex: AnsiString; |
| 157 |
Connection: TConnection; |
| 158 |
begin |
| 159 |
Count := IniFile.ReadInteger('FTP', 'ConnectionCount', 0); |
| 160 |
for I := 1 to Count do |
| 161 |
begin |
| 162 |
sIndex := IntToStr(I); |
| 163 |
Connection := TConnection.Create; |
| 164 |
Connection.ConnectionName := IniFile.ReadString('FTP', 'Connection' + sIndex + 'Name', EmptyStr); |
| 165 |
Connection.Path := IniFile.ReadString('FTP', 'Connection' + sIndex + 'Path', EmptyStr); |
| 166 |
Connection.Host := IniFile.ReadString('FTP', 'Connection' + sIndex + 'Host', EmptyStr); |
| 167 |
Connection.Port := IniFile.ReadString('FTP', 'Connection' + sIndex + 'Port', EmptyStr); |
| 168 |
Connection.UserName := IniFile.ReadString('FTP', 'Connection' + sIndex + 'UserName', EmptyStr); |
| 169 |
Connection.MasterPassword := IniFile.ReadBool('FTP', 'Connection' + sIndex + 'MasterPassword', False); |
| 170 |
if Connection.MasterPassword then |
| 171 |
Connection.Password := EmptyStr |
| 172 |
else |
| 173 |
Connection.Password := DecodeBase64(IniFile.ReadString('FTP', 'Connection' + sIndex + 'Password', EmptyStr)); |
| 174 |
Connection.Proxy := IniFile.ReadString('FTP', 'Connection' + sIndex + 'Proxy', EmptyStr); |
| 175 |
Connection.Encoding := IniFile.ReadString('FTP', 'Connection' + sIndex + 'Encoding', EmptyStr); |
| 176 |
Connection.PassiveMode:= IniFile.ReadBool('FTP', 'Connection' + sIndex + 'PassiveMode', True); |
| 177 |
Connection.AutoTLS:= IniFile.ReadBool('FTP', 'Connection' + sIndex + 'AutoTLS', False); |
| 178 |
Connection.FullSSL:= IniFile.ReadBool('FTP', 'Connection' + sIndex + 'FullSSL', False); |
| 179 |
Connection.OpenSSH:= IniFile.ReadBool('FTP', 'Connection' + sIndex + 'OpenSSH', False); |
| 180 |
Connection.OnlySCP:= IniFile.ReadBool('FTP', 'Connection' + sIndex + 'OnlySCP', False); |
| 181 |
Connection.CopySCP:= IniFile.ReadBool('FTP', 'Connection' + sIndex + 'CopySCP', False); |
| 182 |
Connection.AgentSSH:= IniFile.ReadBool('FTP', 'Connection' + sIndex + 'AgentSSH', False); |
| 183 |
Connection.UseAllocate:= IniFile.ReadBool('FTP', 'Connection' + sIndex + 'UseAllocate', False); |
| 184 |
Connection.PublicKey := IniFile.ReadString('FTP', 'Connection' + sIndex + 'PublicKey', EmptyStr); |
| 185 |
Connection.PrivateKey := IniFile.ReadString('FTP', 'Connection' + sIndex + 'PrivateKey', EmptyStr); |
| 186 |
Connection.Fingerprint:= IniFile.ReadString('FTP', 'Connection' + sIndex + 'Fingerprint', EmptyStr); |
| 187 |
Connection.InitCommands := IniFile.ReadString('FTP', 'Connection' + sIndex + 'InitCommands', EmptyStr); |
| 188 |
Connection.ShowHiddenItems := IniFile.ReadBool('FTP', 'Connection' + sIndex + 'ShowHiddenItems', True); |
| 189 |
Connection.KeepAliveTransfer := IniFile.ReadBool('FTP', 'Connection' + sIndex + 'KeepAliveTransfer', False); |
| 190 |
// add connection to connection list |
| 191 |
ConnectionList.AddObject(Connection.ConnectionName, Connection); |
| 192 |
end; |
| 193 |
LoadProxyList(IniFile); |
| 194 |
end; |
| 195 |
|
| 196 |
procedure WriteConnectionList; |
| 197 |
var |
| 198 |
I, Count: Integer; |
| 199 |
sIndex: AnsiString; |
| 200 |
Connection: TConnection; |
| 201 |
begin |
| 202 |
IniFile.EraseSection('FTP'); |
| 203 |
Count := ConnectionList.Count; |
| 204 |
IniFile.WriteInteger('FTP', 'ConnectionCount', Count); |
| 205 |
for I := 0 to Count - 1 do |
| 206 |
begin |
| 207 |
sIndex := IntToStr(I + 1); |
| 208 |
Connection := TConnection(ConnectionList.Objects[I]); |
| 209 |
IniFile.WriteString('FTP', 'Connection' + sIndex + 'Name', Connection.ConnectionName); |
| 210 |
IniFile.WriteString('FTP', 'Connection' + sIndex + 'Path', Connection.Path); |
| 211 |
IniFile.WriteString('FTP', 'Connection' + sIndex + 'Host', Connection.Host); |
| 212 |
IniFile.WriteString('FTP', 'Connection' + sIndex + 'Port', Connection.Port); |
| 213 |
IniFile.WriteString('FTP', 'Connection' + sIndex + 'UserName', Connection.UserName); |
| 214 |
IniFile.WriteBool('FTP', 'Connection' + sIndex + 'MasterPassword', Connection.MasterPassword); |
| 215 |
if Connection.MasterPassword then |
| 216 |
IniFile.DeleteKey('FTP', 'Connection' + sIndex + 'Password') |
| 217 |
else |
| 218 |
IniFile.WriteString('FTP', 'Connection' + sIndex + 'Password', EncodeBase64(Connection.Password)); |
| 219 |
IniFile.WriteString('FTP', 'Connection' + sIndex + 'Proxy', Connection.Proxy); |
| 220 |
IniFile.WriteString('FTP', 'Connection' + sIndex + 'Encoding', Connection.Encoding); |
| 221 |
IniFile.WriteBool('FTP', 'Connection' + sIndex + 'PassiveMode', Connection.PassiveMode); |
| 222 |
IniFile.WriteBool('FTP', 'Connection' + sIndex + 'AutoTLS', Connection.AutoTLS); |
| 223 |
IniFile.WriteBool('FTP', 'Connection' + sIndex + 'FullSSL', Connection.FullSSL); |
| 224 |
IniFile.WriteBool('FTP', 'Connection' + sIndex + 'OpenSSH', Connection.OpenSSH); |
| 225 |
IniFile.WriteBool('FTP', 'Connection' + sIndex + 'OnlySCP', Connection.OnlySCP); |
| 226 |
IniFile.WriteBool('FTP', 'Connection' + sIndex + 'CopySCP', Connection.CopySCP); |
| 227 |
IniFile.WriteBool('FTP', 'Connection' + sIndex + 'AgentSSH', Connection.AgentSSH); |
| 228 |
IniFile.WriteBool('FTP', 'Connection' + sIndex + 'UseAllocate', Connection.UseAllocate); |
| 229 |
IniFile.WriteString('FTP', 'Connection' + sIndex + 'PublicKey', Connection.PublicKey); |
| 230 |
IniFile.WriteString('FTP', 'Connection' + sIndex + 'PrivateKey', Connection.PrivateKey); |
| 231 |
IniFile.WriteString('FTP', 'Connection' + sIndex + 'Fingerprint', Connection.Fingerprint); |
| 232 |
IniFile.WriteString('FTP', 'Connection' + sIndex + 'InitCommands', Connection.InitCommands); |
| 233 |
IniFile.WriteBool('FTP', 'Connection' + sIndex + 'ShowHiddenItems', Connection.ShowHiddenItems); |
| 234 |
IniFile.WriteBool('FTP', 'Connection' + sIndex + 'KeepAliveTransfer', Connection.KeepAliveTransfer); |
| 235 |
end; |
| 236 |
SaveProxyList(IniFile); |
| 237 |
try |
| 238 |
IniFile.UpdateFile; |
| 239 |
except |
| 240 |
on E: Exception do |
| 241 |
gStartupInfo.MessageBox(PAnsiChar(E.Message), nil, MB_OK or MB_ICONERROR); |
| 242 |
end; |
| 243 |
end; |
| 244 |
|
| 245 |
procedure FreeConnectionList; |
| 246 |
var |
| 247 |
I, Count: Integer; |
| 248 |
Connection: TConnection; |
| 249 |
begin |
| 250 |
Count := ConnectionList.Count; |
| 251 |
for I := Count - 1 downto 0 do |
| 252 |
begin |
| 253 |
Connection := TConnection(ConnectionList.Objects[I]); |
| 254 |
Connection.Free; |
| 255 |
ConnectionList.Delete(I); |
| 256 |
end; |
| 257 |
end; |
| 258 |
|
| 259 |
procedure ZeroPassword(var APassword: String); |
| 260 |
begin |
| 261 |
if (Length(APassword) > 0) then |
| 262 |
begin |
| 263 |
FillChar(APassword[1], Length(APassword), 0); |
| 264 |
SetLength(APassword, 0); |
| 265 |
end; |
| 266 |
end; |
| 267 |
|
| 268 |
function CryptFunc(Mode: LongInt; ConnectionName: String; var Password: String): LongInt; |
| 269 |
var |
| 270 |
APassword: UnicodeString; |
| 271 |
AConnection: UnicodeString; |
| 272 |
begin |
| 273 |
APassword:= CeUtf8ToUtf16(Password); |
| 274 |
AConnection:= CeUtf8ToUtf16(ConnectionName); |
| 275 |
if (Mode = FS_CRYPT_LOAD_PASSWORD) or (Mode = FS_CRYPT_LOAD_PASSWORD_NO_UI) then |
| 276 |
begin |
| 277 |
SetLength(APassword, MAX_PATH); |
| 278 |
FillChar(APassword[1], MAX_PATH * SizeOf(WideChar), #0); |
| 279 |
end; |
| 280 |
Result:= CryptProc(PluginNumber, CryptoNumber, Mode, PWideChar(AConnection), PWideChar(APassword), MAX_PATH); |
| 281 |
if (Mode = FS_CRYPT_LOAD_PASSWORD) or (Mode = FS_CRYPT_LOAD_PASSWORD_NO_UI) then |
| 282 |
begin |
| 283 |
Password:= UTF16ToUTF8(PWideChar(APassword)); // truncate to #0 |
| 284 |
end; |
| 285 |
end; |
| 286 |
|
| 287 |
function ShowPasswordDialog(out Password: String): Boolean; |
| 288 |
var |
| 289 |
APassword: UnicodeString; |
| 290 |
begin |
| 291 |
SetLength(APassword, MAX_PATH); APassword[1] := #0; |
| 292 |
Result := RequestProc(PluginNumber, RT_Password, nil, nil, PWideChar(APassword), MAX_PATH); |
| 293 |
if Result then |
| 294 |
Password := UTF16ToUTF8(PWideChar(APassword)) // truncate to #0 |
| 295 |
else |
| 296 |
Password := EmptyStr; |
| 297 |
end; |
| 298 |
|
| 299 |
function FtpLogin(const Connection: TConnection; const FtpSend: TFTPSendEx): Boolean; |
| 300 |
var |
| 301 |
sTemp: AnsiString; |
| 302 |
begin |
| 303 |
Result := False; |
| 304 |
if FtpSend.Login then |
| 305 |
begin |
| 306 |
sTemp:= Connection.InitCommands; |
| 307 |
while sTemp <> EmptyStr do |
| 308 |
FtpSend.ExecuteCommand(Copy2SymbDel(sTemp, ';')); |
| 309 |
if Length(Connection.Path) > 0 then |
| 310 |
FtpSend.ChangeWorkingDir(FtpSend.ClientToServer(CeUtf8ToUtf16(Connection.Path))); |
| 311 |
Result := True; |
| 312 |
end; |
| 313 |
end; |
| 314 |
|
| 315 |
function FtpConnect(const ConnectionName: AnsiString; out FtpSend: TFTPSendEx): Boolean; |
| 316 |
var |
| 317 |
I: Integer; |
| 318 |
APassword: String; |
| 319 |
Connection: TConnection; |
| 320 |
begin |
| 321 |
Result:= False; |
| 322 |
I:= ActiveConnectionList.IndexOf(ConnectionName); |
| 323 |
// If find active connection then use it |
| 324 |
if I >= 0 then |
| 325 |
begin |
| 326 |
FtpSend:= TFTPSendEx(ActiveConnectionList.Objects[I]); |
| 327 |
|
| 328 |
if FtpSend.NetworkError then |
| 329 |
//Server closed the connection, or network error occurred, or whatever else. |
| 330 |
//Attempt to reconnect and execute login sequence |
| 331 |
begin |
| 332 |
LogProc(PluginNumber, msgtype_details, PWideChar('Network error detected, attempting to reconnect...')); |
| 333 |
I:= ConnectionList.IndexOf(ConnectionName); |
| 334 |
if I >= 0 then |
| 335 |
begin |
| 336 |
Connection := TConnection(ConnectionList.Objects[I]); |
| 337 |
if not FtpLogin(Connection, FtpSend) then |
| 338 |
begin |
| 339 |
RequestProc(PluginNumber, RT_MsgOK, nil, 'Connection lost, unable to reconnect!', nil, MAX_PATH); |
| 340 |
Exit; |
| 341 |
end; |
| 342 |
end; |
| 343 |
end; |
| 344 |
|
| 345 |
Result:= True; |
| 346 |
end |
| 347 |
else |
| 348 |
begin |
| 349 |
// find in exists connection list |
| 350 |
I:= ConnectionList.IndexOf(ConnectionName); |
| 351 |
if I >= 0 then |
| 352 |
begin |
| 353 |
Connection := TConnection(ConnectionList.Objects[I]); |
| 354 |
if Connection.OpenSSH then |
| 355 |
begin |
| 356 |
if Connection.OnlySCP then |
| 357 |
FtpSend := TScpSend.Create(Connection.Encoding) |
| 358 |
else begin |
| 359 |
FtpSend := TSftpSend.Create(Connection.Encoding); |
| 360 |
TSftpSend(FtpSend).CopySCP := Connection.CopySCP; |
| 361 |
end; |
| 362 |
FtpSend.PublicKey:= Connection.PublicKey; |
| 363 |
FtpSend.PrivateKey:= Connection.PrivateKey; |
| 364 |
TScpSend(FtpSend).Agent:= Connection.AgentSSH; |
| 365 |
TScpSend(FtpSend).Fingerprint:= Connection.Fingerprint; |
| 366 |
end |
| 367 |
else begin |
| 368 |
FtpSend := TFTPSendEx.Create(Connection.Encoding); |
| 369 |
FtpSend.ShowHidden := Connection.ShowHiddenItems; |
| 370 |
FtpSend.KeepAliveTransfer := Connection.KeepAliveTransfer; |
| 371 |
end; |
| 372 |
FtpSend.TcpKeepAlive := TcpKeepAlive; |
| 373 |
FtpSend.TargetHost := Connection.Host; |
| 374 |
FtpSend.PassiveMode:= Connection.PassiveMode; |
| 375 |
FtpSend.AutoTLS:= Connection.AutoTLS; |
| 376 |
FtpSend.FullSSL:= Connection.FullSSL; |
| 377 |
FtpSend.UseAllocate:= Connection.UseAllocate; |
| 378 |
if Connection.Port <> EmptyStr then |
| 379 |
FtpSend.TargetPort := Connection.Port |
| 380 |
else if Connection.FullSSL then |
| 381 |
FtpSend.TargetPort := cFtpsPort; |
| 382 |
if Connection.UserName <> EmptyStr then |
| 383 |
FtpSend.UserName := Connection.UserName; |
| 384 |
if Connection.MasterPassword then |
| 385 |
begin |
| 386 |
if CryptFunc(FS_CRYPT_LOAD_PASSWORD, Connection.ConnectionName, Connection.Password) <> FS_FILE_OK then |
| 387 |
ZeroPassword(Connection.Password); |
| 388 |
end; |
| 389 |
// if no saved password then ask it |
| 390 |
if Connection.OpenSSH and (Connection.AgentSSH or ((Connection.PrivateKey <> '') and (Connection.PublicKey <> ''))) then |
| 391 |
APassword:= EmptyStr |
| 392 |
else if Length(Connection.Password) > 0 then |
| 393 |
APassword:= Connection.Password |
| 394 |
else if Length(Connection.CachedPassword) > 0 then |
| 395 |
APassword:= Connection.CachedPassword |
| 396 |
else if not ShowPasswordDialog(APassword) then |
| 397 |
begin |
| 398 |
FreeAndNil(FtpSend); |
| 399 |
Exit; |
| 400 |
end; |
| 401 |
FtpSend.Password := FtpSend.ClientToServer(APassword); |
| 402 |
SetProxy(FtpSend, Connection.Proxy); |
| 403 |
// try to connect |
| 404 |
if not FtpLogin(Connection, FtpSend) then |
| 405 |
begin |
| 406 |
RequestProc(PluginNumber, RT_MsgOK, nil, 'Can not connect to the server!', nil, MAX_PATH); |
| 407 |
FreeAndNil(FtpSend); |
| 408 |
end |
| 409 |
else begin |
| 410 |
Connection.CachedPassword:= APassword; |
| 411 |
LogProc(PluginNumber, MSGTYPE_CONNECT, PWideChar('CONNECT ' + PathDelim + CeUtf8ToUtf16(ConnectionName))); |
| 412 |
ActiveConnectionList.AddObject(ConnectionName, FtpSend); |
| 413 |
if Connection.OpenSSH and (ConnectionName <> cQuickConnection) then |
| 414 |
begin |
| 415 |
// Save connection server fingerprint |
| 416 |
if Connection.Fingerprint <> TScpSend(FtpSend).Fingerprint then |
| 417 |
begin |
| 418 |
Connection.Fingerprint:= TScpSend(FtpSend).Fingerprint; |
| 419 |
WriteConnectionList; |
| 420 |
end; |
| 421 |
end; |
| 422 |
Result:= True; |
| 423 |
end; |
| 424 |
end; |
| 425 |
end; |
| 426 |
end; |
| 427 |
|
| 428 |
function QuickConnection(out FtpSend: TFTPSendEx): Boolean; |
| 429 |
var |
| 430 |
Index: Integer; |
| 431 |
Connection: TConnection; |
| 432 |
begin |
| 433 |
Index:= ActiveConnectionList.IndexOf(cQuickConnection); |
| 434 |
Result:= (Index >= 0); |
| 435 |
if Result then |
| 436 |
FtpSend:= TFTPSendEx(ActiveConnectionList.Objects[Index]) |
| 437 |
else begin |
| 438 |
Connection := TConnection.Create; |
| 439 |
Connection.ConnectionName:= cQuickConnection; |
| 440 |
if ShowFtpConfDlg(Connection) then |
| 441 |
begin |
| 442 |
Connection.ConnectionName:= cQuickConnection; |
| 443 |
Index:= ConnectionList.AddObject(Connection.ConnectionName, Connection); |
| 444 |
Result:= FtpConnect(Connection.ConnectionName, FtpSend); |
| 445 |
ConnectionList.Delete(Index); |
| 446 |
end; |
| 447 |
Connection.Free; |
| 448 |
end; |
| 449 |
end; |
| 450 |
|
| 451 |
function AddConnection: Integer; |
| 452 |
var |
| 453 |
Connection: TConnection; |
| 454 |
begin |
| 455 |
Result := -1; |
| 456 |
Connection := TConnection.Create; |
| 457 |
Connection.PassiveMode := True; |
| 458 |
|
| 459 |
if ShowFtpConfDlg(Connection) then |
| 460 |
begin |
| 461 |
with Connection do |
| 462 |
begin |
| 463 |
if ConnectionList.IndexOf(ConnectionName) >= 0 then begin |
| 464 |
ConnectionName += '+' + IntToStr(Random(MaxInt)); |
| 465 |
end; |
| 466 |
if MasterPassword then |
| 467 |
begin |
| 468 |
if Length(Password) = 0 then |
| 469 |
MasterPassword:= False |
| 470 |
else if CryptFunc(FS_CRYPT_SAVE_PASSWORD, ConnectionName, Password) = FS_FILE_OK then |
| 471 |
Password:= EmptyStr |
| 472 |
else |
| 473 |
MasterPassword:= False; |
| 474 |
end; |
| 475 |
Result:= ConnectionList.AddObject(ConnectionName, Connection); |
| 476 |
end; |
| 477 |
end; |
| 478 |
|
| 479 |
if Result < 0 then |
| 480 |
FreeAndNil(Connection) |
| 481 |
else |
| 482 |
WriteConnectionList; |
| 483 |
end; |
| 484 |
|
| 485 |
function EditConnection(ConnectionName: AnsiString): Boolean; |
| 486 |
var |
| 487 |
I: Integer; |
| 488 |
ATemp: TConnection; |
| 489 |
Connection: TConnection; |
| 490 |
begin |
| 491 |
Result:= False; |
| 492 |
I := ConnectionList.IndexOf(ConnectionName); |
| 493 |
if I >= 0 then |
| 494 |
begin |
| 495 |
ATemp:= TConnection.Create; |
| 496 |
Connection:= TConnection(ConnectionList.Objects[I]); |
| 497 |
ATemp.Assign(Connection); |
| 498 |
if ShowFtpConfDlg(ATemp) then |
| 499 |
begin |
| 500 |
with ATemp do |
| 501 |
begin |
| 502 |
if ConnectionName <> Connection.ConnectionName then |
| 503 |
begin |
| 504 |
if Connection.MasterPassword then |
| 505 |
begin |
| 506 |
if CryptFunc(FS_CRYPT_MOVE_PASSWORD, Connection.ConnectionName, ConnectionName) <> FS_FILE_OK then |
| 507 |
begin |
| 508 |
gStartupInfo.MessageBox('Cannot save connection!', 'FTP', MB_OK or MB_ICONERROR); |
| 509 |
Exit(False); |
| 510 |
end; |
| 511 |
end; |
| 512 |
ConnectionList[I]:= ConnectionName |
| 513 |
end; |
| 514 |
if PasswordChanged then |
| 515 |
begin |
| 516 |
// Master password enabled |
| 517 |
if MasterPassword then |
| 518 |
begin |
| 519 |
if Length(Password) = 0 then |
| 520 |
MasterPassword:= False |
| 521 |
else if CryptFunc(FS_CRYPT_SAVE_PASSWORD, ConnectionName, Password) = FS_FILE_OK then |
| 522 |
Password:= EmptyStr |
| 523 |
else |
| 524 |
MasterPassword:= False; |
| 525 |
end; |
| 526 |
// Master password disabled |
| 527 |
if (MasterPassword = False) and (Connection.MasterPassword <> MasterPassword) then |
| 528 |
begin |
| 529 |
DeletePassword(ConnectionName); |
| 530 |
end; |
| 531 |
end |
| 532 |
end; |
| 533 |
Connection.Assign(ATemp); |
| 534 |
WriteConnectionList; |
| 535 |
Result:= True; |
| 536 |
end; |
| 537 |
FreeAndNil(ATemp); |
| 538 |
end; |
| 539 |
end; |
| 540 |
|
| 541 |
function DeleteConnection(ConnectionName: AnsiString): Boolean; |
| 542 |
var |
| 543 |
I: Integer; |
| 544 |
Connection: TConnection; |
| 545 |
begin |
| 546 |
Result:= False; |
| 547 |
I:= ConnectionList.IndexOf(ConnectionName); |
| 548 |
if I >= 0 then |
| 549 |
begin |
| 550 |
Connection:= TConnection(ConnectionList.Objects[I]); |
| 551 |
Connection.Free; |
| 552 |
ConnectionList.Delete(I); |
| 553 |
WriteConnectionList; |
| 554 |
Result:= True; |
| 555 |
end; |
| 556 |
end; |
| 557 |
|
| 558 |
function ExtractConnectionName(const sPath: AnsiString): AnsiString; |
| 559 |
var |
| 560 |
Index: Integer; |
| 561 |
begin |
| 562 |
Result:= sPath; |
| 563 |
if sPath[1] = PathDelim then |
| 564 |
Result := Copy(sPath, 2, Length(sPath)); |
| 565 |
Index := Pos(PathDelim, Result); |
| 566 |
if Index = 0 then |
| 567 |
Index := MaxInt; |
| 568 |
Result := Copy(Result, 1, Index - 1); |
| 569 |
end; |
| 570 |
|
| 571 |
function ExtractRemoteFileName(const FileName: AnsiString): AnsiString; |
| 572 |
var |
| 573 |
I: Integer; |
| 574 |
begin |
| 575 |
Result := FileName; |
| 576 |
System.Delete(Result, 1, 1); |
| 577 |
|
| 578 |
I := Pos(PathDelim, Result); |
| 579 |
if I = 0 then |
| 580 |
Result := '/' |
| 581 |
else |
| 582 |
begin |
| 583 |
System.Delete(Result, 1, I - 1); |
| 584 |
Result := StringReplace(Result, '\', '/', [rfReplaceAll]); |
| 585 |
end; |
| 586 |
end; |
| 587 |
|
| 588 |
function GetConnectionByPath(const sPath: UnicodeString; out FtpSend: TFTPSendEx; |
| 589 |
out RemotePath: AnsiString): Boolean; |
| 590 |
var |
| 591 |
sConnName: AnsiString; |
| 592 |
begin |
| 593 |
Result := False; |
| 594 |
if (ExtractFileDir(sPath) = PathDelim) then Exit; |
| 595 |
if Assigned(ThreadCon) then |
| 596 |
begin |
| 597 |
Result:= True; |
| 598 |
FtpSend:= ThreadCon; |
| 599 |
end |
| 600 |
else begin |
| 601 |
sConnName:= ExtractConnectionName(UTF16ToUTF8(sPath)); |
| 602 |
Result:= FtpConnect(sConnName, FtpSend); |
| 603 |
end; |
| 604 |
if Result then begin |
| 605 |
RemotePath:= FtpSend.ClientToServer(sPath); |
| 606 |
RemotePath:= ExtractRemoteFileName(RemotePath); |
| 607 |
end; |
| 608 |
end; |
| 609 |
|
| 610 |
function LocalFindNext(Hdl: THandle; var FindData: TWin32FindDataW): Boolean; |
| 611 |
var |
| 612 |
ListRec: PListRec absolute Hdl; |
| 613 |
I, RootCount: Integer; |
| 614 |
Connection: TConnection; |
| 615 |
begin |
| 616 |
Result := False; |
| 617 |
I := ListRec^.Index; |
| 618 |
RootCount := High(RootList) + 1; |
| 619 |
FillChar(FindData, SizeOf(FindData), 0); |
| 620 |
if I < RootCount then |
| 621 |
begin |
| 622 |
StrPCopy(FindData.cFileName, CeUtf8ToUtf16(RootList[I])); |
| 623 |
FindData.dwFileAttributes := 0; |
| 624 |
Inc(ListRec^.Index); |
| 625 |
Result := True; |
| 626 |
end |
| 627 |
else if I - RootCount < ConnectionList.Count then |
| 628 |
begin |
| 629 |
Connection := TConnection(ConnectionList.Objects[I - RootCount]); |
| 630 |
StrPCopy(FindData.cFileName, CeUtf8ToUtf16(Connection.ConnectionName)); |
| 631 |
FindData.dwFileAttributes := FILE_ATTRIBUTE_VOLUME; |
| 632 |
Inc(ListRec^.Index); |
| 633 |
Result := True; |
| 634 |
end; |
| 635 |
if Result then |
| 636 |
begin |
| 637 |
FindData.nFileSizeLow := $FFFFFFFE; |
| 638 |
FindData.nFileSizeHigh := $FFFFFFFF; |
| 639 |
FindData.ftLastWriteTime.dwLowDateTime := $FFFFFFFE; |
| 640 |
FindData.ftLastWriteTime.dwHighDateTime := $FFFFFFFF; |
| 641 |
end; |
| 642 |
end; |
| 643 |
|
| 644 |
function FsInitW(PluginNr: Integer; pProgressProc: TProgressProcW; |
| 645 |
pLogProc: TLogProcW; pRequestProc: TRequestProcW): Integer; dcpcall; export; |
| 646 |
begin |
| 647 |
ProgressProc := pProgressProc; |
| 648 |
LogProc := pLogProc; |
| 649 |
RequestProc := pRequestProc; |
| 650 |
PluginNumber := PluginNr; |
| 651 |
|
| 652 |
Result := 0; |
| 653 |
end; |
| 654 |
|
| 655 |
function FsFindFirstW(Path: PWideChar; var FindData: TWin32FindDataW): THandle; dcpcall; export; |
| 656 |
var |
| 657 |
ListRec: PListRec; |
| 658 |
sPath: AnsiString; |
| 659 |
FtpSend: TFTPSendEx; |
| 660 |
Directory: UnicodeString; |
| 661 |
begin |
| 662 |
New(ListRec); |
| 663 |
ListRec.Index := 0; |
| 664 |
ListRec.Path := Path; |
| 665 |
ListRec.FtpSend := nil; |
| 666 |
ListRec.FtpList := nil; |
| 667 |
Result := wfxInvalidHandle; |
| 668 |
|
| 669 |
if Path = PathDelim then |
| 670 |
begin |
| 671 |
Result := THandle(ListRec); |
| 672 |
LocalFindNext(Result, FindData); |
| 673 |
end |
| 674 |
else |
| 675 |
begin |
| 676 |
ListLock.Acquire; |
| 677 |
try |
| 678 |
Directory:= Path; |
| 679 |
if Directory[Length(Directory)] <> PathDelim then |
| 680 |
Directory:= Directory + PathDelim; |
| 681 |
if GetConnectionByPath(Directory, FtpSend, sPath) then |
| 682 |
begin |
| 683 |
ListRec.FtpSend := FtpSend; |
| 684 |
ListRec.FtpList := FtpSend.FsFindFirstW(sPath, FindData); |
| 685 |
if Assigned(ListRec.FtpList) then Result:= THandle(ListRec); |
| 686 |
end; |
| 687 |
finally |
| 688 |
ListLock.Release; |
| 689 |
if Result = wfxInvalidHandle then |
| 690 |
Dispose(ListRec); |
| 691 |
end; |
| 692 |
end; |
| 693 |
end; |
| 694 |
|
| 695 |
function FsFindNextW(Hdl: THandle; var FindData: TWin32FindDataW): BOOL; dcpcall; export; |
| 696 |
var |
| 697 |
ListRec: PListRec absolute Hdl; |
| 698 |
begin |
| 699 |
if ListRec.Path = PathDelim then |
| 700 |
Result := LocalFindNext(Hdl, FindData) |
| 701 |
else |
| 702 |
Result := ListRec^.FtpSend.FsFindNextW(ListRec.FtpList, FindData); |
| 703 |
end; |
| 704 |
|
| 705 |
function FsFindClose(Hdl: THandle): Integer; dcpcall; export; |
| 706 |
var |
| 707 |
ListRec: PListRec absolute Hdl; |
| 708 |
begin |
| 709 |
Result:= 0; |
| 710 |
if Assigned(ListRec) then |
| 711 |
begin |
| 712 |
if Assigned(ListRec^.FtpSend) then begin |
| 713 |
Result:= ListRec^.FtpSend.FsFindClose(ListRec.FtpList); |
| 714 |
end; |
| 715 |
Dispose(ListRec); |
| 716 |
end; |
| 717 |
end; |
| 718 |
|
| 719 |
function FsExecuteFileW(MainWin: THandle; RemoteName, Verb: PWideChar): Integer; dcpcall; export; |
| 720 |
var |
| 721 |
FtpSend: TFTPSendEx; |
| 722 |
RemoteDir: AnsiString; |
| 723 |
asFileName: AnsiString; |
| 724 |
wsFileName: UnicodeString; |
| 725 |
begin |
| 726 |
Result:= FS_EXEC_YOURSELF; |
| 727 |
if (RemoteName = '') then Exit; |
| 728 |
|
| 729 |
if Verb = 'open' then |
| 730 |
begin |
| 731 |
if (ExtractFileDir(RemoteName) = PathDelim) then // root path |
| 732 |
begin |
| 733 |
asFileName:= UTF16ToUTF8(RemoteName + 1); |
| 734 |
if RemoteName[1] <> '<' then // connection |
| 735 |
begin |
| 736 |
if not FtpConnect(asFileName, FtpSend) then |
| 737 |
Result := FS_EXEC_OK |
| 738 |
else begin |
| 739 |
Result := FS_EXEC_SYMLINK; |
| 740 |
end; |
| 741 |
end |
| 742 |
else // special item |
| 743 |
begin |
| 744 |
if asFileName = cAddConnection then |
| 745 |
begin |
| 746 |
AddConnection; |
| 747 |
Result:= FS_EXEC_OK; |
| 748 |
end |
| 749 |
else if asFileName = cQuickConnection then |
| 750 |
begin |
| 751 |
if not QuickConnection(FtpSend) then |
| 752 |
Result := FS_EXEC_OK |
| 753 |
else begin |
| 754 |
Result := FS_EXEC_SYMLINK; |
| 755 |
end; |
| 756 |
end; |
| 757 |
end; |
| 758 |
if (Result = FS_EXEC_SYMLINK) then |
| 759 |
begin |
| 760 |
wsFileName := FtpSend.ServerToClient(FtpSend.GetCurrentDir); |
| 761 |
wsFileName := SetDirSeparators(RemoteName + wsFileName); |
| 762 |
StrPLCopy(RemoteName, wsFileName, MAX_PATH); |
| 763 |
end; |
| 764 |
end; // root path |
| 765 |
end // Verb = open |
| 766 |
else if Pos('chmod', UnicodeString(Verb)) = 1 then |
| 767 |
begin |
| 768 |
if GetConnectionByPath(RemoteName, FtpSend, asFileName) then |
| 769 |
begin |
| 770 |
if FtpSend.ChangeMode(asFileName, AnsiString(Copy(Verb, 7, MaxInt))) then |
| 771 |
Result:= FS_EXEC_OK |
| 772 |
else |
| 773 |
Result := FS_EXEC_ERROR; |
| 774 |
end; |
| 775 |
end |
| 776 |
else if Pos('quote', UnicodeString(Verb)) = 1 then |
| 777 |
begin |
| 778 |
if GetConnectionByPath(RemoteName, FtpSend, RemoteDir) then |
| 779 |
begin |
| 780 |
asFileName:= FtpSend.ClientToServer(Verb); |
| 781 |
if FtpSend.ExecuteCommand(Copy(asFileName, 7, MaxInt), RemoteDir) then |
| 782 |
Result := FS_EXEC_OK |
| 783 |
else |
| 784 |
Result := FS_EXEC_ERROR; |
| 785 |
end; |
| 786 |
end |
| 787 |
else if Verb = 'properties' then |
| 788 |
begin |
| 789 |
if (ExtractFileDir(RemoteName) = PathDelim) and not (RemoteName[1] in [#0, '<']) then // connection |
| 790 |
begin |
| 791 |
EditConnection(UTF16ToUTF8(RemoteName + 1)); |
| 792 |
end |
| 793 |
else if (ExtractFileDir(RemoteName) <> PathDelim) then |
| 794 |
begin |
| 795 |
if GetConnectionByPath(RemoteName, FtpSend, asFileName) then |
| 796 |
begin |
| 797 |
if FtpSend.FileProperties(asFileName) then |
| 798 |
begin |
| 799 |
wsFileName:= FtpSend.ServerToClient(FtpSend.FullResult.Text); |
| 800 |
ShowPropertiesDlg(PAnsiChar(UTF8Encode(wsFileName))); |
| 801 |
end |
| 802 |
end; |
| 803 |
end; |
| 804 |
Result:= FS_EXEC_OK; |
| 805 |
end; |
| 806 |
end; |
| 807 |
|
| 808 |
function FsRenMovFileW(OldName, NewName: PWideChar; Move, OverWrite: BOOL; |
| 809 |
RemoteInfo: pRemoteInfo): Integer; dcpcall; export; |
| 810 |
var |
| 811 |
O, N: Integer; |
| 812 |
FtpSend: TFTPSendEx; |
| 813 |
sOldName: AnsiString; |
| 814 |
sNewName: AnsiString; |
| 815 |
Connection: TConnection; |
| 816 |
begin |
| 817 |
Result := FS_FILE_NOTSUPPORTED; |
| 818 |
|
| 819 |
if not Move then |
| 820 |
begin |
| 821 |
if (ExtractFileDir(OldName) = PathDelim) and (WideChar(OldName[1]) <> '<') and |
| 822 |
(ExtractFileDir(NewName) = PathDelim) and (WideChar(NewName[1]) <> '<') then |
| 823 |
begin |
| 824 |
O:= ConnectionList.IndexOf(OldName + 1); |
| 825 |
if O < 0 then |
| 826 |
Result:= FS_FILE_NOTFOUND |
| 827 |
else begin |
| 828 |
sNewName:= RepairConnectionName(UTF16ToUTF8(UnicodeString(NewName + 1))); |
| 829 |
N:= ConnectionList.IndexOf(sNewName); |
| 830 |
if (N >= 0) then |
| 831 |
begin |
| 832 |
if not OverWrite then Exit(FS_FILE_EXISTS); |
| 833 |
Connection:= TConnection(ConnectionList.Objects[N]); |
| 834 |
end |
| 835 |
else begin |
| 836 |
Connection:= TConnection.Create; |
| 837 |
ConnectionList.AddObject(sNewName, Connection); |
| 838 |
end; |
| 839 |
Connection.Assign(TConnection(ConnectionList.Objects[O])); |
| 840 |
Connection.ConnectionName:= sNewName; |
| 841 |
|
| 842 |
WriteConnectionList; |
| 843 |
Result:= FS_FILE_OK; |
| 844 |
end; |
| 845 |
end |
| 846 |
else if GetConnectionByPath(OldName, FtpSend, sOldName) then |
| 847 |
begin |
| 848 |
if FtpSend is TScpSend then |
| 849 |
begin |
| 850 |
sNewName := FtpSend.ClientToServer(NewName); |
| 851 |
sNewName := ExtractRemoteFileName(sNewName); |
| 852 |
ProgressProc(PluginNumber, OldName, NewName, 0); |
| 853 |
if (not OverWrite) and (FtpSend.FileExists(sNewName)) then |
| 854 |
begin |
| 855 |
Exit(FS_FILE_EXISTS); |
| 856 |
end; |
| 857 |
if FtpSend.CopyFile(sOldName, sNewName) then |
| 858 |
begin |
| 859 |
ProgressProc(PluginNumber, OldName, NewName, 100); |
| 860 |
Result := FS_FILE_OK; |
| 861 |
end; |
| 862 |
end; |
| 863 |
end; |
| 864 |
Exit; |
| 865 |
end; |
| 866 |
|
| 867 |
if (ExtractFileDir(OldName) = PathDelim) and (WideChar(OldName[1]) <> '<') then |
| 868 |
begin |
| 869 |
O:= ConnectionList.IndexOf(OldName + 1); |
| 870 |
if O < 0 then |
| 871 |
Result:= FS_FILE_NOTFOUND |
| 872 |
else |
| 873 |
begin |
| 874 |
ConnectionList[O]:= RepairConnectionName(UTF16ToUTF8(UnicodeString(NewName + 1))); |
| 875 |
TConnection(ConnectionList.Objects[O]).ConnectionName:= ConnectionList[O]; |
| 876 |
WriteConnectionList; |
| 877 |
Result:= FS_FILE_OK; |
| 878 |
end; |
| 879 |
end |
| 880 |
else if GetConnectionByPath(OldName, FtpSend, sOldName) then |
| 881 |
begin |
| 882 |
sNewName := FtpSend.ClientToServer(NewName); |
| 883 |
sNewName := ExtractRemoteFileName(sNewName); |
| 884 |
ProgressProc(PluginNumber, OldName, NewName, 0); |
| 885 |
if FtpSend.RenameFile(sOldName, sNewName) then |
| 886 |
begin |
| 887 |
ProgressProc(PluginNumber, OldName, NewName, 100); |
| 888 |
Result := FS_FILE_OK; |
| 889 |
end; |
| 890 |
end; |
| 891 |
end; |
| 892 |
|
| 893 |
function FsGetFileW(RemoteName, LocalName: PWideChar; CopyFlags: Integer; |
| 894 |
RemoteInfo: pRemoteInfo): Integer; dcpcall; export; |
| 895 |
var |
| 896 |
FileSize: Int64; |
| 897 |
FtpSend: TFTPSendEx; |
| 898 |
sFileName: AnsiString; |
| 899 |
FileName: AnsiString; |
| 900 |
begin |
| 901 |
Result := FS_FILE_READERROR; |
| 902 |
if GetConnectionByPath(RemoteName, FtpSend, sFileName) then |
| 903 |
try |
| 904 |
FileName:= UTF16ToUTF8(UnicodeString(LocalName)); |
| 905 |
if FileExistsUTF8(FileName) and (CopyFlags and FS_COPYFLAGS_FORCE = 0) then |
| 906 |
begin |
| 907 |
if not FtpSend.CanResume then Exit(FS_FILE_EXISTS); |
| 908 |
Exit(FS_FILE_EXISTSRESUMEALLOWED); |
| 909 |
end; |
| 910 |
FtpSend.DataStream.Clear; |
| 911 |
FtpSend.DirectFileName := FileName; |
| 912 |
Int64Rec(FileSize).Lo := RemoteInfo^.SizeLow; |
| 913 |
Int64Rec(FileSize).Hi := RemoteInfo^.SizeHigh; |
| 914 |
ProgressProc(PluginNumber, RemoteName, LocalName, 0); |
| 915 |
if FtpSend.RetrieveFile(sFileName, FileSize, (CopyFlags and FS_COPYFLAGS_RESUME) <> 0) then |
| 916 |
begin |
| 917 |
ProgressProc(PluginNumber, RemoteName, LocalName, 100); |
| 918 |
Result := FS_FILE_OK; |
| 919 |
end; |
| 920 |
except |
| 921 |
on EUserAbort do Result := FS_FILE_USERABORT; |
| 922 |
on EFOpenError do Result := FS_FILE_READERROR; |
| 923 |
else Result := FS_FILE_WRITEERROR; |
| 924 |
end; |
| 925 |
end; |
| 926 |
|
| 927 |
function FsPutFileW(LocalName, RemoteName: PWideChar; CopyFlags: Integer): Integer; dcpcall; export; |
| 928 |
var |
| 929 |
FtpSend: TFTPSendEx; |
| 930 |
sFileName: AnsiString; |
| 931 |
FileName: AnsiString; |
| 932 |
begin |
| 933 |
Result := FS_FILE_WRITEERROR; |
| 934 |
if GetConnectionByPath(RemoteName, FtpSend, sFileName) then |
| 935 |
try |
| 936 |
FileName:= UTF16ToUTF8(UnicodeString(LocalName)); |
| 937 |
if (CopyFlags and FS_COPYFLAGS_FORCE = 0) and (FtpSend.FileExists(sFileName)) then |
| 938 |
begin |
| 939 |
if not FtpSend.CanResume then Exit(FS_FILE_EXISTS); |
| 940 |
Exit(FS_FILE_EXISTSRESUMEALLOWED); |
| 941 |
end; |
| 942 |
FtpSend.DataStream.Clear; |
| 943 |
FtpSend.DirectFileName := FileName; |
| 944 |
ProgressProc(PluginNumber, LocalName, RemoteName, 0); |
| 945 |
if FtpSend.StoreFile(sFileName, (CopyFlags and FS_COPYFLAGS_RESUME) <> 0) then |
| 946 |
begin |
| 947 |
ProgressProc(PluginNumber, LocalName, RemoteName, 100); |
| 948 |
Result := FS_FILE_OK; |
| 949 |
end; |
| 950 |
except |
| 951 |
on EReadError do Result := FS_FILE_READERROR; |
| 952 |
on EUserAbort do Result := FS_FILE_USERABORT; |
| 953 |
else Result := FS_FILE_WRITEERROR; |
| 954 |
end; |
| 955 |
end; |
| 956 |
|
| 957 |
function FsDeleteFileW(RemoteName: PWideChar): BOOL; dcpcall; export; |
| 958 |
var |
| 959 |
FtpSend: TFTPSendEx; |
| 960 |
sFileName: AnsiString; |
| 961 |
begin |
| 962 |
Result := False; |
| 963 |
// if root path then delete connection |
| 964 |
if (ExtractFileDir(RemoteName) = PathDelim) and (RemoteName[1] <> '<') then |
| 965 |
Result:= DeleteConnection(ExtractConnectionName(UTF16ToUTF8(RemoteName))) |
| 966 |
else if GetConnectionByPath(RemoteName, FtpSend, sFileName) then |
| 967 |
Result := FtpSend.DeleteFile(sFileName); |
| 968 |
end; |
| 969 |
|
| 970 |
function FsMkDirW(RemoteDir: PWideChar): BOOL; dcpcall; export; |
| 971 |
var |
| 972 |
sPath: AnsiString; |
| 973 |
FtpSend: TFTPSendEx; |
| 974 |
begin |
| 975 |
Result := False; |
| 976 |
if GetConnectionByPath(RemoteDir, FtpSend, sPath) then |
| 977 |
Result := FtpSend.CreateDir(sPath); |
| 978 |
end; |
| 979 |
|
| 980 |
function FsRemoveDirW(RemoteName: PWideChar): BOOL; dcpcall; export; |
| 981 |
var |
| 982 |
sPath: AnsiString; |
| 983 |
FtpSend: TFTPSendEx; |
| 984 |
begin |
| 985 |
Result := False; |
| 986 |
if GetConnectionByPath(RemoteName, FtpSend, sPath) then |
| 987 |
Result := FtpSend.DeleteDir(sPath); |
| 988 |
end; |
| 989 |
|
| 990 |
function FsSetTimeW(RemoteName: PWideChar; CreationTime, LastAccessTime, |
| 991 |
LastWriteTime: PFileTime): BOOL; dcpcall; export; |
| 992 |
var |
| 993 |
sPath: AnsiString; |
| 994 |
FtpSend: TFTPSendEx; |
| 995 |
begin |
| 996 |
if (LastAccessTime = nil) and (LastWriteTime = nil) then |
| 997 |
Result := False |
| 998 |
else if GetConnectionByPath(RemoteName, FtpSend, sPath) then |
| 999 |
Result := FtpSend.FsSetTime(sPath, LastAccessTime, LastWriteTime) |
| 1000 |
else begin |
| 1001 |
Result := False; |
| 1002 |
end; |
| 1003 |
end; |
| 1004 |
|
| 1005 |
function FsDisconnectW(DisconnectRoot: PWideChar): BOOL; dcpcall; export; |
| 1006 |
var |
| 1007 |
Index: Integer; |
| 1008 |
asTemp: AnsiString; |
| 1009 |
FtpSend: TFTPSendEx; |
| 1010 |
wsTemp: UnicodeString; |
| 1011 |
begin |
| 1012 |
wsTemp := ExcludeLeadingPathDelimiter(DisconnectRoot); |
| 1013 |
asTemp := ExtractConnectionName(UTF16ToUTF8(wsTemp)); |
| 1014 |
|
| 1015 |
Index := ActiveConnectionList.IndexOf(asTemp); |
| 1016 |
Result := (Index >= 0); |
| 1017 |
|
| 1018 |
if Result then |
| 1019 |
begin |
| 1020 |
FtpSend:= TFTPSendEx(ActiveConnectionList.Objects[Index]); |
| 1021 |
|
| 1022 |
if not FtpSend.NetworkError then |
| 1023 |
begin |
| 1024 |
FtpSend.Logout; |
| 1025 |
end; |
| 1026 |
ActiveConnectionList.Delete(Index); |
| 1027 |
|
| 1028 |
Index:= ConnectionList.IndexOf(asTemp); |
| 1029 |
if Index >= 0 then begin |
| 1030 |
ZeroPassword(TConnection(ConnectionList.Objects[Index]).CachedPassword); |
| 1031 |
end; |
| 1032 |
LogProc(PluginNumber, MSGTYPE_DISCONNECT, PWideChar('DISCONNECT ' + DisconnectRoot)); |
| 1033 |
|
| 1034 |
FreeAndNil(FtpSend); |
| 1035 |
end; |
| 1036 |
end; |
| 1037 |
|
| 1038 |
procedure FsSetCryptCallbackW(pCryptProc: TCryptProcW; CryptoNr, Flags: Integer); dcpcall; export; |
| 1039 |
begin |
| 1040 |
CryptProc:= pCryptProc; |
| 1041 |
CryptoNumber:= CryptoNr; |
| 1042 |
end; |
| 1043 |
|
| 1044 |
procedure FsGetDefRootName(DefRootName: PAnsiChar; MaxLen: Integer); dcpcall; export; |
| 1045 |
begin |
| 1046 |
StrPLCopy(DefRootName, 'FTP', MaxLen); |
| 1047 |
end; |
| 1048 |
|
| 1049 |
procedure FsSetDefaultParams(dps: pFsDefaultParamStruct); dcpcall; export; |
| 1050 |
begin |
| 1051 |
ConnectionList := TStringList.Create; |
| 1052 |
ActiveConnectionList := TStringList.Create; |
| 1053 |
DefaultIniName:= ExtractFileName(dps.DefaultIniName); |
| 1054 |
end; |
| 1055 |
|
| 1056 |
procedure FsStatusInfoW(RemoteDir: PWideChar; InfoStartEnd, InfoOperation: Integer); dcpcall; export; |
| 1057 |
var |
| 1058 |
FtpSend: TFtpSendEx; |
| 1059 |
RemotePath: AnsiString; |
| 1060 |
begin |
| 1061 |
if (InfoOperation in [FS_STATUS_OP_GET_MULTI_THREAD, FS_STATUS_OP_PUT_MULTI_THREAD]) then |
| 1062 |
begin |
| 1063 |
if InfoStartEnd = FS_STATUS_START then |
| 1064 |
begin |
| 1065 |
if GetConnectionByPath(RemoteDir, FtpSend, RemotePath) then |
| 1066 |
begin |
| 1067 |
LogProc(PluginNumber, msgtype_details, 'Create background connection'); |
| 1068 |
ThreadCon:= FtpSend.Clone; |
| 1069 |
if not ThreadCon.Login then |
| 1070 |
begin |
| 1071 |
FreeAndNil(ThreadCon); |
| 1072 |
LogProc(PluginNumber, msgtype_importanterror, 'Cannot create background connection, use foreground'); |
| 1073 |
end; |
| 1074 |
end; |
| 1075 |
end |
| 1076 |
else if Assigned(ThreadCon) then |
| 1077 |
begin |
| 1078 |
LogProc(PluginNumber, msgtype_details, 'Destroy background connection'); |
| 1079 |
ThreadCon.Logout; |
| 1080 |
FreeAndNil(ThreadCon); |
| 1081 |
end; |
| 1082 |
end; |
| 1083 |
end; |
| 1084 |
|
| 1085 |
function FsGetBackgroundFlags: Integer; dcpcall; export; |
| 1086 |
begin |
| 1087 |
Result:= BG_DOWNLOAD or BG_UPLOAD or BG_ASK_USER; |
| 1088 |
end; |
| 1089 |
|
| 1090 |
{ |
| 1091 |
procedure FsNetworkGetSupportedProtocols(Protocols: PAnsiChar; MaxLen: LongInt); dcpcall; export; |
| 1092 |
begin |
| 1093 |
StrPLCopy(Protocols, ftpProtocol, MaxLen); |
| 1094 |
end; |
| 1095 |
|
| 1096 |
function FsNetworkGetConnection(Index: LongInt; Connection: PAnsiChar; |
| 1097 |
MaxLen: LongInt): LongBool; dcpcall; export; |
| 1098 |
begin |
| 1099 |
Result:= False; |
| 1100 |
if Index >= ConnectionList.Count then Exit; |
| 1101 |
StrPLCopy(Connection, TConnection(ConnectionList.Objects[Index]).ConnectionName, MaxLen); |
| 1102 |
Result:= True; |
| 1103 |
end; |
| 1104 |
|
| 1105 |
function FsNetworkManageConnection(MainWin: HWND; Connection: PAnsiChar; Action: LongInt; |
| 1106 |
MaxLen: LongInt): LongBool; dcpcall; export; |
| 1107 |
var |
| 1108 |
I: Integer; |
| 1109 |
begin |
| 1110 |
Result:= False; |
| 1111 |
case Action of |
| 1112 |
FS_NM_ACTION_ADD: |
| 1113 |
begin |
| 1114 |
I:= AddConnection; |
| 1115 |
if I >= 0 then |
| 1116 |
begin |
| 1117 |
StrPLCopy(Connection, ConnectionList[I], MaxLen); |
| 1118 |
Result:= True; |
| 1119 |
end; |
| 1120 |
end; |
| 1121 |
FS_NM_ACTION_EDIT: |
| 1122 |
begin |
| 1123 |
I:= ConnectionList.IndexOf(Connection); |
| 1124 |
if I >= 0 then |
| 1125 |
begin |
| 1126 |
if EditConnection(Connection) then |
| 1127 |
begin |
| 1128 |
StrPLCopy(Connection, ConnectionList[I], MaxLen); |
| 1129 |
Result:= True; |
| 1130 |
end; |
| 1131 |
end; |
| 1132 |
end; |
| 1133 |
FS_NM_ACTION_DELETE: |
| 1134 |
Result:= DeleteConnection(Connection); |
| 1135 |
end; |
| 1136 |
end; |
| 1137 |
|
| 1138 |
function FsNetworkOpenConnection(Connection: PAnsiChar; RootDir, RemotePath: PAnsiChar; |
| 1139 |
MaxLen: LongInt): LongBool; dcpcall; export; |
| 1140 |
var |
| 1141 |
I: Integer; |
| 1142 |
FtpSend: TFTPSendEx; |
| 1143 |
Con: TConnection; |
| 1144 |
begin |
| 1145 |
Result:= False; |
| 1146 |
if Connection = #0 then |
| 1147 |
begin |
| 1148 |
if QuickConnection then |
| 1149 |
begin |
| 1150 |
I:= ActiveConnectionList.IndexOf(cQuickConnection); |
| 1151 |
if I >= 0 then |
| 1152 |
begin |
| 1153 |
Con:= TConnection(ActiveConnectionList.Objects[I]); |
| 1154 |
StrPLCopy(Connection, ftpProtocol + Con.Host, MaxLen); |
| 1155 |
StrPLCopy(RootDir, PathDelim + Con.ConnectionName, MaxLen); |
| 1156 |
StrPLCopy(RemotePath, Con.Path, MaxLen); |
| 1157 |
Result:= True; |
| 1158 |
end; |
| 1159 |
end; |
| 1160 |
end |
| 1161 |
else if FtpConnect(Connection, FtpSend) then |
| 1162 |
begin |
| 1163 |
I:= ConnectionList.IndexOf(Connection); |
| 1164 |
if I >= 0 then |
| 1165 |
begin |
| 1166 |
Con:= TConnection(ConnectionList.Objects[I]); |
| 1167 |
StrPLCopy(Connection, ftpProtocol + Con.Host, MaxLen); |
| 1168 |
StrPLCopy(RootDir, PathDelim + Con.ConnectionName, MaxLen); |
| 1169 |
StrPLCopy(RemotePath, Con.Path, MaxLen); |
| 1170 |
Result:= True; |
| 1171 |
end; |
| 1172 |
end; |
| 1173 |
end; |
| 1174 |
} |
| 1175 |
|
| 1176 |
procedure ExtensionInitialize(StartupInfo: PExtensionStartupInfo); |
| 1177 |
begin |
| 1178 |
gStartupInfo:= StartupInfo^; |
| 1179 |
DefaultIniName:= gStartupInfo.PluginConfDir + DefaultIniName; |
| 1180 |
|
| 1181 |
try |
| 1182 |
IniFile := TIniFileEx.Create(DefaultIniName, fmOpenReadWrite); |
| 1183 |
|
| 1184 |
// Use TCP keep alive for all connections: Useful for certain |
| 1185 |
// firewalls/router if the connection breaks very often. |
| 1186 |
TcpKeepAlive := IniFile.ReadBool('General', 'TcpKeepAlive', TcpKeepAlive); |
| 1187 |
|
| 1188 |
ReadConnectionList; |
| 1189 |
except |
| 1190 |
on E: Exception do |
| 1191 |
gStartupInfo.MessageBox(PAnsiChar(E.Message), nil, MB_OK or MB_ICONERROR); |
| 1192 |
end; |
| 1193 |
end; |
| 1194 |
|
| 1195 |
function ReadPassword(ConnectionName: AnsiString; out Password: AnsiString): Boolean; |
| 1196 |
begin |
| 1197 |
Password:= EmptyStr; |
| 1198 |
case CryptFunc(FS_CRYPT_LOAD_PASSWORD, ConnectionName, Password) of |
| 1199 |
FS_FILE_OK, |
| 1200 |
FS_FILE_READERROR: |
| 1201 |
Result:= True |
| 1202 |
else |
| 1203 |
Result:= False; |
| 1204 |
end; |
| 1205 |
end; |
| 1206 |
|
| 1207 |
function DeletePassword(ConnectionName: AnsiString): Boolean; |
| 1208 |
var |
| 1209 |
Password: String = ''; |
| 1210 |
begin |
| 1211 |
Result:= CryptFunc(FS_CRYPT_DELETE_PASSWORD, ConnectionName, Password) = FS_FILE_OK; |
| 1212 |
end; |
| 1213 |
|
| 1214 |
{ TConnection } |
| 1215 |
|
| 1216 |
procedure TConnection.Assign(Connection: TConnection); |
| 1217 |
begin |
| 1218 |
Path:= Connection.Path; |
| 1219 |
Host:= Connection.Host; |
| 1220 |
Port:= Connection.Port; |
| 1221 |
Proxy:= Connection.Proxy; |
| 1222 |
AutoTLS:= Connection.AutoTLS; |
| 1223 |
FullSSL:= Connection.FullSSL; |
| 1224 |
OpenSSH:= Connection.OpenSSH; |
| 1225 |
CopySCP:= Connection.CopySCP; |
| 1226 |
OnlySCP:= Connection.OnlySCP; |
| 1227 |
AgentSSH:= Connection.AgentSSH; |
| 1228 |
UserName:= Connection.UserName; |
| 1229 |
Password:= Connection.Password; |
| 1230 |
Encoding:= Connection.Encoding; |
| 1231 |
PublicKey:= Connection.PublicKey; |
| 1232 |
PrivateKey:= Connection.PrivateKey; |
| 1233 |
Fingerprint:= Connection.Fingerprint; |
| 1234 |
PassiveMode:= Connection.PassiveMode; |
| 1235 |
UseAllocate:= Connection.UseAllocate; |
| 1236 |
InitCommands:= Connection.InitCommands; |
| 1237 |
MasterPassword:= Connection.MasterPassword; |
| 1238 |
ConnectionName:= Connection.ConnectionName; |
| 1239 |
PasswordChanged:= Connection.PasswordChanged; |
| 1240 |
ShowHiddenItems:= Connection.ShowHiddenItems; |
| 1241 |
KeepAliveTransfer:= Connection.KeepAliveTransfer; |
| 1242 |
end; |
| 1243 |
|
| 1244 |
initialization |
| 1245 |
ListLock := syncobjs.TCriticalSection.Create; |
| 1246 |
|
| 1247 |
finalization |
| 1248 |
FreeAndNil(ListLock); |
| 1249 |
|
| 1250 |
end. |