4
4
#[ cfg( windows) ]
5
5
use {
6
6
registry_lib:: { config:: { Registry , RegistryValueData } , RegistryHelper } ,
7
- crate :: args:: DefaultShell
7
+ crate :: args:: { DefaultShell , Resource } ,
8
+ crate :: metadata:: { DEFAULT_SHELL , DEFAULT_SHELL_CMD_OPTION , DEFAULT_SHELL_ESCAPE_ARGS , REGISTRY_PATH } ,
8
9
} ;
9
10
10
11
use crate :: error:: SshdConfigError ;
@@ -14,56 +15,58 @@ use crate::error::SshdConfigError;
14
15
/// # Errors
15
16
///
16
17
/// This function will return an error if the desired settings cannot be retrieved.
17
- pub fn invoke_get ( ) -> Result < ( ) , SshdConfigError > {
18
- // TODO: distinguish between get commands for default shell, repeatable keywords, and sshd_config
19
- get_default_shell ( ) ?;
20
- Ok ( ( ) )
18
+ pub fn invoke_get ( resource : & Resource ) -> Result < ( ) , SshdConfigError > {
19
+ match resource {
20
+ & Resource :: DefaultShell => get_default_shell ( ) ,
21
+ & Resource :: SshdConfig => Err ( SshdConfigError :: NotImplemented ( "get not yet implemented for Microsoft.OpenSSH.SSHD/sshd_config" . to_string ( ) ) ) ,
22
+ }
21
23
}
22
24
23
25
#[ cfg( windows) ]
24
26
fn get_default_shell ( ) -> Result < ( ) , SshdConfigError > {
25
- let registry_helper = RegistryHelper :: new ( "HKLM \\ SOFTWARE \\ OpenSSH" , Some ( "DefaultShell" . to_string ( ) ) , None ) ?;
27
+ let registry_helper = RegistryHelper :: new ( REGISTRY_PATH , Some ( DEFAULT_SHELL . to_string ( ) ) , None ) ?;
26
28
let default_shell: Registry = registry_helper. get ( ) ?;
27
29
let mut shell = None ;
28
30
let mut shell_arguments = None ;
31
+ // default_shell is a single string consisting of the shell exe path and, optionally, any arguments
29
32
if let Some ( value) = default_shell. value_data {
30
33
match value {
31
34
RegistryValueData :: String ( s) => {
32
35
let parts: Vec < & str > = s. split_whitespace ( ) . collect ( ) ;
33
36
if parts. is_empty ( ) {
34
- return Err ( SshdConfigError :: InvalidInput ( "DefaultShell cannot be empty". to_string ( ) ) ) ;
37
+ return Err ( SshdConfigError :: InvalidInput ( format ! ( "{} cannot be empty", DEFAULT_SHELL ) ) ) ;
35
38
}
36
39
shell = Some ( parts[ 0 ] . to_string ( ) ) ;
37
40
if parts. len ( ) > 1 {
38
41
shell_arguments = Some ( parts[ 1 ..] . iter ( ) . map ( |& s| s. to_string ( ) ) . collect ( ) ) ;
39
42
}
40
43
}
41
- _ => return Err ( SshdConfigError :: InvalidInput ( "DefaultShell must be a string". to_string ( ) ) ) ,
44
+ _ => return Err ( SshdConfigError :: InvalidInput ( format ! ( "{} must be a string", DEFAULT_SHELL ) ) ) ,
42
45
}
43
46
}
44
47
45
- let registry_helper = RegistryHelper :: new ( "HKLM \\ SOFTWARE \\ OpenSSH" , Some ( "DefaultShellCommandOption" . to_string ( ) ) , None ) ?;
48
+ let registry_helper = RegistryHelper :: new ( REGISTRY_PATH , Some ( DEFAULT_SHELL_CMD_OPTION . to_string ( ) ) , None ) ?;
46
49
let option: Registry = registry_helper. get ( ) ?;
47
50
let mut cmd_option = None ;
48
51
if let Some ( value) = option. value_data {
49
52
match value {
50
53
RegistryValueData :: String ( s) => cmd_option = Some ( s) ,
51
- _ => return Err ( SshdConfigError :: InvalidInput ( "DefaultShellCommandOption must be a string". to_string ( ) ) ) ,
54
+ _ => return Err ( SshdConfigError :: InvalidInput ( format ! ( "{} must be a string", DEFAULT_SHELL_CMD_OPTION ) ) ) ,
52
55
}
53
56
}
54
57
55
- let registry_helper = RegistryHelper :: new ( "HKLM \\ SOFTWARE \\ OpenSSH" , Some ( "DefaultShellEscapeArguments" . to_string ( ) ) , None ) ?;
58
+ let registry_helper = RegistryHelper :: new ( REGISTRY_PATH , Some ( DEFAULT_SHELL_ESCAPE_ARGS . to_string ( ) ) , None ) ?;
56
59
let escape_args: Registry = registry_helper. get ( ) ?;
57
60
let mut escape_arguments = None ;
58
61
if let Some ( value) = escape_args. value_data {
59
62
if let RegistryValueData :: DWord ( b) = value {
60
63
if b == 0 || b == 1 {
61
64
escape_arguments = if b == 1 { Some ( true ) } else { Some ( false ) } ;
62
65
} else {
63
- return Err ( SshdConfigError :: InvalidInput ( "DefaultShellEscapeArguments must be a boolean" . to_string ( ) ) ) ;
66
+ return Err ( SshdConfigError :: InvalidInput ( format ! ( "{} must be a 0 or 1" , DEFAULT_SHELL_ESCAPE_ARGS ) ) ) ;
64
67
}
65
68
} else {
66
- return Err ( SshdConfigError :: InvalidInput ( "DefaultShellEscapeArguments must be a boolean" . to_string ( ) ) ) ;
69
+ return Err ( SshdConfigError :: InvalidInput ( format ! ( "{} must be a DWord" , DEFAULT_SHELL_ESCAPE_ARGS ) ) ) ;
67
70
}
68
71
}
69
72
@@ -74,12 +77,12 @@ fn get_default_shell() -> Result<(), SshdConfigError> {
74
77
shell_arguments
75
78
} ;
76
79
77
- let output = serde_json:: to_string_pretty ( & result) ?;
80
+ let output = serde_json:: to_string ( & result) ?;
78
81
println ! ( "{output}" ) ;
79
82
Ok ( ( ) )
80
83
}
81
84
82
85
#[ cfg( not( windows) ) ]
83
86
pub fn get_default_shell ( ) -> Result < ( ) , SshdConfigError > {
84
- Err ( SshdConfigError :: InvalidInput ( "Windows registry operations not applicable to this platform " . to_string ( ) ) )
87
+ Err ( SshdConfigError :: InvalidInput ( "Microsoft.OpenSSH.SSHD/ Windows is only applicable to Windows " . to_string ( ) ) )
85
88
}
0 commit comments