~ubuntu-branches/ubuntu/lucid/mysql-dfsg-5.1/lucid-security

« 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 22:33:55 UTC
  • mto: (1.2.1) (37.1.1 lucid-security)
  • mto: This revision was merged to the branch mainline in revision 36.
  • Revision ID: package-import@ubuntu.com-20120222223355-ku1tb4r70osci6v2
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 */
471
459
      }
472
460
      my_ok(thd);
473
461
      thd->password= test(passwd_len);          // remember for error messages 
 
462
#ifndef EMBEDDED_LIBRARY
 
463
      /*
 
464
        Allow the network layer to skip big packets. Although a malicious
 
465
        authenticated session might use this to trick the server to read
 
466
        big packets indefinitely, this is a previously established behavior
 
467
        that needs to be preserved as to not break backwards compatibility.
 
468
      */
 
469
      thd->net.skip_big_packet= TRUE;
 
470
#endif
474
471
      /* Ready to handle queries */
475
472
      DBUG_RETURN(0);
476
473
    }
573
570
}
574
571
 
575
572
 
576
 
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)
577
588
{
 
589
  CHARSET_INFO *cs;
578
590
  /*
579
591
   Use server character set and collation if
580
592
   - opt_character_set_client_handshake is not set
583
595
   - client character set doesn't exists in server
584
596
  */
585
597
  if (!opt_character_set_client_handshake ||
586
 
      !(thd->variables.character_set_client= get_charset(cs_number, MYF(0))) ||
 
598
      !(cs= get_charset(cs_number, MYF(0))) ||
587
599
      !my_strcasecmp(&my_charset_latin1,
588
600
                     global_system_variables.character_set_client->name,
589
 
                     thd->variables.character_set_client->name))
 
601
                     cs->name))
590
602
  {
591
603
    thd->variables.character_set_client=
592
604
      global_system_variables.character_set_client;
597
609
  }
598
610
  else
599
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
    }
600
619
    thd->variables.character_set_results=
601
620
      thd->variables.collation_connection= 
602
 
      thd->variables.character_set_client;
 
621
      thd->variables.character_set_client= cs;
603
622
  }
 
623
  return false;
604
624
}
605
625
 
606
626
 
621
641
  return 0;
622
642
}
623
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
 
624
792
/*
625
793
  Perform handshake, authorize client and update thd ACL variables.
626
794
 
634
802
   > 0  error code (not sent to user)
635
803
*/
636
804
 
637
 
#ifndef EMBEDDED_LIBRARY
638
805
static int check_connection(THD *thd)
639
806
{
640
807
  uint connect_errors= 0;
642
809
  ulong pkt_len= 0;
643
810
  char *end;
644
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
 
645
820
  DBUG_PRINT("info",
646
821
             ("New connection received on %s", vio_description(net->vio)));
647
822
#ifdef SIGNAL_WITH_VIO_CLOSE
654
829
 
655
830
    if (vio_peer_addr(net->vio, ip, &thd->peer_port))
656
831
    {
657
 
      my_error(ER_BAD_HOST_ERROR, MYF(0), thd->main_security_ctx.host_or_ip);
 
832
      my_error(ER_BAD_HOST_ERROR, MYF(0));
658
833
      return 1;
659
834
    }
660
835
    if (!(thd->main_security_ctx.ip= my_strdup(ip,MYF(MY_WME))))
705
880
  ulong server_capabilites;
706
881
  {
707
882
    /* buff[] needs to big enough to hold the server_version variable */
708
 
    char buff[SERVER_VERSION_LENGTH + SCRAMBLE_LENGTH + 64];
 
883
    char buff[SERVER_VERSION_LENGTH + 1 + SCRAMBLE_LENGTH + 1 + 64];
709
884
    server_capabilites= CLIENT_BASIC_FLAGS;
710
885
 
711
886
    if (opt_using_transactions)
736
911
      part at the end of packet.
737
912
    */
738
913
    end= strmake(end, thd->scramble, SCRAMBLE_LENGTH_323) + 1;
739
 
   
 
914
 
740
915
    int2store(end, server_capabilites);
741
916
    /* write server characteristics: up to 16 bytes allowed */
742
917
    end[2]=(char) default_charset_info->number;
750
925
    /* At this point we write connection message and read reply */
751
926
    if (net_write_command(net, (uchar) protocol_version, (uchar*) "", 0,
752
927
                          (uchar*) buff, (size_t) (end-buff)) ||
753
 
        (pkt_len= my_net_read(net)) == packet_error ||
754
 
        pkt_len < MIN_HANDSHAKE_SIZE)
 
928
        (pkt_len= my_net_read(net)) == packet_error)
755
929
    {
756
930
      inc_host_errors(&thd->remote.sin_addr);
757
 
      my_error(ER_HANDSHAKE_ERROR, MYF(0),
758
 
               thd->main_security_ctx.host_or_ip);
 
931
      my_error(ER_HANDSHAKE_ERROR, MYF(0));
759
932
      return 1;
760
933
    }
761
934
  }
