Skip to content

Commit

Permalink
BSim: Update PostgreSQL (15.3->17.0) to fix SSL errors
Browse files Browse the repository at this point in the history
  • Loading branch information
gemesa committed Nov 8, 2024
1 parent 30ef757 commit 284d09e
Show file tree
Hide file tree
Showing 7 changed files with 151 additions and 12 deletions.
2 changes: 1 addition & 1 deletion Ghidra/Features/BSim/Module.manifest
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 1 addition & 1 deletion Ghidra/Features/BSim/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ <H2 class="title" style="clear: both"><A name="BSimCtl"></A><CODE class=
bsim_ctl resetpassword &lt;username&gt;
bsim_ctl changeauth &lt;/datadir-path&gt; [--auth|-a&nbsp;pki|password|trust] [--noLocalAuth] [--cafile&nbsp;&lt;/cacert-path&gt;] [--dn&nbsp;"&lt;distinguished-name&gt;"]
bsim_ctl changeprivilege &lt;username&gt; admin|user
bsim_ctl dumpall &lt;/dumpfile-path&gt;
bsim_ctl restore &lt;/dumpfile-path&gt;

Global Options:
--port|-p&nbsp;&lt;portnum&gt;
Expand Down Expand Up @@ -208,6 +210,22 @@ <H2 class="title" style="clear: both"><A name="BSimCtl"></A><CODE class=
running.</P>
</DD>

<DT><SPAN class="term"><SPAN class=
"bold"><STRONG>dumpall</STRONG></SPAN></SPAN></DT>

<DD>
<P>Dumps all PostgreSQL databases into a specified file. A dump file must be
specified, and the PostgreSQL server must be running.</P>
</DD>

<DT><SPAN class="term"><SPAN class=
"bold"><STRONG>restore</STRONG></SPAN></SPAN></DT>

<DD>
<P>Restores all PostgreSQL databases from a specified file. A dump file must be
specified, and the PostgreSQL server must be running.</P>
</DD>

<DT><SPAN class="term"><SPAN class="bold"><STRONG>--Global
Options--</STRONG></SPAN></SPAN></DT>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,13 +107,13 @@ <H4 class="title"><A name="PostBuild"></A>Building the Server</H4>
in the module directory <CODE>Ghidra/Features/BSim/support</CODE> 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 <CODE>postgresql-15.3.tar.gz</CODE>, can be obtained from the PostgreSQL
file, currently <CODE>postgresql-17.0.tar.gz</CODE>, can be obtained from the PostgreSQL
website at </P>

<DIV class="informalexample">
<TABLE border="0" summary="Simple list" class="simplelist">
<TR>
<TD><CODE class="computeroutput">https://www.postgresql.org/ftp/source/v15.3
<TD><CODE class="computeroutput">https://www.postgresql.org/ftp/source/v17.0
</CODE></TD>
</TR>
</TABLE>
Expand All @@ -122,12 +122,12 @@ <H4 class="title"><A name="PostBuild"></A>Building the Server</H4>
<P>The steps to build the PostgreSQL server with the BSim extension then are:</P>

<P>1) If not already present, place the PostgreSQL source distribution file
<CODE>postgresql-15.3.tar.gz</CODE> in the Ghidra installation at</P>
<CODE>postgresql-17.0.tar.gz</CODE> in the Ghidra installation at</P>

<DIV class="informalexample">
<TABLE border="0" summary="Simple list" class="simplelist">
<TR>
<TD><CODE class="computeroutput">$(ROOT)/Ghidra/Features/BSim/support/postgresql-15.3.tar.gz
<TD><CODE class="computeroutput">$(ROOT)/Ghidra/Features/BSim/support/postgresql-17.0.tar.gz
</CODE></TD>
</TR>
</TABLE>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -98,6 +100,8 @@ public class BSimControlLaunchable implements GhidraLaunchable {
private static final Set<String> DROPUSER_OPTIONS = Set.of();
private static final Set<String> CHANGEAUTH_OPTIONS = Set.of(
AUTH_OPTION, NO_LOCAL_AUTH_OPTION, CAFILE_OPTION);
private static final Set<String> DUMPALL_OPTIONS = Set.of();
private static final Set<String> RESTORE_OPTIONS = Set.of();

//@formatter:on
private static final Map<String, Set<String>> ALLOWED_OPTION_MAP = new HashMap<>();
Expand All @@ -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";
Expand All @@ -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
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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++);
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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<String> command = new ArrayList<String>();
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<String> command = new ArrayList<String>();
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.
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -1438,6 +1557,8 @@ private static void printUsage() {
" changeauth </datadir-path> [--auth|-a pki|password|trust] [--noLocalAuth] [--cafile \"</cacert-path>\"]\n" +
" resetpassword <username>\n" +
" changeprivilege <username> admin|user\n" +
" dumpall </dumpfile-path>\n" +
" restore </dumpfile-path>\n" +
"\n" +
"Global options:\n" +
" --port|-p <portnum>\n" +
Expand Down
6 changes: 3 additions & 3 deletions Ghidra/Features/BSim/support/make-postgres.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -46,7 +46,7 @@
#
#

POSTGRES=postgresql-15.3
POSTGRES=postgresql-17.0
POSTGRES_GZ=${POSTGRES}.tar.gz
POSTGRES_CONFIG_OPTIONS="--disable-rpath --with-openssl"

Expand Down
6 changes: 3 additions & 3 deletions gradle/support/fetchDependencies.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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")
],
[
Expand Down

0 comments on commit 284d09e

Please sign in to comment.