[Commits] Rev 3864: MDEV-4472 Audit plugin. in file:///home/hf/wmar/mdev-4472/

holyfoot at askmonty.org holyfoot at askmonty.org
Thu Aug 29 13:37:51 EEST 2013


At file:///home/hf/wmar/mdev-4472/

------------------------------------------------------------
revno: 3864
revision-id: holyfoot at askmonty.org-20130829092759-20pmq0wwxnmcdjta
parent: holyfoot at askmonty.org-20130826091144-eri6jpv3alodjaj8
committer: Alexey Botchkov <holyfoot at askmonty.org>
branch nick: mdev-4472
timestamp: Thu 2013-08-29 14:27:59 +0500
message:
  MDEV-4472 Audit plugin.
          additions 4
-------------- next part --------------
=== modified file 'plugin/server_audit/server_audit.c'
--- a/plugin/server_audit/server_audit.c	2013-08-26 09:11:44 +0000
+++ b/plugin/server_audit/server_audit.c	2013-08-29 09:27:59 +0000
@@ -348,6 +348,8 @@ struct connection_info
   int user_length;
   char host[64];
   int host_length;
+  char ip[64];
+  int ip_length;
 };
 
 
@@ -381,7 +383,7 @@ static void get_str_n(char *dest, int *d
 
 static int get_user_host(const char *uh_line, unsigned int uh_len,
                          char *buffer, size_t buf_len,
-                         size_t *user_len, size_t *host_len)
+                         size_t *user_len, size_t *host_len, size_t *ip_len)
 {
   const char *buf_end= buffer + buf_len - 1;
   const char *buf_start;
@@ -421,6 +423,18 @@ static int get_user_host(const char *uh_
   }
   *host_len= buffer - buf_start;
   *(buffer++)= 0;
+
+  while (uh_line < uh_end && *uh_line != '[')
+    ++uh_line;
+
+  buf_start= buffer;
+  if (*uh_line == '[')
+  {
+    ++uh_line;
+    while (uh_line < uh_end && *uh_line != ']')
+      *(buffer++)= *(uh_line++);
+  }
+  *ip_len= buffer - buf_start;
   return 0;
 }
 
@@ -489,6 +503,8 @@ static struct connection_info *
             event->user, event->user_length);
   get_str_n(cn->host, &cn->host_length, sizeof(cn->host),
             event->host, event->host_length);
+  get_str_n(cn->ip, &cn->ip_length, sizeof(cn->ip),
+            event->ip, event->ip_length);
 
   if (my_hash_insert(&connection_hash, (const uchar *) cn))
     return 0;
@@ -501,11 +517,12 @@ static struct connection_info *
   add_connection_initdb(const struct mysql_event_general *event)
 {
   struct connection_info *cn;
-  size_t user_len, host_len;
+  size_t user_len, host_len, ip_len;
   char uh_buffer[512];
 
   if (get_user_host(event->general_user, event->general_user_length,
-                    uh_buffer, sizeof(uh_buffer), &user_len, &host_len) ||
+                    uh_buffer, sizeof(uh_buffer),
+                    &user_len, &host_len, &ip_len) ||
       (cn= malloc(sizeof(struct connection_info))) == NULL)
     return 0;
 
@@ -517,6 +534,8 @@ static struct connection_info *
             uh_buffer, user_len);
   get_str_n(cn->host, &cn->host_length, sizeof(cn->host),
             uh_buffer+user_len+1, host_len);
+  get_str_n(cn->ip, &cn->ip_length, sizeof(cn->ip),
+            uh_buffer+user_len+1+host_len+1, ip_len);
 
   if (my_hash_insert(&connection_hash, (const uchar *) cn))
     return 0;
