443d764
commit 4dc389c6ae95b7bd34e762b5362c8a79fbda7c7c
443d764
Author: Andreas Schneider <asn@samba.org>
443d764
Date:   Wed Dec 21 22:17:22 2016 +0100
443d764
443d764
    auth/credentials: Always set the the realm if we set the principal from the ccache
443d764
    
443d764
    This fixes a bug in gensec_gssapi_client_start() where an invalid realm
443d764
    is used to get a Kerberos ticket.
443d764
    
443d764
    Signed-off-by: Andreas Schneider <asn@samba.org>
443d764
    Reviewed-by: Stefan Metzmacher <metze@samba.org>
443d764
    (cherry picked from commit 30c07065300281e3a67197fe39ed928346480ff7)
443d764
443d764
diff --git a/auth/credentials/credentials_krb5.c b/auth/credentials/credentials_krb5.c
443d764
index 0e68012..1912c48 100644
443d764
--- a/auth/credentials/credentials_krb5.c
443d764
+++ b/auth/credentials/credentials_krb5.c
443d764
@@ -107,7 +107,8 @@ static int cli_credentials_set_from_ccache(struct cli_credentials *cred,
443d764
 					   enum credentials_obtained obtained,
443d764
 					   const char **error_string)
443d764
 {
443d764
-	
443d764
+	bool ok;
443d764
+	char *realm;
443d764
 	krb5_principal princ;
443d764
 	krb5_error_code ret;
443d764
 	char *name;
443d764
@@ -134,11 +135,24 @@ static int cli_credentials_set_from_ccache(struct cli_credentials *cred,
443d764
 		return ret;
443d764
 	}
443d764
 
443d764
-	cli_credentials_set_principal(cred, name, obtained);
443d764
-
443d764
+	ok = cli_credentials_set_principal(cred, name, obtained);
443d764
+	if (!ok) {
443d764
+		krb5_free_principal(ccache->smb_krb5_context->krb5_context, princ);
443d764
+		return ENOMEM;
443d764
+	}
443d764
 	free(name);
443d764
 
443d764
+	realm = smb_krb5_principal_get_realm(ccache->smb_krb5_context->krb5_context,
443d764
+					     princ);
443d764
 	krb5_free_principal(ccache->smb_krb5_context->krb5_context, princ);
443d764
+	if (realm == NULL) {
443d764
+		return ENOMEM;
443d764
+	}
443d764
+	ok = cli_credentials_set_realm(cred, realm, obtained);
443d764
+	SAFE_FREE(realm);
443d764
+	if (!ok) {
443d764
+		return ENOMEM;
443d764
+	}
443d764
 
443d764
 	/* set the ccache_obtained here, as it just got set to UNINITIALISED by the calls above */
443d764
 	cred->ccache_obtained = obtained;