Skip to content

Commit

Permalink
Add files for MySQL replay module
Browse files Browse the repository at this point in the history
  • Loading branch information
wangbin579 committed Jun 4, 2014
0 parents commit 77ae20b
Show file tree
Hide file tree
Showing 8 changed files with 947 additions and 0 deletions.
8 changes: 8 additions & 0 deletions config
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
tc_addon_name=tc_mysql_module
PROTOCOL_MODULES="tc_mysql_module"
TC_PAYLOAD=YES
TC_DIGEST=YES
mysql_header="$tc_addon_dir/password.h $tc_addon_dir/pairs.h $tc_addon_dir/protocol.h"
mysql_src="$tc_addon_dir/password.c $tc_addon_dir/pairs.c $tc_addon_dir/protocol.c"
TC_ADDON_DEPS="$TC_ADDON_DEPS $mysql_header"
TC_ADDON_SRCS="$mysql_src $tc_addon_dir/tc_mysql_module.c"
114 changes: 114 additions & 0 deletions pairs.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@

#include <xcopy.h>
#include "pairs.h"

static hash_table *user_pwd_table;

static uint64_t
get_key_from_user(char *user)
{
size_t len, i;
uint64_t key = 0;

if (user == NULL) {
return key;
}

len = strlen(user);
for (i = 0; i < len; i++ ) {
key = 31*key + user[i];
}

return key;
}

char *
retrieve_user_pwd(char *user)
{
uint64_t key;
mysql_user *p_user_info;

key = get_key_from_user(user);
p_user_info = hash_find(user_pwd_table, key);

while (p_user_info) {
if (strcmp(p_user_info->user, user) == 0) {
return p_user_info->password;
}
p_user_info = p_user_info->next;
}

return NULL;
}

int
retrieve_mysql_user_pwd_info(tc_pool_t *pool, char *pairs)
{
char *p, *end, *q, *next, *pair_end;
size_t len;
uint64_t key;
mysql_user *p_user_info, *p_tmp_user_info;

user_pwd_table = hash_create(pool, 256);
user_pwd_table->pool = pool;

p = pairs;
len = strlen(p);
end = p + len;

if (len <= 1) {
tc_log_info(LOG_WARN, 0, "use password error:%s:", pairs);
return -1;
}

do {
next = strchr(p, ',');
q = strchr(p, '@');

if ( next != NULL) {
if (next != p) {
pair_end = next - 1;
} else {
tc_log_info(LOG_WARN, 0, "use password error:%s:", pairs);
return -1;
}
} else {
pair_end = p + strlen(p) - 1;
}

if ((q-p) >= 256 || (pair_end - q) >= 256) {
tc_log_info(LOG_WARN, 0, "too long for user or password");
return -1;
}

p_user_info = (mysql_user *) tc_pcalloc(pool, sizeof(mysql_user));
strncpy(p_user_info->user, p, q - p);
strncpy(p_user_info->password, q + 1, pair_end - q);
key = get_key_from_user(p_user_info->user);
p_tmp_user_info = hash_find(user_pwd_table, key);

if (p_tmp_user_info == NULL) {
hash_add(user_pwd_table, pool, key, (void *) p_user_info);
} else {
p_tmp_user_info->next = p_user_info;
}

if (next != NULL) {
p = next + 1;
} else {
break;
}
} while (p < end) ;

return 0;
}


void
release_mysql_user_pwd_info()
{
if (user_pwd_table != NULL) {
user_pwd_table = NULL;
}
}

18 changes: 18 additions & 0 deletions pairs.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#ifndef PAIRS_INCLUDED
#define PAIRS_INCLUDED
#include <xcopy.h>
#define MAX_PASSWORD_LEN 256
#define MAX_USER_LEN 256

typedef struct mysql_user{
char user[MAX_USER_LEN];
char password[MAX_PASSWORD_LEN];
struct mysql_user* next;
}mysql_user;

char *retrieve_user_pwd(char *user);
int retrieve_mysql_user_pwd_info(tc_pool_t *, char *);
void release_mysql_user_pwd_info();

#endif

35 changes: 35 additions & 0 deletions password.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#include <xcopy.h>

#include "password.h"

static void
my_crypt(char *to, const unsigned char *s1, const unsigned char *s2, uint len)
{
const unsigned char *s1_end = s1 + len;

while (s1 < s1_end) {
*to++= *s1++ ^ *s2++;
}
}

/**
* SHA1(password) XOR
* SHA1("20-bytes random data from server" <concat> SHA1(SHA1(password)))
*/
void
scramble(char *to, const char *message, const char *password)
{
unsigned char hash_stage1[SHA1_HASH_SIZE];
unsigned char hash_stage2[SHA1_HASH_SIZE];

tc_sha1_digest_one(hash_stage1, SHA1_HASH_SIZE,
(const unsigned char *) password, strlen(password));
tc_sha1_digest_one(hash_stage2, SHA1_HASH_SIZE,
hash_stage1, SHA1_HASH_SIZE);
tc_sha1_digest_two((unsigned char *) to, SCRAMBLE_LENGTH,
(const unsigned char *) message, SCRAMBLE_LENGTH,
(const unsigned char *) hash_stage2, SHA1_HASH_SIZE);

my_crypt(to, (const unsigned char *) to, hash_stage1, SCRAMBLE_LENGTH);
}

10 changes: 10 additions & 0 deletions password.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#ifndef PASSWORD_INCLUDED
#define PASSWORD_INCLUDED

#define SCRAMBLE_LENGTH 20
#define SHA1_HASH_SIZE 20

void scramble(char *to, const char *message, const char *password);

#endif /* ----- #ifndef PASSWORD_INCLUDED ----- */

Loading

0 comments on commit 77ae20b

Please sign in to comment.