wincred: handle empty username/password correctly

Empty (length 0) usernames and/or passwords, when saved in the Windows Credential Manager, come back as null when reading the credential. One use case for such empty credentials is with NTLM authentication, where empty username and password instruct libcurl to authenticate using the credentials of the currently logged-on user (single sign-on). When locating the relevant credentials, make empty username match null. When outputting the credentials, handle nulls correctly. Signed-off-by: Jakub Bereżański <kuba@berezanscy.pl> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jakub Bereżański committed Oct 30, 2017 at 18:20 UTC 601e1e7897022b1f166c81e58c95e9afa06cac25
1 file changed +8 -2
contrib/credential/wincred/git-credential-wincred.c
+8 -2
@@ -94,6 +94,12 @@ static WCHAR *wusername, *password, *protocol, *host, *path, target[1024];
94 static void write_item(const char *what, LPCWSTR wbuf, int wlen)
95 {
96 char *buf;
97 +
98 + if (!wbuf || !wlen) {
99 + printf("%s=\n", what);
100 + return;
101 + }
102 +
103 int len = WideCharToMultiByte(CP_UTF8, 0, wbuf, wlen, NULL, 0, NULL,
104 FALSE);
105 buf = xmalloc(len);
@@ -160,7 +166,7 @@ static int match_part_last(LPCWSTR *ptarget, LPCWSTR want, LPCWSTR delim)
166 static int match_cred(const CREDENTIALW *cred)
167 {
168 LPCWSTR target = cred->TargetName;
163 - if (wusername && wcscmp(wusername, cred->UserName))
169 + if (wusername && wcscmp(wusername, cred->UserName ? cred->UserName : L""))
170 return 0;
171
172 return match_part(&target, L"git", L":") &&
@@ -183,7 +189,7 @@ static void get_credential(void)
189 for (i = 0; i < num_creds; ++i)
190 if (match_cred(creds[i])) {
191 write_item("username", creds[i]->UserName,
186 - wcslen(creds[i]->UserName));
192 + creds[i]->UserName ? wcslen(creds[i]->UserName) : 0);
193 write_item("password",
194 (LPCWSTR)creds[i]->CredentialBlob,
195 creds[i]->CredentialBlobSize / sizeof(WCHAR));