]> git.ipfire.org Git - thirdparty/postgresql.git/commitdiff
Obstruct shell, SQL, and conninfo injection via database and role names.
authorNoah Misch <noah@leadboat.com>
Mon, 8 Aug 2016 14:07:46 +0000 (10:07 -0400)
committerNoah Misch <noah@leadboat.com>
Mon, 8 Aug 2016 14:07:53 +0000 (10:07 -0400)
Due to simplistic quoting and confusion of database names with conninfo
strings, roles with the CREATEDB or CREATEROLE option could escalate to
superuser privileges when a superuser next ran certain maintenance
commands.  The new coding rule for PQconnectdbParams() calls, documented
at conninfo_array_parse(), is to pass expand_dbname=true and wrap
literal database names in a trivial connection string.  Escape
zero-length values in appendConnStrVal().  Back-patch to 9.1 (all
supported versions).

Nathan Bossart, Michael Paquier, and Noah Misch.  Reviewed by Peter
Eisentraut.  Reported by Nathan Bossart.

Security: CVE-2016-5424

19 files changed:
contrib/pg_upgrade/pg_upgrade.h
contrib/pg_upgrade/server.c
contrib/pg_upgrade/test.sh
contrib/pg_upgrade/util.c
contrib/pg_upgrade/version.c
contrib/pg_upgrade/version_old_8_3.c
src/bin/pg_dump/dumputils.c
src/bin/pg_dump/dumputils.h
src/bin/pg_dump/pg_backup.h
src/bin/pg_dump/pg_backup_archiver.c
src/bin/pg_dump/pg_backup_db.c
src/bin/pg_dump/pg_dumpall.c
src/bin/psql/command.c
src/bin/scripts/Makefile
src/bin/scripts/clusterdb.c
src/bin/scripts/reindexdb.c
src/bin/scripts/vacuumdb.c
src/interfaces/libpq/fe-connect.c
src/tools/msvc/vcregress.pl

index e958f2bd18c58c116a8444c8833be54724747b25..8026dfc645e8d8a785e8ff7e1fce09f06c161482 100644 (file)
@@ -12,6 +12,7 @@
 #include <sys/time.h>
 
 #include "libpq-fe.h"
+#include "pqexpbuffer.h"
 
 /* Use port in the private/dynamic port number range */
 #define DEF_PGUPORT                    50432
@@ -427,6 +428,9 @@ void                check_pghost_envvar(void);
 /* util.c */
 
 char      *quote_identifier(const char *s);
+extern void appendShellString(PQExpBuffer buf, const char *str);
+extern void appendConnStrVal(PQExpBuffer buf, const char *str);
+extern void appendPsqlMetaConnect(PQExpBuffer buf, const char *dbname);
 int                    get_user_info(char **user_name);
 void           check_ok(void);
 void
index 22555330684528dfc56dc4fec5a18d814267ba9b..60d387324d966ea62c889307a1e2f7f0d7984ba6 100644 (file)
@@ -51,18 +51,25 @@ connectToServer(ClusterInfo *cluster, const char *db_name)
 static PGconn *
 get_db_conn(ClusterInfo *cluster, const char *db_name)
 {
-       char            conn_opts[2 * NAMEDATALEN + MAXPGPATH + 100];
+       PQExpBufferData conn_opts;
+       PGconn     *conn;
 
+       /* Build connection string with proper quoting */
+       initPQExpBuffer(&conn_opts);
+       appendPQExpBufferStr(&conn_opts, "dbname=");
+       appendConnStrVal(&conn_opts, db_name);
+       appendPQExpBufferStr(&conn_opts, " user=");
+       appendConnStrVal(&conn_opts, os_info.user);
+       appendPQExpBuffer(&conn_opts, " port=%d", cluster->port);
        if (cluster->sockdir)
-               snprintf(conn_opts, sizeof(conn_opts),
-                                "dbname = '%s' user = '%s' host = '%s' port = %d",
-                                db_name, os_info.user, cluster->sockdir, cluster->port);
-       else
-               snprintf(conn_opts, sizeof(conn_opts),
-                                "dbname = '%s' user = '%s' port = %d",
-                                db_name, os_info.user, cluster->port);
+       {
+               appendPQExpBufferStr(&conn_opts, " host=");
+               appendConnStrVal(&conn_opts, cluster->sockdir);
+       }
 
-       return PQconnectdb(conn_opts);
+       conn = PQconnectdb(conn_opts.data);
+       termPQExpBuffer(&conn_opts);
+       return conn;
 }
 
 
@@ -74,23 +81,28 @@ get_db_conn(ClusterInfo *cluster, const char *db_name)
  * sets, but the utilities we need aren't very consistent about the treatment
  * of database name options, so we leave that out.
  *
- * Note result is in static storage, so use it right away.
+ * Result is valid until the next call to this function.
  */
 char *
 cluster_conn_opts(ClusterInfo *cluster)
 {
-       static char     conn_opts[MAXPGPATH + NAMEDATALEN + 100];
+       static PQExpBuffer buf;
 
-       if (cluster->sockdir)
-               snprintf(conn_opts, sizeof(conn_opts),
-                                "--host \"%s\" --port %d --username \"%s\"",
-                                cluster->sockdir, cluster->port, os_info.user);
+       if (buf == NULL)
+               buf = createPQExpBuffer();
        else
-               snprintf(conn_opts, sizeof(conn_opts),
-                                "--port %d --username \"%s\"",
-                                cluster->port, os_info.user);
+               resetPQExpBuffer(buf);
+
+       if (cluster->sockdir)
+       {
+               appendPQExpBufferStr(buf, "--host ");
+               appendShellString(buf, cluster->sockdir);
+               appendPQExpBufferChar(buf, ' ');
+       }
+       appendPQExpBuffer(buf, "--port %d --username ", cluster->port);
+       appendShellString(buf, os_info.user);
 
-       return conn_opts;
+       return buf->data;
 }
 
 
index cb3edf8ea69947c1f89f2774c6e4acc69ffb3afa..bf7e64d5bb00a3c0fce8b2bbf5664ec5350cccc7 100644 (file)
@@ -131,6 +131,20 @@ set -x
 
 standard_initdb "$oldbindir"/initdb
 $oldbindir/pg_ctl start -l "$logdir/postmaster1.log" -o "$POSTMASTER_OPTS" -w
