Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit e5d6b91

Browse files
committed
Add SET ROLE. This is a partial commit of Stephen Frost's recent patch;
I'm still working on the has_role function and information_schema changes.
1 parent f5df006 commit e5d6b91

File tree

17 files changed

+533
-97
lines changed

17 files changed

+533
-97
lines changed

doc/src/sgml/func.sgml

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<!--
2-
$PostgreSQL: pgsql/doc/src/sgml/func.sgml,v 1.269 2005/07/22 21:16:14 momjian Exp $
2+
$PostgreSQL: pgsql/doc/src/sgml/func.sgml,v 1.270 2005/07/25 22:12:30 tgl Exp $
33
PostgreSQL documentation
44
-->
55

@@ -8266,7 +8266,9 @@ select current_date + s.a as dates from generate_series(0,14,7) as s(a);
82668266
with <xref linkend="sql-set-session-authorization" endterm="sql-set-session-authorization-title">.
82678267
The <function>current_user</function> is the user identifier
82688268
that is applicable for permission checking. Normally, it is equal
8269-
to the session user, but it changes during the execution of
8269+
to the session user, but it can be changed with
8270+
<xref linkend="sql-set-role" endterm="sql-set-role-title">.
8271+
It also changes during the execution of
82708272
functions with the attribute <literal>SECURITY DEFINER</literal>.
82718273
In Unix parlance, the session user is the <quote>real user</quote> and
82728274
the current user is the <quote>effective user</quote>.

doc/src/sgml/ref/allfiles.sgml

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<!--
2-
$PostgreSQL: pgsql/doc/src/sgml/ref/allfiles.sgml,v 1.63 2005/06/17 22:32:42 tgl Exp $
2+
$PostgreSQL: pgsql/doc/src/sgml/ref/allfiles.sgml,v 1.64 2005/07/25 22:12:31 tgl Exp $
33
PostgreSQL documentation
44
Complete list of usable sgml source files in this directory.
55
-->
@@ -102,6 +102,7 @@ Complete list of usable sgml source files in this directory.
102102
<!entity selectInto system "select_into.sgml">
103103
<!entity set system "set.sgml">
104104
<!entity setConstraints system "set_constraints.sgml">
105+
<!entity setRole system "set_role.sgml">
105106
<!entity setSessionAuth system "set_session_auth.sgml">
106107
<!entity setTransaction system "set_transaction.sgml">
107108
<!entity show system "show.sgml">

doc/src/sgml/ref/pg_dump.sgml

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<!--
2-
$PostgreSQL: pgsql/doc/src/sgml/ref/pg_dump.sgml,v 1.79 2005/07/10 15:08:52 momjian Exp $
2+
$PostgreSQL: pgsql/doc/src/sgml/ref/pg_dump.sgml,v 1.80 2005/07/25 22:12:31 tgl Exp $
33
PostgreSQL documentation
44
-->
55

@@ -474,8 +474,8 @@ PostgreSQL documentation
474474
<term><option>--use-set-session-authorization</></term>
475475
<listitem>
476476
<para>
477-
Output SQL standard SET SESSION AUTHORIZATION commands instead
478-
of OWNER TO commands. This makes the dump more standards compatible,
477+
Output SQL standard SET SESSION AUTHORIZATION commands instead of
478+
ALTER OWNER commands. This makes the dump more standards compatible,
479479
but depending on the history of the objects in the dump, may not
480480
restore properly.
481481
</para>

doc/src/sgml/ref/pg_dumpall.sgml

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<!--
2-
$PostgreSQL: pgsql/doc/src/sgml/ref/pg_dumpall.sgml,v 1.51 2005/06/21 20:45:43 tgl Exp $
2+
$PostgreSQL: pgsql/doc/src/sgml/ref/pg_dumpall.sgml,v 1.52 2005/07/25 22:12:31 tgl Exp $
33
PostgreSQL documentation
44
-->
55

@@ -277,8 +277,8 @@ PostgreSQL documentation
277277
<term><option>--use-set-session-authorization</></term>
278278
<listitem>
279279
<para>
280-
Output SQL standard SET SESSION AUTHORIZATION commands instead
281-
of OWNER TO commands. This makes the dump more standards compatible,
280+
Output SQL standard SET SESSION AUTHORIZATION commands instead of
281+
ALTER OWNER commands. This makes the dump more standards compatible,
282282
but depending on the history of the objects in the dump, may not
283283
restore properly.
284284
</para>

