PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
dumputils.h File Reference
#include "libpq-fe.h"
#include "pqexpbuffer.h"
Include dependency graph for dumputils.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Macros

#define PGDUMP_STRFTIME_FMT   "%Y-%m-%d %H:%M:%S %Z"
 

Functions

bool buildACLCommands (const char *name, const char *subname, const char *type, const char *acls, const char *racls, const char *owner, const char *prefix, int remoteVersion, PQExpBuffer sql)
 
bool buildDefaultACLCommands (const char *type, const char *nspname, const char *acls, const char *racls, const char *initacls, const char *initracls, const char *owner, int remoteVersion, PQExpBuffer sql)
 
void buildShSecLabelQuery (PGconn *conn, const char *catalog_name, uint32 objectId, PQExpBuffer sql)
 
void emitShSecLabels (PGconn *conn, PGresult *res, PQExpBuffer buffer, const char *target, const char *objname)
 
void buildACLQueries (PQExpBuffer acl_subquery, PQExpBuffer racl_subquery, PQExpBuffer init_acl_subquery, PQExpBuffer init_racl_subquery, const char *acl_column, const char *acl_owner, const char *obj_kind, bool binary_upgrade)
 

Macro Definition Documentation

#define PGDUMP_STRFTIME_FMT   "%Y-%m-%d %H:%M:%S %Z"

Definition at line 33 of file dumputils.h.

Referenced by dumpTimestamp(), and PrintTOCSummary().

Function Documentation

bool buildACLCommands ( const char *  name,
const char *  subname,
const char *  type,
const char *  acls,
const char *  racls,
const char *  owner,
const char *  prefix,
int  remoteVersion,
PQExpBuffer  sql 
)

Definition at line 55 of file dumputils.c.

References appendPQExpBuffer(), appendPQExpBufferStr(), Assert, createPQExpBuffer(), PQExpBufferData::data, destroyPQExpBuffer(), fmtId(), free, i, PQExpBufferData::len, NULL, parseAclItem(), parsePGArray(), and printfPQExpBuffer().

Referenced by buildDefaultACLCommands(), dumpACL(), dumpCreateDB(), and dumpTablespaces().