+
+# Create databases with names covering the ASCII bytes other than NUL, BEL,
+# LF, or CR.  BEL would ring the terminal bell in the course of this test, and
+# it is not otherwise a special case.  PostgreSQL doesn't support the rest.
+dbname1=`awk 'BEGIN { for (i= 1; i < 46; i++)
+       if (i != 7 && i != 10 && i != 13) printf "%c", i }' </dev/null`
+# Exercise backslashes adjacent to double quotes, a Windows special case.
+dbname1='\"\'$dbname1'\\"\\\'
+dbname2=`awk 'BEGIN { for (i = 46; i <  91; i++) printf "%c", i }' </dev/null`
+dbname3=`awk 'BEGIN { for (i = 91; i < 128; i++) printf "%c", i }' </dev/null`
+createdb "$dbname1" || createdb_status=$?
+createdb "$dbname2" || createdb_status=$?
+createdb "$dbname3" || createdb_status=$?
+
 if "$MAKE" -C "$oldsrc" installcheck; then
        pg_dumpall -f "$temp_root"/dump1.sql || pg_dumpall1_status=$?
        if [ "$newsrc" != "$oldsrc" ]; then
@@ -156,6 +170,9 @@ else
        make_installcheck_status=$?
 fi
 $oldbindir/pg_ctl -m fast stop
+if [ -n "$createdb_status" ]; then
+       exit 1
+fi
 if [ -n "$make_installcheck_status" ]; then
        exit 1
 fi
index d323045deb399d9f755d34fb6aeeb3b5322ee554..17770500fa2bc2008df4c513190d41867328ccbf 100644 (file)
@@ -158,6 +158,210 @@ quote_identifier(const char *s)
 }
 
 
