Blob Blame History Raw
Index: b/bashhist.c
===================================================================
--- a/bashhist.c
+++ b/bashhist.c
@@ -266,6 +266,44 @@ bash_history_inhibit_expansion (string, i)
 }
 #endif
 
+#if defined (SYSLOG_HISTORY)
+#define SESSION_ID_MAXLEN 100
+
+static char *session_id = NULL;
+
+static void
+bash_initialize_session_id ()
+{
+  const char *env_var = get_string_value ("LOG_SESSION_ID");
+  if (!env_var) {
+    session_id = "";
+    return;
+  }
+  const int env_var_len = strlen(env_var);
+  if (env_var_len == 0) {
+    session_id = "";
+    return;
+  }
+  const int size = env_var_len < SESSION_ID_MAXLEN ? (env_var_len + 1) : SESSION_ID_MAXLEN;
+  char *trunc = (char *)malloc (size);
+  strncpy (trunc, env_var, size - 1);
+  trunc[size - 1] = '\0';
+  session_id = trunc;
+}
+
+static const char *
+bash_get_session_id () {
+  if (!session_id) bash_initialize_session_id ();
+  return session_id;
+}
+
+static void
+bash_cleanup_session_id () {
+  if (session_id && *session_id) free(session_id);
+  session_id = NULL;
+}
+#endif
+
 void
 bash_initialize_history ()
 {
@@ -275,6 +313,9 @@ bash_initialize_history ()
   history_inhibit_expansion_function = bash_history_inhibit_expansion;
   sv_histchars ("histchars");
 #endif
+#if defined (SYSLOG_HISTORY)
+  bash_initialize_session_id ();
+#endif
 }
 
 void
@@ -509,6 +550,9 @@ maybe_save_shell_history ()
 	  sv_histsize ("HISTFILESIZE");
 	}
     }
+#if defined (SYSLOG_HISTORY)
+  bash_cleanup_session_id ();
+#endif
   return (result);
 }
 
@@ -879,7 +879,8 @@ bash_syslog_history (line)
       first = 0;
     }
 
-  hdrlen = snprintf (loghdr, sizeof(loghdr), "HISTORY: PID=%d UID=%d", getpid(), current_user.uid);
+  const char *user_name = current_user.user_name ?: "";
+  hdrlen = snprintf (loghdr, sizeof(loghdr), "HISTORY: PID=%d UID=%d USER=%s LOG_SESSION_ID=%s", getpid(), current_user.uid, user_name, bash_get_session_id());
   msglen = strlen (line);
 
   if ((msglen + hdrlen + 1) < SYSLOG_MAXLEN)