Search in sources :

Example 6 with UserPrincipalLookupService

use of java.nio.file.attribute.UserPrincipalLookupService 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 7 with UserPrincipalLookupService

use of java.nio.file.attribute.UserPrincipalLookupService in project incubator-rya by apache.

the class PathUtils method isInSecureDir.

/**
 * Indicates whether file lives in a secure directory relative to the
 * program's user.
 * @param file {@link Path} to test.
 * @param user {@link UserPrincipal} to test. If {@code null}, defaults to
 * current user.
 * @param symlinkDepth Number of symbolic links allowed.
 * @return {@code true} if file's directory is secure.
 */
public static boolean isInSecureDir(Path file, UserPrincipal user, final 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
    final Path fileRoot = file.getRoot();
    if (fileRoot == null) {
        return false;
    }
    final FileSystem fileSystem = Paths.get(fileRoot.toString()).getFileSystem();
    final UserPrincipalLookupService upls = fileSystem.getUserPrincipalLookupService();
    UserPrincipal root = null;
    try {
        if (SystemUtils.IS_OS_UNIX) {
            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 (final IOException x) {
        return false;
    }
    // If any parent dirs (from root on down) are not secure, dir is not secure
    for (int i = 1; i <= file.getNameCount(); i++) {
        final Path partialPath = Paths.get(fileRoot.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 {
                final UserPrincipal owner = Files.getOwner(partialPath);
                if (!user.equals(owner) && !root.equals(owner)) {
                    // dir owned by someone else, not secure
                    return false;
                }
            }
        } catch (final 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 8 with UserPrincipalLookupService

use of java.nio.file.attribute.UserPrincipalLookupService in project vespa by vespa-engine.

the class UnixPath method setOwner.

public void setOwner(String owner) {
    UserPrincipalLookupService service = path.getFileSystem().getUserPrincipalLookupService();
    UserPrincipal principal = uncheck(() -> service.lookupPrincipalByName(owner), "While looking up user %s", owner);
    uncheck(() -> Files.setOwner(path, principal));
}
Also used : UserPrincipalLookupService(java.nio.file.attribute.UserPrincipalLookupService) UserPrincipal(java.nio.file.attribute.UserPrincipal)

Example 9 with UserPrincipalLookupService

use of java.nio.file.attribute.UserPrincipalLookupService in project vespa by vespa-engine.

the class UnixPath method setGroup.

public void setGroup(String group) {
    UserPrincipalLookupService service = path.getFileSystem().getUserPrincipalLookupService();
    GroupPrincipal principal = uncheck(() -> service.lookupPrincipalByGroupName(group), "while looking up group %s", group);
    uncheck(() -> Files.getFileAttributeView(path, PosixFileAttributeView.class).setGroup(principal));
}
Also used : UserPrincipalLookupService(java.nio.file.attribute.UserPrincipalLookupService) GroupPrincipal(java.nio.file.attribute.GroupPrincipal)

Example 10 with UserPrincipalLookupService

use of java.nio.file.attribute.UserPrincipalLookupService in project alluxio by Alluxio.

the class AbstractFileManager method addFile.

@Override
public boolean addFile(String fileName, String permission, byte[] content) {
    try {
        verifyFileName(fileName);
        Path path = Paths.get(getNextFilePath(fileName));
        short perm = Short.parseShort(permission, 8);
        Mode mode = new Mode(perm);
        Set<PosixFilePermission> permissions = PosixFilePermissions.fromString(mode.toString());
        FileAttribute<?> fileAttribute = PosixFilePermissions.asFileAttribute(permissions);
        Files.deleteIfExists(path);
        path = Files.createFile(path, fileAttribute);
        FileSystem fileSystem = path.getFileSystem();
        UserPrincipalLookupService service = fileSystem.getUserPrincipalLookupService();
        UserPrincipal userPrincipal = service.lookupPrincipalByName(mUser);
        GroupPrincipal groupPrincipal = service.lookupPrincipalByGroupName(mGroup);
        Files.write(path, content);
        Files.setOwner(path, userPrincipal);
        Files.getFileAttributeView(path, PosixFileAttributeView.class, LinkOption.NOFOLLOW_LINKS).setGroup(groupPrincipal);
        // sometimes umask is applied, so forcefully set permissions
        Files.setPosixFilePermissions(path, permissions);
        return true;
    } catch (InvalidPathException | IOException | AlluxioException e) {
        LOG.warn("Failed to add file {} to version manager", fileName, e);
        return false;
    }
}
Also used : Path(java.nio.file.Path) UserPrincipalLookupService(java.nio.file.attribute.UserPrincipalLookupService) Mode(alluxio.security.authorization.Mode) IOException(java.io.IOException) PosixFilePermission(java.nio.file.attribute.PosixFilePermission) UserPrincipal(java.nio.file.attribute.UserPrincipal) InvalidPathException(java.nio.file.InvalidPathException) PosixFileAttributeView(java.nio.file.attribute.PosixFileAttributeView) GroupPrincipal(java.nio.file.attribute.GroupPrincipal) FileSystem(java.nio.file.FileSystem) AlluxioException(alluxio.exception.AlluxioException)

Aggregations

UserPrincipalLookupService (java.nio.file.attribute.UserPrincipalLookupService)18 PosixFileAttributeView (java.nio.file.attribute.PosixFileAttributeView)12 UserPrincipal (java.nio.file.attribute.UserPrincipal)12 GroupPrincipal (java.nio.file.attribute.GroupPrincipal)11 IOException (java.io.IOException)7 Path (java.nio.file.Path)7 FileSystem (java.nio.file.FileSystem)3 FileSystemException (io.vertx.core.file.FileSystemException)2 PosixFilePermission (java.nio.file.attribute.PosixFilePermission)2 Test (org.junit.Test)2 AlluxioException (alluxio.exception.AlluxioException)1 Mode (alluxio.security.authorization.Mode)1 File (java.io.File)1 InvalidPathException (java.nio.file.InvalidPathException)1 UserPrincipalNotFoundException (java.nio.file.attribute.UserPrincipalNotFoundException)1 DateFormat (java.text.DateFormat)1 SimpleDateFormat (java.text.SimpleDateFormat)1 Date (java.util.Date)1 HashSet (java.util.HashSet)1 IgfsException (org.apache.ignite.igfs.IgfsException)1