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

Commit 2c6b34d

Browse files
committed
Add db-local user names, per discussion on hackers.
1 parent 4b26e7d commit 2c6b34d

File tree

7 files changed

+62
-14
lines changed

7 files changed

+62
-14
lines changed

doc/src/sgml/runtime.sgml

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<!--
2-
$Header: /cvsroot/pgsql/doc/src/sgml/runtime.sgml,v 1.125 2002/08/15 14:26:15 momjian Exp $
2+
$Header: /cvsroot/pgsql/doc/src/sgml/runtime.sgml,v 1.126 2002/08/18 03:03:25 momjian Exp $
33
-->
44

55
<Chapter Id="runtime">
@@ -1190,6 +1190,26 @@ env PGOPTIONS='-c geqo=off' psql
11901190
</listitem>
11911191
</varlistentry>
11921192

1193+
<varlistentry>
1194+
<term><varname>DB_USER_NAMESPACE</varname> (<type>boolean</type>)</term>
1195+
<listitem>
1196+
<para>
1197+
This allows per-database user names. You can create users as <literal>
1198+
username@dbname</>. When <literal>username</> is passed by the client,
1199+
<literal>@</> and the database name is appended to the user name and
1200+
that database-specific user name is looked up by the server.
1201+
When creating user names containing <literal>@</>, you will need
1202+
to quote the user name.
1203+
</para>
1204+
<para>
1205+
With this option enabled, you can still create ordinary global
1206+
users. Simply append <literal>@</> when specifying the user name
1207+
in the client. The <literal>@</> will be stripped off and looked up
1208+
by the server.
1209+
</para>
1210+
</listitem>
1211+
</varlistentry>
1212+
11931213
<varlistentry>
11941214
<indexterm>
11951215
<primary>deadlock</primary>

src/backend/libpq/auth.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/libpq/auth.c,v 1.82 2002/06/20 20:29:28 momjian Exp $
11+
* $Header: /cvsroot/pgsql/src/backend/libpq/auth.c,v 1.83 2002/08/18 03:03:25 momjian Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -117,7 +117,7 @@ pg_krb4_recvauth(Port *port)
117117
version, PG_KRB4_VERSION);
118118
return STATUS_ERROR;
119119
}
120-
if (strncmp(port->user, auth_data.pname, SM_USER) != 0)
120+
if (strncmp(port->user, auth_data.pname, SM_DATABASE_USER) != 0)
121121
{
122122
elog(LOG, "pg_krb4_recvauth: name \"%s\" != \"%s\"",
123123
port->user, auth_data.pname);
@@ -290,7 +290,7 @@ pg_krb5_recvauth(Port *port)
290290
}
291291

