From 284d09eb0fd5db747e5f42131d55f828ec43aca6 Mon Sep 17 00:00:00 2001 From: Andras Gemes Date: Wed, 23 Oct 2024 21:00:38 +0200 Subject: [PATCH] BSim: Update PostgreSQL (15.3->17.0) to fix SSL errors --- Ghidra/Features/BSim/Module.manifest | 2 +- Ghidra/Features/BSim/build.gradle | 2 +- .../topics/BSim/CommandLineReference.html | 18 +++ .../topics/BSim/DatabaseConfiguration.html | 8 +- .../bsim/query/BSimControlLaunchable.java | 121 ++++++++++++++++++ Ghidra/Features/BSim/support/make-postgres.sh | 6 +- gradle/support/fetchDependencies.gradle | 6 +- 7 files changed, 151 insertions(+), 12 deletions(-) diff --git a/Ghidra/Features/BSim/Module.manifest b/Ghidra/Features/BSim/Module.manifest index 60e6f9f4249..a0c98b55690 100755 --- a/Ghidra/Features/BSim/Module.manifest +++ b/Ghidra/Features/BSim/Module.manifest @@ -1,5 +1,5 @@ ##MODULE IP: Oxygen Icons - LGPL 3.0 -MODULE FILE LICENSE: postgresql-15.3.tar.gz Postgresql License +MODULE FILE LICENSE: postgresql-17.0.tar.gz Postgresql License MODULE FILE LICENSE: lib/postgresql-42.6.2.jar PostgresqlJDBC License MODULE FILE LICENSE: lib/json-simple-1.1.1.jar Apache License 2.0 MODULE FILE LICENSE: lib/commons-dbcp2-2.9.0.jar Apache License 2.0 diff --git a/Ghidra/Features/BSim/build.gradle b/Ghidra/Features/BSim/build.gradle index de0bd157e92..27c99974389 100755 --- a/Ghidra/Features/BSim/build.gradle +++ b/Ghidra/Features/BSim/build.gradle @@ -26,7 +26,7 @@ import java.nio.file.Files import org.gradle.util.GUtil // NOTE: fetchDependencies.gradle must be updated if postgresql version changes -def postgresql_distro = "postgresql-15.3.tar.gz" +def postgresql_distro = "postgresql-17.0.tar.gz" dependencies { api project(":Decompiler") diff --git a/Ghidra/Features/BSim/src/main/help/help/topics/BSim/CommandLineReference.html b/Ghidra/Features/BSim/src/main/help/help/topics/BSim/CommandLineReference.html index cf4d16f9bfe..cca78e169e7 100644 --- a/Ghidra/Features/BSim/src/main/help/help/topics/BSim/CommandLineReference.html +++ b/Ghidra/Features/BSim/src/main/help/help/topics/BSim/CommandLineReference.html @@ -46,6 +46,8 @@

+
dumpall
+ +
+

Dumps all PostgreSQL databases into a specified file. A dump file must be + specified, and the PostgreSQL server must be running.

+
+ +
restore
+ +
+

Restores all PostgreSQL databases from a specified file. A dump file must be + specified, and the PostgreSQL server must be running.

+
+
--Global Options--
diff --git a/Ghidra/Features/BSim/src/main/help/help/topics/BSim/DatabaseConfiguration.html b/Ghidra/Features/BSim/src/main/help/help/topics/BSim/DatabaseConfiguration.html index ad09bf241ad..8618472ee21 100644 --- a/Ghidra/Features/BSim/src/main/help/help/topics/BSim/DatabaseConfiguration.html +++ b/Ghidra/Features/BSim/src/main/help/help/topics/BSim/DatabaseConfiguration.html @@ -107,13 +107,13 @@

Building the Server

in the module directory Ghidra/Features/BSim/support that builds both the PostgreSQL server and the BSim extension from source and prepares the installation for use with Ghidra. If not already included in the Ghidra installation, the source distribution - file, currently postgresql-15.3.tar.gz, can be obtained from the PostgreSQL + file, currently postgresql-17.0.tar.gz, can be obtained from the PostgreSQL website at

-
https://www.postgresql.org/ftp/source/v15.3 + https://www.postgresql.org/ftp/source/v17.0
@@ -122,12 +122,12 @@

Building the Server

The steps to build the PostgreSQL server with the BSim extension then are:

1) If not already present, place the PostgreSQL source distribution file - postgresql-15.3.tar.gz in the Ghidra installation at

+ postgresql-17.0.tar.gz in the Ghidra installation at

