Search in sources :

Example 16 with UserPrincipal

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);
}
Also used : UserPrincipalLookupService(java.nio.file.attribute.UserPrincipalLookupService) UserPrincipal(java.nio.file.attribute.UserPrincipal) PosixFileAttributeView(java.nio.file.attribute.PosixFileAttributeView)

Example 17 with 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;
}
Also used : Path(java.nio.file.Path) UserPrincipalLookupService(java.nio.file.attribute.UserPrincipalLookupService) FileSystem(java.nio.file.FileSystem) IOException(java.io.IOException) UserPrincipal(java.nio.file.attribute.UserPrincipal)

Example 18 with UserPrincipal

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));
}
Also used : Path(java.nio.file.Path) File(java.io.File) UserPrincipal(java.nio.file.attribute.UserPrincipal) Test(org.junit.Test)

Example 19 with UserPrincipal

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
    }
}
Also used : Path(java.nio.file.Path) FileOwnerAttributeView(java.nio.file.attribute.FileOwnerAttributeView) File(java.io.File) UserPrincipal(java.nio.file.attribute.UserPrincipal)

Aggregations

UserPrincipal (java.nio.file.attribute.UserPrincipal)19 Path (java.nio.file.Path)13 Test (org.junit.Test)7 File (java.io.File)6 IOException (java.io.IOException)4 GroupPrincipal (java.nio.file.attribute.GroupPrincipal)4 UserPrincipalLookupService (java.nio.file.attribute.UserPrincipalLookupService)4 ArrayList (java.util.ArrayList)4 FileOwnerAttributeView (java.nio.file.attribute.FileOwnerAttributeView)3 PosixFileAttributeView (java.nio.file.attribute.PosixFileAttributeView)3 UserLookupService.createUserPrincipal (com.google.common.jimfs.UserLookupService.createUserPrincipal)2 FileSystem (java.nio.file.FileSystem)2 AclEntry (java.nio.file.attribute.AclEntry)2 BasicFileAttributeView (java.nio.file.attribute.BasicFileAttributeView)2 PosixFilePermission (java.nio.file.attribute.PosixFilePermission)2 HashMap (java.util.HashMap)2 ImmutableSet (com.google.common.collect.ImmutableSet)1 UserLookupService.createGroupPrincipal (com.google.common.jimfs.UserLookupService.createGroupPrincipal)1 FileSystemException (io.vertx.core.file.FileSystemException)1 AclEntryFlag (java.nio.file.attribute.AclEntryFlag)1