+/*
+ * Append the given string to the shell command being built in the buffer,
+ * with suitable shell-style quoting to create exactly one argument.
+ *
+ * Forbid LF or CR characters, which have scant practical use beyond designing
+ * security breaches.  The Windows command shell is unusable as a conduit for
+ * arguments containing LF or CR characters.  A future major release should
+ * reject those characters in CREATE ROLE and CREATE DATABASE, because use
+ * there eventually leads to errors here.
+ */
+void
+appendShellString(PQExpBuffer buf, const char *str)
+{
+       const char *p;
+
+#ifndef WIN32
+       appendPQExpBufferChar(buf, '\'');
+       for (p = str; *p; p++)
+       {
+               if (*p == '\n' || *p == '\r')
+               {
+                       fprintf(stderr,
+                                       _("shell command argument contains a newline or carriage return: \"%s\"\n"),
+                                       str);
+                       exit(EXIT_FAILURE);
+               }
+
+               if (*p == '\'')
+                       appendPQExpBufferStr(buf, "'\"'\"'");
+               else
+                       appendPQExpBufferChar(buf, *p);
+       }
+       appendPQExpBufferChar(buf, '\'');
+#else                                                  /* WIN32 */
+       int                     backslash_run_length = 0;
+
+       /*
+        * A Windows system() argument experiences two layers of interpretation.
+        * First, cmd.exe interprets the string.  Its behavior is undocumented,
+        * but a caret escapes any byte except LF or CR that would otherwise have
+        * special meaning.  Handling of a caret before LF or CR differs between
+        * "cmd.exe /c" and other modes, and it is unusable here.
+        *
+        * Second, the new process parses its command line to construct argv (see
+        * https://msdn.microsoft.com/en-us/library/17w5ykft.aspx).  This treats
+        * backslash-double quote sequences specially.
+        */
+       appendPQExpBufferStr(buf, "^\"");
+       for (p = str; *p; p++)
+       {
+               if (*p == '\n' || *p == '\r')
+               {
+                       fprintf(stderr,
+                                       _("shell command argument contains a newline or carriage return: \"%s\"\n"),
+                                       str);
+                       exit(EXIT_FAILURE);
+               }
+
+               /* Change N backslashes before a double quote to 2N+1 backslashes. */
+               if (*p == '"')
+               {
+                       while (backslash_run_length)
+                       {
+                               appendPQExpBufferStr(buf, "^\\");
+                               backslash_run_length--;
+                       }
+                       appendPQExpBufferStr(buf, "^\\");
+               }
+               else if (*p == '\\')
+                       backslash_run_length++;
+               else
+                       backslash_run_length = 0;
+
+               /*
+                * Decline to caret-escape the most mundane characters, to ease
+                * debugging and lest we approach the command length limit.
+                */
+               if (!((*p >= 'a' && *p <= 'z') ||
+                         (*p >= 'A' && *p <= 'Z') ||
+                         (*p >= '0' && *p <= '9')))
+                       appendPQExpBufferChar(buf, '^');
+               appendPQExpBufferChar(buf, *p);
+       }
+
+       /*
+        * Change N backslashes at end of argument to 2N backslashes, because they
+        * precede the double quote that terminates the argument.
+        */
+       while (backslash_run_length)
+       {
+               appendPQExpBufferStr(buf, "^\\");
+               backslash_run_length--;
+       }
+       appendPQExpBufferStr(buf, "^\"");
+#endif   /* WIN32 */
+}
+
+
+/*
+ * Append the given string to the buffer, with suitable quoting for passing
+ * the string as a value, in a keyword/pair value in a libpq connection
+ * string
+ */
+void
+appendConnStrVal(PQExpBuffer buf, const char *str)
+{
+       const char *s;
+       bool            needquotes;
+
+       /*
+        * If the string is one or more plain ASCII characters, no need to quote
+        * it. This is quite conservative, but better safe than sorry.
+        */
+       needquotes = true;
+       for (s = str; *s; s++)
+       {
+               if (!((*s >= 'a' && *s <= 'z') || (*s >= 'A' && *s <= 'Z') ||
+                         (*s >= '0' && *s <= '9') || *s == '_' || *s == '.'))
+               {
+                       needquotes = true;
+                       break;
+               }
+               needquotes = false;
+       }
+
+       if (needquotes)
+       {
+               appendPQExpBufferChar(buf, '\'');
+               while (*str)
+               {
+                       /* ' and \ must be escaped by to \' and \\ */
+                       if (*str == '\'' || *str == '\\')
+                               appendPQExpBufferChar(buf, '\\');
+
+                       appendPQExpBufferChar(buf, *str);
+                       str++;
+               }
+               appendPQExpBufferChar(buf, '\'');
+       }
+       else
+               appendPQExpBufferStr(buf, str);
+}
+
+
+/*
+ * Append a psql meta-command that connects to the given database with the
+ * then-current connection's user, host and port.
+ */
+void
+appendPsqlMetaConnect(PQExpBuffer buf, const char *dbname)
+{
+       const char *s;
+       bool            complex;
+
+       /*
+        * If the name is plain ASCII characters, emit a trivial "\connect "foo"".
+        * For other names, even many not technically requiring it, skip to the
+        * general case.  No database has a zero-length name.
+        */
+       complex = false;
+       for (s = dbname; *s; s++)
+       {
+               if (*s == '\n' || *s == '\r')
+               {
+                       fprintf(stderr,
+                                       _("database name contains a newline or carriage return: \"%s\"\n"),
+                                       dbname);
+                       exit(EXIT_FAILURE);
+               }
+
+               if (!((*s >= 'a' && *s <= 'z') || (*s >= 'A' && *s <= 'Z') ||
+                         (*s >= '0' && *s <= '9') || *s == '_' || *s == '.'))
+               {
+                       complex = true;
+               }
+       }
+
+       appendPQExpBufferStr(buf, "\\connect ");
+       if (complex)
+       {
+               PQExpBufferData connstr;
+
+               initPQExpBuffer(&connstr);
+               appendPQExpBuffer(&connstr, "dbname=");
+               appendConnStrVal(&connstr, dbname);
+
+               appendPQExpBuffer(buf, "-reuse-previous=on ");
+
+               /*
+                * As long as the name does not contain a newline, SQL identifier
+                * quoting satisfies the psql meta-command parser.  Prefer not to
+                * involve psql-interpreted single quotes, which behaved differently
+                * before PostgreSQL 9.2.
+                */
+               appendPQExpBufferStr(buf, quote_identifier(connstr.data));
+
+               termPQExpBuffer(&connstr);
+       }
+       else
+               appendPQExpBufferStr(buf, quote_identifier(dbname));
+       appendPQExpBufferChar(buf, '\n');
+}
+
+
 /*
  * get_user_info()
  * (copied from initdb.c) find the current user
index 5d790a0803e2a00cd5fe2e7327dc5963fd6366c0..ecee5b40b2caad2233d9e2997c072740b6a3d4e3 100644 (file)
@@ -48,10 +48,16 @@ new_9_0_populate_pg_largeobject_metadata(ClusterInfo *cluster, bool check_mode)
                        found = true;
                        if (!check_mode)
                        {
+                               PQExpBufferData connectbuf;
+
                                if (script == NULL && (script = fopen_priv(output_path, "w")) == NULL)
                                        pg_log(PG_FATAL, "could not open file \"%s\": %s\n", output_path, getErrorText(errno));
-                               fprintf(script, "\\connect %s\n",
-                                               quote_identifier(active_db->db_name));
+
+                               initPQExpBuffer(&connectbuf);
+                               appendPsqlMetaConnect(&connectbuf, active_db->db_name);
+                               fputs(connectbuf.data, script);
+                               termPQExpBuffer(&connectbuf);
+
                                fprintf(script,
                                                "SELECT pg_catalog.lo_create(t.loid)\n"
                                                "FROM (SELECT DISTINCT loid FROM pg_catalog.pg_largeobject) AS t;\n");
index b681c0984e6e9d0bee6c0a561907d6efbbc3a1a4..4f8d5851834bfcdee281541a92e0d67c2eb2407a 100644 (file)
@@ -364,8 +364,13 @@ old_8_3_rebuild_tsvector_tables(ClusterInfo *cluster, bool check_mode)
                                        pg_log(PG_FATAL, "could not open file \"%s\": %s\n", output_path, getErrorText(errno));
                                if (!db_used)
                                {
-                                       fprintf(script, "\\connect %s\n\n",
-                                                       quote_identifier(active_db->db_name));
+                                       PQExpBufferData connectbuf;
+
+                                       initPQExpBuffer(&connectbuf);
+                                       appendPsqlMetaConnect(&connectbuf, active_db->db_name);
+                                       appendPQExpBufferChar(&connectbuf, '\n');
+                                       fputs(connectbuf.data, script);
+                                       termPQExpBuffer(&connectbuf);
                                        db_used = true;
                                }
 
@@ -480,8 +485,12 @@ old_8_3_invalidate_hash_gin_indexes(ClusterInfo *cluster, bool check_mode)
                                        pg_log(PG_FATAL, "could not open file \"%s\": %s\n", output_path, getErrorText(errno));
                                if (!db_used)
                                {
-                                       fprintf(script, "\\connect %s\n",
-                                                       quote_identifier(active_db->db_name));
+                                       PQExpBufferData connectbuf;
+
+                                       initPQExpBuffer(&connectbuf);
+                                       appendPsqlMetaConnect(&connectbuf, active_db->db_name);
+                                       fputs(connectbuf.data, script);
+                                       termPQExpBuffer(&connectbuf);
                                        db_used = true;
                                }
                                fprintf(script, "REINDEX INDEX %s.%s;\n",
@@ -599,8 +608,12 @@ old_8_3_invalidate_bpchar_pattern_ops_indexes(ClusterInfo *cluster,
                                        pg_log(PG_FATAL, "could not open file \"%s\": %s\n", output_path, getErrorText(errno));
                                if (!db_used)
                                {
-                                       fprintf(script, "\\connect %s\n",
-                                                       quote_identifier(active_db->db_name));
+                                       PQExpBufferData connectbuf;
+
+                                       initPQExpBuffer(&connectbuf);
+                                       appendPsqlMetaConnect(&connectbuf, active_db->db_name);
+                                       fputs(connectbuf.data, script);
+                                       termPQExpBuffer(&connectbuf);
                                        db_used = true;
                                }
                                fprintf(script, "REINDEX INDEX %s.%s;\n",
@@ -721,8 +734,13 @@ old_8_3_create_sequence_script(ClusterInfo *cluster)
                                pg_log(PG_FATAL, "could not open file \"%s\": %s\n", output_path, getErrorText(errno));
                        if (!db_used)
                        {
-                               fprintf(script, "\\connect %s\n\n",
-                                               quote_identifier(active_db->db_name));
+                               PQExpBufferData connectbuf;
+
+                               initPQExpBuffer(&connectbuf);
+                               appendPsqlMetaConnect(&connectbuf, active_db->db_name);
+                               appendPQExpBufferChar(&connectbuf, '\n');
+                               fputs(connectbuf.data, script);
+                               termPQExpBuffer(&connectbuf);
                                db_used = true;
                        }
 
index b4edf216740a06d7f77afaf9e408241619a33dca..faf0a85e4c35ae0d6075dbe3b6de77571c8abfb7 100644 (file)
@@ -347,6 +347,210 @@ appendStringLiteralDQ(PQExpBuffer buf, const char *str, const char *dqprefix)
 }
 
 
+/*
+ * Append the given string to the shell command being built in the buffer,
+ * with suitable shell-style quoting to create exactly one argument.
+ *
+ * Forbid LF or CR characters, which have scant practical use beyond designing
+ * security breaches.  The Windows command shell is unusable as a conduit for
+ * arguments containing LF or CR characters.  A future major release should
+ * reject those characters in CREATE ROLE and CREATE DATABASE, because use
+ * there eventually leads to errors here.
+ */
+void
+appendShellString(PQExpBuffer buf, const char *str)
+{
+       const char *p;
+
+#ifndef WIN32
+       appendPQExpBufferChar(buf, '\'');
+       for (p = str; *p; p++)
+       {
+               if (*p == '\n' || *p == '\r')
+               {
+                       fprintf(stderr,
+                                       _("shell command argument contains a newline or carriage return: \"%s\"\n"),
+                                       str);
+                       exit(EXIT_FAILURE);
+               }
+
+               if (*p == '\'')
+                       appendPQExpBufferStr(buf, "'\"'\"'");
+               else
+                       appendPQExpBufferChar(buf, *p);
+       }
+       appendPQExpBufferChar(buf, '\'');
+#else                                                  /* WIN32 */
+       int                     backslash_run_length = 0;
+
+       /*
+        * A Windows system() argument experiences two layers of interpretation.
+        * First, cmd.exe interprets the string.  Its behavior is undocumented,
+        * but a caret escapes any byte except LF or CR that would otherwise have
+        * special meaning.  Handling of a caret before LF or CR differs between
+        * "cmd.exe /c" and other modes, and it is unusable here.
+        *
+        * Second, the new process parses its command line to construct argv (see
+        * https://msdn.microsoft.com/en-us/library/17w5ykft.aspx).  This treats
+        * backslash-double quote sequences specially.
+        */
+       appendPQExpBufferStr(buf, "^\"");
+       for (p = str; *p; p++)
+       {
+               if (*p == '\n' || *p == '\r')
+               {
+                       fprintf(stderr,
+                                       _("shell command argument contains a newline or carriage return: \"%s\"\n"),
+                                       str);
+                       exit(EXIT_FAILURE);
+               }
+
+               /* Change N backslashes before a double quote to 2N+1 backslashes. */
+               if (*p == '"')
+               {
+                       while (backslash_run_length)
+                       {
+                               appendPQExpBufferStr(buf, "^\\");
+                               backslash_run_length--;
+                       }
+                       appendPQExpBufferStr(buf, "^\\");
+               }
+               else if (*p == '\\')
+                       backslash_run_length++;
+               else
+                       backslash_run_length = 0;
+
+               /*
+                * Decline to caret-escape the most mundane characters, to ease
+                * debugging and lest we approach the command length limit.
+                */
+               if (!((*p >= 'a' && *p <= 'z') ||
+                         (*p >= 'A' && *p <= 'Z') ||
+                         (*p >= '0' && *p <= '9')))
+                       appendPQExpBufferChar(buf, '^');
+               appendPQExpBufferChar(buf, *p);
+       }
+
+       /*
+        * Change N backslashes at end of argument to 2N backslashes, because they
+        * precede the double quote that terminates the argument.
+        */
+       while (backslash_run_length)
+       {
+               appendPQExpBufferStr(buf, "^\\");
+               backslash_run_length--;
+       }
+       appendPQExpBufferStr(buf, "^\"");
+#endif   /* WIN32 */
+}
+
+
+/*
+ * Append the given string to the buffer, with suitable quoting for passing
+ * the string as a value, in a keyword/pair value in a libpq connection
+ * string
+ */
+void
+appendConnStrVal(PQExpBuffer buf, const char *str)
+{
+       const char *s;
+       bool            needquotes;
+
+       /*
+        * If the string is one or more plain ASCII characters, no need to quote
+        * it. This is quite conservative, but better safe than sorry.
+        */
+       needquotes = true;
+       for (s = str; *s; s++)
+       {
+               if (!((*s >= 'a' && *s <= 'z') || (*s >= 'A' && *s <= 'Z') ||
+                         (*s >= '0' && *s <= '9') || *s == '_' || *s == '.'))
+               {
+                       needquotes = true;
+                       break;
+               }
+               needquotes = false;
+       }
+
+       if (needquotes)
+       {
+               appendPQExpBufferChar(buf, '\'');
+               while (*str)
+               {
+                       /* ' and \ must be escaped by to \' and \\ */
+                       if (*str == '\'' || *str == '\\')
+                               appendPQExpBufferChar(buf, '\\');
+
+                       appendPQExpBufferChar(buf, *str);
+                       str++;
+               }
+               appendPQExpBufferChar(buf, '\'');
+       }
+       else
+               appendPQExpBufferStr(buf, str);
+}
+
+
+/*
+ * Append a psql meta-command that connects to the given database with the
+ * then-current connection's user, host and port.
+ */
+void
+appendPsqlMetaConnect(PQExpBuffer buf, const char *dbname)
+{
+       const char *s;
+       bool            complex;
+
+       /*
+        * If the name is plain ASCII characters, emit a trivial "\connect "foo"".
+        * For other names, even many not technically requiring it, skip to the
+        * general case.  No database has a zero-length name.
+        */
+       complex = false;
+       for (s = dbname; *s; s++)
+       {
+               if (*s == '\n' || *s == '\r')
+               {
+                       fprintf(stderr,
+                                       _("database name contains a newline or carriage return: \"%s\"\n"),
+                                       dbname);
+                       exit(EXIT_FAILURE);
+               }
+
+               if (!((*s >= 'a' && *s <= 'z') || (*s >= 'A' && *s <= 'Z') ||
+                         (*s >= '0' && *s <= '9') || *s == '_' || *s == '.'))
+               {
+                       complex = true;
+               }
+       }
+
+       appendPQExpBufferStr(buf, "\\connect ");
+       if (complex)
+       {
+               PQExpBufferData connstr;
+
+               initPQExpBuffer(&connstr);
+               appendPQExpBuffer(&connstr, "dbname=");
+               appendConnStrVal(&connstr, dbname);
+
+               appendPQExpBuffer(buf, "-reuse-previous=on ");
+
+               /*
+                * As long as the name does not contain a newline, SQL identifier
+                * quoting satisfies the psql meta-command parser.  Prefer not to
+                * involve psql-interpreted single quotes, which behaved differently
+                * before PostgreSQL 9.2.
+                */
+               appendPQExpBufferStr(buf, fmtId(connstr.data));
+
+               termPQExpBuffer(&connstr);
+       }
+       else
+               appendPQExpBufferStr(buf, fmtId(dbname));
+       appendPQExpBufferChar(buf, '\n');
+}
+
+
 /*
  * Convert a bytea value (presented as raw bytes) to an SQL string literal
  * and append it to the given buffer.  We assume the specified
index 4ef8cb3a4903bc2e9bdca967d76dd051bcb6e692..76ed3ab0aa92cc57dad9fe762d01c3d3d8278e29 100644 (file)
@@ -44,6 +44,9 @@ extern void appendByteaLiteral(PQExpBuffer buf,
                                   const unsigned char *str, size_t length,
                                   bool std_strings);
 extern int     parse_version(const char *versionString);
+extern void appendShellString(PQExpBuffer buf, const char *str);
+extern void appendConnStrVal(PQExpBuffer buf, const char *str);
+extern void appendPsqlMetaConnect(PQExpBuffer buf, const char *dbname);
 extern bool parsePGArray(const char *atext, char ***itemarray, int *nitems);
 extern bool buildACLCommands(const char *name, const char *subname,
                                 const char *type, const char *acls, const char *owner,
index 3b49395ecbc2b2adc2f9f459dbd00263f229824d..e0c2bd3cb5277e9b2b34853124bb4ecf8c30c174 100644 (file)
@@ -130,7 +130,7 @@ typedef struct _restoreOptions
        char       *triggerNames;
 
        int                     useDB;
-       char       *dbname;
+       char       *dbname;                     /* subject to expand_dbname */
        char       *pgport;
        char       *pghost;
        char       *username;
