Page 1 of 1

SymbolicLink Example

Posted: Mon 08 Feb 2021 01:45
by mbillig02
Does anyone have some example code for using OnReadSymbolicLink?

Re: SymbolicLink Example

Posted: Wed 10 Feb 2021 15:11
by ViktorV
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,

Re: SymbolicLink Example

Posted: Sun 14 Feb 2021 20:25
by mbillig02
Thank you.

Re: SymbolicLink Example

Posted: Tue 16 Feb 2021 17:07
by mbillig02
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?

Re: SymbolicLink Example

Posted: Fri 19 Feb 2021 09:07
by ViktorV
What do you mean by virtual mount points? We advise users on using the SFTP protocol, but not on working with a file system.

Re: SymbolicLink Example

Posted: Fri 19 Feb 2021 12:20
by mbillig02
The ability to add an additional home path for the remote user to access.

Re: SymbolicLink Example

Posted: Fri 19 Feb 2021 18:50
by ViktorV
You can use the TScUser.HomePath property to specify a user home path. If you're looking for something else, let us know.

Re: SymbolicLink Example

Posted: Fri 19 Feb 2021 20:40
by mbillig02
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.

Re: SymbolicLink Example

Posted: Mon 22 Feb 2021 17:26
by ViktorV
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.

Re: SymbolicLink Example

Posted: Fri 26 Feb 2021 23:30
by mbillig02
Thank you.

Re: SymbolicLink Example

Posted: Wed 03 Mar 2021 11:27
by ViktorV
Thank you for interest to our product.
Feel free to contact us if you have any further questions about our products.