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))) ||
182
!my_strcasecmp(&my_charset_utf8_general_ci,
186
!(thd->variables.character_set_client= get_charset(cs_number, MYF(0))) ||
187
!my_strcasecmp(&my_charset_latin1,
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;
216
Perform handshake, authorize client and update session ACL variables.
221
Perform handshake, authorize client and update thd ACL variables.
219
224
check_connection()
220
session thread handle
223
0 success, OK is sent to user, session is updated.
228
0 success, OK is sent to user, thd is updated.
224
229
-1 error, which is sent to user
225
230
> 0 error code (not sent to user)
228
static int check_connection(Session *session)
233
static int check_connection(THD *thd)
230
NET *net= &session->net;
231
236
uint32_t pkt_len= 0;
239
#ifdef SIGNAL_WITH_VIO_CLOSE
240
thd->set_active_vio(net->vio);
234
243
// TCP/IP connection
236
245
char ip[NI_MAXHOST];
238
if (net_peer_addr(net, ip, &session->peer_port, NI_MAXHOST))
247
if (vio_peer_addr(net->vio, ip, &thd->peer_port, NI_MAXHOST))
240
my_error(ER_BAD_HOST_ERROR, MYF(0), session->main_security_ctx.ip);
249
my_error(ER_BAD_HOST_ERROR, MYF(0), thd->main_security_ctx.ip);
243
if (!(session->main_security_ctx.ip= my_strdup(ip,MYF(MY_WME))))
252
if (!(thd->main_security_ctx.ip= my_strdup(ip,MYF(MY_WME))))
244
253
return 1; /* The error is set by my_strdup(). */
246
net_keepalive(net, true);
255
vio_keepalive(net->vio, true);
248
257
uint32_t server_capabilites;
257
266
server_capabilites|= CLIENT_COMPRESS;
258
267
#endif /* HAVE_COMPRESS */
260
end= my_stpncpy(buff, server_version, SERVER_VERSION_LENGTH) + 1;
261
int4store((unsigned char*) end, session->thread_id);
269
end= stpncpy(buff, server_version, SERVER_VERSION_LENGTH) + 1;
270
int4store((uchar*) end, thd->thread_id);
264
273
So as check_connection is the only entry point to authorization
265
274
procedure, scramble is set here. This gives us new scramble for
268
create_random_string(session->scramble, SCRAMBLE_LENGTH, &session->rand);
277
create_random_string(thd->scramble, SCRAMBLE_LENGTH, &thd->rand);
270
279
Old clients does not understand long scrambles, but can ignore packet
271
280
tail: that's why first part of the scramble is placed here, and second
272
281
part at the end of packet.
274
end= strmake(end, session->scramble, SCRAMBLE_LENGTH_323) + 1;
283
end= strmake(end, thd->scramble, SCRAMBLE_LENGTH_323) + 1;
276
285
int2store(end, server_capabilites);
277
286
/* write server characteristics: up to 16 bytes allowed */
278
287
end[2]=(char) default_charset_info->number;
279
int2store(end+3, session->server_status);
288
int2store(end+3, thd->server_status);
280
289
memset(end+5, 0, 13);
282
291
/* write scramble tail */
283
end= strmake(end, session->scramble + SCRAMBLE_LENGTH_323,
292
end= strmake(end, thd->scramble + SCRAMBLE_LENGTH_323,
284
293
SCRAMBLE_LENGTH - SCRAMBLE_LENGTH_323) + 1;
286
295
/* 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)) ||
296
if (net_write_command(net, (uchar) protocol_version, (uchar*) "", 0,
297
(uchar*) buff, (size_t) (end-buff)) ||
289
298
(pkt_len= my_net_read(net)) == packet_error ||
290
299
pkt_len < MIN_HANDSHAKE_SIZE)
292
301
my_error(ER_HANDSHAKE_ERROR, MYF(0),
293
session->main_security_ctx.ip);
302
thd->main_security_ctx.ip);
297
if (session->packet.alloc(session->variables.net_buffer_length))
306
if (thd->packet.alloc(thd->variables.net_buffer_length))
298
307
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();
309
thd->client_capabilities= uint2korr(net->read_pos);
312
thd->client_capabilities|= ((uint32_t) uint2korr(net->read_pos+2)) << 16;
313
thd->max_client_packet_length= uint4korr(net->read_pos+4);
314
thd_init_client_charset(thd, (uint) net->read_pos[8]);
315
thd->update_charset();
307
316
end= (char*) net->read_pos+32;
310
319
Disable those bits which are not supported by the server.
311
320
This is a precautionary measure, if the client lies. See Bug#27944.
313
session->client_capabilities&= server_capabilites;
322
thd->client_capabilities&= server_capabilites;
315
324
if (end >= (char*) net->read_pos+ pkt_len +2)
318
my_error(ER_HANDSHAKE_ERROR, MYF(0), session->main_security_ctx.ip);
327
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) &&
331
if (thd->client_capabilities & CLIENT_INTERACTIVE)
332
thd->variables.net_wait_timeout= thd->variables.net_interactive_timeout;
333
if ((thd->client_capabilities & CLIENT_TRANSACTIONS) &&
325
334
opt_using_transactions)
326
net->return_status= &session->server_status;
335
net->return_status= &thd->server_status;
329
338
char *passwd= strchr(user, '\0')+1;
330
uint32_t user_len= passwd - user - 1;
339
uint user_len= passwd - user - 1;
331
340
char *db= passwd;
332
341
char db_buff[NAME_LEN + 1]; // buffer to store db in utf8
333
342
char user_buff[USERNAME_LENGTH + 1]; // buffer to store user in utf8
334
uint32_t dummy_errors;
337
346
Old clients send null-terminated string as password; new clients send
343
352
Cast *passwd to an unsigned char, so that it doesn't extend the sign for
344
353
*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 ?
355
uint passwd_len= thd->client_capabilities & CLIENT_SECURE_CONNECTION ?
356
(uchar)(*passwd++) : strlen(passwd);
357
db= thd->client_capabilities & CLIENT_CONNECT_WITH_DB ?
349
358
db + passwd_len + 1 : 0;
350
359
/* strlen() can't be easily deleted without changing protocol */
351
uint32_t db_len= db ? strlen(db) : 0;
360
uint db_len= db ? strlen(db) : 0;
353
362
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);
364
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))))
391
if (thd->main_security_ctx.user)
392
x_free(thd->main_security_ctx.user);
393
if (!(thd->main_security_ctx.user= my_strdup(user, MYF(MY_WME))))
386
394
return 1; /* The error is set by my_strdup(). */
387
return check_user(session, passwd, passwd_len, db, true);
395
return check_user(thd, COM_CONNECT, passwd, passwd_len, db, true);
463
471
This mainly updates status variables
466
void end_connection(Session *session)
474
void end_connection(THD *thd)
468
NET *net= &session->net;
469
plugin_sessionvar_cleanup(session);
477
plugin_thdvar_cleanup(thd);
471
if (session->killed || (net->error && net->vio != 0))
479
if (thd->killed || (net->error && net->vio != 0))
473
481
statistic_increment(aborted_threads,&LOCK_status);
476
484
if (net->error && net->vio != 0)
478
if (!session->killed && session->variables.log_warnings > 1)
486
if (!thd->killed && thd->variables.log_warnings > 1)
480
Security_context *sctx= session->security_ctx;
488
Security_context *sctx= thd->security_ctx;
482
490
sql_print_warning(ER(ER_NEW_ABORTING_CONNECTION),
483
session->thread_id,(session->db ? session->db : "unconnected"),
491
thd->thread_id,(thd->db ? thd->db : "unconnected"),
484
492
sctx->user ? sctx->user : "unauthenticated",
486
(session->main_da.is_error() ? session->main_da.message() :
494
(thd->main_da.is_error() ? thd->main_da.message() :
487
495
ER(ER_UNKNOWN_ERROR)));
494
Initialize Session to handle queries
502
Initialize THD to handle queries
497
void prepare_new_connection_state(Session* session)
505
void prepare_new_connection_state(THD* thd)
499
Security_context *sctx= session->security_ctx;
507
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
509
if (thd->variables.max_join_size == HA_POS_ERROR)
510
thd->options |= OPTION_BIG_SELECTS;
511
if (thd->client_capabilities & CLIENT_COMPRESS)
512
thd->net.compress=1; // Use compression
507
Much of this is duplicated in create_embedded_session() for the
515
Much of this is duplicated in create_embedded_thd() for the
508
516
embedded server library.
509
517
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();
519
thd->version= refresh_version;
520
thd->set_proc_info(0);
521
thd->command= COM_SLEEP;
523
thd->init_for_queries();
517
525
/* In the past this would only run of the user did not have SUPER_ACL */
518
526
if (sys_init_connect.value_length)
520
execute_init_command(session, &sys_init_connect, &LOCK_sys_init_connect);
521
if (session->is_error())
528
execute_init_command(thd, &sys_init_connect, &LOCK_sys_init_connect);
523
session->killed= Session::KILL_CONNECTION;
531
thd->killed= THD::KILL_CONNECTION;
524
532
sql_print_warning(ER(ER_NEW_ABORTING_CONNECTION),
525
session->thread_id,(session->db ? session->db : "unconnected"),
533
thd->thread_id,(thd->db ? thd->db : "unconnected"),
526
534
sctx->user ? sctx->user : "unauthenticated",
527
535
sctx->ip, "init_connect command failed");
528
sql_print_warning("%s", session->main_da.message());
536
sql_print_warning("%s", thd->main_da.message());
530
session->set_proc_info(0);
532
session->init_for_queries();
538
thd->set_proc_info(0);
540
thd->init_for_queries();
571
579
handle_one_connection() is normally the only way a thread would
572
580
start and would always be on the very high end of the stack ,
573
581
therefore, the thread stack always starts at the address of the
574
first local variable of handle_one_connection, which is session. We
582
first local variable of handle_one_connection, which is thd. We
575
583
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))
586
thd->thread_stack= (char*) &thd;
587
if (setup_connection_thread_globals(thd))
584
NET *net= &session->net;
586
if (login_connection(session))
594
if (login_connection(thd))
589
prepare_new_connection_state(session);
597
prepare_new_connection_state(thd);
591
599
while (!net->error && net->vio != 0 &&
592
!(session->killed == Session::KILL_CONNECTION))
600
!(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))
608
close_connection(thd, 0, 1);
609
if (thread_scheduler.end_thread(thd,1))
602
610
return 0; // Probably no-threads