Ghidra is a software reverse engineering (SRE) framework
GP-7049 ensure repository ACL excludes users not defined on server
ghidra1108b101bed01
diff --git a/Ghidra/Features/GhidraServer/src/main/java/ghidra/server/Repository.java b/Ghidra/Features/GhidraServer/src/main/java/ghidra/server/Repository.javaindex 95b0602692..6a77e75aca 100644--- a/Ghidra/Features/GhidraServer/src/main/java/ghidra/server/Repository.java+++ b/Ghidra/Features/GhidraServer/src/main/java/ghidra/server/Repository.java@@ -17,6 +17,7 @@ package ghidra.server;import java.io.*;import java.util.*;+import java.util.concurrent.atomic.AtomicBoolean;import org.apache.logging.log4j.LogManager;import org.apache.logging.log4j.Logger;@@ -351,7 +352,7 @@ public class Repository implements FileSystemListener, RepositoryLogger {validate();validateAdminPrivilege(currentUser);- Set<String> allUsers = Set.of(mgr.getAllUsers(currentUser));+ Set<String> allUsers = getAllServerUsers();Set<String> updatedUsers = new HashSet<>();LinkedHashMap<String, User> newUserMap = new LinkedHashMap<>();@@ -574,17 +575,20 @@ public class Repository implements FileSystemListener, RepositoryLogger {* Generate formatted list of user access permissions to the specified repository.* This is intended to be used with the svrAdmin console command* @param repositoryDir repository directory+ * @param allUsers set of all valid users known to server* @param pad padding string to be prefixed to each output line* @return formatted list of user access permissions*/- static String getFormattedUserPermissions(File repositoryDir, String pad) {+ static String getFormattedUserPermissions(File repositoryDir, Set<String> allUsers,+ String pad) {StringBuilder buf = new StringBuilder();File userAccessFile = new File(repositoryDir, ACCESS_CONTROL_FILENAME);try {List<User> list = new ArrayList<>();- boolean anonymousAccessAllowed = readAccessFile(userAccessFile, list);+ AtomicBoolean anonymousAccessAllowed = new AtomicBoolean();+ readAccessFile(userAccessFile, allUsers, anonymousAccessAllowed, list);Collections.sort(list);- if (anonymousAccessAllowed) {+ if (anonymousAccessAllowed.get()) {buf.append(pad + "* Anonymous read-only access permitted *\n");}for (User user : list) {@@ -592,7 +596,7 @@ public class Repository implements FileSystemListener, RepositoryLogger {}}catch (IOException e) {- System.out.println(pad + "Failed to read repository access file: " + e.getMessage());+ System.err.println(pad + "Failed to read repository access file: " + e.getMessage());}return buf.toString();}@@ -602,17 +606,19 @@ public class Repository implements FileSystemListener, RepositoryLogger {* restricted to user names contained within listUserAccess.* This is intended to be used with the svrAdmin console command* @param repositoryDir repository directory+ * @param allUsers set of all valid users known to server* @param pad padding string to be prefixed to each output line* @param listUserAccess set of user names of interest* @return formatted list of user access permissions or null if no users of interest found*/- static String getFormattedUserPermissions(File repositoryDir, String pad,+ static String getFormattedUserPermissions(File repositoryDir, Set<String> allUsers, String pad,Set<String> listUserAccess) {StringBuilder buf = null;File userAccessFile = new File(repositoryDir, ACCESS_CONTROL_FILENAME);try {+ AtomicBoolean anonymousAccessAllowed = new AtomicBoolean();ArrayList<User> list = new ArrayList<>();- readAccessFile(userAccessFile, list);+ readAccessFile(userAccessFile, allUsers, anonymousAccessAllowed, list);Collections.sort(list);for (User user : list) {if (!listUserAccess.contains(user.getName())) {@@ -625,11 +631,15 @@ public class Repository implements FileSystemListener, RepositoryLogger {}}catch (IOException e) {- System.out.println(pad + "Failed to read repository access file: " + e.getMessage());+ System.err.println(pad + "Failed to read repository access file: " + e.getMessage());}return buf != null ? buf.toString() : null;}+ private Set<String> getAllServerUsers() {+ return Set.of(mgr.getUserManager().getUsers());+ }+/*** Read user access list from local file.* @throws IOException if an IO error occurs@@ -639,9 +649,12 @@ public class Repository implements FileSystemListener, RepositoryLogger {return;}+ AtomicBoolean anonymousAccess = new AtomicBoolean();ArrayList<User> list = new ArrayList<>();- anonymousAccessAllowed =- readAccessFile(userAccessFile, list) && mgr.anonymousAccessAllowed();+ boolean modified =+ readAccessFile(userAccessFile, getAllServerUsers(), anonymousAccess, list) &&+ mgr.anonymousAccessAllowed();+ anonymousAccessAllowed = mgr.anonymousAccessAllowed() & anonymousAccess.get();LinkedHashMap<String, User> newUserMap = new LinkedHashMap<>();boolean hasAdmin = false;@@ -654,19 +667,27 @@ public class Repository implements FileSystemListener, RepositoryLogger {.info("WARNING: Repository '" + name + "' does not have an assigned Admin");}userMap = newUserMap;++ if (modified) {+ writeUserList(userMap, anonymousAccessAllowed);+ }}/*** Read list of user permissions from userAccessFile and determine if anonymous read-only* access is permitted* @param userAccessFile repository user access file+ * @param allUsers set of all valid users known to server+ * @param allowAnonymous which conveys the anonymous permission state read from the file* @param users list to be populated with user permissions defined by userAccessFile- * @return true if anonymous read-only access is permitted, else false+ * @return true if one of more users were removed from the stored list* @throws IOException if an IO error occurs*/- private static boolean readAccessFile(File userAccessFile, List<User> users)+ private static boolean readAccessFile(File userAccessFile, Set<String> allUsers,+ AtomicBoolean allowAnonymous, List<User> users)throws IOException {- boolean allowAnonymous = false;+ boolean userRemoved = false;+ allowAnonymous.set(false);try (BufferedReader reader = new BufferedReader(new FileReader(userAccessFile))) {String line = "";while (true) {@@ -679,16 +700,21 @@ public class Repository implements FileSystemListener, RepositoryLogger {}line = line.trim();if (ANONYMOUS_STR.equals(line)) {- allowAnonymous = true;+ allowAnonymous.set(true);continue;}User user = processAccessLine(line);if (user != null) {- users.add(user);+ if (allUsers.contains(user.getName())) {+ users.add(user);+ }+ else {+ userRemoved = true;+ }}}}- return allowAnonymous;+ return userRemoved;}/**@@ -986,7 +1012,7 @@ public class Repository implements FileSystemListener, RepositoryLogger {int indexVersion = IndexedLocalFileSystem.readIndexVersion(rootPath);if (indexVersion >= IndexedLocalFileSystem.LATEST_INDEX_VERSION) {if (!silent) {- System.err+ System.out.println("Repository '" + repositoryName + "' is already indexed!");}return false;diff --git a/Ghidra/Features/GhidraServer/src/main/java/ghidra/server/RepositoryManager.java b/Ghidra/Features/GhidraServer/src/main/java/ghidra/server/RepositoryManager.javaindex e0faae5693..d5fd7ed411 100644--- a/Ghidra/Features/GhidraServer/src/main/java/ghidra/server/RepositoryManager.java+++ b/Ghidra/Features/GhidraServer/src/main/java/ghidra/server/RepositoryManager.java@@ -459,8 +459,10 @@ public class RepositoryManager {* @param repositoriesRootDir repositories root directory* @param includeUserAccessDetails if true additional user access details will displayed* for each repository+ * @param allUsers set of all valid users known to server*/- static void listRepositories(File repositoriesRootDir, boolean includeUserAccessDetails) {+ static void listRepositories(File repositoriesRootDir, boolean includeUserAccessDetails,+ Set<String> allUsers) {String[] names = RepositoryManager.getRepositoryNames(repositoriesRootDir);System.out.println("\nRepositories:");if (names.length == 0) {@@ -495,7 +497,7 @@ public class RepositoryManager {System.out.println(" " + name + (type == null ? "" : (" - uses " + type)));if (includeUserAccessDetails) {- System.out.print(Repository.getFormattedUserPermissions(repoDir, " "));+ System.out.print(Repository.getFormattedUserPermissions(repoDir, allUsers, " "));}}}@@ -505,8 +507,10 @@ public class RepositoryManager {* This is intended to be used with the svrAdmin console command* @param repositoriesRootDir repositories root directory* @param usernameSet set of users whose details should be displayed+ * @param allUsers set of all valid users known to server*/- static void listRepositories(File repositoriesRootDir, Set<String> usernameSet) {+ static void listRepositories(File repositoriesRootDir, Set<String> usernameSet,+ Set<String> allUsers) {String[] names = RepositoryManager.getRepositoryNames(repositoriesRootDir);if (names.length == 0) {System.out.println(" <No repositories have been created>");@@ -518,7 +522,7 @@ public class RepositoryManager {File repoDir = new File(repositoriesRootDir, NamingUtilities.mangle(name));String formattedAccessList =- Repository.getFormattedUserPermissions(repoDir, " ", usernameSet);+ Repository.getFormattedUserPermissions(repoDir, allUsers, " ", usernameSet);if (formattedAccessList != null) {if (outputHeader) {System.out.println("\nRepositories:");@@ -562,8 +566,8 @@ public class RepositoryManager {* @throws IOException if error occured while updating repository access lists.*/void userRemoved(String username) throws IOException {- for (String repName : getRepositoryNames()) {- getRepository(repName).removeUser(username);+ for (Repository repo : repositoryMap.values()) {+ repo.removeUser(username);}}diff --git a/Ghidra/Features/GhidraServer/src/main/java/ghidra/server/ServerAdmin.java b/Ghidra/Features/GhidraServer/src/main/java/ghidra/server/ServerAdmin.javaindex 2526b30467..2edbc46cf0 100644--- a/Ghidra/Features/GhidraServer/src/main/java/ghidra/server/ServerAdmin.java+++ b/Ghidra/Features/GhidraServer/src/main/java/ghidra/server/ServerAdmin.java@@ -26,7 +26,8 @@ import ghidra.GhidraApplicationLayout;import ghidra.GhidraLaunchable;import ghidra.framework.Application;import ghidra.framework.ApplicationConfiguration;-import ghidra.util.*;+import ghidra.util.Msg;+import ghidra.util.NamingUtilities;public class ServerAdmin implements GhidraLaunchable {@@ -238,11 +239,18 @@ public class ServerAdmin implements GhidraLaunchable {UserManager.listUsers(serverRootDir);}if (listRepositories) {- if (listUsernameSet.isEmpty()) {- RepositoryManager.listRepositories(serverRootDir, listAllUserPermissions);+ try {+ Set<String> users = UserManager.getUsers(serverRootDir);+ if (listUsernameSet.isEmpty()) {+ RepositoryManager.listRepositories(serverRootDir, listAllUserPermissions,+ users);+ }+ else {+ RepositoryManager.listRepositories(serverRootDir, listUsernameSet, users);+ }}- else {- RepositoryManager.listRepositories(serverRootDir, listUsernameSet);+ catch (IOException e) {+ System.err.println("\nFailed to read user file: " + e.getMessage());}}System.out.println();@@ -477,7 +485,7 @@ public class ServerAdmin implements GhidraLaunchable {File configFile = new File(configFilePath);if (!configFile.exists()) {- System.out.println("Config file not found: " + configFile.getAbsolutePath());+ System.err.println("Config file not found: " + configFile.getAbsolutePath());return null;}@@ -495,7 +503,7 @@ public class ServerAdmin implements GhidraLaunchable {config.load(in);}catch (IOException e) {- System.out.println("Failed to read " + configFile.getName() + ": " + e.getMessage());+ System.err.println("Failed to read " + configFile.getName() + ": " + e.getMessage());}finally {if (in != null) {@@ -510,7 +518,7 @@ public class ServerAdmin implements GhidraLaunchable {String p = config.getProperty(SERVER_DIR_CONFIG_PROPERTY);if (p == null) {- System.out.println("Failed to find property: " + SERVER_DIR_CONFIG_PROPERTY);+ System.err.println("Failed to find property: " + SERVER_DIR_CONFIG_PROPERTY);return null;}File dir = new File(p);@@ -518,7 +526,7 @@ public class ServerAdmin implements GhidraLaunchable {// Make relative repositories dir relative to installation rootResourceFile installRoot = Application.getInstallationDirectory();if (installRoot == null || installRoot.getFile(false) == null) {- System.out.println("Failed to resolve installation root directory!");+ System.err.println("Failed to resolve installation root directory!");return null;}dir = new File(installRoot.getFile(false), p);diff --git a/Ghidra/Features/GhidraServer/src/main/java/ghidra/server/UserManager.java b/Ghidra/Features/GhidraServer/src/main/java/ghidra/server/UserManager.javaindex a64090f1f6..a57d8ad983 100644--- a/Ghidra/Features/GhidraServer/src/main/java/ghidra/server/UserManager.java+++ b/Ghidra/Features/GhidraServer/src/main/java/ghidra/server/UserManager.java@@ -554,10 +554,26 @@ public class UserManager {}}catch (IOException e) {- System.out.println("\nFailed to read user file: " + e.getMessage());+ System.err.println("\nFailed to read user file: " + e.getMessage());}}+ /**+ * Get list of all users known to server.+ * @param repositoriesRootDir repositories root directory+ * @return list of known users+ * @throws IOException if error occurs while reading passwd file+ */+ static Set<String> getUsers(File repositoriesRootDir) throws IOException {+ File userFile = new File(repositoriesRootDir, USER_PASSWORD_FILE);++ LinkedHashMap<String, UserEntry> list = new LinkedHashMap<>();+ HashMap<X500Principal, UserEntry> lookupMap = new HashMap<>();++ readUserList(userFile, list, lookupMap);+ return list.keySet();+ }+private static void readUserList(File file, Map<String, UserEntry> usersIndexByName,Map<X500Principal, UserEntry> x500LookupMap) throws IOException {try (BufferedReader br = new BufferedReader(new FileReader(file))) {