Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit457ac03

Browse files
committed
Implement differentiation between CURRENT_USER and SESSION_USER as per SQL.
There is still no effective difference but it will kick in once setuidfunctions exist (not included here). Make old getpgusername() alias forcurrent_user.
1 parente9c3f02 commit457ac03

File tree

12 files changed

+166
-65
lines changed

12 files changed

+166
-65
lines changed

‎doc/src/sgml/datatype.sgml

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<!--
2-
$Header: /cvsroot/pgsql/doc/src/sgml/datatype.sgml,v 1.35 2000/08/29 20:02:07 momjian Exp $
2+
$Header: /cvsroot/pgsql/doc/src/sgml/datatype.sgml,v 1.36 2000/09/19 18:17:53 petere Exp $
33
-->
44

55
<chapter id="datatype">
@@ -211,11 +211,6 @@ $Header: /cvsroot/pgsql/doc/src/sgml/datatype.sgml,v 1.35 2000/08/29 20:02:07 mo
211211
</row>
212212
</thead>
213213
<tbody>
214-
<row>
215-
<entry>getpgusername()</entry>
216-
<entry>current_user</entry>
217-
<entry>user name in current session</entry>
218-
</row>
219214
<row>
220215
<entry>date('now')</entry>
221216
<entry>current_date</entry>

‎doc/src/sgml/func.sgml

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1509,6 +1509,61 @@ Not defined by this name. Implements the intersection operator '#'
15091509

15101510
</sect1>
15111511

1512+
<sect1>
1513+
<title id="misc-funcs">Miscellaneous Functions</>
1514+
1515+
<table>
1516+
<title>Miscellaneous Functions</>
1517+
<tgroup cols="3">
1518+
<thead>
1519+
<row><entry>Name</> <entry>Return type</> <entry>Description</></row>
1520+
</thead>
1521+
1522+
<tbody>
1523+
<row>
1524+
<entry>current_user</>
1525+
<entry>name</>
1526+
<entry>user name of current execution context</>
1527+
</row>
1528+
<row>
1529+
<entry>user</>
1530+
<entry>name</>
1531+
<entry>equivalent to <function>current_user</></>
1532+
</row>
1533+
<row>
1534+
<entry>session_user</>
1535+
<entry>name</>
1536+
<entry>session user name</>
1537+
</row>
1538+
</tbody>
1539+
</tgroup>
1540+
</table>
1541+
1542+
<para>
1543+
The <function>session_user</> is the user that initiated a database
1544+
connection and is fixed for the duration of that connection. The
1545+
<function>current_user</> is the user identifier that is applicable
1546+
for permission checking. Currently it is always equal to the session
1547+
user, but in the future there might be <quote>setuid</> functions and
1548+
other facilities to allow the current user to change temporarily.
1549+
In Unix parlance, the session user is the <quote>real user</>
1550+
and the current user is the <quote>effective user</>.
1551+
</para>
1552+
1553+
<para>
1554+
Note that these functions have special syntactic status in <acronym>SQL</>;
1555+
they must be called without trailing parentheses.
1556+
</para>
1557+
1558+
<note>
1559+
<title>Deprecated</>
1560+
<para>
1561+
The function <function>getpgusername()</> is an obsolete equivalent
1562+
of <function>current_user</>.
1563+
</para>
1564+
</note>
1565+
</sect1>
1566+
15121567
<sect1>
15131568
<title id="aggregate-funcs">Aggregate Functions</title>
15141569

‎src/backend/commands/user.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* Portions Copyright (c) 1996-2000, PostgreSQL, Inc
77
* Portions Copyright (c) 1994, Regents of the University of California
88
*
9-
* $Header: /cvsroot/pgsql/src/backend/commands/user.c,v 1.67 2000/08/27 21:50:17 tgl Exp $
9+
* $Header: /cvsroot/pgsql/src/backend/commands/user.c,v 1.68 2000/09/19 18:17:54 petere Exp $
1010
*
1111
*-------------------------------------------------------------------------
1212
*/
@@ -348,7 +348,7 @@ AlterUser(AlterUserStmt *stmt)
348348
/* must be superuser or just want to change your own password */
349349
if (!superuser()&&
350350
!(stmt->createdb==0&&stmt->createuser==0&& !stmt->validUntil
351-
&&stmt->password&&strcmp(GetPgUserName(),stmt->user)==0))
351+
&&stmt->password&&strcmp(GetUserName(GetUserId()),stmt->user)==0))
352352
elog(ERROR,"ALTER USER: permission denied");
353353

