changeset 616:0172740f5c6e

Include user information in windows event log messages
author Andre Heinecke <andre.heinecke@intevation.de>
date Thu, 19 Jun 2014 12:06:47 +0200
parents 2a4f7364ab81
children 63a128400bf3
files common/logging.c
diffstat 1 files changed, 28 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- 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))

http://wald.intevation.org/projects/trustbridge/