-
Notifications
You must be signed in to change notification settings - Fork 121
/
sec_update.c
903 lines (733 loc) · 21.7 KB
/
sec_update.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
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
/*
===========================================================================
Copyright (C) 2010-2013 Ninja and TheKelm
This file is part of CoD4X18-Server source code.
CoD4X18-Server source code is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
CoD4X18-Server source code 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 Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>
===========================================================================
*/
#include "q_shared.h"
#include "qcommon.h"
#include "sec_update.h"
#include "sec_crypto.h"
#include "sec_common.h"
#include "sec_sign.h"
#include "sys_main.h"
#include "msg.h"
#include "sys_net.h"
#include "netchan.h"
#include "filesystem.h"
#include "cmd.h"
#include "cvar.h"
#include "sys_cod4defs.h"
#include "httpftp.h"
#include "sys_thread.h"
//#include <windows.h>
#include <unistd.h>
#include <errno.h>
static char upd_binpath[1024]; //On Windows it is filepath of executable file. On Linux it is current working directory
static char upd_execpath[1024];
static char upd_homepath[1024];
void Sec_SetupPaths()
{
Q_strncpyz(upd_homepath, fs_homepath->string, sizeof(upd_homepath));
#ifdef _WIN32
Q_strncpyz(upd_binpath, Sys_BinaryPath(), sizeof(upd_binpath));
#else
Q_strncpyz(upd_binpath, Sys_Cwd(), sizeof(upd_binpath));
#endif
Q_strncpyz(upd_execpath, Sys_BinaryPath(), sizeof(upd_execpath));
}
const char* Sec_GetStorePath(const char* path)
{
char filename[1024];
const char* f = strrchr(path, '/');
if(f)
{
path = f +1;
}
Q_strncpyz(filename, path, sizeof(filename));
int len = strlen(filename);
if(len < 4)
{
return upd_homepath; //Error should not happen
}
if(Q_stricmp(filename + (len -4), ".exe") == 0 || Q_stricmp(filename + (len -4), ".dll") == 0)
{
return upd_binpath;
}
if(Q_stricmp(filename + (len -3), ".so") == 0 || strstr(filename, ".so.") != NULL)
{
return upd_binpath;
}
if(strstr(filename, EXECUTABLE_NAME))
{
return upd_execpath;
}
return upd_homepath;
}
const char* Sec_GetStoreFilename(const char* shortname, char *fullfilepath, int maxpathlen)
{
const char* p = Sec_GetStorePath(shortname);
if(strstr(shortname, EXECUTABLE_NAME))
{
Com_sprintf(fullfilepath, maxpathlen, "%s/%s", p, Sys_ExeFileShort());
}else{
Com_sprintf(fullfilepath, maxpathlen, "%s/%s", p, shortname);
}
FS_StripSeperators(fullfilepath);
FS_ReplaceSeparators(fullfilepath);
return fullfilepath;
}
char* FS_UpdateGetFilepath( const char *file, char* testpath, int lenpath )
{
FILE *f;
if(lenpath < MAX_OSPATH)
Com_Error(ERR_FATAL, "FS_SV_GetFilepath: Given buffer has less than %d bytes length\n", MAX_OSPATH );
FS_BuildOSPathForThread(fs_homepath->string, file, "", testpath, 0 );
FS_StripTrailingSeperator( testpath );
f = fopen( testpath, "rb" );
if (f) {
fclose( f );
return testpath;
}
FS_BuildOSPathForThread(fs_basepath->string, file, "", testpath, 0 );
FS_StripTrailingSeperator( testpath );
f = fopen( testpath, "rb" );
if (f) {
fclose( f );
return testpath;
}
/*
Q_strncpyz( file, testpath, 0 );
FS_ReplaceSeparators(testpath);
FS_StripTrailingSeperator( testpath );
f = fopen( testpath, "rb" );
if (f) {
fclose( f );
return testpath;
}
*/
return NULL;
}
#define BUFSIZE 10240
char *Sec_StrTok(char *str,char *tokens,int id){
static char *mem[100] = {NULL};
char *ptr,*ptr2;
if(id<0||id>99||tokens==NULL)
return NULL;
if(str==NULL){
if(mem[id]==NULL){
return NULL;
}
for(ptr=mem[id];*ptr != 0;++ptr){
//printf("---%c\n",*ptr);
for(ptr2=tokens;*ptr2!=0;++ptr2){
//printf("------%c\n",*ptr2);
if(*ptr == *ptr2){
//printf("DEBUG!!!!!!!! %p:\"%s\", %p:\"%s\",%p:\"%s\".\n\n",ptr,ptr,ptr2,ptr2,mem[id],mem[id]);
*ptr=0;
ptr2=mem[id];
mem[id]=ptr+1;
//printf("DEBUG!!!!!!!! %p:\"%s\", %p:\"%s\".\n\n",ptr,ptr,ptr2,ptr2);
//__asm__("int $3");
return ptr2;
}
}
}
if(ptr!=mem[id]){
ptr = mem[id];
}
else
ptr = NULL;
mem[id]=NULL;
return ptr;
}
else{
//printf("Debugging: \"%s\"\n",str);
//mem[id]=str;
for(ptr=str;*ptr != 0 && mem[id]==NULL;++ptr){
for(ptr2=tokens;*ptr2!=0 && mem[id]==NULL;++ptr2){
if(*ptr != *ptr2){
mem[id]=ptr;
}
}
}
if(mem[id] == NULL) return NULL;
return Sec_StrTok(NULL,tokens,id); // BECAUSE I CAN.
}
}
void Sec_FreeFileStruct(sec_file_t *file){
if(file->next != NULL)
Sec_FreeFileStruct(file->next);
Sec_Free(file);
}
qboolean Sec_VerifyFile(byte* data, int length, const char* hash)
{
if(Sec_VerifyMemory(hash, data, length) == qfalse)
{
return qfalse;
}
return qtrue;
/*
unsigned long size;
char outhash[129];
size = sizeof(outhash);
if(!Sec_HashMemory(SEC_HASH_SHA256, data, length, outhash, &size,qfalse))
{
return qfalse;
}
if(Q_strncmp(hash, outhash, size))
{
return qfalse;
}
return qtrue;
*/
}
qboolean Sec_BuildNeededList(sec_file_t *argcurrFile)
{
char name1[1024];
qboolean isvalid;
byte* filebuf;
int len;
sec_file_t *currFile = argcurrFile;
while(currFile != NULL)
{
currFile->needed = qtrue;
Sec_GetStoreFilename(currFile->name, name1, sizeof(name1));
//Does our file already exist?
len = FS_ReadFileOSPath(name1, (void*)&filebuf);
if(len > 0)
{ //Is it already the file we need? Then don't download it.
isvalid = Sec_VerifyFile(filebuf, len, currFile->hash);
FS_FreeFileOSPath(filebuf);
if(isvalid)
{
currFile->needed = qfalse;
}
}
currFile = currFile->next;
}
currFile = argcurrFile;
while(currFile != NULL)
{
if(currFile->needed)
{
return qtrue;
}
currFile = currFile->next;
}
return qfalse;
}
int Sec_DownloadFile(const char* baseurl, sec_file_t *currFile)
{
qboolean isvalid;
char buff[1024];
int transret, len;
ftRequest_t* curfileobj;
char sfnb[1024];
const char* sfn = Sec_GetStoreFilename(currFile->path, sfnb, sizeof(sfnb));
Com_sprintf(buff, sizeof(buff), SEC_UPDATE_DOWNLOAD(baseurl, currFile->path));
curfileobj = FileDownloadRequest(buff);
if(curfileobj == NULL)
{
return -1;
}
Com_Printf(CON_CHANNEL_SYSTEM,"Downloading file: \"%s\"\n\n",currFile->name);
do {
transret = FileDownloadSendReceive( curfileobj );
#ifndef _WIN32
// Com_Printf(CON_CHANNEL_SYSTEM,"%s", FileDownloadGenerateProgress( curfileobj ));
#endif
Sys_SleepUSec(20000);
} while (transret == 0);
Com_Printf(CON_CHANNEL_SYSTEM,"\n");
if(transret < 0)
{
FileDownloadFreeRequest(curfileobj);
return -1;
}
Q_strncpyz(buff, sfn, sizeof(buff));
Q_strncat(buff, sizeof(buff),".new");
if(curfileobj->code != 200){
Com_PrintError(CON_CHANNEL_SYSTEM,"Downloading has failed! Error code: %d. Update aborted.\n", curfileobj->code);
FileDownloadFreeRequest(curfileobj);
return -1;
}
if(curfileobj->contentLength != currFile->size)
{
Com_PrintError(CON_CHANNEL_SYSTEM,"Downloading has failed! Downloaded file has a different size %d expected %d.\n", curfileobj->contentLength, currFile->size);
FileDownloadFreeRequest(curfileobj);
return -1;
}
isvalid = Sec_VerifyFile(curfileobj->recvmsg.data + curfileobj->headerLength, curfileobj->contentLength, currFile->hash);
if(isvalid){
Com_Printf(CON_CHANNEL_SYSTEM,"Successfully downloaded file \"%s\".\n", currFile->name);
}else{
FileDownloadFreeRequest(curfileobj);
Com_PrintError(CON_CHANNEL_SYSTEM,"File \"%s\" is corrupt!\nUpdate aborted.\n",currFile->name);
return -1;
}
Com_Printf(CON_CHANNEL_SYSTEM,"Writing file to %s\n", buff);
len = FS_WriteFileOSPath(buff, curfileobj->recvmsg.data + curfileobj->headerLength, curfileobj->contentLength);
if(len != curfileobj->contentLength)
{
Com_PrintError(CON_CHANNEL_SYSTEM,"Opening \"%s\" for writing! Update aborted.\n",buff);
FileDownloadFreeRequest(curfileobj);
return -1;
}
FileDownloadFreeRequest(curfileobj);
return 1;
}
sec_file_t* Sec_ParseLine(const char* line)
{
char* ptr, *ptr2;
ptr = (char*)line;
sec_file_t *currFile;
currFile = Sec_GMalloc(sec_file_t,1);
if(currFile == NULL)
{
return currFile;
}
Com_Memset(currFile,0,sizeof(sec_file_t));
ptr2 = strchr(ptr,' ');
if(ptr2 == NULL){
Com_PrintWarning(CON_CHANNEL_SYSTEM,"Sec_Update: Corrupt data from update server. Update aborted.\nDebug:\"%s\"\n",ptr);
Sec_FreeFileStruct(currFile);
return NULL;
}
*ptr2++ = 0;
Q_strncpyz(currFile->path,ptr,sizeof(currFile->path));
ptr = ptr2;
ptr2 = strchr(ptr,' ');
if(ptr2 == NULL){
Com_PrintWarning(CON_CHANNEL_SYSTEM,"Sec_Update: Corrupt data from update server. Update aborted.\nDebug:\"%s\"\n",ptr);
Sec_FreeFileStruct(currFile);
return NULL;
}
*ptr2++ = 0;
if(!isInteger(ptr, 0)){
Com_PrintWarning(CON_CHANNEL_SYSTEM,"Sec_Update: Corrupt data from update server - size is not a number. Update aborted.\nDebug:\"%s\"\n",ptr);
Sec_FreeFileStruct(currFile);
return NULL;
}
currFile->size = atoi(ptr);
Q_strncpyz(currFile->hash,ptr2,sizeof(currFile->hash));
Q_strncpyz(currFile->name,currFile->path, sizeof(currFile->name));
return currFile;
}
qboolean Sec_MakeExecutable(sec_file_t *currFile)
{
char buff[1024];
Sec_GetStoreFilename(currFile->name, buff, sizeof(buff));
Q_strncat(buff, sizeof(buff),".new");
if(FS_FileExistsOSPath(buff) == qfalse)
{
Com_PrintError(CON_CHANNEL_SYSTEM,"Can not find file %s\n", buff);
Com_PrintError(CON_CHANNEL_SYSTEM,"Update has failed!\n");
return qfalse;
}
Com_Printf(CON_CHANNEL_SYSTEM,"Set permissions for: %s\n", buff);
if(FS_SetPermissionsExec(buff) == qfalse)
{
Com_PrintError(CON_CHANNEL_SYSTEM,"CRITICAL ERROR: failed to change mode of the file \"%s\"! Aborting, manual installation might be required.\n", buff);
return qfalse;
}
return qtrue;
}
qboolean Sec_RemoveOldInstallfiles(sec_file_t *currFile)
{
char name1[1024];
while(currFile != NULL)
{
if(!currFile->needed)
{
currFile = currFile->next;
continue;
}
Sec_GetStoreFilename(currFile->name, name1, sizeof(name1));
Q_strncat(name1, sizeof(name1), ".new");
if(FS_FileExistsOSPath(name1))
{ // Old backupfile exists, delete it!
Com_Printf(CON_CHANNEL_SYSTEM,"Removing old install file %s...\n", name1);
FS_RemoveOSPath( name1 );
if(FS_FileExistsOSPath(name1))
{
Com_PrintWarning(CON_CHANNEL_SYSTEM,"Couldn't remove install file: %s\n", name1);
return qfalse;
}
}
currFile = currFile->next;
}
return qtrue;
}
qboolean Sec_RemoveOldBackupfiles(sec_file_t *currFile)
{
char name1[1024];
while(currFile != NULL)
{
Sec_GetStoreFilename(currFile->name, name1, sizeof(name1));
Q_strncat(name1, sizeof(name1), ".old");
if(FS_FileExistsOSPath(name1))
{ // Old backupfile exists, delete it!
Com_Printf(CON_CHANNEL_SYSTEM,"Removing backup file %s...\n", name1);
FS_RemoveOSPath( name1 );
if(FS_FileExistsOSPath(name1))
{
Com_PrintWarning(CON_CHANNEL_SYSTEM,"Couldn't remove backup file: %s\n", name1);
return qfalse;
}
}
currFile = currFile->next;
}
return qtrue;
}
qboolean Sec_Backupfiles(sec_file_t *currFile)
{
char name1[1024];
char curfilename[1024];
qboolean fexist;
while(currFile != NULL)
{
if(!currFile->needed)
{
currFile = currFile->next;
continue;
}
Sec_GetStoreFilename(currFile->name, curfilename, sizeof(curfilename));
Q_strncpyz(name1, curfilename, sizeof(name1));
Q_strncat(name1, sizeof(name1), ".old");
Com_Printf(CON_CHANNEL_SYSTEM,"Backing up file %s...\n", curfilename);
FS_RenameOSPath(curfilename, name1);
fexist = FS_FileExistsOSPath(curfilename);
// We couldn't back it up. Raise error
if(fexist)
{
Com_PrintError(CON_CHANNEL_SYSTEM,"Couldn't backup file %s\n", curfilename);
return qfalse;
}
currFile = currFile->next;
}
return qtrue;
}
void Sec_UndoBackup(sec_file_t *currFile)
{
char name1[1024];
char curfilename[1024];
while(currFile != NULL)
{
if(!currFile->needed)
{
currFile = currFile->next;
continue;
}
Com_Printf(CON_CHANNEL_SYSTEM,"Undo backup file %s...\n", currFile->name);
Sec_GetStoreFilename(currFile->name, curfilename, sizeof(curfilename));
Q_strncpyz(name1, curfilename, sizeof(name1));
Q_strncat(name1, sizeof(name1), ".old");
// Check if an backupfile exists with this name
if(FS_FileExistsOSPath(name1) == qfalse)
{
Com_Printf(CON_CHANNEL_SYSTEM,"Backupfile %s not found!\n", name1);
}else{
//Backupfile exists, rename it back
FS_RemoveOSPath(curfilename);
Com_Printf(CON_CHANNEL_SYSTEM,"Rename from %s to %s\n", name1, curfilename);
FS_RenameOSPath(name1, curfilename);
}
currFile = currFile->next;
}
}
qboolean Sec_InstallNewFiles(sec_file_t *currFile)
{
char name1[1024];
char curfilename[1024];
while(currFile != NULL)
{
if(!currFile->needed)
{
currFile = currFile->next;
continue;
}
Com_Printf(CON_CHANNEL_SYSTEM,"Install file %s...\n", currFile->name);
Sec_GetStoreFilename(currFile->name, curfilename, sizeof(curfilename));
Q_strncpyz(name1, curfilename, sizeof(name1));
Q_strncat(name1, sizeof(name1), ".new");
FS_RenameOSPath(name1, curfilename);
if(!FS_FileExistsOSPath(curfilename))
{
Com_PrintError(CON_CHANNEL_SYSTEM,"Failed to rename file %s to %s\n", name1, curfilename);
Com_PrintError(CON_CHANNEL_SYSTEM,"Update has failed!\n");
return qfalse;
}
currFile = currFile->next;
}
return qtrue;
}
FILE* Sec_AutoaupdateLock()
{
char lockfilename[1024];
FILE* h;
time_t start, now;
FS_BuildOSPathForThread(Sys_BinaryPath(), "autoupdate.lock", "", lockfilename, 0);
FS_StripTrailingSeperator(lockfilename);
time(&start);
h = fopen(lockfilename, "wb");
if(h != NULL){
return h;
}
while(h == NULL)
{
time(&now);
if(now - start > 240)
{//Timeout
return NULL;
}
h = fopen(lockfilename, "wb");
Com_Printf(CON_CHANNEL_SYSTEM,"Autoupdater: waiting for %s to unlock for %d more seconds\n", lockfilename, 240 - (now - start));
Sys_SleepSec(15);
}
fclose( h );
Sys_SleepSec(15);
Sys_Restart("System saw an update lock. Will restart now.");
return NULL;
}
void Sec_AutoupdateUnlock(FILE* h)
{
if(h != NULL)
{
fclose( h );
}
}
cvar_t* sv_updateservers;
void Sec_Update( qboolean getbasefiles ){
char buff[SEC_UPDATE_INITIALBUFFSIZE];
char *ptr;
char baseurl[1024];
char version[1024];
sec_file_t files, *currFile = &files;
int len;
ftRequest_t* filetransferobj;
int transret;
FILE* lockfile;
struct version_t
{
int minor;
int major;
};
struct version_t newversion;
struct version_t currentversion;
if(!Sec_Initialized()){
return;
}
Com_Printf(CON_CHANNEL_SYSTEM,"\n-----------------------------\n");
Com_Printf(CON_CHANNEL_SYSTEM," CoD4X Auto Update\n");
Com_Printf(CON_CHANNEL_SYSTEM," Current version: %g\n", SEC_VERSION);
Com_Printf(CON_CHANNEL_SYSTEM," Current subversion: %g\n", Sys_GetCommonVersion());
Com_Printf(CON_CHANNEL_SYSTEM," Current build: %d\n", Sys_GetBuild());
Com_Printf(CON_CHANNEL_SYSTEM,"-----------------------------\n\n");
Sec_SetupPaths();
sv_updateservers = Cvar_RegisterString("sv_updateservers", "NULL", CVAR_INIT, "Names of the update servers");
Com_sprintf(buff, sizeof(buff), "%s?mode=11&os=" OS_STRING "&ver=%g", sv_updateservers->string, Sys_GetCommonVersion());
filetransferobj = FileDownloadRequest( buff );
if(filetransferobj == NULL){
return;
}
do {
transret = FileDownloadSendReceive( filetransferobj );
Sys_SleepUSec(20000);
} while (transret == 0);
if(transret < 0)
{
FileDownloadFreeRequest(filetransferobj);
return;
}
/* Need to catch errors */
// FS_WriteFile("tmp.txt", va("%d", status), 1);
// TODO: Do something with the status?
// FS_WriteFile("tmp2.txt", packet.header, packet.headerLength);
// FS_WriteFile("tmp3.txt", packet.content, packet.contentLength);
if(filetransferobj->code <= 0){
Com_PrintError(CON_CHANNEL_SYSTEM,"Receiving data. Error code: %d.\n", filetransferobj->code);
FileDownloadFreeRequest(filetransferobj);
return;
}
if(filetransferobj->code == 204){
Com_Printf(CON_CHANNEL_SYSTEM,"\nServer is up to date.\n\n"); //Should not happen anymore. Instead we want to verify all files
FileDownloadFreeRequest(filetransferobj);
return;
}
else if(filetransferobj->code != 200){
Com_PrintWarning(CON_CHANNEL_SYSTEM,"The update server's malfunction.\nStatus code: %d.\n", filetransferobj->code);
FileDownloadFreeRequest(filetransferobj);
return;
}
Com_Memset(&files, 0, sizeof(files));
if(filetransferobj->contentLength >= sizeof(buff))
{
len = sizeof(buff) -1;
}else{
len = filetransferobj->contentLength;
}
memcpy(buff, filetransferobj->recvmsg.data + filetransferobj->headerLength, len);
buff[len] = '\0';
lockfile = Sec_AutoaupdateLock();
if(lockfile == NULL)
{
Com_PrintWarning(CON_CHANNEL_SYSTEM,"Sec_Update: Couldn't acquire a lock for autoupdate. Autoupdate will abort here\n");
FileDownloadFreeRequest(filetransferobj);
return;
}
/* We need to parse filenames etc */
ptr = Sec_StrTok(buff,"\n",42); // Yes, 42.
if(ptr == NULL || Q_stricmpn("version: ", ptr, 9))
{
Com_PrintWarning(CON_CHANNEL_SYSTEM,"Sec_Update: Corrupt data from update server. Update aborted.\n");
FileDownloadFreeRequest(filetransferobj);
Sec_AutoupdateUnlock(lockfile);
return;
}
Q_strncpyz(version, ptr +9, sizeof(version));
int l1 = sscanf(Sys_GetCommonVersionString(), "%d.%d", ¤tversion.major, ¤tversion.minor);
int l2 = sscanf(version, "%d.%d", &newversion.major, &newversion.minor);
if(l1 != 2 || l2 != 2)
{
Com_PrintWarning(CON_CHANNEL_SYSTEM,"Sec_Update: Corrupt version strings. Update aborted.\n");
FileDownloadFreeRequest(filetransferobj);
Sec_AutoupdateUnlock(lockfile);
return;
}
Com_Printf(CON_CHANNEL_SYSTEM,"New subversion %d.%d\n", newversion.major, newversion.minor);
ptr = Sec_StrTok(buff,"\n",42); // Yes, 42.
if(ptr == NULL || Q_stricmpn("baseurl: ", ptr, 9))
{
Com_PrintWarning(CON_CHANNEL_SYSTEM,"Sec_Update: Corrupt data from update server. Update aborted.\n");
FileDownloadFreeRequest(filetransferobj);
Sec_AutoupdateUnlock(lockfile);
return;
}
Q_strncpyz(baseurl, ptr +9, sizeof(baseurl));
//Parsing update filelist
while((ptr = Sec_StrTok(NULL,"\n",42)) != NULL)
{
currFile->next = Sec_ParseLine(ptr);
currFile = currFile->next;
if(currFile == NULL)
{
Sec_FreeFileStruct(files.next);
FileDownloadFreeRequest(filetransferobj);
Sec_AutoupdateUnlock(lockfile);
Com_PrintError(CON_CHANNEL_SYSTEM,"Update has failed with: Parser error\n");
return;
}
}
FileDownloadFreeRequest(filetransferobj);
#ifndef OFFICIAL
//IF this is a custom build we want to bail out early if this is not really needed
if(currentversion.major >= newversion.major)
{
Com_Printf(CON_CHANNEL_SYSTEM,"Update rejected because it is not required.\n");
Sec_FreeFileStruct(files.next);
Sec_AutoupdateUnlock(lockfile);
return;
}
#endif
//IF this is a custom build we want to bail out early if this is not really needed
if(currentversion.major > newversion.major || (currentversion.minor > newversion.minor && currentversion.major == newversion.major))
{
Com_Printf(CON_CHANNEL_SYSTEM,"Update rejected because updateserver reports older version.\n");
Sec_FreeFileStruct(files.next);
Sec_AutoupdateUnlock(lockfile);
return;
}
if(Sec_RemoveOldInstallfiles(files.next) == qfalse)
{
Com_PrintError(CON_CHANNEL_SYSTEM,"Update has failed\n");
Sec_FreeFileStruct(files.next);
Sec_AutoupdateUnlock(lockfile);
return;
}
if(!Sec_BuildNeededList(files.next))
{
Com_Printf(CON_CHANNEL_SYSTEM,"Update not needed. All files are equal.\n");
Sec_FreeFileStruct(files.next);
Sec_AutoupdateUnlock(lockfile);
return;
}
sec_file_t *f = files.next;
while(f != NULL)
{
if(!f->needed)
{
f = f->next;
continue;
}
switch(Sec_DownloadFile(baseurl, f))
{
case 0:
f = f->next;
continue;
case 1:
break;
case -1:
default:
FileDownloadFreeRequest(filetransferobj);
Sec_FreeFileStruct(files.next);
Sec_AutoupdateUnlock(lockfile);
Com_PrintError(CON_CHANNEL_SYSTEM,"Update has failed\n");
return;
}
if(strstr(f->name, "run"))
{
if(Sec_MakeExecutable(f) == qfalse)
{
Sec_FreeFileStruct(files.next);
Sec_AutoupdateUnlock(lockfile);
Com_PrintError(CON_CHANNEL_SYSTEM,"Update has failed\n");
return;
}
}
f = f->next;
}
//Everything is downloaded and parsed
Com_Printf(CON_CHANNEL_SYSTEM,"All files downloaded successfully. Applying update...\n");
//Installing the update files...
if(Sec_RemoveOldBackupfiles(files.next) == qfalse)
{
Com_PrintError(CON_CHANNEL_SYSTEM,"Update has failed\n");
Sec_FreeFileStruct(files.next);
Sec_AutoupdateUnlock(lockfile);
return;
}
if(Sec_Backupfiles(files.next) == qfalse)
{
Com_PrintError(CON_CHANNEL_SYSTEM,"Update has failed. Trying to recover...\n");
Sec_UndoBackup(files.next);
Sec_FreeFileStruct(files.next);
Sec_AutoupdateUnlock(lockfile);
return;
}
if(Sec_InstallNewFiles(files.next) == qfalse)
{
Com_PrintError(CON_CHANNEL_SYSTEM,"Update has failed. Trying to recover...\n");
Sec_UndoBackup(files.next);
// MessageBoxA(NULL, "Couldn't install update", "Couldn't install update", MB_OK);
Sec_AutoupdateUnlock(lockfile);
Com_Quit_f();
return;
}
//Try just in case we got a copy
FS_SetPermissionsExec(Sys_ExeFile());
Sec_FreeFileStruct(files.next);
Sec_AutoupdateUnlock(lockfile);
Com_Printf(CON_CHANNEL_SYSTEM,"Finalizing update...\n");
Sys_Restart("System has been updated and will restart now.");
}