2018-12-07 02:05:45 +01:00
|
|
|
#![feature(proc_macro_hygiene, decl_macro, vec_remove_item, try_trait)]
|
|
|
|
#![recursion_limit = "128"]
|
2018-07-21 17:27:00 +02:00
|
|
|
#![allow(proc_macro_derive_resolution_fallback)] // TODO: Remove this when diesel update fixes warnings
|
2018-10-10 20:40:39 +02:00
|
|
|
|
2018-12-30 23:34:31 +01:00
|
|
|
#[macro_use]
|
|
|
|
extern crate rocket;
|
|
|
|
#[macro_use]
|
|
|
|
extern crate serde_derive;
|
|
|
|
#[macro_use]
|
|
|
|
extern crate serde_json;
|
|
|
|
#[macro_use]
|
|
|
|
extern crate log;
|
|
|
|
#[macro_use]
|
|
|
|
extern crate diesel;
|
|
|
|
#[macro_use]
|
|
|
|
extern crate diesel_migrations;
|
|
|
|
#[macro_use]
|
|
|
|
extern crate lazy_static;
|
|
|
|
#[macro_use]
|
|
|
|
extern crate derive_more;
|
|
|
|
#[macro_use]
|
|
|
|
extern crate num_derive;
|
|
|
|
|
2018-02-23 00:38:54 +01:00
|
|
|
use rocket::Rocket;
|
2018-12-30 23:34:31 +01:00
|
|
|
use std::{
|
|
|
|
path::Path,
|
|
|
|
process::{exit, Command},
|
|
|
|
};
|
2018-02-10 01:00:55 +01:00
|
|
|
|
2018-12-30 23:34:31 +01:00
|
|
|
#[macro_use]
|
|
|
|
mod error;
|
2018-02-10 01:00:55 +01:00
|
|
|
mod api;
|
|
|
|
mod auth;
|
2018-12-30 23:34:31 +01:00
|
|
|
mod crypto;
|
|
|
|
mod db;
|
2018-08-15 08:32:19 +02:00
|
|
|
mod mail;
|
2018-12-30 23:34:31 +01:00
|
|
|
mod util;
|
2018-02-10 01:00:55 +01:00
|
|
|
|
|
|
|
fn init_rocket() -> Rocket {
|
|
|
|
rocket::ignite()
|
|
|
|
.mount("/", api::web_routes())
|
|
|
|
.mount("/api", api::core_routes())
|
2018-12-23 22:37:02 +01:00
|
|
|
.mount("/admin", api::admin_routes())
|
2018-02-10 01:00:55 +01:00
|
|
|
.mount("/identity", api::identity_routes())
|
|
|
|
.mount("/icons", api::icons_routes())
|
2018-08-24 19:02:34 +02:00
|
|
|
.mount("/notifications", api::notifications_routes())
|
2018-02-10 01:00:55 +01:00
|
|
|
.manage(db::init_pool())
|
2018-08-30 17:43:46 +02:00
|
|
|
.manage(api::start_notification_server())
|
2018-12-23 22:37:02 +01:00
|
|
|
.attach(util::AppHeaders())
|
2018-02-10 01:00:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Embed the migrations from the migrations folder into the application
|
|
|
|
// This way, the program automatically migrates the database to the latest version
|
|
|
|
// https://docs.rs/diesel_migrations/*/diesel_migrations/macro.embed_migrations.html
|
2018-06-12 17:24:29 +02:00
|
|
|
#[allow(unused_imports)]
|
|
|
|
mod migrations {
|
|
|
|
embed_migrations!();
|
|
|
|
|
|
|
|
pub fn run_migrations() {
|
|
|
|
// Make sure the database is up to date (create if it doesn't exist, or run the migrations)
|
2018-12-07 02:05:45 +01:00
|
|
|
let connection = crate::db::get_connection().expect("Can't conect to DB");
|
2018-06-12 17:24:29 +02:00
|
|
|
|
|
|
|
use std::io::stdout;
|
|
|
|
embedded_migrations::run_with_output(&connection, &mut stdout()).expect("Can't run migrations");
|
|
|
|
}
|
|
|
|
}
|
2018-02-10 01:00:55 +01:00
|
|
|
|
|
|
|
fn main() {
|
2018-12-06 20:35:25 +01:00
|
|
|
if CONFIG.extended_logging {
|
|
|
|
init_logging().ok();
|
|
|
|
}
|
|
|
|
|
2018-05-12 22:55:18 +02:00
|
|
|
check_db();
|
|
|
|
check_rsa_keys();
|
2018-09-13 20:59:51 +02:00
|
|
|
check_web_vault();
|
2018-08-30 17:43:46 +02:00
|
|
|
migrations::run_migrations();
|
2018-02-10 01:00:55 +01:00
|
|
|
|
|
|
|
init_rocket().launch();
|
|
|
|
}
|
|
|
|
|
2018-12-06 20:35:25 +01:00
|
|
|
fn init_logging() -> Result<(), fern::InitError> {
|
|
|
|
let mut logger = fern::Dispatch::new()
|
2018-12-30 23:34:31 +01:00
|
|
|
.format(|out, message, record| {
|
|
|
|
out.finish(format_args!(
|
|
|
|
"{}[{}][{}] {}",
|
|
|
|
chrono::Local::now().format("[%Y-%m-%d %H:%M:%S]"),
|
|
|
|
record.target(),
|
|
|
|
record.level(),
|
|
|
|
message
|
|
|
|
))
|
|
|
|
})
|
|
|
|
.level(log::LevelFilter::Debug)
|
|
|
|
.level_for("hyper", log::LevelFilter::Warn)
|
|
|
|
.level_for("ws", log::LevelFilter::Info)
|
|
|
|
.level_for("multipart", log::LevelFilter::Info)
|
|
|
|
.chain(std::io::stdout());
|
2018-12-06 20:35:25 +01:00
|
|
|
|
|
|
|
if let Some(log_file) = CONFIG.log_file.as_ref() {
|
|
|
|
logger = logger.chain(fern::log_file(log_file)?);
|
|
|
|
}
|
|
|
|
|
|
|
|
logger = chain_syslog(logger);
|
|
|
|
logger.apply()?;
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(not(feature = "enable_syslog"))]
|
2018-12-30 23:34:31 +01:00
|
|
|
fn chain_syslog(logger: fern::Dispatch) -> fern::Dispatch {
|
|
|
|
logger
|
|
|
|
}
|
2018-12-06 20:35:25 +01:00
|
|
|
|
|
|
|
#[cfg(feature = "enable_syslog")]
|
|
|
|
fn chain_syslog(logger: fern::Dispatch) -> fern::Dispatch {
|
|
|
|
let syslog_fmt = syslog::Formatter3164 {
|
|
|
|
facility: syslog::Facility::LOG_USER,
|
|
|
|
hostname: None,
|
|
|
|
process: "bitwarden_rs".into(),
|
|
|
|
pid: 0,
|
|
|
|
};
|
|
|
|
|
|
|
|
match syslog::unix(syslog_fmt) {
|
|
|
|
Ok(sl) => logger.chain(sl),
|
|
|
|
Err(e) => {
|
|
|
|
error!("Unable to connect to syslog: {:?}", e);
|
|
|
|
logger
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-12 22:55:18 +02:00
|
|
|
fn check_db() {
|
|
|
|
let path = Path::new(&CONFIG.database_url);
|
|
|
|
|
|
|
|
if let Some(parent) = path.parent() {
|
|
|
|
use std::fs;
|
|
|
|
if fs::create_dir_all(parent).is_err() {
|
2018-12-06 20:35:25 +01:00
|
|
|
error!("Error creating database directory");
|
2018-05-12 22:55:18 +02:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
2018-07-31 16:07:17 +02:00
|
|
|
|
|
|
|
// Turn on WAL in SQLite
|
|
|
|
use diesel::RunQueryDsl;
|
|
|
|
let connection = db::get_connection().expect("Can't conect to DB");
|
2018-12-30 23:34:31 +01:00
|
|
|
diesel::sql_query("PRAGMA journal_mode=wal")
|
|
|
|
.execute(&connection)
|
|
|
|
.expect("Failed to turn on WAL");
|
2018-05-12 22:55:18 +02:00
|
|
|
}
|
|
|
|
|
2018-02-17 01:13:02 +01:00
|
|
|
fn check_rsa_keys() {
|
|
|
|
// If the RSA keys don't exist, try to create them
|
2018-12-30 23:34:31 +01:00
|
|
|
if !util::file_exists(&CONFIG.private_rsa_key) || !util::file_exists(&CONFIG.public_rsa_key) {
|
2018-12-06 20:35:25 +01:00
|
|
|
info!("JWT keys don't exist, checking if OpenSSL is available...");
|
2018-02-17 01:13:02 +01:00
|
|
|
|
2018-12-30 23:34:31 +01:00
|
|
|
Command::new("openssl").arg("version").output().unwrap_or_else(|_| {
|
2018-12-06 20:35:25 +01:00
|
|
|
info!("Can't create keys because OpenSSL is not available, make sure it's installed and available on the PATH");
|
2018-02-17 01:13:02 +01:00
|
|
|
exit(1);
|
|
|
|
});
|
|
|
|
|
2018-12-06 20:35:25 +01:00
|
|
|
info!("OpenSSL detected, creating keys...");
|
2018-02-17 01:13:02 +01:00
|
|
|
|
2018-12-30 23:34:31 +01:00
|
|
|
let mut success = Command::new("openssl")
|
|
|
|
.arg("genrsa")
|
|
|
|
.arg("-out")
|
|
|
|
.arg(&CONFIG.private_rsa_key_pem)
|
|
|
|
.output()
|
|
|
|
.expect("Failed to create private pem file")
|
|
|
|
.status
|
|
|
|
.success();
|
|
|
|
|
|
|
|
success &= Command::new("openssl")
|
|
|
|
.arg("rsa")
|
|
|
|
.arg("-in")
|
|
|
|
.arg(&CONFIG.private_rsa_key_pem)
|
|
|
|
.arg("-outform")
|
|
|
|
.arg("DER")
|
|
|
|
.arg("-out")
|
|
|
|
.arg(&CONFIG.private_rsa_key)
|
|
|
|
.output()
|
|
|
|
.expect("Failed to create private der file")
|
|
|
|
.status
|
|
|
|
.success();
|
|
|
|
|
|
|
|
success &= Command::new("openssl")
|
|
|
|
.arg("rsa")
|
|
|
|
.arg("-in")
|
|
|
|
.arg(&CONFIG.private_rsa_key)
|
|
|
|
.arg("-inform")
|
|
|
|
.arg("DER")
|
2018-02-17 01:13:02 +01:00
|
|
|
.arg("-RSAPublicKey_out")
|
2018-12-30 23:34:31 +01:00
|
|
|
.arg("-outform")
|
|
|
|
.arg("DER")
|
|
|
|
.arg("-out")
|
|
|
|
.arg(&CONFIG.public_rsa_key)
|
|
|
|
.output()
|
|
|
|
.expect("Failed to create public der file")
|
|
|
|
.status
|
|
|
|
.success();
|
2018-02-17 01:13:02 +01:00
|
|
|
|
|
|
|
if success {
|
2018-12-06 20:35:25 +01:00
|
|
|
info!("Keys created correctly.");
|
2018-02-17 01:13:02 +01:00
|
|
|
} else {
|
2018-12-06 20:35:25 +01:00
|
|
|
error!("Error creating keys, exiting...");
|
2018-02-17 01:13:02 +01:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-24 22:38:23 +02:00
|
|
|
fn check_web_vault() {
|
2018-06-12 21:09:42 +02:00
|
|
|
if !CONFIG.web_vault_enabled {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-04-24 22:38:23 +02:00
|
|
|
let index_path = Path::new(&CONFIG.web_vault_folder).join("index.html");
|
|
|
|
|
|
|
|
if !index_path.exists() {
|
2018-12-06 20:35:25 +01:00
|
|
|
error!("Web vault is not found. Please follow the steps in the README to install it");
|
2018-04-24 22:38:23 +02:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-10 01:00:55 +01:00
|
|
|
lazy_static! {
|
|
|
|
// Load the config from .env or from environment variables
|
|
|
|
static ref CONFIG: Config = Config::load();
|
|
|
|
}
|
|
|
|
|
2018-08-13 13:46:32 +02:00
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct MailConfig {
|
|
|
|
smtp_host: String,
|
|
|
|
smtp_port: u16,
|
|
|
|
smtp_ssl: bool,
|
2018-08-15 08:32:19 +02:00
|
|
|
smtp_from: String,
|
2018-08-15 17:00:55 +02:00
|
|
|
smtp_username: Option<String>,
|
|
|
|
smtp_password: Option<String>,
|
2018-08-13 13:46:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
impl MailConfig {
|
|
|
|
fn load() -> Option<Self> {
|
2018-12-07 02:05:45 +01:00
|
|
|
use crate::util::{get_env, get_env_or};
|
2018-09-13 20:59:51 +02:00
|
|
|
|
2018-08-13 13:46:32 +02:00
|
|
|
// When SMTP_HOST is absent, we assume the user does not want to enable it.
|
2018-09-13 20:59:51 +02:00
|
|
|
let smtp_host = match get_env("SMTP_HOST") {
|
|
|
|
Some(host) => host,
|
|
|
|
None => return None,
|
|
|
|
};
|
|
|
|
|
|
|
|
let smtp_from = get_env("SMTP_FROM").unwrap_or_else(|| {
|
2018-12-06 20:35:25 +01:00
|
|
|
error!("Please specify SMTP_FROM to enable SMTP support.");
|
2018-09-13 20:59:51 +02:00
|
|
|
exit(1);
|
|
|
|
});
|
2018-08-13 13:46:32 +02:00
|
|
|
|
2018-09-13 20:59:51 +02:00
|
|
|
let smtp_ssl = get_env_or("SMTP_SSL", true);
|
2018-12-30 23:34:31 +01:00
|
|
|
let smtp_port = get_env("SMTP_PORT").unwrap_or_else(|| if smtp_ssl { 587u16 } else { 25u16 });
|
2018-09-13 20:59:51 +02:00
|
|
|
|
|
|
|
let smtp_username = get_env("SMTP_USERNAME");
|
|
|
|
let smtp_password = get_env("SMTP_PASSWORD").or_else(|| {
|
2018-08-15 17:00:55 +02:00
|
|
|
if smtp_username.as_ref().is_some() {
|
2018-12-06 20:35:25 +01:00
|
|
|
error!("SMTP_PASSWORD is mandatory when specifying SMTP_USERNAME.");
|
2018-08-15 17:00:55 +02:00
|
|
|
exit(1);
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2018-08-13 13:46:32 +02:00
|
|
|
Some(MailConfig {
|
2018-09-13 20:59:51 +02:00
|
|
|
smtp_host,
|
|
|
|
smtp_port,
|
|
|
|
smtp_ssl,
|
|
|
|
smtp_from,
|
|
|
|
smtp_username,
|
|
|
|
smtp_password,
|
2018-08-13 13:46:32 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-10 01:00:55 +01:00
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct Config {
|
|
|
|
database_url: String,
|
|
|
|
icon_cache_folder: String,
|
|
|
|
attachments_folder: String,
|
2018-02-17 01:13:02 +01:00
|
|
|
|
2018-12-18 22:33:31 +01:00
|
|
|
icon_cache_ttl: u64,
|
|
|
|
icon_cache_negttl: u64,
|
|
|
|
|
2018-02-17 01:13:02 +01:00
|
|
|
private_rsa_key: String,
|
|
|
|
private_rsa_key_pem: String,
|
|
|
|
public_rsa_key: String,
|
|
|
|
|
2018-02-10 01:00:55 +01:00
|
|
|
web_vault_folder: String,
|
2018-06-12 21:09:42 +02:00
|
|
|
web_vault_enabled: bool,
|
2018-02-10 01:00:55 +01:00
|
|
|
|
2018-10-15 00:25:16 +02:00
|
|
|
websocket_enabled: bool,
|
2018-09-28 13:46:13 +02:00
|
|
|
websocket_url: String,
|
2018-09-13 20:59:51 +02:00
|
|
|
|
2018-12-06 20:35:25 +01:00
|
|
|
extended_logging: bool,
|
|
|
|
log_file: Option<String>,
|
|
|
|
|
2018-06-12 21:09:42 +02:00
|
|
|
local_icon_extractor: bool,
|
2018-02-10 01:00:55 +01:00
|
|
|
signups_allowed: bool,
|
2018-09-10 15:51:40 +02:00
|
|
|
invitations_allowed: bool,
|
2018-12-18 18:52:58 +01:00
|
|
|
admin_token: Option<String>,
|
2018-02-10 01:00:55 +01:00
|
|
|
password_iterations: i32,
|
2018-08-10 15:21:42 +02:00
|
|
|
show_password_hint: bool,
|
2018-08-13 13:46:32 +02:00
|
|
|
|
2018-07-12 21:46:50 +02:00
|
|
|
domain: String,
|
2018-07-12 23:28:01 +02:00
|
|
|
domain_set: bool,
|
2018-08-13 13:46:32 +02:00
|
|
|
|
2018-11-16 02:40:27 +01:00
|
|
|
yubico_cred_set: bool,
|
|
|
|
yubico_client_id: String,
|
|
|
|
yubico_secret_key: String,
|
2018-11-16 02:54:53 +01:00
|
|
|
yubico_server: Option<String>,
|
2018-11-16 02:40:27 +01:00
|
|
|
|
2018-08-13 13:46:32 +02:00
|
|
|
mail: Option<MailConfig>,
|
2018-02-10 01:00:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Config {
|
|
|
|
fn load() -> Self {
|
2018-12-07 02:05:45 +01:00
|
|
|
use crate::util::{get_env, get_env_or};
|
2018-02-10 01:00:55 +01:00
|
|
|
dotenv::dotenv().ok();
|
|
|
|
|
2018-09-13 20:59:51 +02:00
|
|
|
let df = get_env_or("DATA_FOLDER", "data".to_string());
|
|
|
|
let key = get_env_or("RSA_KEY_FILENAME", format!("{}/{}", &df, "rsa_key"));
|
2018-02-17 01:13:02 +01:00
|
|
|
|
2018-09-13 20:59:51 +02:00
|
|
|
let domain = get_env("DOMAIN");
|
2018-07-12 23:28:01 +02:00
|
|
|
|
2018-11-16 02:40:27 +01:00
|
|
|
let yubico_client_id = get_env("YUBICO_CLIENT_ID");
|
|
|
|
let yubico_secret_key = get_env("YUBICO_SECRET_KEY");
|
|
|
|
|
2018-02-10 01:00:55 +01:00
|
|
|
Config {
|
2018-09-13 20:59:51 +02:00
|
|
|
database_url: get_env_or("DATABASE_URL", format!("{}/{}", &df, "db.sqlite3")),
|
|
|
|
icon_cache_folder: get_env_or("ICON_CACHE_FOLDER", format!("{}/{}", &df, "icon_cache")),
|
|
|
|
attachments_folder: get_env_or("ATTACHMENTS_FOLDER", format!("{}/{}", &df, "attachments")),
|
2018-02-17 01:13:02 +01:00
|
|
|
|
2018-12-18 22:33:31 +01:00
|
|
|
// icon_cache_ttl defaults to 30 days (30 * 24 * 60 * 60 seconds)
|
2019-01-04 00:25:38 +01:00
|
|
|
icon_cache_ttl: get_env_or("ICON_CACHE_TTL", 2_592_000),
|
2018-12-18 22:33:31 +01:00
|
|
|
// icon_cache_negttl defaults to 3 days (3 * 24 * 60 * 60 seconds)
|
2019-01-04 00:25:38 +01:00
|
|
|
icon_cache_negttl: get_env_or("ICON_CACHE_NEGTTL", 259_200),
|
2018-12-18 22:33:31 +01:00
|
|
|
|
2018-06-12 21:09:42 +02:00
|
|
|
private_rsa_key: format!("{}.der", &key),
|
|
|
|
private_rsa_key_pem: format!("{}.pem", &key),
|
|
|
|
public_rsa_key: format!("{}.pub.der", &key),
|
2018-02-17 01:13:02 +01:00
|
|
|
|
2018-09-13 20:59:51 +02:00
|
|
|
web_vault_folder: get_env_or("WEB_VAULT_FOLDER", "web-vault/".into()),
|
|
|
|
web_vault_enabled: get_env_or("WEB_VAULT_ENABLED", true),
|
|
|
|
|
2018-10-15 00:25:16 +02:00
|
|
|
websocket_enabled: get_env_or("WEBSOCKET_ENABLED", false),
|
2018-12-30 23:34:31 +01:00
|
|
|
websocket_url: format!(
|
|
|
|
"{}:{}",
|
|
|
|
get_env_or("WEBSOCKET_ADDRESS", "0.0.0.0".to_string()),
|
|
|
|
get_env_or("WEBSOCKET_PORT", 3012)
|
|
|
|
),
|
|
|
|
|
2018-12-06 20:35:25 +01:00
|
|
|
extended_logging: get_env_or("EXTENDED_LOGGING", true),
|
|
|
|
log_file: get_env("LOG_FILE"),
|
2018-02-10 01:00:55 +01:00
|
|
|
|
2018-09-13 20:59:51 +02:00
|
|
|
local_icon_extractor: get_env_or("LOCAL_ICON_EXTRACTOR", false),
|
|
|
|
signups_allowed: get_env_or("SIGNUPS_ALLOWED", true),
|
2018-12-18 18:52:58 +01:00
|
|
|
admin_token: get_env("ADMIN_TOKEN"),
|
2018-09-13 20:59:51 +02:00
|
|
|
invitations_allowed: get_env_or("INVITATIONS_ALLOWED", true),
|
|
|
|
password_iterations: get_env_or("PASSWORD_ITERATIONS", 100_000),
|
|
|
|
show_password_hint: get_env_or("SHOW_PASSWORD_HINT", true),
|
2018-08-10 15:21:42 +02:00
|
|
|
|
2018-09-13 20:59:51 +02:00
|
|
|
domain_set: domain.is_some(),
|
2018-07-12 23:28:01 +02:00
|
|
|
domain: domain.unwrap_or("http://localhost".into()),
|
2018-08-13 13:46:32 +02:00
|
|
|
|
2018-11-16 02:40:27 +01:00
|
|
|
yubico_cred_set: yubico_client_id.is_some() && yubico_secret_key.is_some(),
|
|
|
|
yubico_client_id: yubico_client_id.unwrap_or("00000".into()),
|
|
|
|
yubico_secret_key: yubico_secret_key.unwrap_or("AAAAAAA".into()),
|
2018-11-16 02:54:53 +01:00
|
|
|
yubico_server: get_env("YUBICO_SERVER"),
|
2018-11-16 02:40:27 +01:00
|
|
|
|
2018-08-13 13:46:32 +02:00
|
|
|
mail: MailConfig::load(),
|
2018-02-10 01:00:55 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|