|
1 | 1 | // Copyright (c) Microsoft Corporation.
|
2 | 2 | // Licensed under the MIT License.
|
3 | 3 |
|
4 |
| -use registry_lib::{config::{Registry, RegistryValueData}, RegistryHelper}; |
5 |
| -use serde_json::{json, Value}; |
6 |
| -use std::{path::Path, process::Command}; |
| 4 | +use registry_lib::{config::RegistryValueData, RegistryHelper}; |
| 5 | +use std::path::Path; |
7 | 6 |
|
8 |
| -use crate::args::SetCommand; |
| 7 | +use crate::args::DefaultShell; |
9 | 8 | use crate::error::SshdConfigError;
|
10 |
| -use crate::metadata::RepeatableKeyword; |
11 | 9 |
|
12 | 10 | /// Invoke the set command.
|
13 | 11 | ///
|
14 | 12 | /// # Errors
|
15 | 13 | ///
|
16 |
| -/// This function will return an error if the sshd_config is not updated with the desired settings. |
17 |
| -pub fn invoke_set(input: &Option<String>, subcommand: &Option<SetCommand>) -> Result<(), SshdConfigError> { |
18 |
| - if let Some(subcommand) = subcommand { |
19 |
| - match subcommand { |
20 |
| - SetCommand::DefaultShell { shell, cmd_option, escape_arguments, shell_arguments } => { |
21 |
| - set_default_shell(shell, cmd_option, escape_arguments, shell_arguments) |
22 |
| - } |
23 |
| - SetCommand::RepeatableKeyworld { keyword, name, value } => { |
24 |
| - set_repeatable_keyword(keyword, name, value) |
25 |
| - } |
| 14 | +/// This function will return an error if the desired settings cannot be applied. |
| 15 | +pub fn invoke_set(input: &String) -> Result<(), SshdConfigError> { |
| 16 | + match serde_json::from_str::<DefaultShell>(input) { |
| 17 | + Ok(default_shell) => { |
| 18 | + set_default_shell(&default_shell.shell, &default_shell.cmd_option, &default_shell.escape_arguments, &default_shell.shell_arguments) |
| 19 | + }, |
| 20 | + Err(e) => { |
| 21 | + // TODO: handle other commands like repeatable keywords or sshd_config modifications |
| 22 | + return Err(SshdConfigError::InvalidInput(format!("Failed to parse input as DefaultShell: {}", e))); |
26 | 23 | }
|
27 |
| - } else { |
28 |
| - set_regular_keywords(input) |
29 | 24 | }
|
30 | 25 | }
|
31 | 26 |
|
32 |
| -fn set_default_shell(shell: &String, cmd_option: &Option<String>, escape_arguments: &bool, shell_arguments: &Option<Vec<String>>) -> Result<(), SshdConfigError> { |
33 |
| - let shell_path = Path::new(shell); |
34 |
| - if shell_path.is_relative() { |
35 |
| - if shell_path.components().any(|c| c == std::path::Component::ParentDir) { |
36 |
| - return Err(SshdConfigError::InvalidInput("shell path must not be relative".to_string())); |
| 27 | +fn set_default_shell(shell: &Option<String>, cmd_option: &Option<String>, escape_arguments: &Option<bool>, shell_arguments: &Option<Vec<String>>) -> Result<(), SshdConfigError> { |
| 28 | + if let Some(shell) = shell { |
| 29 | + let shell_path = Path::new(shell); |
| 30 | + if shell_path.is_relative() { |
| 31 | + if shell_path.components().any(|c| c == std::path::Component::ParentDir) { |
| 32 | + return Err(SshdConfigError::InvalidInput("shell path must not be relative".to_string())); |
| 33 | + } |
| 34 | + } |
| 35 | + if !shell_path.exists() { |
| 36 | + return Err(SshdConfigError::InvalidInput(format!("shell path does not exist: {}", shell))); |
37 | 37 | }
|
38 |
| - } |
39 |
| - // TODO check if binary exists when it is not an absolute path |
40 |
| - if !shell_path.exists() { |
41 |
| - return Err(SshdConfigError::InvalidInput(format!("shell path does not exist: {}", shell))); |
42 |
| - } |
43 | 38 |
|
44 |
| - let mut shell_data = shell.clone(); |
45 |
| - if let Some(shell_args) = shell_arguments { |
46 |
| - let args_str = shell_args.join(" "); |
47 |
| - shell_data = format!("{} {}", shell, args_str); |
48 |
| - } |
| 39 | + let mut shell_data = shell.clone(); |
| 40 | + if let Some(shell_args) = shell_arguments { |
| 41 | + let args_str = shell_args.join(" "); |
| 42 | + shell_data = format!("{} {}", shell, args_str); |
| 43 | + } |
| 44 | + |
| 45 | + set_registry("DefaultShell", RegistryValueData::String(shell_data))?; |
| 46 | + } else { |
| 47 | + remove_registry("DefaultShell")?; |
| 48 | + }; |
49 | 49 |
|
50 |
| - set_registry_default_shell("DefaultShell", RegistryValueData::String(shell_data))?; |
51 | 50 |
|
52 | 51 | if let Some(cmd_option) = cmd_option {
|
53 |
| - set_registry_default_shell("DefaultShellCommandOption", RegistryValueData::String(cmd_option.clone()))?; |
54 |
| - } |
55 |
| - else { |
56 |
| - return Err(SshdConfigError::InvalidInput("cmd_option is required".to_string())); |
| 52 | + set_registry("DefaultShellCommandOption", RegistryValueData::String(cmd_option.clone()))?; |
| 53 | + } else { |
| 54 | + remove_registry("DefaultShellCommandOption")?; |
57 | 55 | }
|
58 | 56 |
|
59 |
| - let mut escape_data = 0; |
60 |
| - if *escape_arguments { |
61 |
| - escape_data = 1; |
| 57 | + if let Some(escape_args) = escape_arguments { |
| 58 | + let mut escape_data = 0; |
| 59 | + if *escape_args { |
| 60 | + escape_data = 1; |
| 61 | + } |
| 62 | + set_registry("DefaultShellEscapeArguments", RegistryValueData::DWord(escape_data))?; |
| 63 | + } else { |
| 64 | + remove_registry("DefaultShellEscapeArguments")?; |
62 | 65 | }
|
63 |
| - set_registry_default_shell("DefaultShellEscapeArguments ", RegistryValueData::DWord(escape_data))?; |
64 | 66 |
|
65 | 67 | Ok(())
|
66 | 68 | }
|
67 | 69 |
|
68 |
| -pub fn set_repeatable_keyword(_keyword: &RepeatableKeyword, _name: &String, _value: &Option<String>) -> Result<(), SshdConfigError> { |
69 |
| - // TODO: handle repeat keywords like subsystem |
70 |
| - Ok(()) |
71 |
| -} |
72 |
| - |
73 |
| -pub fn set_regular_keywords(_input: &Option<String>) -> Result<(), SshdConfigError> { |
74 |
| - // modify or add all values in the input file |
75 |
| - // have a banner that we are managing the file and check that before modifying |
76 |
| - // if the file is managed by this tool, we can modify it |
77 |
| - // if the file is not managed by the tool, we should back it up |
78 |
| - // get existing sshd_config settings from sshd -T |
79 |
| - // get default sshd_config settings from sshd -T |
80 |
| - // save explicit settings |
81 |
| - // for each of the input keys, update it with the new value |
82 |
| - // or insert it if it doesn't exist (above any match statements) |
83 |
| - // write the updated settings back to the sshd_config file |
84 |
| - // ensure sshd -T is valid after the update? |
| 70 | +fn set_registry(name: &str, data: RegistryValueData) -> Result<(), SshdConfigError> { |
| 71 | + let registry_helper = RegistryHelper::new("HKLM\\SOFTWARE\\OpenSSH", Some(name.to_string()), Some(data))?; |
| 72 | + registry_helper.set()?; |
85 | 73 | Ok(())
|
86 | 74 | }
|
87 | 75 |
|
88 |
| -fn set_registry_default_shell(name: &str, data: RegistryValueData) -> Result<(), SshdConfigError> { |
89 |
| - let registry_helper = RegistryHelper::new("HKLM\\SOFTWARE\\OpenSSH", Some(name.to_string()), Some(data))?; |
90 |
| - registry_helper.set()?; |
| 76 | +fn remove_registry(name: &str) -> Result<(), SshdConfigError> { |
| 77 | + let registry_helper = RegistryHelper::new("HKLM\\SOFTWARE\\OpenSSH", Some(name.to_string()), None)?; |
| 78 | + registry_helper.remove()?; |
91 | 79 | Ok(())
|
92 | 80 | }
|
0 commit comments