354354
/* changes to the flat password file cannot be rolled back */

‎src/backend/parser/gram.y

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
*
1212
*
1313
* IDENTIFICATION
14-
* $Header: /cvsroot/pgsql/src/backend/parser/gram.y,v 2.190 2000/09/15 18:45:30 tgl Exp $
14+
* $Header: /cvsroot/pgsql/src/backend/parser/gram.y,v 2.191 2000/09/19 18:17:55 petere Exp $
1515
*
1616
* HISTORY
1717
* AUTHORDATEMAJOR EVENT
@@ -4993,7 +4993,7 @@ c_expr: attr
49934993
|CURRENT_USER
49944994
{
49954995
FuncCall *n = makeNode(FuncCall);
4996-
n->funcname ="getpgusername";
4996+
n->funcname ="current_user";
49974997
n->args = NIL;
49984998
n->agg_star =FALSE;
49994999
n->agg_distinct =FALSE;
@@ -5002,7 +5002,7 @@ c_expr: attr
50025002
|SESSION_USER
50035003
{
50045004
FuncCall *n = makeNode(FuncCall);
5005-
n->funcname ="getpgusername";
5005+
n->funcname ="session_user";
50065006
n->args = NIL;
50075007
n->agg_star =FALSE;
50085008
n->agg_distinct =FALSE;
@@ -5011,7 +5011,7 @@ c_expr: attr
50115011
|USER
50125012
{
50135013
FuncCall *n = makeNode(FuncCall);
5014-
n->funcname ="getpgusername";
5014+
n->funcname ="current_user";
50155015
n->args = NIL;
50165016
n->agg_star =FALSE;
50175017
n->agg_distinct =FALSE;

‎src/backend/utils/adt/name.c

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
*
1313
*
1414
* IDENTIFICATION
15-
* $Header: /cvsroot/pgsql/src/backend/utils/adt/name.c,v 1.29 2000/08/03 16:34:22 tgl Exp $
15+
* $Header: /cvsroot/pgsql/src/backend/utils/adt/name.c,v 1.30 2000/09/19 18:17:56 petere Exp $
1616
*
1717
*-------------------------------------------------------------------------
1818
*/
@@ -136,13 +136,6 @@ namege(PG_FUNCTION_ARGS)
136136
PG_RETURN_BOOL(strncmp(NameStr(*arg1),NameStr(*arg2),NAMEDATALEN) >=0);
137137
}
138138

139-
/* SQL-function interface to GetPgUserName() */
140-
Datum
141-
getpgusername(PG_FUNCTION_ARGS)
142-
{
143-
PG_RETURN_DATUM(DirectFunctionCall1(namein,
144-
CStringGetDatum(GetPgUserName())));
145-
}
146139

147140
/* (see char.c for comparison/operation routines) */
148141

@@ -218,6 +211,21 @@ namestrcmp(Name name, const char *str)
218211
returnstrncmp(NameStr(*name),str,NAMEDATALEN);
219212
}
220213

214+
215+
/* SQL-functions CURRENT_USER and SESSION_USER */
216+
Datum
217+
current_user(PG_FUNCTION_ARGS)
218+
{
219+
PG_RETURN_DATUM(DirectFunctionCall1(namein,CStringGetDatum(GetUserName(GetUserId()))));
220+
}
221+
222+
Datum
223+
session_user(PG_FUNCTION_ARGS)
224+
{
225+
PG_RETURN_DATUM(DirectFunctionCall1(namein,CStringGetDatum(GetUserName(GetSessionUserId()))));
226+
}
227+
228+
221229
/*****************************************************************************
222230
* PRIVATE ROUTINES *
223231
*****************************************************************************/

‎src/backend/utils/init/miscinit.c

Lines changed: 60 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $Header: /cvsroot/pgsql/src/backend/utils/init/miscinit.c,v 1.54 2000/09/06 14:15:22 petere Exp $
11+
* $Header: /cvsroot/pgsql/src/backend/utils/init/miscinit.c,v 1.55 2000/09/19 18:17:57 petere Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -272,50 +272,65 @@ convertstr(unsigned char *buff, int len, int dest)
272272

273273
#endif
274274

275-
/* ----------------
276-
*GetPgUserName
277-
* ----------------
278-
*/
279-
char*
280-
GetPgUserName(void)
281-
{
282-
HeapTupletuple;
283-
Oiduserid;
284-
285-
userid=GetUserId();
286-
287-
tuple=SearchSysCacheTuple(SHADOWSYSID,ObjectIdGetDatum(userid),0,0,0);
288-
if (!HeapTupleIsValid(tuple))
289-
elog(ERROR,"invalid user id %u", (unsigned)userid);
290-
291-
returnpstrdup(NameStr(((Form_pg_shadow)GETSTRUCT(tuple))->usename) );
292-
}
293275

294276

295277
/* ----------------------------------------------------------------
296-
*GetUserId and SetUserId
278+
* User ID things
279+
*
280+
* The session user is determined at connection start and never
281+
* changes. The current user may change when "setuid" functions
282+
* are implemented. Conceptually there is a stack, whose bottom
283+
* is the session user. You are yourself responsible to save and
284+
* restore the current user id if you need to change it.
297285
* ----------------------------------------------------------------
298286
*/
299-
staticOidUserId=InvalidOid;
287+
staticOidCurrentUserId=InvalidOid;
288+
staticOidSessionUserId=InvalidOid;
300289

301290

291+
/*
292+
* This function is relevant for all privilege checks.
293+
*/
302294
Oid
303-
GetUserId()
295+
GetUserId(void)
304296
{
305-
AssertState(OidIsValid(UserId));
306-
returnUserId;
297+
AssertState(OidIsValid(CurrentUserId));
298+
returnCurrentUserId;
307299
}
308300

309301

310302
void
311303
SetUserId(Oidnewid)
312304
{
313-
UserId=newid;
305+
AssertArg(OidIsValid(newid));
306+
CurrentUserId=newid;
307+
}
308+
309+
310+
/*
311+
* This value is only relevant for informational purposes.
312+
*/
313+
Oid
314+
GetSessionUserId(void)
315+
{
316+
AssertState(OidIsValid(SessionUserId));
317+
returnSessionUserId;
318+
}
319+
320+
321+
void
322+
SetSessionUserId(Oidnewid)
323+
{
324+
AssertArg(OidIsValid(newid));
325+
SessionUserId=newid;
326+
/* Current user defaults to session user. */
327+
if (!OidIsValid(CurrentUserId))
328+
CurrentUserId=newid;
314329
}
315330

316331

317332
void
318-
SetUserIdFromUserName(constchar*username)
333+
SetSessionUserIdFromUserName(constchar*username)
319334
{
320335
HeapTupleuserTup;
321336

@@ -330,13 +345,30 @@ SetUserIdFromUserName(const char *username)
330345
0,0,0);
331346
if (!HeapTupleIsValid(userTup))
332347
elog(FATAL,"user \"%s\" does not exist",username);
333-
SetUserId( ((Form_pg_shadow)GETSTRUCT(userTup))->usesysid );
348+
SetSessionUserId( ((Form_pg_shadow)GETSTRUCT(userTup))->usesysid );
334349
}
335350

