file-logger: Set owner/group of log file
authorTobias Brunner <tobias@strongswan.org>
Tue, 14 Apr 2020 08:31:49 +0000 (10:31 +0200)
committerTobias Brunner <tobias@strongswan.org>
Thu, 7 May 2020 07:30:57 +0000 (09:30 +0200)
The file is usually opened/created by root, however, if user/group IDs
are configured and the configuration is reloaded, the file will be reopened
as configured user.  Like with UNIX sockets we only attempt to change
the user if we have CAP_CHOWN allowing a start as regular user.

We don't have chown() on Windows, so check for it.

configure.ac
src/libcharon/bus/listeners/file_logger.c

index 7788121..867b204 100644 (file)
@@ -661,7 +661,7 @@ AC_CHECK_FUNC(
        ]
 )
 
-AC_CHECK_FUNCS(prctl mallinfo getpass closefrom getpwnam_r getgrnam_r getpwuid_r)
+AC_CHECK_FUNCS(prctl mallinfo getpass closefrom getpwnam_r getgrnam_r getpwuid_r chown)
 AC_CHECK_FUNCS(fmemopen funopen mmap memrchr setlinebuf strptime dirfd sigwaitinfo explicit_bzero)
 
 AC_CHECK_FUNC([syslog], [
index d1f1802..704c4a5 100644 (file)
@@ -243,6 +243,25 @@ METHOD(file_logger_t, open_, void,
                                 this->filename, strerror(errno));
                        return;
                }
+#ifdef HAVE_CHOWN
+               if (lib->caps->check(lib->caps, CAP_CHOWN))
+               {
+                       if (chown(this->filename, lib->caps->get_uid(lib->caps),
+                                         lib->caps->get_gid(lib->caps)) != 0)
+                       {
+                               DBG1(DBG_NET, "changing owner/group for '%s' failed: %s",
+                                        this->filename, strerror(errno));
+                       }
+               }
+               else
+               {
+                       if (chown(this->filename, -1, lib->caps->get_gid(lib->caps)) != 0)
+                       {
+                               DBG1(DBG_NET, "changing group for '%s' failed: %s",
+                                        this->filename, strerror(errno));
+                       }
+               }
+#endif /* HAVE_CHOWN */
 #ifdef HAVE_SETLINEBUF
                if (flush_line)
                {