1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
use crate::error::Error;
use crate::host_handler::localhost::WhichUser;
use crate::{command::CommandResult, host_handler::privilege::Privilege};
use serde::{Deserialize, Serialize};
pub trait HostHandler: Sized {
fn connect(&mut self, endpoint: &str) -> Result<(), Error>;
fn is_connected(&mut self) -> bool;
fn disconnect(&mut self) -> Result<(), Error>;
fn is_this_command_available(
&mut self,
command: &str,
privilege: &Privilege,
) -> Result<bool, Error>;
fn run_command(&mut self, command: &str, privilege: &Privilege)
-> Result<CommandResult, Error>;
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum ConnectionDetails {
// LocalHost(WhichUser),
// Ssh2(NewSsh2ConnectionDetails),
}
// TODO : add some syntax checks
pub fn final_command(cmd: &str, privilege: &Privilege, user: &WhichUser) -> String {
match user {
WhichUser::CurrentUser => match privilege {
Privilege::None => format!("{} 2>&1", cmd),
Privilege::WithSudo => format!("sudo {} 2>&1", cmd),
Privilege::WithSudoRs => format!("sudo-rs {} 2>&1", cmd),
},
WhichUser::PasswordLessUser(username) => match privilege {
Privilege::None | Privilege::WithSudo => format!("sudo -u {} {} 2>&1", username, cmd),
Privilege::WithSudoRs => format!("sudo-rs -u {} {} 2>&1", username, cmd),
},
WhichUser::UsernamePassword(credentials) => match privilege {
Privilege::None | Privilege::WithSudo => format!(
"echo {} | sudo -S -u {} {} 2>&1",
credentials.password(),
credentials.username(),
cmd
),
Privilege::WithSudoRs => format!(
"echo {} | sudo-rs -S -u {} {} 2>&1",
credentials.password(),
credentials.username(),
cmd
),
},
}
// match privilege {
// Privilege::None => {
// let final_cmd = format!("{} 2>&1", cmd);
// return final_cmd;
// }
// // Privilege::WithSuAsUser(credentials) => {
// // let final_cmd = format!("echo {} | su - {} -c {} 2>&1", credentials.password(), credentials.username(), cmd);
// // return final_cmd;
// // }
// Privilege::WithSudo => {
// let final_cmd = format!("sudo {} 2>&1", cmd);
// return final_cmd;
// }
// // Privilege::WithSudoAsUser(credentials) => {
// // let final_cmd = format!("echo {} | sudo -S -u {} {} 2>&1", credentials.password(), credentials.username(), cmd);
// // return final_cmd;
// // }
// Privilege::WithSudoRs => {
// let final_cmd = format!("sudo-rs {} 2>&1", cmd);
// return final_cmd;
// }
// // Privilege::WithSudoRsAsUser(credentials) => {
// // let final_cmd = format!("echo {} | sudo-rs -u {} {} 2>&1", credentials.password(), credentials.username(), cmd);
// // return final_cmd;
// // }
// }
}