[PATCH] Include user information in windows event log messages

Wald Commits scm-commit at wald.intevation.org
Thu Jun 19 12:18:49 CEST 2014


# HG changeset patch
# User Andre Heinecke <andre.heinecke at intevation.de>
# Date 1403172407 -7200
# Node ID 0172740f5c6e4e5ae34e2d6918f33fcc1f67b479
# Parent  2a4f7364ab8150b88e5e4f43604418e4f4f72f48
Include user information in windows event log messages

diff -r 2a4f7364ab81 -r 0172740f5c6e common/logging.c
--- a/common/logging.c	Thu Jun 19 11:53:07 2014 +0200
+++ b/common/logging.c	Thu Jun 19 12:06:47 2014 +0200
@@ -25,12 +25,15 @@
 static void
 win_log(const char *format, va_list ap, bool error)
 {
-  HANDLE log_src = NULL;
+  HANDLE log_src = NULL,
+         process_token = NULL;
   wchar_t *wmsg = NULL;
   BOOL failure = TRUE;
   WORD type = 0,
        category = 0;
   char buffer[MAX_LOG+1];
+  PTOKEN_USER user_struct = NULL;
+  PSID user_sid = NULL;
 
   vsnprintf (buffer, MAX_LOG, format, ap);
   buffer[MAX_LOG] = '\0';
@@ -59,11 +62,29 @@
       goto done;
     }
 
+  /* Get the current user sid for logging */
+
+  OpenProcessToken (GetCurrentProcess(), TOKEN_READ, &process_token);
+  if (process_token)
+    {
+      DWORD size = 0;
+
+      // check how much space is needed
+      GetTokenInformation (process_token, TokenUser, NULL, 0, &size);
+      if (ERROR_INSUFFICIENT_BUFFER == GetLastError())
+        {
+          user_struct = xmalloc (size);
+          GetTokenInformation (process_token, TokenUser, user_struct, size, &size);
+          user_sid = user_struct->User.Sid;
+        }
+    }
+
+
   failure = ReportEventW (log_src,
                           type,
                           category,
                           0,
-                          NULL,
+                          user_sid,
                           1,
                           0,
                           (const WCHAR **) &wmsg,
@@ -74,6 +95,11 @@
     }
 
 done:
+  if (process_token)
+    {
+      CloseHandle(process_token);
+    }
+  xfree (user_struct);
   xfree (wmsg);
 
   if (!DeregisterEventSource (log_src))


More information about the Trustbridge-commits mailing list