292292
kusername = pg_an_to_ln(kusername);
293-
if (strncmp(port->user, kusername, SM_USER))
293+
if (strncmp(port->user, kusername, SM_DATABASE_USER))
294294
{
295295
elog(LOG, "pg_krb5_recvauth: user name \"%s\" != krb5 name \"%s\"",
296296
port->user, kusername);

src/backend/postmaster/postmaster.c

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
*
3838
*
3939
* IDENTIFICATION
40-
* $Header: /cvsroot/pgsql/src/backend/postmaster/postmaster.c,v 1.284 2002/08/17 15:12:06 momjian Exp $
40+
* $Header: /cvsroot/pgsql/src/backend/postmaster/postmaster.c,v 1.285 2002/08/18 03:03:25 momjian Exp $
4141
*
4242
* NOTES
4343
*
@@ -116,7 +116,6 @@
116116
sigset_t UnBlockSig,
117117
BlockSig,
118118
AuthBlockSig;
119-
120119
#else
121120
int UnBlockSig,
122121
BlockSig,
@@ -191,6 +190,8 @@ int CheckPointTimeout = 300;
191190
bool HostnameLookup; /* for ps display */
192191
bool ShowPortNumber;
193192
bool Log_connections = false;
193+
bool Db_user_namespace = false;
194+
194195

195196
/* Startup/shutdown state */
196197
static pid_t StartupPID = 0,
@@ -1155,6 +1156,26 @@ ProcessStartupPacket(Port *port, bool SSLdone)
11551156
if (port->user[0] == '\0')
11561157
elog(FATAL, "no PostgreSQL user name specified in startup packet");
11571158

1159+
if (Db_user_namespace)
1160+
{
1161+
/*
1162+
* If user@, it is a global user, remove '@'.
1163+
* We only want to do this if there is an '@' at the end and no
1164+
* earlier in the user string or they may fake as a local user
1165+
* of another database attaching to this database.
1166+
*/
1167+
if (strchr(port->user, '@') == port->user + strlen(port->user)-1)
1168+
*strchr(port->user, '@') = '\0';
1169+
else
1170+
{
1171+
/* Append '@' and dbname */
1172+
char hold_user[SM_DATABASE_USER+1];
1173+
snprintf(hold_user, SM_DATABASE_USER+1, "%s@%s", port->user,
1174+
port->database);
1175+
strcpy(port->user, hold_user);
1176+
}
1177+
}
1178+
11581179
/*
11591180
* If we're going to reject the connection due to database state, say
11601181
* so now instead of wasting cycles on an authentication exchange.
@@ -2581,11 +2602,10 @@ CreateOptsFile(int argc, char *argv[])
25812602
if (FindExec(fullprogname, argv[0], "postmaster") < 0)
25822603
return false;
25832604

2584-
filename = palloc(strlen(DataDir) + 20);
2605+
filename = palloc(strlen(DataDir) + 17);
25852606
sprintf(filename, "%s/postmaster.opts", DataDir);
25862607

2587-
fp = fopen(filename, "w");
2588-
if (fp == NULL)
2608+
if ((fp = fopen(filename, "w")) == NULL)
25892609
{
25902610
postmaster_error("cannot create file %s: %s",
25912611
filename, strerror(errno));

src/backend/utils/misc/guc.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* command, configuration file, and command line options.
66
* See src/backend/utils/misc/README for more information.
77
*
8-
* $Header: /cvsroot/pgsql/src/backend/utils/misc/guc.c,v 1.82 2002/08/15 02:51:26 momjian Exp $
8+
* $Header: /cvsroot/pgsql/src/backend/utils/misc/guc.c,v 1.83 2002/08/18 03:03:25 momjian Exp $
99
*
1010
* Copyright 2000 by PostgreSQL Global Development Group
1111
* Written by Peter Eisentraut <peter_e@gmx.net>.
@@ -483,6 +483,10 @@ static struct config_bool
483483
{ "transform_null_equals", PGC_USERSET }, &Transform_null_equals,
484484
false, NULL, NULL
485485
},
486+
{
487+
{ "db_user_namespace", PGC_SIGHUP }, &Db_user_namespace,
488+
false, NULL, NULL
489+
},
486490

487491
{
488492
{ NULL, 0 }, NULL, false, NULL, NULL

src/backend/utils/misc/postgresql.conf.sample

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,6 @@
113113
#
114114
# Message display
115115
#
116-
117116
#server_min_messages = notice # Values, in order of decreasing detail:
118117
# debug5, debug4, debug3, debug2, debug1,
119118
# info, notice, warning, error, log, fatal,
@@ -201,3 +200,4 @@
201200
#sql_inheritance = true
202201
#transform_null_equals = false
203202
#statement_timeout = 0 # 0 is disabled
203+
#db_user_namespace = false

src/include/libpq/libpq-be.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
* Portions Copyright (c) 1996-2002, PostgreSQL Global Development Group
1212
* Portions Copyright (c) 1994, Regents of the University of California
1313
*
14-
* $Id: libpq-be.h,v 1.32 2002/06/20 20:29:49 momjian Exp $
14+
* $Id: libpq-be.h,v 1.33 2002/08/18 03:03:26 momjian Exp $
1515
*
1616
*-------------------------------------------------------------------------
1717
*/
@@ -59,7 +59,7 @@ typedef struct Port
5959

6060
ProtocolVersion proto;
6161
char database[SM_DATABASE + 1];
62-
char user[SM_USER + 1];
62+
char user[SM_DATABASE_USER + 1];
6363
char options[SM_OPTIONS + 1];
6464
char tty[SM_TTY + 1];
6565
char auth_arg[MAX_AUTH_ARG];

src/include/libpq/pqcomm.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
* Portions Copyright (c) 1996-2002, PostgreSQL Global Development Group
1010
* Portions Copyright (c) 1994, Regents of the University of California
1111
*
12-
* $Id: pqcomm.h,v 1.65 2002/08/12 14:35:26 tgl Exp $
12+
* $Id: pqcomm.h,v 1.66 2002/08/18 03:03:26 momjian Exp $
1313
*
1414
*-------------------------------------------------------------------------
1515
*/
@@ -114,6 +114,8 @@ typedef uint32 PacketLen;
114114
#define SM_DATABASE 64
115115
/* SM_USER should be the same size as the others. bjm 2002-06-02 */
116116
#define SM_USER 32
117+
/* We append database name if db_user_namespace true. */
118+
#define SM_DATABASE_USER (SM_DATABASE+SM_USER+1) /* +1 for @ */
117119
#define SM_OPTIONS 64
118120
#define SM_UNUSED 64
119121
#define SM_TTY 64
@@ -124,12 +126,14 @@ typedef struct StartupPacket
124126
{
125127
ProtocolVersion protoVersion; /* Protocol version */
126128
char database[SM_DATABASE]; /* Database name */
129+
/* Db_user_namespace appends dbname */
127130
char user[SM_USER]; /* User name */
128131
char options[SM_OPTIONS]; /* Optional additional args */
129132
char unused[SM_UNUSED]; /* Unused */
130133
char tty[SM_TTY]; /* Tty for debug output */
131134
} StartupPacket;
132135

136+
extern bool Db_user_namespace;
133137

134138
/* These are the authentication requests sent by the backend. */
135139

0 commit comments

Comments
 (0)