-
Notifications
You must be signed in to change notification settings - Fork 4k
/
Copy pathsql_rewrite.cc
1952 lines (1714 loc) · 69.5 KB
/
sql_rewrite.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* Copyright (c) 2011, 2024, Oracle and/or its affiliates.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License, version 2.0,
as published by the Free Software Foundation.
This program is designed to work with certain software (including
but not limited to OpenSSL) that is licensed under separate terms,
as designated in a particular file or component or in included license
documentation. The authors of MySQL hereby grant you an additional
permission to link the program and your derivative works with the
separately licensed software that they have either included with
the program or referenced in the documentation.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License, version 2.0, for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
/**
@brief In here, we rewrite queries.
For now, this is only used to obfuscate passwords before we log a
statement. If we ever get other clients for rewriting, we should
introduce a rewrite_flags to determine what kind of rewriting
(password obfuscation etc.) is desired by the client.
Some items in the server can self-print anyway, but many can't.
For instance, you'll see a re-synthesized SELECT in EXPLAIN EXTENDED,
but you won't get a resynthized query in EXPLAIN EXTENDED if you
were explaining an UPDATE.
The following does not claim to be able to re-synthesize every
statement, but attempts to ultimately be able to resynthesize
all statements that have need of rewriting.
Stored procedures may also rewrite their statements (to show the actual
values of their variables etc.). There is currently no scenario where
a statement can be eligible for both rewrites (see sp_instr.cc).
Special consideration will need to be taken if this is intenionally
changed at a later date. (There is an ASSERT() in place that will
hopefully catch unintentional changes.)
Finally, sp_* have code to print a stored program for use by
SHOW PROCEDURE CODE / SHOW FUNCTION CODE.
Thus, regular query parsing comes through here for logging.
So does prepared statement logging.
Stored instructions of the sp_instr_stmt type (which should
be the only ones to contain passwords, and therefore at this
time be eligible for rewriting) go through the regular parsing
facilities and therefore also come through here for logging
(other sp_instr_* types don't).
Finally, as rewriting goes, by default we replace the password with a literal
\<secret\>, with *no* quotation marks so the statement would fail if the
user were to cut & paste it without filling in the real password. This
default behavior is ON for rewriting to the textual logs. For instance :
General, slow query and audit log. Rewriters also have a provision to
replace the password with its hash where we have the latter. (so they
could be replayed, IDENTIFIED WITH \<plugin_name\> AS \<hash\>);
This hash is needed while writing the statements for binlog.
*/
#include "sql/sql_rewrite.h"
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <algorithm>
#include <memory>
#include <set>
#include <string>
#include "lex_string.h"
#include "my_compiler.h"
#include "my_dbug.h"
#include "my_inttypes.h"
#include "mysql/strings/m_ctype.h"
#include "prealloced_array.h"
#include "sql/auth/auth_acls.h"
#include "sql/auth/auth_common.h" // GRANT_ACL
#include "sql/auth/authentication_policy.h"
#include "sql/auth/sql_authentication.h"
#include "sql/handler.h"
#include "sql/log_event.h" // append_query_string
#include "sql/rpl_replica.h" // REPLICA_SQL, REPLICA_IO
#include "sql/set_var.h"
#include "sql/sql_admin.h" // Sql_cmd_clone
#include "sql/sql_class.h" // THD
#include "sql/sql_connect.h"
#include "sql/sql_lex.h" // LEX
#include "sql/sql_list.h"
#include "sql/sql_parse.h" // get_current_user
#include "sql/sql_servers.h"
#include "sql/sql_show.h" // append_identifier_*
#include "sql/table.h"
#include "sql_string.h" // String
#include "string_with_len.h"
#include "violite.h"
#ifndef NDEBUG
#define HASH_STRING_WITH_QUOTE \
"$5$BVZy9O>'a+2MH]_?$fpWyabcdiHjfCVqId/quykZzjaA7adpkcen/uiQrtmOK4p4"
#endif
namespace {
/**
Append a comma to given string if item wasn't the first to be added.
@param[in,out] str The string to (maybe) append to.
@param[in,out] comma If true, there are already items in the list.
Always true afterwards.
*/
void comma_maybe(String *str, bool *comma) {
if (*comma)
str->append(STRING_WITH_LEN(", "));
else
*comma = true;
}
/**
Append a key/value pair to a string, with an optional preceding comma.
For numeric values.
@param[in,out] str The string to append to
@param comma Prepend a comma?
@param txt C-string, must end in a space
@param len strlen(txt)
@param val numeric value
@param cond only append if this evaluates to true
@retval false if any subsequent key/value pair would be the first
*/
bool append_int(String *str, bool comma, const char *txt, size_t len, long val,
int cond) {
if (cond) {
String numbuf(42);
comma_maybe(str, &comma);
str->append(txt, len);
str->append(STRING_WITH_LEN(" "));
numbuf.set((longlong)val, &my_charset_bin);
str->append(numbuf);
return true;
}
return comma;
}
/**
Append a key/value pair to a string if the value is non-NULL,
with an optional preceding comma.
@param[in,out] str The string to append to
@param comma Prepend a comma?
@param key C-string: the key, must be non-NULL
@param val C-string: the value
@retval false if any subsequent key/value pair would be the first
*/
bool append_str(String *str, bool comma, const char *key, const char *val) {
if (val) {
comma_maybe(str, &comma);
str->append(key);
str->append(STRING_WITH_LEN(" '"));
str->append(val);
str->append(STRING_WITH_LEN("'"));
return true;
}
return comma;
}
/**
Append the authorization id for the user
@param [in] thd The THD to find the SQL mode
@param [in] user LEX User to retrieve the plugin string
@param [in] comma Separator to be prefixed before adding user info
@param [in, out] str The string in which authID is suffixed
*/
void append_auth_id(const THD *thd, const LEX_USER *user, bool comma,
String *str) {
assert(thd);
String from_user(user->user.str, user->user.length, system_charset_info);
String from_host(user->host.str, user->host.length, system_charset_info);
if (comma) str->append(',');
append_query_string(thd, system_charset_info, &from_user, str);
str->append(STRING_WITH_LEN("@"));
append_query_string(thd, system_charset_info, &from_host, str);
}
/**
Append the authorization id for the user. This quotes auth_id with ` or "
based on the sql_mode set.
@param [in] thd The THD to find the SQL mode
@param [in] user LEX User to retrieve the plugin string
@param [in] comma Separator to be prefixed before adding user info
@param [in, out] str The string in which authID is suffixed
*/
void append_auth_id_identifier(const THD *thd, const LEX_USER *user, bool comma,
String *str) {
assert(thd);
if (comma) str->append(',');
append_auth_id_string(thd, user->user.str, user->user.length, user->host.str,
user->host.length, str);
}
/**
Used with List<>::sort for alphabetic sorting of LEX_USER records
using user,host as keys.
@param l1 A LEX_USER element
@param l2 A LEX_USER element
@retval 1 if n1 > n2
@retval 0 if n1 <= n2
*/
int lex_user_comp(LEX_USER *l1, LEX_USER *l2) {
size_t length = std::min(l1->user.length, l2->user.length);
int key = memcmp(l1->user.str, l2->user.str, length);
if (key == 0 && l1->user.length == l2->user.length) {
length = std::min(l1->host.length, l2->host.length);
key = memcmp(l1->host.str, l2->host.str, length);
if (key == 0 && l1->host.length == l2->host.length) return 0;
}
if (key == 0)
return (l1->user.length > l2->user.length ? 1 : 0);
else
return (key > 0 ? 1 : 0);
}
/**
Util method which does the real rewrite of the SQL statement.
If a Rewriter is available for the specified SQL command then
the rewritten query will be stored in the String rlb; otherwise,
the string will just be cleared.
@param thd The THD to rewrite for.
@param type Purpose of rewriting the query
@param params Wrapper object of parameters in case needed by a SQL
rewriter.
@param[in,out] rlb Buffer to return the rewritten query in.
Will be empty if no rewriting happened.
@retval true If the Query is re-written.
@retval false Otherwise
*/
bool rewrite_query(THD *thd, Consumer_type type, const Rewrite_params *params,
String &rlb) {
DBUG_TRACE;
std::unique_ptr<I_rewriter> rw = nullptr;
bool rewrite = false;
rlb.length(0);
switch (thd->lex->sql_command) {
case SQLCOM_GRANT:
rw.reset(new Rewriter_grant(thd, type, params));
break;
case SQLCOM_SET_PASSWORD:
rw.reset(new Rewriter_set_password(thd, type, params));
break;
case SQLCOM_SET_OPTION:
rw.reset(new Rewriter_set(thd, type));
break;
case SQLCOM_CREATE_USER:
rw.reset(new Rewriter_create_user(thd, type));
break;
case SQLCOM_ALTER_USER:
rw.reset(new Rewriter_alter_user(thd, type));
break;
case SQLCOM_SHOW_CREATE_USER:
rw.reset(new Rewriter_show_create_user(thd, type, params));
break;
case SQLCOM_CHANGE_REPLICATION_SOURCE:
rw.reset(new Rewriter_change_replication_source(thd, type));
break;
case SQLCOM_REPLICA_START:
rw.reset(new Rewriter_replica_start(thd, type));
break;
case SQLCOM_CREATE_SERVER:
rw.reset(new Rewriter_create_server(thd, type));
break;
case SQLCOM_ALTER_SERVER:
rw.reset(new Rewriter_alter_server(thd, type));
break;
case SQLCOM_SELECT: {
if (thd->lex->export_result_to_object_storage()) {
rw.reset(new Rewriter_select_query(thd, type));
}
break;
}
case SQLCOM_CREATE_PROCEDURE:
case SQLCOM_CREATE_FUNCTION: {
if (type == Consumer_type::TEXTLOG) {
rw.reset(new Rewriter_create_procedure(thd, type));
}
break;
}
case SQLCOM_CREATE_TABLE:
if (type == Consumer_type::TEXTLOG) {
rw.reset(new Rewriter_create_table(thd, type));
}
break;
case SQLCOM_ALTER_TABLE:
if (type == Consumer_type::TEXTLOG) {
rw.reset(new Rewriter_alter_table(thd, type));
}
break;
/*
PREPARE stmt FROM <string> is rewritten so that <string> is
not logged. The statement in <string> will in turn be logged
by the prepare and the execute functions in sql_prepare.cc.
They do call rewrite so they can safely log the statement,
but when they call us, it'll be with sql_command set to reflect
the statement in question, not SQLCOM_PREPARE or SQLCOM_EXECUTE.
Therefore, there is no SQLCOM_EXECUTE case here, and all
SQLCOM_PREPARE does is remove <string>; the "other half",
i.e. printing what string we prepare from happens when the
prepare function calls the logger (and comes by here with
sql_command set to the command being prepared).
*/
case SQLCOM_PREPARE:
rw.reset(new Rewriter_prepare(thd, type));
break;
case SQLCOM_START_GROUP_REPLICATION:
rw.reset(new Rewriter_start_group_replication(thd, type));
break;
case SQLCOM_CLONE: {
rw.reset(new Rewriter_clone(thd, type));
break;
}
default: /* unhandled query types are legal. */
break;
}
if (rw) rewrite = rw->rewrite(rlb);
return rewrite;
}
} // anonymous namespace
/**
Provides the default interface to rewrite the SQL statements to
to obfuscate passwords.
The query aimed to be rewritten in the usual log files
(i.e. General, slow query and audit log) uses default value of
type which is Consumer_type::TEXTLOG
Side-effects:
- thd->m_rewritten_query will contain a rewritten query,
or be cleared if no rewriting took place.
LOCK_thd_query will be temporarily acquired to make that change.
@note Keep in mind that these side-effects will only happen when
calling this top-level function, but not when calling
individual sub-functions directly!
@param thd The THD to rewrite for.
@param type Purpose of rewriting the query
Consumer_type::TEXTLOG
To rewrite the query either for general, slow query
and audit log.
Consumer_type::BINLOG
To rewrite the query for binlogs.
Consumer_type::STDOUT
To rewrite the query for standard output.
@param params Wrapper object of parameters in case needed by a SQL
rewriter.
*/
void mysql_rewrite_query(THD *thd, Consumer_type type,
const Rewrite_params *params) {
String rlb;
DBUG_TRACE;
assert(thd);
// We should not come through here twice for the same query.
assert(thd->rewritten_query().length() == 0);
if (thd->lex->contains_plaintext_password ||
thd->lex->is_rewrite_required()) {
rewrite_query(thd, type, params, rlb);
if (rlb.length() > 0) thd->swap_rewritten_query(rlb);
// The previous rewritten query is in rlb now, which now goes out of scope.
}
}
/**
Provides the default interface to rewrite the ACL query.
@param thd The THD to rewrite for.
@param rlb Buffer to return rewritten query in (if any) if
do_ps_instrument is false.
@param type Purpose of rewriting the query
Consumer_type::TEXTLOG
To rewrite the query either for general, slow query
and audit log.
Consumer_type::BINLOG
To rewrite the query for binlogs.
Consumer_type::STDOUT
To rewrite the query for standard output.
@param params Wrapper object of parameters in case needed by a SQL
rewriter.
@param do_ps_instrument flag to indicate if the query has to be instrumented
in the PSI. Default value is true.
If instrumented, the previous
*/
void mysql_rewrite_acl_query(THD *thd, String &rlb, Consumer_type type,
const Rewrite_params *params /* = nullptr */,
bool do_ps_instrument /* = true */) {
if (rewrite_query(thd, type, params, rlb) && (rlb.length() > 0) &&
do_ps_instrument) {
thd->swap_rewritten_query(rlb);
/*
Queries rewritten for Consumer_type::BINLOG may contain
sensitive informations, encoded in the binlog event.
Do not print these.
A subsequent call, using Consumer_type::TEXTLOG,
will display a proper sanitized query.
*/
if (type != Consumer_type::BINLOG) {
thd->set_query_for_display(thd->rewritten_query().ptr(),
thd->rewritten_query().length());
}
/*
rlb now contains the previous rewritten query.
We clear it here both to save memory and to prevent possible confusion.
*/
rlb.mem_free();
}
}
I_rewriter::I_rewriter(THD *thd, Consumer_type type)
: m_thd(thd), m_consumer_type(type) {
assert(thd);
}
I_rewriter::~I_rewriter() = default;
/**
Reset the previous consumer type.
@param [in] type new consumer type for which query is to be rewritten
*/
void I_rewriter::set_consumer_type(Consumer_type type) {
m_consumer_type = type;
}
/**
Return the current consumer type set in the object
@retval Consumer type set currently.
*/
Consumer_type I_rewriter::consumer_type() { return m_consumer_type; }
/* Constructor */
Rewriter_user::Rewriter_user(THD *thd, Consumer_type type)
: I_rewriter(thd, type) {}
/**
Appends the essential clauses for SHOW CREATE|CREATE|ALTER USER statements
in the buffer rlb.
@param[in,out] rlb Buffer to return the rewritten query in.
@retval false Since it does the partial query rewrite.
Must be called through derived classes rewrite().
*/
bool Rewriter_user::rewrite(String &rlb) const {
LEX *lex = m_thd->lex;
rewrite_users(lex, &rlb);
rewrite_default_roles(lex, &rlb);
rewrite_ssl_properties(lex, &rlb);
rewrite_user_resources(lex, &rlb);
rewrite_password_expired(lex, &rlb);
rewrite_account_lock(lex, &rlb);
rewrite_password_history(lex, &rlb);
rewrite_password_reuse(lex, &rlb);
rewrite_password_require_current(lex, &rlb);
rewrite_account_lock_state(lex, &rlb);
rewrite_user_application_user_metadata(lex, &rlb);
return false;
}
/**
Use the LEX for reconstructing the ATTRIBUTE or COMMENT clause.
@param [in] lex LEX struct to know if the clause was specified
@param [in, out] str The string in which the clause is suffixed
*/
void Rewriter_user::rewrite_in_memory_user_application_user_metadata(
const LEX *lex, String *str) const {
if (lex->alter_user_attribute ==
enum_alter_user_attribute::ALTER_USER_ATTRIBUTE) {
str->append(" ATTRIBUTE ");
} else if (lex->alter_user_attribute ==
enum_alter_user_attribute::ALTER_USER_COMMENT) {
str->append(" COMMENT ");
}
if (lex->alter_user_attribute !=
enum_alter_user_attribute::ALTER_USER_COMMENT_NOT_USED) {
String comment_text(lex->alter_user_comment_text.str,
lex->alter_user_comment_text.length,
system_charset_info);
append_query_string(m_thd, system_charset_info, &comment_text, str);
}
}
/**
Default implementation of the the rewriter for user applicatiton
user metadata.
@param [in] lex LEX struct to know if the clause was specified
@param [in, out] str The string in which the clause is suffixed
*/
void Rewriter_create_user::rewrite_user_application_user_metadata(
const LEX *lex, String *str) const {
parent::rewrite_in_memory_user_application_user_metadata(lex, str);
}
/**
Default implementation of the the rewriter for user applicatiton
user metadata.
@param [in] lex LEX struct to know if the clause was specified
@param [in, out] str The string in which the clause is suffixed
*/
void Rewriter_alter_user::rewrite_user_application_user_metadata(
const LEX *lex, String *str) const {
parent::rewrite_in_memory_user_application_user_metadata(lex, str);
}
/**
Append the literal \<secret\> in place of password to the output string
@param [in, out] str The string in which literal value is suffixed
*/
void Rewriter_user::append_literal_secret(String *str) const {
str->append(STRING_WITH_LEN("<secret>"));
}
/**
Append the password hash to the output string
@param [in] user LEX_USER to fetch the auth string of it.
@param [in, out] str The string in which hash value is suffixed
*/
void Rewriter_user::append_auth_str(LEX_USER *user, String *str) const {
String from_auth(user->first_factor_auth_info.auth.str,
user->first_factor_auth_info.auth.length,
system_charset_info);
append_query_string(m_thd, system_charset_info, &from_auth, str);
}
/**
Append the SSL clause for users iff it is specified
@param [in] lex LEX struct to check if clause is specified
@param [in, out] str The string in which clause is suffixed
*/
void Rewriter_user::rewrite_ssl_properties(const LEX *lex, String *str) const {
if (lex->ssl_type != SSL_TYPE_NOT_SPECIFIED) {
str->append(STRING_WITH_LEN(" REQUIRE"));
switch (lex->ssl_type) {
case SSL_TYPE_SPECIFIED:
if (lex->x509_subject) {
str->append(STRING_WITH_LEN(" SUBJECT '"));
str->append(lex->x509_subject);
str->append(STRING_WITH_LEN("'"));
}
if (lex->x509_issuer) {
str->append(STRING_WITH_LEN(" ISSUER '"));
str->append(lex->x509_issuer);
str->append(STRING_WITH_LEN("'"));
}
if (lex->ssl_cipher) {
str->append(STRING_WITH_LEN(" CIPHER '"));
str->append(lex->ssl_cipher);
str->append(STRING_WITH_LEN("'"));
}
break;
case SSL_TYPE_X509:
str->append(STRING_WITH_LEN(" X509"));
break;
case SSL_TYPE_ANY:
str->append(STRING_WITH_LEN(" SSL"));
break;
case SSL_TYPE_NONE:
str->append(STRING_WITH_LEN(" NONE"));
break;
default:
assert(false);
break;
}
}
}
/**
Append the user resource clauses for users
@param [in] lex LEX struct to check if clause is specified
@param [in, out] str The string in which clause is suffixed
*/
void Rewriter_user::rewrite_user_resources(const LEX *lex, String *str) const {
if (lex->mqh.specified_limits) {
str->append(" WITH");
append_int(str, false, STRING_WITH_LEN(" MAX_QUERIES_PER_HOUR"),
lex->mqh.questions,
lex->mqh.specified_limits & USER_RESOURCES::QUERIES_PER_HOUR);
append_int(str, false, STRING_WITH_LEN(" MAX_UPDATES_PER_HOUR"),
lex->mqh.updates,
lex->mqh.specified_limits & USER_RESOURCES::UPDATES_PER_HOUR);
append_int(
str, false, STRING_WITH_LEN(" MAX_CONNECTIONS_PER_HOUR"),
lex->mqh.conn_per_hour,
lex->mqh.specified_limits & USER_RESOURCES::CONNECTIONS_PER_HOUR);
append_int(str, false, STRING_WITH_LEN(" MAX_USER_CONNECTIONS"),
lex->mqh.user_conn,
lex->mqh.specified_limits & USER_RESOURCES::USER_CONNECTIONS);
}
}
/**
Append the ACCOUNT LOCK clause for users iff it is specified
@param [in] lex LEX struct to check if clause is specified
@param [in, out] str The string in which clause is suffixed
*/
void Rewriter_user::rewrite_account_lock(const LEX *lex, String *str) const {
if (lex->alter_password.update_account_locked_column) {
if (lex->alter_password.account_locked) {
str->append(STRING_WITH_LEN(" ACCOUNT LOCK"));
} else {
str->append(STRING_WITH_LEN(" ACCOUNT UNLOCK"));
}
}
}
/**
Append the PASSWORD EXPIRE clause for users iff it is specified
@param [in] lex LEX struct to check if clause is specified
@param [in, out] str The string in which clause is suffixed
*/
void Rewriter_user::rewrite_password_expired(const LEX *lex,
String *str) const {
if (lex->alter_password.update_password_expired_fields) {
if (lex->alter_password.update_password_expired_column) {
str->append(STRING_WITH_LEN(" PASSWORD EXPIRE"));
} else if (lex->alter_password.expire_after_days) {
append_int(str, false, STRING_WITH_LEN(" PASSWORD EXPIRE INTERVAL"),
lex->alter_password.expire_after_days, true);
str->append(STRING_WITH_LEN(" DAY"));
} else if (lex->alter_password.use_default_password_lifetime) {
str->append(STRING_WITH_LEN(" PASSWORD EXPIRE DEFAULT"));
} else {
str->append(STRING_WITH_LEN(" PASSWORD EXPIRE NEVER"));
}
}
}
/**
Append the PASSWORD REQUIRE CURRENT clause for users
@param [in] lex LEX struct to know if the clause was specified
@param [in, out] str The string in which the clause is suffixed
*/
void Rewriter_user::rewrite_password_require_current(LEX *lex,
String *str) const {
switch (lex->alter_password.update_password_require_current) {
case Lex_acl_attrib_udyn::YES:
str->append(STRING_WITH_LEN(" PASSWORD REQUIRE CURRENT"));
break;
case Lex_acl_attrib_udyn::DEFAULT:
str->append(STRING_WITH_LEN(" PASSWORD REQUIRE CURRENT DEFAULT"));
break;
case Lex_acl_attrib_udyn::NO:
str->append(STRING_WITH_LEN(" PASSWORD REQUIRE CURRENT OPTIONAL"));
break;
case Lex_acl_attrib_udyn::UNCHANGED:
// Do nothing
break;
default:
assert(false);
}
}
/**
Append the account lock state
Append FAILED_LOGIN_ATTEMPTS/PASSWORD_LOCK_TIME if account auto-lock
is active.
@param [in] lex LEX to retrieve data from
@param [in, out] str The string in which plugin info is suffixed
*/
void Rewriter_user::rewrite_account_lock_state(LEX *lex, String *str) const {
if (lex->alter_password.update_failed_login_attempts) {
append_int(str, false, STRING_WITH_LEN(" FAILED_LOGIN_ATTEMPTS"),
lex->alter_password.failed_login_attempts, true);
}
if (lex->alter_password.update_password_lock_time) {
if (lex->alter_password.password_lock_time >= 0)
append_int(str, false, STRING_WITH_LEN(" PASSWORD_LOCK_TIME"),
lex->alter_password.password_lock_time, true);
else
str->append(STRING_WITH_LEN(" PASSWORD_LOCK_TIME UNBOUNDED"));
}
}
/**
Append the authentication plugin name for the user
@param [in] user LEX User to retrieve the plugin string
@param [in, out] str The string in which plugin info is suffixed
*/
void Rewriter_user::append_plugin_name(const LEX_USER *user,
String *str) const {
str->append(STRING_WITH_LEN(" WITH "));
if (user->first_factor_auth_info.plugin.length > 0) {
String from_plugin(user->first_factor_auth_info.plugin.str,
user->first_factor_auth_info.plugin.length,
system_charset_info);
append_query_string(m_thd, system_charset_info, &from_plugin, str);
} else {
std::string default_plugin_name;
authentication_policy::get_first_factor_default_plugin(default_plugin_name);
String default_plugin(default_plugin_name.c_str(),
default_plugin_name.length(), system_charset_info);
append_query_string(m_thd, system_charset_info, &default_plugin, str);
}
}
/**
Append the authentication plugin name from LEX_MFA for the user
@param [in] user User to retrieve the plugin string
@param [in, out] str The string in which plugin info is suffixed
*/
void Rewriter_user::append_mfa_plugin_name(const LEX_MFA *user,
String *str) const {
if (user->plugin.length > 0) {
str->append(STRING_WITH_LEN(" WITH "));
String from_plugin(user->plugin.str, user->plugin.length,
system_charset_info);
append_query_string(m_thd, system_charset_info, &from_plugin, str);
}
}
/**
Append the authentication string from LEX_MFA for the user
@param [in] user User to retrieve the plugin string
@param [in, out] str The string in which plugin info is suffixed
*/
void Rewriter_user::append_mfa_auth_str(const LEX_MFA *user,
String *str) const {
if (user->uses_identified_by_clause &&
m_consumer_type == Consumer_type::TEXTLOG) {
str->append(STRING_WITH_LEN(" BY "));
append_literal_secret(str);
} else if (user->auth.length > 0) {
str->append(STRING_WITH_LEN(" AS "));
String auth_str(user->auth.str, user->auth.length, system_charset_info);
append_query_string(m_thd, system_charset_info, &auth_str, str);
}
}
/**
The default implementation is to append the PASSWORD HISTORY clause iff it
is specified. Though concrete classes may add their own implementation.
@param [in] lex LEX struct to check if clause is specified
@param [in, out] str The string in which clause is suffixed
*/
void Rewriter_user::rewrite_password_history(const LEX *lex,
String *str) const {
if (lex->alter_password.use_default_password_history) {
str->append(STRING_WITH_LEN(" PASSWORD HISTORY DEFAULT"));
} else {
append_int(str, false, STRING_WITH_LEN(" PASSWORD HISTORY"),
lex->alter_password.password_history_length, true);
}
}
/**
The default implementation is to append the PASSWORD REUSE clause iff it is
specified. Though concrete classes may add their own implementation.
@param [in] lex LEX struct to check if clause is specified
@param [in, out] str The string in which clause is suffixed
*/
void Rewriter_user::rewrite_password_reuse(const LEX *lex, String *str) const {
if (lex->alter_password.use_default_password_reuse_interval) {
str->append(STRING_WITH_LEN(" PASSWORD REUSE INTERVAL DEFAULT"));
} else {
append_int(str, false, STRING_WITH_LEN(" PASSWORD REUSE INTERVAL"),
lex->alter_password.password_reuse_interval, true);
str->append(STRING_WITH_LEN(" DAY"));
}
}
/**
Fetch the users from user_list in LEX struct and append them to the String.
@param [in] lex LEX struct to check if clause is specified
@param [in, out] str The string in which clause is suffixed
*/
void Rewriter_user::rewrite_users(LEX *lex, String *str) const {
bool comma = false;
LEX_USER *user_name, *tmp_user_name;
List_iterator<LEX_USER> user_list(lex->users_list);
while ((tmp_user_name = user_list++)) {
if ((user_name = get_current_user(m_thd, tmp_user_name))) {
append_user_auth_info(user_name, comma, str);
comma = true;
}
}
}
/**
Append the DEFAULT ROLE clause for users iff it is specified
@param [in] lex LEX struct to check if clause is specified
@param [in, out] str The string in which clause is suffixed
*/
void Rewriter_user::rewrite_default_roles(const LEX *lex, String *str) const {
bool comma = false;
if (lex->default_roles && lex->default_roles->elements > 0) {
str->append(" DEFAULT ROLE ");
lex->default_roles->sort(&lex_user_comp);
List_iterator<LEX_USER> role_it(*(lex->default_roles));
LEX_USER *role;
while ((role = role_it++)) {
if (comma) str->append(',');
str->append(create_authid_str_from(role).c_str());
comma = true;
}
}
}
Rewriter_create_user::Rewriter_create_user(THD *thd, Consumer_type type)
: Rewriter_user(thd, type) {}
/**
Rewrite the query for the CREATE USER statement.
@param[in,out] rlb Buffer to return the rewritten query in.
@retval true The query was rewritten.
*/
bool Rewriter_create_user::rewrite(String &rlb) const {
LEX *lex = m_thd->lex;
rlb.append("CREATE USER ");
if (lex->create_info->options & HA_LEX_CREATE_IF_NOT_EXISTS)
rlb.append("IF NOT EXISTS ");
parent::rewrite(rlb);
return true;
}
/**
Append the authID, plugin and auth str of the user to output string :
- If the corresponding clause is specified.
- Always add the plugin info for the target type BINLOG
- Add the literal \<secret\> in place of plain text password
for the target type LOG
@param [in] user Lex user to fetch the info
@param [in] comma separator to be prefixed while appending user info
@param [in, out] str String to which user auth info is suffixed.
*/
void Rewriter_create_user::append_user_auth_info(LEX_USER *user, bool comma,
String *str) const {
append_auth_id(m_thd, user, comma, str);
List_iterator<LEX_MFA> mfa_list(user->mfa_list);
LEX_MFA *tmp_mfa;
if (user->with_initial_auth) {
/*
In case of passwordless user, server fiddles with user specified syntax,
thus we are expected to write the original syntax.
ex:
CREATE USER foo IDENTIFIED WITH authentication_webauthn INITIAL
AUTHENTICATION IDENTIFIED BY 'abc'; above sql is converted by server as:
CREATE USER foo IDENTIFIED BY 'abc' AND IDENTIFIED WITH
authentication_webauthn;
This block ensures that query is rewritten in logs as:
CREATE USER foo IDENTIFIED WITH authentication_webauthn INITIAL
AUTHENTICATION IDENTIFIED WITH <default auth plugin> AS
<auth_hash_string>;
*/
assert(user->mfa_list.size());
/* point to 2nd factor which is authentication_webauthn */
tmp_mfa = mfa_list++;
str->append(STRING_WITH_LEN(" IDENTIFIED"));
append_mfa_plugin_name(tmp_mfa, str);
str->append(STRING_WITH_LEN(" INITIAL AUTHENTICATION"));
}
if (user->first_factor_auth_info.uses_identified_by_clause ||
user->first_factor_auth_info.uses_identified_with_clause ||
user->first_factor_auth_info.uses_authentication_string_clause ||
m_consumer_type == Consumer_type::BINLOG) {
str->append(STRING_WITH_LEN(" IDENTIFIED"));
if (user->first_factor_auth_info.uses_identified_with_clause ||
m_consumer_type == Consumer_type::BINLOG)
append_plugin_name(user, str);
if (user->first_factor_auth_info.uses_identified_by_clause &&
m_consumer_type == Consumer_type::TEXTLOG) {
str->append(STRING_WITH_LEN(" BY "));
append_literal_secret(str);
} else if (user->first_factor_auth_info.auth.length > 0) {
str->append(STRING_WITH_LEN(" AS "));
append_auth_str(user, str);
}
}
if (user->mfa_list.size() && !user->with_initial_auth) {
while ((tmp_mfa = mfa_list++)) {
str->append(STRING_WITH_LEN(" AND IDENTIFIED"));
append_mfa_plugin_name(tmp_mfa, str);
append_mfa_auth_str(tmp_mfa, str);
}
}
}
/**
Append the PASSWORD HISTORY clause for users iff it is specified
@param [in] lex LEX struct to check if clause is specified
@param [in, out] str The string in which clause is suffixed
*/
void Rewriter_create_user::rewrite_password_history(const LEX *lex,
String *str) const {
if (lex->alter_password.update_password_history) {
parent::rewrite_password_history(lex, str);
}
}
/**
Append the PASSWORD REUSE clause for users iff it is specified
@param [in] lex LEX struct to check if clause is specified
@param [in, out] str The string in which clause is suffixed
*/
void Rewriter_create_user::rewrite_password_reuse(const LEX *lex,
String *str) const {
if (lex->alter_password.update_password_reuse_interval) {
parent::rewrite_password_reuse(lex, str);
}
}
Rewriter_alter_user::Rewriter_alter_user(THD *thd, Consumer_type type)
: Rewriter_user(thd, type) {}
/**
Rewrite the query for the ALTER USER statement.
@param[in,out] rlb Buffer to return the rewritten query in.
@retval true The query was rewritten.
*/
bool Rewriter_alter_user::rewrite(String &rlb) const {
LEX *lex = m_thd->lex;
rlb.append("ALTER USER ");
if (lex->drop_if_exists) rlb.append("IF EXISTS ");
parent::rewrite(rlb);
return true;
}
/**
Append the authID, plugin and auth str of the user to output string :
- If the corresponding clause is specified.
- Always add the plugin info for the target type BINLOG
- Add the literal \<secret\> in place of plain text password
for the target type LOG
@param [in] user Lex user to fetch the info
@param [in] comma separator to be prefixed while appending user info
@param [in, out] str String to which user auth info is suffixed.
*/
void Rewriter_alter_user::append_user_auth_info(LEX_USER *user, bool comma,
String *str) const {
append_auth_id(m_thd, user, comma, str);
if (user->first_factor_auth_info.uses_identified_by_clause ||
user->first_factor_auth_info.uses_identified_with_clause ||
user->first_factor_auth_info.uses_authentication_string_clause) {
str->append(STRING_WITH_LEN(" IDENTIFIED"));
if (user->first_factor_auth_info.uses_identified_with_clause ||
m_consumer_type == Consumer_type::BINLOG)
append_plugin_name(user, str);
if (user->first_factor_auth_info.uses_identified_by_clause &&
m_consumer_type == Consumer_type::TEXTLOG) {
str->append(STRING_WITH_LEN(" BY "));
append_literal_secret(str);
/* Add the literal value in place of current password in the textlogs. */
if (user->uses_replace_clause) {
str->append(STRING_WITH_LEN(" REPLACE <secret>"));
}
} else if (user->first_factor_auth_info.auth.length > 0) {
str->append(STRING_WITH_LEN(" AS "));
append_auth_str(user, str);
}
if (user->retain_current_password) {
str->append(STRING_WITH_LEN(" RETAIN CURRENT PASSWORD"));