~ubuntu-branches/ubuntu/maverick/mysql-5.1/maverick-proposed

« back to all changes in this revision

Viewing changes to sql/sql_connect.cc

  • Committer: Package Import Robot
  • Author(s): Marc Deslauriers
  • Date: 2012-02-22 14:16:05 UTC
  • mto: This revision was merged to the branch mainline in revision 20.
  • Revision ID: package-import@ubuntu.com-20120222141605-nxlu9yzc6attylc2
Tags: upstream-5.1.61
ImportĀ upstreamĀ versionĀ 5.1.61

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* Copyright (C) 2007 MySQL AB
 
1
/*
 
2
   Copyright (c) 2007, 2011, Oracle and/or its affiliates. All rights reserved.
2
3
 
3
4
   This program is free software; you can redistribute it and/or modify
4
5
   it under the terms of the GNU General Public License as published by
11
12
 
12
13
   You should have received a copy of the GNU General Public License
13
14
   along with this program; if not, write to the Free Software
14
 
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
15
 
 
 
15
   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA
 
16
*/
16
17
 
17
18
/*
18
19
  Functions to autenticate and handle reqests for a connection
20
21
 
21
22
#include "mysql_priv.h"
22
23
 
23
 
#ifdef HAVE_OPENSSL
24
 
/*
25
 
  Without SSL the handshake consists of one packet. This packet
26
 
  has both client capabilites and scrambled password.
27
 
  With SSL the handshake might consist of two packets. If the first
28
 
  packet (client capabilities) has CLIENT_SSL flag set, we have to
29
 
  switch to SSL and read the second packet. The scrambled password
30
 
  is in the second packet and client_capabilites field will be ignored.
31
 
  Maybe it is better to accept flags other than CLIENT_SSL from the
32
 
  second packet?
33
 
*/
34
 
#define SSL_HANDSHAKE_SIZE      2
35
 
#define NORMAL_HANDSHAKE_SIZE   6
36
 
#define MIN_HANDSHAKE_SIZE      2
37
 
#else
38
 
#define MIN_HANDSHAKE_SIZE      6
39
 
#endif /* HAVE_OPENSSL */
 
24
/** Size of the header fields of an authentication packet. */
 
25
#define AUTH_PACKET_HEADER_SIZE_PROTO_41    32
 
26
#define AUTH_PACKET_HEADER_SIZE_PROTO_40    5  
 
27
#define AUTH_PACKET_HEADER_SIZE_CONNJ_SSL   4
40
28
 
41
29
#ifdef __WIN__
42
30
extern void win_install_sigabrt_handler();
342
330
      passwd_len != SCRAMBLE_LENGTH &&
343
331
      passwd_len != SCRAMBLE_LENGTH_323)
344
332
  {
345
 
    my_error(ER_HANDSHAKE_ERROR, MYF(0), thd->main_security_ctx.host_or_ip);
 
333
    my_error(ER_HANDSHAKE_ERROR, MYF(0));
346
334
    DBUG_RETURN(1);
347
335
  }
348
336
 
373
361
        my_net_read(net) != SCRAMBLE_LENGTH_323 + 1)
374
362
    {
375
363
      inc_host_errors(&thd->remote.sin_addr);
376
 
      my_error(ER_HANDSHAKE_ERROR, MYF(0), thd->main_security_ctx.host_or_ip);
 
364
      my_error(ER_HANDSHAKE_ERROR, MYF(0));
377
365
      DBUG_RETURN(1);
378
366
    }
379
367
    /* Final attempt to check the user based on reply */
582
570
}
583
571
 
584
572
 
585
 
void thd_init_client_charset(THD *thd, uint cs_number)
 
573
/**
 
574
  Set thread character set variables from the given ID
 
575
 
 
576
  @param  thd         thread handle
 
577
  @param  cs_number   character set and collation ID
 
578
 
 
579
  @retval  0  OK; character_set_client, collation_connection and
 
580
              character_set_results are set to the new value,
 
581
              or to the default global values.
 
582
 
 
583
  @retval  1  error, e.g. the given ID is not supported by parser.
 
584
              Corresponding SQL error is sent.
 
585
*/
 
