Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Checking SFTP permissions using Jsch

Tags:

java

ssh

sftp

jsch

I'm currently implementing a SFTP client using Jsch.

For this client I need to check the permissions the logged in user has on the SFTP server in order to check if user is able to perform certain operations. Unfortunately I can't seem to find any documentation or examples in which is shown how to to this.

Thanks in advance.

like image 649
PascalV Avatar asked Oct 23 '22 14:10

PascalV


1 Answers

This code will do what you want:

ChannelSftp channel = (ChannelSftp)session.openChannel("sftp");
SftpATTRS attrs = channel.lstat(fileOnServer)
boolean userHasPermissionsToWriteFile = attrs != null && ((attrs.getPermissions() & 00200) != 0) && attrs.getUId() != 0; 

Where

attrs.getUId() != 0

checks that user is not root

like image 149
Dmitry Trofimov Avatar answered Nov 13 '22 04:11

Dmitry Trofimov