767
940
  if (thd->packet.alloc(thd->variables.net_buffer_length))
768
941
    return 1; /* The error is set by alloc(). */
769
942
 
770
 
  thd->client_capabilities= uint2korr(net->read_pos);
771
 
  if (thd->client_capabilities & CLIENT_PROTOCOL_41)
772
 
  {
773
 
    thd->client_capabilities|= ((ulong) uint2korr(net->read_pos+2)) << 16;
774
 
    thd->max_client_packet_length= uint4korr(net->read_pos+4);
775
 
    DBUG_PRINT("info", ("client_character_set: %d", (uint) net->read_pos[8]));
776
 
    thd_init_client_charset(thd, (uint) net->read_pos[8]);
777
 
    thd->update_charset();
778
 
    end= (char*) net->read_pos+32;
779
 
  }
780
 
  else
781
 
  {
782
 
    thd->max_client_packet_length= uint3korr(net->read_pos+2);
783
 
    end= (char*) net->read_pos+5;
784
 
  }
 
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
 
785
1019
  /*
786
1020
    Disable those bits which are not supported by the server.
787
1021
    This is a precautionary measure, if the client lies. See Bug#27944.
792
1026
    thd->variables.sql_mode|= MODE_IGNORE_SPACE;
793
1027
#ifdef HAVE_OPENSSL
794
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
  */
795
1035
  if (thd->client_capabilities & CLIENT_SSL)
796
1036
  {
797
1037
    /* Do the SSL layering. */
798
1038
    if (!ssl_acceptor_fd)
799
 
    {
800
 
      inc_host_errors(&thd->remote.sin_addr);
801
 
      my_error(ER_HANDSHAKE_ERROR, MYF(0), thd->main_security_ctx.host_or_ip);
802
 
      return 1;
803
 
    }
 
1039
      goto error;
 
1040
 
804
1041
    DBUG_PRINT("info", ("IO layer change in progress..."));
805
1042
    if (sslaccept(ssl_acceptor_fd, net->vio, net->read_timeout))
806
1043
    {
807
1044
      DBUG_PRINT("error", ("Failed to accept new SSL connection"));
808
 
      inc_host_errors(&thd->remote.sin_addr);
809
 
      my_error(ER_HANDSHAKE_ERROR, MYF(0), thd->main_security_ctx.host_or_ip);
810
 
      return 1;
 
1045
      goto error;
811
1046
    }
 
1047
    
812
1048
    DBUG_PRINT("info", ("Reading user information over SSL layer"));
813
 
    if ((pkt_len= my_net_read(net)) == packet_error ||
814
 
        pkt_len < NORMAL_HANDSHAKE_SIZE)
 
1049
    if ((pkt_len= my_net_read(net)) == packet_error)
815
1050
    {
816
1051
      DBUG_PRINT("error", ("Failed to read user information (pkt_len= %lu)",
817
1052
                           pkt_len));
818
 
      inc_host_errors(&thd->remote.sin_addr);
819
 
      my_error(ER_HANDSHAKE_ERROR, MYF(0), thd->main_security_ctx.host_or_ip);
820
 
      return 1;
821
 
    }
 
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;
822
1084
  }
