2023-04-24 11:47:52 +02:00
|
|
|
#![deny(warnings)]
|
|
|
|
#![warn(unused_extern_crates)]
|
|
|
|
#![deny(clippy::todo)]
|
|
|
|
#![deny(clippy::unimplemented)]
|
|
|
|
#![deny(clippy::unwrap_used)]
|
|
|
|
#![deny(clippy::expect_used)]
|
|
|
|
#![deny(clippy::panic)]
|
|
|
|
#![deny(clippy::unreachable)]
|
|
|
|
#![deny(clippy::await_holding_lock)]
|
|
|
|
#![deny(clippy::needless_pass_by_value)]
|
|
|
|
#![deny(clippy::trivially_copy_pass_by_ref)]
|
|
|
|
|
|
|
|
#[macro_use]
|
|
|
|
extern crate tracing;
|
|
|
|
|
|
|
|
use std::process::ExitCode;
|
|
|
|
|
|
|
|
use clap::Parser;
|
|
|
|
use kanidm_unix_common::client::call_daemon;
|
|
|
|
use kanidm_unix_common::constants::DEFAULT_CONFIG_PATH;
|
|
|
|
use kanidm_unix_common::unix_config::KanidmUnixdConfig;
|
2023-08-28 01:27:29 +02:00
|
|
|
use kanidm_unix_common::unix_proto::{
|
2024-10-02 04:12:13 +02:00
|
|
|
ClientRequest, ClientResponse, PamAuthRequest, PamAuthResponse, PamServiceInfo,
|
2023-08-28 01:27:29 +02:00
|
|
|
};
|
|
|
|
// use std::io;
|
2023-04-24 11:47:52 +02:00
|
|
|
use std::path::PathBuf;
|
|
|
|
|
2024-06-17 00:21:25 +02:00
|
|
|
include!("../opt/tool.rs");
|
2023-04-24 11:47:52 +02:00
|
|
|
|
|
|
|
#[tokio::main(flavor = "current_thread")]
|
|
|
|
async fn main() -> ExitCode {
|
|
|
|
let opt = KanidmUnixParser::parse();
|
|
|
|
|
|
|
|
let debug = match opt.commands {
|
|
|
|
KanidmUnixOpt::AuthTest {
|
|
|
|
debug,
|
|
|
|
account_id: _,
|
|
|
|
} => debug,
|
|
|
|
KanidmUnixOpt::CacheClear { debug, really: _ } => debug,
|
|
|
|
KanidmUnixOpt::CacheInvalidate { debug } => debug,
|
|
|
|
KanidmUnixOpt::Status { debug } => debug,
|
|
|
|
KanidmUnixOpt::Version { debug } => debug,
|
|
|
|
};
|
|
|
|
|
|
|
|
if debug {
|
|
|
|
::std::env::set_var("RUST_LOG", "kanidm=debug,kanidm_client=debug");
|
|
|
|
}
|
|
|
|
sketching::tracing_subscriber::fmt::init();
|
|
|
|
|
|
|
|
match opt.commands {
|
|
|
|
KanidmUnixOpt::AuthTest {
|
|
|
|
debug: _,
|
|
|
|
account_id,
|
|
|
|
} => {
|
|
|
|
debug!("Starting PAM auth tester tool ...");
|
|
|
|
|
2023-08-28 01:27:29 +02:00
|
|
|
let Ok(cfg) =
|
|
|
|
KanidmUnixdConfig::new().read_options_from_optional_config(DEFAULT_CONFIG_PATH)
|
|
|
|
else {
|
|
|
|
error!("Failed to parse {}", DEFAULT_CONFIG_PATH);
|
|
|
|
return ExitCode::FAILURE;
|
2023-04-24 11:47:52 +02:00
|
|
|
};
|
|
|
|
|
2023-12-03 07:34:02 +01:00
|
|
|
info!("Sending request for user {}", &account_id);
|
|
|
|
|
2024-10-02 04:12:13 +02:00
|
|
|
let mut req = ClientRequest::PamAuthenticateInit {
|
|
|
|
account_id: account_id.clone(),
|
|
|
|
info: PamServiceInfo {
|
|
|
|
service: "kanidm-unix".to_string(),
|
|
|
|
tty: "/dev/null".to_string(),
|
|
|
|
rhost: "localhost".to_string(),
|
|
|
|
},
|
|
|
|
};
|
2023-08-28 01:27:29 +02:00
|
|
|
loop {
|
|
|
|
match call_daemon(cfg.sock_path.as_str(), req, cfg.unix_sock_timeout).await {
|
|
|
|
Ok(r) => match r {
|
|
|
|
ClientResponse::PamAuthenticateStepResponse(PamAuthResponse::Success) => {
|
|
|
|
println!("auth success!");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
ClientResponse::PamAuthenticateStepResponse(PamAuthResponse::Denied) => {
|
|
|
|
println!("auth failed!");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
ClientResponse::PamAuthenticateStepResponse(PamAuthResponse::Unknown) => {
|
2023-12-03 07:34:02 +01:00
|
|
|
debug!("User may need to be in allow_local_account_override");
|
2023-08-28 01:27:29 +02:00
|
|
|
println!("auth user unknown");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
ClientResponse::PamAuthenticateStepResponse(PamAuthResponse::Password) => {
|
|
|
|
// Prompt for and get the password
|
2024-10-26 09:19:13 +02:00
|
|
|
let cred = match dialoguer::Password::new()
|
|
|
|
.with_prompt("Enter Unix password: ")
|
|
|
|
.interact()
|
|
|
|
{
|
2023-08-28 01:27:29 +02:00
|
|
|
Ok(p) => p,
|
|
|
|
Err(e) => {
|
|
|
|
error!("Problem getting input: {}", e);
|
|
|
|
return ExitCode::FAILURE;
|
|
|
|
}
|
|
|
|
};
|
2023-04-24 11:47:52 +02:00
|
|
|
|
2023-08-28 01:27:29 +02:00
|
|
|
// Setup the req for the next loop.
|
|
|
|
req = ClientRequest::PamAuthenticateStep(PamAuthRequest::Password {
|
|
|
|
cred,
|
|
|
|
});
|
|
|
|
continue;
|
|
|
|
}
|
2024-10-19 01:51:45 +02:00
|
|
|
ClientResponse::Error(err) => {
|
|
|
|
error!("Error from kanidm-unixd: {}", err);
|
|
|
|
break;
|
|
|
|
}
|
2023-12-03 07:34:02 +01:00
|
|
|
ClientResponse::PamAuthenticateStepResponse(_)
|
|
|
|
| ClientResponse::SshKeys(_)
|
|
|
|
| ClientResponse::NssAccounts(_)
|
|
|
|
| ClientResponse::NssAccount(_)
|
|
|
|
| ClientResponse::NssGroup(_)
|
|
|
|
| ClientResponse::NssGroups(_)
|
2024-08-16 01:54:35 +02:00
|
|
|
| ClientResponse::ProviderStatus(_)
|
2023-12-03 07:34:02 +01:00
|
|
|
| ClientResponse::Ok
|
|
|
|
| ClientResponse::PamStatus(_) => {
|
2023-08-28 01:27:29 +02:00
|
|
|
// unexpected response.
|
|
|
|
error!("Error: unexpected response -> {:?}", r);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
Err(e) => {
|
|
|
|
error!("Error -> {:?}", e);
|
|
|
|
break;
|
2023-04-24 11:47:52 +02:00
|
|
|
}
|
|
|
|
}
|
2023-08-28 01:27:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
let sereq = ClientRequest::PamAccountAllowed(account_id);
|
2023-04-24 11:47:52 +02:00
|
|
|
|
2023-07-28 02:48:56 +02:00
|
|
|
match call_daemon(cfg.sock_path.as_str(), sereq, cfg.unix_sock_timeout).await {
|
2023-04-24 11:47:52 +02:00
|
|
|
Ok(r) => match r {
|
|
|
|
ClientResponse::PamStatus(Some(true)) => {
|
|
|
|
println!("account success!");
|
|
|
|
}
|
|
|
|
ClientResponse::PamStatus(Some(false)) => {
|
|
|
|
println!("account failed!");
|
|
|
|
}
|
|
|
|
ClientResponse::PamStatus(None) => {
|
|
|
|
println!("account user unknown");
|
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
// unexpected response.
|
|
|
|
error!("Error: unexpected response -> {:?}", r);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
Err(e) => {
|
|
|
|
error!("Error -> {:?}", e);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
ExitCode::SUCCESS
|
|
|
|
}
|
|
|
|
KanidmUnixOpt::CacheClear { debug: _, really } => {
|
|
|
|
debug!("Starting cache clear tool ...");
|
|
|
|
|
|
|
|
let cfg = match KanidmUnixdConfig::new()
|
|
|
|
.read_options_from_optional_config(DEFAULT_CONFIG_PATH)
|
|
|
|
{
|
|
|
|
Ok(c) => c,
|
|
|
|
Err(_e) => {
|
|
|
|
error!("Failed to parse {}", DEFAULT_CONFIG_PATH);
|
|
|
|
return ExitCode::FAILURE;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
if !really {
|
|
|
|
error!("Are you sure you want to proceed? If so use --really");
|
|
|
|
return ExitCode::SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
let req = ClientRequest::ClearCache;
|
|
|
|
|
2023-07-28 02:48:56 +02:00
|
|
|
match call_daemon(cfg.sock_path.as_str(), req, cfg.unix_sock_timeout).await {
|
2023-04-24 11:47:52 +02:00
|
|
|
Ok(r) => match r {
|
|
|
|
ClientResponse::Ok => info!("success"),
|
|
|
|
_ => {
|
|
|
|
error!("Error: unexpected response -> {:?}", r);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
Err(e) => {
|
|
|
|
error!("Error -> {:?}", e);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
println!("success");
|
|
|
|
ExitCode::SUCCESS
|
|
|
|
}
|
|
|
|
KanidmUnixOpt::CacheInvalidate { debug: _ } => {
|
|
|
|
debug!("Starting cache invalidate tool ...");
|
|
|
|
|
|
|
|
let cfg = match KanidmUnixdConfig::new()
|
|
|
|
.read_options_from_optional_config(DEFAULT_CONFIG_PATH)
|
|
|
|
{
|
|
|
|
Ok(c) => c,
|
|
|
|
Err(_e) => {
|
|
|
|
error!("Failed to parse {}", DEFAULT_CONFIG_PATH);
|
|
|
|
return ExitCode::FAILURE;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
let req = ClientRequest::InvalidateCache;
|
|
|
|
|
2023-07-28 02:48:56 +02:00
|
|
|
match call_daemon(cfg.sock_path.as_str(), req, cfg.unix_sock_timeout).await {
|
2023-04-24 11:47:52 +02:00
|
|
|
Ok(r) => match r {
|
|
|
|
ClientResponse::Ok => info!("success"),
|
|
|
|
_ => {
|
|
|
|
error!("Error: unexpected response -> {:?}", r);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
Err(e) => {
|
|
|
|
error!("Error -> {:?}", e);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
println!("success");
|
|
|
|
ExitCode::SUCCESS
|
|
|
|
}
|
|
|
|
KanidmUnixOpt::Status { debug: _ } => {
|
|
|
|
trace!("Starting cache status tool ...");
|
|
|
|
|
|
|
|
let cfg = match KanidmUnixdConfig::new()
|
|
|
|
.read_options_from_optional_config(DEFAULT_CONFIG_PATH)
|
|
|
|
{
|
|
|
|
Ok(c) => c,
|
|
|
|
Err(_e) => {
|
|
|
|
error!("Failed to parse {}", DEFAULT_CONFIG_PATH);
|
|
|
|
return ExitCode::FAILURE;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
let req = ClientRequest::Status;
|
|
|
|
|
|
|
|
let spath = PathBuf::from(cfg.sock_path.as_str());
|
|
|
|
if !spath.exists() {
|
|
|
|
error!(
|
|
|
|
"kanidm_unixd socket {} does not exist - is the service running?",
|
|
|
|
cfg.sock_path
|
|
|
|
)
|
|
|
|
} else {
|
2023-07-28 02:48:56 +02:00
|
|
|
match call_daemon(cfg.sock_path.as_str(), req, cfg.unix_sock_timeout).await {
|
2023-04-24 11:47:52 +02:00
|
|
|
Ok(r) => match r {
|
2024-08-16 01:54:35 +02:00
|
|
|
ClientResponse::ProviderStatus(results) => {
|
|
|
|
for provider in results {
|
|
|
|
println!(
|
|
|
|
"{}: {}",
|
|
|
|
provider.name,
|
|
|
|
if provider.online { "online" } else { "offline" }
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2023-04-24 11:47:52 +02:00
|
|
|
_ => {
|
|
|
|
error!("Error: unexpected response -> {:?}", r);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
Err(e) => {
|
|
|
|
error!("Error -> {:?}", e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ExitCode::SUCCESS
|
|
|
|
}
|
|
|
|
KanidmUnixOpt::Version { debug: _ } => {
|
2023-07-31 14:27:21 +02:00
|
|
|
println!("kanidm-unix {}", env!("KANIDM_PKG_VERSION"));
|
2023-04-24 11:47:52 +02:00
|
|
|
ExitCode::SUCCESS
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|