]> granicus.if.org Git - postgresql/commitdiff
Prevent ALTER USER f RESET ALL from removing the settings that were put there
authorAlvaro Herrera <alvherre@alvh.no-ip.org>
Thu, 25 Mar 2010 14:45:06 +0000 (14:45 +0000)
committerAlvaro Herrera <alvherre@alvh.no-ip.org>
Thu, 25 Mar 2010 14:45:06 +0000 (14:45 +0000)
by a superuser -- "ALTER USER f RESET setting" already disallows removing such a
setting.

Apply the same treatment to ALTER DATABASE d RESET ALL when run by a database
owner that's not superuser.

src/backend/commands/dbcommands.c
src/backend/commands/user.c
src/backend/utils/misc/guc.c
src/include/utils/guc.h

index df86095b1a19a835a75e06a865221e8a6ee0fb50..f7bf94883afbaf1da58ed1a76eff4b0cfe355dac 100644 (file)
@@ -13,7 +13,7 @@
  *
  *
  * IDENTIFICATION
- *       $PostgreSQL: pgsql/src/backend/commands/dbcommands.c,v 1.204.2.4 2008/10/09 10:34:22 heikki Exp $
+ *       $PostgreSQL: pgsql/src/backend/commands/dbcommands.c,v 1.204.2.5 2010/03/25 14:45:06 alvherre Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -982,9 +982,30 @@ AlterDatabaseSet(AlterDatabaseSetStmt *stmt)
 
        if (stmt->setstmt->kind == VAR_RESET_ALL)
        {
-               /* RESET ALL, so just set datconfig to null */
-               repl_null[Anum_pg_database_datconfig - 1] = 'n';
-               repl_val[Anum_pg_database_datconfig - 1] = (Datum) 0;
+               ArrayType  *new = NULL;
+               Datum           datum;
+               bool            isnull;
+
+               /*
+                * in RESET ALL, request GUC to reset the settings array; if none
+                * left, we can set datconfig to null; otherwise use the returned
+                * array
+                */
+               datum = heap_getattr(tuple, Anum_pg_database_datconfig,
+                                                        RelationGetDescr(rel), &isnull);
+               if (!isnull)
+                       new = GUCArrayReset(DatumGetArrayTypeP(datum));
+               if (new)
+               {
+                       repl_val[Anum_pg_database_datconfig - 1] = PointerGetDatum(new);
+                       repl_repl[Anum_pg_database_datconfig - 1] = 'r';
+                       repl_null[Anum_pg_database_datconfig - 1] = ' ';
+               }
+               else
+               {
+                       repl_null[Anum_pg_database_datconfig - 1] = 'n';
+                       repl_val[Anum_pg_database_datconfig - 1] = (Datum) 0;
+               }
        }
        else
        {
index 64e5cd8f0b856d6cbd8e24a3d538a1853d448ad8..34e24fd8779c8eb6008a695414424d2fa340c0cd 100644 (file)
@@ -6,7 +6,7 @@
  * Portions Copyright (c) 1996-2008, PostgreSQL Global Development Group
  * Portions Copyright (c) 1994, Regents of the University of California
  *
- * $PostgreSQL: pgsql/src/backend/commands/user.c,v 1.178 2008/01/01 19:45:49 momjian Exp $
+ * $PostgreSQL: pgsql/src/backend/commands/user.c,v 1.178.2.1 2010/03/25 14:45:06 alvherre Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -758,9 +758,30 @@ AlterRoleSet(AlterRoleSetStmt *stmt)
 
        if (stmt->setstmt->kind == VAR_RESET_ALL)
        {
-               /* RESET ALL, so just set rolconfig to null */
-               repl_null[Anum_pg_authid_rolconfig - 1] = 'n';
-               repl_val[Anum_pg_authid_rolconfig - 1] = (Datum) 0;
+               ArrayType  *new = NULL;
+               Datum           datum;
+               bool            isnull;
+
+               /*
+                * in RESET ALL, request GUC to reset the settings array; if none
+                * left, we can set rolconfig to null; otherwise use the returned
+                * array
+                */
+               datum = SysCacheGetAttr(AUTHNAME, oldtuple,
+                                                               Anum_pg_authid_rolconfig, &isnull);
+               if (!isnull)
+                       new = GUCArrayReset(DatumGetArrayTypeP(datum));
+               if (new)
+               {
+                       repl_val[Anum_pg_authid_rolconfig - 1] = PointerGetDatum(new);
+                       repl_repl[Anum_pg_authid_rolconfig - 1] = 'r';
+                       repl_null[Anum_pg_authid_rolconfig - 1] = ' ';
+               }
+               else
+               {
+                       repl_null[Anum_pg_authid_rolconfig - 1] = 'n';
+                       repl_val[Anum_pg_authid_rolconfig - 1] = (Datum) 0;
+               }
        }
        else
        {
index ba6e781c1c0d3dc7c54b06bacaea1b5f10a39805..38dc8a95d5ce53d147f5377db2a901273181c2c4 100644 (file)
@@ -10,7 +10,7 @@
  * Written by Peter Eisentraut <peter_e@gmx.net>.
  *
  * IDENTIFICATION
- *       $PostgreSQL: pgsql/src/backend/utils/misc/guc.c,v 1.432.2.7 2010/02/25 13:26:22 mha Exp $
+ *       $PostgreSQL: pgsql/src/backend/utils/misc/guc.c,v 1.432.2.8 2010/03/25 14:45:06 alvherre Exp $
  *
  *--------------------------------------------------------------------
  */
@@ -6346,6 +6346,7 @@ ProcessGUCArray(ArrayType *array,
                free(name);
                if (value)
                        free(value);
+               pfree(s);
        }
 }
 
@@ -6480,6 +6481,7 @@ GUCArrayDelete(ArrayType *array, const char *name)
                        && val[strlen(name)] == '=')
                        continue;
 
