-
Notifications
You must be signed in to change notification settings - Fork 0
/
central.c
835 lines (689 loc) · 22.2 KB
/
central.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
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
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
// central.c - communication with central server
#include "qwsvdef.h"
#include <curl/curl.h>
#ifndef SERVER_ONLY
#include "quakedef.h"
#endif
#define GENERATE_CHALLENGE_PATH "Authentication/GenerateChallenge"
#define CHALLENGETEXT_RESPONSE_PATH "Authentication/GenerateResponse"
#define VERIFY_RESPONSE_PATH "Authentication/VerifyResponse"
#define CHECKIN_PATH "ServerApi/Checkin"
#define MIN_CHECKIN_PERIOD 60
#ifdef _WIN32
#ifdef SERVER_ONLY
#pragma comment(lib, "libcurld.lib")
#pragma comment(lib, "ws2_32.lib")
#pragma comment(lib, "wldap32.lib")
#endif
#endif
#ifndef SERVER_ONLY
// Have set this to read-only to stop this accidentally on-purpose being set to another site by someone else's config
static cvar_t cl_www_address = { "cl_www_address", "https://badplace.eu/", CVAR_ROM };
#else
static cvar_t sv_www_address = { "sv_www_address", "" };
static cvar_t sv_www_authkey = { "sv_www_authkey", "" };
static cvar_t sv_www_checkin_period = { "sv_www_checkin_period", "60" };
static void Web_ConstructServerURL(char* url, const char* path, int sizeof_url);
static void Web_SubmitRequestFormServer(const char* url, struct curl_httppost* first_form_ptr, struct curl_httppost* last_form_ptr, web_response_func_t callback, const char* requestId, void* internal_data);
#endif
static CURLM* curl_handle = NULL;
static double last_checkin_time;
#define MAX_RESPONSE_LENGTH (4096*2)
struct web_request_data_s;
typedef void(*web_response_func_t)(struct web_request_data_s* req, qbool valid);
typedef struct web_request_data_s {
CURL* handle;
double time_sent;
// response from server
char response[MAX_RESPONSE_LENGTH];
size_t response_length;
// form data
struct curl_httppost *first_form_ptr;
struct curl_httppost *last_form_ptr;
// called when response complete
web_response_func_t onCompleteCallback;
void* internal_data;
char* request_id; // if set, content will be passed to game-mod. will be Q_free()'d
struct web_request_data_s* next;
} web_request_data_t;
static web_request_data_t* web_requests;
static size_t Web_StandardTokenWrite(void* buffer, size_t size, size_t nmemb, void* userp)
{
web_request_data_t* data = (web_request_data_t*)userp;
size_t available = sizeof(data->response) - data->response_length;
if (size * nmemb > available) {
Con_DPrintf("WWW: Response too large, size*nmemb = %d, available %d\n", size * nmemb, available);
return 0;
}
else if (size * nmemb > 0) {
Con_DPrintf("WWW: response received, writing %d bytes\n", size * nmemb);
memcpy(data->response + data->response_length, buffer, size * nmemb);
data->response_length += size * nmemb;
Con_DPrintf("WWW: response_length now %d bytes\n", data->response_length);
}
return size * nmemb;
}
static void Web_SubmitRequestFormGeneric(const char* url, struct curl_httppost* first_form_ptr, struct curl_httppost* last_form_ptr, web_response_func_t callback, const char* requestId, void* internal_data)
{
CURL* req = curl_easy_init();
web_request_data_t* data = (web_request_data_t*)Q_malloc(sizeof(web_request_data_t));
data->onCompleteCallback = callback;
data->time_sent = curtime;
data->internal_data = internal_data;
data->handle = req;
data->request_id = requestId && requestId[0] ? Q_strdup(requestId) : NULL;
data->first_form_ptr = first_form_ptr;
curl_easy_setopt(req, CURLOPT_URL, url);
curl_easy_setopt(req, CURLOPT_WRITEDATA, data);
curl_easy_setopt(req, CURLOPT_WRITEFUNCTION, Web_StandardTokenWrite);
if (first_form_ptr) {
curl_easy_setopt(req, CURLOPT_POST, 1);
curl_easy_setopt(req, CURLOPT_HTTPPOST, first_form_ptr);
}
curl_multi_add_handle(curl_handle, req);
data->next = web_requests;
web_requests = data;
}
typedef struct response_field_s {
const char* name;
const char** value;
} response_field_t;
static int ProcessWebResponse(web_request_data_t* req, response_field_t* fields, int field_count)
{
char* colon, *newline;
char* start = req->response;
int i;
int total_fields = 0;
// Response should be multiple lines, <Key>:<Value>\n
// For multi-line values, prefix end of line with $
req->response[req->response_length] = '\0';
while ((colon = strchr(start, ':'))) {
newline = strchr(colon, '\n');
if (newline) {
while (newline && newline != colon + 1 && *(newline - 1) == '$') {
*(newline - 1) = ' ';
newline = strchr(newline + 1, '\n');
}
if (newline) {
*newline = '\0';
}
}
*colon = '\0';
for (i = 0; i < field_count; ++i) {
if (!strcmp(start, fields[i].name)) {
*fields[i].value = colon + 1;
}
}
if (newline == NULL) {
break;
}
start = newline + 1;
while (*start && (*start == 10 || *start == 13)) {
++start;
}
++total_fields;
}
return total_fields;
}
#ifdef SERVER_ONLY
static int utf8Encode(char in, char* out1, char* out2) {
if (in <= 0x7F) {
// <= 127 is encoded as single byte, no translation
*out1 = in;
return 1;
}
else {
// Two byte characters... 5 bits then 6
*out1 = 0xC0 | ((in >> 6) & 0x1F);
*out2 = 0x80 | (in & 0x3F);
return 2;
}
}
static void Auth_GenerateChallengeResponse(web_request_data_t* req, qbool valid)
{
client_t* client = (client_t*) req->internal_data;
const char* response = NULL;
const char* challenge = NULL;
const char* message = NULL;
response_field_t fields[] = {
{ "Result", &response },
{ "Challenge", &challenge },
{ "Message", &message }
};
if (client->login_request_time != req->time_sent) {
// Ignore result, subsequent request sent
return;
}
req->internal_data = NULL;
ProcessWebResponse(req, fields, sizeof(fields) / sizeof(fields[0]));
if (response && !strcmp(response, "Success") && challenge) {
char buffer[128];
strlcpy(buffer, "//challenge ", sizeof(buffer));
strlcat(buffer, challenge, sizeof(buffer));
strlcat(buffer, "\n", sizeof(buffer));
strlcpy(client->challenge, challenge, sizeof(client->challenge));
SV_ClientPrintf2(client, PRINT_HIGH, "Challenge stored...\n", message);
ClientReliableWrite_Begin (client, svc_stufftext, 2+strlen(buffer));
ClientReliableWrite_String (client, buffer);
}
else if (message) {
SV_ClientPrintf2(client, PRINT_HIGH, "Error: %s\n", message);
}
else {
// Maybe add CURLOPT_ERRORBUFFER?
SV_ClientPrintf2(client, PRINT_HIGH, "Error: unknown error\n");
}
}
// Initial stage of login: ask server to create challenge/response pair
// Connected client to supply the response in order to prove identity
void Central_GenerateChallenge(client_t* client, const char* username, qbool during_login)
{
char url[512];
struct curl_httppost* first_form_ptr = NULL;
struct curl_httppost* last_form_ptr = NULL;
CURLFORMcode code;
if (!sv_www_address.string[0]) {
SV_ClientPrintf2(client, PRINT_HIGH, "Remote logins not supported on this server\n");
return;
}
Web_ConstructServerURL(url, GENERATE_CHALLENGE_PATH, sizeof(url));
code = curl_formadd(&first_form_ptr, &last_form_ptr,
CURLFORM_PTRNAME, "userName",
CURLFORM_COPYCONTENTS, username,
CURLFORM_END
);
if (code != CURL_FORMADD_OK) {
SV_ClientPrintf2(client, PRINT_HIGH, "Failed to generate challenge (0: %u)\n", code);
return;
}
code = curl_formadd(&first_form_ptr, &last_form_ptr,
CURLFORM_PTRNAME, "status",
CURLFORM_COPYCONTENTS, during_login ? 0 : 1,
CURLFORM_END
);
if (code != CURL_FORMADD_OK) {
SV_ClientPrintf2(client, PRINT_HIGH, "Failed to generate challenge (1: %u)\n", code);
return;
}
client->login_request_time = curtime;
Web_SubmitRequestFormServer(url, first_form_ptr, last_form_ptr, Auth_GenerateChallengeResponse, NULL, client);
}
// Final stage of login: verify challenge/response with central server
void Auth_ProcessLoginAttempt(web_request_data_t* req, qbool valid)
{
client_t* client = (client_t*) req->internal_data;
const char* response = NULL;
const char* login = NULL;
const char* preferred_alias = NULL;
const char* message = NULL;
const char* flag = NULL;
response_field_t fields[] = {
{ "Result", &response },
{ "Alias", &preferred_alias },
{ "Login", &login },
{ "Message", &message },
{ "Flag", &flag }
};
req->internal_data = NULL;
if (client->login_request_time != req->time_sent) {
// Ignore result, subsequent request sent
return;
}
ProcessWebResponse(req, fields, sizeof(fields) / sizeof(fields[0]));
if (response && !strcmp(response, "Success")) {
if (login) {
char oldval[MAX_EXT_INFO_STRING];
strlcpy(oldval, Info_Get(&client->_userinfo_ctx_, "*auth"), sizeof(oldval));
strlcpy(client->login, login, sizeof(client->login));
Info_SetStar(&client->_userinfo_ctx_, "*auth", login);
ProcessUserInfoChange(client, "*auth", oldval);
}
flag = (flag ? flag : "");
strlcpy(client->login_flag, flag, sizeof(client->login_flag));
Info_SetStar(&client->_userinfo_ctx_, "*flag", flag);
ProcessUserInfoChange(client, "*flag", flag);
preferred_alias = preferred_alias ? preferred_alias : login;
if (preferred_alias) {
strlcpy(client->login_alias, preferred_alias, sizeof(client->login_alias));
}
client->logged_in_via_web = true;
SV_LoginWebCheck(client);
}
else if (message) {
SV_ClientPrintf2(client, PRINT_HIGH, "Error: %s\n", message);
SV_LoginWebFailed(client);
}
else {
// Maybe add CURLOPT_ERRORBUFFER?
SV_ClientPrintf2(client, PRINT_HIGH, "Error: unknown error (invalid response from server)\n");
SV_LoginWebFailed(client);
}
}
// Final stage of login: client has supplied response to challenge, now check with central server
void Central_VerifyChallengeResponse(client_t* client, const char* challenge, const char* response)
{
char url[512];
struct curl_httppost* first_form_ptr = NULL;
struct curl_httppost* last_form_ptr = NULL;
CURLFORMcode code;
if (!sv_www_address.string[0]) {
SV_ClientPrintf2(client, PRINT_HIGH, "Remote logins not supported on this server\n");
return;
}
code = curl_formadd(&first_form_ptr, &last_form_ptr,
CURLFORM_PTRNAME, "challenge",
CURLFORM_COPYCONTENTS, challenge,
CURLFORM_END
);
if (code != CURL_FORMADD_OK) {
SV_ClientPrintf2(client, PRINT_HIGH, "Failed to generate request (1: %u)\n", code);
return;
}
code = curl_formadd(&first_form_ptr, &last_form_ptr,
CURLFORM_PTRNAME, "response",
CURLFORM_COPYCONTENTS, response,
CURLFORM_END
);
if (code != CURL_FORMADD_OK) {
SV_ClientPrintf2(client, PRINT_HIGH, "Failed to generate request (2: %u)\n", code);
return;
}
Web_ConstructServerURL(url, VERIFY_RESPONSE_PATH, sizeof(url));
client->login_request_time = curtime;
Web_SubmitRequestFormServer(url, first_form_ptr, last_form_ptr, Auth_ProcessLoginAttempt, NULL, client);
}
static qbool CheckFileExists(const char* path)
{
FILE* f;
if (!(f = fopen(path, "rb"))) {
return false;
}
fclose(f);
return true;
}
void Web_PostResponse(web_request_data_t* req, qbool valid)
{
if (valid) {
const char* broadcast = NULL;
const char* upload = NULL;
const char* uploadPath = NULL;
response_field_t fields[] = {
{ "Broadcast", &broadcast },
{ "UploadPath", &uploadPath },
{ "Upload", &upload }
};
req->response[req->response_length] = '\0';
Con_DPrintf("Response from web server:\n");
Con_DPrintf(req->response);
ProcessWebResponse(req, fields, sizeof(fields) / sizeof(fields[0]));
if (broadcast) {
// Server is making announcement
SV_BroadcastPrintfEx(PRINT_HIGH, 0, "%s\n", broadcast);
}
if (upload && uploadPath) {
if (strstr(uploadPath, "//") || FS_UnsafeFilename(upload)) {
Con_Printf("Upload request deemed unsafe, ignoring...\n");
}
else {
if (!strncmp(upload, "demos/", 6)) {
// Ok - could be demo_dir, or full path
char demoName[MAX_OSPATH];
if (sv_demoDir.string[0]) {
char url[512];
struct curl_httppost *first_form_ptr = NULL;
struct curl_httppost *last_form_ptr = NULL;
snprintf(demoName, sizeof(demoName), "%s/%s/%s", fs_gamedir, sv_demoDir.string, upload + 6);
if (!CheckFileExists(demoName) && sv_demoDirAlt.string[0]) {
snprintf(demoName, sizeof(demoName), "%s/%s/%s", fs_gamedir, sv_demoDirAlt.string, upload + 6);
}
if (CheckFileExists(demoName)) {
Web_ConstructServerURL(url, uploadPath, sizeof(url));
curl_formadd(&first_form_ptr, &last_form_ptr,
CURLFORM_PTRNAME, "file",
CURLFORM_FILE, demoName,
CURLFORM_END
);
curl_formadd(&first_form_ptr, &last_form_ptr,
CURLFORM_COPYNAME, "localPath",
CURLFORM_COPYCONTENTS, upload,
CURLFORM_END
);
Web_SubmitRequestFormServer(url, first_form_ptr, last_form_ptr, Web_PostResponse, "upload", NULL);
Con_Printf("Uploading %s...\n", demoName);
}
else {
Con_Printf("Couldn't find file %s, ignoring...\n", demoName);
}
}
}
else {
Con_Printf("Upload request folder not authorised, ignoring...\n");
}
}
}
}
else {
Con_Printf("Failure contacting central server.\n");
}
if (req->internal_data) {
Q_free(req->internal_data);
}
}
static void Web_SubmitRequestFormServer(const char* url, struct curl_httppost *first_form_ptr, struct curl_httppost *last_form_ptr, web_response_func_t callback, const char* requestId, void* internal_data)
{
curl_formadd(&first_form_ptr, &last_form_ptr,
CURLFORM_PTRNAME, "authKey",
CURLFORM_COPYCONTENTS, sv_www_authkey.string,
CURLFORM_END
);
Web_SubmitRequestFormGeneric(url, first_form_ptr, last_form_ptr, callback, requestId, internal_data);
}
static void Web_ConstructServerURL(char* url, const char* path, int sizeof_url)
{
Web_ConstructGenericURL(sv_www_address.string, url, path, sizeof_url);
}
static void Web_SendRequest(qbool post)
{
struct curl_httppost* first_form_ptr = NULL;
struct curl_httppost* last_form_ptr = NULL;
char url[512];
char* requestId = NULL;
int i;
if (!sv_www_address.string[0]) {
Con_Printf("Address not set - functionality disabled\n");
return;
}
if (Cmd_Argc() < 3) {
Con_Printf("Usage: %s <url> <request-id> (<key> <value>)*\n", Cmd_Argv(0));
return;
}
Web_ConstructServerURL(url, Cmd_Argv(1), sizeof(url));
requestId = Cmd_Argv(2);
for (i = 3; i < Cmd_Argc() - 1; i += 2) {
char encoded_value[128];
int encoded_length = 0;
int j;
char* name;
char* value;
CURLFORMcode code;
name = Cmd_Argv(i);
value = Cmd_Argv(i + 1);
for (j = 0; j < strlen(value) && encoded_length < sizeof(encoded_value) - 2; ++j) {
encoded_length += utf8Encode(value[j], &encoded_value[encoded_length], &encoded_value[encoded_length + 1]);
}
encoded_value[encoded_length] = '\0';
code = curl_formadd(&first_form_ptr, &last_form_ptr,
CURLFORM_COPYNAME, name,
CURLFORM_COPYCONTENTS, encoded_value,
CURLFORM_END
);
if (code != CURL_FORMADD_OK) {
curl_formfree(first_form_ptr);
Con_Printf("Request failed\n");
return;
}
}
Web_SubmitRequestFormServer(url, first_form_ptr, last_form_ptr, Web_PostResponse, requestId, NULL);
}
static void Web_GetRequest_f(void)
{
Web_SendRequest(false);
}
static void Web_PostRequest_f(void)
{
Web_SendRequest(true);
}
static void Web_PostFileRequest_f(void)
{
struct curl_httppost* first_form_ptr = NULL;
struct curl_httppost* last_form_ptr = NULL;
char* requestId = NULL;
char url[512];
char path[MAX_OSPATH];
CURLFORMcode code = CURL_FORMADD_OK;
const char* specified = Cmd_Argv(3);
if (!sv_www_address.string[0]) {
Con_Printf("Address not set - functionality disabled\n");
return;
}
if (specified[0] == '*' && specified[1] == '\0') {
const char* demoname = SV_MVDDemoName();
if (!sv.mvdrecording || !demoname) {
Con_Printf("Not recording demo!\n");
return;
}
snprintf(path, MAX_OSPATH, "%s/%s/%s", fs_gamedir, sv_demoDir.string, SV_MVDName2Txt(demoname));
}
else {
if (strstr(specified, ".cfg") || !strchr(specified, '/')) {
Con_Printf("Filename invalid\n");
return;
}
snprintf(path, MAX_OSPATH, "%s/%s", fs_gamedir, specified);
}
if (Cmd_Argc() < 4) {
Con_Printf("Usage: %s <url> <request-id> <file> (<key> <value>)*\n", Cmd_Argv(0));
return;
}
if (FS_UnsafeFilename(path)) {
Con_Printf("Filename invalid\n");
return;
}
Web_ConstructServerURL(url, Cmd_Argv(1), sizeof(url));
requestId = Cmd_Argv(2);
if (!CheckFileExists(path)) {
Con_Printf("Failed to open file\n");
return;
}
code = curl_formadd(&first_form_ptr, &last_form_ptr,
CURLFORM_PTRNAME, "file",
CURLFORM_FILE, path,
CURLFORM_END
);
if (code != CURL_FORMADD_OK) {
Con_Printf("Failed to generate form (0: %u)\n", code);
return;
}
Web_SubmitRequestFormServer(url, first_form_ptr, last_form_ptr, Web_PostResponse, requestId, NULL);
}
static void Web_PeriodicCheck(qbool server_busy)
{
if (sv_www_address.string[0] && !server_busy && curtime - last_checkin_time > max(MIN_CHECKIN_PERIOD, sv_www_checkin_period.value)) {
char url[512];
Web_ConstructServerURL(url, CHECKIN_PATH, sizeof(url));
Web_SubmitRequestFormServer(url, NULL, NULL, Web_PostResponse, NULL, NULL);
last_checkin_time = curtime;
}
else if (server_busy) {
last_checkin_time = curtime;
}
}
#else
#define Web_PeriodicCheck(...)
#endif // SERVER_ONLY
static void Web_ConstructGenericURL(const char* base_url, char* url, const char* path, int sizeof_url)
{
strlcpy(url, base_url, sizeof_url);
if (url[strlen(url) - 1] != '/') {
strlcat(url, "/", sizeof_url);
}
while (*path == '/') {
++path;
}
strlcat(url, path, sizeof_url);
}
void Central_ProcessResponses(void)
{
CURLMsg* msg;
int running_handles = 0;
int messages_in_queue = 0;
if (!last_checkin_time) {
last_checkin_time = curtime;
return;
}
curl_multi_perform(curl_handle, &running_handles);
while ((msg = curl_multi_info_read(curl_handle, &messages_in_queue))) {
if (msg->msg == CURLMSG_DONE) {
CURL* handle = msg->easy_handle;
CURLcode result = msg->data.result;
web_request_data_t** list_pointer = &web_requests;
curl_multi_remove_handle(curl_handle, handle);
while (*list_pointer) {
web_request_data_t* this = *list_pointer;
if (this->handle == handle) {
// Remove from queue immediately
*list_pointer = this->next;
if (this->onCompleteCallback) {
if (result == CURLE_OK) {
this->onCompleteCallback(this, true);
}
else {
Con_DPrintf("ERROR: %s\n", curl_easy_strerror(result));
this->onCompleteCallback(this, false);
}
}
else {
if (result != CURLE_OK) {
Con_DPrintf("ERROR: %s\n", curl_easy_strerror(result));
}
else {
Con_DPrintf("WEB OK, no callback\n");
}
}
// free memory
curl_formfree(this->first_form_ptr);
Q_free(this->request_id);
Q_free(this);
break;
}
list_pointer = &this->next;
}
curl_easy_cleanup(handle);
}
}
// Periodically check in when idle
Web_PeriodicCheck(running_handles || GameStarted());
}
void Central_Shutdown(void)
{
if (curl_handle) {
curl_multi_cleanup(curl_handle);
curl_handle = NULL;
}
curl_global_cleanup();
}
void Central_Init(void)
{
curl_global_init(CURL_GLOBAL_DEFAULT);
// TODO: client-only version
#ifdef SERVER_ONLY
Cvar_Register(&sv_www_address);
Cvar_Register(&sv_www_authkey);
Cvar_Register(&sv_www_checkin_period);
#endif
#ifndef SERVER_ONLY
Cvar_Register(&cl_www_address);
#endif
curl_handle = curl_multi_init();
#ifdef SERVER_ONLY
if (curl_handle) {
Cmd_AddCommand("sv_web_get", Web_GetRequest_f);
Cmd_AddCommand("sv_web_post", Web_PostRequest_f);
Cmd_AddCommand("sv_web_postfile", Web_PostFileRequest_f);
}
#endif
}
#ifndef SERVER_ONLY
static void Web_ConstructClientURL(char* url, const char* path, int sizeof_url)
{
Web_ConstructGenericURL(cl_www_address.string, url, path, sizeof_url);
}
// Web response to client asking for challenge response text
void Auth_FindChallengeResponse(web_request_data_t* req, qbool valid)
{
const char* challengeText = req->request_id;
const char* result = NULL;
const char* responseText = NULL;
const char* message = NULL;
response_field_t fields[] = {
{ "Result", &result },
{ "Response", &responseText },
{ "Message", &message }
};
if (strcmp(cl.auth_challenge, challengeText)) {
// Ignore result, subsequent request sent
return;
}
req->internal_data = NULL;
ProcessWebResponse(req, fields, sizeof(fields) / sizeof(fields[0]));
if (result && !strcmp(result, "Success") && responseText) {
char buffer[256];
strlcpy(buffer, "cmd login-response ", sizeof(buffer));
strlcat(buffer, responseText, sizeof(buffer));
strlcat(buffer, "\n", sizeof(buffer));
Com_Printf("Responding to challenge...\n");
Cbuf_AddTextEx(&cbuf_main, buffer);
}
else if (message) {
Com_Printf("Error receiving challenge response: %s\n", message);
}
else {
// Maybe add CURLOPT_ERRORBUFFER?
Com_Printf("Error receiving challenge response: unknown error\n");
}
}
// Called when //challenge received from server
void Central_FindChallengeResponse(const char* token, const char* challengeText)
{
char url[512];
struct curl_httppost* first_form_ptr = NULL;
struct curl_httppost* last_form_ptr = NULL;
CURLFORMcode code;
SHA1_CTX context;
unsigned char hash[DIGEST_SIZE];
char hash_buffer[128] = { 0 };
if (!cl_www_address.string[0]) {
Com_Printf("cl_www_address not configured: please authenticate manually\n");
return;
}
// FIXME: something stronger
SHA1Init(&context);
SHA1Update(&context, (unsigned char*)challengeText, strlen(challengeText));
SHA1Update(&context, (unsigned char*)"^", 1);
SHA1Update(&context, (unsigned char*)token, strlen(token));
SHA1Final(hash, &context);
strlcpy(hash_buffer, bin2hex(hash), sizeof(hash_buffer));
code = curl_formadd(&first_form_ptr, &last_form_ptr,
CURLFORM_PTRNAME, "hashalg",
CURLFORM_COPYCONTENTS, "sha1",
CURLFORM_END
);
if (code != CURL_FORMADD_OK) {
curl_formfree(first_form_ptr);
Con_Printf("Failed to generate form (0: %u)\n", code);
return;
}
code = curl_formadd(&first_form_ptr, &last_form_ptr,
CURLFORM_PTRNAME, "authKey",
CURLFORM_COPYCONTENTS, hash_buffer,
CURLFORM_END
);
if (code != CURL_FORMADD_OK) {
curl_formfree(first_form_ptr);
Con_Printf("Failed to generate form (0: %u)\n", code);
return;
}
code = curl_formadd(&first_form_ptr, &last_form_ptr,
CURLFORM_PTRNAME, "challenge",
CURLFORM_COPYCONTENTS, challengeText,
CURLFORM_END
);
if (code != CURL_FORMADD_OK) {
curl_formfree(first_form_ptr);
Con_Printf("Failed to generate form (0: %u)\n", code);
return;
}
Web_ConstructClientURL(url, CHALLENGETEXT_RESPONSE_PATH, sizeof(url));
Web_SubmitRequestFormGeneric(url, first_form_ptr, last_form_ptr, Auth_FindChallengeResponse, challengeText, NULL);
}
#endif