586
 
 
587
bool thd_init_client_charset(THD *thd, uint cs_number)
586
588
{
 
589
  CHARSET_INFO *cs;
587
590
  /*
588
591
   Use server character set and collation if
589
592
   - opt_character_set_client_handshake is not set
592
595
   - client character set doesn't exists in server
593
596
  */
594
597
  if (!opt_character_set_client_handshake ||
595
 
      !(thd->variables.character_set_client= get_charset(cs_number, MYF(0))) ||
 
598
      !(cs= get_charset(cs_number, MYF(0))) ||
596
599
      !my_strcasecmp(&my_charset_latin1,
597
600
                     global_system_variables.character_set_client->name,
598
 
                     thd->variables.character_set_client->name))
 
601
                     cs->name))
599
602
  {
600
603
    thd->variables.character_set_client=
601
604
      global_system_variables.character_set_client;
606
609
  }
607
610
  else
608
611
  {
 
612
    if (!is_supported_parser_charset(cs))
 
613
    {
 
614
      /* Disallow non-supported parser character sets: UCS2, UTF16, UTF32 */
 
615
      my_error(ER_WRONG_VALUE_FOR_VAR, MYF(0), "character_set_client",
 
616
               cs->csname);
 
617
      return true;
 
618
    }
609
619
    thd->variables.character_set_results=
610
620
      thd->variables.collation_connection= 
611
 
      thd->variables.character_set_client;
 
621
      thd->variables.character_set_client= cs;
612
622
  }
 
623
  return false;
613
624
}
614
625
 
615
626
 
630
641
  return 0;
631
642
}
632
643
 
 
644
#ifndef EMBEDDED_LIBRARY
 
645
 
 
646
/** Get a string according to the protocol of the underlying buffer. */
 
647
typedef char * (*get_proto_string_func_t) (char **, size_t *, size_t *);
 
648
 
 
649
/**
 
650
  Get a string formatted according to the 4.1 version of the MySQL protocol.
 
651
 
 
652
  @param buffer[in, out]    Pointer to the user-supplied buffer to be scanned.
 
653
  @param max_bytes_available[in, out]  Limit the bytes to scan.
 
654
  @param string_length[out] The number of characters scanned not including
 
655
                            the null character.
 
656
 
 
657
  @remark Strings are always null character terminated in this version of the
 
658
          protocol.
 
659
 
 
660
  @remark The string_length does not include the terminating null character.
 
661
          However, after the call, the buffer is increased by string_length+1
 
662
          bytes, beyond the null character if there still available bytes to
 
663
          scan.
 
664
 
 
665
  @return pointer to beginning of the string scanned.
 
666
    @retval NULL The buffer content is malformed
 
667
*/
 
668
 
 
669
static
 
670
char *get_41_protocol_string(char **buffer,
 
671
                             size_t *max_bytes_available,
 
672
                             size_t *string_length)
 
673
{
 
674
  char *str= (char *)memchr(*buffer, '\0', *max_bytes_available);
 
675
 
 
676
  if (str == NULL)
 
677
    return NULL;
 
678
 
 
679
  *string_length= (size_t)(str - *buffer);
 
680
  *max_bytes_available-= *string_length + 1;
 
681
  str= *buffer;
 
682
  *buffer += *string_length + 1;
 
683
 
 
684
  return str;
 
685
}
 
686
 
 
687
 
 
688
/**
 
689
  Get a string formatted according to the 4.0 version of the MySQL protocol.
 
690
 
 
691
  @param buffer[in, out]    Pointer to the user-supplied buffer to be scanned.
 
692
  @param max_bytes_available[in, out]  Limit the bytes to scan.
 
693
  @param string_length[out] The number of characters scanned not including
 
694
                            the null character.
 
695
 
 
696
  @remark If there are not enough bytes left after the current position of
 
697
          the buffer to satisfy the current string, the string is considered
 
698
          to be empty and a pointer to empty_c_string is returned.
 
699
 
 
700
  @remark A string at the end of the packet is not null terminated.
 
701
 
 
702
  @return Pointer to beginning of the string scanned, or a pointer to a empty
 
703
          string.
 
704
*/
 
705
static
 
706
char *get_40_protocol_string(char **buffer,
 
707
                             size_t *max_bytes_available,
 
708
                             size_t *string_length)
 
