mirror of
https://github.com/vincentmli/bpfire.git
synced 2026-04-27 19:23:24 +02:00
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 <stefan.schantl@ipfire.org>
This commit is contained in:
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user