Commit ba55592fdfda5ae63584c73dbe1effa9779201c4

Edward Thomson 2018-08-02T20:34:56

Merge pull request #4743 from Agent00Log/dev/winbugfixes Windows: default credentials / fallback credential handling

diff --git a/src/transports/winhttp.c b/src/transports/winhttp.c
index 1a317f6..3df892d 100644
--- a/src/transports/winhttp.c
+++ b/src/transports/winhttp.c
@@ -184,10 +184,10 @@ static int apply_default_credentials(HINTERNET request, int mechanisms)
 	DWORD native_scheme = 0;
 
 	if ((mechanisms & GIT_WINHTTP_AUTH_NTLM) != 0)
-		native_scheme |= WINHTTP_AUTH_SCHEME_NTLM;
+		native_scheme = WINHTTP_AUTH_SCHEME_NTLM;
 
 	if ((mechanisms & GIT_WINHTTP_AUTH_NEGOTIATE) != 0)
-		native_scheme |= WINHTTP_AUTH_SCHEME_NEGOTIATE;
+		native_scheme = WINHTTP_AUTH_SCHEME_NEGOTIATE;
 
 	if (!native_scheme) {
 		giterr_set(GITERR_NET, "invalid authentication scheme");
@@ -219,6 +219,7 @@ static int fallback_cred_acquire_cb(
 	 * as an authentication mechanism */
 	if (GIT_CREDTYPE_DEFAULT & allowed_types) {
 		wchar_t *wide_url;
+		HRESULT hCoInitResult;
 
 		/* Convert URL to wide characters */
 		if (git__utf8_to_16_alloc(&wide_url, url) < 0) {
@@ -226,7 +227,9 @@ static int fallback_cred_acquire_cb(
 			return -1;
 		}
 
-		if (SUCCEEDED(CoInitializeEx(NULL, COINIT_MULTITHREADED))) {
+		hCoInitResult = CoInitializeEx(NULL, COINIT_MULTITHREADED);
+			
+		if (SUCCEEDED(hCoInitResult) || hCoInitResult == RPC_E_CHANGED_MODE) {
 			IInternetSecurityManager* pISM;
 
 			/* And if the target URI is in the My Computer, Intranet, or Trusted zones */
@@ -250,7 +253,9 @@ static int fallback_cred_acquire_cb(
 				pISM->lpVtbl->Release(pISM);
 			}
 
-			CoUninitialize();
+            if (SUCCEEDED(hCoInitResult))
+                /* Only unitialize if the call to CoInitializeEx was successful. */
+                CoUninitialize();
 		}
 
 		git__free(wide_url);