use of java.nio.file.attribute.UserPrincipal in project alluxio by Alluxio.
the class FileUtils method changeLocalFileUser.
/**
* Changes the local file's user.
*
* @param path that will change owner
* @param user the new user
* @throws IOException if the group is unable to be changed
*/
public static void changeLocalFileUser(String path, String user) throws IOException {
UserPrincipalLookupService lookupService = FileSystems.getDefault().getUserPrincipalLookupService();
PosixFileAttributeView view = Files.getFileAttributeView(Paths.get(path), PosixFileAttributeView.class, LinkOption.NOFOLLOW_LINKS);
UserPrincipal userPrincipal = lookupService.lookupPrincipalByName(user);
view.setOwner(userPrincipal);
}
use of java.nio.file.attribute.UserPrincipal in project java-chassis by ServiceComb.
the class FortifyUtils method isInSecureDir.
public static boolean isInSecureDir(Path file, UserPrincipal user, int symlinkDepth) {
if (!file.isAbsolute()) {
file = file.toAbsolutePath();
}
if (symlinkDepth <= 0) {
// Too many levels of symbolic links
return false;
}
// Get UserPrincipal for specified user and superuser
Path fileRoot = file.getRoot();
if (fileRoot == null) {
return false;
}
FileSystem fileSystem = Paths.get(fileRoot.toString()).getFileSystem();
UserPrincipalLookupService upls = fileSystem.getUserPrincipalLookupService();
UserPrincipal root = null;
try {
if (isPosix()) {
root = upls.lookupPrincipalByName("root");
} else {
root = upls.lookupPrincipalByName("Administrators");
}
if (user == null) {
user = upls.lookupPrincipalByName(System.getProperty("user.name"));
}
if (root == null || user == null) {
return false;
}
} catch (IOException x) {
return false;
}
// dir is not secure
for (int i = 1; i <= file.getNameCount(); i++) {
Path fRoot = file.getRoot();
if (fRoot == null) {
return false;
}
Path partialPath = Paths.get(fRoot.toString(), file.subpath(0, i).toString());
try {
if (Files.isSymbolicLink(partialPath)) {
if (!isInSecureDir(Files.readSymbolicLink(partialPath), user, symlinkDepth - 1)) {
// Symbolic link, linked-to dir not secure
return false;
}
} else {
UserPrincipal owner = Files.getOwner(partialPath);
if (!user.equals(owner) && !root.equals(owner)) {
// dir owned by someone else, not secure
return false;
}
}
} catch (IOException x) {
return false;
}
}
return true;
}
use of java.nio.file.attribute.UserPrincipal in project java-chassis by ServiceComb.
the class TestFortifyUtils method testIsInSecureDirSymLink.
@Test
public void testIsInSecureDirSymLink() {
Path file = new File("src/test/resources/config/test.1.properties").toPath();
UserPrincipal user = null;
int symlinkDepth = 0;
FortifyUtils.isInSecureDir(file, user, symlinkDepth);
Assert.assertNotEquals(true, FortifyUtils.isInSecureDir(file, user, symlinkDepth));
}
use of java.nio.file.attribute.UserPrincipal in project processdash by dtuma.
the class WhoAmI method identifyUserFromFileIO.
private void identifyUserFromFileIO() {
try {
File f = File.createTempFile("whoami", ".tmp");
Path path = Paths.get(f.getAbsolutePath());
FileOwnerAttributeView ownerAttributeView = Files.getFileAttributeView(path, FileOwnerAttributeView.class);
UserPrincipal owner = ownerAttributeView.getOwner();
this.username = discardDomain(owner.getName());
f.delete();
logger.info("From NIO, current user is " + username);
} catch (Throwable t) {
// this will fail on Java 1.6. Try the next option
}
}
Aggregations