59 {
60  bool ok = true;
61  char **aclitems = NULL;
62  char **raclitems = NULL;
63  int naclitems = 0;
64  int nraclitems = 0;
65  int i;
66  PQExpBuffer grantee,
67  grantor,
68  privs,
69  privswgo;
70  PQExpBuffer firstsql,
71  secondsql;
72  bool found_owner_privs = false;
73 
74  if (strlen(acls) == 0 && strlen(racls) == 0)
75  return true; /* object has default permissions */
76 
77  /* treat empty-string owner same as NULL */
78  if (owner && *owner == '\0')
79  owner = NULL;
80 
81  if (strlen(acls) != 0)
82  {
83  if (!parsePGArray(acls, &aclitems, &naclitems))
84  {
85  if (aclitems)
86  free(aclitems);
87  return false;
88  }
89  }
90 
91  if (strlen(racls) != 0)
92  {
93  if (!parsePGArray(racls, &raclitems, &nraclitems))
94  {
95  if (raclitems)
96  free(raclitems);
97  return false;
98  }
99  }
100 
101  grantee = createPQExpBuffer();
102  grantor = createPQExpBuffer();
103  privs = createPQExpBuffer();
104  privswgo = createPQExpBuffer();
105 
106  /*
107  * At the end, these two will be pasted together to form the result.
108  *
109  * For older systems we use these to ensure that the owner privileges go
110  * before the other ones, as a GRANT could create the default entry for
111  * the object, which generally includes all rights for the owner. In more
112  * recent versions we normally handle this because the owner rights come
113  * first in the ACLs, but older versions might have them after the PUBLIC
114  * privileges.
115  *
116  * For 9.6 and later systems, much of this changes. With 9.6, we check
117  * the default privileges for the objects at dump time and create two sets
118  * of ACLs- "racls" which are the ACLs to REVOKE from the object (as the
119  * object may have initial privileges on it, along with any default ACLs
120  * which are not part of the current set of privileges), and regular
121  * "acls", which are the ACLs to GRANT to the object. We handle the
122  * REVOKEs first, followed by the GRANTs.
123  */
124  firstsql = createPQExpBuffer();
125  secondsql = createPQExpBuffer();
126 
127  /*
128  * For pre-9.6 systems, we always start with REVOKE ALL FROM PUBLIC, as we
129  * don't wish to make any assumptions about what the default ACLs are, and
130  * we do not collect them during the dump phase (and racls will always be
131  * the empty set, see above).
132  *
133  * For 9.6 and later, if any revoke ACLs have been provided, then include
134  * them in 'firstsql'.
135  *
136  * Revoke ACLs happen when an object starts out life with a set of
137  * privileges (eg: GRANT SELECT ON pg_class TO PUBLIC;) and the user has
138  * decided to revoke those rights. Since those objects come into being
139  * with those default privileges, we have to revoke them to match what the
140  * current state of affairs is. Note that we only started explicitly
141  * tracking such initial rights in 9.6, and prior to that all initial
142  * rights are actually handled by the simple 'REVOKE ALL .. FROM PUBLIC'
143  * case, for initdb-created objects. Prior to 9.6, we didn't handle
144  * extensions correctly, but we do now by tracking their initial
145  * privileges, in the same way we track initdb initial privileges, see
146  * pg_init_privs.
147  */
148  if (remoteVersion < 90600)
149  {
150  Assert(nraclitems == 0);
151 
152  appendPQExpBuffer(firstsql, "%sREVOKE ALL", prefix);
153  if (subname)
154  appendPQExpBuffer(firstsql, "(%s)", subname);
155  appendPQExpBuffer(firstsql, " ON %s %s FROM PUBLIC;\n", type, name);
156  }
157  else
158  {
159  /* Scan individual REVOKE ACL items */
160  for (i = 0; i < nraclitems; i++)
161  {
162  if (!parseAclItem(raclitems[i], type, name, subname, remoteVersion,
163  grantee, grantor, privs, privswgo))
164  {
165  ok = false;
166  break;
167  }
168 
169  if (privs->len > 0 || privswgo->len > 0)
170  {
171  if (privs->len > 0)
172  {
173  appendPQExpBuffer(firstsql, "%sREVOKE %s ON %s %s FROM ",
174  prefix, privs->data, type, name);
175  if (grantee->len == 0)
176  appendPQExpBufferStr(firstsql, "PUBLIC;\n");
177  else if (strncmp(grantee->data, "group ",
178  strlen("group ")) == 0)
179  appendPQExpBuffer(firstsql, "GROUP %s;\n",
180  fmtId(grantee->data + strlen("group ")));
181  else
182  appendPQExpBuffer(firstsql, "%s;\n",
183  fmtId(grantee->data));
184  }
185  if (privswgo->len > 0)
186  {
187  appendPQExpBuffer(firstsql,
188  "%sREVOKE GRANT OPTION FOR %s ON %s %s FROM ",
189  prefix, privswgo->data, type, name);
190  if (grantee->len == 0)
191  appendPQExpBufferStr(firstsql, "PUBLIC");
192  else if (strncmp(grantee->data, "group ",
193  strlen("group ")) == 0)
194  appendPQExpBuffer(firstsql, "GROUP %s",
195  fmtId(grantee->data + strlen("group ")));
196  else
197  appendPQExpBufferStr(firstsql, fmtId(grantee->data));
198  }
199  }
200  }
201  }
202 
203  /*
204  * We still need some hacking though to cover the case where new default
205  * public privileges are added in new versions: the REVOKE ALL will revoke
206  * them, leading to behavior different from what the old version had,
207  * which is generally not what's wanted. So add back default privs if the
208  * source database is too old to have had that particular priv.
209  */
210  if (remoteVersion < 80200 && strcmp(type, "DATABASE") == 0)
211  {
212  /* database CONNECT priv didn't exist before 8.2 */
213  appendPQExpBuffer(firstsql, "%sGRANT CONNECT ON %s %s TO PUBLIC;\n",
214  prefix, type, name);
215  }
216 
217  /* Scan individual ACL items */
218  for (i = 0; i < naclitems; i++)
219  {
220  if (!parseAclItem(aclitems[i], type, name, subname, remoteVersion,
221  grantee, grantor, privs, privswgo))
222  {
223  ok = false;
224  break;
225  }
226 
227  if (grantor->len == 0 && owner)
228  printfPQExpBuffer(grantor, "%s", owner);
229 
230  if (privs->len > 0 || privswgo->len > 0)
231  {
232  /*
233  * Prior to 9.6, we had to handle owner privileges in a special
234  * manner by first REVOKE'ing the rights and then GRANT'ing them
235  * after. With 9.6 and above, what we need to REVOKE and what we
236  * need to GRANT is figured out when we dump and stashed into
237  * "racls" and "acls", respectively. See above.
238  */
239  if (remoteVersion < 90600 && owner
240  && strcmp(grantee->data, owner) == 0
241  && strcmp(grantor->data, owner) == 0)
242  {
243  found_owner_privs = true;
244 
245  /*
246  * For the owner, the default privilege level is ALL WITH
247  * GRANT OPTION.
248  */
249  if (strcmp(privswgo->data, "ALL") != 0)
250  {
251  appendPQExpBuffer(firstsql, "%sREVOKE ALL", prefix);
252  if (subname)
253  appendPQExpBuffer(firstsql, "(%s)", subname);
254  appendPQExpBuffer(firstsql, " ON %s %s FROM %s;\n",
255  type, name, fmtId(grantee->data));
256  if (privs->len > 0)
257  appendPQExpBuffer(firstsql,
258  "%sGRANT %s ON %s %s TO %s;\n",
259  prefix, privs->data, type, name,
260  fmtId(grantee->data));
261  if (privswgo->len > 0)
262  appendPQExpBuffer(firstsql,
263  "%sGRANT %s ON %s %s TO %s WITH GRANT OPTION;\n",
264  prefix, privswgo->data, type, name,
265  fmtId(grantee->data));
266  }
267  }
268  else
269  {
270  /*
271  * For systems prior to 9.6, we can assume we are starting
272  * from no privs at this point.
273  *
274  * For 9.6 and above, at this point we have issued REVOKE
275  * statements for all initial and default privileges which are
276  * no longer present on the object (as they were passed in as
277  * 'racls') and we can simply GRANT the rights which are in
278  * 'acls'.
279  */
280  if (grantor->len > 0
281  && (!owner || strcmp(owner, grantor->data) != 0))
282  appendPQExpBuffer(secondsql, "SET SESSION AUTHORIZATION %s;\n",
283  fmtId(grantor->data));
284 
285  if (privs->len > 0)
286  {
287  appendPQExpBuffer(secondsql, "%sGRANT %s ON %s %s TO ",
288  prefix, privs->data, type, name);
289  if (grantee->len == 0)
290  appendPQExpBufferStr(secondsql, "PUBLIC;\n");
291  else if (strncmp(grantee->data, "group ",
292  strlen("group ")) == 0)
293  appendPQExpBuffer(secondsql, "GROUP %s;\n",
294  fmtId(grantee->data + strlen("group ")));
295  else
296  appendPQExpBuffer(secondsql, "%s;\n", fmtId(grantee->data));
297  }
298  if (privswgo->len > 0)
299  {
300  appendPQExpBuffer(secondsql, "%sGRANT %s ON %s %s TO ",
301  prefix, privswgo->data, type, name);
302  if (grantee->len == 0)
303  appendPQExpBufferStr(secondsql, "PUBLIC");
304  else if (strncmp(grantee->data, "group ",
305  strlen("group ")) == 0)
306  appendPQExpBuffer(secondsql, "GROUP %s",
307  fmtId(grantee->data + strlen("group ")));
308  else
309  appendPQExpBufferStr(secondsql, fmtId(grantee->data));
310  appendPQExpBufferStr(secondsql, " WITH GRANT OPTION;\n");
311  }
312 
313  if (grantor->len > 0
314  && (!owner || strcmp(owner, grantor->data) != 0))
315  appendPQExpBufferStr(secondsql, "RESET SESSION AUTHORIZATION;\n");
316  }
317  }
318  }
319 
320  /*
321  * For systems prior to 9.6, if we didn't find any owner privs, the owner
322  * must have revoked 'em all.
323  *
324  * For 9.6 and above, we handle this through the 'racls'. See above.
325  */
326  if (remoteVersion < 90600 && !found_owner_privs && owner)
327  {
328  appendPQExpBuffer(firstsql, "%sREVOKE ALL", prefix);
329  if (subname)
330  appendPQExpBuffer(firstsql, "(%s)", subname);
331  appendPQExpBuffer(firstsql, " ON %s %s FROM %s;\n",
332  type, name, fmtId(owner));
333  }
334 
335  destroyPQExpBuffer(grantee);
336  destroyPQExpBuffer(grantor);
337  destroyPQExpBuffer(privs);
338  destroyPQExpBuffer(privswgo);
339 
340  appendPQExpBuffer(sql, "%s%s", firstsql->data, secondsql->data);
341  destroyPQExpBuffer(firstsql);
342  destroyPQExpBuffer(secondsql);
343 
344  if (aclitems)
345  free(aclitems);
346 
347  if (raclitems)
348  free(raclitems);
349 
350  return ok;
351 }
void printfPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:234
bool parsePGArray(const char *atext, char ***itemarray, int *nitems)
Definition: string_utils.c:663
const char * fmtId(const char *rawid)
Definition: string_utils.c:66
void appendPQExpBufferStr(PQExpBuffer str, const char *data)
Definition: pqexpbuffer.c:385
void destroyPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:113
void appendPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:262
PQExpBuffer createPQExpBuffer(void)
Definition: pqexpbuffer.c:71
#define free(a)
Definition: header.h:65
#define NULL
Definition: c.h:229
#define Assert(condition)
Definition: c.h:676
const char * name
Definition: encode.c:521
int i
static bool parseAclItem(const char *item, const char *type, const char *name, const char *subname, int remoteVersion, PQExpBuffer grantee, PQExpBuffer grantor, PQExpBuffer privs, PQExpBuffer privswgo)
Definition: dumputils.c:432
void buildACLQueries ( PQExpBuffer  acl_subquery,
PQExpBuffer  racl_subquery,
PQExpBuffer  init_acl_subquery,
PQExpBuffer  init_racl_subquery,
const char *  acl_column,
const char *  acl_owner,
const char *  obj_kind,
bool  binary_upgrade 
)

