File n_If-auth-with-credentials-for-hostname-fails-retry-with-XAUTHLOCALHOSTNAME.patch of Package libxcb

From: Egbert Eich <eich@suse.de>
Date: Thu Dec 24 00:11:43 2015 +0100
Subject: [PATCH]If auth with credentials for hostname fails retry with XAUTHLOCALHOSTNAME
Patch-mainline: never

References: boo#906622
Signed-off-by: Egbert Eich <eich@suse.com>

Signed-off-by: Egbert Eich <eich@suse.de>
---
 src/xcb_auth.c | 26 ++++++++++++++++++--------
 1 file changed, 18 insertions(+), 8 deletions(-)

diff --git a/src/xcb_auth.c b/src/xcb_auth.c
index 6eb6528..777c772 100644
--- a/src/xcb_auth.c
+++ b/src/xcb_auth.c
@@ -109,7 +109,7 @@ static int authname_match(enum auth_protos kind, char *name, size_t namelen)
 
 #define SIN6_ADDR(s) (&((struct sockaddr_in6 *)s)->sin6_addr)
 
-static Xauth *get_authptr(struct sockaddr *sockname, int display)
+static Xauth *get_authptr(struct sockaddr *sockname, int display, int *stage)
 {
     char *addr = 0;
     int addrlen = 0;
@@ -119,6 +119,8 @@ static Xauth *get_authptr(struct sockaddr *sockname, int display)
     int dispbuflen;
     Xauth *authptr = NULL;
 
+    if (*stage == 2)
+	    return NULL;
     family = FamilyLocal; /* 256 */
     switch(sockname->sa_family)
     {
@@ -161,10 +163,13 @@ static Xauth *get_authptr(struct sockaddr *sockname, int display)
         addrlen = strlen(addr);
     }
 
-    authptr = XauGetBestAuthByAddr (family,
-				   (unsigned short) addrlen, addr,
-				   (unsigned short) dispbuflen, dispbuf,
-				   N_AUTH_PROTOS, authnames, authnameslen);
+    if (*stage == 0) {
+	    authptr = XauGetBestAuthByAddr (family,
+					    (unsigned short) addrlen, addr,
+					    (unsigned short) dispbuflen, dispbuf,
+					    N_AUTH_PROTOS, authnames, authnameslen);
+	    *stage = 1;
+    }
     // && !phostname
     if ((!authptr || !authptr->data_length) && family == FamilyLocal) {
 	if ( (addr = getenv("XAUTHLOCALHOSTNAME")) ) {
@@ -172,6 +177,7 @@ static Xauth *get_authptr(struct sockaddr *sockname, int display)
 					   (unsigned short) strlen(addr), addr,
 					   (unsigned short) dispbuflen, dispbuf,
 					   N_AUTH_PROTOS, authnames, authnameslen);
+	    *stage = 2;
 	}
     }
     return authptr;
@@ -328,6 +334,7 @@ int _xcb_get_auth_info(int fd, xcb_auth_info_t *info, int display)
     int gotsockname = 0;
     Xauth *authptr = 0;
     int ret = 1;
+    int stage = 0;
 
     /* Some systems like hpux or Hurd do not expose peer names
      * for UNIX Domain Sockets, but this is irrelevant,
@@ -344,8 +351,8 @@ int _xcb_get_auth_info(int fd, xcb_auth_info_t *info, int display)
         }
         gotsockname = 1;
     }
-
-    authptr = get_authptr(sockname, display);
+retry:
+    authptr = get_authptr(sockname, display, &stage);
     if (authptr == 0)
     {
         free(sockname);
@@ -371,7 +378,10 @@ int _xcb_get_auth_info(int fd, xcb_auth_info_t *info, int display)
     if(!ret)
     {
         free(info->name);
-        goto no_auth;   /* cannot build auth record */
+	info->name = 0;
+	info->namelen = 0;
+	XauDisposeAuth(authptr);
+        goto retry;   /* cannot build auth record */
     }
 
     free(sockname);