36 #include "sql_parse.h"
56 bool append_int(
String *str,
bool comma,
const char *txt,
size_t len,
63 str->append(STRING_WITH_LEN(
", "));
65 numbuf.set((longlong)val,&my_charset_bin);
85 bool append_str(
String *str,
bool comma,
const char *key,
char *val)
90 str->append(STRING_WITH_LEN(
", "));
92 str->append(STRING_WITH_LEN(
" '"));
94 str->append(STRING_WITH_LEN(
"'"));
108 static void mysql_rewrite_grant(THD *thd,
String *rlb)
112 bool comma= FALSE, comma_inner;
116 rlb->append(STRING_WITH_LEN(
"GRANT "));
118 if (lex->all_privileges)
119 rlb->append(STRING_WITH_LEN(
"ALL PRIVILEGES"));
124 for (c= 0, priv= SELECT_ACL; priv <= GLOBAL_ACLS; c++, priv <<= 1)
126 if (priv == GRANT_ACL)
131 if (lex->columns.elements)
137 cols.append(STRING_WITH_LEN(
" ("));
145 while ((column= column_iter++))
147 if (column->rights & priv)
150 cols.append(STRING_WITH_LEN(
", "));
153 cols.append(column->column.ptr(),column->column.length());
156 cols.append(STRING_WITH_LEN(
")"));
159 if (comma_inner || (lex->grant & priv))
162 rlb->append(STRING_WITH_LEN(
", "));
165 rlb->append(command_array[c],command_lengths[c]);
166 if (!(lex->grant & priv))
171 rlb->append(STRING_WITH_LEN(
"USAGE"));
174 rlb->append(STRING_WITH_LEN(
" ON "));
177 case TYPE_ENUM_PROCEDURE: rlb->append(STRING_WITH_LEN(
"PROCEDURE "));
break;
178 case TYPE_ENUM_FUNCTION: rlb->append(STRING_WITH_LEN(
"FUNCTION "));
break;
184 append_identifier(thd, rlb, first_table->db, strlen(first_table->db));
185 rlb->append(STRING_WITH_LEN(
"."));
186 append_identifier(thd, rlb, first_table->table_name,
187 strlen(first_table->table_name));
191 if (lex->current_select->db)
192 append_identifier(thd, rlb, lex->current_select->db,
193 strlen(lex->current_select->db));
196 rlb->append(STRING_WITH_LEN(
".*"));
199 rlb->append(STRING_WITH_LEN(
" TO "));
201 LEX_USER *user_name, *tmp_user_name;
205 while ((tmp_user_name= user_list++))
209 append_user(thd, rlb, user_name, comma,
true);
215 if (lex->ssl_type != SSL_TYPE_NOT_SPECIFIED)
217 rlb->append(STRING_WITH_LEN(
" REQUIRE"));
218 switch (lex->ssl_type)
220 case SSL_TYPE_SPECIFIED:
221 if (lex->x509_subject)
223 rlb->append(STRING_WITH_LEN(
" SUBJECT '"));
224 rlb->append(lex->x509_subject);
225 rlb->append(STRING_WITH_LEN(
"'"));
227 if (lex->x509_issuer)
229 rlb->append(STRING_WITH_LEN(
" ISSUER '"));
230 rlb->append(lex->x509_issuer);
231 rlb->append(STRING_WITH_LEN(
"'"));
235 rlb->append(STRING_WITH_LEN(
" CIPHER '"));
236 rlb->append(lex->ssl_cipher);
237 rlb->append(STRING_WITH_LEN(
"'"));
241 rlb->append(STRING_WITH_LEN(
" X509"));
244 rlb->append(STRING_WITH_LEN(
" SSL"));
246 case SSL_TYPE_NOT_SPECIFIED:
249 rlb->append(STRING_WITH_LEN(
" NONE"));
254 if (lex->mqh.specified_limits || (lex->grant & GRANT_ACL))
256 rlb->append(STRING_WITH_LEN(
" WITH"));
257 if (lex->grant & GRANT_ACL)
258 rlb->append(STRING_WITH_LEN(
" GRANT OPTION"));
260 append_int(rlb,
false, STRING_WITH_LEN(
" MAX_QUERIES_PER_HOUR "),
262 lex->mqh.specified_limits & USER_RESOURCES::QUERIES_PER_HOUR);
264 append_int(rlb,
false, STRING_WITH_LEN(
" MAX_UPDATES_PER_HOUR "),
266 lex->mqh.specified_limits & USER_RESOURCES::UPDATES_PER_HOUR);
268 append_int(rlb,
false, STRING_WITH_LEN(
" MAX_CONNECTIONS_PER_HOUR "),
269 lex->mqh.conn_per_hour,
270 lex->mqh.specified_limits & USER_RESOURCES::CONNECTIONS_PER_HOUR);
272 append_int(rlb,
false, STRING_WITH_LEN(
" MAX_USER_CONNECTIONS "),
274 lex->mqh.specified_limits & USER_RESOURCES::USER_CONNECTIONS);
286 static void mysql_rewrite_set(THD *thd,
String *rlb)
293 rlb->append(STRING_WITH_LEN(
"SET "));
298 rlb->append(STRING_WITH_LEN(
","));
302 var->print(thd, rlb);
314 static void mysql_rewrite_create_user(THD *thd,
String *rlb)
317 LEX_USER *user_name, *tmp_user_name;
321 rlb->append(STRING_WITH_LEN(
"CREATE USER "));
322 while ((tmp_user_name= user_list++))
326 append_user(thd, rlb, user_name, comma, TRUE);
340 static void mysql_rewrite_change_master(THD *thd,
String *rlb)
344 rlb->append(STRING_WITH_LEN(
"CHANGE MASTER TO"));
348 rlb->append(STRING_WITH_LEN(
" MASTER_HOST = '"));
349 rlb->append(lex->mi.host);
350 rlb->append(STRING_WITH_LEN(
"'"));
354 rlb->append(STRING_WITH_LEN(
" MASTER_USER = '"));
355 rlb->append(lex->mi.user);
356 rlb->append(STRING_WITH_LEN(
"'"));
358 if (lex->mi.password)
360 rlb->append(STRING_WITH_LEN(
" MASTER_PASSWORD = <secret>"));
364 rlb->append(STRING_WITH_LEN(
" MASTER_PORT = "));
365 rlb->append_ulonglong(lex->mi.port);
367 if (lex->mi.connect_retry)
369 rlb->append(STRING_WITH_LEN(
" MASTER_CONNECT_RETRY = "));
370 rlb->append_ulonglong(lex->mi.connect_retry);
374 rlb->append(STRING_WITH_LEN(
" MASTER_SSL = "));
375 rlb->append(lex->mi.ssl == LEX_MASTER_INFO::LEX_MI_ENABLE ?
"1" :
"0");
379 rlb->append(STRING_WITH_LEN(
" MASTER_SSL_CA = '"));
380 rlb->append(lex->mi.ssl_ca);
381 rlb->append(STRING_WITH_LEN(
"'"));
383 if (lex->mi.ssl_capath)
385 rlb->append(STRING_WITH_LEN(
" MASTER_SSL_CAPATH = '"));
386 rlb->append(lex->mi.ssl_capath);
387 rlb->append(STRING_WITH_LEN(
"'"));
389 if (lex->mi.ssl_cert)
391 rlb->append(STRING_WITH_LEN(
" MASTER_SSL_CERT = '"));
392 rlb->append(lex->mi.ssl_cert);
393 rlb->append(STRING_WITH_LEN(
"'"));
395 if (lex->mi.ssl_cipher)
397 rlb->append(STRING_WITH_LEN(
" MASTER_SSL_CIPHER = '"));
398 rlb->append(lex->mi.ssl_cipher);
399 rlb->append(STRING_WITH_LEN(
"'"));
403 rlb->append(STRING_WITH_LEN(
" MASTER_SSL_KEY = '"));
404 rlb->append(lex->mi.ssl_key);
405 rlb->append(STRING_WITH_LEN(
"'"));
407 if (lex->mi.log_file_name)
409 rlb->append(STRING_WITH_LEN(
" MASTER_LOG_FILE = '"));
410 rlb->append(lex->mi.log_file_name);
411 rlb->append(STRING_WITH_LEN(
"'"));
415 rlb->append(STRING_WITH_LEN(
" MASTER_LOG_POS = "));
416 rlb->append_ulonglong(lex->mi.pos);
418 if (lex->mi.relay_log_name)
420 rlb->append(STRING_WITH_LEN(
" RELAY_LOG_FILE = '"));
421 rlb->append(lex->mi.relay_log_name);
422 rlb->append(STRING_WITH_LEN(
"'"));
424 if (lex->mi.relay_log_pos)
426 rlb->append(STRING_WITH_LEN(
" RELAY_LOG_POS = "));
427 rlb->append_ulonglong(lex->mi.relay_log_pos);
430 if (lex->mi.ssl_verify_server_cert)
432 rlb->append(STRING_WITH_LEN(
" MASTER_SSL_VERIFY_SERVER_CERT = "));
433 rlb->append(lex->mi.ssl_verify_server_cert == LEX_MASTER_INFO::LEX_MI_ENABLE ?
"1" :
"0");
435 if (lex->mi.repl_ignore_server_ids_opt)
438 rlb->append(STRING_WITH_LEN(
" IGNORE_SERVER_IDS = ( "));
439 for (uint
i= 0;
i < lex->mi.repl_ignore_server_ids.elements;
i++)
442 get_dynamic(&lex->mi.repl_ignore_server_ids, (uchar*) &s_id,
i);
446 rlb->append(STRING_WITH_LEN(
", "));
447 rlb->append_ulonglong(s_id);
449 rlb->append(STRING_WITH_LEN(
" )"));
451 if (lex->mi.heartbeat_opt != LEX_MASTER_INFO::LEX_MI_UNCHANGED)
453 rlb->append(STRING_WITH_LEN(
" MASTER_HEARTBEAT_PERIOD = "));
454 if (lex->mi.heartbeat_opt == LEX_MASTER_INFO::LEX_MI_DISABLE)
455 rlb->append(STRING_WITH_LEN(
"0"));
459 snprintf(buf, 64,
"%f", lex->mi.heartbeat_period);
473 static void mysql_rewrite_start_slave(THD *thd,
String *rlb)
477 if (!lex->slave_connection.password)
480 rlb->append(STRING_WITH_LEN(
"START SLAVE"));
482 if (lex->slave_thd_opt & SLAVE_IO)
483 rlb->append(STRING_WITH_LEN(
" IO_THREAD"));
486 if (lex->slave_thd_opt & SLAVE_IO &&
487 lex->slave_thd_opt & SLAVE_SQL)
488 rlb->append(STRING_WITH_LEN(
","));
490 if (lex->slave_thd_opt & SLAVE_SQL)
491 rlb->append(STRING_WITH_LEN(
" SQL_THREAD"));
494 if (lex->mi.log_file_name || lex->mi.relay_log_name)
496 rlb->append(STRING_WITH_LEN(
" UNTIL"));
497 if (lex->mi.log_file_name)
499 rlb->append(STRING_WITH_LEN(
" MASTER_LOG_FILE = '"));
500 rlb->append(lex->mi.log_file_name);
501 rlb->append(STRING_WITH_LEN(
"', "));
502 rlb->append(STRING_WITH_LEN(
"MASTER_LOG_POS = "));
503 rlb->append_ulonglong(lex->mi.pos);
506 if (lex->mi.relay_log_name)
508 rlb->append(STRING_WITH_LEN(
" RELAY_LOG_FILE = '"));
509 rlb->append(lex->mi.relay_log_name);
510 rlb->append(STRING_WITH_LEN(
"', "));
511 rlb->append(STRING_WITH_LEN(
"RELAY_LOG_POS = "));
512 rlb->append_ulonglong(lex->mi.relay_log_pos);
517 if (lex->slave_connection.user)
519 rlb->append(STRING_WITH_LEN(
" USER = '"));
520 rlb->append(lex->slave_connection.user);
521 rlb->append(STRING_WITH_LEN(
"'"));
524 if (lex->slave_connection.password)
525 rlb->append(STRING_WITH_LEN(
" PASSWORD = '<secret>'"));
527 if (lex->slave_connection.plugin_auth)
529 rlb->append(STRING_WITH_LEN(
" DEFAULT_AUTH = '"));
530 rlb->append(lex->slave_connection.plugin_auth);
531 rlb->append(STRING_WITH_LEN(
"'"));
534 if (lex->slave_connection.plugin_dir)
536 rlb->append(STRING_WITH_LEN(
" PLUGIN_DIR = '"));
537 rlb->append(lex->slave_connection.plugin_dir);
538 rlb->append(STRING_WITH_LEN(
"'"));
550 static void mysql_rewrite_server_options(THD *thd,
String *rlb)
554 rlb->append(STRING_WITH_LEN(
" OPTIONS ( "));
556 rlb->append(STRING_WITH_LEN(
"PASSWORD '<secret>'"));
557 append_str(rlb,
true,
"USER", lex->server_options.username);
558 append_str(rlb,
true,
"HOST", lex->server_options.host);
559 append_str(rlb,
true,
"DATABASE", lex->server_options.db);
560 append_str(rlb,
true,
"OWNER", lex->server_options.owner);
561 append_str(rlb,
true,
"SOCKET", lex->server_options.socket);
562 append_int(rlb,
true, STRING_WITH_LEN(
"PORT "), lex->server_options.port,
563 lex->server_options.port > 0);
565 rlb->append(STRING_WITH_LEN(
" )"));
576 static void mysql_rewrite_create_server(THD *thd,
String *rlb)
580 if (!lex->server_options.password)
583 rlb->append(STRING_WITH_LEN(
"CREATE SERVER "));
585 rlb->append(lex->server_options.server_name ?
586 lex->server_options.server_name :
"");
588 rlb->append(STRING_WITH_LEN(
" FOREIGN DATA WRAPPER '"));
589 rlb->append(lex->server_options.scheme ?
590 lex->server_options.scheme :
"");
591 rlb->append(STRING_WITH_LEN(
"'"));
593 mysql_rewrite_server_options(thd, rlb);
604 static void mysql_rewrite_alter_server(THD *thd,
String *rlb)
608 if (!lex->server_options.password)
611 rlb->append(STRING_WITH_LEN(
"ALTER SERVER "));
613 rlb->append(lex->server_options.server_name ?
614 lex->server_options.server_name :
"");
616 mysql_rewrite_server_options(thd, rlb);
629 static void mysql_rewrite_prepare(THD *thd,
String *rlb)
633 if (lex->prepared_stmt_code_is_varref)
636 rlb->append(STRING_WITH_LEN(
"PREPARE "));
637 rlb->append(lex->prepared_stmt_name.str,
638 lex->prepared_stmt_name.length);
639 rlb->append(STRING_WITH_LEN(
" FROM ..."));
654 void mysql_rewrite_query(THD *thd)
656 String *rlb= &thd->rewritten_query;
660 if (thd->lex->contains_plaintext_password)
662 switch(thd->lex->sql_command)
664 case SQLCOM_GRANT: mysql_rewrite_grant(thd, rlb);
break;
665 case SQLCOM_SET_OPTION: mysql_rewrite_set(thd, rlb);
break;
666 case SQLCOM_CREATE_USER: mysql_rewrite_create_user(thd, rlb);
break;
667 case SQLCOM_CHANGE_MASTER: mysql_rewrite_change_master(thd, rlb);
break;
668 case SQLCOM_SLAVE_START: mysql_rewrite_start_slave(thd, rlb);
break;
669 case SQLCOM_CREATE_SERVER: mysql_rewrite_create_server(thd, rlb);
break;
670 case SQLCOM_ALTER_SERVER: mysql_rewrite_alter_server(thd, rlb);
break;
685 case SQLCOM_PREPARE: mysql_rewrite_prepare(thd, rlb);
break;