summaryrefslogtreecommitdiffstats
path: root/src/VBox/Main/src-server/UpdateAgentImpl.cpp
blob: 3a89815a40a0ce60ec6a33fe73aeb6514bf711c4 (plain)
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
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
/* $Id: UpdateAgentImpl.cpp $ */
/** @file
 * IUpdateAgent COM class implementations.
 */

/*
 * Copyright (C) 2020-2023 Oracle and/or its affiliates.
 *
 * This file is part of VirtualBox base platform packages, as
 * available from https://www.virtualbox.org.
 *
 * 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, in version 3 of the
 * License.
 *
 * 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, see <https://www.gnu.org/licenses>.
 *
 * SPDX-License-Identifier: GPL-3.0-only
 */


#define LOG_GROUP LOG_GROUP_MAIN_UPDATEAGENT

#include <iprt/cpp/utils.h>
#include <iprt/param.h>
#include <iprt/path.h>
#include <iprt/http.h>
#include <iprt/system.h>
#include <iprt/message.h>
#include <iprt/pipe.h>
#include <iprt/env.h>
#include <iprt/process.h>
#include <iprt/assert.h>
#include <iprt/err.h>
#include <iprt/stream.h>
#include <iprt/time.h>
#include <VBox/com/defs.h>
#include <VBox/err.h>
#include <VBox/version.h>

#include "HostImpl.h"
#include "UpdateAgentImpl.h"
#include "ProgressImpl.h"
#include "AutoCaller.h"
#include "LoggingNew.h"
#include "VirtualBoxImpl.h"
#include "VBoxEvents.h"
#include "SystemPropertiesImpl.h"
#include "ThreadTask.h"
#include "VirtualBoxImpl.h"
#include "VirtualBoxBase.h"


/*********************************************************************************************************************************
*   Update agent task implementation                                                                                             *
*********************************************************************************************************************************/

/**
 * Base task class for asynchronous update agent tasks.
 */
class UpdateAgentTask : public ThreadTask
{
public:
    UpdateAgentTask(UpdateAgentBase *aThat, Progress *aProgress)
        : m_pParent(aThat)
        , m_pProgress(aProgress)
    {
        m_strTaskName = "UpdateAgentTask";
    }
    virtual ~UpdateAgentTask(void) { }

private:
    void handler(void);

    /** Weak pointer to parent (update agent). */
    UpdateAgentBase     *m_pParent;
    /** Smart pointer to the progress object for this job. */
    ComObjPtr<Progress>  m_pProgress;

    friend class UpdateAgent;  // allow member functions access to private data
};

void UpdateAgentTask::handler(void)
{
    UpdateAgentBase *pUpdateAgent = this->m_pParent;
    AssertPtr(pUpdateAgent);

    /** @todo Differentiate tasks once we have more stuff to do (downloading, installing, ++). */

    HRESULT hrc = pUpdateAgent->i_checkForUpdateTask(this);

    if (!m_pProgress.isNull())
        m_pProgress->i_notifyComplete(hrc);

    LogFlowFunc(("hrc=%Rhrc\n", hrc)); RT_NOREF(hrc);
}


/*********************************************************************************************************************************
*   Update agent base class implementation                                                                                       *
*********************************************************************************************************************************/

/**
 * Returns platform information as a string.
 *
 * @returns Platform information as string.
 */
