Invoke sshd -T. # Errors This function will return an error if sshd -T fails to validate `sshd_config`.
(args: Option<SshdCommandArgs>)
| 113 | /// |
| 114 | /// This function will return an error if sshd -T fails to validate `sshd_config`. |
| 115 | pub fn invoke_sshd_config_validation(args: Option<SshdCommandArgs>) -> Result<String, SshdConfigError> { |
| 116 | let mut command = Command::new("sshd"); |
| 117 | command.arg("-T"); |
| 118 | |
| 119 | if let Some(args) = args { |
| 120 | if let Some(filepath) = args.filepath { |
| 121 | if !filepath.exists() { |
| 122 | return Err(SshdConfigError::FileNotFound(filepath.display().to_string())); |
| 123 | } |
| 124 | command.arg("-f").arg(&filepath); |
| 125 | } |
| 126 | if let Some(additional_args) = args.additional_args { |
| 127 | command.args(additional_args); |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | let output = command.output() |
| 132 | .map_err(|e| SshdConfigError::CommandError(e.to_string()))?; |
| 133 | |
| 134 | if output.status.success() { |
| 135 | let stdout = String::from_utf8(output.stdout) |
| 136 | .map_err(|e| SshdConfigError::CommandError(e.to_string()))?; |
| 137 | Ok(stdout) |
| 138 | } else { |
| 139 | let stderr = String::from_utf8(output.stderr) |
| 140 | .map_err(|e| SshdConfigError::CommandError(e.to_string()))?; |
| 141 | if stderr.contains("sshd: no hostkeys available") || stderr.contains("Permission denied") { |
| 142 | return Err(SshdConfigError::CommandError( |
| 143 | t!("util.sshdElevation").to_string() |
| 144 | )); |
| 145 | } |
| 146 | Err(SshdConfigError::CommandError(stderr)) |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | /// Extract SSH server defaults by running sshd -T with an empty configuration file. |
| 151 | /// |
no test coverage detected