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  if (name && *name)
188  appendPQExpBuffer(firstsql, "%s ", name);
189  appendPQExpBufferStr(firstsql, "FROM ");
190  if (grantee->len == 0)
191  appendPQExpBufferStr(firstsql, "PUBLIC;\n");
192  else
193  appendPQExpBuffer(firstsql, "%s;\n",
194  fmtId(grantee->data));
195  }
196  }
197 
198  /*
199  * At this point we have issued REVOKE statements for all initial and
200  * default privileges that are no longer present on the object, so we are
201  * almost ready to GRANT the privileges listed in grantitems[].
202  *
203  * We still need some hacking though to cover the case where new default
204  * public privileges are added in new versions: the REVOKE ALL will revoke
205  * them, leading to behavior different from what the old version had,
206  * which is generally not what's wanted. So add back default privs if the
207  * source database is too old to have had that particular priv. (As of
208  * right now, no such cases exist in supported versions.)
209  */
210 
211  /*
212  * Scan individual ACL items to be granted.
213  *
214  * The order in which privileges appear in the ACL string (the order they
215  * have been GRANT'd in, which the backend maintains) must be preserved to
216  * ensure that GRANTs WITH GRANT OPTION and subsequent GRANTs based on
217  * those are dumped in the correct order. However, some old server
218  * versions will show grants to PUBLIC before the owner's own grants; for
219  * consistency's sake, force the owner's grants to be output first.
220  */
221  for (i = 0; i < ngrantitems; i++)
222  {
223  if (parseAclItem(grantitems[i], type, name, subname, remoteVersion,
224  grantee, grantor, privs, privswgo))
225  {
226  /*
227  * If the grantor isn't the owner, we'll need to use SET SESSION
228  * AUTHORIZATION to become the grantor. Issue the SET/RESET only
229  * if there's something useful to do.
230  */
231  if (privs->len > 0 || privswgo->len > 0)
232  {
233  PQExpBuffer thissql;
234 
235  /* Set owner as grantor if that's not explicit in the ACL */
236  if (grantor->len == 0 && owner)
237  printfPQExpBuffer(grantor, "%s", owner);
238 
239  /* Make sure owner's own grants are output before others */
240  if (owner &&
241  strcmp(grantee->data, owner) == 0 &&
242  strcmp(grantor->data, owner) == 0)
243  thissql = firstsql;
244  else
245  thissql = secondsql;
246 
247  if (grantor->len > 0
248  && (!owner || strcmp(owner, grantor->data) != 0))
249  appendPQExpBuffer(thissql, "SET SESSION AUTHORIZATION %s;\n",
250  fmtId(grantor->data));
251 
252  if (privs->len > 0)
253  {
254  appendPQExpBuffer(thissql, "%sGRANT %s ON %s ",
255  prefix, privs->data, type);
256  if (nspname && *nspname)
257  appendPQExpBuffer(thissql, "%s.", fmtId(nspname));
258  if (name && *name)
259  appendPQExpBuffer(thissql, "%s ", name);
260  appendPQExpBufferStr(thissql, "TO ");
261  if (grantee->len == 0)
262  appendPQExpBufferStr(thissql, "PUBLIC;\n");
263  else
264  appendPQExpBuffer(thissql, "%s;\n", fmtId(grantee->data));
265  }
266  if (privswgo->len > 0)
267  {
268  appendPQExpBuffer(thissql, "%sGRANT %s ON %s ",
269  prefix, privswgo->data, type);
270  if (nspname && *nspname)
271  appendPQExpBuffer(thissql, "%s.", fmtId(nspname));
272  if (name && *name)
273  appendPQExpBuffer(thissql, "%s ", name);
274  appendPQExpBufferStr(thissql, "TO ");
275  if (grantee->len == 0)
276  appendPQExpBufferStr(thissql, "PUBLIC");
277  else
278  appendPQExpBufferStr(thissql, fmtId(grantee->data));
279  appendPQExpBufferStr(thissql, " WITH GRANT OPTION;\n");
280  }
281 
282  if (grantor->len > 0
283  && (!owner || strcmp(owner, grantor->data) != 0))
284  appendPQExpBufferStr(thissql, "RESET SESSION AUTHORIZATION;\n");
285  }
286  }
287  else
288  {
289  /* parseAclItem failed, give up */
290  ok = false;
291  break;
292  }
293  }
294 
295  destroyPQExpBuffer(grantee);
296  destroyPQExpBuffer(grantor);
297  destroyPQExpBuffer(privs);
298  destroyPQExpBuffer(privswgo);
299 
300  appendPQExpBuffer(sql, "%s%s", firstsql->data, secondsql->data);
301  destroyPQExpBuffer(firstsql);
302  destroyPQExpBuffer(secondsql);
303 
304  free(aclitems);
305  free(baseitems);
306  free(grantitems);
307  free(revokeitems);
308 
309  return ok;
310 }
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:383
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
const char * type
const char * name

References appendPQExpBuffer(), appendPQExpBufferStr(), createPQExpBuffer(), PQExpBufferData::data, destroyPQExpBuffer(), fmtId(), free, i, j, PQExpBufferData::len, name, parseAclItem(), parsePGArray(), pg_malloc(), printfPQExpBuffer(), subname, and 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 326 of file dumputils.c.

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

Referenced by dumpDefaultACL().

◆ buildShSecLabelQuery()

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

Definition at line 636 of file dumputils.c.

638 {
639  appendPQExpBuffer(sql,
640  "SELECT provider, label FROM pg_catalog.pg_shseclabel "
641  "WHERE classoid = 'pg_catalog.%s'::pg_catalog.regclass "
642  "AND objoid = '%u'", catalog_name, objectId);
643 }

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 654 of file dumputils.c.

656 {
657  int i;
658 
659  for (i = 0; i < PQntuples(res); i++)
660  {
661  char *provider = PQgetvalue(res, i, 0);
662  char *label = PQgetvalue(res, i, 1);
663 
664  /* must use fmtId result before calling it again */
665  appendPQExpBuffer(buffer,
666  "SECURITY LABEL FOR %s ON %s",
667  fmtId(provider), objtype);
668  appendPQExpBuffer(buffer,
669  " %s IS ",
670  fmtId(objname));
672  appendPQExpBufferStr(buffer, ";\n");
673  }
674 }
int PQntuples(const PGresult *res)
Definition: fe-exec.c:3422
char * PQgetvalue(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3817
static JitProviderCallbacks provider
Definition: jit.c:42
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 822 of file dumputils.c.

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

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

Referenced by dumpDatabaseConfig(), and dumpUserConfig().

◆ quoteAclUserName()

void quoteAclUserName ( PQExpBuffer  output,
const char *  input 
)

Definition at line 543 of file dumputils.c.

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

References appendPQExpBufferChar(), input, and output.

Referenced by getNamespaces().

◆ SplitGUCList()

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

Definition at line 722 of file dumputils.c.

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

References pg_malloc().

Referenced by makeAlterConfigCommand().

◆ variable_is_guc_list_quote()

bool variable_is_guc_list_quote ( const char *  name)

Definition at line 688 of file dumputils.c.

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

References name, and pg_strcasecmp().

Referenced by dumpFunc(), and makeAlterConfigCommand().