/* static */
Utf8Str UpdateAgentBase::i_getPlatformInfo(void)
{
    /* Prepare platform report: */
    Utf8Str strPlatform;

# if defined (RT_OS_WINDOWS)
    strPlatform = "win";
# elif defined (RT_OS_LINUX)
    strPlatform = "linux";
# elif defined (RT_OS_DARWIN)
    strPlatform = "macosx";
# elif defined (RT_OS_OS2)
    strPlatform = "os2";
# elif defined (RT_OS_FREEBSD)
    strPlatform = "freebsd";
# elif defined (RT_OS_SOLARIS)
    strPlatform = "solaris";
# else
    strPlatform = "unknown";
# endif

    /* The format is <system>.<bitness>: */
    strPlatform.appendPrintf(".%lu", ARCH_BITS);

    /* Add more system information: */
    int vrc;
# ifdef RT_OS_LINUX
    // WORKAROUND:
    // On Linux we try to generate information using script first of all..

    /* Get script path: */
    char szAppPrivPath[RTPATH_MAX];
    vrc = RTPathAppPrivateNoArch(szAppPrivPath, sizeof(szAppPrivPath));
    AssertRC(vrc);
    if (RT_SUCCESS(vrc))
        vrc = RTPathAppend(szAppPrivPath, sizeof(szAppPrivPath), "/VBoxSysInfo.sh");
    AssertRC(vrc);
    if (RT_SUCCESS(vrc))
    {
        RTPIPE hPipeR;
        RTHANDLE hStdOutPipe;
        hStdOutPipe.enmType = RTHANDLETYPE_PIPE;
        vrc = RTPipeCreate(&hPipeR, &hStdOutPipe.u.hPipe, RTPIPE_C_INHERIT_WRITE);
        AssertLogRelRC(vrc);

        char const *szAppPrivArgs[2];
        szAppPrivArgs[0] = szAppPrivPath;
        szAppPrivArgs[1] = NULL;
        RTPROCESS hProc = NIL_RTPROCESS;

        /* Run script: */
        vrc = RTProcCreateEx(szAppPrivPath, szAppPrivArgs, RTENV_DEFAULT, 0 /*fFlags*/, NULL /*phStdin*/, &hStdOutPipe,
                             NULL /*phStderr*/, NULL /*pszAsUser*/, NULL /*pszPassword*/, NULL /*pvExtraData*/, &hProc);

        (void) RTPipeClose(hStdOutPipe.u.hPipe);
        hStdOutPipe.u.hPipe = NIL_RTPIPE;

        if (RT_SUCCESS(vrc))
        {
            RTPROCSTATUS  ProcStatus;
            size_t        cbStdOutBuf  = 0;
            size_t        offStdOutBuf = 0;
            char          *pszStdOutBuf = NULL;
            do
            {
                if (hPipeR != NIL_RTPIPE)
                {
                    char    achBuf[1024];
                    size_t  cbRead;
                    vrc = RTPipeReadBlocking(hPipeR, achBuf, sizeof(achBuf), &cbRead);
                    if (RT_SUCCESS(vrc))
                    {
                        /* grow the buffer? */
                        size_t cbBufReq = offStdOutBuf + cbRead + 1;
                        if (   cbBufReq > cbStdOutBuf
                            && cbBufReq < _256K)
                        {
                            size_t cbNew = RT_ALIGN_Z(cbBufReq, 16); // 1024
                            void  *pvNew = RTMemRealloc(pszStdOutBuf, cbNew);
                            if (pvNew)
                            {
                                pszStdOutBuf = (char *)pvNew;
                                cbStdOutBuf  = cbNew;
                            }
                        }

                        /* append if we've got room. */
                        if (cbBufReq <= cbStdOutBuf)
                        {
                            (void) memcpy(&pszStdOutBuf[offStdOutBuf], achBuf, cbRead);
                            offStdOutBuf = offStdOutBuf + cbRead;
                            pszStdOutBuf[offStdOutBuf] = '\0';
                        }
                    }
                    else
                    {
                        AssertLogRelMsg(vrc == VERR_BROKEN_PIPE, ("%Rrc\n", vrc));
                        RTPipeClose(hPipeR);
                        hPipeR = NIL_RTPIPE;
                    }
                }

                /*
                 * Service the process.  Block if we have no pipe.
                 */
                if (hProc != NIL_RTPROCESS)
                {
                    vrc = RTProcWait(hProc,
                                     hPipeR == NIL_RTPIPE ? RTPROCWAIT_FLAGS_BLOCK : RTPROCWAIT_FLAGS_NOBLOCK,
                                     &ProcStatus);
                    if (RT_SUCCESS(vrc))
                        hProc = NIL_RTPROCESS;
                    else
                        AssertLogRelMsgStmt(vrc == VERR_PROCESS_RUNNING, ("%Rrc\n", vrc), hProc = NIL_RTPROCESS);
                }
            } while (   hPipeR != NIL_RTPIPE
                     || hProc != NIL_RTPROCESS);

            if (   ProcStatus.enmReason == RTPROCEXITREASON_NORMAL
                && ProcStatus.iStatus == 0) {
                pszStdOutBuf[offStdOutBuf-1] = '\0';  // remove trailing newline
                Utf8Str pszStdOutBufUTF8(pszStdOutBuf);
                strPlatform.appendPrintf(" [%s]", pszStdOutBufUTF8.strip().c_str());
                // For testing, here is some sample output:
                //strPlatform.appendPrintf(" [Distribution: Redhat | Version: 7.6.1810 | Kernel: Linux version 3.10.0-952.27.2.el7.x86_64 (gcc version 4.8.5 20150623 (Red Hat 4.8.5-36) (GCC) ) #1 SMP Mon Jul 29 17:46:05 UTC 2019]");
            }
        }
        else
            vrc = VERR_TRY_AGAIN; /* (take the fallback path) */
    }

    if (RT_FAILURE(vrc))
# endif /* RT_OS_LINUX */
    {
        /* Use RTSystemQueryOSInfo: */
        char szTmp[256];

        vrc = RTSystemQueryOSInfo(RTSYSOSINFO_PRODUCT, szTmp, sizeof(szTmp));
        if ((RT_SUCCESS(vrc) || vrc == VERR_BUFFER_OVERFLOW) && szTmp[0] != '\0')
            strPlatform.appendPrintf(" [Product: %s", szTmp);

        vrc = RTSystemQueryOSInfo(RTSYSOSINFO_RELEASE, szTmp, sizeof(szTmp));
        if ((RT_SUCCESS(vrc) || vrc == VERR_BUFFER_OVERFLOW) && szTmp[0] != '\0')
            strPlatform.appendPrintf(" %sRelease: %s", strlen(szTmp) == 0 ? "[" : "| ", szTmp);

        vrc = RTSystemQueryOSInfo(RTSYSOSINFO_VERSION, szTmp, sizeof(szTmp));
        if ((RT_SUCCESS(vrc) || vrc == VERR_BUFFER_OVERFLOW) && szTmp[0] != '\0')
            strPlatform.appendPrintf(" %sVersion: %s", strlen(szTmp) == 0 ? "[" : "| ", szTmp);

        vrc = RTSystemQueryOSInfo(RTSYSOSINFO_SERVICE_PACK, szTmp, sizeof(szTmp));
        if ((RT_SUCCESS(vrc) || vrc == VERR_BUFFER_OVERFLOW) && szTmp[0] != '\0')
            strPlatform.appendPrintf(" %sSP: %s]", strlen(szTmp) == 0 ? "[" : "| ", szTmp);

        if (!strPlatform.endsWith("]"))
            strPlatform.append("]");
    }

    LogRel2(("UpdateAgent: Platform is '%s'\n", strPlatform.c_str()));

    return strPlatform;
}

