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
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
use log::{debug, trace};
use std::ffi::CString;
use std::io::Write;
use std::mem;
use std::path::Path;
use std::process::{Command, Stdio};
use std::ptr;
use url;
use crate::util::Binding;
use crate::{raw, Config, Error, IntoCString};
pub struct Cred {
raw: *mut raw::git_cred,
}
pub struct CredentialHelper {
pub username: Option<String>,
protocol: Option<String>,
host: Option<String>,
port: Option<u16>,
path: Option<String>,
url: String,
commands: Vec<String>,
}
impl Cred {
pub fn default() -> Result<Cred, Error> {
crate::init();
let mut out = ptr::null_mut();
unsafe {
try_call!(raw::git_cred_default_new(&mut out));
Ok(Binding::from_raw(out))
}
}
pub fn ssh_key_from_agent(username: &str) -> Result<Cred, Error> {
crate::init();
let mut out = ptr::null_mut();
let username = CString::new(username)?;
unsafe {
try_call!(raw::git_cred_ssh_key_from_agent(&mut out, username));
Ok(Binding::from_raw(out))
}
}
pub fn ssh_key(
username: &str,
publickey: Option<&Path>,
privatekey: &Path,
passphrase: Option<&str>,
) -> Result<Cred, Error> {
crate::init();
let username = CString::new(username)?;
let publickey = crate::opt_cstr(publickey)?;
let privatekey = privatekey.into_c_string()?;
let passphrase = crate::opt_cstr(passphrase)?;
let mut out = ptr::null_mut();
unsafe {
try_call!(raw::git_cred_ssh_key_new(
&mut out, username, publickey, privatekey, passphrase
));
Ok(Binding::from_raw(out))
}
}
pub fn ssh_key_from_memory(
username: &str,
publickey: Option<&str>,
privatekey: &str,
passphrase: Option<&str>,
) -> Result<Cred, Error> {
crate::init();
let username = CString::new(username)?;
let publickey = crate::opt_cstr(publickey)?;
let privatekey = CString::new(privatekey)?;
let passphrase = crate::opt_cstr(passphrase)?;
let mut out = ptr::null_mut();
unsafe {
try_call!(raw::git_cred_ssh_key_memory_new(
&mut out, username, publickey, privatekey, passphrase
));
Ok(Binding::from_raw(out))
}
}
pub fn userpass_plaintext(username: &str, password: &str) -> Result<Cred, Error> {
crate::init();
let username = CString::new(username)?;
let password = CString::new(password)?;
let mut out = ptr::null_mut();
unsafe {
try_call!(raw::git_cred_userpass_plaintext_new(
&mut out, username, password
));
Ok(Binding::from_raw(out))
}
}
pub fn credential_helper(
config: &Config,
url: &str,
username: Option<&str>,
) -> Result<Cred, Error> {
match CredentialHelper::new(url)
.config(config)
.username(username)
.execute()
{
Some((username, password)) => Cred::userpass_plaintext(&username, &password),
None => Err(Error::from_str(
"failed to acquire username/password \
from local configuration",
)),
}
}
pub fn username(username: &str) -> Result<Cred, Error> {
crate::init();
let username = CString::new(username)?;
let mut out = ptr::null_mut();
unsafe {
try_call!(raw::git_cred_username_new(&mut out, username));
Ok(Binding::from_raw(out))
}
}
pub fn has_username(&self) -> bool {
unsafe { raw::git_cred_has_username(self.raw) == 1 }
}
pub fn credtype(&self) -> raw::git_credtype_t {
unsafe { (*self.raw).credtype }
}
pub unsafe fn unwrap(mut self) -> *mut raw::git_cred {
mem::replace(&mut self.raw, ptr::null_mut())
}
}
impl Binding for Cred {
type Raw = *mut raw::git_cred;
unsafe fn from_raw(raw: *mut raw::git_cred) -> Cred {
Cred { raw }
}
fn raw(&self) -> *mut raw::git_cred {
self.raw
}
}
impl Drop for Cred {
fn drop(&mut self) {
if !self.raw.is_null() {
unsafe {
if let Some(f) = (*self.raw).free {
f(self.raw)
}
}
}
}
}
impl CredentialHelper {
pub fn new(url: &str) -> CredentialHelper {
let mut ret = CredentialHelper {
protocol: None,
host: None,
port: None,
path: None,
username: None,
url: url.to_string(),
commands: Vec::new(),
};
if let Ok(url) = url::Url::parse(url) {
if let Some(url::Host::Domain(s)) = url.host() {
ret.host = Some(s.to_string());
}
ret.port = url.port();
ret.protocol = Some(url.scheme().to_string());
}
ret
}
pub fn username(&mut self, username: Option<&str>) -> &mut CredentialHelper {
self.username = username.map(|s| s.to_string());
self
}
pub fn config(&mut self, config: &Config) -> &mut CredentialHelper {
if self.username.is_none() {
self.config_username(config);
}
self.config_helper(config);
self.config_use_http_path(config);
self
}
fn config_username(&mut self, config: &Config) {
let key = self.exact_key("username");
self.username = config
.get_string(&key)
.ok()
.or_else(|| {
self.url_key("username")
.and_then(|s| config.get_string(&s).ok())
})
.or_else(|| config.get_string("credential.username").ok())
}
fn config_helper(&mut self, config: &Config) {
let exact = config.get_string(&self.exact_key("helper"));
self.add_command(exact.as_ref().ok().map(|s| &s[..]));
if let Some(key) = self.url_key("helper") {
let url = config.get_string(&key);
self.add_command(url.as_ref().ok().map(|s| &s[..]));
}
let global = config.get_string("credential.helper");
self.add_command(global.as_ref().ok().map(|s| &s[..]));
}
fn config_use_http_path(&mut self, config: &Config) {
let mut use_http_path = false;
if let Some(value) = config.get_bool(&self.exact_key("useHttpPath")).ok() {
use_http_path = value;
} else if let Some(value) = self
.url_key("useHttpPath")
.and_then(|key| config.get_bool(&key).ok())
{
use_http_path = value;
} else if let Some(value) = config.get_bool("credential.useHttpPath").ok() {
use_http_path = value;
}
if use_http_path {
if let Ok(url) = url::Url::parse(&self.url) {
let path = url.path();
self.path = Some(path.strip_prefix('/').unwrap_or(path).to_string());
}
}
}
fn add_command(&mut self, cmd: Option<&str>) {
let cmd = match cmd {
Some("") | None => return,
Some(s) => s,
};
if cmd.starts_with('!') {
self.commands.push(cmd[1..].to_string());
} else if cmd.contains("/") || cmd.contains("\\") {
self.commands.push(cmd.to_string());
} else {
self.commands.push(format!("git credential-{}", cmd));
}
}
fn exact_key(&self, name: &str) -> String {
format!("credential.{}.{}", self.url, name)
}
fn url_key(&self, name: &str) -> Option<String> {
match (&self.host, &self.protocol) {
(&Some(ref host), &Some(ref protocol)) => {
Some(format!("credential.{}://{}.{}", protocol, host, name))
}
_ => None,
}
}
pub fn execute(&self) -> Option<(String, String)> {
let mut username = self.username.clone();
let mut password = None;
for cmd in &self.commands {
let (u, p) = self.execute_cmd(cmd, &username);
if u.is_some() && username.is_none() {
username = u;
}
if p.is_some() && password.is_none() {
password = p;
}
if username.is_some() && password.is_some() {
break;
}
}
match (username, password) {
(Some(u), Some(p)) => Some((u, p)),
_ => None,
}
}
fn execute_cmd(
&self,
cmd: &str,
username: &Option<String>,
) -> (Option<String>, Option<String>) {
macro_rules! my_try( ($e:expr) => (
match $e {
Ok(e) => e,
Err(e) => {
debug!("{} failed with {}", stringify!($e), e);
return (None, None)
}
}
) );
let mut c = Command::new("sh");
c.arg("-c")
.arg(&format!("{} get", cmd))
.stdin(Stdio::piped())
.stdout(Stdio::piped())
.stderr(Stdio::piped());
debug!("executing credential helper {:?}", c);
let mut p = match c.spawn() {
Ok(p) => p,
Err(e) => {
debug!("`sh` failed to spawn: {}", e);
let mut parts = cmd.split_whitespace();
let mut c = Command::new(parts.next().unwrap());
for arg in parts {
c.arg(arg);
}
c.arg("get")
.stdin(Stdio::piped())
.stdout(Stdio::piped())
.stderr(Stdio::piped());
debug!("executing credential helper {:?}", c);
match c.spawn() {
Ok(p) => p,
Err(e) => {
debug!("fallback of {:?} failed with {}", cmd, e);
return (None, None);
}
}
}
};
{
let stdin = p.stdin.as_mut().unwrap();
if let Some(ref p) = self.protocol {
let _ = writeln!(stdin, "protocol={}", p);
}
if let Some(ref p) = self.host {
if let Some(ref p2) = self.port {
let _ = writeln!(stdin, "host={}:{}", p, p2);
} else {
let _ = writeln!(stdin, "host={}", p);
}
}
if let Some(ref p) = self.path {
let _ = writeln!(stdin, "path={}", p);
}
if let Some(ref p) = *username {
let _ = writeln!(stdin, "username={}", p);
}
}
let output = my_try!(p.wait_with_output());
if !output.status.success() {
debug!(
"credential helper failed: {}\nstdout ---\n{}\nstderr ---\n{}",
output.status,
String::from_utf8_lossy(&output.stdout),
String::from_utf8_lossy(&output.stderr)
);
return (None, None);
}
trace!(
"credential helper stderr ---\n{}",
String::from_utf8_lossy(&output.stderr)
);
self.parse_output(output.stdout)
}
fn parse_output(&self, output: Vec<u8>) -> (Option<String>, Option<String>) {
let mut username = None;
let mut password = None;
for line in output.split(|t| *t == b'\n') {
let mut parts = line.splitn(2, |t| *t == b'=');
let key = parts.next().unwrap();
let value = match parts.next() {
Some(s) => s,
None => {
trace!("ignoring output line: {}", String::from_utf8_lossy(line));
continue;
}
};
let value = match String::from_utf8(value.to_vec()) {
Ok(s) => s,
Err(..) => continue,
};
match key {
b"username" => username = Some(value),
b"password" => password = Some(value),
_ => {}
}
}
(username, password)
}
}
#[cfg(test)]
mod test {
use std::env;
use std::fs::File;
use std::io::prelude::*;
use std::path::Path;
use tempfile::TempDir;
use crate::{Config, ConfigLevel, Cred, CredentialHelper};
macro_rules! test_cfg( ($($k:expr => $v:expr),*) => ({
let td = TempDir::new().unwrap();
let mut cfg = Config::new().unwrap();
cfg.add_file(&td.path().join("cfg"), ConfigLevel::Highest, false).unwrap();
$(cfg.set_str($k, $v).unwrap();)*
cfg
}) );
#[test]
fn smoke() {
Cred::default().unwrap();
}
#[test]
fn credential_helper1() {
let cfg = test_cfg! {
"credential.helper" => "!f() { echo username=a; echo password=b; }; f"
};
let (u, p) = CredentialHelper::new("https://example.com/foo/bar")
.config(&cfg)
.execute()
.unwrap();
assert_eq!(u, "a");
assert_eq!(p, "b");
}
#[test]
fn credential_helper2() {
let cfg = test_cfg! {};
assert!(CredentialHelper::new("https://example.com/foo/bar")
.config(&cfg)
.execute()
.is_none());
}
#[test]
fn credential_helper3() {
let cfg = test_cfg! {
"credential.https://example.com.helper" =>
"!f() { echo username=c; }; f",
"credential.helper" => "!f() { echo username=a; echo password=b; }; f"
};
let (u, p) = CredentialHelper::new("https://example.com/foo/bar")
.config(&cfg)
.execute()
.unwrap();
assert_eq!(u, "c");
assert_eq!(p, "b");
}
#[test]
fn credential_helper4() {
if cfg!(windows) {
return;
}
let td = TempDir::new().unwrap();
let path = td.path().join("script");
File::create(&path)
.unwrap()
.write(
br"\
#!/bin/sh
echo username=c
",
)
.unwrap();
chmod(&path);
let cfg = test_cfg! {
"credential.https://example.com.helper" =>
&path.display().to_string()[..],
"credential.helper" => "!f() { echo username=a; echo password=b; }; f"
};
let (u, p) = CredentialHelper::new("https://example.com/foo/bar")
.config(&cfg)
.execute()
.unwrap();
assert_eq!(u, "c");
assert_eq!(p, "b");
}
#[test]
fn credential_helper5() {
if cfg!(windows) {
return;
}
let td = TempDir::new().unwrap();
let path = td.path().join("git-credential-script");
File::create(&path)
.unwrap()
.write(
br"\
#!/bin/sh
echo username=c
",
)
.unwrap();
chmod(&path);
let paths = env::var("PATH").unwrap();
let paths =
env::split_paths(&paths).chain(path.parent().map(|p| p.to_path_buf()).into_iter());
env::set_var("PATH", &env::join_paths(paths).unwrap());
let cfg = test_cfg! {
"credential.https://example.com.helper" => "script",
"credential.helper" => "!f() { echo username=a; echo password=b; }; f"
};
let (u, p) = CredentialHelper::new("https://example.com/foo/bar")
.config(&cfg)
.execute()
.unwrap();
assert_eq!(u, "c");
assert_eq!(p, "b");
}
#[test]
fn credential_helper6() {
let cfg = test_cfg! {
"credential.helper" => ""
};
assert!(CredentialHelper::new("https://example.com/foo/bar")
.config(&cfg)
.execute()
.is_none());
}
#[test]
fn credential_helper7() {
if cfg!(windows) {
return;
}
let td = TempDir::new().unwrap();
let path = td.path().join("script");
File::create(&path)
.unwrap()
.write(
br"\
#!/bin/sh
echo username=$1
echo password=$2
",
)
.unwrap();
chmod(&path);
let cfg = test_cfg! {
"credential.helper" => &format!("{} a b", path.display())
};
let (u, p) = CredentialHelper::new("https://example.com/foo/bar")
.config(&cfg)
.execute()
.unwrap();
assert_eq!(u, "a");
assert_eq!(p, "b");
}
#[test]
fn credential_helper8() {
let cfg = test_cfg! {
"credential.useHttpPath" => "true"
};
let mut helper = CredentialHelper::new("https://example.com/foo/bar");
helper.config(&cfg);
assert_eq!(helper.path.as_deref(), Some("foo/bar"));
}
#[test]
fn credential_helper9() {
let cfg = test_cfg! {
"credential.helper" => "!f() { while read line; do eval $line; done; if [ \"$host\" = example.com:3000 ]; then echo username=a; echo password=b; fi; }; f"
};
let (u, p) = CredentialHelper::new("https://example.com:3000/foo/bar")
.config(&cfg)
.execute()
.unwrap();
assert_eq!(u, "a");
assert_eq!(p, "b");
}
#[test]
#[cfg(feature = "ssh")]
fn ssh_key_from_memory() {
let cred = Cred::ssh_key_from_memory(
"test",
Some("ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDByAO8uj+kXicj6C2ODMspgmUoVyl5eaw8vR6a1yEnFuJFzevabNlN6Ut+CPT3TRnYk5BW73pyXBtnSL2X95BOnbjMDXc4YIkgs3YYHWnxbqsD4Pj/RoGqhf+gwhOBtL0poh8tT8WqXZYxdJQKLQC7oBqf3ykCEYulE4oeRUmNh4IzEE+skD/zDkaJ+S1HRD8D8YCiTO01qQnSmoDFdmIZTi8MS8Cw+O/Qhym1271ThMlhD6PubSYJXfE6rVbE7A9RzH73A6MmKBlzK8VTb4SlNSrr/DOk+L0uq+wPkv+pm+D9WtxoqQ9yl6FaK1cPawa3+7yRNle3m+72KCtyMkQv"),
r#"
-----BEGIN RSA PRIVATE KEY-----
Proc-Type: 4,ENCRYPTED
DEK-Info: AES-128-CBC,818C7722D3B01F2161C2ACF6A5BBAAE8
3Cht4QB3PcoQ0I55j1B3m2ZzIC/mrh+K5nQeA1Vy2GBTMyM7yqGHqTOv7qLhJscd
H+cB0Pm6yCr3lYuNrcKWOCUto+91P7ikyARruHVwyIxKdNx15uNulOzQJHQWNbA4
RQHlhjON4atVo2FyJ6n+ujK6QiBg2PR5V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M0LtbRVZPs+MgiqoGIwYc/ujoCq7GL38gezsBQoHaTt79yYBqCp6UR0LMuZ5
f/7CtWqffgySfJ/0wjGidDAumDv8CK45AURpL/Z+tbFG3M9ar/LZz/Y6EyBcLtGY
Wwb4zs8zXIA0qHrjNTnPqHDvezziArYfgPjxCIHMZzms9Yn8+N02p39uIytqg434
BAlCqZ7GYdDFfTpWIwX+segTK9ux0KdBqcQv+9Fwwjkq9KySnRKqNl7ZJcefFZJq
c6PA1iinZWBjuaO1HKx3PFulrl0bcpR9Kud1ZIyfnh5rwYN8UQkkcR/wZPla04TY
8l5dq/LI/3G5sZXwUHKOcuQWTj7Saq7Q6gkKoMfqt0wC5bpZ1m17GHPoMz6GtX9O
-----END RSA PRIVATE KEY-----
"#,
Some("test123"));
assert!(cred.is_ok());
}
#[cfg(unix)]
fn chmod(path: &Path) {
use std::fs;
use std::os::unix::prelude::*;
let mut perms = fs::metadata(path).unwrap().permissions();
perms.set_mode(0o755);
fs::set_permissions(path, perms).unwrap();
}
#[cfg(windows)]
fn chmod(_path: &Path) {}
}