index 5057ce0368abf0002e80a4adb77865331a99541e..45e90140126ac24f37b4b07fcf1608a797022558 100644 (file)
@@ -640,9 +640,16 @@ restore_toc_entry(ArchiveHandle *AH, TocEntry *te,
                /* If we created a DB, connect to it... */
                if (strcmp(te->desc, "DATABASE") == 0)
                {
+                       PQExpBufferData connstr;
+
+                       initPQExpBuffer(&connstr);
+                       appendPQExpBufferStr(&connstr, "dbname=");
+                       appendConnStrVal(&connstr, te->tag);
+                       /* Abandon struct, but keep its buffer until process exit. */
+
                        ahlog(AH, 1, "connecting to new database \"%s\"\n", te->tag);
                        _reconnectToDB(AH, te->tag);
-                       ropt->dbname = pg_strdup(te->tag);
+                       ropt->dbname = connstr.data;
                }
        }
 
@@ -2721,12 +2728,17 @@ _reconnectToDB(ArchiveHandle *AH, const char *dbname)
                ReconnectToServer(AH, dbname, NULL);
        else
        {
-               PQExpBuffer qry = createPQExpBuffer();
+               if (dbname)
+               {
+                       PQExpBufferData connectbuf;
 
-               appendPQExpBuffer(qry, "\\connect %s\n\n",
-                                                 dbname ? fmtId(dbname) : "-");
-               ahprintf(AH, "%s", qry->data);
-               destroyPQExpBuffer(qry);
+                       initPQExpBuffer(&connectbuf);
+                       appendPsqlMetaConnect(&connectbuf, dbname);
+                       ahprintf(AH, "%s\n", connectbuf.data);
+                       termPQExpBuffer(&connectbuf);
+               }
+               else
+                       ahprintf(AH, "%s\n", "\\connect -\n");
        }
 
        /*
index 1600c0e8f43e7cb89a642667b95eeb831a367836..b20fcd45a0c020804a85d14b4988facc7611fb69 100644 (file)
@@ -123,6 +123,7 @@ ReconnectToServer(ArchiveHandle *AH, const char *dbname, const char *username)
 static PGconn *
 _connectDB(ArchiveHandle *AH, const char *reqdb, const char *requser)
 {
+       PQExpBufferData connstr;
        PGconn     *newConn;
        const char *newdb;
        const char *newuser;
@@ -149,6 +150,10 @@ _connectDB(ArchiveHandle *AH, const char *reqdb, const char *requser)
                        exit_horribly(modulename, "out of memory\n");
        }
 
+       initPQExpBuffer(&connstr);
+       appendPQExpBuffer(&connstr, "dbname=");
+       appendConnStrVal(&connstr, newdb);
+
        do
        {
 #define PARAMS_ARRAY_SIZE      7
@@ -164,7 +169,7 @@ _connectDB(ArchiveHandle *AH, const char *reqdb, const char *requser)
                keywords[3] = "password";
                values[3] = password;
                keywords[4] = "dbname";
-               values[4] = newdb;
+               values[4] = connstr.data;
                keywords[5] = "fallback_application_name";
                values[5] = progname;
                keywords[6] = NULL;
@@ -208,6 +213,8 @@ _connectDB(ArchiveHandle *AH, const char *reqdb, const char *requser)
 
        AH->savedPassword = password;
 
+       termPQExpBuffer(&connstr);
+
        /* check for version mismatch */
        _check_database_version(AH);
 