/**
 * Returns the proxy mode as a string.
 *
 * @returns Proxy mode as string.
 * @param   enmMode             Proxy mode to return as string.
 */
/* static */
const char *UpdateAgentBase::i_proxyModeToStr(ProxyMode_T enmMode)
{
    switch (enmMode)
    {
        case ProxyMode_System:  return "System";
        case ProxyMode_Manual:  return "Manual";
        case ProxyMode_NoProxy: return "None";
        default:                break;
    }

    AssertFailed();
    return "<Invalid>";
}

/**
 * Returns whether a given URL's scheme is supported or not.
 *
 * @returns \c true if scheme is supported, or \c false if not.
 * @param   strUrl              URL to check scheme for.
 *
 * @note Empty URL are considered as being supported for convenience.
 */
bool UpdateAgentBase::i_urlSchemeIsSupported(const Utf8Str &strUrl) const
{
    if (strUrl.isEmpty())
        return true;
    return strUrl.startsWith("https://", com::Utf8Str::CaseInsensitive);
}


/*********************************************************************************************************************************
*   Update agent class implementation                                                                                            *
*********************************************************************************************************************************/
UpdateAgent::UpdateAgent()
{
}

UpdateAgent::~UpdateAgent()
{
}

HRESULT UpdateAgent::FinalConstruct(void)
{
    return BaseFinalConstruct();
}

void UpdateAgent::FinalRelease(void)
{
    uninit();

    BaseFinalRelease();
}

HRESULT UpdateAgent::init(VirtualBox *aVirtualBox)
{
    /* Weak reference to a VirtualBox object */
    unconst(m_VirtualBox) = aVirtualBox;

    HRESULT hrc = unconst(m_EventSource).createObject();
    if (SUCCEEDED(hrc))
        hrc = m_EventSource->init();

    return hrc;
}

void UpdateAgent::uninit(void)
{
    // Enclose the state transition Ready->InUninit->NotReady.
    AutoUninitSpan autoUninitSpan(this);
    if (autoUninitSpan.uninitDone())
        return;

    unconst(m_EventSource).setNull();
}

HRESULT UpdateAgent::checkFor(ComPtr<IProgress> &aProgress)
{
    RT_NOREF(aProgress);

    return VBOX_E_NOT_SUPPORTED;
}

HRESULT UpdateAgent::download(ComPtr<IProgress> &aProgress)
{
    RT_NOREF(aProgress);

    return VBOX_E_NOT_SUPPORTED;
}

HRESULT UpdateAgent::install(ComPtr<IProgress> &aProgress)
{
    RT_NOREF(aProgress);

    return VBOX_E_NOT_SUPPORTED;
}

HRESULT UpdateAgent::rollback(void)
{
    return VBOX_E_NOT_SUPPORTED;
}

HRESULT UpdateAgent::getName(com::Utf8Str &aName)
{
    AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);

    aName = mData.m_strName;

    return S_OK;
}

HRESULT UpdateAgent::getEventSource(ComPtr<IEventSource> &aEventSource)
{
    LogFlowThisFuncEnter();

    /* No need to lock - lifetime constant. */
    m_EventSource.queryInterfaceTo(aEventSource.asOutParam());

    LogFlowFuncLeaveRC(S_OK);
    return S_OK;
}

HRESULT UpdateAgent::getOrder(ULONG *aOrder)
{
    AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);

    *aOrder = 0; /* 0 means no order / disabled. */

    return S_OK;
}

HRESULT UpdateAgent::getDependsOn(std::vector<com::Utf8Str> &aDeps)
{
    AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);

    aDeps.resize(0); /* No dependencies by default. */

    return S_OK;
}

HRESULT UpdateAgent::getVersion(com::Utf8Str &aVer)
{
    AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);

    aVer = mData.m_lastResult.strVer;

    return S_OK;
}

HRESULT UpdateAgent::getDownloadUrl(com::Utf8Str &aUrl)
{
    AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);

    aUrl = mData.m_lastResult.strDownloadUrl;

    return S_OK;
}


