Clone of PhatAC @ https://github.com/floaterxk/PhatAC

plugin_audit.h 17KB

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