1 /* Copyright (c) 2016, 2019, Oracle and/or its affiliates. All rights reserved.
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 /* Execute CALL statement */
24 
25 #include "sql/sql_call.h"
26 
27 #include <limits.h>
28 #include <stddef.h>
29 #include <sys/types.h>
30 #include <algorithm>
31 #include <atomic>
32 
33 #include "lex_string.h"
34 #include "my_base.h"
35 #include "my_dbug.h"
36 #include "my_inttypes.h"
37 #include "my_sys.h"
38 #include "mysql/plugin_audit.h"
39 #include "mysql_com.h"
40 #include "mysqld_error.h"
41 #include "sql/auth/auth_acls.h"
42 #include "sql/auth/auth_common.h"  // check_routine_access, check_table_access
43 #include "sql/item.h"              // class Item
44 #include "sql/protocol.h"
45 #include "sql/sp.h"  // sp_find_routine
46 #include "sql/sp_head.h"
47 #include "sql/sp_pcontext.h"  // class sp_variable
48 #include "sql/sql_audit.h"    // AUDIT_EVENT
49 #include "sql/sql_class.h"    // class THD
50 #include "sql/sql_lex.h"
51 #include "sql/sql_list.h"
52 #include "sql/system_variables.h"
53 #include "template_utils.h"
54 
55 using std::max;
56 
precheck(THD * thd)57 bool Sql_cmd_call::precheck(THD *thd) {
58   // Check execute privilege on stored procedure
59   if (check_routine_access(thd, EXECUTE_ACL, proc_name->m_db.str,
60                            proc_name->m_name.str, true, false))
61     return true;
62 
63   // Check SELECT privileges for any subqueries
64   if (check_table_access(thd, SELECT_ACL, lex->query_tables, false, UINT_MAX,
65                          false))
66     return true;
67   return false;
68 }
69 
prepare_inner(THD * thd)70 bool Sql_cmd_call::prepare_inner(THD *thd) {
71   // All required SPs should be in cache so no need to look into DB.
72 
73   sp_head *sp = sp_find_routine(thd, enum_sp_type::PROCEDURE, proc_name,
74                                 &thd->sp_proc_cache, true);
75   if (sp == nullptr) {
76     my_error(ER_SP_DOES_NOT_EXIST, MYF(0), "PROCEDURE", proc_name->m_qname.str);
77     return true;
78   }
79 
80   sp_pcontext *root_parsing_context = sp->get_root_parsing_context();
81 
82   uint arg_count = proc_args != nullptr ? proc_args->elements : 0;
83 
84   if (root_parsing_context->context_var_count() != arg_count) {
85     my_error(ER_SP_WRONG_NO_OF_ARGS, MYF(0), "PROCEDURE",
86              proc_name->m_qname.str, root_parsing_context->context_var_count(),
87              arg_count);
88     return true;
89   }
90 
91   if (proc_args == nullptr) return false;
92 
93   List_iterator<Item> it(*proc_args);
94   Item *item;
95   int arg_no = 0;
96   while ((item = it++)) {
97     if (item->type() == Item::TRIGGER_FIELD_ITEM) {
98       Item_trigger_field *itf = down_cast<Item_trigger_field *>(item);
99       sp_variable *spvar = root_parsing_context->find_variable(arg_no);
100       if (spvar->mode != sp_variable::MODE_IN)
101         itf->set_required_privilege(spvar->mode == sp_variable::MODE_INOUT);
102     }
103     if ((!item->fixed && item->fix_fields(thd, it.ref())) ||
104         item->check_cols(1))
105       return true; /* purecov: inspected */
106     arg_no++;
107   }
108 
109   return false;
110 }
111 
execute_inner(THD * thd)112 bool Sql_cmd_call::execute_inner(THD *thd) {
113   // All required SPs should be in cache so no need to look into DB.
114 
115   sp_head *sp = sp_setup_routine(thd, enum_sp_type::PROCEDURE, proc_name,
116                                  &thd->sp_proc_cache);
117   if (sp == nullptr) {
118     my_error(ER_SP_DOES_NOT_EXIST, MYF(0), "PROCEDURE", proc_name->m_qname.str);
119     return true;
120   }
121 
122   // bits to be cleared in thd->server_status
123   uint bits_to_be_cleared = 0;
124   /*
125     Check that the stored procedure doesn't contain Dynamic SQL and doesn't
126     return result sets: such stored procedures can't be called from
127     a function or trigger.
128   */
129   if (thd->in_sub_stmt) {
130     const char *where =
131         (thd->in_sub_stmt & SUB_STMT_TRIGGER ? "trigger" : "function");
132     if (sp->is_not_allowed_in_function(where)) return true;
133   }
134 
135   if (mysql_audit_notify(thd, AUDIT_EVENT(MYSQL_AUDIT_STORED_PROGRAM_EXECUTE),
136                          proc_name->m_db.str, proc_name->m_name.str, nullptr))
137     return true;
138 
139   if (sp->m_flags & sp_head::MULTI_RESULTS) {
140     if (!thd->get_protocol()->has_client_capability(CLIENT_MULTI_RESULTS)) {
141       // Client does not support multiple result sets
142       my_error(ER_SP_BADSELECT, MYF(0), sp->m_qname.str);
143       return true;
144     }
145     /*
146       If SERVER_MORE_RESULTS_EXISTS is not set,
147       then remember that it should be cleared
148     */
149     bits_to_be_cleared = (~thd->server_status & SERVER_MORE_RESULTS_EXISTS);
150     thd->server_status |= SERVER_MORE_RESULTS_EXISTS;
151   }
152 
153   ha_rows select_limit = thd->variables.select_limit;
154   thd->variables.select_limit = HA_POS_ERROR;
155 
156   /*
157     Never write CALL statements into binlog:
158     - If the mode is non-prelocked, each statement will be logged separately.
159     - If the mode is prelocked, the invoking statement will care about writing
160       into binlog.
161     So just execute the statement.
162   */
163   bool result = sp->execute_procedure(thd, proc_args);
164 
165   thd->variables.select_limit = select_limit;
166 
167   thd->server_status &= ~bits_to_be_cleared;
168 
169   if (result) {
170     DBUG_ASSERT(thd->is_error() || thd->killed);
171     return true;  // Substatement should already have sent error
172   }
173 
174   my_ok(thd, max(thd->get_row_count_func(), 0LL));
175 
176   return false;
177 }
178