Skip to content

Commit

Permalink
Refactor direct SSL handshake logic
Browse files Browse the repository at this point in the history
This commit refactors the SSL handshake logic in the engine to support
protocol-specific handling through the fn_ssl_handshake function
pointer. A wrapper function encapsulates the static ProcessSSLStartup
logic for non-TDS connections, ensuring the original functionality is
preserved. The connection startup process now delegates SSL handshake
handling to the protocol-defined function.

Task: BABEL-5342
Signed-off-by: Roshan Kanwar <[email protected]>
  • Loading branch information
roshan0708 committed Dec 16, 2024
1 parent db4e138 commit 3bd2029
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 5 deletions.
8 changes: 7 additions & 1 deletion src/backend/postmaster/postmaster.c
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,8 @@ ProtocolExtensionConfig default_protocol_config = {
libpq_end_command,
NULL, NULL, NULL, NULL, /* use libpq defaults for printtup*() */
NULL,
libpq_report_param_status
libpq_report_param_status,
libpq_ssl_handshake
};

/* still more option variables */
Expand Down Expand Up @@ -1511,6 +1512,11 @@ libpq_end_command(QueryCompletion *qc, CommandDest dest)
EndCommand(qc, dest, false);
}

int
libpq_ssl_handshake(struct Port *port)
{
return WrapperProcessSSLStartup(port);
}

/*
* on_proc_exit callback to close server's listen sockets
Expand Down
16 changes: 12 additions & 4 deletions src/backend/tcop/backend_startup.c
Original file line number Diff line number Diff line change
Expand Up @@ -253,15 +253,14 @@ BackendInitialize(ClientSocket *client_sock, CAC_state cac, ProtocolExtensionCon
RegisterTimeout(STARTUP_PACKET_TIMEOUT, StartupPacketTimeoutHandler);
enable_timeout_after(STARTUP_PACKET_TIMEOUT, AuthenticationTimeout * 1000);

/* Handle direct SSL handshake for non-TDS connections */
if (!port->is_tds_conn)
status = ProcessSSLStartup(port);
/* Handle protocol-specific SSL handshake */
status = port->protocol_config->fn_ssl_handshake(port);

/*
* Receive the startup packet (which might turn out to be a cancel request
* packet).
*/
if (port->is_tds_conn || status == STATUS_OK)
if (status == STATUS_OK)
status = (port->protocol_config->fn_start)(port);

/*
Expand Down Expand Up @@ -889,3 +888,12 @@ StartupPacketTimeoutHandler(void)
{
_exit(1);
}

/*
* Wrapper for ProcessSSLStartup to handle direct SSL handshake
*/
int
WrapperProcessSSLStartup(Port *port)
{
return ProcessSSLStartup(port);
}
3 changes: 3 additions & 0 deletions src/include/libpq/libpq-be.h
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,9 @@ typedef struct ProtocolExtensionConfig {
void (*fn_printtup_destroy)(DestReceiver *self);
int (*fn_process_command)(void);
void (*fn_report_param_status)(const char *name, char *val);

/* function pointer for handling direct SSL handshake */
int (*fn_ssl_handshake)(struct Port *port);
} ProtocolExtensionConfig;

/*
Expand Down
1 change: 1 addition & 0 deletions src/include/postmaster/protocol_extension.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,6 @@ extern void libpq_send_ready_for_query(CommandDest dest);
extern int libpq_read_command(StringInfo inBuf);
extern void libpq_end_command(QueryCompletion *qc, CommandDest dest);
extern void libpq_report_param_status(const char *name, char *val);
extern int libpq_ssl_handshake(struct Port *port);

#endif /* _PROTOCOL_EXTENSION_H */
1 change: 1 addition & 0 deletions src/include/tcop/backend_startup.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,6 @@ typedef struct BackendStartupData

extern void BackendMain(char *startup_data, size_t startup_data_len) pg_attribute_noreturn();
extern int ProcessStartupPacket(Port *port, bool ssl_done, bool gss_done);
extern int WrapperProcessSSLStartup(Port *port);

#endif /* BACKEND_STARTUP_H */

0 comments on commit 3bd2029

Please sign in to comment.