HRESULT UpdateAgent::getWebUrl(com::Utf8Str &aUrl)
{
    AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);

    aUrl = mData.m_lastResult.strWebUrl;

    return S_OK;
}

HRESULT UpdateAgent::getReleaseNotes(com::Utf8Str &aRelNotes)
{
    AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);

    aRelNotes = mData.m_lastResult.strReleaseNotes;

    return S_OK;
}

HRESULT UpdateAgent::getEnabled(BOOL *aEnabled)
{
    AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);

    *aEnabled = m->fEnabled;

    return S_OK;
}

HRESULT UpdateAgent::setEnabled(const BOOL aEnabled)
{
    AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);

    m->fEnabled = aEnabled;

    return i_commitSettings(alock);
}


HRESULT UpdateAgent::getHidden(BOOL *aHidden)
{
    AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);

    *aHidden = mData.m_fHidden;

    return S_OK;
}

HRESULT UpdateAgent::getState(UpdateState_T *aState)
{
    AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);

    *aState = mData.m_enmState;

    return S_OK;
}

HRESULT UpdateAgent::getCheckFrequency(ULONG *aFreqSeconds)
{
    AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);

    *aFreqSeconds = m->uCheckFreqSeconds;

    return S_OK;
}

HRESULT UpdateAgent::setCheckFrequency(ULONG aFreqSeconds)
{
    if (aFreqSeconds < RT_SEC_1DAY) /* Don't allow more frequent checks for now. */
        return setError(E_INVALIDARG, tr("Frequency too small; one day is the minimum"));

    AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);

    m->uCheckFreqSeconds = aFreqSeconds;

    return i_commitSettings(alock);
}

HRESULT UpdateAgent::getChannel(UpdateChannel_T *aChannel)
{
    AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);

    *aChannel = m->enmChannel;

    return S_OK;
}

HRESULT UpdateAgent::setChannel(UpdateChannel_T aChannel)
{
    AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);

    m->enmChannel = aChannel;

    return i_commitSettings(alock);
}

HRESULT UpdateAgent::getCheckCount(ULONG *aCount)
{
    AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);

    *aCount = m->uCheckCount;

    return S_OK;
}

HRESULT UpdateAgent::getRepositoryURL(com::Utf8Str &aRepo)
{
    AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);

    aRepo = m->strRepoUrl;

    return S_OK;
}

HRESULT UpdateAgent::setRepositoryURL(const com::Utf8Str &aRepo)
{
    if (!i_urlSchemeIsSupported(aRepo))
        return setError(E_INVALIDARG, tr("Invalid URL scheme specified!"));

    AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);

    m->strRepoUrl = aRepo;

    return i_commitSettings(alock);
}

HRESULT UpdateAgent::getLastCheckDate(com::Utf8Str &aDate)
{
    AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);

    aDate = m->strLastCheckDate;

    return S_OK;
}

HRESULT UpdateAgent::getIsCheckNeeded(BOOL *aCheckNeeded)
{
    AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);

    /*
     * Is update checking enabled at all?
     */
    if (!m->fEnabled)
    {
        *aCheckNeeded = FALSE;
        return S_OK;
    }

    /*
     * When was the last update?
     */
    if (m->strLastCheckDate.isEmpty()) /* No prior update check performed -- do so now. */
    {
        *aCheckNeeded = TRUE;
        return S_OK;
    }

    RTTIMESPEC LastCheckTime;
    if (!RTTimeSpecFromString(&LastCheckTime, Utf8Str(m->strLastCheckDate).c_str()))
    {
        *aCheckNeeded = TRUE; /* Invalid date set or error? Perform check. */
        return S_OK;
    }

    /*
     * Compare last update with how often we are supposed to check for updates.
     */
    if (   !m->uCheckFreqSeconds                /* Paranoia */
        ||  m->uCheckFreqSeconds < RT_SEC_1DAY) /* This is the minimum we currently allow. */
    {
        /* Consider config (enable, 0 day interval) as checking once but never again.
           We've already check since we've got a date. */
        *aCheckNeeded = FALSE;
        return S_OK;
    }

    uint64_t const cCheckFreqDays = m->uCheckFreqSeconds / RT_SEC_1DAY_64;

    RTTIMESPEC TimeDiff;
    RTTimeSpecSub(RTTimeNow(&TimeDiff), &LastCheckTime);

    int64_t const diffLastCheckSecs = RTTimeSpecGetSeconds(&TimeDiff);
    int64_t const diffLastCheckDays = diffLastCheckSecs / (int64_t)RT_SEC_1DAY_64;

    /* Be as accurate as possible. */
    *aCheckNeeded = diffLastCheckSecs >= (int64_t)m->uCheckFreqSeconds ? TRUE : FALSE;

    LogRel2(("Update agent (%s): Last update %RI64 days (%RI64 seconds) ago, check frequency is every %RU64 days (%RU64 seconds) -> Check %s\n",
             mData.m_strName.c_str(), diffLastCheckDays, diffLastCheckSecs, cCheckFreqDays, m->uCheckFreqSeconds,
             *aCheckNeeded ? "needed" : "not needed"));

    return S_OK;
}

