sysknife_daemon/actions/
ssh.rs1use super::{command_mechanism, ActionMechanism, ActionSpec};
2use sysknife_types::RiskLevel;
3
4pub fn specs() -> Vec<ActionSpec> {
5 vec![
6 get_authorized_keys("alice"),
7 add_authorized_key("alice", "ssh-ed25519 AAAA..."),
8 remove_authorized_key("alice", "ssh-ed25519 AAAA..."),
9 set_sshd_option("PermitRootLogin", "prohibit-password"),
10 ]
11}
12
13const SSHD_OPTION_HELPER: &str = "/usr/lib/sysknife/sshd-option-edit";
17
18pub fn set_sshd_option(option: &str, value: &str) -> ActionSpec {
27 ActionSpec {
28 action_name: "SetSshdOption",
29 mechanism: command_mechanism(
30 "sudo",
31 [SSHD_OPTION_HELPER, "--option", option, "--value", value],
32 ),
33 risk_level: RiskLevel::High,
34 reboot_required: false,
35 rollback_available: false,
36 }
37}
38
39pub fn get_authorized_keys(username: &str) -> ActionSpec {
40 ActionSpec {
41 action_name: "GetAuthorizedKeys",
42 mechanism: command_mechanism("cat", [&format!("/home/{username}/.ssh/authorized_keys")]),
43 risk_level: RiskLevel::Low,
44 reboot_required: false,
45 rollback_available: false,
46 }
47}
48
49pub fn add_authorized_key(username: &str, public_key: &str) -> ActionSpec {
50 let keys_path = format!("/home/{username}/.ssh/authorized_keys");
51 let script = format!(
55 "grep -Fxq '{key}' '{path}' 2>/dev/null || echo '{key}' >> '{path}'",
56 key = public_key,
57 path = keys_path,
58 );
59
60 ActionSpec {
61 action_name: "AddAuthorizedKey",
62 mechanism: ActionMechanism::Command {
63 program: "sudo",
64 args: vec!["sh".to_string(), "-c".to_string(), script],
65 },
66 risk_level: RiskLevel::High,
67 reboot_required: false,
68 rollback_available: false,
69 }
70}
71
72pub fn remove_authorized_key(username: &str, public_key: &str) -> ActionSpec {
73 let keys_path = format!("/home/{username}/.ssh/authorized_keys");
74 let script = format!(
77 "sed -i '\\|^{key}$|d' '{path}'",
78 key = public_key,
79 path = keys_path,
80 );
81
82 ActionSpec {
83 action_name: "RemoveAuthorizedKey",
84 mechanism: ActionMechanism::Command {
85 program: "sudo",
86 args: vec!["sh".to_string(), "-c".to_string(), script],
87 },
88 risk_level: RiskLevel::High,
92 reboot_required: false,
93 rollback_available: false,
94 }
95}