index 591f74a4cad0d22a13449bc9eb9a75890f8f2422..2a4fb19ebfa76f092a8506fea7a6ed2eb501f59c 100644 (file)
@@ -48,8 +48,6 @@ static void makeAlterConfigCommand(PGconn *conn, const char *arrayitem,
                                           const char *name2);
 static void dumpDatabases(PGconn *conn);
 static void dumpTimestamp(char *msg);
-static void appendShellString(PQExpBuffer buf, const char *str);
-static void appendConnStrVal(PQExpBuffer buf, const char *str);
 
 static int     runPgDump(const char *dbname);
 static void buildShSecLabels(PGconn *conn, const char *catalog_name,
@@ -1376,8 +1374,9 @@ dumpCreateDB(PGconn *conn)
                                                          fdbname, fmtId(dbtablespace));
 
                        /* connect to original database */
-                       appendPQExpBuffer(buf, "%s\\connect %s\n",
-                               binary_upgrade ? " " : "", fdbname);
+                       if (binary_upgrade)
+                               appendPQExpBufferChar(buf, ' ');
+                       appendPsqlMetaConnect(buf, dbname);
                }
 
                if (binary_upgrade)
@@ -1605,11 +1604,15 @@ dumpDatabases(PGconn *conn)
                int                     ret;
 
                char       *dbname = PQgetvalue(res, i, 0);