HRESULT UpdateAgent::getSupportedChannels(std::vector<UpdateChannel_T> &aSupportedChannels)
{
    /* No need to take the read lock, as m_enmChannels is const. */

    aSupportedChannels = mData.m_enmChannels;

    return S_OK;
}


/*********************************************************************************************************************************
*   Internal helper methods of update agent class                                                                                *
*********************************************************************************************************************************/

/**
 * Loads the settings of the update agent base class.
 *
 * @returns HRESULT
 * @retval  E_INVALIDARG if to-load settings are invalid / not supported.
 * @param   data                Where to load the settings from.
 */
HRESULT UpdateAgent::i_loadSettings(const settings::UpdateAgent &data)
{
    AutoCaller autoCaller(this);
    if (FAILED(autoCaller.hrc())) return autoCaller.hrc();

    AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);

    m->fEnabled          = data.fEnabled;
    m->enmChannel        = data.enmChannel;
    m->uCheckFreqSeconds = data.uCheckFreqSeconds;
    if (data.strRepoUrl.isNotEmpty()) /* Prevent overwriting the agent's default URL when XML settings are empty. */
        m->strRepoUrl    = data.strRepoUrl;
    m->strLastCheckDate  = data.strLastCheckDate;
    m->uCheckCount       = data.uCheckCount;

    /* Sanity checks. */
    if (!i_urlSchemeIsSupported(data.strRepoUrl))
        return setError(E_INVALIDARG, tr("Invalid URL scheme specified!"));

    return S_OK;
}

/**
 * Saves the settings of the update agent base class.
 *
 * @returns HRESULT
 * @param   data                Where to save the settings to.
 */
HRESULT UpdateAgent::i_saveSettings(settings::UpdateAgent &data)
{
    AutoCaller autoCaller(this);
    if (FAILED(autoCaller.hrc())) return autoCaller.hrc();

    AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);

    data = *m;

    return S_OK;
}

/**
 * Sets the update check count.
 *
 * @returns HRESULT
 * @param   aCount              Update check count to set.
 */
HRESULT UpdateAgent::i_setCheckCount(ULONG aCount)
{
    AutoCaller autoCaller(this);
    if (FAILED(autoCaller.hrc())) return autoCaller.hrc();

    AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);

    m->uCheckCount = aCount;

    return i_commitSettings(alock);
}

/**
 * Sets the last update check date.
 *
 * @returns HRESULT
 * @param   aDate               Last update check date to set.
 *                              Must be in ISO 8601 format (e.g. 2020-05-11T21:13:39.348416000Z).
 */
HRESULT UpdateAgent::i_setLastCheckDate(const com::Utf8Str &aDate)
{
    AutoCaller autoCaller(this);
    if (FAILED(autoCaller.hrc())) return autoCaller.hrc();

    AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);

    m->strLastCheckDate = aDate;

    return i_commitSettings(alock);
}

/**
 * Internal helper function to commit modified settings.
 *
 * @returns HRESULT
 * @param   aLock               Write lock to release before committing settings.
 */
HRESULT UpdateAgent::i_commitSettings(AutoWriteLock &aLock)
{
    aLock.release();

    m_VirtualBox->i_onUpdateAgentSettingsChanged(this, "" /** @todo Include attribute hints */);

    AutoWriteLock vboxLock(m_VirtualBox COMMA_LOCKVAL_SRC_POS);
    return m_VirtualBox->i_saveSettings();
}

/**
 * Returns the proxy mode to use.
 *
 * @returns HRESULT
 * @param   aMode               Where to return the proxy mode.
 */
HRESULT UpdateAgent::i_getProxyMode(ProxyMode_T *aMode)
{
    ComPtr<ISystemProperties> pSystemProperties;
    HRESULT hrc = m_VirtualBox->COMGETTER(SystemProperties)(pSystemProperties.asOutParam());
    if (SUCCEEDED(hrc))
        hrc = pSystemProperties->COMGETTER(ProxyMode)(aMode);

    return hrc;
}

/**
 * Returns the proxy URL to use.
 *
 * @returns HRESULT
 * @param   aUrl                Where to return the proxy URL to use.
 */
HRESULT UpdateAgent::i_getProxyURL(com::Utf8Str &aUrl)
{
    ComPtr<ISystemProperties> pSystemProperties;
    HRESULT hrc = m_VirtualBox->COMGETTER(SystemProperties)(pSystemProperties.asOutParam());
    if (SUCCEEDED(hrc))
    {
        com::Bstr bstrVal;
        hrc = pSystemProperties->COMGETTER(ProxyURL)(bstrVal.asOutParam());
        if (SUCCEEDED(hrc))
            aUrl = bstrVal;
    }

    return hrc;
}

/**
 * Configures a HTTP client's proxy.
 *
 * @returns HRESULT
 * @param   hHttp               HTTP client to configure proxy for.
 */
