-
Notifications
You must be signed in to change notification settings - Fork 85
/
Program.cs
377 lines (341 loc) · 14.3 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Runtime.InteropServices;
using System.Text.RegularExpressions;
using System.ComponentModel;
using System.Diagnostics;
using System.Threading;
using System.Windows.Forms;
namespace Git.Credential.WinStore
{
class Program
{
private static Dictionary<string, Func<IDictionary<string, string>, IEnumerable<Tuple<string, string>>>> _commands = new Dictionary<string, Func<IDictionary<string, string>, IEnumerable<Tuple<string, string>>>>(StringComparer.OrdinalIgnoreCase)
{
{ "get", GetCommand },
{ "store", StoreCommand },
{ "erase", EraseCommand }
};
static void Main(string[] args)
{
TryLaunchDebugger(ref args);
if (TrySilentInstall(ref args)) { return; }
// Parse command
Func<IDictionary<string, string>, IEnumerable<Tuple<string, string>>> command = null;
string cmd;
if (args.Length == 0)
{
InstallTheApp(silent: false);
return;
}
cmd = args[0];
IDictionary<string, string> parameters = ReadGitParameters();
if (!_commands.TryGetValue(cmd, out command))
{
WriteUsage();
return;
}
IDictionary<string, string> response = command(parameters).ToDictionary(
t => t.Item1,
t => t.Item2);
WriteGitParameters(response);
}
// Conditional methods can return anything, so we use a ref arg... :S
[Conditional("DEBUG")]
private static void TryLaunchDebugger(ref string[] args)
{
if (args.Length > 0 && args[0] == "-d")
{
Console.Error.WriteLine("Launching debugger...");
Debugger.Launch();
args = args.Skip(1).ToArray();
}
}
private static bool TrySilentInstall(ref string[] args)
{
if (args.Length > 0 && args[0] == "-s")
{
Console.Out.WriteLine("Silently Installing...");
InstallTheApp(silent: true);
args = args.Skip(1).ToArray();
return true;
}
return false;
}
private static void WriteGitParameters(IDictionary<string, string> response)
{
foreach (var pair in response)
{
Console.Write("{0}={1}\n", pair.Key, pair.Value);
}
}
private static IDictionary<string, string> ReadGitParameters()
{
string line;
Dictionary<string, string> values = new Dictionary<string, string>();
while (!String.IsNullOrWhiteSpace((line = Console.ReadLine())))
{
string[] splitted = line.Split('=');
values[splitted[0]] = splitted[1];
}
return values;
}
private static void WriteUsage()
{
Console.Error.WriteLine("If you see this. git-credential-winstore is correctly installed!");
Console.Error.WriteLine("This application is designed to be used by git as a credential helper and should not be invoked separately");
Console.Error.WriteLine("See the following link for more info: http://www.manpagez.com/man/1/git-credential-cache/");
}
private static void InstallTheApp(bool silent)
{
if(!silent)
{
if (MessageBox.Show("Do you want to install git-credential-winstore to prompt for passwords?",
"Installing git-credential-winstore", MessageBoxButtons.YesNo) != DialogResult.Yes)
{
return;
}
}
var target = new DirectoryInfo(Environment.ExpandEnvironmentVariables(@"%AppData%\GitCredStore"));
if (!target.Exists)
{
target.Create();
}
var dest = new FileInfo(Environment.ExpandEnvironmentVariables(@"%AppData%\GitCredStore\git-credential-winstore.exe"));
File.Copy(Assembly.GetExecutingAssembly().Location, dest.FullName, true);
Process.Start("git", string.Format("config --global credential.helper \"!'{0}'\"", dest.FullName));
}
static IEnumerable<Tuple<string, string>> GetCommand(IDictionary<string, string> args)
{
// Build the URL
Uri url = ExtractUrl(args);
string userName = args.GetOrDefault("username", null);
string password = null;
IntPtr credPtr = IntPtr.Zero;
try
{
// Check for a credential
string target = GetTargetName(url);
if (!NativeMethods.CredRead(target, NativeMethods.CRED_TYPE.GENERIC, 0, out credPtr))
{
// Don't have a credential for this user. Are we on XP? If so, sorry no dice.
if (OnXP())
{
// Users will get a Git prompt for user name and password. We'll still store them.
yield break;
}
credPtr = IntPtr.Zero;
// If we have a username, pack an input authentication buffer
Tuple<int, IntPtr> inputBuffer = null;;
IntPtr outputBuffer = IntPtr.Zero;
int outputBufferSize = 0;
try
{
inputBuffer = PackUserNameBuffer(userName);
if (inputBuffer == null) { yield break; }
// Setup UI
NativeMethods.CREDUI_INFO ui = new NativeMethods.CREDUI_INFO()
{
pszCaptionText = "Git Credentials",
pszMessageText = "Enter your credentials for: " + url.AbsoluteUri
};
ui.cbSize = Marshal.SizeOf(ui);
// Prompt!
int authPackage = 0;
bool save = false;
var ret = NativeMethods.CredUIPromptForWindowsCredentials(
uiInfo: ref ui,
authError: 0,
authPackage: ref authPackage,
InAuthBuffer: inputBuffer.Item2,
InAuthBufferSize: inputBuffer.Item1,
refOutAuthBuffer: out outputBuffer,
refOutAuthBufferSize: out outputBufferSize,
fSave: ref save,
flags: NativeMethods.PromptForWindowsCredentialsFlags.CREDUIWIN_GENERIC);
if (ret != NativeMethods.CredUIReturnCodes.NO_ERROR)
{
Console.Error.WriteLine("Error prompting for credentials: " + ret.ToString());
yield break;
}
}
finally
{
if (inputBuffer != null && inputBuffer.Item2 != IntPtr.Zero)
{
Marshal.FreeHGlobal(inputBuffer.Item2);
}
}
try
{
// Unpack
if (!UnPackAuthBuffer(outputBuffer, outputBufferSize, out userName, out password))
{
yield break;
}
}
finally
{
if (outputBuffer != IntPtr.Zero)
{
Marshal.FreeHGlobal(outputBuffer);
}
}
}
else
{
// Decode the credential
NativeMethods.CREDENTIAL cred = (NativeMethods.CREDENTIAL)Marshal.PtrToStructure(credPtr, typeof(NativeMethods.CREDENTIAL));
userName = cred.userName;
password = Marshal.PtrToStringBSTR(cred.credentialBlob);
}
yield return Tuple.Create("username", userName);
yield return Tuple.Create("password", password);
}
finally
{
if (credPtr != IntPtr.Zero)
{
NativeMethods.CredFree(credPtr);
}
}
}
private static bool OnXP()
{
// I know, version detection a Bad Thing(TM). But "feature detection" is supposed to be done
// via checking DLL exports, which is gross in C#...
return Environment.OSVersion.Version.Major == 5;
}
private static bool UnPackAuthBuffer(IntPtr buffer, int size, out string userName, out string password)
{
userName = String.Empty;
password = String.Empty;
StringBuilder userNameBuffer = new StringBuilder(255);
StringBuilder passwordBuffer = new StringBuilder(255);
StringBuilder domainBuffer = new StringBuilder(255);
int userNameSize = 255;
int passwordSize = 255;
int domainSize = 255;
if (!NativeMethods.CredUnPackAuthenticationBuffer(
dwFlags: 0,
pAuthBuffer: buffer,
cbAuthBuffer: size,
pszUserName: userNameBuffer,
pcchMaxUserName: ref userNameSize,
pszDomainName: domainBuffer,
pcchMaxDomainame: ref domainSize,
pszPassword: passwordBuffer,
pcchMaxPassword: ref passwordSize))
{
Console.Error.WriteLine("Unable to unpack credential: " + GetLastErrorMessage());
return false;
}
userName = userNameBuffer.ToString();
password = passwordBuffer.ToString();
return true;
}
private static Tuple<int, IntPtr> PackUserNameBuffer(string userName)
{
if (String.IsNullOrWhiteSpace(userName))
{
return Tuple.Create(0, IntPtr.Zero);
}
IntPtr buf = IntPtr.Zero;
int size = 0;
// First, calculate size. (buf == IntPtr.Zero)
var result = NativeMethods.CredPackAuthenticationBuffer(
dwFlags: 4, // CRED_PACK_GENERIC_CREDENTIALS
pszUserName: userName,
pszPassword: String.Empty,
pPackedCredentials: buf,
pcbPackedCredentials: ref size);
Debug.Assert(!result);
if (Marshal.GetLastWin32Error() != 122)
{
Console.Error.WriteLine("Unable to calculate size of packed authentication buffer: " + GetLastErrorMessage());
return null;
}
buf = Marshal.AllocHGlobal(size);
if (!NativeMethods.CredPackAuthenticationBuffer(
dwFlags: 4, // CRED_PACK_GENERIC_CREDENTIALS
pszUserName: userName,
pszPassword: String.Empty,
pPackedCredentials: buf,
pcbPackedCredentials: ref size))
{
Console.Error.WriteLine("Unable to pack incoming username: " + GetLastErrorMessage());
return null;
}
return Tuple.Create(size, buf);
}
static IEnumerable<Tuple<string, string>> StoreCommand(IDictionary<string, string> args)
{
// Build the URL
Uri url = ExtractUrl(args);
string userName = args.GetOrDefault("username", String.Empty);
string password = args.GetOrDefault("password", String.Empty);
bool abort = false;
if(abort |= String.IsNullOrEmpty(userName)) {
Console.Error.WriteLine("username parameter must be provided");
}
if(abort |= String.IsNullOrEmpty(password)) {
Console.Error.WriteLine("password parameter must be provided");
}
if (!abort)
{
string target = GetTargetName(url);
IntPtr passwordPtr = Marshal.StringToBSTR(password);
NativeMethods.CREDENTIAL cred = new NativeMethods.CREDENTIAL()
{
type = 0x01, // Generic
targetName = target,
credentialBlob = Marshal.StringToCoTaskMemUni(password),
persist = 0x03, // Enterprise (roaming)
attributeCount = 0,
userName = userName
};
cred.credentialBlobSize = Encoding.Unicode.GetByteCount(password);
if (!NativeMethods.CredWrite(ref cred, 0))
{
Console.Error.WriteLine(
"Failed to write credential: " +
GetLastErrorMessage());
}
}
return Enumerable.Empty<Tuple<string, string>>();
}
static IEnumerable<Tuple<string, string>> EraseCommand(IDictionary<string, string> args)
{
Uri url = ExtractUrl(args);
if (!NativeMethods.CredDelete(GetTargetName(url), NativeMethods.CRED_TYPE.GENERIC, 0))
{
Console.Error.WriteLine(
"Failed to erase credential: " +
GetLastErrorMessage());
}
yield break;
}
private static string GetLastErrorMessage()
{
return new Win32Exception(Marshal.GetLastWin32Error()).Message;
}
private static Uri ExtractUrl(IDictionary<string, string> args)
{
Uri url = new UriBuilder()
{
Scheme = args.GetOrDefault("protocol", "https"),
Host = args.GetOrDefault("host", "no-host.git"),
Path = args.GetOrDefault("path", "/")
}.Uri;
return url;
}
private static string GetTargetName(Uri url)
{
return "git:" + url.AbsoluteUri;
}
}
}