MCPcopy Create free account
hub / github.com/PowerShell/DSC / invoke_sshd_config_validation

Function invoke_sshd_config_validation

resources/sshdconfig/src/util.rs:115–148  ·  view source on GitHub ↗

Invoke sshd -T. # Errors This function will return an error if sshd -T fails to validate `sshd_config`.

(args: Option<SshdCommandArgs>)

Source from the content-addressed store, hash-verified

113///
114/// This function will return an error if sshd -T fails to validate `sshd_config`.
115pub 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///

Callers 3

extract_sshd_defaultsFunction · 0.85
get_sshd_settingsFunction · 0.85

Calls 1

newFunction · 0.50

Tested by

no test coverage detected