HRESULT UpdateAgent::i_configureProxy(RTHTTP hHttp)
{
    ProxyMode_T enmProxyMode;
    HRESULT hrc = i_getProxyMode(&enmProxyMode);
    ComAssertComRCRetRC(hrc);

    Utf8Str strProxyUrl;
    hrc = i_getProxyURL(strProxyUrl);
    ComAssertComRCRetRC(hrc);

    if (enmProxyMode == ProxyMode_Manual)
    {
        int vrc = RTHttpSetProxyByUrl(hHttp, strProxyUrl.c_str());
        if (RT_FAILURE(vrc))
            return i_reportError(vrc, tr("RTHttpSetProxyByUrl() failed: %Rrc"), vrc);
    }
    else if (enmProxyMode == ProxyMode_System)
    {
        int vrc = RTHttpUseSystemProxySettings(hHttp);
        if (RT_FAILURE(vrc))
            return i_reportError(vrc, tr("RTHttpUseSystemProxySettings() failed: %Rrc"), vrc);
    }
    else
        Assert(enmProxyMode == ProxyMode_NoProxy);

    LogRel2(("Update agent (%s): Using proxy mode = '%s', URL = '%s'\n",
             mData.m_strName.c_str(), UpdateAgentBase::i_proxyModeToStr(enmProxyMode), strProxyUrl.c_str()));

    return S_OK;
}

/**
 * Reports an error by setting the error info and also informs subscribed listeners.
 *
 * @returns HRESULT
 * @param   vrc                 Result code (IPRT-style) to report.
 * @param   pcszMsgFmt          Error message to report.
 * @param   ...                 Format string for \a pcszMsgFmt.
 */
HRESULT UpdateAgent::i_reportError(int vrc, const char *pcszMsgFmt, ...)
{
    AssertReturn(pcszMsgFmt && *pcszMsgFmt != '\0', E_INVALIDARG);

    va_list va;
    va_start(va, pcszMsgFmt);

    Utf8Str strMsg;
    int const vrc2 = strMsg.printfVNoThrow(pcszMsgFmt, va);
    if (RT_FAILURE(vrc2))
    {
        va_end(va);
        return setErrorBoth(VBOX_E_IPRT_ERROR, vrc2, tr("Failed to format update agent error string (%Rrc)"), vrc2);
    }

    va_end(va);

    LogRel(("Update agent (%s): %s\n", mData.m_strName.c_str(), strMsg.c_str()));

    m_VirtualBox->i_onUpdateAgentError(this, strMsg.c_str(), vrc);

    return setErrorBoth(VBOX_E_IPRT_ERROR, vrc, strMsg.c_str());
}


/*********************************************************************************************************************************
*   Host update implementation                                                                                                   *
*********************************************************************************************************************************/

HostUpdateAgent::HostUpdateAgent(void)
{
}

HostUpdateAgent::~HostUpdateAgent(void)
{
}


HRESULT HostUpdateAgent::FinalConstruct(void)
{
    return BaseFinalConstruct();
}

void HostUpdateAgent::FinalRelease(void)
{
    uninit();

    BaseFinalRelease();
}

HRESULT HostUpdateAgent::init(VirtualBox *aVirtualBox)
{
    // Enclose the state transition NotReady->InInit->Ready.
    AutoInitSpan autoInitSpan(this);
    AssertReturn(autoInitSpan.isOk(), E_FAIL);

    /* Initialize the bare minimum to get things going.
     ** @todo Add more stuff later here. */
    mData.m_strName = "VirtualBox";
    mData.m_fHidden = false;

    const UpdateChannel_T aChannels[] =
    {
        UpdateChannel_Stable,
        UpdateChannel_All,
        UpdateChannel_WithBetas
        /** @todo Add UpdateChannel_WithTesting once it's implemented on the backend. */
    };
    unconst(mData.m_enmChannels).assign(aChannels, aChannels + RT_ELEMENTS(aChannels));

    /* Set default repository. */
    m->strRepoUrl = "https://update.virtualbox.org";

    HRESULT hrc = UpdateAgent::init(aVirtualBox);
    if (SUCCEEDED(hrc))
        autoInitSpan.setSucceeded();

    return hrc;
}

void HostUpdateAgent::uninit(void)
{
    // Enclose the state transition Ready->InUninit->NotReady.
    AutoUninitSpan autoUninitSpan(this);
    if (autoUninitSpan.uninitDone())
        return;
}

HRESULT HostUpdateAgent::checkFor(ComPtr<IProgress> &aProgress)
{
    AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);

    ComObjPtr<Progress> pProgress;
    HRESULT hrc = pProgress.createObject();
    if (FAILED(hrc))
        return hrc;

    hrc = pProgress->init(m_VirtualBox,
                          static_cast<IUpdateAgent*>(this),
                          tr("Checking for update for %s ...", this->mData.m_strName.c_str()),
                          TRUE /* aCancelable */);
    if (FAILED(hrc))
        return hrc;

    /* initialize the worker task */
    UpdateAgentTask *pTask = new UpdateAgentTask(this, pProgress);
    hrc = pTask->createThread();
    pTask = NULL;
    if (FAILED(hrc))
        return hrc;

    return pProgress.queryInterfaceTo(aProgress.asOutParam());
}


