LCOV - code coverage report
Current view: top level - src/backend/utils/adt - acl.c (source / functions) Coverage Total Hit
Test: Code coverage Lines: 57.6 % 2483 1430
Test Date: 2026-01-26 10:56:24 Functions: 60.2 % 171 103
Legend: Lines:     hit not hit
Branches: + taken - not taken # not executed
Branches: 50.8 % 952 484

             Branch data     Line data    Source code
       1                 :             : /*-------------------------------------------------------------------------
       2                 :             :  *
       3                 :             :  * acl.c
       4                 :             :  *        Basic access control list data structures manipulation routines.
       5                 :             :  *
       6                 :             :  * Portions Copyright (c) 1996-2026, PostgreSQL Global Development Group
       7                 :             :  * Portions Copyright (c) 1994, Regents of the University of California
       8                 :             :  *
       9                 :             :  *
      10                 :             :  * IDENTIFICATION
      11                 :             :  *        src/backend/utils/adt/acl.c
      12                 :             :  *
      13                 :             :  *-------------------------------------------------------------------------
      14                 :             :  */
      15                 :             : #include "postgres.h"
      16                 :             : 
      17                 :             : #include <ctype.h>
      18                 :             : 
      19                 :             : #include "access/htup_details.h"
      20                 :             : #include "catalog/catalog.h"
      21                 :             : #include "catalog/namespace.h"
      22                 :             : #include "catalog/pg_auth_members.h"
      23                 :             : #include "catalog/pg_authid.h"
      24                 :             : #include "catalog/pg_class.h"
      25                 :             : #include "catalog/pg_database.h"
      26                 :             : #include "catalog/pg_foreign_data_wrapper.h"
      27                 :             : #include "catalog/pg_foreign_server.h"
      28                 :             : #include "catalog/pg_language.h"
      29                 :             : #include "catalog/pg_largeobject.h"
      30                 :             : #include "catalog/pg_namespace.h"
      31                 :             : #include "catalog/pg_proc.h"
      32                 :             : #include "catalog/pg_tablespace.h"
      33                 :             : #include "catalog/pg_type.h"
      34                 :             : #include "commands/proclang.h"
      35                 :             : #include "commands/tablespace.h"
      36                 :             : #include "common/hashfn.h"
      37                 :             : #include "foreign/foreign.h"
      38                 :             : #include "funcapi.h"
      39                 :             : #include "lib/bloomfilter.h"
      40                 :             : #include "lib/qunique.h"
      41                 :             : #include "miscadmin.h"
      42                 :             : #include "storage/large_object.h"
      43                 :             : #include "utils/acl.h"
      44                 :             : #include "utils/array.h"
      45                 :             : #include "utils/builtins.h"
      46                 :             : #include "utils/catcache.h"
      47                 :             : #include "utils/inval.h"
      48                 :             : #include "utils/lsyscache.h"
      49                 :             : #include "utils/memutils.h"
      50                 :             : #include "utils/snapmgr.h"
      51                 :             : #include "utils/syscache.h"
      52                 :             : #include "utils/varlena.h"
      53                 :             : 
      54                 :             : typedef struct
      55                 :             : {
      56                 :             :         const char *name;
      57                 :             :         AclMode         value;
      58                 :             : } priv_map;
      59                 :             : 
      60                 :             : /*
      61                 :             :  * We frequently need to test whether a given role is a member of some other
      62                 :             :  * role.  In most of these tests the "given role" is the same, namely the
      63                 :             :  * active current user.  So we can optimize it by keeping cached lists of all
      64                 :             :  * the roles the "given role" is a member of, directly or indirectly.
      65                 :             :  *
      66                 :             :  * Possibly this mechanism should be generalized to allow caching membership
      67                 :             :  * info for multiple roles?
      68                 :             :  *
      69                 :             :  * Each element of cached_roles is an OID list of constituent roles for the
      70                 :             :  * corresponding element of cached_role (always including the cached_role
      71                 :             :  * itself).  There's a separate cache for each RoleRecurseType, with the
      72                 :             :  * corresponding semantics.
      73                 :             :  */
      74                 :             : enum RoleRecurseType
      75                 :             : {
      76                 :             :         ROLERECURSE_MEMBERS = 0,        /* recurse unconditionally */
      77                 :             :         ROLERECURSE_PRIVS = 1,          /* recurse through inheritable grants */
      78                 :             :         ROLERECURSE_SETROLE = 2         /* recurse through grants with set_option */
      79                 :             : };
      80                 :             : static Oid      cached_role[] = {InvalidOid, InvalidOid, InvalidOid};
      81                 :             : static List *cached_roles[] = {NIL, NIL, NIL};
      82                 :             : static uint32 cached_db_hash;
      83                 :             : 
      84                 :             : /*
      85                 :             :  * If the list of roles gathered by roles_is_member_of() grows larger than the
      86                 :             :  * below threshold, a Bloom filter is created to speed up list membership
      87                 :             :  * checks.  This threshold is set arbitrarily high to avoid the overhead of
      88                 :             :  * creating the Bloom filter until it seems likely to provide a net benefit.
      89                 :             :  */
      90                 :             : #define ROLES_LIST_BLOOM_THRESHOLD 1024
      91                 :             : 
      92                 :             : static const char *getid(const char *s, char *n, Node *escontext);
      93                 :             : static void putid(char *p, const char *s);
      94                 :             : static Acl *allocacl(int n);
      95                 :             : static void check_acl(const Acl *acl);
      96                 :             : static const char *aclparse(const char *s, AclItem *aip, Node *escontext);
      97                 :             : static bool aclitem_match(const AclItem *a1, const AclItem *a2);
      98                 :             : static int      aclitemComparator(const void *arg1, const void *arg2);
      99                 :             : static void check_circularity(const Acl *old_acl, const AclItem *mod_aip,
     100                 :             :                                                           Oid ownerId);
     101                 :             : static Acl *recursive_revoke(Acl *acl, Oid grantee, AclMode revoke_privs,
     102                 :             :                                                          Oid ownerId, DropBehavior behavior);
     103                 :             : 
     104                 :             : static AclMode convert_any_priv_string(text *priv_type_text,
     105                 :             :                                                                            const priv_map *privileges);
     106                 :             : 
     107                 :             : static Oid      convert_table_name(text *tablename);
     108                 :             : static AclMode convert_table_priv_string(text *priv_type_text);
     109                 :             : static AclMode convert_sequence_priv_string(text *priv_type_text);
     110                 :             : static AttrNumber convert_column_name(Oid tableoid, text *column);
     111                 :             : static AclMode convert_column_priv_string(text *priv_type_text);
     112                 :             : static Oid      convert_database_name(text *databasename);
     113                 :             : static AclMode convert_database_priv_string(text *priv_type_text);
     114                 :             : static Oid      convert_foreign_data_wrapper_name(text *fdwname);
     115                 :             : static AclMode convert_foreign_data_wrapper_priv_string(text *priv_type_text);
     116                 :             : static Oid      convert_function_name(text *functionname);
     117                 :             : static AclMode convert_function_priv_string(text *priv_type_text);
     118                 :             : static Oid      convert_language_name(text *languagename);
     119                 :             : static AclMode convert_language_priv_string(text *priv_type_text);
     120                 :             : static Oid      convert_schema_name(text *schemaname);
     121                 :             : static AclMode convert_schema_priv_string(text *priv_type_text);
     122                 :             : static Oid      convert_server_name(text *servername);
     123                 :             : static AclMode convert_server_priv_string(text *priv_type_text);
     124                 :             : static Oid      convert_tablespace_name(text *tablespacename);
     125                 :             : static AclMode convert_tablespace_priv_string(text *priv_type_text);
     126                 :             : static Oid      convert_type_name(text *typename);
     127                 :             : static AclMode convert_type_priv_string(text *priv_type_text);
     128                 :             : static AclMode convert_parameter_priv_string(text *priv_text);
     129                 :             : static AclMode convert_largeobject_priv_string(text *priv_type_text);
     130                 :             : static AclMode convert_role_priv_string(text *priv_type_text);
     131                 :             : static AclResult pg_role_aclcheck(Oid role_oid, Oid roleid, AclMode mode);
     132                 :             : 
     133                 :             : static void RoleMembershipCacheCallback(Datum arg, int cacheid, uint32 hashvalue);
     134                 :             : 
     135                 :             : 
     136                 :             : /*
     137                 :             :  * Test whether an identifier char can be left unquoted in ACLs.
     138                 :             :  *
     139                 :             :  * Formerly, we used isalnum() even on non-ASCII characters, resulting in
     140                 :             :  * unportable behavior.  To ensure dump compatibility with old versions,
     141                 :             :  * we now treat high-bit-set characters as always requiring quoting during
     142                 :             :  * putid(), but getid() will always accept them without quotes.
     143                 :             :  */
     144                 :             : static inline bool
     145                 :        2356 : is_safe_acl_char(unsigned char c, bool is_getid)
     146                 :             : {
     147         [ -  + ]:        2356 :         if (IS_HIGHBIT_SET(c))
     148                 :           0 :                 return is_getid;
     149         [ +  + ]:        2356 :         return isalnum(c) || c == '_';
     150                 :        2356 : }
     151                 :             : 
     152                 :             : /*
     153                 :             :  * getid
     154                 :             :  *              Consumes the first alphanumeric string (identifier) found in string
     155                 :             :  *              's', ignoring any leading white space.  If it finds a double quote
     156                 :             :  *              it returns the word inside the quotes.
     157                 :             :  *
     158                 :             :  * RETURNS:
     159                 :             :  *              the string position in 's' that points to the next non-space character
     160                 :             :  *              in 's', after any quotes.  Also:
     161                 :             :  *              - loads the identifier into 'n'.  (If no identifier is found, 'n'
     162                 :             :  *                contains an empty string.)  'n' must be NAMEDATALEN bytes.
     163                 :             :  *
     164                 :             :  * Errors are reported via ereport, unless escontext is an ErrorSaveData node,
     165                 :             :  * in which case we log the error there and return NULL.
     166                 :             :  */
     167                 :             : static const char *
     168                 :          20 : getid(const char *s, char *n, Node *escontext)
     169                 :             : {
     170                 :          20 :         int                     len = 0;
     171                 :          20 :         bool            in_quotes = false;
     172                 :             : 
     173         [ +  - ]:          20 :         Assert(s && n);
     174                 :             : 
     175         [ -  + ]:          20 :         while (isspace((unsigned char) *s))
     176                 :           0 :                 s++;
     177         [ +  + ]:         518 :         for (;
     178         [ +  + ]:         259 :                  *s != '\0' &&
     179   [ +  +  +  + ]:         250 :                  (in_quotes || *s == '"' || is_safe_acl_char(*s, true));
     180                 :         239 :                  s++)
     181                 :             :         {
     182         [ +  + ]:         239 :                 if (*s == '"')
     183                 :             :                 {
     184         [ +  + ]:          12 :                         if (!in_quotes)
     185                 :             :                         {
     186                 :           5 :                                 in_quotes = true;
     187                 :           5 :                                 continue;
     188                 :             :                         }
     189                 :             :                         /* safe to look at next char (could be '\0' though) */
     190         [ +  + ]:           7 :                         if (*(s + 1) != '"')
     191                 :             :                         {
     192                 :           5 :                                 in_quotes = false;
     193                 :           5 :                                 continue;
     194                 :             :                         }
     195                 :             :                         /* it's an escaped double quote; skip the escaping char */
     196                 :           2 :                         s++;
     197                 :           2 :                 }
     198                 :             : 
     199                 :             :                 /* Add the character to the string */
     200         [ +  - ]:         229 :                 if (len >= NAMEDATALEN - 1)
     201         [ #  # ]:           0 :                         ereturn(escontext, NULL,
     202                 :             :                                         (errcode(ERRCODE_NAME_TOO_LONG),
     203                 :             :                                          errmsg("identifier too long"),
     204                 :             :                                          errdetail("Identifier must be less than %d characters.",
     205                 :             :                                                            NAMEDATALEN)));
     206                 :             : 
     207                 :         229 :                 n[len++] = *s;
     208                 :         229 :         }
     209                 :          20 :         n[len] = '\0';
     210         [ -  + ]:          20 :         while (isspace((unsigned char) *s))
     211                 :           0 :                 s++;
     212                 :          20 :         return s;
     213                 :          20 : }
     214                 :             : 
     215                 :             : /*
     216                 :             :  * Write a role name at *p, adding double quotes if needed.
     217                 :             :  * There must be at least (2*NAMEDATALEN)+2 bytes available at *p.
     218                 :             :  * This needs to be kept in sync with dequoteAclUserName in pg_dump/dumputils.c
     219                 :             :  */
     220                 :             : static void
     221                 :         117 : putid(char *p, const char *s)
     222                 :             : {
     223                 :         117 :         const char *src;
     224                 :         117 :         bool            safe = true;
     225                 :             : 
     226                 :             :         /* Detect whether we need to use double quotes */
     227         [ +  + ]:        2264 :         for (src = s; *src; src++)
     228                 :             :         {
     229         [ +  + ]:        2151 :                 if (!is_safe_acl_char(*src, false))
     230                 :             :                 {
     231                 :           4 :                         safe = false;
     232                 :           4 :                         break;
     233                 :             :                 }
     234                 :        2147 :         }
     235         [ +  + ]:         117 :         if (!safe)
     236                 :           4 :                 *p++ = '"';
     237         [ +  + ]:        2292 :         for (src = s; *src; src++)
     238                 :             :         {
     239                 :             :                 /* A double quote character in a username is encoded as "" */
     240         [ +  + ]:        2175 :                 if (*src == '"')
     241                 :           4 :                         *p++ = '"';
     242                 :        2175 :                 *p++ = *src;
     243                 :        2175 :         }
     244         [ +  + ]:         117 :         if (!safe)
     245                 :           4 :                 *p++ = '"';
     246                 :         117 :         *p = '\0';
     247                 :         117 : }
     248                 :             : 
     249                 :             : /*
     250                 :             :  * aclparse
     251                 :             :  *              Consumes and parses an ACL specification of the form:
     252                 :             :  *                              [group|user] [A-Za-z0-9]*=[rwaR]*
     253                 :             :  *              from string 's', ignoring any leading white space or white space
     254                 :             :  *              between the optional id type keyword (group|user) and the actual
     255                 :             :  *              ACL specification.
     256                 :             :  *
     257                 :             :  *              The group|user decoration is unnecessary in the roles world,
     258                 :             :  *              but we still accept it for backward compatibility.
     259                 :             :  *
     260                 :             :  *              This routine is called by the parser as well as aclitemin(), hence
     261                 :             :  *              the added generality.
     262                 :             :  *
     263                 :             :  * RETURNS:
     264                 :             :  *              the string position in 's' immediately following the ACL
     265                 :             :  *              specification.  Also:
     266                 :             :  *              - loads the structure pointed to by 'aip' with the appropriate
     267                 :             :  *                UID/GID, id type identifier and mode type values.
     268                 :             :  *
     269                 :             :  * Errors are reported via ereport, unless escontext is an ErrorSaveData node,
     270                 :             :  * in which case we log the error there and return NULL.
     271                 :             :  */
     272                 :             : static const char *
     273                 :          10 : aclparse(const char *s, AclItem *aip, Node *escontext)
     274                 :             : {
     275                 :          10 :         AclMode         privs,
     276                 :             :                                 goption,
     277                 :             :                                 read;
     278                 :          10 :         char            name[NAMEDATALEN];
     279                 :          10 :         char            name2[NAMEDATALEN];
     280                 :             : 
     281         [ +  - ]:          10 :         Assert(s && aip);
     282                 :             : 
     283                 :          10 :         s = getid(s, name, escontext);
     284         [ -  + ]:          10 :         if (s == NULL)
     285                 :           0 :                 return NULL;
     286         [ +  - ]:          10 :         if (*s != '=')
     287                 :             :         {
     288                 :             :                 /* we just read a keyword, not a name */
     289   [ #  #  #  # ]:           0 :                 if (strcmp(name, "group") != 0 && strcmp(name, "user") != 0)
     290         [ #  # ]:           0 :                         ereturn(escontext, NULL,
     291                 :             :                                         (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
     292                 :             :                                          errmsg("unrecognized key word: \"%s\"", name),
     293                 :             :                                          errhint("ACL key word must be \"group\" or \"user\".")));
     294                 :             :                 /* move s to the name beyond the keyword */
     295                 :           0 :                 s = getid(s, name, escontext);
     296         [ #  # ]:           0 :                 if (s == NULL)
     297                 :           0 :                         return NULL;
     298         [ #  # ]:           0 :                 if (name[0] == '\0')
     299         [ #  # ]:           0 :                         ereturn(escontext, NULL,
     300                 :             :                                         (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
     301                 :             :                                          errmsg("missing name"),
     302                 :             :                                          errhint("A name must follow the \"group\" or \"user\" key word.")));
     303                 :           0 :         }
     304                 :             : 
     305         [ +  - ]:          10 :         if (*s != '=')
     306         [ #  # ]:           0 :                 ereturn(escontext, NULL,
     307                 :             :                                 (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
     308                 :             :                                  errmsg("missing \"=\" sign")));
     309                 :             : 
     310                 :          10 :         privs = goption = ACL_NO_RIGHTS;
     311                 :             : 
     312   [ +  +  +  + ]:          24 :         for (++s, read = 0; isalpha((unsigned char) *s) || *s == '*'; s++)
     313                 :             :         {
     314   [ +  -  +  -  :          16 :                 switch (*s)
          -  -  -  -  -  
          +  +  -  -  -  
                -  -  + ]
     315                 :             :                 {
     316                 :             :                         case '*':
     317                 :           2 :                                 goption |= read;
     318                 :           2 :                                 break;
     319                 :             :                         case ACL_INSERT_CHR:
     320                 :           0 :                                 read = ACL_INSERT;
     321                 :           0 :                                 break;
     322                 :             :                         case ACL_SELECT_CHR:
     323                 :          10 :                                 read = ACL_SELECT;
     324                 :          10 :                                 break;
     325                 :             :                         case ACL_UPDATE_CHR:
     326                 :           0 :                                 read = ACL_UPDATE;
     327                 :           0 :                                 break;
     328                 :             :                         case ACL_DELETE_CHR:
     329                 :           0 :                                 read = ACL_DELETE;
     330                 :           0 :                                 break;
     331                 :             :                         case ACL_TRUNCATE_CHR:
     332                 :           0 :                                 read = ACL_TRUNCATE;
     333                 :           0 :                                 break;
     334                 :             :                         case ACL_REFERENCES_CHR:
     335                 :           0 :                                 read = ACL_REFERENCES;
     336                 :           0 :                                 break;
     337                 :             :                         case ACL_TRIGGER_CHR:
     338                 :           0 :                                 read = ACL_TRIGGER;
     339                 :           0 :                                 break;
     340                 :             :                         case ACL_EXECUTE_CHR:
     341                 :           0 :                                 read = ACL_EXECUTE;
     342                 :           0 :                                 break;
     343                 :             :                         case ACL_USAGE_CHR:
     344                 :           1 :                                 read = ACL_USAGE;
     345                 :           1 :                                 break;
     346                 :             :                         case ACL_CREATE_CHR:
     347                 :           1 :                                 read = ACL_CREATE;
     348                 :           1 :                                 break;
     349                 :             :                         case ACL_CREATE_TEMP_CHR:
     350                 :           0 :                                 read = ACL_CREATE_TEMP;
     351                 :           0 :                                 break;
     352                 :             :                         case ACL_CONNECT_CHR:
     353                 :           0 :                                 read = ACL_CONNECT;
     354                 :           0 :                                 break;
     355                 :             :                         case ACL_SET_CHR:
     356                 :           0 :                                 read = ACL_SET;
     357                 :           0 :                                 break;
     358                 :             :                         case ACL_ALTER_SYSTEM_CHR:
     359                 :           0 :                                 read = ACL_ALTER_SYSTEM;
     360                 :           0 :                                 break;
     361                 :             :                         case ACL_MAINTAIN_CHR:
     362                 :           0 :                                 read = ACL_MAINTAIN;
     363                 :           0 :                                 break;
     364                 :             :                         default:
     365         [ +  + ]:           2 :                                 ereturn(escontext, NULL,
     366                 :             :                                                 (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
     367                 :             :                                                  errmsg("invalid mode character: must be one of \"%s\"",
     368                 :             :                                                                 ACL_ALL_RIGHTS_STR)));
     369                 :           0 :                 }
     370                 :             : 
     371                 :          14 :                 privs |= read;
     372                 :          14 :         }
     373                 :             : 
     374         [ +  + ]:           8 :         if (name[0] == '\0')
     375                 :           2 :                 aip->ai_grantee = ACL_ID_PUBLIC;
     376                 :             :         else
     377                 :             :         {
     378                 :           6 :                 aip->ai_grantee = get_role_oid(name, true);
     379         [ +  - ]:           6 :                 if (!OidIsValid(aip->ai_grantee))
     380         [ #  # ]:           0 :                         ereturn(escontext, NULL,
     381                 :             :                                         (errcode(ERRCODE_UNDEFINED_OBJECT),
     382                 :             :                                          errmsg("role \"%s\" does not exist", name)));
     383                 :             :         }
     384                 :             : 
     385                 :             :         /*
     386                 :             :          * XXX Allow a degree of backward compatibility by defaulting the grantor
     387                 :             :          * to the superuser.
     388                 :             :          */
     389         [ +  - ]:           8 :         if (*s == '/')
     390                 :             :         {
     391                 :           8 :                 s = getid(s + 1, name2, escontext);
     392         [ +  - ]:           8 :                 if (s == NULL)
     393                 :           0 :                         return NULL;
     394         [ +  + ]:           8 :                 if (name2[0] == '\0')
     395         [ -  + ]:           2 :                         ereturn(escontext, NULL,
     396                 :             :                                         (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
     397                 :             :                                          errmsg("a name must follow the \"/\" sign")));
     398                 :           6 :                 aip->ai_grantor = get_role_oid(name2, true);
     399         [ +  + ]:           6 :                 if (!OidIsValid(aip->ai_grantor))
     400         [ +  + ]:           2 :                         ereturn(escontext, NULL,
     401                 :             :                                         (errcode(ERRCODE_UNDEFINED_OBJECT),
     402                 :             :                                          errmsg("role \"%s\" does not exist", name2)));
     403                 :           4 :         }
     404                 :             :         else
     405                 :             :         {
     406                 :           0 :                 aip->ai_grantor = BOOTSTRAP_SUPERUSERID;
     407   [ #  #  #  # ]:           0 :                 ereport(WARNING,
     408                 :             :                                 (errcode(ERRCODE_INVALID_GRANTOR),
     409                 :             :                                  errmsg("defaulting grantor to user ID %u",
     410                 :             :                                                 BOOTSTRAP_SUPERUSERID)));
     411                 :             :         }
     412                 :             : 
     413                 :           4 :         ACLITEM_SET_PRIVS_GOPTIONS(*aip, privs, goption);
     414                 :             : 
     415                 :           4 :         return s;
     416                 :          10 : }
     417                 :             : 
     418                 :             : /*
     419                 :             :  * allocacl
     420                 :             :  *              Allocates storage for a new Acl with 'n' entries.
     421                 :             :  *
     422                 :             :  * RETURNS:
     423                 :             :  *              the new Acl
     424                 :             :  */
     425                 :             : static Acl *
     426                 :       13333 : allocacl(int n)
     427                 :             : {
     428                 :       13333 :         Acl                *new_acl;
     429                 :       13333 :         Size            size;
     430                 :             : 
     431         [ +  - ]:       13333 :         if (n < 0)
     432   [ #  #  #  # ]:           0 :                 elog(ERROR, "invalid size: %d", n);
     433                 :       13333 :         size = ACL_N_SIZE(n);
     434                 :       13333 :         new_acl = (Acl *) palloc0(size);
     435                 :       13333 :         SET_VARSIZE(new_acl, size);
     436                 :       13333 :         new_acl->ndim = 1;
     437                 :       13333 :         new_acl->dataoffset = 0;     /* we never put in any nulls */
     438                 :       13333 :         new_acl->elemtype = ACLITEMOID;
     439                 :       13333 :         ARR_LBOUND(new_acl)[0] = 1;
     440                 :       13333 :         ARR_DIMS(new_acl)[0] = n;
     441                 :       26666 :         return new_acl;
     442                 :       13333 : }
     443                 :             : 
     444                 :             : /*
     445                 :             :  * Create a zero-entry ACL
     446                 :             :  */
     447                 :             : Acl *
     448                 :          10 : make_empty_acl(void)
     449                 :             : {
     450                 :          10 :         return allocacl(0);
     451                 :             : }
     452                 :             : 
     453                 :             : /*
     454                 :             :  * Copy an ACL
     455                 :             :  */
     456                 :             : Acl *
     457                 :         433 : aclcopy(const Acl *orig_acl)
     458                 :             : {
     459                 :         433 :         Acl                *result_acl;
     460                 :             : 
     461                 :         433 :         result_acl = allocacl(ACL_NUM(orig_acl));
     462                 :             : 
     463   [ -  +  -  +  :         433 :         memcpy(ACL_DAT(result_acl),
                   -  + ]
     464                 :             :                    ACL_DAT(orig_acl),
     465                 :             :                    ACL_NUM(orig_acl) * sizeof(AclItem));
     466                 :             : 
     467                 :         866 :         return result_acl;
     468                 :         433 : }
     469                 :             : 
     470                 :             : /*
     471                 :             :  * Concatenate two ACLs
     472                 :             :  *
     473                 :             :  * This is a bit cheesy, since we may produce an ACL with redundant entries.
     474                 :             :  * Be careful what the result is used for!
     475                 :             :  */
     476                 :             : Acl *
     477                 :         812 : aclconcat(const Acl *left_acl, const Acl *right_acl)
     478                 :             : {
     479                 :         812 :         Acl                *result_acl;
     480                 :             : 
     481                 :         812 :         result_acl = allocacl(ACL_NUM(left_acl) + ACL_NUM(right_acl));
     482                 :             : 
     483   [ -  +  -  +  :         812 :         memcpy(ACL_DAT(result_acl),
                   -  + ]
     484                 :             :                    ACL_DAT(left_acl),
     485                 :             :                    ACL_NUM(left_acl) * sizeof(AclItem));
     486                 :             : 
     487   [ -  +  -  +  :         812 :         memcpy(ACL_DAT(result_acl) + ACL_NUM(left_acl),
                   -  + ]
     488                 :             :                    ACL_DAT(right_acl),
     489                 :             :                    ACL_NUM(right_acl) * sizeof(AclItem));
     490                 :             : 
     491                 :        1624 :         return result_acl;
     492                 :         812 : }
     493                 :             : 
     494                 :             : /*
     495                 :             :  * Merge two ACLs
     496                 :             :  *
     497                 :             :  * This produces a properly merged ACL with no redundant entries.
     498                 :             :  * Returns NULL on NULL input.
     499                 :             :  */
     500                 :             : Acl *
     501                 :          35 : aclmerge(const Acl *left_acl, const Acl *right_acl, Oid ownerId)
     502                 :             : {
     503                 :          35 :         Acl                *result_acl;
     504                 :          35 :         AclItem    *aip;
     505                 :          35 :         int                     i,
     506                 :             :                                 num;
     507                 :             : 
     508                 :             :         /* Check for cases where one or both are empty/null */
     509   [ +  -  -  + ]:          35 :         if (left_acl == NULL || ACL_NUM(left_acl) == 0)
     510                 :             :         {
     511   [ #  #  #  # ]:           0 :                 if (right_acl == NULL || ACL_NUM(right_acl) == 0)
     512                 :           0 :                         return NULL;
     513                 :             :                 else
     514                 :           0 :                         return aclcopy(right_acl);
     515                 :             :         }
     516                 :             :         else
     517                 :             :         {
     518   [ +  +  -  + ]:          35 :                 if (right_acl == NULL || ACL_NUM(right_acl) == 0)
     519                 :          23 :                         return aclcopy(left_acl);
     520                 :             :         }
     521                 :             : 
     522                 :             :         /* Merge them the hard way, one item at a time */
     523                 :          12 :         result_acl = aclcopy(left_acl);
     524                 :             : 
     525         [ -  + ]:          12 :         aip = ACL_DAT(right_acl);
     526                 :          12 :         num = ACL_NUM(right_acl);
     527                 :             : 
     528         [ +  + ]:          30 :         for (i = 0; i < num; i++, aip++)
     529                 :             :         {
     530                 :          18 :                 Acl                *tmp_acl;
     531                 :             : 
     532                 :          36 :                 tmp_acl = aclupdate(result_acl, aip, ACL_MODECHG_ADD,
     533                 :          18 :                                                         ownerId, DROP_RESTRICT);
     534                 :          18 :                 pfree(result_acl);
     535                 :          18 :                 result_acl = tmp_acl;
     536                 :          18 :         }
     537                 :             : 
     538                 :          12 :         return result_acl;
     539                 :          35 : }
     540                 :             : 
     541                 :             : /*
     542                 :             :  * Sort the items in an ACL (into an arbitrary but consistent order)
     543                 :             :  */
     544                 :             : void
     545                 :         144 : aclitemsort(Acl *acl)
     546                 :             : {
     547   [ +  -  +  + ]:         144 :         if (acl != NULL && ACL_NUM(acl) > 1)
     548         [ -  + ]:          42 :                 qsort(ACL_DAT(acl), ACL_NUM(acl), sizeof(AclItem), aclitemComparator);
     549                 :         144 : }
     550                 :             : 
     551                 :             : /*
     552                 :             :  * Check if two ACLs are exactly equal
     553                 :             :  *
     554                 :             :  * This will not detect equality if the two arrays contain the same items
     555                 :             :  * in different orders.  To handle that case, sort both inputs first,
     556                 :             :  * using aclitemsort().
     557                 :             :  */
     558                 :             : bool
     559                 :          72 : aclequal(const Acl *left_acl, const Acl *right_acl)
     560                 :             : {
     561                 :             :         /* Check for cases where one or both are empty/null */
     562   [ +  -  -  + ]:          72 :         if (left_acl == NULL || ACL_NUM(left_acl) == 0)
     563                 :             :         {
     564   [ #  #  #  # ]:           0 :                 if (right_acl == NULL || ACL_NUM(right_acl) == 0)
     565                 :           0 :                         return true;
     566                 :             :                 else
     567                 :           0 :                         return false;
     568                 :             :         }
     569                 :             :         else
     570                 :             :         {
     571   [ +  -  +  + ]:          72 :                 if (right_acl == NULL || ACL_NUM(right_acl) == 0)
     572                 :           8 :                         return false;
     573                 :             :         }
     574                 :             : 
     575         [ +  + ]:          64 :         if (ACL_NUM(left_acl) != ACL_NUM(right_acl))
     576                 :          27 :                 return false;
     577                 :             : 
     578         [ -  + ]:          74 :         if (memcmp(ACL_DAT(left_acl),
     579         [ -  + ]:          37 :                            ACL_DAT(right_acl),
     580   [ +  +  +  + ]:          74 :                            ACL_NUM(left_acl) * sizeof(AclItem)) == 0)
     581                 :          14 :                 return true;
     582                 :             : 
     583                 :          23 :         return false;
     584                 :          72 : }
     585                 :             : 
     586                 :             : /*
     587                 :             :  * Verify that an ACL array is acceptable (one-dimensional and has no nulls)
     588                 :             :  */
     589                 :             : static void
     590                 :       20088 : check_acl(const Acl *acl)
     591                 :             : {
     592         [ +  - ]:       20088 :         if (ARR_ELEMTYPE(acl) != ACLITEMOID)
     593   [ #  #  #  # ]:           0 :                 ereport(ERROR,
     594                 :             :                                 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
     595                 :             :                                  errmsg("ACL array contains wrong data type")));
     596         [ +  - ]:       20088 :         if (ARR_NDIM(acl) != 1)
     597   [ #  #  #  # ]:           0 :                 ereport(ERROR,
     598                 :             :                                 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
     599                 :             :                                  errmsg("ACL arrays must be one-dimensional")));
     600         [ +  - ]:       20088 :         if (ARR_HASNULL(acl))
     601   [ #  #  #  # ]:           0 :                 ereport(ERROR,
     602                 :             :                                 (errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
     603                 :             :                                  errmsg("ACL arrays must not contain null values")));
     604                 :       20088 : }
     605                 :             : 
     606                 :             : /*
     607                 :             :  * aclitemin
     608                 :             :  *              Allocates storage for, and fills in, a new AclItem given a string
     609                 :             :  *              's' that contains an ACL specification.  See aclparse for details.
     610                 :             :  *
     611                 :             :  * RETURNS:
     612                 :             :  *              the new AclItem
     613                 :             :  */
     614                 :             : Datum
     615                 :          10 : aclitemin(PG_FUNCTION_ARGS)
     616                 :             : {
     617                 :          10 :         const char *s = PG_GETARG_CSTRING(0);
     618                 :          10 :         Node       *escontext = fcinfo->context;
     619                 :          10 :         AclItem    *aip;
     620                 :             : 
     621                 :          10 :         aip = palloc_object(AclItem);
     622                 :             : 
     623                 :          10 :         s = aclparse(s, aip, escontext);
     624         [ +  + ]:          10 :         if (s == NULL)
     625                 :           6 :                 PG_RETURN_NULL();
     626                 :             : 
     627         [ -  + ]:           4 :         while (isspace((unsigned char) *s))
     628                 :           0 :                 ++s;
     629         [ +  - ]:           4 :         if (*s)
     630         [ #  # ]:           0 :                 ereturn(escontext, (Datum) 0,
     631                 :             :                                 (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
     632                 :             :                                  errmsg("extra garbage at the end of the ACL specification")));
     633                 :             : 
     634                 :           4 :         PG_RETURN_ACLITEM_P(aip);
     635                 :          10 : }
     636                 :             : 
     637                 :             : /*
     638                 :             :  * aclitemout
     639                 :             :  *              Allocates storage for, and fills in, a new null-delimited string
     640                 :             :  *              containing a formatted ACL specification.  See aclparse for details.
     641                 :             :  *
     642                 :             :  * RETURNS:
     643                 :             :  *              the new string
     644                 :             :  */
     645                 :             : Datum
     646                 :          61 : aclitemout(PG_FUNCTION_ARGS)
     647                 :             : {
     648                 :          61 :         AclItem    *aip = PG_GETARG_ACLITEM_P(0);
     649                 :          61 :         char       *p;
     650                 :          61 :         char       *out;
     651                 :          61 :         HeapTuple       htup;
     652                 :          61 :         unsigned        i;
     653                 :             : 
     654                 :          61 :         out = palloc(strlen("=/") +
     655                 :             :                                  2 * N_ACL_RIGHTS +
     656                 :             :                                  2 * (2 * NAMEDATALEN + 2) +
     657                 :             :                                  1);
     658                 :             : 
     659                 :          61 :         p = out;
     660                 :          61 :         *p = '\0';
     661                 :             : 
     662         [ +  + ]:          61 :         if (aip->ai_grantee != ACL_ID_PUBLIC)
     663                 :             :         {
     664                 :          56 :                 htup = SearchSysCache1(AUTHOID, ObjectIdGetDatum(aip->ai_grantee));
     665         [ +  - ]:          56 :                 if (HeapTupleIsValid(htup))
     666                 :             :                 {
     667                 :          56 :                         putid(p, NameStr(((Form_pg_authid) GETSTRUCT(htup))->rolname));
     668                 :          56 :                         ReleaseSysCache(htup);
     669                 :          56 :                 }
     670                 :             :                 else
     671                 :             :                 {
     672                 :             :                         /* Generate numeric OID if we don't find an entry */
     673                 :           0 :                         sprintf(p, "%u", aip->ai_grantee);
     674                 :             :                 }
     675                 :          56 :         }
     676         [ +  + ]:        1098 :         while (*p)
     677                 :        1037 :                 ++p;
     678                 :             : 
     679                 :          61 :         *p++ = '=';
     680                 :             : 
     681         [ +  + ]:         976 :         for (i = 0; i < N_ACL_RIGHTS; ++i)
     682                 :             :         {
     683         [ +  + ]:         915 :                 if (ACLITEM_GET_PRIVS(*aip) & (UINT64CONST(1) << i))
     684                 :         168 :                         *p++ = ACL_ALL_RIGHTS_STR[i];
     685         [ +  + ]:         915 :                 if (ACLITEM_GET_GOPTIONS(*aip) & (UINT64CONST(1) << i))
     686                 :          24 :                         *p++ = '*';
     687                 :         915 :         }
     688                 :             : 
     689                 :          61 :         *p++ = '/';
     690                 :          61 :         *p = '\0';
     691                 :             : 
     692                 :          61 :         htup = SearchSysCache1(AUTHOID, ObjectIdGetDatum(aip->ai_grantor));
     693         [ +  - ]:          61 :         if (HeapTupleIsValid(htup))
     694                 :             :         {
     695                 :          61 :                 putid(p, NameStr(((Form_pg_authid) GETSTRUCT(htup))->rolname));
     696                 :          61 :                 ReleaseSysCache(htup);
     697                 :          61 :         }
     698                 :             :         else
     699                 :             :         {
     700                 :             :                 /* Generate numeric OID if we don't find an entry */
     701                 :           0 :                 sprintf(p, "%u", aip->ai_grantor);
     702                 :             :         }
     703                 :             : 
     704                 :         122 :         PG_RETURN_CSTRING(out);
     705                 :          61 : }
     706                 :             : 
     707                 :             : /*
     708                 :             :  * aclitem_match
     709                 :             :  *              Two AclItems are considered to match iff they have the same
     710                 :             :  *              grantee and grantor; the privileges are ignored.
     711                 :             :  */
     712                 :             : static bool
     713                 :         945 : aclitem_match(const AclItem *a1, const AclItem *a2)
     714                 :             : {
     715         [ +  + ]:        1253 :         return a1->ai_grantee == a2->ai_grantee &&
     716                 :         308 :                 a1->ai_grantor == a2->ai_grantor;
     717                 :             : }
     718                 :             : 
     719                 :             : /*
     720                 :             :  * aclitemComparator
     721                 :             :  *              qsort comparison function for AclItems
     722                 :             :  */
     723                 :             : static int
     724                 :          45 : aclitemComparator(const void *arg1, const void *arg2)
     725                 :             : {
     726                 :          45 :         const AclItem *a1 = (const AclItem *) arg1;
     727                 :          45 :         const AclItem *a2 = (const AclItem *) arg2;
     728                 :             : 
     729         [ +  + ]:          45 :         if (a1->ai_grantee > a2->ai_grantee)
     730                 :           7 :                 return 1;
     731         [ +  - ]:          38 :         if (a1->ai_grantee < a2->ai_grantee)
     732                 :          38 :                 return -1;
     733         [ #  # ]:           0 :         if (a1->ai_grantor > a2->ai_grantor)
     734                 :           0 :                 return 1;
     735         [ #  # ]:           0 :         if (a1->ai_grantor < a2->ai_grantor)
     736                 :           0 :                 return -1;
     737         [ #  # ]:           0 :         if (a1->ai_privs > a2->ai_privs)
     738                 :           0 :                 return 1;
     739         [ #  # ]:           0 :         if (a1->ai_privs < a2->ai_privs)
     740                 :           0 :                 return -1;
     741                 :           0 :         return 0;
     742                 :          45 : }
     743                 :             : 
     744                 :             : /*
     745                 :             :  * aclitem equality operator
     746                 :             :  */
     747                 :             : Datum
     748                 :        1222 : aclitem_eq(PG_FUNCTION_ARGS)
     749                 :             : {
     750                 :        1222 :         AclItem    *a1 = PG_GETARG_ACLITEM_P(0);
     751                 :        1222 :         AclItem    *a2 = PG_GETARG_ACLITEM_P(1);
     752                 :        1222 :         bool            result;
     753                 :             : 
     754         [ +  + ]:        2354 :         result = a1->ai_privs == a2->ai_privs &&
     755         [ +  + ]:        1132 :                 a1->ai_grantee == a2->ai_grantee &&
     756                 :        1103 :                 a1->ai_grantor == a2->ai_grantor;
     757                 :        2444 :         PG_RETURN_BOOL(result);
     758                 :        1222 : }
     759                 :             : 
     760                 :             : /*
     761                 :             :  * aclitem hash function
     762                 :             :  *
     763                 :             :  * We make aclitems hashable not so much because anyone is likely to hash
     764                 :             :  * them, as because we want array equality to work on aclitem arrays, and
     765                 :             :  * with the typcache mechanism we must have a hash or btree opclass.
     766                 :             :  */
     767                 :             : Datum
     768                 :         468 : hash_aclitem(PG_FUNCTION_ARGS)
     769                 :             : {
     770                 :         468 :         AclItem    *a = PG_GETARG_ACLITEM_P(0);
     771                 :             : 
     772                 :             :         /* not very bright, but avoids any issue of padding in struct */
     773                 :         936 :         PG_RETURN_UINT32((uint32) (a->ai_privs + a->ai_grantee + a->ai_grantor));
     774                 :         468 : }
     775                 :             : 
     776                 :             : /*
     777                 :             :  * 64-bit hash function for aclitem.
     778                 :             :  *
     779                 :             :  * Similar to hash_aclitem, but accepts a seed and returns a uint64 value.
     780                 :             :  */
     781                 :             : Datum
     782                 :           2 : hash_aclitem_extended(PG_FUNCTION_ARGS)
     783                 :             : {
     784                 :           2 :         AclItem    *a = PG_GETARG_ACLITEM_P(0);
     785                 :           2 :         uint64          seed = PG_GETARG_INT64(1);
     786                 :           2 :         uint32          sum = (uint32) (a->ai_privs + a->ai_grantee + a->ai_grantor);
     787                 :             : 
     788         [ +  + ]:           2 :         return (seed == 0) ? UInt64GetDatum(sum) : hash_uint32_extended(sum, seed);
     789                 :           2 : }
     790                 :             : 
     791                 :             : /*
     792                 :             :  * acldefault()  --- create an ACL describing default access permissions
     793                 :             :  *
     794                 :             :  * Change this routine if you want to alter the default access policy for
     795                 :             :  * newly-created objects (or any object with a NULL acl entry).  When
     796                 :             :  * you make a change here, don't forget to update the GRANT man page,
     797                 :             :  * which explains all the default permissions.
     798                 :             :  *
     799                 :             :  * Note that these are the hard-wired "defaults" that are used in the
     800                 :             :  * absence of any pg_default_acl entry.
     801                 :             :  */
     802                 :             : Acl *
     803                 :       10608 : acldefault(ObjectType objtype, Oid ownerId)
     804                 :             : {
     805                 :       10608 :         AclMode         world_default;
     806                 :       10608 :         AclMode         owner_default;
     807                 :       10608 :         int                     nacl;
     808                 :       10608 :         Acl                *acl;
     809                 :       10608 :         AclItem    *aip;
     810                 :             : 
     811   [ +  +  +  +  :       10608 :         switch (objtype)
          +  +  +  +  +  
             +  +  +  -  
                      - ]
     812                 :             :         {
     813                 :             :                 case OBJECT_COLUMN:
     814                 :             :                         /* by default, columns have no extra privileges */
     815                 :         763 :                         world_default = ACL_NO_RIGHTS;
     816                 :         763 :                         owner_default = ACL_NO_RIGHTS;
     817                 :         763 :                         break;
     818                 :             :                 case OBJECT_TABLE:
     819                 :        2764 :                         world_default = ACL_NO_RIGHTS;
     820                 :        2764 :                         owner_default = ACL_ALL_RIGHTS_RELATION;
     821                 :        2764 :                         break;
     822                 :             :                 case OBJECT_SEQUENCE:
     823                 :          19 :                         world_default = ACL_NO_RIGHTS;
     824                 :          19 :                         owner_default = ACL_ALL_RIGHTS_SEQUENCE;
     825                 :          19 :                         break;
     826                 :             :                 case OBJECT_DATABASE:
     827                 :             :                         /* for backwards compatibility, grant some rights by default */
     828                 :          49 :                         world_default = ACL_CREATE_TEMP | ACL_CONNECT;
     829                 :          49 :                         owner_default = ACL_ALL_RIGHTS_DATABASE;
     830                 :          49 :                         break;
     831                 :             :                 case OBJECT_FUNCTION:
     832                 :             :                         /* Grant EXECUTE by default, for now */
     833                 :        6111 :                         world_default = ACL_EXECUTE;
     834                 :        6111 :                         owner_default = ACL_ALL_RIGHTS_FUNCTION;
     835                 :        6111 :                         break;
     836                 :             :                 case OBJECT_LANGUAGE:
     837                 :             :                         /* Grant USAGE by default, for now */
     838                 :          51 :                         world_default = ACL_USAGE;
     839                 :          51 :                         owner_default = ACL_ALL_RIGHTS_LANGUAGE;
     840                 :          51 :                         break;
     841                 :             :                 case OBJECT_LARGEOBJECT:
     842                 :          27 :                         world_default = ACL_NO_RIGHTS;
     843                 :          27 :                         owner_default = ACL_ALL_RIGHTS_LARGEOBJECT;
     844                 :          27 :                         break;
     845                 :             :                 case OBJECT_SCHEMA:
     846                 :          76 :                         world_default = ACL_NO_RIGHTS;
     847                 :          76 :                         owner_default = ACL_ALL_RIGHTS_SCHEMA;
     848                 :          76 :                         break;
     849                 :             :                 case OBJECT_TABLESPACE:
     850                 :           4 :                         world_default = ACL_NO_RIGHTS;
     851                 :           4 :                         owner_default = ACL_ALL_RIGHTS_TABLESPACE;
     852                 :           4 :                         break;
     853                 :             :                 case OBJECT_FDW:
     854                 :           3 :                         world_default = ACL_NO_RIGHTS;
     855                 :           3 :                         owner_default = ACL_ALL_RIGHTS_FDW;
     856                 :           3 :                         break;
     857                 :             :                 case OBJECT_FOREIGN_SERVER:
     858                 :          21 :                         world_default = ACL_NO_RIGHTS;
     859                 :          21 :                         owner_default = ACL_ALL_RIGHTS_FOREIGN_SERVER;
     860                 :          21 :                         break;
     861                 :             :                 case OBJECT_DOMAIN:
     862                 :             :                 case OBJECT_TYPE:
     863                 :         720 :                         world_default = ACL_USAGE;
     864                 :         720 :                         owner_default = ACL_ALL_RIGHTS_TYPE;
     865                 :         720 :                         break;
     866                 :             :                 case OBJECT_PARAMETER_ACL:
     867                 :           0 :                         world_default = ACL_NO_RIGHTS;
     868                 :           0 :                         owner_default = ACL_ALL_RIGHTS_PARAMETER_ACL;
     869                 :           0 :                         break;
     870                 :             :                 default:
     871   [ #  #  #  # ]:           0 :                         elog(ERROR, "unrecognized object type: %d", (int) objtype);
     872                 :           0 :                         world_default = ACL_NO_RIGHTS;  /* keep compiler quiet */
     873                 :           0 :                         owner_default = ACL_NO_RIGHTS;
     874                 :           0 :                         break;
     875                 :             :         }
     876                 :             : 
     877                 :       10608 :         nacl = 0;
     878         [ +  + ]:       10608 :         if (world_default != ACL_NO_RIGHTS)
     879                 :        6931 :                 nacl++;
     880         [ +  + ]:       10608 :         if (owner_default != ACL_NO_RIGHTS)
     881                 :        9845 :                 nacl++;
     882                 :             : 
     883                 :       10608 :         acl = allocacl(nacl);
     884         [ -  + ]:       10608 :         aip = ACL_DAT(acl);
     885                 :             : 
     886         [ +  + ]:       10608 :         if (world_default != ACL_NO_RIGHTS)
     887                 :             :         {
     888                 :        6931 :                 aip->ai_grantee = ACL_ID_PUBLIC;
     889                 :        6931 :                 aip->ai_grantor = ownerId;
     890                 :        6931 :                 ACLITEM_SET_PRIVS_GOPTIONS(*aip, world_default, ACL_NO_RIGHTS);
     891                 :        6931 :                 aip++;
     892                 :        6931 :         }
     893                 :             : 
     894                 :             :         /*
     895                 :             :          * Note that the owner's entry shows all ordinary privileges but no grant
     896                 :             :          * options.  This is because his grant options come "from the system" and
     897                 :             :          * not from his own efforts.  (The SQL spec says that the owner's rights
     898                 :             :          * come from a "_SYSTEM" authid.)  However, we do consider that the
     899                 :             :          * owner's ordinary privileges are self-granted; this lets him revoke
     900                 :             :          * them.  We implement the owner's grant options without any explicit
     901                 :             :          * "_SYSTEM"-like ACL entry, by internally special-casing the owner
     902                 :             :          * wherever we are testing grant options.
     903                 :             :          */
     904         [ +  + ]:       10608 :         if (owner_default != ACL_NO_RIGHTS)
     905                 :             :         {
     906                 :        9845 :                 aip->ai_grantee = ownerId;
     907                 :        9845 :                 aip->ai_grantor = ownerId;
     908                 :        9845 :                 ACLITEM_SET_PRIVS_GOPTIONS(*aip, owner_default, ACL_NO_RIGHTS);
     909                 :        9845 :         }
     910                 :             : 
     911                 :       21216 :         return acl;
     912                 :       10608 : }
     913                 :             : 
     914                 :             : 
     915                 :             : /*
     916                 :             :  * SQL-accessible version of acldefault().  Hackish mapping from "char" type to
     917                 :             :  * OBJECT_* values.
     918                 :             :  */
     919                 :             : Datum
     920                 :        1002 : acldefault_sql(PG_FUNCTION_ARGS)
     921                 :             : {
     922                 :        1002 :         char            objtypec = PG_GETARG_CHAR(0);
     923                 :        1002 :         Oid                     owner = PG_GETARG_OID(1);
     924                 :        1002 :         ObjectType      objtype = 0;
     925                 :             : 
     926   [ -  -  +  -  :        1002 :         switch (objtypec)
          -  -  -  -  -  
             -  -  -  -  
                      - ]
     927                 :             :         {
     928                 :             :                 case 'c':
     929                 :           0 :                         objtype = OBJECT_COLUMN;
     930                 :           0 :                         break;
     931                 :             :                 case 'r':
     932                 :        1002 :                         objtype = OBJECT_TABLE;
     933                 :        1002 :                         break;
     934                 :             :                 case 's':
     935                 :           0 :                         objtype = OBJECT_SEQUENCE;
     936                 :           0 :                         break;
     937                 :             :                 case 'd':
     938                 :           0 :                         objtype = OBJECT_DATABASE;
     939                 :           0 :                         break;
     940                 :             :                 case 'f':
     941                 :           0 :                         objtype = OBJECT_FUNCTION;
     942                 :           0 :                         break;
     943                 :             :                 case 'l':
     944                 :           0 :                         objtype = OBJECT_LANGUAGE;
     945                 :           0 :                         break;
     946                 :             :                 case 'L':
     947                 :           0 :                         objtype = OBJECT_LARGEOBJECT;
     948                 :           0 :                         break;
     949                 :             :                 case 'n':
     950                 :           0 :                         objtype = OBJECT_SCHEMA;
     951                 :           0 :                         break;
     952                 :             :                 case 'p':
     953                 :           0 :                         objtype = OBJECT_PARAMETER_ACL;
     954                 :           0 :                         break;
     955                 :             :                 case 't':
     956                 :           0 :                         objtype = OBJECT_TABLESPACE;
     957                 :           0 :                         break;
     958                 :             :                 case 'F':
     959                 :           0 :                         objtype = OBJECT_FDW;
     960                 :           0 :                         break;
     961                 :             :                 case 'S':
     962                 :           0 :                         objtype = OBJECT_FOREIGN_SERVER;
     963                 :           0 :                         break;
     964                 :             :                 case 'T':
     965                 :           0 :                         objtype = OBJECT_TYPE;
     966                 :           0 :                         break;
     967                 :             :                 default:
     968   [ #  #  #  # ]:           0 :                         elog(ERROR, "unrecognized object type abbreviation: %c", objtypec);
     969                 :           0 :         }
     970                 :             : 
     971                 :        2004 :         PG_RETURN_ACL_P(acldefault(objtype, owner));
     972                 :        1002 : }
     973                 :             : 
     974                 :             : 
     975                 :             : /*
     976                 :             :  * Update an ACL array to add or remove specified privileges.
     977                 :             :  *
     978                 :             :  *      old_acl: the input ACL array
     979                 :             :  *      mod_aip: defines the privileges to be added, removed, or substituted
     980                 :             :  *      modechg: ACL_MODECHG_ADD, ACL_MODECHG_DEL, or ACL_MODECHG_EQL
     981                 :             :  *      ownerId: Oid of object owner
     982                 :             :  *      behavior: RESTRICT or CASCADE behavior for recursive removal
     983                 :             :  *
     984                 :             :  * ownerid and behavior are only relevant when the update operation specifies
     985                 :             :  * deletion of grant options.
     986                 :             :  *
     987                 :             :  * The result is a modified copy; the input object is not changed.
     988                 :             :  *
     989                 :             :  * NB: caller is responsible for having detoasted the input ACL, if needed.
     990                 :             :  */
     991                 :             : Acl *
     992                 :        1459 : aclupdate(const Acl *old_acl, const AclItem *mod_aip,
     993                 :             :                   int modechg, Oid ownerId, DropBehavior behavior)
     994                 :             : {
     995                 :        1459 :         Acl                *new_acl = NULL;
     996                 :        1459 :         AclItem    *old_aip,
     997                 :        1459 :                            *new_aip = NULL;
     998                 :        1459 :         AclMode         old_rights,
     999                 :             :                                 old_goptions,
    1000                 :             :                                 new_rights,
    1001                 :             :                                 new_goptions;
    1002                 :        1459 :         int                     dst,
    1003                 :             :                                 num;
    1004                 :             : 
    1005                 :             :         /* Caller probably already checked old_acl, but be safe */
    1006                 :        1459 :         check_acl(old_acl);
    1007                 :             : 
    1008                 :             :         /* If granting grant options, check for circularity */
    1009   [ +  +  +  + ]:        1459 :         if (modechg != ACL_MODECHG_DEL &&
    1010                 :         492 :                 ACLITEM_GET_GOPTIONS(*mod_aip) != ACL_NO_RIGHTS)
    1011                 :          14 :                 check_circularity(old_acl, mod_aip, ownerId);
    1012                 :             : 
    1013                 :        1459 :         num = ACL_NUM(old_acl);
    1014         [ -  + ]:        1459 :         old_aip = ACL_DAT(old_acl);
    1015                 :             : 
    1016                 :             :         /*
    1017                 :             :          * Search the ACL for an existing entry for this grantee and grantor. If
    1018                 :             :          * one exists, just modify the entry in-place (well, in the same position,
    1019                 :             :          * since we actually return a copy); otherwise, insert the new entry at
    1020                 :             :          * the end.
    1021                 :             :          */
    1022                 :             : 
    1023         [ +  + ]:        2100 :         for (dst = 0; dst < num; ++dst)
    1024                 :             :         {
    1025         [ +  + ]:         944 :                 if (aclitem_match(mod_aip, old_aip + dst))
    1026                 :             :                 {
    1027                 :             :                         /* found a match, so modify existing item */
    1028                 :         303 :                         new_acl = allocacl(num);
    1029         [ -  + ]:         303 :                         new_aip = ACL_DAT(new_acl);
    1030                 :         303 :                         memcpy(new_acl, old_acl, ACL_SIZE(old_acl));
    1031                 :         303 :                         break;
    1032                 :             :                 }
    1033                 :         641 :         }
    1034                 :             : 
    1035         [ +  + ]:        1459 :         if (dst == num)
    1036                 :             :         {
    1037                 :             :                 /* need to append a new item */
    1038                 :        1156 :                 new_acl = allocacl(num + 1);
    1039         [ -  + ]:        1156 :                 new_aip = ACL_DAT(new_acl);
    1040                 :        1156 :                 memcpy(new_aip, old_aip, num * sizeof(AclItem));
    1041                 :             : 
    1042                 :             :                 /* initialize the new entry with no permissions */
    1043                 :        1156 :                 new_aip[dst].ai_grantee = mod_aip->ai_grantee;
    1044                 :        1156 :                 new_aip[dst].ai_grantor = mod_aip->ai_grantor;
    1045                 :        1156 :                 ACLITEM_SET_PRIVS_GOPTIONS(new_aip[dst],
    1046                 :             :                                                                    ACL_NO_RIGHTS, ACL_NO_RIGHTS);
    1047                 :        1156 :                 num++;                                  /* set num to the size of new_acl */
    1048                 :        1156 :         }
    1049                 :             : 
    1050                 :        1459 :         old_rights = ACLITEM_GET_RIGHTS(new_aip[dst]);
    1051                 :        1459 :         old_goptions = ACLITEM_GET_GOPTIONS(new_aip[dst]);
    1052                 :             : 
    1053                 :             :         /* apply the specified permissions change */
    1054   [ +  -  +  - ]:        1459 :         switch (modechg)
    1055                 :             :         {
    1056                 :             :                 case ACL_MODECHG_ADD:
    1057                 :         492 :                         ACLITEM_SET_RIGHTS(new_aip[dst],
    1058                 :             :                                                            old_rights | ACLITEM_GET_RIGHTS(*mod_aip));
    1059                 :         492 :                         break;
    1060                 :             :                 case ACL_MODECHG_DEL:
    1061                 :         967 :                         ACLITEM_SET_RIGHTS(new_aip[dst],
    1062                 :             :                                                            old_rights & ~ACLITEM_GET_RIGHTS(*mod_aip));
    1063                 :         967 :                         break;
    1064                 :             :                 case ACL_MODECHG_EQL:
    1065                 :           0 :                         ACLITEM_SET_RIGHTS(new_aip[dst],
    1066                 :             :                                                            ACLITEM_GET_RIGHTS(*mod_aip));
    1067                 :           0 :                         break;
    1068                 :             :         }
    1069                 :             : 
    1070                 :        1459 :         new_rights = ACLITEM_GET_RIGHTS(new_aip[dst]);
    1071                 :        1459 :         new_goptions = ACLITEM_GET_GOPTIONS(new_aip[dst]);
    1072                 :             : 
    1073                 :             :         /*
    1074                 :             :          * If the adjusted entry has no permissions, delete it from the list.
    1075                 :             :          */
    1076         [ +  + ]:        1459 :         if (new_rights == ACL_NO_RIGHTS)
    1077                 :             :         {
    1078                 :         948 :                 memmove(new_aip + dst,
    1079                 :             :                                 new_aip + dst + 1,
    1080                 :             :                                 (num - dst - 1) * sizeof(AclItem));
    1081                 :             :                 /* Adjust array size to be 'num - 1' items */
    1082                 :         948 :                 ARR_DIMS(new_acl)[0] = num - 1;
    1083                 :         948 :                 SET_VARSIZE(new_acl, ACL_N_SIZE(num - 1));
    1084                 :         948 :         }
    1085                 :             : 
    1086                 :             :         /*
    1087                 :             :          * Remove abandoned privileges (cascading revoke).  Currently we can only
    1088                 :             :          * handle this when the grantee is not PUBLIC.
    1089                 :             :          */
    1090         [ +  + ]:        1459 :         if ((old_goptions & ~new_goptions) != 0)
    1091                 :             :         {
    1092         [ +  - ]:          13 :                 Assert(mod_aip->ai_grantee != ACL_ID_PUBLIC);
    1093                 :          26 :                 new_acl = recursive_revoke(new_acl, mod_aip->ai_grantee,
    1094                 :          13 :                                                                    (old_goptions & ~new_goptions),
    1095                 :          13 :                                                                    ownerId, behavior);
    1096                 :          13 :         }
    1097                 :             : 
    1098                 :        2918 :         return new_acl;
    1099                 :        1459 : }
    1100                 :             : 
    1101                 :             : /*
    1102                 :             :  * Update an ACL array to reflect a change of owner to the parent object
    1103                 :             :  *
    1104                 :             :  *      old_acl: the input ACL array (must not be NULL)
    1105                 :             :  *      oldOwnerId: Oid of the old object owner
    1106                 :             :  *      newOwnerId: Oid of the new object owner
    1107                 :             :  *
    1108                 :             :  * The result is a modified copy; the input object is not changed.
    1109                 :             :  *
    1110                 :             :  * NB: caller is responsible for having detoasted the input ACL, if needed.
    1111                 :             :  *
    1112                 :             :  * Note: the name of this function is a bit of a misnomer, since it will
    1113                 :             :  * happily make the specified role substitution whether the old role is
    1114                 :             :  * really the owner of the parent object or merely mentioned in its ACL.
    1115                 :             :  * But the vast majority of callers use it in connection with ALTER OWNER
    1116                 :             :  * operations, so we'll keep the name.
    1117                 :             :  */
    1118                 :             : Acl *
    1119                 :           8 : aclnewowner(const Acl *old_acl, Oid oldOwnerId, Oid newOwnerId)
    1120                 :             : {
    1121                 :           8 :         Acl                *new_acl;
    1122                 :           8 :         AclItem    *new_aip;
    1123                 :           8 :         AclItem    *old_aip;
    1124                 :           8 :         AclItem    *dst_aip;
    1125                 :           8 :         AclItem    *src_aip;
    1126                 :           8 :         AclItem    *targ_aip;
    1127                 :           8 :         bool            newpresent = false;
    1128                 :           8 :         int                     dst,
    1129                 :             :                                 src,
    1130                 :             :                                 targ,
    1131                 :             :                                 num;
    1132                 :             : 
    1133                 :           8 :         check_acl(old_acl);
    1134                 :             : 
    1135                 :             :         /*
    1136                 :             :          * Make a copy of the given ACL, substituting new owner ID for old
    1137                 :             :          * wherever it appears as either grantor or grantee.  Also note if the new
    1138                 :             :          * owner ID is already present.
    1139                 :             :          */
    1140                 :           8 :         num = ACL_NUM(old_acl);
    1141         [ -  + ]:           8 :         old_aip = ACL_DAT(old_acl);
    1142                 :           8 :         new_acl = allocacl(num);
    1143         [ -  + ]:           8 :         new_aip = ACL_DAT(new_acl);
    1144                 :           8 :         memcpy(new_aip, old_aip, num * sizeof(AclItem));
    1145         [ +  + ]:          19 :         for (dst = 0, dst_aip = new_aip; dst < num; dst++, dst_aip++)
    1146                 :             :         {
    1147         [ +  - ]:          11 :                 if (dst_aip->ai_grantor == oldOwnerId)
    1148                 :          11 :                         dst_aip->ai_grantor = newOwnerId;
    1149         [ #  # ]:           0 :                 else if (dst_aip->ai_grantor == newOwnerId)
    1150                 :           0 :                         newpresent = true;
    1151         [ +  + ]:          11 :                 if (dst_aip->ai_grantee == oldOwnerId)
    1152                 :           8 :                         dst_aip->ai_grantee = newOwnerId;
    1153         [ +  + ]:           3 :                 else if (dst_aip->ai_grantee == newOwnerId)
    1154                 :           1 :                         newpresent = true;
    1155                 :          11 :         }
    1156                 :             : 
    1157                 :             :         /*
    1158                 :             :          * If the old ACL contained any references to the new owner, then we may
    1159                 :             :          * now have generated an ACL containing duplicate entries.  Find them and
    1160                 :             :          * merge them so that there are not duplicates.  (This is relatively
    1161                 :             :          * expensive since we use a stupid O(N^2) algorithm, but it's unlikely to
    1162                 :             :          * be the normal case.)
    1163                 :             :          *
    1164                 :             :          * To simplify deletion of duplicate entries, we temporarily leave them in
    1165                 :             :          * the array but set their privilege masks to zero; when we reach such an
    1166                 :             :          * entry it's just skipped.  (Thus, a side effect of this code will be to
    1167                 :             :          * remove privilege-free entries, should there be any in the input.)  dst
    1168                 :             :          * is the next output slot, targ is the currently considered input slot
    1169                 :             :          * (always >= dst), and src scans entries to the right of targ looking for
    1170                 :             :          * duplicates.  Once an entry has been emitted to dst it is known
    1171                 :             :          * duplicate-free and need not be considered anymore.
    1172                 :             :          */
    1173         [ +  + ]:           8 :         if (newpresent)
    1174                 :             :         {
    1175                 :           1 :                 dst = 0;
    1176         [ +  + ]:           3 :                 for (targ = 0, targ_aip = new_aip; targ < num; targ++, targ_aip++)
    1177                 :             :                 {
    1178                 :             :                         /* ignore if deleted in an earlier pass */
    1179         [ +  + ]:           2 :                         if (ACLITEM_GET_RIGHTS(*targ_aip) == ACL_NO_RIGHTS)
    1180                 :           1 :                                 continue;
    1181                 :             :                         /* find and merge any duplicates */
    1182         [ +  + ]:           2 :                         for (src = targ + 1, src_aip = targ_aip + 1; src < num;
    1183                 :           1 :                                  src++, src_aip++)
    1184                 :             :                         {
    1185         [ +  - ]:           1 :                                 if (ACLITEM_GET_RIGHTS(*src_aip) == ACL_NO_RIGHTS)
    1186                 :           0 :                                         continue;
    1187         [ -  + ]:           1 :                                 if (aclitem_match(targ_aip, src_aip))
    1188                 :             :                                 {
    1189                 :           1 :                                         ACLITEM_SET_RIGHTS(*targ_aip,
    1190                 :             :                                                                            ACLITEM_GET_RIGHTS(*targ_aip) |
    1191                 :             :                                                                            ACLITEM_GET_RIGHTS(*src_aip));
    1192                 :             :                                         /* mark the duplicate deleted */
    1193                 :           1 :                                         ACLITEM_SET_RIGHTS(*src_aip, ACL_NO_RIGHTS);
    1194                 :           1 :                                 }
    1195                 :           1 :                         }
    1196                 :             :                         /* and emit to output */
    1197                 :           1 :                         new_aip[dst] = *targ_aip;
    1198                 :           1 :                         dst++;
    1199                 :           1 :                 }
    1200                 :             :                 /* Adjust array size to be 'dst' items */
    1201                 :           1 :                 ARR_DIMS(new_acl)[0] = dst;
    1202                 :           1 :                 SET_VARSIZE(new_acl, ACL_N_SIZE(dst));
    1203                 :           1 :         }
    1204                 :             : 
    1205                 :          16 :         return new_acl;
    1206                 :           8 : }
    1207                 :             : 
    1208                 :             : 
    1209                 :             : /*
    1210                 :             :  * When granting grant options, we must disallow attempts to set up circular
    1211                 :             :  * chains of grant options.  Suppose A (the object owner) grants B some
    1212                 :             :  * privileges with grant option, and B re-grants them to C.  If C could
    1213                 :             :  * grant the privileges to B as well, then A would be unable to effectively
    1214                 :             :  * revoke the privileges from B, since recursive_revoke would consider that
    1215                 :             :  * B still has 'em from C.
    1216                 :             :  *
    1217                 :             :  * We check for this by recursively deleting all grant options belonging to
    1218                 :             :  * the target grantee, and then seeing if the would-be grantor still has the
    1219                 :             :  * grant option or not.
    1220                 :             :  */
    1221                 :             : static void
    1222                 :          14 : check_circularity(const Acl *old_acl, const AclItem *mod_aip,
    1223                 :             :                                   Oid ownerId)
    1224                 :             : {
    1225                 :          14 :         Acl                *acl;
    1226                 :          14 :         AclItem    *aip;
    1227                 :          14 :         int                     i,
    1228                 :             :                                 num;
    1229                 :          14 :         AclMode         own_privs;
    1230                 :             : 
    1231                 :          14 :         check_acl(old_acl);
    1232                 :             : 
    1233                 :             :         /*
    1234                 :             :          * For now, grant options can only be granted to roles, not PUBLIC.
    1235                 :             :          * Otherwise we'd have to work a bit harder here.
    1236                 :             :          */
    1237         [ +  - ]:          14 :         Assert(mod_aip->ai_grantee != ACL_ID_PUBLIC);
    1238                 :             : 
    1239                 :             :         /* The owner always has grant options, no need to check */
    1240         [ +  + ]:          14 :         if (mod_aip->ai_grantor == ownerId)
    1241                 :          11 :                 return;
    1242                 :             : 
    1243                 :             :         /* Make a working copy */
    1244                 :           3 :         acl = allocacl(ACL_NUM(old_acl));
    1245                 :           3 :         memcpy(acl, old_acl, ACL_SIZE(old_acl));
    1246                 :             : 
    1247                 :             :         /* Zap all grant options of target grantee, plus what depends on 'em */
    1248                 :             : cc_restart:
    1249                 :           4 :         num = ACL_NUM(acl);
    1250         [ -  + ]:           4 :         aip = ACL_DAT(acl);
    1251         [ +  + ]:          16 :         for (i = 0; i < num; i++)
    1252                 :             :         {
    1253   [ +  +  +  - ]:          13 :                 if (aip[i].ai_grantee == mod_aip->ai_grantee &&
    1254                 :           1 :                         ACLITEM_GET_GOPTIONS(aip[i]) != ACL_NO_RIGHTS)
    1255                 :             :                 {
    1256                 :           1 :                         Acl                *new_acl;
    1257                 :             : 
    1258                 :             :                         /* We'll actually zap ordinary privs too, but no matter */
    1259                 :           2 :                         new_acl = aclupdate(acl, &aip[i], ACL_MODECHG_DEL,
    1260                 :           1 :                                                                 ownerId, DROP_CASCADE);
    1261                 :             : 
    1262                 :           1 :                         pfree(acl);
    1263                 :           1 :                         acl = new_acl;
    1264                 :             : 
    1265                 :             :                         goto cc_restart;
    1266                 :           1 :                 }
    1267                 :          12 :         }
    1268                 :             : 
    1269                 :             :         /* Now we can compute grantor's independently-derived privileges */
    1270                 :           6 :         own_privs = aclmask(acl,
    1271                 :           3 :                                                 mod_aip->ai_grantor,
    1272                 :           3 :                                                 ownerId,
    1273                 :           3 :                                                 ACL_GRANT_OPTION_FOR(ACLITEM_GET_GOPTIONS(*mod_aip)),
    1274                 :             :                                                 ACLMASK_ALL);
    1275                 :           3 :         own_privs = ACL_OPTION_TO_PRIVS(own_privs);
    1276                 :             : 
    1277         [ +  - ]:           3 :         if ((ACLITEM_GET_GOPTIONS(*mod_aip) & ~own_privs) != 0)
    1278   [ #  #  #  # ]:           0 :                 ereport(ERROR,
    1279                 :             :                                 (errcode(ERRCODE_INVALID_GRANT_OPERATION),
    1280                 :             :                                  errmsg("grant options cannot be granted back to your own grantor")));
    1281                 :             : 
    1282                 :           3 :         pfree(acl);
    1283         [ -  + ]:          14 : }
    1284                 :             : 
    1285                 :             : 
    1286                 :             : /*
    1287                 :             :  * Ensure that no privilege is "abandoned".  A privilege is abandoned
    1288                 :             :  * if the user that granted the privilege loses the grant option.  (So
    1289                 :             :  * the chain through which it was granted is broken.)  Either the
    1290                 :             :  * abandoned privileges are revoked as well, or an error message is
    1291                 :             :  * printed, depending on the drop behavior option.
    1292                 :             :  *
    1293                 :             :  *      acl: the input ACL list
    1294                 :             :  *      grantee: the user from whom some grant options have been revoked
    1295                 :             :  *      revoke_privs: the grant options being revoked
    1296                 :             :  *      ownerId: Oid of object owner
    1297                 :             :  *      behavior: RESTRICT or CASCADE behavior for recursive removal
    1298                 :             :  *
    1299                 :             :  * The input Acl object is pfree'd if replaced.
    1300                 :             :  */
    1301                 :             : static Acl *
    1302                 :          13 : recursive_revoke(Acl *acl,
    1303                 :             :                                  Oid grantee,
    1304                 :             :                                  AclMode revoke_privs,
    1305                 :             :                                  Oid ownerId,
    1306                 :             :                                  DropBehavior behavior)
    1307                 :             : {
    1308                 :          13 :         AclMode         still_has;
    1309                 :          13 :         AclItem    *aip;
    1310                 :          13 :         int                     i,
    1311                 :             :                                 num;
    1312                 :             : 
    1313                 :          13 :         check_acl(acl);
    1314                 :             : 
    1315                 :             :         /* The owner can never truly lose grant options, so short-circuit */
    1316         [ -  + ]:          13 :         if (grantee == ownerId)
    1317                 :           0 :                 return acl;
    1318                 :             : 
    1319                 :             :         /* The grantee might still have some grant options via another grantor */
    1320                 :          26 :         still_has = aclmask(acl, grantee, ownerId,
    1321                 :          13 :                                                 ACL_GRANT_OPTION_FOR(revoke_privs),
    1322                 :             :                                                 ACLMASK_ALL);
    1323                 :          13 :         revoke_privs &= ~ACL_OPTION_TO_PRIVS(still_has);
    1324         [ +  + ]:          13 :         if (revoke_privs == ACL_NO_RIGHTS)
    1325                 :           1 :                 return acl;
    1326                 :             : 
    1327                 :             : restart:
    1328                 :          18 :         num = ACL_NUM(acl);
    1329         [ -  + ]:          18 :         aip = ACL_DAT(acl);
    1330         [ +  + ]:          59 :         for (i = 0; i < num; i++)
    1331                 :             :         {
    1332                 :          49 :                 if (aip[i].ai_grantor == grantee
    1333   [ +  +  -  + ]:          49 :                         && (ACLITEM_GET_PRIVS(aip[i]) & revoke_privs) != 0)
    1334                 :             :                 {
    1335                 :           8 :                         AclItem         mod_acl;
    1336                 :           8 :                         Acl                *new_acl;
    1337                 :             : 
    1338         [ +  + ]:           8 :                         if (behavior == DROP_RESTRICT)
    1339   [ +  -  +  - ]:           2 :                                 ereport(ERROR,
    1340                 :             :                                                 (errcode(ERRCODE_DEPENDENT_OBJECTS_STILL_EXIST),
    1341                 :             :                                                  errmsg("dependent privileges exist"),
    1342                 :             :                                                  errhint("Use CASCADE to revoke them too.")));
    1343                 :             : 
    1344                 :           6 :                         mod_acl.ai_grantor = grantee;
    1345                 :           6 :                         mod_acl.ai_grantee = aip[i].ai_grantee;
    1346                 :           6 :                         ACLITEM_SET_PRIVS_GOPTIONS(mod_acl,
    1347                 :             :                                                                            revoke_privs,
    1348                 :             :                                                                            revoke_privs);
    1349                 :             : 
    1350                 :          12 :                         new_acl = aclupdate(acl, &mod_acl, ACL_MODECHG_DEL,
    1351                 :           6 :                                                                 ownerId, behavior);
    1352                 :             : 
    1353                 :           6 :                         pfree(acl);
    1354                 :           6 :                         acl = new_acl;
    1355                 :             : 
    1356                 :             :                         goto restart;
    1357                 :           6 :                 }
    1358                 :          41 :         }
    1359                 :             : 
    1360                 :          10 :         return acl;
    1361                 :          11 : }
    1362                 :             : 
    1363                 :             : 
    1364                 :             : /*
    1365                 :             :  * aclmask --- compute bitmask of all privileges held by roleid.
    1366                 :             :  *
    1367                 :             :  * When 'how' = ACLMASK_ALL, this simply returns the privilege bits
    1368                 :             :  * held by the given roleid according to the given ACL list, ANDed
    1369                 :             :  * with 'mask'.  (The point of passing 'mask' is to let the routine
    1370                 :             :  * exit early if all privileges of interest have been found.)
    1371                 :             :  *
    1372                 :             :  * When 'how' = ACLMASK_ANY, returns as soon as any bit in the mask
    1373                 :             :  * is known true.  (This lets us exit soonest in cases where the
    1374                 :             :  * caller is only going to test for zero or nonzero result.)
    1375                 :             :  *
    1376                 :             :  * Usage patterns:
    1377                 :             :  *
    1378                 :             :  * To see if any of a set of privileges are held:
    1379                 :             :  *              if (aclmask(acl, roleid, ownerId, privs, ACLMASK_ANY) != 0)
    1380                 :             :  *
    1381                 :             :  * To see if all of a set of privileges are held:
    1382                 :             :  *              if (aclmask(acl, roleid, ownerId, privs, ACLMASK_ALL) == privs)
    1383                 :             :  *
    1384                 :             :  * To determine exactly which of a set of privileges are held:
    1385                 :             :  *              heldprivs = aclmask(acl, roleid, ownerId, privs, ACLMASK_ALL);
    1386                 :             :  */
    1387                 :             : AclMode
    1388                 :       16025 : aclmask(const Acl *acl, Oid roleid, Oid ownerId,
    1389                 :             :                 AclMode mask, AclMaskHow how)
    1390                 :             : {
    1391                 :       16025 :         AclMode         result;
    1392                 :       16025 :         AclMode         remaining;
    1393                 :       16025 :         AclItem    *aidat;
    1394                 :       16025 :         int                     i,
    1395                 :             :                                 num;
    1396                 :             : 
    1397                 :             :         /*
    1398                 :             :          * Null ACL should not happen, since caller should have inserted
    1399                 :             :          * appropriate default
    1400                 :             :          */
    1401         [ +  - ]:       16025 :         if (acl == NULL)
    1402   [ #  #  #  # ]:           0 :                 elog(ERROR, "null ACL");
    1403                 :             : 
    1404                 :       16025 :         check_acl(acl);
    1405                 :             : 
    1406                 :             :         /* Quick exit for mask == 0 */
    1407         [ +  + ]:       16025 :         if (mask == 0)
    1408                 :          11 :                 return 0;
    1409                 :             : 
    1410                 :       16014 :         result = 0;
    1411                 :             : 
    1412                 :             :         /* Owner always implicitly has all grant options */
    1413   [ +  +  +  + ]:       16014 :         if ((mask & ACLITEM_ALL_GOPTION_BITS) &&
    1414                 :          29 :                 has_privs_of_role(roleid, ownerId))
    1415                 :             :         {
    1416                 :           1 :                 result = mask & ACLITEM_ALL_GOPTION_BITS;
    1417   [ -  +  +  - ]:           1 :                 if ((how == ACLMASK_ALL) ? (result == mask) : (result != 0))
    1418                 :           1 :                         return result;
    1419                 :           0 :         }
    1420                 :             : 
    1421                 :       16013 :         num = ACL_NUM(acl);
    1422         [ -  + ]:       16013 :         aidat = ACL_DAT(acl);
    1423                 :             : 
    1424                 :             :         /*
    1425                 :             :          * Check privileges granted directly to roleid or to public
    1426                 :             :          */
    1427         [ +  + ]:       24253 :         for (i = 0; i < num; i++)
    1428                 :             :         {
    1429                 :       22871 :                 AclItem    *aidata = &aidat[i];
    1430                 :             : 
    1431   [ +  +  +  + ]:       22871 :                 if (aidata->ai_grantee == ACL_ID_PUBLIC ||
    1432                 :       10568 :                         aidata->ai_grantee == roleid)
    1433                 :             :                 {
    1434                 :       15038 :                         result |= aidata->ai_privs & mask;
    1435   [ +  +  +  + ]:       15038 :                         if ((how == ACLMASK_ALL) ? (result == mask) : (result != 0))
    1436                 :       14631 :                                 return result;
    1437                 :         407 :                 }
    1438         [ +  + ]:       22871 :         }
    1439                 :             : 
    1440                 :             :         /*
    1441                 :             :          * Check privileges granted indirectly via role memberships. We do this in
    1442                 :             :          * a separate pass to minimize expensive indirect membership tests.  In
    1443                 :             :          * particular, it's worth testing whether a given ACL entry grants any
    1444                 :             :          * privileges still of interest before we perform the has_privs_of_role
    1445                 :             :          * test.
    1446                 :             :          */
    1447                 :        1382 :         remaining = mask & ~result;
    1448         [ +  + ]:        3383 :         for (i = 0; i < num; i++)
    1449                 :             :         {
    1450                 :        2030 :                 AclItem    *aidata = &aidat[i];
    1451                 :             : 
    1452   [ +  +  +  + ]:        2030 :                 if (aidata->ai_grantee == ACL_ID_PUBLIC ||
    1453                 :        1956 :                         aidata->ai_grantee == roleid)
    1454                 :         391 :                         continue;                       /* already checked it */
    1455                 :             : 
    1456   [ +  +  +  + ]:        1639 :                 if ((aidata->ai_privs & remaining) &&
    1457                 :        1490 :                         has_privs_of_role(roleid, aidata->ai_grantee))
    1458                 :             :                 {
    1459                 :          29 :                         result |= aidata->ai_privs & mask;
    1460   [ +  +  +  - ]:          29 :                         if ((how == ACLMASK_ALL) ? (result == mask) : (result != 0))
    1461                 :          29 :                                 return result;
    1462                 :           0 :                         remaining = mask & ~result;
    1463                 :           0 :                 }
    1464      [ +  +  + ]:        2030 :         }
    1465                 :             : 
    1466                 :        1353 :         return result;
    1467                 :       16025 : }
    1468                 :             : 
    1469                 :             : 
    1470                 :             : /*
    1471                 :             :  * aclmask_direct --- compute bitmask of all privileges held by roleid.
    1472                 :             :  *
    1473                 :             :  * This is exactly like aclmask() except that we consider only privileges
    1474                 :             :  * held *directly* by roleid, not those inherited via role membership.
    1475                 :             :  */
    1476                 :             : static AclMode
    1477                 :          45 : aclmask_direct(const Acl *acl, Oid roleid, Oid ownerId,
    1478                 :             :                            AclMode mask, AclMaskHow how)
    1479                 :             : {
    1480                 :          45 :         AclMode         result;
    1481                 :          45 :         AclItem    *aidat;
    1482                 :          45 :         int                     i,
    1483                 :             :                                 num;
    1484                 :             : 
    1485                 :             :         /*
    1486                 :             :          * Null ACL should not happen, since caller should have inserted
    1487                 :             :          * appropriate default
    1488                 :             :          */
    1489         [ +  - ]:          45 :         if (acl == NULL)
    1490   [ #  #  #  # ]:           0 :                 elog(ERROR, "null ACL");
    1491                 :             : 
    1492                 :          45 :         check_acl(acl);
    1493                 :             : 
    1494                 :             :         /* Quick exit for mask == 0 */
    1495         [ +  - ]:          45 :         if (mask == 0)
    1496                 :           0 :                 return 0;
    1497                 :             : 
    1498                 :          45 :         result = 0;
    1499                 :             : 
    1500                 :             :         /* Owner always implicitly has all grant options */
    1501   [ +  -  +  - ]:          45 :         if ((mask & ACLITEM_ALL_GOPTION_BITS) &&
    1502                 :          45 :                 roleid == ownerId)
    1503                 :             :         {
    1504                 :           0 :                 result = mask & ACLITEM_ALL_GOPTION_BITS;
    1505   [ #  #  #  # ]:           0 :                 if ((how == ACLMASK_ALL) ? (result == mask) : (result != 0))
    1506                 :           0 :                         return result;
    1507                 :           0 :         }
    1508                 :             : 
    1509                 :          45 :         num = ACL_NUM(acl);
    1510         [ -  + ]:          45 :         aidat = ACL_DAT(acl);
    1511                 :             : 
    1512                 :             :         /*
    1513                 :             :          * Check privileges granted directly to roleid (and not to public)
    1514                 :             :          */
    1515         [ +  + ]:         143 :         for (i = 0; i < num; i++)
    1516                 :             :         {
    1517                 :         125 :                 AclItem    *aidata = &aidat[i];
    1518                 :             : 
    1519         [ +  + ]:         125 :                 if (aidata->ai_grantee == roleid)
    1520                 :             :                 {
    1521                 :          48 :                         result |= aidata->ai_privs & mask;
    1522   [ +  +  +  + ]:          48 :                         if ((how == ACLMASK_ALL) ? (result == mask) : (result != 0))
    1523                 :          41 :                                 return result;
    1524                 :           7 :                 }
    1525         [ +  + ]:         111 :         }
    1526                 :             : 
    1527                 :          18 :         return result;
    1528                 :          31 : }
    1529                 :             : 
    1530                 :             : 
    1531                 :             : /*
    1532                 :             :  * aclmembers
    1533                 :             :  *              Find out all the roleids mentioned in an Acl.
    1534                 :             :  *              Note that we do not distinguish grantors from grantees.
    1535                 :             :  *
    1536                 :             :  * *roleids is set to point to a palloc'd array containing distinct OIDs
    1537                 :             :  * in sorted order.  The length of the array is the function result.
    1538                 :             :  */
    1539                 :             : int
    1540                 :        1734 : aclmembers(const Acl *acl, Oid **roleids)
    1541                 :             : {
    1542                 :        1734 :         Oid                *list;
    1543                 :        1734 :         const AclItem *acldat;
    1544                 :        1734 :         int                     i,
    1545                 :             :                                 j;
    1546                 :             : 
    1547   [ +  -  +  + ]:        1734 :         if (acl == NULL || ACL_NUM(acl) == 0)
    1548                 :             :         {
    1549                 :         724 :                 *roleids = NULL;
    1550                 :         724 :                 return 0;
    1551                 :             :         }
    1552                 :             : 
    1553                 :        1010 :         check_acl(acl);
    1554                 :             : 
    1555                 :             :         /* Allocate the worst-case space requirement */
    1556                 :        1010 :         list = palloc(ACL_NUM(acl) * 2 * sizeof(Oid));
    1557         [ -  + ]:        1010 :         acldat = ACL_DAT(acl);
    1558                 :             : 
    1559                 :             :         /*
    1560                 :             :          * Walk the ACL collecting mentioned RoleIds.
    1561                 :             :          */
    1562                 :        1010 :         j = 0;
    1563         [ +  + ]:        2772 :         for (i = 0; i < ACL_NUM(acl); i++)
    1564                 :             :         {
    1565                 :        1762 :                 const AclItem *ai = &acldat[i];
    1566                 :             : 
    1567         [ +  + ]:        1762 :                 if (ai->ai_grantee != ACL_ID_PUBLIC)
    1568                 :        1562 :                         list[j++] = ai->ai_grantee;
    1569                 :             :                 /* grantor is currently never PUBLIC, but let's check anyway */
    1570         [ -  + ]:        1762 :                 if (ai->ai_grantor != ACL_ID_PUBLIC)
    1571                 :        1762 :                         list[j++] = ai->ai_grantor;
    1572                 :        1762 :         }
    1573                 :             : 
    1574                 :             :         /* Sort the array */
    1575                 :        1010 :         qsort(list, j, sizeof(Oid), oid_cmp);
    1576                 :             : 
    1577                 :             :         /*
    1578                 :             :          * We could repalloc the array down to minimum size, but it's hardly worth
    1579                 :             :          * it since it's only transient memory.
    1580                 :             :          */
    1581                 :        1010 :         *roleids = list;
    1582                 :             : 
    1583                 :             :         /* Remove duplicates from the array */
    1584                 :        1010 :         return qunique(list, j, sizeof(Oid), oid_cmp);
    1585                 :        1734 : }
    1586                 :             : 
    1587                 :             : 
    1588                 :             : /*
    1589                 :             :  * aclinsert (exported function)
    1590                 :             :  */
    1591                 :             : Datum
    1592                 :           0 : aclinsert(PG_FUNCTION_ARGS)
    1593                 :             : {
    1594   [ #  #  #  # ]:           0 :         ereport(ERROR,
    1595                 :             :                         (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
    1596                 :             :                          errmsg("aclinsert is no longer supported")));
    1597                 :             : 
    1598                 :           0 :         PG_RETURN_NULL();                       /* keep compiler quiet */
    1599                 :             : }
    1600                 :             : 
    1601                 :             : Datum
    1602                 :           0 : aclremove(PG_FUNCTION_ARGS)
    1603                 :             : {
    1604   [ #  #  #  # ]:           0 :         ereport(ERROR,
    1605                 :             :                         (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
    1606                 :             :                          errmsg("aclremove is no longer supported")));
    1607                 :             : 
    1608                 :           0 :         PG_RETURN_NULL();                       /* keep compiler quiet */
    1609                 :             : }
    1610                 :             : 
    1611                 :             : Datum
    1612                 :           0 : aclcontains(PG_FUNCTION_ARGS)
    1613                 :             : {
    1614                 :           0 :         Acl                *acl = PG_GETARG_ACL_P(0);
    1615                 :           0 :         AclItem    *aip = PG_GETARG_ACLITEM_P(1);
    1616                 :           0 :         AclItem    *aidat;
    1617                 :           0 :         int                     i,
    1618                 :             :                                 num;
    1619                 :             : 
    1620                 :           0 :         check_acl(acl);
    1621                 :           0 :         num = ACL_NUM(acl);
    1622         [ #  # ]:           0 :         aidat = ACL_DAT(acl);
    1623         [ #  # ]:           0 :         for (i = 0; i < num; ++i)
    1624                 :             :         {
    1625         [ #  # ]:           0 :                 if (aip->ai_grantee == aidat[i].ai_grantee &&
    1626   [ #  #  #  # ]:           0 :                         aip->ai_grantor == aidat[i].ai_grantor &&
    1627                 :           0 :                         (ACLITEM_GET_RIGHTS(*aip) & ACLITEM_GET_RIGHTS(aidat[i])) == ACLITEM_GET_RIGHTS(*aip))
    1628                 :           0 :                         PG_RETURN_BOOL(true);
    1629                 :           0 :         }
    1630                 :           0 :         PG_RETURN_BOOL(false);
    1631                 :           0 : }
    1632                 :             : 
    1633                 :             : Datum
    1634                 :           4 : makeaclitem(PG_FUNCTION_ARGS)
    1635                 :             : {
    1636                 :           4 :         Oid                     grantee = PG_GETARG_OID(0);
    1637                 :           4 :         Oid                     grantor = PG_GETARG_OID(1);
    1638                 :           4 :         text       *privtext = PG_GETARG_TEXT_PP(2);
    1639                 :           4 :         bool            goption = PG_GETARG_BOOL(3);
    1640                 :           4 :         AclItem    *result;
    1641                 :           4 :         AclMode         priv;
    1642                 :             :         static const priv_map any_priv_map[] = {
    1643                 :             :                 {"SELECT", ACL_SELECT},
    1644                 :             :                 {"INSERT", ACL_INSERT},
    1645                 :             :                 {"UPDATE", ACL_UPDATE},
    1646                 :             :                 {"DELETE", ACL_DELETE},
    1647                 :             :                 {"TRUNCATE", ACL_TRUNCATE},
    1648                 :             :                 {"REFERENCES", ACL_REFERENCES},
    1649                 :             :                 {"TRIGGER", ACL_TRIGGER},
    1650                 :             :                 {"EXECUTE", ACL_EXECUTE},
    1651                 :             :                 {"USAGE", ACL_USAGE},
    1652                 :             :                 {"CREATE", ACL_CREATE},
    1653                 :             :                 {"TEMP", ACL_CREATE_TEMP},
    1654                 :             :                 {"TEMPORARY", ACL_CREATE_TEMP},
    1655                 :             :                 {"CONNECT", ACL_CONNECT},
    1656                 :             :                 {"SET", ACL_SET},
    1657                 :             :                 {"ALTER SYSTEM", ACL_ALTER_SYSTEM},
    1658                 :             :                 {"MAINTAIN", ACL_MAINTAIN},
    1659                 :             :                 {NULL, 0}
    1660                 :             :         };
    1661                 :             : 
    1662                 :           4 :         priv = convert_any_priv_string(privtext, any_priv_map);
    1663                 :             : 
    1664                 :           4 :         result = palloc_object(AclItem);
    1665                 :             : 
    1666                 :           4 :         result->ai_grantee = grantee;
    1667                 :           4 :         result->ai_grantor = grantor;
    1668                 :             : 
    1669         [ +  + ]:           4 :         ACLITEM_SET_PRIVS_GOPTIONS(*result, priv,
    1670                 :             :                                                            (goption ? priv : ACL_NO_RIGHTS));
    1671                 :             : 
    1672                 :           8 :         PG_RETURN_ACLITEM_P(result);
    1673                 :           4 : }
    1674                 :             : 
    1675                 :             : 
    1676                 :             : /*
    1677                 :             :  * convert_any_priv_string: recognize privilege strings for has_foo_privilege
    1678                 :             :  *
    1679                 :             :  * We accept a comma-separated list of case-insensitive privilege names,
    1680                 :             :  * producing a bitmask of the OR'd privilege bits.  We are liberal about
    1681                 :             :  * whitespace between items, not so much about whitespace within items.
    1682                 :             :  * The allowed privilege names are given as an array of priv_map structs,
    1683                 :             :  * terminated by one with a NULL name pointer.
    1684                 :             :  */
    1685                 :             : static AclMode
    1686                 :       14596 : convert_any_priv_string(text *priv_type_text,
    1687                 :             :                                                 const priv_map *privileges)
    1688                 :             : {
    1689                 :       14596 :         AclMode         result = 0;
    1690                 :       14596 :         char       *priv_type = text_to_cstring(priv_type_text);
    1691                 :       14596 :         char       *chunk;
    1692                 :       14596 :         char       *next_chunk;
    1693                 :             : 
    1694                 :             :         /* We rely on priv_type being a private, modifiable string */
    1695         [ +  + ]:       29193 :         for (chunk = priv_type; chunk; chunk = next_chunk)
    1696                 :             :         {
    1697                 :       14600 :                 int                     chunk_len;
    1698                 :       14600 :                 const priv_map *this_priv;
    1699                 :             : 
    1700                 :             :                 /* Split string at commas */
    1701                 :       14600 :                 next_chunk = strchr(chunk, ',');
    1702         [ +  + ]:       14600 :                 if (next_chunk)
    1703                 :           4 :                         *next_chunk++ = '\0';
    1704                 :             : 
    1705                 :             :                 /* Drop leading/trailing whitespace in this chunk */
    1706   [ -  +  +  + ]:       14605 :                 while (*chunk && isspace((unsigned char) *chunk))
    1707                 :           5 :                         chunk++;
    1708                 :       14600 :                 chunk_len = strlen(chunk);
    1709   [ -  +  +  + ]:       14603 :                 while (chunk_len > 0 && isspace((unsigned char) chunk[chunk_len - 1]))
    1710                 :           3 :                         chunk_len--;
    1711                 :       14600 :                 chunk[chunk_len] = '\0';
    1712                 :             : 
    1713                 :             :                 /* Match to the privileges list */
    1714         [ +  + ]:       14889 :                 for (this_priv = privileges; this_priv->name; this_priv++)
    1715                 :             :                 {
    1716         [ +  + ]:       14886 :                         if (pg_strcasecmp(this_priv->name, chunk) == 0)
    1717                 :             :                         {
    1718                 :       14597 :                                 result |= this_priv->value;
    1719                 :       14597 :                                 break;
    1720                 :             :                         }
    1721                 :         289 :                 }
    1722         [ +  + ]:       14600 :                 if (!this_priv->name)
    1723   [ +  -  +  - ]:           3 :                         ereport(ERROR,
    1724                 :             :                                         (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
    1725                 :             :                                          errmsg("unrecognized privilege type: \"%s\"", chunk)));
    1726                 :       14597 :         }
    1727                 :             : 
    1728                 :       14593 :         pfree(priv_type);
    1729                 :       29186 :         return result;
    1730                 :       14593 : }
    1731                 :             : 
    1732                 :             : 
    1733                 :             : static const char *
    1734                 :       12670 : convert_aclright_to_string(int aclright)
    1735                 :             : {
    1736   [ +  +  +  +  :       12670 :         switch (aclright)
          +  +  +  -  +  
          -  -  -  -  -  
                   +  - ]
    1737                 :             :         {
    1738                 :             :                 case ACL_INSERT:
    1739                 :        1506 :                         return "INSERT";
    1740                 :             :                 case ACL_SELECT:
    1741                 :        2106 :                         return "SELECT";
    1742                 :             :                 case ACL_UPDATE:
    1743                 :        1515 :                         return "UPDATE";
    1744                 :             :                 case ACL_DELETE:
    1745                 :        1503 :                         return "DELETE";
    1746                 :             :                 case ACL_TRUNCATE:
    1747                 :        1506 :                         return "TRUNCATE";
    1748                 :             :                 case ACL_REFERENCES:
    1749                 :        1506 :                         return "REFERENCES";
    1750                 :             :                 case ACL_TRIGGER:
    1751                 :        1506 :                         return "TRIGGER";
    1752                 :             :                 case ACL_EXECUTE:
    1753                 :           0 :                         return "EXECUTE";
    1754                 :             :                 case ACL_USAGE:
    1755                 :          16 :                         return "USAGE";
    1756                 :             :                 case ACL_CREATE:
    1757                 :           0 :                         return "CREATE";
    1758                 :             :                 case ACL_CREATE_TEMP:
    1759                 :           0 :                         return "TEMPORARY";
    1760                 :             :                 case ACL_CONNECT:
    1761                 :           0 :                         return "CONNECT";
    1762                 :             :                 case ACL_SET:
    1763                 :           0 :                         return "SET";
    1764                 :             :                 case ACL_ALTER_SYSTEM:
    1765                 :           0 :                         return "ALTER SYSTEM";
    1766                 :             :                 case ACL_MAINTAIN:
    1767                 :        1506 :                         return "MAINTAIN";
    1768                 :             :                 default:
    1769   [ #  #  #  # ]:           0 :                         elog(ERROR, "unrecognized aclright: %d", aclright);
    1770                 :           0 :                         return NULL;
    1771                 :             :         }
    1772                 :       12670 : }
    1773                 :             : 
    1774                 :             : 
    1775                 :             : /*----------
    1776                 :             :  * Convert an aclitem[] to a table.
    1777                 :             :  *
    1778                 :             :  * Example:
    1779                 :             :  *
    1780                 :             :  * aclexplode('{=r/joe,foo=a*w/joe}'::aclitem[])
    1781                 :             :  *
    1782                 :             :  * returns the table
    1783                 :             :  *
    1784                 :             :  * {{ OID(joe), 0::OID,   'SELECT', false },
    1785                 :             :  *      { OID(joe), OID(foo), 'INSERT', true },
    1786                 :             :  *      { OID(joe), OID(foo), 'UPDATE', false }}
    1787                 :             :  *----------
    1788                 :             :  */
    1789                 :             : Datum
    1790                 :       14184 : aclexplode(PG_FUNCTION_ARGS)
    1791                 :             : {
    1792                 :       14184 :         Acl                *acl = PG_GETARG_ACL_P(0);
    1793                 :       14184 :         FuncCallContext *funcctx;
    1794                 :       14184 :         int                *idx;
    1795                 :       14184 :         AclItem    *aidat;
    1796                 :             : 
    1797         [ +  + ]:       14184 :         if (SRF_IS_FIRSTCALL())
    1798                 :             :         {
    1799                 :        1514 :                 TupleDesc       tupdesc;
    1800                 :        1514 :                 MemoryContext oldcontext;
    1801                 :             : 
    1802                 :        1514 :                 check_acl(acl);
    1803                 :             : 
    1804                 :        1514 :                 funcctx = SRF_FIRSTCALL_INIT();
    1805                 :        1514 :                 oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
    1806                 :             : 
    1807                 :             :                 /*
    1808                 :             :                  * build tupdesc for result tuples (matches out parameters in pg_proc
    1809                 :             :                  * entry)
    1810                 :             :                  */
    1811                 :        1514 :                 tupdesc = CreateTemplateTupleDesc(4);
    1812                 :        1514 :                 TupleDescInitEntry(tupdesc, (AttrNumber) 1, "grantor",
    1813                 :             :                                                    OIDOID, -1, 0);
    1814                 :        1514 :                 TupleDescInitEntry(tupdesc, (AttrNumber) 2, "grantee",
    1815                 :             :                                                    OIDOID, -1, 0);
    1816                 :        1514 :                 TupleDescInitEntry(tupdesc, (AttrNumber) 3, "privilege_type",
    1817                 :             :                                                    TEXTOID, -1, 0);
    1818                 :        1514 :                 TupleDescInitEntry(tupdesc, (AttrNumber) 4, "is_grantable",
    1819                 :             :                                                    BOOLOID, -1, 0);
    1820                 :             : 
    1821                 :        1514 :                 funcctx->tuple_desc = BlessTupleDesc(tupdesc);
    1822                 :             : 
    1823                 :             :                 /* allocate memory for user context */
    1824                 :        1514 :                 idx = palloc_array(int, 2);
    1825                 :        1514 :                 idx[0] = 0;                             /* ACL array item index */
    1826                 :        1514 :                 idx[1] = -1;                    /* privilege type counter */
    1827                 :        1514 :                 funcctx->user_fctx = idx;
    1828                 :             : 
    1829                 :        1514 :                 MemoryContextSwitchTo(oldcontext);
    1830                 :        1514 :         }
    1831                 :             : 
    1832                 :       14184 :         funcctx = SRF_PERCALL_SETUP();
    1833                 :       14184 :         idx = (int *) funcctx->user_fctx;
    1834         [ -  + ]:       14184 :         aidat = ACL_DAT(acl);
    1835                 :             : 
    1836                 :             :         /* need test here in case acl has no items */
    1837         [ -  + ]:       33344 :         while (idx[0] < ACL_NUM(acl))
    1838                 :             :         {
    1839                 :       33344 :                 AclItem    *aidata;
    1840                 :       33344 :                 AclMode         priv_bit;
    1841                 :             : 
    1842                 :       33344 :                 idx[1]++;
    1843         [ +  + ]:       33344 :                 if (idx[1] == N_ACL_RIGHTS)
    1844                 :             :                 {
    1845                 :        2122 :                         idx[1] = 0;
    1846                 :        2122 :                         idx[0]++;
    1847         [ +  + ]:        2122 :                         if (idx[0] >= ACL_NUM(acl)) /* done */
    1848                 :        1514 :                                 break;
    1849                 :         608 :                 }
    1850                 :       31830 :                 aidata = &aidat[idx[0]];
    1851                 :       31830 :                 priv_bit = UINT64CONST(1) << idx[1];
    1852                 :             : 
    1853         [ +  + ]:       31830 :                 if (ACLITEM_GET_PRIVS(*aidata) & priv_bit)
    1854                 :             :                 {
    1855                 :       12670 :                         Datum           result;
    1856                 :       12670 :                         Datum           values[4];
    1857                 :       12670 :                         bool            nulls[4] = {0};
    1858                 :       12670 :                         HeapTuple       tuple;
    1859                 :             : 
    1860                 :       12670 :                         values[0] = ObjectIdGetDatum(aidata->ai_grantor);
    1861                 :       12670 :                         values[1] = ObjectIdGetDatum(aidata->ai_grantee);
    1862                 :       12670 :                         values[2] = CStringGetTextDatum(convert_aclright_to_string(priv_bit));
    1863                 :       12670 :                         values[3] = BoolGetDatum((ACLITEM_GET_GOPTIONS(*aidata) & priv_bit) != 0);
    1864                 :             : 
    1865                 :       12670 :                         tuple = heap_form_tuple(funcctx->tuple_desc, values, nulls);
    1866                 :       12670 :                         result = HeapTupleGetDatum(tuple);
    1867                 :             : 
    1868                 :       12670 :                         SRF_RETURN_NEXT(funcctx, result);
    1869         [ +  - ]:       12670 :                 }
    1870      [ +  +  + ]:       33344 :         }
    1871                 :             : 
    1872         [ +  - ]:        1514 :         SRF_RETURN_DONE(funcctx);
    1873         [ -  + ]:       14184 : }
    1874                 :             : 
    1875                 :             : 
    1876                 :             : /*
    1877                 :             :  * has_table_privilege variants
    1878                 :             :  *              These are all named "has_table_privilege" at the SQL level.
    1879                 :             :  *              They take various combinations of relation name, relation OID,
    1880                 :             :  *              user name, user OID, or implicit user = current_user.
    1881                 :             :  *
    1882                 :             :  *              The result is a boolean value: true if user has the indicated
    1883                 :             :  *              privilege, false if not.  The variants that take a relation OID
    1884                 :             :  *              return NULL if the OID doesn't exist (rather than failing, as
    1885                 :             :  *              they did before Postgres 8.4).
    1886                 :             :  */
    1887                 :             : 
    1888                 :             : /*
    1889                 :             :  * has_table_privilege_name_name
    1890                 :             :  *              Check user privileges on a table given
    1891                 :             :  *              name username, text tablename, and text priv name.
    1892                 :             :  */
    1893                 :             : Datum
    1894                 :          36 : has_table_privilege_name_name(PG_FUNCTION_ARGS)
    1895                 :             : {
    1896                 :          36 :         Name            rolename = PG_GETARG_NAME(0);
    1897                 :          36 :         text       *tablename = PG_GETARG_TEXT_PP(1);
    1898                 :          36 :         text       *priv_type_text = PG_GETARG_TEXT_PP(2);
    1899                 :          36 :         Oid                     roleid;
    1900                 :          36 :         Oid                     tableoid;
    1901                 :          36 :         AclMode         mode;
    1902                 :          36 :         AclResult       aclresult;
    1903                 :             : 
    1904                 :          36 :         roleid = get_role_oid_or_public(NameStr(*rolename));
    1905                 :          36 :         tableoid = convert_table_name(tablename);
    1906                 :          36 :         mode = convert_table_priv_string(priv_type_text);
    1907                 :             : 
    1908                 :          36 :         aclresult = pg_class_aclcheck(tableoid, roleid, mode);
    1909                 :             : 
    1910                 :          72 :         PG_RETURN_BOOL(aclresult == ACLCHECK_OK);
    1911                 :          36 : }
    1912                 :             : 
    1913                 :             : /*
    1914                 :             :  * has_table_privilege_name
    1915                 :             :  *              Check user privileges on a table given
    1916                 :             :  *              text tablename and text priv name.
    1917                 :             :  *              current_user is assumed
    1918                 :             :  */
    1919                 :             : Datum
    1920                 :          11 : has_table_privilege_name(PG_FUNCTION_ARGS)
    1921                 :             : {
    1922                 :          11 :         text       *tablename = PG_GETARG_TEXT_PP(0);
    1923                 :          11 :         text       *priv_type_text = PG_GETARG_TEXT_PP(1);
    1924                 :          11 :         Oid                     roleid;
    1925                 :          11 :         Oid                     tableoid;
    1926                 :          11 :         AclMode         mode;
    1927                 :          11 :         AclResult       aclresult;
    1928                 :             : 
    1929                 :          11 :         roleid = GetUserId();
    1930                 :          11 :         tableoid = convert_table_name(tablename);
    1931                 :          11 :         mode = convert_table_priv_string(priv_type_text);
    1932                 :             : 
    1933                 :          11 :         aclresult = pg_class_aclcheck(tableoid, roleid, mode);
    1934                 :             : 
    1935                 :          22 :         PG_RETURN_BOOL(aclresult == ACLCHECK_OK);
    1936                 :          11 : }
    1937                 :             : 
    1938                 :             : /*
    1939                 :             :  * has_table_privilege_name_id
    1940                 :             :  *              Check user privileges on a table given
    1941                 :             :  *              name usename, table oid, and text priv name.
    1942                 :             :  */
    1943                 :             : Datum
    1944                 :           3 : has_table_privilege_name_id(PG_FUNCTION_ARGS)
    1945                 :             : {
    1946                 :           3 :         Name            username = PG_GETARG_NAME(0);
    1947                 :           3 :         Oid                     tableoid = PG_GETARG_OID(1);
    1948                 :           3 :         text       *priv_type_text = PG_GETARG_TEXT_PP(2);
    1949                 :           3 :         Oid                     roleid;
    1950                 :           3 :         AclMode         mode;
    1951                 :           3 :         AclResult       aclresult;
    1952                 :           3 :         bool            is_missing = false;
    1953                 :             : 
    1954                 :           3 :         roleid = get_role_oid_or_public(NameStr(*username));
    1955                 :           3 :         mode = convert_table_priv_string(priv_type_text);
    1956                 :             : 
    1957                 :           3 :         aclresult = pg_class_aclcheck_ext(tableoid, roleid, mode, &is_missing);
    1958                 :             : 
    1959         [ +  - ]:           3 :         if (is_missing)
    1960                 :           0 :                 PG_RETURN_NULL();
    1961                 :             : 
    1962                 :           3 :         PG_RETURN_BOOL(aclresult == ACLCHECK_OK);
    1963                 :           3 : }
    1964                 :             : 
    1965                 :             : /*
    1966                 :             :  * has_table_privilege_id
    1967                 :             :  *              Check user privileges on a table given
    1968                 :             :  *              table oid, and text priv name.
    1969                 :             :  *              current_user is assumed
    1970                 :             :  */
    1971                 :             : Datum
    1972                 :          19 : has_table_privilege_id(PG_FUNCTION_ARGS)
    1973                 :             : {
    1974                 :          19 :         Oid                     tableoid = PG_GETARG_OID(0);
    1975                 :          19 :         text       *priv_type_text = PG_GETARG_TEXT_PP(1);
    1976                 :          19 :         Oid                     roleid;
    1977                 :          19 :         AclMode         mode;
    1978                 :          19 :         AclResult       aclresult;
    1979                 :          19 :         bool            is_missing = false;
    1980                 :             : 
    1981                 :          19 :         roleid = GetUserId();
    1982                 :          19 :         mode = convert_table_priv_string(priv_type_text);
    1983                 :             : 
    1984                 :          19 :         aclresult = pg_class_aclcheck_ext(tableoid, roleid, mode, &is_missing);
    1985                 :             : 
    1986         [ +  + ]:          19 :         if (is_missing)
    1987                 :           1 :                 PG_RETURN_NULL();
    1988                 :             : 
    1989                 :          18 :         PG_RETURN_BOOL(aclresult == ACLCHECK_OK);
    1990                 :          19 : }
    1991                 :             : 
    1992                 :             : /*
    1993                 :             :  * has_table_privilege_id_name
    1994                 :             :  *              Check user privileges on a table given
    1995                 :             :  *              roleid, text tablename, and text priv name.
    1996                 :             :  */
    1997                 :             : Datum
    1998                 :           7 : has_table_privilege_id_name(PG_FUNCTION_ARGS)
    1999                 :             : {
    2000                 :           7 :         Oid                     roleid = PG_GETARG_OID(0);
    2001                 :           7 :         text       *tablename = PG_GETARG_TEXT_PP(1);
    2002                 :           7 :         text       *priv_type_text = PG_GETARG_TEXT_PP(2);
    2003                 :           7 :         Oid                     tableoid;
    2004                 :           7 :         AclMode         mode;
    2005                 :           7 :         AclResult       aclresult;
    2006                 :             : 
    2007                 :           7 :         tableoid = convert_table_name(tablename);
    2008                 :           7 :         mode = convert_table_priv_string(priv_type_text);
    2009                 :             : 
    2010                 :           7 :         aclresult = pg_class_aclcheck(tableoid, roleid, mode);
    2011                 :             : 
    2012                 :          14 :         PG_RETURN_BOOL(aclresult == ACLCHECK_OK);
    2013                 :           7 : }
    2014                 :             : 
    2015                 :             : /*
    2016                 :             :  * has_table_privilege_id_id
    2017                 :             :  *              Check user privileges on a table given
    2018                 :             :  *              roleid, table oid, and text priv name.
    2019                 :             :  */
    2020                 :             : Datum
    2021                 :           6 : has_table_privilege_id_id(PG_FUNCTION_ARGS)
    2022                 :             : {
    2023                 :           6 :         Oid                     roleid = PG_GETARG_OID(0);
    2024                 :           6 :         Oid                     tableoid = PG_GETARG_OID(1);
    2025                 :           6 :         text       *priv_type_text = PG_GETARG_TEXT_PP(2);
    2026                 :           6 :         AclMode         mode;
    2027                 :           6 :         AclResult       aclresult;
    2028                 :           6 :         bool            is_missing = false;
    2029                 :             : 
    2030                 :           6 :         mode = convert_table_priv_string(priv_type_text);
    2031                 :             : 
    2032                 :           6 :         aclresult = pg_class_aclcheck_ext(tableoid, roleid, mode, &is_missing);
    2033                 :             : 
    2034         [ +  - ]:           6 :         if (is_missing)
    2035                 :           0 :                 PG_RETURN_NULL();
    2036                 :             : 
    2037                 :           6 :         PG_RETURN_BOOL(aclresult == ACLCHECK_OK);
    2038                 :           6 : }
    2039                 :             : 
    2040                 :             : /*
    2041                 :             :  *              Support routines for has_table_privilege family.
    2042                 :             :  */
    2043                 :             : 
    2044                 :             : /*
    2045                 :             :  * Given a table name expressed as a string, look it up and return Oid
    2046                 :             :  */
    2047                 :             : static Oid
    2048                 :          64 : convert_table_name(text *tablename)
    2049                 :             : {
    2050                 :          64 :         RangeVar   *relrv;
    2051                 :             : 
    2052                 :          64 :         relrv = makeRangeVarFromNameList(textToQualifiedNameList(tablename));
    2053                 :             : 
    2054                 :             :         /* We might not even have permissions on this relation; don't lock it. */
    2055                 :         128 :         return RangeVarGetRelid(relrv, NoLock, false);
    2056                 :          64 : }
    2057                 :             : 
    2058                 :             : /*
    2059                 :             :  * convert_table_priv_string
    2060                 :             :  *              Convert text string to AclMode value.
    2061                 :             :  */
    2062                 :             : static AclMode
    2063                 :          80 : convert_table_priv_string(text *priv_type_text)
    2064                 :             : {
    2065                 :             :         static const priv_map table_priv_map[] = {
    2066                 :             :                 {"SELECT", ACL_SELECT},
    2067                 :             :                 {"SELECT WITH GRANT OPTION", ACL_GRANT_OPTION_FOR(ACL_SELECT)},
    2068                 :             :                 {"INSERT", ACL_INSERT},
    2069                 :             :                 {"INSERT WITH GRANT OPTION", ACL_GRANT_OPTION_FOR(ACL_INSERT)},
    2070                 :             :                 {"UPDATE", ACL_UPDATE},
    2071                 :             :                 {"UPDATE WITH GRANT OPTION", ACL_GRANT_OPTION_FOR(ACL_UPDATE)},
    2072                 :             :                 {"DELETE", ACL_DELETE},
    2073                 :             :                 {"DELETE WITH GRANT OPTION", ACL_GRANT_OPTION_FOR(ACL_DELETE)},
    2074                 :             :                 {"TRUNCATE", ACL_TRUNCATE},
    2075                 :             :                 {"TRUNCATE WITH GRANT OPTION", ACL_GRANT_OPTION_FOR(ACL_TRUNCATE)},
    2076                 :             :                 {"REFERENCES", ACL_REFERENCES},
    2077                 :             :                 {"REFERENCES WITH GRANT OPTION", ACL_GRANT_OPTION_FOR(ACL_REFERENCES)},
    2078                 :             :                 {"TRIGGER", ACL_TRIGGER},
    2079                 :             :                 {"TRIGGER WITH GRANT OPTION", ACL_GRANT_OPTION_FOR(ACL_TRIGGER)},
    2080                 :             :                 {"MAINTAIN", ACL_MAINTAIN},
    2081                 :             :                 {"MAINTAIN WITH GRANT OPTION", ACL_GRANT_OPTION_FOR(ACL_MAINTAIN)},
    2082                 :             :                 {NULL, 0}
    2083                 :             :         };
    2084                 :             : 
    2085                 :          80 :         return convert_any_priv_string(priv_type_text, table_priv_map);
    2086                 :             : }
    2087                 :             : 
    2088                 :             : /*
    2089                 :             :  * has_sequence_privilege variants
    2090                 :             :  *              These are all named "has_sequence_privilege" at the SQL level.
    2091                 :             :  *              They take various combinations of relation name, relation OID,
    2092                 :             :  *              user name, user OID, or implicit user = current_user.
    2093                 :             :  *
    2094                 :             :  *              The result is a boolean value: true if user has the indicated
    2095                 :             :  *              privilege, false if not.  The variants that take a relation OID
    2096                 :             :  *              return NULL if the OID doesn't exist.
    2097                 :             :  */
    2098                 :             : 
    2099                 :             : /*
    2100                 :             :  * has_sequence_privilege_name_name
    2101                 :             :  *              Check user privileges on a sequence given
    2102                 :             :  *              name username, text sequencename, and text priv name.
    2103                 :             :  */
    2104                 :             : Datum
    2105                 :           3 : has_sequence_privilege_name_name(PG_FUNCTION_ARGS)
    2106                 :             : {
    2107                 :           3 :         Name            rolename = PG_GETARG_NAME(0);
    2108                 :           3 :         text       *sequencename = PG_GETARG_TEXT_PP(1);
    2109                 :           3 :         text       *priv_type_text = PG_GETARG_TEXT_PP(2);
    2110                 :           3 :         Oid                     roleid;
    2111                 :           3 :         Oid                     sequenceoid;
    2112                 :           3 :         AclMode         mode;
    2113                 :           3 :         AclResult       aclresult;
    2114                 :             : 
    2115                 :           3 :         roleid = get_role_oid_or_public(NameStr(*rolename));
    2116                 :           3 :         mode = convert_sequence_priv_string(priv_type_text);
    2117                 :           3 :         sequenceoid = convert_table_name(sequencename);
    2118         [ +  + ]:           3 :         if (get_rel_relkind(sequenceoid) != RELKIND_SEQUENCE)
    2119   [ +  -  +  - ]:           1 :                 ereport(ERROR,
    2120                 :             :                                 (errcode(ERRCODE_WRONG_OBJECT_TYPE),
    2121                 :             :                                  errmsg("\"%s\" is not a sequence",
    2122                 :             :                                                 text_to_cstring(sequencename))));
    2123                 :             : 
    2124                 :           2 :         aclresult = pg_class_aclcheck(sequenceoid, roleid, mode);
    2125                 :             : 
    2126                 :           4 :         PG_RETURN_BOOL(aclresult == ACLCHECK_OK);
    2127                 :           2 : }
    2128                 :             : 
    2129                 :             : /*
    2130                 :             :  * has_sequence_privilege_name
    2131                 :             :  *              Check user privileges on a sequence given
    2132                 :             :  *              text sequencename and text priv name.
    2133                 :             :  *              current_user is assumed
    2134                 :             :  */
    2135                 :             : Datum
    2136                 :           1 : has_sequence_privilege_name(PG_FUNCTION_ARGS)
    2137                 :             : {
    2138                 :           1 :         text       *sequencename = PG_GETARG_TEXT_PP(0);
    2139                 :           1 :         text       *priv_type_text = PG_GETARG_TEXT_PP(1);
    2140                 :           1 :         Oid                     roleid;
    2141                 :           1 :         Oid                     sequenceoid;
    2142                 :           1 :         AclMode         mode;
    2143                 :           1 :         AclResult       aclresult;
    2144                 :             : 
    2145                 :           1 :         roleid = GetUserId();
    2146                 :           1 :         mode = convert_sequence_priv_string(priv_type_text);
    2147                 :           1 :         sequenceoid = convert_table_name(sequencename);
    2148         [ +  - ]:           1 :         if (get_rel_relkind(sequenceoid) != RELKIND_SEQUENCE)
    2149   [ #  #  #  # ]:           0 :                 ereport(ERROR,
    2150                 :             :                                 (errcode(ERRCODE_WRONG_OBJECT_TYPE),
    2151                 :             :                                  errmsg("\"%s\" is not a sequence",
    2152                 :             :                                                 text_to_cstring(sequencename))));
    2153                 :             : 
    2154                 :           1 :         aclresult = pg_class_aclcheck(sequenceoid, roleid, mode);
    2155                 :             : 
    2156                 :           2 :         PG_RETURN_BOOL(aclresult == ACLCHECK_OK);
    2157                 :           1 : }
    2158                 :             : 
    2159                 :             : /*
    2160                 :             :  * has_sequence_privilege_name_id
    2161                 :             :  *              Check user privileges on a sequence given
    2162                 :             :  *              name usename, sequence oid, and text priv name.
    2163                 :             :  */
    2164                 :             : Datum
    2165                 :           0 : has_sequence_privilege_name_id(PG_FUNCTION_ARGS)
    2166                 :             : {
    2167                 :           0 :         Name            username = PG_GETARG_NAME(0);
    2168                 :           0 :         Oid                     sequenceoid = PG_GETARG_OID(1);
    2169                 :           0 :         text       *priv_type_text = PG_GETARG_TEXT_PP(2);
    2170                 :           0 :         Oid                     roleid;
    2171                 :           0 :         AclMode         mode;
    2172                 :           0 :         AclResult       aclresult;
    2173                 :           0 :         char            relkind;
    2174                 :           0 :         bool            is_missing = false;
    2175                 :             : 
    2176                 :           0 :         roleid = get_role_oid_or_public(NameStr(*username));
    2177                 :           0 :         mode = convert_sequence_priv_string(priv_type_text);
    2178                 :           0 :         relkind = get_rel_relkind(sequenceoid);
    2179         [ #  # ]:           0 :         if (relkind == '\0')
    2180                 :           0 :                 PG_RETURN_NULL();
    2181         [ #  # ]:           0 :         else if (relkind != RELKIND_SEQUENCE)
    2182   [ #  #  #  # ]:           0 :                 ereport(ERROR,
    2183                 :             :                                 (errcode(ERRCODE_WRONG_OBJECT_TYPE),
    2184                 :             :                                  errmsg("\"%s\" is not a sequence",
    2185                 :             :                                                 get_rel_name(sequenceoid))));
    2186                 :             : 
    2187                 :           0 :         aclresult = pg_class_aclcheck_ext(sequenceoid, roleid, mode, &is_missing);
    2188                 :             : 
    2189         [ #  # ]:           0 :         if (is_missing)
    2190                 :           0 :                 PG_RETURN_NULL();
    2191                 :             : 
    2192                 :           0 :         PG_RETURN_BOOL(aclresult == ACLCHECK_OK);
    2193                 :           0 : }
    2194                 :             : 
    2195                 :             : /*
    2196                 :             :  * has_sequence_privilege_id
    2197                 :             :  *              Check user privileges on a sequence given
    2198                 :             :  *              sequence oid, and text priv name.
    2199                 :             :  *              current_user is assumed
    2200                 :             :  */
    2201                 :             : Datum
    2202                 :           0 : has_sequence_privilege_id(PG_FUNCTION_ARGS)
    2203                 :             : {
    2204                 :           0 :         Oid                     sequenceoid = PG_GETARG_OID(0);
    2205                 :           0 :         text       *priv_type_text = PG_GETARG_TEXT_PP(1);
    2206                 :           0 :         Oid                     roleid;
    2207                 :           0 :         AclMode         mode;
    2208                 :           0 :         AclResult       aclresult;
    2209                 :           0 :         char            relkind;
    2210                 :           0 :         bool            is_missing = false;
    2211                 :             : 
    2212                 :           0 :         roleid = GetUserId();
    2213                 :           0 :         mode = convert_sequence_priv_string(priv_type_text);
    2214                 :           0 :         relkind = get_rel_relkind(sequenceoid);
    2215         [ #  # ]:           0 :         if (relkind == '\0')
    2216                 :           0 :                 PG_RETURN_NULL();
    2217         [ #  # ]:           0 :         else if (relkind != RELKIND_SEQUENCE)
    2218   [ #  #  #  # ]:           0 :                 ereport(ERROR,
    2219                 :             :                                 (errcode(ERRCODE_WRONG_OBJECT_TYPE),
    2220                 :             :                                  errmsg("\"%s\" is not a sequence",
    2221                 :             :                                                 get_rel_name(sequenceoid))));
    2222                 :             : 
    2223                 :           0 :         aclresult = pg_class_aclcheck_ext(sequenceoid, roleid, mode, &is_missing);
    2224                 :             : 
    2225         [ #  # ]:           0 :         if (is_missing)
    2226                 :           0 :                 PG_RETURN_NULL();
    2227                 :             : 
    2228                 :           0 :         PG_RETURN_BOOL(aclresult == ACLCHECK_OK);
    2229                 :           0 : }
    2230                 :             : 
    2231                 :             : /*
    2232                 :             :  * has_sequence_privilege_id_name
    2233                 :             :  *              Check user privileges on a sequence given
    2234                 :             :  *              roleid, text sequencename, and text priv name.
    2235                 :             :  */
    2236                 :             : Datum
    2237                 :           0 : has_sequence_privilege_id_name(PG_FUNCTION_ARGS)
    2238                 :             : {
    2239                 :           0 :         Oid                     roleid = PG_GETARG_OID(0);
    2240                 :           0 :         text       *sequencename = PG_GETARG_TEXT_PP(1);
    2241                 :           0 :         text       *priv_type_text = PG_GETARG_TEXT_PP(2);
    2242                 :           0 :         Oid                     sequenceoid;
    2243                 :           0 :         AclMode         mode;
    2244                 :           0 :         AclResult       aclresult;
    2245                 :             : 
    2246                 :           0 :         mode = convert_sequence_priv_string(priv_type_text);
    2247                 :           0 :         sequenceoid = convert_table_name(sequencename);
    2248         [ #  # ]:           0 :         if (get_rel_relkind(sequenceoid) != RELKIND_SEQUENCE)
    2249   [ #  #  #  # ]:           0 :                 ereport(ERROR,
    2250                 :             :                                 (errcode(ERRCODE_WRONG_OBJECT_TYPE),
    2251                 :             :                                  errmsg("\"%s\" is not a sequence",
    2252                 :             :                                                 text_to_cstring(sequencename))));
    2253                 :             : 
    2254                 :           0 :         aclresult = pg_class_aclcheck(sequenceoid, roleid, mode);
    2255                 :             : 
    2256                 :           0 :         PG_RETURN_BOOL(aclresult == ACLCHECK_OK);
    2257                 :           0 : }
    2258                 :             : 
    2259                 :             : /*
    2260                 :             :  * has_sequence_privilege_id_id
    2261                 :             :  *              Check user privileges on a sequence given
    2262                 :             :  *              roleid, sequence oid, and text priv name.
    2263                 :             :  */
    2264                 :             : Datum
    2265                 :           0 : has_sequence_privilege_id_id(PG_FUNCTION_ARGS)
    2266                 :             : {
    2267                 :           0 :         Oid                     roleid = PG_GETARG_OID(0);
    2268                 :           0 :         Oid                     sequenceoid = PG_GETARG_OID(1);
    2269                 :           0 :         text       *priv_type_text = PG_GETARG_TEXT_PP(2);
    2270                 :           0 :         AclMode         mode;
    2271                 :           0 :         AclResult       aclresult;
    2272                 :           0 :         char            relkind;
    2273                 :           0 :         bool            is_missing = false;
    2274                 :             : 
    2275                 :           0 :         mode = convert_sequence_priv_string(priv_type_text);
    2276                 :           0 :         relkind = get_rel_relkind(sequenceoid);
    2277         [ #  # ]:           0 :         if (relkind == '\0')
    2278                 :           0 :                 PG_RETURN_NULL();
    2279         [ #  # ]:           0 :         else if (relkind != RELKIND_SEQUENCE)
    2280   [ #  #  #  # ]:           0 :                 ereport(ERROR,
    2281                 :             :                                 (errcode(ERRCODE_WRONG_OBJECT_TYPE),
    2282                 :             :                                  errmsg("\"%s\" is not a sequence",
    2283                 :             :                                                 get_rel_name(sequenceoid))));
    2284                 :             : 
    2285                 :           0 :         aclresult = pg_class_aclcheck_ext(sequenceoid, roleid, mode, &is_missing);
    2286                 :             : 
    2287         [ #  # ]:           0 :         if (is_missing)
    2288                 :           0 :                 PG_RETURN_NULL();
    2289                 :             : 
    2290                 :           0 :         PG_RETURN_BOOL(aclresult == ACLCHECK_OK);
    2291                 :           0 : }
    2292                 :             : 
    2293                 :             : /*
    2294                 :             :  * convert_sequence_priv_string
    2295                 :             :  *              Convert text string to AclMode value.
    2296                 :             :  */
    2297                 :             : static AclMode
    2298                 :           4 : convert_sequence_priv_string(text *priv_type_text)
    2299                 :             : {
    2300                 :             :         static const priv_map sequence_priv_map[] = {
    2301                 :             :                 {"USAGE", ACL_USAGE},
    2302                 :             :                 {"USAGE WITH GRANT OPTION", ACL_GRANT_OPTION_FOR(ACL_USAGE)},
    2303                 :             :                 {"SELECT", ACL_SELECT},
    2304                 :             :                 {"SELECT WITH GRANT OPTION", ACL_GRANT_OPTION_FOR(ACL_SELECT)},
    2305                 :             :                 {"UPDATE", ACL_UPDATE},
    2306                 :             :                 {"UPDATE WITH GRANT OPTION", ACL_GRANT_OPTION_FOR(ACL_UPDATE)},
    2307                 :             :                 {NULL, 0}
    2308                 :             :         };
    2309                 :             : 
    2310                 :           4 :         return convert_any_priv_string(priv_type_text, sequence_priv_map);
    2311                 :             : }
    2312                 :             : 
    2313                 :             : 
    2314                 :             : /*
    2315                 :             :  * has_any_column_privilege variants
    2316                 :             :  *              These are all named "has_any_column_privilege" at the SQL level.
    2317                 :             :  *              They take various combinations of relation name, relation OID,
    2318                 :             :  *              user name, user OID, or implicit user = current_user.
    2319                 :             :  *
    2320                 :             :  *              The result is a boolean value: true if user has the indicated
    2321                 :             :  *              privilege for any column of the table, false if not.  The variants
    2322                 :             :  *              that take a relation OID return NULL if the OID doesn't exist.
    2323                 :             :  */
    2324                 :             : 
    2325                 :             : /*
    2326                 :             :  * has_any_column_privilege_name_name
    2327                 :             :  *              Check user privileges on any column of a table given
    2328                 :             :  *              name username, text tablename, and text priv name.
    2329                 :             :  */
    2330                 :             : Datum
    2331                 :           0 : has_any_column_privilege_name_name(PG_FUNCTION_ARGS)
    2332                 :             : {
    2333                 :           0 :         Name            rolename = PG_GETARG_NAME(0);
    2334                 :           0 :         text       *tablename = PG_GETARG_TEXT_PP(1);
    2335                 :           0 :         text       *priv_type_text = PG_GETARG_TEXT_PP(2);
    2336                 :           0 :         Oid                     roleid;
    2337                 :           0 :         Oid                     tableoid;
    2338                 :           0 :         AclMode         mode;
    2339                 :           0 :         AclResult       aclresult;
    2340                 :             : 
    2341                 :           0 :         roleid = get_role_oid_or_public(NameStr(*rolename));
    2342                 :           0 :         tableoid = convert_table_name(tablename);
    2343                 :           0 :         mode = convert_column_priv_string(priv_type_text);
    2344                 :             : 
    2345                 :             :         /* First check at table level, then examine each column if needed */
    2346                 :           0 :         aclresult = pg_class_aclcheck(tableoid, roleid, mode);
    2347         [ #  # ]:           0 :         if (aclresult != ACLCHECK_OK)
    2348                 :           0 :                 aclresult = pg_attribute_aclcheck_all(tableoid, roleid, mode,
    2349                 :             :                                                                                           ACLMASK_ANY);
    2350                 :             : 
    2351                 :           0 :         PG_RETURN_BOOL(aclresult == ACLCHECK_OK);
    2352                 :           0 : }
    2353                 :             : 
    2354                 :             : /*
    2355                 :             :  * has_any_column_privilege_name
    2356                 :             :  *              Check user privileges on any column of a table given
    2357                 :             :  *              text tablename and text priv name.
    2358                 :             :  *              current_user is assumed
    2359                 :             :  */
    2360                 :             : Datum
    2361                 :           0 : has_any_column_privilege_name(PG_FUNCTION_ARGS)
    2362                 :             : {
    2363                 :           0 :         text       *tablename = PG_GETARG_TEXT_PP(0);
    2364                 :           0 :         text       *priv_type_text = PG_GETARG_TEXT_PP(1);
    2365                 :           0 :         Oid                     roleid;
    2366                 :           0 :         Oid                     tableoid;
    2367                 :           0 :         AclMode         mode;
    2368                 :           0 :         AclResult       aclresult;
    2369                 :             : 
    2370                 :           0 :         roleid = GetUserId();
    2371                 :           0 :         tableoid = convert_table_name(tablename);
    2372                 :           0 :         mode = convert_column_priv_string(priv_type_text);
    2373                 :             : 
    2374                 :             :         /* First check at table level, then examine each column if needed */
    2375                 :           0 :         aclresult = pg_class_aclcheck(tableoid, roleid, mode);
    2376         [ #  # ]:           0 :         if (aclresult != ACLCHECK_OK)
    2377                 :           0 :                 aclresult = pg_attribute_aclcheck_all(tableoid, roleid, mode,
    2378                 :             :                                                                                           ACLMASK_ANY);
    2379                 :             : 
    2380                 :           0 :         PG_RETURN_BOOL(aclresult == ACLCHECK_OK);
    2381                 :           0 : }
    2382                 :             : 
    2383                 :             : /*
    2384                 :             :  * has_any_column_privilege_name_id
    2385                 :             :  *              Check user privileges on any column of a table given
    2386                 :             :  *              name usename, table oid, and text priv name.
    2387                 :             :  */
    2388                 :             : Datum
    2389                 :           0 : has_any_column_privilege_name_id(PG_FUNCTION_ARGS)
    2390                 :             : {
    2391                 :           0 :         Name            username = PG_GETARG_NAME(0);
    2392                 :           0 :         Oid                     tableoid = PG_GETARG_OID(1);
    2393                 :           0 :         text       *priv_type_text = PG_GETARG_TEXT_PP(2);
    2394                 :           0 :         Oid                     roleid;
    2395                 :           0 :         AclMode         mode;
    2396                 :           0 :         AclResult       aclresult;
    2397                 :           0 :         bool            is_missing = false;
    2398                 :             : 
    2399                 :           0 :         roleid = get_role_oid_or_public(NameStr(*username));
    2400                 :           0 :         mode = convert_column_priv_string(priv_type_text);
    2401                 :             : 
    2402                 :             :         /* First check at table level, then examine each column if needed */
    2403                 :           0 :         aclresult = pg_class_aclcheck_ext(tableoid, roleid, mode, &is_missing);
    2404         [ #  # ]:           0 :         if (aclresult != ACLCHECK_OK)
    2405                 :             :         {
    2406         [ #  # ]:           0 :                 if (is_missing)
    2407                 :           0 :                         PG_RETURN_NULL();
    2408                 :           0 :                 aclresult = pg_attribute_aclcheck_all_ext(tableoid, roleid, mode,
    2409                 :             :                                                                                                   ACLMASK_ANY, &is_missing);
    2410         [ #  # ]:           0 :                 if (is_missing)
    2411                 :           0 :                         PG_RETURN_NULL();
    2412                 :           0 :         }
    2413                 :             : 
    2414                 :           0 :         PG_RETURN_BOOL(aclresult == ACLCHECK_OK);
    2415                 :           0 : }
    2416                 :             : 
    2417                 :             : /*
    2418                 :             :  * has_any_column_privilege_id
    2419                 :             :  *              Check user privileges on any column of a table given
    2420                 :             :  *              table oid, and text priv name.
    2421                 :             :  *              current_user is assumed
    2422                 :             :  */
    2423                 :             : Datum
    2424                 :           0 : has_any_column_privilege_id(PG_FUNCTION_ARGS)
    2425                 :             : {
    2426                 :           0 :         Oid                     tableoid = PG_GETARG_OID(0);
    2427                 :           0 :         text       *priv_type_text = PG_GETARG_TEXT_PP(1);
    2428                 :           0 :         Oid                     roleid;
    2429                 :           0 :         AclMode         mode;
    2430                 :           0 :         AclResult       aclresult;
    2431                 :           0 :         bool            is_missing = false;
    2432                 :             : 
    2433                 :           0 :         roleid = GetUserId();
    2434                 :           0 :         mode = convert_column_priv_string(priv_type_text);
    2435                 :             : 
    2436                 :             :         /* First check at table level, then examine each column if needed */
    2437                 :           0 :         aclresult = pg_class_aclcheck_ext(tableoid, roleid, mode, &is_missing);
    2438         [ #  # ]:           0 :         if (aclresult != ACLCHECK_OK)
    2439                 :             :         {
    2440         [ #  # ]:           0 :                 if (is_missing)
    2441                 :           0 :                         PG_RETURN_NULL();
    2442                 :           0 :                 aclresult = pg_attribute_aclcheck_all_ext(tableoid, roleid, mode,
    2443                 :             :                                                                                                   ACLMASK_ANY, &is_missing);
    2444         [ #  # ]:           0 :                 if (is_missing)
    2445                 :           0 :                         PG_RETURN_NULL();
    2446                 :           0 :         }
    2447                 :             : 
    2448                 :           0 :         PG_RETURN_BOOL(aclresult == ACLCHECK_OK);
    2449                 :           0 : }
    2450                 :             : 
    2451                 :             : /*
    2452                 :             :  * has_any_column_privilege_id_name
    2453                 :             :  *              Check user privileges on any column of a table given
    2454                 :             :  *              roleid, text tablename, and text priv name.
    2455                 :             :  */
    2456                 :             : Datum
    2457                 :           0 : has_any_column_privilege_id_name(PG_FUNCTION_ARGS)
    2458                 :             : {
    2459                 :           0 :         Oid                     roleid = PG_GETARG_OID(0);
    2460                 :           0 :         text       *tablename = PG_GETARG_TEXT_PP(1);
    2461                 :           0 :         text       *priv_type_text = PG_GETARG_TEXT_PP(2);
    2462                 :           0 :         Oid                     tableoid;
    2463                 :           0 :         AclMode         mode;
    2464                 :           0 :         AclResult       aclresult;
    2465                 :             : 
    2466                 :           0 :         tableoid = convert_table_name(tablename);
    2467                 :           0 :         mode = convert_column_priv_string(priv_type_text);
    2468                 :             : 
    2469                 :             :         /* First check at table level, then examine each column if needed */
    2470                 :           0 :         aclresult = pg_class_aclcheck(tableoid, roleid, mode);
    2471         [ #  # ]:           0 :         if (aclresult != ACLCHECK_OK)
    2472                 :           0 :                 aclresult = pg_attribute_aclcheck_all(tableoid, roleid, mode,
    2473                 :             :                                                                                           ACLMASK_ANY);
    2474                 :             : 
    2475                 :           0 :         PG_RETURN_BOOL(aclresult == ACLCHECK_OK);
    2476                 :           0 : }
    2477                 :             : 
    2478                 :             : /*
    2479                 :             :  * has_any_column_privilege_id_id
    2480                 :             :  *              Check user privileges on any column of a table given
    2481                 :             :  *              roleid, table oid, and text priv name.
    2482                 :             :  */
    2483                 :             : Datum
    2484                 :           0 : has_any_column_privilege_id_id(PG_FUNCTION_ARGS)
    2485                 :             : {
    2486                 :           0 :         Oid                     roleid = PG_GETARG_OID(0);
    2487                 :           0 :         Oid                     tableoid = PG_GETARG_OID(1);
    2488                 :           0 :         text       *priv_type_text = PG_GETARG_TEXT_PP(2);
    2489                 :           0 :         AclMode         mode;
    2490                 :           0 :         AclResult       aclresult;
    2491                 :           0 :         bool            is_missing = false;
    2492                 :             : 
    2493                 :           0 :         mode = convert_column_priv_string(priv_type_text);
    2494                 :             : 
    2495                 :             :         /* First check at table level, then examine each column if needed */
    2496                 :           0 :         aclresult = pg_class_aclcheck_ext(tableoid, roleid, mode, &is_missing);
    2497         [ #  # ]:           0 :         if (aclresult != ACLCHECK_OK)
    2498                 :             :         {
    2499         [ #  # ]:           0 :                 if (is_missing)
    2500                 :           0 :                         PG_RETURN_NULL();
    2501                 :           0 :                 aclresult = pg_attribute_aclcheck_all_ext(tableoid, roleid, mode,
    2502                 :             :                                                                                                   ACLMASK_ANY, &is_missing);
    2503         [ #  # ]:           0 :                 if (is_missing)
    2504                 :           0 :                         PG_RETURN_NULL();
    2505                 :           0 :         }
    2506                 :             : 
    2507                 :           0 :         PG_RETURN_BOOL(aclresult == ACLCHECK_OK);
    2508                 :           0 : }
    2509                 :             : 
    2510                 :             : 
    2511                 :             : /*
    2512                 :             :  * has_column_privilege variants
    2513                 :             :  *              These are all named "has_column_privilege" at the SQL level.
    2514                 :             :  *              They take various combinations of relation name, relation OID,
    2515                 :             :  *              column name, column attnum, user name, user OID, or
    2516                 :             :  *              implicit user = current_user.
    2517                 :             :  *
    2518                 :             :  *              The result is a boolean value: true if user has the indicated
    2519                 :             :  *              privilege, false if not.  The variants that take a relation OID
    2520                 :             :  *              return NULL (rather than throwing an error) if that relation OID
    2521                 :             :  *              doesn't exist.  Likewise, the variants that take an integer attnum
    2522                 :             :  *              return NULL (rather than throwing an error) if there is no such
    2523                 :             :  *              pg_attribute entry.  All variants return NULL if an attisdropped
    2524                 :             :  *              column is selected.  These rules are meant to avoid unnecessary
    2525                 :             :  *              failures in queries that scan pg_attribute.
    2526                 :             :  */
    2527                 :             : 
    2528                 :             : /*
    2529                 :             :  * column_privilege_check: check column privileges, but don't throw an error
    2530                 :             :  *              for dropped column or table
    2531                 :             :  *
    2532                 :             :  * Returns 1 if have the privilege, 0 if not, -1 if dropped column/table.
    2533                 :             :  */
    2534                 :             : static int
    2535                 :         330 : column_privilege_check(Oid tableoid, AttrNumber attnum,
    2536                 :             :                                            Oid roleid, AclMode mode)
    2537                 :             : {
    2538                 :         330 :         AclResult       aclresult;
    2539                 :         330 :         bool            is_missing = false;
    2540                 :             : 
    2541                 :             :         /*
    2542                 :             :          * If convert_column_name failed, we can just return -1 immediately.
    2543                 :             :          */
    2544         [ +  + ]:         330 :         if (attnum == InvalidAttrNumber)
    2545                 :           2 :                 return -1;
    2546                 :             : 
    2547                 :             :         /*
    2548                 :             :          * Check for column-level privileges first. This serves in part as a check
    2549                 :             :          * on whether the column even exists, so we need to do it before checking
    2550                 :             :          * table-level privilege.
    2551                 :             :          */
    2552                 :         656 :         aclresult = pg_attribute_aclcheck_ext(tableoid, attnum, roleid,
    2553                 :         328 :                                                                                   mode, &is_missing);
    2554         [ +  + ]:         328 :         if (aclresult == ACLCHECK_OK)
    2555                 :           2 :                 return 1;
    2556         [ +  + ]:         326 :         else if (is_missing)
    2557                 :           7 :                 return -1;
    2558                 :             : 
    2559                 :             :         /* Next check if we have the privilege at the table level */
    2560                 :         319 :         aclresult = pg_class_aclcheck_ext(tableoid, roleid, mode, &is_missing);
    2561         [ -  + ]:         319 :         if (aclresult == ACLCHECK_OK)
    2562                 :         319 :                 return 1;
    2563         [ #  # ]:           0 :         else if (is_missing)
    2564                 :           0 :                 return -1;
    2565                 :             :         else
    2566                 :           0 :                 return 0;
    2567                 :         330 : }
    2568                 :             : 
    2569                 :             : /*
    2570                 :             :  * has_column_privilege_name_name_name
    2571                 :             :  *              Check user privileges on a column given
    2572                 :             :  *              name username, text tablename, text colname, and text priv name.
    2573                 :             :  */
    2574                 :             : Datum
    2575                 :           0 : has_column_privilege_name_name_name(PG_FUNCTION_ARGS)
    2576                 :             : {
    2577                 :           0 :         Name            rolename = PG_GETARG_NAME(0);
    2578                 :           0 :         text       *tablename = PG_GETARG_TEXT_PP(1);
    2579                 :           0 :         text       *column = PG_GETARG_TEXT_PP(2);
    2580                 :           0 :         text       *priv_type_text = PG_GETARG_TEXT_PP(3);
    2581                 :           0 :         Oid                     roleid;
    2582                 :           0 :         Oid                     tableoid;
    2583                 :           0 :         AttrNumber      colattnum;
    2584                 :           0 :         AclMode         mode;
    2585                 :           0 :         int                     privresult;
    2586                 :             : 
    2587                 :           0 :         roleid = get_role_oid_or_public(NameStr(*rolename));
    2588                 :           0 :         tableoid = convert_table_name(tablename);
    2589                 :           0 :         colattnum = convert_column_name(tableoid, column);
    2590                 :           0 :         mode = convert_column_priv_string(priv_type_text);
    2591                 :             : 
    2592                 :           0 :         privresult = column_privilege_check(tableoid, colattnum, roleid, mode);
    2593         [ #  # ]:           0 :         if (privresult < 0)
    2594                 :           0 :                 PG_RETURN_NULL();
    2595                 :           0 :         PG_RETURN_BOOL(privresult);
    2596                 :           0 : }
    2597                 :             : 
    2598                 :             : /*
    2599                 :             :  * has_column_privilege_name_name_attnum
    2600                 :             :  *              Check user privileges on a column given
    2601                 :             :  *              name username, text tablename, int attnum, and text priv name.
    2602                 :             :  */
    2603                 :             : Datum
    2604                 :           0 : has_column_privilege_name_name_attnum(PG_FUNCTION_ARGS)
    2605                 :             : {
    2606                 :           0 :         Name            rolename = PG_GETARG_NAME(0);
    2607                 :           0 :         text       *tablename = PG_GETARG_TEXT_PP(1);
    2608                 :           0 :         AttrNumber      colattnum = PG_GETARG_INT16(2);
    2609                 :           0 :         text       *priv_type_text = PG_GETARG_TEXT_PP(3);
    2610                 :           0 :         Oid                     roleid;
    2611                 :           0 :         Oid                     tableoid;
    2612                 :           0 :         AclMode         mode;
    2613                 :           0 :         int                     privresult;
    2614                 :             : 
    2615                 :           0 :         roleid = get_role_oid_or_public(NameStr(*rolename));
    2616                 :           0 :         tableoid = convert_table_name(tablename);
    2617                 :           0 :         mode = convert_column_priv_string(priv_type_text);
    2618                 :             : 
    2619                 :           0 :         privresult = column_privilege_check(tableoid, colattnum, roleid, mode);
    2620         [ #  # ]:           0 :         if (privresult < 0)
    2621                 :           0 :                 PG_RETURN_NULL();
    2622                 :           0 :         PG_RETURN_BOOL(privresult);
    2623                 :           0 : }
    2624                 :             : 
    2625                 :             : /*
    2626                 :             :  * has_column_privilege_name_id_name
    2627                 :             :  *              Check user privileges on a column given
    2628                 :             :  *              name username, table oid, text colname, and text priv name.
    2629                 :             :  */
    2630                 :             : Datum
    2631                 :           0 : has_column_privilege_name_id_name(PG_FUNCTION_ARGS)
    2632                 :             : {
    2633                 :           0 :         Name            username = PG_GETARG_NAME(0);
    2634                 :           0 :         Oid                     tableoid = PG_GETARG_OID(1);
    2635                 :           0 :         text       *column = PG_GETARG_TEXT_PP(2);
    2636                 :           0 :         text       *priv_type_text = PG_GETARG_TEXT_PP(3);
    2637                 :           0 :         Oid                     roleid;
    2638                 :           0 :         AttrNumber      colattnum;
    2639                 :           0 :         AclMode         mode;
    2640                 :           0 :         int                     privresult;
    2641                 :             : 
    2642                 :           0 :         roleid = get_role_oid_or_public(NameStr(*username));
    2643                 :           0 :         colattnum = convert_column_name(tableoid, column);
    2644                 :           0 :         mode = convert_column_priv_string(priv_type_text);
    2645                 :             : 
    2646                 :           0 :         privresult = column_privilege_check(tableoid, colattnum, roleid, mode);
    2647         [ #  # ]:           0 :         if (privresult < 0)
    2648                 :           0 :                 PG_RETURN_NULL();
    2649                 :           0 :         PG_RETURN_BOOL(privresult);
    2650                 :           0 : }
    2651                 :             : 
    2652                 :             : /*
    2653                 :             :  * has_column_privilege_name_id_attnum
    2654                 :             :  *              Check user privileges on a column given
    2655                 :             :  *              name username, table oid, int attnum, and text priv name.
    2656                 :             :  */
    2657                 :             : Datum
    2658                 :           0 : has_column_privilege_name_id_attnum(PG_FUNCTION_ARGS)
    2659                 :             : {
    2660                 :           0 :         Name            username = PG_GETARG_NAME(0);
    2661                 :           0 :         Oid                     tableoid = PG_GETARG_OID(1);
    2662                 :           0 :         AttrNumber      colattnum = PG_GETARG_INT16(2);
    2663                 :           0 :         text       *priv_type_text = PG_GETARG_TEXT_PP(3);
    2664                 :           0 :         Oid                     roleid;
    2665                 :           0 :         AclMode         mode;
    2666                 :           0 :         int                     privresult;
    2667                 :             : 
    2668                 :           0 :         roleid = get_role_oid_or_public(NameStr(*username));
    2669                 :           0 :         mode = convert_column_priv_string(priv_type_text);
    2670                 :             : 
    2671                 :           0 :         privresult = column_privilege_check(tableoid, colattnum, roleid, mode);
    2672         [ #  # ]:           0 :         if (privresult < 0)
    2673                 :           0 :                 PG_RETURN_NULL();
    2674                 :           0 :         PG_RETURN_BOOL(privresult);
    2675                 :           0 : }
    2676                 :             : 
    2677                 :             : /*
    2678                 :             :  * has_column_privilege_id_name_name
    2679                 :             :  *              Check user privileges on a column given
    2680                 :             :  *              oid roleid, text tablename, text colname, and text priv name.
    2681                 :             :  */
    2682                 :             : Datum
    2683                 :           0 : has_column_privilege_id_name_name(PG_FUNCTION_ARGS)
    2684                 :             : {
    2685                 :           0 :         Oid                     roleid = PG_GETARG_OID(0);
    2686                 :           0 :         text       *tablename = PG_GETARG_TEXT_PP(1);
    2687                 :           0 :         text       *column = PG_GETARG_TEXT_PP(2);
    2688                 :           0 :         text       *priv_type_text = PG_GETARG_TEXT_PP(3);
    2689                 :           0 :         Oid                     tableoid;
    2690                 :           0 :         AttrNumber      colattnum;
    2691                 :           0 :         AclMode         mode;
    2692                 :           0 :         int                     privresult;
    2693                 :             : 
    2694                 :           0 :         tableoid = convert_table_name(tablename);
    2695                 :           0 :         colattnum = convert_column_name(tableoid, column);
    2696                 :           0 :         mode = convert_column_priv_string(priv_type_text);
    2697                 :             : 
    2698                 :           0 :         privresult = column_privilege_check(tableoid, colattnum, roleid, mode);
    2699         [ #  # ]:           0 :         if (privresult < 0)
    2700                 :           0 :                 PG_RETURN_NULL();
    2701                 :           0 :         PG_RETURN_BOOL(privresult);
    2702                 :           0 : }
    2703                 :             : 
    2704                 :             : /*
    2705                 :             :  * has_column_privilege_id_name_attnum
    2706                 :             :  *              Check user privileges on a column given
    2707                 :             :  *              oid roleid, text tablename, int attnum, and text priv name.
    2708                 :             :  */
    2709                 :             : Datum
    2710                 :           0 : has_column_privilege_id_name_attnum(PG_FUNCTION_ARGS)
    2711                 :             : {
    2712                 :           0 :         Oid                     roleid = PG_GETARG_OID(0);
    2713                 :           0 :         text       *tablename = PG_GETARG_TEXT_PP(1);
    2714                 :           0 :         AttrNumber      colattnum = PG_GETARG_INT16(2);
    2715                 :           0 :         text       *priv_type_text = PG_GETARG_TEXT_PP(3);
    2716                 :           0 :         Oid                     tableoid;
    2717                 :           0 :         AclMode         mode;
    2718                 :           0 :         int                     privresult;
    2719                 :             : 
    2720                 :           0 :         tableoid = convert_table_name(tablename);
    2721                 :           0 :         mode = convert_column_priv_string(priv_type_text);
    2722                 :             : 
    2723                 :           0 :         privresult = column_privilege_check(tableoid, colattnum, roleid, mode);
    2724         [ #  # ]:           0 :         if (privresult < 0)
    2725                 :           0 :                 PG_RETURN_NULL();
    2726                 :           0 :         PG_RETURN_BOOL(privresult);
    2727                 :           0 : }
    2728                 :             : 
    2729                 :             : /*
    2730                 :             :  * has_column_privilege_id_id_name
    2731                 :             :  *              Check user privileges on a column given
    2732                 :             :  *              oid roleid, table oid, text colname, and text priv name.
    2733                 :             :  */
    2734                 :             : Datum
    2735                 :           0 : has_column_privilege_id_id_name(PG_FUNCTION_ARGS)
    2736                 :             : {
    2737                 :           0 :         Oid                     roleid = PG_GETARG_OID(0);
    2738                 :           0 :         Oid                     tableoid = PG_GETARG_OID(1);
    2739                 :           0 :         text       *column = PG_GETARG_TEXT_PP(2);
    2740                 :           0 :         text       *priv_type_text = PG_GETARG_TEXT_PP(3);
    2741                 :           0 :         AttrNumber      colattnum;
    2742                 :           0 :         AclMode         mode;
    2743                 :           0 :         int                     privresult;
    2744                 :             : 
    2745                 :           0 :         colattnum = convert_column_name(tableoid, column);
    2746                 :           0 :         mode = convert_column_priv_string(priv_type_text);
    2747                 :             : 
    2748                 :           0 :         privresult = column_privilege_check(tableoid, colattnum, roleid, mode);
    2749         [ #  # ]:           0 :         if (privresult < 0)
    2750                 :           0 :                 PG_RETURN_NULL();
    2751                 :           0 :         PG_RETURN_BOOL(privresult);
    2752                 :           0 : }
    2753                 :             : 
    2754                 :             : /*
    2755                 :             :  * has_column_privilege_id_id_attnum
    2756                 :             :  *              Check user privileges on a column given
    2757                 :             :  *              oid roleid, table oid, int attnum, and text priv name.
    2758                 :             :  */
    2759                 :             : Datum
    2760                 :           0 : has_column_privilege_id_id_attnum(PG_FUNCTION_ARGS)
    2761                 :             : {
    2762                 :           0 :         Oid                     roleid = PG_GETARG_OID(0);
    2763                 :           0 :         Oid                     tableoid = PG_GETARG_OID(1);
    2764                 :           0 :         AttrNumber      colattnum = PG_GETARG_INT16(2);
    2765                 :           0 :         text       *priv_type_text = PG_GETARG_TEXT_PP(3);
    2766                 :           0 :         AclMode         mode;
    2767                 :           0 :         int                     privresult;
    2768                 :             : 
    2769                 :           0 :         mode = convert_column_priv_string(priv_type_text);
    2770                 :             : 
    2771                 :           0 :         privresult = column_privilege_check(tableoid, colattnum, roleid, mode);
    2772         [ #  # ]:           0 :         if (privresult < 0)
    2773                 :           0 :                 PG_RETURN_NULL();
    2774                 :           0 :         PG_RETURN_BOOL(privresult);
    2775                 :           0 : }
    2776                 :             : 
    2777                 :             : /*
    2778                 :             :  * has_column_privilege_name_name
    2779                 :             :  *              Check user privileges on a column given
    2780                 :             :  *              text tablename, text colname, and text priv name.
    2781                 :             :  *              current_user is assumed
    2782                 :             :  */
    2783                 :             : Datum
    2784                 :           1 : has_column_privilege_name_name(PG_FUNCTION_ARGS)
    2785                 :             : {
    2786                 :           1 :         text       *tablename = PG_GETARG_TEXT_PP(0);
    2787                 :           1 :         text       *column = PG_GETARG_TEXT_PP(1);
    2788                 :           1 :         text       *priv_type_text = PG_GETARG_TEXT_PP(2);
    2789                 :           1 :         Oid                     roleid;
    2790                 :           1 :         Oid                     tableoid;
    2791                 :           1 :         AttrNumber      colattnum;
    2792                 :           1 :         AclMode         mode;
    2793                 :           1 :         int                     privresult;
    2794                 :             : 
    2795                 :           1 :         roleid = GetUserId();
    2796                 :           1 :         tableoid = convert_table_name(tablename);
    2797                 :           1 :         colattnum = convert_column_name(tableoid, column);
    2798                 :           1 :         mode = convert_column_priv_string(priv_type_text);
    2799                 :             : 
    2800                 :           1 :         privresult = column_privilege_check(tableoid, colattnum, roleid, mode);
    2801         [ -  + ]:           1 :         if (privresult < 0)
    2802                 :           1 :                 PG_RETURN_NULL();
    2803                 :           0 :         PG_RETURN_BOOL(privresult);
    2804                 :           1 : }
    2805                 :             : 
    2806                 :             : /*
    2807                 :             :  * has_column_privilege_name_attnum
    2808                 :             :  *              Check user privileges on a column given
    2809                 :             :  *              text tablename, int attnum, and text priv name.
    2810                 :             :  *              current_user is assumed
    2811                 :             :  */
    2812                 :             : Datum
    2813                 :           5 : has_column_privilege_name_attnum(PG_FUNCTION_ARGS)
    2814                 :             : {
    2815                 :           5 :         text       *tablename = PG_GETARG_TEXT_PP(0);
    2816                 :           5 :         AttrNumber      colattnum = PG_GETARG_INT16(1);
    2817                 :           5 :         text       *priv_type_text = PG_GETARG_TEXT_PP(2);
    2818                 :           5 :         Oid                     roleid;
    2819                 :           5 :         Oid                     tableoid;
    2820                 :           5 :         AclMode         mode;
    2821                 :           5 :         int                     privresult;
    2822                 :             : 
    2823                 :           5 :         roleid = GetUserId();
    2824                 :           5 :         tableoid = convert_table_name(tablename);
    2825                 :           5 :         mode = convert_column_priv_string(priv_type_text);
    2826                 :             : 
    2827                 :           5 :         privresult = column_privilege_check(tableoid, colattnum, roleid, mode);
    2828         [ -  + ]:           5 :         if (privresult < 0)
    2829                 :           5 :                 PG_RETURN_NULL();
    2830                 :           0 :         PG_RETURN_BOOL(privresult);
    2831                 :           5 : }
    2832                 :             : 
    2833                 :             : /*
    2834                 :             :  * has_column_privilege_id_name
    2835                 :             :  *              Check user privileges on a column given
    2836                 :             :  *              table oid, text colname, and text priv name.
    2837                 :             :  *              current_user is assumed
    2838                 :             :  */
    2839                 :             : Datum
    2840                 :           1 : has_column_privilege_id_name(PG_FUNCTION_ARGS)
    2841                 :             : {
    2842                 :           1 :         Oid                     tableoid = PG_GETARG_OID(0);
    2843                 :           1 :         text       *column = PG_GETARG_TEXT_PP(1);
    2844                 :           1 :         text       *priv_type_text = PG_GETARG_TEXT_PP(2);
    2845                 :           1 :         Oid                     roleid;
    2846                 :           1 :         AttrNumber      colattnum;
    2847                 :           1 :         AclMode         mode;
    2848                 :           1 :         int                     privresult;
    2849                 :             : 
    2850                 :           1 :         roleid = GetUserId();
    2851                 :           1 :         colattnum = convert_column_name(tableoid, column);
    2852                 :           1 :         mode = convert_column_priv_string(priv_type_text);
    2853                 :             : 
    2854                 :           1 :         privresult = column_privilege_check(tableoid, colattnum, roleid, mode);
    2855         [ -  + ]:           1 :         if (privresult < 0)
    2856                 :           1 :                 PG_RETURN_NULL();
    2857                 :           0 :         PG_RETURN_BOOL(privresult);
    2858                 :           1 : }
    2859                 :             : 
    2860                 :             : /*
    2861                 :             :  * has_column_privilege_id_attnum
    2862                 :             :  *              Check user privileges on a column given
    2863                 :             :  *              table oid, int attnum, and text priv name.
    2864                 :             :  *              current_user is assumed
    2865                 :             :  */
    2866                 :             : Datum
    2867                 :         323 : has_column_privilege_id_attnum(PG_FUNCTION_ARGS)
    2868                 :             : {
    2869                 :         323 :         Oid                     tableoid = PG_GETARG_OID(0);
    2870                 :         323 :         AttrNumber      colattnum = PG_GETARG_INT16(1);
    2871                 :         323 :         text       *priv_type_text = PG_GETARG_TEXT_PP(2);
    2872                 :         323 :         Oid                     roleid;
    2873                 :         323 :         AclMode         mode;
    2874                 :         323 :         int                     privresult;
    2875                 :             : 
    2876                 :         323 :         roleid = GetUserId();
    2877                 :         323 :         mode = convert_column_priv_string(priv_type_text);
    2878                 :             : 
    2879                 :         323 :         privresult = column_privilege_check(tableoid, colattnum, roleid, mode);
    2880         [ +  + ]:         323 :         if (privresult < 0)
    2881                 :           2 :                 PG_RETURN_NULL();
    2882                 :         321 :         PG_RETURN_BOOL(privresult);
    2883                 :         323 : }
    2884                 :             : 
    2885                 :             : /*
    2886                 :             :  *              Support routines for has_column_privilege family.
    2887                 :             :  */
    2888                 :             : 
    2889                 :             : /*
    2890                 :             :  * Given a table OID and a column name expressed as a string, look it up
    2891                 :             :  * and return the column number.  Returns InvalidAttrNumber in cases
    2892                 :             :  * where caller should return NULL instead of failing.
    2893                 :             :  */
    2894                 :             : static AttrNumber
    2895                 :           4 : convert_column_name(Oid tableoid, text *column)
    2896                 :             : {
    2897                 :           4 :         char       *colname;
    2898                 :           4 :         HeapTuple       attTuple;
    2899                 :           4 :         AttrNumber      attnum;
    2900                 :             : 
    2901                 :           4 :         colname = text_to_cstring(column);
    2902                 :             : 
    2903                 :             :         /*
    2904                 :             :          * We don't use get_attnum() here because it will report that dropped
    2905                 :             :          * columns don't exist.  We need to treat dropped columns differently from
    2906                 :             :          * nonexistent columns.
    2907                 :             :          */
    2908                 :           4 :         attTuple = SearchSysCache2(ATTNAME,
    2909                 :           4 :                                                            ObjectIdGetDatum(tableoid),
    2910                 :           4 :                                                            CStringGetDatum(colname));
    2911         [ +  + ]:           4 :         if (HeapTupleIsValid(attTuple))
    2912                 :             :         {
    2913                 :           1 :                 Form_pg_attribute attributeForm;
    2914                 :             : 
    2915                 :           1 :                 attributeForm = (Form_pg_attribute) GETSTRUCT(attTuple);
    2916                 :             :                 /* We want to return NULL for dropped columns */
    2917         [ +  - ]:           1 :                 if (attributeForm->attisdropped)
    2918                 :           1 :                         attnum = InvalidAttrNumber;
    2919                 :             :                 else
    2920                 :           0 :                         attnum = attributeForm->attnum;
    2921                 :           1 :                 ReleaseSysCache(attTuple);
    2922                 :           1 :         }
    2923                 :             :         else
    2924                 :             :         {
    2925                 :           3 :                 char       *tablename = get_rel_name(tableoid);
    2926                 :             : 
    2927                 :             :                 /*
    2928                 :             :                  * If the table OID is bogus, or it's just been dropped, we'll get
    2929                 :             :                  * NULL back.  In such cases we want has_column_privilege to return
    2930                 :             :                  * NULL too, so just return InvalidAttrNumber.
    2931                 :             :                  */
    2932         [ +  + ]:           3 :                 if (tablename != NULL)
    2933                 :             :                 {
    2934                 :             :                         /* tableoid exists, colname does not, so throw error */
    2935   [ +  -  +  - ]:           2 :                         ereport(ERROR,
    2936                 :             :                                         (errcode(ERRCODE_UNDEFINED_COLUMN),
    2937                 :             :                                          errmsg("column \"%s\" of relation \"%s\" does not exist",
    2938                 :             :                                                         colname, tablename)));
    2939                 :           0 :                 }
    2940                 :             :                 /* tableoid doesn't exist, so act like attisdropped case */
    2941                 :           1 :                 attnum = InvalidAttrNumber;
    2942                 :           1 :         }
    2943                 :             : 
    2944                 :           2 :         pfree(colname);
    2945                 :           4 :         return attnum;
    2946                 :           2 : }
    2947                 :             : 
    2948                 :             : /*
    2949                 :             :  * convert_column_priv_string
    2950                 :             :  *              Convert text string to AclMode value.
    2951                 :             :  */
    2952                 :             : static AclMode
    2953                 :         330 : convert_column_priv_string(text *priv_type_text)
    2954                 :             : {
    2955                 :             :         static const priv_map column_priv_map[] = {
    2956                 :             :                 {"SELECT", ACL_SELECT},
    2957                 :             :                 {"SELECT WITH GRANT OPTION", ACL_GRANT_OPTION_FOR(ACL_SELECT)},
    2958                 :             :                 {"INSERT", ACL_INSERT},
    2959                 :             :                 {"INSERT WITH GRANT OPTION", ACL_GRANT_OPTION_FOR(ACL_INSERT)},
    2960                 :             :                 {"UPDATE", ACL_UPDATE},
    2961                 :             :                 {"UPDATE WITH GRANT OPTION", ACL_GRANT_OPTION_FOR(ACL_UPDATE)},
    2962                 :             :                 {"REFERENCES", ACL_REFERENCES},
    2963                 :             :                 {"REFERENCES WITH GRANT OPTION", ACL_GRANT_OPTION_FOR(ACL_REFERENCES)},
    2964                 :             :                 {NULL, 0}
    2965                 :             :         };
    2966                 :             : 
    2967                 :         330 :         return convert_any_priv_string(priv_type_text, column_priv_map);
    2968                 :             : }
    2969                 :             : 
    2970                 :             : 
    2971                 :             : /*
    2972                 :             :  * has_database_privilege variants
    2973                 :             :  *              These are all named "has_database_privilege" at the SQL level.
    2974                 :             :  *              They take various combinations of database name, database OID,
    2975                 :             :  *              user name, user OID, or implicit user = current_user.
    2976                 :             :  *
    2977                 :             :  *              The result is a boolean value: true if user has the indicated
    2978                 :             :  *              privilege, false if not, or NULL if object doesn't exist.
    2979                 :             :  */
    2980                 :             : 
    2981                 :             : /*
    2982                 :             :  * has_database_privilege_name_name
    2983                 :             :  *              Check user privileges on a database given
    2984                 :             :  *              name username, text databasename, and text priv name.
    2985                 :             :  */
    2986                 :             : Datum
    2987                 :           0 : has_database_privilege_name_name(PG_FUNCTION_ARGS)
    2988                 :             : {
    2989                 :           0 :         Name            username = PG_GETARG_NAME(0);
    2990                 :           0 :         text       *databasename = PG_GETARG_TEXT_PP(1);
    2991                 :           0 :         text       *priv_type_text = PG_GETARG_TEXT_PP(2);
    2992                 :           0 :         Oid                     roleid;
    2993                 :           0 :         Oid                     databaseoid;
    2994                 :           0 :         AclMode         mode;
    2995                 :           0 :         AclResult       aclresult;
    2996                 :             : 
    2997                 :           0 :         roleid = get_role_oid_or_public(NameStr(*username));
    2998                 :           0 :         databaseoid = convert_database_name(databasename);
    2999                 :           0 :         mode = convert_database_priv_string(priv_type_text);
    3000                 :             : 
    3001                 :           0 :         aclresult = object_aclcheck(DatabaseRelationId, databaseoid, roleid, mode);
    3002                 :             : 
    3003                 :           0 :         PG_RETURN_BOOL(aclresult == ACLCHECK_OK);
    3004                 :           0 : }
    3005                 :             : 
    3006                 :             : /*
    3007                 :             :  * has_database_privilege_name
    3008                 :             :  *              Check user privileges on a database given
    3009                 :             :  *              text databasename and text priv name.
    3010                 :             :  *              current_user is assumed
    3011                 :             :  */
    3012                 :             : Datum
    3013                 :           0 : has_database_privilege_name(PG_FUNCTION_ARGS)
    3014                 :             : {
    3015                 :           0 :         text       *databasename = PG_GETARG_TEXT_PP(0);
    3016                 :           0 :         text       *priv_type_text = PG_GETARG_TEXT_PP(1);
    3017                 :           0 :         Oid                     roleid;
    3018                 :           0 :         Oid                     databaseoid;
    3019                 :           0 :         AclMode         mode;
    3020                 :           0 :         AclResult       aclresult;
    3021                 :             : 
    3022                 :           0 :         roleid = GetUserId();
    3023                 :           0 :         databaseoid = convert_database_name(databasename);
    3024                 :           0 :         mode = convert_database_priv_string(priv_type_text);
    3025                 :             : 
    3026                 :           0 :         aclresult = object_aclcheck(DatabaseRelationId, databaseoid, roleid, mode);
    3027                 :             : 
    3028                 :           0 :         PG_RETURN_BOOL(aclresult == ACLCHECK_OK);
    3029                 :           0 : }
    3030                 :             : 
    3031                 :             : /*
    3032                 :             :  * has_database_privilege_name_id
    3033                 :             :  *              Check user privileges on a database given
    3034                 :             :  *              name usename, database oid, and text priv name.
    3035                 :             :  */
    3036                 :             : Datum
    3037                 :           0 : has_database_privilege_name_id(PG_FUNCTION_ARGS)
    3038                 :             : {
    3039                 :           0 :         Name            username = PG_GETARG_NAME(0);
    3040                 :           0 :         Oid                     databaseoid = PG_GETARG_OID(1);
    3041                 :           0 :         text       *priv_type_text = PG_GETARG_TEXT_PP(2);
    3042                 :           0 :         Oid                     roleid;
    3043                 :           0 :         AclMode         mode;
    3044                 :           0 :         AclResult       aclresult;
    3045                 :           0 :         bool            is_missing = false;
    3046                 :             : 
    3047                 :           0 :         roleid = get_role_oid_or_public(NameStr(*username));
    3048                 :           0 :         mode = convert_database_priv_string(priv_type_text);
    3049                 :             : 
    3050                 :           0 :         aclresult = object_aclcheck_ext(DatabaseRelationId, databaseoid,
    3051                 :           0 :                                                                         roleid, mode,
    3052                 :             :                                                                         &is_missing);
    3053                 :             : 
    3054         [ #  # ]:           0 :         if (is_missing)
    3055                 :           0 :                 PG_RETURN_NULL();
    3056                 :             : 
    3057                 :           0 :         PG_RETURN_BOOL(aclresult == ACLCHECK_OK);
    3058                 :           0 : }
    3059                 :             : 
    3060                 :             : /*
    3061                 :             :  * has_database_privilege_id
    3062                 :             :  *              Check user privileges on a database given
    3063                 :             :  *              database oid, and text priv name.
    3064                 :             :  *              current_user is assumed
    3065                 :             :  */
    3066                 :             : Datum
    3067                 :           0 : has_database_privilege_id(PG_FUNCTION_ARGS)
    3068                 :             : {
    3069                 :           0 :         Oid                     databaseoid = PG_GETARG_OID(0);
    3070                 :           0 :         text       *priv_type_text = PG_GETARG_TEXT_PP(1);
    3071                 :           0 :         Oid                     roleid;
    3072                 :           0 :         AclMode         mode;
    3073                 :           0 :         AclResult       aclresult;
    3074                 :           0 :         bool            is_missing = false;
    3075                 :             : 
    3076                 :           0 :         roleid = GetUserId();
    3077                 :           0 :         mode = convert_database_priv_string(priv_type_text);
    3078                 :             : 
    3079                 :           0 :         aclresult = object_aclcheck_ext(DatabaseRelationId, databaseoid,
    3080                 :           0 :                                                                         roleid, mode,
    3081                 :             :                                                                         &is_missing);
    3082                 :             : 
    3083         [ #  # ]:           0 :         if (is_missing)
    3084                 :           0 :                 PG_RETURN_NULL();
    3085                 :             : 
    3086                 :           0 :         PG_RETURN_BOOL(aclresult == ACLCHECK_OK);
    3087                 :           0 : }
    3088                 :             : 
    3089                 :             : /*
    3090                 :             :  * has_database_privilege_id_name
    3091                 :             :  *              Check user privileges on a database given
    3092                 :             :  *              roleid, text databasename, and text priv name.
    3093                 :             :  */
    3094                 :             : Datum
    3095                 :           0 : has_database_privilege_id_name(PG_FUNCTION_ARGS)
    3096                 :             : {
    3097                 :           0 :         Oid                     roleid = PG_GETARG_OID(0);
    3098                 :           0 :         text       *databasename = PG_GETARG_TEXT_PP(1);
    3099                 :           0 :         text       *priv_type_text = PG_GETARG_TEXT_PP(2);
    3100                 :           0 :         Oid                     databaseoid;
    3101                 :           0 :         AclMode         mode;
    3102                 :           0 :         AclResult       aclresult;
    3103                 :             : 
    3104                 :           0 :         databaseoid = convert_database_name(databasename);
    3105                 :           0 :         mode = convert_database_priv_string(priv_type_text);
    3106                 :             : 
    3107                 :           0 :         aclresult = object_aclcheck(DatabaseRelationId, databaseoid, roleid, mode);
    3108                 :             : 
    3109                 :           0 :         PG_RETURN_BOOL(aclresult == ACLCHECK_OK);
    3110                 :           0 : }
    3111                 :             : 
    3112                 :             : /*
    3113                 :             :  * has_database_privilege_id_id
    3114                 :             :  *              Check user privileges on a database given
    3115                 :             :  *              roleid, database oid, and text priv name.
    3116                 :             :  */
    3117                 :             : Datum
    3118                 :           0 : has_database_privilege_id_id(PG_FUNCTION_ARGS)
    3119                 :             : {
    3120                 :           0 :         Oid                     roleid = PG_GETARG_OID(0);
    3121                 :           0 :         Oid                     databaseoid = PG_GETARG_OID(1);
    3122                 :           0 :         text       *priv_type_text = PG_GETARG_TEXT_PP(2);
    3123                 :           0 :         AclMode         mode;
    3124                 :           0 :         AclResult       aclresult;
    3125                 :           0 :         bool            is_missing = false;
    3126                 :             : 
    3127                 :           0 :         mode = convert_database_priv_string(priv_type_text);
    3128                 :             : 
    3129                 :           0 :         aclresult = object_aclcheck_ext(DatabaseRelationId, databaseoid,
    3130                 :           0 :                                                                         roleid, mode,
    3131                 :             :                                                                         &is_missing);
    3132                 :             : 
    3133         [ #  # ]:           0 :         if (is_missing)
    3134                 :           0 :                 PG_RETURN_NULL();
    3135                 :             : 
    3136                 :           0 :         PG_RETURN_BOOL(aclresult == ACLCHECK_OK);
    3137                 :           0 : }
    3138                 :             : 
    3139                 :             : /*
    3140                 :             :  *              Support routines for has_database_privilege family.
    3141                 :             :  */
    3142                 :             : 
    3143                 :             : /*
    3144                 :             :  * Given a database name expressed as a string, look it up and return Oid
    3145                 :             :  */
    3146                 :             : static Oid
    3147                 :           0 : convert_database_name(text *databasename)
    3148                 :             : {
    3149                 :           0 :         char       *dbname = text_to_cstring(databasename);
    3150                 :             : 
    3151                 :           0 :         return get_database_oid(dbname, false);
    3152                 :           0 : }
    3153                 :             : 
    3154                 :             : /*
    3155                 :             :  * convert_database_priv_string
    3156                 :             :  *              Convert text string to AclMode value.
    3157                 :             :  */
    3158                 :             : static AclMode
    3159                 :           0 : convert_database_priv_string(text *priv_type_text)
    3160                 :             : {
    3161                 :             :         static const priv_map database_priv_map[] = {
    3162                 :             :                 {"CREATE", ACL_CREATE},
    3163                 :             :                 {"CREATE WITH GRANT OPTION", ACL_GRANT_OPTION_FOR(ACL_CREATE)},
    3164                 :             :                 {"TEMPORARY", ACL_CREATE_TEMP},
    3165                 :             :                 {"TEMPORARY WITH GRANT OPTION", ACL_GRANT_OPTION_FOR(ACL_CREATE_TEMP)},
    3166                 :             :                 {"TEMP", ACL_CREATE_TEMP},
    3167                 :             :                 {"TEMP WITH GRANT OPTION", ACL_GRANT_OPTION_FOR(ACL_CREATE_TEMP)},
    3168                 :             :                 {"CONNECT", ACL_CONNECT},
    3169                 :             :                 {"CONNECT WITH GRANT OPTION", ACL_GRANT_OPTION_FOR(ACL_CONNECT)},
    3170                 :             :                 {NULL, 0}
    3171                 :             :         };
    3172                 :             : 
    3173                 :           0 :         return convert_any_priv_string(priv_type_text, database_priv_map);
    3174                 :             : }
    3175                 :             : 
    3176                 :             : 
    3177                 :             : /*
    3178                 :             :  * has_foreign_data_wrapper_privilege variants
    3179                 :             :  *              These are all named "has_foreign_data_wrapper_privilege" at the SQL level.
    3180                 :             :  *              They take various combinations of foreign-data wrapper name,
    3181                 :             :  *              fdw OID, user name, user OID, or implicit user = current_user.
    3182                 :             :  *
    3183                 :             :  *              The result is a boolean value: true if user has the indicated
    3184                 :             :  *              privilege, false if not.
    3185                 :             :  */
    3186                 :             : 
    3187                 :             : /*
    3188                 :             :  * has_foreign_data_wrapper_privilege_name_name
    3189                 :             :  *              Check user privileges on a foreign-data wrapper given
    3190                 :             :  *              name username, text fdwname, and text priv name.
    3191                 :             :  */
    3192                 :             : Datum
    3193                 :           2 : has_foreign_data_wrapper_privilege_name_name(PG_FUNCTION_ARGS)
    3194                 :             : {
    3195                 :           2 :         Name            username = PG_GETARG_NAME(0);
    3196                 :           2 :         text       *fdwname = PG_GETARG_TEXT_PP(1);
    3197                 :           2 :         text       *priv_type_text = PG_GETARG_TEXT_PP(2);
    3198                 :           2 :         Oid                     roleid;
    3199                 :           2 :         Oid                     fdwid;
    3200                 :           2 :         AclMode         mode;
    3201                 :           2 :         AclResult       aclresult;
    3202                 :             : 
    3203                 :           2 :         roleid = get_role_oid_or_public(NameStr(*username));
    3204                 :           2 :         fdwid = convert_foreign_data_wrapper_name(fdwname);
    3205                 :           2 :         mode = convert_foreign_data_wrapper_priv_string(priv_type_text);
    3206                 :             : 
    3207                 :           2 :         aclresult = object_aclcheck(ForeignDataWrapperRelationId, fdwid, roleid, mode);
    3208                 :             : 
    3209                 :           4 :         PG_RETURN_BOOL(aclresult == ACLCHECK_OK);
    3210                 :           2 : }
    3211                 :             : 
    3212                 :             : /*
    3213                 :             :  * has_foreign_data_wrapper_privilege_name
    3214                 :             :  *              Check user privileges on a foreign-data wrapper given
    3215                 :             :  *              text fdwname and text priv name.
    3216                 :             :  *              current_user is assumed
    3217                 :             :  */
    3218                 :             : Datum
    3219                 :           1 : has_foreign_data_wrapper_privilege_name(PG_FUNCTION_ARGS)
    3220                 :             : {
    3221                 :           1 :         text       *fdwname = PG_GETARG_TEXT_PP(0);
    3222                 :           1 :         text       *priv_type_text = PG_GETARG_TEXT_PP(1);
    3223                 :           1 :         Oid                     roleid;
    3224                 :           1 :         Oid                     fdwid;
    3225                 :           1 :         AclMode         mode;
    3226                 :           1 :         AclResult       aclresult;
    3227                 :             : 
    3228                 :           1 :         roleid = GetUserId();
    3229                 :           1 :         fdwid = convert_foreign_data_wrapper_name(fdwname);
    3230                 :           1 :         mode = convert_foreign_data_wrapper_priv_string(priv_type_text);
    3231                 :             : 
    3232                 :           1 :         aclresult = object_aclcheck(ForeignDataWrapperRelationId, fdwid, roleid, mode);
    3233                 :             : 
    3234                 :           2 :         PG_RETURN_BOOL(aclresult == ACLCHECK_OK);
    3235                 :           1 : }
    3236                 :             : 
    3237                 :             : /*
    3238                 :             :  * has_foreign_data_wrapper_privilege_name_id
    3239                 :             :  *              Check user privileges on a foreign-data wrapper given
    3240                 :             :  *              name usename, foreign-data wrapper oid, and text priv name.
    3241                 :             :  */
    3242                 :             : Datum
    3243                 :           1 : has_foreign_data_wrapper_privilege_name_id(PG_FUNCTION_ARGS)
    3244                 :             : {
    3245                 :           1 :         Name            username = PG_GETARG_NAME(0);
    3246                 :           1 :         Oid                     fdwid = PG_GETARG_OID(1);
    3247                 :           1 :         text       *priv_type_text = PG_GETARG_TEXT_PP(2);
    3248                 :           1 :         Oid                     roleid;
    3249                 :           1 :         AclMode         mode;
    3250                 :           1 :         AclResult       aclresult;
    3251                 :           1 :         bool            is_missing = false;
    3252                 :             : 
    3253                 :           1 :         roleid = get_role_oid_or_public(NameStr(*username));
    3254                 :           1 :         mode = convert_foreign_data_wrapper_priv_string(priv_type_text);
    3255                 :             : 
    3256                 :           2 :         aclresult = object_aclcheck_ext(ForeignDataWrapperRelationId, fdwid,
    3257                 :           1 :                                                                         roleid, mode,
    3258                 :             :                                                                         &is_missing);
    3259                 :             : 
    3260         [ +  - ]:           1 :         if (is_missing)
    3261                 :           0 :                 PG_RETURN_NULL();
    3262                 :             : 
    3263                 :           1 :         PG_RETURN_BOOL(aclresult == ACLCHECK_OK);
    3264                 :           1 : }
    3265                 :             : 
    3266                 :             : /*
    3267                 :             :  * has_foreign_data_wrapper_privilege_id
    3268                 :             :  *              Check user privileges on a foreign-data wrapper given
    3269                 :             :  *              foreign-data wrapper oid, and text priv name.
    3270                 :             :  *              current_user is assumed
    3271                 :             :  */
    3272                 :             : Datum
    3273                 :           1 : has_foreign_data_wrapper_privilege_id(PG_FUNCTION_ARGS)
    3274                 :             : {
    3275                 :           1 :         Oid                     fdwid = PG_GETARG_OID(0);
    3276                 :           1 :         text       *priv_type_text = PG_GETARG_TEXT_PP(1);
    3277                 :           1 :         Oid                     roleid;
    3278                 :           1 :         AclMode         mode;
    3279                 :           1 :         AclResult       aclresult;
    3280                 :           1 :         bool            is_missing = false;
    3281                 :             : 
    3282                 :           1 :         roleid = GetUserId();
    3283                 :           1 :         mode = convert_foreign_data_wrapper_priv_string(priv_type_text);
    3284                 :             : 
    3285                 :           2 :         aclresult = object_aclcheck_ext(ForeignDataWrapperRelationId, fdwid,
    3286                 :           1 :                                                                         roleid, mode,
    3287                 :             :                                                                         &is_missing);
    3288                 :             : 
    3289         [ +  - ]:           1 :         if (is_missing)
    3290                 :           0 :                 PG_RETURN_NULL();
    3291                 :             : 
    3292                 :           1 :         PG_RETURN_BOOL(aclresult == ACLCHECK_OK);
    3293                 :           1 : }
    3294                 :             : 
    3295                 :             : /*
    3296                 :             :  * has_foreign_data_wrapper_privilege_id_name
    3297                 :             :  *              Check user privileges on a foreign-data wrapper given
    3298                 :             :  *              roleid, text fdwname, and text priv name.
    3299                 :             :  */
    3300                 :             : Datum
    3301                 :           1 : has_foreign_data_wrapper_privilege_id_name(PG_FUNCTION_ARGS)
    3302                 :             : {
    3303                 :           1 :         Oid                     roleid = PG_GETARG_OID(0);
    3304                 :           1 :         text       *fdwname = PG_GETARG_TEXT_PP(1);
    3305                 :           1 :         text       *priv_type_text = PG_GETARG_TEXT_PP(2);
    3306                 :           1 :         Oid                     fdwid;
    3307                 :           1 :         AclMode         mode;
    3308                 :           1 :         AclResult       aclresult;
    3309                 :             : 
    3310                 :           1 :         fdwid = convert_foreign_data_wrapper_name(fdwname);
    3311                 :           1 :         mode = convert_foreign_data_wrapper_priv_string(priv_type_text);
    3312                 :             : 
    3313                 :           1 :         aclresult = object_aclcheck(ForeignDataWrapperRelationId, fdwid, roleid, mode);
    3314                 :             : 
    3315                 :           2 :         PG_RETURN_BOOL(aclresult == ACLCHECK_OK);
    3316                 :           1 : }
    3317                 :             : 
    3318                 :             : /*
    3319                 :             :  * has_foreign_data_wrapper_privilege_id_id
    3320                 :             :  *              Check user privileges on a foreign-data wrapper given
    3321                 :             :  *              roleid, fdw oid, and text priv name.
    3322                 :             :  */
    3323                 :             : Datum
    3324                 :           1 : has_foreign_data_wrapper_privilege_id_id(PG_FUNCTION_ARGS)
    3325                 :             : {
    3326                 :           1 :         Oid                     roleid = PG_GETARG_OID(0);
    3327                 :           1 :         Oid                     fdwid = PG_GETARG_OID(1);
    3328                 :           1 :         text       *priv_type_text = PG_GETARG_TEXT_PP(2);
    3329                 :           1 :         AclMode         mode;
    3330                 :           1 :         AclResult       aclresult;
    3331                 :           1 :         bool            is_missing = false;
    3332                 :             : 
    3333                 :           1 :         mode = convert_foreign_data_wrapper_priv_string(priv_type_text);
    3334                 :             : 
    3335                 :           2 :         aclresult = object_aclcheck_ext(ForeignDataWrapperRelationId, fdwid,
    3336                 :           1 :                                                                         roleid, mode,
    3337                 :             :                                                                         &is_missing);
    3338                 :             : 
    3339         [ +  - ]:           1 :         if (is_missing)
    3340                 :           0 :                 PG_RETURN_NULL();
    3341                 :             : 
    3342                 :           1 :         PG_RETURN_BOOL(aclresult == ACLCHECK_OK);
    3343                 :           1 : }
    3344                 :             : 
    3345                 :             : /*
    3346                 :             :  *              Support routines for has_foreign_data_wrapper_privilege family.
    3347                 :             :  */
    3348                 :             : 
    3349                 :             : /*
    3350                 :             :  * Given a FDW name expressed as a string, look it up and return Oid
    3351                 :             :  */
    3352                 :             : static Oid
    3353                 :           4 : convert_foreign_data_wrapper_name(text *fdwname)
    3354                 :             : {
    3355                 :           4 :         char       *fdwstr = text_to_cstring(fdwname);
    3356                 :             : 
    3357                 :           8 :         return get_foreign_data_wrapper_oid(fdwstr, false);
    3358                 :           4 : }
    3359                 :             : 
    3360                 :             : /*
    3361                 :             :  * convert_foreign_data_wrapper_priv_string
    3362                 :             :  *              Convert text string to AclMode value.
    3363                 :             :  */
    3364                 :             : static AclMode
    3365                 :           7 : convert_foreign_data_wrapper_priv_string(text *priv_type_text)
    3366                 :             : {
    3367                 :             :         static const priv_map foreign_data_wrapper_priv_map[] = {
    3368                 :             :                 {"USAGE", ACL_USAGE},
    3369                 :             :                 {"USAGE WITH GRANT OPTION", ACL_GRANT_OPTION_FOR(ACL_USAGE)},
    3370                 :             :                 {NULL, 0}
    3371                 :             :         };
    3372                 :             : 
    3373                 :           7 :         return convert_any_priv_string(priv_type_text, foreign_data_wrapper_priv_map);
    3374                 :             : }
    3375                 :             : 
    3376                 :             : 
    3377                 :             : /*
    3378                 :             :  * has_function_privilege variants
    3379                 :             :  *              These are all named "has_function_privilege" at the SQL level.
    3380                 :             :  *              They take various combinations of function name, function OID,
    3381                 :             :  *              user name, user OID, or implicit user = current_user.
    3382                 :             :  *
    3383                 :             :  *              The result is a boolean value: true if user has the indicated
    3384                 :             :  *              privilege, false if not, or NULL if object doesn't exist.
    3385                 :             :  */
    3386                 :             : 
    3387                 :             : /*
    3388                 :             :  * has_function_privilege_name_name
    3389                 :             :  *              Check user privileges on a function given
    3390                 :             :  *              name username, text functionname, and text priv name.
    3391                 :             :  */
    3392                 :             : Datum
    3393                 :          26 : has_function_privilege_name_name(PG_FUNCTION_ARGS)
    3394                 :             : {
    3395                 :          26 :         Name            username = PG_GETARG_NAME(0);
    3396                 :          26 :         text       *functionname = PG_GETARG_TEXT_PP(1);
    3397                 :          26 :         text       *priv_type_text = PG_GETARG_TEXT_PP(2);
    3398                 :          26 :         Oid                     roleid;
    3399                 :          26 :         Oid                     functionoid;
    3400                 :          26 :         AclMode         mode;
    3401                 :          26 :         AclResult       aclresult;
    3402                 :             : 
    3403                 :          26 :         roleid = get_role_oid_or_public(NameStr(*username));
    3404                 :          26 :         functionoid = convert_function_name(functionname);
    3405                 :          26 :         mode = convert_function_priv_string(priv_type_text);
    3406                 :             : 
    3407                 :          26 :         aclresult = object_aclcheck(ProcedureRelationId, functionoid, roleid, mode);
    3408                 :             : 
    3409                 :          52 :         PG_RETURN_BOOL(aclresult == ACLCHECK_OK);
    3410                 :          26 : }
    3411                 :             : 
    3412                 :             : /*
    3413                 :             :  * has_function_privilege_name
    3414                 :             :  *              Check user privileges on a function given
    3415                 :             :  *              text functionname and text priv name.
    3416                 :             :  *              current_user is assumed
    3417                 :             :  */
    3418                 :             : Datum
    3419                 :           0 : has_function_privilege_name(PG_FUNCTION_ARGS)
    3420                 :             : {
    3421                 :           0 :         text       *functionname = PG_GETARG_TEXT_PP(0);
    3422                 :           0 :         text       *priv_type_text = PG_GETARG_TEXT_PP(1);
    3423                 :           0 :         Oid                     roleid;
    3424                 :           0 :         Oid                     functionoid;
    3425                 :           0 :         AclMode         mode;
    3426                 :           0 :         AclResult       aclresult;
    3427                 :             : 
    3428                 :           0 :         roleid = GetUserId();
    3429                 :           0 :         functionoid = convert_function_name(functionname);
    3430                 :           0 :         mode = convert_function_priv_string(priv_type_text);
    3431                 :             : 
    3432                 :           0 :         aclresult = object_aclcheck(ProcedureRelationId, functionoid, roleid, mode);
    3433                 :             : 
    3434                 :           0 :         PG_RETURN_BOOL(aclresult == ACLCHECK_OK);
    3435                 :           0 : }
    3436                 :             : 
    3437                 :             : /*
    3438                 :             :  * has_function_privilege_name_id
    3439                 :             :  *              Check user privileges on a function given
    3440                 :             :  *              name usename, function oid, and text priv name.
    3441                 :             :  */
    3442                 :             : Datum
    3443                 :           0 : has_function_privilege_name_id(PG_FUNCTION_ARGS)
    3444                 :             : {
    3445                 :           0 :         Name            username = PG_GETARG_NAME(0);
    3446                 :           0 :         Oid                     functionoid = PG_GETARG_OID(1);
    3447                 :           0 :         text       *priv_type_text = PG_GETARG_TEXT_PP(2);
    3448                 :           0 :         Oid                     roleid;
    3449                 :           0 :         AclMode         mode;
    3450                 :           0 :         AclResult       aclresult;
    3451                 :           0 :         bool            is_missing = false;
    3452                 :             : 
    3453                 :           0 :         roleid = get_role_oid_or_public(NameStr(*username));
    3454                 :           0 :         mode = convert_function_priv_string(priv_type_text);
    3455                 :             : 
    3456                 :           0 :         aclresult = object_aclcheck_ext(ProcedureRelationId, functionoid,
    3457                 :           0 :                                                                         roleid, mode,
    3458                 :             :                                                                         &is_missing);
    3459                 :             : 
    3460         [ #  # ]:           0 :         if (is_missing)
    3461                 :           0 :                 PG_RETURN_NULL();
    3462                 :             : 
    3463                 :           0 :         PG_RETURN_BOOL(aclresult == ACLCHECK_OK);
    3464                 :           0 : }
    3465                 :             : 
    3466                 :             : /*
    3467                 :             :  * has_function_privilege_id
    3468                 :             :  *              Check user privileges on a function given
    3469                 :             :  *              function oid, and text priv name.
    3470                 :             :  *              current_user is assumed
    3471                 :             :  */
    3472                 :             : Datum
    3473                 :           0 : has_function_privilege_id(PG_FUNCTION_ARGS)
    3474                 :             : {
    3475                 :           0 :         Oid                     functionoid = PG_GETARG_OID(0);
    3476                 :           0 :         text       *priv_type_text = PG_GETARG_TEXT_PP(1);
    3477                 :           0 :         Oid                     roleid;
    3478                 :           0 :         AclMode         mode;
    3479                 :           0 :         AclResult       aclresult;
    3480                 :           0 :         bool            is_missing = false;
    3481                 :             : 
    3482                 :           0 :         roleid = GetUserId();
    3483                 :           0 :         mode = convert_function_priv_string(priv_type_text);
    3484                 :             : 
    3485                 :           0 :         aclresult = object_aclcheck_ext(ProcedureRelationId, functionoid,
    3486                 :           0 :                                                                         roleid, mode,
    3487                 :             :                                                                         &is_missing);
    3488                 :             : 
    3489         [ #  # ]:           0 :         if (is_missing)
    3490                 :           0 :                 PG_RETURN_NULL();
    3491                 :             : 
    3492                 :           0 :         PG_RETURN_BOOL(aclresult == ACLCHECK_OK);
    3493                 :           0 : }
    3494                 :             : 
    3495                 :             : /*
    3496                 :             :  * has_function_privilege_id_name
    3497                 :             :  *              Check user privileges on a function given
    3498                 :             :  *              roleid, text functionname, and text priv name.
    3499                 :             :  */
    3500                 :             : Datum
    3501                 :           0 : has_function_privilege_id_name(PG_FUNCTION_ARGS)
    3502                 :             : {
    3503                 :           0 :         Oid                     roleid = PG_GETARG_OID(0);
    3504                 :           0 :         text       *functionname = PG_GETARG_TEXT_PP(1);
    3505                 :           0 :         text       *priv_type_text = PG_GETARG_TEXT_PP(2);
    3506                 :           0 :         Oid                     functionoid;
    3507                 :           0 :         AclMode         mode;
    3508                 :           0 :         AclResult       aclresult;
    3509                 :             : 
    3510                 :           0 :         functionoid = convert_function_name(functionname);
    3511                 :           0 :         mode = convert_function_priv_string(priv_type_text);
    3512                 :             : 
    3513                 :           0 :         aclresult = object_aclcheck(ProcedureRelationId, functionoid, roleid, mode);
    3514                 :             : 
    3515                 :           0 :         PG_RETURN_BOOL(aclresult == ACLCHECK_OK);
    3516                 :           0 : }
    3517                 :             : 
    3518                 :             : /*
    3519                 :             :  * has_function_privilege_id_id
    3520                 :             :  *              Check user privileges on a function given
    3521                 :             :  *              roleid, function oid, and text priv name.
    3522                 :             :  */
    3523                 :             : Datum
    3524                 :           0 : has_function_privilege_id_id(PG_FUNCTION_ARGS)
    3525                 :             : {
    3526                 :           0 :         Oid                     roleid = PG_GETARG_OID(0);
    3527                 :           0 :         Oid                     functionoid = PG_GETARG_OID(1);
    3528                 :           0 :         text       *priv_type_text = PG_GETARG_TEXT_PP(2);
    3529                 :           0 :         AclMode         mode;
    3530                 :           0 :         AclResult       aclresult;
    3531                 :           0 :         bool            is_missing = false;
    3532                 :             : 
    3533                 :           0 :         mode = convert_function_priv_string(priv_type_text);
    3534                 :             : 
    3535                 :           0 :         aclresult = object_aclcheck_ext(ProcedureRelationId, functionoid,
    3536                 :           0 :                                                                         roleid, mode,
    3537                 :             :                                                                         &is_missing);
    3538                 :             : 
    3539         [ #  # ]:           0 :         if (is_missing)
    3540                 :           0 :                 PG_RETURN_NULL();
    3541                 :             : 
    3542                 :           0 :         PG_RETURN_BOOL(aclresult == ACLCHECK_OK);
    3543                 :           0 : }
    3544                 :             : 
    3545                 :             : /*
    3546                 :             :  *              Support routines for has_function_privilege family.
    3547                 :             :  */
    3548                 :             : 
    3549                 :             : /*
    3550                 :             :  * Given a function name expressed as a string, look it up and return Oid
    3551                 :             :  */
    3552                 :             : static Oid
    3553                 :          26 : convert_function_name(text *functionname)
    3554                 :             : {
    3555                 :          26 :         char       *funcname = text_to_cstring(functionname);
    3556                 :          26 :         Oid                     oid;
    3557                 :             : 
    3558                 :          26 :         oid = DatumGetObjectId(DirectFunctionCall1(regprocedurein,
    3559                 :             :                                                                                            CStringGetDatum(funcname)));
    3560                 :             : 
    3561         [ +  - ]:          26 :         if (!OidIsValid(oid))
    3562   [ #  #  #  # ]:           0 :                 ereport(ERROR,
    3563                 :             :                                 (errcode(ERRCODE_UNDEFINED_FUNCTION),
    3564                 :             :                                  errmsg("function \"%s\" does not exist", funcname)));
    3565                 :             : 
    3566                 :          52 :         return oid;
    3567                 :          26 : }
    3568                 :             : 
    3569                 :             : /*
    3570                 :             :  * convert_function_priv_string
    3571                 :             :  *              Convert text string to AclMode value.
    3572                 :             :  */
    3573                 :             : static AclMode
    3574                 :          26 : convert_function_priv_string(text *priv_type_text)
    3575                 :             : {
    3576                 :             :         static const priv_map function_priv_map[] = {
    3577                 :             :                 {"EXECUTE", ACL_EXECUTE},
    3578                 :             :                 {"EXECUTE WITH GRANT OPTION", ACL_GRANT_OPTION_FOR(ACL_EXECUTE)},
    3579                 :             :                 {NULL, 0}
    3580                 :             :         };
    3581                 :             : 
    3582                 :          26 :         return convert_any_priv_string(priv_type_text, function_priv_map);
    3583                 :             : }
    3584                 :             : 
    3585                 :             : 
    3586                 :             : /*
    3587                 :             :  * has_language_privilege variants
    3588                 :             :  *              These are all named "has_language_privilege" at the SQL level.
    3589                 :             :  *              They take various combinations of language name, language OID,
    3590                 :             :  *              user name, user OID, or implicit user = current_user.
    3591                 :             :  *
    3592                 :             :  *              The result is a boolean value: true if user has the indicated
    3593                 :             :  *              privilege, false if not, or NULL if object doesn't exist.
    3594                 :             :  */
    3595                 :             : 
    3596                 :             : /*
    3597                 :             :  * has_language_privilege_name_name
    3598                 :             :  *              Check user privileges on a language given
    3599                 :             :  *              name username, text languagename, and text priv name.
    3600                 :             :  */
    3601                 :             : Datum
    3602                 :           0 : has_language_privilege_name_name(PG_FUNCTION_ARGS)
    3603                 :             : {
    3604                 :           0 :         Name            username = PG_GETARG_NAME(0);
    3605                 :           0 :         text       *languagename = PG_GETARG_TEXT_PP(1);
    3606                 :           0 :         text       *priv_type_text = PG_GETARG_TEXT_PP(2);
    3607                 :           0 :         Oid                     roleid;
    3608                 :           0 :         Oid                     languageoid;
    3609                 :           0 :         AclMode         mode;
    3610                 :           0 :         AclResult       aclresult;
    3611                 :             : 
    3612                 :           0 :         roleid = get_role_oid_or_public(NameStr(*username));
    3613                 :           0 :         languageoid = convert_language_name(languagename);
    3614                 :           0 :         mode = convert_language_priv_string(priv_type_text);
    3615                 :             : 
    3616                 :           0 :         aclresult = object_aclcheck(LanguageRelationId, languageoid, roleid, mode);
    3617                 :             : 
    3618                 :           0 :         PG_RETURN_BOOL(aclresult == ACLCHECK_OK);
    3619                 :           0 : }
    3620                 :             : 
    3621                 :             : /*
    3622                 :             :  * has_language_privilege_name
    3623                 :             :  *              Check user privileges on a language given
    3624                 :             :  *              text languagename and text priv name.
    3625                 :             :  *              current_user is assumed
    3626                 :             :  */
    3627                 :             : Datum
    3628                 :           0 : has_language_privilege_name(PG_FUNCTION_ARGS)
    3629                 :             : {
    3630                 :           0 :         text       *languagename = PG_GETARG_TEXT_PP(0);
    3631                 :           0 :         text       *priv_type_text = PG_GETARG_TEXT_PP(1);
    3632                 :           0 :         Oid                     roleid;
    3633                 :           0 :         Oid                     languageoid;
    3634                 :           0 :         AclMode         mode;
    3635                 :           0 :         AclResult       aclresult;
    3636                 :             : 
    3637                 :           0 :         roleid = GetUserId();
    3638                 :           0 :         languageoid = convert_language_name(languagename);
    3639                 :           0 :         mode = convert_language_priv_string(priv_type_text);
    3640                 :             : 
    3641                 :           0 :         aclresult = object_aclcheck(LanguageRelationId, languageoid, roleid, mode);
    3642                 :             : 
    3643                 :           0 :         PG_RETURN_BOOL(aclresult == ACLCHECK_OK);
    3644                 :           0 : }
    3645                 :             : 
    3646                 :             : /*
    3647                 :             :  * has_language_privilege_name_id
    3648                 :             :  *              Check user privileges on a language given
    3649                 :             :  *              name usename, language oid, and text priv name.
    3650                 :             :  */
    3651                 :             : Datum
    3652                 :           0 : has_language_privilege_name_id(PG_FUNCTION_ARGS)
    3653                 :             : {
    3654                 :           0 :         Name            username = PG_GETARG_NAME(0);
    3655                 :           0 :         Oid                     languageoid = PG_GETARG_OID(1);
    3656                 :           0 :         text       *priv_type_text = PG_GETARG_TEXT_PP(2);
    3657                 :           0 :         Oid                     roleid;
    3658                 :           0 :         AclMode         mode;
    3659                 :           0 :         AclResult       aclresult;
    3660                 :           0 :         bool            is_missing = false;
    3661                 :             : 
    3662                 :           0 :         roleid = get_role_oid_or_public(NameStr(*username));
    3663                 :           0 :         mode = convert_language_priv_string(priv_type_text);
    3664                 :             : 
    3665                 :           0 :         aclresult = object_aclcheck_ext(LanguageRelationId, languageoid,
    3666                 :           0 :                                                                         roleid, mode,
    3667                 :             :                                                                         &is_missing);
    3668                 :             : 
    3669         [ #  # ]:           0 :         if (is_missing)
    3670                 :           0 :                 PG_RETURN_NULL();
    3671                 :             : 
    3672                 :           0 :         PG_RETURN_BOOL(aclresult == ACLCHECK_OK);
    3673                 :           0 : }
    3674                 :             : 
    3675                 :             : /*
    3676                 :             :  * has_language_privilege_id
    3677                 :             :  *              Check user privileges on a language given
    3678                 :             :  *              language oid, and text priv name.
    3679                 :             :  *              current_user is assumed
    3680                 :             :  */
    3681                 :             : Datum
    3682                 :           0 : has_language_privilege_id(PG_FUNCTION_ARGS)
    3683                 :             : {
    3684                 :           0 :         Oid                     languageoid = PG_GETARG_OID(0);
    3685                 :           0 :         text       *priv_type_text = PG_GETARG_TEXT_PP(1);
    3686                 :           0 :         Oid                     roleid;
    3687                 :           0 :         AclMode         mode;
    3688                 :           0 :         AclResult       aclresult;
    3689                 :           0 :         bool            is_missing = false;
    3690                 :             : 
    3691                 :           0 :         roleid = GetUserId();
    3692                 :           0 :         mode = convert_language_priv_string(priv_type_text);
    3693                 :             : 
    3694                 :           0 :         aclresult = object_aclcheck_ext(LanguageRelationId, languageoid,
    3695                 :           0 :                                                                         roleid, mode,
    3696                 :             :                                                                         &is_missing);
    3697                 :             : 
    3698         [ #  # ]:           0 :         if (is_missing)
    3699                 :           0 :                 PG_RETURN_NULL();
    3700                 :             : 
    3701                 :           0 :         PG_RETURN_BOOL(aclresult == ACLCHECK_OK);
    3702                 :           0 : }
    3703                 :             : 
    3704                 :             : /*
    3705                 :             :  * has_language_privilege_id_name
    3706                 :             :  *              Check user privileges on a language given
    3707                 :             :  *              roleid, text languagename, and text priv name.
    3708                 :             :  */
    3709                 :             : Datum
    3710                 :           0 : has_language_privilege_id_name(PG_FUNCTION_ARGS)
    3711                 :             : {
    3712                 :           0 :         Oid                     roleid = PG_GETARG_OID(0);
    3713                 :           0 :         text       *languagename = PG_GETARG_TEXT_PP(1);
    3714                 :           0 :         text       *priv_type_text = PG_GETARG_TEXT_PP(2);
    3715                 :           0 :         Oid                     languageoid;
    3716                 :           0 :         AclMode         mode;
    3717                 :           0 :         AclResult       aclresult;
    3718                 :             : 
    3719                 :           0 :         languageoid = convert_language_name(languagename);
    3720                 :           0 :         mode = convert_language_priv_string(priv_type_text);
    3721                 :             : 
    3722                 :           0 :         aclresult = object_aclcheck(LanguageRelationId, languageoid, roleid, mode);
    3723                 :             : 
    3724                 :           0 :         PG_RETURN_BOOL(aclresult == ACLCHECK_OK);
    3725                 :           0 : }
    3726                 :             : 
    3727                 :             : /*
    3728                 :             :  * has_language_privilege_id_id
    3729                 :             :  *              Check user privileges on a language given
    3730                 :             :  *              roleid, language oid, and text priv name.
    3731                 :             :  */
    3732                 :             : Datum
    3733                 :           0 : has_language_privilege_id_id(PG_FUNCTION_ARGS)
    3734                 :             : {
    3735                 :           0 :         Oid                     roleid = PG_GETARG_OID(0);
    3736                 :           0 :         Oid                     languageoid = PG_GETARG_OID(1);
    3737                 :           0 :         text       *priv_type_text = PG_GETARG_TEXT_PP(2);
    3738                 :           0 :         AclMode         mode;
    3739                 :           0 :         AclResult       aclresult;
    3740                 :           0 :         bool            is_missing = false;
    3741                 :             : 
    3742                 :           0 :         mode = convert_language_priv_string(priv_type_text);
    3743                 :             : 
    3744                 :           0 :         aclresult = object_aclcheck_ext(LanguageRelationId, languageoid,
    3745                 :           0 :                                                                         roleid, mode,
    3746                 :             :                                                                         &is_missing);
    3747                 :             : 
    3748         [ #  # ]:           0 :         if (is_missing)
    3749                 :           0 :                 PG_RETURN_NULL();
    3750                 :             : 
    3751                 :           0 :         PG_RETURN_BOOL(aclresult == ACLCHECK_OK);
    3752                 :           0 : }
    3753                 :             : 
    3754                 :             : /*
    3755                 :             :  *              Support routines for has_language_privilege family.
    3756                 :             :  */
    3757                 :             : 
    3758                 :             : /*
    3759                 :             :  * Given a language name expressed as a string, look it up and return Oid
    3760                 :             :  */
    3761                 :             : static Oid
    3762                 :           0 : convert_language_name(text *languagename)
    3763                 :             : {
    3764                 :           0 :         char       *langname = text_to_cstring(languagename);
    3765                 :             : 
    3766                 :           0 :         return get_language_oid(langname, false);
    3767                 :           0 : }
    3768                 :             : 
    3769                 :             : /*
    3770                 :             :  * convert_language_priv_string
    3771                 :             :  *              Convert text string to AclMode value.
    3772                 :             :  */
    3773                 :             : static AclMode
    3774                 :           0 : convert_language_priv_string(text *priv_type_text)
    3775                 :             : {
    3776                 :             :         static const priv_map language_priv_map[] = {
    3777                 :             :                 {"USAGE", ACL_USAGE},
    3778                 :             :                 {"USAGE WITH GRANT OPTION", ACL_GRANT_OPTION_FOR(ACL_USAGE)},
    3779                 :             :                 {NULL, 0}
    3780                 :             :         };
    3781                 :             : 
    3782                 :           0 :         return convert_any_priv_string(priv_type_text, language_priv_map);
    3783                 :             : }
    3784                 :             : 
    3785                 :             : 
    3786                 :             : /*
    3787                 :             :  * has_schema_privilege variants
    3788                 :             :  *              These are all named "has_schema_privilege" at the SQL level.
    3789                 :             :  *              They take various combinations of schema name, schema OID,
    3790                 :             :  *              user name, user OID, or implicit user = current_user.
    3791                 :             :  *
    3792                 :             :  *              The result is a boolean value: true if user has the indicated
    3793                 :             :  *              privilege, false if not, or NULL if object doesn't exist.
    3794                 :             :  */
    3795                 :             : 
    3796                 :             : /*
    3797                 :             :  * has_schema_privilege_name_name
    3798                 :             :  *              Check user privileges on a schema given
    3799                 :             :  *              name username, text schemaname, and text priv name.
    3800                 :             :  */
    3801                 :             : Datum
    3802                 :           9 : has_schema_privilege_name_name(PG_FUNCTION_ARGS)
    3803                 :             : {
    3804                 :           9 :         Name            username = PG_GETARG_NAME(0);
    3805                 :           9 :         text       *schemaname = PG_GETARG_TEXT_PP(1);
    3806                 :           9 :         text       *priv_type_text = PG_GETARG_TEXT_PP(2);
    3807                 :           9 :         Oid                     roleid;
    3808                 :           9 :         Oid                     schemaoid;
    3809                 :           9 :         AclMode         mode;
    3810                 :           9 :         AclResult       aclresult;
    3811                 :             : 
    3812                 :           9 :         roleid = get_role_oid_or_public(NameStr(*username));
    3813                 :           9 :         schemaoid = convert_schema_name(schemaname);
    3814                 :           9 :         mode = convert_schema_priv_string(priv_type_text);
    3815                 :             : 
    3816                 :           9 :         aclresult = object_aclcheck(NamespaceRelationId, schemaoid, roleid, mode);
    3817                 :             : 
    3818                 :          18 :         PG_RETURN_BOOL(aclresult == ACLCHECK_OK);
    3819                 :           9 : }
    3820                 :             : 
    3821                 :             : /*
    3822                 :             :  * has_schema_privilege_name
    3823                 :             :  *              Check user privileges on a schema given
    3824                 :             :  *              text schemaname and text priv name.
    3825                 :             :  *              current_user is assumed
    3826                 :             :  */
    3827                 :             : Datum
    3828                 :           0 : has_schema_privilege_name(PG_FUNCTION_ARGS)
    3829                 :             : {
    3830                 :           0 :         text       *schemaname = PG_GETARG_TEXT_PP(0);
    3831                 :           0 :         text       *priv_type_text = PG_GETARG_TEXT_PP(1);
    3832                 :           0 :         Oid                     roleid;
    3833                 :           0 :         Oid                     schemaoid;
    3834                 :           0 :         AclMode         mode;
    3835                 :           0 :         AclResult       aclresult;
    3836                 :             : 
    3837                 :           0 :         roleid = GetUserId();
    3838                 :           0 :         schemaoid = convert_schema_name(schemaname);
    3839                 :           0 :         mode = convert_schema_priv_string(priv_type_text);
    3840                 :             : 
    3841                 :           0 :         aclresult = object_aclcheck(NamespaceRelationId, schemaoid, roleid, mode);
    3842                 :             : 
    3843                 :           0 :         PG_RETURN_BOOL(aclresult == ACLCHECK_OK);
    3844                 :           0 : }
    3845                 :             : 
    3846                 :             : /*
    3847                 :             :  * has_schema_privilege_name_id
    3848                 :             :  *              Check user privileges on a schema given
    3849                 :             :  *              name usename, schema oid, and text priv name.
    3850                 :             :  */
    3851                 :             : Datum
    3852                 :           0 : has_schema_privilege_name_id(PG_FUNCTION_ARGS)
    3853                 :             : {
    3854                 :           0 :         Name            username = PG_GETARG_NAME(0);
    3855                 :           0 :         Oid                     schemaoid = PG_GETARG_OID(1);
    3856                 :           0 :         text       *priv_type_text = PG_GETARG_TEXT_PP(2);
    3857                 :           0 :         Oid                     roleid;
    3858                 :           0 :         AclMode         mode;
    3859                 :           0 :         AclResult       aclresult;
    3860                 :           0 :         bool            is_missing = false;
    3861                 :             : 
    3862                 :           0 :         roleid = get_role_oid_or_public(NameStr(*username));
    3863                 :           0 :         mode = convert_schema_priv_string(priv_type_text);
    3864                 :             : 
    3865                 :           0 :         aclresult = object_aclcheck_ext(NamespaceRelationId, schemaoid,
    3866                 :           0 :                                                                         roleid, mode,
    3867                 :             :                                                                         &is_missing);
    3868                 :             : 
    3869         [ #  # ]:           0 :         if (is_missing)
    3870                 :           0 :                 PG_RETURN_NULL();
    3871                 :             : 
    3872                 :           0 :         PG_RETURN_BOOL(aclresult == ACLCHECK_OK);
    3873                 :           0 : }
    3874                 :             : 
    3875                 :             : /*
    3876                 :             :  * has_schema_privilege_id
    3877                 :             :  *              Check user privileges on a schema given
    3878                 :             :  *              schema oid, and text priv name.
    3879                 :             :  *              current_user is assumed
    3880                 :             :  */
    3881                 :             : Datum
    3882                 :           0 : has_schema_privilege_id(PG_FUNCTION_ARGS)
    3883                 :             : {
    3884                 :           0 :         Oid                     schemaoid = PG_GETARG_OID(0);
    3885                 :           0 :         text       *priv_type_text = PG_GETARG_TEXT_PP(1);
    3886                 :           0 :         Oid                     roleid;
    3887                 :           0 :         AclMode         mode;
    3888                 :           0 :         AclResult       aclresult;
    3889                 :           0 :         bool            is_missing = false;
    3890                 :             : 
    3891                 :           0 :         roleid = GetUserId();
    3892                 :           0 :         mode = convert_schema_priv_string(priv_type_text);
    3893                 :             : 
    3894                 :           0 :         aclresult = object_aclcheck_ext(NamespaceRelationId, schemaoid,
    3895                 :           0 :                                                                         roleid, mode,
    3896                 :             :                                                                         &is_missing);
    3897                 :             : 
    3898         [ #  # ]:           0 :         if (is_missing)
    3899                 :           0 :                 PG_RETURN_NULL();
    3900                 :             : 
    3901                 :           0 :         PG_RETURN_BOOL(aclresult == ACLCHECK_OK);
    3902                 :           0 : }
    3903                 :             : 
    3904                 :             : /*
    3905                 :             :  * has_schema_privilege_id_name
    3906                 :             :  *              Check user privileges on a schema given
    3907                 :             :  *              roleid, text schemaname, and text priv name.
    3908                 :             :  */
    3909                 :             : Datum
    3910                 :           0 : has_schema_privilege_id_name(PG_FUNCTION_ARGS)
    3911                 :             : {
    3912                 :           0 :         Oid                     roleid = PG_GETARG_OID(0);
    3913                 :           0 :         text       *schemaname = PG_GETARG_TEXT_PP(1);
    3914                 :           0 :         text       *priv_type_text = PG_GETARG_TEXT_PP(2);
    3915                 :           0 :         Oid                     schemaoid;
    3916                 :           0 :         AclMode         mode;
    3917                 :           0 :         AclResult       aclresult;
    3918                 :             : 
    3919                 :           0 :         schemaoid = convert_schema_name(schemaname);
    3920                 :           0 :         mode = convert_schema_priv_string(priv_type_text);
    3921                 :             : 
    3922                 :           0 :         aclresult = object_aclcheck(NamespaceRelationId, schemaoid, roleid, mode);
    3923                 :             : 
    3924                 :           0 :         PG_RETURN_BOOL(aclresult == ACLCHECK_OK);
    3925                 :           0 : }
    3926                 :             : 
    3927                 :             : /*
    3928                 :             :  * has_schema_privilege_id_id
    3929                 :             :  *              Check user privileges on a schema given
    3930                 :             :  *              roleid, schema oid, and text priv name.
    3931                 :             :  */
    3932                 :             : Datum
    3933                 :           0 : has_schema_privilege_id_id(PG_FUNCTION_ARGS)
    3934                 :             : {
    3935                 :           0 :         Oid                     roleid = PG_GETARG_OID(0);
    3936                 :           0 :         Oid                     schemaoid = PG_GETARG_OID(1);
    3937                 :           0 :         text       *priv_type_text = PG_GETARG_TEXT_PP(2);
    3938                 :           0 :         AclMode         mode;
    3939                 :           0 :         AclResult       aclresult;
    3940                 :           0 :         bool            is_missing = false;
    3941                 :             : 
    3942                 :           0 :         mode = convert_schema_priv_string(priv_type_text);
    3943                 :             : 
    3944                 :           0 :         aclresult = object_aclcheck_ext(NamespaceRelationId, schemaoid,
    3945                 :           0 :                                                                         roleid, mode,
    3946                 :             :                                                                         &is_missing);
    3947                 :             : 
    3948         [ #  # ]:           0 :         if (is_missing)
    3949                 :           0 :                 PG_RETURN_NULL();
    3950                 :             : 
    3951                 :           0 :         PG_RETURN_BOOL(aclresult == ACLCHECK_OK);
    3952                 :           0 : }
    3953                 :             : 
    3954                 :             : /*
    3955                 :             :  *              Support routines for has_schema_privilege family.
    3956                 :             :  */
    3957                 :             : 
    3958                 :             : /*
    3959                 :             :  * Given a schema name expressed as a string, look it up and return Oid
    3960                 :             :  */
    3961                 :             : static Oid
    3962                 :           9 : convert_schema_name(text *schemaname)
    3963                 :             : {
    3964                 :           9 :         char       *nspname = text_to_cstring(schemaname);
    3965                 :             : 
    3966                 :          18 :         return get_namespace_oid(nspname, false);
    3967                 :           9 : }
    3968                 :             : 
    3969                 :             : /*
    3970                 :             :  * convert_schema_priv_string
    3971                 :             :  *              Convert text string to AclMode value.
    3972                 :             :  */
    3973                 :             : static AclMode
    3974                 :           9 : convert_schema_priv_string(text *priv_type_text)
    3975                 :             : {
    3976                 :             :         static const priv_map schema_priv_map[] = {
    3977                 :             :                 {"CREATE", ACL_CREATE},
    3978                 :             :                 {"CREATE WITH GRANT OPTION", ACL_GRANT_OPTION_FOR(ACL_CREATE)},
    3979                 :             :                 {"USAGE", ACL_USAGE},
    3980                 :             :                 {"USAGE WITH GRANT OPTION", ACL_GRANT_OPTION_FOR(ACL_USAGE)},
    3981                 :             :                 {NULL, 0}
    3982                 :             :         };
    3983                 :             : 
    3984                 :           9 :         return convert_any_priv_string(priv_type_text, schema_priv_map);
    3985                 :             : }
    3986                 :             : 
    3987                 :             : 
    3988                 :             : /*
    3989                 :             :  * has_server_privilege variants
    3990                 :             :  *              These are all named "has_server_privilege" at the SQL level.
    3991                 :             :  *              They take various combinations of foreign server name,
    3992                 :             :  *              server OID, user name, user OID, or implicit user = current_user.
    3993                 :             :  *
    3994                 :             :  *              The result is a boolean value: true if user has the indicated
    3995                 :             :  *              privilege, false if not.
    3996                 :             :  */
    3997                 :             : 
    3998                 :             : /*
    3999                 :             :  * has_server_privilege_name_name
    4000                 :             :  *              Check user privileges on a foreign server given
    4001                 :             :  *              name username, text servername, and text priv name.
    4002                 :             :  */
    4003                 :             : Datum
    4004                 :           2 : has_server_privilege_name_name(PG_FUNCTION_ARGS)
    4005                 :             : {
    4006                 :           2 :         Name            username = PG_GETARG_NAME(0);
    4007                 :           2 :         text       *servername = PG_GETARG_TEXT_PP(1);
    4008                 :           2 :         text       *priv_type_text = PG_GETARG_TEXT_PP(2);
    4009                 :           2 :         Oid                     roleid;
    4010                 :           2 :         Oid                     serverid;
    4011                 :           2 :         AclMode         mode;
    4012                 :           2 :         AclResult       aclresult;
    4013                 :             : 
    4014                 :           2 :         roleid = get_role_oid_or_public(NameStr(*username));
    4015                 :           2 :         serverid = convert_server_name(servername);
    4016                 :           2 :         mode = convert_server_priv_string(priv_type_text);
    4017                 :             : 
    4018                 :           2 :         aclresult = object_aclcheck(ForeignServerRelationId, serverid, roleid, mode);
    4019                 :             : 
    4020                 :           4 :         PG_RETURN_BOOL(aclresult == ACLCHECK_OK);
    4021                 :           2 : }
    4022                 :             : 
    4023                 :             : /*
    4024                 :             :  * has_server_privilege_name
    4025                 :             :  *              Check user privileges on a foreign server given
    4026                 :             :  *              text servername and text priv name.
    4027                 :             :  *              current_user is assumed
    4028                 :             :  */
    4029                 :             : Datum
    4030                 :           1 : has_server_privilege_name(PG_FUNCTION_ARGS)
    4031                 :             : {
    4032                 :           1 :         text       *servername = PG_GETARG_TEXT_PP(0);
    4033                 :           1 :         text       *priv_type_text = PG_GETARG_TEXT_PP(1);
    4034                 :           1 :         Oid                     roleid;
    4035                 :           1 :         Oid                     serverid;
    4036                 :           1 :         AclMode         mode;
    4037                 :           1 :         AclResult       aclresult;
    4038                 :             : 
    4039                 :           1 :         roleid = GetUserId();
    4040                 :           1 :         serverid = convert_server_name(servername);
    4041                 :           1 :         mode = convert_server_priv_string(priv_type_text);
    4042                 :             : 
    4043                 :           1 :         aclresult = object_aclcheck(ForeignServerRelationId, serverid, roleid, mode);
    4044                 :             : 
    4045                 :           2 :         PG_RETURN_BOOL(aclresult == ACLCHECK_OK);
    4046                 :           1 : }
    4047                 :             : 
    4048                 :             : /*
    4049                 :             :  * has_server_privilege_name_id
    4050                 :             :  *              Check user privileges on a foreign server given
    4051                 :             :  *              name usename, foreign server oid, and text priv name.
    4052                 :             :  */
    4053                 :             : Datum
    4054                 :           1 : has_server_privilege_name_id(PG_FUNCTION_ARGS)
    4055                 :             : {
    4056                 :           1 :         Name            username = PG_GETARG_NAME(0);
    4057                 :           1 :         Oid                     serverid = PG_GETARG_OID(1);
    4058                 :           1 :         text       *priv_type_text = PG_GETARG_TEXT_PP(2);
    4059                 :           1 :         Oid                     roleid;
    4060                 :           1 :         AclMode         mode;
    4061                 :           1 :         AclResult       aclresult;
    4062                 :           1 :         bool            is_missing = false;
    4063                 :             : 
    4064                 :           1 :         roleid = get_role_oid_or_public(NameStr(*username));
    4065                 :           1 :         mode = convert_server_priv_string(priv_type_text);
    4066                 :             : 
    4067                 :           2 :         aclresult = object_aclcheck_ext(ForeignServerRelationId, serverid,
    4068                 :           1 :                                                                         roleid, mode,
    4069                 :             :                                                                         &is_missing);
    4070                 :             : 
    4071         [ +  - ]:           1 :         if (is_missing)
    4072                 :           0 :                 PG_RETURN_NULL();
    4073                 :             : 
    4074                 :           1 :         PG_RETURN_BOOL(aclresult == ACLCHECK_OK);
    4075                 :           1 : }
    4076                 :             : 
    4077                 :             : /*
    4078                 :             :  * has_server_privilege_id
    4079                 :             :  *              Check user privileges on a foreign server given
    4080                 :             :  *              server oid, and text priv name.
    4081                 :             :  *              current_user is assumed
    4082                 :             :  */
    4083                 :             : Datum
    4084                 :          13 : has_server_privilege_id(PG_FUNCTION_ARGS)
    4085                 :             : {
    4086                 :          13 :         Oid                     serverid = PG_GETARG_OID(0);
    4087                 :          13 :         text       *priv_type_text = PG_GETARG_TEXT_PP(1);
    4088                 :          13 :         Oid                     roleid;
    4089                 :          13 :         AclMode         mode;
    4090                 :          13 :         AclResult       aclresult;
    4091                 :          13 :         bool            is_missing = false;
    4092                 :             : 
    4093                 :          13 :         roleid = GetUserId();
    4094                 :          13 :         mode = convert_server_priv_string(priv_type_text);
    4095                 :             : 
    4096                 :          26 :         aclresult = object_aclcheck_ext(ForeignServerRelationId, serverid,
    4097                 :          13 :                                                                         roleid, mode,
    4098                 :             :                                                                         &is_missing);
    4099                 :             : 
    4100         [ +  - ]:          13 :         if (is_missing)
    4101                 :           0 :                 PG_RETURN_NULL();
    4102                 :             : 
    4103                 :          13 :         PG_RETURN_BOOL(aclresult == ACLCHECK_OK);
    4104                 :          13 : }
    4105                 :             : 
    4106                 :             : /*
    4107                 :             :  * has_server_privilege_id_name
    4108                 :             :  *              Check user privileges on a foreign server given
    4109                 :             :  *              roleid, text servername, and text priv name.
    4110                 :             :  */
    4111                 :             : Datum
    4112                 :           1 : has_server_privilege_id_name(PG_FUNCTION_ARGS)
    4113                 :             : {
    4114                 :           1 :         Oid                     roleid = PG_GETARG_OID(0);
    4115                 :           1 :         text       *servername = PG_GETARG_TEXT_PP(1);
    4116                 :           1 :         text       *priv_type_text = PG_GETARG_TEXT_PP(2);
    4117                 :           1 :         Oid                     serverid;
    4118                 :           1 :         AclMode         mode;
    4119                 :           1 :         AclResult       aclresult;
    4120                 :             : 
    4121                 :           1 :         serverid = convert_server_name(servername);
    4122                 :           1 :         mode = convert_server_priv_string(priv_type_text);
    4123                 :             : 
    4124                 :           1 :         aclresult = object_aclcheck(ForeignServerRelationId, serverid, roleid, mode);
    4125                 :             : 
    4126                 :           2 :         PG_RETURN_BOOL(aclresult == ACLCHECK_OK);
    4127                 :           1 : }
    4128                 :             : 
    4129                 :             : /*
    4130                 :             :  * has_server_privilege_id_id
    4131                 :             :  *              Check user privileges on a foreign server given
    4132                 :             :  *              roleid, server oid, and text priv name.
    4133                 :             :  */
    4134                 :             : Datum
    4135                 :           1 : has_server_privilege_id_id(PG_FUNCTION_ARGS)
    4136                 :             : {
    4137                 :           1 :         Oid                     roleid = PG_GETARG_OID(0);
    4138                 :           1 :         Oid                     serverid = PG_GETARG_OID(1);
    4139                 :           1 :         text       *priv_type_text = PG_GETARG_TEXT_PP(2);
    4140                 :           1 :         AclMode         mode;
    4141                 :           1 :         AclResult       aclresult;
    4142                 :           1 :         bool            is_missing = false;
    4143                 :             : 
    4144                 :           1 :         mode = convert_server_priv_string(priv_type_text);
    4145                 :             : 
    4146                 :           2 :         aclresult = object_aclcheck_ext(ForeignServerRelationId, serverid,
    4147                 :           1 :                                                                         roleid, mode,
    4148                 :             :                                                                         &is_missing);
    4149                 :             : 
    4150         [ +  - ]:           1 :         if (is_missing)
    4151                 :           0 :                 PG_RETURN_NULL();
    4152                 :             : 
    4153                 :           1 :         PG_RETURN_BOOL(aclresult == ACLCHECK_OK);
    4154                 :           1 : }
    4155                 :             : 
    4156                 :             : /*
    4157                 :             :  *              Support routines for has_server_privilege family.
    4158                 :             :  */
    4159                 :             : 
    4160                 :             : /*
    4161                 :             :  * Given a server name expressed as a string, look it up and return Oid
    4162                 :             :  */
    4163                 :             : static Oid
    4164                 :           4 : convert_server_name(text *servername)
    4165                 :             : {
    4166                 :           4 :         char       *serverstr = text_to_cstring(servername);
    4167                 :             : 
    4168                 :           8 :         return get_foreign_server_oid(serverstr, false);
    4169                 :           4 : }
    4170                 :             : 
    4171                 :             : /*
    4172                 :             :  * convert_server_priv_string
    4173                 :             :  *              Convert text string to AclMode value.
    4174                 :             :  */
    4175                 :             : static AclMode
    4176                 :          19 : convert_server_priv_string(text *priv_type_text)
    4177                 :             : {
    4178                 :             :         static const priv_map server_priv_map[] = {
    4179                 :             :                 {"USAGE", ACL_USAGE},
    4180                 :             :                 {"USAGE WITH GRANT OPTION", ACL_GRANT_OPTION_FOR(ACL_USAGE)},
    4181                 :             :                 {NULL, 0}
    4182                 :             :         };
    4183                 :             : 
    4184                 :          19 :         return convert_any_priv_string(priv_type_text, server_priv_map);
    4185                 :             : }
    4186                 :             : 
    4187                 :             : 
    4188                 :             : /*
    4189                 :             :  * has_tablespace_privilege variants
    4190                 :             :  *              These are all named "has_tablespace_privilege" at the SQL level.
    4191                 :             :  *              They take various combinations of tablespace name, tablespace OID,
    4192                 :             :  *              user name, user OID, or implicit user = current_user.
    4193                 :             :  *
    4194                 :             :  *              The result is a boolean value: true if user has the indicated
    4195                 :             :  *              privilege, false if not.
    4196                 :             :  */
    4197                 :             : 
    4198                 :             : /*
    4199                 :             :  * has_tablespace_privilege_name_name
    4200                 :             :  *              Check user privileges on a tablespace given
    4201                 :             :  *              name username, text tablespacename, and text priv name.
    4202                 :             :  */
    4203                 :             : Datum
    4204                 :           0 : has_tablespace_privilege_name_name(PG_FUNCTION_ARGS)
    4205                 :             : {
    4206                 :           0 :         Name            username = PG_GETARG_NAME(0);
    4207                 :           0 :         text       *tablespacename = PG_GETARG_TEXT_PP(1);
    4208                 :           0 :         text       *priv_type_text = PG_GETARG_TEXT_PP(2);
    4209                 :           0 :         Oid                     roleid;
    4210                 :           0 :         Oid                     tablespaceoid;
    4211                 :           0 :         AclMode         mode;
    4212                 :           0 :         AclResult       aclresult;
    4213                 :             : 
    4214                 :           0 :         roleid = get_role_oid_or_public(NameStr(*username));
    4215                 :           0 :         tablespaceoid = convert_tablespace_name(tablespacename);
    4216                 :           0 :         mode = convert_tablespace_priv_string(priv_type_text);
    4217                 :             : 
    4218                 :           0 :         aclresult = object_aclcheck(TableSpaceRelationId, tablespaceoid, roleid, mode);
    4219                 :             : 
    4220                 :           0 :         PG_RETURN_BOOL(aclresult == ACLCHECK_OK);
    4221                 :           0 : }
    4222                 :             : 
    4223                 :             : /*
    4224                 :             :  * has_tablespace_privilege_name
    4225                 :             :  *              Check user privileges on a tablespace given
    4226                 :             :  *              text tablespacename and text priv name.
    4227                 :             :  *              current_user is assumed
    4228                 :             :  */
    4229                 :             : Datum
    4230                 :           0 : has_tablespace_privilege_name(PG_FUNCTION_ARGS)
    4231                 :             : {
    4232                 :           0 :         text       *tablespacename = PG_GETARG_TEXT_PP(0);
    4233                 :           0 :         text       *priv_type_text = PG_GETARG_TEXT_PP(1);
    4234                 :           0 :         Oid                     roleid;
    4235                 :           0 :         Oid                     tablespaceoid;
    4236                 :           0 :         AclMode         mode;
    4237                 :           0 :         AclResult       aclresult;
    4238                 :             : 
    4239                 :           0 :         roleid = GetUserId();
    4240                 :           0 :         tablespaceoid = convert_tablespace_name(tablespacename);
    4241                 :           0 :         mode = convert_tablespace_priv_string(priv_type_text);
    4242                 :             : 
    4243                 :           0 :         aclresult = object_aclcheck(TableSpaceRelationId, tablespaceoid, roleid, mode);
    4244                 :             : 
    4245                 :           0 :         PG_RETURN_BOOL(aclresult == ACLCHECK_OK);
    4246                 :           0 : }
    4247                 :             : 
    4248                 :             : /*
    4249                 :             :  * has_tablespace_privilege_name_id
    4250                 :             :  *              Check user privileges on a tablespace given
    4251                 :             :  *              name usename, tablespace oid, and text priv name.
    4252                 :             :  */
    4253                 :             : Datum
    4254                 :           0 : has_tablespace_privilege_name_id(PG_FUNCTION_ARGS)
    4255                 :             : {
    4256                 :           0 :         Name            username = PG_GETARG_NAME(0);
    4257                 :           0 :         Oid                     tablespaceoid = PG_GETARG_OID(1);
    4258                 :           0 :         text       *priv_type_text = PG_GETARG_TEXT_PP(2);
    4259                 :           0 :         Oid                     roleid;
    4260                 :           0 :         AclMode         mode;
    4261                 :           0 :         AclResult       aclresult;
    4262                 :           0 :         bool            is_missing = false;
    4263                 :             : 
    4264                 :           0 :         roleid = get_role_oid_or_public(NameStr(*username));
    4265                 :           0 :         mode = convert_tablespace_priv_string(priv_type_text);
    4266                 :             : 
    4267                 :           0 :         aclresult = object_aclcheck_ext(TableSpaceRelationId, tablespaceoid,
    4268                 :           0 :                                                                         roleid, mode,
    4269                 :             :                                                                         &is_missing);
    4270                 :             : 
    4271         [ #  # ]:           0 :         if (is_missing)
    4272                 :           0 :                 PG_RETURN_NULL();
    4273                 :             : 
    4274                 :           0 :         PG_RETURN_BOOL(aclresult == ACLCHECK_OK);
    4275                 :           0 : }
    4276                 :             : 
    4277                 :             : /*
    4278                 :             :  * has_tablespace_privilege_id
    4279                 :             :  *              Check user privileges on a tablespace given
    4280                 :             :  *              tablespace oid, and text priv name.
    4281                 :             :  *              current_user is assumed
    4282                 :             :  */
    4283                 :             : Datum
    4284                 :           0 : has_tablespace_privilege_id(PG_FUNCTION_ARGS)
    4285                 :             : {
    4286                 :           0 :         Oid                     tablespaceoid = PG_GETARG_OID(0);
    4287                 :           0 :         text       *priv_type_text = PG_GETARG_TEXT_PP(1);
    4288                 :           0 :         Oid                     roleid;
    4289                 :           0 :         AclMode         mode;
    4290                 :           0 :         AclResult       aclresult;
    4291                 :           0 :         bool            is_missing = false;
    4292                 :             : 
    4293                 :           0 :         roleid = GetUserId();
    4294                 :           0 :         mode = convert_tablespace_priv_string(priv_type_text);
    4295                 :             : 
    4296                 :           0 :         aclresult = object_aclcheck_ext(TableSpaceRelationId, tablespaceoid,
    4297                 :           0 :                                                                         roleid, mode,
    4298                 :             :                                                                         &is_missing);
    4299                 :             : 
    4300         [ #  # ]:           0 :         if (is_missing)
    4301                 :           0 :                 PG_RETURN_NULL();
    4302                 :             : 
    4303                 :           0 :         PG_RETURN_BOOL(aclresult == ACLCHECK_OK);
    4304                 :           0 : }
    4305                 :             : 
    4306                 :             : /*
    4307                 :             :  * has_tablespace_privilege_id_name
    4308                 :             :  *              Check user privileges on a tablespace given
    4309                 :             :  *              roleid, text tablespacename, and text priv name.
    4310                 :             :  */
    4311                 :             : Datum
    4312                 :           0 : has_tablespace_privilege_id_name(PG_FUNCTION_ARGS)
    4313                 :             : {
    4314                 :           0 :         Oid                     roleid = PG_GETARG_OID(0);
    4315                 :           0 :         text       *tablespacename = PG_GETARG_TEXT_PP(1);
    4316                 :           0 :         text       *priv_type_text = PG_GETARG_TEXT_PP(2);
    4317                 :           0 :         Oid                     tablespaceoid;
    4318                 :           0 :         AclMode         mode;
    4319                 :           0 :         AclResult       aclresult;
    4320                 :             : 
    4321                 :           0 :         tablespaceoid = convert_tablespace_name(tablespacename);
    4322                 :           0 :         mode = convert_tablespace_priv_string(priv_type_text);
    4323                 :             : 
    4324                 :           0 :         aclresult = object_aclcheck(TableSpaceRelationId, tablespaceoid, roleid, mode);
    4325                 :             : 
    4326                 :           0 :         PG_RETURN_BOOL(aclresult == ACLCHECK_OK);
    4327                 :           0 : }
    4328                 :             : 
    4329                 :             : /*
    4330                 :             :  * has_tablespace_privilege_id_id
    4331                 :             :  *              Check user privileges on a tablespace given
    4332                 :             :  *              roleid, tablespace oid, and text priv name.
    4333                 :             :  */
    4334                 :             : Datum
    4335                 :           0 : has_tablespace_privilege_id_id(PG_FUNCTION_ARGS)
    4336                 :             : {
    4337                 :           0 :         Oid                     roleid = PG_GETARG_OID(0);
    4338                 :           0 :         Oid                     tablespaceoid = PG_GETARG_OID(1);
    4339                 :           0 :         text       *priv_type_text = PG_GETARG_TEXT_PP(2);
    4340                 :           0 :         AclMode         mode;
    4341                 :           0 :         AclResult       aclresult;
    4342                 :           0 :         bool            is_missing = false;
    4343                 :             : 
    4344                 :           0 :         mode = convert_tablespace_priv_string(priv_type_text);
    4345                 :             : 
    4346                 :           0 :         aclresult = object_aclcheck_ext(TableSpaceRelationId, tablespaceoid,
    4347                 :           0 :                                                                         roleid, mode,
    4348                 :             :                                                                         &is_missing);
    4349                 :             : 
    4350         [ #  # ]:           0 :         if (is_missing)
    4351                 :           0 :                 PG_RETURN_NULL();
    4352                 :             : 
    4353                 :           0 :         PG_RETURN_BOOL(aclresult == ACLCHECK_OK);
    4354                 :           0 : }
    4355                 :             : 
    4356                 :             : /*
    4357                 :             :  *              Support routines for has_tablespace_privilege family.
    4358                 :             :  */
    4359                 :             : 
    4360                 :             : /*
    4361                 :             :  * Given a tablespace name expressed as a string, look it up and return Oid
    4362                 :             :  */
    4363                 :             : static Oid
    4364                 :           0 : convert_tablespace_name(text *tablespacename)
    4365                 :             : {
    4366                 :           0 :         char       *spcname = text_to_cstring(tablespacename);
    4367                 :             : 
    4368                 :           0 :         return get_tablespace_oid(spcname, false);
    4369                 :           0 : }
    4370                 :             : 
    4371                 :             : /*
    4372                 :             :  * convert_tablespace_priv_string
    4373                 :             :  *              Convert text string to AclMode value.
    4374                 :             :  */
    4375                 :             : static AclMode
    4376                 :           0 : convert_tablespace_priv_string(text *priv_type_text)
    4377                 :             : {
    4378                 :             :         static const priv_map tablespace_priv_map[] = {
    4379                 :             :                 {"CREATE", ACL_CREATE},
    4380                 :             :                 {"CREATE WITH GRANT OPTION", ACL_GRANT_OPTION_FOR(ACL_CREATE)},
    4381                 :             :                 {NULL, 0}
    4382                 :             :         };
    4383                 :             : 
    4384                 :           0 :         return convert_any_priv_string(priv_type_text, tablespace_priv_map);
    4385                 :             : }
    4386                 :             : 
    4387                 :             : /*
    4388                 :             :  * has_type_privilege variants
    4389                 :             :  *              These are all named "has_type_privilege" at the SQL level.
    4390                 :             :  *              They take various combinations of type name, type OID,
    4391                 :             :  *              user name, user OID, or implicit user = current_user.
    4392                 :             :  *
    4393                 :             :  *              The result is a boolean value: true if user has the indicated
    4394                 :             :  *              privilege, false if not, or NULL if object doesn't exist.
    4395                 :             :  */
    4396                 :             : 
    4397                 :             : /*
    4398                 :             :  * has_type_privilege_name_name
    4399                 :             :  *              Check user privileges on a type given
    4400                 :             :  *              name username, text typename, and text priv name.
    4401                 :             :  */
    4402                 :             : Datum
    4403                 :           2 : has_type_privilege_name_name(PG_FUNCTION_ARGS)
    4404                 :             : {
    4405                 :           2 :         Name            username = PG_GETARG_NAME(0);
    4406                 :           2 :         text       *typename = PG_GETARG_TEXT_PP(1);
    4407                 :           2 :         text       *priv_type_text = PG_GETARG_TEXT_PP(2);
    4408                 :           2 :         Oid                     roleid;
    4409                 :           2 :         Oid                     typeoid;
    4410                 :           2 :         AclMode         mode;
    4411                 :           2 :         AclResult       aclresult;
    4412                 :             : 
    4413                 :           2 :         roleid = get_role_oid_or_public(NameStr(*username));
    4414                 :           2 :         typeoid = convert_type_name(typename);
    4415                 :           2 :         mode = convert_type_priv_string(priv_type_text);
    4416                 :             : 
    4417                 :           2 :         aclresult = object_aclcheck(TypeRelationId, typeoid, roleid, mode);
    4418                 :             : 
    4419                 :           4 :         PG_RETURN_BOOL(aclresult == ACLCHECK_OK);
    4420                 :           2 : }
    4421                 :             : 
    4422                 :             : /*
    4423                 :             :  * has_type_privilege_name
    4424                 :             :  *              Check user privileges on a type given
    4425                 :             :  *              text typename and text priv name.
    4426                 :             :  *              current_user is assumed
    4427                 :             :  */
    4428                 :             : Datum
    4429                 :           0 : has_type_privilege_name(PG_FUNCTION_ARGS)
    4430                 :             : {
    4431                 :           0 :         text       *typename = PG_GETARG_TEXT_PP(0);
    4432                 :           0 :         text       *priv_type_text = PG_GETARG_TEXT_PP(1);
    4433                 :           0 :         Oid                     roleid;
    4434                 :           0 :         Oid                     typeoid;
    4435                 :           0 :         AclMode         mode;
    4436                 :           0 :         AclResult       aclresult;
    4437                 :             : 
    4438                 :           0 :         roleid = GetUserId();
    4439                 :           0 :         typeoid = convert_type_name(typename);
    4440                 :           0 :         mode = convert_type_priv_string(priv_type_text);
    4441                 :             : 
    4442                 :           0 :         aclresult = object_aclcheck(TypeRelationId, typeoid, roleid, mode);
    4443                 :             : 
    4444                 :           0 :         PG_RETURN_BOOL(aclresult == ACLCHECK_OK);
    4445                 :           0 : }
    4446                 :             : 
    4447                 :             : /*
    4448                 :             :  * has_type_privilege_name_id
    4449                 :             :  *              Check user privileges on a type given
    4450                 :             :  *              name usename, type oid, and text priv name.
    4451                 :             :  */
    4452                 :             : Datum
    4453                 :           0 : has_type_privilege_name_id(PG_FUNCTION_ARGS)
    4454                 :             : {
    4455                 :           0 :         Name            username = PG_GETARG_NAME(0);
    4456                 :           0 :         Oid                     typeoid = PG_GETARG_OID(1);
    4457                 :           0 :         text       *priv_type_text = PG_GETARG_TEXT_PP(2);
    4458                 :           0 :         Oid                     roleid;
    4459                 :           0 :         AclMode         mode;
    4460                 :           0 :         AclResult       aclresult;
    4461                 :           0 :         bool            is_missing = false;
    4462                 :             : 
    4463                 :           0 :         roleid = get_role_oid_or_public(NameStr(*username));
    4464                 :           0 :         mode = convert_type_priv_string(priv_type_text);
    4465                 :             : 
    4466                 :           0 :         aclresult = object_aclcheck_ext(TypeRelationId, typeoid,
    4467                 :           0 :                                                                         roleid, mode,
    4468                 :             :                                                                         &is_missing);
    4469                 :             : 
    4470         [ #  # ]:           0 :         if (is_missing)
    4471                 :           0 :                 PG_RETURN_NULL();
    4472                 :             : 
    4473                 :           0 :         PG_RETURN_BOOL(aclresult == ACLCHECK_OK);
    4474                 :           0 : }
    4475                 :             : 
    4476                 :             : /*
    4477                 :             :  * has_type_privilege_id
    4478                 :             :  *              Check user privileges on a type given
    4479                 :             :  *              type oid, and text priv name.
    4480                 :             :  *              current_user is assumed
    4481                 :             :  */
    4482                 :             : Datum
    4483                 :           0 : has_type_privilege_id(PG_FUNCTION_ARGS)
    4484                 :             : {
    4485                 :           0 :         Oid                     typeoid = PG_GETARG_OID(0);
    4486                 :           0 :         text       *priv_type_text = PG_GETARG_TEXT_PP(1);
    4487                 :           0 :         Oid                     roleid;
    4488                 :           0 :         AclMode         mode;
    4489                 :           0 :         AclResult       aclresult;
    4490                 :           0 :         bool            is_missing = false;
    4491                 :             : 
    4492                 :           0 :         roleid = GetUserId();
    4493                 :           0 :         mode = convert_type_priv_string(priv_type_text);
    4494                 :             : 
    4495                 :           0 :         aclresult = object_aclcheck_ext(TypeRelationId, typeoid,
    4496                 :           0 :                                                                         roleid, mode,
    4497                 :             :                                                                         &is_missing);
    4498                 :             : 
    4499         [ #  # ]:           0 :         if (is_missing)
    4500                 :           0 :                 PG_RETURN_NULL();
    4501                 :             : 
    4502                 :           0 :         PG_RETURN_BOOL(aclresult == ACLCHECK_OK);
    4503                 :           0 : }
    4504                 :             : 
    4505                 :             : /*
    4506                 :             :  * has_type_privilege_id_name
    4507                 :             :  *              Check user privileges on a type given
    4508                 :             :  *              roleid, text typename, and text priv name.
    4509                 :             :  */
    4510                 :             : Datum
    4511                 :           0 : has_type_privilege_id_name(PG_FUNCTION_ARGS)
    4512                 :             : {
    4513                 :           0 :         Oid                     roleid = PG_GETARG_OID(0);
    4514                 :           0 :         text       *typename = PG_GETARG_TEXT_PP(1);
    4515                 :           0 :         text       *priv_type_text = PG_GETARG_TEXT_PP(2);
    4516                 :           0 :         Oid                     typeoid;
    4517                 :           0 :         AclMode         mode;
    4518                 :           0 :         AclResult       aclresult;
    4519                 :             : 
    4520                 :           0 :         typeoid = convert_type_name(typename);
    4521                 :           0 :         mode = convert_type_priv_string(priv_type_text);
    4522                 :             : 
    4523                 :           0 :         aclresult = object_aclcheck(TypeRelationId, typeoid, roleid, mode);
    4524                 :             : 
    4525                 :           0 :         PG_RETURN_BOOL(aclresult == ACLCHECK_OK);
    4526                 :           0 : }
    4527                 :             : 
    4528                 :             : /*
    4529                 :             :  * has_type_privilege_id_id
    4530                 :             :  *              Check user privileges on a type given
    4531                 :             :  *              roleid, type oid, and text priv name.
    4532                 :             :  */
    4533                 :             : Datum
    4534                 :           0 : has_type_privilege_id_id(PG_FUNCTION_ARGS)
    4535                 :             : {
    4536                 :           0 :         Oid                     roleid = PG_GETARG_OID(0);
    4537                 :           0 :         Oid                     typeoid = PG_GETARG_OID(1);
    4538                 :           0 :         text       *priv_type_text = PG_GETARG_TEXT_PP(2);
    4539                 :           0 :         AclMode         mode;
    4540                 :           0 :         AclResult       aclresult;
    4541                 :           0 :         bool            is_missing = false;
    4542                 :             : 
    4543                 :           0 :         mode = convert_type_priv_string(priv_type_text);
    4544                 :             : 
    4545                 :           0 :         aclresult = object_aclcheck_ext(TypeRelationId, typeoid,
    4546                 :           0 :                                                                         roleid, mode,
    4547                 :             :                                                                         &is_missing);
    4548                 :             : 
    4549         [ #  # ]:           0 :         if (is_missing)
    4550                 :           0 :                 PG_RETURN_NULL();
    4551                 :             : 
    4552                 :           0 :         PG_RETURN_BOOL(aclresult == ACLCHECK_OK);
    4553                 :           0 : }
    4554                 :             : 
    4555                 :             : /*
    4556                 :             :  *              Support routines for has_type_privilege family.
    4557                 :             :  */
    4558                 :             : 
    4559                 :             : /*
    4560                 :             :  * Given a type name expressed as a string, look it up and return Oid
    4561                 :             :  */
    4562                 :             : static Oid
    4563                 :           2 : convert_type_name(text *typename)
    4564                 :             : {
    4565                 :           2 :         char       *typname = text_to_cstring(typename);
    4566                 :           2 :         Oid                     oid;
    4567                 :             : 
    4568                 :           2 :         oid = DatumGetObjectId(DirectFunctionCall1(regtypein,
    4569                 :             :                                                                                            CStringGetDatum(typname)));
    4570                 :             : 
    4571         [ +  - ]:           2 :         if (!OidIsValid(oid))
    4572   [ #  #  #  # ]:           0 :                 ereport(ERROR,
    4573                 :             :                                 (errcode(ERRCODE_UNDEFINED_OBJECT),
    4574                 :             :                                  errmsg("type \"%s\" does not exist", typname)));
    4575                 :             : 
    4576                 :           4 :         return oid;
    4577                 :           2 : }
    4578                 :             : 
    4579                 :             : /*
    4580                 :             :  * convert_type_priv_string
    4581                 :             :  *              Convert text string to AclMode value.
    4582                 :             :  */
    4583                 :             : static AclMode
    4584                 :           2 : convert_type_priv_string(text *priv_type_text)
    4585                 :             : {
    4586                 :             :         static const priv_map type_priv_map[] = {
    4587                 :             :                 {"USAGE", ACL_USAGE},
    4588                 :             :                 {"USAGE WITH GRANT OPTION", ACL_GRANT_OPTION_FOR(ACL_USAGE)},
    4589                 :             :                 {NULL, 0}
    4590                 :             :         };
    4591                 :             : 
    4592                 :           2 :         return convert_any_priv_string(priv_type_text, type_priv_map);
    4593                 :             : }
    4594                 :             : 
    4595                 :             : /*
    4596                 :             :  * has_parameter_privilege variants
    4597                 :             :  *              These are all named "has_parameter_privilege" at the SQL level.
    4598                 :             :  *              They take various combinations of parameter name with
    4599                 :             :  *              user name, user OID, or implicit user = current_user.
    4600                 :             :  *
    4601                 :             :  *              The result is a boolean value: true if user has been granted
    4602                 :             :  *              the indicated privilege or false if not.
    4603                 :             :  */
    4604                 :             : 
    4605                 :             : /*
    4606                 :             :  * has_param_priv_byname
    4607                 :             :  *
    4608                 :             :  *              Helper function to check user privileges on a parameter given the
    4609                 :             :  *              role by Oid, parameter by text name, and privileges as AclMode.
    4610                 :             :  */
    4611                 :             : static bool
    4612                 :           0 : has_param_priv_byname(Oid roleid, const text *parameter, AclMode priv)
    4613                 :             : {
    4614                 :           0 :         char       *paramstr = text_to_cstring(parameter);
    4615                 :             : 
    4616                 :           0 :         return pg_parameter_aclcheck(paramstr, roleid, priv) == ACLCHECK_OK;
    4617                 :           0 : }
    4618                 :             : 
    4619                 :             : /*
    4620                 :             :  * has_parameter_privilege_name_name
    4621                 :             :  *              Check user privileges on a parameter given name username, text
    4622                 :             :  *              parameter, and text priv name.
    4623                 :             :  */
    4624                 :             : Datum
    4625                 :           0 : has_parameter_privilege_name_name(PG_FUNCTION_ARGS)
    4626                 :             : {
    4627                 :           0 :         Name            username = PG_GETARG_NAME(0);
    4628                 :           0 :         text       *parameter = PG_GETARG_TEXT_PP(1);
    4629                 :           0 :         AclMode         priv = convert_parameter_priv_string(PG_GETARG_TEXT_PP(2));
    4630                 :           0 :         Oid                     roleid = get_role_oid_or_public(NameStr(*username));
    4631                 :             : 
    4632                 :           0 :         PG_RETURN_BOOL(has_param_priv_byname(roleid, parameter, priv));
    4633                 :           0 : }
    4634                 :             : 
    4635                 :             : /*
    4636                 :             :  * has_parameter_privilege_name
    4637                 :             :  *              Check user privileges on a parameter given text parameter and text priv
    4638                 :             :  *              name.  current_user is assumed
    4639                 :             :  */
    4640                 :             : Datum
    4641                 :           0 : has_parameter_privilege_name(PG_FUNCTION_ARGS)
    4642                 :             : {
    4643                 :           0 :         text       *parameter = PG_GETARG_TEXT_PP(0);
    4644                 :           0 :         AclMode         priv = convert_parameter_priv_string(PG_GETARG_TEXT_PP(1));
    4645                 :             : 
    4646                 :           0 :         PG_RETURN_BOOL(has_param_priv_byname(GetUserId(), parameter, priv));
    4647                 :           0 : }
    4648                 :             : 
    4649                 :             : /*
    4650                 :             :  * has_parameter_privilege_id_name
    4651                 :             :  *              Check user privileges on a parameter given roleid, text parameter, and
    4652                 :             :  *              text priv name.
    4653                 :             :  */
    4654                 :             : Datum
    4655                 :           0 : has_parameter_privilege_id_name(PG_FUNCTION_ARGS)
    4656                 :             : {
    4657                 :           0 :         Oid                     roleid = PG_GETARG_OID(0);
    4658                 :           0 :         text       *parameter = PG_GETARG_TEXT_PP(1);
    4659                 :           0 :         AclMode         priv = convert_parameter_priv_string(PG_GETARG_TEXT_PP(2));
    4660                 :             : 
    4661                 :           0 :         PG_RETURN_BOOL(has_param_priv_byname(roleid, parameter, priv));
    4662                 :           0 : }
    4663                 :             : 
    4664                 :             : /*
    4665                 :             :  *              Support routines for has_parameter_privilege family.
    4666                 :             :  */
    4667                 :             : 
    4668                 :             : /*
    4669                 :             :  * convert_parameter_priv_string
    4670                 :             :  *              Convert text string to AclMode value.
    4671                 :             :  */
    4672                 :             : static AclMode
    4673                 :           0 : convert_parameter_priv_string(text *priv_text)
    4674                 :             : {
    4675                 :             :         static const priv_map parameter_priv_map[] = {
    4676                 :             :                 {"SET", ACL_SET},
    4677                 :             :                 {"SET WITH GRANT OPTION", ACL_GRANT_OPTION_FOR(ACL_SET)},
    4678                 :             :                 {"ALTER SYSTEM", ACL_ALTER_SYSTEM},
    4679                 :             :                 {"ALTER SYSTEM WITH GRANT OPTION", ACL_GRANT_OPTION_FOR(ACL_ALTER_SYSTEM)},
    4680                 :             :                 {NULL, 0}
    4681                 :             :         };
    4682                 :             : 
    4683                 :           0 :         return convert_any_priv_string(priv_text, parameter_priv_map);
    4684                 :             : }
    4685                 :             : 
    4686                 :             : /*
    4687                 :             :  * has_largeobject_privilege variants
    4688                 :             :  *              These are all named "has_largeobject_privilege" at the SQL level.
    4689                 :             :  *              They take various combinations of large object OID with
    4690                 :             :  *              user name, user OID, or implicit user = current_user.
    4691                 :             :  *
    4692                 :             :  *              The result is a boolean value: true if user has the indicated
    4693                 :             :  *              privilege, false if not, or NULL if object doesn't exist.
    4694                 :             :  */
    4695                 :             : 
    4696                 :             : /*
    4697                 :             :  * has_lo_priv_byid
    4698                 :             :  *
    4699                 :             :  *              Helper function to check user privileges on a large object given the
    4700                 :             :  *              role by Oid, large object by Oid, and privileges as AclMode.
    4701                 :             :  */
    4702                 :             : static bool
    4703                 :          35 : has_lo_priv_byid(Oid roleid, Oid lobjId, AclMode priv, bool *is_missing)
    4704                 :             : {
    4705                 :          35 :         Snapshot        snapshot = NULL;
    4706                 :          35 :         AclResult       aclresult;
    4707                 :             : 
    4708         [ +  + ]:          35 :         if (priv & ACL_UPDATE)
    4709                 :          16 :                 snapshot = NULL;
    4710                 :             :         else
    4711                 :          19 :                 snapshot = GetActiveSnapshot();
    4712                 :             : 
    4713         [ +  + ]:          35 :         if (!LargeObjectExistsWithSnapshot(lobjId, snapshot))
    4714                 :             :         {
    4715         [ +  - ]:           1 :                 Assert(is_missing != NULL);
    4716                 :           1 :                 *is_missing = true;
    4717                 :           1 :                 return false;
    4718                 :             :         }
    4719                 :             : 
    4720         [ +  + ]:          34 :         if (lo_compat_privileges)
    4721                 :           2 :                 return true;
    4722                 :             : 
    4723                 :          64 :         aclresult = pg_largeobject_aclcheck_snapshot(lobjId,
    4724                 :          32 :                                                                                                  roleid,
    4725                 :          32 :                                                                                                  priv,
    4726                 :          32 :                                                                                                  snapshot);
    4727                 :          32 :         return aclresult == ACLCHECK_OK;
    4728                 :          35 : }
    4729                 :             : 
    4730                 :             : /*
    4731                 :             :  * has_largeobject_privilege_name_id
    4732                 :             :  *              Check user privileges on a large object given
    4733                 :             :  *              name username, large object oid, and text priv name.
    4734                 :             :  */
    4735                 :             : Datum
    4736                 :          14 : has_largeobject_privilege_name_id(PG_FUNCTION_ARGS)
    4737                 :             : {
    4738                 :          14 :         Name            username = PG_GETARG_NAME(0);
    4739                 :          14 :         Oid                     roleid = get_role_oid_or_public(NameStr(*username));
    4740                 :          14 :         Oid                     lobjId = PG_GETARG_OID(1);
    4741                 :          14 :         text       *priv_type_text = PG_GETARG_TEXT_PP(2);
    4742                 :          14 :         AclMode         mode;
    4743                 :          14 :         bool            is_missing = false;
    4744                 :          14 :         bool            result;
    4745                 :             : 
    4746                 :          14 :         mode = convert_largeobject_priv_string(priv_type_text);
    4747                 :          14 :         result = has_lo_priv_byid(roleid, lobjId, mode, &is_missing);
    4748                 :             : 
    4749         [ +  - ]:          14 :         if (is_missing)
    4750                 :           0 :                 PG_RETURN_NULL();
    4751                 :             : 
    4752                 :          14 :         PG_RETURN_BOOL(result);
    4753                 :          14 : }
    4754                 :             : 
    4755                 :             : /*
    4756                 :             :  * has_largeobject_privilege_id
    4757                 :             :  *              Check user privileges on a large object given
    4758                 :             :  *              large object oid, and text priv name.
    4759                 :             :  *              current_user is assumed
    4760                 :             :  */
    4761                 :             : Datum
    4762                 :          21 : has_largeobject_privilege_id(PG_FUNCTION_ARGS)
    4763                 :             : {
    4764                 :          21 :         Oid                     lobjId = PG_GETARG_OID(0);
    4765                 :          21 :         Oid                     roleid = GetUserId();
    4766                 :          21 :         text       *priv_type_text = PG_GETARG_TEXT_PP(1);
    4767                 :          21 :         AclMode         mode;
    4768                 :          21 :         bool            is_missing = false;
    4769                 :          21 :         bool            result;
    4770                 :             : 
    4771                 :          21 :         mode = convert_largeobject_priv_string(priv_type_text);
    4772                 :          21 :         result = has_lo_priv_byid(roleid, lobjId, mode, &is_missing);
    4773                 :             : 
    4774         [ +  + ]:          21 :         if (is_missing)
    4775                 :           1 :                 PG_RETURN_NULL();
    4776                 :             : 
    4777                 :          20 :         PG_RETURN_BOOL(result);
    4778                 :          21 : }
    4779                 :             : 
    4780                 :             : /*
    4781                 :             :  * has_largeobject_privilege_id_id
    4782                 :             :  *              Check user privileges on a large object given
    4783                 :             :  *              roleid, large object oid, and text priv name.
    4784                 :             :  */
    4785                 :             : Datum
    4786                 :           0 : has_largeobject_privilege_id_id(PG_FUNCTION_ARGS)
    4787                 :             : {
    4788                 :           0 :         Oid                     roleid = PG_GETARG_OID(0);
    4789                 :           0 :         Oid                     lobjId = PG_GETARG_OID(1);
    4790                 :           0 :         text       *priv_type_text = PG_GETARG_TEXT_PP(2);
    4791                 :           0 :         AclMode         mode;
    4792                 :           0 :         bool            is_missing = false;
    4793                 :           0 :         bool            result;
    4794                 :             : 
    4795                 :           0 :         mode = convert_largeobject_priv_string(priv_type_text);
    4796                 :           0 :         result = has_lo_priv_byid(roleid, lobjId, mode, &is_missing);
    4797                 :             : 
    4798         [ #  # ]:           0 :         if (is_missing)
    4799                 :           0 :                 PG_RETURN_NULL();
    4800                 :             : 
    4801                 :           0 :         PG_RETURN_BOOL(result);
    4802                 :           0 : }
    4803                 :             : 
    4804                 :             : /*
    4805                 :             :  * convert_largeobject_priv_string
    4806                 :             :  *              Convert text string to AclMode value.
    4807                 :             :  */
    4808                 :             : static AclMode
    4809                 :          35 : convert_largeobject_priv_string(text *priv_type_text)
    4810                 :             : {
    4811                 :             :         static const priv_map largeobject_priv_map[] = {
    4812                 :             :                 {"SELECT", ACL_SELECT},
    4813                 :             :                 {"SELECT WITH GRANT OPTION", ACL_GRANT_OPTION_FOR(ACL_SELECT)},
    4814                 :             :                 {"UPDATE", ACL_UPDATE},
    4815                 :             :                 {"UPDATE WITH GRANT OPTION", ACL_GRANT_OPTION_FOR(ACL_UPDATE)},
    4816                 :             :                 {NULL, 0}
    4817                 :             :         };
    4818                 :             : 
    4819                 :          35 :         return convert_any_priv_string(priv_type_text, largeobject_priv_map);
    4820                 :             : }
    4821                 :             : 
    4822                 :             : /*
    4823                 :             :  * pg_has_role variants
    4824                 :             :  *              These are all named "pg_has_role" at the SQL level.
    4825                 :             :  *              They take various combinations of role name, role OID,
    4826                 :             :  *              user name, user OID, or implicit user = current_user.
    4827                 :             :  *
    4828                 :             :  *              The result is a boolean value: true if user has the indicated
    4829                 :             :  *              privilege, false if not.
    4830                 :             :  */
    4831                 :             : 
    4832                 :             : /*
    4833                 :             :  * pg_has_role_name_name
    4834                 :             :  *              Check user privileges on a role given
    4835                 :             :  *              name username, name rolename, and text priv name.
    4836                 :             :  */
    4837                 :             : Datum
    4838                 :           6 : pg_has_role_name_name(PG_FUNCTION_ARGS)
    4839                 :             : {
    4840                 :           6 :         Name            username = PG_GETARG_NAME(0);
    4841                 :           6 :         Name            rolename = PG_GETARG_NAME(1);
    4842                 :           6 :         text       *priv_type_text = PG_GETARG_TEXT_PP(2);
    4843                 :           6 :         Oid                     roleid;
    4844                 :           6 :         Oid                     roleoid;
    4845                 :           6 :         AclMode         mode;
    4846                 :           6 :         AclResult       aclresult;
    4847                 :             : 
    4848                 :           6 :         roleid = get_role_oid(NameStr(*username), false);
    4849                 :           6 :         roleoid = get_role_oid(NameStr(*rolename), false);
    4850                 :           6 :         mode = convert_role_priv_string(priv_type_text);
    4851                 :             : 
    4852                 :           6 :         aclresult = pg_role_aclcheck(roleoid, roleid, mode);
    4853                 :             : 
    4854                 :          12 :         PG_RETURN_BOOL(aclresult == ACLCHECK_OK);
    4855                 :           6 : }
    4856                 :             : 
    4857                 :             : /*
    4858                 :             :  * pg_has_role_name
    4859                 :             :  *              Check user privileges on a role given
    4860                 :             :  *              name rolename and text priv name.
    4861                 :             :  *              current_user is assumed
    4862                 :             :  */
    4863                 :             : Datum
    4864                 :           3 : pg_has_role_name(PG_FUNCTION_ARGS)
    4865                 :             : {
    4866                 :           3 :         Name            rolename = PG_GETARG_NAME(0);
    4867                 :           3 :         text       *priv_type_text = PG_GETARG_TEXT_PP(1);
    4868                 :           3 :         Oid                     roleid;
    4869                 :           3 :         Oid                     roleoid;
    4870                 :           3 :         AclMode         mode;
    4871                 :           3 :         AclResult       aclresult;
    4872                 :             : 
    4873                 :           3 :         roleid = GetUserId();
    4874                 :           3 :         roleoid = get_role_oid(NameStr(*rolename), false);
    4875                 :           3 :         mode = convert_role_priv_string(priv_type_text);
    4876                 :             : 
    4877                 :           3 :         aclresult = pg_role_aclcheck(roleoid, roleid, mode);
    4878                 :             : 
    4879                 :           6 :         PG_RETURN_BOOL(aclresult == ACLCHECK_OK);
    4880                 :           3 : }
    4881                 :             : 
    4882                 :             : /*
    4883                 :             :  * pg_has_role_name_id
    4884                 :             :  *              Check user privileges on a role given
    4885                 :             :  *              name usename, role oid, and text priv name.
    4886                 :             :  */
    4887                 :             : Datum
    4888                 :           0 : pg_has_role_name_id(PG_FUNCTION_ARGS)
    4889                 :             : {
    4890                 :           0 :         Name            username = PG_GETARG_NAME(0);
    4891                 :           0 :         Oid                     roleoid = PG_GETARG_OID(1);
    4892                 :           0 :         text       *priv_type_text = PG_GETARG_TEXT_PP(2);
    4893                 :           0 :         Oid                     roleid;
    4894                 :           0 :         AclMode         mode;
    4895                 :           0 :         AclResult       aclresult;
    4896                 :             : 
    4897                 :           0 :         roleid = get_role_oid(NameStr(*username), false);
    4898                 :           0 :         mode = convert_role_priv_string(priv_type_text);
    4899                 :             : 
    4900                 :           0 :         aclresult = pg_role_aclcheck(roleoid, roleid, mode);
    4901                 :             : 
    4902                 :           0 :         PG_RETURN_BOOL(aclresult == ACLCHECK_OK);
    4903                 :           0 : }
    4904                 :             : 
    4905                 :             : /*
    4906                 :             :  * pg_has_role_id
    4907                 :             :  *              Check user privileges on a role given
    4908                 :             :  *              role oid, and text priv name.
    4909                 :             :  *              current_user is assumed
    4910                 :             :  */
    4911                 :             : Datum
    4912                 :       14050 : pg_has_role_id(PG_FUNCTION_ARGS)
    4913                 :             : {
    4914                 :       14050 :         Oid                     roleoid = PG_GETARG_OID(0);
    4915                 :       14050 :         text       *priv_type_text = PG_GETARG_TEXT_PP(1);
    4916                 :       14050 :         Oid                     roleid;
    4917                 :       14050 :         AclMode         mode;
    4918                 :       14050 :         AclResult       aclresult;
    4919                 :             : 
    4920                 :       14050 :         roleid = GetUserId();
    4921                 :       14050 :         mode = convert_role_priv_string(priv_type_text);
    4922                 :             : 
    4923                 :       14050 :         aclresult = pg_role_aclcheck(roleoid, roleid, mode);
    4924                 :             : 
    4925                 :       28100 :         PG_RETURN_BOOL(aclresult == ACLCHECK_OK);
    4926                 :       14050 : }
    4927                 :             : 
    4928                 :             : /*
    4929                 :             :  * pg_has_role_id_name
    4930                 :             :  *              Check user privileges on a role given
    4931                 :             :  *              roleid, name rolename, and text priv name.
    4932                 :             :  */
    4933                 :             : Datum
    4934                 :           0 : pg_has_role_id_name(PG_FUNCTION_ARGS)
    4935                 :             : {
    4936                 :           0 :         Oid                     roleid = PG_GETARG_OID(0);
    4937                 :           0 :         Name            rolename = PG_GETARG_NAME(1);
    4938                 :           0 :         text       *priv_type_text = PG_GETARG_TEXT_PP(2);
    4939                 :           0 :         Oid                     roleoid;
    4940                 :           0 :         AclMode         mode;
    4941                 :           0 :         AclResult       aclresult;
    4942                 :             : 
    4943                 :           0 :         roleoid = get_role_oid(NameStr(*rolename), false);
    4944                 :           0 :         mode = convert_role_priv_string(priv_type_text);
    4945                 :             : 
    4946                 :           0 :         aclresult = pg_role_aclcheck(roleoid, roleid, mode);
    4947                 :             : 
    4948                 :           0 :         PG_RETURN_BOOL(aclresult == ACLCHECK_OK);
    4949                 :           0 : }
    4950                 :             : 
    4951                 :             : /*
    4952                 :             :  * pg_has_role_id_id
    4953                 :             :  *              Check user privileges on a role given
    4954                 :             :  *              roleid, role oid, and text priv name.
    4955                 :             :  */
    4956                 :             : Datum
    4957                 :          20 : pg_has_role_id_id(PG_FUNCTION_ARGS)
    4958                 :             : {
    4959                 :          20 :         Oid                     roleid = PG_GETARG_OID(0);
    4960                 :          20 :         Oid                     roleoid = PG_GETARG_OID(1);
    4961                 :          20 :         text       *priv_type_text = PG_GETARG_TEXT_PP(2);
    4962                 :          20 :         AclMode         mode;
    4963                 :          20 :         AclResult       aclresult;
    4964                 :             : 
    4965                 :          20 :         mode = convert_role_priv_string(priv_type_text);
    4966                 :             : 
    4967                 :          20 :         aclresult = pg_role_aclcheck(roleoid, roleid, mode);
    4968                 :             : 
    4969                 :          40 :         PG_RETURN_BOOL(aclresult == ACLCHECK_OK);
    4970                 :          20 : }
    4971                 :             : 
    4972                 :             : /*
    4973                 :             :  *              Support routines for pg_has_role family.
    4974                 :             :  */
    4975                 :             : 
    4976                 :             : /*
    4977                 :             :  * convert_role_priv_string
    4978                 :             :  *              Convert text string to AclMode value.
    4979                 :             :  *
    4980                 :             :  * We use USAGE to denote whether the privileges of the role are accessible
    4981                 :             :  * (has_privs_of_role), MEMBER to denote is_member, and MEMBER WITH GRANT
    4982                 :             :  * (or ADMIN) OPTION to denote is_admin.  There is no ACL bit corresponding
    4983                 :             :  * to MEMBER so we cheat and use ACL_CREATE for that.  This convention
    4984                 :             :  * is shared only with pg_role_aclcheck, below.
    4985                 :             :  */
    4986                 :             : static AclMode
    4987                 :       14079 : convert_role_priv_string(text *priv_type_text)
    4988                 :             : {
    4989                 :             :         static const priv_map role_priv_map[] = {
    4990                 :             :                 {"USAGE", ACL_USAGE},
    4991                 :             :                 {"MEMBER", ACL_CREATE},
    4992                 :             :                 {"SET", ACL_SET},
    4993                 :             :                 {"USAGE WITH GRANT OPTION", ACL_GRANT_OPTION_FOR(ACL_CREATE)},
    4994                 :             :                 {"USAGE WITH ADMIN OPTION", ACL_GRANT_OPTION_FOR(ACL_CREATE)},
    4995                 :             :                 {"MEMBER WITH GRANT OPTION", ACL_GRANT_OPTION_FOR(ACL_CREATE)},
    4996                 :             :                 {"MEMBER WITH ADMIN OPTION", ACL_GRANT_OPTION_FOR(ACL_CREATE)},
    4997                 :             :                 {"SET WITH GRANT OPTION", ACL_GRANT_OPTION_FOR(ACL_CREATE)},
    4998                 :             :                 {"SET WITH ADMIN OPTION", ACL_GRANT_OPTION_FOR(ACL_CREATE)},
    4999                 :             :                 {NULL, 0}
    5000                 :             :         };
    5001                 :             : 
    5002                 :       14079 :         return convert_any_priv_string(priv_type_text, role_priv_map);
    5003                 :             : }
    5004                 :             : 
    5005                 :             : /*
    5006                 :             :  * pg_role_aclcheck
    5007                 :             :  *              Quick-and-dirty support for pg_has_role
    5008                 :             :  */
    5009                 :             : static AclResult
    5010                 :       14079 : pg_role_aclcheck(Oid role_oid, Oid roleid, AclMode mode)
    5011                 :             : {
    5012         [ +  + ]:       14079 :         if (mode & ACL_GRANT_OPTION_FOR(ACL_CREATE))
    5013                 :             :         {
    5014         [ -  + ]:           2 :                 if (is_admin_of_role(roleid, role_oid))
    5015                 :           0 :                         return ACLCHECK_OK;
    5016                 :           2 :         }
    5017         [ +  + ]:       14079 :         if (mode & ACL_CREATE)
    5018                 :             :         {
    5019         [ +  + ]:           2 :                 if (is_member_of_role(roleid, role_oid))
    5020                 :           1 :                         return ACLCHECK_OK;
    5021                 :           1 :         }
    5022         [ +  + ]:       14078 :         if (mode & ACL_USAGE)
    5023                 :             :         {
    5024         [ +  + ]:       14075 :                 if (has_privs_of_role(roleid, role_oid))
    5025                 :       13905 :                         return ACLCHECK_OK;
    5026                 :         170 :         }
    5027         [ +  - ]:         173 :         if (mode & ACL_SET)
    5028                 :             :         {
    5029         [ #  # ]:           0 :                 if (member_can_set_role(roleid, role_oid))
    5030                 :           0 :                         return ACLCHECK_OK;
    5031                 :           0 :         }
    5032                 :         173 :         return ACLCHECK_NO_PRIV;
    5033                 :       14079 : }
    5034                 :             : 
    5035                 :             : 
    5036                 :             : /*
    5037                 :             :  * initialization function (called by InitPostgres)
    5038                 :             :  */
    5039                 :             : void
    5040                 :         796 : initialize_acl(void)
    5041                 :             : {
    5042         [ +  + ]:         796 :         if (!IsBootstrapProcessingMode())
    5043                 :             :         {
    5044                 :         795 :                 cached_db_hash =
    5045                 :         795 :                         GetSysCacheHashValue1(DATABASEOID,
    5046                 :             :                                                                   ObjectIdGetDatum(MyDatabaseId));
    5047                 :             : 
    5048                 :             :                 /*
    5049                 :             :                  * In normal mode, set a callback on any syscache invalidation of rows
    5050                 :             :                  * of pg_auth_members (for roles_is_member_of()) pg_database (for
    5051                 :             :                  * roles_is_member_of())
    5052                 :             :                  */
    5053                 :         795 :                 CacheRegisterSyscacheCallback(AUTHMEMROLEMEM,
    5054                 :             :                                                                           RoleMembershipCacheCallback,
    5055                 :             :                                                                           (Datum) 0);
    5056                 :         795 :                 CacheRegisterSyscacheCallback(AUTHOID,
    5057                 :             :                                                                           RoleMembershipCacheCallback,
    5058                 :             :                                                                           (Datum) 0);
    5059                 :         795 :                 CacheRegisterSyscacheCallback(DATABASEOID,
    5060                 :             :                                                                           RoleMembershipCacheCallback,
    5061                 :             :                                                                           (Datum) 0);
    5062                 :         795 :         }
    5063                 :         796 : }
    5064                 :             : 
    5065                 :             : /*
    5066                 :             :  * RoleMembershipCacheCallback
    5067                 :             :  *              Syscache inval callback function
    5068                 :             :  */
    5069                 :             : static void
    5070                 :        8225 : RoleMembershipCacheCallback(Datum arg, int cacheid, uint32 hashvalue)
    5071                 :             : {
    5072         [ +  + ]:        8225 :         if (cacheid == DATABASEOID &&
    5073   [ +  +  +  + ]:         707 :                 hashvalue != cached_db_hash &&
    5074                 :         581 :                 hashvalue != 0)
    5075                 :             :         {
    5076                 :          42 :                 return;                                 /* ignore pg_database changes for other DBs */
    5077                 :             :         }
    5078                 :             : 
    5079                 :             :         /* Force membership caches to be recomputed on next use */
    5080                 :        8183 :         cached_role[ROLERECURSE_MEMBERS] = InvalidOid;
    5081                 :        8183 :         cached_role[ROLERECURSE_PRIVS] = InvalidOid;
    5082                 :        8183 :         cached_role[ROLERECURSE_SETROLE] = InvalidOid;
    5083                 :        8225 : }
    5084                 :             : 
    5085                 :             : /*
    5086                 :             :  * A helper function for roles_is_member_of() that provides an optimized
    5087                 :             :  * implementation of list_append_unique_oid() via a Bloom filter.  The caller
    5088                 :             :  * (i.e., roles_is_member_of()) is responsible for freeing bf once it is done
    5089                 :             :  * using this function.
    5090                 :             :  */
    5091                 :             : static inline List *
    5092                 :         682 : roles_list_append(List *roles_list, bloom_filter **bf, Oid role)
    5093                 :             : {
    5094                 :         682 :         unsigned char *roleptr = (unsigned char *) &role;
    5095                 :             : 
    5096                 :             :         /*
    5097                 :             :          * If there is a previously-created Bloom filter, use it to try to
    5098                 :             :          * determine whether the role is missing from the list.  If it says yes,
    5099                 :             :          * that's a hard fact and we can go ahead and add the role.  If it says
    5100                 :             :          * no, that's only probabilistic and we'd better search the list.  Without
    5101                 :             :          * a filter, we must always do an ordinary linear search through the
    5102                 :             :          * existing list.
    5103                 :             :          */
    5104   [ -  +  +  + ]:         682 :         if ((*bf && bloom_lacks_element(*bf, roleptr, sizeof(Oid))) ||
    5105                 :         682 :                 !list_member_oid(roles_list, role))
    5106                 :             :         {
    5107                 :             :                 /*
    5108                 :             :                  * If the list is large, we take on the overhead of creating and
    5109                 :             :                  * populating a Bloom filter to speed up future calls to this
    5110                 :             :                  * function.
    5111                 :             :                  */
    5112   [ +  -  +  - ]:         542 :                 if (*bf == NULL &&
    5113                 :         542 :                         list_length(roles_list) > ROLES_LIST_BLOOM_THRESHOLD)
    5114                 :             :                 {
    5115                 :           0 :                         *bf = bloom_create(ROLES_LIST_BLOOM_THRESHOLD * 10, work_mem, 0);
    5116   [ #  #  #  #  :           0 :                         foreach_oid(roleid, roles_list)
             #  #  #  # ]
    5117                 :           0 :                                 bloom_add_element(*bf, (unsigned char *) &roleid, sizeof(Oid));
    5118                 :           0 :                 }
    5119                 :             : 
    5120                 :             :                 /*
    5121                 :             :                  * Finally, add the role to the list and the Bloom filter, if it
    5122                 :             :                  * exists.
    5123                 :             :                  */
    5124                 :         542 :                 roles_list = lappend_oid(roles_list, role);
    5125         [ +  - ]:         542 :                 if (*bf)
    5126                 :           0 :                         bloom_add_element(*bf, roleptr, sizeof(Oid));
    5127                 :         542 :         }
    5128                 :             : 
    5129                 :        1364 :         return roles_list;
    5130                 :         682 : }
    5131                 :             : 
    5132                 :             : /*
    5133                 :             :  * Get a list of roles that the specified roleid is a member of
    5134                 :             :  *
    5135                 :             :  * Type ROLERECURSE_MEMBERS recurses through all grants; ROLERECURSE_PRIVS
    5136                 :             :  * recurses only through inheritable grants; and ROLERECURSE_SETROLE recurses
    5137                 :             :  * only through grants with set_option.
    5138                 :             :  *
    5139                 :             :  * Since indirect membership testing is relatively expensive, we cache
    5140                 :             :  * a list of memberships.  Hence, the result is only guaranteed good until
    5141                 :             :  * the next call of roles_is_member_of()!
    5142                 :             :  *
    5143                 :             :  * For the benefit of select_best_grantor, the result is defined to be
    5144                 :             :  * in breadth-first order, ie, closer relationships earlier.
    5145                 :             :  *
    5146                 :             :  * If admin_of is not InvalidOid, this function sets *admin_role, either
    5147                 :             :  * to the OID of the first role in the result list that directly possesses
    5148                 :             :  * ADMIN OPTION on the role corresponding to admin_of, or to InvalidOid if
    5149                 :             :  * there is no such role.
    5150                 :             :  */
    5151                 :             : static List *
    5152                 :        4893 : roles_is_member_of(Oid roleid, enum RoleRecurseType type,
    5153                 :             :                                    Oid admin_of, Oid *admin_role)
    5154                 :             : {
    5155                 :        4893 :         Oid                     dba;
    5156                 :        4893 :         List       *roles_list;
    5157                 :        4893 :         ListCell   *l;
    5158                 :        4893 :         List       *new_cached_roles;
    5159                 :        4893 :         MemoryContext oldctx;
    5160                 :        4893 :         bloom_filter *bf = NULL;
    5161                 :             : 
    5162         [ +  - ]:        4893 :         Assert(OidIsValid(admin_of) == (admin_role != NULL));
    5163         [ +  + ]:        4893 :         if (admin_role != NULL)
    5164                 :         145 :                 *admin_role = InvalidOid;
    5165                 :             : 
    5166                 :             :         /* If cache is valid and ADMIN OPTION not sought, just return the list */
    5167   [ +  +  +  +  :        4893 :         if (cached_role[type] == roleid && !OidIsValid(admin_of) &&
                   -  + ]
    5168                 :        4274 :                 OidIsValid(cached_role[type]))
    5169                 :        4274 :                 return cached_roles[type];
    5170                 :             : 
    5171                 :             :         /*
    5172                 :             :          * Role expansion happens in a non-database backend when guc.c checks
    5173                 :             :          * ROLE_PG_READ_ALL_SETTINGS for a physical walsender SHOW command.  In
    5174                 :             :          * that case, no role gets pg_database_owner.
    5175                 :             :          */
    5176         [ +  - ]:         619 :         if (!OidIsValid(MyDatabaseId))
    5177                 :           0 :                 dba = InvalidOid;
    5178                 :             :         else
    5179                 :             :         {
    5180                 :         619 :                 HeapTuple       dbtup;
    5181                 :             : 
    5182                 :         619 :                 dbtup = SearchSysCache1(DATABASEOID, ObjectIdGetDatum(MyDatabaseId));
    5183         [ +  - ]:         619 :                 if (!HeapTupleIsValid(dbtup))
    5184   [ #  #  #  # ]:           0 :                         elog(ERROR, "cache lookup failed for database %u", MyDatabaseId);
    5185                 :         619 :                 dba = ((Form_pg_database) GETSTRUCT(dbtup))->datdba;
    5186                 :         619 :                 ReleaseSysCache(dbtup);
    5187                 :         619 :         }
    5188                 :             : 
    5189                 :             :         /*
    5190                 :             :          * Find all the roles that roleid is a member of, including multi-level
    5191                 :             :          * recursion.  The role itself will always be the first element of the
    5192                 :             :          * resulting list.
    5193                 :             :          *
    5194                 :             :          * Each element of the list is scanned to see if it adds any indirect
    5195                 :             :          * memberships.  We can use a single list as both the record of
    5196                 :             :          * already-found memberships and the agenda of roles yet to be scanned.
    5197                 :             :          * This is a bit tricky but works because the foreach() macro doesn't
    5198                 :             :          * fetch the next list element until the bottom of the loop.
    5199                 :             :          */
    5200                 :         619 :         roles_list = list_make1_oid(roleid);
    5201                 :             : 
    5202   [ +  -  +  +  :        1780 :         foreach(l, roles_list)
                   +  + ]
    5203                 :             :         {
    5204                 :        1161 :                 Oid                     memberid = lfirst_oid(l);
    5205                 :        1161 :                 CatCList   *memlist;
    5206                 :        1161 :                 int                     i;
    5207                 :             : 
    5208                 :             :                 /* Find roles that memberid is directly a member of */
    5209                 :        1161 :                 memlist = SearchSysCacheList1(AUTHMEMMEMROLE,
    5210                 :             :                                                                           ObjectIdGetDatum(memberid));
    5211         [ +  + ]:        2206 :                 for (i = 0; i < memlist->n_members; i++)
    5212                 :             :                 {
    5213                 :        1045 :                         HeapTuple       tup = &memlist->members[i]->tuple;
    5214                 :        1045 :                         Form_pg_auth_members form = (Form_pg_auth_members) GETSTRUCT(tup);
    5215                 :        1045 :                         Oid                     otherid = form->roleid;
    5216                 :             : 
    5217                 :             :                         /*
    5218                 :             :                          * While otherid==InvalidOid shouldn't appear in the catalog, the
    5219                 :             :                          * OidIsValid() avoids crashing if that arises.
    5220                 :             :                          */
    5221   [ +  +  +  + ]:        1045 :                         if (otherid == admin_of && form->admin_option &&
    5222   [ +  -  +  + ]:         120 :                                 OidIsValid(admin_of) && !OidIsValid(*admin_role))
    5223                 :         116 :                                 *admin_role = memberid;
    5224                 :             : 
    5225                 :             :                         /* If we're supposed to ignore non-heritable grants, do so. */
    5226   [ +  +  +  + ]:        1045 :                         if (type == ROLERECURSE_PRIVS && !form->inherit_option)
    5227                 :         345 :                                 continue;
    5228                 :             : 
    5229                 :             :                         /* If we're supposed to ignore non-SET grants, do so. */
    5230   [ +  +  +  + ]:         700 :                         if (type == ROLERECURSE_SETROLE && !form->set_option)
    5231                 :          21 :                                 continue;
    5232                 :             : 
    5233                 :             :                         /*
    5234                 :             :                          * Even though there shouldn't be any loops in the membership
    5235                 :             :                          * graph, we must test for having already seen this role. It is
    5236                 :             :                          * legal for instance to have both A->B and A->C->B.
    5237                 :             :                          */
    5238                 :         679 :                         roles_list = roles_list_append(roles_list, &bf, otherid);
    5239      [ -  +  + ]:        1045 :                 }
    5240                 :        1161 :                 ReleaseSysCacheList(memlist);
    5241                 :             : 
    5242                 :             :                 /* implement pg_database_owner implicit membership */
    5243   [ +  +  -  + ]:        1161 :                 if (memberid == dba && OidIsValid(dba))
    5244                 :           3 :                         roles_list = roles_list_append(roles_list, &bf,
    5245                 :             :                                                                                    ROLE_PG_DATABASE_OWNER);
    5246                 :        1161 :         }
    5247                 :             : 
    5248                 :             :         /*
    5249                 :             :          * Free the Bloom filter created by roles_list_append(), if there is one.
    5250                 :             :          */
    5251         [ +  - ]:         619 :         if (bf)
    5252                 :           0 :                 bloom_free(bf);
    5253                 :             : 
    5254                 :             :         /*
    5255                 :             :          * Copy the completed list into TopMemoryContext so it will persist.
    5256                 :             :          */
    5257                 :         619 :         oldctx = MemoryContextSwitchTo(TopMemoryContext);
    5258                 :         619 :         new_cached_roles = list_copy(roles_list);
    5259                 :         619 :         MemoryContextSwitchTo(oldctx);
    5260                 :         619 :         list_free(roles_list);
    5261                 :             : 
    5262                 :             :         /*
    5263                 :             :          * Now safe to assign to state variable
    5264                 :             :          */
    5265                 :         619 :         cached_role[type] = InvalidOid; /* just paranoia */
    5266                 :         619 :         list_free(cached_roles[type]);
    5267                 :         619 :         cached_roles[type] = new_cached_roles;
    5268                 :         619 :         cached_role[type] = roleid;
    5269                 :             : 
    5270                 :             :         /* And now we can return the answer */
    5271                 :         619 :         return cached_roles[type];
    5272                 :        4893 : }
    5273                 :             : 
    5274                 :             : 
    5275                 :             : /*
    5276                 :             :  * Does member have the privileges of role (directly or indirectly)?
    5277                 :             :  *
    5278                 :             :  * This is defined not to recurse through grants that are not inherited,
    5279                 :             :  * and only inherited grants confer the associated privileges automatically.
    5280                 :             :  *
    5281                 :             :  * See also member_can_set_role, below.
    5282                 :             :  */
    5283                 :             : bool
    5284                 :       19822 : has_privs_of_role(Oid member, Oid role)
    5285                 :             : {
    5286                 :             :         /* Fast path for simple case */
    5287         [ +  + ]:       19822 :         if (member == role)
    5288                 :       14627 :                 return true;
    5289                 :             : 
    5290                 :             :         /* Superusers have every privilege, so are part of every role */
    5291         [ +  + ]:        5195 :         if (superuser_arg(member))
    5292                 :         643 :                 return true;
    5293                 :             : 
    5294                 :             :         /*
    5295                 :             :          * Find all the roles that member has the privileges of, including
    5296                 :             :          * multi-level recursion, then see if target role is any one of them.
    5297                 :             :          */
    5298                 :        9104 :         return list_member_oid(roles_is_member_of(member, ROLERECURSE_PRIVS,
    5299                 :             :                                                                                           InvalidOid, NULL),
    5300                 :        4552 :                                                    role);
    5301                 :       19822 : }
    5302                 :             : 
    5303                 :             : /*
    5304                 :             :  * Can member use SET ROLE to this role?
    5305                 :             :  *
    5306                 :             :  * There must be a chain of grants from 'member' to 'role' each of which
    5307                 :             :  * permits SET ROLE; that is, each of which has set_option = true.
    5308                 :             :  *
    5309                 :             :  * It doesn't matter whether the grants are inheritable. That's a separate
    5310                 :             :  * question; see has_privs_of_role.
    5311                 :             :  *
    5312                 :             :  * This function should be used to determine whether the session user can
    5313                 :             :  * use SET ROLE to become the target user. We also use it to determine whether
    5314                 :             :  * the session user can change an existing object to be owned by the target
    5315                 :             :  * user, or create new objects owned by the target user.
    5316                 :             :  */
    5317                 :             : bool
    5318                 :        2323 : member_can_set_role(Oid member, Oid role)
    5319                 :             : {
    5320                 :             :         /* Fast path for simple case */
    5321         [ +  + ]:        2323 :         if (member == role)
    5322                 :        2127 :                 return true;
    5323                 :             : 
    5324                 :             :         /* Superusers have every privilege, so can always SET ROLE */
    5325         [ +  + ]:         196 :         if (superuser_arg(member))
    5326                 :         146 :                 return true;
    5327                 :             : 
    5328                 :             :         /*
    5329                 :             :          * Find all the roles that member can access via SET ROLE, including
    5330                 :             :          * multi-level recursion, then see if target role is any one of them.
    5331                 :             :          */
    5332                 :         100 :         return list_member_oid(roles_is_member_of(member, ROLERECURSE_SETROLE,
    5333                 :             :                                                                                           InvalidOid, NULL),
    5334                 :          50 :                                                    role);
    5335                 :        2323 : }
    5336                 :             : 
    5337                 :             : /*
    5338                 :             :  * Permission violation error unless able to SET ROLE to target role.
    5339                 :             :  */
    5340                 :             : void
    5341                 :         179 : check_can_set_role(Oid member, Oid role)
    5342                 :             : {
    5343         [ +  + ]:         179 :         if (!member_can_set_role(member, role))
    5344   [ +  -  +  - ]:          24 :                 ereport(ERROR,
    5345                 :             :                                 (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
    5346                 :             :                                  errmsg("must be able to SET ROLE \"%s\"",
    5347                 :             :                                                 GetUserNameFromId(role, false))));
    5348                 :         155 : }
    5349                 :             : 
    5350                 :             : /*
    5351                 :             :  * Is member a member of role (directly or indirectly)?
    5352                 :             :  *
    5353                 :             :  * This is defined to recurse through grants whether they are inherited or not.
    5354                 :             :  *
    5355                 :             :  * Do not use this for privilege checking, instead use has_privs_of_role().
    5356                 :             :  * Don't use it for determining whether it's possible to SET ROLE to some
    5357                 :             :  * other role; for that, use member_can_set_role(). And don't use it for
    5358                 :             :  * determining whether it's OK to create an object owned by some other role:
    5359                 :             :  * use member_can_set_role() for that, too.
    5360                 :             :  *
    5361                 :             :  * In short, calling this function is the wrong thing to do nearly everywhere.
    5362                 :             :  */
    5363                 :             : bool
    5364                 :           2 : is_member_of_role(Oid member, Oid role)
    5365                 :             : {
    5366                 :             :         /* Fast path for simple case */
    5367         [ -  + ]:           2 :         if (member == role)
    5368                 :           0 :                 return true;
    5369                 :             : 
    5370                 :             :         /* Superusers have every privilege, so are part of every role */
    5371         [ -  + ]:           2 :         if (superuser_arg(member))
    5372                 :           0 :                 return true;
    5373                 :             : 
    5374                 :             :         /*
    5375                 :             :          * Find all the roles that member is a member of, including multi-level
    5376                 :             :          * recursion, then see if target role is any one of them.
    5377                 :             :          */
    5378                 :           4 :         return list_member_oid(roles_is_member_of(member, ROLERECURSE_MEMBERS,
    5379                 :             :                                                                                           InvalidOid, NULL),
    5380                 :           2 :                                                    role);
    5381                 :           2 : }
    5382                 :             : 
    5383                 :             : /*
    5384                 :             :  * Is member a member of role, not considering superuserness?
    5385                 :             :  *
    5386                 :             :  * This is identical to is_member_of_role except we ignore superuser
    5387                 :             :  * status.
    5388                 :             :  *
    5389                 :             :  * Do not use this for privilege checking, instead use has_privs_of_role()
    5390                 :             :  */
    5391                 :             : bool
    5392                 :         106 : is_member_of_role_nosuper(Oid member, Oid role)
    5393                 :             : {
    5394                 :             :         /* Fast path for simple case */
    5395         [ +  + ]:         106 :         if (member == role)
    5396                 :           2 :                 return true;
    5397                 :             : 
    5398                 :             :         /*
    5399                 :             :          * Find all the roles that member is a member of, including multi-level
    5400                 :             :          * recursion, then see if target role is any one of them.
    5401                 :             :          */
    5402                 :         208 :         return list_member_oid(roles_is_member_of(member, ROLERECURSE_MEMBERS,
    5403                 :             :                                                                                           InvalidOid, NULL),
    5404                 :         104 :                                                    role);
    5405                 :         106 : }
    5406                 :             : 
    5407                 :             : 
    5408                 :             : /*
    5409                 :             :  * Is member an admin of role?  That is, is member the role itself (subject to
    5410                 :             :  * restrictions below), a member (directly or indirectly) WITH ADMIN OPTION,
    5411                 :             :  * or a superuser?
    5412                 :             :  */
    5413                 :             : bool
    5414                 :         394 : is_admin_of_role(Oid member, Oid role)
    5415                 :             : {
    5416                 :         394 :         Oid                     admin_role;
    5417                 :             : 
    5418         [ +  + ]:         394 :         if (superuser_arg(member))
    5419                 :         316 :                 return true;
    5420                 :             : 
    5421                 :             :         /* By policy, a role cannot have WITH ADMIN OPTION on itself. */
    5422         [ +  + ]:          78 :         if (member == role)
    5423                 :           3 :                 return false;
    5424                 :             : 
    5425                 :          75 :         (void) roles_is_member_of(member, ROLERECURSE_MEMBERS, role, &admin_role);
    5426                 :          75 :         return OidIsValid(admin_role);
    5427                 :         394 : }
    5428                 :             : 
    5429                 :             : /*
    5430                 :             :  * Find a role whose privileges "member" inherits which has ADMIN OPTION
    5431                 :             :  * on "role", ignoring super-userness.
    5432                 :             :  *
    5433                 :             :  * There might be more than one such role; prefer one which involves fewer
    5434                 :             :  * hops. That is, if member has ADMIN OPTION, prefer that over all other
    5435                 :             :  * options; if not, prefer a role from which member inherits more directly
    5436                 :             :  * over more indirect inheritance.
    5437                 :             :  */
    5438                 :             : Oid
    5439                 :          71 : select_best_admin(Oid member, Oid role)
    5440                 :             : {
    5441                 :          71 :         Oid                     admin_role;
    5442                 :             : 
    5443                 :             :         /* By policy, a role cannot have WITH ADMIN OPTION on itself. */
    5444         [ +  + ]:          71 :         if (member == role)
    5445                 :           1 :                 return InvalidOid;
    5446                 :             : 
    5447                 :          70 :         (void) roles_is_member_of(member, ROLERECURSE_PRIVS, role, &admin_role);
    5448                 :          70 :         return admin_role;
    5449                 :          71 : }
    5450                 :             : 
    5451                 :             : /*
    5452                 :             :  * Select the effective grantor ID for a GRANT or REVOKE operation.
    5453                 :             :  *
    5454                 :             :  * The grantor must always be either the object owner or some role that has
    5455                 :             :  * been explicitly granted grant options.  This ensures that all granted
    5456                 :             :  * privileges appear to flow from the object owner, and there are never
    5457                 :             :  * multiple "original sources" of a privilege.  Therefore, if the would-be
    5458                 :             :  * grantor is a member of a role that has the needed grant options, we have
    5459                 :             :  * to do the grant as that role instead.
    5460                 :             :  *
    5461                 :             :  * It is possible that the would-be grantor is a member of several roles
    5462                 :             :  * that have different subsets of the desired grant options, but no one
    5463                 :             :  * role has 'em all.  In this case we pick a role with the largest number
    5464                 :             :  * of desired options.  Ties are broken in favor of closer ancestors.
    5465                 :             :  *
    5466                 :             :  * roleId: the role attempting to do the GRANT/REVOKE
    5467                 :             :  * privileges: the privileges to be granted/revoked
    5468                 :             :  * acl: the ACL of the object in question
    5469                 :             :  * ownerId: the role owning the object in question
    5470                 :             :  * *grantorId: receives the OID of the role to do the grant as
    5471                 :             :  * *grantOptions: receives the grant options actually held by grantorId
    5472                 :             :  *
    5473                 :             :  * If no grant options exist, we set grantorId to roleId, grantOptions to 0.
    5474                 :             :  */
    5475                 :             : void
    5476                 :        1383 : select_best_grantor(Oid roleId, AclMode privileges,
    5477                 :             :                                         const Acl *acl, Oid ownerId,
    5478                 :             :                                         Oid *grantorId, AclMode *grantOptions)
    5479                 :             : {
    5480                 :        1383 :         AclMode         needed_goptions = ACL_GRANT_OPTION_FOR(privileges);
    5481                 :        1383 :         List       *roles_list;
    5482                 :        1383 :         int                     nrights;
    5483                 :        1383 :         ListCell   *l;
    5484                 :             : 
    5485                 :             :         /*
    5486                 :             :          * The object owner is always treated as having all grant options, so if
    5487                 :             :          * roleId is the owner it's easy.  Also, if roleId is a superuser it's
    5488                 :             :          * easy: superusers are implicitly members of every role, so they act as
    5489                 :             :          * the object owner.
    5490                 :             :          */
    5491   [ +  +  +  + ]:        1383 :         if (roleId == ownerId || superuser_arg(roleId))
    5492                 :             :         {
    5493                 :        1343 :                 *grantorId = ownerId;
    5494                 :        1343 :                 *grantOptions = needed_goptions;
    5495                 :        1343 :                 return;
    5496                 :             :         }
    5497                 :             : 
    5498                 :             :         /*
    5499                 :             :          * Otherwise we have to do a careful search to see if roleId has the
    5500                 :             :          * privileges of any suitable role.  Note: we can hang onto the result of
    5501                 :             :          * roles_is_member_of() throughout this loop, because aclmask_direct()
    5502                 :             :          * doesn't query any role memberships.
    5503                 :             :          */
    5504                 :          40 :         roles_list = roles_is_member_of(roleId, ROLERECURSE_PRIVS,
    5505                 :             :                                                                         InvalidOid, NULL);
    5506                 :             : 
    5507                 :             :         /* initialize candidate result as default */
    5508                 :          40 :         *grantorId = roleId;
    5509                 :          40 :         *grantOptions = ACL_NO_RIGHTS;
    5510                 :          40 :         nrights = 0;
    5511                 :             : 
    5512   [ +  -  +  +  :          85 :         foreach(l, roles_list)
             +  +  +  + ]
    5513                 :             :         {
    5514                 :          45 :                 Oid                     otherrole = lfirst_oid(l);
    5515                 :          45 :                 AclMode         otherprivs;
    5516                 :             : 
    5517                 :          90 :                 otherprivs = aclmask_direct(acl, otherrole, ownerId,
    5518                 :          45 :                                                                         needed_goptions, ACLMASK_ALL);
    5519         [ +  + ]:          45 :                 if (otherprivs == needed_goptions)
    5520                 :             :                 {
    5521                 :             :                         /* Found a suitable grantor */
    5522                 :          27 :                         *grantorId = otherrole;
    5523                 :          27 :                         *grantOptions = otherprivs;
    5524                 :          27 :                         return;
    5525                 :             :                 }
    5526                 :             : 
    5527                 :             :                 /*
    5528                 :             :                  * If it has just some of the needed privileges, remember best
    5529                 :             :                  * candidate.
    5530                 :             :                  */
    5531         [ +  + ]:          18 :                 if (otherprivs != ACL_NO_RIGHTS)
    5532                 :             :                 {
    5533                 :           1 :                         int                     nnewrights = pg_popcount64(otherprivs);
    5534                 :             : 
    5535         [ -  + ]:           1 :                         if (nnewrights > nrights)
    5536                 :             :                         {
    5537                 :           1 :                                 *grantorId = otherrole;
    5538                 :           1 :                                 *grantOptions = otherprivs;
    5539                 :           1 :                                 nrights = nnewrights;
    5540                 :           1 :                         }
    5541                 :           1 :                 }
    5542         [ +  + ]:          45 :         }
    5543         [ -  + ]:        1383 : }
    5544                 :             : 
    5545                 :             : /*
    5546                 :             :  * get_role_oid - Given a role name, look up the role's OID.
    5547                 :             :  *
    5548                 :             :  * If missing_ok is false, throw an error if role name not found.  If
    5549                 :             :  * true, just return InvalidOid.
    5550                 :             :  */
    5551                 :             : Oid
    5552                 :        1570 : get_role_oid(const char *rolname, bool missing_ok)
    5553                 :             : {
    5554                 :        1570 :         Oid                     oid;
    5555                 :             : 
    5556                 :        1570 :         oid = GetSysCacheOid1(AUTHNAME, Anum_pg_authid_oid,
    5557                 :             :                                                   CStringGetDatum(rolname));
    5558   [ +  +  +  + ]:        1570 :         if (!OidIsValid(oid) && !missing_ok)
    5559   [ +  -  +  - ]:           8 :                 ereport(ERROR,
    5560                 :             :                                 (errcode(ERRCODE_UNDEFINED_OBJECT),
    5561                 :             :                                  errmsg("role \"%s\" does not exist", rolname)));
    5562                 :        3124 :         return oid;
    5563                 :        1562 : }
    5564                 :             : 
    5565                 :             : /*
    5566                 :             :  * get_role_oid_or_public - As above, but return ACL_ID_PUBLIC if the
    5567                 :             :  *              role name is "public".
    5568                 :             :  */
    5569                 :             : Oid
    5570                 :          99 : get_role_oid_or_public(const char *rolname)
    5571                 :             : {
    5572         [ +  - ]:          99 :         if (strcmp(rolname, "public") == 0)
    5573                 :           0 :                 return ACL_ID_PUBLIC;
    5574                 :             : 
    5575                 :          99 :         return get_role_oid(rolname, false);
    5576                 :          99 : }
    5577                 :             : 
    5578                 :             : /*
    5579                 :             :  * Given a RoleSpec node, return the OID it corresponds to.  If missing_ok is
    5580                 :             :  * true, return InvalidOid if the role does not exist.
    5581                 :             :  *
    5582                 :             :  * PUBLIC is always disallowed here.  Routines wanting to handle the PUBLIC
    5583                 :             :  * case must check the case separately.
    5584                 :             :  */
    5585                 :             : Oid
    5586                 :         807 : get_rolespec_oid(const RoleSpec *role, bool missing_ok)
    5587                 :             : {
    5588                 :         807 :         Oid                     oid;
    5589                 :             : 
    5590   [ -  +  +  -  :         807 :         switch (role->roletype)
                      - ]
    5591                 :             :         {
    5592                 :             :                 case ROLESPEC_CSTRING:
    5593         [ +  - ]:         759 :                         Assert(role->rolename);
    5594                 :         759 :                         oid = get_role_oid(role->rolename, missing_ok);
    5595                 :         759 :                         break;
    5596                 :             : 
    5597                 :             :                 case ROLESPEC_CURRENT_ROLE:
    5598                 :             :                 case ROLESPEC_CURRENT_USER:
    5599                 :          48 :                         oid = GetUserId();
    5600                 :          48 :                         break;
    5601                 :             : 
    5602                 :             :                 case ROLESPEC_SESSION_USER:
    5603                 :           0 :                         oid = GetSessionUserId();
    5604                 :           0 :                         break;
    5605                 :             : 
    5606                 :             :                 case ROLESPEC_PUBLIC:
    5607   [ #  #  #  # ]:           0 :                         ereport(ERROR,
    5608                 :             :                                         (errcode(ERRCODE_UNDEFINED_OBJECT),
    5609                 :             :                                          errmsg("role \"%s\" does not exist", "public")));
    5610                 :           0 :                         oid = InvalidOid;       /* make compiler happy */
    5611                 :           0 :                         break;
    5612                 :             : 
    5613                 :             :                 default:
    5614   [ #  #  #  # ]:           0 :                         elog(ERROR, "unexpected role type %d", role->roletype);
    5615                 :           0 :         }
    5616                 :             : 
    5617                 :        1614 :         return oid;
    5618                 :         807 : }
    5619                 :             : 
    5620                 :             : /*
    5621                 :             :  * Given a RoleSpec node, return the pg_authid HeapTuple it corresponds to.
    5622                 :             :  * Caller must ReleaseSysCache when done with the result tuple.
    5623                 :             :  */
    5624                 :             : HeapTuple
    5625                 :          73 : get_rolespec_tuple(const RoleSpec *role)
    5626                 :             : {
    5627                 :          73 :         HeapTuple       tuple;
    5628                 :             : 
    5629   [ -  +  -  -  :          73 :         switch (role->roletype)
                      - ]
    5630                 :             :         {
    5631                 :             :                 case ROLESPEC_CSTRING:
    5632         [ +  - ]:          73 :                         Assert(role->rolename);
    5633                 :          73 :                         tuple = SearchSysCache1(AUTHNAME, CStringGetDatum(role->rolename));
    5634         [ +  - ]:          73 :                         if (!HeapTupleIsValid(tuple))
    5635   [ #  #  #  # ]:           0 :                                 ereport(ERROR,
    5636                 :             :                                                 (errcode(ERRCODE_UNDEFINED_OBJECT),
    5637                 :             :                                                  errmsg("role \"%s\" does not exist", role->rolename)));
    5638                 :          73 :                         break;
    5639                 :             : 
    5640                 :             :                 case ROLESPEC_CURRENT_ROLE:
    5641                 :             :                 case ROLESPEC_CURRENT_USER:
    5642                 :           0 :                         tuple = SearchSysCache1(AUTHOID, ObjectIdGetDatum(GetUserId()));
    5643         [ #  # ]:           0 :                         if (!HeapTupleIsValid(tuple))
    5644   [ #  #  #  # ]:           0 :                                 elog(ERROR, "cache lookup failed for role %u", GetUserId());
    5645                 :           0 :                         break;
    5646                 :             : 
    5647                 :             :                 case ROLESPEC_SESSION_USER:
    5648                 :           0 :                         tuple = SearchSysCache1(AUTHOID, ObjectIdGetDatum(GetSessionUserId()));
    5649         [ #  # ]:           0 :                         if (!HeapTupleIsValid(tuple))
    5650   [ #  #  #  # ]:           0 :                                 elog(ERROR, "cache lookup failed for role %u", GetSessionUserId());
    5651                 :           0 :                         break;
    5652                 :             : 
    5653                 :             :                 case ROLESPEC_PUBLIC:
    5654   [ #  #  #  # ]:           0 :                         ereport(ERROR,
    5655                 :             :                                         (errcode(ERRCODE_UNDEFINED_OBJECT),
    5656                 :             :                                          errmsg("role \"%s\" does not exist", "public")));
    5657                 :           0 :                         tuple = NULL;           /* make compiler happy */
    5658                 :           0 :                         break;
    5659                 :             : 
    5660                 :             :                 default:
    5661   [ #  #  #  # ]:           0 :                         elog(ERROR, "unexpected role type %d", role->roletype);
    5662                 :           0 :         }
    5663                 :             : 
    5664                 :         146 :         return tuple;
    5665                 :          73 : }
    5666                 :             : 
    5667                 :             : /*
    5668                 :             :  * Given a RoleSpec, returns a palloc'ed copy of the corresponding role's name.
    5669                 :             :  */
    5670                 :             : char *
    5671                 :           7 : get_rolespec_name(const RoleSpec *role)
    5672                 :             : {
    5673                 :           7 :         HeapTuple       tp;
    5674                 :           7 :         Form_pg_authid authForm;
    5675                 :           7 :         char       *rolename;
    5676                 :             : 
    5677                 :           7 :         tp = get_rolespec_tuple(role);
    5678                 :           7 :         authForm = (Form_pg_authid) GETSTRUCT(tp);
    5679                 :           7 :         rolename = pstrdup(NameStr(authForm->rolname));
    5680                 :           7 :         ReleaseSysCache(tp);
    5681                 :             : 
    5682                 :          14 :         return rolename;
    5683                 :           7 : }
    5684                 :             : 
    5685                 :             : /*
    5686                 :             :  * Given a RoleSpec, throw an error if the name is reserved, using detail_msg,
    5687                 :             :  * if provided (which must be already translated).
    5688                 :             :  *
    5689                 :             :  * If node is NULL, no error is thrown.  If detail_msg is NULL then no detail
    5690                 :             :  * message is provided.
    5691                 :             :  */
    5692                 :             : void
    5693                 :          51 : check_rolespec_name(const RoleSpec *role, const char *detail_msg)
    5694                 :             : {
    5695         [ +  - ]:          51 :         if (!role)
    5696                 :           0 :                 return;
    5697                 :             : 
    5698         [ +  - ]:          51 :         if (role->roletype != ROLESPEC_CSTRING)
    5699                 :           0 :                 return;
    5700                 :             : 
    5701         [ +  - ]:          51 :         if (IsReservedName(role->rolename))
    5702                 :             :         {
    5703         [ #  # ]:           0 :                 if (detail_msg)
    5704   [ #  #  #  # ]:           0 :                         ereport(ERROR,
    5705                 :             :                                         (errcode(ERRCODE_RESERVED_NAME),
    5706                 :             :                                          errmsg("role name \"%s\" is reserved",
    5707                 :             :                                                         role->rolename),
    5708                 :             :                                          errdetail_internal("%s", detail_msg)));
    5709                 :             :                 else
    5710   [ #  #  #  # ]:           0 :                         ereport(ERROR,
    5711                 :             :                                         (errcode(ERRCODE_RESERVED_NAME),
    5712                 :             :                                          errmsg("role name \"%s\" is reserved",
    5713                 :             :                                                         role->rolename)));
    5714                 :           0 :         }
    5715                 :          51 : }
        

Generated by: LCOV version 2.3.2-1