709
{
 
710
  char *str;
 
711
  size_t len;
 
712
 
 
713
  /* No bytes to scan left, treat string as empty. */
 
714
  if ((*max_bytes_available) == 0)
 
715
  {
 
716
    *string_length= 0;
 
717
    return empty_c_string;
 
718
  }
 
719
 
 
720
  str= (char *) memchr(*buffer, '\0', *max_bytes_available);
 
721
 
 
722
  /*
 
723
    If the string was not null terminated by the client,
 
724
    the remainder of the packet is the string. Otherwise,
 
725
    advance the buffer past the end of the null terminated
 
726
    string.
 
727
  */
 
728
  if (str == NULL)
 
729
    len= *string_length= *max_bytes_available;
 
730
  else
 
731
    len= (*string_length= (size_t)(str - *buffer)) + 1;
 
732
 
 
733
  str= *buffer;
 
734
  *buffer+= len;
 
735
  *max_bytes_available-= len;
 
736
 
 
737
  return str;
 
738
}
 
739
 
 
740
 
 
741
/**
 
742
  Get a length encoded string from a user-supplied buffer.
 
743
 
 
744
  @param buffer[in, out] The buffer to scan; updates position after scan.
 
745
  @param max_bytes_available[in, out] Limit the number of bytes to scan
 
746
  @param string_length[out] Number of characters scanned
 
747
 
 
748
  @remark In case the length is zero, then the total size of the string is
 
749
    considered to be 1 byte; the size byte.
 
750
 
 
751
  @return pointer to first byte after the header in buffer.
 
752
    @retval NULL The buffer content is malformed
 
753
*/
 
754
 
 
755
static
 
756
char *get_length_encoded_string(char **buffer,
 
757
                                size_t *max_bytes_available,
 
758
                                size_t *string_length)
 
759
{
 
760
  if (*max_bytes_available == 0)
 
761
    return NULL;
 
762
 
 
763
  /* Do double cast to prevent overflow from signed / unsigned conversion */
 
764
  size_t str_len= (size_t)(unsigned char)**buffer;
 
765
 
 
766
  /*
 
767
    If the length encoded string has the length 0
 
768
    the total size of the string is only one byte long (the size byte)
 
769
  */
 
770
  if (str_len == 0)
 
771
  {
 
772
    ++*buffer;
 
773
    *string_length= 0;
 
774
    /*
 
775
      Return a pointer to the 0 character so the return value will be
 
776
      an empty string.
 
777
    */
 
778
    return *buffer-1;
 
779
  }
 
780
 
 
781
  if (str_len >= *max_bytes_available)
 
782
    return NULL;
 
783
 
 
784
  char *str= *buffer+1;
 
785
  *string_length= str_len;
 
786
  *max_bytes_available-= *string_length + 1;
 
787
  *buffer+= *string_length + 1;
 
788
  return str;
 
789
}
 
790
 
 
791
 
633
792
/*
634
793
  Perform handshake, authorize client and update thd ACL variables.
635
794
 
643
802
   > 0  error code (not sent to user)
644
803
*/
645
804
 
646
 
#ifndef EMBEDDED_LIBRARY
647
805
static int check_connection(THD *thd)
648
806
{
649
807
  uint connect_errors= 0;
651
809
  ulong pkt_len= 0;
652
810
  char *end;
653
811
 
 
812
  bool packet_has_required_size= false;
 
813
  char *db;
 
814
  size_t db_len;
 
815
  char *passwd;
 
816
  size_t passwd_len;
 
817
  char *user;
 
818
  size_t user_len;
 
819
 
654
820
  DBUG_PRINT("info",
655
821
             ("New connection received on %s", vio_description(net->vio)));
656
822
#ifdef SIGNAL_WITH_VIO_CLOSE
663
829
 
664
830
    if (vio_peer_addr(net->vio, ip, &thd->peer_port))
665
831
    {
666
 
      my_error(ER_BAD_HOST_ERROR, MYF(0), thd->main_security_ctx.host_or_ip);
 
832
      my_error(ER_BAD_HOST_ERROR, MYF(0));
667
833
      return 1;
668
834
    }
669
835
    if (!(thd->main_security_ctx.ip= my_strdup(ip,MYF(MY_WME))))
745
911
      part at the end of packet.
746
912
    */
747
913
    end= strmake(end, thd->scramble, SCRAMBLE_LENGTH_323) + 1;
748
 
   
 
914
 
749
915
    int2store(end, server_capabilites);
750
916
    /* write server characteristics: up to 16 bytes allowed */
751
917
    end[2]=(char) default_charset_info->number;
759
925
    /* At this point we write connection message and read reply */
760
926
    if (net_write_command(net, (uchar) protocol_version, (uchar*) "", 0,
761
927
                          (uchar*) buff, (size_t) (end-buff)) ||
762
 
        (pkt_len= my_net_read(net)) == packet_error ||
763
 
        pkt_len < MIN_HANDSHAKE_SIZE)
 
928
        (pkt_len= my_net_read(net)) == packet_error)
