Blame fb-Add-SSH-session-ID-to-bash-syslog-logging.patch

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