2023-04-14 01:05:27 +02:00
|
|
|
use chrono::Utc;
|
|
|
|
|
2023-04-13 22:00:33 +02:00
|
|
|
use crate::errors::Error;
|
2023-04-13 18:56:32 +02:00
|
|
|
use crate::management::{delete_key, Action, Pending};
|
2023-04-14 00:52:54 +02:00
|
|
|
use crate::pending_path;
|
2023-04-14 12:18:49 +02:00
|
|
|
use crate::settings::{SMTPEncryption, SETTINGS};
|
2023-04-13 18:56:32 +02:00
|
|
|
use crate::utils::{get_email_from_cert, parse_pem};
|
2023-04-14 12:18:49 +02:00
|
|
|
use crate::PENDING_FOLDER;
|
2023-04-13 18:56:32 +02:00
|
|
|
|
2023-04-14 12:18:49 +02:00
|
|
|
use lettre::transport::smtp::authentication::Credentials;
|
|
|
|
use lettre::{Message, SmtpTransport, Transport};
|
2023-04-13 18:56:32 +02:00
|
|
|
use std::fs;
|
|
|
|
use std::path::Path;
|
|
|
|
|
2023-04-13 22:00:33 +02:00
|
|
|
pub fn confirm_action(token: &str) -> Result<(), Error> {
|
2023-04-13 18:56:32 +02:00
|
|
|
let pending_path = pending_path!().join(token);
|
2023-04-13 22:55:05 +02:00
|
|
|
let content = if pending_path.is_file() {
|
2023-04-13 22:00:33 +02:00
|
|
|
match fs::read_to_string(&pending_path) {
|
2023-04-13 22:26:41 +02:00
|
|
|
Ok(content) => content,
|
2023-04-13 22:00:33 +02:00
|
|
|
Err(_) => return Err(Error::Inaccessible),
|
|
|
|
}
|
|
|
|
} else {
|
2023-04-13 23:32:12 +02:00
|
|
|
return Err(Error::MissingPending);
|
2023-04-13 22:00:33 +02:00
|
|
|
};
|
2023-04-13 22:26:41 +02:00
|
|
|
let key = match serde_json::from_str::<Pending>(&content) {
|
2023-04-13 22:00:33 +02:00
|
|
|
Ok(key) => key,
|
2023-04-13 23:32:12 +02:00
|
|
|
Err(_) => return Err(Error::DeserializeData),
|
2023-04-13 22:00:33 +02:00
|
|
|
};
|
2023-04-14 01:05:27 +02:00
|
|
|
if Utc::now().timestamp() - key.timestamp() > SETTINGS.max_age {
|
|
|
|
match fs::remove_file(pending_path) {
|
|
|
|
Ok(_) => Err(Error::MissingPending),
|
|
|
|
Err(_) => Err(Error::Inaccessible),
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
match key.action() {
|
|
|
|
Action::Add => {
|
|
|
|
let cert = parse_pem(key.data())?;
|
|
|
|
let domain = match get_email_from_cert(&cert)?.split('@').last() {
|
|
|
|
Some(domain) => domain.to_string(),
|
|
|
|
None => return Err(Error::ParseEmail),
|
|
|
|
};
|
|
|
|
match sequoia_net::wkd::insert(
|
2023-04-14 12:18:49 +02:00
|
|
|
&SETTINGS.root_folder,
|
2023-04-14 01:05:27 +02:00
|
|
|
domain,
|
|
|
|
SETTINGS.variant,
|
|
|
|
&cert,
|
|
|
|
) {
|
|
|
|
Ok(_) => (),
|
|
|
|
Err(_) => return Err(Error::AddingKey),
|
|
|
|
}
|
2023-04-13 22:00:33 +02:00
|
|
|
}
|
2023-04-14 01:05:27 +02:00
|
|
|
Action::Delete => delete_key(key.data())?,
|
|
|
|
}
|
|
|
|
match fs::remove_file(&pending_path) {
|
|
|
|
Ok(_) => Ok(()),
|
|
|
|
Err(_) => Err(Error::Inaccessible),
|
2023-04-13 18:56:32 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-14 12:18:49 +02:00
|
|
|
pub fn send_confirmation_email(email: &str, action: &Action, token: &str) -> Result<(), Error> {
|
|
|
|
println!("Sending mail, token: {}", &token);
|
|
|
|
let email = Message::builder()
|
|
|
|
.from(match SETTINGS.mail_settings.mail_from.parse() {
|
|
|
|
Ok(mailbox) => mailbox,
|
|
|
|
Err(_) => panic!("Unable to parse the email in the settings!"),
|
|
|
|
})
|
|
|
|
.to(match email.parse() {
|
|
|
|
Ok(mailbox) => mailbox,
|
|
|
|
Err(_) => return Err(Error::ParseEmail),
|
|
|
|
})
|
|
|
|
.subject(&SETTINGS.mail_settings.mail_subject)
|
|
|
|
.body(format!("{action} - {token}"));
|
|
|
|
let message = match email {
|
|
|
|
Ok(message) => message,
|
|
|
|
Err(_) => return Err(Error::MailGeneration),
|
|
|
|
};
|
|
|
|
let creds = Credentials::new(
|
|
|
|
SETTINGS.mail_settings.smtp_username.to_owned(),
|
|
|
|
SETTINGS.mail_settings.smtp_password.to_owned(),
|
|
|
|
);
|
|
|
|
let builder = match &SETTINGS.mail_settings.smtp_tls {
|
|
|
|
SMTPEncryption::Tls => SmtpTransport::relay(&SETTINGS.mail_settings.smtp_host),
|
|
|
|
SMTPEncryption::Starttls => {
|
|
|
|
SmtpTransport::starttls_relay(&SETTINGS.mail_settings.smtp_host)
|
|
|
|
}
|
|
|
|
};
|
|
|
|
let mailer = match builder {
|
|
|
|
Ok(builder) => builder,
|
|
|
|
Err(_) => return Err(Error::SmtpBuilder),
|
|
|
|
}
|
|
|
|
.credentials(creds)
|
|
|
|
.port(SETTINGS.mail_settings.smtp_port)
|
|
|
|
.build();
|
|
|
|
match mailer.send(&message) {
|
|
|
|
Ok(_) => Ok(()),
|
|
|
|
Err(_) => Err(Error::SendMail),
|
|
|
|
}
|
2023-04-13 18:56:32 +02:00
|
|
|
}
|