Extract SSH server defaults by running sshd -T with an empty configuration file. # Errors This function will return an error if it fails to extract the defaults from sshd.
()
| 153 | /// |
| 154 | /// This function will return an error if it fails to extract the defaults from sshd. |
| 155 | pub fn extract_sshd_defaults() -> Result<Map<String, Value>, SshdConfigError> { |
| 156 | let temp_file = tempfile::Builder::new() |
| 157 | .prefix("sshd_config_empty_") |
| 158 | .suffix(".tmp") |
| 159 | .tempfile()?; |
| 160 | |
| 161 | // on Windows, sshd cannot read from the file if it is still open |
| 162 | let temp_path = temp_file.path().to_path_buf(); |
| 163 | // do not automatically delete the file when it goes out of scope |
| 164 | let (file, path) = temp_file.keep()?; |
| 165 | // close the file handle to allow sshd to read it |
| 166 | drop(file); |
| 167 | |
| 168 | debug!("{}", t!("util.tempFileCreated", path = temp_path.display())); |
| 169 | let args = Some( |
| 170 | SshdCommandArgs { |
| 171 | filepath: Some(temp_path), |
| 172 | additional_args: None, |
| 173 | } |
| 174 | ); |
| 175 | |
| 176 | // Clean up the temporary file regardless of success or failure |
| 177 | let output = invoke_sshd_config_validation(args); |
| 178 | if let Err(e) = std::fs::remove_file(&path) { |
| 179 | debug!("{}", t!("util.cleanupFailed", path = path.display(), error = e)); |
| 180 | } |
| 181 | let result = output?; |
| 182 | let sshd_config: Map<String, Value> = parse_text_to_map(&result)?; |
| 183 | Ok(sshd_config) |
| 184 | } |
| 185 | |
| 186 | /// Extract _metadata field from the input string, if it can be parsed as JSON. |
| 187 | /// |
no test coverage detected