336351

352+
/*
353+
* Get user name from user id
354+
*/
355+
char*
356+
GetUserName(Oiduserid)
357+
{
358+
HeapTupletuple;
359+
360+
tuple=SearchSysCacheTuple(SHADOWSYSID,ObjectIdGetDatum(userid),0,0,0);
361+
if (!HeapTupleIsValid(tuple))
362+
elog(ERROR,"invalid user id %u", (unsigned)userid);
363+
364+
returnpstrdup(NameStr(((Form_pg_shadow)GETSTRUCT(tuple))->usename) );
365+
}
366+
367+
368+
337369
/*-------------------------------------------------------------------------
338370
*
339-
*posmaster pid file stuffs. $DATADIR/postmaster.pid is created when:
371+
*postmaster pid file stuffs. $DATADIR/postmaster.pid is created when:
340372
*
341373
*(1) postmaster starts. In this case pid > 0.
342374
*(2) postgres starts in standalone mode. In this case

‎src/backend/utils/init/postinit.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $Header: /cvsroot/pgsql/src/backend/utils/init/postinit.c,v 1.65 2000/09/06 14:15:22 petere Exp $
11+
* $Header: /cvsroot/pgsql/src/backend/utils/init/postinit.c,v 1.66 2000/09/19 18:17:57 petere Exp $
1212
*
1313
*
1414
*-------------------------------------------------------------------------
@@ -374,9 +374,9 @@ InitPostgres(const char *dbname, const char *username)
374374
* user id.
375375
*/
376376
if (bootstrap)
377-
SetUserId(geteuid());
377+
SetSessionUserId(geteuid());
378378
else
379-
SetUserIdFromUserName(username);
379+
SetSessionUserIdFromUserName(username);
380380

