use super::{command_mechanism, ActionMechanism, ActionSpec};
use sysknife_types::RiskLevel;
pub fn specs() -> Vec<ActionSpec> {
vec![
get_authorized_keys("alice"),
add_authorized_key("alice", "ssh-ed25519 AAAA..."),
remove_authorized_key("alice", "ssh-ed25519 AAAA..."),
set_sshd_option("PermitRootLogin", "prohibit-password"),
]
}
const SSHD_OPTION_HELPER: &str = "/usr/lib/sysknife/sshd-option-edit";
pub fn set_sshd_option(option: &str, value: &str) -> ActionSpec {
ActionSpec {
action_name: "SetSshdOption",
mechanism: command_mechanism(
"sudo",
[SSHD_OPTION_HELPER, "--option", option, "--value", value],
),
risk_level: RiskLevel::High,
reboot_required: false,
rollback_available: false,
}
}
pub fn get_authorized_keys(username: &str) -> ActionSpec {
ActionSpec {
action_name: "GetAuthorizedKeys",
mechanism: command_mechanism("cat", [&format!("/home/{username}/.ssh/authorized_keys")]),
risk_level: RiskLevel::Low,
reboot_required: false,
rollback_available: false,
}
}
pub fn add_authorized_key(username: &str, public_key: &str) -> ActionSpec {
let keys_path = format!("/home/{username}/.ssh/authorized_keys");
let script = format!(
"grep -Fxq '{key}' '{path}' 2>/dev/null || echo '{key}' >> '{path}'",
key = public_key,
path = keys_path,
);
ActionSpec {
action_name: "AddAuthorizedKey",
mechanism: ActionMechanism::Command {
program: "sudo",
args: vec!["sh".to_string(), "-c".to_string(), script],
},
risk_level: RiskLevel::High,
reboot_required: false,
rollback_available: false,
}
}
pub fn remove_authorized_key(username: &str, public_key: &str) -> ActionSpec {
let keys_path = format!("/home/{username}/.ssh/authorized_keys");
let script = format!(
"sed -i '\\|^{key}$|d' '{path}'",
key = public_key,
path = keys_path,
);
ActionSpec {
action_name: "RemoveAuthorizedKey",
mechanism: ActionMechanism::Command {
program: "sudo",
args: vec!["sh".to_string(), "-c".to_string(), script],
},
risk_level: RiskLevel::High,
reboot_required: false,
rollback_available: false,
}
}