Debian Codegraph @

netgames/dmfc/dmfcremote.cpp

raw symbols SCIP
1 /*
2 * Descent 3
3 * Copyright (C) 2024 Parallax Software
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17
18 --- HISTORICAL COMMENTS FOLLOW ---
19
20 * $Logfile: /DescentIII/Main/Dmfc/dmfcremote.cpp $
21 * $Revision: 1.1.1.1 $
22 * $Date: 2003/08/26 03:57:21 $
23 * $Author: kevinb $
24 *
25 * Remote administration
26 *
27 * $Log: dmfcremote.cpp,v $
28 * Revision 1.1.1.1 2003/08/26 03:57:21 kevinb
29 * initial 1.5 import
30 *
31 *
32 * 6 7/14/99 11:48a Jeff
33 * localized text for patch fixes
34 *
35 * 5 7/09/99 6:18p Jeff
36 * added $remoteadminlogout and $wait commands
37 *
38 * 4 7/08/99 6:25p Jeff
39 * remote admin in and working
40 *
41 * 3 7/08/99 2:38a Jeff
42 * rough implementation of remote administration checked in. Still needs
43 * some polishing, but should work basically.
44 *
45 * 2 7/07/99 7:07p Jeff
46 * created dmfcremote.cpp
47 *
48 * $NoKeywords: $
49 */
50
51 #include "gamedll_header.h"
52 #include "DMFC.h"
53 #include "dmfcinternal.h"
54 #include "encryption.h"
55
56 #include <stdlib.h>
57 #include <memory.h>
58
59 extern DMFCBase *basethis;
60
61 struct tRemotePlayerData {
62 bool authorized; // whether the player is authorized
63 uint8_t curr_key[8]; // current encryption key
64 };
65
66 static tRemotePlayerData Authorized_players[MAX_PLAYER_RECORDS];
67 static char Remote_Admin_pass[64];
68 bool Use_remote_admin = false;
69 static bool IAmAnAdmin = false;
70
71 static void Remote_encrypt(uint8_t key[8], int size_buffer, uint8_t *buffer, int *new_size, uint8_t **new_buffer);
72 static void Remote_decrypt(uint8_t key[8], int size_buffer, uint8_t *buffer, int *new_size, uint8_t **new_buffer);
73
74 // Sends a string to the server (function encrypts it before sending)
75 void Remote_SendStringToServer(const char *string);
76 // Recieves a string from the server (function decrypts it)
77 void Remote_RecieveStringFromServer(uint8_t *data, char *buffer, int size);
78 // Sends a string to a client (function encrypts it before sending)
79 void Remote_SendStringToClient(int client, const char *string);
80 // Recieves a string from a client (function decrypts it)
81 int Remote_RecieveStringFromClient(uint8_t *data, char *buffer, int size);
82 // Handles remote admin packets from a client
83 void Remote_HandleClientPacket(uint8_t *data);
84 // Handles remote admin packets from a client
85 void Remote_HandleServerPacket(uint8_t *data);
86
87 // Sends a message from the server to a client
88 void Remote_SendMessage(int client, const char *message);
89 // Receives a message from the server
90 void Remote_GetMessage(uint8_t *data);
91
92 // Logs a player out from being an administrator
93 void Remote_Logout(int precnum);
94
95 // filters any remote commands not allowed to be executed remotely
96 // -1 : not found
97 // 0 : can't call
98 // 1 : can call
99 int FilterNonRemoteCommands(char *command);
100
101 // Initializes the remote administration system
102 void Remote_Initialize(void) {
103 // initialize encryption keys
104 srand((uint32_t)time(0));
105
106 int i, j;
107 for (i = 0; i < MAX_PLAYER_RECORDS; i++) {
108 Authorized_players[i].authorized = false;
109
110 for (j = 0; j < 8; j++) {
111 Authorized_players[i].curr_key[j] = rand() % 255;
112 }
113 }
114
115 memset(Remote_Admin_pass, 0, 64);
116
117 // setup packet recievers
118 #ifdef WIN32
119 basethis->RegisterPacketReceiver(SPID_REMOTETOSERVER, Remote_HandleClientPacket);
120 basethis->RegisterPacketReceiver(SPID_REMOTETOCLIENT, Remote_HandleServerPacket);
121 basethis->RegisterPacketReceiver(SPID_SERVERREMOTEMSG, Remote_GetMessage);
122 #else
123 basethis->RegisterPacketReceiver(SPID_REMOTETOSERVER, &Remote_HandleClientPacket);
124 basethis->RegisterPacketReceiver(SPID_REMOTETOCLIENT, &Remote_HandleServerPacket);
125 basethis->RegisterPacketReceiver(SPID_SERVERREMOTEMSG, &Remote_GetMessage);
126 #endif
127
128 if (basethis->GetLocalRole() == LR_SERVER) {
129 bool bTemp;
130 int pass_len = 64;
131 DatabaseRead1("DMFCRemoteAdminPass", Remote_Admin_pass, &pass_len);
132 if (DatabaseRead3("DMFCRemoteAdmin", &bTemp)) {
133 Remote_Enable(bTemp);
134 }
135 }
136 }
137
138 // Processes remote administration for the frame
139 void Remote_ProcessFrame(void) {
140 if (basethis->GetLocalRole() != LR_SERVER)
141 return;
142
143 // go through all the players, and update their state (whether
144 // they are in the game or not)
145 int p;
146 for (p = 0; p < MAX_PLAYER_RECORDS; p++) {
147 player_record *pr = PRec_GetPRecord(p);
148
149 if (Authorized_players[p].authorized) {
150 // this is an authorized player, update them
151
152 // make sure they are in the game
153 if (!pr || pr->state != STATE_INGAME) {
154 // the player is no longer in the game
155 mprintf(0, "REMOTE: Removing authorization for %s\n", (pr->callsign) ? (pr->callsign) : "<UNKNOWN>");
156 Remote_Logout(p);
157
158 // change the key for the slot
159 int j;
160 for (j = 0; j < 8; j++) {
161 Authorized_players[p].curr_key[j] = rand() % 255;
162 }
163
164 } else {
165 // the player is authorized and in the game
166 }
167 }
168 }
169 }
170
171 // Gets the key for the given player record
172 uint8_t *Remote_GetKey(int prec) {
173 ASSERT(prec >= 0 && prec < MAX_PLAYER_RECORDS);
174 return Authorized_players[prec].curr_key;
175 }
176
177 // Enables/Disables remote administration
178 void Remote_Enable(bool enable) {
179 if (basethis->GetLocalRole() != LR_SERVER)
180 return;
181
182 Use_remote_admin = enable;
183
184 if (*Remote_Admin_pass == '\0' && enable) {
185 Use_remote_admin = false;
186 }
187
188 mprintf(0, "Remote Administration: %s\n", (Use_remote_admin) ? "On" : "Off");
189
190 if (!Use_remote_admin) {
191 // log out all the logged in players
192 int p;
193 for (p = 0; p < MAX_PLAYER_RECORDS; p++) {
194 if (Authorized_players[p].authorized) {
195 // log the player out
196 Remote_Logout(p);
197 }
198 }
199 }
200
201 DatabaseWrite2("DMFCRemoteAdmin", Use_remote_admin);
202 }
203
204 // sets the administration password for the server
205 // pass in an unencrypted password
206 void Remote_SetPassword(char *password) {
207 if (basethis->GetLocalRole() != LR_SERVER)
208 return;
209 strncpy(Remote_Admin_pass, password, 63);
210 Remote_Admin_pass[63] = '\0';
211 DatabaseWrite1("DMFCRemoteAdminPass", Remote_Admin_pass, strlen(Remote_Admin_pass));
212 }
213
214 // Logs a player record in as an administrator
215 bool Remote_Login(int precnum, char *pass) {
216 if (precnum < 0 || precnum >= MAX_PLAYER_RECORDS)
217 return false;
218
219 player_record *pr = PRec_GetPRecord(precnum);
220 if (!pr || pr->state != STATE_INGAME) {
221 return false;
222 }
223
224 if (Authorized_players[precnum].authorized)
225 return true;
226
227 if (strcmp(pass, Remote_Admin_pass) == 0) {
228 Authorized_players[precnum].authorized = true;
229 }
230
231 return Authorized_players[precnum].authorized;
232 }
233
234 // Logs a player out from being an administrator
235 void Remote_Logout(int precnum) {
236 if (precnum < 0 || precnum >= MAX_PLAYER_RECORDS)
237 return;
238
239 if (!Authorized_players[precnum].authorized)
240 return;
241
242 player_record *pr = PRec_GetPRecord(precnum);
243 if (!pr)
244 return;
245 if (pr->state == STATE_INGAME) {
246 Remote_SendStringToClient(pr->pnum, "LOGOUTSUCCESS");
247 Remote_SendMessage(pr->pnum, DTXT_REMOTELOGGEDOUT);
248 }
249 Authorized_players[precnum].authorized = false;
250
251 DLLAddColoredHUDMessage(GR_RGB(200, 200, 40), DTXT_HUDMSGLOGGEDOUT, pr->callsign);
252 }
253
254 // Lists the players logged in
255 void Remote_ListLogins(void) {
256 int prec;
257 player_record *pr;
258
259 char buffer[512];
260 char temp[64];
261 int len, curlen;
262
263 curlen = 0;
264 *buffer = '\0';
265
266 for (prec = 0; prec < MAX_PLAYER_RECORDS; prec++) {
267 if (Authorized_players[prec].authorized) {
268 pr = PRec_GetPRecord(prec);
269 if (pr && pr->state != STATE_EMPTY) {
270 len = strlen(pr->callsign);
271 if (curlen + len > 500) {
272 // output the hudmessage
273 DLLAddColoredHUDMessage(GR_RGB(200, 200, 40), buffer);
274 *buffer = '\0';
275 curlen = 0;
276 }
277 if (*buffer != '\0') {
278 strncat(buffer, ":", sizeof(buffer) - strlen(buffer) - 1);
279 }
280 snprintf(temp, sizeof(temp), "%s[%d]", pr->callsign, prec);
281 strncat(buffer, temp, sizeof(buffer) - strlen(buffer) - 1);
282 curlen = strlen(buffer);
283 }
284 }
285 }
286
287 if (*buffer != '\0') {
288 DLLAddColoredHUDMessage(GR_RGB(200, 200, 40), buffer);
289 }
290 }
291
292 extern player_record Player_records[MAX_PLAYER_RECORDS];
293 int translate_precptr_to_index(player_record *pr) {
294 int index = (pr - Player_records);
295 if (index < 0 || index >= MAX_PLAYER_RECORDS)
296 index = -1;
297 return index;
298 }
299
300 // Sets a clients key
301 void Remote_SetMyKey(uint8_t key[8]) {
302 player_record *pr;
303 pr = PRec_GetPRecordByPnum(basethis->GetPlayerNum());
304 int prec = translate_precptr_to_index(pr);
305 if (prec == -1) {
306 mprintf(0, "INVALID PREC!\n");
307 Int3();
308 return;
309 }
310
311 memcpy(Authorized_players[prec].curr_key, key, 8);
312 }
313
314 // handles a remote command (client side)
315 void Remote_ClientProcess(const char *command) {
316 if (basethis->GetLocalRole() == LR_SERVER)
317 return;
318 if (!IAmAnAdmin) {
319 // only let "login" go through
320 if (!strnicmp(command, "login", strlen("login"))) {
321 mprintf(0, "Sending login attempt for remote administration\n");
322 } else {
323 return;
324 }
325 }
326
327 const char *ptr = command;
328 while (*ptr == ' ')
329 ptr++;
330 if (*ptr) {
331 Remote_SendStringToServer(ptr);
332 }
333 }
334
335 // handles a remote command (server side)
336 void Remote_ServerProcess(int precnum, char *command) {
337 if (basethis->GetLocalRole() != LR_SERVER)
338 return;
339 if (precnum < 0 || precnum >= MAX_PLAYER_RECORDS)
340 return;
341 if (!Use_remote_admin)
342 return;
343
344 if (!Authorized_players[precnum].authorized) {
345 // check to see if the player is trying to login
346 int loginlen = strlen("login ");
347 if (!strnicmp(command, "login ", loginlen)) {
348 char *ptr = &command[loginlen - 1];
349 while (*ptr == ' ')
350 ptr++;
351 if (Remote_Login(precnum, ptr)) {
352 // successful login
353 player_record *pr = PRec_GetPRecord(precnum);
354 DLLAddColoredHUDMessage(GR_RGB(200, 200, 40), DTXT_HUDMSGLOGGEDIN, pr->callsign);
355 Remote_SendStringToClient(pr->pnum, "LOGINSUCCESS");
356 Remote_SendMessage(pr->pnum, DTXT_REMOTELOGGEDIN);
357 }
358 return;
359 }
360 }
361
362 player_record *pr = PRec_GetPRecord(precnum);
363
364 // check to see if they are logging out
365 if (!stricmp(command, "logout")) {
366 // log the player out
367 Remote_Logout(precnum);
368 return;
369 }
370
371 // process the command
372 if (command[0] == '$') {
373 switch (FilterNonRemoteCommands(command)) {
374 case 1: {
375 DLLAddColoredHUDMessage(GR_RGB(200, 200, 40), DTXT_REMOTEEXECUTE, pr->callsign, command);
376
377 if (basethis->InputCommandHandle(command)) {
378 // command handled
379 Remote_SendMessage(pr->pnum, DTXT_REMOTESUCCESS);
380 } else {
381 // command not handled
382 Remote_SendMessage(pr->pnum, DTXT_REMOTENOTSUCCESS);
383 }
384 } break;
385 case 0: {
386 // command not handled
387 Remote_SendMessage(pr->pnum, DTXT_REMOTENOTALLOWED);
388 } break;
389 default: {
390 // command not handled
391 Remote_SendMessage(pr->pnum, DTXT_REMOTENOTFOUND);
392 } break;
393 }
394 } else {
395 // command not handled
396 Remote_SendMessage(pr->pnum, DTXT_REMOTENOTFOUND);
397 }
398 }
399
400 // filters any remote commands not allowed to be executed remotely
401 int FilterNonRemoteCommands(char *command) {
402 // cut off whats after the first word (the command)
403 char cmd[64];
404 strncpy(cmd, command, 63);
405 cmd[63] = '\0';
406 char *ptr = cmd;
407 while (*ptr != ' ' && *ptr != '\0')
408 ptr++;
409 *ptr = '\0';
410
411 return basethis->CanInputCommandBeUsedRemotely((*cmd == '$') ? cmd + 1 : cmd);
412 }
413
414 // Sends a message from the server to a client
415 void Remote_SendMessage(int client, const char *message) {
416 if (basethis->GetLocalRole() != LR_SERVER)
417 return;
418
419 char *allocated = NULL;
420 int len = strlen(message);
421 const int maxlen = ((MAX_GAME_DATA_SIZE - 5) < 511) ? (MAX_GAME_DATA_SIZE - 5) : 511;
422 if (len > maxlen) {
423 allocated = new char[maxlen + 1];
424 memcpy(allocated, message, maxlen);
425 allocated[maxlen] = '\0';
426 message = allocated;
427 len = maxlen;
428 }
429
430 int count = 0;
431 uint8_t data[MAX_GAME_DATA_SIZE];
432 basethis->StartPacket(data, SPID_SERVERREMOTEMSG, &count);
433
434 MultiAddString(message, data, &count);
435
436 basethis->SendPacket(data, count, client);
437 delete[] allocated;
438 }
439
440 // Receives a message from the server
441 void Remote_GetMessage(uint8_t *data) {
442 int count = 0;
443 char buffer[512];
444
445 MultiGetString(buffer, data, &count);
446
447 DLLAddColoredHUDMessage(GR_RGB(200, 200, 40), "==%s==", buffer);
448 }
449
450 // Sends a string to the server (function encrypts it before sending)
451 void Remote_SendStringToServer(const char *string) {
452 if (basethis->GetLocalRole() == LR_SERVER) {
453 Int3();
454 return;
455 }
456
457 int slen;
458 int new_strlen;
459 uint8_t *packet_data;
460
461 slen = strlen(string);
462 player_record *pr;
463 pr = PRec_GetPRecordByPnum(basethis->GetPlayerNum());
464 int prec = translate_precptr_to_index(pr);
465 if (prec == -1) {
466 mprintf(0, "INVALID PREC!\n");
467 Int3();
468 return;
469 }
470
471 Remote_encrypt(Authorized_players[prec].curr_key, slen, (uint8_t *)string, &new_strlen, &packet_data);
472
473 if (new_strlen == 0) {
474 mprintf(0, "COULDN'T ENCRYPT\n");
475 Int3();
476 return;
477 }
478
479 // now we can send off the packet
480 int count = 0;
481 uint8_t data[MAX_GAME_DATA_SIZE];
482 basethis->StartPacket(data, SPID_REMOTETOSERVER, &count);
483
484 MultiAddByte(prec, data, &count);
485 MultiAddInt(new_strlen, data, &count);
486 memcpy(&data[count], packet_data, new_strlen);
487 count += new_strlen;
488
489 basethis->SendPacket(data, count, SP_SERVER);
490 free(packet_data);
491 }
492
493 // Handles remote admin packets from a client
494 void Remote_HandleServerPacket(uint8_t *data) {
495 char buffer[512];
496 Remote_RecieveStringFromServer(data, buffer, 512);
497
498 if (!strcmp(buffer, "LOGOUTSUCCESS")) {
499 // we logged out
500 IAmAnAdmin = false;
501 } else if (!strcmp(buffer, "LOGINSUCCESS")) {
502 // we logged in
503 IAmAnAdmin = true;
504 }
505 }
506
507 // Recieves a string from the server (function decrypts it)
508 void Remote_RecieveStringFromServer(uint8_t *data, char *buffer, int size) {
509 int count = 0;
510 int len = MultiGetInt(data, &count);
511
512 player_record *pr;
513 pr = PRec_GetPRecordByPnum(basethis->GetPlayerNum());
514 int prec = translate_precptr_to_index(pr);
515 if (prec == -1) {
516 mprintf(0, "INVALID PREC!\n");
517 Int3();
518 return;
519 }
520
521 ASSERT(len > 0);
522 if (len <= 0)
523 return;
524
525 uint8_t *packet;
526 packet = (uint8_t *)malloc(len);
527 memcpy(packet, &data[count], len);
528 count += len;
529
530 int new_size;
531 uint8_t *new_buffer;
532 Remote_decrypt(Authorized_players[prec].curr_key, len, packet, &new_size, &new_buffer);
533 if (new_buffer) {
534 strncpy(buffer, (char *)new_buffer, size - 1);
535 buffer[size - 1] = '\0';
536 free(new_buffer);
537 } else {
538 *buffer = '\0';
539 }
540
541 free(packet);
542 }
543
544 // Sends a string to a client (function encrypts it before sending)
545 void Remote_SendStringToClient(int client, const char *string) {
546 if (basethis->GetLocalRole() != LR_SERVER) {
547 Int3();
548 return;
549 }
550
551 int slen;
552 int new_strlen;
553 uint8_t *packet_data;
554
555 slen = strlen(string);
556 player_record *pr;
557 pr = PRec_GetPRecordByPnum(client);
558 int prec = translate_precptr_to_index(pr);
559 if (prec == -1) {
560 mprintf(0, "INVALID PREC!\n");
561 Int3();
562 return;
563 }
564
565 // check to make sure the player is authorized before sending
566 if (!Authorized_players[prec].authorized) {
567 Int3();
568 return;
569 }
570
571 Remote_encrypt(Authorized_players[prec].curr_key, slen, (uint8_t *)string, &new_strlen, &packet_data);
572
573 if (new_strlen == 0) {
574 mprintf(0, "COULDN'T ENCRYPT\n");
575 Int3();
576 return;
577 }
578
579 // now we can send off the packet
580 int count = 0;
581 uint8_t data[MAX_GAME_DATA_SIZE];
582 basethis->StartPacket(data, SPID_REMOTETOCLIENT, &count);
583
584 MultiAddInt(new_strlen, data, &count);
585 memcpy(&data[count], packet_data, new_strlen);
586 count += new_strlen;
587
588 basethis->SendPacket(data, count, client);
589 free(packet_data);
590 }
591
592 // Handles remote admin packets from a client
593 void Remote_HandleClientPacket(uint8_t *data) {
594 char buffer[512];
595 int prec = Remote_RecieveStringFromClient(data, buffer, 512);
596 if (prec == -1)
597 return;
598
599 Remote_ServerProcess(prec, buffer);
600 }
601
602 // Recieves a string from a client (function decrypts it)
603 int Remote_RecieveStringFromClient(uint8_t *data, char *buffer, int size) {
604 int count = 0;
605 int prec = MultiGetByte(data, &count);
606 int len = MultiGetInt(data, &count);
607
608 ASSERT(len > 0);
609 ASSERT(prec >= 0 && prec < MAX_PLAYER_RECORDS);
610 if (prec < 0 || prec >= MAX_PLAYER_RECORDS)
611 return -1;
612 if (len <= 0)
613 return -1;
614
615 uint8_t *packet;
616 packet = (uint8_t *)malloc(len);
617 memcpy(packet, &data[count], len);
618 count += len;
619
620 int new_size;
621 uint8_t *new_buffer;
622 Remote_decrypt(Authorized_players[prec].curr_key, len, packet, &new_size, &new_buffer);
623 if (new_buffer) {
624 size = (len < size) ? len + 1 : size;
625 strncpy(buffer, (char *)new_buffer, size - 1);
626 buffer[size - 1] = '\0';
627 free(new_buffer);
628 } else {
629 *buffer = '\0';
630 }
631
632 free(packet);
633 return prec;
634 }
635
636 void Remote_pad_buffer(int orig_size, const uint8_t *orig_buffer, int *new_size, uint8_t **new_buffer) {
637 // bump buffer up to size of mod 8
638 *new_size = orig_size;
639 if (orig_size < 8 || (orig_size % 8) != 0) {
640 // we need to pad up
641 int chunks = orig_size / 8;
642 *new_size = (chunks + 1) * 8;
643 }
644
645 *new_buffer = (uint8_t *)malloc(*new_size);
646 if (!(*new_buffer)) {
647 *new_size = 0;
648 return;
649 }
650 memset(*new_buffer, 0, *new_size);
651 memcpy(*new_buffer, orig_buffer, orig_size);
652 }
653
654 static void Remote_encrypt(uint8_t key[8], int size_buffer, uint8_t *buffer, int *new_size, uint8_t **new_buffer) {
655 *new_buffer = NULL;
656
657 uint8_t *buff;
658 Remote_pad_buffer(size_buffer, buffer, new_size, &buff);
659 if (*new_size == 0)
660 return;
661
662 IceKey ice(0);
663
664 ice.set((uint8_t *)key);
665
666 int i, num_chunks = 0;
667 int chunk_offset = 0;
668 uint8_t *cipher;
669 cipher = (uint8_t *)malloc(*new_size);
670 if (!cipher)
671 return;
672
673 num_chunks = (*new_size) / 8;
674 for (i = 0; i < num_chunks; i++) {
675 ice.encrypt(&buff[chunk_offset], &cipher[chunk_offset]);
676 chunk_offset += 8;
677 }
678
679 free(buff);
680 *new_buffer = cipher;
681 }
682
683 static void Remote_decrypt(uint8_t key[8], int size_buffer, uint8_t *buffer, int *new_size, uint8_t **new_buffer) {
684 *new_buffer = NULL;
685
686 uint8_t *buff;
687 Remote_pad_buffer(size_buffer, buffer, new_size, &buff);
688 if (*new_size == 0)
689 return;
690
691 IceKey ice(0);
692
693 ice.set((uint8_t *)key);
694
695 int i, num_chunks = 0;
696 int chunk_offset = 0;
697 uint8_t *cipher;
698 cipher = (uint8_t *)malloc(*new_size);
699 if (!cipher)
700 return;
701
702 num_chunks = (*new_size) / 8;
703 for (i = 0; i < num_chunks; i++) {
704 ice.decrypt(&buff[chunk_offset], &cipher[chunk_offset]);
705 chunk_offset += 8;
706 }
707
708 free(buff);
709 *new_buffer = cipher;
710 }