Browse Source

Updating code to PuTTY 109df9f4

Source commit: 551deb1bf65b34cf4c505b174ad9cf8008e3b369
Martin Prikryl 6 years ago
parent
commit
e8993e53c9

+ 1 - 10
source/core/PuttyIntf.cpp

@@ -224,15 +224,6 @@ char * get_ttymode(Frontend * /*frontend*/, const char * /*mode*/)
   return NULL;
 }
 //---------------------------------------------------------------------------
-void logevent(Frontend * frontend, const char * string)
-{
-  // Frontend maybe NULL here
-  if (frontend != NULL)
-  {
-    ((TSecureShell *)frontend)->PuttyLogEvent(string);
-  }
-}
-//---------------------------------------------------------------------------
 void connection_fatal(Frontend * frontend, const char * fmt, ...)
 {
   va_list Param;
@@ -333,7 +324,7 @@ void cleanup_exit(int /*code*/)
   throw ESshFatal(NULL, "");
 }
 //---------------------------------------------------------------------------
-int askappend(Frontend * /*frontend*/, Filename * /*filename*/,
+int askappend(LogPolicy *, Filename * /*filename*/,
   void (*/*callback*/)(void * ctx, int result), void * /*ctx*/)
 {
   // this is called from logging.c of putty, which is never used with WinSCP

+ 32 - 1
source/core/SecureShell.cpp

@@ -30,6 +30,11 @@ struct TPuttyTranslation
   UnicodeString HelpKeyword;
 };
 //---------------------------------------------------------------------------
+struct ScpLogPolicy : public LogPolicy
+{
+  TSecureShell * SecureShell;
+};
+//---------------------------------------------------------------------------
 __fastcall TSecureShell::TSecureShell(TSessionUI* UI,
   TSessionData * SessionData, TSessionLog * Log, TConfiguration * Configuration)
 {
@@ -44,6 +49,8 @@ __fastcall TSecureShell::TSecureShell(TSessionUI* UI,
   OutPtr = NULL;
   Pending = NULL;
   FBackendHandle = NULL;
+  FLogPolicy = NULL;
+  FLogCtx = NULL;
   ResetConnection();
   FOnCaptureOutput = NULL;
   FOnReceive = NULL;
@@ -84,6 +91,13 @@ void __fastcall TSecureShell::ResetConnection()
   FStoredPasswordTried = false;
   FStoredPasswordTriedForKI = false;
   FStoredPassphraseTried = false;
+  delete FLogPolicy;
+  FLogPolicy = NULL;
+  if (FLogCtx != NULL)
+  {
+    log_free(FLogCtx);
+  }
+  FLogCtx = NULL;
 }
 //---------------------------------------------------------------------------
 void __fastcall TSecureShell::ResetSessionInfo()
@@ -386,6 +400,18 @@ Conf * __fastcall TSecureShell::StoreToConfig(TSessionData * Data, bool Simple)
   return conf;
 }
 //---------------------------------------------------------------------------
+static void eventlog(LogPolicy * ALogPolicy, const char * string)
+{
+  static_cast<ScpLogPolicy *>(ALogPolicy)->SecureShell->PuttyLogEvent(string);
+}
+//---------------------------------------------------------------------------
+static const LogPolicyVtable ScpLogPolicyVTable =
+  {
+    eventlog,
+    NULL, // Should never be called
+    NULL, // Should never be called
+  };
+//---------------------------------------------------------------------------
 void __fastcall TSecureShell::Open()
 {
   ResetConnection();
@@ -410,9 +436,14 @@ void __fastcall TSecureShell::Open()
     const char * InitError;
     Conf * conf = StoreToConfig(FSessionData, Simple);
     FSendBuf = FSessionData->SendBuf;
+    FLogPolicy = new ScpLogPolicy();
+    FLogPolicy->vt = &ScpLogPolicyVTable;
+    FLogPolicy->SecureShell = this;
     try
     {
-      InitError = backend_init(&ssh_backend, reinterpret_cast<Frontend *>(this), &FBackendHandle, conf,
+      Frontend * AFrontend = reinterpret_cast<Frontend *>(this);
+      FLogCtx = log_init(FLogPolicy, conf, AFrontend);
+      InitError = backend_init(&ssh_backend, AFrontend, &FBackendHandle, FLogCtx, conf,
         AnsiString(FSessionData->HostNameExpanded).c_str(), FSessionData->PortNumber, &RealHost,
         (FSessionData->TcpNoDelay ? 1 : 0),
         conf_get_int(conf, CONF_tcp_keepalives));

+ 4 - 0
source/core/SecureShell.h

@@ -20,6 +20,8 @@ typedef std::set<SOCKET> TSockets;
 struct TPuttyTranslation;
 struct callback_set;
 enum TSshImplementation { sshiUnknown, sshiOpenSSH, sshiProFTPD, sshiBitvise, sshiTitan, sshiOpenVMS, sshiCerberus };
+struct ScpLogPolicy;
+struct LogContext;
 //---------------------------------------------------------------------------
 class TSecureShell
 {
@@ -70,6 +72,8 @@ private:
   DWORD FLastSendBufferUpdate;
   int FSendBuf;
   std::auto_ptr<callback_set> FCallbackSet;
+  ScpLogPolicy * FLogPolicy;
+  LogContext * FLogCtx;
 
   void __fastcall Init();
   void __fastcall SetActive(bool value);

+ 9 - 1
source/putty/logging.c

@@ -20,6 +20,7 @@ struct LogContext {
     LogPolicy *lp;
     Conf *conf;
     int logtype;		       /* cached out of conf */
+    Frontend *frontend; // WINSCP
 };
 
 static Filename *xlatlognam(Filename *s, char *hostname, int port,
@@ -365,7 +366,7 @@ void log_packet(LogContext *ctx, int direction, int type,
     logflush(ctx);
 }
 
-LogContext *log_init(LogPolicy *lp, Conf *conf)
+LogContext *log_init(LogPolicy *lp, Conf *conf, Frontend* frontend) // WINSCP
 {
     LogContext *ctx = snew(LogContext);
     ctx->lgfp = NULL;
@@ -374,10 +375,17 @@ LogContext *log_init(LogPolicy *lp, Conf *conf)
     ctx->conf = conf_copy(conf);
     ctx->logtype = conf_get_int(ctx->conf, CONF_logtype);
     ctx->currlogfilename = NULL;
+    ctx->frontend = frontend;
     bufchain_init(&ctx->queue);
     return ctx;
 }
 
+// WINSCP
+Frontend *log_get_frontend(LogContext *ctx)
+{
+    return ctx->frontend;
+}
+
 void log_free(LogContext *ctx)
 {
     logfclose(ctx);

+ 4 - 4
source/putty/portfwd.c

@@ -1071,18 +1071,18 @@ int is_pfwd(Plug * plug)
 
 Frontend * get_pfwd_frontend(Plug * plug)
 {
-  Ssh * ssh = NULL;
+  Frontend * frontend = NULL;
   if (plug->vt->closing == pfl_closing)
   {
     struct PortListener *pl = container_of(plug, struct PortListener, plug);
-    ssh = pl->cl->frontend;
+    frontend = log_get_frontend(pl->cl->logctx);
   }
   else if (plug->vt->closing == pfd_closing)
   {
     struct PortForwarding *pf = container_of(plug, struct PortForwarding, plug);
-    ssh = pf->cl->frontend;
+    frontend = log_get_frontend(pf->cl->logctx);
   }
-  return ssh;
+  return frontend;
 }
 
 #endif

+ 2 - 1
source/putty/putty.h

@@ -1215,7 +1215,7 @@ struct LogPolicy {
 #define lp_askappend(lp, fn, cb, ctx) ((lp)->vt->askappend(lp, fn, cb, ctx))
 #define lp_logging_error(lp, event) ((lp)->vt->logging_error(lp, event))
 
-LogContext *log_init(LogPolicy *lp, Conf *conf);
+LogContext *log_init(LogPolicy *lp, Conf *conf, Frontend* frontend); // WINSCP
 void log_free(LogContext *logctx);
 void log_reconfig(LogContext *logctx, Conf *conf);
 void logfopen(LogContext *logctx);
@@ -1710,6 +1710,7 @@ int toplevel_callback_pending(CALLBACK_SET_ONLY);
 struct callback_set * get_callback_set(Plug * plug);
 struct callback_set * get_frontend_callback_set(Frontend * frontend);
 void delete_callbacks_for_context(CALLBACK_SET void *ctx);
+Frontend *log_get_frontend(LogContext *ctx); // WINSCP
 
 /*
  * Another facility in callback.c deals with 'idempotent' callbacks,

+ 1 - 1
source/putty/ssh1connection.c

@@ -904,7 +904,7 @@ static void ssh1_channel_destroy(struct ssh1_channel *c)
      * toplevel callback, just in case anything on the current call
      * stack objects to this entire PPL being freed.
      */
-    queue_toplevel_callback(s->cl.frontend, ssh1_check_termination_callback, s); // WINSCP
+    queue_toplevel_callback(log_get_frontend(s->cl.logctx), ssh1_check_termination_callback, s); // WINSCP
 }
 
 static int ssh1_check_termination(struct ssh1_connection_state *s)

+ 3 - 2
source/putty/ssh2connection.c

@@ -1559,7 +1559,7 @@ static void ssh2_channel_destroy(struct ssh2_channel *c)
      * toplevel callback, just in case anything on the current call
      * stack objects to this entire PPL being freed.
      */
-    queue_toplevel_callback(s->cl.frontend, ssh2_check_termination_callback, s); // WINSCP
+    queue_toplevel_callback(log_get_frontend(s->cl.logctx), ssh2_check_termination_callback, s); // WINSCP
 }
 
 static void ssh2_check_termination(struct ssh2_connection_state *s)
@@ -2111,7 +2111,8 @@ static void ssh2_sharing_no_more_downstreams(ConnectionLayer *cl)
 {
     struct ssh2_connection_state *s =
         container_of(cl, struct ssh2_connection_state, cl);
-    queue_toplevel_callback(get_frontend_callback_set(cl->frontend), ssh2_check_termination_callback, s);
+    // WINSCP
+    queue_toplevel_callback(get_frontend_callback_set(log_get_frontend(cl->logctx)), ssh2_check_termination_callback, s);
 }
 
 static struct X11FakeAuth *ssh2_add_sharing_x11_display(

+ 6 - 5
source/putty/sshcommon.c

@@ -739,21 +739,22 @@ static void ssh_bpp_output_packet_callback(void *context)
 
 void ssh_bpp_common_setup(BinaryPacketProtocol *bpp)
 {
-    pq_in_init(&bpp->in_pq, bpp->frontend); // WINSCP
-    pq_out_init(&bpp->out_pq, bpp->frontend); // WINSCP
+    pq_in_init(&bpp->in_pq, log_get_frontend(bpp->logctx)); // WINSCP
+    pq_out_init(&bpp->out_pq, log_get_frontend(bpp->logctx)); // WINSCP
     bpp->input_eof = FALSE;
     bpp->ic_in_raw.fn = ssh_bpp_input_raw_data_callback;
-    bpp->ic_in_raw.set = get_frontend_callback_set(bpp->frontend);
+    bpp->ic_in_raw.set = get_frontend_callback_set(log_get_frontend(bpp->logctx));
     bpp->ic_in_raw.ctx = bpp;
     bpp->ic_out_pq.fn = ssh_bpp_output_packet_callback;
-    bpp->ic_out_pq.set = get_frontend_callback_set(bpp->frontend);
+    bpp->ic_out_pq.set = get_frontend_callback_set(log_get_frontend(bpp->logctx));
     bpp->ic_out_pq.ctx = bpp;
     bpp->out_pq.pqb.ic = &bpp->ic_out_pq;
 }
 
 void ssh_bpp_free(BinaryPacketProtocol *bpp)
 {
-    delete_callbacks_for_context(get_frontend_callback_set(bpp->frontend), bpp);
+    // WINSCP
+    delete_callbacks_for_context(get_frontend_callback_set(log_get_frontend(bpp->logctx)), bpp);
     bpp->vt->free(bpp);
 }
 

+ 0 - 1
source/putty/sshverstring.c

@@ -88,7 +88,6 @@ BinaryPacketProtocol *ssh_verstring_new(
 
     s->conf = conf_copy(conf);
     s->bpp.logctx = logctx;
-    s->bpp.frontend = frontend;
     s->our_protoversion = dupstr(protoversion);
     s->receiver = rcv;