+
                /* else add it to the output array */
                if (newarray)
                {
@@ -6502,6 +6504,85 @@ GUCArrayDelete(ArrayType *array, const char *name)
        return newarray;
 }
 
+/*
+ * Given a GUC array, delete all settings from it that our permission
+ * level allows: if superuser, delete them all; if regular user, only
+ * those that are PGC_USERSET
+ */
+ArrayType *
+GUCArrayReset(ArrayType *array)
+{
+       ArrayType  *newarray;
+       int                     i;
+       int                     index;
+
+       /* if array is currently null, nothing to do */
+       if (!array)
+               return NULL;
+
+       /* if we're superuser, we can delete everything */
+       if (superuser())
+               return NULL;
+
+       newarray = NULL;
+       index = 1;
+
+       for (i = 1; i <= ARR_DIMS(array)[0]; i++)
+       {
+               Datum           d;
+               char       *val;
+               char       *eqsgn;
+               bool            isnull;
+               struct config_generic *gconf;
+
+               d = array_ref(array, 1, &i,
+                                         -1 /* varlenarray */ ,
+                                         -1 /* TEXT's typlen */ ,
+                                         false /* TEXT's typbyval */ ,
+                                         'i' /* TEXT's typalign */ ,
+                                         &isnull);
+
+               if (isnull)
+                       continue;
+               val = DatumGetCString(DirectFunctionCall1(textout, d));
+
+               eqsgn = strchr(val, '=');
+               *eqsgn = '\0';
+
+               gconf = find_option(val, false, WARNING);
+               if (!gconf)
+                       continue;
+
+               /* note: superuser-ness was already checked above */
+               /* skip entry if OK to delete */
+               if (gconf->context == PGC_USERSET)
+                       continue;
+
+               /* XXX do we need to worry about database owner? */
+
+               /* else add it to the output array */
+               if (newarray)
+               {
+                       newarray = array_set(newarray, 1, &index,
+                                                                d,
+                                                                false,
+                                                                -1 /* varlenarray */ ,
+                                                                -1 /* TEXT's typlen */ ,
+                                                                false /* TEXT's typbyval */ ,
+                                                                'i' /* TEXT's typalign */ );
+               }
+               else
+                       newarray = construct_array(&d, 1,
+                                                                          TEXTOID,
+                                                                          -1, false, 'i');
+
+               index++;
+               pfree(val);
+       }
+
+       return newarray;
+}
+
 
 /*
  * assign_hook and show_hook subroutines
index 75dd42aaf918b9e7de1d66162821902fc5392f2f..48318eed6fca8d04dd212ff2c1b2e33f5ee640e9 100644 (file)
@@ -7,7 +7,7 @@
  * Copyright (c) 2000-2008, PostgreSQL Global Development Group
  * Written by Peter Eisentraut <peter_e@gmx.net>.
  *
- * $PostgreSQL: pgsql/src/include/utils/guc.h,v 1.90 2008/01/01 19:45:59 momjian Exp $
+ * $PostgreSQL: pgsql/src/include/utils/guc.h,v 1.90.2.1 2010/03/25 14:45:06 alvherre Exp $
  *--------------------------------------------------------------------
  */
 #ifndef GUC_H
@@ -220,6 +220,7 @@ extern void ProcessGUCArray(ArrayType *array,
                                GucContext context, GucSource source, GucAction action);
 extern ArrayType *GUCArrayAdd(ArrayType *array, const char *name, const char *value);
 extern ArrayType *GUCArrayDelete(ArrayType *array, const char *name);
+extern ArrayType *GUCArrayReset(ArrayType *array);
 
 extern int     GUC_complaint_elevel(GucSource source);