~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to plugin/mysql_protocol/mysql_protocol.cc

  • Committer: Eric Day
  • Date: 2010-03-18 18:08:25 UTC
  • mto: This revision was merged to the branch mainline in revision 1409.
  • Revision ID: eday@oddments.org-20100318180825-fxhhirp8vky2sqh9
Added MySQL password hash support.

Show diffs side-by-side

added added

removed removed

Lines of Context:
49
49
static uint32_t retry_count;
50
50
static uint32_t buffer_length;
51
51
static char* bind_address;
 
52
static uint32_t random_seed1;
 
53
static uint32_t random_seed2;
 
54
static const uint32_t random_max= 0x3FFFFFFF;
 
55
static const double random_max_double= (double)0x3FFFFFFF;
52
56
 
53
57
const char* ListenMySQLProtocol::getHost(void) const
54
58
{
615
619
{
616
620
  uint32_t pkt_len= 0;
617
621
  char *end;
 
622
  char scramble[SCRAMBLE_LENGTH];
 
623
 
 
624
  makeScramble(scramble);
618
625
 
619
626
  // TCP/IP connection
620
627
  {
656
663
    end+= 4;
657
664
 
658
665
    /* We don't use scramble anymore. */
659
 
    memset(end, 'X', SCRAMBLE_LENGTH_323);
 
666
    memcpy(end, scramble, SCRAMBLE_LENGTH_323);
660
667
    end+= SCRAMBLE_LENGTH_323;
661
668
    *end++= 0; /* an empty byte for some reason */
662
669
 
668
675
    end+= 18;
669
676
 
670
677
    /* Write scramble tail. */
671
 
    memset(end, 'X', SCRAMBLE_LENGTH - SCRAMBLE_LENGTH_323);
 
678
    memcpy(end, scramble + SCRAMBLE_LENGTH_323, SCRAMBLE_LENGTH - SCRAMBLE_LENGTH_323);
672
679
    end+= (SCRAMBLE_LENGTH - SCRAMBLE_LENGTH_323);
673
680
    *end++= 0; /* an empty byte for some reason */
674
681
 
727
734
  */
728
735
  uint32_t passwd_len= client_capabilities & CLIENT_SECURE_CONNECTION ?
729
736
    (unsigned char)(*passwd++) : strlen(passwd);
 
737
  if (passwd_len > 0)
 
738
  {
 
739
    session->getSecurityContext().setPasswordType(SecurityContext::MYSQL_HASH);
 
740
    session->getSecurityContext().setPasswordContext(scramble, SCRAMBLE_LENGTH);
 
741
  }
 
742
 
730
743
  l_db= client_capabilities & CLIENT_CONNECT_WITH_DB ? l_db + passwd_len + 1 : 0;
731
744
 
732
745
  /* strlen() can't be easily deleted without changing client */
832
845
  return buffer+8;
833
846
}
834
847
 
 
848
void ClientMySQLProtocol::makeScramble(char *scramble)
 
849
{
 
850
  /* This is the MySQL algorithm with minimal changes. */
 
851
  random_seed1= (random_seed1 * 3 + random_seed2) % random_max;
 
852
  random_seed2= (random_seed1 + random_seed2 + 33) % random_max;
 
853
  uint32_t seed= static_cast<uint32_t>((static_cast<double>(random_seed1) / random_max_double) * 0xffffffff);
 
854
 
 
855
  void *pointer= this;
 
856
  uint32_t pointer_seed;
 
857
  memcpy(&pointer_seed, &pointer, 4);
 
858
  uint32_t random1= (seed + pointer_seed) % random_max;
 
859
  uint32_t random2= (seed + global_thread_id + net.vio->sd) % random_max;
 
860
 
 
861
  for (char *end= scramble + SCRAMBLE_LENGTH; scramble != end; scramble++)
 
862
  {
 
863
    random1= (random1 * 3 + random2) % random_max;
 
864
    random2= (random1 + random2 + 33) % random_max;
 
865
    *scramble= static_cast<char>((static_cast<double>(random1) / random_max_double) * 94 + 33);
 
866
  }
 
867
}
 
868
 
835
869
static ListenMySQLProtocol *listen_obj= NULL;
836
870
plugin::Create_function<MySQLPassword> *mysql_password= NULL;
837
871
 
838
872
static int init(drizzled::plugin::Registry &registry)
839
873
{
 
874
  /* Initialize random seeds for the MySQL algorithm with minimal changes. */
 
875
  time_t seed_time= time(NULL);
 
876
  random_seed1= seed_time % random_max;
 
877
  random_seed2= (seed_time / 2) % random_max;
 
878
 
840
879
  mysql_password= new plugin::Create_function<MySQLPassword>(MySQLPasswordName);
841
880
  registry.add(mysql_password);
842
881