]> git.ipfire.org Git - people/stevee/guardian.git/commitdiff
Allow to configure the owner of the UNIX socket.
authorStefan Schantl <stefan.schantl@ipfire.org>
Thu, 18 Feb 2016 14:04:22 +0000 (15:04 +0100)
committerStefan Schantl <stefan.schantl@ipfire.org>
Thu, 18 Feb 2016 14:04:22 +0000 (15:04 +0100)
Add option to configure an alternative owner of the created
UNIX socket, by using "SocketOwner = user:group" in the config
file.

Signed-off-by: Stefan Schantl <stefan.schantl@ipfire.org>
guardian
modules/Config.pm
modules/Socket.pm

index 9b571145c57b83812db4144c81bcf26ad320262f..148f50e38e9f2b166336c16642087965e0c02be6 100644 (file)
--- a/guardian
+++ b/guardian
@@ -279,7 +279,7 @@ sub Worker ($) {
 #
 sub Socket () {
        # Create the Server socket by calling the responsible function.
-       my $server = &Guardian::Socket::Server();
+       my $server = &Guardian::Socket::Server($mainsettings{SocketOwner});
 
        # Log successfull creation of socket.
        $logger->Log("debug", "Listening to Socket...");
index 8da587cde15eb3357add35718f18d1b5ccdce708..75f1ab02496be95b735bd46b8f0d2b695fccd66c 100644 (file)
@@ -162,6 +162,17 @@ sub CheckConfig (\%) {
                return "Invalid LogLevel: $config{LogLevel}";
        }
 
+       # Check if an optional configured SocketOwner is valid.
+       if (exists($config{SocketOwner})) {
+               my ($user, $group) = split(/:/, $config{SocketOwner});
+
+               # Get the ID for the given user name.
+               my $uid = getpwnam($user) or return "The user $user does not exist.";
+
+               # Get the ID for given group name.
+               my $gid = getgrnam($group) or return "The group $group does not exist.";
+       } 
+
        # The config looks good, so return nothing (no error message).
        return undef
 }
index 9c1985c924bc253a5a2af06e918a9a3f352258eb..637a17e1280840008b7a69687c8e4377e84bfff6 100644 (file)
@@ -29,7 +29,9 @@ my %supported_commands = (
 ## mechanism for guardian.  The server function creates an UNIX
 ## socket.
 #
-sub Server () {
+sub Server ($) {
+       my $socket_owner = shift;
+
        # If the path for the socketfile does not exist, try to
        # create it.
        unless (-d "$socketpath") {
@@ -49,6 +51,22 @@ sub Server () {
                Type => SOCK_STREAM,
        ) or die "Could not create socket: $!";
 
+       
+       # Translate the given user/group name into ID values.
+       if (defined ($socket_owner)) {
+               # Splitt provided user/group into single arguments.
+               my ($username, $groupname) = split(/:/, $socket_owner);
+
+               # Get the ID for the given user name.
+               my $uid = getpwnam($username) or die "Could not get an UID for $username: $!";
+
+               # Get the ID for given group name.
+               my $gid = getgrnam($groupname) or die "Could not get a GID for $groupname: $!";
+
+               # Set new ownership for the socket file.
+               chown($uid, $gid, "$socketfile") or die "Could not change ownership to ($uid:$gid) for $socketfile: $!";
+       }
+
        # Return the server object.
        return $server;
 }