SymbolicLink Example

Discussion of open issues, suggestions and bugs regarding network security and data protection solution - SecureBridge
Post Reply
mbillig02
Posts: 19
Joined: Sat 09 Feb 2019 23:16

SymbolicLink Example

Post by mbillig02 » Mon 08 Feb 2021 01:45

Does anyone have some example code for using OnReadSymbolicLink?

ViktorV
Devart Team
Posts: 3168
Joined: Wed 30 Jul 2014 07:16

Re: SymbolicLink Example

Post by ViktorV » Wed 10 Feb 2021 15:11

To test you can use our SimpleSSHServer demo project. You can find the demo project in the %SecureBridgeDemos%\SimpleSSHServer directory. %SecureBridgeDemos% is the SecureBridge Demo projects installation path on your computer.
Then you can use the following code:

Code: Select all

procedure TSSHServerFrame.ScSFTPServerReadSymbolicLink(Sender: TObject;
  SFTPSessionInfo: TScSFTPSessionInfo; const Path: string;
  out SymbolicName: string; var Error: TScSFTPError);
var
  FullPath: string;
{$IFDEF MSWINDOWS}
  IObj: IUnknown;
  ILink: IShellLink;
  IPFile: IPersistFile;
  fd: TWin32FindData;
  ws: WideString;
  pPath: PChar;
{$ENDIF}
begin
  SymbolicName := '';
  FullPath := ScSFTPServer.GetFullPath(SFTPSessionInfo, Path);
  if not FileExists(FullPath) and (ExtractFileExt(FullPath) = '') then
    FullPath := ChangeFileExt(FullPath, '.lnk');

{$IFDEF SC_USE_SYMLINK}
  if FileGetSymLinkTarget(FullPath, SymbolicName) then
    InitError(Error, erOk)
  else
{$ENDIF}
  {$IFDEF MSWINDOWS}
    if FileExists(FullPath) then begin
      InitError(Error, erFailure);
      try
        IObj := CreateComObject(CLSID_ShellLink);
        ILink := IObj as IShellLink;
        IPFile := IObj as IPersistFile;

        ws := WideString(FullPath);
        if IPFile.Load(POleStr(ws), 0) = S_OK then begin
          pPath := Marshal.AllocHGlobal(MAX_PATH * sizeof(char));
          try
            if ILink.GetPath(pPath, MAX_PATH, fd, 0) = S_OK then begin
              SymbolicName := string(pPath);
              SymbolicName := ExtractRelativePath(SFTPSessionInfo.HomePath, SymbolicName);
              InitError(Error, erOk);
              Exit;
            end;
          finally
            Marshal.FreeHGlobal(pPath);
          end;
        end;
      finally
        if Error.ErrorCode <> erOk then
          InitError(Error, erFailure, SysErrorMessage(GetLastError));
      end;
    end
    else
  {$ENDIF}
      InitError(Error, erNoSuchFile);
end;
Note, please add the following modules in the USES clause of your module:

Code: Select all

uses
{$IFDEF MSWINDOWS}
  ShlObj, ActiveX, ComObj,
{$ENDIF}
  ScSFTPUtils, ScSFTPConsts, ScCLRClasses,

mbillig02
Posts: 19
Joined: Sat 09 Feb 2019 23:16

Re: SymbolicLink Example

Post by mbillig02 » Sun 14 Feb 2021 20:25

Thank you.

mbillig02
Posts: 19
Joined: Sat 09 Feb 2019 23:16

Re: SymbolicLink Example

Post by mbillig02 » Tue 16 Feb 2021 17:07

I was hoping this would help show how to create virtual mounts for the sftp server. I do not see it. Is there a way to configure the server to use virtual mount points?

ViktorV
Devart Team
Posts: 3168
Joined: Wed 30 Jul 2014 07:16

Re: SymbolicLink Example

Post by ViktorV » Fri 19 Feb 2021 09:07

What do you mean by virtual mount points? We advise users on using the SFTP protocol, but not on working with a file system.

mbillig02
Posts: 19
Joined: Sat 09 Feb 2019 23:16

Re: SymbolicLink Example

Post by mbillig02 » Fri 19 Feb 2021 12:20

The ability to add an additional home path for the remote user to access.

ViktorV
Devart Team
Posts: 3168
Joined: Wed 30 Jul 2014 07:16

Re: SymbolicLink Example

Post by ViktorV » Fri 19 Feb 2021 18:50

You can use the TScUser.HomePath property to specify a user home path. If you're looking for something else, let us know.

mbillig02
Posts: 19
Joined: Sat 09 Feb 2019 23:16

Re: SymbolicLink Example

Post by mbillig02 » Fri 19 Feb 2021 20:40

In addition to the homepath I would like to be able to add one or more virtual mounts points that would act like directories to the remote system but point to other network or local paths. I will look through the source but I have not found where it builds the directory listing yet.

ViktorV
Devart Team
Posts: 3168
Joined: Wed 30 Jul 2014 07:16

Re: SymbolicLink Example

Post by ViktorV » Mon 22 Feb 2021 17:26

In the SFTP specification, there's no such concept as a directory or directory navigation. You should pass the full path whenever you write or read a file on the server. The TScSFTPServer.GetFullPath method receives the path from the client and returns the path that the operating system understands. You can parse and handle the path either in this method or in the OnGetFullPath handler. You can implement your own rules for specifying the path to virtual catalogs and parse the path using the GetFullPath method.

mbillig02
Posts: 19
Joined: Sat 09 Feb 2019 23:16

Re: SymbolicLink Example

Post by mbillig02 » Fri 26 Feb 2021 23:30

Thank you.

ViktorV
Devart Team
Posts: 3168
Joined: Wed 30 Jul 2014 07:16

Re: SymbolicLink Example

Post by ViktorV » Wed 03 Mar 2021 11:27

Thank you for interest to our product.
Feel free to contact us if you have any further questions about our products.

Post Reply