Browse Source

Bug 2107: Private key pattern in PuTTY command-line

https://winscp.net/tracker/2107

Source commit: 9131c33ed478233657c25df035764a7cd1f03e4c
Martin Prikryl 3 years ago
parent
commit
070ff9ad32

+ 8 - 0
source/core/FileMasks.cpp

@@ -1298,6 +1298,7 @@ int __fastcall TFileCustomCommand::PatternLen(const UnicodeString & Command, int
     case L'@':
     case L'u':
     case L'p':
+    case L'k':
     case L'#':
     case L'/':
     case L'&':
@@ -1359,6 +1360,13 @@ bool __fastcall TFileCustomCommand::PatternReplacement(
       Replacement = IntToStr(FData.SessionData->PortNumber);
     }
   }
+  else if (SameText(Pattern, L"!k"))
+  {
+    if (FData.SessionData != NULL)
+    {
+      Replacement = FData.SessionData->ResolvePublicKeyFile();
+    }
+  }
   else if (Pattern == L"!/")
   {
     Replacement = UnixIncludeTrailingBackslash(FPath);

+ 1 - 5
source/core/SecureShell.cpp

@@ -245,11 +245,7 @@ Conf * __fastcall TSecureShell::StoreToConfig(TSessionData * Data, bool Simple)
   conf_set_filename(conf, CONF_ssh_gss_custom, GssLibCustomFileName);
   filename_free(GssLibCustomFileName);
 
-  UnicodeString SPublicKeyFile = Data->PublicKeyFile;
-  if (SPublicKeyFile.IsEmpty()) SPublicKeyFile = Configuration->DefaultKeyFile;
-  // StripPathQuotes should not be needed as we do not feed quotes anymore
-  SPublicKeyFile = StripPathQuotes(ExpandEnvironmentVariables(SPublicKeyFile));
-  Filename * KeyFileFileName = filename_from_str(UTF8String(SPublicKeyFile).c_str());
+  Filename * KeyFileFileName = filename_from_str(UTF8String(Data->ResolvePublicKeyFile()).c_str());
   conf_set_filename(conf, CONF_keyfile, KeyFileFileName);
   filename_free(KeyFileFileName);
 

+ 12 - 0
source/core/SessionData.cpp

@@ -3139,6 +3139,18 @@ void __fastcall TSessionData::SetPublicKeyFile(UnicodeString value)
   }
 }
 //---------------------------------------------------------------------
+UnicodeString TSessionData::ResolvePublicKeyFile()
+{
+  UnicodeString Result = PublicKeyFile;
+  if (Result.IsEmpty())
+  {
+    Result = Configuration->DefaultKeyFile;
+  }
+  // StripPathQuotes should not be needed as we do not feed quotes anymore
+  Result = StripPathQuotes(::ExpandEnvironmentVariables(Result));
+  return Result;
+}
+//---------------------------------------------------------------------
 void __fastcall TSessionData::SetPassphrase(UnicodeString avalue)
 {
   RawByteString value = EncryptPassword(avalue, PublicKeyFile);

+ 1 - 0
source/core/SessionData.h

@@ -528,6 +528,7 @@ public:
   bool __fastcall HasSessionName();
   bool HasAutoCredentials();
   int GetDefaultPort();
+  UnicodeString ResolvePublicKeyFile();
 
   UnicodeString __fastcall GenerateOpenCommandArgs(bool Rtf);
   void __fastcall GenerateAssemblyCode(TAssemblyLanguage Language, UnicodeString & Head, UnicodeString & Tail, int & Indent);

+ 1 - 1
source/core/SessionInfo.cpp

@@ -1252,7 +1252,7 @@ void __fastcall TSessionLog::DoAddStartupInfo(TSessionData * Data)
     }
     ADF(L"User name: %s (Password: %s, Key file: %s, Passphrase: %s)",
       (UserName, LogSensitive(Data->Password),
-       LogSensitive(Data->PublicKeyFile), LogSensitive(Data->Passphrase)));
+       LogSensitive(Data->ResolvePublicKeyFile()), LogSensitive(Data->Passphrase)));
     if (Data->UsesSsh)
     {
       ADF(L"Tunnel: %s", (BooleanToEngStr(Data->Tunnel)));

+ 3 - 2
source/windows/GUITools.cpp

@@ -351,9 +351,10 @@ void OpenSessionInPutty(TSessionData * SessionData)
 
         if (!Telnet)
         {
-          if (!SessionData->PublicKeyFile.IsEmpty())
+          UnicodeString PublicKeyFile = SessionData->ResolvePublicKeyFile();
+          if (!PublicKeyFile.IsEmpty())
           {
-            AddToList(PuttyParams, FORMAT(L"-i \"%s\"", (SessionData->PublicKeyFile)), L" ");
+            AddToList(PuttyParams, FORMAT(L"-i \"%s\"", (PublicKeyFile)), L" ");
           }
           AddToList(PuttyParams, (SessionData->TryAgent ? L"-agent" : L"-noagent"), L" ");
           if (SessionData->TryAgent)