PostgreSQL Source Code  git master
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 *nspname, const char *type, const char *acls, const char *baseacls, const char *owner, const char *prefix, int remoteVersion, PQExpBuffer sql)
 
bool buildDefaultACLCommands (const char *type, const char *nspname, const char *acls, const char *acldefault, const char *owner, int remoteVersion, PQExpBuffer sql)
 
void quoteAclUserName (PQExpBuffer output, const char *input)
 
void buildShSecLabelQuery (const char *catalog_name, Oid objectId, PQExpBuffer sql)
 
void emitShSecLabels (PGconn *conn, PGresult *res, PQExpBuffer buffer, const char *objtype, const char *objname)
 
bool variable_is_guc_list_quote (const char *name)
 
bool SplitGUCList (char *rawstring, char separator, char ***namelist)
 
void makeAlterConfigCommand (PGconn *conn, const char *configitem, const char *type, const char *name, const char *type2, const char *name2, PQExpBuffer buf)
 

Macro Definition Documentation

◆ PGDUMP_STRFTIME_FMT

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

Definition at line 33 of file dumputils.h.

Function Documentation

◆ buildACLCommands()

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

Definition at line 64 of file dumputils.c.

68 {
69  bool ok = true;
70  char **aclitems = NULL;
71  char **baseitems = NULL;
72  char **grantitems = NULL;
73  char **revokeitems = NULL;
74  int naclitems = 0;
75  int nbaseitems = 0;
76  int ngrantitems = 0;
77  int nrevokeitems = 0;
78  int i;
79  PQExpBuffer grantee,
80  grantor,
81  privs,
82  privswgo;
83  PQExpBuffer firstsql,
84  secondsql;
85 
86  /*
87  * If the acl was NULL (initial default state), we need do nothing. Note
88  * that this is distinguishable from all-privileges-revoked, which will
89  * look like an empty array ("{}").
90  */
91  if (acls == NULL || *acls == '\0')
92  return true; /* object has default permissions */
93 
94  /* treat empty-string owner same as NULL */
95  if (owner && *owner == '\0')
96  owner = NULL;
97 
98  /* Parse the acls array */
99  if (!parsePGArray(acls, &aclitems, &naclitems))
100  {
101  free(aclitems);
102  return false;
103  }
104 
105  /* Parse the baseacls too */
106  if (!parsePGArray(baseacls, &baseitems, &nbaseitems))
107  {
108  free(aclitems);
109  free(baseitems);
110  return false;
111  }
112 
113  /*
114  * Compare the actual ACL with the base ACL, extracting the privileges
115  * that need to be granted (i.e., are in the actual ACL but not the base
116  * ACL) and the ones that need to be revoked (the reverse). We use plain
117  * string comparisons to check for matches. In principle that could be
118  * fooled by extraneous issues such as whitespace, but since all these
119  * strings are the work of aclitemout(), it should be OK in practice.
120  * Besides, a false mismatch will just cause the output to be a little
121  * more verbose than it really needed to be.
122  */
123  grantitems = (char **) pg_malloc(naclitems * sizeof(char *));
124  for (i = 0; i < naclitems; i++)
125  {
126  bool found = false;
127 
128  for (int j = 0; j < nbaseitems; j++)
129  {
130  if (strcmp(aclitems[i], baseitems[j]) == 0)
131  {
132  found = true;
133  break;
134  }
135  }
136  if (!found)
137  grantitems[ngrantitems++] = aclitems[i];
138  }
139  revokeitems = (char **) pg_malloc(nbaseitems * sizeof(char *));
140  for (i = 0; i < nbaseitems; i++)
141  {
142  bool found = false;
143 
144  for (int j = 0; j < naclitems; j++)
145  {
146  if (strcmp(baseitems[i], aclitems[j]) == 0)
147  {
148  found = true;
149  break;
150  }
151  }
152  if (!found)
153  revokeitems[nrevokeitems++] = baseitems[i];
154  }
155 
156  /* Prepare working buffers */
157  grantee = createPQExpBuffer();
158  grantor = createPQExpBuffer();
159  privs = createPQExpBuffer();
160  privswgo = createPQExpBuffer();
161 
162  /*
163  * At the end, these two will be pasted together to form the result.
164  */
165  firstsql = createPQExpBuffer();
166  secondsql = createPQExpBuffer();
167 
168  /*
169  * Build REVOKE statements for ACLs listed in revokeitems[].
170  */
171  for (i = 0; i < nrevokeitems; i++)
172  {
173  if (!parseAclItem(revokeitems[i],
174  type, name, subname, remoteVersion,
175  grantee, grantor, privs, NULL))
176  {
177  ok = false;
178  break;
179  }
180 
181  if (privs->len > 0)
182  {
183  appendPQExpBuffer(firstsql, "%sREVOKE %s ON %s ",
184  prefix, privs->data, type);
185  if (nspname && *nspname)
186  appendPQExpBuffer(firstsql, "%s.", fmtId(nspname));
187  appendPQExpBuffer(firstsql, "%s FROM ", name);
188  if (grantee->len == 0)
189  appendPQExpBufferStr(firstsql, "PUBLIC;\n");
190  else
191  appendPQExpBuffer(firstsql, "%s;\n",
192  fmtId(grantee->data));
193  }
194  }
195 
196  /*
197  * At this point we have issued REVOKE statements for all initial and
198  * default privileges that are no longer present on the object, so we are
199  * almost ready to GRANT the privileges listed in grantitems[].
200  *
201  * We still need some hacking though to cover the case where new default
202  * public privileges are added in new versions: the REVOKE ALL will revoke
203  * them, leading to behavior different from what the old version had,
204  * which is generally not what's wanted. So add back default privs if the
205  * source database is too old to have had that particular priv. (As of
206  * right now, no such cases exist in supported versions.)
207  */
208 
209  /*
210  * Scan individual ACL items to be granted.
211  *
212  * The order in which privileges appear in the ACL string (the order they
213  * have been GRANT'd in, which the backend maintains) must be preserved to
214  * ensure that GRANTs WITH GRANT OPTION and subsequent GRANTs based on
215  * those are dumped in the correct order. However, some old server
216  * versions will show grants to PUBLIC before the owner's own grants; for
217  * consistency's sake, force the owner's grants to be output first.
218  */
219  for (i = 0; i < ngrantitems; i++)
220  {
221  if (parseAclItem(grantitems[i], type, name, subname, remoteVersion,
222  grantee, grantor, privs, privswgo))
223  {
224  /*
225  * If the grantor isn't the owner, we'll need to use SET SESSION
226  * AUTHORIZATION to become the grantor. Issue the SET/RESET only
227  * if there's something useful to do.
228  */
229  if (privs->len > 0 || privswgo->len > 0)
230  {
231  PQExpBuffer thissql;
232 
233  /* Set owner as grantor if that's not explicit in the ACL */
234  if (grantor->len == 0 && owner)
235  printfPQExpBuffer(grantor, "%s", owner);
236 
237  /* Make sure owner's own grants are output before others */
238  if (owner &&
239  strcmp(grantee->data, owner) == 0 &&
240  strcmp(grantor->data, owner) == 0)
241  thissql = firstsql;
242  else
243  thissql = secondsql;
244 
245  if (grantor->len > 0
246  && (!owner || strcmp(owner, grantor->data) != 0))
247  appendPQExpBuffer(thissql, "SET SESSION AUTHORIZATION %s;\n",
248  fmtId(grantor->data));
249 
250  if (privs->len > 0)
251  {
252  appendPQExpBuffer(thissql, "%sGRANT %s ON %s ",
253  prefix, privs->data, type);
254  if (nspname && *nspname)
255  appendPQExpBuffer(thissql, "%s.", fmtId(nspname));
256  appendPQExpBuffer(thissql, "%s TO ", name);
257  if (grantee->len == 0)
258  appendPQExpBufferStr(thissql, "PUBLIC;\n");
259  else
260  appendPQExpBuffer(thissql, "%s;\n", fmtId(grantee->data));
261  }
262  if (privswgo->len > 0)
263  {
264  appendPQExpBuffer(thissql, "%sGRANT %s ON %s ",
265  prefix, privswgo->data, type);
266  if (nspname && *nspname)
267  appendPQExpBuffer(thissql, "%s.", fmtId(nspname));
268  appendPQExpBuffer(thissql, "%s TO ", name);
269  if (grantee->len == 0)
270  appendPQExpBufferStr(thissql, "PUBLIC");
271  else
272  appendPQExpBufferStr(thissql, fmtId(grantee->data));
273  appendPQExpBufferStr(thissql, " WITH GRANT OPTION;\n");
274  }
275 
276  if (grantor->len > 0
277  && (!owner || strcmp(owner, grantor->data) != 0))
278  appendPQExpBufferStr(thissql, "RESET SESSION AUTHORIZATION;\n");
279  }
280  }
281  else
282  {
283  /* parseAclItem failed, give up */
284  ok = false;
285  break;
286  }
287  }
288 
289  destroyPQExpBuffer(grantee);
290  destroyPQExpBuffer(grantor);
291  destroyPQExpBuffer(privs);
292  destroyPQExpBuffer(privswgo);
293 
294  appendPQExpBuffer(sql, "%s%s", firstsql->data, secondsql->data);
295  destroyPQExpBuffer(firstsql);
296  destroyPQExpBuffer(secondsql);
297 
298  free(aclitems);
299  free(baseitems);
300  free(grantitems);
301  free(revokeitems);
302 
303  return ok;
304 }
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:377
const char * name
Definition: encode.c:561
void * pg_malloc(size_t size)
Definition: fe_memutils.c:47
#define free(a)
Definition: header.h:65
int j
Definition: isn.c:74
int i
Definition: isn.c:73
NameData subname
void printfPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:235
PQExpBuffer createPQExpBuffer(void)
Definition: pqexpbuffer.c:72
void appendPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:265
void destroyPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:114
void appendPQExpBufferStr(PQExpBuffer str, const char *data)
Definition: pqexpbuffer.c:367
const char * fmtId(const char *rawid)
Definition: string_utils.c:64
bool parsePGArray(const char *atext, char ***itemarray, int *nitems)
Definition: string_utils.c:657

