Skip to content

Commit 97ff735

Browse files
authored
Amend password and sshd logic to support Mariner OS (#156)
* Adding OS check to support other distros with different sshd configuration paths * Altering update logic for Mariner -- rename causing linking device issue as file moves and it can't find it * Refactor SSH configuration logic to dynamically select the appropriate configuration path based on filesystem structure and replace distro-specific file operations with universal temp file handling using copy/remove_file. * Refactor update_sshd_config logic: eliminate temp files, simplify write process Replaced tempfile-based logic with direct file operations for updating sshd_config. The function now reads the file, updates or appends the necessary configuration, and writes it back securely with appropriate permissions (0o600). This simplifies the implementation and avoids potential cross-device issues with temporary files. * Removing temp dependency * Amending config line to make it clear the PasswordAuthentication was added by azure-init, and removing the file permissions settings * Adding another added by azure-init comment and making sure file permissions line is deleted.
1 parent a4fa057 commit 97ff735

File tree

3 files changed

+27
-18
lines changed

3 files changed

+27
-18
lines changed

libazureinit/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ fstab = "0.4.0"
2323
toml = "0.8"
2424
regex = "1"
2525
lazy_static = "1.4"
26-
tempfile = "3.3.0"
2726
figment = { version = "0.10", features = ["toml"] }
2827

2928
[dev-dependencies]

libazureinit/src/provision/password.rs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33

44
use std::process::Command;
55

6+
use std::path::PathBuf;
7+
68
use tracing::instrument;
79

810
use crate::{error::Error, User};
@@ -21,10 +23,21 @@ impl PasswordProvisioner {
2123
}
2224
}
2325

26+
// Determines the appropriate SSH configuration file path based on the filesystem.
27+
// If the "/etc/ssh/sshd_config.d" directory exists, it returns the path for a drop-in configuration file.
28+
// Otherwise, it defaults to the main SSH configuration file at "/etc/ssh/sshd_config".
29+
fn get_sshd_config_path() -> &'static str {
30+
if PathBuf::from("/etc/ssh/sshd_config.d").is_dir() {
31+
"/etc/ssh/sshd_config.d/50-azure-init.conf"
32+
} else {
33+
"/etc/ssh/sshd_config"
34+
}
35+
}
36+
2437
#[instrument(skip_all)]
2538
fn passwd(user: &User) -> Result<(), Error> {
2639
// Update the sshd configuration to allow password authentication.
27-
let sshd_config_path = "/etc/ssh/sshd_config.d/50-azure-init.conf";
40+
let sshd_config_path = get_sshd_config_path();
2841
if let Err(error) = update_sshd_config(sshd_config_path) {
2942
tracing::error!(
3043
?error,

libazureinit/src/provision/ssh.rs

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,13 @@ use nix::unistd::{chown, User};
1313
use regex::Regex;
1414
use std::{
1515
fs::{
16-
self, OpenOptions, {File, Permissions},
16+
OpenOptions, {File, Permissions},
1717
},
1818
io::{self, Read, Write},
1919
os::unix::fs::{DirBuilderExt, PermissionsExt},
2020
path::PathBuf,
2121
process::{Command, Output},
2222
};
23-
use tempfile::NamedTempFile;
2423
use tracing::{error, info, instrument};
2524

2625
lazy_static! {
@@ -220,7 +219,7 @@ pub(crate) fn update_sshd_config(
220219
let sshd_config_path = PathBuf::from(sshd_config_path);
221220
if !sshd_config_path.exists() {
222221
let mut file = std::fs::File::create(&sshd_config_path)?;
223-
file.set_permissions(Permissions::from_mode(0o644))?;
222+
file.set_permissions(Permissions::from_mode(0o600))?;
224223
file.write_all(b"PasswordAuthentication yes\n")?;
225224
tracing::info!(
226225
?sshd_config_path,
@@ -237,32 +236,30 @@ pub(crate) fn update_sshd_config(
237236

238237
let re = &PASSWORD_REGEX;
239238
if re.is_match(&file_content) {
240-
let modified_content =
241-
re.replace_all(&file_content, "PasswordAuthentication yes\n");
239+
let modified_content = re.replace_all(
240+
&file_content,
241+
"PasswordAuthentication yes # modified by azure-init\n",
242+
);
242243

243-
let temp_sshd_config = NamedTempFile::new()?;
244-
let temp_sshd_config_path = temp_sshd_config.path();
245-
let mut temp_file = OpenOptions::new()
244+
let mut sshd_config = OpenOptions::new()
246245
.write(true)
247-
.create(true)
248246
.truncate(true)
249-
.open(temp_sshd_config_path)?;
250-
temp_file.write_all(modified_content.as_bytes())?;
251-
temp_file.set_permissions(fs::Permissions::from_mode(0o644))?;
247+
.open(&sshd_config_path)?;
248+
sshd_config.write_all(modified_content.as_bytes())?;
252249

253-
fs::rename(temp_sshd_config_path, &sshd_config_path)?;
254250
tracing::info!(
255251
?sshd_config_path,
256252
"Updated existing sshd setting to allow password authentication"
257-
)
253+
);
258254
} else {
259255
let mut file =
260256
OpenOptions::new().append(true).open(&sshd_config_path)?;
261-
file.write_all(b"PasswordAuthentication yes\n")?;
257+
file.write_all(b"PasswordAuthentication yes # added by azure-init\n")?;
258+
262259
tracing::info!(
263260
?sshd_config_path,
264261
"Added new sshd setting to allow password authentication"
265-
)
262+
);
266263
}
267264

268265
Ok(())

0 commit comments

Comments
 (0)