SSHClient Connect Fails

Discussion of open issues, suggestions and bugs regarding network security and data protection solution - SecureBridge
Post Reply
MRoth
Posts: 14
Joined: Thu 30 Sep 2021 01:06

SSHClient Connect Fails

Post by MRoth » Mon 11 Oct 2021 02:25

Hello,

We are implementing the SSHClient and SSHChannel into our application. We currently use an old SSClient that is no longer supported so we switched to your Securebridge components.

We added the below code and it will not connect to the SSHServer we are currently testing with. We can connect to our test SSHServer using Putty and our old component. I have images of the connection using putty and our old component but could not insert them.

Here is the code we added:

Code: Select all

const
  maxrecords=10;
  
type
  tsshcsocket=class(TScSSHChannel)
  public
    devs:integer;
    port:integer;
    procedure ChannelSocketAsyncerror(Sender:tobject; E:Exception);
    procedure ChannelSocketAsyncRead(Sender:tobject);
  end;
  tsshcclient=class(TScSSHClient)
  public
    devs:integer;
    port:integer;
    procedure ClientKeyValidation(Sender:tobject; Newserverkey:TSckey; var Accept:boolean);
  end;
  
 var 
    ssclint:array[1..maxrecords] of tsshcclient;
    ssteln:array[1..maxrecords] of  tsshcsocket;
    ssfiles:TScFileStorage;

function createssclient(whdev,whindx:longint):integer;
var tcpipdtb_rec:dtbdefrec;
begin
  with portvars^ do
  begin
    init_dtb_rec(tcpipdtb_rec);
    read_dtb_rec(tcpipdtb_rec,portvars^.indxmap[whindx]);
    createssclient:=create_sstelnet_socket(tcpipdtb_rec,portvars^.ip_strs[whdev]);
  end;
end;

function create_sstelnet_socket(whportdtb:dtbdefrec; ipprt:string):integer;
var crteans:integer; ipstr:string;
begin
  with portvars^ do
  begin
    ipstr:=ipprt;
    delete(ipstr,1,6);
    if ssfiles=nil then ssfiles:=TScFileStorage.create(nil);
    if ssfiles<>nil then
    begin
      ssfiles.Path:=wavepath;
      ssclint[whportdtb.prtid]:=tsshcclient.Create(nil);
      if ssclint[whportdtb.prtid]<>nil then
      begin
        ssclint[whportdtb.prtid].KeyStorage:=ssfiles;
        ssclint[whportdtb.prtid].HostKeyAlgorithms.AsString:='ssh-rsa,ssh-dss';
        ssclint[whportdtb.prtid].Authentication:=atpassword;
        ssclint[whportdtb.prtid].HostName:=get_str_item(ipstr,1,';');
        ssclint[whportdtb.prtid].Port:=strtoint(get_str_item(ipstr,2,';'));
        ssclint[whportdtb.prtid].User:=get_str_item(ipstr,3,';');
        ssclint[whportdtb.prtid].Password:=get_str_item(ipstr,4,';');
        //
        ssclint[whportdtb.prtid].OnServerKeyValidate:=ssclint[whportdtb.prtid].ClientKeyValidation;
        //
        debuginfo(wavepath,'  Create SSHChannel',0);
        sstelnt[whportdtb.prtid]:=tsshcsocket.Create(nil);
        if (ssfiles<>nil) and (ssclint[whportdtb.prtid]<>nil) and (sstelnt[whportdtb.prtid]<>nil) then
        begin
          sstelnt[whportdtb.prtid].onasyncerror:=sstelnt[whportdtb.prtid].ChannelSocketAsyncerror;
          sstelnt[whportdtb.prtid].onasyncreceive:=sstelnt[whportdtb.prtid].ChannelSocketAsyncRead;
          //
          sstelnt[whportdtb.prtid].Client:=ssclint[whportdtb.prtid];
          sstelnt[whportdtb.prtid].Direct:=false;
          crteans:=0;
          try
            if initmsg.Visible then
            begin
              inc(portset);
              initmsg.prtset.caption:=inttostr(portset);
              initmsg.prtset.update;
              inc(portpass);
              initmsg.prtpass.caption:=inttostr(portpass);
              initmsg.prtpass.update;
            end;
          except
          end;
        end else
        begin
          crteans:=1;
        end;
      end else
      begin
        crteans:=1;
      end;
    end else
    begin
      crteans:=1;
      debuginfo(wavepath,'  File Storage Not Created',0);
    end;
    if crteans=1 then
    begin
      if portok=0 then
      begin
        portok:=2;
        init_lstr:='Error: Connecting to Port '+decode_portid(whportdtb.prtid)+' at SSTelnet Client '+ipstr+':'+#13+#13+
          'Use the Device Configuration Dialog Inside the Device Manager''s Menu to Correct the Configuration.';
        writeto_opencomm_dtb('SSHTelnet Client at Port '+decode_portid(whportdtb.prtid)+' for IP address: '+ipstr+'  Error:Connecting-C');
      end;
    end;
  end;
  create_sstelnet_socket:=crteans;
end;
  find_sstelnetsocket:=scktfnd;