doc/src/sgml/ref/pg_restore.sgml

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<!-- $PostgreSQL: pgsql/doc/src/sgml/ref/pg_restore.sgml,v 1.53 2005/06/21 20:45:43 tgl Exp $ -->
1+
<!-- $PostgreSQL: pgsql/doc/src/sgml/ref/pg_restore.sgml,v 1.54 2005/07/25 22:12:31 tgl Exp $ -->
22

33
<refentry id="APP-PGRESTORE">
44
<refmeta>
@@ -361,8 +361,8 @@
361361
<term><option>--use-set-session-authorization</option></term>
362362
<listitem>
363363
<para>
364-
Output SQL standard SET SESSION AUTHORIZATION commands instead
365-
of OWNER TO commands. This makes the dump more standards compatible,
364+
Output SQL standard SET SESSION AUTHORIZATION commands instead of
365+
ALTER OWNER commands. This makes the dump more standards compatible,
366366
but depending on the history of the objects in the dump, may not
367367
restore properly.
368368
</para>

doc/src/sgml/ref/set_role.sgml

+116
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
<!-- $PostgreSQL: pgsql/doc/src/sgml/ref/set_role.sgml,v 1.1 2005/07/25 22:12:31 tgl Exp $ -->
2+
<refentry id="SQL-SET-ROLE">
3+
<refmeta>
4+
<refentrytitle id="sql-set-role-title">SET ROLE</refentrytitle>
5+
<refmiscinfo>SQL - Language Statements</refmiscinfo>
6+
</refmeta>
7+
8+
<refnamediv>
9+
<refname>SET ROLE</refname>
10+
<refpurpose>set the current user identifier of the current session</refpurpose>
11+
</refnamediv>
12+
13+
<indexterm zone="sql-set-role">
14+
<primary>SET ROLE</primary>
15+
</indexterm>
16+
17+
<refsynopsisdiv>
18+
<synopsis>
19+
SET [ SESSION | LOCAL ] ROLE <replaceable class="parameter">rolename</replaceable>
20+
SET [ SESSION | LOCAL ] ROLE NONE
21+
RESET ROLE
22+
</synopsis>
23+
</refsynopsisdiv>
24+
25+
<refsect1>
26+
<title>Description</title>
27+
28+
<para>
29+
This command sets the current user
30+
identifier of the current SQL-session context to be <replaceable
31+
class="parameter">rolename</replaceable>. The role name may be
32+
written as either an identifier or a string literal. Using this
33+
command, it is possible to either add privileges or restrict one's
34+
privileges.
35+
</para>
36+
37+
<para>
38+
The specified <replaceable class="parameter">rolename</replaceable>
39+
must be a role that the current session user is a member of.
40+
(If the session user is a superuser, any role can be selected.)
41+
</para>
42+
43+
<para>
44+
The <literal>SESSION</> and <literal>LOCAL</> modifiers act the same
45+
as for the regular <xref linkend="SQL-SET" endterm="SQL-SET-title">
46+
command.
47+
</para>
48+
49+
<para>
50+
The <literal>NONE</> and <literal>RESET</> forms reset the current
51+
user identifier to be the current session user identifier.
52+
These forms may be executed by any user.
53+
</para>
54+
</refsect1>
55+
56+
<refsect1>
57+
<title>Examples</title>
58+
59+
<programlisting>
60+
SELECT SESSION_USER, CURRENT_USER;
61+
62+
session_user | current_user
63+
--------------+--------------
64+
peter | peter
65+
66+
SET ROLE 'paul';
67+
68+
SELECT SESSION_USER, CURRENT_USER;
69+
70+
session_user | current_user
71+
--------------+--------------
72+
peter | paul
73+
</programlisting>
74+
</refsect1>
75+
76+
<refsect1>
77+
<title>Compatibility</title>
78+
79+
<para>
80+
<productname>PostgreSQL</productname>
81+
allows identifier syntax (<literal>"rolename"</literal>), while
82+
the SQL standard requires the role name to be written as a string
83+
literal. SQL does not allow this command during a transaction;
84+
<productname>PostgreSQL</productname> does not make this
85+
restriction because there is no reason to.
86+
The <literal>SESSION</> and <literal>LOCAL</> modifiers are a
87+
<productname>PostgreSQL</productname> extension, as is the
88+
<literal>RESET</> syntax.
89+
</para>
90+
</refsect1>
91+
92+
<refsect1>
93+
<title>See Also</title>
94+
95+
<simplelist type="inline">
96+
<member><xref linkend="sql-set-session-authorization" endterm="sql-set-session-authorization-title"></member>
97+
</simplelist>
98+
</refsect1>
99+
</refentry>
100+
101+
<!-- Keep this comment at the end of the file
102+
Local variables:
103+
mode:sgml
104+
sgml-omittag:nil
105+
sgml-shorttag:t
106+
sgml-minimize-attributes:nil
107+
sgml-always-quote-attributes:t
108+
sgml-indent-step:1
109+
sgml-indent-data:t
110+
sgml-parent-document:nil
111+
sgml-default-dtd-file:"../reference.ced"
112+
sgml-exposed-tags:nil
113+
sgml-local-catalogs:("/usr/lib/sgml/catalog")
114+
sgml-local-ecat-files:nil
115+
End:
116+
-->