Definition at line 695 of file dumputils.c.

References printfPQExpBuffer().

Referenced by dumpTable(), getAggregates(), getBlobs(), getDefaultACLs(), getForeignDataWrappers(), getForeignServers(), getFuncs(), getNamespaces(), getProcLangs(), getTables(), and getTypes().

699 {
700  /*
701  * To get the delta from what the permissions were at creation time
702  * (either initdb or CREATE EXTENSION) vs. what they are now, we have to
703  * look at two things:
704  *
705  * What privileges have been added, which we calculate by extracting all
706  * the current privileges (using the set of default privileges for the
707  * object type if current privileges are NULL) and then removing those
708  * which existed at creation time (again, using the set of default
709  * privileges for the object type if there were no creation time
710  * privileges).
711  *
712  * What privileges have been removed, which we calculate by extracting the
713  * privileges as they were at creation time (or the default privileges, as
714  * above), and then removing the current privileges (or the default
715  * privileges, if current privileges are NULL).
716  *
717  * As a good cross-check, both directions of these checks should result in
718  * the empty set if both the current ACL and the initial privs are NULL
719  * (meaning, in practice, that the default ACLs were there at init time
720  * and is what the current privileges are).
721  *
722  * We always perform this delta on all ACLs and expect that by the time
723  * these are run the initial privileges will be in place, even in a binary
724  * upgrade situation (see below).
725  */
726  printfPQExpBuffer(acl_subquery, "(SELECT pg_catalog.array_agg(acl) FROM "
727  "(SELECT pg_catalog.unnest(coalesce(%s,pg_catalog.acldefault(%s,%s))) AS acl "
728  "EXCEPT "
729  "SELECT pg_catalog.unnest(coalesce(pip.initprivs,pg_catalog.acldefault(%s,%s)))) as foo)",
730  acl_column,
731  obj_kind,
732  acl_owner,
733  obj_kind,
734  acl_owner);
735 
736  printfPQExpBuffer(racl_subquery, "(SELECT pg_catalog.array_agg(acl) FROM "
737  "(SELECT pg_catalog.unnest(coalesce(pip.initprivs,pg_catalog.acldefault(%s,%s))) AS acl "
738  "EXCEPT "
739  "SELECT pg_catalog.unnest(coalesce(%s,pg_catalog.acldefault(%s,%s)))) as foo)",
740  obj_kind,
741  acl_owner,
742  acl_column,
743  obj_kind,
744  acl_owner);
745 
746  /*
747  * In binary upgrade mode we don't run the extension script but instead
748  * dump out the objects independently and then recreate them. To preserve
749  * the initial privileges which were set on extension objects, we need to
750  * grab the set of GRANT and REVOKE commands necessary to get from the
751  * default privileges of an object to the initial privileges as recorded
752  * in pg_init_privs.
753  *
754  * These will then be run ahead of the regular ACL commands, which were
755  * calculated using the queries above, inside of a block which sets a flag
756  * to indicate that the backend should record the results of these GRANT
757  * and REVOKE statements into pg_init_privs. This is how we preserve the
758  * contents of that catalog across binary upgrades.
759  */
760  if (binary_upgrade)
761  {
762  printfPQExpBuffer(init_acl_subquery,
763  "CASE WHEN privtype = 'e' THEN "
764  "(SELECT pg_catalog.array_agg(acl) FROM "
765  "(SELECT pg_catalog.unnest(pip.initprivs) AS acl "
766  "EXCEPT "
767  "SELECT pg_catalog.unnest(pg_catalog.acldefault(%s,%s))) as foo) END",
768  obj_kind,
769  acl_owner);
770 
771  printfPQExpBuffer(init_racl_subquery,
772  "CASE WHEN privtype = 'e' THEN "
773  "(SELECT pg_catalog.array_agg(acl) FROM "
774  "(SELECT pg_catalog.unnest(pg_catalog.acldefault(%s,%s)) AS acl "
775  "EXCEPT "
776  "SELECT pg_catalog.unnest(pip.initprivs)) as foo) END",
777  obj_kind,
778  acl_owner);
779  }
780  else
781  {
782  printfPQExpBuffer(init_acl_subquery, "NULL");
783  printfPQExpBuffer(init_racl_subquery, "NULL");
784  }
785 }
void printfPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:234
static int binary_upgrade
Definition: pg_dumpall.c:69
bool buildDefaultACLCommands ( const char *  type,
const char *  nspname,
const char *  acls,
const char *  racls,
const char *  initacls,
const char *  initracls,
const char *  owner,
int  remoteVersion,
PQExpBuffer  sql 
)