764
929
    {
765
930
      inc_host_errors(&thd->remote.sin_addr);
766
 
      my_error(ER_HANDSHAKE_ERROR, MYF(0),
767
 
               thd->main_security_ctx.host_or_ip);
 
931
      my_error(ER_HANDSHAKE_ERROR, MYF(0));
768
932
      return 1;
769
933
    }
770
934
  }
776
940
  if (thd->packet.alloc(thd->variables.net_buffer_length))
777
941
    return 1; /* The error is set by alloc(). */
778
942
 
779
 
  thd->client_capabilities= uint2korr(net->read_pos);
780
 
  if (thd->client_capabilities & CLIENT_PROTOCOL_41)
781
 
  {
782
 
    thd->client_capabilities|= ((ulong) uint2korr(net->read_pos+2)) << 16;
783
 
    thd->max_client_packet_length= uint4korr(net->read_pos+4);
784
 
    DBUG_PRINT("info", ("client_character_set: %d", (uint) net->read_pos[8]));
785
 
    thd_init_client_charset(thd, (uint) net->read_pos[8]);
786
 
    thd->update_charset();
787
 
    end= (char*) net->read_pos+32;
788
 
  }
789
 
  else
790
 
  {
791
 
    thd->max_client_packet_length= uint3korr(net->read_pos+2);
792
 
    end= (char*) net->read_pos+5;
793
 
  }
 
943
  uint charset_code= 0;
 
944
  end= (char *)net->read_pos;
 
945
  /*
 
946
    In order to safely scan a head for '\0' string terminators
 
947
    we must keep track of how many bytes remain in the allocated
 
948
    buffer or we might read past the end of the buffer.
 
949
  */
 
950
  size_t bytes_remaining_in_packet= pkt_len;
 
951
  
 
952
  /*
 
953
    Peek ahead on the client capability packet and determine which version of
 
954
    the protocol should be used.
 
955
  */
 
956
  if (bytes_remaining_in_packet < 2)
 
957
    goto error;
 
958
  
 
959
  thd->client_capabilities= uint2korr(end);
 
960
 
 
961
  /*
 
962
    Connector/J only sends client capabilities (4 bytes) before starting SSL
 
963
    negotiation so we don't have char_set and other information for client in
 
964
    packet read. In that case, skip reading those information. The below code 
 
965
    is patch for this.
 
966
  */
 
967
  if(bytes_remaining_in_packet == AUTH_PACKET_HEADER_SIZE_CONNJ_SSL &&
 
968
     (thd->client_capabilities & CLIENT_SSL))
 
969
  {
 
970
    thd->client_capabilities= uint4korr(end);
 
971
    thd->max_client_packet_length= global_system_variables.max_allowed_packet;
 
972
    charset_code= default_charset_info->number;
 
973
    end+= AUTH_PACKET_HEADER_SIZE_CONNJ_SSL;
 
974
    bytes_remaining_in_packet-= AUTH_PACKET_HEADER_SIZE_CONNJ_SSL;
 
975
    goto skip_to_ssl;
 
976
  }
 
977
 
 
978
  if (thd->client_capabilities & CLIENT_PROTOCOL_41)
 
979
    packet_has_required_size= bytes_remaining_in_packet >= 
 
980
      AUTH_PACKET_HEADER_SIZE_PROTO_41;
 
981
  else
 
982
    packet_has_required_size= bytes_remaining_in_packet >=
 
983
      AUTH_PACKET_HEADER_SIZE_PROTO_40;
 
984
  
 
985
  if (!packet_has_required_size)
 
986
    goto error;
 
987
  
 
988
  if (thd->client_capabilities & CLIENT_PROTOCOL_41)
 