/*********************************************************************************************************************************
*   Host update internal functions                                                                                               *
*********************************************************************************************************************************/

/**
 * Task callback to perform an update check for the VirtualBox host (core).
 *
 * @returns HRESULT
 * @param   pTask               Associated update agent task to use.
 */
DECLCALLBACK(HRESULT) HostUpdateAgent::i_checkForUpdateTask(UpdateAgentTask *pTask)
{
    RT_NOREF(pTask);

    AssertReturn(m->strRepoUrl.isNotEmpty(), E_INVALIDARG);

    // Following the sequence of steps in UIUpdateStepVirtualBox::sltStartStep()
    // Build up our query URL starting with the configured repository.
    Utf8Str strUrl;
    strUrl.appendPrintf("%s/query.php/?", m->strRepoUrl.c_str());

    // Add platform ID.
    Bstr platform;
    HRESULT hrc = m_VirtualBox->COMGETTER(PackageType)(platform.asOutParam());
    AssertComRCReturn(hrc, hrc);
    strUrl.appendPrintf("platform=%ls", platform.raw()); // e.g. SOLARIS_64BITS_GENERIC

    // Get the complete current version string for the query URL
    Bstr versionNormalized;
    hrc = m_VirtualBox->COMGETTER(VersionNormalized)(versionNormalized.asOutParam());
    AssertComRCReturn(hrc, hrc);
    strUrl.appendPrintf("&version=%ls", versionNormalized.raw()); // e.g. 6.1.1
#ifdef DEBUG // Comment out previous line and uncomment this one for testing.
//  strUrl.appendPrintf("&version=6.0.12");
#endif

    ULONG revision = 0;
    hrc = m_VirtualBox->COMGETTER(Revision)(&revision);
    AssertComRCReturn(hrc, hrc);
    strUrl.appendPrintf("_%u", revision); // e.g. 135618

    // Update the last update check timestamp.
    RTTIME Time;
    RTTIMESPEC TimeNow;
    char szTimeStr[RTTIME_STR_LEN];
    RTTimeToString(RTTimeExplode(&Time, RTTimeNow(&TimeNow)), szTimeStr, sizeof(szTimeStr));
    LogRel2(("Update agent (%s): Setting last update check timestamp to '%s'\n", mData.m_strName.c_str(), szTimeStr));

    AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);

    m->strLastCheckDate = szTimeStr;
    m->uCheckCount++;

    hrc = i_commitSettings(alock);
    AssertComRCReturn(hrc, hrc);

    strUrl.appendPrintf("&count=%RU32", m->uCheckCount);

    // Update the query URL (if necessary) with the 'channel' information.
    switch (m->enmChannel)
    {
        case UpdateChannel_All:
            strUrl.appendPrintf("&branch=allrelease"); // query.php expects 'allrelease' and not 'allreleases'
            break;
        case UpdateChannel_WithBetas:
            strUrl.appendPrintf("&branch=withbetas");
            break;
        /** @todo Handle UpdateChannel_WithTesting once implemented on the backend. */
        case UpdateChannel_Stable:
            RT_FALL_THROUGH();
        default:
            strUrl.appendPrintf("&branch=stable");
            break;
    }

    LogRel2(("Update agent (%s): Using URL '%s'\n", mData.m_strName.c_str(), strUrl.c_str()));

    /*
     * Compose the User-Agent header for the GET request.
     */
    Bstr version;
    hrc = m_VirtualBox->COMGETTER(Version)(version.asOutParam()); // e.g. 6.1.0_RC1
    AssertComRCReturn(hrc, hrc);

    Utf8StrFmt const strUserAgent("VirtualBox %ls <%s>", version.raw(), UpdateAgent::i_getPlatformInfo().c_str());
    LogRel2(("Update agent (%s): Using user agent '%s'\n",  mData.m_strName.c_str(), strUserAgent.c_str()));

    /*
     * Create the HTTP client instance and pass it to a inner worker method to
     * ensure proper cleanup.
     */
    RTHTTP hHttp = NIL_RTHTTP;
    int vrc = RTHttpCreate(&hHttp);
    if (RT_SUCCESS(vrc))
    {
        try
        {
            hrc = i_checkForUpdateInner(hHttp, strUrl, strUserAgent);
        }
        catch (...)
        {
            AssertFailed();
            hrc = E_UNEXPECTED;
        }
        RTHttpDestroy(hHttp);
    }
    else
        hrc = i_reportError(vrc, tr("RTHttpCreate() failed: %Rrc"), vrc);

    return hrc;
}

/**
 * Inner function of the actual update checking mechanism.
 *
 * @returns HRESULT
 * @param   hHttp               HTTP client instance to use for checking.
 * @param   strUrl              URL of repository to check.
 * @param   strUserAgent        HTTP user agent to use for checking.
 */
