mirror of
https://github.com/postgres/postgres.git
synced 2025-11-12 05:01:15 +03:00
Implement differentiation between CURRENT_USER and SESSION_USER as per SQL.
There is still no effective difference but it will kick in once setuid functions exist (not included here). Make old getpgusername() alias for current_user.
This commit is contained in:
@@ -6,7 +6,7 @@
|
||||
* Portions Copyright (c) 1996-2000, PostgreSQL, Inc
|
||||
* Portions Copyright (c) 1994, Regents of the University of California
|
||||
*
|
||||
* $Header: /cvsroot/pgsql/src/backend/commands/user.c,v 1.67 2000/08/27 21:50:17 tgl Exp $
|
||||
* $Header: /cvsroot/pgsql/src/backend/commands/user.c,v 1.68 2000/09/19 18:17:54 petere Exp $
|
||||
*
|
||||
*-------------------------------------------------------------------------
|
||||
*/
|
||||
@@ -348,7 +348,7 @@ AlterUser(AlterUserStmt *stmt)
|
||||
/* must be superuser or just want to change your own password */
|
||||
if (!superuser() &&
|
||||
!(stmt->createdb == 0 && stmt->createuser == 0 && !stmt->validUntil
|
||||
&& stmt->password && strcmp(GetPgUserName(), stmt->user) == 0))
|
||||
&& stmt->password && strcmp(GetUserName(GetUserId()), stmt->user) == 0))
|
||||
elog(ERROR, "ALTER USER: permission denied");
|
||||
|
||||
/* changes to the flat password file cannot be rolled back */
|
||||
|
||||
@@ -11,7 +11,7 @@
|
||||
*
|
||||
*
|
||||
* IDENTIFICATION
|
||||
* $Header: /cvsroot/pgsql/src/backend/parser/gram.y,v 2.190 2000/09/15 18:45:30 tgl Exp $
|
||||
* $Header: /cvsroot/pgsql/src/backend/parser/gram.y,v 2.191 2000/09/19 18:17:55 petere Exp $
|
||||
*
|
||||
* HISTORY
|
||||
* AUTHOR DATE MAJOR EVENT
|
||||
@@ -4993,7 +4993,7 @@ c_expr: attr
|
||||
| CURRENT_USER
|
||||
{
|
||||
FuncCall *n = makeNode(FuncCall);
|
||||
n->funcname = "getpgusername";
|
||||
n->funcname = "current_user";
|
||||
n->args = NIL;
|
||||
n->agg_star = FALSE;
|
||||
n->agg_distinct = FALSE;
|
||||
@@ -5002,7 +5002,7 @@ c_expr: attr
|
||||
| SESSION_USER
|
||||
{
|
||||
FuncCall *n = makeNode(FuncCall);
|
||||
n->funcname = "getpgusername";
|
||||
n->funcname = "session_user";
|
||||
n->args = NIL;
|
||||
n->agg_star = FALSE;
|
||||
n->agg_distinct = FALSE;
|
||||
@@ -5011,7 +5011,7 @@ c_expr: attr
|
||||
| USER
|
||||
{
|
||||
FuncCall *n = makeNode(FuncCall);
|
||||
n->funcname = "getpgusername";
|
||||
n->funcname = "current_user";
|
||||
n->args = NIL;
|
||||
n->agg_star = FALSE;
|
||||
n->agg_distinct = FALSE;
|
||||
|
||||
@@ -12,7 +12,7 @@
|
||||
*
|
||||
*
|
||||
* IDENTIFICATION
|
||||
* $Header: /cvsroot/pgsql/src/backend/utils/adt/name.c,v 1.29 2000/08/03 16:34:22 tgl Exp $
|
||||
* $Header: /cvsroot/pgsql/src/backend/utils/adt/name.c,v 1.30 2000/09/19 18:17:56 petere Exp $
|
||||
*
|
||||
*-------------------------------------------------------------------------
|
||||
*/
|
||||
@@ -136,13 +136,6 @@ namege(PG_FUNCTION_ARGS)
|
||||
PG_RETURN_BOOL(strncmp(NameStr(*arg1), NameStr(*arg2), NAMEDATALEN) >= 0);
|
||||
}
|
||||
|
||||
/* SQL-function interface to GetPgUserName() */
|
||||
Datum
|
||||
getpgusername(PG_FUNCTION_ARGS)
|
||||
{
|
||||
PG_RETURN_DATUM(DirectFunctionCall1(namein,
|
||||
CStringGetDatum(GetPgUserName())));
|
||||
}
|
||||
|
||||
/* (see char.c for comparison/operation routines) */
|
||||
|
||||
@@ -218,6 +211,21 @@ namestrcmp(Name name, const char *str)
|
||||
return strncmp(NameStr(*name), str, NAMEDATALEN);
|
||||
}
|
||||
|
||||
|
||||
/* SQL-functions CURRENT_USER and SESSION_USER */
|
||||
Datum
|
||||
current_user(PG_FUNCTION_ARGS)
|
||||
{
|
||||
PG_RETURN_DATUM(DirectFunctionCall1(namein, CStringGetDatum(GetUserName(GetUserId()))));
|
||||
}
|
||||
|
||||
Datum
|
||||
session_user(PG_FUNCTION_ARGS)
|
||||
{
|
||||
PG_RETURN_DATUM(DirectFunctionCall1(namein, CStringGetDatum(GetUserName(GetSessionUserId()))));
|
||||
}
|
||||
|
||||
|
||||
/*****************************************************************************
|
||||
* PRIVATE ROUTINES *
|
||||
*****************************************************************************/
|
||||
|
||||
@@ -8,7 +8,7 @@
|
||||
*
|
||||
*
|
||||
* IDENTIFICATION
|
||||
* $Header: /cvsroot/pgsql/src/backend/utils/init/miscinit.c,v 1.54 2000/09/06 14:15:22 petere Exp $
|
||||
* $Header: /cvsroot/pgsql/src/backend/utils/init/miscinit.c,v 1.55 2000/09/19 18:17:57 petere Exp $
|
||||
*
|
||||
*-------------------------------------------------------------------------
|
||||
*/
|
||||
@@ -272,50 +272,65 @@ convertstr(unsigned char *buff, int len, int dest)
|
||||
|
||||
#endif
|
||||
|
||||
/* ----------------
|
||||
* GetPgUserName
|
||||
* ----------------
|
||||
*/
|
||||
char *
|
||||
GetPgUserName(void)
|
||||
{
|
||||
HeapTuple tuple;
|
||||
Oid userid;
|
||||
|
||||
userid = GetUserId();
|
||||
|
||||
tuple = SearchSysCacheTuple(SHADOWSYSID, ObjectIdGetDatum(userid), 0, 0, 0);
|
||||
if (!HeapTupleIsValid(tuple))
|
||||
elog(ERROR, "invalid user id %u", (unsigned) userid);
|
||||
|
||||
return pstrdup( NameStr(((Form_pg_shadow) GETSTRUCT(tuple))->usename) );
|
||||
}
|
||||
|
||||
|
||||
/* ----------------------------------------------------------------
|
||||
* GetUserId and SetUserId
|
||||
* User ID things
|
||||
*
|
||||
* The session user is determined at connection start and never
|
||||
* changes. The current user may change when "setuid" functions
|
||||
* are implemented. Conceptually there is a stack, whose bottom
|
||||
* is the session user. You are yourself responsible to save and
|
||||
* restore the current user id if you need to change it.
|
||||
* ----------------------------------------------------------------
|
||||
*/
|
||||
static Oid UserId = InvalidOid;
|
||||
static Oid CurrentUserId = InvalidOid;
|
||||
static Oid SessionUserId = InvalidOid;
|
||||
|
||||
|
||||
/*
|
||||
* This function is relevant for all privilege checks.
|
||||
*/
|
||||
Oid
|
||||
GetUserId()
|
||||
GetUserId(void)
|
||||
{
|
||||
AssertState(OidIsValid(UserId));
|
||||
return UserId;
|
||||
AssertState(OidIsValid(CurrentUserId));
|
||||
return CurrentUserId;
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
SetUserId(Oid newid)
|
||||
{
|
||||
UserId = newid;
|
||||
AssertArg(OidIsValid(newid));
|
||||
CurrentUserId = newid;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* This value is only relevant for informational purposes.
|
||||
*/
|
||||
Oid
|
||||
GetSessionUserId(void)
|
||||
{
|
||||
AssertState(OidIsValid(SessionUserId));
|
||||
return SessionUserId;
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
SetUserIdFromUserName(const char *username)
|
||||
SetSessionUserId(Oid newid)
|
||||
{
|
||||
AssertArg(OidIsValid(newid));
|
||||
SessionUserId = newid;
|
||||
/* Current user defaults to session user. */
|
||||
if (!OidIsValid(CurrentUserId))
|
||||
CurrentUserId = newid;
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
SetSessionUserIdFromUserName(const char *username)
|
||||
{
|
||||
HeapTuple userTup;
|
||||
|
||||
@@ -330,13 +345,30 @@ SetUserIdFromUserName(const char *username)
|
||||
0, 0, 0);
|
||||
if (!HeapTupleIsValid(userTup))
|
||||
elog(FATAL, "user \"%s\" does not exist", username);
|
||||
SetUserId( ((Form_pg_shadow) GETSTRUCT(userTup))->usesysid );
|
||||
SetSessionUserId( ((Form_pg_shadow) GETSTRUCT(userTup))->usesysid );
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* Get user name from user id
|
||||
*/
|
||||
char *
|
||||
GetUserName(Oid userid)
|
||||
{
|
||||
HeapTuple tuple;
|
||||
|
||||
tuple = SearchSysCacheTuple(SHADOWSYSID, ObjectIdGetDatum(userid), 0, 0, 0);
|
||||
if (!HeapTupleIsValid(tuple))
|
||||
elog(ERROR, "invalid user id %u", (unsigned) userid);
|
||||
|
||||
return pstrdup( NameStr(((Form_pg_shadow) GETSTRUCT(tuple))->usename) );
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*-------------------------------------------------------------------------
|
||||
*
|
||||
* posmaster pid file stuffs. $DATADIR/postmaster.pid is created when:
|
||||
* postmaster pid file stuffs. $DATADIR/postmaster.pid is created when:
|
||||
*
|
||||
* (1) postmaster starts. In this case pid > 0.
|
||||
* (2) postgres starts in standalone mode. In this case
|
||||
|
||||
@@ -8,7 +8,7 @@
|
||||
*
|
||||
*
|
||||
* IDENTIFICATION
|
||||
* $Header: /cvsroot/pgsql/src/backend/utils/init/postinit.c,v 1.65 2000/09/06 14:15:22 petere Exp $
|
||||
* $Header: /cvsroot/pgsql/src/backend/utils/init/postinit.c,v 1.66 2000/09/19 18:17:57 petere Exp $
|
||||
*
|
||||
*
|
||||
*-------------------------------------------------------------------------
|
||||
@@ -374,9 +374,9 @@ InitPostgres(const char *dbname, const char *username)
|
||||
* user id.
|
||||
*/
|
||||
if (bootstrap)
|
||||
SetUserId(geteuid());
|
||||
SetSessionUserId(geteuid());
|
||||
else
|
||||
SetUserIdFromUserName(username);
|
||||
SetSessionUserIdFromUserName(username);
|
||||
|
||||
setuid(geteuid());
|
||||
|
||||
|
||||
Reference in New Issue
Block a user