]> git.ipfire.org Git - ipfire-2.x.git/commitdiff
ids-functions.pl: Add set_ownership() function.
authorStefan Schantl <stefan.schantl@ipfire.org>
Tue, 29 Jan 2019 07:50:16 +0000 (08:50 +0100)
committerStefan Schantl <stefan.schantl@ipfire.org>
Tue, 29 Jan 2019 07:50:16 +0000 (08:50 +0100)
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>
config/cfgroot/ids-functions.pl

index 2a358b1cc114faede8a879d14113a6c5807ac837..d8044b4e86bb3d6d5975c52e908e9a88ebb79224 100644 (file)
@@ -794,4 +794,48 @@ sub generate_ignore_file() {
        close(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;
 1;