1 /* Copyright (c) 2015, 2021, Oracle and/or its affiliates.
2 
3    This program is free software; you can redistribute it and/or modify
4    it under the terms of the GNU General Public License, version 2.0,
5    as published by the Free Software Foundation.
6 
7    This program is also distributed with certain software (including
8    but not limited to OpenSSL) that is licensed under separate terms,
9    as designated in a particular file or component or in included license
10    documentation.  The authors of MySQL hereby grant you an additional
11    permission to link the program and your derivative works with the
12    separately licensed software that they have included with MySQL.
13 
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License, version 2.0, for more details.
18 
19    You should have received a copy of the GNU General Public License
20    along with this program; if not, write to the Free Software
21    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA */
22 
23 #include "my_global.h"
24 #include "mysql/plugin_audit.h"
25 #include "mysql/service_rules_table.h"
26 #include "mysql/service_ssl_wrapper.h"
27 #include "sql_cache.h"
28 #include "sql_error.h"
29 #include "sql_parse.h"
30 #include "sql_plugin.h"
31 #include "sql_query_rewrite.h"
32 #include "log.h"
33 #include "sql_base.h"
34 #include "sql_class.h"
35 #include "sql_audit.h"
36 
raise_query_rewritten_note(THD * thd,const char * original_query,const char * rewritten_query)37 void raise_query_rewritten_note(THD *thd,
38                                 const char *original_query,
39                                 const char *rewritten_query)
40 {
41   Sql_condition::enum_severity_level sl= Sql_condition::SL_NOTE;
42   const char *message= "Query '%s' rewritten to '%s' by a query rewrite plugin";
43   push_warning_printf(thd, sl, ER_UNKNOWN_ERROR, message,
44                       original_query, rewritten_query);
45 };
46 
47 #ifndef EMBEDDED_LIBRARY
48 
invoke_pre_parse_rewrite_plugins(THD * thd)49 void invoke_pre_parse_rewrite_plugins(THD *thd)
50 {
51   Diagnostics_area *plugin_da= thd->get_query_rewrite_plugin_da();
52   if (plugin_da == NULL)
53     return;
54   plugin_da->reset_diagnostics_area();
55   plugin_da->reset_condition_info(thd);
56 
57   Diagnostics_area *da= thd->get_parser_da();
58   thd->push_diagnostics_area(plugin_da, false);
59   mysql_event_parse_rewrite_plugin_flag flags=
60                                         MYSQL_AUDIT_PARSE_REWRITE_PLUGIN_NONE;
61   LEX_CSTRING rewritten_query = { NULL, 0 };
62   mysql_audit_notify(thd, AUDIT_EVENT(MYSQL_AUDIT_PARSE_PREPARSE),
63                      &flags,
64                      &rewritten_query);
65 
66   /* Do not continue when the plugin set the error state. */
67   if (!plugin_da->is_error() &&
68       flags & MYSQL_AUDIT_PARSE_REWRITE_PLUGIN_QUERY_REWRITTEN)
69   {
70     // It is a rewrite fulltext plugin and we need a rewrite we must have
71     // generated a new query then.
72     assert(rewritten_query.str != NULL &&
73            rewritten_query.length > 0);
74     raise_query_rewritten_note(thd, thd->query().str, rewritten_query.str);
75     alloc_query(thd, rewritten_query.str, rewritten_query.length);
76     thd->m_parser_state->init(thd, thd->query().str, thd->query().length);
77     my_free((void *)rewritten_query.str);
78   }
79 
80   da->copy_non_errors_from_da(thd, plugin_da);
81   thd->pop_diagnostics_area();
82 
83   if (plugin_da->is_error())
84   {
85     thd->get_stmt_da()->set_error_status(plugin_da->mysql_errno(),
86                                          plugin_da->message_text(),
87                                          plugin_da->returned_sqlstate());
88     plugin_da->reset_diagnostics_area();
89   }
90 }
91 
enable_digest_if_any_plugin_needs_it(THD * thd,Parser_state * ps)92 void enable_digest_if_any_plugin_needs_it(THD *thd, Parser_state *ps)
93 {
94   if (is_audit_plugin_class_active(thd,
95                          static_cast<unsigned long>(MYSQL_AUDIT_PARSE_CLASS)))
96     ps->m_input.m_compute_digest= true;
97 }
98 
99 
invoke_post_parse_rewrite_plugins(THD * thd,my_bool is_prepared)100 bool invoke_post_parse_rewrite_plugins(THD *thd, my_bool is_prepared)
101 {
102   Diagnostics_area *plugin_da= thd->get_query_rewrite_plugin_da();
103   plugin_da->reset_diagnostics_area();
104   plugin_da->reset_condition_info(thd);
105 
106   Diagnostics_area *stmt_da= thd->get_stmt_da();
107 
108   /*
109     We save the value of keep_diagnostics here as it gets reset by
110     push_diagnostics_area(), see below for use.
111   */
112   bool keeping_diagnostics= thd->lex->keep_diagnostics == DA_KEEP_PARSE_ERROR;
113 
114   thd->push_diagnostics_area(plugin_da, false);
115 
116   {
117     /*
118        We have to call a function in rules_table_service.cc, or the service
119        won't be visible to plugins.
120     */
121 #ifndef NDEBUG
122     int dummy=
123 #endif
124       rules_table_service::
125       dummy_function_to_ensure_we_are_linked_into_the_server();
126     assert(dummy == 1);
127 
128 #ifndef NDEBUG
129     dummy=
130 #endif
131       ssl_wrappe_service::
132       dummy_function_to_ensure_we_are_linked_into_the_server();
133     assert(dummy == 1);
134   }
135 
136   mysql_event_parse_rewrite_plugin_flag flags=
137        is_prepared ?  MYSQL_AUDIT_PARSE_REWRITE_PLUGIN_IS_PREPARED_STATEMENT :
138                       MYSQL_AUDIT_PARSE_REWRITE_PLUGIN_NONE;
139   bool err= false;
140   const char *original_query= thd->query().str;
141   mysql_audit_notify(thd, AUDIT_EVENT(MYSQL_AUDIT_PARSE_POSTPARSE),
142                      &flags, NULL);
143 
144   if (flags & MYSQL_AUDIT_PARSE_REWRITE_PLUGIN_QUERY_REWRITTEN)
145   {
146     raise_query_rewritten_note(thd, original_query, thd->query().str);
147     thd->lex->safe_to_cache_query= false;
148   }
149 
150   if (plugin_da->current_statement_cond_count() != 0)
151   {
152     /*
153       A plugin raised at least one condition. At this point these are in the
154       plugin DA, and we should copy them to the statement DA. But before we do
155       that, we may have to clear it as this DA may contain conditions from the
156       previous statement. We have to clear it *unless* the statement is a
157       diagnostics statement, in which case we keep everything: conditions from
158       previous statements, parser conditions and plugin conditions. If this is
159       not a diagnostics statement, parse_sql() has already cleared the
160       statement DA, copied the parser condtitions to the statement DA and set
161       DA_KEEP_PARSE_ERROR. So we arrive at the below condition for telling us
162       when to clear the statement DA.
163     */
164     if (thd->lex->sql_command != SQLCOM_SHOW_WARNS && !keeping_diagnostics)
165       stmt_da->reset_condition_info(thd);
166 
167     /* We need to put any errors in the DA as well as the condition list. */
168     if (plugin_da->is_error())
169       stmt_da->set_error_status(plugin_da->mysql_errno(),
170                                 plugin_da->message_text(),
171                                 plugin_da->returned_sqlstate());
172 
173     stmt_da->copy_sql_conditions_from_da(thd, plugin_da);
174 
175     /*
176       Do not clear the condition list when starting execution as it now
177       contains not the results of the previous executions, but a non-zero
178       number of errors/warnings thrown during parsing or plugin execution.
179     */
180     thd->lex->keep_diagnostics= DA_KEEP_PARSE_ERROR;
181   }
182 
183   thd->pop_diagnostics_area();
184 
185   return err;
186 }
187 
188 
189 #else /* EMBEDDED_LIBRARY */
190 
invoke_pre_parse_rewrite_plugins(THD * thd)191 void invoke_pre_parse_rewrite_plugins(THD *thd) {}
192 
enable_digest_if_any_plugin_needs_it(THD * thd,Parser_state * ps)193 void enable_digest_if_any_plugin_needs_it(THD *thd, Parser_state *ps) {}
194 
invoke_post_parse_rewrite_plugins(THD * thd,my_bool is_prepared)195 bool invoke_post_parse_rewrite_plugins(THD *thd, my_bool is_prepared)
196 {
197   return false;
198 }
199 
200 #endif /* EMBEDDED_LIBRARY */
201