PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
dumputils.c
Go to the documentation of this file.
1 /*-------------------------------------------------------------------------
2  *
3  * Utility routines for SQL dumping
4  *
5  * Basically this is stuff that is useful in both pg_dump and pg_dumpall.
6  *
7  *
8  * Portions Copyright (c) 1996-2017, PostgreSQL Global Development Group
9  * Portions Copyright (c) 1994, Regents of the University of California
10  *
11  * src/bin/pg_dump/dumputils.c
12  *
13  *-------------------------------------------------------------------------
14  */
15 #include "postgres_fe.h"
16 
17 #include "dumputils.h"
18 #include "fe_utils/string_utils.h"
19 
20 
21 static bool parseAclItem(const char *item, const char *type,
22  const char *name, const char *subname, int remoteVersion,
23  PQExpBuffer grantee, PQExpBuffer grantor,
24  PQExpBuffer privs, PQExpBuffer privswgo);
25 static char *copyAclUserName(PQExpBuffer output, char *input);
26 static void AddAcl(PQExpBuffer aclbuf, const char *keyword,
27  const char *subname);
28 
29 
30 /*
31  * Build GRANT/REVOKE command(s) for an object.
32  *
33  * name: the object name, in the form to use in the commands (already quoted)
34  * subname: the sub-object name, if any (already quoted); NULL if none
35  * type: the object type (as seen in GRANT command: must be one of
36  * TABLE, SEQUENCE, FUNCTION, LANGUAGE, SCHEMA, DATABASE, TABLESPACE,
37  * FOREIGN DATA WRAPPER, SERVER, or LARGE OBJECT)
38  * acls: the ACL string fetched from the database
39  * racls: the ACL string of any initial-but-now-revoked privileges
40  * owner: username of object owner (will be passed through fmtId); can be
41  * NULL or empty string to indicate "no owner known"
42  * prefix: string to prefix to each generated command; typically empty
43  * remoteVersion: version of database
44  *
45  * Returns TRUE if okay, FALSE if could not parse the acl string.
46  * The resulting commands (if any) are appended to the contents of 'sql'.
47  *
48  * Note: when processing a default ACL, prefix is "ALTER DEFAULT PRIVILEGES "
49  * or something similar, and name is an empty string.
50  *
51  * Note: beware of passing a fmtId() result directly as 'name' or 'subname',
52  * since this routine uses fmtId() internally.
53  */
54 bool
55 buildACLCommands(const char *name, const char *subname,
56  const char *type, const char *acls, const char *racls,
57  const char *owner, const char *prefix, int remoteVersion,
58  PQExpBuffer sql)
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 }
352 
353 /*
354  * Build ALTER DEFAULT PRIVILEGES command(s) for single pg_default_acl entry.
355  *
356  * type: the object type (TABLES, FUNCTIONS, etc)
357  * nspname: schema name, or NULL for global default privileges
358  * acls: the ACL string fetched from the database
359  * owner: username of privileges owner (will be passed through fmtId)
360  * remoteVersion: version of database
361  *
362  * Returns TRUE if okay, FALSE if could not parse the acl string.
363  * The resulting commands (if any) are appended to the contents of 'sql'.
364  */
365 bool
366 buildDefaultACLCommands(const char *type, const char *nspname,
367  const char *acls, const char *racls,
368  const char *initacls, const char *initracls,
369  const char *owner,
370  int remoteVersion,
371  PQExpBuffer sql)
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  return false;
394  appendPQExpBuffer(sql, "SELECT pg_catalog.binary_upgrade_set_record_init_privs(false);\n");
395  }
396 
397  if (!buildACLCommands("", NULL, type, acls, racls, owner,
398  prefix->data, remoteVersion, sql))
399  return false;
400 
401  destroyPQExpBuffer(prefix);
402 
403  return true;
404 }
405 
406 /*
407  * This will parse an aclitem string, having the general form
408  * username=privilegecodes/grantor
409  * or
410  * group groupname=privilegecodes/grantor
411  * (the "group" case occurs only with servers before 8.1).
412  *
413  * Returns true on success, false on parse error. On success, the components
414  * of the string are returned in the PQExpBuffer parameters.
415  *
416  * The returned grantee string will be the dequoted username or groupname
417  * (preceded with "group " in the latter case). Note that a grant to PUBLIC
418  * is represented by an empty grantee string. The returned grantor is the
419  * dequoted grantor name. Privilege characters are decoded and split between
420  * privileges with grant option (privswgo) and without (privs).
421  *
422  * Note: for cross-version compatibility, it's important to use ALL to
423  * represent the privilege sets whenever appropriate.
424  */
425 static bool
426 parseAclItem(const char *item, const char *type,
427  const char *name, const char *subname, int remoteVersion,
428  PQExpBuffer grantee, PQExpBuffer grantor,
429  PQExpBuffer privs, PQExpBuffer privswgo)
430 {
431  char *buf;
432  bool all_with_go = true;
433  bool all_without_go = true;
434  char *eqpos;
435  char *slpos;
436  char *pos;
437 
438  buf = strdup(item);
439  if (!buf)
440  return false;
441 
442  /* user or group name is string up to = */
443  eqpos = copyAclUserName(grantee, buf);
444  if (*eqpos != '=')
445  {
446  free(buf);
447  return false;
448  }
449 
450  /* grantor should appear after / */
451  slpos = strchr(eqpos + 1, '/');
452  if (slpos)
453  {
454  *slpos++ = '\0';
455  slpos = copyAclUserName(grantor, slpos);
456  if (*slpos != '\0')
457  {
458  free(buf);
459  return false;
460  }
461  }
462  else
463  {
464  free(buf);
465  return false;
466  }
467 
468  /* privilege codes */
469 #define CONVERT_PRIV(code, keywd) \
470 do { \
471  if ((pos = strchr(eqpos + 1, code))) \
472  { \
473  if (*(pos + 1) == '*') \
474  { \
475  AddAcl(privswgo, keywd, subname); \
476  all_without_go = false; \
477  } \
478  else \
479  { \
480  AddAcl(privs, keywd, subname); \
481  all_with_go = false; \
482  } \
483  } \
484  else \
485  all_with_go = all_without_go = false; \
486 } while (0)
487 
488  resetPQExpBuffer(privs);
489  resetPQExpBuffer(privswgo);
490 
491  if (strcmp(type, "TABLE") == 0 || strcmp(type, "SEQUENCE") == 0 ||
492  strcmp(type, "TABLES") == 0 || strcmp(type, "SEQUENCES") == 0)
493  {
494  CONVERT_PRIV('r', "SELECT");
495 
496  if (strcmp(type, "SEQUENCE") == 0 ||
497  strcmp(type, "SEQUENCES") == 0)
498  /* sequence only */
499  CONVERT_PRIV('U', "USAGE");
500  else
501  {
502  /* table only */
503  CONVERT_PRIV('a', "INSERT");
504  CONVERT_PRIV('x', "REFERENCES");
505  /* rest are not applicable to columns */
506  if (subname == NULL)
507  {
508  CONVERT_PRIV('d', "DELETE");
509  CONVERT_PRIV('t', "TRIGGER");
510  if (remoteVersion >= 80400)
511  CONVERT_PRIV('D', "TRUNCATE");
512  }
513  }
514 
515  /* UPDATE */
516  CONVERT_PRIV('w', "UPDATE");
517  }
518  else if (strcmp(type, "FUNCTION") == 0 ||
519  strcmp(type, "FUNCTIONS") == 0)
520  CONVERT_PRIV('X', "EXECUTE");
521  else if (strcmp(type, "LANGUAGE") == 0)
522  CONVERT_PRIV('U', "USAGE");
523  else if (strcmp(type, "SCHEMA") == 0 ||
524  strcmp(type, "SCHEMAS") == 0
525  )
526  {
527  CONVERT_PRIV('C', "CREATE");
528  CONVERT_PRIV('U', "USAGE");
529  }
530  else if (strcmp(type, "DATABASE") == 0)
531  {
532  CONVERT_PRIV('C', "CREATE");
533  CONVERT_PRIV('c', "CONNECT");
534  CONVERT_PRIV('T', "TEMPORARY");
535  }
536  else if (strcmp(type, "TABLESPACE") == 0)
537  CONVERT_PRIV('C', "CREATE");
538  else if (strcmp(type, "TYPE") == 0 ||
539  strcmp(type, "TYPES") == 0)
540  CONVERT_PRIV('U', "USAGE");
541  else if (strcmp(type, "FOREIGN DATA WRAPPER") == 0)
542  CONVERT_PRIV('U', "USAGE");
543  else if (strcmp(type, "FOREIGN SERVER") == 0)
544  CONVERT_PRIV('U', "USAGE");
545  else if (strcmp(type, "FOREIGN TABLE") == 0)
546  CONVERT_PRIV('r', "SELECT");
547  else if (strcmp(type, "LARGE OBJECT") == 0)
548  {
549  CONVERT_PRIV('r', "SELECT");
550  CONVERT_PRIV('w', "UPDATE");
551  }
552  else
553  abort();
554 
555 #undef CONVERT_PRIV
556 
557  if (all_with_go)
558  {
559  resetPQExpBuffer(privs);
560  printfPQExpBuffer(privswgo, "ALL");
561  if (subname)
562  appendPQExpBuffer(privswgo, "(%s)", subname);
563  }
564  else if (all_without_go)
565  {
566  resetPQExpBuffer(privswgo);
567  printfPQExpBuffer(privs, "ALL");
568  if (subname)
569  appendPQExpBuffer(privs, "(%s)", subname);
570  }
571 
572  free(buf);
573 
574  return true;
575 }
576 
577 /*
578  * Transfer a user or group name starting at *input into the output buffer,
579  * dequoting if needed. Returns a pointer to just past the input name.
580  * The name is taken to end at an unquoted '=' or end of string.
581  */
582 static char *
584 {
585  resetPQExpBuffer(output);
586 
587  while (*input && *input != '=')
588  {
589  /*
590  * If user name isn't quoted, then just add it to the output buffer
591  */
592  if (*input != '"')
593  appendPQExpBufferChar(output, *input++);
594  else
595  {
596  /* Otherwise, it's a quoted username */
597  input++;
598  /* Loop until we come across an unescaped quote */
599  while (!(*input == '"' && *(input + 1) != '"'))
600  {
601  if (*input == '\0')
602  return input; /* really a syntax error... */
603 
604  /*
605  * Quoting convention is to escape " as "". Keep this code in
606  * sync with putid() in backend's acl.c.
607  */
608  if (*input == '"' && *(input + 1) == '"')
609  input++;
610  appendPQExpBufferChar(output, *input++);
611  }
612  input++;
613  }
614  }
615  return input;
616 }
617 
618 /*
619  * Append a privilege keyword to a keyword list, inserting comma if needed.
620  */
621 static void
622 AddAcl(PQExpBuffer aclbuf, const char *keyword, const char *subname)
623 {
624  if (aclbuf->len > 0)
625  appendPQExpBufferChar(aclbuf, ',');
626  appendPQExpBufferStr(aclbuf, keyword);
627  if (subname)
628  appendPQExpBuffer(aclbuf, "(%s)", subname);
629 }
630 
631 
632 /*
633  * buildShSecLabelQuery
634  *
635  * Build a query to retrieve security labels for a shared object.
636  */
637 void
638 buildShSecLabelQuery(PGconn *conn, const char *catalog_name, uint32 objectId,
639  PQExpBuffer sql)
640 {
641  appendPQExpBuffer(sql,
642  "SELECT provider, label FROM pg_catalog.pg_shseclabel "
643  "WHERE classoid = '%s'::pg_catalog.regclass AND "
644  "objoid = %u", catalog_name, objectId);
645 }
646 
647 /*
648  * emitShSecLabels
649  *
650  * Format security label data retrieved by the query generated in
651  * buildShSecLabelQuery.
652  */
653 void
655  const char *target, const char *objname)
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), target);
668  appendPQExpBuffer(buffer,
669  " %s IS ",
670  fmtId(objname));
671  appendStringLiteralConn(buffer, label, conn);
672  appendPQExpBufferStr(buffer, ";\n");
673  }
674 }
675 
676 /*
677  * buildACLQueries
678  *
679  * Build the subqueries to extract out the correct set of ACLs to be
680  * GRANT'd and REVOKE'd for the specific kind of object, accounting for any
681  * initial privileges (from pg_init_privs) and based on if we are in binary
682  * upgrade mode or not.
683  *
684  * Also builds subqueries to extract out the set of ACLs to go from the object
685  * default privileges to the privileges in pg_init_privs, if we are in binary
686  * upgrade mode, so that those privileges can be set up and recorded in the new
687  * cluster before the regular privileges are added on top of those.
688  */
689 void
690 buildACLQueries(PQExpBuffer acl_subquery, PQExpBuffer racl_subquery,
691  PQExpBuffer init_acl_subquery, PQExpBuffer init_racl_subquery,
692  const char *acl_column, const char *acl_owner,
693  const char *obj_kind, bool binary_upgrade)
694 {
695  /*
696  * To get the delta from what the permissions were at creation time
697  * (either initdb or CREATE EXTENSION) vs. what they are now, we have to
698  * look at two things:
699  *
700  * What privileges have been added, which we calculate by extracting all
701  * the current privileges (using the set of default privileges for the
702  * object type if current privileges are NULL) and then removing those
703  * which existed at creation time (again, using the set of default
704  * privileges for the object type if there were no creation time
705  * privileges).
706  *
707  * What privileges have been removed, which we calculate by extracting the
708  * privileges as they were at creation time (or the default privileges, as
709  * above), and then removing the current privileges (or the default
710  * privileges, if current privileges are NULL).
711  *
712  * As a good cross-check, both directions of these checks should result in
713  * the empty set if both the current ACL and the initial privs are NULL
714  * (meaning, in practice, that the default ACLs were there at init time
715  * and is what the current privileges are).
716  *
717  * We always perform this delta on all ACLs and expect that by the time
718  * these are run the initial privileges will be in place, even in a binary
719  * upgrade situation (see below).
720  */
721  printfPQExpBuffer(acl_subquery, "(SELECT pg_catalog.array_agg(acl) FROM "
722  "(SELECT pg_catalog.unnest(coalesce(%s,pg_catalog.acldefault(%s,%s))) AS acl "
723  "EXCEPT "
724  "SELECT pg_catalog.unnest(coalesce(pip.initprivs,pg_catalog.acldefault(%s,%s)))) as foo)",
725  acl_column,
726  obj_kind,
727  acl_owner,
728  obj_kind,
729  acl_owner);
730 
731  printfPQExpBuffer(racl_subquery, "(SELECT pg_catalog.array_agg(acl) FROM "
732  "(SELECT pg_catalog.unnest(coalesce(pip.initprivs,pg_catalog.acldefault(%s,%s))) AS acl "
733  "EXCEPT "
734  "SELECT pg_catalog.unnest(coalesce(%s,pg_catalog.acldefault(%s,%s)))) as foo)",
735  obj_kind,
736  acl_owner,
737  acl_column,
738  obj_kind,
739  acl_owner);
740 
741  /*
742  * In binary upgrade mode we don't run the extension script but instead
743  * dump out the objects independently and then recreate them. To preserve
744  * the initial privileges which were set on extension objects, we need to
745  * grab the set of GRANT and REVOKE commands necessary to get from the
746  * default privileges of an object to the initial privileges as recorded
747  * in pg_init_privs.
748  *
749  * These will then be run ahead of the regular ACL commands, which were
750  * calculated using the queries above, inside of a block which sets a flag
751  * to indicate that the backend should record the results of these GRANT
752  * and REVOKE statements into pg_init_privs. This is how we preserve the
753  * contents of that catalog across binary upgrades.
754  */
755  if (binary_upgrade)
756  {
757  printfPQExpBuffer(init_acl_subquery,
758  "CASE WHEN privtype = 'e' THEN "
759  "(SELECT pg_catalog.array_agg(acl) FROM "
760  "(SELECT pg_catalog.unnest(pip.initprivs) AS acl "
761  "EXCEPT "
762  "SELECT pg_catalog.unnest(pg_catalog.acldefault(%s,%s))) as foo) END",
763  obj_kind,
764  acl_owner);
765 
766  printfPQExpBuffer(init_racl_subquery,
767  "CASE WHEN privtype = 'e' THEN "
768  "(SELECT pg_catalog.array_agg(acl) FROM "
769  "(SELECT pg_catalog.unnest(pg_catalog.acldefault(%s,%s)) AS acl "
770  "EXCEPT "
771  "SELECT pg_catalog.unnest(pip.initprivs)) as foo) END",
772  obj_kind,
773  acl_owner);
774  }
775  else
776  {
777  printfPQExpBuffer(init_acl_subquery, "NULL");
778  printfPQExpBuffer(init_racl_subquery, "NULL");
779  }
780 }
void printfPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:234
char * PQgetvalue(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3067
bool parsePGArray(const char *atext, char ***itemarray, int *nitems)
Definition: string_utils.c:662
static void AddAcl(PQExpBuffer aclbuf, const char *keyword, const char *subname)
Definition: dumputils.c:622
const char * fmtId(const char *rawid)
Definition: string_utils.c:66
static void output(uint64 loop_count)
void buildShSecLabelQuery(PGconn *conn, const char *catalog_name, uint32 objectId, PQExpBuffer sql)
Definition: dumputils.c:638
void appendPQExpBufferStr(PQExpBuffer str, const char *data)
Definition: pqexpbuffer.c:385
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: dumputils.c:366
int PQntuples(const PGresult *res)
Definition: fe-exec.c:2673
static char * copyAclUserName(PQExpBuffer output, char *input)
Definition: dumputils.c:583
void destroyPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:113
PGconn * conn
Definition: streamutil.c:42
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: dumputils.c:690
#define CONVERT_PRIV(code, keywd)
void appendPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:262
static char * buf
Definition: pg_test_fsync.c:66
unsigned int uint32
Definition: c.h:268
PQExpBuffer createPQExpBuffer(void)
Definition: pqexpbuffer.c:71
static char * label
Definition: pg_basebackup.c:81
void appendPQExpBufferChar(PQExpBuffer str, char ch)
Definition: pqexpbuffer.c:396
#define free(a)
Definition: header.h:65
void emitShSecLabels(PGconn *conn, PGresult *res, PQExpBuffer buffer, const char *target, const char *objname)
Definition: dumputils.c:654
#define NULL
Definition: c.h:229
#define Assert(condition)
Definition: c.h:675
WalTimeSample buffer[LAG_TRACKER_BUFFER_SIZE]
Definition: walsender.c:207
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
const char * name
Definition: encode.c:521
static int binary_upgrade
Definition: pg_dumpall.c:69
void appendStringLiteralConn(PQExpBuffer buf, const char *str, PGconn *conn)
Definition: string_utils.c:298
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:426
void resetPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:145