-
$(ROOT)/Ghidra/Features/BSim/support/postgresql-15.3.tar.gz + $(ROOT)/Ghidra/Features/BSim/support/postgresql-17.0.tar.gz
diff --git a/Ghidra/Features/BSim/src/main/java/ghidra/features/bsim/query/BSimControlLaunchable.java b/Ghidra/Features/BSim/src/main/java/ghidra/features/bsim/query/BSimControlLaunchable.java index a922e02a8ae..ad28a551f57 100644 --- a/Ghidra/Features/BSim/src/main/java/ghidra/features/bsim/query/BSimControlLaunchable.java +++ b/Ghidra/Features/BSim/src/main/java/ghidra/features/bsim/query/BSimControlLaunchable.java @@ -57,6 +57,8 @@ public class BSimControlLaunchable implements GhidraLaunchable { public final static String COMMAND_ADDUSER = "adduser"; public final static String COMMAND_DROPUSER = "dropuser"; public final static String COMMAND_CHANGEAUTH = "changeauth"; + public final static String COMMAND_DUMPALL = "dumpall"; + public final static String COMMAND_RESTORE = "restore"; // Options that require a value argument public static final String CAFILE_OPTION = "--cafile"; @@ -98,6 +100,8 @@ public class BSimControlLaunchable implements GhidraLaunchable { private static final Set DROPUSER_OPTIONS = Set.of(); private static final Set CHANGEAUTH_OPTIONS = Set.of( AUTH_OPTION, NO_LOCAL_AUTH_OPTION, CAFILE_OPTION); + private static final Set DUMPALL_OPTIONS = Set.of(); + private static final Set RESTORE_OPTIONS = Set.of(); //@formatter:on private static final Map> ALLOWED_OPTION_MAP = new HashMap<>(); @@ -109,6 +113,8 @@ public class BSimControlLaunchable implements GhidraLaunchable { ALLOWED_OPTION_MAP.put(COMMAND_ADDUSER, ADDUSER_OPTIONS); ALLOWED_OPTION_MAP.put(COMMAND_DROPUSER, DROPUSER_OPTIONS); ALLOWED_OPTION_MAP.put(COMMAND_CHANGEAUTH, CHANGEAUTH_OPTIONS); + ALLOWED_OPTION_MAP.put(COMMAND_DUMPALL, DUMPALL_OPTIONS); + ALLOWED_OPTION_MAP.put(COMMAND_RESTORE, RESTORE_OPTIONS); } private final static String POSTGRES = "postgresql"; @@ -132,8 +138,11 @@ public class BSimControlLaunchable implements GhidraLaunchable { private GhidraApplicationLayout layout; private File dataDirectory; // Directory containing postgres datafiles + private File dumpFile; // Path to database dump file private File postgresRoot; // Directory containing postgres software private File postgresControl; // "pg_ctl" utility within postgres software + private File postgresDumpAll; // "pg_dumpall" utility within postgres software + private File postgresPsql; // "psql" utility within postgres software private File certAuthorityFile; // Certificate authority file provided by the user private String certParameter; // Path to certificate provided by user private String distinguishedName; // Certificate distinguished name provided by the user @@ -163,8 +172,11 @@ public BSimControlLaunchable() { private void clearParams() { dataDirectory = null; + dumpFile = null; postgresRoot = null; postgresControl = null; + postgresDumpAll = null; + postgresPsql = null; certAuthorityFile = null; certParameter = null; distinguishedName = null; @@ -215,6 +227,12 @@ private String readCommandLine(String[] params) throws IllegalArgumentException, case COMMAND_CHANGEAUTH: scanDataDirectory(params, slot++); break; + case COMMAND_DUMPALL: + scanDumpFile(params, slot++); + break; + case COMMAND_RESTORE: + scanDumpFile(params, slot++); + break; case COMMAND_CHANGE_PRIVILEGE: scanUsername(params, slot++); scanPrivilege(params, slot++); @@ -776,6 +794,14 @@ private void discoverPostgresInstall() throws IOException { if (!postgresControl.isFile()) { throw new IOException("PostgreSQL pg_ctl command not found: " + postgresControl); } + postgresDumpAll = new File(postgresRoot, "bin/pg_dumpall"); + if (!postgresDumpAll.isFile()) { + throw new IOException("PostgreSQL pg_dumpall command not found: " + postgresDumpAll); + } + postgresPsql = new File(postgresRoot, "bin/psql"); + if (!postgresPsql.isFile()) { + throw new IOException("PostgreSQL psql command not found: " + postgresPsql); + } setupPostgresSharedLibrary(); } catch (OSFileNotFoundException e) { @@ -983,6 +1009,19 @@ else if (params[slot].equals("user")) { } } + /** + * Scan the PostgreSQL dump file from the command-line + * @param params are the command-line arguments + * @param slot is the position to retrieve the dump file argument + * @throws IllegalArgumentException if the dump file is invalid + */ + private void scanDumpFile(String [] params, int slot) throws IllegalArgumentException { + if (params.length <= slot) { + throw new IllegalArgumentException("Missing dump file"); + } + dumpFile = new File(params[slot]); + } + /** * Start a PostgreSQL server, configured for BSim, on the local host. * If the data directory is already populated, the server process is simply restarted. @@ -1036,6 +1075,80 @@ private void startCommand() } } + /** + * Dumps all PostgreSQL databases from the local host into a specified file. + * Authentication may be necessary, either via password or certificate. + * + * @throws IOException if the postgres databases can not be dumped + * @throws InterruptedException if the process fails during the run + * @throws GeneralSecurityException if the authentication fails + */ + private void dumpAllCommand() + throws IOException, InterruptedException, GeneralSecurityException { + discoverPostgresInstall(); + + if (localAuthentication == AUTHENTICATION_PKI && certParameter == null) { + throw new GeneralSecurityException( + "Path to certificate necessary to dump databases (--cert /path/to/cert)"); + } + + List command = new ArrayList(); + command.add(postgresDumpAll.getAbsolutePath()); + command.add("-f"); + command.add(dumpFile.getAbsolutePath()); + command.add("-U"); + command.add(connectingUserName); + command.add("-h"); + command.add("localhost"); + if ((port != -1) && (port != 5432)) { // Non-default port + command.add("-p"); + command.add(Integer.toString(port)); + } + int res = runCommand(null, command, loadLibraryVar, loadLibraryValue); + if (res != 0) { + throw new IOException("Could not dump databases"); + } + System.out.println("Databases dumped to " + dumpFile.getAbsolutePath()); + } + + /** + * Restore all PostgreSQL databases to the local host from a specified file. + * Authentication may be necessary, either via password or certificate. + * + * @throws IOException if the postgres databases can not be restored + * @throws InterruptedException if the process fails during the run + * @throws GeneralSecurityException if the authentication fails + */ + private void restoreCommand() + throws IOException, InterruptedException, GeneralSecurityException { + discoverPostgresInstall(); + + if (localAuthentication == AUTHENTICATION_PKI && certParameter == null) { + throw new GeneralSecurityException( + "Path to certificate necessary to restore databases (--cert /path/to/cert)"); + } + + List command = new ArrayList(); + command.add(postgresPsql.getAbsolutePath()); + command.add("-f"); + command.add(dumpFile.getAbsolutePath()); + command.add("-U"); + command.add(connectingUserName); + command.add("-h"); + command.add("localhost"); + command.add("-d"); + command.add("postgres"); // psql requires a database, and 'postgres' is always available + if ((port != -1) && (port != 5432)) { // Non-default port + command.add("-p"); + command.add(Integer.toString(port)); + } + int res = runCommand(null, command, loadLibraryVar, loadLibraryValue); + if (res != 0) { + throw new IOException("Could not restore databases"); + } + System.out.println("Databases restored from " + dumpFile.getAbsolutePath()); + } + /** * Stop the running PostgreSQL processes on the local host. No authentication is required to shutdown * the server. User must be the process owner. @@ -1407,6 +1520,12 @@ public void run(String[] params) throws Exception { case COMMAND_CHANGEAUTH: changeAuthCommand(); break; + case COMMAND_DUMPALL: + dumpAllCommand(); + break; + case COMMAND_RESTORE: + restoreCommand(); + break; case COMMAND_RESET_PASSWORD: passwordCommand(); break; @@ -1438,6 +1557,8 @@ private static void printUsage() { " changeauth [--auth|-a pki|password|trust] [--noLocalAuth] [--cafile \"\"]\n" + " resetpassword \n" + " changeprivilege admin|user\n" + + " dumpall \n" + + " restore \n" + "\n" + "Global options:\n" + " --port|-p \n" + diff --git a/Ghidra/Features/BSim/support/make-postgres.sh b/Ghidra/Features/BSim/support/make-postgres.sh index b36a7609cd9..14c8dda01da 100755 --- a/Ghidra/Features/BSim/support/make-postgres.sh +++ b/Ghidra/Features/BSim/support/make-postgres.sh @@ -18,11 +18,11 @@ # This script builds the postgresql server and BSim extension within a # GHIDRA installation. # -# The PostgreSQL source distribution file postgresql-15.3.tar.gz must +# The PostgreSQL source distribution file postgresql-17.0.tar.gz must # be placed in the BSim module directory prior to running this script. # This file can be downloaded directly from the PostgreSQL website at: # -# https://www.postgresql.org/ftp/source/v15.3 +# https://www.postgresql.org/ftp/source/v17.0 # # Within development environments, this script will first check the # ghidra.bin repo for this source file. @@ -46,7 +46,7 @@ # # -POSTGRES=postgresql-15.3 +POSTGRES=postgresql-17.0 POSTGRES_GZ=${POSTGRES}.tar.gz POSTGRES_CONFIG_OPTIONS="--disable-rpath --with-openssl" diff --git a/gradle/support/fetchDependencies.gradle b/gradle/support/fetchDependencies.gradle index 4b358694dbb..0a4ddd03741 100644 --- a/gradle/support/fetchDependencies.gradle +++ b/gradle/support/fetchDependencies.gradle @@ -89,9 +89,9 @@ ext.deps = [ destination: file("${DEPS_DIR}/GhidraServer") ], [ - name: "postgresql-15.3.tar.gz", - url: "https://ftp.postgresql.org/pub/source/v15.3/postgresql-15.3.tar.gz", - sha256: "086d38533e28747966a4d5f1e78ea432e33a78f21dcb9133010ecb5189fad98c", + name: "postgresql-17.0.tar.gz", + url: "https://ftp.postgresql.org/pub/source/v17.0/postgresql-17.0.tar.gz", + sha256: "bf81c0c5161e456a886ede5f1f4133f43af000637e377156a02e7e83569081ad", destination: file("${DEPS_DIR}/BSim") ], [