Skip to content

Commit 48e93ad

Browse files
committed
add part install for linux
1 parent 44490ca commit 48e93ad

File tree

3 files changed

+79
-8
lines changed

3 files changed

+79
-8
lines changed

src/cert.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ use self::openssl::x509::*;
2323

2424
use self::rand::prelude::random;
2525

26-
use super::utils;
26+
use utils;
2727

2828
pub struct Cert {
2929
ca: Option<X509>,

src/install.rs

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
extern crate dirs;
2+
3+
use std::fs::copy;
4+
use std::fs::DirBuilder;
5+
use std::path::Path;
6+
use std::process::Command;
7+
8+
use utils;
9+
10+
pub fn install() {
11+
if cfg!(unix) {
12+
install_linux();
13+
}
14+
}
15+
16+
pub fn install_linux() {
17+
let ssl_dirs = vec![
18+
(
19+
"/etc/ca-certificates/trust-source/anchors",
20+
"update-ca-trust",
21+
),
22+
("/etc/pki/ca-trust/source/anchors", "update-ca-trust"),
23+
];
24+
25+
for (ssl_dir, update_command) in ssl_dirs {
26+
if Path::new(ssl_dir).exists() {
27+
let target_path = format!("{}/easycert-ca.pem", ssl_dir);
28+
29+
Command::new("sudo")
30+
.args(&["cp", &utils::ca_path(), &target_path])
31+
.output()
32+
.unwrap();
33+
34+
Command::new("sudo")
35+
.args(&[update_command])
36+
.output()
37+
.unwrap();
38+
39+
return;
40+
}
41+
}
42+
43+
eprintln!("can't determinate system trusted store path");
44+
eprintln!("failed to install certificate, try install to nss store");
45+
}

src/main.rs

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,15 @@ extern crate gdi32;
55
#[cfg(windows)]
66
extern crate user32;
77

8+
use std::fs::File;
9+
10+
use clap::{App, Arg, ArgMatches, SubCommand};
11+
812
mod cert;
13+
mod install;
914
mod utils;
1015

11-
use clap::{App, Arg, SubCommand};
16+
use install::install;
1217

1318
fn main() {
1419
let args = App::new("mkcert")
@@ -63,13 +68,10 @@ fn main() {
6368
.help("lengh of key, default 2048")
6469
.takes_value(true),
6570
),
66-
);
67-
68-
let matches = args.get_matches();
69-
70-
let mut c = cert::Cert::new();
71+
)
72+
.subcommand(SubCommand::with_name("install").about("install ca to trusted store"));
7173

72-
if let Some(matches) = matches.subcommand_matches("init") {
74+
let get_init_args = |matches: &ArgMatches| {
7375
let length: u32 = matches
7476
.value_of("length")
7577
.unwrap_or("2048")
@@ -88,6 +90,16 @@ fn main() {
8890
false
8991
};
9092

93+
return (cn, length, force);
94+
};
95+
96+
let matches = args.get_matches();
97+
98+
let mut c = cert::Cert::new();
99+
100+
if let Some(matches) = matches.subcommand_matches("init") {
101+
let (cn, length, force) = get_init_args(&matches);
102+
91103
c.init(&cn, length, force);
92104
}
93105

@@ -109,4 +121,18 @@ fn main() {
109121
eprintln!("must provide a name!");
110122
}
111123
}
124+
125+
if let Some(matches) = matches.subcommand_matches("install") {
126+
let ca = File::open(utils::ca_path());
127+
let key = File::open(utils::key_path());
128+
129+
if let (Ok(_ca), Ok(_key)) = (ca, key) {
130+
} else {
131+
let (cn, length, force) = get_init_args(&matches);
132+
133+
c.init(&cn, length, force);
134+
}
135+
136+
install();
137+
}
112138
}

0 commit comments

Comments
 (0)