+               PQExpBufferData connectbuf;
 
                if (verbose)
                        fprintf(stderr, _("%s: dumping database \"%s\"...\n"), progname, dbname);
 
-               fprintf(OPF, "\\connect %s\n\n", fmtId(dbname));
+               initPQExpBuffer(&connectbuf);
+               appendPsqlMetaConnect(&connectbuf, dbname);
+               fprintf(OPF, "%s\n", connectbuf.data);
+               termPQExpBuffer(&connectbuf);
 
                /*
                 * Restore will need to write to the target cluster.  This connection
@@ -1932,145 +1935,3 @@ dumpTimestamp(char *msg)
                                 localtime(&now)) != 0)
                fprintf(OPF, "-- %s %s\n\n", msg, buf);
 }
-
-
-/*
- * Append the given string to the buffer, with suitable quoting for passing
- * the string as a value, in a keyword/pair value in a libpq connection
- * string
- */
-static void
-appendConnStrVal(PQExpBuffer buf, const char *str)
-{
-       const char *s;
-       bool            needquotes;
-
-       /*
-        * If the string consists entirely of plain ASCII characters, no need to
-        * quote it. This is quite conservative, but better safe than sorry.
-        */
-       needquotes = false;
-       for (s = str; *s; s++)
-       {
-               if (!((*s >= 'a' && *s <= 'z') || (*s >= 'A' && *s <= 'Z') ||
-                         (*s >= '0' && *s <= '9') || *s == '_' || *s == '.'))
-               {
-                       needquotes = true;
-                       break;
-               }
-       }
-
-       if (needquotes)
-       {
-               appendPQExpBufferChar(buf, '\'');
-               while (*str)
-               {
-                       /* ' and \ must be escaped by to \' and \\ */
-                       if (*str == '\'' || *str == '\\')
-                               appendPQExpBufferChar(buf, '\\');
-
-                       appendPQExpBufferChar(buf, *str);
-                       str++;
-               }
-               appendPQExpBufferChar(buf, '\'');
-       }
-       else
-               appendPQExpBufferStr(buf, str);
-}
-
-/*
- * Append the given string to the shell command being built in the buffer,
- * with suitable shell-style quoting to create exactly one argument.
- *
- * Forbid LF or CR characters, which have scant practical use beyond designing
- * security breaches.  The Windows command shell is unusable as a conduit for
- * arguments containing LF or CR characters.  A future major release should
- * reject those characters in CREATE ROLE and CREATE DATABASE, because use
- * there eventually leads to errors here.
- */
-static void
-appendShellString(PQExpBuffer buf, const char *str)
-{
-       const char *p;
-
-#ifndef WIN32
-       appendPQExpBufferChar(buf, '\'');
-       for (p = str; *p; p++)
-       {
-               if (*p == '\n' || *p == '\r')
-               {
-                       fprintf(stderr,
-                                       _("shell command argument contains a newline or carriage return: \"%s\"\n"),
-                                       str);
-                       exit(EXIT_FAILURE);
-               }
-
-               if (*p == '\'')
-                       appendPQExpBuffer(buf, "'\"'\"'");
-               else
-                       appendPQExpBufferChar(buf, *p);
-       }
-       appendPQExpBufferChar(buf, '\'');
-#else                                                  /* WIN32 */
-       int                     backslash_run_length = 0;
-
-       /*
-        * A Windows system() argument experiences two layers of interpretation.
-        * First, cmd.exe interprets the string.  Its behavior is undocumented,
-        * but a caret escapes any byte except LF or CR that would otherwise have
-        * special meaning.  Handling of a caret before LF or CR differs between
-        * "cmd.exe /c" and other modes, and it is unusable here.
-        *
-        * Second, the new process parses its command line to construct argv (see
-        * https://msdn.microsoft.com/en-us/library/17w5ykft.aspx).  This treats
-        * backslash-double quote sequences specially.
-        */
-       appendPQExpBufferStr(buf, "^\"");
-       for (p = str; *p; p++)
-       {
-               if (*p == '\n' || *p == '\r')
-               {
-                       fprintf(stderr,
-                                       _("shell command argument contains a newline or carriage return: \"%s\"\n"),
-                                       str);
-                       exit(EXIT_FAILURE);
-               }
-
-               /* Change N backslashes before a double quote to 2N+1 backslashes. */
-               if (*p == '"')
-               {
-                       while (backslash_run_length)
-                       {
-                               appendPQExpBufferStr(buf, "^\\");
-                               backslash_run_length--;
-                       }
-                       appendPQExpBufferStr(buf, "^\\");
-               }
-               else if (*p == '\\')
-                       backslash_run_length++;
-               else
-                       backslash_run_length = 0;
-
-               /*
-                * Decline to caret-escape the most mundane characters, to ease
-                * debugging and lest we approach the command length limit.
-                */
-               if (!((*p >= 'a' && *p <= 'z') ||
-                         (*p >= 'A' && *p <= 'Z') ||
-                         (*p >= '0' && *p <= '9')))
-                       appendPQExpBufferChar(buf, '^');
-               appendPQExpBufferChar(buf, *p);
-       }
-
-       /*
-        * Change N backslashes at end of argument to 2N backslashes, because they
-        * precede the double quote that terminates the argument.
-        */
-       while (backslash_run_length)
-       {
-               appendPQExpBufferStr(buf, "^\\");
-               backslash_run_length--;
-       }
-       appendPQExpBufferStr(buf, "^\"");
-#endif   /* WIN32 */
-}
index 4be60b9ca96d1ce2d12f3edfbdfed83b5a2b4a73..3bcf2f0dbc5dcb4fe7edc04e73821bcbd56bf674 100644 (file)
@@ -1524,6 +1524,7 @@ do_connect(enum trivalue reuse_previous_specification,
        bool            keep_password;
        bool            has_connection_string;
        bool            reuse_previous;
+       PQExpBufferData connstr;
 
        has_connection_string = dbname ?
                recognized_connection_string(dbname) : false;
@@ -1575,7 +1576,15 @@ do_connect(enum trivalue reuse_previous_specification,
         * changes: passwords aren't (usually) database-specific.
         */
        if (!dbname && reuse_previous)
-               dbname = PQdb(o_conn);
+       {
+               initPQExpBuffer(&connstr);
+               appendPQExpBuffer(&connstr, "dbname=");
+               appendConnStrVal(&connstr, PQdb(o_conn));
+               dbname = connstr.data;
+               /* has_connection_string=true would be a dead store */
+       }
+       else
+               connstr.data = NULL;
 
        /*
         * If the user asked to be prompted for a password, ask for one now. If
@@ -1680,8 +1689,12 @@ do_connect(enum trivalue reuse_previous_specification,
                }
 
                PQfinish(n_conn);
+               if (connstr.data)
+                       termPQExpBuffer(&connstr);
                return false;
        }
+       if (connstr.data)
+               termPQExpBuffer(&connstr);
 
        /*
         * Replace the old connection with the new one, and update
index 0980b4ca40c60a7810794f52b2535e68be71f838..d9ba29da878cdc1c312c567b4e3f45f68f56a909 100644 (file)
@@ -32,7 +32,7 @@ dropdb: dropdb.o common.o dumputils.o kwlookup.o keywords.o | submake-libpq
 droplang: droplang.o common.o print.o mbprint.o | submake-libpq
 dropuser: dropuser.o common.o dumputils.o kwlookup.o keywords.o | submake-libpq
 clusterdb: clusterdb.o common.o dumputils.o kwlookup.o keywords.o | submake-libpq
-vacuumdb: vacuumdb.o common.o | submake-libpq
+vacuumdb: vacuumdb.o common.o dumputils.o kwlookup.o keywords.o | submake-libpq
 reindexdb: reindexdb.o common.o dumputils.o kwlookup.o keywords.o | submake-libpq
 
 dumputils.c keywords.c: % : $(top_srcdir)/src/bin/pg_dump/%
index 8755d60db7370936d3c426fc66a4ea4fcdb1dbcc..60c39e6f68ca45ffc2e8e5d355fb0751d1acf414 100644 (file)
@@ -216,6 +216,7 @@ cluster_all_databases(bool verbose, const char *maintenance_db,
 {
        PGconn     *conn;
        PGresult   *result;
+       PQExpBufferData connstr;
        int                     i;
 
        conn = connectMaintenanceDatabase(maintenance_db, host, port, username,
@@ -223,6 +224,7 @@ cluster_all_databases(bool verbose, const char *maintenance_db,
        result = executeQuery(conn, "SELECT datname FROM pg_database WHERE datallowconn ORDER BY 1;", progname, echo);
        PQfinish(conn);
 
+       initPQExpBuffer(&connstr);
        for (i = 0; i < PQntuples(result); i++)
        {
                char       *dbname = PQgetvalue(result, i, 0);
@@ -233,10 +235,15 @@ cluster_all_databases(bool verbose, const char *maintenance_db,
                        fflush(stdout);
                }
 
-               cluster_one_database(dbname, verbose, NULL,
+               resetPQExpBuffer(&connstr);
+               appendPQExpBuffer(&connstr, "dbname=");
+               appendConnStrVal(&connstr, dbname);
+
+               cluster_one_database(connstr.data, verbose, NULL,
                                                         host, port, username, prompt_password,
                                                         progname, echo);
        }
+       termPQExpBuffer(&connstr);
 
        PQclear(result);
 }
index 7132f377cb546497db00101c0d7fb5a9088e4cbe..1497b58881eb0f30039fdd3e7777d46cd7e5ac1a 100644 (file)
@@ -271,6 +271,7 @@ reindex_all_databases(const char *maintenance_db,
 {
        PGconn     *conn;
        PGresult   *result;
+       PQExpBufferData connstr;
        int                     i;
 
        conn = connectMaintenanceDatabase(maintenance_db, host, port, username,
@@ -278,6 +279,7 @@ reindex_all_databases(const char *maintenance_db,
        result = executeQuery(conn, "SELECT datname FROM pg_database WHERE datallowconn ORDER BY 1;", progname, echo);
        PQfinish(conn);
 
+       initPQExpBuffer(&connstr);
        for (i = 0; i < PQntuples(result); i++)
        {
                char       *dbname = PQgetvalue(result, i, 0);
@@ -288,9 +290,15 @@ reindex_all_databases(const char *maintenance_db,
                        fflush(stdout);
                }
 
-               reindex_one_database(dbname, dbname, "DATABASE", host, port, username,
-                                                        prompt_password, progname, echo);
+               resetPQExpBuffer(&connstr);
+               appendPQExpBuffer(&connstr, "dbname=");
+               appendConnStrVal(&connstr, dbname);
+
+               reindex_one_database(NULL, connstr.data, "DATABASE", host,
+                                                        port, username, prompt_password,
+                                                        progname, echo);
        }
+       termPQExpBuffer(&connstr);
 
        PQclear(result);
 }
@@ -308,7 +316,7 @@ reindex_system_catalogs(const char *dbname, const char *host, const char *port,
 
        initPQExpBuffer(&sql);
 
-       appendPQExpBuffer(&sql, "REINDEX SYSTEM %s;\n", PQdb(conn));
+       appendPQExpBuffer(&sql, "REINDEX SYSTEM %s;\n", fmtId(PQdb(conn)));
 
        if (!executeMaintenanceCommand(conn, sql.data, echo))
        {
index 2cacfdae3e6026df6ac14e2c2f65712968cfd93f..45ebc978523b11dcd76a8df3d6c3ea36c64b1463 100644 (file)
@@ -12,6 +12,7 @@
 
 #include "postgres_fe.h"
 #include "common.h"
+#include "dumputils.h"
 
 
 static void vacuum_one_database(const char *dbname, bool full, bool verbose,
@@ -309,6 +310,7 @@ vacuum_all_databases(bool full, bool verbose, bool and_analyze, bool analyze_onl
 {
        PGconn     *conn;
        PGresult   *result;
+       PQExpBufferData connstr;
        int                     i;
 
        conn = connectMaintenanceDatabase(maintenance_db, host, port,
@@ -316,6 +318,7 @@ vacuum_all_databases(bool full, bool verbose, bool and_analyze, bool analyze_onl
        result = executeQuery(conn, "SELECT datname FROM pg_database WHERE datallowconn ORDER BY 1;", progname, echo);
        PQfinish(conn);
 
+       initPQExpBuffer(&connstr);
        for (i = 0; i < PQntuples(result); i++)
        {
                char       *dbname = PQgetvalue(result, i, 0);
@@ -326,10 +329,16 @@ vacuum_all_databases(bool full, bool verbose, bool and_analyze, bool analyze_onl
                        fflush(stdout);
                }
 
-               vacuum_one_database(dbname, full, verbose, and_analyze, analyze_only,
+               resetPQExpBuffer(&connstr);
+               appendPQExpBuffer(&connstr, "dbname=");
+               appendConnStrVal(&connstr, PQgetvalue(result, i, 0));
+
+               vacuum_one_database(connstr.data, full, verbose, and_analyze,
+                                                       analyze_only,
                                                 freeze, NULL, host, port, username, prompt_password,
                                                        progname, echo);
        }
+       termPQExpBuffer(&connstr);
 
        PQclear(result);
 }
index 30f3c539d42ce9552c55c2f212b2834bf83d677e..553cf7660f78ed97cb4c968430eeead5917d526c 100644 (file)
@@ -4403,7 +4403,11 @@ conninfo_parse(const char *conninfo, PQExpBuffer errorMessage,
  * of "dbname" keyword is a connection string (as indicated by
  * recognized_connection_string) then parse and process it, overriding any
  * previously processed conflicting keywords. Subsequent keywords will take
- * precedence, however.
+ * precedence, however. In-tree programs generally specify expand_dbname=true,
+ * so command-line arguments naming a database can use a connection string.
+ * Some code acquires arbitrary database names from known-literal sources like
+ * PQdb(), PQconninfoParse() and pg_database.datname.  When connecting to such
+ * a database, in-tree code first wraps the name in a connection string.
  */
 static PQconninfoOption *
 conninfo_array_parse(const char *const * keywords, const char *const * values,
index e1d6e71eda0828929a8cb5cf98435fb4db101c2d..851622a5cb4eb4e1852d3c941f19c18cabb61a90 100644 (file)
@@ -250,6 +250,41 @@ sub standard_initdb
                        $ENV{PGDATA}) == 0);
 }
 
