2018-12-07 01:05:45 +00:00
|
|
|
#![feature(proc_macro_hygiene, decl_macro, vec_remove_item, try_trait)]
|
|
|
|
#![recursion_limit = "128"]
|
2018-10-10 18:40:39 +00:00
|
|
|
|
2018-12-30 22:34:31 +00: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;
|
|
|
|
|
2019-01-11 13:18:13 +00:00
|
|
|
use rocket::{fairing::AdHoc, Rocket};
|
2019-01-13 00:39:29 +00:00
|
|
|
|
2018-12-30 22:34:31 +00:00
|
|
|
use std::{
|
|
|
|
path::Path,
|
|
|
|
process::{exit, Command},
|
|
|
|
};
|
2018-02-10 00:00:55 +00:00
|
|
|
|
2018-12-30 22:34:31 +00:00
|
|
|
#[macro_use]
|
|
|
|
mod error;
|
2018-02-10 00:00:55 +00:00
|
|
|
mod api;
|
|
|
|
mod auth;
|
2019-01-25 17:23:51 +00:00
|
|
|
mod config;
|
2018-12-30 22:34:31 +00:00
|
|
|
mod crypto;
|
|
|
|
mod db;
|
2018-08-15 06:32:19 +00:00
|
|
|
mod mail;
|
2018-12-30 22:34:31 +00:00
|
|
|
mod util;
|
2018-02-10 00:00:55 +00:00
|
|
|
|
2019-01-25 17:23:51 +00:00
|
|
|
pub use config::CONFIG;
|
|
|
|
|
2018-02-10 00:00:55 +00:00
|
|
|
fn init_rocket() -> Rocket {
|
|
|
|
rocket::ignite()
|
|
|
|
.mount("/", api::web_routes())
|
|
|
|
.mount("/api", api::core_routes())
|
2018-12-23 21:37:02 +00:00
|
|
|
.mount("/admin", api::admin_routes())
|
2018-02-10 00:00:55 +00:00
|
|
|
.mount("/identity", api::identity_routes())
|
|
|
|
.mount("/icons", api::icons_routes())
|
2018-08-24 17:02:34 +00:00
|
|
|
.mount("/notifications", api::notifications_routes())
|
2018-02-10 00:00:55 +00:00
|
|
|
.manage(db::init_pool())
|
2018-08-30 15:43:46 +00:00
|
|
|
.manage(api::start_notification_server())
|
2018-12-23 21:37:02 +00:00
|
|
|
.attach(util::AppHeaders())
|
2019-01-11 13:18:13 +00:00
|
|
|
.attach(unofficial_warning())
|
2018-02-10 00:00:55 +00: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 15:24:29 +00: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 01:05:45 +00:00
|
|
|
let connection = crate::db::get_connection().expect("Can't conect to DB");
|
2018-06-12 15:24:29 +00:00
|
|
|
|
|
|
|
use std::io::stdout;
|
|
|
|
embedded_migrations::run_with_output(&connection, &mut stdout()).expect("Can't run migrations");
|
|
|
|
}
|
|
|
|
}
|
2018-02-10 00:00:55 +00:00
|
|
|
|
|
|
|
fn main() {
|
2019-01-25 17:23:51 +00:00
|
|
|
if CONFIG.extended_logging() {
|
2018-12-06 19:35:25 +00:00
|
|
|
init_logging().ok();
|
|
|
|
}
|
|
|
|
|
2018-05-12 20:55:18 +00:00
|
|
|
check_db();
|
|
|
|
check_rsa_keys();
|
2018-09-13 18:59:51 +00:00
|
|
|
check_web_vault();
|
2018-08-30 15:43:46 +00:00
|
|
|
migrations::run_migrations();
|
2018-02-10 00:00:55 +00:00
|
|
|
|
|
|
|
init_rocket().launch();
|
|
|
|
}
|
|
|
|
|
2018-12-06 19:35:25 +00:00
|
|
|
fn init_logging() -> Result<(), fern::InitError> {
|
|
|
|
let mut logger = fern::Dispatch::new()
|
2018-12-30 22:34:31 +00: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)
|
2019-01-12 14:23:46 +00:00
|
|
|
.level_for("rustls", log::LevelFilter::Warn)
|
2019-01-19 20:36:34 +00:00
|
|
|
.level_for("handlebars", log::LevelFilter::Warn)
|
2018-12-30 22:34:31 +00:00
|
|
|
.level_for("ws", log::LevelFilter::Info)
|
|
|
|
.level_for("multipart", log::LevelFilter::Info)
|
|
|
|
.chain(std::io::stdout());
|
2018-12-06 19:35:25 +00:00
|
|
|
|
2019-01-25 17:23:51 +00:00
|
|
|
if let Some(log_file) = CONFIG.log_file() {
|
2018-12-06 19:35:25 +00:00
|
|
|
logger = logger.chain(fern::log_file(log_file)?);
|
|
|
|
}
|
|
|
|
|
|
|
|
logger = chain_syslog(logger);
|
|
|
|
logger.apply()?;
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(not(feature = "enable_syslog"))]
|
2018-12-30 22:34:31 +00:00
|
|
|
fn chain_syslog(logger: fern::Dispatch) -> fern::Dispatch {
|
|
|
|
logger
|
|
|
|
}
|
2018-12-06 19:35:25 +00: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 20:55:18 +00:00
|
|
|
fn check_db() {
|
2019-01-25 17:23:51 +00:00
|
|
|
let url = CONFIG.database_url();
|
|
|
|
let path = Path::new(&url);
|
2018-05-12 20:55:18 +00:00
|
|
|
|
|
|
|
if let Some(parent) = path.parent() {
|
|
|
|
use std::fs;
|
|
|
|
if fs::create_dir_all(parent).is_err() {
|
2018-12-06 19:35:25 +00:00
|
|
|
error!("Error creating database directory");
|
2018-05-12 20:55:18 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
2018-07-31 14:07:17 +00:00
|
|
|
|
|
|
|
// Turn on WAL in SQLite
|
|
|
|
use diesel::RunQueryDsl;
|
|
|
|
let connection = db::get_connection().expect("Can't conect to DB");
|
2018-12-30 22:34:31 +00:00
|
|
|
diesel::sql_query("PRAGMA journal_mode=wal")
|
|
|
|
.execute(&connection)
|
|
|
|
.expect("Failed to turn on WAL");
|
2018-05-12 20:55:18 +00:00
|
|
|
}
|
|
|
|
|
2018-02-17 00:13:02 +00:00
|
|
|
fn check_rsa_keys() {
|
|
|
|
// If the RSA keys don't exist, try to create them
|
2019-01-25 17:23:51 +00:00
|
|
|
if !util::file_exists(&CONFIG.private_rsa_key()) || !util::file_exists(&CONFIG.public_rsa_key()) {
|
2018-12-06 19:35:25 +00:00
|
|
|
info!("JWT keys don't exist, checking if OpenSSL is available...");
|
2018-02-17 00:13:02 +00:00
|
|
|
|
2018-12-30 22:34:31 +00:00
|
|
|
Command::new("openssl").arg("version").output().unwrap_or_else(|_| {
|
2018-12-06 19:35:25 +00:00
|
|
|
info!("Can't create keys because OpenSSL is not available, make sure it's installed and available on the PATH");
|
2018-02-17 00:13:02 +00:00
|
|
|
exit(1);
|
|
|
|
});
|
|
|
|
|
2018-12-06 19:35:25 +00:00
|
|
|
info!("OpenSSL detected, creating keys...");
|
2018-02-17 00:13:02 +00:00
|
|
|
|
2018-12-30 22:34:31 +00:00
|
|
|
let mut success = Command::new("openssl")
|
|
|
|
.arg("genrsa")
|
|
|
|
.arg("-out")
|
2019-01-25 17:23:51 +00:00
|
|
|
.arg(&CONFIG.private_rsa_key_pem())
|
2018-12-30 22:34:31 +00:00
|
|
|
.output()
|
|
|
|
.expect("Failed to create private pem file")
|
|
|
|
.status
|
|
|
|
.success();
|
|
|
|
|
|
|
|
success &= Command::new("openssl")
|
|
|
|
.arg("rsa")
|
|
|
|
.arg("-in")
|
2019-01-25 17:23:51 +00:00
|
|
|
.arg(&CONFIG.private_rsa_key_pem())
|
2018-12-30 22:34:31 +00:00
|
|
|
.arg("-outform")
|
|
|
|
.arg("DER")
|
|
|
|
.arg("-out")
|
2019-01-25 17:23:51 +00:00
|
|
|
.arg(&CONFIG.private_rsa_key())
|
2018-12-30 22:34:31 +00:00
|
|
|
.output()
|
|
|
|
.expect("Failed to create private der file")
|
|
|
|
.status
|
|
|
|
.success();
|
|
|
|
|
|
|
|
success &= Command::new("openssl")
|
|
|
|
.arg("rsa")
|
|
|
|
.arg("-in")
|
2019-01-25 17:23:51 +00:00
|
|
|
.arg(&CONFIG.private_rsa_key())
|
2018-12-30 22:34:31 +00:00
|
|
|
.arg("-inform")
|
|
|
|
.arg("DER")
|
2018-02-17 00:13:02 +00:00
|
|
|
.arg("-RSAPublicKey_out")
|
2018-12-30 22:34:31 +00:00
|
|
|
.arg("-outform")
|
|
|
|
.arg("DER")
|
|
|
|
.arg("-out")
|
2019-01-25 17:23:51 +00:00
|
|
|
.arg(&CONFIG.public_rsa_key())
|
2018-12-30 22:34:31 +00:00
|
|
|
.output()
|
|
|
|
.expect("Failed to create public der file")
|
|
|
|
.status
|
|
|
|
.success();
|
2018-02-17 00:13:02 +00:00
|
|
|
|
|
|
|
if success {
|
2018-12-06 19:35:25 +00:00
|
|
|
info!("Keys created correctly.");
|
2018-02-17 00:13:02 +00:00
|
|
|
} else {
|
2018-12-06 19:35:25 +00:00
|
|
|
error!("Error creating keys, exiting...");
|
2018-02-17 00:13:02 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-24 20:38:23 +00:00
|
|
|
fn check_web_vault() {
|
2019-01-25 17:23:51 +00:00
|
|
|
if !CONFIG.web_vault_enabled() {
|
2018-06-12 19:09:42 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-01-25 17:23:51 +00:00
|
|
|
let index_path = Path::new(&CONFIG.web_vault_folder()).join("index.html");
|
2018-04-24 20:38:23 +00:00
|
|
|
|
|
|
|
if !index_path.exists() {
|
2018-12-06 19:35:25 +00:00
|
|
|
error!("Web vault is not found. Please follow the steps in the README to install it");
|
2018-04-24 20:38:23 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-11 13:18:13 +00:00
|
|
|
fn unofficial_warning() -> AdHoc {
|
|
|
|
AdHoc::on_launch("Unofficial Warning", |_| {
|
|
|
|
warn!("/--------------------------------------------------------------------\\");
|
|
|
|
warn!("| This is an *unofficial* Bitwarden implementation, DO NOT use the |");
|
|
|
|
warn!("| official channels to report bugs/features, regardless of client. |");
|
|
|
|
warn!("| Report URL: https://github.com/dani-garcia/bitwarden_rs/issues/new |");
|
|
|
|
warn!("\\--------------------------------------------------------------------/");
|
|
|
|
})
|
|
|
|
}
|