823
1085
#endif /* HAVE_OPENSSL */
824
1086
 
825
 
  if (end >= (char*) net->read_pos+ pkt_len +2)
826
 
  {
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
 
  }
831
 
 
832
1087
  if (thd->client_capabilities & CLIENT_INTERACTIVE)
833
1088
    thd->variables.net_wait_timeout= thd->variables.net_interactive_timeout;
834
1089
  if ((thd->client_capabilities & CLIENT_TRANSACTIONS) &&
835
1090
      opt_using_transactions)
836
1091
    net->return_status= &thd->server_status;
837
1092
 
838
 
  char *user= end;
839
 
  char *passwd= strend(user)+1;
840
 
  uint user_len= passwd - user - 1;
841
 
  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
 
842
1147
  char db_buff[NAME_LEN + 1];           // buffer to store db in utf8
843
1148
  char user_buff[USERNAME_LENGTH + 1];  // buffer to store user in utf8
844
1149
  uint dummy_errors;
845
1150
 
846
1151
  /*
847
 
    Old clients send null-terminated string as password; new clients send
848
 
    the size (1 byte) + string (not null-terminated). Hence in case of empty
849
 
    password both send '\0'.
850
 
 
851
 
    This strlen() can't be easily deleted without changing protocol.
852
 
 
853
 
    Cast *passwd to an unsigned char, so that it doesn't extend the sign for
854
 
    *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.
855
1156
  */
856
 
  uint passwd_len= thd->client_capabilities & CLIENT_SECURE_CONNECTION ?
857
 
    (uchar)(*passwd++) : strlen(passwd);
858
 
  db= thd->client_capabilities & CLIENT_CONNECT_WITH_DB ?
859
 
    db + passwd_len + 1 : 0;
860
 
  /* strlen() can't be easily deleted without changing protocol */
861
 
  uint db_len= db ? strlen(db) : 0;
862
 
 
863
 
  if (passwd + passwd_len + db_len > (char *)net->read_pos + pkt_len)
864
 
  {
865
 
    inc_host_errors(&thd->remote.sin_addr);
866
 
    my_error(ER_HANDSHAKE_ERROR, MYF(0), thd->main_security_ctx.host_or_ip);
867
 
    return 1;
868
 
  }
869
 
 
870
 
  /* Since 4.1 all database names are stored in utf8 */
871
1157
  if (db)
872
1158
  {
873
 
    db_buff[copy_and_convert(db_buff, sizeof(db_buff)-1,
874
 
                             system_charset_info,
875
 
                             db, db_len,
876
 
                             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';
877
1162
    db= db_buff;
878
1163
  }
879
1164
 
880
 
  user_buff[user_len= copy_and_convert(user_buff, sizeof(user_buff)-1,
881
 
                                       system_charset_info, user, user_len,
882
 
                                       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';
883
1169
  user= user_buff;
884
1170
 
885
1171
  /* If username starts and ends in "'", chop them off */
890
1176
    user_len-= 2;
891
1177
  }
892
1178
 
893
 
  if (thd->main_security_ctx.user)
894
 
    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
 
895
1192
  if (!(thd->main_security_ctx.user= my_strdup(user, MYF(MY_WME))))
896
1193
    return 1; /* The error is set by my_strdup(). */
897
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;
898
1200
}
899
1201
 
900
1202
 
1144
1446
  }
1145
1447
}
1146
1448
#endif /* EMBEDDED_LIBRARY */
 
1449