doc/src/sgml/ref/set_session_auth.sgml

+22-7
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<!-- $PostgreSQL: pgsql/doc/src/sgml/ref/set_session_auth.sgml,v 1.12 2003/11/29 19:51:39 pgsql Exp $ -->
1+
<!-- $PostgreSQL: pgsql/doc/src/sgml/ref/set_session_auth.sgml,v 1.13 2005/07/25 22:12:31 tgl Exp $ -->
22
<refentry id="SQL-SET-SESSION-AUTHORIZATION">
33
<refmeta>
44
<refentrytitle id="sql-set-session-authorization-title">SET SESSION AUTHORIZATION</refentrytitle>
@@ -31,16 +31,17 @@ RESET SESSION AUTHORIZATION
3131
class="parameter">username</replaceable>. The user name may be
3232
written as either an identifier or a string literal. Using this
3333
command, it is possible, for example, to temporarily become an
34-
unprivileged user and later switch back to become a superuser.
34+
unprivileged user and later switch back to being a superuser.
3535
</para>
3636

3737
<para>
3838
The session user identifier is initially set to be the (possibly
3939
authenticated) user name provided by the client. The current user
4040
identifier is normally equal to the session user identifier, but
4141
may change temporarily in the context of <quote>setuid</quote>
42-
functions and similar mechanisms. The current user identifier is
43-
relevant for permission checking.
42+
functions and similar mechanisms; it can also be changed by
43+
<xref linkend="sql-set-role" endterm="sql-set-role-title">.
44+
The current user identifier is relevant for permission checking.
4445
</para>
4546

4647
<para>
@@ -93,10 +94,24 @@ SELECT SESSION_USER, CURRENT_USER;
9394
allows identifier syntax (<literal>"username"</literal>), which SQL
9495
does not. SQL does not allow this command during a transaction;
9596
<productname>PostgreSQL</productname> does not make this
96-
restriction because there is no reason to. The privileges
97-
necessary to execute this command are left implementation-defined
98-
by the standard.
97+
restriction because there is no reason to.
98+
The <literal>SESSION</> and <literal>LOCAL</> modifiers are a
99+
<productname>PostgreSQL</productname> extension, as is the
100+
<literal>RESET</> syntax.
99101
</para>
102+
103+
<para>
104+
The privileges necessary to execute this command are left
105+
implementation-defined by the standard.
106+
</para>
107+
</refsect1>
108+
109+
<refsect1>
110+
<title>See Also</title>
111+
112+
<simplelist type="inline">
113+
<member><xref linkend="sql-set-role" endterm="sql-set-role-title"></member>
114+
</simplelist>
100115
</refsect1>
101116
</refentry>
102117

doc/src/sgml/reference.sgml

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<!-- reference.sgml
2-
$PostgreSQL: pgsql/doc/src/sgml/reference.sgml,v 1.53 2005/06/17 22:32:42 tgl Exp $
2+
$PostgreSQL: pgsql/doc/src/sgml/reference.sgml,v 1.54 2005/07/25 22:12:30 tgl Exp $
33

44
PostgreSQL Reference Manual
55
-->
@@ -134,6 +134,7 @@ PostgreSQL Reference Manual
134134
&selectInto;
135135
&set;
136136
&setConstraints;
137+
&setRole;
137138
&setSessionAuth;
138139
&setTransaction;
139140
&show;

src/backend/access/transam/xact.c