Definition at line 366 of file dumputils.c.

References appendPQExpBuffer(), buildACLCommands(), createPQExpBuffer(), PQExpBufferData::data, destroyPQExpBuffer(), fmtId(), and NULL.

Referenced by dumpDefaultACL().

372 {
373  PQExpBuffer prefix;
374 
375  prefix = createPQExpBuffer();
376 
377  /*
378  * We incorporate the target role directly into the command, rather than
379  * playing around with SET ROLE or anything like that. This is so that a
380  * permissions error leads to nothing happening, rather than changing
381  * default privileges for the wrong user.
382  */
383  appendPQExpBuffer(prefix, "ALTER DEFAULT PRIVILEGES FOR ROLE %s ",
384  fmtId(owner));
385  if (nspname)
386  appendPQExpBuffer(prefix, "IN SCHEMA %s ", fmtId(nspname));
387 
388  if (strlen(initacls) != 0 || strlen(initracls) != 0)
389  {
390  appendPQExpBuffer(sql, "SELECT pg_catalog.binary_upgrade_set_record_init_privs(true);\n");
391  if (!buildACLCommands("", NULL, type, initacls, initracls, owner,
392  prefix->data, remoteVersion, sql))
393  {
394  destroyPQExpBuffer(prefix);
395  return false;
396  }
397  appendPQExpBuffer(sql, "SELECT pg_catalog.binary_upgrade_set_record_init_privs(false);\n");
398  }
399 
400  if (!buildACLCommands("", NULL, type, acls, racls, owner,
401  prefix->data, remoteVersion, sql))
402  {
403  destroyPQExpBuffer(prefix);
404  return false;
405  }
406 
407  destroyPQExpBuffer(prefix);
408 
409  return true;
410 }
const char * fmtId(const char *rawid)
Definition: string_utils.c:66
void destroyPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:113
void appendPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:262
PQExpBuffer createPQExpBuffer(void)
Definition: pqexpbuffer.c:71
#define NULL
Definition: c.h:229
bool buildACLCommands(const char *name, const char *subname, const char *type, const char *acls, const char *racls, const char *owner, const char *prefix, int remoteVersion, PQExpBuffer sql)
Definition: dumputils.c:55
void buildShSecLabelQuery ( PGconn conn,
const char *  catalog_name,
uint32  objectId,
PQExpBuffer  sql 
)

