]> granicus.if.org Git - postgresql/commitdiff
Fix privilege dumping from servers too old to have that type of privilege.
authorTom Lane <tgl@sss.pgh.pa.us>
Tue, 11 Aug 2015 00:10:16 +0000 (20:10 -0400)
committerTom Lane <tgl@sss.pgh.pa.us>
Tue, 11 Aug 2015 00:10:16 +0000 (20:10 -0400)
pg_dump produced fairly silly GRANT/REVOKE commands when dumping types from
pre-9.2 servers, and when dumping functions or procedural languages from
pre-7.3 servers.  Those server versions lack the typacl, proacl, and/or
lanacl columns respectively, and pg_dump substituted default values that
were in fact incorrect.  We ended up revoking all the owner's own
privileges for the object while granting all privileges to PUBLIC.
Of course the owner would then have those privileges again via PUBLIC, so
long as she did not try to revoke PUBLIC's privileges; which may explain
the lack of field reports.  Nonetheless this is pretty silly behavior.

The stakes were raised by my recent patch to make pg_dump dump shell types,
because 9.2 and up pg_dump would proceed to emit bogus GRANT/REVOKE
commands for a shell type if dumping from a pre-9.2 server; and the server
will not accept GRANT/REVOKE commands for a shell type.  (Perhaps it
should, but that's a topic for another day.)  So the resulting dump script
wouldn't load without errors.

The right thing to do is to act as though these objects have default
privileges (null ACL entries), which causes pg_dump to print no
GRANT/REVOKE commands at all for them.  That fixes the silly results
and also dodges the problem with shell types.

In passing, modify getProcLangs() to be less creatively different about
how to handle missing columns when dumping from older server versions.
Every other data-acquisition function in pg_dump does that by substituting
appropriate default values in the version-specific SQL commands, and I see
no reason why this one should march to its own drummer.  Its use of
"SELECT *" was likewise not conformant with anyplace else, not to mention
it's not considered good SQL style for production queries.

Back-patch to all supported versions.  Although 9.0 and 9.1 pg_dump don't
have the issue with typacl, they are more likely than newer versions to be
used to dump from ancient servers, so we ought to fix the proacl/lanacl
issues all the way back.

src/bin/pg_dump/pg_dump.c

index a7d106d44b2f4550a5ed286b432bdfb2c7dbc5ef..02d1221b455144b62a0a921d43ecb5bcf45ec029 100644 (file)
@@ -3645,7 +3645,7 @@ getAggregates(int *numAggs)
                                  "CASE WHEN aggbasetype = 0 THEN 0 ELSE 1 END AS pronargs, "
                                                  "aggbasetype AS proargtypes, "
                                                  "(%s aggowner) AS rolname, "
-                                                 "'{=X}' AS aggacl "
+                                                 "NULL AS aggacl "
                                                  "FROM pg_aggregate "
                                                  "where oid > '%u'::oid",
                                                  username_subquery,
@@ -3660,7 +3660,7 @@ getAggregates(int *numAggs)
                                  "CASE WHEN aggbasetype = 0 THEN 0 ELSE 1 END AS pronargs, "
                                                  "aggbasetype AS proargtypes, "
                                                  "(%s aggowner) AS rolname, "
-                                                 "'{=X}' AS aggacl "
+                                                 "NULL AS aggacl "
                                                  "FROM pg_aggregate "
                                                  "where oid > '%u'::oid",
                                                  username_subquery,
@@ -3791,7 +3791,7 @@ getFuncs(int *numFuncs)
                appendPQExpBuffer(query,
                                                  "SELECT tableoid, oid, proname, prolang, "
                                                  "pronargs, proargtypes, prorettype, "
-                                                 "'{=X}' AS proacl, "
+                                                 "NULL AS proacl, "
                                                  "0::oid AS pronamespace, "
                                                  "(%s proowner) AS rolname "
                                                  "FROM pg_proc "
@@ -3807,7 +3807,7 @@ getFuncs(int *numFuncs)
                                                  " WHERE relname = 'pg_proc') AS tableoid, "
                                                  "oid, proname, prolang, "
                                                  "pronargs, proargtypes, prorettype, "
-                                                 "'{=X}' AS proacl, "
+                                                 "NULL AS proacl, "
                                                  "0::oid AS pronamespace, "
                                                  "(%s proowner) AS rolname "
                                                  "FROM pg_proc "
@@ -5374,7 +5374,7 @@ getProcLangs(int *numProcLangs)
                /* pg_language has a laninline column */
                appendPQExpBuffer(query, "SELECT tableoid, oid, "
                                                  "lanname, lanpltrusted, lanplcallfoid, "
-                                                 "laninline, lanvalidator,  lanacl, "
+                                                 "laninline, lanvalidator, lanacl, "
                                                  "(%s lanowner) AS lanowner "
                                                  "FROM pg_language "
                                                  "WHERE lanispl "
@@ -5386,7 +5386,7 @@ getProcLangs(int *numProcLangs)
                /* pg_language has a lanowner column */
                appendPQExpBuffer(query, "SELECT tableoid, oid, "
                                                  "lanname, lanpltrusted, lanplcallfoid, "
-                                                 "lanvalidator,  lanacl, "
+                                                 "0 AS laninline, lanvalidator, lanacl, "
                                                  "(%s lanowner) AS lanowner "
                                                  "FROM pg_language "
                                                  "WHERE lanispl "
@@ -5396,7 +5396,9 @@ getProcLangs(int *numProcLangs)
        else if (g_fout->remoteVersion >= 80100)
        {
                /* Languages are owned by the bootstrap superuser, OID 10 */
-               appendPQExpBuffer(query, "SELECT tableoid, oid, *, "
+               appendPQExpBuffer(query, "SELECT tableoid, oid, "
+                                                 "lanname, lanpltrusted, lanplcallfoid, "
+                                                 "0 AS laninline, lanvalidator, lanacl, "
                                                  "(%s '10') AS lanowner "
                                                  "FROM pg_language "
                                                  "WHERE lanispl "
@@ -5406,17 +5408,33 @@ getProcLangs(int *numProcLangs)
        else if (g_fout->remoteVersion >= 70400)
        {
                /* Languages are owned by the bootstrap superuser, sysid 1 */
-               appendPQExpBuffer(query, "SELECT tableoid, oid, *, "
+               appendPQExpBuffer(query, "SELECT tableoid, oid, "
+                                                 "lanname, lanpltrusted, lanplcallfoid, "
+                                                 "0 AS laninline, lanvalidator, lanacl, "
                                                  "(%s '1') AS lanowner "
                                                  "FROM pg_language "
                                                  "WHERE lanispl "
                                                  "ORDER BY oid",
                                                  username_subquery);
        }
-       else if (g_fout->remoteVersion >= 70100)
+       else if (g_fout->remoteVersion >= 70300)
        {
                /* No clear notion of an owner at all before 7.4 ... */
-               appendPQExpBuffer(query, "SELECT tableoid, oid, * FROM pg_language "
+               appendPQExpBuffer(query, "SELECT tableoid, oid, "
+                                                 "lanname, lanpltrusted, lanplcallfoid, "
+                                                 "0 AS laninline, lanvalidator, lanacl, "
+                                                 "NULL AS lanowner "
+                                                 "FROM pg_language "
+                                                 "WHERE lanispl "
+                                                 "ORDER BY oid");
+       }
+       else if (g_fout->remoteVersion >= 70100)
+       {
+               appendPQExpBuffer(query, "SELECT tableoid, oid, "
+                                                 "lanname, lanpltrusted, lanplcallfoid, "
+                                               "0 AS laninline, 0 AS lanvalidator, NULL AS lanacl, "
+                                                 "NULL AS lanowner "
+                                                 "FROM pg_language "
                                                  "WHERE lanispl "
                                                  "ORDER BY oid");
        }
@@ -5424,7 +5442,11 @@ getProcLangs(int *numProcLangs)
        {
                appendPQExpBuffer(query, "SELECT "
                                                  "(SELECT oid FROM pg_class WHERE relname = 'pg_language') AS tableoid, "
-                                                 "oid, * FROM pg_language "
+                                                 "oid, "
+                                                 "lanname, lanpltrusted, lanplcallfoid, "
+                                               "0 AS laninline, 0 AS lanvalidator, NULL AS lanacl, "
+                                                 "NULL AS lanowner "
+                                                 "FROM pg_language "
                                                  "WHERE lanispl "
                                                  "ORDER BY oid");
        }
@@ -5443,7 +5465,6 @@ getProcLangs(int *numProcLangs)
        i_lanname = PQfnumber(res, "lanname");
        i_lanpltrusted = PQfnumber(res, "lanpltrusted");
        i_lanplcallfoid = PQfnumber(res, "lanplcallfoid");
-       /* these may fail and return -1: */
        i_laninline = PQfnumber(res, "laninline");
        i_lanvalidator = PQfnumber(res, "lanvalidator");
        i_lanacl = PQfnumber(res, "lanacl");
@@ -5459,22 +5480,10 @@ getProcLangs(int *numProcLangs)
                planginfo[i].dobj.name = strdup(PQgetvalue(res, i, i_lanname));
                planginfo[i].lanpltrusted = *(PQgetvalue(res, i, i_lanpltrusted)) == 't';
                planginfo[i].lanplcallfoid = atooid(PQgetvalue(res, i, i_lanplcallfoid));
-               if (i_laninline >= 0)
-                       planginfo[i].laninline = atooid(PQgetvalue(res, i, i_laninline));
-               else
-                       planginfo[i].laninline = InvalidOid;
-               if (i_lanvalidator >= 0)
-                       planginfo[i].lanvalidator = atooid(PQgetvalue(res, i, i_lanvalidator));
-               else
-                       planginfo[i].lanvalidator = InvalidOid;
-               if (i_lanacl >= 0)
-                       planginfo[i].lanacl = strdup(PQgetvalue(res, i, i_lanacl));
-               else
-                       planginfo[i].lanacl = strdup("{=U}");
-               if (i_lanowner >= 0)
-                       planginfo[i].lanowner = strdup(PQgetvalue(res, i, i_lanowner));
-               else
-                       planginfo[i].lanowner = strdup("");
+               planginfo[i].laninline = atooid(PQgetvalue(res, i, i_laninline));
+               planginfo[i].lanvalidator = atooid(PQgetvalue(res, i, i_lanvalidator));
+               planginfo[i].lanacl = strdup(PQgetvalue(res, i, i_lanacl));
+               planginfo[i].lanowner = strdup(PQgetvalue(res, i, i_lanowner));
 
                if (g_fout->remoteVersion < 70300)
                {