diff options
| author | Peter Eisentraut | 2000-09-19 18:18:04 +0000 |
|---|---|---|
| committer | Peter Eisentraut | 2000-09-19 18:18:04 +0000 |
| commit | 457ac0331cd3e28ecc5d783e7504645837c41a1d (patch) | |
| tree | ba4676d77b28de759955919a8dc14303fb7330bc /src/backend/utils | |
| parent | e9c3f0255fb54600e1c03533cc2e72d78928634d (diff) | |
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.
Diffstat (limited to 'src/backend/utils')
| -rw-r--r-- | src/backend/utils/adt/name.c | 24 | ||||
| -rw-r--r-- | src/backend/utils/init/miscinit.c | 88 | ||||
| -rw-r--r-- | src/backend/utils/init/postinit.c | 6 |
3 files changed, 79 insertions, 39 deletions
diff --git a/src/backend/utils/adt/name.c b/src/backend/utils/adt/name.c index 3acf40e0d4..edc14303d4 100644 --- a/src/backend/utils/adt/name.c +++ b/src/backend/utils/adt/name.c @@ -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 * *****************************************************************************/ diff --git a/src/backend/utils/init/miscinit.c b/src/backend/utils/init/miscinit.c index 20babcc616..0974a05715 100644 --- a/src/backend/utils/init/miscinit.c +++ b/src/backend/utils/init/miscinit.c @@ -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 +SetSessionUserId(Oid newid) +{ + AssertArg(OidIsValid(newid)); + SessionUserId = newid; + /* Current user defaults to session user. */ + if (!OidIsValid(CurrentUserId)) + CurrentUserId = newid; } void -SetUserIdFromUserName(const char *username) +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 diff --git a/src/backend/utils/init/postinit.c b/src/backend/utils/init/postinit.c index a9e083557e..c0502d99ab 100644 --- a/src/backend/utils/init/postinit.c +++ b/src/backend/utils/init/postinit.c @@ -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()); |