end;

Procedure Connect_to_SSClient;
begin
     if (portvars^.sstelnt[whport]<>nil) then
     begin
       if sstelnt[whport].connected then sstelnt[whport].Disconnect;
       freeandnil(sstelnt[whport]);
       sstelnt[whport]:=nil;
     end;
     if (portvars^.ssclint[whport]<>nil) then
     begin
       if ssclint[whport].Connected then ssclint[whport].Disconnect;
       freeandnil(ssclint[whport]);
       ssclint[whport]:=nil;
     end;
     if ssfiles=nil then ssfiles:=TScFileStorage.create(nil);
     if ssfiles<>nil then
     begin
       ssfiles.Path:=wavepath;
       connans:=createssclient(whport,whdev);
       if connans=0 then
       begin
         try
           ssclint[whport].Connect;  //It constantly fails when trying to connect to the SSHServer
         except showmessage('Connection to SSHClient Failed'); end;
         if ssclint[whport].Connected then
         begin
           with sstelnt[whport] do
           begin
             try
               set_varval('IPCONNECT',whdev,'NO',0);
               connect;
               if connected then
               begin
                 set_ethernet_connect(whdev);
                 connans:=0;
               end;
             except  showmessage('Connection to SSHChannel Failed'); end;
           end;
         end;
       end;
     end;
   end;
end;

function find_sstelnetsocket(hoststr:string; hostprt:integer):integer;
var ert,scktfnd:integer;
begin
  scktfnd:=-1;
  with portvars^ do
  begin
    ert:=0;
    while ((ert<=maxrecords) and (scktfnd=-1)) do
    begin
      try
        if ((ssclint[ert]<>nil) and (hoststr=ssclint[ert].HostName) and (hostprt=ssclint[ert].Port)) then
        begin
          scktfnd:=ert;
        end;
      except
        on e:exception do scktfnd:=-1;
      end;
      ert:=ert+1;
   end;
 end;

procedure tsshcsocket.ChannelSocketAsyncerror(Sender:tobject; E:Exception);
var fnddev,ssd,azm:integer;
begin
  fnddev:=find_sstelnetsocket(TScSSHChannel(sender).client.HostName,TScSSHChannel(sender).client.Port);
  if (fnddev>-1) then debuginfo(wavepath,'Found SSHChannel - dev = '+inttostr(fnddev),0);
end;

procedure tsshcsocket.ChannelSocketAsyncRead(Sender:tobject);
var fnddev,ssd,azm:integer; et_txtbuf:wrkrchr; indata:tbytes; movechar:dword;
begin
  with portvars^ do
  begin
    fnddev:=find_sstelnetsocket(TScSSHChannel(sender).client.HostName,TScSSHChannel(sender).client.Port);
    if (fnddev>-1) and (TScSSHChannel(sender).InCount>0) then
    begin
      with sender as TScSSHChannel do
      begin
        setlength(indata,portvars^.sstelnt[devword[ithport]].InCount);
        movechar:=portvars^.sstelnt[devword[ithport]].ReadBuffer(indata,0,portvars^.sstelnt[devword[ithport]].InCount);
        if movechar>0 then
        begin
          azm:=0;
          for ssd:=1 to movechar do
          begin
            et_txtbuf[azm]:=char(indata[ssd]);
            inc(azm);
            if ((ssd=movechar) or (azm>1022)) then
            begin
              log_et_entry(portvars^.prtsarr[fnddev],et_txtbuf,azm);
              azm:=0;
            end;
          end;
        end;
      end;
    end;
  end;
end;

procedure tsshcclient.ClientKeyValidation(Sender:tobject; Newserverkey:TSckey; var Accept:boolean);
begin
  Accept:=true;  //This call back procedure is never called
end;
Please any help will be greatly appreciated. We are currently on a deadline.

Thanks for all your help.

YanishevskiyVI
Devart Team
Posts: 70
Joined: Wed 02 Jun 2021 09:30

Re: SSHClient Connect Fails

Post by YanishevskiyVI » Wed 13 Oct 2021 15:23

Hi there!
Thank you for your request!

Please note that in order to reproduce and investigate your issue, we need you to specify the exact exception you are getting!

Looking forward to your reply!

MRoth
Posts: 14
Joined: Thu 30 Sep 2021 01:06

Re: SSHClient Connect Fails

Post by MRoth » Wed 13 Oct 2021 18:53

Hello,

We are not getting any exception errors.

It just will not connect and the ClientKeyValidation call back function never gets called.

We can connect just fine using Putty and our old SSHClient component.

Is there any logging we can turn on to see why it will not connect.

Thanks.

YanishevskiyVI
Devart Team
Posts: 70
Joined: Wed 02 Jun 2021 09:30

Re: SSHClient Connect Fails

Post by YanishevskiyVI » Wed 20 Oct 2021 12:23

Hi there,

Since exception is handled by except clause, error messages are not shown.

In order to show an exception please add raise operator at the end of your except clause or modify as follow:

Code: Select all

    except on e: exception do showmessage(e.Message) end;
Best regards,
Vitaliy

Post Reply