forked from session-replay-tools/mysql-replay-module
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pairs.c
120 lines (95 loc) · 2.69 KB
/
pairs.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#include <xcopy.h>
#include "pairs.h"
static hash_table *user_pwd_table = NULL;
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;
if (user_pwd_table == NULL) {
tc_log_info(LOG_ERR, 0, "empty user info in conf/plugin.conf");
fprintf(stderr, "empty user info in conf/plugin.conf\n");
return NULL;
}
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 == NULL || pair_end <= q) {
tc_log_info(LOG_WARN, 0, "user without password is found");
return -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));
if (p_user_info == NULL) {
return -1;
}
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;
}