-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuser.cpp
More file actions
399 lines (327 loc) · 13.2 KB
/
user.cpp
File metadata and controls
399 lines (327 loc) · 13.2 KB
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
#include "stratum.h"
#include "md5.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// sql injection security, unwanted chars
void db_check_user_input(char* input)
{
char *p = NULL;
if (input && input[0]) {
p = strpbrk(input, " \"'\\");
if(p) *p = '\0';
}
}
int db_get_coinid(YAAMP_DB *db, char* symbol)
{
size_t len = strlen(symbol);
if (len >= 2 && len <= 12) {
#ifdef NO_EXCHANGE
db_query(db, "SELECT id FROM coins WHERE installed AND symbol='%s'", symbol);
#else
db_query(db, "SELECT id FROM coins WHERE installed AND (symbol='%s' OR symbol2='%s')", symbol, symbol);
#endif
MYSQL_RES *result = mysql_store_result(&db->mysql);
*symbol = '\0';
if (!result) return 0;
MYSQL_ROW row = mysql_fetch_row(result);
if (row) {
//printf("fuckx 0= %s 1= %s 2= %s 3= %s \n",row[0],row[1],row[2],row[3]);
return atoi(row[0]);
}
mysql_free_result(result);
} else {
return 0;
}
}
void db_check_symbol(YAAMP_DB *db, char* symbol)
{
if (!symbol) return;
size_t len = strlen(symbol);
if (len >= 2 && len <= 12) {
#ifdef NO_EXCHANGE
db_query(db, "SELECT symbol FROM coins WHERE installed AND algo='%s' AND symbol='%s'", g_stratum_algo, symbol);
#else
db_query(db, "SELECT symbol FROM coins WHERE installed AND (symbol='%s' OR symbol2='%s')", symbol, symbol);
#endif
MYSQL_RES *result = mysql_store_result(&db->mysql);
*symbol = '\0';
if (!result) return;
MYSQL_ROW row = mysql_fetch_row(result);
if (row) {
strcpy(symbol, row[0]);
}
mysql_free_result(result);
} else {
*symbol = '\0';
}
}
void db_add_user(YAAMP_DB *db, YAAMP_CLIENT *client)
{
db_clean_string(db, client->username);
db_clean_string(db, client->password);
db_clean_string(db, client->version);
db_clean_string(db, client->notify_id);
db_clean_string(db, client->worker);
if (strlen(client->username)==0) return;
char symbol[16] = { 0 };
char pass[126] = { 0 };
char *p = strstr(client->password, "c=");
if(!p) p = strstr(client->password, "s=");
if(p) {
strncpy(symbol, p + 2, 15);
strncpy(client->symbol, p + 2, 15);
db_clean_string(db, client->symbol);
}
p = strchr(symbol, ',');
if(p) *p = '\0';
char *p1= strstr(client->password, "p=");
if (p1) {
strncpy(pass, p1 + 2, 125);
p1 = strchr(pass, ',');
if (p1) *p1 = '\0';
}
if (strlen(symbol)<2) {
strcpy(symbol, (const char *)"KYF\0");
strcpy(client->symbol, (const char *)"KYF\0" );
}
if (strlen(pass)<5) strncpy(pass, (char *) client->password, 125);
//printf("Symbol = %s - ",client->symbol);
//printf("password = %s - ",pass);
client->coinid = db_get_coinid(db, client->symbol);
//printf("coinid = %d \n",client->coinid);
char buffer3[1024];
snprintf(buffer3, 1000,"Currently mining %s with id %d and address %s ", symbol, client->coinid, client->username);
client_send_message(client, buffer3);
strncpy(client->password,pass,64);
bool guest = false;
int gift = -1;
#ifdef ALLOW_CUSTOM_DONATIONS
// donation percent
p = strstr(client->password, "g=");
if(p) gift = atoi(p+2);
if(gift > 100) gift = 100;
#endif
if (strlen(client->username)<=5 ) {
debuglog("Username not good= %i \n", client->username);
client_send_message(client, "username is wrong");
client->userid=-1;
return;
}
if (strlen(pass)<=5 ) {
debuglog("password not good= %i \n", client->password);
client_send_message(client, "password is wrong");
client->userid=-1;
return;
}
if (client->coinid==0 ) {
client->userid=-1;
client_send_message(client, "coin selection is wrong");
debuglog("Coinid not good= %i \n", client->coinid);
return;
}
if (strstr(client->version, "ncpool")==0 ) {
// add pass
debuglog("Wrong mining software, this mining pool is using a specific software '%s'\n", client->version);
client_send_message(client, "mining software is wrong");
client->userid=-3;
return;
}
db_check_user_input(client->username);
db_check_symbol(db, symbol);
if(strlen(client->username) < MIN_ADDRESS_LEN) {
if (!strlen(client->worker)) strncpy(client->worker, client->username,64);
// allow benchmark / test / donate usernames
/*if (!strcmp(client->username, "benchmark") || !strcmp(client->username, "donate") || !strcmp(client->username, "test")) {
guest = true;
if (g_list_coind.first) {
CLI li = g_list_coind.first;
YAAMP_COIND *coind = (YAAMP_COIND *)li->data;
if (!strlen(client->worker)) strcpy(client->worker, client->username);
strcpy(client->username, coind->wallet);
if (!strcmp(client->username, "benchmark")) strcat(client->password, ",stats");
if (!strcmp(client->username, "donate")) gift = 100;
}
}
if (!guest) {*/
//client_send_message(client, "username is wrong 2");
debuglog("Invalid user address '%s'\n", client->username);
client->userid=-4;
return;
//}
}
//debuglog("User %s symbol %s \n", client->username, client->symbol);
db_query(db, "SELECT id, is_locked, logtraffic, coinid, donation,pass FROM accounts WHERE username='%s' and coinid=%i", client->username,client->coinid);
//db_query(db, "SELECT id, is_locked, logtraffic, coinid, donation,pass FROM accounts WHERE username='%s'", client->username);
MYSQL_RES *result = mysql_store_result(&db->mysql);
if(result) {
MYSQL_ROW row = mysql_fetch_row(result);
if (row) {
// locked
if (row[1] && atoi(row[1])) {
client->userid = -1;
clientlog(client, "username is wrong 3");
client_send_message(client, "username is wrong 3");
mysql_free_result(result);
return;
}
else client->userid = atoi(row[0]);
client->logtraffic = row[2] && atoi(row[2]);
client->coinid = row[3] ? atoi(row[3]) : 0;
if (gift == -1) gift = row[4] ? atoi(row[4]) : 0; // keep current
// wrong pass
if (strcmp(pass ,row[5])!=0) {
clientlog(client, "password is wrong 3");
//db_check_user_input(row[5]);
//strncpy(client->password, row[5], 64);
// add pass
client->userid = -3;
mysql_free_result(result);
return;
}
}
}
mysql_free_result(result);
// check double worker
//debuglog("user %s %s \n", client->username, symbol, gift);
if (gift < 0) gift = 0;
client->donation = gift;
if (strlen(pass) <= MIN_PASSWORD) {
client_send_message(client, "password is wrong 2");
debuglog("Password too short: %s\n",pass);
client->userid = -3;
return;
}
if(client->userid == -1) {
client_send_message(client, "userid is wrong");
return;
}
else if(client->userid == 0 && strlen(client->username) >= MIN_ADDRESS_LEN )
{
// create account
db_query(db, "INSERT INTO accounts ("
"username, coinsymbol, is_locked, coinid, balance, donation, hostaddr,pass) "
"values ('%s','%s', 1, ,'%d', 0, %d, '%s', '%s')",
client->username, client->symbol, client->coinid, gift, client->sock->ip,pass);
client->userid = (int)mysql_insert_id(&db->mysql);
}
else {
db_query(db, "UPDATE accounts SET coinsymbol='%s', swap_time=%u, donation=%d, hostaddr='%s' WHERE id=%d AND balance = 0"
" AND (SELECT COUNT(id) FROM payouts WHERE account_id=%d AND tx IS NULL) = 0" // failed balance
" AND (SELECT pending FROM balanceuser WHERE userid=%d ORDER by time DESC LIMIT 1) = 0" // pending balance
, client->symbol, (uint) time(NULL), gift, client->sock->ip, client->userid, client->userid, client->userid);
if (mysql_affected_rows(&db->mysql) > 0 && strlen(client->symbol)) {
//debuglog("%s: %s coinsymbol set to %s %s ip %s uid (%d)\n",g_current_algo->name, client->username, symbol, client->coinid, client->sock->ip, client->userid);
}
}
}
//////////////////////////////////////////////////////////////////////////////////////
void db_clear_worker(YAAMP_DB *db, YAAMP_CLIENT *client)
{
if(!client->workerid)
return;
db_query(db, "DELETE FROM workers WHERE id=%d", client->workerid);
client->workerid = 0;
}
void db_add_worker(YAAMP_DB *db, YAAMP_CLIENT *client)
{
//if (db->mysql) return;
char password[128] = { 0 };
char version[128] = { 0 };
char worker[128] = { 0 };
int now = time(NULL);
db_clear_worker(db, client);
db_check_user_input(client->username);
db_check_user_input(client->version);
db_check_user_input(client->password);
db_check_user_input(client->worker);
if (strlen(client->username)==0) {
clientlog(client, "username empty");
return;
}
// strip for recent mysql defaults (error if fields are too long)
if (strlen(client->password) > 64 || strlen(client->password)<2)
clientlog(client, "password too long truncated: %s", client->password);
if (strlen(client->version) > 64)
clientlog(client, "version too long truncated: %s", client->version);
if (strlen(client->worker) > 64)
clientlog(client, "worker too long truncated: %s", client->worker);
if (strlen(client->worker) <2 )
strncpy(client->worker,client->username,64);
// check if worker is a user
db_query(db, "SELECT id, is_locked, logtraffic, coinid, donation,pass FROM accounts WHERE username='%s' and pass='%s'",
client->username,client->password);
MYSQL_RES *result = mysql_store_result(&db->mysql);
if(!result) {
client_send_message(client, "worker deos not match mendatory credentials");
clientlog(client, "worker does not match mendatory credentials %s\n", client->username);
mysql_free_result(result);
client->userid=-1;
return;
}
MYSQL_ROW row = mysql_fetch_row(result);
if(row)
{
if (strcmp(client->password, row[5])!=0 ) {
client_send_message(client, "worker password is wrong");
clientlog(client, "worker password is wrong: %s - %s\n", client->password,row[5]);
mysql_free_result(result);
client->userid=-2;
return;
}
}
if(row[1] && atoi(row[1])) {
client_send_message(client, "worker is locked");
clientlog(client, "worker is locked %s\n", client->username);
client->userid = -1;return;
}
else client->userid = atoi(row[0]);
mysql_free_result(result);
//strncpy(password, client->password, 64);
//strncpy(version, client->version, 64);
//strncpy(worker, client->worker, 64);
db_query(db, "SELECT id FROM workers WHERE worker='%s' and coinid='%d' and name='%s'",client->worker,client->coinid,client->username);
result = mysql_store_result(&db->mysql);
if (result) {
if ( mysql_num_rows( result )==0) {
db_query(db,"INSERT INTO workers (userid, coinid, ip, name, difficulty, version, password, worker, algo, time, pid) "\
"VALUES (%d, %d, '%s', '%s', %f, '%s', '%s', '%s', '%s', %d, %d)",
client->userid, client->coinid, client->sock->ip, client->username, client->difficulty_actual,
client->version, client->password, client->worker, g_stratum_algo, now, getpid());
client->workerid = (int) mysql_insert_id(&db->mysql);
//client_send_message(client, "worker added");
clientlog(client,"ADDWORKER: userid=%i - pass=%s - version= %s - workerid=%i - worker=%s\n",client->userid,client->password,client->version,client->workerid,client->worker);
}
}
}
void db_update_workers(YAAMP_DB *db)
{
g_list_client.Enter();
for(CLI li = g_list_client.first; li; li = li->next)
{
YAAMP_CLIENT *client = (YAAMP_CLIENT *)li->data;
if(client->deleted) continue;
if(!client->workerid) continue;
if(client->speed < 0.00001)
{
clientlog(client, "speed %f", client->speed);
shutdown(client->sock->sock, SHUT_RDWR);
db_clear_worker(db, client);
object_delete(client);
continue;
}
client->speed *= 0.8;
if(client->difficulty_written == client->difficulty_actual) continue;
db_query(db, "UPDATE workers SET difficulty=%f,coinid=%d, subscribe=%d WHERE id=%d",
client->difficulty_actual,client->coinid, client->extranonce_subscribe, client->workerid);
client->difficulty_written = client->difficulty_actual;
}
client_sort();
g_list_client.Leave();
}
void db_init_user_coinid(YAAMP_DB *db, YAAMP_CLIENT *client)
{
if (!client->userid)
return;
db_query(db, "UPDATE accounts SET coinid=%d WHERE id=%d AND IFNULL(coinid,0) = 0",client->coinid, client->userid);
}