This repository has been archived by the owner on Mar 14, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
/
commands.cpp
729 lines (615 loc) · 16.2 KB
/
commands.cpp
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
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
//////////////////////////////////////////////////////////////////////
// OTAdmin - OpenTibia
//////////////////////////////////////////////////////////////////////
//
//////////////////////////////////////////////////////////////////////
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software Foundation,
// Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
//////////////////////////////////////////////////////////////////////
#include <iostream>
#include <string>
#include "commands.h"
#include "networkmessage.h"
#include "rsa.h"
extern long next_command_delay;
extern SOCKET g_socket;
extern bool g_connected;
extern bool g_shutdown;
int sendCommand(char commandByte, char* command);
void setSocketMode(bool blocking);
SocketCode sendMsg(NetworkMessage& msg, uint32_t* key = NULL);
std::string serverHost;
uint16_t serverPort;
#if defined WIN32 || defined __WINDOWS__
#define ERROR_SOCKET WSAGetLastError()
#else
#include <errno.h>
#define ERROR_SOCKET errno
#endif
//server localhost 7171
int setServer(char* params)
{
char host[256];
int port;
if(!params){
std::cerr << "[server] missing parameters" << std::endl;
return -1;
}
if(strlen(params) > 255){
std::cerr << "[server] too long host and port" << std::endl;
return -1;
}
if(sscanf(params, "%s %d", &host, &port) != 2){
std::cerr << "[server] no valid host or port" << std::endl;
return -1;
}
else{
serverHost = host;
serverPort = port;
return 1;
}
}
//connect test
int commandConnect(char* params)
{
if(g_connected == true){
std::cerr << "[connect] already connected" << std::endl;
return -1;
}
if(!params){
std::cerr << "[connect] missing parameters" << std::endl;
return -1;
}
char password[128];
if(strlen(params) > 127){
std::cerr << "[connect] too long password" << std::endl;
return -1;
}
if(sscanf(params, "%s", &password) != 1){
std::cerr << "[connect] no valid password" << std::endl;
return -1;
}
g_socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
uint32_t remote_ip = inet_addr(serverHost.c_str());
if(remote_ip == INADDR_NONE){
struct hostent* hp = gethostbyname(serverHost.c_str());
if(hp != 0){
remote_ip = *(long*)hp->h_addr;
}
else{
closesocket(g_socket);
std::cerr << "[connect] can not resolve server: " << serverHost << " - " << ERROR_SOCKET << std::endl;
return -1;
}
}
sockaddr_in serveraddr;
serveraddr.sin_family = AF_INET;
serveraddr.sin_addr.s_addr = remote_ip;
serveraddr.sin_port = htons(serverPort);
if(connect(g_socket, (SOCKADDR*)&serveraddr, sizeof(serveraddr)) == SOCKET_ERROR){
closesocket(g_socket);
std::cerr << "[connect] can not connect to server: " << serverHost << " - " << ERROR_SOCKET << std::endl;
return -1;
}
std::cout << "Connected to " << serverHost << std::endl;
setSocketMode(false);
NetworkMessage msg;
msg.AddByte(0xFE);
if(msg.WriteToSocket(g_socket) != SOCKET_CODE_OK){
closesocket(g_socket);
std::cerr << "[connect] error while sending first byte - " << ERROR_SOCKET << std::endl;
return -1;
}
msg.Reset();
//read server hello
if(msg.ReadFromSocket(g_socket) != SOCKET_CODE_OK){
closesocket(g_socket);
std::cerr << "[connect] error while reading hello - " << ERROR_SOCKET << std::endl;
return -1;
}
char byte = msg.GetByte();
if(byte != AP_MSG_HELLO){
closesocket(g_socket);
std::cerr << "[connect] no valid server hello" << std::endl;
return -1;
}
msg.GetU32();
std::string strversion = msg.GetString();
uint16_t security = msg.GetU16();
uint32_t options = msg.GetU32();
if(security & REQUIRE_ENCRYPTION){
strversion = strversion + " encryption";
if(options & ENCRYPTION_RSA1024XTEA){
strversion = strversion + "(RSA1024XTEA)";
}
else{
strversion = strversion + "(Not supported)";
}
}
if(security & REQUIRE_LOGIN){
strversion = strversion + " login";
}
std::cout << "Hello from " << strversion << std::endl ;
//set encryption
if(security & REQUIRE_ENCRYPTION){
if(options & ENCRYPTION_RSA1024XTEA){
//get public key
msg.Reset();
msg.AddByte(AP_MSG_KEY_EXCHANGE);
msg.AddByte(ENCRYPTION_RSA1024XTEA);
if(sendMsg(msg) != SOCKET_CODE_OK){
closesocket(g_socket);
std::cerr << "[connect] error while getting public key" << std::endl;
return -1;
}
char ret_code = msg.GetByte();
if(ret_code == AP_MSG_KEY_EXCHANGE_OK){
std::cout << "Key exchange OK" << std::endl;
}
else if(ret_code == AP_MSG_KEY_EXCHANGE_FAILED){
std::string error_desc = msg.GetString();
closesocket(g_socket);
std::cerr << "[connect] can not get public key: " << error_desc << std::endl;
return -1;
}
else{
closesocket(g_socket);
std::cerr << "[connect] not known response to key exchange request" << std::endl;
return -1;
}
unsigned char key_type = msg.GetByte();
if(key_type != ENCRYPTION_RSA1024XTEA){
closesocket(g_socket);
std::cerr << "[connect] no valid key returned" << std::endl;
return -1;
}
//the public key is 128 bytes
uint32_t rsa_mod[32];
for(unsigned int i = 0; i < 32; ++i){
rsa_mod[i] = msg.GetU32();
}
RSA::getInstance()->setPublicKey((char*)rsa_mod, "65537");
uint32_t random_key[32];
for(unsigned int i = 0; i < 32; ++i){
random_key[i] = rand() << 16 ^ rand();
}
msg.setRSAInstance(RSA::getInstance());
msg.Reset();
msg.AddByte(AP_MSG_ENCRYPTION);
msg.AddByte(ENCRYPTION_RSA1024XTEA);
//build the 128 bytes block
msg.AddByte(0);
for(unsigned int i = 0; i < 31; ++i){
msg.AddU32(random_key[i]);
}
msg.AddByte(0);
msg.AddByte(0);
msg.AddByte(0);
//
msg.RSA_encrypt();
if(sendMsg(msg, random_key) != SOCKET_CODE_OK){
closesocket(g_socket);
std::cerr << "[connect] error while sending private key" << std::endl;
return -1;
}
ret_code = msg.GetByte();
if(ret_code == AP_MSG_ENCRYPTION_OK){
std::cout << "Encryption OK" << std::endl;
}
else if(ret_code == AP_MSG_ENCRYPTION_FAILED){
std::string error_desc = msg.GetString();
closesocket(g_socket);
std::cerr << "[connect] can not set private key: " << error_desc << std::endl;
return -1;
}
else{
closesocket(g_socket);
std::cerr << "[connect] not known response to set private key request" << std::endl;
return -1;
}
}
else{
closesocket(g_socket);
std::cerr << "[connect] can not initiate encryption" << std::endl;
return -1;
}
}
//login
if(security & REQUIRE_LOGIN){
msg.Reset();
msg.AddByte(AP_MSG_LOGIN);
msg.AddString(std::string(password));
if(sendMsg(msg) != SOCKET_CODE_OK){
closesocket(g_socket);
std::cerr << "[connect] error while sending login" << std::endl;
return -1;
}
char ret_code = msg.GetByte();
if(ret_code == AP_MSG_LOGIN_OK){
std::cout << "Login OK" << std::endl;
}
else if(ret_code == AP_MSG_LOGIN_FAILED){
std::string error_desc = msg.GetString();
closesocket(g_socket);
std::cerr << "[connect] can not login: " << error_desc << std::endl;
return -1;
}
else{
closesocket(g_socket);
std::cerr << "[connect] not known response to login request" << std::endl;
return -1;
}
}
g_connected = true;
return 1;
}
//disconnect
int commandDisconnect(char* params)
{
if(g_connected != true){
std::cerr << "[disconnect] not connected" << std::endl;
return 1;
}
if(params){
std::cerr << "[disconnect] Warning: parameters ignored" << std::endl;
}
closesocket(g_socket);
g_socket = SOCKET_ERROR;
std::cout << "Disconnected" << std::endl;
g_connected = false;
return 1;
}
//sleep 10000
int sleep(char* params)
{
if(!params){
std::cerr << "[sleep] missing parameter" << std::endl;
return -1;
}
int delay;
if(sscanf(params, "%d", &delay) != 1){
std::cerr << "[sleep] no valid delay" << std::endl;
return -1;
}
else{
next_command_delay = delay;
std::cout << "Sleeping " << delay << " ms" << std::endl;
return 1;
}
}
//broadcast hello all
int commandBroadcast(char* params)
{
if(g_connected != true){
std::cerr << "[broadcast] not connected" << std::endl;
return -1;
}
if(!params){
std::cerr << "[broadcast] missing parameters" << std::endl;
return -1;
}
long n = strlen(params);
if(n > 127 || n == 0){
std::cerr << "[broadcast] no valid parameters" << std::endl;
return -1;
}
char message[128];
strcpy(message, params);
std::cout << "Broadcast: " << message << std::endl;
int ret = sendCommand(CMD_BROADCAST, message);
if(ret == -1){
std::cerr << "[broadcast] error sending broadcast" << std::endl;
return -1;
}
return ret;
}
//closeserver
int commandCloseServer(char* params)
{
if(g_connected != true){
std::cerr << "[closeserver] not connected" << std::endl;
return -1;
}
if(params){
std::cerr << "[closeserver] Warning: parameters ignored" << std::endl;
}
std::cout << "Closing server." << std::endl;
int ret = sendCommand(CMD_CLOSE_SERVER, NULL);
if(ret == -1){
std::cerr << "[closeserver] error closing server" << std::endl;
return -1;
}
return ret;
}
//shutdown
int commandShutdown(char* params)
{
if(g_connected != true){
std::cerr << "[shutdown] not connected" << std::endl;
return -1;
}
if(params){
std::cerr << "[shutdown] Warning: parameters ignored" << std::endl;
}
std::cout << "Server shutdown." << std::endl;
int ret = sendCommand(CMD_SHUTDOWN_SERVER, NULL);
if(ret == 1){
g_shutdown = true;
}
if(ret == -1){
std::cerr << "[shutdown] error in server shutdown" << std::endl;
return -1;
}
return ret;
}
//saveserver
int commandSaveServer(char* params)
{
if(g_connected != true){
std::cerr << "[saveserver] not connected" << std::endl;
return -1;
}
if(params){
std::cerr << "[saveserver] Warning: parameters ignored" << std::endl;
}
std::cout << "Saving server." << std::endl;
int ret = sendCommand(CMD_SAVE_SERVER, NULL);
if(ret == -1){
std::cerr << "[saveserver] error in server save" << std::endl;
return -1;
}
return ret;
}
//shallowsaveserver
int commandShallowSaveServer(char* params)
{
if(g_connected != true){
std::cerr << "[shallowsaveserver] not connected" << std::endl;
return -1;
}
if(params){
std::cerr << "[shallowsaveserver] Warning: parameters ignored" << std::endl;
}
std::cout << "Saving server (shallow)." << std::endl;
int ret = sendCommand(CMD_SHALLOW_SAVE_SERVER, NULL);
if(ret == -1){
std::cerr << "[shallowsaveserver] error in server save" << std::endl;
return -1;
}
return ret;
}
//relationalsaveserver
int commandRelationalSaveServer(char* params)
{
if(g_connected != true){
std::cerr << "[relationalsaveserver] not connected" << std::endl;
return -1;
}
if(params){
std::cerr << "[relationalsaveserver] Warning: parameters ignored" << std::endl;
}
std::cout << "Saving server (relational)." << std::endl;
int ret = sendCommand(CMD_RELATIONAL_SAVE_SERVER, NULL);
if(ret == -1){
std::cerr << "[relationalsaveserver] error in server save" << std::endl;
return -1;
}
return ret;
}
//sendmail
int commandSendMail(char* params)
{
if(g_connected != true){
std::cerr << "[sendmail] not connected" << std::endl;
return -1;
}
std::cout << "Sending mail." << std::endl;
int ret = sendCommand(CMD_SEND_MAIL, params);
if(ret == -1){
std::cerr << "[sendmail] error in send mail" << std::endl;
return -1;
}
return ret;
}
//kickplayer
int commandKickPlayer(char* params)
{
if(g_connected != true){
std::cerr << "[kickplayer] not connected" << std::endl;
return -1;
}
long n = strlen(params);
if(n > 127 || n == 0){
std::cerr << "[kickplayer] no valid parameters" << std::endl;
return -1;
}
char player[128];
strcpy(player, params);
std::cout << "Kicking player." << std::endl;
int ret = sendCommand(CMD_KICK, player);
if(ret == -1){
std::cerr << "[kickplayer] error while kicking player" << std::endl;
return -1;
}
return ret;
}
//payhouses
int commandPayHouses(char* params)
{
if(g_connected != true){
std::cerr << "[payhouses] not connected" << std::endl;
return -1;
}
if(params){
std::cerr << "[payhouses] Warning: parameters ignored" << std::endl;
}
std::cout << "Paying houses." << std::endl;
int ret = sendCommand(CMD_PAY_HOUSES, NULL);
if(ret == -1){
std::cerr << "[payhouses] error paying houses" << std::endl;
return -1;
}
return ret;
}
//internal use
int ping(char* params)
{
if(g_connected != true){
std::cerr << "[ping] not connected" << std::endl;
return -1;
}
if(params){
std::cerr << "[ping] Warning: parameters ignored" << std::endl;
}
//std::cout << "PING" << std::endl;
NetworkMessage msg;
msg.AddByte(AP_MSG_PING);
SocketCode ret = sendMsg(msg);
if(ret == SOCKET_CODE_ERROR){
std::cerr << "[ping] error sending ping" << std::endl;
return -1;
}
if(ret == SOCKET_CODE_TIMEOUT){
return 0;
}
char ret_code = msg.GetByte();
if(ret_code != AP_MSG_PING_OK){
std::cerr << "[ping] no valid ping" << std::endl;
return -1;
}
return ret;
}
//dummy function
int last(char* params)
{
std::cout << "[last] you should not be here!" << std::endl;
return 1;
}
//help functions
int sendCommand(char commandByte, char* command)
{
NetworkMessage msg;
msg.AddByte(AP_MSG_COMMAND);
msg.AddByte(commandByte);
if(command){
msg.AddString(command);
}
SocketCode ret = sendMsg(msg);
if(ret == SOCKET_CODE_ERROR){
std::cerr << "[sendCommand] error while sending command" << std::endl;
return -1;
}
if(ret == SOCKET_CODE_TIMEOUT){
return 0;
}
char ret_code = msg.GetByte();
if(ret_code == AP_MSG_COMMAND_OK){
return 1;
}
else if(ret_code == AP_MSG_COMMAND_FAILED){
std::string error_desc = msg.GetString();
std::cerr << "[sendCommand] error: " << error_desc << std::endl;
return 0;
}
else{
std::cerr << "[sendCommand] no known return code - " << ret_code << std::endl;
return 0;
}
}
void setSocketMode(bool blocking)
{
#if defined WIN32 || defined __WINDOWS__
// Set the socket I/O mode; iMode = 0 for blocking; iMode != 0 for non-blocking
unsigned long mode;
if(blocking){
mode = 0;
}
else{
mode = 1;
}
ioctlsocket(g_socket, FIONBIO, &mode);
#else
int flags = fcntl(g_socket, F_GETFL);
if(blocking){
flags &= (~O_NONBLOCK);
}
else{
flags |= O_NONBLOCK;
}
fcntl(g_socket, F_SETFL, flags);
#endif
}
SocketCode sendMsg(NetworkMessage& msg, uint32_t* key /*= NULL*/)
{
SocketCode ret = msg.WriteToSocket(g_socket);
msg.Reset();
if(ret == SOCKET_CODE_OK){
if(key){
msg.setEncryptionState(true);
msg.setEncryptionKey(key);
}
do{
ret = msg.ReadFromSocket(g_socket);
if(ret == SOCKET_CODE_OK){
char ret_code = msg.InspectByte();
if(ret_code == AP_MSG_ERROR){
msg.GetByte();
std::string error_desc = msg.GetString();
std::cerr << "[sendMsg] MSG_ERROR: " << error_desc << std::endl;
return SOCKET_CODE_ERROR;
}
}
else if(ret == SOCKET_CODE_ERROR){
std::cerr << "[sendMsg] error while reading - " << ERROR_SOCKET << std::endl;
return ret;
}
else{
//timeout, lets just wait a bit
OTSYS_SLEEP(5000);
NetworkMessage msg_keepAlive;
msg_keepAlive.AddByte(AP_MSG_KEEP_ALIVE);
if(msg_keepAlive.WriteToSocket(g_socket) != SOCKET_CODE_OK){
return SOCKET_CODE_ERROR;
}
}
}while(ret == SOCKET_CODE_TIMEOUT);
}
else if(ret == SOCKET_CODE_ERROR){
std::cerr << "[sendMsg] error while sending - " << ERROR_SOCKET << std::endl;
}
return ret;
}
//commands list
defcommands commands[] = {
{"server", &setServer},
{"connect", &commandConnect},
{"broadcast", &commandBroadcast},
{"closeserver", &commandCloseServer},
{"saveserver", &commandSaveServer},
{"shallowsaveserver", &commandShallowSaveServer},
{"relationalsaveserver", &commandRelationalSaveServer},
{"sendmail", &commandSendMail},
{"shutdown", &commandShutdown},
{"payhouses", &commandPayHouses},
{"disconnect", &commandDisconnect},
{"sleep", &sleep},
{"LAST", &last},
//internal commands
{"ping", &ping},
{"", NULL},
};
defcommands* getCommadsList()
{
return commands;
}