Skip to content

Commit

Permalink
feat(firefox): incomplete firefox implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
shubhexists committed Sep 4, 2024
1 parent 5a957f5 commit 8ef8f37
Show file tree
Hide file tree
Showing 6 changed files with 107 additions and 34 deletions.
8 changes: 4 additions & 4 deletions core/src/commands/generate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -313,10 +313,10 @@ pub fn generate(
};
ca_value_object.install_certificate()?;
let nss_profile_object: NSSProfile = NSSProfile::new();
let ca_unique_name = "vanish-root-test-123456-ujjwal".to_string();
let caroot = "/home/jerry/.local/share/vanish/ca_cert.pem".to_string();
let mkcert = NSSValue::new(nss_profile_object, ca_unique_name, caroot);
let success = mkcert.install_nss();
let ca_unique_name: String = "vanish-root-test-123456-ujjwalpppp".to_string();
let caroot: String = "/home/jerry/.local/share/vanish/ca_cert.pem".to_string();
let mkcert: NSSValue = NSSValue::new(nss_profile_object, ca_unique_name, caroot);
let success: bool = mkcert.install_nss();

if success {
println!("Certificate installed successfully.");
Expand Down
26 changes: 24 additions & 2 deletions core/src/trust_stores/errors.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use openssl::error::ErrorStack;
use std::{error::Error, fmt, io};
use std::{env::VarError, error::Error, fmt, io};

#[derive(Debug)]
pub enum TrustStoreError {
Expand All @@ -24,4 +24,26 @@ impl fmt::Display for TrustStoreError {
}
}

impl Error for TrustStoreError {}
impl Error for TrustStoreError {}

pub enum FirefoxTrustStoreError {
ENVVariableNotFound(VarError, String),
IOError(io::Error),
}

impl fmt::Display for FirefoxTrustStoreError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::ENVVariableNotFound(err, variable) => {
write!(
f,
"Error getting Environment varibale {} : {}",
variable, err
)
}
Self::IOError(err) => {
write!(f, "Error reading the default firefox directoryL {}", err)
}
}
}
}
71 changes: 71 additions & 0 deletions core/src/trust_stores/firefox.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
use super::errors::FirefoxTrustStoreError;
use std::{
env, ffi, fs, io,
path::{Path, PathBuf},
};

pub struct FirefoxTrustStore {
firefox_profile: Vec<String>,
}

impl FirefoxTrustStore {
pub fn new() -> Result<FirefoxTrustStore, FirefoxTrustStoreError> {
let mut firefox_profile: Vec<String> = Vec::<String>::new();
#[cfg(target_os = "linux")]
{
let home: String = env::var("HOME").map_err(|err: env::VarError| {
FirefoxTrustStoreError::ENVVariableNotFound(err, "HOME".to_string())
})?;
firefox_profile.push(home.clone() + "/.morzilla/firefox/");
firefox_profile.push(home + "/snap/firefox/common/.mozilla/firefox/");
}
#[cfg(target_os = "windows")]
{
let userprofile: String = env::var("USERPROFILE").map_err(|err: env::VarError| {
FirefoxTrustStoreError::ENVVariableNotFound(err, "USERPROFILE".to_string())
})?;
firefox_profile.push(userprofile + "\\AppData\\Roaming\\Mozilla\\Firefox\\Profiles");
}
#[cfg(target_os = "macos")]
{
let home: String = env::var("HOME").map_err(|err: env::VarError| {
FirefoxTrustStoreError::ENVVariableNotFound(err, "HOME".to_string())
})?;
firefox_profile.push(home + "/Library/Application Support/Firefox/Profiles/");
}
Ok(FirefoxTrustStore { firefox_profile })
}

pub fn find_cert_directories(&self) -> Result<Vec<PathBuf>, FirefoxTrustStoreError> {
let mut cert_dirs: Vec<PathBuf> = Vec::new();

for profile_dir in &self.firefox_profile {
let path: &Path = Path::new(profile_dir);
if path.exists() && path.is_dir() {
for entry in fs::read_dir(path)
.map_err(|err: io::Error| FirefoxTrustStoreError::IOError(err))?
{
let entry: fs::DirEntry =
entry.map_err(|err: io::Error| FirefoxTrustStoreError::IOError(err))?;
let entry_path: PathBuf = entry.path();
if entry_path.is_dir() {
let cert9_path: PathBuf = entry_path.join("cert9.db");
let cert8_path: PathBuf = entry_path.join("cert8.db");
if cert9_path.exists() || cert8_path.exists() {
cert_dirs.push(entry_path);
}
}
}
}
}

if cert_dirs.is_empty() {
eprintln!("No directories containing certificate databases were found for any of your Firefox Profiles.");
std::process::exit(1);
} else {
Ok(cert_dirs)
}
}

pub fn install_firefox_certificates(&self) {}
}
1 change: 1 addition & 0 deletions core/src/trust_stores/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
mod errors;
pub mod firefox;
pub mod nss;
pub mod nss_profile;

Expand Down
18 changes: 6 additions & 12 deletions core/src/trust_stores/nss.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use super::nss_profile::NSSProfile;
use crate::trust_stores::nss_profile::NSSProfile;
use std::{
io,
fs, io,
path::Path,
process::{Command, ExitStatus},
};
Expand Down Expand Up @@ -107,18 +107,12 @@ impl NSSValue {
F: FnMut(&str),
{
let mut found: usize = 0;
let mut profiles: Vec<String> = Vec::new();
let profiles = &self.profile.nss_dbs;

profiles.extend_from_slice(&self.profile.nss_dbs);
for ff in &self.profile.firefox_paths {
let path: &Path = Path::new(ff);
if path.exists() {
profiles.push(ff.clone());
}
}
println!("{:?}", profiles);

for profile in &profiles {
let stat: Result<std::fs::Metadata, io::Error> = Path::new(profile).metadata();
for profile in profiles {
let stat: Result<fs::Metadata, io::Error> = Path::new(profile).metadata();
if stat.is_ok() && stat.unwrap().is_dir() {
if NSSProfile::path_exists(&format!("{}/cert9.db", profile)) {
f(&format!("sql:{}", profile));
Expand Down
17 changes: 1 addition & 16 deletions core/src/trust_stores/nss_profile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ pub struct NSSProfile {
pub has_certutil: bool,
pub certutil_path: Option<String>,
pub nss_dbs: Vec<String>,
pub firefox_paths: Vec<String>,
}

impl NSSProfile {
Expand All @@ -27,21 +26,8 @@ impl NSSProfile {
"/etc/pki/nssdb".to_string(),
];

let firefox_paths: Vec<String> = vec![
"/usr/bin/firefox".to_string(),
"/usr/bin/firefox-nightly".to_string(),
"/usr/bin/firefox-developer-edition".to_string(),
"/snap/firefox".to_string(),
"/Applications/Firefox.app".to_string(),
"/Applications/FirefoxDeveloperEdition.app".to_string(),
"/Applications/Firefox Developer Edition.app".to_string(),
"/Applications/Firefox Nightly.app".to_string(),
"C:\\Program Files\\Mozilla Firefox".to_string(),
];

let mut has_nss: bool = false;
let all_paths: Vec<String> = [&nss_dbs[..], &firefox_paths[..]].concat();
for path in &all_paths {
for path in &nss_dbs {
if Path::new(path).exists() {
has_nss = true;
break;
Expand Down Expand Up @@ -78,7 +64,6 @@ impl NSSProfile {
has_certutil,
certutil_path,
nss_dbs,
firefox_paths,
}
}

Expand Down

0 comments on commit 8ef8f37

Please sign in to comment.