Skip to content

Commit e2d244c

Browse files
committed
remove shell args
1 parent c635fdd commit e2d244c

File tree

7 files changed

+29
-52
lines changed

7 files changed

+29
-52
lines changed

sshdconfig/locales/en-us.toml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@ registry = "Registry"
1515

1616
[get]
1717
notImplemented = "get not yet implemented for Microsoft.OpenSSH.SSHD/sshd_config"
18-
defaultShellEmpty = "default_shell cannot be empty"
19-
defaultShellMustBeString = "default_shell must be a string"
20-
defaultShellCmdOptionMustBeString = "value must be a string"
18+
defaultShellEmpty = "shell cannot be empty"
19+
defaultShellMustBeString = "shell must be a string"
20+
defaultShellCmdOptionMustBeString = "cmdOption must be a string"
2121
defaultShellEscapeArgsMustBe0Or1 = "'%{input}' must be a 0 or 1"
22-
defaultShellEscapeArgsMustBeDWord = "value must be a DWord"
22+
defaultShellEscapeArgsMustBeDWord = "escapeArguments must be a DWord"
2323
windowsOnly = "Microsoft.OpenSSH.SSHD/Windows is only applicable to Windows"
2424

2525
[set]
@@ -44,4 +44,4 @@ invalidValue = "operator is an invalid value for node"
4444
unknownNode = "unknown node: '%{kind}'"
4545

4646
[util]
47-
sshdNoHostkeys = "elevated security context required"
47+
sshdNoHostkeys = "elevated security context required"

sshdconfig/src/args.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,10 @@ pub struct DefaultShell {
3838
pub shell: Option<String>,
3939
pub cmd_option: Option<String>,
4040
pub escape_arguments: Option<bool>,
41-
pub shell_arguments: Option<Vec<String>>,
4241
}
4342

4443
#[derive(Clone, Debug, Eq, PartialEq, ValueEnum)]
4544
pub enum Resource {
46-
DefaultShell,
4745
SshdConfig,
46+
WindowsGlobal
4847
}

