First dispatcher
- Create (sub)command structure. - First experimental dispatcher to start the daemon.
This commit is contained in:
@@ -0,0 +1,107 @@
|
||||
use clap::Parser;
|
||||
use clap::Subcommand;
|
||||
|
||||
#[derive(Parser, Debug)]
|
||||
#[clap(
|
||||
name = "Husk milter",
|
||||
about = "Encrypting milter.",
|
||||
arg_required_else_help = true,
|
||||
)]
|
||||
pub struct CliArgs {
|
||||
#[clap(
|
||||
long, short,
|
||||
help = "Configuration file.",
|
||||
)]
|
||||
pub config: Option<String>,
|
||||
|
||||
#[clap(subcommand)]
|
||||
pub subcommand: HuskSubcommands,
|
||||
}
|
||||
|
||||
// Subcommands Level 1
|
||||
|
||||
#[derive(Debug, Subcommand)]
|
||||
pub enum HuskSubcommands {
|
||||
Daemon(DaemonCommand),
|
||||
Introducer(IntroducerCommand),
|
||||
Local,
|
||||
}
|
||||
|
||||
// Daemon subcommands
|
||||
|
||||
#[derive(Parser, Debug)]
|
||||
#[clap(
|
||||
name = "daemon",
|
||||
about = "Manage milter daemon.",
|
||||
subcommand_required = true,
|
||||
arg_required_else_help = true,
|
||||
disable_colored_help = true,
|
||||
disable_version_flag = true,
|
||||
)]
|
||||
pub struct DaemonCommand {
|
||||
#[clap(subcommand)]
|
||||
pub subcommand: DaemonSubcommand,
|
||||
}
|
||||
#[derive(Debug, Subcommand)]
|
||||
pub enum DaemonSubcommand {
|
||||
Start,
|
||||
Stop,
|
||||
Status
|
||||
}
|
||||
|
||||
// Introducer subcommands
|
||||
|
||||
#[derive(Parser, Debug)]
|
||||
#[clap(
|
||||
name = "introducer",
|
||||
about = "Manage introducers.",
|
||||
subcommand_required = true,
|
||||
arg_required_else_help = true,
|
||||
disable_colored_help = true,
|
||||
disable_version_flag = true,
|
||||
)]
|
||||
pub struct IntroducerCommand {
|
||||
#[clap(subcommand)]
|
||||
pub subcommand: IntroducerSubcommands,
|
||||
}
|
||||
#[derive(Debug, Subcommand)]
|
||||
pub enum IntroducerSubcommands {
|
||||
Add(IntroducerAddCommand),
|
||||
Remove(IntroducerRemoveCommand),
|
||||
List
|
||||
}
|
||||
|
||||
#[derive(Parser, Debug)]
|
||||
#[clap(
|
||||
name = "introducer",
|
||||
about = "Add introducers.",
|
||||
)]
|
||||
pub struct IntroducerAddCommand {
|
||||
#[clap(
|
||||
long = "cert",
|
||||
help = "Certificate to declare as introducer."
|
||||
)]
|
||||
pub cert: String,
|
||||
|
||||
#[clap(
|
||||
long = "domains",
|
||||
help = "Domains this certificate introduces.",
|
||||
required = true,
|
||||
num_args = 1..,
|
||||
value_delimiter = ' '
|
||||
)]
|
||||
pub domains: Vec<String>,
|
||||
}
|
||||
|
||||
#[derive(Parser, Debug)]
|
||||
#[clap(
|
||||
name = "introducer",
|
||||
about = "Remove introducers.",
|
||||
)]
|
||||
pub struct IntroducerRemoveCommand {
|
||||
#[clap(
|
||||
long = "cert",
|
||||
help = "Certificate to be removed as introducer."
|
||||
)]
|
||||
pub cert: String,
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
pub mod cli_args;
|
||||
@@ -0,0 +1,19 @@
|
||||
use anyhow::Result;
|
||||
|
||||
use crate::commands;
|
||||
use crate::cli::cli_args::{CliArgs, HuskSubcommands};
|
||||
use crate::config::HuskConfigContainer;
|
||||
|
||||
pub mod daemon;
|
||||
|
||||
pub async fn dispatch(cli: CliArgs, config: HuskConfigContainer) -> Result<()> {
|
||||
|
||||
match cli.subcommand {
|
||||
HuskSubcommands::Daemon(subcmd) => {
|
||||
commands::daemon::dispatch(subcmd, config).await?;
|
||||
},
|
||||
_ => { println!("something else"); }
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
use anyhow::Result;
|
||||
|
||||
use crate::cli::cli_args::{DaemonCommand, DaemonSubcommand};
|
||||
use crate::config::HuskConfigContainer;
|
||||
use crate::Daemon;
|
||||
|
||||
pub async fn dispatch(cmd: DaemonCommand, config: HuskConfigContainer) -> Result<()> {
|
||||
|
||||
match cmd.subcommand {
|
||||
DaemonSubcommand::Start => {
|
||||
Daemon::run(config).await?;
|
||||
},
|
||||
DaemonSubcommand::Stop => {
|
||||
println!("stop");
|
||||
},
|
||||
DaemonSubcommand::Status => {
|
||||
println!("status");
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
+1
-1
@@ -19,7 +19,7 @@ pub struct HuskConfig {
|
||||
|
||||
impl HuskConfig {
|
||||
|
||||
pub fn load(path: &String) -> anyhow::Result<Self> {
|
||||
pub fn load(path: &str) -> anyhow::Result<Self> {
|
||||
match fs::read_to_string(path) {
|
||||
Ok(data) => {
|
||||
match toml::from_str(data.as_str()) {
|
||||
|
||||
+24
-24
@@ -26,7 +26,7 @@ use wot::{Depth, Path};
|
||||
|
||||
use crate::types::errors::HuskError;
|
||||
use crate::types::husk_context::HuskContext;
|
||||
use crate::types::introducer::{self, Introducer};
|
||||
use crate::types::introducer::Introducer;
|
||||
use crate::types::recipient::Recipient;
|
||||
|
||||
/// Returns the local trust root from the cert_store. If the trust root
|
||||
@@ -72,7 +72,7 @@ pub fn get_local_trust_root(cert_store: &CertStore) -> anyhow::Result<Cert> {
|
||||
pub fn get_local_certificates<'hc>(context: &HuskContext<'hc>, email: &str)
|
||||
-> Vec<Arc<LazyCert<'hc>>> {
|
||||
|
||||
log::debug!("local lookup for {}", email);
|
||||
log::debug!("local lookup for {}", email);
|
||||
|
||||
let cert_store = &context.cert_store;
|
||||
let trust_roots = vec![(context.local_trust_root.fingerprint(), wot::FULLY_TRUSTED)];
|
||||
@@ -107,14 +107,14 @@ const USER_AGENT: &'static str = concat!("husk/", env!("CARGO_PKG_VERSION"));
|
||||
const CONNECT_TIMEOUT: Duration = Duration::new(5, 0);
|
||||
const REQUEST_TIMEOUT: Duration = Duration::new(5, 0);
|
||||
|
||||
/// Try to fetch certificates for `email` from online sources (keyservers, wkd,
|
||||
/// dane).
|
||||
pub async fn lookup_certificates<'hc>(context: &HuskContext<'hc>, email: &str)
|
||||
/// Try to fetch certificates for `email` from online sources (keyservers, wkd,
|
||||
/// dane).
|
||||
pub async fn lookup_certificates<'hc>(context: &HuskContext<'hc>, email: &str)
|
||||
-> anyhow::Result<Vec<Cert>> {
|
||||
|
||||
log::debug!("remote lookup for {}", email);
|
||||
log::debug!("remote lookup for {}", email);
|
||||
|
||||
let mut jobs = JoinSet::new();
|
||||
let mut jobs = JoinSet::new();
|
||||
let http_client = sequoia_net::reqwest::Client::builder()
|
||||
.user_agent(USER_AGENT)
|
||||
.connect_timeout(CONNECT_TIMEOUT)
|
||||
@@ -128,8 +128,8 @@ pub async fn lookup_certificates<'hc>(context: &HuskContext<'hc>, email: &str)
|
||||
for cert in certs {
|
||||
if let Ok(c) = cert {
|
||||
if let Ok(vc) = c.with_policy(policy, None) {
|
||||
|
||||
// check if the returned certificate contains a userid with the
|
||||
|
||||
// check if the returned certificate contains a userid with the
|
||||
// email address in question.
|
||||
let mut userid_found = false;
|
||||
for userid in vc.userids() {
|
||||
@@ -139,7 +139,7 @@ pub async fn lookup_certificates<'hc>(context: &HuskContext<'hc>, email: &str)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if userid_found {
|
||||
// check if the certificate can be used (for encryption).
|
||||
vc.keys()
|
||||
@@ -147,10 +147,10 @@ pub async fn lookup_certificates<'hc>(context: &HuskContext<'hc>, email: &str)
|
||||
.alive()
|
||||
.revoked(false)
|
||||
.for_transport_encryption()
|
||||
.for_each(|_| {
|
||||
.for_each(|_| {
|
||||
// XXX: cert gets returned for each transport key
|
||||
// once would be enough
|
||||
result.push(c.clone());
|
||||
result.push(c.clone());
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -158,22 +158,22 @@ pub async fn lookup_certificates<'hc>(context: &HuskContext<'hc>, email: &str)
|
||||
}
|
||||
result
|
||||
};
|
||||
|
||||
|
||||
// Keyservers
|
||||
for keyserver in &context.keyservers {
|
||||
|
||||
log::debug!("querying {}", keyserver);
|
||||
|
||||
if let Ok(ks) = KeyServer::with_client(keyserver.as_str(), http_client.clone()) {
|
||||
|
||||
|
||||
let e = email.to_string();
|
||||
let p = StandardPolicy::new();
|
||||
|
||||
jobs.spawn(async move {
|
||||
jobs.spawn(async move {
|
||||
let mut partial_result = Vec::new();
|
||||
|
||||
if let Ok(certs) = ks.search(e.as_str()).await {
|
||||
partial_result = collect_certs(certs, e, &p);
|
||||
partial_result = collect_certs(certs, e, &p);
|
||||
}
|
||||
partial_result
|
||||
});
|
||||
@@ -191,8 +191,8 @@ pub async fn lookup_certificates<'hc>(context: &HuskContext<'hc>, email: &str)
|
||||
}
|
||||
partial_result
|
||||
});
|
||||
|
||||
// DANE
|
||||
|
||||
// DANE
|
||||
let e = email.to_string();
|
||||
let p = StandardPolicy::new();
|
||||
jobs.spawn(async move {
|
||||
@@ -215,13 +215,13 @@ pub async fn lookup_certificates<'hc>(context: &HuskContext<'hc>, email: &str)
|
||||
log::debug!("found cert {}", fpr);
|
||||
|
||||
match set.entry(fpr.clone()) {
|
||||
Occupied(entry) => {
|
||||
Occupied(entry) => {
|
||||
let existing_cert = entry.into_mut();
|
||||
if let Ok(merged_cert) = existing_cert.clone().merge_public(c) {
|
||||
*existing_cert = merged_cert;
|
||||
}
|
||||
},
|
||||
Vacant(_entry) => {
|
||||
Vacant(_entry) => {
|
||||
set.insert(fpr.clone(), c);
|
||||
},
|
||||
}
|
||||
@@ -231,7 +231,7 @@ pub async fn lookup_certificates<'hc>(context: &HuskContext<'hc>, email: &str)
|
||||
Ok(result)
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
pub async fn get_certificates<'hc>(context: &HuskContext<'hc>, email: &str)
|
||||
-> Vec<Arc<LazyCert<'hc>>> {
|
||||
@@ -239,14 +239,14 @@ pub async fn get_certificates<'hc>(context: &HuskContext<'hc>, email: &str)
|
||||
log::debug!("get certificate for {}", email);
|
||||
|
||||
let locals = get_local_certificates(context, email);
|
||||
if locals.len() > 0 {
|
||||
return locals;
|
||||
if locals.len() > 0 {
|
||||
return locals;
|
||||
}
|
||||
|
||||
let mut result = Vec::new();
|
||||
|
||||
for introducer in &context.introducers {
|
||||
log::debug!("consider introducer {:?}",
|
||||
log::debug!("consider introducer {:?}",
|
||||
introducer.cert.userids().map(|u| u.userid()).collect::<Vec<_>>());
|
||||
if introducer.can_introduce(format!("<{}>", email).as_str()) {
|
||||
log::debug!(" introducer regex matches!");
|
||||
|
||||
@@ -20,7 +20,6 @@ use indymilter::{
|
||||
ProtoOpts,
|
||||
SocketInfo,
|
||||
Status,
|
||||
Config,
|
||||
ContextActions,
|
||||
};
|
||||
|
||||
|
||||
+15
-4
@@ -2,9 +2,15 @@
|
||||
// Husk milter
|
||||
//
|
||||
|
||||
use std::{env, process};
|
||||
use std::process;
|
||||
use std::sync::{Arc, Mutex};
|
||||
|
||||
use clap::Parser;
|
||||
use types::defaults;
|
||||
|
||||
pub mod cli;
|
||||
use cli::cli_args::CliArgs;
|
||||
pub mod commands;
|
||||
pub mod config;
|
||||
use config::{HuskConfig, HuskConfigContainer};
|
||||
pub mod types;
|
||||
@@ -15,9 +21,14 @@ use daemon::Daemon;
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() {
|
||||
let args = CliArgs::parse();
|
||||
|
||||
let config_file = env::args().nth(1).expect("config file missing");
|
||||
let husk_config = match HuskConfig::load(&config_file) {
|
||||
let config_file = match &args.config {
|
||||
Some(c) => c.as_str(),
|
||||
None => defaults::CONFIG_FILE_LOCATION
|
||||
};
|
||||
|
||||
let husk_config = match HuskConfig::load(config_file) {
|
||||
Ok(config) => config,
|
||||
Err(e) => {
|
||||
eprintln!("{:?}", e);
|
||||
@@ -31,7 +42,7 @@ async fn main() {
|
||||
|
||||
let config_container: HuskConfigContainer = Arc::new(Mutex::new(husk_config));
|
||||
|
||||
match Daemon::run(config_container).await {
|
||||
match commands::dispatch(args, config_container).await {
|
||||
Ok(_) => {
|
||||
println!("exiting...");
|
||||
},
|
||||
|
||||
@@ -0,0 +1,5 @@
|
||||
//
|
||||
// Some defaults
|
||||
//
|
||||
|
||||
pub const CONFIG_FILE_LOCATION: &'static str = "/etc/husk/config.toml";
|
||||
@@ -8,7 +8,7 @@ use anyhow;
|
||||
use sequoia_openpgp::policy::StandardPolicy;
|
||||
use std::path::PathBuf;
|
||||
use sequoia_openpgp::Cert;
|
||||
use sequoia_cert_store::{CertStore, StoreUpdate};
|
||||
use sequoia_cert_store::CertStore;
|
||||
use sequoia_directories::Home;
|
||||
|
||||
use crate::{config::HuskConfig, crypto};
|
||||
@@ -58,7 +58,7 @@ impl<'hc> HuskContext<'hc> {
|
||||
}
|
||||
|
||||
pub async fn add_recipient(&mut self, rcpt: String) {
|
||||
|
||||
|
||||
let certs = crypto::get_certificates(&self, rcpt.as_str()).await;
|
||||
|
||||
self.mail.add_recipient(rcpt, certs)
|
||||
|
||||
@@ -6,7 +6,8 @@
|
||||
|
||||
use sequoia_wot as wot;
|
||||
use wot::{CertSynopsis, Certification};
|
||||
use sequoia_openpgp::{policy::StandardPolicy, Cert, KeyID};
|
||||
use sequoia_openpgp::policy::StandardPolicy;
|
||||
use sequoia_openpgp::Cert;
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct Introducer {
|
||||
@@ -44,7 +45,7 @@ impl Introducer {
|
||||
u.certifications().for_each(|s| {
|
||||
if s.signature_alive(None, None).is_ok() {
|
||||
s.issuers().for_each(|i| {
|
||||
if i == &my_keyid { result = true; }
|
||||
if i == &my_keyid { result = true; }
|
||||
});
|
||||
}
|
||||
})
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
pub mod defaults;
|
||||
pub mod husk_context;
|
||||
pub mod mail_context;
|
||||
pub mod errors;
|
||||
pub mod recipient;
|
||||
pub mod introducer;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user