浏览代码

Bug 2102: Reading password from a file in scripting

https://winscp.net/tracker/2102

Source commit: 800bece84b818b09f26abf28e2ccc2d8700b3d26
Martin Prikryl 3 年之前
父节点
当前提交
93a1a15195
共有 5 个文件被更改,包括 36 次插入3 次删除
  1. 6 1
      source/core/Script.cpp
  2. 26 0
      source/core/SessionData.cpp
  3. 1 0
      source/core/SessionData.h
  4. 1 1
      source/resource/TextsCore.h
  5. 2 1
      source/resource/TextsCore2.rc

+ 6 - 1
source/core/Script.cpp

@@ -2180,7 +2180,7 @@ __fastcall TManagementScript::TManagementScript(TStoredSessionList * StoredSessi
 
   FCommands->Register(L"exit", SCRIPT_EXIT_DESC, SCRIPT_EXIT_HELP, &ExitProc, 0, 0, false);
   FCommands->Register(L"bye", 0, SCRIPT_EXIT_HELP, &ExitProc, 0, 0, false);
-  FCommands->Register(L"open", SCRIPT_OPEN_DESC, SCRIPT_OPEN_HELP10, &OpenProc, 0, -1, true);
+  FCommands->Register(L"open", SCRIPT_OPEN_DESC, SCRIPT_OPEN_HELP11, &OpenProc, 0, -1, true);
   FCommands->Register(L"close", SCRIPT_CLOSE_DESC, SCRIPT_CLOSE_HELP, &CloseProc, 0, 1, false);
   FCommands->Register(L"session", SCRIPT_SESSION_DESC, SCRIPT_SESSION_HELP, &SessionProc, 0, 1, false);
   FCommands->Register(L"lpwd", SCRIPT_LPWD_DESC, SCRIPT_LPWD_HELP, &LPwdProc, 0, 0, false);
@@ -2715,6 +2715,11 @@ void __fastcall TManagementScript::Connect(const UnicodeString Session,
 
     try
     {
+      if (Options->FindSwitch(L"passwordsfromfiles"))
+      {
+        Data->ReadPasswordsFromFiles();
+      }
+
       if (CheckParams)
       {
         if (Options->ParamCount > 1)

+ 26 - 0
source/core/SessionData.cpp

@@ -1811,6 +1811,32 @@ void __fastcall TSessionData::RecryptPasswords()
   EncryptKey = EncryptKey;
 }
 //---------------------------------------------------------------------
+static UnicodeString ReadPasswordFromFile(const UnicodeString & FileName)
+{
+  UnicodeString Result = FileName;
+  if (!Result.IsEmpty())
+  {
+    std::unique_ptr<TStrings> Lines(new TStringList());
+    LoadScriptFromFile(Result, Lines.get());
+    if (Lines->Count > 0)
+    {
+      Result = Lines->Strings[0];
+    }
+  }
+  return Result;
+}
+//---------------------------------------------------------------------
+void TSessionData::ReadPasswordsFromFiles()
+{
+  Password = ReadPasswordFromFile(Password);
+  NewPassword = ReadPasswordFromFile(NewPassword);
+  ProxyPassword = ReadPasswordFromFile(ProxyPassword);
+  TunnelPassword = ReadPasswordFromFile(TunnelPassword);
+  TunnelPassphrase = ReadPasswordFromFile(TunnelPassphrase);
+  Passphrase = ReadPasswordFromFile(Passphrase);
+  EncryptKey = ReadPasswordFromFile(EncryptKey);
+}
+//---------------------------------------------------------------------
 bool __fastcall TSessionData::HasPassword()
 {
   return !FPassword.IsEmpty();

+ 1 - 0
source/core/SessionData.h

@@ -498,6 +498,7 @@ public:
     const TSessionData * Default = NULL);
   void __fastcall SaveRecryptedPasswords(THierarchicalStorage * Storage);
   void __fastcall RecryptPasswords();
+  void ReadPasswordsFromFiles();
   bool __fastcall HasPassword();
   bool __fastcall HasAnySessionPassword();
   bool __fastcall HasAnyPassword();

+ 1 - 1
source/resource/TextsCore.h

@@ -7,7 +7,7 @@
 
 #define SCRIPT_HELP_HELP        4
 #define SCRIPT_EXIT_HELP        5
-#define SCRIPT_OPEN_HELP10      6
+#define SCRIPT_OPEN_HELP11      6
 #define SCRIPT_CLOSE_HELP       7
 #define SCRIPT_SESSION_HELP     8
 #define SCRIPT_PWD_HELP         9

+ 2 - 1
source/resource/TextsCore2.rc

@@ -61,7 +61,7 @@ BEGIN
     "  Closes all sessions and terminates the program.\n"
     "alias:\n"
     "  bye\n"
-  SCRIPT_OPEN_HELP10,
+  SCRIPT_OPEN_HELP11,
     "open <site>\n"
     "open sftp|scp|ftp[es]|dav[s]|s3 :// [ <user> [ :password ] @ ] <host> [ :<port> ]\n"
     "  Establishes connection to given host. Use either name of the site or\n"
@@ -84,6 +84,7 @@ BEGIN
     "                     as in an INI file\n"
     "  -filezilla         Load <site> from FileZilla site manager\n"
     "  -newpassword=<password> Changes password to <password>\n"
+    "  -passwordsfromfiles Read all passwords from files\n"
     "examples:\n"
     "  open\n"
     "  open sftp://[email protected]:2222 -privatekey=mykey.ppk\n"