Retrieve sshd settings. # Arguments `cmd_info` - `CommandInfo` struct containing optional filters, metadata, and includeDefaults flag. # Errors This function will return an error if it cannot retrieve the sshd settings.
(cmd_info: &CommandInfo, is_get: bool)
| 113 | /// |
| 114 | /// This function will return an error if it cannot retrieve the sshd settings. |
| 115 | pub fn get_sshd_settings(cmd_info: &CommandInfo, is_get: bool) -> Result<Map<String, Value>, SshdConfigError> { |
| 116 | let sshd_config_text = invoke_sshd_config_validation(cmd_info.sshd_args.clone())?; |
| 117 | let mut result = parse_text_to_map(&sshd_config_text)?; |
| 118 | let mut inherited_defaults: Vec<String> = Vec::new(); |
| 119 | |
| 120 | // parse settings from sshd_config file |
| 121 | let sshd_config_file = read_sshd_config(cmd_info.metadata.filepath.clone())?; |
| 122 | let explicit_settings = parse_text_to_map(&sshd_config_file)?; |
| 123 | |
| 124 | // handle special cases for keywords |
| 125 | if explicit_settings.contains_key("include") { |
| 126 | warn!("{}", t!("get.includeWarning").to_string()); |
| 127 | } |
| 128 | |
| 129 | if explicit_settings.contains_key("match") { |
| 130 | let Some(match_value) = explicit_settings.get("match") else { |
| 131 | return Err(SshdConfigError::InvalidInput(t!("get.matchParsingError").to_string())); |
| 132 | }; |
| 133 | result.insert("match".to_string(), match_value.clone()); |
| 134 | } |
| 135 | |
| 136 | if cmd_info.include_defaults { |
| 137 | // get default from SSHD -T with empty config |
| 138 | let mut defaults = extract_sshd_defaults()?; |
| 139 | for key in explicit_settings.keys() { |
| 140 | if defaults.contains_key(key) { |
| 141 | defaults.remove(key); |
| 142 | } |
| 143 | } |
| 144 | // Update inherited_defaults with any keys that are not explicitly set |
| 145 | // check result for any keys that are in defaults |
| 146 | for (key, value) in &result { |
| 147 | if let Some(default) = defaults.get(key) && default == value { |
| 148 | inherited_defaults.push(key.clone()); |
| 149 | } |
| 150 | } |
| 151 | } else { |
| 152 | // only need to return keys that are explicitly set in the config file |
| 153 | result.retain(|key, _| explicit_settings.contains_key(key)); |
| 154 | } |
| 155 | |
| 156 | if !cmd_info.input.is_empty() { |
| 157 | // Filter result based on the keys provided in the input JSON. |
| 158 | // If a provided key is not found in the result, its value is null. |
| 159 | result.retain(|key, _| cmd_info.input.contains_key(key)); |
| 160 | inherited_defaults.retain(|key| cmd_info.input.contains_key(key)); |
| 161 | for key in cmd_info.input.keys() { |
| 162 | result.entry(key.clone()).or_insert(Value::Null); |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | if cmd_info.metadata.filepath.is_some() { |
| 167 | result.insert(CanonicalProperty::Metadata.to_string(), serde_json::to_value(cmd_info.metadata.clone())?); |
| 168 | } |
| 169 | if cmd_info.include_defaults && is_get { |
| 170 | result.insert(CanonicalProperty::InheritedDefaults.to_string(), serde_json::to_value(inherited_defaults)?); |
| 171 | } |
| 172 | Ok(result) |
no test coverage detected