Definition at line 643 of file dumputils.c.

References appendPQExpBuffer().

Referenced by buildShSecLabels(), and dumpDatabase().

645 {
646  appendPQExpBuffer(sql,
647  "SELECT provider, label FROM pg_catalog.pg_shseclabel "
648  "WHERE classoid = '%s'::pg_catalog.regclass AND "
649  "objoid = %u", catalog_name, objectId);
650 }
void appendPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:262
void emitShSecLabels ( PGconn conn,
PGresult res,
PQExpBuffer  buffer,
const char *  target,
const char *  objname 
)

Definition at line 659 of file dumputils.c.

References appendPQExpBuffer(), appendPQExpBufferStr(), appendStringLiteralConn(), fmtId(), i, label, PQgetvalue(), and PQntuples().

Referenced by buildShSecLabels(), and dumpDatabase().

661 {
662  int i;
663 
664  for (i = 0; i < PQntuples(res); i++)
665  {
666  char *provider = PQgetvalue(res, i, 0);
667  char *label = PQgetvalue(res, i, 1);
668 
669  /* must use fmtId result before calling it again */
670  appendPQExpBuffer(buffer,
671  "SECURITY LABEL FOR %s ON %s",
672  fmtId(provider), target);
673  appendPQExpBuffer(buffer,
674  " %s IS ",
675  fmtId(objname));
676  appendStringLiteralConn(buffer, label, conn);
677  appendPQExpBufferStr(buffer, ";\n");
678  }
679 }
char * PQgetvalue(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3067
const char * fmtId(const char *rawid)
Definition: string_utils.c:66
void appendPQExpBufferStr(PQExpBuffer str, const char *data)
Definition: pqexpbuffer.c:385
int PQntuples(const PGresult *res)
Definition: fe-exec.c:2673
void appendPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:262
static char * label
Definition: pg_basebackup.c:81
void appendStringLiteralConn(PQExpBuffer buf, const char *str, PGconn *conn)
Definition: string_utils.c:298
int i