FTP file and folder permissions

You can access remote file or folder permissions using Limilabs .NET FTP library. First you should learn how to connect and download files from FTP server.

It is important to realize that originally FTP protocol didn’t have the concept of file/folder permissions build-in directly.

LIST command, that allowed listing contents of the remote server, was build around system listing commands: ls on UNIX/LINUX machines, dir on Windows.
ls returns file permissions divided into 3 groups: Owning user, owning group and others e.g.: rwxr–rw-.

When using Ftp.dll Ftp.List and Ftp.GetList methods with UNIX FTP server (or a server that imitates ls output for LIST command) you can access these UNIX permissions using UnixPermissions property on FtpItem object:

// C#

List<FtpItem> items = client.List();
UnixPermissionSet set = items[0].UnixPermissions;

bool ownerCanRead = (set.OwnerUser | UnixPermission.Read) != 0;
bool groupCanRead = (set.OwnerGroup | UnixPermission.Read) != 0;
bool othersCanRead = (set.Others | UnixPermission.Read) != 0;

bool ownerCanWrite = (set.OwnerUser | UnixPermission.Write) != 0;

' VB.NET

Dim items As List(Of FtpItem) = client.List()
Dim [set] As UnixPermissionSet = items(0).UnixPermissions

Dim ownerCanRead As Boolean = ([set].OwnerUser Or UnixPermission.Read) <> 0
Dim groupCanRead As Boolean = ([set].OwnerGroup Or UnixPermission.Read) <> 0
Dim othersCanRead As Boolean = ([set].Others Or UnixPermission.Read) <> 0

Dim ownerCanWrite As Boolean = ([set].OwnerUser Or UnixPermission.Write) <> 0

MLSD and perm fact

As FTP protocol evolved, MLSD command was introduced with RFC 3659.

MLSD standardized the way listings are returned from the server. It introduced facts concept, and in particular perm fact.
Ftp.GetList method uses MLSD command if it is available, that is if MLST extension is supported and advertised by the remote server. It is possible to force this command to be used by calling Ftp.MLSD method directly.

You can access parsed version of perm fact using FtpItem.Permission property:

// C#

List<FtpItem> items = client.GetList();
List<FtpPermission> permissions = items[0].Permissions;

bool canRead = permissions.Contains(FtpPermission.Read);
bool canWrite = permissions.Contains(FtpPermission.Write);
bool canCreateFile = permissions.Contains(FtpPermission.CreateFile);
bool canChangeFolder = permissions.Contains(FtpPermission.ChangeFolder);

' VB.NET

Dim items As List(Of FtpItem) = client.GetList()
Dim permissions As List(Of FtpPermission) = items(0).Permissions

Dim canRead As Boolean = permissions.Contains(FtpPermission.Read)
Dim canWrite As Boolean = permissions.Contains(FtpPermission.Write)
Dim canCreateFile As Boolean = permissions.Contains(FtpPermission.CreateFile)
Dim canChangeFolder As Boolean = permissions.Contains(FtpPermission.ChangeFolder)

As you can see Ftp permissions can contain additional information such as if you can select particular folder or create a file within.

Changing the permissions

There is no standardized way of changing the permissions. You’ll need SITE command (this command is used by the server to provide services specific to his system but not sufficiently universal to be included as commands in protocol) and CHMOD (again UNIX command).

Limilabs .NET FTP library contains useful shortcut for that – SiteChangeMode method:

// C#

client.SiteChangeMode("file.txt", new UnixPermissionSet(UnixPermission.Write));
' VB.NET

client.SiteChangeMode("file.txt", New UnixPermissionSet(UnixPermission.Write))

Note that the same UnixPermissionSet class can is used that is returned by FtpItem.UnixPermissions property

Tags:    

Questions?

Consider using our Q&A forum for asking questions.