2018-08-30 17:43:46 +02:00
|
|
|
#![feature(plugin, custom_derive, vec_remove_item)]
|
2018-02-10 01:00:55 +01:00
|
|
|
#![plugin(rocket_codegen)]
|
2018-07-21 17:27:00 +02:00
|
|
|
#![allow(proc_macro_derive_resolution_fallback)] // TODO: Remove this when diesel update fixes warnings
|
2018-02-10 01:00:55 +01:00
|
|
|
extern crate rocket;
|
|
|
|
extern crate rocket_contrib;
|
|
|
|
extern crate reqwest;
|
|
|
|
extern crate multipart;
|
2018-08-30 17:43:46 +02:00
|
|
|
extern crate ws;
|
|
|
|
extern crate rmpv;
|
|
|
|
extern crate chashmap;
|
2018-02-10 01:00:55 +01:00
|
|
|
extern crate serde;
|
|
|
|
#[macro_use]
|
|
|
|
extern crate serde_derive;
|
|
|
|
#[macro_use]
|
|
|
|
extern crate serde_json;
|
|
|
|
#[macro_use]
|
|
|
|
extern crate diesel;
|
|
|
|
#[macro_use]
|
|
|
|
extern crate diesel_migrations;
|
|
|
|
extern crate ring;
|
|
|
|
extern crate uuid;
|
|
|
|
extern crate chrono;
|
|
|
|
extern crate oath;
|
|
|
|
extern crate data_encoding;
|
|
|
|
extern crate jsonwebtoken as jwt;
|
2018-07-12 21:46:50 +02:00
|
|
|
extern crate u2f;
|
2018-02-10 01:00:55 +01:00
|
|
|
extern crate dotenv;
|
|
|
|
#[macro_use]
|
|
|
|
extern crate lazy_static;
|
2018-07-12 21:46:50 +02:00
|
|
|
#[macro_use]
|
|
|
|
extern crate num_derive;
|
|
|
|
extern crate num_traits;
|
2018-08-15 08:32:19 +02:00
|
|
|
extern crate lettre;
|
|
|
|
extern crate lettre_email;
|
|
|
|
extern crate native_tls;
|
2018-08-15 17:25:59 +02:00
|
|
|
extern crate fast_chemail;
|
2018-08-30 17:43:46 +02:00
|
|
|
extern crate byteorder;
|
2018-02-10 01:00:55 +01:00
|
|
|
|
2018-06-12 17:24:29 +02:00
|
|
|
use std::{env, path::Path, process::{exit, Command}};
|
2018-02-23 00:38:54 +01:00
|
|
|
use rocket::Rocket;
|
2018-02-10 01:00:55 +01:00
|
|
|
|
|
|
|
#[macro_use]
|
|
|
|
mod util;
|
|
|
|
|
|
|
|
mod api;
|
|
|
|
mod db;
|
|
|
|
mod crypto;
|
|
|
|
mod auth;
|
2018-08-15 08:32:19 +02:00
|
|
|
mod mail;
|
2018-02-10 01:00:55 +01:00
|
|
|
|
|
|
|
fn init_rocket() -> Rocket {
|
|
|
|
rocket::ignite()
|
|
|
|
.mount("/", api::web_routes())
|
|
|
|
.mount("/api", api::core_routes())
|
|
|
|
.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-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)
|
|
|
|
let connection = ::db::get_connection().expect("Can't conect to DB");
|
|
|
|
|
|
|
|
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-05-12 22:55:18 +02:00
|
|
|
check_db();
|
|
|
|
check_rsa_keys();
|
2018-06-12 17:24:29 +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-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() {
|
|
|
|
println!("Error creating database directory");
|
|
|
|
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");
|
|
|
|
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
|
|
|
|
if !util::file_exists(&CONFIG.private_rsa_key)
|
|
|
|
|| !util::file_exists(&CONFIG.public_rsa_key) {
|
|
|
|
println!("JWT keys don't exist, checking if OpenSSL is available...");
|
|
|
|
|
|
|
|
Command::new("openssl")
|
|
|
|
.arg("version")
|
|
|
|
.output().unwrap_or_else(|_| {
|
|
|
|
println!("Can't create keys because OpenSSL is not available, make sure it's installed and available on the PATH");
|
|
|
|
exit(1);
|
|
|
|
});
|
|
|
|
|
|
|
|
println!("OpenSSL detected, creating keys...");
|
|
|
|
|
|
|
|
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")
|
|
|
|
.arg("-RSAPublicKey_out")
|
|
|
|
.arg("-outform").arg("DER")
|
|
|
|
.arg("-out").arg(&CONFIG.public_rsa_key)
|
|
|
|
.output().expect("Failed to create public der file")
|
|
|
|
.status.success();
|
|
|
|
|
|
|
|
if success {
|
2018-03-21 00:08:46 +01:00
|
|
|
println!("Keys created correctly.");
|
2018-02-17 01:13:02 +01:00
|
|
|
} else {
|
|
|
|
println!("Error creating keys, exiting...");
|
|
|
|
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() {
|
|
|
|
println!("Web vault is not found. Please follow the steps in the README to install it");
|
|
|
|
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-08-15 17:00:55 +02:00
|
|
|
let smtp_host = env::var("SMTP_HOST").ok();
|
2018-08-13 13:46:32 +02:00
|
|
|
|
|
|
|
// When SMTP_HOST is absent, we assume the user does not want to enable it.
|
|
|
|
if smtp_host.is_none() {
|
|
|
|
return None
|
|
|
|
}
|
|
|
|
|
2018-08-15 08:32:19 +02:00
|
|
|
let smtp_ssl = util::parse_option_string(env::var("SMTP_SSL").ok()).unwrap_or(true);
|
2018-08-13 13:46:32 +02:00
|
|
|
let smtp_port = util::parse_option_string(env::var("SMTP_PORT").ok())
|
|
|
|
.unwrap_or_else(|| {
|
|
|
|
if smtp_ssl {
|
2018-08-15 08:32:19 +02:00
|
|
|
587u16
|
2018-08-13 13:46:32 +02:00
|
|
|
} else {
|
|
|
|
25u16
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2018-08-15 17:00:55 +02:00
|
|
|
let smtp_username = env::var("SMTP_USERNAME").ok();
|
|
|
|
let smtp_password = env::var("SMTP_PASSWORD").ok().or_else(|| {
|
|
|
|
if smtp_username.as_ref().is_some() {
|
|
|
|
println!("Please specify SMTP_PASSWORD to enable SMTP support.");
|
|
|
|
exit(1);
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2018-08-13 13:46:32 +02:00
|
|
|
Some(MailConfig {
|
|
|
|
smtp_host: smtp_host.unwrap(),
|
|
|
|
smtp_port: smtp_port,
|
|
|
|
smtp_ssl: smtp_ssl,
|
2018-08-15 08:32:19 +02:00
|
|
|
smtp_from: util::parse_option_string(env::var("SMTP_FROM").ok())
|
2018-08-15 17:00:55 +02:00
|
|
|
.unwrap_or("bitwarden-rs@localhost".to_string()),
|
|
|
|
smtp_username: smtp_username,
|
|
|
|
smtp_password: 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
|
|
|
|
|
|
|
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-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-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
|
|
|
|
|
|
|
mail: Option<MailConfig>,
|
2018-02-10 01:00:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Config {
|
|
|
|
fn load() -> Self {
|
|
|
|
dotenv::dotenv().ok();
|
|
|
|
|
2018-02-17 01:13:02 +01:00
|
|
|
let df = env::var("DATA_FOLDER").unwrap_or("data".into());
|
2018-06-12 21:09:42 +02:00
|
|
|
let key = env::var("RSA_KEY_FILENAME").unwrap_or(format!("{}/{}", &df, "rsa_key"));
|
2018-02-17 01:13:02 +01:00
|
|
|
|
2018-07-12 23:28:01 +02:00
|
|
|
let domain = env::var("DOMAIN");
|
|
|
|
|
2018-02-10 01:00:55 +01:00
|
|
|
Config {
|
2018-02-17 01:13:02 +01:00
|
|
|
database_url: env::var("DATABASE_URL").unwrap_or(format!("{}/{}", &df, "db.sqlite3")),
|
|
|
|
icon_cache_folder: env::var("ICON_CACHE_FOLDER").unwrap_or(format!("{}/{}", &df, "icon_cache")),
|
|
|
|
attachments_folder: env::var("ATTACHMENTS_FOLDER").unwrap_or(format!("{}/{}", &df, "attachments")),
|
|
|
|
|
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-02-10 01:00:55 +01:00
|
|
|
web_vault_folder: env::var("WEB_VAULT_FOLDER").unwrap_or("web-vault/".into()),
|
2018-06-12 21:09:42 +02:00
|
|
|
web_vault_enabled: util::parse_option_string(env::var("WEB_VAULT_ENABLED").ok()).unwrap_or(true),
|
2018-02-10 01:00:55 +01:00
|
|
|
|
2018-06-12 21:09:42 +02:00
|
|
|
local_icon_extractor: util::parse_option_string(env::var("LOCAL_ICON_EXTRACTOR").ok()).unwrap_or(false),
|
|
|
|
signups_allowed: util::parse_option_string(env::var("SIGNUPS_ALLOWED").ok()).unwrap_or(true),
|
2018-09-10 15:51:40 +02:00
|
|
|
invitations_allowed: util::parse_option_string(env::var("INVITATIONS_ALLOWED").ok()).unwrap_or(true),
|
2018-02-10 01:00:55 +01:00
|
|
|
password_iterations: util::parse_option_string(env::var("PASSWORD_ITERATIONS").ok()).unwrap_or(100_000),
|
2018-08-10 15:21:42 +02:00
|
|
|
show_password_hint: util::parse_option_string(env::var("SHOW_PASSWORD_HINT").ok()).unwrap_or(true),
|
|
|
|
|
2018-07-12 23:28:01 +02:00
|
|
|
domain_set: domain.is_ok(),
|
|
|
|
domain: domain.unwrap_or("http://localhost".into()),
|
2018-08-13 13:46:32 +02:00
|
|
|
|
|
|
|
mail: MailConfig::load(),
|
2018-02-10 01:00:55 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|