forked from trdtnguyen/mysql-plnvm
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsql_user.cc
1986 lines (1759 loc) · 59.1 KB
/
sql_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) 2000, 2017, Oracle and/or its affiliates. All rights reserved.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; version 2 of the License.
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 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_parse.h" /* check_access */
#include "rpl_filter.h" /* rpl_filter */
#include "sql_base.h" /* MYSQL_LOCK_IGNORE_TIMEOUT */
#include "sql_table.h" /* open_ltable */
#include "sql_plugin.h" /* lock_plugin_data etc. */
#include "password.h" /* my_make_scrambled_password */
#include "log_event.h" /* append_query_string */
#include "key.h" /* key_copy, key_cmp_if_same */
/* key_restore */
#include "auth_internal.h"
#include "sql_auth_cache.h"
#include "sql_authentication.h"
#include "prealloced_array.h"
#include "tztime.h"
#include "crypt_genhash_impl.h" /* CRYPT_MAX_PASSWORD_SIZE */
#include "sql_user_table.h"
#ifndef DBUG_OFF
#define HASH_STRING_WITH_QUOTE \
"$5$BVZy9O>'a+2MH]_?$fpWyabcdiHjfCVqId/quykZzjaA7adpkcen/uiQrtmOK4p4"
#endif
/**
Auxiliary function for constructing a user list string.
This function is used for error reporting and logging.
@param thd Thread context
@param str A String to store the user list.
@param user A LEX_USER which will be appended into user list.
@param comma If TRUE, append a ',' before the the user.
@param ident If TRUE, append ' IDENTIFIED BY/WITH...' after the user,
if the given user has credentials set with 'IDENTIFIED BY/WITH'
*/
void append_user(THD *thd, String *str, LEX_USER *user, bool comma= true,
bool ident= false)
{
String from_user(user->user.str, user->user.length, system_charset_info);
String from_plugin(user->plugin.str, user->plugin.length, system_charset_info);
String from_auth(user->auth.str, user->auth.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);
if (ident)
{
if (user->plugin.str && (user->plugin.length > 0) &&
memcmp(user->plugin.str, native_password_plugin_name.str,
user->plugin.length))
{
/**
The plugin identifier is allowed to be specified,
both with and without quote marks. We log it with
quotes always.
*/
str->append(STRING_WITH_LEN(" IDENTIFIED WITH "));
append_query_string(thd, system_charset_info, &from_plugin, str);
if (user->auth.str && (user->auth.length > 0))
{
str->append(STRING_WITH_LEN(" AS "));
append_query_string(thd, system_charset_info, &from_auth, str);
}
}
else if (user->auth.str)
{
str->append(STRING_WITH_LEN(" IDENTIFIED BY PASSWORD '"));
if (user->uses_identified_by_password_clause ||
user->uses_authentication_string_clause)
{
str->append(user->auth.str, user->auth.length);
str->append("'");
}
else
{
/*
Password algorithm is chosen based on old_passwords variable or
TODO the new password_algorithm variable.
It is assumed that the variable hasn't changed since parsing.
*/
if (thd->variables.old_passwords == 0)
{
/*
my_make_scrambled_password_sha1() requires a target buffer size of
SCRAMBLED_PASSWORD_CHAR_LENGTH + 1.
The extra character is for the probably originate from either '\0'
or the initial '*' character.
*/
char tmp[SCRAMBLED_PASSWORD_CHAR_LENGTH + 1];
my_make_scrambled_password_sha1(tmp, user->auth.str,
user->auth.length);
str->append(tmp);
}
else
{
/*
With old_passwords == 2 the scrambled password will be binary.
*/
DBUG_ASSERT(thd->variables.old_passwords = 2);
str->append("<secret>");
}
str->append("'");
}
}
}
}
void append_user_new(THD *thd, String *str, LEX_USER *user, bool comma= true)
{
String from_user(user->user.str, user->user.length, system_charset_info);
String from_plugin(user->plugin.str, user->plugin.length, system_charset_info);
String default_plugin(default_auth_plugin_name.str,
default_auth_plugin_name.length, system_charset_info);
String from_auth(user->auth.str, user->auth.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);
/* CREATE USER is always rewritten with IDENTIFIED WITH .. AS */
if (thd->lex->sql_command == SQLCOM_CREATE_USER)
{
str->append(STRING_WITH_LEN(" IDENTIFIED WITH "));
if (user->plugin.length > 0)
append_query_string(thd, system_charset_info, &from_plugin, str);
else
append_query_string(thd, system_charset_info, &default_plugin, str);
if (user->auth.length > 0)
{
str->append(STRING_WITH_LEN(" AS "));
if (thd->lex->contains_plaintext_password)
{
str->append("'");
str->append(STRING_WITH_LEN("<secret>"));
str->append("'");
}
else
append_query_string(thd, system_charset_info, &from_auth, str);
}
}
else
{
if (user->uses_identified_by_clause ||
user->uses_identified_with_clause ||
user->uses_identified_by_password_clause)
{
str->append(STRING_WITH_LEN(" IDENTIFIED WITH "));
if (user->plugin.length > 0)
append_query_string(thd, system_charset_info, &from_plugin, str);
else
append_query_string(thd, system_charset_info, &default_plugin, str);
if (user->auth.length > 0)
{
str->append(STRING_WITH_LEN(" AS "));
if (thd->lex->contains_plaintext_password)
{
str->append("'");
str->append(STRING_WITH_LEN("<secret>"));
str->append("'");
}
else
append_query_string(thd, system_charset_info, &from_auth, str);
}
}
}
}
/**
Escapes special characters in the unescaped string, taking into account
the current character set and sql mode.
@param thd [in] The thd structure.
@param to [out] Escaped string output buffer.
@param from [in] String to escape.
@param length [in] String to escape length.
@return Result value.
@retval != (ulong)-1 Succeeded. Number of bytes written to the output
buffer without the '\0' character.
@retval (ulong)-1 Failed.
*/
inline ulong escape_string_mysql(THD *thd, char *to, const char *from,
ulong length)
{
if (!(thd->variables.sql_mode & MODE_NO_BACKSLASH_ESCAPES))
return (uint)escape_string_for_mysql(system_charset_info, to, 0, from,
length);
else
return (uint)escape_quotes_for_mysql(system_charset_info, to, 0, from,
length, '\'');
}
#ifndef NO_EMBEDDED_ACCESS_CHECKS
/*
Enumeration of various ACL's and Hashes used in handle_grant_struct()
*/
enum enum_acl_lists
{
USER_ACL= 0,
DB_ACL,
COLUMN_PRIVILEGES_HASH,
PROC_PRIVILEGES_HASH,
FUNC_PRIVILEGES_HASH,
PROXY_USERS_ACL
};
int check_change_password(THD *thd, const char *host, const char *user,
const char *new_password, size_t new_password_len)
{
Security_context *sctx;
if (!initialized)
{
my_error(ER_OPTION_PREVENTS_STATEMENT, MYF(0), "--skip-grant-tables");
return(1);
}
sctx= thd->security_context();
if (!thd->slave_thread &&
(strcmp(sctx->user().str, user) ||
my_strcasecmp(system_charset_info, host,
sctx->priv_host().str)))
{
if (sctx->password_expired())
{
my_error(ER_MUST_CHANGE_PASSWORD, MYF(0));
return(1);
}
if (check_access(thd, UPDATE_ACL, "mysql", NULL, NULL, 1, 0))
return(1);
}
if (!thd->slave_thread &&
likely((get_server_state() == SERVER_OPERATING)) &&
!strcmp(thd->security_context()->priv_user().str,""))
{
my_message(ER_PASSWORD_ANONYMOUS_USER, ER(ER_PASSWORD_ANONYMOUS_USER),
MYF(0));
return(1);
}
return(0);
}
/**
Auxiliary function for constructing CREATE USER sql for a given user.
@param thd Thread context
@param user_name user for which the sql should be constructed.
@retval
0 OK.
1 Error.
*/
bool mysql_show_create_user(THD *thd, LEX_USER *user_name)
{
int error= 0;
ACL_USER *acl_user;
LEX *lex= thd->lex;
Protocol *protocol= thd->get_protocol();
USER_RESOURCES tmp_user_resource;
enum SSL_type ssl_type;
char *ssl_cipher, *x509_issuer, *x509_subject;
char buff[256];
Item_string *field= NULL;
List<Item> field_list;
String sql_text(buff,sizeof(buff),system_charset_info);
LEX_ALTER alter_info;
DBUG_ENTER("mysql_show_create_user");
mysql_mutex_lock(&acl_cache->lock);
if (!(acl_user= find_acl_user(user_name->host.str, user_name->user.str, TRUE)))
{
mysql_mutex_unlock(&acl_cache->lock);
String wrong_users;
append_user(thd, &wrong_users, user_name, wrong_users.length() > 0, false);
my_error(ER_CANNOT_USER, MYF(0), "SHOW CREATE USER",
wrong_users.c_ptr_safe());
DBUG_RETURN(1);
}
/* fill in plugin, auth_str from acl_user */
user_name->auth.str= acl_user->auth_string.str;
user_name->auth.length= acl_user->auth_string.length;
user_name->plugin= acl_user->plugin;
user_name->uses_identified_by_clause= true;
user_name->uses_identified_with_clause= false;
user_name->uses_identified_by_password_clause= false;
user_name->uses_authentication_string_clause= false;
/* make a copy of user resources, ssl and password expire attributes */
tmp_user_resource= lex->mqh;
lex->mqh= acl_user->user_resource;
/* Set specified_limits flags so user resources are shown properly. */
if (lex->mqh.user_conn)
lex->mqh.specified_limits|= USER_RESOURCES::USER_CONNECTIONS;
if (lex->mqh.questions)
lex->mqh.specified_limits|= USER_RESOURCES::QUERIES_PER_HOUR;
if (lex->mqh.updates)
lex->mqh.specified_limits|= USER_RESOURCES::UPDATES_PER_HOUR;
if (lex->mqh.conn_per_hour)
lex->mqh.specified_limits|= USER_RESOURCES::CONNECTIONS_PER_HOUR;
ssl_type= lex->ssl_type;
ssl_cipher= lex->ssl_cipher;
x509_issuer= lex->x509_issuer;
x509_subject= lex->x509_subject;
lex->ssl_type= acl_user->ssl_type;
lex->ssl_cipher= const_cast<char*>(acl_user->ssl_cipher);
lex->x509_issuer= const_cast<char*>(acl_user->x509_issuer);
lex->x509_subject= const_cast<char*>(acl_user->x509_subject);
alter_info= lex->alter_password;
lex->alter_password.update_password_expired_column= acl_user->password_expired;
lex->alter_password.use_default_password_lifetime= acl_user->use_default_password_lifetime;
lex->alter_password.expire_after_days= acl_user->password_lifetime;
lex->alter_password.update_account_locked_column= true;
lex->alter_password.account_locked= acl_user->account_locked;
lex->alter_password.update_password_expired_fields= true;
/* send the metadata to client */
field=new Item_string("",0,&my_charset_latin1);
field->max_length=256;
strxmov(buff,"CREATE USER for ",user_name->user.str,"@",
user_name->host.str,NullS);
field->item_name.set(buff);
field_list.push_back(field);
if (thd->send_result_metadata(&field_list,
Protocol::SEND_NUM_ROWS | Protocol::SEND_EOF))
{
error= 1;
goto err;
}
sql_text.length(0);
lex->users_list.push_back(user_name);
mysql_rewrite_create_alter_user(thd, &sql_text);
/* send the result row to client */
protocol->start_row();
protocol->store(sql_text.ptr(),sql_text.length(),sql_text.charset());
if (protocol->end_row())
{
error= 1;
goto err;
}
err:
/* restore user resources, ssl and password expire attributes */
lex->mqh= tmp_user_resource;
lex->ssl_type= ssl_type;
lex->ssl_cipher= ssl_cipher;
lex->x509_issuer= x509_issuer;
lex->x509_subject= x509_subject;
lex->alter_password= alter_info;
mysql_mutex_unlock(&acl_cache->lock);
my_eof(thd);
DBUG_RETURN(error);
}
/**
This function does following:
1. Convert plain text password to hash and update the same in
user definition.
2. Validate hash string if specified in user definition.
3. Identify what all fields needs to be updated in mysql.user
table based on user definition.
@param thd Thread context
@param Str user on which attributes has to be applied
@param what_to_set User attributes
@param is_privileged_user Whether caller has CREATE_USER_ACL
or UPDATE_ACL over mysql.*
@retval 0 ok
@retval 1 ERROR;
*/
bool set_and_validate_user_attributes(THD *thd,
LEX_USER *Str,
ulong &what_to_set,
bool is_privileged_user)
{
bool user_exists= false;
ACL_USER *acl_user;
plugin_ref plugin= NULL;
char outbuf[MAX_FIELD_WIDTH]= {0};
unsigned int buflen= MAX_FIELD_WIDTH, inbuflen;
const char *inbuf;
char *password= NULL;
what_to_set= 0;
/* update plugin,auth str attributes */
if (Str->uses_identified_by_clause ||
Str->uses_identified_by_password_clause ||
Str->uses_identified_with_clause ||
Str->uses_authentication_string_clause)
what_to_set|= PLUGIN_ATTR;
else
what_to_set|= DEFAULT_AUTH_ATTR;
/* update ssl attributes */
if (thd->lex->ssl_type != SSL_TYPE_NOT_SPECIFIED)
what_to_set|= SSL_ATTR;
/* update connection attributes */
if (thd->lex->mqh.specified_limits)
what_to_set|= RESOURCE_ATTR;
if ((acl_user= find_acl_user(Str->host.str, Str->user.str, TRUE)))
user_exists= true;
/* copy password expire attributes to individual user */
Str->alter_status= thd->lex->alter_password;
/* update password expire attributes */
if (Str->alter_status.update_password_expired_column ||
!Str->alter_status.use_default_password_lifetime ||
Str->alter_status.expire_after_days)
what_to_set|= PASSWORD_EXPIRE_ATTR;
/* update account lock attribute */
if (Str->alter_status.update_account_locked_column)
what_to_set|= ACCOUNT_LOCK_ATTR;
if (user_exists)
{
if (thd->lex->sql_command == SQLCOM_ALTER_USER)
{
/* If no plugin is given, get existing plugin */
if (!Str->uses_identified_with_clause)
Str->plugin= acl_user->plugin;
/*
always check for password expire/interval attributes as there is no
way to differentiate NEVER EXPIRE and EXPIRE DEFAULT scenario
*/
if (Str->alter_status.update_password_expired_fields)
what_to_set|= PASSWORD_EXPIRE_ATTR;
}
else
{
/* if IDENTIFIED WITH is not specified set plugin from cache */
if (!Str->uses_identified_with_clause)
{
Str->plugin= acl_user->plugin;
/* set auth str from cache when not specified for existing user */
if (!(Str->uses_identified_by_clause ||
Str->uses_identified_by_password_clause ||
Str->uses_authentication_string_clause))
{
Str->auth.str= acl_user->auth_string.str;
Str->auth.length= acl_user->auth_string.length;
}
}
}
/*
if there is a plugin specified with no auth string, and that
plugin supports password expiration then set the account as expired.
*/
if (Str->uses_identified_with_clause &&
!(Str->uses_identified_by_clause ||
Str->uses_authentication_string_clause) &&
auth_plugin_supports_expiration(Str->plugin.str))
{
Str->alter_status.update_password_expired_column= true;
what_to_set|= PASSWORD_EXPIRE_ATTR;
}
}
else
{
/* set default plugin for new users if not specified */
if (!Str->uses_identified_with_clause)
Str->plugin= default_auth_plugin_name;
}
plugin= my_plugin_lock_by_name(0, Str->plugin,
MYSQL_AUTHENTICATION_PLUGIN);
/* check if plugin is loaded */
if (!plugin)
{
my_error(ER_PLUGIN_IS_NOT_LOADED, MYF(0), Str->plugin.str);
return(1);
}
if (user_exists &&
(what_to_set & PLUGIN_ATTR))
{
st_mysql_auth *auth= (st_mysql_auth *) plugin_decl(plugin)->info;
if (auth->authentication_flags &
AUTH_FLAG_PRIVILEGED_USER_FOR_PASSWORD_CHANGE)
{
if (!is_privileged_user &&
(thd->lex->sql_command == SQLCOM_ALTER_USER ||
thd->lex->sql_command == SQLCOM_GRANT))
{
/*
An external plugin that prevents user
to change authentication_string information
unless user is privileged.
*/
what_to_set= NONE_ATTR;
my_error(ER_ACCESS_DENIED_ERROR, MYF(0),
thd->security_context()->priv_user().str,
thd->security_context()->priv_host().str,
thd->password ? ER_THD(thd, ER_YES) : ER_THD(thd, ER_NO));
plugin_unlock(0, plugin);
return (1);
}
}
if (!(auth->authentication_flags & AUTH_FLAG_USES_INTERNAL_STORAGE))
{
if (thd->lex->sql_command == SQLCOM_SET_OPTION)
{
/*
A plugin that does not use internal storage and
hence does not support SET PASSWORD
*/
char warning_buffer[MYSQL_ERRMSG_SIZE];
my_snprintf(warning_buffer, sizeof(warning_buffer),
"SET PASSWORD has no significance for user '%s'@'%s' as "
"authentication plugin does not support it.",
Str->user.str, Str->host.str);
warning_buffer[MYSQL_ERRMSG_SIZE-1]= '\0';
push_warning(thd, Sql_condition::SL_NOTE,
ER_SET_PASSWORD_AUTH_PLUGIN,
warning_buffer);
plugin_unlock(0, plugin);
what_to_set= NONE_ATTR;
return (0);
}
}
}
/*
If auth string is specified, change it to hash.
Validate empty credentials for new user ex: CREATE USER u1;
*/
if (Str->uses_identified_by_clause ||
(Str->auth.length == 0 && !user_exists))
{
st_mysql_auth *auth= (st_mysql_auth *) plugin_decl(plugin)->info;
inbuf= Str->auth.str;
inbuflen= Str->auth.length;
if (auth->generate_authentication_string(outbuf,
&buflen,
inbuf,
inbuflen))
{
plugin_unlock(0, plugin);
return(1);
}
if (buflen)
{
password= (char *) thd->alloc(buflen);
memcpy(password, outbuf, buflen);
}
else
password= const_cast<char*>("");
/* erase in memory copy of plain text password */
memset((char*)(Str->auth.str), 0, Str->auth.length);
/* Use the authentication_string field as password */
Str->auth.str= password;
Str->auth.length= buflen;
thd->lex->contains_plaintext_password= false;
}
/* Validate hash string */
if(Str->uses_identified_by_password_clause ||
Str->uses_authentication_string_clause)
{
st_mysql_auth *auth= (st_mysql_auth *) plugin_decl(plugin)->info;
/*
Validate hash string in following cases:
1. IDENTIFIED BY PASSWORD.
2. IDENTIFIED WITH .. AS 'auth_str' for ALTER USER statement
and its a replication slave thread
*/
if (Str->uses_identified_by_password_clause ||
(Str->uses_authentication_string_clause &&
thd->lex->sql_command == SQLCOM_ALTER_USER &&
thd->slave_thread))
{
if (auth->validate_authentication_string((char*)Str->auth.str,
Str->auth.length))
{
my_error(ER_PASSWORD_FORMAT, MYF(0));
plugin_unlock(0, plugin);
return(1);
}
}
}
plugin_unlock(0, plugin);
return(0);
}
/**
Change a password hash for a user.
@param thd Thread handle
@param host Hostname
@param user User name
@param new_password New password hash for host@user
Note : it will also reset the change_password flag.
This is safe to do unconditionally since the simple userless form
SET PASSWORD = 'text' will be the only allowed form when
this flag is on. So we don't need to check user names here.
@see set_var_password::update(THD *thd)
@return Error code
@retval 0 ok
@retval 1 ERROR; In this case the error is sent to the client.
*/
bool change_password(THD *thd, const char *host, const char *user,
char *new_password)
{
TABLE_LIST tables;
TABLE *table;
Acl_table_intact table_intact;
LEX_USER *combo= NULL;
/* Buffer should be extended when password length is extended. */
char buff[2048];
/* buffer to store the hash string */
char hash_str[MAX_FIELD_WIDTH]= {0};
char *hash_str_escaped= NULL;
ulong query_length= 0;
ulong what_to_set= 0;
bool save_binlog_row_based;
size_t new_password_len= strlen(new_password);
size_t escaped_hash_str_len= 0;
bool result= true, rollback_whole_statement= false;
int ret;
DBUG_ENTER("change_password");
DBUG_PRINT("enter",("host: '%s' user: '%s' new_password: '%s'",
host,user,new_password));
DBUG_ASSERT(host != 0); // Ensured by parent
if (check_change_password(thd, host, user, new_password, new_password_len))
DBUG_RETURN(1);
tables.init_one_table("mysql", 5, "user", 4, "user", TL_WRITE);
#ifdef HAVE_REPLICATION
/*
GRANT and REVOKE are applied the slave in/exclusion rules as they are
some kind of updates to the mysql.% tables.
*/
if (thd->slave_thread && rpl_filter->is_on())
{
/*
The tables must be marked "updating" so that tables_ok() takes them into
account in tests. It's ok to leave 'updating' set after tables_ok.
*/
tables.updating= 1;
/* Thanks to memset, tables.next==0 */
if (!(thd->sp_runtime_ctx || rpl_filter->tables_ok(0, &tables)))
DBUG_RETURN(0);
}
#endif
if (!(table= open_ltable(thd, &tables, TL_WRITE, MYSQL_LOCK_IGNORE_TIMEOUT)))
DBUG_RETURN(1);
if (table_intact.check(table, &mysql_user_table_def))
DBUG_RETURN(1);
if (!table->key_info)
{
my_error(ER_TABLE_CORRUPT, MYF(0), table->s->db.str,
table->s->table_name.str);
DBUG_RETURN(1);
}
/*
This statement will be replicated as a statement, even when using
row-based replication. The flag will be reset at the end of the
statement.
*/
if ((save_binlog_row_based= thd->is_current_stmt_binlog_format_row()))
thd->clear_current_stmt_binlog_format_row();
mysql_mutex_lock(&acl_cache->lock);
ACL_USER *acl_user;
if (!(acl_user= find_acl_user(host, user, TRUE)))
{
mysql_mutex_unlock(&acl_cache->lock);
my_message(ER_PASSWORD_NO_MATCH, ER(ER_PASSWORD_NO_MATCH), MYF(0));
goto end;
}
DBUG_ASSERT(acl_user->plugin.length != 0);
if (!(combo=(LEX_USER*) thd->alloc(sizeof(st_lex_user))))
DBUG_RETURN(1);
combo->user.str= user;
combo->host.str= host;
combo->user.length= strlen(user);
combo->host.length= strlen(host);
thd->make_lex_string(&combo->user,
combo->user.str, strlen(combo->user.str), 0);
thd->make_lex_string(&combo->host,
combo->host.str, strlen(combo->host.str), 0);
combo->plugin= EMPTY_CSTR;
combo->auth.str= new_password;
combo->auth.length= new_password_len;
combo->uses_identified_by_clause= true;
combo->uses_identified_with_clause= false;
combo->uses_identified_by_password_clause= false;
combo->uses_authentication_string_clause= false;
/* set default values */
thd->lex->ssl_type= SSL_TYPE_NOT_SPECIFIED;
memset(&(thd->lex->mqh), 0, sizeof(thd->lex->mqh));
thd->lex->alter_password.update_password_expired_column= false;
thd->lex->alter_password.use_default_password_lifetime= true;
thd->lex->alter_password.expire_after_days= 0;
thd->lex->alter_password.update_account_locked_column= false;
thd->lex->alter_password.account_locked= false;
thd->lex->alter_password.update_password_expired_fields= false;
/*
When @@log-backward-compatible-user-definitions variable is ON
and its a slave thread, then the password is already hashed. So
do not generate another hash.
*/
if (opt_log_builtin_as_identified_by_password &&
thd->slave_thread)
combo->uses_identified_by_clause= false;
if (set_and_validate_user_attributes(thd, combo, what_to_set, true))
{
result= 1;
mysql_mutex_unlock(&acl_cache->lock);
goto end;
}
ret= replace_user_table(thd, table, combo, 0, false, true, what_to_set);
if (ret)
{
mysql_mutex_unlock(&acl_cache->lock);
result= 1;
if (ret < 0)
rollback_whole_statement= true;
goto end;
}
if (!update_sctx_cache(thd->security_context(), acl_user, false) &&
thd->security_context()->password_expired())
{
/* the current user is not the same as the user we operate on */
my_error(ER_MUST_CHANGE_PASSWORD, MYF(0));
result= 1;
mysql_mutex_unlock(&acl_cache->lock);
goto end;
}
mysql_mutex_unlock(&acl_cache->lock);
result= 0;
escaped_hash_str_len= (opt_log_builtin_as_identified_by_password?
combo->auth.length:
acl_user->auth_string.length)*2+1;
/*
Allocate a buffer for the escaped password. It should at least have place
for length*2+1 chars.
*/
hash_str_escaped= (char *)alloc_root(thd->mem_root, escaped_hash_str_len);
if (!hash_str_escaped)
{
my_error(ER_OUTOFMEMORY, MYF(ME_FATALERROR), 0);
result= 1;
goto end;
}
/*
Based on @@log-backward-compatible-user-definitions variable
rewrite SET PASSWORD
*/
if (opt_log_builtin_as_identified_by_password)
{
memcpy(hash_str, combo->auth.str, combo->auth.length);
DBUG_EXECUTE_IF("force_hash_string_with_quote",
strcpy(hash_str, HASH_STRING_WITH_QUOTE);
);
escape_string_mysql(thd, hash_str_escaped, hash_str, strlen(hash_str));
query_length= sprintf(buff, "SET PASSWORD FOR '%-.120s'@'%-.120s'='%s'",
acl_user->user ? acl_user->user : "",
acl_user->host.get_host() ? acl_user->host.get_host() : "",
hash_str_escaped);
}
else
{
DBUG_EXECUTE_IF("force_hash_string_with_quote",
strcpy(acl_user->auth_string.str, HASH_STRING_WITH_QUOTE);
);
escape_string_mysql(thd, hash_str_escaped, acl_user->auth_string.str,
strlen(acl_user->auth_string.str));
query_length= sprintf(buff, "ALTER USER '%-.120s'@'%-.120s' IDENTIFIED WITH '%-.120s' AS '%s'",
acl_user->user ? acl_user->user : "",
acl_user->host.get_host() ? acl_user->host.get_host() : "",
acl_user->plugin.str,
hash_str_escaped);
}
result= write_bin_log(thd, true, buff, query_length,
table->file->has_transactions());
end:
result|= acl_end_trans_and_close_tables(thd,
thd->transaction_rollback_request ||
rollback_whole_statement);
if (!result)
acl_notify_htons(thd, buff, query_length);
/* Restore the state of binlog format */
DBUG_ASSERT(!thd->is_current_stmt_binlog_format_row());
if (save_binlog_row_based)
thd->set_current_stmt_binlog_format_row();
DBUG_RETURN(result);
}
/**
Handle an in-memory privilege structure.
@param struct_no The number of the structure to handle (0..5).
@param drop If user_from is to be dropped.
@param user_from The the user to be searched/dropped/renamed.
@param user_to The new name for the user if to be renamed, NULL otherwise.
@note
Scan through all elements in an in-memory grant structure and apply
the requested operation.
Delete from grant structure if drop is true.
Update in grant structure if drop is false and user_to is not NULL.
Search in grant structure if drop is false and user_to is NULL.
Structures are enumerated as follows:
0 ACL_USER
1 ACL_DB
2 COLUMN_PRIVILIGES_HASH
3 PROC_PRIVILEGES_HASH
4 FUNC_PRIVILEGES_HASH
5 ACL_PROXY_USERS
@retval > 0 At least one element matched.
@retval 0 OK, but no element matched.
@retval -1 Wrong arguments to function or Out of Memory.
*/
static int handle_grant_struct(enum enum_acl_lists struct_no, bool drop,
LEX_USER *user_from, LEX_USER *user_to)
{
int result= 0;
size_t idx;
size_t elements;
const char *user= NULL;
const char *host= NULL;
ACL_USER *acl_user= NULL;
ACL_DB *acl_db= NULL;
ACL_PROXY_USER *acl_proxy_user= NULL;
GRANT_NAME *grant_name= NULL;
/*
Dynamic array acl_grant_name used to store pointers to all
GRANT_NAME objects
*/
Prealloced_array<GRANT_NAME *, 16> acl_grant_name(PSI_INSTRUMENT_ME);
HASH *grant_name_hash= NULL;
DBUG_ENTER("handle_grant_struct");
DBUG_PRINT("info",("scan struct: %u search: '%s'@'%s'",
struct_no, user_from->user.str, user_from->host.str));
mysql_mutex_assert_owner(&acl_cache->lock);
/* Get the number of elements in the in-memory structure. */
switch (struct_no) {
case USER_ACL:
elements= acl_users->size();
break;
case DB_ACL:
elements= acl_dbs->size();
break;
case COLUMN_PRIVILEGES_HASH:
elements= column_priv_hash.records;
grant_name_hash= &column_priv_hash;
break;
case PROC_PRIVILEGES_HASH:
elements= proc_priv_hash.records;
grant_name_hash= &proc_priv_hash;
break;
case FUNC_PRIVILEGES_HASH:
elements= func_priv_hash.records;
grant_name_hash= &func_priv_hash;
break;
case PROXY_USERS_ACL:
elements= acl_proxy_users->size();
break;
default:
DBUG_RETURN(-1);
}
#ifdef EXTRA_DEBUG
DBUG_PRINT("loop",("scan struct: %u search user: '%s' host: '%s'",
struct_no, user_from->user.str, user_from->host.str));
#endif
/* Loop over all elements. */
for (idx= 0; idx < elements; idx++)
{
/*
Get a pointer to the element.
*/
switch (struct_no) {
case USER_ACL:
acl_user= &acl_users->at(idx);
user= acl_user->user;
host= acl_user->host.get_host();
break;
case DB_ACL:
acl_db= &acl_dbs->at(idx);
user= acl_db->user;
host= acl_db->host.get_host();
break;
case COLUMN_PRIVILEGES_HASH:
case PROC_PRIVILEGES_HASH:
case FUNC_PRIVILEGES_HASH:
grant_name= (GRANT_NAME*) my_hash_element(grant_name_hash, idx);
user= grant_name->user;
host= grant_name->host.get_host();
break;
case PROXY_USERS_ACL:
acl_proxy_user= &acl_proxy_users->at(idx);
user= acl_proxy_user->get_user();
host= acl_proxy_user->host.get_host();
break;
default:
MY_ASSERT_UNREACHABLE();
}
if (! user)
user= "";
if (! host)
host= "";
#ifdef EXTRA_DEBUG
DBUG_PRINT("loop",("scan struct: %u index: %zu user: '%s' host: '%s'",
struct_no, idx, user, host));
#endif
if (strcmp(user_from->user.str, user) ||
my_strcasecmp(system_charset_info, user_from->host.str, host))
continue;
result= 1; /* At least one element found. */
if ( drop )
{
switch ( struct_no ) {
case USER_ACL:
acl_users->erase(idx);
elements--;
/*
- If we are iterating through an array then we just have moved all
elements after the current element one position closer to its head.
This means that we have to take another look at the element at
current position as it is a new element from the array's tail.
- This is valid for case USER_ACL, DB_ACL and PROXY_USERS_ACL.
*/
idx--;