2019-07-29 09:09:09 +02:00
|
|
|
#![deny(warnings)]
|
|
|
|
|
2020-01-08 11:49:26 +01:00
|
|
|
use std::path::PathBuf;
|
2019-05-08 02:39:46 +02:00
|
|
|
|
2019-09-14 15:44:08 +02:00
|
|
|
use kanidm::config::Configuration;
|
|
|
|
use kanidm::core::{
|
2019-11-29 01:48:22 +01:00
|
|
|
backup_server_core, create_server_core, domain_rename_core, recover_account_core,
|
2020-06-07 01:53:10 +02:00
|
|
|
reindex_server_core, restore_server_core, verify_server_core,
|
2019-07-29 09:09:18 +02:00
|
|
|
};
|
2019-07-15 01:15:25 +02:00
|
|
|
|
2020-01-08 11:49:26 +01:00
|
|
|
use log::{error, info};
|
2019-07-15 01:15:25 +02:00
|
|
|
use structopt::StructOpt;
|
|
|
|
|
|
|
|
#[derive(Debug, StructOpt)]
|
2019-09-14 10:21:41 +02:00
|
|
|
struct CommonOpt {
|
2019-07-15 01:15:25 +02:00
|
|
|
#[structopt(short = "d", long = "debug")]
|
|
|
|
debug: bool,
|
|
|
|
#[structopt(parse(from_os_str), short = "D", long = "db_path")]
|
|
|
|
db_path: PathBuf,
|
|
|
|
}
|
|
|
|
|
2019-09-14 10:21:41 +02:00
|
|
|
#[derive(Debug, StructOpt)]
|
|
|
|
struct ServerOpt {
|
|
|
|
#[structopt(parse(from_os_str), short = "C", long = "ca")]
|
|
|
|
ca_path: Option<PathBuf>,
|
|
|
|
#[structopt(parse(from_os_str), short = "c", long = "cert")]
|
|
|
|
cert_path: Option<PathBuf>,
|
|
|
|
#[structopt(parse(from_os_str), short = "k", long = "key")]
|
|
|
|
key_path: Option<PathBuf>,
|
|
|
|
#[structopt(short = "b", long = "bindaddr")]
|
|
|
|
bind: Option<String>,
|
2020-06-10 04:07:43 +02:00
|
|
|
#[structopt(short = "l", long = "ldapbindaddr")]
|
|
|
|
ldapbind: Option<String>,
|
2019-09-14 10:21:41 +02:00
|
|
|
#[structopt(flatten)]
|
|
|
|
commonopts: CommonOpt,
|
|
|
|
}
|
|
|
|
|
2019-07-15 01:15:25 +02:00
|
|
|
#[derive(Debug, StructOpt)]
|
|
|
|
struct BackupOpt {
|
|
|
|
#[structopt(parse(from_os_str))]
|
|
|
|
path: PathBuf,
|
|
|
|
#[structopt(flatten)]
|
2019-09-14 10:21:41 +02:00
|
|
|
commonopts: CommonOpt,
|
2019-07-15 01:15:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, StructOpt)]
|
|
|
|
struct RestoreOpt {
|
|
|
|
#[structopt(parse(from_os_str))]
|
|
|
|
path: PathBuf,
|
|
|
|
#[structopt(flatten)]
|
2019-09-14 10:21:41 +02:00
|
|
|
commonopts: CommonOpt,
|
2019-07-15 01:15:25 +02:00
|
|
|
}
|
|
|
|
|
2019-09-04 03:06:37 +02:00
|
|
|
#[derive(Debug, StructOpt)]
|
|
|
|
struct RecoverAccountOpt {
|
|
|
|
#[structopt(short)]
|
|
|
|
name: String,
|
|
|
|
#[structopt(flatten)]
|
2019-09-14 10:21:41 +02:00
|
|
|
commonopts: CommonOpt,
|
2019-09-04 03:06:37 +02:00
|
|
|
}
|
|
|
|
|
2019-11-29 01:48:22 +01:00
|
|
|
#[derive(Debug, StructOpt)]
|
|
|
|
struct DomainOpt {
|
|
|
|
#[structopt(short)]
|
|
|
|
new_domain_name: String,
|
|
|
|
#[structopt(flatten)]
|
|
|
|
commonopts: CommonOpt,
|
|
|
|
}
|
|
|
|
|
2019-07-15 01:15:25 +02:00
|
|
|
#[derive(Debug, StructOpt)]
|
|
|
|
enum Opt {
|
|
|
|
#[structopt(name = "server")]
|
|
|
|
Server(ServerOpt),
|
|
|
|
#[structopt(name = "backup")]
|
|
|
|
Backup(BackupOpt),
|
|
|
|
#[structopt(name = "restore")]
|
|
|
|
Restore(RestoreOpt),
|
2019-07-29 09:09:09 +02:00
|
|
|
#[structopt(name = "verify")]
|
2019-09-14 10:21:41 +02:00
|
|
|
Verify(CommonOpt),
|
2019-09-04 03:06:37 +02:00
|
|
|
#[structopt(name = "recover_account")]
|
|
|
|
RecoverAccount(RecoverAccountOpt),
|
2020-06-07 01:53:10 +02:00
|
|
|
// #[structopt(name = "reset_server_id")]
|
|
|
|
// ResetServerId(CommonOpt),
|
2019-11-17 03:36:32 +01:00
|
|
|
#[structopt(name = "reindex")]
|
|
|
|
Reindex(CommonOpt),
|
2019-11-29 01:48:22 +01:00
|
|
|
#[structopt(name = "domain_name_change")]
|
|
|
|
DomainChange(DomainOpt),
|
2019-07-15 01:15:25 +02:00
|
|
|
}
|
2018-11-11 22:59:09 +01:00
|
|
|
|
2019-09-06 05:05:27 +02:00
|
|
|
impl Opt {
|
|
|
|
fn debug(&self) -> bool {
|
|
|
|
match self {
|
2019-09-14 10:21:41 +02:00
|
|
|
Opt::Server(sopt) => sopt.commonopts.debug,
|
2020-06-07 01:53:10 +02:00
|
|
|
Opt::Verify(sopt) | Opt::Reindex(sopt) => sopt.debug,
|
2019-09-14 10:21:41 +02:00
|
|
|
Opt::Backup(bopt) => bopt.commonopts.debug,
|
|
|
|
Opt::Restore(ropt) => ropt.commonopts.debug,
|
|
|
|
Opt::RecoverAccount(ropt) => ropt.commonopts.debug,
|
2019-11-29 01:48:22 +01:00
|
|
|
Opt::DomainChange(dopt) => dopt.commonopts.debug,
|
2019-09-06 05:05:27 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-05 06:01:20 +02:00
|
|
|
#[actix_rt::main]
|
|
|
|
async fn main() {
|
2019-07-15 01:15:25 +02:00
|
|
|
// Read cli args, determine if we should backup/restore
|
|
|
|
let opt = Opt::from_args();
|
|
|
|
|
2019-07-12 07:28:46 +02:00
|
|
|
// Read our config (if any)
|
2019-07-15 01:15:25 +02:00
|
|
|
let mut config = Configuration::new();
|
|
|
|
// Apply any cli overrides?
|
2019-07-12 07:28:46 +02:00
|
|
|
|
|
|
|
// Configure the server logger. This could be adjusted based on what config
|
|
|
|
// says.
|
2019-09-06 05:05:27 +02:00
|
|
|
if opt.debug() {
|
2019-09-30 11:01:20 +02:00
|
|
|
::std::env::set_var("RUST_LOG", "actix_web=debug,kanidm=debug");
|
2019-09-06 05:05:27 +02:00
|
|
|
} else {
|
2020-06-10 04:07:43 +02:00
|
|
|
::std::env::set_var("RUST_LOG", "actix_web=info,kanidm=warn");
|
2019-09-06 05:05:27 +02:00
|
|
|
}
|
2020-06-05 06:01:20 +02:00
|
|
|
|
|
|
|
env_logger::builder()
|
|
|
|
.format_timestamp(None)
|
|
|
|
.format_level(false)
|
|
|
|
.init();
|
2019-07-12 07:28:46 +02:00
|
|
|
|
2019-07-15 01:15:25 +02:00
|
|
|
match opt {
|
|
|
|
Opt::Server(sopt) => {
|
|
|
|
info!("Running in server mode ...");
|
|
|
|
|
2019-09-14 10:21:41 +02:00
|
|
|
config.update_db_path(&sopt.commonopts.db_path);
|
|
|
|
config.update_tls(&sopt.ca_path, &sopt.cert_path, &sopt.key_path);
|
|
|
|
config.update_bind(&sopt.bind);
|
2020-06-10 04:07:43 +02:00
|
|
|
config.update_ldapbind(&sopt.ldapbind);
|
2019-07-15 01:15:25 +02:00
|
|
|
|
2020-06-10 04:07:43 +02:00
|
|
|
let sctx = create_server_core(config).await;
|
2020-06-05 06:01:20 +02:00
|
|
|
match sctx {
|
|
|
|
Ok(sctx) => {
|
|
|
|
tokio::signal::ctrl_c().await.unwrap();
|
|
|
|
println!("Ctrl-C received, shutting down");
|
|
|
|
sctx.stop()
|
|
|
|
}
|
|
|
|
Err(_) => {
|
|
|
|
error!("Failed to start server core!");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2019-07-15 01:15:25 +02:00
|
|
|
}
|
|
|
|
Opt::Backup(bopt) => {
|
|
|
|
info!("Running in backup mode ...");
|
|
|
|
|
2019-09-14 10:21:41 +02:00
|
|
|
config.update_db_path(&bopt.commonopts.db_path);
|
2019-07-15 01:15:25 +02:00
|
|
|
|
|
|
|
let p = match bopt.path.to_str() {
|
|
|
|
Some(p) => p,
|
|
|
|
None => {
|
|
|
|
error!("Invalid backup path");
|
|
|
|
std::process::exit(1);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
backup_server_core(config, p);
|
|
|
|
}
|
|
|
|
Opt::Restore(ropt) => {
|
|
|
|
info!("Running in restore mode ...");
|
|
|
|
|
2019-09-14 10:21:41 +02:00
|
|
|
config.update_db_path(&ropt.commonopts.db_path);
|
2018-09-29 09:54:16 +02:00
|
|
|
|
2019-07-15 01:15:25 +02:00
|
|
|
let p = match ropt.path.to_str() {
|
|
|
|
Some(p) => p,
|
|
|
|
None => {
|
|
|
|
error!("Invalid restore path");
|
|
|
|
std::process::exit(1);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
restore_server_core(config, p);
|
|
|
|
}
|
2019-07-29 09:09:09 +02:00
|
|
|
Opt::Verify(vopt) => {
|
2019-11-17 03:36:32 +01:00
|
|
|
info!("Running in db verification mode ...");
|
2019-07-29 09:09:09 +02:00
|
|
|
|
|
|
|
config.update_db_path(&vopt.db_path);
|
|
|
|
verify_server_core(config);
|
|
|
|
}
|
2019-09-04 03:06:37 +02:00
|
|
|
Opt::RecoverAccount(raopt) => {
|
|
|
|
info!("Running account recovery ...");
|
|
|
|
|
|
|
|
let password = rpassword::prompt_password_stderr("new password: ").unwrap();
|
2019-09-14 10:21:41 +02:00
|
|
|
config.update_db_path(&raopt.commonopts.db_path);
|
2019-09-04 03:06:37 +02:00
|
|
|
|
|
|
|
recover_account_core(config, raopt.name, password);
|
|
|
|
}
|
2020-06-07 01:53:10 +02:00
|
|
|
/*
|
2019-09-14 10:21:41 +02:00
|
|
|
Opt::ResetServerId(vopt) => {
|
2020-06-07 01:53:10 +02:00
|
|
|
info!("Resetting server id. THIS WILL BREAK REPLICATION");
|
2019-09-14 10:21:41 +02:00
|
|
|
|
|
|
|
config.update_db_path(&vopt.db_path);
|
|
|
|
reset_sid_core(config);
|
|
|
|
}
|
2020-06-07 01:53:10 +02:00
|
|
|
*/
|
2019-11-17 03:36:32 +01:00
|
|
|
Opt::Reindex(copt) => {
|
|
|
|
info!("Running in reindex mode ...");
|
|
|
|
|
|
|
|
config.update_db_path(&copt.db_path);
|
|
|
|
reindex_server_core(config);
|
|
|
|
}
|
2019-11-29 01:48:22 +01:00
|
|
|
Opt::DomainChange(dopt) => {
|
|
|
|
info!("Running in domain name change mode ... this may take a long time ...");
|
|
|
|
|
|
|
|
config.update_db_path(&dopt.commonopts.db_path);
|
|
|
|
domain_rename_core(config, dopt.new_domain_name);
|
|
|
|
}
|
2019-07-15 01:15:25 +02:00
|
|
|
}
|
2018-09-29 09:54:16 +02:00
|
|
|
}
|