From 1fedede6a0982500847ef5d8747b5d3483991a05 Mon Sep 17 00:00:00 2001 From: Stefan Schantl Date: Tue, 29 Jan 2019 08:50:16 +0100 Subject: [PATCH] ids-functions.pl: Add set_ownership() function. This function is used to change the ownership of a given file or directory to the user "nobody" and the group "nobody", which is used by the WUI. Signed-off-by: Stefan Schantl --- config/cfgroot/ids-functions.pl | 44 +++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/config/cfgroot/ids-functions.pl b/config/cfgroot/ids-functions.pl index 2a358b1cc1..d8044b4e86 100644 --- a/config/cfgroot/ids-functions.pl +++ b/config/cfgroot/ids-functions.pl @@ -794,4 +794,48 @@ sub generate_ignore_file() { close(FILE); } +# +## Function to set correct ownership for single files and directories. +# + +sub set_ownership($) { + my ($target) = @_; + + # User and group of the WUI. + my $uname = "nobody"; + my $grname = "nobody"; + + # The chown function implemented in perl requies the user and group as nummeric id's. + my $uid = getpwnam($uname); + my $gid = getgrnam($grname); + + # Check if the given target exists. + unless ($target) { + # Stop the script and print error message. + die "The $target does not exist. Cannot change the ownership!\n"; + } + + # Check weather the target is a file or directory. + if (-f $target) { + # Change ownership ot the single file. + chown($uid, $gid, "$target"); + } elsif (-d $target) { + # Do a directory listing. + opendir(DIR, $target) or die $!; + # Loop through the direcory. + while (my $file = readdir(DIR)) { + + # We only want files. + next unless (-f "$target/$file"); + + # Set correct ownership for the files. + chown($uid, $gid, "$target/$file"); + } + + closedir(DIR); + + # Change ownership of the directory. + chown($uid, $gid, "$target"); + } +} 1; -- 2.39.2