sshdconfig/src/get.rs

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@ use rust_i18n::t;
1717
///
1818
/// This function will return an error if the desired settings cannot be retrieved.
1919
pub fn invoke_get(resource: &Resource) -> Result<(), SshdConfigError> {
20-
match resource {
21-
&Resource::DefaultShell => get_default_shell(),
22-
&Resource::SshdConfig => Err(SshdConfigError::NotImplemented(t!("get.notImplemented").to_string())),
20+
match *resource {
21+
Resource::SshdConfig => Err(SshdConfigError::NotImplemented(t!("get.notImplemented").to_string())),
22+
Resource::WindowsGlobal => get_default_shell()
2323
}
2424
}
2525

@@ -28,19 +28,11 @@ fn get_default_shell() -> Result<(), SshdConfigError> {
2828
let registry_helper = RegistryHelper::new(REGISTRY_PATH, Some(DEFAULT_SHELL.to_string()), None)?;
2929
let default_shell: Registry = registry_helper.get()?;
3030
let mut shell = None;
31-
let mut shell_arguments = None;
32-
// default_shell is a single string consisting of the shell exe path and, optionally, any arguments
31+
// default_shell is a single string consisting of the shell exe path
3332
if let Some(value) = default_shell.value_data {
3433
match value {
3534
RegistryValueData::String(s) => {
36-
let parts: Vec<&str> = s.split_whitespace().collect();
37-
if parts.is_empty() {
38-
return Err(SshdConfigError::InvalidInput(t!("get.defaultShellEmpty").to_string()));
39-
}
40-
shell = Some(parts[0].to_string());
41-
if parts.len() > 1 {
42-
shell_arguments = Some(parts[1..].iter().map(|&s| s.to_string()).collect());
43-
}
35+
shell = Some(s);
4436
}
4537
_ => return Err(SshdConfigError::InvalidInput(t!("get.defaultShellMustBeString").to_string())),
4638
}
@@ -74,8 +66,7 @@ fn get_default_shell() -> Result<(), SshdConfigError> {
7466
let result = DefaultShell {
7567
shell,
7668
cmd_option,
77-
escape_arguments,
78-
shell_arguments
69+
escape_arguments
7970
};
8071

8172
let output = serde_json::to_string(&result)?;

sshdconfig/src/main.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,11 @@ fn main() {
3131
Command::Set { input } => invoke_set(input),
3232
Command::Schema { resource } => {
3333
let schema = match resource {
34-
Resource::DefaultShell => {
35-
schema_for!(DefaultShell)
36-
}
3734
Resource::SshdConfig => {
3835
schema_for!(SshdConfigParser)
36+
},
37+
Resource::WindowsGlobal => {
38+
schema_for!(DefaultShell)
3939
}
4040
};
4141
println!("{}", serde_json::to_string(&schema).unwrap());

sshdconfig/src/set.rs

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,18 +20,18 @@ use rust_i18n::t;
2020
pub fn invoke_set(input: &str) -> Result<(), SshdConfigError> {
2121
match serde_json::from_str::<DefaultShell>(input) {
2222
Ok(default_shell) => {
23-
set_default_shell(default_shell.shell, default_shell.cmd_option, default_shell.escape_arguments, default_shell.shell_arguments)
23+
set_default_shell(default_shell.shell, default_shell.cmd_option, default_shell.escape_arguments)
2424
},
2525
Err(e) => {
26-
// TODO: handle other commands like repeatable keywords or sshd_config modifications
2726
Err(SshdConfigError::InvalidInput(t!("set.failedToParseInput", error = e).to_string()))
2827
}
2928
}
3029
}
3130

3231
#[cfg(windows)]
33-
fn set_default_shell(shell: Option<String>, cmd_option: Option<String>, escape_arguments: Option<bool>, shell_arguments: Option<Vec<String>>) -> Result<(), SshdConfigError> {
32+
fn set_default_shell(shell: Option<String>, cmd_option: Option<String>, escape_arguments: Option<bool>) -> Result<(), SshdConfigError> {
3433
if let Some(shell) = shell {
34+
// TODO: if shell contains quotes, we need to remove them
3535
let shell_path = Path::new(&shell);
3636
if shell_path.is_relative() && shell_path.components().any(|c| c == std::path::Component::ParentDir) {
3737
return Err(SshdConfigError::InvalidInput(t!("set.shellPathMustNotBeRelative").to_string()));
@@ -40,13 +40,7 @@ fn set_default_shell(shell: Option<String>, cmd_option: Option<String>, escape_a
4040
return Err(SshdConfigError::InvalidInput(t!("set.shellPathDoesNotExist", shell = shell).to_string()));
4141
}
4242

43-
let mut shell_data = shell.clone();
44-
if let Some(shell_args) = shell_arguments {
45-
let args_str = shell_args.join(" ");
46-
shell_data = format!("{shell} {args_str}");
47-
}
48-
49-
set_registry(DEFAULT_SHELL, RegistryValueData::String(shell_data))?;
43+
set_registry(DEFAULT_SHELL, RegistryValueData::String(shell))?;
5044
} else {
5145
remove_registry(DEFAULT_SHELL)?;
5246
}
@@ -72,7 +66,7 @@ fn set_default_shell(shell: Option<String>, cmd_option: Option<String>, escape_a
7266
}
7367

7468
#[cfg(not(windows))]
75-
fn set_default_shell(_shell: Option<String>, _cmd_option: Option<String>, _escape_arguments: Option<bool>, _shell_arguments: Option<Vec<String>>) -> Result<(), SshdConfigError> {
69+
fn set_default_shell(_shell: Option<String>, _cmd_option: Option<String>, _escape_arguments: Option<bool>) -> Result<(), SshdConfigError> {
7670
Err(SshdConfigError::InvalidInput(t!("get.windowsOnly")))
7771
}
7872

sshdconfig/sshd.dsc.resource.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
"args": [
1212
"get",
1313
"-r",
14-
"default-shell"
14+
"windows-global"
1515
]
1616
},
1717
"set": {
@@ -30,7 +30,7 @@
3030
"args": [
3131
"schema",
3232
"-r",
33-
"default-shell"
33+
"windows-global"
3434
]
3535
}
3636
}

sshdconfig/tests/defaultshell.tests.ps1

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -62,62 +62,57 @@ Describe 'Default Shell Configuration Tests' -Skip:(!$IsWindows) {
6262
$testShell = "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe"
6363
New-ItemProperty -Path $RegistryPath -Name "DefaultShell" -Value $testShell
6464

65-
$output = sshdconfig get
65+
$output = sshdconfig get -r windows-global
6666
$LASTEXITCODE | Should -Be 0
6767

6868
$result = $output | ConvertFrom-Json
6969
$result.shell | Should -Be $testShell
7070
$result.cmd_option | Should -BeNullOrEmpty
7171
$result.escape_arguments | Should -BeNullOrEmpty
72-
$result.shell_arguments | Should -BeNullOrEmpty
7372
}
7473

7574
It 'Should get default shell with args when registry value exists' {
7675
$testShell = "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe"
77-
$testShellWithArgs = "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -NoProfile -NonInteractive"
76+
$testShellWithArgs = "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe"
7877
New-ItemProperty -Path $RegistryPath -Name "DefaultShell" -Value $testShellWithArgs
7978
New-ItemProperty -Path $RegistryPath -Name "DefaultShellCommandOption" -Value "/c"
8079
New-ItemProperty -Path $RegistryPath -Name "DefaultShellEscapeArguments" -Value 0 -Type DWord
8180

82-
$output = sshdconfig get
81+
$output = sshdconfig get -r windows-global
8382
$LASTEXITCODE | Should -Be 0
8483

8584
$result = $output | ConvertFrom-Json
8685
$result.shell | Should -Be $testShell
8786
$result.cmd_option | Should -Be "/c"
8887
$result.escape_arguments | Should -Be $false
89-
$result.shell_arguments | Should -Be @("-NoProfile", "-NonInteractive")
9088
}
9189

9290
It 'Should handle empty default shell registry values' -Skip:(!$IsWindows) {
93-
$output = sshdconfig get
91+
$output = sshdconfig get -r windows-global
9492
$LASTEXITCODE | Should -Be 0
9593

9694
$result = $output | ConvertFrom-Json
9795
$result.shell | Should -BeNullOrEmpty
9896
$result.cmd_option | Should -BeNullOrEmpty
9997
$result.escape_arguments | Should -BeNullOrEmpty
100-
$result.shell_arguments | Should -BeNullOrEmpty
10198
}
10299
}
103100

104101
Context 'Set Default Shell' {
105102
It 'Should set default shell with valid configuration' {
106103
$testShell = "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe"
107-
$testShellWithArgs = "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -NoProfile -NonInteractive"
108104

109105
$inputConfig = @{
110106
shell = $testShell
111107
cmd_option = "/c"
112108
escape_arguments = $false
113-
shell_arguments = @("-NoProfile", "-NonInteractive")
114109
} | ConvertTo-Json
115110

116111
sshdconfig set --input $inputConfig
117112
$LASTEXITCODE | Should -Be 0
118113

119114
$defaultShell = Get-ItemProperty -Path $RegistryPath -Name "DefaultShell" -ErrorAction SilentlyContinue
120-
$defaultShell.DefaultShell | Should -Be $testShellWithArgs
115+
$defaultShell.DefaultShell | Should -Be $testShell
121116

122117
$cmdOption = Get-ItemProperty -Path $RegistryPath -Name "DefaultShellCommandOption" -ErrorAction SilentlyContinue
123118
$cmdOption.DefaultShellCommandOption | Should -Be "/c"
@@ -164,22 +159,20 @@ Describe 'Default Shell Configuration Tests' -Skip:(!$IsWindows) {
164159
shell = "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe"
165160
cmd_option = "/c"
166161
escape_arguments = $true
167-
shell_arguments = @("-NoProfile", "-NonInteractive")
168162
}
169163
$inputJson = $originalConfig | ConvertTo-Json
170164

171165
sshdconfig set --input $inputJson
172166
$LASTEXITCODE | Should -Be 0
173167

174-
$getOutput = sshdconfig get
168+
$getOutput = sshdconfig get -r windows-global
175169
$LASTEXITCODE | Should -Be 0
176170

177171
$retrievedConfig = $getOutput | ConvertFrom-Json
178172

179173
$retrievedConfig.shell | Should -Be $originalConfig.shell
180174
$retrievedConfig.cmd_option | Should -Be $originalConfig.cmd_option
181175
$retrievedConfig.escape_arguments | Should -Be $originalConfig.escape_arguments
182-
$retrievedConfig.shell_arguments | Should -Be $originalConfig.shell_arguments
183176
}
184177
}
185178

@@ -209,7 +202,7 @@ Describe 'Default Shell Configuration Error Handling on Non-Windows Platforms' -
209202
}
210203

211204
It 'Should return error for get command' {
212-
$out = sshdconfig get 2>&1
205+
$out = sshdconfig get -r windows-global 2>&1
213206
$LASTEXITCODE | Should -Not -Be 0
214207
$out | Should -BeLike '*not applicable to this platform*'
215208
}

0 commit comments

Comments
 (0)