References appendPQExpBuffer(), appendPQExpBufferStr(), createPQExpBuffer(), PQExpBufferData::data, destroyPQExpBuffer(), fmtId(), free, i, j, PQExpBufferData::len, name, parseAclItem(), parsePGArray(), pg_malloc(), printfPQExpBuffer(), subname, and generate_unaccent_rules::type.

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

◆ buildDefaultACLCommands()

bool buildDefaultACLCommands ( const char *  type,
const char *  nspname,
const char *  acls,
const char *  acldefault,
const char *  owner,
int  remoteVersion,
PQExpBuffer  sql 
)

Definition at line 320 of file dumputils.c.

325 {
326  PQExpBuffer prefix;
327 
328  prefix = createPQExpBuffer();
329 
330  /*
331  * We incorporate the target role directly into the command, rather than
332  * playing around with SET ROLE or anything like that. This is so that a
333  * permissions error leads to nothing happening, rather than changing
334  * default privileges for the wrong user.
335  */
336  appendPQExpBuffer(prefix, "ALTER DEFAULT PRIVILEGES FOR ROLE %s ",
337  fmtId(owner));
338  if (nspname)
339  appendPQExpBuffer(prefix, "IN SCHEMA %s ", fmtId(nspname));
340 
341  /*
342  * There's no such thing as initprivs for a default ACL, so the base ACL
343  * is always just the object-type-specific default.
344  */
345  if (!buildACLCommands("", NULL, NULL, type,
346  acls, acldefault, owner,
347  prefix->data, remoteVersion, sql))
348  {
349  destroyPQExpBuffer(prefix);
350  return false;
351  }
352 
353  destroyPQExpBuffer(prefix);
354 
355  return true;
356 }
Acl * acldefault(ObjectType objtype, Oid ownerId)
Definition: acl.c:755
bool buildACLCommands(const char *name, const char *subname, const char *nspname, const char *type, const char *acls, const char *baseacls, const char *owner, const char *prefix, int remoteVersion, PQExpBuffer sql)
Definition: dumputils.c:64

