aerogramme/src/main.rs

205 lines
6 KiB
Rust
Raw Normal View History

#![feature(async_fn_in_trait)]
mod timestamp;
2022-05-18 10:24:37 +00:00
mod bayou;
2022-05-19 10:10:48 +00:00
mod config;
2022-05-18 10:24:37 +00:00
mod cryptoblob;
2022-06-17 16:39:36 +00:00
mod imap;
2022-07-13 10:30:35 +00:00
mod k2v_util;
2022-05-31 15:07:34 +00:00
mod lmtp;
2022-05-19 10:10:48 +00:00
mod login;
2022-06-27 14:56:20 +00:00
mod mail;
2022-05-19 13:14:36 +00:00
mod server;
2023-10-30 17:07:40 +00:00
mod storage;
2022-05-18 10:24:37 +00:00
2022-05-19 13:14:36 +00:00
use std::path::PathBuf;
2023-12-08 18:06:12 +00:00
use anyhow::{bail, Result, Context};
2022-05-19 13:14:36 +00:00
use clap::{Parser, Subcommand};
2022-05-19 10:10:48 +00:00
use config::*;
2022-05-19 12:33:49 +00:00
use server::Server;
2023-12-08 18:06:12 +00:00
use login::{static_provider::*, *};
2022-05-18 10:24:37 +00:00
2022-05-19 13:14:36 +00:00
#[derive(Parser, Debug)]
#[clap(author, version, about, long_about = None)]
struct Args {
#[clap(subcommand)]
command: Command,
2023-12-06 19:57:25 +00:00
#[clap(short, long, env = "CONFIG_FILE", default_value = "aerogramme.toml")]
config_file: PathBuf,
2022-05-19 13:14:36 +00:00
}
#[derive(Subcommand, Debug)]
enum Command {
2023-12-06 19:57:25 +00:00
#[clap(subcommand)]
Companion(CompanionCommand),
#[clap(subcommand)]
Provider(ProviderCommand),
//Test,
}
#[derive(Subcommand, Debug)]
enum CompanionCommand {
/// Runs the IMAP proxy
Daemon,
Reload {
#[clap(short, long, env = "AEROGRAMME_PID")]
pid: Option<u64>,
2023-11-24 10:44:42 +00:00
},
2023-12-06 19:57:25 +00:00
Wizard,
#[clap(subcommand)]
Account(AccountManagement),
2022-05-19 13:14:36 +00:00
}
2023-12-06 19:57:25 +00:00
#[derive(Subcommand, Debug)]
enum ProviderCommand {
/// Runs the IMAP+LMTP server daemon
Daemon,
2023-12-08 18:06:12 +00:00
/// Reload the daemon
2023-12-06 19:57:25 +00:00
Reload,
2023-12-08 18:06:12 +00:00
/// Manage static accounts
2023-12-06 19:57:25 +00:00
#[clap(subcommand)]
Account(AccountManagement),
}
#[derive(Subcommand, Debug)]
enum AccountManagement {
2023-12-08 18:06:12 +00:00
/// Add an account
2023-12-06 19:57:25 +00:00
Add {
#[clap(short, long)]
login: String,
#[clap(short, long)]
setup: PathBuf,
},
2023-12-08 18:06:12 +00:00
/// Delete an account
2023-12-06 19:57:25 +00:00
Delete {
#[clap(short, long)]
login: String,
},
2023-12-08 18:06:12 +00:00
/// Change password for a given account
2023-12-06 19:57:25 +00:00
ChangePassword {
#[clap(short, long)]
login: String
},
}
2022-05-18 10:24:37 +00:00
#[tokio::main]
2022-05-19 13:14:36 +00:00
async fn main() -> Result<()> {
2022-05-20 11:36:45 +00:00
if std::env::var("RUST_LOG").is_err() {
std::env::set_var("RUST_LOG", "main=info,aerogramme=info,k2v_client=info")
2022-05-20 11:36:45 +00:00
}
2022-06-02 15:59:29 +00:00
2022-06-29 18:00:38 +00:00
// Abort on panic (same behavior as in Go)
std::panic::set_hook(Box::new(|panic_info| {
2023-05-15 16:23:23 +00:00
eprintln!("{}", panic_info);
2022-07-15 14:15:48 +00:00
eprintln!("{:?}", backtrace::Backtrace::new());
2022-06-29 18:00:38 +00:00
std::process::abort();
}));
2022-06-02 15:59:29 +00:00
tracing_subscriber::fmt::init();
2022-05-20 11:36:45 +00:00
2022-05-19 13:14:36 +00:00
let args = Args::parse();
2023-12-06 19:57:25 +00:00
let any_config = read_config(args.config_file)?;
2022-05-19 13:14:36 +00:00
2023-12-08 18:06:12 +00:00
match (&args.command, any_config) {
2023-12-06 19:57:25 +00:00
(Command::Companion(subcommand), AnyConfig::Companion(config)) => match subcommand {
CompanionCommand::Daemon => {
let server = Server::from_companion_config(config).await?;
server.run().await?;
},
CompanionCommand::Reload { pid } => {
unimplemented!();
},
CompanionCommand::Wizard => {
unimplemented!();
},
CompanionCommand::Account(cmd) => {
let user_file = config.users.user_list;
2023-12-08 18:06:12 +00:00
account_management(&args.command, cmd, user_file)?;
2023-12-06 19:57:25 +00:00
}
},
(Command::Provider(subcommand), AnyConfig::Provider(config)) => match subcommand {
ProviderCommand::Daemon => {
let server = Server::from_provider_config(config).await?;
server.run().await?;
},
ProviderCommand::Reload => {
unimplemented!();
},
ProviderCommand::Account(cmd) => {
let user_file = match config.users {
UserManagement::Static(conf) => conf.user_list,
UserManagement::Ldap(_) => panic!("LDAP account management is not supported from Aerogramme.")
};
2023-12-08 18:06:12 +00:00
account_management(&args.command, cmd, user_file)?;
2023-12-06 19:57:25 +00:00
}
},
(Command::Provider(_), AnyConfig::Companion(_)) => {
panic!("Your want to run a 'Provider' command but your configuration file has role 'Companion'.");
},
(Command::Companion(_), AnyConfig::Provider(_)) => {
panic!("Your want to run a 'Companion' command but your configuration file has role 'Provider'.");
},
2022-05-19 10:10:48 +00:00
}
2022-05-19 13:14:36 +00:00
Ok(())
2022-05-18 10:24:37 +00:00
}
2023-12-08 18:06:12 +00:00
fn account_management(root: &Command, cmd: &AccountManagement, users: PathBuf) -> Result<()> {
let mut ulist: UserList = read_config(users.clone())?;
2023-12-06 19:57:25 +00:00
match cmd {
2023-12-08 18:06:12 +00:00
AccountManagement::Add { login, setup } => {
tracing::debug!(user=login, "will-create");
let stp: SetupEntry = read_config(setup.clone())?;
tracing::debug!(user=login, "loaded setup entry");
let crypto_root = match root {
Command::Provider(_) => CryptographyRoot::PasswordProtected,
Command::Companion(_) => {
// @TODO use keyring by default instead of inplace in the future
// @TODO generate keys
CryptographyRoot::InPlace {
master_key: "".to_string(),
secret_key: "".to_string(),
}
}
};
let password = match stp.clear_password {
Some(pwd) => pwd,
None => {
let password = rpassword::prompt_password("Enter password: ")?;
let password_confirm = rpassword::prompt_password("Confirm password: ")?;
if password != password_confirm {
bail!("Passwords don't match.");
}
password
}
};
let hash = hash_password(password.as_str()).context("unable to hash password")?;
ulist.insert(login.clone(), UserEntry {
email_addresses: stp.email_addresses,
password: hash,
crypto_root,
storage: stp.storage,
});
write_config(users.clone(), &ulist)?;
2023-12-06 19:57:25 +00:00
},
2023-12-08 18:06:12 +00:00
AccountManagement::Delete { login } => {
2023-12-12 08:17:59 +00:00
tracing::debug!(user=login, "will-delete");
ulist.remove(&login);
write_config(users.clone(), &ulist)?;
2023-12-06 19:57:25 +00:00
},
2023-12-08 18:06:12 +00:00
AccountManagement::ChangePassword { login } => {
2023-12-06 19:57:25 +00:00
unimplemented!();
},
2023-12-08 18:06:12 +00:00
};
Ok(())
2023-12-06 19:57:25 +00:00
}