989
  {
 
990
    thd->client_capabilities= uint4korr(end);
 
991
    thd->max_client_packet_length= uint4korr(end + 4);
 
992
    charset_code= (uint)(uchar)*(end + 8);
 
993
    /*
 
994
      Skip 23 remaining filler bytes which have no particular meaning.
 
995
    */
 
996
    end+= AUTH_PACKET_HEADER_SIZE_PROTO_41;
 
997
    bytes_remaining_in_packet-= AUTH_PACKET_HEADER_SIZE_PROTO_41;
 
998
  }
 
999
  else
 
1000
  {
 
1001
    thd->client_capabilities= uint2korr(end);
 
1002
    thd->max_client_packet_length= uint3korr(end + 2);
 
1003
    end+= AUTH_PACKET_HEADER_SIZE_PROTO_40;
 
1004
    bytes_remaining_in_packet-= AUTH_PACKET_HEADER_SIZE_PROTO_40;
 
1005
    /**
 
1006
      Old clients didn't have their own charset. Instead the assumption
 
1007
      was that they used what ever the server used.
 
1008
    */
 
1009
    charset_code= default_charset_info->number;
 
1010
  }
 
1011
 
 
1012
skip_to_ssl:
 
1013
 
 
1014
  DBUG_PRINT("info", ("client_character_set: %u", charset_code));
 
1015
  if (thd_init_client_charset(thd, charset_code))
 
1016
    goto error;
 
1017
  thd->update_charset();
 
1018
 
794
1019
  /*
795
1020
    Disable those bits which are not supported by the server.
796
1021
    This is a precautionary measure, if the client lies. See Bug#27944.
801
1026
    thd->variables.sql_mode|= MODE_IGNORE_SPACE;
802
1027
#ifdef HAVE_OPENSSL
803
1028
  DBUG_PRINT("info", ("client capabilities: %lu", thd->client_capabilities));
 
1029
  
 
1030
  /*
 
1031
    If client requested SSL then we must stop parsing, try to switch to SSL,
 
1032
    and wait for the client to send a new handshake packet.
 
1033
    The client isn't expected to send any more bytes until SSL is initialized.
 
1034
  */
804
1035
  if (thd->client_capabilities & CLIENT_SSL)
805
1036
  {
806
1037
    /* Do the SSL layering. */
807
1038
    if (!ssl_acceptor_fd)
808
 
    {
809
 
      inc_host_errors(&thd->remote.sin_addr);
810
 
      my_error(ER_HANDSHAKE_ERROR, MYF(0), thd->main_security_ctx.host_or_ip);
811
 
      return 1;
812
 
    }
 
1039
      goto error;
 
1040
 
813
1041
    DBUG_PRINT("info", ("IO layer change in progress..."));
814
1042
    if (sslaccept(ssl_acceptor_fd, net->vio, net->read_timeout))
815
1043
    {
816
1044
      DBUG_PRINT("error", ("Failed to accept new SSL connection"));
817
 
      inc_host_errors(&thd->remote.sin_addr);
818
 
      my_error(ER_HANDSHAKE_ERROR, MYF(0), thd->main_security_ctx.host_or_ip);
819
 
      return 1;
 
1045
      goto error;
820
1046
    }
 
1047
    
821
1048
    DBUG_PRINT("info", ("Reading user information over SSL layer"));
822
 
    if ((pkt_len= my_net_read(net)) == packet_error ||
823
 
        pkt_len < NORMAL_HANDSHAKE_SIZE)
 
1049
    if ((pkt_len= my_net_read(net)) == packet_error)
824
1050
    {
825
1051
      DBUG_PRINT("error", ("Failed to read user information (pkt_len= %lu)",
826
1052
                           pkt_len));
827
 
      inc_host_errors(&thd->remote.sin_addr);
828
 
      my_error(ER_HANDSHAKE_ERROR, MYF(0), thd->main_security_ctx.host_or_ip);
829
 
      return 1;
830
 
    }
 
1053
      goto error;
 
1054
    }
 
1055
    /*
 
1056
      A new packet was read and the statistics reflecting the remaining bytes
 
1057
      in the packet must be updated.
 
1058
    */
 
1059
    bytes_remaining_in_packet= pkt_len;
 
1060
 
 
1061
    /*
 
1062
      After the SSL handshake is performed the client resends the handshake
 
1063
      packet but because of legacy reasons we chose not to parse the packet
 
1064
      fields a second time and instead only assert the length of the packet.
 
1065
    */
 
1066
    if (thd->client_capabilities & CLIENT_PROTOCOL_41)
 