381381
setuid(geteuid());
382382

‎src/include/catalog/catversion.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
* Portions Copyright (c) 1996-2000, PostgreSQL, Inc
3838
* Portions Copyright (c) 1994, Regents of the University of California
3939
*
40-
* $Id: catversion.h,v 1.46 2000/09/15 18:45:27 tgl Exp $
40+
* $Id: catversion.h,v 1.47 2000/09/19 18:18:01 petere Exp $
4141
*
4242
*-------------------------------------------------------------------------
4343
*/
@@ -53,6 +53,6 @@
5353
*/
5454

5555
/*yyyymmddN */
56-
#defineCATALOG_VERSION_NO200009151
56+
#defineCATALOG_VERSION_NO200009191
5757

5858
#endif

‎src/include/catalog/pg_proc.h

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* Portions Copyright (c) 1996-2000, PostgreSQL, Inc
88
* Portions Copyright (c) 1994, Regents of the University of California
99
*
10-
* $Id: pg_proc.h,v 1.166 2000/09/15 18:45:27 tgl Exp $
10+
* $Id: pg_proc.h,v 1.167 2000/09/19 18:18:01 petere Exp $
1111
*
1212
* NOTES
1313
* The script catalog/genbki.sh reads this file and generates .bki
@@ -928,8 +928,8 @@ DATA(insert OID = 681 ( oidvectorgt PGUID 12 f t t t 2 f 16 "30 30" 100 0 0
928928
DESCR("greater-than");
929929

930930
/* OIDS 700 - 799 */
931-
DATA(insertOID=710 (getpgusernamePGUID12ftft0f19"0"10000100getpgusername- ));
932-
DESCR("Return username");
931+
DATA(insertOID=710 (getpgusernamePGUID12ftft0f19"0"10000100current_user- ));
932+
DESCR("deprecated -- use current_user");
933933
DATA(insertOID=711 (userfntestPGUID12fttt1f23"23"10000100userfntest- ));
934934
DESCR("");
935935
DATA(insertOID=713 (oidrandPGUID12ftft2f16"26 23"10000100oidrand- ));
@@ -974,6 +974,12 @@ DESCR("greater-than-or-equal");
974974

975975
DATA(insertOID=744 (array_eqPGUID12fttt2f16"0 0"10000100array_eq-));
976976
DESCR("array equal");
977+
978+
DATA(insertOID=745 (current_userPGUID12ftft0f19"0"10000100current_user- ));
979+
DESCR("current user name");
980+
DATA(insertOID=746 (session_userPGUID12ftft0f19"0"10000100session_user- ));
981+
DESCR("session user name");
982+
977983
DATA(insertOID=747 (array_dimsPGUID12fttt1f25"0"10000100array_dims-));
978984
DESCR("array dimensions");
979985
DATA(insertOID=750 (array_inPGUID12fttt3f23"0 26 23"10000100array_in- ));

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp