implement shellchat net tls delete

This commit is contained in:
nworbnhoj 2023-06-29 14:41:25 +10:00
parent 0545a08c8e
commit 1e95167f47
3 changed files with 49 additions and 1 deletions

View file

@ -11,6 +11,10 @@
"en": "net tls <sub-command>",
"en-tts": "net tls <sub-command>"
},
"tls.delete_cmd": {
"en": "delete ALL trusted Certificates",
"en-tts": "delete ALL trusted Certificates"
},
"tls.mozilla_cmd": {
"en": "trust all Root CA's in webpki-roots",
"en-tts": "trust all Root CA's in webpki-roots"

View file

@ -1,4 +1,4 @@
use crate::{check_trust, danger, trusted::Trusted};
use crate::{check_trust, danger, del_all_cert, trusted::Trusted};
use locales::t;
use std::convert::TryInto;
use std::io::Read;
@ -12,6 +12,13 @@ pub fn shellchat<'a>(
use core::fmt::Write;
let mut ret = String::new();
match tokens.next() {
// delete ALL trusted CA Certificates
Some("delete") => {
log::info!("starting TLS delete certificates");
let count = del_all_cert().unwrap();
write!(ret, "delete {count} certificates").ok();
log::info!("finished TLS delete certificates");
}
Some("help") => {
write!(ret, "{}", t!("tls.cmd_help", locales::LANG)).ok();
()
@ -168,6 +175,7 @@ pub fn shellchat<'a>(
}
None | _ => {
write!(ret, "{}\n", t!("tls.cmd", locales::LANG)).ok();
write!(ret, "\tdelete\t{}\n", t!("tls.delete_cmd", locales::LANG)).ok();
write!(ret, "\thelp\n").ok();
#[cfg(feature = "rootCA")]
write!(ret, "\tmozilla\t{}\n", t!("tls.mozilla_cmd", locales::LANG)).ok();

View file

@ -91,6 +91,42 @@ pub fn check_trust(certificates: &[Certificate]) -> usize {
}
}
// deletes ALL tls trust-anchors from the pddb
// returns the number of certs deleted
pub fn del_all_cert() -> Result<usize, Error> {
let mut count = 0;
let mut keypath = PathBuf::new();
keypath.push(TLS_CERT_DICT);
if std::fs::metadata(&keypath).is_ok() {
for entry in std::fs::read_dir(keypath)? {
let entry = entry?;
let key = entry.file_name().into_string().unwrap();
let path = entry.path();
match std::fs::remove_file(path) {
Ok(_) => {
log::info!("deleted {key}");
count += 1;
}
Err(e) => log::warn!("failed to delete {key} : {e}"),
};
};
};
Ok(count)
}
// deletes a tls trust-anchor from the pddb
pub fn del_cert(key: &str) -> Result<(), Error> {
let mut keypath = PathBuf::new();
keypath.push(TLS_CERT_DICT);
keypath.push(key);
match std::fs::remove_file(keypath) {
Ok(_) => log::info!("deleted {key}") ,
Err(e) => log::warn!("failed to delete {key} : {e}"),
};
return Ok(());
}
// saves a tls trust-anchor to the pddb
pub fn save_cert(key: &str, ta: &RustlsOwnedTrustAnchor) -> Result<(), Error> {
if key.starts_with("__") {