Skip to content

Commit

Permalink
Copy skel from usr
Browse files Browse the repository at this point in the history
  • Loading branch information
joseivanlopez committed Apr 5, 2021
1 parent e04e485 commit 9ae9f83
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 23 deletions.
8 changes: 5 additions & 3 deletions src/modules/Users.pm
Expand Up @@ -4493,6 +4493,7 @@ sub Write {
my $chown_home = $user{"chown_home"};
$chown_home = 1 if (!defined $chown_home);
my $skel = $useradd_defaults{"skel"};
my $copy_usr_skel = "yes";

if ($user_mod eq "imported" || $user_mod eq "added") {
y2usernote ("User '$username' created");
Expand All @@ -4503,11 +4504,12 @@ sub Write {
next;
}
if (bool ($user{"no_skeleton"})) {
$skel = "";
$skel = "";
$copy_usr_skel = "";
}
if (bool ($create_home) || $user_mod eq "imported")
{
UsersRoutines->CreateHome ($skel, $home, $use_btrfs_subvolume);
UsersRoutines->CreateHome ($skel, $home, $use_btrfs_subvolume, $copy_usr_skel);
}
if ($home ne "/var/lib/nobody" && bool ($chown_home)) {
if (UsersRoutines->ChownHome ($uid, $gid, $home))
Expand Down Expand Up @@ -4555,7 +4557,7 @@ sub Write {
}
# create new home directory
elsif (not %{SCR->Read (".target.stat", $home)}) {
UsersRoutines->CreateHome ($skel, $home);
UsersRoutines->CreateHome ($skel, $home, "", $copy_usr_skel);
}
# do not change root's ownership of home directories
if (bool ($chown_home))
Expand Down
2 changes: 1 addition & 1 deletion src/modules/UsersLDAP.pm
Expand Up @@ -1404,7 +1404,7 @@ sub WriteUsers {
if ($server) {
if ($create_home) {
UsersRoutines->CreateHome (
$useradd_defaults{"skel"}, $home);
$useradd_defaults{"skel"}, $home, "", "yes");
}
if ($home ne "/var/lib/nobody" && $chown_home) {
if (UsersRoutines->ChownHome ($uid, $gid, $home)) {
Expand Down
51 changes: 32 additions & 19 deletions src/modules/UsersRoutines.pm
Expand Up @@ -50,6 +50,9 @@ YaST::YCP::Import ("String");
# path to btrfs
my $btrfs = "/usr/sbin/btrfs";

# path to the usr skel
my $usr_skel = "/usr/etc/skel";

##-------------------------------------------------------------------------
##----------------- helper routines ---------------------------------------

Expand All @@ -60,23 +63,45 @@ sub btrfs_subvolume {
return ( SCR->Execute( ".target.bash", $cmd ) eq 0 );
}

sub copy_skel {
my ( $skel, $home ) = @_;

if ( $skel ne "" && %{ SCR->Read( ".target.stat", $skel ) } ) {
my $cmd = sprintf(
"/usr/bin/cp -r '%s/.' '%s'",
String->Quote($skel),
String->Quote($home)
);
my %cmd_out = %{ SCR->Execute( ".target.bash_output", $cmd ) };
my $stderr = $cmd_out{"stderr"} || "";

if ( $stderr ne "" ) {
y2error( "Error calling $cmd: $stderr" );
return 0;
}

y2usernote("Home skeleton copied: '$cmd'.");
}
}

##-------------------------------------------------------------------------
##----------------- directory manipulation routines -----------------------

##------------------------------------
# Create home directory
# @param skeleton skeleton directory for new home
# @param skel skeleton directory for new home (typically from /etc/default/useradd config)
# @param home name of new home directory
# @param use_btrfs whether the home directory must be a btrfs subvolume
# @param copy_usr_skel whether the usr skel (i.e., /usr/etc/skel) should be copied
# @return success
BEGIN { $TYPEINFO{CreateHome} = ["function",
"boolean",
"string", "string", "string"];
"string", "string", "string", "string"];
}
sub CreateHome {

my $self = shift;
my ( $skel, $home, $use_btrfs ) = @_;
my ( $skel, $home, $use_btrfs, $copy_usr_skel ) = @_;

# Create a path to new home directory, if not exists
my $home_path = substr( $home, 0, rindex( $home, "/" ) );
Expand Down Expand Up @@ -133,24 +158,12 @@ sub CreateHome {
}
}

# Now copy the skeleton
if ( $skel ne "" && %{ SCR->Read( ".target.stat", $skel ) } ) {
my $cmd = sprintf(
"/usr/bin/cp -r '%s/.' '%s'",
String->Quote($skel),
String->Quote($home)
);
my %cmd_out = %{ SCR->Execute( ".target.bash_output", $cmd ) };
my $stderr = $cmd_out{"stderr"} || "";

if ( $stderr ne "" ) {
y2error( "Error calling $cmd: $stderr" );
return 0;
}

y2usernote("Home skeleton copied: '$cmd'.");
if ($copy_usr_skel) {
copy_skel($usr_skel, $home);
}

copy_skel($skel, $home);

y2milestone("The directory $home was successfully created.");

return 1;
Expand Down

0 comments on commit 9ae9f83

Please sign in to comment.