Add logic to create $HOME/.ssh if not exists, but only as user root.

This commit is contained in:
Patrick Van der Veken 2025-07-25 21:15:13 +02:00
parent 64bb1b31d7
commit 4d7a988618

View File

@ -42,7 +42,7 @@ use Pod::Usage;
# ------------------------- CONFIGURATION starts here ------------------------- # ------------------------- CONFIGURATION starts here -------------------------
# define the version (YYYY-MM-DD) # define the version (YYYY-MM-DD)
my $script_version = "2025-04-27"; my $script_version = "2025-07-25";
# name of global configuration file (no path, must be located in the script directory) # name of global configuration file (no path, must be located in the script directory)
my $global_config_file = "update_ssh.conf"; my $global_config_file = "update_ssh.conf";
# name of localized configuration file (no path, must be located in the script directory) # name of localized configuration file (no path, must be located in the script directory)
@ -720,8 +720,20 @@ SET_KEY: foreach my $account (sort (@accounts)) {
if ($access{$account}) { if ($access{$account}) {
unless ($preview) { unless ($preview) {
# do not create root or intermediate paths in $access_file; # create $HOME/.ssh if needed but only when we are root
# e.g. if $HOME/.ssh/authorized_keys is the public key path, then $HOME/.ssh must already exist if ($key_location eq 'use_sshd' and defined ($authorizedkeys_option)) {
if ($> == 0) {
if (! -d "$home_dir/.ssh") {
mkdir ("$home_dir/.ssh", 0700)
or do_log "ERROR: failed to create the $home_dir/.ssh directory [$!/$hostname]"
and next SET_KEY;
chown ($uid, $gid, "$home_dir/.ssh")
or do_log "ERROR: failed to set onwerships on the $home_dir/.ssh directory [$!/$hostname]"
and next SET_KEY;
do_log ("DEBUG: created directory $home_dir/.ssh for $account [$hostname]");
}
}
}
open (KEYFILE, "+>", $access_file) open (KEYFILE, "+>", $access_file)
or do_log ("ERROR: cannot open file for writing at $access_file [$!/$hostname]") or do_log ("ERROR: cannot open file for writing at $access_file [$!/$hostname]")
and next SET_KEY; and next SET_KEY;