1
/* Copyright (C) 2007 MySQL AB
3
This program is free software; you can redistribute it and/or modify
4
it under the terms of the GNU General Public License as published by
5
the Free Software Foundation; version 2 of the License.
7
This program is distributed in the hope that it will be useful,
8
but WITHOUT ANY WARRANTY; without even the implied warranty of
9
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10
GNU General Public License for more details.
12
You should have received a copy of the GNU General Public License
13
along with this program; if not, write to the Free Software
14
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
1
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
4
* Copyright (C) 2008 Sun Microsystems
6
* This program is free software; you can redistribute it and/or modify
7
* it under the terms of the GNU General Public License as published by
8
* the Free Software Foundation; version 2 of the License.
10
* This program is distributed in the hope that it will be useful,
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
* GNU General Public License for more details.
15
* You should have received a copy of the GNU General Public License
16
* along with this program; if not, write to the Free Software
17
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
79
check_user(THD *thd, enum enum_server_command command,
81
uint passwd_len, const char *db,
86
check_user(Session *session, const char *passwd,
87
uint32_t passwd_len, const char *db,
84
90
LEX_STRING db_str= { (char *) db, db ? strlen(db) : 0 };
85
91
bool is_authenticated;
88
Clear thd->db as it points to something, that will be freed when
94
Clear session->db as it points to something, that will be freed when
89
95
connection is closed. We don't want to accidentally free a wrong
90
96
pointer if connect failed. Also in case of 'CHANGE USER' failure,
91
97
current database will be switched to 'no database selected'.
93
thd->reset_db(NULL, 0);
99
session->reset_db(NULL, 0);
95
101
if (passwd_len != 0 && passwd_len != SCRAMBLE_LENGTH)
97
my_error(ER_HANDSHAKE_ERROR, MYF(0), thd->main_security_ctx.ip);
103
my_error(ER_HANDSHAKE_ERROR, MYF(0), session->main_security_ctx.ip);
101
is_authenticated= authenticate_user(thd, passwd);
107
is_authenticated= authenticate_user(session, passwd);
103
109
if (is_authenticated != true)
105
111
my_error(ER_ACCESS_DENIED_ERROR, MYF(0),
106
thd->main_security_ctx.user,
107
thd->main_security_ctx.ip,
112
session->main_security_ctx.user,
113
session->main_security_ctx.ip,
108
114
passwd_len ? ER(ER_YES) : ER(ER_NO));
132
Log the command before authentication checks, so that the user can
133
check the log for the tried login tried and also to detect
136
general_log_print(thd, command,
137
((char*) "%s@%s on %s"),
138
thd->main_security_ctx.user,
139
thd->main_security_ctx.ip,
140
db ? db : (char*) "");
142
137
/* Change database if necessary */
145
if (mysql_change_db(thd, &db_str, false))
140
if (mysql_change_db(session, &db_str, false))
147
142
/* mysql_change_db() has pushed the error message. */
152
thd->password= test(passwd_len); // remember for error messages
147
session->password= test(passwd_len); // remember for error messages
153
148
/* Ready to handle queries */
183
178
- client character set doesn't exists in server
185
180
if (!opt_character_set_client_handshake ||
186
!(thd->variables.character_set_client= get_charset(cs_number, MYF(0))) ||
181
!(session->variables.character_set_client= get_charset(cs_number, MYF(0))) ||
187
182
!my_strcasecmp(&my_charset_utf8_general_ci,
188
183
global_system_variables.character_set_client->name,
189
thd->variables.character_set_client->name))
184
session->variables.character_set_client->name))
191
thd->variables.character_set_client=
186
session->variables.character_set_client=
192
187
global_system_variables.character_set_client;
193
thd->variables.collation_connection=
188
session->variables.collation_connection=
194
189
global_system_variables.collation_connection;
195
thd->variables.character_set_results=
190
session->variables.character_set_results=
196
191
global_system_variables.character_set_results;
200
thd->variables.character_set_results=
201
thd->variables.collation_connection=
202
thd->variables.character_set_client;
195
session->variables.character_set_results=
196
session->variables.collation_connection=
197
session->variables.character_set_client;
262
257
server_capabilites|= CLIENT_COMPRESS;
263
258
#endif /* HAVE_COMPRESS */
265
end= stpncpy(buff, server_version, SERVER_VERSION_LENGTH) + 1;
266
int4store((uchar*) end, thd->thread_id);
260
end= my_stpncpy(buff, server_version, SERVER_VERSION_LENGTH) + 1;
261
int4store((unsigned char*) end, session->thread_id);
269
264
So as check_connection is the only entry point to authorization
270
265
procedure, scramble is set here. This gives us new scramble for
273
create_random_string(thd->scramble, SCRAMBLE_LENGTH, &thd->rand);
268
create_random_string(session->scramble, SCRAMBLE_LENGTH, &session->rand);
275
270
Old clients does not understand long scrambles, but can ignore packet
276
271
tail: that's why first part of the scramble is placed here, and second
277
272
part at the end of packet.
279
end= strmake(end, thd->scramble, SCRAMBLE_LENGTH_323) + 1;
274
end= strmake(end, session->scramble, SCRAMBLE_LENGTH_323) + 1;
281
276
int2store(end, server_capabilites);
282
277
/* write server characteristics: up to 16 bytes allowed */
283
278
end[2]=(char) default_charset_info->number;
284
int2store(end+3, thd->server_status);
279
int2store(end+3, session->server_status);
285
280
memset(end+5, 0, 13);
287
282
/* write scramble tail */
288
end= strmake(end, thd->scramble + SCRAMBLE_LENGTH_323,
283
end= strmake(end, session->scramble + SCRAMBLE_LENGTH_323,
289
284
SCRAMBLE_LENGTH - SCRAMBLE_LENGTH_323) + 1;
291
286
/* At this point we write connection message and read reply */
292
if (net_write_command(net, (uchar) protocol_version, (uchar*) "", 0,
293
(uchar*) buff, (size_t) (end-buff)) ||
287
if (net_write_command(net, (unsigned char) protocol_version, (unsigned char*) "", 0,
288
(unsigned char*) buff, (size_t) (end-buff)) ||
294
289
(pkt_len= my_net_read(net)) == packet_error ||
295
290
pkt_len < MIN_HANDSHAKE_SIZE)
297
292
my_error(ER_HANDSHAKE_ERROR, MYF(0),
298
thd->main_security_ctx.ip);
293
session->main_security_ctx.ip);
302
if (thd->packet.alloc(thd->variables.net_buffer_length))
297
if (session->packet.alloc(session->variables.net_buffer_length))
303
298
return 1; /* The error is set by alloc(). */
305
thd->client_capabilities= uint2korr(net->read_pos);
308
thd->client_capabilities|= ((uint32_t) uint2korr(net->read_pos+2)) << 16;
309
thd->max_client_packet_length= uint4korr(net->read_pos+4);
310
thd_init_client_charset(thd, (uint) net->read_pos[8]);
311
thd->update_charset();
300
session->client_capabilities= uint2korr(net->read_pos);
303
session->client_capabilities|= ((uint32_t) uint2korr(net->read_pos+2)) << 16;
304
session->max_client_packet_length= uint4korr(net->read_pos+4);
305
session_init_client_charset(session, (uint) net->read_pos[8]);
306
session->update_charset();
312
307
end= (char*) net->read_pos+32;
315
310
Disable those bits which are not supported by the server.
316
311
This is a precautionary measure, if the client lies. See Bug#27944.
318
thd->client_capabilities&= server_capabilites;
313
session->client_capabilities&= server_capabilites;
320
315
if (end >= (char*) net->read_pos+ pkt_len +2)
323
my_error(ER_HANDSHAKE_ERROR, MYF(0), thd->main_security_ctx.ip);
318
my_error(ER_HANDSHAKE_ERROR, MYF(0), session->main_security_ctx.ip);
327
if (thd->client_capabilities & CLIENT_INTERACTIVE)
328
thd->variables.net_wait_timeout= thd->variables.net_interactive_timeout;
329
if ((thd->client_capabilities & CLIENT_TRANSACTIONS) &&
322
if (session->client_capabilities & CLIENT_INTERACTIVE)
323
session->variables.net_wait_timeout= session->variables.net_interactive_timeout;
324
if ((session->client_capabilities & CLIENT_TRANSACTIONS) &&
330
325
opt_using_transactions)
331
net->return_status= &thd->server_status;
326
net->return_status= &session->server_status;
334
329
char *passwd= strchr(user, '\0')+1;
335
uint user_len= passwd - user - 1;
330
uint32_t user_len= passwd - user - 1;
336
331
char *db= passwd;
337
332
char db_buff[NAME_LEN + 1]; // buffer to store db in utf8
338
333
char user_buff[USERNAME_LENGTH + 1]; // buffer to store user in utf8
334
uint32_t dummy_errors;
342
337
Old clients send null-terminated string as password; new clients send
348
343
Cast *passwd to an unsigned char, so that it doesn't extend the sign for
349
344
*passwd > 127 and become 2**32-127+ after casting to uint.
351
uint passwd_len= thd->client_capabilities & CLIENT_SECURE_CONNECTION ?
352
(uchar)(*passwd++) : strlen(passwd);
353
db= thd->client_capabilities & CLIENT_CONNECT_WITH_DB ?
346
uint32_t passwd_len= session->client_capabilities & CLIENT_SECURE_CONNECTION ?
347
(unsigned char)(*passwd++) : strlen(passwd);
348
db= session->client_capabilities & CLIENT_CONNECT_WITH_DB ?
354
349
db + passwd_len + 1 : 0;
355
350
/* strlen() can't be easily deleted without changing protocol */
356
uint db_len= db ? strlen(db) : 0;
351
uint32_t db_len= db ? strlen(db) : 0;
358
353
if (passwd + passwd_len + db_len > (char *)net->read_pos + pkt_len)
360
my_error(ER_HANDSHAKE_ERROR, MYF(0), thd->main_security_ctx.ip);
355
my_error(ER_HANDSHAKE_ERROR, MYF(0), session->main_security_ctx.ip);
387
if (thd->main_security_ctx.user)
388
x_free(thd->main_security_ctx.user);
389
if (!(thd->main_security_ctx.user= my_strdup(user, MYF(MY_WME))))
382
if (session->main_security_ctx.user)
383
if (session->main_security_ctx.user)
384
free(session->main_security_ctx.user);
385
if (!(session->main_security_ctx.user= my_strdup(user, MYF(MY_WME))))
390
386
return 1; /* The error is set by my_strdup(). */
391
return check_user(thd, COM_CONNECT, passwd, passwd_len, db, true);
387
return check_user(session, passwd, passwd_len, db, true);
467
463
This mainly updates status variables
470
void end_connection(THD *thd)
466
void end_connection(Session *session)
473
plugin_thdvar_cleanup(thd);
468
NET *net= &session->net;
469
plugin_sessionvar_cleanup(session);
475
if (thd->killed || (net->error && net->vio != 0))
471
if (session->killed || (net->error && net->vio != 0))
477
473
statistic_increment(aborted_threads,&LOCK_status);
480
476
if (net->error && net->vio != 0)
482
if (!thd->killed && thd->variables.log_warnings > 1)
478
if (!session->killed && session->variables.log_warnings > 1)
484
Security_context *sctx= thd->security_ctx;
480
Security_context *sctx= session->security_ctx;
486
482
sql_print_warning(ER(ER_NEW_ABORTING_CONNECTION),
487
thd->thread_id,(thd->db ? thd->db : "unconnected"),
483
session->thread_id,(session->db ? session->db : "unconnected"),
488
484
sctx->user ? sctx->user : "unauthenticated",
490
(thd->main_da.is_error() ? thd->main_da.message() :
486
(session->main_da.is_error() ? session->main_da.message() :
491
487
ER(ER_UNKNOWN_ERROR)));
498
Initialize THD to handle queries
494
Initialize Session to handle queries
501
void prepare_new_connection_state(THD* thd)
497
void prepare_new_connection_state(Session* session)
503
Security_context *sctx= thd->security_ctx;
499
Security_context *sctx= session->security_ctx;
505
if (thd->variables.max_join_size == HA_POS_ERROR)
506
thd->options |= OPTION_BIG_SELECTS;
507
if (thd->client_capabilities & CLIENT_COMPRESS)
508
thd->net.compress=1; // Use compression
501
if (session->variables.max_join_size == HA_POS_ERROR)
502
session->options |= OPTION_BIG_SELECTS;
503
if (session->client_capabilities & CLIENT_COMPRESS)
504
session->net.compress=1; // Use compression
511
Much of this is duplicated in create_embedded_thd() for the
507
Much of this is duplicated in create_embedded_session() for the
512
508
embedded server library.
513
509
TODO: refactor this to avoid code duplication there
515
thd->version= refresh_version;
516
thd->set_proc_info(0);
517
thd->command= COM_SLEEP;
519
thd->init_for_queries();
511
session->version= refresh_version;
512
session->set_proc_info(0);
513
session->command= COM_SLEEP;
515
session->init_for_queries();
521
517
/* In the past this would only run of the user did not have SUPER_ACL */
522
518
if (sys_init_connect.value_length)
524
execute_init_command(thd, &sys_init_connect, &LOCK_sys_init_connect);
520
execute_init_command(session, &sys_init_connect, &LOCK_sys_init_connect);
521
if (session->is_error())
527
thd->killed= THD::KILL_CONNECTION;
523
session->killed= Session::KILL_CONNECTION;
528
524
sql_print_warning(ER(ER_NEW_ABORTING_CONNECTION),
529
thd->thread_id,(thd->db ? thd->db : "unconnected"),
525
session->thread_id,(session->db ? session->db : "unconnected"),
530
526
sctx->user ? sctx->user : "unauthenticated",
531
527
sctx->ip, "init_connect command failed");
532
sql_print_warning("%s", thd->main_da.message());
528
sql_print_warning("%s", session->main_da.message());
534
thd->set_proc_info(0);
536
thd->init_for_queries();
530
session->set_proc_info(0);
532
session->init_for_queries();
575
571
handle_one_connection() is normally the only way a thread would
576
572
start and would always be on the very high end of the stack ,
577
573
therefore, the thread stack always starts at the address of the
578
first local variable of handle_one_connection, which is thd. We
574
first local variable of handle_one_connection, which is session. We
579
575
need to know the start of the stack so that we could check for
582
thd->thread_stack= (char*) &thd;
583
if (setup_connection_thread_globals(thd))
578
session->thread_stack= (char*) &session;
579
if (setup_connection_thread_globals(session))
584
NET *net= &session->net;
590
if (login_connection(thd))
586
if (login_connection(session))
593
prepare_new_connection_state(thd);
589
prepare_new_connection_state(session);
595
591
while (!net->error && net->vio != 0 &&
596
!(thd->killed == THD::KILL_CONNECTION))
592
!(session->killed == Session::KILL_CONNECTION))
594
if (do_command(session))
597
end_connection(session);
604
close_connection(thd, 0, 1);
605
if (thread_scheduler.end_thread(thd,1))
600
close_connection(session, 0, 1);
601
if (thread_scheduler.end_thread(session,1))
606
602
return 0; // Probably no-threads