-
Notifications
You must be signed in to change notification settings - Fork 4k
/
Copy pathacl_table_user.cc
2353 lines (2100 loc) · 85.6 KB
/
acl_table_user.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) 2018, 2025, 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 */
#include "sql/auth/acl_table_user.h" /* For user table data */
#include <stdlib.h> /* atoi */
#include <string.h> /* strlen, strcmp, NULL, memcmp, memcpy */
#include <algorithm> /* sort */
#include <map> /* map */
#include "field_types.h" /* MYSQL_TYPE_ENUM, MYSQL_TYPE_JSON */
#include "lex_string.h" /* LEX_CSTRING */
#include "my_base.h" /* HA_ERR_* */
#include "my_dbug.h" /* DBUG macros */
#include "my_inttypes.h" /* MYF, uchar, longlong, ulonglong */
#include "my_sqlcommand.h" /* SQLCOM_ALTER_USER, SQLCOM_GRANT */
#include "my_sys.h" /* my_error */
#include "mysql/components/services/bits/psi_bits.h" /* PSI_NOT_INSTRUMENTED */
#include "mysql/components/services/log_builtins.h" /* for LogEvent, LogErr */
#include "mysql/my_loglevel.h" /* WARNING_LEVEL */
#include "mysql/plugin.h" /* st_mysql_plugin, MYSQL_AUTHENTICATION_PLUGIN */
#include "mysql/plugin_auth.h" /* st_mysql_auth */
#include "mysql/strings/m_ctype.h" /* my_charset_* */
#include "mysql_time.h" /* MYSQL_TIME, MYSQL_TIMESTAMP_ERROR */
#include "mysqld_error.h" /* ER_* */
#include "prealloced_array.h" /* Prealloced_array */
#include "sql/auth/auth_acls.h" /* ACLs */
#include "sql/auth/auth_common.h" /* User_table_schema, ... */
#include "sql/auth/auth_internal.h" /* acl_print_ha_error */
#include "sql/auth/partial_revokes.h"
#include "sql/auth/sql_auth_cache.h" /* global_acl_memory */
#include "sql/auth/sql_authentication.h" /* Cached_authentication_plugins */
#include "sql/auth/sql_user_table.h" /* Acl_table_intact */
#include "sql/auth/user_table.h" /* replace_user_table */
#include "sql/field.h" /* Field, Field_json, Field_enum, TYPE_OK */
#include "sql/handler.h" /* handler, handlerton */
#include "sql/item_func.h" /* mqh_used */
#include "sql/iterators/row_iterator.h" /* RowIterator */
#include "sql/key.h" /* key_copy, KEY */
#include "sql/mysqld.h" /* specialflag */
#include "sql/sql_class.h" /* THD */
#include "sql/sql_const.h" /* ACL_ALLOC_BLOCK_SIZE, MAX_KEY_LENGTH */
#include "sql/sql_executor.h"
#include "sql/sql_lex.h" /* LEX */
#include "sql/sql_plugin.h" /* plugin_unlock, my_plugin_lock_by_name */
#include "sql/sql_plugin_ref.h" /* plugin_decl, plugin_ref */
#include "sql/sql_time.h" /* str_to_time_with_warn */
#include "sql/sql_update.h" /* compare_records */
#include "sql/system_variables.h" /* System_variables */
#include "sql/table.h" /* TABLE, TABLE_SHARE, ... */
#include "sql/tztime.h" /* Time_zone */
#include "sql_string.h" /* String */
#include "string_with_len.h" /* STRING_WITH_LEN */
#include "template_utils.h" /* down_cast */
#include "typelib.h" /* TYPELIB */
#include "violite.h" /* SSL_* */
#define INVALID_DATE "0000-00-00 00:00:00"
namespace consts {
/** Initial timestamp */
const my_timeval BEGIN_TIMESTAMP = {0, 0};
/** Error indicating table operation error */
const int CRITICAL_ERROR = -1;
/** Empty string */
const std::string empty_string("");
/* Name of the fields in mysql.user.user_attributes */
/** For secondary password */
const std::string additional_password("additional_password");
/** For partial revokes */
const std::string Restrictions("Restrictions");
/** for password locking */
const std::string Password_locking("Password_locking");
/** underkeys of password locking */
const std::string failed_login_attempts("failed_login_attempts");
/** underkeys of password locking */
const std::string password_lock_time_days("password_lock_time_days");
/** metadata tag */
const std::string json_metadata_tag("metadata");
/** comment tag */
const std::string json_comment_tag("comment");
/** multi factor authentication methods */
const std::string json_multi_factor_authentication(
"multi_factor_authentication");
} // namespace consts
static bool replace_user_metadata(const std::string &json_blob,
bool expect_text, TABLE *user_table);
namespace acl_table {
/** Keys used in mysql.user.user_attributes */
static std::map<const User_attribute_type, const std::string>
attribute_type_to_str = {
{User_attribute_type::ADDITIONAL_PASSWORD, consts::additional_password},
{User_attribute_type::RESTRICTIONS, consts::Restrictions},
{User_attribute_type::PASSWORD_LOCKING, consts::Password_locking},
{User_attribute_type::METADATA, consts::json_metadata_tag},
{User_attribute_type::COMMENT, consts::json_comment_tag},
{User_attribute_type::MULTI_FACTOR_AUTHENTICATION_DATA,
consts::json_multi_factor_authentication}};
Acl_user_attributes::Acl_user_attributes(MEM_ROOT *mem_root,
bool read_restrictions,
Auth_id &auth_id,
Access_bitmask global_privs)
: m_mem_root(mem_root),
m_read_restrictions(read_restrictions),
m_auth_id(auth_id),
m_additional_password(),
m_restrictions(),
m_global_privs(global_privs),
m_password_lock(),
m_mfa(nullptr),
m_user_attributes_json(nullptr) {}
Acl_user_attributes::Acl_user_attributes(MEM_ROOT *mem_root,
bool read_restrictions,
Auth_id &auth_id,
Restrictions *restrictions,
I_multi_factor_auth *mfa)
: Acl_user_attributes(mem_root, read_restrictions, auth_id, ALL_ACCESS) {
if (restrictions) m_restrictions = *restrictions;
m_mfa = mfa;
}
Acl_user_attributes::~Acl_user_attributes() { m_restrictions.clear_db(); }
bool Acl_user_attributes::consume_user_attributes_json(Json_dom_ptr json) {
if (!json || json->json_type() != enum_json_type::J_OBJECT) {
json = create_dom_ptr<Json_object>();
if (!json) return true;
}
Json_object *ob = down_cast<Json_object *>(json.get());
Json_dom *metadata =
ob->get(attribute_type_to_str[User_attribute_type::METADATA]);
if (metadata) {
Json_object *metadata_ob = down_cast<Json_object *>(metadata);
m_user_attributes_json = create_dom_ptr<Json_object>();
Json_object *user_attributes_ob =
down_cast<Json_object *>(m_user_attributes_json.get());
user_attributes_ob->add_clone(
attribute_type_to_str[User_attribute_type::METADATA], metadata_ob);
}
return false;
}
void Acl_user_attributes::report_and_remove_invalid_db_restrictions(
DB_restrictions &db_restrictions, Access_bitmask mask, enum loglevel level,
ulonglong errcode) {
if (!db_restrictions.is_empty()) {
for (auto &itr : db_restrictions()) {
Access_bitmask privs = itr.second;
if (privs != (privs & mask)) {
std::string invalid_privs;
std::string separator(", ");
bool second = false;
Access_bitmask filtered_privs = privs & ~mask;
if (filtered_privs)
db_restrictions.remove(itr.first.c_str(), filtered_privs);
while (filtered_privs != 0) {
std::string one_priv = get_one_priv(filtered_privs);
if (one_priv.length()) {
if (second) invalid_privs.append(separator);
invalid_privs.append(one_priv);
if (!second) second = true;
}
}
if (!invalid_privs.length())
invalid_privs.append("<unknown_privileges>");
std::string auth_id;
m_auth_id.auth_str(&auth_id);
LogErr(level, errcode, auth_id.c_str(), invalid_privs.c_str(),
itr.first.length() ? itr.first.c_str() : "<invalid_database>");
}
}
/*
Now, remove the databases with no restrictions without invalidating
the internal container of DB_restrictions
*/
db_restrictions.remove(0);
}
}
bool Acl_user_attributes::deserialize_multi_factor(
const Json_object &json_object) {
Json_dom *mfa = json_object.get(
attribute_type_to_str
[User_attribute_type::MULTI_FACTOR_AUTHENTICATION_DATA]);
if (mfa) {
if (mfa->json_type() != enum_json_type::J_ARRAY) return true;
Json_array *mfa_arr = down_cast<Json_array *>(mfa);
I_multi_factor_auth *i_mfa =
(mfa_arr->size() ? new (&global_acl_memory)
Multi_factor_auth_list(&global_acl_memory)
: nullptr);
for (uint i = 0; i < mfa_arr->size(); i++) {
i_mfa->add_factor(new (&global_acl_memory)
Multi_factor_auth_info(&global_acl_memory));
Json_dom *mfa_arr_obj = (*mfa_arr)[i];
if (i_mfa->deserialize(i, mfa_arr_obj)) return true;
}
set_mfa(i_mfa);
}
return false;
}
bool Acl_user_attributes::deserialize_password_lock(
const Json_object &json_object) {
/* password locking */
m_password_lock.password_lock_time_days = 0;
m_password_lock.failed_login_attempts = 0;
const Json_dom *password_locking_dom = json_object.get(
attribute_type_to_str[User_attribute_type::PASSWORD_LOCKING]);
if (password_locking_dom) {
if (password_locking_dom->json_type() != enum_json_type::J_OBJECT)
return true;
const Json_object *password_locking =
down_cast<const Json_object *>(password_locking_dom);
const Json_dom *password_lock_time_days_dom =
password_locking->get(consts::password_lock_time_days);
if (password_lock_time_days_dom) {
if (password_lock_time_days_dom->json_type() != enum_json_type::J_INT)
return true;
const Json_int *password_lock_time_days =
down_cast<const Json_int *>(password_lock_time_days_dom);
const longlong val = password_lock_time_days->value();
if (val < -1 || val > INT_MAX) return true;
m_password_lock.password_lock_time_days = val;
}
const Json_dom *failed_login_attempts_dom =
password_locking->get(consts::failed_login_attempts);
if (failed_login_attempts_dom) {
if (failed_login_attempts_dom->json_type() != enum_json_type::J_INT) {
m_password_lock.password_lock_time_days = 0;
return true;
}
const Json_int *failed_login_attempts =
down_cast<const Json_int *>(failed_login_attempts_dom);
const longlong val = failed_login_attempts->value();
if (val < 0 || val > UINT_MAX) {
m_password_lock.password_lock_time_days = 0;
return true;
}
m_password_lock.failed_login_attempts = val;
}
}
return false;
}
bool Acl_user_attributes::deserialize(const Json_object &json_object) {
{
/** Second password */
const Json_dom *additional_password_dom = json_object.get(
attribute_type_to_str[User_attribute_type::ADDITIONAL_PASSWORD]);
if (additional_password_dom) {
if (additional_password_dom->json_type() != enum_json_type::J_STRING)
return true;
const Json_string *additional_password =
down_cast<const Json_string *>(additional_password_dom);
m_additional_password = additional_password->value();
}
}
/* In cse of writes, DB restrictions are always overwritten */
if (m_read_restrictions) {
DB_restrictions db_restrictions;
if (db_restrictions.add(json_object)) return true;
/* Filtering & warnings */
report_and_remove_invalid_db_restrictions(
db_restrictions, DB_OP_ACLS, WARNING_LEVEL,
ER_WARN_INCORRECT_PRIVILEGE_FOR_DB_RESTRICTIONS);
report_and_remove_invalid_db_restrictions(db_restrictions, m_global_privs,
WARNING_LEVEL,
ER_WARN_INVALID_DB_RESTRICTIONS);
m_restrictions.set_db(db_restrictions);
}
if (deserialize_password_lock(json_object)) return true;
if (deserialize_multi_factor(json_object)) return true;
return false;
}
bool Acl_user_attributes::serialize(Json_object &json_object) const {
if (m_additional_password.length()) {
Json_string additional_password(m_additional_password);
if (json_object.add_clone(
attribute_type_to_str[User_attribute_type::ADDITIONAL_PASSWORD],
&additional_password))
return true;
} else if (m_user_attributes_json) {
Json_object *obj = down_cast<Json_object *>(m_user_attributes_json.get());
obj->remove(
attribute_type_to_str[User_attribute_type::ADDITIONAL_PASSWORD]);
}
if (!m_restrictions.db().is_empty()) {
Json_array restrictions_array;
m_restrictions.db().get_as_json(restrictions_array);
if (json_object.add_clone(
attribute_type_to_str[User_attribute_type::RESTRICTIONS],
&restrictions_array))
return true;
} else if (m_user_attributes_json) {
Json_object *obj = down_cast<Json_object *>(m_user_attributes_json.get());
obj->remove(attribute_type_to_str[User_attribute_type::RESTRICTIONS]);
}
if (m_password_lock.password_lock_time_days ||
m_password_lock.failed_login_attempts) {
Json_object password_lock;
Json_int password_lock_time_days(m_password_lock.password_lock_time_days);
Json_int failed_login_attempts(m_password_lock.failed_login_attempts);
password_lock.add_clone(consts::password_lock_time_days,
&password_lock_time_days);
password_lock.add_clone(consts::failed_login_attempts,
&failed_login_attempts);
json_object.add_clone(
attribute_type_to_str[User_attribute_type::PASSWORD_LOCKING],
&password_lock);
} else if (m_user_attributes_json) {
Json_object *obj = down_cast<Json_object *>(m_user_attributes_json.get());
obj->remove(attribute_type_to_str[User_attribute_type::PASSWORD_LOCKING]);
}
if (m_mfa) {
Json_array mfa_arr;
if (m_mfa->serialize(mfa_arr)) return true;
json_object.add_clone(
attribute_type_to_str
[User_attribute_type::MULTI_FACTOR_AUTHENTICATION_DATA],
&mfa_arr);
}
if (m_user_attributes_json) {
Json_dom_ptr copy_attributes = m_user_attributes_json->clone();
Json_object_ptr tmp(down_cast<Json_object *>(copy_attributes.release()));
json_object.merge_patch(std::move(tmp));
}
return false;
}
bool Acl_user_attributes::update_additional_password(std::string &credential) {
if (credential.length()) {
m_additional_password = credential;
} else {
return true;
}
return false;
}
void Acl_user_attributes::discard_additional_password() {
m_additional_password.clear();
}
const std::string Acl_user_attributes::get_additional_password() const {
return m_additional_password;
}
Restrictions Acl_user_attributes::get_restrictions() const {
return m_restrictions;
}
void Acl_user_attributes::update_restrictions(
const Restrictions &restricitions) {
m_restrictions = restricitions;
}
namespace {
/**
Helper function to parse mysql.user.user_attributes column
@param [in] table Handle to mysql.user table
@param [in] table_schema mysql.user schema version
@param [out] user_attributes Deserialized user attributes
@returns status of parsing user_attributes column
@retval false Success
@retval true Problem parsing the column
*/
bool parse_user_attributes(TABLE *table, User_table_schema *table_schema,
Acl_user_attributes &user_attributes) {
// Read only if the column of type JSON and it is not null.
if (table->field[table_schema->user_attributes_idx()]->type() ==
MYSQL_TYPE_JSON &&
!table->field[table_schema->user_attributes_idx()]->is_null()) {
Json_wrapper json_wrapper;
if ((down_cast<Field_json *>(
table->field[table_schema->user_attributes_idx()])
->val_json(&json_wrapper)))
return true;
if (user_attributes.consume_user_attributes_json(json_wrapper.clone_dom()))
return true;
const Json_object *json_object =
down_cast<const Json_object *>(json_wrapper.to_dom());
if (user_attributes.deserialize(*json_object)) return true;
}
return false;
}
} // namespace
Acl_table_user_writer_status::Acl_table_user_writer_status()
: skip_cache_update(true),
updated_rights(NO_ACCESS),
error(consts::CRITICAL_ERROR),
password_change_timestamp(consts::BEGIN_TIMESTAMP),
second_cred(consts::empty_string),
restrictions(),
password_lock(),
multi_factor(nullptr) {}
/**
mysql.user table writer constructor
Note: Table handle must be non-null.
@param [in] thd Thread handle
@param [in] table Handle to mysql.user table
@param [in] combo User information
@param [in] rights Updated global privileges
@param [in] revoke_grant If its REVOKE statement
@param [in] can_create_user Whether user has ability to create new user
@param [in] what_to_update Things to be updated
@param [in] restrictions Restrictions of the user, if there is any
@param [in] mfa Interface pointer to Multi factor authentication
methods
*/
Acl_table_user_writer::Acl_table_user_writer(
THD *thd, TABLE *table, LEX_USER *combo, Access_bitmask rights,
bool revoke_grant, bool can_create_user,
Pod_user_what_to_update what_to_update,
Restrictions *restrictions = nullptr, I_multi_factor_auth *mfa = nullptr)
: Acl_table(thd, table, acl_table::Acl_table_operation::OP_INSERT),
m_has_user_application_user_metadata(false),
m_combo(combo),
m_rights(rights),
m_revoke_grant(revoke_grant),
m_can_create_user(can_create_user),
m_what_to_update(what_to_update),
m_table_schema(nullptr),
m_restrictions(restrictions),
m_mfa(mfa) {
if (table) {
User_table_schema_factory user_table_schema_factory;
m_table_schema = user_table_schema_factory.get_user_table_schema(table);
}
}
/** Cleanup */
Acl_table_user_writer::~Acl_table_user_writer() {
if (m_table_schema) delete m_table_schema;
}
/**
Perform add/update to mysql.user table
@returns status of add/update operation. In case of success it contains
information that's useful for cache update.
*/
Acl_table_user_writer_status Acl_table_user_writer::driver() {
bool builtin_plugin = false;
const bool update_password = (m_what_to_update.m_what & PLUGIN_ATTR);
Table_op_error_code error;
LEX *lex = m_thd->lex;
Acl_table_user_writer_status return_value;
Acl_table_user_writer_status err_return_value;
DBUG_TRACE;
assert(assert_acl_cache_write_lock(m_thd));
/* Setup the table for writing */
if (setup_table(error, builtin_plugin)) {
return_value.error = error;
return return_value;
}
if (m_operation == Acl_table_operation::OP_UPDATE) {
if ((lex->sql_command != SQLCOM_ALTER_USER) && !m_rights &&
lex->ssl_type == SSL_TYPE_NOT_SPECIFIED && !lex->mqh.specified_limits &&
!m_revoke_grant && (!builtin_plugin || !update_password) &&
!m_restrictions) {
DBUG_PRINT("info", ("Dynamic privileges exit path"));
/*
At this point, even though there is no error,
we want to skip updates to cache because it's a no-op.
*/
return_value.error = 0;
return return_value;
}
}
std::string current_password;
if ((m_what_to_update.m_what & USER_ATTRIBUTES) &&
(m_what_to_update.m_user_attributes & USER_ATTRIBUTE_RETAIN_PASSWORD))
current_password = get_current_credentials();
/*
Set in memory copy of Multi factor authentication details. In case ALTER
USER is executed to alter Multi factor authentication attributes,
update_user_attributes call will modify the needed data structures, else in
case of GRANT/REVOKE in memory copy is returned.
*/
if (m_mfa) return_value.multi_factor = m_mfa;
if (update_authentication_info(return_value) ||
update_privileges(return_value) || update_ssl_properties() ||
update_user_attributes(current_password, return_value) ||
update_user_resources() || update_password_expiry() ||
update_password_history() || update_password_reuse() ||
update_password_require_current() || update_account_locking() ||
update_user_application_user_metadata()) {
return err_return_value;
}
(void)finish_operation(error);
if (!error) {
return_value.error = 0;
return_value.skip_cache_update = false;
}
return return_value;
}
/**
Position user table.
Try to find a row matching with given account information. If one is
found, set record pointer to it and set operation type as UPDATE. If no
record is found, then set record pointer to empty record.
Raises error in DA in various cases where sanity of table and
intention of operation is checked.
@param [out] error Table operation error
@param [out] builtin_plugin For existing record, if authentication plugin
is one of the builtins or not.
@returns Operation status
@retval false Table is positioned. In case of insert, it means no record
is found for given (user,host). In case of update, table
is set to point to existing record.
@retval true Error positioning table.
*/
bool Acl_table_user_writer::setup_table(int &error, bool &builtin_plugin) {
const bool update_password = (m_what_to_update.m_what & PLUGIN_ATTR);
switch (m_operation) {
case Acl_table_operation::OP_INSERT:
case Acl_table_operation::OP_UPDATE: {
uchar user_key[MAX_KEY_LENGTH];
Acl_table_intact table_intact(m_thd);
LEX_CSTRING old_plugin;
error = consts::CRITICAL_ERROR;
builtin_plugin = false;
if (table_intact.check(m_table, ACL_TABLES::TABLE_USER)) return true;
m_table->use_all_columns();
assert(m_combo->host.str != nullptr);
m_table->field[m_table_schema->host_idx()]->store(
m_combo->host.str, m_combo->host.length, system_charset_info);
m_table->field[m_table_schema->user_idx()]->store(
m_combo->user.str, m_combo->user.length, system_charset_info);
key_copy(user_key, m_table->record[0], m_table->key_info,
m_table->key_info->key_length);
error = m_table->file->ha_index_read_idx_map(
m_table->record[0], 0, user_key, HA_WHOLE_KEY, HA_READ_KEY_EXACT);
assert(error != HA_ERR_LOCK_DEADLOCK);
assert(error != HA_ERR_LOCK_WAIT_TIMEOUT);
DBUG_EXECUTE_IF("wl7158_replace_user_table_1",
error = HA_ERR_LOCK_DEADLOCK;);
if (error) {
if (error != HA_ERR_KEY_NOT_FOUND && error != HA_ERR_END_OF_FILE) {
acl_print_ha_error(error);
return true;
}
m_operation = Acl_table_operation::OP_INSERT;
/*
The user record wasn't found; if the intention was to revoke
privileges (indicated by what == 'N') then execution must fail
now.
*/
if (m_revoke_grant) {
const bool ret = report_missing_user_grant_message(
m_thd, false, m_combo->user.str, m_combo->host.str, nullptr,
ER_NONEXISTING_GRANT);
/*
Return 1 as an indication that expected error occurred during
handling of REVOKE statement for an unknown user.
*/
if (ret) error = 1;
return ret;
}
if (m_thd->lex->sql_command == SQLCOM_ALTER_USER) {
/* Entry should have existsed since this is ALTER USER */
error = 1;
return true;
}
optimize_plugin_compare_by_pointer(
&m_combo->first_factor_auth_info.plugin);
builtin_plugin =
auth_plugin_is_built_in(m_combo->first_factor_auth_info.plugin.str);
/* The user record was neither present nor the intention was to
* create it */
if (!m_can_create_user) {
if (!update_password) {
/* Have come here to GRANT privilege to the non-existing user */
my_error(ER_CANT_CREATE_USER_WITH_GRANT, MYF(0));
} else {
/* Have come here to update the password of the non-existing
* user
*/
my_error(ER_PASSWORD_NO_MATCH, MYF(0), m_combo->user.str,
m_combo->host.str);
}
error = 1;
return true;
}
if (m_thd->lex->sql_command == SQLCOM_GRANT) {
my_error(ER_PASSWORD_NO_MATCH, MYF(0), m_combo->user.str,
m_combo->host.str);
error = 1;
return true;
}
restore_record(m_table, s->default_values);
assert(m_combo->host.str != nullptr);
m_table->field[m_table_schema->host_idx()]->store(
m_combo->host.str, m_combo->host.length, system_charset_info);
m_table->field[m_table_schema->user_idx()]->store(
m_combo->user.str, m_combo->user.length, system_charset_info);
} else {
/* There is a matching user record */
m_operation = Acl_table_operation::OP_UPDATE;
/* Check if there is such a user in user table in memory? */
if (!find_acl_user(m_combo->host.str, m_combo->user.str, false)) {
my_error(ER_PASSWORD_NO_MATCH, MYF(0));
error = consts::CRITICAL_ERROR;
return true;
}
store_record(m_table, record[1]); // Save copy for update
/* 1. resolve plugins in the LEX_USER struct if needed */
/* Get old plugin value from storage. */
old_plugin.str = get_field(
m_thd->mem_root, m_table->field[m_table_schema->plugin_idx()]);
if (old_plugin.str == nullptr || *old_plugin.str == '\0') {
my_error(ER_PASSWORD_NO_MATCH, MYF(0));
error = 1;
return true;
}
/*
It is important not to include the trailing '\0' in the string
length because otherwise the plugin hash search will fail.
*/
old_plugin.length = strlen(old_plugin.str);
/* Optimize for pointer comparison of built-in plugin name */
optimize_plugin_compare_by_pointer(&old_plugin);
builtin_plugin = auth_plugin_is_built_in(old_plugin.str);
}
break;
}
default:
return false;
}
return false;
}
/**
Finish the operation
Depending on type of operation (INSERT/UPDATE), either insert a new row
in mysql.user table or update an existing row using SE APIs.
@param [out] out_error Table operation error, if any
@returns status of write operation
*/
Acl_table_op_status Acl_table_user_writer::finish_operation(
Table_op_error_code &out_error) {
switch (m_operation) {
case Acl_table_operation::OP_INSERT: {
out_error = m_table->file->ha_write_row(m_table->record[0]); // insert
assert(out_error != HA_ERR_FOUND_DUPP_KEY);
assert(out_error != HA_ERR_LOCK_DEADLOCK);
assert(out_error != HA_ERR_LOCK_WAIT_TIMEOUT);
DBUG_EXECUTE_IF("wl7158_replace_user_table_3",
out_error = HA_ERR_LOCK_DEADLOCK;);
if (out_error) {
if (!m_table->file->is_ignorable_error(out_error)) {
acl_print_ha_error(out_error);
out_error = consts::CRITICAL_ERROR;
return Acl_table_op_status::OP_ERROR_CRITICAL;
}
}
break;
}
case Acl_table_operation::OP_UPDATE: {
/*
We should NEVER delete from the user table, as a uses can still
use mysqld even if he doesn't have any privileges in the user table!
*/
if (compare_records(m_table)) {
out_error = m_table->file->ha_update_row(m_table->record[1],
m_table->record[0]);
assert(out_error != HA_ERR_FOUND_DUPP_KEY);
assert(out_error != HA_ERR_LOCK_DEADLOCK);
assert(out_error != HA_ERR_LOCK_WAIT_TIMEOUT);
DBUG_EXECUTE_IF("wl7158_replace_user_table_2",
out_error = HA_ERR_LOCK_DEADLOCK;);
if (out_error && out_error != HA_ERR_RECORD_IS_THE_SAME) {
acl_print_ha_error(out_error);
out_error = consts::CRITICAL_ERROR;
return Acl_table_op_status::OP_ERROR_CRITICAL;
} else
out_error = 0;
}
break;
}
default:
out_error = 0;
}
return Acl_table_op_status::OP_OK;
}
/**
Update user's authentication information
Raises error in DA if mysql.user table does not have following columns:
- plugin
- password_last_changed
- password_expired
@param [out] return_value To update password change timestamp
@returns update operation status
@retval false Success
@retval true Error storing authentication info or table is not in
expected format
*/
bool Acl_table_user_writer::update_authentication_info(
Acl_table_user_writer_status &return_value) {
if (m_what_to_update.m_what & PLUGIN_ATTR ||
(m_what_to_update.m_what & DEFAULT_AUTH_ATTR &&
m_operation == Acl_table_operation::OP_INSERT)) {
bool builtin_plugin;
if (m_table->s->fields >= m_table_schema->plugin_idx()) {
m_table->field[m_table_schema->plugin_idx()]->store(
m_combo->first_factor_auth_info.plugin.str,
m_combo->first_factor_auth_info.plugin.length, system_charset_info);
m_table->field[m_table_schema->plugin_idx()]->set_notnull();
m_table->field[m_table_schema->authentication_string_idx()]->store(
m_combo->first_factor_auth_info.auth.str,
m_combo->first_factor_auth_info.auth.length, &my_charset_utf8mb3_bin);
m_table->field[m_table_schema->authentication_string_idx()]
->set_notnull();
} else {
my_error(ER_BAD_FIELD_ERROR, MYF(0), "plugin", "mysql.user");
return true;
}
/* If we change user plugin then check if it is builtin plugin */
optimize_plugin_compare_by_pointer(&m_combo->first_factor_auth_info.plugin);
builtin_plugin =
auth_plugin_is_built_in(m_combo->first_factor_auth_info.plugin.str);
/*
we update the password last changed field whenever there is change
in auth str and plugin is built in
*/
if (m_table->s->fields > m_table_schema->password_last_changed_idx()) {
if (builtin_plugin) {
/*
Calculate time stamp up to seconds elapsed from 1 Jan 1970
00:00:00.
*/
return_value.password_change_timestamp =
m_thd->query_start_timeval_trunc(0);
m_table->field[m_table_schema->password_last_changed_idx()]
->store_timestamp(&return_value.password_change_timestamp);
m_table->field[m_table_schema->password_last_changed_idx()]
->set_notnull();
}
} else {
my_error(ER_BAD_FIELD_ERROR, MYF(0), "password_last_changed",
"mysql.user");
return true;
}
/* if we have a password supplied we update the expiration field */
if (m_table->s->fields > m_table_schema->password_expired_idx()) {
m_table->field[m_table_schema->password_expired_idx()]->store(
"N", 1, system_charset_info);
} else {
my_error(ER_BAD_FIELD_ERROR, MYF(0), "password_expired", "mysql.user");
return true;
}
}
return false;
}
/**
Update global privileges for user
@param [out] return_value To store updated global privileges
@returns Update status for global privileges
*/
bool Acl_table_user_writer::update_privileges(
Acl_table_user_writer_status &return_value) {
if (m_what_to_update.m_what & ACCESS_RIGHTS_ATTR) {
/* Update table columns with new privileges */
const char what = m_revoke_grant ? 'N' : 'Y';
Field **tmp_field;
Access_bitmask priv;
for (tmp_field = m_table->field + 2, priv = SELECT_ACL;
*tmp_field && (*tmp_field)->real_type() == MYSQL_TYPE_ENUM &&
((Field_enum *)(*tmp_field))->typelib->count == 2;
tmp_field++, priv <<= 1) {
if (priv & m_rights) {
// set requested privileges
(*tmp_field)->store(&what, 1, &my_charset_latin1);
DBUG_PRINT("info",
("Updating field %lu with privilege %c",
(ulong)(m_table->field + 2 - tmp_field), (char)what));
}
}
if (m_table->s->fields > m_table_schema->create_role_priv_idx()) {
if (CREATE_ROLE_ACL & m_rights) {
m_table->field[m_table_schema->create_role_priv_idx()]->store(
&what, 1, &my_charset_latin1);
}
if (DROP_ROLE_ACL & m_rights) {
m_table->field[m_table_schema->drop_role_priv_idx()]->store(
&what, 1, &my_charset_latin1);
}
}
}
return_value.updated_rights = get_user_privileges();
DBUG_PRINT("info", ("Privileges on disk are now %" PRIu32,
return_value.updated_rights));
DBUG_PRINT("info", ("table fields: %d", m_table->s->fields));
return false;
}
/**
Update SSL properties
@returns Update status
@retval false Success
@retval true Table is not in expected format
*/
bool Acl_table_user_writer::update_ssl_properties() {
if (m_what_to_update.m_what & SSL_ATTR) {
LEX *lex = m_thd->lex;
if (m_table->s->fields >= m_table_schema->x509_subject_idx()) {
switch (lex->ssl_type) {
case SSL_TYPE_ANY: {
m_table->field[m_table_schema->ssl_type_idx()]->store(
STRING_WITH_LEN("ANY"), &my_charset_latin1);
m_table->field[m_table_schema->ssl_cipher_idx()]->store(
"", 0, &my_charset_latin1);
m_table->field[m_table_schema->x509_issuer_idx()]->store(
"", 0, &my_charset_latin1);
m_table->field[m_table_schema->x509_subject_idx()]->store(
"", 0, &my_charset_latin1);
break;
}
case SSL_TYPE_X509: {
m_table->field[m_table_schema->ssl_type_idx()]->store(
STRING_WITH_LEN("X509"), &my_charset_latin1);
m_table->field[m_table_schema->ssl_cipher_idx()]->store(
"", 0, &my_charset_latin1);
m_table->field[m_table_schema->x509_issuer_idx()]->store(
"", 0, &my_charset_latin1);
m_table->field[m_table_schema->x509_subject_idx()]->store(
"", 0, &my_charset_latin1);
break;
}
case SSL_TYPE_SPECIFIED: {
m_table->field[m_table_schema->ssl_type_idx()]->store(
STRING_WITH_LEN("SPECIFIED"), &my_charset_latin1);
m_table->field[m_table_schema->ssl_cipher_idx()]->store(
"", 0, &my_charset_latin1);
m_table->field[m_table_schema->x509_issuer_idx()]->store(
"", 0, &my_charset_latin1);
m_table->field[m_table_schema->x509_subject_idx()]->store(
"", 0, &my_charset_latin1);
if (lex->ssl_cipher)
m_table->field[m_table_schema->ssl_cipher_idx()]->store(
lex->ssl_cipher, strlen(lex->ssl_cipher), system_charset_info);
if (lex->x509_issuer)
m_table->field[m_table_schema->x509_issuer_idx()]->store(
lex->x509_issuer, strlen(lex->x509_issuer),
system_charset_info);
if (lex->x509_subject)
m_table->field[m_table_schema->x509_subject_idx()]->store(
lex->x509_subject, strlen(lex->x509_subject),
system_charset_info);
break;
}
case SSL_TYPE_NOT_SPECIFIED:
break;
case SSL_TYPE_NONE: {
m_table->field[m_table_schema->ssl_type_idx()]->store(
"", 0, &my_charset_latin1);
m_table->field[m_table_schema->ssl_cipher_idx()]->store(
"", 0, &my_charset_latin1);
m_table->field[m_table_schema->x509_issuer_idx()]->store(
"", 0, &my_charset_latin1);
m_table->field[m_table_schema->x509_subject_idx()]->store(
"", 0, &my_charset_latin1);
break;
default:
return true;
}
}
} else {
return true;
}
}
return false;
}
/**
Update user resource restrictions
@returns status of the operation
*/
bool Acl_table_user_writer::update_user_resources() {
if (m_what_to_update.m_what & RESOURCE_ATTR) {
const USER_RESOURCES mqh = m_thd->lex->mqh;
if (mqh.specified_limits & USER_RESOURCES::QUERIES_PER_HOUR)
m_table->field[m_table_schema->max_questions_idx()]->store(
(longlong)mqh.questions, true);
if (mqh.specified_limits & USER_RESOURCES::UPDATES_PER_HOUR)
m_table->field[m_table_schema->max_updates_idx()]->store(
(longlong)mqh.updates, true);
if (mqh.specified_limits & USER_RESOURCES::CONNECTIONS_PER_HOUR)
m_table->field[m_table_schema->max_connections_idx()]->store(
(longlong)mqh.conn_per_hour, true);
if (m_table->s->fields >= 36 &&
(mqh.specified_limits & USER_RESOURCES::USER_CONNECTIONS))
m_table->field[m_table_schema->max_user_connections_idx()]->store(
(longlong)mqh.user_conn, true);
}
mqh_used = mqh_used || m_thd->lex->mqh.questions || m_thd->lex->mqh.updates ||
m_thd->lex->mqh.conn_per_hour;
return false;
}
/**
Update password expiration info
Raises error in DA if mysql.user table does not have password_expired