References acldefault(), appendPQExpBuffer(), buildACLCommands(), createPQExpBuffer(), PQExpBufferData::data, destroyPQExpBuffer(), fmtId(), and generate_unaccent_rules::type.

Referenced by dumpDefaultACL().

◆ buildShSecLabelQuery()

void buildShSecLabelQuery ( const char *  catalog_name,
Oid  objectId,
PQExpBuffer  sql 
)

Definition at line 632 of file dumputils.c.

634 {
635  appendPQExpBuffer(sql,
636  "SELECT provider, label FROM pg_catalog.pg_shseclabel "
637  "WHERE classoid = 'pg_catalog.%s'::pg_catalog.regclass "
638  "AND objoid = '%u'", catalog_name, objectId);
639 }

References appendPQExpBuffer().

Referenced by buildShSecLabels(), and dumpDatabase().

◆ emitShSecLabels()

void emitShSecLabels ( PGconn conn,
PGresult res,
PQExpBuffer  buffer,
const char *  objtype,
const char *  objname 
)

Definition at line 650 of file dumputils.c.

652 {
653  int i;
654 
655  for (i = 0; i < PQntuples(res); i++)
656  {
657  char *provider = PQgetvalue(res, i, 0);
658  char *label = PQgetvalue(res, i, 1);
659 
660  /* must use fmtId result before calling it again */
661  appendPQExpBuffer(buffer,
662  "SECURITY LABEL FOR %s ON %s",
663  fmtId(provider), objtype);
664  appendPQExpBuffer(buffer,
665  " %s IS ",
666  fmtId(objname));
668  appendPQExpBufferStr(buffer, ";\n");
669  }
670 }
int PQntuples(const PGresult *res)
Definition: fe-exec.c:3310
char * PQgetvalue(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3705
static JitProviderCallbacks provider
Definition: jit.c:43
static char * label
PGconn * conn
Definition: streamutil.c:54
void appendStringLiteralConn(PQExpBuffer buf, const char *str, PGconn *conn)
Definition: string_utils.c:293

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

Referenced by buildShSecLabels(), and dumpDatabase().

◆ makeAlterConfigCommand()

void makeAlterConfigCommand ( PGconn conn,
const char *  configitem,
const char *  type,
const char *  name,
const char *  type2,
const char *  name2,
PQExpBuffer  buf 
)

Definition at line 818 of file dumputils.c.

822 {
823  char *mine;
824  char *pos;
825 
826  /* Parse the configitem. If we can't find an "=", silently do nothing. */
827  mine = pg_strdup(configitem);
828  pos = strchr(mine, '=');
829  if (pos == NULL)
830  {
831  pg_free(mine);
832  return;
833  }
834  *pos++ = '\0';
835 
836  /* Build the command, with suitable quoting for everything. */
837  appendPQExpBuffer(buf, "ALTER %s %s ", type, fmtId(name));
838  if (type2 != NULL && name2 != NULL)
839  appendPQExpBuffer(buf, "IN %s %s ", type2, fmtId(name2));
840  appendPQExpBuffer(buf, "SET %s TO ", fmtId(mine));
841 
842  /*
843  * Variables that are marked GUC_LIST_QUOTE were already fully quoted by
844  * flatten_set_variable_args() before they were put into the setconfig
845  * array. However, because the quoting rules used there aren't exactly
846  * like SQL's, we have to break the list value apart and then quote the
847  * elements as string literals. (The elements may be double-quoted as-is,
848  * but we can't just feed them to the SQL parser; it would do the wrong
849  * thing with elements that are zero-length or longer than NAMEDATALEN.)
850  *
851  * Variables that are not so marked should just be emitted as simple
852  * string literals. If the variable is not known to
853  * variable_is_guc_list_quote(), we'll do that; this makes it unsafe to
854  * use GUC_LIST_QUOTE for extension variables.
855  */
856  if (variable_is_guc_list_quote(mine))
857  {
858  char **namelist;
859  char **nameptr;
860 
861  /* Parse string into list of identifiers */
862  /* this shouldn't fail really */
863  if (SplitGUCList(pos, ',', &namelist))
864  {
865  for (nameptr = namelist; *nameptr; nameptr++)
866  {
867  if (nameptr != namelist)
868  appendPQExpBufferStr(buf, ", ");
869  appendStringLiteralConn(buf, *nameptr, conn);
870  }
871  }
872  pg_free(namelist);
873  }
874  else
876 
877  appendPQExpBufferStr(buf, ";\n");
878 
879  pg_free(mine);
880 }
bool variable_is_guc_list_quote(const char *name)
Definition: dumputils.c:684
bool SplitGUCList(char *rawstring, char separator, char ***namelist)
Definition: dumputils.c:718
char * pg_strdup(const char *in)
Definition: fe_memutils.c:85
void pg_free(void *ptr)
Definition: fe_memutils.c:105
static char * buf
Definition: pg_test_fsync.c:67

References appendPQExpBuffer(), appendPQExpBufferStr(), appendStringLiteralConn(), buf, conn, fmtId(), name, pg_free(), pg_strdup(), SplitGUCList(), generate_unaccent_rules::type, and variable_is_guc_list_quote().

Referenced by dumpDatabaseConfig(), and dumpUserConfig().

◆ quoteAclUserName()

void quoteAclUserName ( PQExpBuffer  output,
const char *  input 
)

Definition at line 539 of file dumputils.c.

540 {
541  const char *src;
542  bool safe = true;
543 
544  for (src = input; *src; src++)
545  {
546  /* This test had better match what putid() does */
547  if (!isalnum((unsigned char) *src) && *src != '_')
548  {
549  safe = false;
550  break;
551  }
552  }
553  if (!safe)
555  for (src = input; *src; src++)
556  {
557  /* A double quote character in a username is encoded as "" */
558  if (*src == '"')
561  }
562  if (!safe)
564 }
static void output(uint64 loop_count)
void appendPQExpBufferChar(PQExpBuffer str, char ch)
Definition: pqexpbuffer.c:378

References appendPQExpBufferChar(), and output().

Referenced by getNamespaces().

◆ SplitGUCList()

bool SplitGUCList ( char *  rawstring,
char  separator,
char ***  namelist 
)

Definition at line 718 of file dumputils.c.

720 {
721  char *nextp = rawstring;
722  bool done = false;
723  char **nextptr;
724 
725  /*
726  * Since we disallow empty identifiers, this is a conservative
727  * overestimate of the number of pointers we could need. Allow one for
728  * list terminator.
729  */
730  *namelist = nextptr = (char **)
731  pg_malloc((strlen(rawstring) / 2 + 2) * sizeof(char *));
732  *nextptr = NULL;
733 
734  while (isspace((unsigned char) *nextp))
735  nextp++; /* skip leading whitespace */
736 
737  if (*nextp == '\0')
738  return true; /* allow empty string */
739 
740  /* At the top of the loop, we are at start of a new identifier. */
741  do
742  {
743  char *curname;
744  char *endp;
745 
746  if (*nextp == '"')
747  {
748  /* Quoted name --- collapse quote-quote pairs */
749  curname = nextp + 1;
750  for (;;)
751  {
752  endp = strchr(nextp + 1, '"');
753  if (endp == NULL)
754  return false; /* mismatched quotes */
755  if (endp[1] != '"')
756  break; /* found end of quoted name */
757  /* Collapse adjacent quotes into one quote, and look again */
758  memmove(endp, endp + 1, strlen(endp));
759  nextp = endp;
760  }
761  /* endp now points at the terminating quote */
762  nextp = endp + 1;
763  }
764  else
765  {
766  /* Unquoted name --- extends to separator or whitespace */
767  curname = nextp;
768  while (*nextp && *nextp != separator &&
769  !isspace((unsigned char) *nextp))
770  nextp++;
771  endp = nextp;
772  if (curname == nextp)
773  return false; /* empty unquoted name not allowed */
774  }
775 
776  while (isspace((unsigned char) *nextp))
777  nextp++; /* skip trailing whitespace */
778 
779  if (*nextp == separator)
780  {
781  nextp++;
782  while (isspace((unsigned char) *nextp))
783  nextp++; /* skip leading whitespace for next */
784  /* we expect another name, so done remains false */
785  }
786  else if (*nextp == '\0')
787  done = true;
788  else
789  return false; /* invalid syntax */
790 
791  /* Now safe to overwrite separator with a null */
792  *endp = '\0';
793 
794  /*
795  * Finished isolating current name --- add it to output array
796  */
797  *nextptr++ = curname;
798 
799  /* Loop back if we didn't reach end of string */
800  } while (!done);
801 
802  *nextptr = NULL;
803  return true;
804 }

References pg_malloc().

Referenced by makeAlterConfigCommand().

◆ variable_is_guc_list_quote()

bool variable_is_guc_list_quote ( const char *  name)

Definition at line 684 of file dumputils.c.

685 {
686  if (pg_strcasecmp(name, "local_preload_libraries") == 0 ||
687  pg_strcasecmp(name, "search_path") == 0 ||
688  pg_strcasecmp(name, "session_preload_libraries") == 0 ||
689  pg_strcasecmp(name, "shared_preload_libraries") == 0 ||
690  pg_strcasecmp(name, "temp_tablespaces") == 0 ||
691  pg_strcasecmp(name, "unix_socket_directories") == 0)
692  return true;
693  else
694  return false;
695 }
int pg_strcasecmp(const char *s1, const char *s2)
Definition: pgstrcasecmp.c:36

References name, and pg_strcasecmp().

Referenced by dumpFunc(), and makeAlterConfigCommand().