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
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 */
86
check_user(Session *session, const char *passwd,
87
uint32_t passwd_len, const char *db,
79
check_user(THD *thd, enum enum_server_command command,
81
uint passwd_len, const char *db,
90
84
LEX_STRING db_str= { (char *) db, db ? strlen(db) : 0 };
91
85
bool is_authenticated;
94
Clear session->db as it points to something, that will be freed when
88
Clear thd->db as it points to something, that will be freed when
95
89
connection is closed. We don't want to accidentally free a wrong
96
90
pointer if connect failed. Also in case of 'CHANGE USER' failure,
97
91
current database will be switched to 'no database selected'.
99
session->reset_db(NULL, 0);
93
thd->reset_db(NULL, 0);
101
95
if (passwd_len != 0 && passwd_len != SCRAMBLE_LENGTH)
103
my_error(ER_HANDSHAKE_ERROR, MYF(0), session->main_security_ctx.ip);
97
my_error(ER_HANDSHAKE_ERROR, MYF(0), thd->main_security_ctx.ip);
107
is_authenticated= authenticate_user(session, passwd);
101
is_authenticated= authenticate_user(thd, passwd);
109
103
if (is_authenticated != true)
111
105
my_error(ER_ACCESS_DENIED_ERROR, MYF(0),
112
session->main_security_ctx.user,
113
session->main_security_ctx.ip,
106
thd->main_security_ctx.user,
107
thd->main_security_ctx.ip,
114
108
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*) "");
137
142
/* Change database if necessary */
140
if (mysql_change_db(session, &db_str, false))
145
if (mysql_change_db(thd, &db_str, false))
142
147
/* mysql_change_db() has pushed the error message. */
147
session->password= test(passwd_len); // remember for error messages
152
thd->password= test(passwd_len); // remember for error messages
148
153
/* Ready to handle queries */
178
183
- client character set doesn't exists in server
180
185
if (!opt_character_set_client_handshake ||
181
!(session->variables.character_set_client= get_charset(cs_number, MYF(0))) ||
186
!(thd->variables.character_set_client= get_charset(cs_number, MYF(0))) ||
182
187
!my_strcasecmp(&my_charset_utf8_general_ci,
183
188
global_system_variables.character_set_client->name,
184
session->variables.character_set_client->name))
189
thd->variables.character_set_client->name))
186
session->variables.character_set_client=
191
thd->variables.character_set_client=
187
192
global_system_variables.character_set_client;
188
session->variables.collation_connection=
193
thd->variables.collation_connection=
189
194
global_system_variables.collation_connection;
190
session->variables.character_set_results=
195
thd->variables.character_set_results=
191
196
global_system_variables.character_set_results;
195
session->variables.character_set_results=
196
session->variables.collation_connection=
197
session->variables.character_set_client;
200
thd->variables.character_set_results=
201
thd->variables.collation_connection=
202
thd->variables.character_set_client;
257
262
server_capabilites|= CLIENT_COMPRESS;
258
263
#endif /* HAVE_COMPRESS */
260
end= my_stpncpy(buff, server_version, SERVER_VERSION_LENGTH) + 1;
261
int4store((unsigned char*) end, session->thread_id);
265
end= stpncpy(buff, server_version, SERVER_VERSION_LENGTH) + 1;
266
int4store((uchar*) end, thd->thread_id);
264
269
So as check_connection is the only entry point to authorization
265
270
procedure, scramble is set here. This gives us new scramble for
268
create_random_string(session->scramble, SCRAMBLE_LENGTH, &session->rand);
273
create_random_string(thd->scramble, SCRAMBLE_LENGTH, &thd->rand);
270
275
Old clients does not understand long scrambles, but can ignore packet
271
276
tail: that's why first part of the scramble is placed here, and second
272
277
part at the end of packet.
274
end= strmake(end, session->scramble, SCRAMBLE_LENGTH_323) + 1;
279
end= strmake(end, thd->scramble, SCRAMBLE_LENGTH_323) + 1;
276
281
int2store(end, server_capabilites);
277
282
/* write server characteristics: up to 16 bytes allowed */
278
283
end[2]=(char) default_charset_info->number;
279
int2store(end+3, session->server_status);
284
int2store(end+3, thd->server_status);
280
285
memset(end+5, 0, 13);
282
287
/* write scramble tail */
283
end= strmake(end, session->scramble + SCRAMBLE_LENGTH_323,
288
end= strmake(end, thd->scramble + SCRAMBLE_LENGTH_323,
284
289
SCRAMBLE_LENGTH - SCRAMBLE_LENGTH_323) + 1;
286
291
/* At this point we write connection message and read reply */
287
if (net_write_command(net, (unsigned char) protocol_version, (unsigned char*) "", 0,
288
(unsigned char*) buff, (size_t) (end-buff)) ||
292
if (net_write_command(net, (uchar) protocol_version, (uchar*) "", 0,
293
(uchar*) buff, (size_t) (end-buff)) ||
289
294
(pkt_len= my_net_read(net)) == packet_error ||
290
295
pkt_len < MIN_HANDSHAKE_SIZE)
292
297
my_error(ER_HANDSHAKE_ERROR, MYF(0),
293
session->main_security_ctx.ip);
298
thd->main_security_ctx.ip);
297
if (session->packet.alloc(session->variables.net_buffer_length))
302
if (thd->packet.alloc(thd->variables.net_buffer_length))
298
303
return 1; /* The error is set by alloc(). */
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();
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();
307
312
end= (char*) net->read_pos+32;
310
315
Disable those bits which are not supported by the server.
311
316
This is a precautionary measure, if the client lies. See Bug#27944.
313
session->client_capabilities&= server_capabilites;
318
thd->client_capabilities&= server_capabilites;
315
320
if (end >= (char*) net->read_pos+ pkt_len +2)
318
my_error(ER_HANDSHAKE_ERROR, MYF(0), session->main_security_ctx.ip);
323
my_error(ER_HANDSHAKE_ERROR, MYF(0), thd->main_security_ctx.ip);
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) &&
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) &&
325
330
opt_using_transactions)
326
net->return_status= &session->server_status;
331
net->return_status= &thd->server_status;
329
334
char *passwd= strchr(user, '\0')+1;
330
uint32_t user_len= passwd - user - 1;
335
uint user_len= passwd - user - 1;
331
336
char *db= passwd;
332
337
char db_buff[NAME_LEN + 1]; // buffer to store db in utf8
333
338
char user_buff[USERNAME_LENGTH + 1]; // buffer to store user in utf8
334
uint32_t dummy_errors;
337
342
Old clients send null-terminated string as password; new clients send
343
348
Cast *passwd to an unsigned char, so that it doesn't extend the sign for
344
349
*passwd > 127 and become 2**32-127+ after casting to uint.
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 ?
351
uint passwd_len= thd->client_capabilities & CLIENT_SECURE_CONNECTION ?
352
(uchar)(*passwd++) : strlen(passwd);
353
db= thd->client_capabilities & CLIENT_CONNECT_WITH_DB ?
349
354
db + passwd_len + 1 : 0;
350
355
/* strlen() can't be easily deleted without changing protocol */
351
uint32_t db_len= db ? strlen(db) : 0;
356
uint db_len= db ? strlen(db) : 0;
353
358
if (passwd + passwd_len + db_len > (char *)net->read_pos + pkt_len)
355
my_error(ER_HANDSHAKE_ERROR, MYF(0), session->main_security_ctx.ip);
360
my_error(ER_HANDSHAKE_ERROR, MYF(0), thd->main_security_ctx.ip);
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))))
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))))
386
390
return 1; /* The error is set by my_strdup(). */
387
return check_user(session, passwd, passwd_len, db, true);
391
return check_user(thd, COM_CONNECT, passwd, passwd_len, db, true);
463
467
This mainly updates status variables
466
void end_connection(Session *session)
470
void end_connection(THD *thd)
468
NET *net= &session->net;
469
plugin_sessionvar_cleanup(session);
473
plugin_thdvar_cleanup(thd);
471
if (session->killed || (net->error && net->vio != 0))
475
if (thd->killed || (net->error && net->vio != 0))
473
477
statistic_increment(aborted_threads,&LOCK_status);
476
480
if (net->error && net->vio != 0)
478
if (!session->killed && session->variables.log_warnings > 1)
482
if (!thd->killed && thd->variables.log_warnings > 1)
480
Security_context *sctx= session->security_ctx;
484
Security_context *sctx= thd->security_ctx;
482
486
sql_print_warning(ER(ER_NEW_ABORTING_CONNECTION),
483
session->thread_id,(session->db ? session->db : "unconnected"),
487
thd->thread_id,(thd->db ? thd->db : "unconnected"),
484
488
sctx->user ? sctx->user : "unauthenticated",
486
(session->main_da.is_error() ? session->main_da.message() :
490
(thd->main_da.is_error() ? thd->main_da.message() :
487
491
ER(ER_UNKNOWN_ERROR)));
494
Initialize Session to handle queries
498
Initialize THD to handle queries
497
void prepare_new_connection_state(Session* session)
501
void prepare_new_connection_state(THD* thd)
499
Security_context *sctx= session->security_ctx;
503
Security_context *sctx= thd->security_ctx;
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
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
507
Much of this is duplicated in create_embedded_session() for the
511
Much of this is duplicated in create_embedded_thd() for the
508
512
embedded server library.
509
513
TODO: refactor this to avoid code duplication there
511
session->version= refresh_version;
512
session->set_proc_info(0);
513
session->command= COM_SLEEP;
515
session->init_for_queries();
515
thd->version= refresh_version;
516
thd->set_proc_info(0);
517
thd->command= COM_SLEEP;
519
thd->init_for_queries();
517
521
/* In the past this would only run of the user did not have SUPER_ACL */
518
522
if (sys_init_connect.value_length)
520
execute_init_command(session, &sys_init_connect, &LOCK_sys_init_connect);
521
if (session->is_error())
524
execute_init_command(thd, &sys_init_connect, &LOCK_sys_init_connect);
523
session->killed= Session::KILL_CONNECTION;
527
thd->killed= THD::KILL_CONNECTION;
524
528
sql_print_warning(ER(ER_NEW_ABORTING_CONNECTION),
525
session->thread_id,(session->db ? session->db : "unconnected"),
529
thd->thread_id,(thd->db ? thd->db : "unconnected"),
526
530
sctx->user ? sctx->user : "unauthenticated",
527
531
sctx->ip, "init_connect command failed");
528
sql_print_warning("%s", session->main_da.message());
532
sql_print_warning("%s", thd->main_da.message());
530
session->set_proc_info(0);
532
session->init_for_queries();
534
thd->set_proc_info(0);
536
thd->init_for_queries();
571
575
handle_one_connection() is normally the only way a thread would
572
576
start and would always be on the very high end of the stack ,
573
577
therefore, the thread stack always starts at the address of the
574
first local variable of handle_one_connection, which is session. We
578
first local variable of handle_one_connection, which is thd. We
575
579
need to know the start of the stack so that we could check for
578
session->thread_stack= (char*) &session;
579
if (setup_connection_thread_globals(session))
582
thd->thread_stack= (char*) &thd;
583
if (setup_connection_thread_globals(thd))
584
NET *net= &session->net;
586
if (login_connection(session))
590
if (login_connection(thd))
589
prepare_new_connection_state(session);
593
prepare_new_connection_state(thd);
591
595
while (!net->error && net->vio != 0 &&
592
!(session->killed == Session::KILL_CONNECTION))
596
!(thd->killed == THD::KILL_CONNECTION))
594
if (do_command(session))
597
end_connection(session);
600
close_connection(session, 0, 1);
601
if (thread_scheduler.end_thread(session,1))
604
close_connection(thd, 0, 1);
605
if (thread_scheduler.end_thread(thd,1))
602
606
return 0; // Probably no-threads