@@ -530,8 +549,10 @@ static void change_connection(const stru
   struct connection_info *cn= find_connection(event->thread_id);
   if (!cn)
     return;
-  get_str_n(cn->user, &cn->user_length, sizeof(cn->db),
+  get_str_n(cn->user, &cn->user_length, sizeof(cn->user),
             event->user, event->user_length);
+  get_str_n(cn->ip, &cn->ip_length, sizeof(cn->ip),
+            event->ip, event->ip_length);
 }
 
 static int write_log(const char *message, int len)
@@ -558,18 +579,25 @@ static size_t log_header(char *message, 
                       time_t *ts,
                       const char *serverhost, unsigned int serverhost_len,
                       const char *username, unsigned int username_len,
-                      const char *host, unsigned int hostlen,
+                      const char *host, unsigned int host_len,
+                      const char *userip, unsigned int userip_len,
                       unsigned int connection_id, long long query_id,
                       const char *operation)
 {
   struct tm tm_time;
 
+  if (host_len == 0 && userip_len != 0)
+  {
+    host_len= userip_len;
+    host= userip;
+  }
+
   if (output_type == OUTPUT_SYSLOG)
     return my_snprintf(message, message_len,
         "%.*s,%.*s,%.*s,%d,%lld,%s",
         serverhost_len, serverhost,
         username_len, username,
-        hostlen, host,
+        host_len, host,
         connection_id, query_id, operation);
 
   (void) localtime_r(ts, &tm_time);
@@ -579,7 +607,7 @@ static size_t log_header(char *message, 
       tm_time.tm_hour, tm_time.tm_min, tm_time.tm_sec,
       serverhost_len, serverhost,
       username_len, username,
-      hostlen, host,
+      host_len, host,
       connection_id, query_id, operation);
 }
 
@@ -596,6 +624,7 @@ static int log_connection(const struct m
                     servhost, servhost_len,
                     event->user, event->user_length,
                     event->host, event->host_length,
+                    event->ip, event->ip_length,
                     event->thread_id, 0, type);
   csize+= my_snprintf(message+csize, sizeof(message) - 1 - csize,
     ",%.*s,,%d", event->database_length, event->database, event->status);
@@ -606,6 +635,7 @@ static int log_connection(const struct m
 
 static int log_disconnect_mysql(const struct mysql_event_connection *event,
                                 const char *user, unsigned int user_length,
+                                const char *ip, unsigned int ip_length,
                                 const char *host, unsigned int host_length)
 {
   time_t ctime;
@@ -615,7 +645,7 @@ static int log_disconnect_mysql(const st
   (void) time(&ctime);
   csize= log_header(message, sizeof(message)-1, &ctime,
                     servhost, servhost_len,
-                    user, user_length, host, host_length,
+                    user, user_length, host, host_length, ip, ip_length,
                     event->thread_id, 0, "DISCONNECT");
   csize+= my_snprintf(message+csize, sizeof(message) - 1 - csize,
     ",%.*s,,%d", event->database_length, event->database, event->status);
@@ -653,6 +683,8 @@ static size_t escape_string(const char *
 }
 
 
+#define SAFE_STRLEN(s) (s ? strlen(s) : 0)
+
 static int do_log_user(const char *name)
 {
   size_t len;
@@ -674,7 +706,7 @@ static int do_log_user(const char *name)
 static int log_statement(const struct mysql_event_general *event,
                          const char *type)
 {
-  size_t csize, user_len, host_len, esc_q_len;
+  size_t csize, user_len, host_len, ip_len, esc_q_len;
   char message[1024];
   char uh_buffer[768];
   time_t ev_time= (time_t) event->general_time;
@@ -706,12 +738,13 @@ static int log_statement(const struct my
   }
 
   if (get_user_host(event->general_user, event->general_user_length,
-                    uh_buffer, sizeof(uh_buffer), &user_len, &host_len))
+                    uh_buffer, sizeof(uh_buffer),
+                    &user_len, &host_len, &ip_len))
   {
     csize= log_header(message, sizeof(message)-1, &ev_time,
                       servhost, servhost_len,
                       event->general_user, event->general_user_length,
-                      "", 0, event->general_thread_id, query_id, type);
+                      "", 0, "", 0, event->general_thread_id, query_id, type);
   }
   else
   {
@@ -721,6 +754,7 @@ static int log_statement(const struct my
     csize= log_header(message, sizeof(message)-1, &ev_time,
                       servhost, servhost_len,
                       uh_buffer, user_len, uh_buffer+user_len+1, host_len,
+                      uh_buffer+user_len+1+host_len+1, ip_len,
                       event->general_thread_id, query_id, type);
   }
 
@@ -753,8 +787,9 @@ static int log_table(const struct mysql_
   (void) time(&ctime);
   csize= log_header(message, sizeof(message)-1, &ctime,
                     servhost, servhost_len,
-                    event->user, strlen(event->user),
-                    event->host, strlen(event->host),
+                    event->user, SAFE_STRLEN(event->user),
+                    event->host, SAFE_STRLEN(event->host),
+                    event->ip, SAFE_STRLEN(event->ip),
                     event->thread_id, query_id, type);
   csize+= my_snprintf(message+csize, sizeof(message) - 1 - csize,
             ",%.*s,%.*s,",event->database_length, event->database,
@@ -781,8 +816,9 @@ static int log_rename(const struct mysql
   (void) time(&ctime);
   csize= log_header(message, sizeof(message)-1, &ctime,
                     servhost, servhost_len,
-                    event->user, strlen(event->user),
-                    event->host, strlen(event->host),
+                    event->user, SAFE_STRLEN(event->user),
+                    event->host, SAFE_STRLEN(event->host),
+                    event->ip, SAFE_STRLEN(event->ip),
                     event->thread_id, query_id, "RENAME");
   csize+= my_snprintf(message+csize, sizeof(message) - 1 - csize,
             ",%.*s,%.*s|%.*s.%.*s,",event->database_length, event->database,
@@ -887,11 +923,11 @@ static void auditing(MYSQL_THD thd __att
           if (cn)
           {
             log_disconnect_mysql(event, cn->user, cn->user_length,
-                                 cn->host, cn->host_length);
+              cn->ip, cn->ip_length, cn->host, cn->host_length);
             my_hash_delete(&connection_hash, (uchar *) cn);
           }
           else
-            log_disconnect_mysql(event, "", 0, "", 0);
+            log_disconnect_mysql(event, "", 0, "", 0, "", 0);
         }
         else
           log_connection(event, "DISCONNECT");



More information about the commits mailing list