+3-3
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
*
1111
*
1212
* IDENTIFICATION
13-
* $PostgreSQL: pgsql/src/backend/access/transam/xact.c,v 1.210 2005/07/13 22:46:09 tgl Exp $
13+
* $PostgreSQL: pgsql/src/backend/access/transam/xact.c,v 1.211 2005/07/25 22:12:31 tgl Exp $
1414
*
1515
*-------------------------------------------------------------------------
1616
*/
@@ -1865,7 +1865,7 @@ AbortTransaction(void)
18651865

18661866
/*
18671867
* Reset user id which might have been changed transiently. We cannot
1868-
* use s->currentUser, but must get the session userid from
1868+
* use s->currentUser, but must get the session outer-level userid from
18691869
* miscinit.c.
18701870
*
18711871
* (Note: it is not necessary to restore session authorization here
@@ -1874,7 +1874,7 @@ AbortTransaction(void)
18741874
* DEFINER function could send control here with the wrong current
18751875
* userid.)
18761876
*/
1877-
SetUserId(GetSessionUserId());
1877+
SetUserId(GetOuterUserId());
18781878

18791879
/*
18801880
* do abort processing

src/backend/commands/user.c

+23-6
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* Portions Copyright (c) 1996-2005, PostgreSQL Global Development Group
77
* Portions Copyright (c) 1994, Regents of the University of California
88
*
9-
* $PostgreSQL: pgsql/src/backend/commands/user.c,v 1.156 2005/07/07 20:39:58 tgl Exp $
9+
* $PostgreSQL: pgsql/src/backend/commands/user.c,v 1.157 2005/07/25 22:12:31 tgl Exp $
1010
*
1111
*-------------------------------------------------------------------------
1212
*/
@@ -227,7 +227,8 @@ CreateRole(CreateRoleStmt *stmt)
227227
errmsg("permission denied to create role")));
228228
}
229229

230-
if (strcmp(stmt->role, "public") == 0)
230+
if (strcmp(stmt->role, "public") == 0 ||
231+
strcmp(stmt->role, "none") == 0)
231232
ereport(ERROR,
232233
(errcode(ERRCODE_RESERVED_NAME),
233234
errmsg("role name \"%s\" is reserved",
@@ -760,11 +761,15 @@ DropRole(DropRoleStmt *stmt)
760761
if (roleid == GetUserId())
761762
ereport(ERROR,
762763
(errcode(ERRCODE_OBJECT_IN_USE),
763-
errmsg("current role cannot be dropped")));
764+
errmsg("current user cannot be dropped")));
765+
if (roleid == GetOuterUserId())
766+
ereport(ERROR,
767+
(errcode(ERRCODE_OBJECT_IN_USE),
768+
errmsg("current user cannot be dropped")));
764769
if (roleid == GetSessionUserId())
765770
ereport(ERROR,
766771
(errcode(ERRCODE_OBJECT_IN_USE),
767-
errmsg("session role cannot be dropped")));
772+
errmsg("session user cannot be dropped")));
768773

769774
/*
770775
* For safety's sake, we allow createrole holders to drop ordinary
@@ -893,15 +898,20 @@ RenameRole(const char *oldname, const char *newname)
893898
* XXX Client applications probably store the session user somewhere,
894899
* so renaming it could cause confusion. On the other hand, there may
895900
* not be an actual problem besides a little confusion, so think about
896-
* this and decide.
901+
* this and decide. Same for SET ROLE ... we don't restrict renaming
902+
* the current effective userid, though.
897903
*/
898904

899905
roleid = HeapTupleGetOid(oldtuple);
900906

901907
if (roleid == GetSessionUserId())
902908
ereport(ERROR,
903909
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
904-
errmsg("session role may not be renamed")));
910+
errmsg("session user may not be renamed")));
911+
if (roleid == GetOuterUserId())
912+
ereport(ERROR,
913+
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
914+
errmsg("current user may not be renamed")));
905915

906916
/* make sure the new name doesn't exist */
907917
if (SearchSysCacheExists(AUTHNAME,
@@ -911,6 +921,13 @@ RenameRole(const char *oldname, const char *newname)
911921
(errcode(ERRCODE_DUPLICATE_OBJECT),
912922
errmsg("role \"%s\" already exists", newname)));
913923

924+
if (strcmp(newname, "public") == 0 ||
925+
strcmp(newname, "none") == 0)
926+
ereport(ERROR,
927+
(errcode(ERRCODE_RESERVED_NAME),
928+
errmsg("role name \"%s\" is reserved",
929+
newname)));
930+
914931
/*
915932
* createrole is enough privilege unless you want to mess with a superuser
916933
*/

0 commit comments

Comments
 (0)