From 052dc07034c53c5009dbae3083d5437b21cdeb70 Mon Sep 17 00:00:00 2001 From: Don Seiler Date: Wed, 20 Jun 2018 13:46:50 -0500 Subject: [PATCH] Changes to add application_name to Port struct so we can display the application name in "connection authorized" log messages. Creating a clean_ascii() function and using that before setting port->application_name as well as in check_application_name() and check_cluster_name() which used the same logic. --- src/backend/postmaster/postmaster.c | 12 ++++++++++++ src/backend/utils/init/postinit.c | 14 ++++++++++++-- src/backend/utils/misc/guc.c | 17 +++-------------- src/common/string.c | 17 +++++++++++++++++ src/include/common/string.h | 1 + src/include/libpq/libpq-be.h | 7 +++++++ 6 files changed, 52 insertions(+), 16 deletions(-) diff --git a/src/backend/postmaster/postmaster.c b/src/backend/postmaster/postmaster.c index 305ff36258..cb4248a95f 100644 --- a/src/backend/postmaster/postmaster.c +++ b/src/backend/postmaster/postmaster.c @@ -2096,6 +2096,18 @@ retry1: pstrdup(nameptr)); port->guc_options = lappend(port->guc_options, pstrdup(valptr)); + + /* + * Copy application_name to port when we come across it. + * This is used so we can log the application_name upon + * connection authorization. + */ + if (strcmp(nameptr, "application_name") == 0) + { + char *tmp_app_name = pstrdup(valptr); + clean_ascii(tmp_app_name); + port->application_name = pstrdup(tmp_app_name); + } } offset = valoffset + strlen(valptr) + 1; } diff --git a/src/backend/utils/init/postinit.c b/src/backend/utils/init/postinit.c index 5ef6315d20..bb8c2c6f48 100644 --- a/src/backend/utils/init/postinit.c +++ b/src/backend/utils/init/postinit.c @@ -266,7 +266,14 @@ PerformAuthentication(Port *port) #ifdef USE_SSL if (port->ssl_in_use) ereport(LOG, - (errmsg("connection authorized: user=%s database=%s SSL enabled (protocol=%s, cipher=%s, bits=%d, compression=%s)", + (port->application_name + ? errmsg("connection authorized: user=%s database=%s application_name=%s SSL enabled (protocol=%s, cipher=%s, bits=%d, compression=%s)", + port->user_name, port->database_name, port->application_name, + be_tls_get_version(port), + be_tls_get_cipher(port), + be_tls_get_cipher_bits(port), + be_tls_get_compression(port) ? _("on") : _("off")) + : errmsg("connection authorized: user=%s database=%s SSL enabled (protocol=%s, cipher=%s, bits=%d, compression=%s)", port->user_name, port->database_name, be_tls_get_version(port), be_tls_get_cipher(port), @@ -275,7 +282,10 @@ PerformAuthentication(Port *port) else #endif ereport(LOG, - (errmsg("connection authorized: user=%s database=%s", + (port->application_name + ? errmsg("connection authorized: user=%s database=%s application_name=%s", + port->user_name, port->database_name, port->application_name) + : errmsg("connection authorized: user=%s database=%s", port->user_name, port->database_name))); } } diff --git a/src/backend/utils/misc/guc.c b/src/backend/utils/misc/guc.c index 77662aff7f..97988be092 100644 --- a/src/backend/utils/misc/guc.c +++ b/src/backend/utils/misc/guc.c @@ -41,6 +41,7 @@ #include "commands/vacuum.h" #include "commands/variable.h" #include "commands/trigger.h" +#include "common/string.h" #include "funcapi.h" #include "jit/jit.h" #include "libpq/auth.h" @@ -10754,13 +10755,7 @@ static bool check_application_name(char **newval, void **extra, GucSource source) { /* Only allow clean ASCII chars in the application name */ - char *p; - - for (p = *newval; *p; p++) - { - if (*p < 32 || *p > 126) - *p = '?'; - } + clean_ascii(*newval); return true; } @@ -10776,13 +10771,7 @@ static bool check_cluster_name(char **newval, void **extra, GucSource source) { /* Only allow clean ASCII chars in the cluster name */ - char *p; - - for (p = *newval; *p; p++) - { - if (*p < 32 || *p > 126) - *p = '?'; - } + clean_ascii(*newval); return true; } diff --git a/src/common/string.c b/src/common/string.c index 3260d37a84..088fb91027 100644 --- a/src/common/string.c +++ b/src/common/string.c @@ -56,3 +56,20 @@ strtoint(const char *pg_restrict str, char **pg_restrict endptr, int base) errno = ERANGE; return (int) val; } + + +/* + * clean_ascii -- Replace any non-ASCII chars with a '?' char + */ +void +clean_ascii(char *newval) +{ + /* Only allow clean ASCII chars in the string */ + char *p; + + for (p = newval; *p; p++) + { + if (*p < 32 || *p > 126) + *p = '?'; + } +} diff --git a/src/include/common/string.h b/src/include/common/string.h index 78a450192e..659b369fbc 100644 --- a/src/include/common/string.h +++ b/src/include/common/string.h @@ -13,5 +13,6 @@ extern bool pg_str_endswith(const char *str, const char *end); extern int strtoint(const char *pg_restrict str, char **pg_restrict endptr, int base); +extern void clean_ascii(char *newval); #endif /* COMMON_STRING_H */ diff --git a/src/include/libpq/libpq-be.h b/src/include/libpq/libpq-be.h index ef5528c897..e66b39663d 100644 --- a/src/include/libpq/libpq-be.h +++ b/src/include/libpq/libpq-be.h @@ -138,6 +138,13 @@ typedef struct Port char *cmdline_options; List *guc_options; + /* + * The startup packet application name, only used here for the "connection + * authorized" log message. We shouldn't use this post-startup, instead the + * GUC should be used as application can change it afterward. + */ + char *application_name; + /* * Information that needs to be held during the authentication cycle. */ -- 2.19.0