plugin_audit.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565
  1. /* Copyright (c) 2007, 2015, Oracle and/or its affiliates. All rights reserved.
  2. This program is free software; you can redistribute it and/or modify
  3. it under the terms of the GNU General Public License, version 2.0,
  4. as published by the Free Software Foundation.
  5. This program is also distributed with certain software (including
  6. but not limited to OpenSSL) that is licensed under separate terms,
  7. as designated in a particular file or component or in included license
  8. documentation. The authors of MySQL hereby grant you an additional
  9. permission to link the program and your derivative works with the
  10. separately licensed software that they have included with MySQL.
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. GNU General Public License, version 2.0, for more details.
  15. You should have received a copy of the GNU General Public License
  16. along with this program; if not, write to the Free Software
  17. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
  18. #ifndef _my_audit_h
  19. #define _my_audit_h
  20. #include "plugin.h"
  21. #include "mysql/mysql_lex_string.h"
  22. #ifndef MYSQL_ABI_CHECK
  23. #include "m_string.h"
  24. #endif
  25. #include "my_command.h"
  26. #include "my_sqlcommand.h"
  27. #define MYSQL_AUDIT_INTERFACE_VERSION 0x0401
  28. /**
  29. @enum mysql_event_class_t
  30. Audit event classes.
  31. */
  32. typedef enum
  33. {
  34. MYSQL_AUDIT_GENERAL_CLASS = 0,
  35. MYSQL_AUDIT_CONNECTION_CLASS = 1,
  36. MYSQL_AUDIT_PARSE_CLASS = 2,
  37. MYSQL_AUDIT_AUTHORIZATION_CLASS = 3,
  38. MYSQL_AUDIT_TABLE_ACCESS_CLASS = 4,
  39. MYSQL_AUDIT_GLOBAL_VARIABLE_CLASS = 5,
  40. MYSQL_AUDIT_SERVER_STARTUP_CLASS = 6,
  41. MYSQL_AUDIT_SERVER_SHUTDOWN_CLASS = 7,
  42. MYSQL_AUDIT_COMMAND_CLASS = 8,
  43. MYSQL_AUDIT_QUERY_CLASS = 9,
  44. MYSQL_AUDIT_STORED_PROGRAM_CLASS = 10,
  45. /* This item must be last in the list. */
  46. MYSQL_AUDIT_CLASS_MASK_SIZE
  47. } mysql_event_class_t;
  48. /**
  49. @struct st_mysql_audit
  50. The descriptor structure that is referred from st_mysql_plugin.
  51. */
  52. struct st_mysql_audit
  53. {
  54. /**
  55. Interface version.
  56. */
  57. int interface_version;
  58. /**
  59. Event occurs when the event class consumer is to be
  60. disassociated from the specified THD.This would typically occur
  61. before some operation which may require sleeping - such as when
  62. waiting for the next query from the client.
  63. */
  64. void (*release_thd)(MYSQL_THD);
  65. /**
  66. Invoked whenever an event occurs which is of any
  67. class for which the plugin has interest.The second argument
  68. indicates the specific event class and the third argument is data
  69. as required for that class.
  70. */
  71. int (*event_notify)(MYSQL_THD, mysql_event_class_t, const void *);
  72. /**
  73. An array of bits used to indicate what event classes
  74. that this plugin wants to receive.
  75. */
  76. unsigned long class_mask[MYSQL_AUDIT_CLASS_MASK_SIZE];
  77. };
  78. /**
  79. @typedef enum_sql_command_t
  80. SQL command type definition.
  81. */
  82. typedef enum enum_sql_command enum_sql_command_t;
  83. /**
  84. @enum mysql_event_general_subclass_t
  85. Events for the MYSQL_AUDIT_GENERAL_CLASS event class.
  86. */
  87. typedef enum
  88. {
  89. /** occurs before emitting to the general query log. */
  90. MYSQL_AUDIT_GENERAL_LOG = 1 << 0,
  91. /** occurs before transmitting errors to the user. */
  92. MYSQL_AUDIT_GENERAL_ERROR = 1 << 1,
  93. /** occurs after transmitting a resultset to the user. */
  94. MYSQL_AUDIT_GENERAL_RESULT = 1 << 2,
  95. /** occurs after transmitting a resultset or errors */
  96. MYSQL_AUDIT_GENERAL_STATUS = 1 << 3
  97. } mysql_event_general_subclass_t;
  98. #define MYSQL_AUDIT_GENERAL_ALL (MYSQL_AUDIT_GENERAL_LOG | \
  99. MYSQL_AUDIT_GENERAL_ERROR | \
  100. MYSQL_AUDIT_GENERAL_RESULT | \
  101. MYSQL_AUDIT_GENERAL_STATUS)
  102. /**
  103. @struct mysql_event_general
  104. Structure for the MYSQL_AUDIT_GENERAL_CLASS event class.
  105. */
  106. struct mysql_event_general
  107. {
  108. mysql_event_general_subclass_t event_subclass;
  109. int general_error_code;
  110. unsigned long general_thread_id;
  111. MYSQL_LEX_CSTRING general_user;
  112. MYSQL_LEX_CSTRING general_command;
  113. MYSQL_LEX_CSTRING general_query;
  114. struct charset_info_st *general_charset;
  115. unsigned long long general_time;
  116. unsigned long long general_rows;
  117. MYSQL_LEX_CSTRING general_host;
  118. MYSQL_LEX_CSTRING general_sql_command;
  119. MYSQL_LEX_CSTRING general_external_user;
  120. MYSQL_LEX_CSTRING general_ip;
  121. };
  122. /**
  123. @enum mysql_event_connection_subclass_t
  124. Events for MYSQL_AUDIT_CONNECTION_CLASS event class.
  125. */
  126. typedef enum
  127. {
  128. /** occurs after authentication phase is completed. */
  129. MYSQL_AUDIT_CONNECTION_CONNECT = 1 << 0,
  130. /** occurs after connection is terminated. */
  131. MYSQL_AUDIT_CONNECTION_DISCONNECT = 1 << 1,
  132. /** occurs after COM_CHANGE_USER RPC is completed. */
  133. MYSQL_AUDIT_CONNECTION_CHANGE_USER = 1 << 2,
  134. /** occurs before authentication. */
  135. MYSQL_AUDIT_CONNECTION_PRE_AUTHENTICATE = 1 << 3
  136. } mysql_event_connection_subclass_t;
  137. #define MYSQL_AUDIT_CONNECTION_ALL (MYSQL_AUDIT_CONNECTION_CONNECT | \
  138. MYSQL_AUDIT_CONNECTION_DISCONNECT | \
  139. MYSQL_AUDIT_CONNECTION_CHANGE_USER | \
  140. MYSQL_AUDIT_CONNECTION_PRE_AUTHENTICATE)
  141. /**
  142. @struct mysql_event_connection
  143. Structure for the MYSQL_AUDIT_CONNECTION_CLASS event class.
  144. */
  145. struct mysql_event_connection
  146. {
  147. /** Event subclass. */
  148. mysql_event_connection_subclass_t event_subclass;
  149. /** Current status of the connection. */
  150. int status;
  151. /** Connection id. */
  152. unsigned long connection_id;
  153. /** User name of this connection. */
  154. MYSQL_LEX_CSTRING user;
  155. /** Priv user name. */
  156. MYSQL_LEX_CSTRING priv_user;
  157. /** External user name. */
  158. MYSQL_LEX_CSTRING external_user;
  159. /** Proxy user used for this connection. */
  160. MYSQL_LEX_CSTRING proxy_user;
  161. /** Connection host. */
  162. MYSQL_LEX_CSTRING host;
  163. /** IP of the connection. */
  164. MYSQL_LEX_CSTRING ip;
  165. /** Database name specified at connection time. */
  166. MYSQL_LEX_CSTRING database;
  167. /** Connection type:
  168. - 0 Undefined
  169. - 1 TCP/IP
  170. - 2 Socket
  171. - 3 Named pipe
  172. - 4 SSL
  173. - 5 Shared memory
  174. */
  175. int connection_type;
  176. };
  177. /**
  178. @enum mysql_event_parse_subclass_t
  179. Events for MYSQL_AUDIT_PARSE_CLASS event class.
  180. */
  181. typedef enum
  182. {
  183. /** occurs before the query parsing. */
  184. MYSQL_AUDIT_PARSE_PREPARSE = 1 << 0,
  185. /** occurs after the query parsing. */
  186. MYSQL_AUDIT_PARSE_POSTPARSE = 1 << 1
  187. } mysql_event_parse_subclass_t;
  188. #define MYSQL_AUDIT_PARSE_ALL (MYSQL_AUDIT_PARSE_PREPARSE | \
  189. MYSQL_AUDIT_PARSE_POSTPARSE)
  190. typedef enum
  191. {
  192. MYSQL_AUDIT_PARSE_REWRITE_PLUGIN_NONE = 0,
  193. /// mysql_event_parse::flags Must be set by a plugin if the query is rewritten.
  194. MYSQL_AUDIT_PARSE_REWRITE_PLUGIN_QUERY_REWRITTEN = 1 << 0,
  195. /// mysql_event_parse::flags Is set by the server if the query is prepared statement.
  196. MYSQL_AUDIT_PARSE_REWRITE_PLUGIN_IS_PREPARED_STATEMENT = 1 << 1
  197. } mysql_event_parse_rewrite_plugin_flag;
  198. /** Data for the MYSQL_AUDIT_PARSE events */
  199. struct mysql_event_parse
  200. {
  201. /** MYSQL_AUDIT_[PRE|POST]_PARSE event id */
  202. mysql_event_parse_subclass_t event_subclass;
  203. /** one of FLAG_REWRITE_PLUGIN_* */
  204. mysql_event_parse_rewrite_plugin_flag *flags;
  205. /** input: the original query text */
  206. MYSQL_LEX_CSTRING query;
  207. /** output: returns the null-terminated rewriten query allocated by my_malloc() */
  208. MYSQL_LEX_CSTRING *rewritten_query;
  209. };
  210. /**
  211. @enum mysql_event_authorization_subclass_t
  212. Events for MYSQL_AUDIT_AUTHORIZATION_CLASS event class.
  213. */
  214. typedef enum
  215. {
  216. MYSQL_AUDIT_AUTHORIZATION_USER = 1 << 0,
  217. /** Occurs when database privilege is checked. */
  218. MYSQL_AUDIT_AUTHORIZATION_DB = 1 << 1,
  219. /** Occurs when table privilege is checked. */
  220. MYSQL_AUDIT_AUTHORIZATION_TABLE = 1 << 2,
  221. /** Occurs when column privilege is checked. */
  222. MYSQL_AUDIT_AUTHORIZATION_COLUMN = 1 << 3,
  223. /** Occurs when procedure privilege is checked. */
  224. MYSQL_AUDIT_AUTHORIZATION_PROCEDURE = 1 << 4,
  225. /** Occurs when proxy privilege is checked. */
  226. MYSQL_AUDIT_AUTHORIZATION_PROXY = 1 << 5
  227. } mysql_event_authorization_subclass_t;
  228. #define MYSQL_AUDIT_AUTHORIZATION_ALL (MYSQL_AUDIT_AUTHORIZATION_USER | \
  229. MYSQL_AUDIT_AUTHORIZATION_DB | \
  230. MYSQL_AUDIT_AUTHORIZATION_TABLE | \
  231. MYSQL_AUDIT_AUTHORIZATION_COLUMN | \
  232. MYSQL_AUDIT_AUTHORIZATION_PROCEDURE | \
  233. MYSQL_AUDIT_AUTHORIZATION_PROXY)
  234. /**
  235. @struct mysql_event_authorization
  236. Structure for MYSQL_AUDIT_AUTHORIZATION_CLASS event class.
  237. */
  238. struct mysql_event_authorization
  239. {
  240. /** Event subclass. */
  241. mysql_event_authorization_subclass_t event_subclass;
  242. /** Event status. */
  243. int status;
  244. /** Connection id. */
  245. unsigned int connection_id;
  246. /** SQL command id. */
  247. enum_sql_command_t sql_command_id;
  248. /** SQL query text. */
  249. MYSQL_LEX_CSTRING query;
  250. /** SQL query charset. */
  251. const struct charset_info_st *query_charset;
  252. /** Database name. */
  253. MYSQL_LEX_CSTRING database;
  254. /** Table name. */
  255. MYSQL_LEX_CSTRING table;
  256. /** Other name associated with the event. */
  257. MYSQL_LEX_CSTRING object;
  258. /** Requested authorization privileges. */
  259. unsigned long requested_privilege;
  260. /** Currently granted authorization privileges. */
  261. unsigned long granted_privilege;
  262. };
  263. /**
  264. @enum mysql_event_table_row_access_subclass_t
  265. Events for MYSQL_AUDIT_TABLE_ACCES_CLASS event class.
  266. */
  267. typedef enum
  268. {
  269. /** Occurs when table data are read. */
  270. MYSQL_AUDIT_TABLE_ACCESS_READ = 1 << 0,
  271. /** Occurs when table data are inserted. */
  272. MYSQL_AUDIT_TABLE_ACCESS_INSERT = 1 << 1,
  273. /** Occurs when table data are updated. */
  274. MYSQL_AUDIT_TABLE_ACCESS_UPDATE = 1 << 2,
  275. /** Occurs when table data are deleted. */
  276. MYSQL_AUDIT_TABLE_ACCESS_DELETE = 1 << 3
  277. } mysql_event_table_access_subclass_t;
  278. #define MYSQL_AUDIT_TABLE_ACCESS_ALL (MYSQL_AUDIT_TABLE_ACCESS_READ | \
  279. MYSQL_AUDIT_TABLE_ACCESS_INSERT | \
  280. MYSQL_AUDIT_TABLE_ACCESS_UPDATE | \
  281. MYSQL_AUDIT_TABLE_ACCESS_DELETE)
  282. /**
  283. @struct mysql_event_table_row_access
  284. Structure for MYSQL_AUDIT_TABLE_ACCES_CLASS event class.
  285. */
  286. struct mysql_event_table_access
  287. {
  288. /** Event subclass. */
  289. mysql_event_table_access_subclass_t event_subclass;
  290. /** Connection id. */
  291. unsigned long connection_id;
  292. /** SQL command id. */
  293. enum_sql_command_t sql_command_id;
  294. /** SQL query. */
  295. MYSQL_LEX_CSTRING query;
  296. /** SQL query charset. */
  297. const struct charset_info_st *query_charset;
  298. /** Database name. */
  299. MYSQL_LEX_CSTRING table_database;
  300. /** Table name. */
  301. MYSQL_LEX_CSTRING table_name;
  302. };
  303. /**
  304. @enum mysql_event_global_variable_subclass_t
  305. Events for MYSQL_AUDIT_GLOBAL_VARIABLE_CLASS event class.
  306. */
  307. typedef enum
  308. {
  309. /** Occurs when global variable is retrieved. */
  310. MYSQL_AUDIT_GLOBAL_VARIABLE_GET = 1 << 0,
  311. /** Occurs when global variable is set. */
  312. MYSQL_AUDIT_GLOBAL_VARIABLE_SET = 1 << 1
  313. } mysql_event_global_variable_subclass_t;
  314. #define MYSQL_AUDIT_GLOBAL_VARIABLE_ALL (MYSQL_AUDIT_GLOBAL_VARIABLE_GET | \
  315. MYSQL_AUDIT_GLOBAL_VARIABLE_SET)
  316. /** Events for MYSQL_AUDIT_GLOBAL_VARIABLE_CLASS event class. */
  317. struct mysql_event_global_variable
  318. {
  319. /** Event subclass. */
  320. mysql_event_global_variable_subclass_t event_subclass;
  321. /** Connection id. */
  322. unsigned long connection_id;
  323. /** SQL command id. */
  324. enum_sql_command_t sql_command_id;
  325. /** Variable name. */
  326. MYSQL_LEX_CSTRING variable_name;
  327. /** Variable value. */
  328. MYSQL_LEX_CSTRING variable_value;
  329. };
  330. /**
  331. @enum mysql_event_server_startup_subclass_t
  332. Events for MYSQL_AUDIT_SERVER_STARTUP_CLASS event class.
  333. */
  334. typedef enum
  335. {
  336. /** Occurs after all subsystem are initialized during system start. */
  337. MYSQL_AUDIT_SERVER_STARTUP_STARTUP = 1 << 0
  338. } mysql_event_server_startup_subclass_t;
  339. #define MYSQL_AUDIT_SERVER_STARTUP_ALL (MYSQL_AUDIT_SERVER_STARTUP_STARTUP)
  340. /**
  341. @struct mysql_event_server_startup
  342. Structure for MYSQL_AUDIT_SERVER_STARTUP_CLASS event class.
  343. */
  344. struct mysql_event_server_startup
  345. {
  346. /** Event subclass. */
  347. mysql_event_server_startup_subclass_t event_subclass;
  348. /** Command line arguments. */
  349. const char **argv;
  350. /** Command line arguments count. */
  351. unsigned int argc;
  352. };
  353. /**
  354. @enum mysql_event_server_shutdown_subclass_t
  355. Events for MYSQL_AUDIT_SERVER_SHUTDOWN_CLASS event class.
  356. */
  357. typedef enum
  358. {
  359. /** Occurs when global variable is set. */
  360. MYSQL_AUDIT_SERVER_SHUTDOWN_SHUTDOWN = 1 << 0
  361. } mysql_event_server_shutdown_subclass_t;
  362. #define MYSQL_AUDIT_SERVER_SHUTDOWN_ALL (MYSQL_AUDIT_SERVER_SHUTDOWN_SHUTDOWN)
  363. /**
  364. @enum mysql_server_shutdown_reason_t
  365. Server shutdown reason.
  366. */
  367. typedef enum
  368. {
  369. /** User requested shut down. */
  370. MYSQL_AUDIT_SERVER_SHUTDOWN_REASON_SHUTDOWN,
  371. /** The server aborts. */
  372. MYSQL_AUDIT_SERVER_SHUTDOWN_REASON_ABORT
  373. } mysql_server_shutdown_reason_t;
  374. /**
  375. @struct mysql_event_server_shutdown
  376. Structure for MYSQL_AUDIT_SERVER_SHUTDOWN_CLASS event class.
  377. */
  378. struct mysql_event_server_shutdown
  379. {
  380. /** Shutdown event. */
  381. mysql_event_server_shutdown_subclass_t event_subclass;
  382. /** Exit code associated with the shutdown event. */
  383. int exit_code;
  384. /** Shutdown reason. */
  385. mysql_server_shutdown_reason_t reason;
  386. };
  387. /**
  388. @enum mysql_event_command_subclass_t
  389. Events for MYSQL_AUDIT_COMMAND_CLASS event class.
  390. */
  391. typedef enum
  392. {
  393. /** Command start event. */
  394. MYSQL_AUDIT_COMMAND_START = 1 << 0,
  395. /** Command end event. */
  396. MYSQL_AUDIT_COMMAND_END = 1 << 1
  397. } mysql_event_command_subclass_t;
  398. #define MYSQL_AUDIT_COMMAND_ALL (MYSQL_AUDIT_COMMAND_START | \
  399. MYSQL_AUDIT_COMMAND_END)
  400. /**
  401. @typedef enum_server_command_t
  402. Server command type definition.
  403. */
  404. typedef enum enum_server_command enum_server_command_t;
  405. /**
  406. @struct mysql_event_command
  407. Event for MYSQL_AUDIT_COMMAND_CLASS event class.
  408. Events generated as a result of RPC command requests.
  409. */
  410. struct mysql_event_command
  411. {
  412. /** Command event subclass. */
  413. mysql_event_command_subclass_t event_subclass;
  414. /** Command event status. */
  415. int status;
  416. /** Connection id. */
  417. unsigned long connection_id;
  418. /** Command id. */
  419. enum_server_command_t command_id;
  420. };
  421. /**
  422. @enum mysql_event_query_subclass_t
  423. Events for MYSQL_AUDIT_QUERY_CLASS event class.
  424. */
  425. typedef enum
  426. {
  427. /** Query start event. */
  428. MYSQL_AUDIT_QUERY_START = 1 << 0,
  429. /** Nested query start event. */
  430. MYSQL_AUDIT_QUERY_NESTED_START = 1 << 1,
  431. /** Query post parse event. */
  432. MYSQL_AUDIT_QUERY_STATUS_END = 1 << 2,
  433. /** Nested query status end event. */
  434. MYSQL_AUDIT_QUERY_NESTED_STATUS_END = 1 << 3
  435. } mysql_event_query_subclass_t;
  436. #define MYSQL_AUDIT_QUERY_ALL (MYSQL_AUDIT_QUERY_START | \
  437. MYSQL_AUDIT_QUERY_NESTED_START | \
  438. MYSQL_AUDIT_QUERY_STATUS_END | \
  439. MYSQL_AUDIT_QUERY_NESTED_STATUS_END)
  440. /**
  441. @struct mysql_event_command
  442. Event for MYSQL_AUDIT_COMMAND_CLASS event class.
  443. */
  444. struct mysql_event_query
  445. {
  446. /** Event subclass. */
  447. mysql_event_query_subclass_t event_subclass;
  448. /** Event status. */
  449. int status;
  450. /** Connection id. */
  451. unsigned long connection_id;
  452. /** SQL command id. */
  453. enum_sql_command_t sql_command_id;
  454. /** SQL query. */
  455. MYSQL_LEX_CSTRING query;
  456. /** SQL query charset. */
  457. const struct charset_info_st *query_charset;
  458. };
  459. /**
  460. @enum mysql_event_stored_program_subclass_t
  461. Events for MYSQL_AUDIT_STORED_PROGRAM_CLASS event class.
  462. */
  463. typedef enum
  464. {
  465. /** Stored program execution event. */
  466. MYSQL_AUDIT_STORED_PROGRAM_EXECUTE = 1 << 0
  467. } mysql_event_stored_program_subclass_t;
  468. #define MYSQL_AUDIT_STORED_PROGRAM_ALL (MYSQL_AUDIT_STORED_PROGRAM_EXECUTE)
  469. /**
  470. @struct mysql_event_command
  471. Event for MYSQL_AUDIT_COMMAND_CLASS event class.
  472. */
  473. struct mysql_event_stored_program
  474. {
  475. /** Event subclass. */
  476. mysql_event_stored_program_subclass_t event_subclass;
  477. /** Connection id. */
  478. unsigned long connection_id;
  479. /** SQL command id. */
  480. enum_sql_command_t sql_command_id;
  481. /** SQL query text. */
  482. MYSQL_LEX_CSTRING query;
  483. /** SQL query charset. */
  484. const struct charset_info_st *query_charset;
  485. /** The Database the procedure is defined in. */
  486. MYSQL_LEX_CSTRING database;
  487. /** Name of the stored program. */
  488. MYSQL_LEX_CSTRING name;
  489. /** Stored program parameters. */
  490. void *parameters;
  491. };
  492. #endif