~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/sql_connect.cc

  • Committer: Brian Aker
  • Date: 2008-08-12 03:12:57 UTC
  • Revision ID: brian@tangent.org-20080812031257-ln3uk87y1r22byeg
First pass of new sql_db.cc work

Show diffs side-by-side

added added

removed removed

Lines of Context:
76
76
*/
77
77
 
78
78
int
79
 
check_user(THD *thd, const char *passwd,
80
 
           uint32_t passwd_len, const char *db,
 
79
check_user(THD *thd, enum enum_server_command command,
 
80
           const char *passwd,
 
81
           uint passwd_len, const char *db,
81
82
           bool check_count)
82
83
{
83
84
  LEX_STRING db_str= { (char *) db, db ? strlen(db) : 0 };
118
119
  {
119
120
    pthread_mutex_lock(&LOCK_connection_count);
120
121
    bool count_ok= connection_count <= max_connections;
121
 
    pthread_mutex_unlock(&LOCK_connection_count);
 
122
    VOID(pthread_mutex_unlock(&LOCK_connection_count));
122
123
 
123
124
    if (!count_ok)
124
125
    {                                         // too many connections
127
128
    }
128
129
  }
129
130
 
 
131
  /*
 
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
 
134
    break-in attempts.
 
135
  */
 
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*) "");
 
141
 
130
142
  /* Change database if necessary */
131
143
  if (db && db[0])
