9
9
*
10
10
*
11
11
* IDENTIFICATION
12
- * $PostgreSQL: pgsql/src/backend/commands/variable.c,v 1.98 2004/07/01 00:50:12 tgl Exp $
12
+ * $PostgreSQL: pgsql/src/backend/commands/variable.c,v 1.99 2004/08/11 21:10:36 tgl Exp $
13
13
*
14
14
*-------------------------------------------------------------------------
15
15
*/
@@ -591,31 +591,37 @@ assign_client_encoding(const char *value, bool doit, GucSource source)
591
591
* lookups. Hence, the stored form of the value must provide a numeric userid
592
592
* that can be re-used directly. We store the string in the form of
593
593
* NAMEDATALEN 'x's, followed by T or F to indicate superuserness, followed
594
- * by the numeric userid --- this cannot conflict with any valid user name,
595
- * because of the NAMEDATALEN limit on names.
594
+ * by the numeric userid, followed by a comma, followed by the user name.
595
+ * This cannot be confused with a plain user name because of the NAMEDATALEN
596
+ * limit on names, so we can tell whether we're being passed an initial
597
+ * username or a saved/restored value.
596
598
*/
599
+ extern char * session_authorization_string ; /* in guc.c */
600
+
597
601
const char *
598
602
assign_session_authorization (const char * value , bool doit , GucSource source )
599
603
{
600
604
AclId usesysid = 0 ;
601
605
bool is_superuser = false;
606
+ const char * actual_username = NULL ;
602
607
char * result ;
603
608
604
609
if (strspn (value , "x" ) == NAMEDATALEN &&
605
610
(value [NAMEDATALEN ] == 'T' || value [NAMEDATALEN ] == 'F' ))
606
611
{
607
- /* might be a saved numeric userid */
612
+ /* might be a saved userid string */
613
+ AclId savedsysid ;
608
614
char * endptr ;
609
615
610
- usesysid = (AclId ) strtoul (value + NAMEDATALEN + 1 , & endptr , 10 );
616
+ savedsysid = (AclId ) strtoul (value + NAMEDATALEN + 1 , & endptr , 10 );
611
617
612
- if (endptr != value + NAMEDATALEN + 1 && * endptr == '\0 ' )
618
+ if (endptr != value + NAMEDATALEN + 1 && * endptr == ', ' )
613
619
{
614
- /* syntactically valid, so use the numeric user ID and flag */
620
+ /* syntactically valid, so break out the data */
621
+ usesysid = savedsysid ;
615
622
is_superuser = (value [NAMEDATALEN ] == 'T' );
623
+ actual_username = endptr + 1 ;
616
624
}
617
- else
618
- usesysid = 0 ;
619
625
}
620
626
621
627
if (usesysid == 0 )
@@ -647,22 +653,24 @@ assign_session_authorization(const char *value, bool doit, GucSource source)
647
653
648
654
usesysid = ((Form_pg_shadow ) GETSTRUCT (userTup ))-> usesysid ;
649
655
is_superuser = ((Form_pg_shadow ) GETSTRUCT (userTup ))-> usesuper ;
656
+ actual_username = value ;
650
657
651
658
ReleaseSysCache (userTup );
652
659
}
653
660
654
661
if (doit )
655
662
SetSessionAuthorization (usesysid , is_superuser );
656
663
657
- result = (char * ) malloc (NAMEDATALEN + 32 );
664
+ result = (char * ) malloc (NAMEDATALEN + 32 + strlen ( actual_username ) );
658
665
if (!result )
659
666
return NULL ;
660
667
661
668
memset (result , 'x' , NAMEDATALEN );
662
669
663
- snprintf (result + NAMEDATALEN , 32 , "%c%lu" ,
664
- is_superuser ? 'T' : 'F' ,
665
- (unsigned long ) usesysid );
670
+ sprintf (result + NAMEDATALEN , "%c%lu,%s" ,
671
+ is_superuser ? 'T' : 'F' ,
672
+ (unsigned long ) usesysid ,
673
+ actual_username );
666
674
667
675
return result ;
668
676
}
@@ -671,8 +679,19 @@ const char *
671
679
show_session_authorization (void )
672
680
{
673
681
/*
674
- * We can't use the stored string; see comments for
682
+ * Extract the user name from the stored string; see
675
683
* assign_session_authorization
676
684
*/
677
- return GetUserNameFromId (GetSessionUserId ());
685
+ const char * value = session_authorization_string ;
686
+ AclId savedsysid ;
687
+ char * endptr ;
688
+
689
+ Assert (strspn (value , "x" ) == NAMEDATALEN &&
690
+ (value [NAMEDATALEN ] == 'T' || value [NAMEDATALEN ] == 'F' ));
691
+
692
+ savedsysid = (AclId ) strtoul (value + NAMEDATALEN + 1 , & endptr , 10 );
693
+
694
+ Assert (endptr != value + NAMEDATALEN + 1 && * endptr == ',' );
695
+
696
+ return endptr + 1 ;
678
697
}
0 commit comments