On Wed, Jan 18, 2012 at 9:19 AM, Jim Mlodgenski <jimm...@gmail.com> wrote: > On Wed, Jan 18, 2012 at 3:08 AM, Heikki Linnakangas > <heikki.linnakan...@enterprisedb.com> wrote: >> On 18.01.2012 07:49, Fujii Masao wrote: >>> >>> On Fri, Jan 6, 2012 at 1:38 AM, Jim Mlodgenski<jimm...@gmail.com> wrote: >>>> >>>> I have a need to send banner messages to a psql client that I can set >>>> on the server and will be displayed on any psql client that connects >>>> to the database. This would be mostly used as an additional indicator >>>> to which database you are connecting, but could also be used by people >>>> to force their users to see an security message when connecting to the >>>> database. The attached patch will allow you to execute >>>> >>>> ALTER DATABASE postgres SET >>>> >>>> client_message=E'********************************************************************************\nBEWARE: >>>> You are connecting to a production database. If you do anything to\n >>>> bring this server down, you will be destroyed by your supreme >>>> >>>> overlord.\n********************************************************************************\n'; >>>> >>>> And then when you connect to psql, you will see: >>>> >>>> [e3@workstation bin]$ ./psql -U user1 postgres >>>> psql (9.2devel) >>>> >>>> ******************************************************************************** >>>> BEWARE: You are connecting to a production database. If you do anything >>>> to >>>> bring this server down, you will be destroyed by your supreme >>>> overlord. >>>> >>>> ******************************************************************************** >>>> >>>> Type "help" for help. >>>> >>>> postgres=> >>>> >>>> >>>> Any feedback is welcome. >>> >>> >>> Adding new GUC parameter only for the purpose of warning psql users >>> seems overkill to me. Basically we try to reduce the number of GUC >>> parameters to make a configuration easier to a user, so I don't think that >>> it's good idea to add new GUC for such a small benefit. >> >> >> It seems quite useful to me... >> >> >>> Instead, how >>> about using .psqlrc file and writing a warning message in it by using >>> \echo command? >> >> >> That's not the same thing at all. Each client would need to put the warning >> in that file, and you'd get it regardless of the database you connect to. >> >> >>> Anyway, I found one problem in the patch. The patch defines client_message >>> as PGC_USERSET parameter, which means that any psql can falsify a >>> warning message, e.g., by setting the environment variable PGOPTIONS >>> to "-c client_message=hoge". This seems to be something to avoid from >>> security point of view. >> >> >> I don't think that's a problem, it's just a free-form message to display. >> But it also doesn't seem very useful to have it PGC_USERSET: if it's only >> displayed at connect time, there's no point in changing it after connecting. > Should we make it PGC_BACKEND? > >> >> The only security problem that I can think of is a malicious server >> (man-in-the-middle perhaps), that sends a banner that confuses >> >> Docs for PQparameterStatus() needs adjustment, now that client_message is >> also one of the settings automatically reported to the client. > I'll add the docs for that.. > >> >> The placement of the banner in psql looks currently like this: >> >>> $ psql postgres >>> >>> psql (9.2devel) >>> Hello world! >>> Type "help" for help. >> >> >> or >> >>> postgres=# \c postgres >>> Hello world! >>> You are now connected to database "postgres" as user "heikki". >> >> >> Are we happy with that? I think it would be better to print the banner just >> before the prompt: > I like that better. I'll make that change as well.
Here is the revised patch based on the feedback. > >> >>> psql (9.2devel) >>> Type "help" for help. >>> >>> Hello world! >>> >>> postgres=# \c postgres >>> You are now connected to database "postgres" as user "heikki". >> >>> Hello world! >>> postgres=# >> >> Should we prefix the banner with something that makes it clear that it's a >> message coming from the server? Something like: > I don't think the default prefix adds much for the user. If the > administrator wants to let the user know that its from the server, he > can add it to the message. > >> >>> psql (9.2devel) >>> Type "help" for help. >>> >>> Notice from server: Hello world! >>> >>> postgres=# \c postgres >>> You are now connected to database "postgres" as user "heikki". >>> Notice from server: Hello world! >>> postgres=# >> >> -- >> Heikki Linnakangas >> EnterpriseDB http://www.enterprisedb.com
diff --git a/doc/src/sgml/config.sgml b/doc/src/sgml/config.sgml index e55b503..04bc671 100644 --- a/doc/src/sgml/config.sgml +++ b/doc/src/sgml/config.sgml @@ -5324,6 +5324,19 @@ dynamic_library_path = 'C:\tools\postgresql;H:\my_project\lib;$libdir' </listitem> </varlistentry> + <varlistentry id="guc-client-message" xreflabel="client_message"> + <term><varname>client_message</varname> (<type>string</type>)</term> + <indexterm> + <primary><varname>client_message</> configuration parameter</primary> + </indexterm> + <listitem> + <para> + The <varname>client_message</varname> can be any string that will be + displayed to the user in the banner of psql. + </para> + </listitem> + </varlistentry> + </variablelist> </sect2> </sect1> diff --git a/doc/src/sgml/libpq.sgml b/doc/src/sgml/libpq.sgml index 72c9384..7dcb0bb 100644 --- a/doc/src/sgml/libpq.sgml +++ b/doc/src/sgml/libpq.sgml @@ -1487,6 +1487,7 @@ const char *PQparameterStatus(const PGconn *conn, const char *paramName); <varname>server_encoding</>, <varname>client_encoding</>, <varname>application_name</>, + <varname>client_message</>, <varname>is_superuser</>, <varname>session_authorization</>, <varname>DateStyle</>, @@ -1499,7 +1500,8 @@ const char *PQparameterStatus(const PGconn *conn, const char *paramName); <varname>standard_conforming_strings</> was not reported by releases before 8.1; <varname>IntervalStyle</> was not reported by releases before 8.4; - <varname>application_name</> was not reported by releases before 9.0.) + <varname>application_name</> was not reported by releases before 9.0; + <varname>client_message</> was not reported by releases before 9.2.) Note that <varname>server_version</>, <varname>server_encoding</> and diff --git a/src/backend/utils/misc/guc.c b/src/backend/utils/misc/guc.c index 9fc96b2..39177cc 100644 --- a/src/backend/utils/misc/guc.c +++ b/src/backend/utils/misc/guc.c @@ -455,6 +455,7 @@ static char *log_destination_string; static char *syslog_ident_str; static bool phony_autocommit; static bool session_auth_is_superuser; +static char *client_message_string; static double phony_random_seed; static char *client_encoding_string; static char *datestyle_string; @@ -3018,6 +3019,17 @@ static struct config_string ConfigureNamesString[] = check_application_name, assign_application_name, NULL }, + { + {"client_message", PGC_BACKEND, CLIENT_CONN_OTHER, + gettext_noop("Sets a message to be displayed to the user when connecting via psql."), + NULL, + GUC_REPORT | GUC_NO_SHOW_ALL + }, + &client_message_string, + "", + NULL, NULL, NULL + }, + /* End-of-list marker */ { {NULL, 0, 0, NULL, NULL}, NULL, NULL, NULL, NULL, NULL diff --git a/src/backend/utils/misc/postgresql.conf.sample b/src/backend/utils/misc/postgresql.conf.sample index 315db46..8eb5af5 100644 --- a/src/backend/utils/misc/postgresql.conf.sample +++ b/src/backend/utils/misc/postgresql.conf.sample @@ -515,6 +515,7 @@ #dynamic_library_path = '$libdir' #local_preload_libraries = '' +#client_message = '' #------------------------------------------------------------------------------ diff --git a/src/bin/psql/command.c b/src/bin/psql/command.c index 6c3f0aa..cd90a64 100644 --- a/src/bin/psql/command.c +++ b/src/bin/psql/command.c @@ -1653,6 +1653,8 @@ do_connect(char *dbname, char *user, char *host, char *port) else printf(_("You are now connected to database \"%s\" as user \"%s\".\n"), PQdb(pset.db), PQuser(pset.db)); + + printClientMessage(); } if (o_conn) @@ -1707,6 +1709,23 @@ connection_warnings(bool in_startup) /* + * printClientMessage + * + * Prints any message stored in the client_message GUC + */ +void +printClientMessage(void) +{ + const char *message; + + message = PQparameterStatus(pset.db, "client_message"); + + if (message) + printf(_("%s\n"), message); +} + + +/* * printSSLInfo * * Prints information about the current SSL connection, if SSL is in use diff --git a/src/bin/psql/command.h b/src/bin/psql/command.h index f0bcea0..0e37386 100644 --- a/src/bin/psql/command.h +++ b/src/bin/psql/command.h @@ -36,6 +36,8 @@ extern bool do_pset(const char *param, extern void connection_warnings(bool in_startup); +extern void printClientMessage(void); + extern void SyncVariables(void); extern void UnsyncVariables(void); diff --git a/src/bin/psql/startup.c b/src/bin/psql/startup.c index 8b1864c..b04daa1 100644 --- a/src/bin/psql/startup.c +++ b/src/bin/psql/startup.c @@ -302,7 +302,10 @@ main(int argc, char *argv[]) connection_warnings(true); if (!pset.quiet && !pset.notty) + { printf(_("Type \"help\" for help.\n\n")); + printClientMessage(); + } if (!pset.notty) initializeInput(options.no_readline ? 0 : 1); if (options.action_string) /* -f - was used */
-- Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-hackers