]> git.ipfire.org Git - thirdparty/git.git/blob - contrib/credential/wincred/git-credential-wincred.c
t9001: fix indentation in test_no_confirm()
[thirdparty/git.git] / contrib / credential / wincred / git-credential-wincred.c
1 /*
2 * A git credential helper that interface with Windows' Credential Manager
3 *
4 */
5 #include <windows.h>
6 #include <stdio.h>
7 #include <io.h>
8 #include <fcntl.h>
9
10 /* common helpers */
11
12 #define ARRAY_SIZE(x) (sizeof(x)/sizeof(x[0]))
13
14 __attribute__((format (printf, 1, 2)))
15 static void die(const char *err, ...)
16 {
17 char msg[4096];
18 va_list params;
19 va_start(params, err);
20 vsnprintf(msg, sizeof(msg), err, params);
21 fprintf(stderr, "%s\n", msg);
22 va_end(params);
23 exit(1);
24 }
25
26 static void *xmalloc(size_t size)
27 {
28 void *ret = malloc(size);
29 if (!ret && !size)
30 ret = malloc(1);
31 if (!ret)
32 die("Out of memory");
33 return ret;
34 }
35
36 /* MinGW doesn't have wincred.h, so we need to define stuff */
37
38 typedef struct _CREDENTIAL_ATTRIBUTEW {
39 LPWSTR Keyword;
40 DWORD Flags;
41 DWORD ValueSize;
42 LPBYTE Value;
43 } CREDENTIAL_ATTRIBUTEW, *PCREDENTIAL_ATTRIBUTEW;
44
45 typedef struct _CREDENTIALW {
46 DWORD Flags;
47 DWORD Type;
48 LPWSTR TargetName;
49 LPWSTR Comment;
50 FILETIME LastWritten;
51 DWORD CredentialBlobSize;
52 LPBYTE CredentialBlob;
53 DWORD Persist;
54 DWORD AttributeCount;
55 PCREDENTIAL_ATTRIBUTEW Attributes;
56 LPWSTR TargetAlias;
57 LPWSTR UserName;
58 } CREDENTIALW, *PCREDENTIALW;
59
60 #define CRED_TYPE_GENERIC 1
61 #define CRED_PERSIST_LOCAL_MACHINE 2
62 #define CRED_MAX_ATTRIBUTES 64
63
64 typedef BOOL (WINAPI *CredWriteWT)(PCREDENTIALW, DWORD);
65 typedef BOOL (WINAPI *CredEnumerateWT)(LPCWSTR, DWORD, DWORD *,
66 PCREDENTIALW **);
67 typedef VOID (WINAPI *CredFreeT)(PVOID);
68 typedef BOOL (WINAPI *CredDeleteWT)(LPCWSTR, DWORD, DWORD);
69
70 static HMODULE advapi;
71 static CredWriteWT CredWriteW;
72 static CredEnumerateWT CredEnumerateW;
73 static CredFreeT CredFree;
74 static CredDeleteWT CredDeleteW;
75
76 static void load_cred_funcs(void)
77 {
78 /* load DLLs */
79 advapi = LoadLibraryExA("advapi32.dll", NULL,
80 LOAD_LIBRARY_SEARCH_SYSTEM32);
81 if (!advapi)
82 die("failed to load advapi32.dll");
83
84 /* get function pointers */
85 CredWriteW = (CredWriteWT)GetProcAddress(advapi, "CredWriteW");
86 CredEnumerateW = (CredEnumerateWT)GetProcAddress(advapi,
87 "CredEnumerateW");
88 CredFree = (CredFreeT)GetProcAddress(advapi, "CredFree");
89 CredDeleteW = (CredDeleteWT)GetProcAddress(advapi, "CredDeleteW");
90 if (!CredWriteW || !CredEnumerateW || !CredFree || !CredDeleteW)
91 die("failed to load functions");
92 }
93
94 static WCHAR *wusername, *password, *protocol, *host, *path, target[1024];
95
96 static void write_item(const char *what, LPCWSTR wbuf, int wlen)
97 {
98 char *buf;
99
100 if (!wbuf || !wlen) {
101 printf("%s=\n", what);
102 return;
103 }
104
105 int len = WideCharToMultiByte(CP_UTF8, 0, wbuf, wlen, NULL, 0, NULL,
106 FALSE);
107 buf = xmalloc(len);
108
109 if (!WideCharToMultiByte(CP_UTF8, 0, wbuf, wlen, buf, len, NULL, FALSE))
110 die("WideCharToMultiByte failed!");
111
112 printf("%s=", what);
113 fwrite(buf, 1, len, stdout);
114 putchar('\n');
115 free(buf);
116 }
117
118 /*
119 * Match an (optional) expected string and a delimiter in the target string,
120 * consuming the matched text by updating the target pointer.
121 */
122
123 static LPCWSTR wcsstr_last(LPCWSTR str, LPCWSTR find)
124 {
125 LPCWSTR res = NULL, pos;
126 for (pos = wcsstr(str, find); pos; pos = wcsstr(pos + 1, find))
127 res = pos;
128 return res;
129 }
130
131 static int match_part_with_last(LPCWSTR *ptarget, LPCWSTR want, LPCWSTR delim, int last)
132 {
133 LPCWSTR delim_pos, start = *ptarget;
134 int len;
135
136 /* find start of delimiter (or end-of-string if delim is empty) */
137 if (*delim)
138 delim_pos = last ? wcsstr_last(start, delim) : wcsstr(start, delim);
139 else
140 delim_pos = start + wcslen(start);
141
142 /*
143 * match text up to delimiter, or end of string (e.g. the '/' after
144 * host is optional if not followed by a path)
145 */
146 if (delim_pos)
147 len = delim_pos - start;
148 else
149 len = wcslen(start);
150
151 /* update ptarget if we either found a delimiter or need a match */
152 if (delim_pos || want)
153 *ptarget = delim_pos ? delim_pos + wcslen(delim) : start + len;
154
155 return !want || (!wcsncmp(want, start, len) && !want[len]);
156 }
157
158 static int match_part(LPCWSTR *ptarget, LPCWSTR want, LPCWSTR delim)
159 {
160 return match_part_with_last(ptarget, want, delim, 0);
161 }
162
163 static int match_part_last(LPCWSTR *ptarget, LPCWSTR want, LPCWSTR delim)
164 {
165 return match_part_with_last(ptarget, want, delim, 1);
166 }
167
168 static int match_cred(const CREDENTIALW *cred)
169 {
170 LPCWSTR target = cred->TargetName;
171 if (wusername && wcscmp(wusername, cred->UserName ? cred->UserName : L""))
172 return 0;
173
174 return match_part(&target, L"git", L":") &&
175 match_part(&target, protocol, L"://") &&
176 match_part_last(&target, wusername, L"@") &&
177 match_part(&target, host, L"/") &&
178 match_part(&target, path, L"");
179 }
180
181 static void get_credential(void)
182 {
183 CREDENTIALW **creds;
184 DWORD num_creds;
185 int i;
186
187 if (!CredEnumerateW(L"git:*", 0, &num_creds, &creds))
188 return;
189
190 /* search for the first credential that matches username */
191 for (i = 0; i < num_creds; ++i)
192 if (match_cred(creds[i])) {
193 write_item("username", creds[i]->UserName,
194 creds[i]->UserName ? wcslen(creds[i]->UserName) : 0);
195 write_item("password",
196 (LPCWSTR)creds[i]->CredentialBlob,
197 creds[i]->CredentialBlobSize / sizeof(WCHAR));
198 break;
199 }
200
201 CredFree(creds);
202 }
203
204 static void store_credential(void)
205 {
206 CREDENTIALW cred;
207
208 if (!wusername || !password)
209 return;
210
211 cred.Flags = 0;
212 cred.Type = CRED_TYPE_GENERIC;
213 cred.TargetName = target;
214 cred.Comment = L"saved by git-credential-wincred";
215 cred.CredentialBlobSize = (wcslen(password)) * sizeof(WCHAR);
216 cred.CredentialBlob = (LPVOID)password;
217 cred.Persist = CRED_PERSIST_LOCAL_MACHINE;
218 cred.AttributeCount = 0;
219 cred.Attributes = NULL;
220 cred.TargetAlias = NULL;
221 cred.UserName = wusername;
222
223 if (!CredWriteW(&cred, 0))
224 die("CredWrite failed");
225 }
226
227 static void erase_credential(void)
228 {
229 CREDENTIALW **creds;
230 DWORD num_creds;
231 int i;
232
233 if (!CredEnumerateW(L"git:*", 0, &num_creds, &creds))
234 return;
235
236 for (i = 0; i < num_creds; ++i) {
237 if (match_cred(creds[i]))
238 CredDeleteW(creds[i]->TargetName, creds[i]->Type, 0);
239 }
240
241 CredFree(creds);
242 }
243
244 static WCHAR *utf8_to_utf16_dup(const char *str)
245 {
246 int wlen = MultiByteToWideChar(CP_UTF8, 0, str, -1, NULL, 0);
247 WCHAR *wstr = xmalloc(sizeof(WCHAR) * wlen);
248 MultiByteToWideChar(CP_UTF8, 0, str, -1, wstr, wlen);
249 return wstr;
250 }
251
252 static void read_credential(void)
253 {
254 char buf[1024];
255
256 while (fgets(buf, sizeof(buf), stdin)) {
257 char *v;
258 int len = strlen(buf);
259 /* strip trailing CR / LF */
260 while (len && strchr("\r\n", buf[len - 1]))
261 buf[--len] = 0;
262
263 if (!*buf)
264 break;
265
266 v = strchr(buf, '=');
267 if (!v)
268 die("bad input: %s", buf);
269 *v++ = '\0';
270
271 if (!strcmp(buf, "protocol"))
272 protocol = utf8_to_utf16_dup(v);
273 else if (!strcmp(buf, "host"))
274 host = utf8_to_utf16_dup(v);
275 else if (!strcmp(buf, "path"))
276 path = utf8_to_utf16_dup(v);
277 else if (!strcmp(buf, "username")) {
278 wusername = utf8_to_utf16_dup(v);
279 } else if (!strcmp(buf, "password"))
280 password = utf8_to_utf16_dup(v);
281 /*
282 * Ignore other lines; we don't know what they mean, but
283 * this future-proofs us when later versions of git do
284 * learn new lines, and the helpers are updated to match.
285 */
286 }
287 }
288
289 int main(int argc, char *argv[])
290 {
291 const char *usage =
292 "usage: git credential-wincred <get|store|erase>\n";
293
294 if (!argv[1])
295 die(usage);
296
297 /* git use binary pipes to avoid CRLF-issues */
298 _setmode(_fileno(stdin), _O_BINARY);
299 _setmode(_fileno(stdout), _O_BINARY);
300
301 read_credential();
302
303 load_cred_funcs();
304
305 if (!protocol || !(host || path))
306 return 0;
307
308 /* prepare 'target', the unique key for the credential */
309 wcscpy(target, L"git:");
310 wcsncat(target, protocol, ARRAY_SIZE(target));
311 wcsncat(target, L"://", ARRAY_SIZE(target));
312 if (wusername) {
313 wcsncat(target, wusername, ARRAY_SIZE(target));
314 wcsncat(target, L"@", ARRAY_SIZE(target));
315 }
316 if (host)
317 wcsncat(target, host, ARRAY_SIZE(target));
318 if (path) {
319 wcsncat(target, L"/", ARRAY_SIZE(target));
320 wcsncat(target, path, ARRAY_SIZE(target));
321 }
322
323 if (!strcmp(argv[1], "get"))
324 get_credential();
325 else if (!strcmp(argv[1], "store"))
326 store_credential();
327 else if (!strcmp(argv[1], "erase"))
328 erase_credential();
329 /* otherwise, ignore unknown action */
330 return 0;
331 }