/// <summary>Initializes a new instance of the <see cref="FtpAccessEventArgs" /> class.</summary> /// <param name="client">The client.</param> /// <param name="accessType">Type of the access.</param> /// <param name="ftpName">Name of the FTP.</param> /// <param name="ftpFolder">The FTP folder.</param> /// <param name="fileSystemDirectory">The file system directory.</param> /// <param name="entries">The entries.</param> public FtpAccessEventArgs(FtpServerClient client, FtpAccessType accessType, string ftpName, string ftpFolder, string fileSystemDirectory, FtpDirectoryEntry[] entries) { Client = client; AccessType = accessType; FileSystemDirectory = fileSystemDirectory; FtpFolder = ftpFolder; FtpName = ftpName; Entries = entries; }
bool CheckAccess(FtpAccessType accessType, string ftpName) { bool allow = false; string fileSystemDir = null; if (ftpName.Contains("/")) { //absolute path if (ftpName == "/") { allow = true; } else { string[] parts = ftpName.Split(new char[] { '/' }, StringSplitOptions.RemoveEmptyEntries); if (Server.RootFolders.TryGetValue(parts.FirstOrDefault(), out string root)) { string ftpDir = FileSystem.Combine('/', currentFtpFolder, ftpName); if (!ftpDir.StartsWith("..")) { fileSystemDir = FileSystem.Combine(root, parts.SubRange(1).Join('/')); } } } } else { if (currentFileSystemDirectory == null) { if (Server.RootFolders.TryGetValue(ftpName, out string root)) { fileSystemDir = root; } } else { string ftpDir = FileSystem.Combine('/', currentFtpFolder, ftpName); if (ftpDir == "/") { fileSystemDir = null; allow = true; } else if (!ftpDir.StartsWith("..")) { fileSystemDir = FileSystem.Combine(currentFileSystemDirectory, ftpName); } } } if (fileSystemDir != null) { switch (accessType) { case FtpAccessType.CreateDirectory: { allow = !Directory.Exists(fileSystemDir); break; } case FtpAccessType.ChangeDirectory: case FtpAccessType.ListDirectory: case FtpAccessType.DeleteDirectory: { allow = Directory.Exists(fileSystemDir); break; } case FtpAccessType.UploadFile: { allow = true; break; } case FtpAccessType.DeleteFile: case FtpAccessType.DownloadFile: { allow = File.Exists(fileSystemDir); break; } case FtpAccessType.RenameFrom: { allow = File.Exists(fileSystemDir) || Directory.Exists(fileSystemDir); break; } case FtpAccessType.RenameTo: { allow = !File.Exists(fileSystemDir) && !Directory.Exists(fileSystemDir); break; } default: throw new NotImplementedException(); } } if (!allow) { SendAnswer("550 Requested action not taken."); return(false); } FtpAccessEventArgs e = new FtpAccessEventArgs(this, accessType, currentFileSystemDirectory, currentFtpFolder, ftpName, null); Server.OnCheckAccess(e); if (e.Denied) { SendAnswer("550 Requested action not taken. Access denied."); return(false); } return(true); }