132
144
  {
148
160
  started with corresponding variable that is greater then 0.
149
161
*/
150
162
 
151
 
extern "C" unsigned char *get_key_conn(user_conn *buff, size_t *length,
 
163
extern "C" uchar *get_key_conn(user_conn *buff, size_t *length,
152
164
                               bool not_used __attribute__((unused)))
153
165
{
154
166
  *length= buff->len;
155
 
  return (unsigned char*) buff->user;
 
167
  return (uchar*) buff->user;
156
168
}
157
169
 
158
170
 
159
171
extern "C" void free_user(struct user_conn *uc)
160
172
{
161
 
  free((char*) uc);
 
173
  my_free((char*) uc,MYF(0));
162
174
}
163
175
 
164
 
void thd_init_client_charset(THD *thd, uint32_t cs_number)
 
176
void thd_init_client_charset(THD *thd, uint cs_number)
165
177
{
166
178
  /*
167
179
   Use server character set and collation if
172
184
  */
173
185
  if (!opt_character_set_client_handshake ||
174
186
      !(thd->variables.character_set_client= get_charset(cs_number, MYF(0))) ||
175
 
      !my_strcasecmp(&my_charset_utf8_general_ci,
 
187
      !my_strcasecmp(&my_charset_latin1,
176
188
                     global_system_variables.character_set_client->name,
177
189
                     thd->variables.character_set_client->name))
178
190
  {
224
236
  uint32_t pkt_len= 0;
225
237
  char *end;
226
238
 
 
239
#ifdef SIGNAL_WITH_VIO_CLOSE
 
240
  thd->set_active_vio(net->vio);
 
241
#endif
 
242
 
227
243
  // TCP/IP connection
228
244
  {
229
245
    char ip[NI_MAXHOST];
230
246
 
231
 
    if (net_peer_addr(net, ip, &thd->peer_port, NI_MAXHOST))
 
247
    if (vio_peer_addr(net->vio, ip, &thd->peer_port, NI_MAXHOST))
232
248
    {
233
249
      my_error(ER_BAD_HOST_ERROR, MYF(0), thd->main_security_ctx.ip);
234
250
      return 1;
236
252
    if (!(thd->main_security_ctx.ip= my_strdup(ip,MYF(MY_WME))))
237
253
      return 1; /* The error is set by my_strdup(). */
238
254
  }
239
 
  net_keepalive(net, true);
 
255
  vio_keepalive(net->vio, true);
240
256
  
241
257
  uint32_t server_capabilites;
242
258
  {
250
266
    server_capabilites|= CLIENT_COMPRESS;
251
267
#endif /* HAVE_COMPRESS */
252
268
 
253
 
    end= my_stpncpy(buff, server_version, SERVER_VERSION_LENGTH) + 1;
254
 
    int4store((unsigned char*) end, thd->thread_id);
 
269
    end= stpncpy(buff, server_version, SERVER_VERSION_LENGTH) + 1;
 
270
    int4store((uchar*) end, thd->thread_id);
255
271
    end+= 4;
256
272
    /*
257
273
      So as check_connection is the only entry point to authorization
277
293
                 SCRAMBLE_LENGTH - SCRAMBLE_LENGTH_323) + 1;
278
294
 
279
295
    /* At this point we write connection message and read reply */
280
 
    if (net_write_command(net, (unsigned char) protocol_version, (unsigned char*) "", 0,
281
 
                          (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)) ||
282
298
        (pkt_len= my_net_read(net)) == packet_error ||
283
299
        pkt_len < MIN_HANDSHAKE_SIZE)
284
300
    {
305
321
  */
306
322
  thd->client_capabilities&= server_capabilites;
307
323
 
 
324
  if (thd->client_capabilities & CLIENT_IGNORE_SPACE)
 
325
    thd->variables.sql_mode|= MODE_IGNORE_SPACE;
 
326
 
308
327
  if (end >= (char*) net->read_pos+ pkt_len +2)
309
328
  {
310
329
 
319
338
    net->return_status= &thd->server_status;
320
339
 
321
340
  char *user= end;
322
 
  char *passwd= strchr(user, '\0')+1;
323
 
  uint32_t user_len= passwd - user - 1;
 
341
  char *passwd= strend(user)+1;
 
342
  uint user_len= passwd - user - 1;
324
343
  char *db= passwd;
325
344
  char db_buff[NAME_LEN + 1];           // buffer to store db in utf8
326
345
  char user_buff[USERNAME_LENGTH + 1];  // buffer to store user in utf8
327
 
  uint32_t dummy_errors;
 
346
  uint dummy_errors;
328
347
 
329
348
  /*
330
349
    Old clients send null-terminated string as password; new clients send
336
355
    Cast *passwd to an unsigned char, so that it doesn't extend the sign for
337
356
    *passwd > 127 and become 2**32-127+ after casting to uint.
338
357
  */
339
 
  uint32_t passwd_len= thd->client_capabilities & CLIENT_SECURE_CONNECTION ?
340
 
    (unsigned char)(*passwd++) : strlen(passwd);
 
358
  uint passwd_len= thd->client_capabilities & CLIENT_SECURE_CONNECTION ?
 
359
    (uchar)(*passwd++) : strlen(passwd);
341
360
  db= thd->client_capabilities & CLIENT_CONNECT_WITH_DB ?
342
361
    db + passwd_len + 1 : 0;
343
362
  /* strlen() can't be easily deleted without changing protocol */
344
 
  uint32_t db_len= db ? strlen(db) : 0;
 
363
  uint db_len= db ? strlen(db) : 0;
345
364
 
346
365
  if (passwd + passwd_len + db_len > (char *)net->read_pos + pkt_len)
347
366
  {
373
392
  }
374
393
 
375
394
  if (thd->main_security_ctx.user)
376
 
    if (thd->main_security_ctx.user)
377
 
      free(thd->main_security_ctx.user);
 
395
    x_free(thd->main_security_ctx.user);
378
396
  if (!(thd->main_security_ctx.user= my_strdup(user, MYF(MY_WME))))
379
397
    return 1; /* The error is set by my_strdup(). */
380
 
  return check_user(thd, passwd, passwd_len, db, true);
 
398
  return check_user(thd, COM_CONNECT, passwd, passwd_len, db, true);
381
399
}
382
400
 
383
401
 
502
520
    TODO: refactor this to avoid code duplication there
503
521
  */
504
522
  thd->version= refresh_version;
505
 
  thd->set_proc_info(0);
 
523
  thd->proc_info= 0;
506
524
  thd->command= COM_SLEEP;
507
525
  thd->set_time();
508
526
  thd->init_for_queries();
520
538
                        sctx->ip, "init_connect command failed");
521
539
      sql_print_warning("%s", thd->main_da.message());
522
540
    }
523
 
    thd->set_proc_info(0);
 
541
    thd->proc_info=0;
524
542
    thd->set_time();
525
543
    thd->init_for_queries();
526
544
  }