1067
    {
 
1068
      
 
1069
      packet_has_required_size= bytes_remaining_in_packet >= 
 
1070
        AUTH_PACKET_HEADER_SIZE_PROTO_41;
 
1071
      end= (char *)net->read_pos + AUTH_PACKET_HEADER_SIZE_PROTO_41;
 
1072
      bytes_remaining_in_packet -= AUTH_PACKET_HEADER_SIZE_PROTO_41;
 
1073
    }
 
1074
    else
 
1075
    {
 
1076
      packet_has_required_size= bytes_remaining_in_packet >= 
 
1077
        AUTH_PACKET_HEADER_SIZE_PROTO_40;
 
1078
      end= (char *)net->read_pos + AUTH_PACKET_HEADER_SIZE_PROTO_40;
 
1079
      bytes_remaining_in_packet -= AUTH_PACKET_HEADER_SIZE_PROTO_40;
 
1080
    }
 
1081
  
 
1082
    if (!packet_has_required_size)
 
1083
      goto error;
831
1084
  }
832
1085
#endif /* HAVE_OPENSSL */
833
1086
 
834
 
  if (end >= (char*) net->read_pos+ pkt_len +2)
835
 
  {
836
 
    inc_host_errors(&thd->remote.sin_addr);
837
 
    my_error(ER_HANDSHAKE_ERROR, MYF(0), thd->main_security_ctx.host_or_ip);
838
 
    return 1;
839
 
  }
840
 
 
841
1087
  if (thd->client_capabilities & CLIENT_INTERACTIVE)
842
1088
    thd->variables.net_wait_timeout= thd->variables.net_interactive_timeout;
843
1089
  if ((thd->client_capabilities & CLIENT_TRANSACTIONS) &&
844
1090
      opt_using_transactions)
845
1091
    net->return_status= &thd->server_status;
846
1092
 
847
 
  char *user= end;
848
 
  char *passwd= strend(user)+1;
849
 
  uint user_len= passwd - user - 1;
850
 
  char *db= passwd;
 
1093
  /*
 
1094
    The 4.0 and 4.1 versions of the protocol differ on how strings
 
1095
    are terminated. In the 4.0 version, if a string is at the end
 
1096
    of the packet, the string is not null terminated. Do not assume
 
1097
    that the returned string is always null terminated.
 
1098
  */
 
1099
  get_proto_string_func_t get_string;
 
1100
 
 
1101
  if (thd->client_capabilities & CLIENT_PROTOCOL_41)
 
1102
    get_string= get_41_protocol_string;
 
1103
  else
 
1104
    get_string= get_40_protocol_string;
 
1105
 
 
1106
  user= get_string(&end, &bytes_remaining_in_packet, &user_len);
 
1107
  if (user == NULL)
 
1108
    goto error;
 
1109
 
 
1110
  /*
 
1111
    Old clients send a null-terminated string as password; new clients send
 
1112
    the size (1 byte) + string (not null-terminated). Hence in case of empty
 
1113
    password both send '\0'.
 
1114
  */
 
1115
  passwd_len= 0;
 
1116
  passwd= NULL;
 
1117
 
 
1118
  if (thd->client_capabilities & CLIENT_SECURE_CONNECTION)
 
1119
  {
 
1120
    /*
 
1121
      4.1+ password. First byte is password length.
 
1122
    */
 
1123
    passwd= get_length_encoded_string(&end, &bytes_remaining_in_packet,
 
1124
                                      &passwd_len);
 
1125
  }
 
1126
  else
 
1127
  {
 
1128
    /*
 
1129
      Old passwords are zero terminated strings.
 
1130
    */
 
1131
    passwd= get_string(&end, &bytes_remaining_in_packet, &passwd_len);
 
1132
  }
 
1133
 
 
1134
  if (passwd == NULL)
 
1135
    goto error;
 
1136
 
 
1137
  db_len= 0;
 
1138
  db= NULL;
 
1139
 
 
1140
  if (thd->client_capabilities & CLIENT_CONNECT_WITH_DB)
 
1141
  {
 
1142
    db= get_string(&end, &bytes_remaining_in_packet, &db_len);
 
1143
    if (db == NULL)
 
1144
      goto error;
 
1145
  }
 
1146
 
851
1147
  char db_buff[NAME_LEN + 1];           // buffer to store db in utf8