+# This is similar to appendShellString().  Perl system(@args) bypasses
+# cmd.exe, so omit the caret escape layer.
+sub quote_system_arg
+{
+       my $arg = shift;
+
+       # Change N >= 0 backslashes before a double quote to 2N+1 backslashes.
+       $arg =~ s/(\\*)"/${\($1 . $1)}\\"/gs;
+
+       # Change N >= 1 backslashes at end of argument to 2N backslashes.
+       $arg =~ s/(\\+)$/${\($1 . $1)}/gs;
+
+       # Wrap the whole thing in unescaped double quotes.
+       return "\"$arg\"";
+}
+
+# Generate a database with a name made of a range of ASCII characters, useful
+# for testing pg_upgrade.
+sub generate_db
+{
+       my ($prefix, $from_char, $to_char, $suffix) = @_;
+
+       my $dbname = $prefix;
+       for my $i ($from_char .. $to_char)
+       {
+               next if $i == 7 || $i == 10 || $i == 13;    # skip BEL, LF, and CR
+               $dbname = $dbname . sprintf('%c', $i);
+       }
+       $dbname .= $suffix;
+
+       system('createdb', quote_system_arg($dbname));
+       my $status = $? >> 8;
+       exit $status if $status;
+}
+
 sub upgradecheck
 {
        my $status;
@@ -282,6 +317,12 @@ sub upgradecheck
        print "\nStarting old cluster\n\n";
        my @args = ('pg_ctl', 'start', '-l', "$logdir/postmaster1.log", '-w');
        system(@args) == 0 or exit 1;
+
+       print "\nCreating databases with names covering most ASCII bytes\n\n";
+       generate_db("\\\"\\", 1,  45,  "\\\\\"\\\\\\");
+       generate_db('',       46, 90,  '');
+       generate_db('',       91, 127, '');
+
        print "\nSetting up data for upgrading\n\n";
        installcheck();
        # now we can chdir into the source dir