HRESULT HostUpdateAgent::i_checkForUpdateInner(RTHTTP hHttp, Utf8Str const &strUrl, Utf8Str const &strUserAgent)
{
    /*
     * Configure the proxy (if any).
     */
    HRESULT hrc = i_configureProxy(hHttp);
    if (FAILED(hrc))
        return hrc;

    /** @todo Are there any other headers needed to be added first via RTHttpSetHeaders()? */
    int vrc = RTHttpAddHeader(hHttp, "User-Agent", strUserAgent.c_str(), strUserAgent.length(), RTHTTPADDHDR_F_BACK);
    if (RT_FAILURE(vrc))
        return i_reportError(vrc, tr("RTHttpAddHeader() failed: %Rrc (user agent)"), vrc);

    /*
     * Perform the GET request, returning raw binary stuff.
     */
    void *pvResponse = NULL;
    size_t cbResponse = 0;
    vrc = RTHttpGetBinary(hHttp, strUrl.c_str(), &pvResponse, &cbResponse);
    if (RT_FAILURE(vrc))
        return i_reportError(vrc, tr("RTHttpGetBinary() failed: %Rrc"), vrc);

    /* Note! We can do nothing that might throw exceptions till we call RTHttpFreeResponse! */

    /*
     * If url is platform=DARWIN_64BITS_GENERIC&version=6.0.12&branch=stable for example, the reply is:
     *      6.0.14<SPACE>https://download.virtualbox.org/virtualbox/6.0.14/VirtualBox-6.0.14-133895-OSX.dmg
     * If no update required, 'UPTODATE' is returned.
     */
    /* Parse out the two first words of the response, ignoring whatever follows: */
    const char *pchResponse = (const char *)pvResponse;
    while (cbResponse > 0 && *pchResponse == ' ')
        cbResponse--, pchResponse++;

    char ch;
    const char *pchWord0 = pchResponse;
    while (cbResponse > 0 && (ch = *pchResponse) != ' ' && ch != '\0')
        cbResponse--, pchResponse++;
    size_t const cchWord0 = (size_t)(pchResponse - pchWord0);

    while (cbResponse > 0 && *pchResponse == ' ')
        cbResponse--, pchResponse++;
    const char *pchWord1 = pchResponse;
    while (cbResponse > 0 && (ch = *pchResponse) != ' ' && ch != '\0')
        cbResponse--, pchResponse++;
    size_t const cchWord1 = (size_t)(pchResponse - pchWord1);

    /* Decode the two word: */
    static char const s_szUpToDate[] = "UPTODATE";
    if (   cchWord0 == sizeof(s_szUpToDate) - 1
        && memcmp(pchWord0, s_szUpToDate, sizeof(s_szUpToDate) - 1) == 0)
    {
        AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);

        mData.m_enmState = UpdateState_NotAvailable;
        hrc = S_OK;

        alock.release(); /* Release lock before firing off event. */

        m_VirtualBox->i_onUpdateAgentStateChanged(this, UpdateState_NotAvailable);
    }
    else
    {
        mData.m_enmState = UpdateState_Error; /* Play safe by default. */

        vrc = RTStrValidateEncodingEx(pchWord0, cchWord0, 0 /*fFlags*/);
        if (RT_SUCCESS(vrc))
            vrc = RTStrValidateEncodingEx(pchWord1, cchWord1, 0 /*fFlags*/);
        if (RT_SUCCESS(vrc))
        {
            /** @todo Any additional sanity checks we could perform here? */
            hrc = mData.m_lastResult.strVer.assignEx(pchWord0, cchWord0);
            if (SUCCEEDED(hrc))
                hrc = mData.m_lastResult.strDownloadUrl.assignEx(pchWord1, cchWord1);

            if (SUCCEEDED(hrc))
            {
                AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);

                /** @todo Implement this on the backend first.
                 *        We also could do some guessing based on the installed version vs. reported update version? */
                mData.m_lastResult.enmSeverity = UpdateSeverity_Invalid;
                mData.m_enmState               = UpdateState_Available;

                alock.release(); /* Release lock before firing off events. */

                m_VirtualBox->i_onUpdateAgentStateChanged(this, UpdateState_Available);
                m_VirtualBox->i_onUpdateAgentAvailable(this, mData.m_lastResult.strVer, m->enmChannel,
                                                       mData.m_lastResult.enmSeverity, mData.m_lastResult.strDownloadUrl,
                                                       mData.m_lastResult.strWebUrl, mData.m_lastResult.strReleaseNotes);
            }
            else
                hrc = i_reportError(VERR_GENERAL_FAILURE /** @todo Use a better hrc */,
                                    tr("Invalid server response [1]: %Rhrc (%.*Rhxs -- %.*Rhxs)"),
                                    hrc, cchWord0, pchWord0, cchWord1, pchWord1);

            LogRel2(("Update agent (%s): HTTP server replied: %.*s %.*s\n",
                     mData.m_strName.c_str(), cchWord0, pchWord0, cchWord1, pchWord1));
        }
        else
            hrc = i_reportError(vrc, tr("Invalid server response [2]: %Rrc (%.*Rhxs -- %.*Rhxs)"),
                                vrc, cchWord0, pchWord0, cchWord1, pchWord1);
    }

    RTHttpFreeResponse(pvResponse);

    return hrc;
}