852
1148
  char user_buff[USERNAME_LENGTH + 1];  // buffer to store user in utf8
853
1149
  uint dummy_errors;
854
1150
 
855
1151
  /*
856
 
    Old clients send null-terminated string as password; new clients send
857
 
    the size (1 byte) + string (not null-terminated). Hence in case of empty
858
 
    password both send '\0'.
859
 
 
860
 
    This strlen() can't be easily deleted without changing protocol.
861
 
 
862
 
    Cast *passwd to an unsigned char, so that it doesn't extend the sign for
863
 
    *passwd > 127 and become 2**32-127+ after casting to uint.
 
1152
    Copy and convert the user and database names to the character set used
 
1153
    by the server. Since 4.1 all database names are stored in UTF-8. Also,
 
1154
    ensure that the names are properly null-terminated as this is relied
 
1155
    upon later.
864
1156
  */
865
 
  uint passwd_len= thd->client_capabilities & CLIENT_SECURE_CONNECTION ?
866
 
    (uchar)(*passwd++) : strlen(passwd);
867
 
  db= thd->client_capabilities & CLIENT_CONNECT_WITH_DB ?
868
 
    db + passwd_len + 1 : 0;
869
 
  /* strlen() can't be easily deleted without changing protocol */
870
 
  uint db_len= db ? strlen(db) : 0;
871
 
 
872
 
  if (passwd + passwd_len + db_len > (char *)net->read_pos + pkt_len)
873
 
  {
874
 
    inc_host_errors(&thd->remote.sin_addr);
875
 
    my_error(ER_HANDSHAKE_ERROR, MYF(0), thd->main_security_ctx.host_or_ip);
876
 
    return 1;
877
 
  }
878
 
 
879
 
  /* Since 4.1 all database names are stored in utf8 */
880
1157
  if (db)
881
1158
  {
882
 
    db_buff[copy_and_convert(db_buff, sizeof(db_buff)-1,
883
 
                             system_charset_info,
884
 
                             db, db_len,
885
 
                             thd->charset(), &dummy_errors)]= 0;
 
1159
    db_len= copy_and_convert(db_buff, sizeof(db_buff)-1, system_charset_info,
 
1160
                             db, db_len, thd->charset(), &dummy_errors);
 
1161
    db_buff[db_len]= '\0';
886
1162
    db= db_buff;
887
1163
  }
888
1164
 
889
 
  user_buff[user_len= copy_and_convert(user_buff, sizeof(user_buff)-1,
890
 
                                       system_charset_info, user, user_len,
891
 
                                       thd->charset(), &dummy_errors)]= '\0';
 
1165
  user_len= copy_and_convert(user_buff, sizeof(user_buff)-1,
 
1166
                             system_charset_info, user, user_len,
 
1167
                             thd->charset(), &dummy_errors);
 
1168
  user_buff[user_len]= '\0';
892
1169
  user= user_buff;
893
1170
 
894
1171
  /* If username starts and ends in "'", chop them off */
899
1176
    user_len-= 2;
900
1177
  }
901
1178
 
902
 
  if (thd->main_security_ctx.user)
903
 
    x_free(thd->main_security_ctx.user);
 
1179
  /*
 
1180
    Clip username to allowed length in characters (not bytes).  This is
 
1181
    mostly for backward compatibility.
 
1182
  */
 
1183
  {
 
1184
    CHARSET_INFO *cs= system_charset_info;
 
1185
    int           err;
 
1186
 
 
1187
    user_len= (uint) cs->cset->well_formed_len(cs, user, user + user_len,
 
1188
                                               USERNAME_CHAR_LENGTH, &err);
 
1189
    user[user_len]= '\0';
 
1190
  }
 
1191
 
904
1192
  if (!(thd->main_security_ctx.user= my_strdup(user, MYF(MY_WME))))
905
1193
    return 1; /* The error is set by my_strdup(). */
906
1194
  return check_user(thd, COM_CONNECT, passwd, passwd_len, db, TRUE);
 
1195
  
 
1196
error:
 
1197
  inc_host_errors(&thd->remote.sin_addr);
 
1198
  my_error(ER_HANDSHAKE_ERROR, MYF(0));
 
1199
  return 1;
907
1200
}
908
1201
 
909
1202
 
1153
1446
  }
1154
1447
}
1155
1448
#endif /* EMBEDDED_LIBRARY */
 
1449