99 *
1010 *
1111 * IDENTIFICATION
12- * $Header: /cvsroot/pgsql/src/backend/commands/dbcommands.c,v 1.85 2002/03/06 06:09:32 momjian Exp $
12+ * $Header: /cvsroot/pgsql/src/backend/commands/dbcommands.c,v 1.86 2002/04/11 05:32:03 petere Exp $
1313 *
1414 *-------------------------------------------------------------------------
1515 */
@@ -49,7 +49,7 @@ static bool get_db_info(const char *name, Oid *dbIdP, int4 *ownerIdP,
4949int * encodingP ,bool * dbIsTemplateP ,Oid * dbLastSysOidP ,
5050TransactionId * dbVacuumXidP ,TransactionId * dbFrozenXidP ,
5151char * dbpath );
52- static bool get_user_info ( Oid use_sysid , bool * use_super , bool * use_createdb );
52+ static bool have_createdb_privilege ( void );
5353static char * resolve_alt_dbpath (const char * dbpath ,Oid dboid );
5454static bool remove_dbdirs (const char * real_loc ,const char * altloc );
5555
@@ -67,8 +67,6 @@ createdb(const char *dbname, const char *dbowner,
6767char * target_dir ;
6868char src_loc [MAXPGPATH ];
6969char buf [2 * MAXPGPATH + 100 ];
70- bool use_super ,
71- use_createdb ;
7270Oid src_dboid ;
7371int4 src_owner ;
7472int src_encoding ;
@@ -91,21 +89,17 @@ createdb(const char *dbname, const char *dbowner,
9189else
9290datdba = GetUserId ();
9391
94- /* check permission to create database */
95- if (!get_user_info (GetUserId (),& use_super ,& use_createdb ))
96- elog (ERROR ,"current user name is invalid" );
97-
9892if (datdba == (int32 )GetUserId ())
9993{
10094/* creating database for self: can be superuser or createdb */
101- if (!use_createdb && !use_super )
95+ if (!superuser () && !have_createdb_privilege () )
10296elog (ERROR ,"CREATE DATABASE: permission denied" );
10397}
10498else
10599{
106100/* creating database for someone else: must be superuser */
107101/* note that the someone else need not have any permissions */
108- if (!use_super )
102+ if (!superuser () )
109103elog (ERROR ,"CREATE DATABASE: permission denied" );
110104}
111105
@@ -143,7 +137,7 @@ createdb(const char *dbname, const char *dbowner,
143137 */
144138if (!src_istemplate )
145139{
146- if (!use_super && GetUserId ()!= src_owner )
140+ if (!superuser () && GetUserId ()!= src_owner )
147141elog (ERROR ,"CREATE DATABASE: permission to copy \"%s\" denied" ,
148142dbtemplate );
149143}
@@ -332,7 +326,6 @@ dropdb(const char *dbname)
332326{
333327int4 db_owner ;
334328bool db_istemplate ;
335- bool use_super ;
336329Oid db_id ;
337330char * alt_loc ;
338331char * nominal_loc ;
@@ -350,9 +343,6 @@ dropdb(const char *dbname)
350343if (IsTransactionBlock ())
351344elog (ERROR ,"DROP DATABASE: may not be called in a transaction block" );
352345
353- if (!get_user_info (GetUserId (),& use_super ,NULL ))
354- elog (ERROR ,"current user name is invalid" );
355-
356346/*
357347 * Obtain exclusive lock on pg_database. We need this to ensure that
358348 * no new backend starts up in the target database while we are
@@ -368,7 +358,7 @@ dropdb(const char *dbname)
368358& db_istemplate ,NULL ,NULL ,NULL ,dbpath ))
369359elog (ERROR ,"DROP DATABASE: database \"%s\" does not exist" ,dbname );
370360
371- if (! use_super && GetUserId ()!= db_owner )
361+ if (GetUserId ()!= db_owner && ! superuser () )
372362elog (ERROR ,"DROP DATABASE: permission denied" );
373363
374364/*
@@ -605,25 +595,23 @@ get_db_info(const char *name, Oid *dbIdP, int4 *ownerIdP,
605595}
606596
607597static bool
608- get_user_info ( Oid use_sysid , bool * use_super , bool * use_createdb )
598+ have_createdb_privilege ( void )
609599{
610600HeapTuple utup ;
601+ bool retval ;
611602
612603utup = SearchSysCache (SHADOWSYSID ,
613- ObjectIdGetDatum (use_sysid ),
604+ ObjectIdGetDatum (GetUserId () ),
6146050 ,0 ,0 );
615606
616607if (!HeapTupleIsValid (utup ))
617- return false;
618-
619- if (use_super )
620- * use_super = ((Form_pg_shadow )GETSTRUCT (utup ))-> usesuper ;
621- if (use_createdb )
622- * use_createdb = ((Form_pg_shadow )GETSTRUCT (utup ))-> usecreatedb ;
608+ retval = true;
609+ else
610+ retval = ((Form_pg_shadow )GETSTRUCT (utup ))-> usecreatedb ;
623611
624612ReleaseSysCache (utup );
625613
626- return true ;
614+ return retval ;
627615}
628616
629617