2020-05-03 15:24:51 +00:00
|
|
|
#![forbid(unsafe_code)]
|
2020-07-14 21:44:01 +00:00
|
|
|
#![cfg_attr(feature = "unstable", feature(ip))]
|
2019-02-02 23:22:18 +00:00
|
|
|
#![recursion_limit = "256"]
|
2018-10-10 18:40:39 +00:00
|
|
|
|
2020-06-03 23:21:30 +00:00
|
|
|
extern crate openssl;
|
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;
|
2020-03-16 16:53:22 +00:00
|
|
|
|
2018-12-30 22:34:31 +00:00
|
|
|
use std::{
|
2019-12-06 21:19:07 +00:00
|
|
|
fs::create_dir_all,
|
2020-06-15 21:40:39 +00:00
|
|
|
panic,
|
2018-12-30 22:34:31 +00:00
|
|
|
path::Path,
|
|
|
|
process::{exit, Command},
|
2019-12-06 21:19:07 +00:00
|
|
|
str::FromStr,
|
2020-06-15 21:40:39 +00:00
|
|
|
thread,
|
2018-12-30 22:34:31 +00:00
|
|
|
};
|
2018-02-10 00:00:55 +00:00
|
|
|
|
2020-08-12 16:45:26 +00:00
|
|
|
use structopt::StructOpt;
|
|
|
|
|
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;
|
2020-08-18 15:15:44 +00:00
|
|
|
#[macro_use]
|
2018-12-30 22:34:31 +00:00
|
|
|
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;
|
2019-02-14 01:03:37 +00:00
|
|
|
pub use error::{Error, MapResult};
|
2019-01-25 17:23:51 +00:00
|
|
|
|
2020-03-02 19:57:06 +00:00
|
|
|
#[derive(Debug, StructOpt)]
|
|
|
|
#[structopt(name = "bitwarden_rs", about = "A Bitwarden API server written in Rust")]
|
|
|
|
struct Opt {
|
|
|
|
/// Prints the app version
|
|
|
|
#[structopt(short, long)]
|
|
|
|
version: bool,
|
|
|
|
}
|
|
|
|
|
2018-02-10 00:00:55 +00:00
|
|
|
fn main() {
|
2020-03-02 19:57:06 +00:00
|
|
|
parse_args();
|
2019-02-20 19:59:37 +00:00
|
|
|
launch_info();
|
|
|
|
|
2019-12-06 21:19:07 +00:00
|
|
|
use log::LevelFilter as LF;
|
|
|
|
let level = LF::from_str(&CONFIG.log_level()).expect("Valid log level");
|
|
|
|
init_logging(level).ok();
|
|
|
|
|
|
|
|
let extra_debug = match level {
|
|
|
|
LF::Trace | LF::Debug => true,
|
|
|
|
_ => false,
|
|
|
|
};
|
2018-12-06 19:35:25 +00:00
|
|
|
|
2018-05-12 20:55:18 +00:00
|
|
|
check_rsa_keys();
|
2018-09-13 18:59:51 +00:00
|
|
|
check_web_vault();
|
2018-02-10 00:00:55 +00:00
|
|
|
|
2019-11-06 19:21:47 +00:00
|
|
|
create_icon_cache_folder();
|
|
|
|
|
2019-12-06 21:19:07 +00:00
|
|
|
launch_rocket(extra_debug);
|
2018-02-10 00:00:55 +00:00
|
|
|
}
|
|
|
|
|
2020-03-02 19:57:06 +00:00
|
|
|
fn parse_args() {
|
|
|
|
let opt = Opt::from_args();
|
|
|
|
if opt.version {
|
2020-03-22 15:13:34 +00:00
|
|
|
if let Some(version) = option_env!("BWRS_VERSION") {
|
2020-03-02 19:57:06 +00:00
|
|
|
println!("bitwarden_rs {}", version);
|
|
|
|
} else {
|
|
|
|
println!("bitwarden_rs (Version info from Git not present)");
|
|
|
|
}
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-20 19:59:37 +00:00
|
|
|
fn launch_info() {
|
|
|
|
println!("/--------------------------------------------------------------------\\");
|
|
|
|
println!("| Starting Bitwarden_RS |");
|
|
|
|
|
2020-03-22 15:13:34 +00:00
|
|
|
if let Some(version) = option_env!("BWRS_VERSION") {
|
2019-02-20 19:59:37 +00:00
|
|
|
println!("|{:^68}|", format!("Version {}", version));
|
|
|
|
}
|
|
|
|
|
|
|
|
println!("|--------------------------------------------------------------------|");
|
|
|
|
println!("| This is an *unofficial* Bitwarden implementation, DO NOT use the |");
|
|
|
|
println!("| official channels to report bugs/features, regardless of client. |");
|
2020-05-13 19:29:47 +00:00
|
|
|
println!("| Send usage/configuration questions or feature requests to: |");
|
|
|
|
println!("| https://bitwardenrs.discourse.group/ |");
|
|
|
|
println!("| Report suspected bugs/issues in the software itself at: |");
|
|
|
|
println!("| https://github.com/dani-garcia/bitwarden_rs/issues/new |");
|
2019-02-20 19:59:37 +00:00
|
|
|
println!("\\--------------------------------------------------------------------/\n");
|
|
|
|
}
|
|
|
|
|
2019-12-06 21:19:07 +00:00
|
|
|
fn init_logging(level: log::LevelFilter) -> Result<(), fern::InitError> {
|
2018-12-06 19:35:25 +00:00
|
|
|
let mut logger = fern::Dispatch::new()
|
2019-12-06 21:19:07 +00:00
|
|
|
.level(level)
|
|
|
|
// Hide unknown certificate errors if using self-signed
|
|
|
|
.level_for("rustls::session", log::LevelFilter::Off)
|
|
|
|
// Hide failed to close stream messages
|
|
|
|
.level_for("hyper::server", log::LevelFilter::Warn)
|
|
|
|
// Silence rocket logs
|
|
|
|
.level_for("_", log::LevelFilter::Off)
|
|
|
|
.level_for("launch", log::LevelFilter::Off)
|
|
|
|
.level_for("launch_", log::LevelFilter::Off)
|
|
|
|
.level_for("rocket::rocket", log::LevelFilter::Off)
|
|
|
|
.level_for("rocket::fairing", log::LevelFilter::Off)
|
|
|
|
.chain(std::io::stdout());
|
|
|
|
|
|
|
|
if CONFIG.extended_logging() {
|
|
|
|
logger = logger.format(|out, message, record| {
|
2018-12-30 22:34:31 +00:00
|
|
|
out.finish(format_args!(
|
2020-07-23 04:50:49 +00:00
|
|
|
"[{}][{}][{}] {}",
|
|
|
|
chrono::Local::now().format(&CONFIG.log_timestamp_format()),
|
2018-12-30 22:34:31 +00:00
|
|
|
record.target(),
|
|
|
|
record.level(),
|
|
|
|
message
|
|
|
|
))
|
2019-12-06 21:19:07 +00:00
|
|
|
});
|
|
|
|
} else {
|
|
|
|
logger = logger.format(|out, message, _| out.finish(format_args!("{}", message)));
|
|
|
|
}
|
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)?);
|
|
|
|
}
|
|
|
|
|
2019-04-26 20:08:26 +00:00
|
|
|
#[cfg(not(windows))]
|
|
|
|
{
|
|
|
|
if cfg!(feature = "enable_syslog") || CONFIG.use_syslog() {
|
2019-03-29 19:27:20 +00:00
|
|
|
logger = chain_syslog(logger);
|
|
|
|
}
|
|
|
|
}
|
2018-12-06 19:35:25 +00:00
|
|
|
|
|
|
|
logger.apply()?;
|
|
|
|
|
2020-02-25 13:10:52 +00:00
|
|
|
// Catch panics and log them instead of default output to StdErr
|
|
|
|
panic::set_hook(Box::new(|info| {
|
2020-03-16 16:53:22 +00:00
|
|
|
let thread = thread::current();
|
|
|
|
let thread = thread.name().unwrap_or("unnamed");
|
|
|
|
|
|
|
|
let msg = match info.payload().downcast_ref::<&'static str>() {
|
|
|
|
Some(s) => *s,
|
|
|
|
None => match info.payload().downcast_ref::<String>() {
|
|
|
|
Some(s) => &**s,
|
|
|
|
None => "Box<Any>",
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2020-08-12 16:45:26 +00:00
|
|
|
let backtrace = backtrace::Backtrace::new();
|
|
|
|
|
2020-03-16 16:53:22 +00:00
|
|
|
match info.location() {
|
|
|
|
Some(location) => {
|
|
|
|
error!(
|
2020-08-12 16:45:26 +00:00
|
|
|
target: "panic", "thread '{}' panicked at '{}': {}:{}\n{:?}",
|
2020-03-16 16:53:22 +00:00
|
|
|
thread,
|
|
|
|
msg,
|
|
|
|
location.file(),
|
|
|
|
location.line(),
|
2020-08-12 16:45:26 +00:00
|
|
|
backtrace
|
2020-03-16 16:53:22 +00:00
|
|
|
);
|
|
|
|
}
|
2020-06-15 21:40:39 +00:00
|
|
|
None => error!(
|
|
|
|
target: "panic",
|
2020-08-12 16:45:26 +00:00
|
|
|
"thread '{}' panicked at '{}'\n{:?}",
|
2020-06-15 21:40:39 +00:00
|
|
|
thread,
|
|
|
|
msg,
|
2020-08-12 16:45:26 +00:00
|
|
|
backtrace
|
2020-06-15 21:40:39 +00:00
|
|
|
),
|
2020-03-16 16:53:22 +00:00
|
|
|
}
|
2020-02-25 13:10:52 +00:00
|
|
|
}));
|
|
|
|
|
2018-12-06 19:35:25 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2019-03-29 19:27:20 +00:00
|
|
|
#[cfg(not(windows))]
|
2018-12-06 19:35:25 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-06 19:21:47 +00:00
|
|
|
fn create_icon_cache_folder() {
|
|
|
|
// Try to create the icon cache folder, and generate an error if it could not.
|
|
|
|
create_dir_all(&CONFIG.icon_cache_folder()).expect("Error creating icon cache directory");
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2019-02-20 19:59:37 +00:00
|
|
|
Command::new("openssl").arg("version").status().unwrap_or_else(|_| {
|
2020-03-02 19:57:06 +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
|
|
|
|
2019-02-20 19:59:37 +00:00
|
|
|
let key = CONFIG.rsa_key_filename();
|
|
|
|
|
|
|
|
let pem = format!("{}.pem", key);
|
|
|
|
let priv_der = format!("{}.der", key);
|
|
|
|
let pub_der = format!("{}.pub.der", key);
|
|
|
|
|
2018-12-30 22:34:31 +00:00
|
|
|
let mut success = Command::new("openssl")
|
2019-02-20 19:59:37 +00:00
|
|
|
.args(&["genrsa", "-out", &pem])
|
|
|
|
.status()
|
2018-12-30 22:34:31 +00:00
|
|
|
.expect("Failed to create private pem file")
|
|
|
|
.success();
|
|
|
|
|
|
|
|
success &= Command::new("openssl")
|
2019-02-20 19:59:37 +00:00
|
|
|
.args(&["rsa", "-in", &pem, "-outform", "DER", "-out", &priv_der])
|
|
|
|
.status()
|
2018-12-30 22:34:31 +00:00
|
|
|
.expect("Failed to create private der file")
|
|
|
|
.success();
|
|
|
|
|
|
|
|
success &= Command::new("openssl")
|
2019-02-20 19:59:37 +00:00
|
|
|
.args(&["rsa", "-in", &priv_der, "-inform", "DER"])
|
|
|
|
.args(&["-RSAPublicKey_out", "-outform", "DER", "-out", &pub_der])
|
|
|
|
.status()
|
2018-12-30 22:34:31 +00:00
|
|
|
.expect("Failed to create public der file")
|
|
|
|
.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() {
|
2020-08-18 15:15:44 +00:00
|
|
|
error!("Web vault is not found at '{}'. To install it, please follow the steps in: ", CONFIG.web_vault_folder());
|
2019-12-29 14:29:46 +00:00
|
|
|
error!("https://github.com/dani-garcia/bitwarden_rs/wiki/Building-binary#install-the-web-vault");
|
|
|
|
error!("You can also set the environment variable 'WEB_VAULT_ENABLED=false' to disable it");
|
2018-04-24 20:38:23 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-06 21:19:07 +00:00
|
|
|
fn launch_rocket(extra_debug: bool) {
|
2020-08-18 15:15:44 +00:00
|
|
|
let pool = match db::DbPool::from_config() {
|
|
|
|
Ok(p) => p,
|
|
|
|
Err(e) => {
|
|
|
|
error!("Error creating database pool: {:?}", e);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-02-19 05:27:00 +00:00
|
|
|
let basepath = &CONFIG.domain_path();
|
|
|
|
|
|
|
|
// If adding more paths here, consider also adding them to
|
2019-12-06 21:19:07 +00:00
|
|
|
// crate::utils::LOGGED_ROUTES to make sure they appear in the log
|
2020-06-15 21:40:39 +00:00
|
|
|
let result = rocket::ignite()
|
2020-02-19 05:27:00 +00:00
|
|
|
.mount(&[basepath, "/"].concat(), api::web_routes())
|
|
|
|
.mount(&[basepath, "/api"].concat(), api::core_routes())
|
|
|
|
.mount(&[basepath, "/admin"].concat(), api::admin_routes())
|
|
|
|
.mount(&[basepath, "/identity"].concat(), api::identity_routes())
|
|
|
|
.mount(&[basepath, "/icons"].concat(), api::icons_routes())
|
|
|
|
.mount(&[basepath, "/notifications"].concat(), api::notifications_routes())
|
2020-08-18 15:15:44 +00:00
|
|
|
.manage(pool)
|
2019-02-20 19:59:37 +00:00
|
|
|
.manage(api::start_notification_server())
|
2019-09-01 11:00:12 +00:00
|
|
|
.attach(util::AppHeaders())
|
2019-12-06 21:19:07 +00:00
|
|
|
.attach(util::CORS())
|
2020-06-15 21:40:39 +00:00
|
|
|
.attach(util::BetterLogging(extra_debug))
|
|
|
|
.launch();
|
2019-02-20 19:59:37 +00:00
|
|
|
|
|
|
|
// Launch and print error if there is one
|
|
|
|
// The launch will restore the original logging level
|
2020-06-15 21:40:39 +00:00
|
|
|
error!("Launch error {:#?}", result);
|
2019-01-11 13:18:13 +00:00
|
|
|
}
|