VirtualBox

source: vbox/trunk/src/VBox/Main/src-all/ExtPackManagerImpl.cpp@ 74087

Last change on this file since 74087 was 73916, checked in by vboxsync, 6 years ago

Main/ExtPackManager: add missing space in the release log between the
program name and the command.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 116.4 KB
Line 
1/* $Id: ExtPackManagerImpl.cpp 73916 2018-08-27 14:10:00Z vboxsync $ */
2/** @file
3 * VirtualBox Main - interface for Extension Packs, VBoxSVC & VBoxC.
4 */
5
6/*
7 * Copyright (C) 2010-2017 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 */
17
18
19/*********************************************************************************************************************************
20* Header Files *
21*********************************************************************************************************************************/
22#include "ExtPackManagerImpl.h"
23#include "ExtPackUtil.h"
24#include "ThreadTask.h"
25
26#include <iprt/buildconfig.h>
27#include <iprt/ctype.h>
28#include <iprt/dir.h>
29#include <iprt/env.h>
30#include <iprt/file.h>
31#include <iprt/ldr.h>
32#include <iprt/manifest.h>
33#include <iprt/param.h>
34#include <iprt/path.h>
35#include <iprt/pipe.h>
36#include <iprt/process.h>
37#include <iprt/string.h>
38
39#include <VBox/com/array.h>
40#include <VBox/com/ErrorInfo.h>
41#include <VBox/err.h>
42#include <VBox/log.h>
43#include <VBox/sup.h>
44#include <VBox/version.h>
45#include "AutoCaller.h"
46#include "Global.h"
47#include "ProgressImpl.h"
48#ifdef VBOX_COM_INPROC
49# include "ConsoleImpl.h"
50#else
51# include "VirtualBoxImpl.h"
52#endif
53
54
55/*********************************************************************************************************************************
56* Defined Constants And Macros *
57*********************************************************************************************************************************/
58/** @def VBOX_EXTPACK_HELPER_NAME
59 * The name of the utility application we employ to install and uninstall the
60 * extension packs. This is a set-uid-to-root binary on unixy platforms, which
61 * is why it has to be a separate application.
62 */
63#if defined(RT_OS_WINDOWS) || defined(RT_OS_OS2)
64# define VBOX_EXTPACK_HELPER_NAME "VBoxExtPackHelperApp.exe"
65#else
66# define VBOX_EXTPACK_HELPER_NAME "VBoxExtPackHelperApp"
67#endif
68
69
70/*********************************************************************************************************************************
71* Structures and Typedefs *
72*********************************************************************************************************************************/
73struct ExtPackBaseData
74{
75public:
76 /** The extension pack descriptor (loaded from the XML, mostly). */
77 VBOXEXTPACKDESC Desc;
78 /** The file system object info of the XML file.
79 * This is for detecting changes and save time in refresh(). */
80 RTFSOBJINFO ObjInfoDesc;
81 /** Whether it's usable or not. */
82 bool fUsable;
83 /** Why it is unusable. */
84 Utf8Str strWhyUnusable;
85};
86
87#ifndef VBOX_COM_INPROC
88/**
89 * Private extension pack data.
90 */
91struct ExtPackFile::Data : public ExtPackBaseData
92{
93public:
94 /** The path to the tarball. */
95 Utf8Str strExtPackFile;
96 /** The SHA-256 hash of the file (as string). */
97 Utf8Str strDigest;
98 /** The file handle of the extension pack file. */
99 RTFILE hExtPackFile;
100 /** Our manifest for the tarball. */
101 RTMANIFEST hOurManifest;
102 /** Pointer to the extension pack manager. */
103 ComObjPtr<ExtPackManager> ptrExtPackMgr;
104 /** Pointer to the VirtualBox object so we can create a progress object. */
105 VirtualBox *pVirtualBox;
106
107 RTMEMEF_NEW_AND_DELETE_OPERATORS();
108};
109#endif
110
111/**
112 * Private extension pack data.
113 */
114struct ExtPack::Data : public ExtPackBaseData
115{
116public:
117 /** Where the extension pack is located. */
118 Utf8Str strExtPackPath;
119 /** The file system object info of the extension pack directory.
120 * This is for detecting changes and save time in refresh(). */
121 RTFSOBJINFO ObjInfoExtPack;
122 /** The full path to the main module. */
123 Utf8Str strMainModPath;
124 /** The file system object info of the main module.
125 * This is used to determin whether to bother try reload it. */
126 RTFSOBJINFO ObjInfoMainMod;
127 /** The module handle of the main extension pack module. */
128 RTLDRMOD hMainMod;
129
130 /** The helper callbacks for the extension pack. */
131 VBOXEXTPACKHLP Hlp;
132 /** Pointer back to the extension pack object (for Hlp methods). */
133 ExtPack *pThis;
134#ifndef VBOX_COM_INPROC
135 /** The extension pack main registration structure. */
136 PCVBOXEXTPACKREG pReg;
137#else
138 /** The extension pack main VM registration structure. */
139 PCVBOXEXTPACKVMREG pReg;
140#endif
141 /** The current context. */
142 VBOXEXTPACKCTX enmContext;
143 /** Set if we've made the pfnVirtualBoxReady or pfnConsoleReady call. */
144 bool fMadeReadyCall;
145#ifndef VBOX_COM_INPROC
146 /** Pointer to the VirtualBox object so we can create a progress object. */
147 VirtualBox *pVirtualBox;
148#endif
149
150 RTMEMEF_NEW_AND_DELETE_OPERATORS();
151};
152
153/** List of extension packs. */
154typedef std::list< ComObjPtr<ExtPack> > ExtPackList;
155
156/**
157 * Private extension pack manager data.
158 */
159struct ExtPackManager::Data
160{
161 Data()
162 : cUpdate(0)
163 {}
164
165 /** The directory where the extension packs are installed. */
166 Utf8Str strBaseDir;
167 /** The directory where the certificates this installation recognizes are
168 * stored. */
169 Utf8Str strCertificatDirPath;
170 /** The list of installed extension packs. */
171 ExtPackList llInstalledExtPacks;
172#ifndef VBOX_COM_INPROC
173 /** Pointer to the VirtualBox object, our parent. */
174 VirtualBox *pVirtualBox;
175#endif
176 /** The current context. */
177 VBOXEXTPACKCTX enmContext;
178 /** Update counter for the installed extension packs, increased in every list update. */
179 uint64_t cUpdate;
180
181 RTMEMEF_NEW_AND_DELETE_OPERATORS();
182};
183
184#ifndef VBOX_COM_INPROC
185
186/**
187 * Extension pack installation job.
188 */
189class ExtPackInstallTask : public ThreadTask
190{
191public:
192 explicit ExtPackInstallTask() : ThreadTask("ExtPackInst") { }
193 ~ExtPackInstallTask() { }
194
195 void handler()
196 {
197 HRESULT hrc = ptrExtPackMgr->i_doInstall(ptrExtPackFile, fReplace, &strDisplayInfo);
198 ptrProgress->i_notifyComplete(hrc);
199 }
200
201 HRESULT Init(const ComPtr<ExtPackFile> &a_strExtPackFile, bool a_fReplace,
202 const Utf8Str &strDispInfo, const ComPtr<ExtPackManager> &a_ptrExtPackMgr)
203 {
204 ptrExtPackFile = a_strExtPackFile;
205 fReplace = a_fReplace;
206 strDisplayInfo = strDispInfo;
207 ptrExtPackMgr = a_ptrExtPackMgr;
208
209 HRESULT hrc = ptrProgress.createObject();
210 if (SUCCEEDED(hrc))
211 {
212 Bstr bstrDescription("Installing extension pack");
213 hrc = ptrProgress->init(ptrExtPackFile->m->pVirtualBox,
214 static_cast<IExtPackFile *>(ptrExtPackFile),
215 bstrDescription.raw(),
216 FALSE /*aCancelable*/);
217 }
218
219 return hrc;
220 }
221
222 /** Smart pointer to the progress object for this job. */
223 ComObjPtr<Progress> ptrProgress;
224private:
225 /** Smart pointer to the extension pack file. */
226 ComPtr<ExtPackFile> ptrExtPackFile;
227 /** The replace argument. */
228 bool fReplace;
229 /** The display info argument. */
230 Utf8Str strDisplayInfo;
231 /** Smart pointer to the extension manager. */
232 ComPtr<ExtPackManager> ptrExtPackMgr;
233};
234
235/**
236 * Extension pack uninstallation job.
237 */
238class ExtPackUninstallTask : public ThreadTask
239{
240public:
241 explicit ExtPackUninstallTask() : ThreadTask("ExtPackUninst") { }
242 ~ExtPackUninstallTask() { }
243
244 void handler()
245 {
246 HRESULT hrc = ptrExtPackMgr->i_doUninstall(&strName, fForcedRemoval, &strDisplayInfo);
247 ptrProgress->i_notifyComplete(hrc);
248 }
249
250 HRESULT Init(const ComPtr<ExtPackManager> &a_ptrExtPackMgr, const Utf8Str &a_strName,
251 bool a_fForcedRemoval, const Utf8Str &a_strDisplayInfo)
252 {
253 ptrExtPackMgr = a_ptrExtPackMgr;
254 strName = a_strName;
255 fForcedRemoval = a_fForcedRemoval;
256 strDisplayInfo = a_strDisplayInfo;
257
258 HRESULT hrc = ptrProgress.createObject();
259 if (SUCCEEDED(hrc))
260 {
261 Bstr bstrDescription("Uninstalling extension pack");
262 hrc = ptrProgress->init(ptrExtPackMgr->m->pVirtualBox,
263 static_cast<IExtPackManager *>(ptrExtPackMgr),
264 bstrDescription.raw(),
265 FALSE /*aCancelable*/);
266 }
267
268 return hrc;
269 }
270
271 /** Smart pointer to the progress object for this job. */
272 ComObjPtr<Progress> ptrProgress;
273private:
274 /** Smart pointer to the extension manager. */
275 ComPtr<ExtPackManager> ptrExtPackMgr;
276 /** The name of the extension pack. */
277 Utf8Str strName;
278 /** The replace argument. */
279 bool fForcedRemoval;
280 /** The display info argument. */
281 Utf8Str strDisplayInfo;
282};
283
284DEFINE_EMPTY_CTOR_DTOR(ExtPackFile)
285
286/**
287 * Called by ComObjPtr::createObject when creating the object.
288 *
289 * Just initialize the basic object state, do the rest in initWithDir().
290 *
291 * @returns S_OK.
292 */
293HRESULT ExtPackFile::FinalConstruct()
294{
295 m = NULL;
296 return BaseFinalConstruct();
297}
298
299/**
300 * Initializes the extension pack by reading its file.
301 *
302 * @returns COM status code.
303 * @param a_pszFile The path to the extension pack file.
304 * @param a_pszDigest The SHA-256 digest of the file. Or an empty string.
305 * @param a_pExtPackMgr Pointer to the extension pack manager.
306 * @param a_pVirtualBox Pointer to the VirtualBox object.
307 */
308HRESULT ExtPackFile::initWithFile(const char *a_pszFile, const char *a_pszDigest, ExtPackManager *a_pExtPackMgr,
309 VirtualBox *a_pVirtualBox)
310{
311 AutoInitSpan autoInitSpan(this);
312 AssertReturn(autoInitSpan.isOk(), E_FAIL);
313
314 /*
315 * Allocate + initialize our private data.
316 */
317 m = new ExtPackFile::Data;
318 VBoxExtPackInitDesc(&m->Desc);
319 RT_ZERO(m->ObjInfoDesc);
320 m->fUsable = false;
321 m->strWhyUnusable = tr("ExtPack::init failed");
322 m->strExtPackFile = a_pszFile;
323 m->strDigest = a_pszDigest;
324 m->hExtPackFile = NIL_RTFILE;
325 m->hOurManifest = NIL_RTMANIFEST;
326 m->ptrExtPackMgr = a_pExtPackMgr;
327 m->pVirtualBox = a_pVirtualBox;
328
329 RTCString *pstrTarName = VBoxExtPackExtractNameFromTarballPath(a_pszFile);
330 if (pstrTarName)
331 {
332 m->Desc.strName = *pstrTarName;
333 delete pstrTarName;
334 pstrTarName = NULL;
335 }
336
337 autoInitSpan.setSucceeded();
338
339 /*
340 * Try open the extension pack and check that it is a regular file.
341 */
342 int vrc = RTFileOpen(&m->hExtPackFile, a_pszFile,
343 RTFILE_O_READ | RTFILE_O_DENY_WRITE | RTFILE_O_OPEN);
344 if (RT_FAILURE(vrc))
345 {
346 if (vrc == VERR_FILE_NOT_FOUND || vrc == VERR_PATH_NOT_FOUND)
347 return initFailed(tr("'%s' file not found"), a_pszFile);
348 return initFailed(tr("RTFileOpen('%s',,) failed with %Rrc"), a_pszFile, vrc);
349 }
350
351 RTFSOBJINFO ObjInfo;
352 vrc = RTFileQueryInfo(m->hExtPackFile, &ObjInfo, RTFSOBJATTRADD_UNIX);
353 if (RT_FAILURE(vrc))
354 return initFailed(tr("RTFileQueryInfo failed with %Rrc on '%s'"), vrc, a_pszFile);
355 if (!RTFS_IS_FILE(ObjInfo.Attr.fMode))
356 return initFailed(tr("Not a regular file: %s"), a_pszFile);
357
358 /*
359 * Validate the tarball and extract the XML file.
360 */
361 char szError[8192];
362 RTVFSFILE hXmlFile;
363 vrc = VBoxExtPackValidateTarball(m->hExtPackFile, NULL /*pszExtPackName*/, a_pszFile, a_pszDigest,
364 szError, sizeof(szError), &m->hOurManifest, &hXmlFile, &m->strDigest);
365 if (RT_FAILURE(vrc))
366 return initFailed(tr("%s"), szError);
367
368 /*
369 * Parse the XML.
370 */
371 RTCString strSavedName(m->Desc.strName);
372 RTCString *pStrLoadErr = VBoxExtPackLoadDescFromVfsFile(hXmlFile, &m->Desc, &m->ObjInfoDesc);
373 RTVfsFileRelease(hXmlFile);
374 if (pStrLoadErr != NULL)
375 {
376 m->strWhyUnusable.printf(tr("Failed to the xml file: %s"), pStrLoadErr->c_str());
377 m->Desc.strName = strSavedName;
378 delete pStrLoadErr;
379 return S_OK;
380 }
381
382 /*
383 * Match the tarball name with the name from the XML.
384 */
385 /** @todo drop this restriction after the old install interface is
386 * dropped. */
387 if (!strSavedName.equalsIgnoreCase(m->Desc.strName))
388 return initFailed(tr("Extension pack name mismatch between the downloaded file and the XML inside it (xml='%s' file='%s')"),
389 m->Desc.strName.c_str(), strSavedName.c_str());
390
391
392 m->fUsable = true;
393 m->strWhyUnusable.setNull();
394 return S_OK;
395}
396
397/**
398 * Protected helper that formats the strWhyUnusable value.
399 *
400 * @returns S_OK
401 * @param a_pszWhyFmt Why it failed, format string.
402 * @param ... The format arguments.
403 */
404HRESULT ExtPackFile::initFailed(const char *a_pszWhyFmt, ...)
405{
406 va_list va;
407 va_start(va, a_pszWhyFmt);
408 m->strWhyUnusable.printfV(a_pszWhyFmt, va);
409 va_end(va);
410 return S_OK;
411}
412
413/**
414 * COM cruft.
415 */
416void ExtPackFile::FinalRelease()
417{
418 uninit();
419 BaseFinalRelease();
420}
421
422/**
423 * Do the actual cleanup.
424 */
425void ExtPackFile::uninit()
426{
427 /* Enclose the state transition Ready->InUninit->NotReady */
428 AutoUninitSpan autoUninitSpan(this);
429 if (!autoUninitSpan.uninitDone() && m != NULL)
430 {
431 VBoxExtPackFreeDesc(&m->Desc);
432 RTFileClose(m->hExtPackFile);
433 m->hExtPackFile = NIL_RTFILE;
434 RTManifestRelease(m->hOurManifest);
435 m->hOurManifest = NIL_RTMANIFEST;
436
437 delete m;
438 m = NULL;
439 }
440}
441
442HRESULT ExtPackFile::getName(com::Utf8Str &aName)
443{
444 aName = m->Desc.strName;
445 return S_OK;
446}
447
448HRESULT ExtPackFile::getDescription(com::Utf8Str &aDescription)
449{
450 aDescription = m->Desc.strDescription;
451 return S_OK;
452}
453
454HRESULT ExtPackFile::getVersion(com::Utf8Str &aVersion)
455{
456 aVersion = m->Desc.strVersion;
457 return S_OK;
458}
459
460HRESULT ExtPackFile::getEdition(com::Utf8Str &aEdition)
461{
462 aEdition = m->Desc.strEdition;
463 return S_OK;
464}
465
466HRESULT ExtPackFile::getRevision(ULONG *aRevision)
467{
468 *aRevision = m->Desc.uRevision;
469 return S_OK;
470}
471
472HRESULT ExtPackFile::getVRDEModule(com::Utf8Str &aVRDEModule)
473{
474 aVRDEModule = m->Desc.strVrdeModule;
475 return S_OK;
476}
477
478HRESULT ExtPackFile::getPlugIns(std::vector<ComPtr<IExtPackPlugIn> > &aPlugIns)
479{
480 /** @todo implement plug-ins. */
481#ifdef VBOX_WITH_XPCOM
482 NOREF(aPlugIns);
483#endif
484 NOREF(aPlugIns);
485 ReturnComNotImplemented();
486}
487
488HRESULT ExtPackFile::getUsable(BOOL *aUsable)
489{
490 *aUsable = m->fUsable;
491 return S_OK;
492}
493
494HRESULT ExtPackFile::getWhyUnusable(com::Utf8Str &aWhyUnusable)
495{
496 aWhyUnusable = m->strWhyUnusable;
497 return S_OK;
498}
499
500HRESULT ExtPackFile::getShowLicense(BOOL *aShowLicense)
501{
502 *aShowLicense = m->Desc.fShowLicense;
503 return S_OK;
504}
505
506HRESULT ExtPackFile::getLicense(com::Utf8Str &aLicense)
507{
508 Utf8Str strHtml("html");
509 Utf8Str str("");
510 return queryLicense(str, str, strHtml, aLicense);
511}
512
513/* Same as ExtPack::QueryLicense, should really explore the subject of base classes here... */
514HRESULT ExtPackFile::queryLicense(const com::Utf8Str &aPreferredLocale, const com::Utf8Str &aPreferredLanguage,
515 const com::Utf8Str &aFormat, com::Utf8Str &aLicenseText)
516{
517 HRESULT hrc = S_OK;
518
519 /*
520 * Validate input.
521 */
522
523 if (aPreferredLocale.length() != 2 && aPreferredLocale.length() != 0)
524 return setError(E_FAIL, tr("The preferred locale is a two character string or empty."));
525
526 if (aPreferredLanguage.length() != 2 && aPreferredLanguage.length() != 0)
527 return setError(E_FAIL, tr("The preferred lanuage is a two character string or empty."));
528
529 if ( !aFormat.equals("html")
530 && !aFormat.equals("rtf")
531 && !aFormat.equals("txt"))
532 return setError(E_FAIL, tr("The license format can only have the values 'html', 'rtf' and 'txt'."));
533
534 /*
535 * Combine the options to form a file name before locking down anything.
536 */
537 char szName[sizeof(VBOX_EXTPACK_LICENSE_NAME_PREFIX "-de_DE.html") + 2];
538 if (aPreferredLocale.isNotEmpty() && aPreferredLanguage.isNotEmpty())
539 RTStrPrintf(szName, sizeof(szName), VBOX_EXTPACK_LICENSE_NAME_PREFIX "-%s_%s.%s",
540 aPreferredLocale.c_str(), aPreferredLanguage.c_str(), aFormat.c_str());
541 else if (aPreferredLocale.isNotEmpty())
542 RTStrPrintf(szName, sizeof(szName), VBOX_EXTPACK_LICENSE_NAME_PREFIX "-%s.%s",
543 aPreferredLocale.c_str(), aFormat.c_str());
544 else if (aPreferredLanguage.isNotEmpty())
545 RTStrPrintf(szName, sizeof(szName), VBOX_EXTPACK_LICENSE_NAME_PREFIX "-_%s.%s",
546 aPreferredLocale.c_str(), aFormat.c_str());
547 else
548 RTStrPrintf(szName, sizeof(szName), VBOX_EXTPACK_LICENSE_NAME_PREFIX ".%s",
549 aFormat.c_str());
550 /*
551 * Lock the extension pack. We need a write lock here as there must not be
552 * concurrent accesses to the tar file handle.
553 */
554 AutoWriteLock autoLock(this COMMA_LOCKVAL_SRC_POS);
555
556 /*
557 * Do not permit this query on a pack that isn't considered usable (could
558 * be marked so because of bad license files).
559 */
560 if (!m->fUsable)
561 hrc = setError(E_FAIL, tr("%s"), m->strWhyUnusable.c_str());
562 else
563 {
564 /*
565 * Look it up in the manifest before scanning the tarball for it
566 */
567 if (RTManifestEntryExists(m->hOurManifest, szName))
568 {
569 RTVFSFSSTREAM hTarFss;
570 char szError[8192];
571 int vrc = VBoxExtPackOpenTarFss(m->hExtPackFile, szError, sizeof(szError), &hTarFss, NULL);
572 if (RT_SUCCESS(vrc))
573 {
574 for (;;)
575 {
576 /* Get the first/next. */
577 char *pszName;
578 RTVFSOBJ hVfsObj;
579 RTVFSOBJTYPE enmType;
580 vrc = RTVfsFsStrmNext(hTarFss, &pszName, &enmType, &hVfsObj);
581 if (RT_FAILURE(vrc))
582 {
583 if (vrc != VERR_EOF)
584 hrc = setErrorBoth(VBOX_E_IPRT_ERROR, vrc, tr("RTVfsFsStrmNext failed: %Rrc"), vrc);
585 else
586 hrc = setErrorBoth(E_UNEXPECTED, vrc, tr("'%s' was found in the manifest but not in the tarball"), szName);
587 break;
588 }
589
590 /* Is this it? */
591 const char *pszAdjName = pszName[0] == '.' && pszName[1] == '/' ? &pszName[2] : pszName;
592 if ( !strcmp(pszAdjName, szName)
593 && ( enmType == RTVFSOBJTYPE_IO_STREAM
594 || enmType == RTVFSOBJTYPE_FILE))
595 {
596 RTVFSIOSTREAM hVfsIos = RTVfsObjToIoStream(hVfsObj);
597 RTVfsObjRelease(hVfsObj);
598 RTStrFree(pszName);
599
600 /* Load the file into memory. */
601 RTFSOBJINFO ObjInfo;
602 vrc = RTVfsIoStrmQueryInfo(hVfsIos, &ObjInfo, RTFSOBJATTRADD_NOTHING);
603 if (RT_SUCCESS(vrc))
604 {
605 size_t cbFile = (size_t)ObjInfo.cbObject;
606 void *pvFile = RTMemAllocZ(cbFile + 1);
607 if (pvFile)
608 {
609 vrc = RTVfsIoStrmRead(hVfsIos, pvFile, cbFile, true /*fBlocking*/, NULL);
610 if (RT_SUCCESS(vrc))
611 {
612 /* try translate it into a string we can return. */
613 Bstr bstrLicense((const char *)pvFile, cbFile);
614 if (bstrLicense.isNotEmpty())
615 {
616 aLicenseText = Utf8Str(bstrLicense);
617 hrc = S_OK;
618 }
619 else
620 hrc = setErrorBoth(VBOX_E_IPRT_ERROR, vrc,
621 tr("The license file '%s' is empty or contains invalid UTF-8 encoding"),
622 szName);
623 }
624 else
625 hrc = setErrorBoth(VBOX_E_IPRT_ERROR, vrc, tr("Failed to read '%s': %Rrc"), szName, vrc);
626 RTMemFree(pvFile);
627 }
628 else
629 hrc = setError(E_OUTOFMEMORY, tr("Failed to allocate %zu bytes for '%s'"), cbFile, szName);
630 }
631 else
632 hrc = setErrorBoth(VBOX_E_IPRT_ERROR, vrc, tr("RTVfsIoStrmQueryInfo on '%s': %Rrc"), szName, vrc);
633 RTVfsIoStrmRelease(hVfsIos);
634 break;
635 }
636
637 /* Release current. */
638 RTVfsObjRelease(hVfsObj);
639 RTStrFree(pszName);
640 }
641 RTVfsFsStrmRelease(hTarFss);
642 }
643 else
644 hrc = setError(VBOX_E_OBJECT_NOT_FOUND, tr("%s"), szError);
645 }
646 else
647 hrc = setError(VBOX_E_OBJECT_NOT_FOUND, tr("The license file '%s' was not found in '%s'"),
648 szName, m->strExtPackFile.c_str());
649 }
650 return hrc;
651}
652
653HRESULT ExtPackFile::getFilePath(com::Utf8Str &aFilePath)
654{
655
656 aFilePath = m->strExtPackFile;
657 return S_OK;
658}
659
660HRESULT ExtPackFile::install(BOOL aReplace, const com::Utf8Str &aDisplayInfo, ComPtr<IProgress> &aProgress)
661{
662 HRESULT hrc;
663 if (m->fUsable)
664 {
665 ExtPackInstallTask *pTask = NULL;
666 try
667 {
668 pTask = new ExtPackInstallTask();
669 hrc = pTask->Init(this, aReplace != FALSE, aDisplayInfo, m->ptrExtPackMgr);
670 if (SUCCEEDED(hrc))
671 {
672 ComPtr<Progress> ptrProgress = pTask->ptrProgress;
673 hrc = pTask->createThreadWithType(RTTHREADTYPE_DEFAULT);
674 pTask = NULL; /* The _completely_ _undocumented_ createThread method always consumes pTask. */
675 if (SUCCEEDED(hrc))
676 hrc = ptrProgress.queryInterfaceTo(aProgress.asOutParam());
677 else
678 hrc = setError(VBOX_E_IPRT_ERROR,
679 tr("Starting thread for an extension pack installation failed with %Rrc"), hrc);
680 }
681 else
682 hrc = setError(VBOX_E_IPRT_ERROR,
683 tr("Looks like creating a progress object for ExtraPackInstallTask object failed"));
684 }
685 catch (std::bad_alloc &)
686 {
687 hrc = E_OUTOFMEMORY;
688 }
689 catch (HRESULT hrcXcpt)
690 {
691 LogFlowThisFunc(("Exception was caught in the function ExtPackFile::install() \n"));
692 hrc = hrcXcpt;
693 }
694 if (pTask)
695 delete pTask;
696 }
697 else
698 hrc = setError(E_FAIL, "%s", m->strWhyUnusable.c_str());
699 return hrc;
700}
701
702#endif /* !VBOX_COM_INPROC */
703
704
705
706
707DEFINE_EMPTY_CTOR_DTOR(ExtPack)
708
709/**
710 * Called by ComObjPtr::createObject when creating the object.
711 *
712 * Just initialize the basic object state, do the rest in initWithDir().
713 *
714 * @returns S_OK.
715 */
716HRESULT ExtPack::FinalConstruct()
717{
718 m = NULL;
719 return BaseFinalConstruct();
720}
721
722/**
723 * Initializes the extension pack by reading its file.
724 *
725 * @returns COM status code.
726 * @param a_pVirtualBox The VirtualBox object.
727 * @param a_enmContext The context we're in.
728 * @param a_pszName The name of the extension pack. This is also the
729 * name of the subdirector under @a a_pszParentDir
730 * where the extension pack is installed.
731 * @param a_pszDir The extension pack directory name.
732 */
733HRESULT ExtPack::initWithDir(VirtualBox *a_pVirtualBox, VBOXEXTPACKCTX a_enmContext, const char *a_pszName, const char *a_pszDir)
734{
735 AutoInitSpan autoInitSpan(this);
736 AssertReturn(autoInitSpan.isOk(), E_FAIL);
737
738 static const VBOXEXTPACKHLP s_HlpTmpl =
739 {
740 /* u32Version = */ VBOXEXTPACKHLP_VERSION,
741 /* uVBoxFullVersion = */ VBOX_FULL_VERSION,
742 /* uVBoxVersionRevision = */ 0,
743 /* u32Padding = */ 0,
744 /* pszVBoxVersion = */ "",
745 /* pfnFindModule = */ ExtPack::i_hlpFindModule,
746 /* pfnGetFilePath = */ ExtPack::i_hlpGetFilePath,
747 /* pfnGetContext = */ ExtPack::i_hlpGetContext,
748 /* pfnLoadHGCMService = */ ExtPack::i_hlpLoadHGCMService,
749 /* pfnLoadVDPlugin = */ ExtPack::i_hlpLoadVDPlugin,
750 /* pfnUnloadVDPlugin = */ ExtPack::i_hlpUnloadVDPlugin,
751 /* pfnCreateProgress = */ ExtPack::i_hlpCreateProgress,
752 /* pfnGetCanceledProgress = */ ExtPack::i_hlpGetCanceledProgress,
753 /* pfnUpdateProgress = */ ExtPack::i_hlpUpdateProgress,
754 /* pfnNextOperationProgress = */ ExtPack::i_hlpNextOperationProgress,
755 /* pfnCompleteProgress = */ ExtPack::i_hlpCompleteProgress,
756 /* pfnReserved1 = */ ExtPack::i_hlpReservedN,
757 /* pfnReserved2 = */ ExtPack::i_hlpReservedN,
758 /* pfnReserved3 = */ ExtPack::i_hlpReservedN,
759 /* pfnReserved4 = */ ExtPack::i_hlpReservedN,
760 /* pfnReserved5 = */ ExtPack::i_hlpReservedN,
761 /* pfnReserved6 = */ ExtPack::i_hlpReservedN,
762 /* u32EndMarker = */ VBOXEXTPACKHLP_VERSION
763 };
764
765 /*
766 * Allocate + initialize our private data.
767 */
768 m = new Data;
769 VBoxExtPackInitDesc(&m->Desc);
770 m->Desc.strName = a_pszName;
771 RT_ZERO(m->ObjInfoDesc);
772 m->fUsable = false;
773 m->strWhyUnusable = tr("ExtPack::init failed");
774 m->strExtPackPath = a_pszDir;
775 RT_ZERO(m->ObjInfoExtPack);
776 m->strMainModPath.setNull();
777 RT_ZERO(m->ObjInfoMainMod);
778 m->hMainMod = NIL_RTLDRMOD;
779 m->Hlp = s_HlpTmpl;
780 m->Hlp.pszVBoxVersion = RTBldCfgVersion();
781 m->Hlp.uVBoxInternalRevision = RTBldCfgRevision();
782 m->pThis = this;
783 m->pReg = NULL;
784 m->enmContext = a_enmContext;
785 m->fMadeReadyCall = false;
786#ifndef VBOX_COM_INPROC
787 m->pVirtualBox = a_pVirtualBox;
788#else
789 RT_NOREF(a_pVirtualBox);
790#endif
791
792 /*
793 * Make sure the SUPR3Hardened API works (ignoring errors for now).
794 */
795 int rc = SUPR3HardenedVerifyInit();
796 if (RT_FAILURE(rc))
797 LogRel(("SUPR3HardenedVerifyInit failed: %Rrc\n", rc));
798
799 /*
800 * Probe the extension pack (this code is shared with refresh()).
801 */
802 i_probeAndLoad();
803
804 autoInitSpan.setSucceeded();
805 return S_OK;
806}
807
808/**
809 * COM cruft.
810 */
811void ExtPack::FinalRelease()
812{
813 uninit();
814 BaseFinalRelease();
815}
816
817/**
818 * Do the actual cleanup.
819 */
820void ExtPack::uninit()
821{
822 /* Enclose the state transition Ready->InUninit->NotReady */
823 AutoUninitSpan autoUninitSpan(this);
824 if (!autoUninitSpan.uninitDone() && m != NULL)
825 {
826 if (m->hMainMod != NIL_RTLDRMOD)
827 {
828 AssertPtr(m->pReg);
829 if (m->pReg->pfnUnload != NULL)
830 m->pReg->pfnUnload(m->pReg);
831
832 RTLdrClose(m->hMainMod);
833 m->hMainMod = NIL_RTLDRMOD;
834 m->pReg = NULL;
835 }
836
837 VBoxExtPackFreeDesc(&m->Desc);
838
839 delete m;
840 m = NULL;
841 }
842}
843
844
845#ifndef VBOX_COM_INPROC
846/**
847 * Calls the installed hook.
848 *
849 * @returns true if we left the lock, false if we didn't.
850 * @param a_pVirtualBox The VirtualBox interface.
851 * @param a_pLock The write lock held by the caller.
852 * @param pErrInfo Where to return error information.
853 */
854bool ExtPack::i_callInstalledHook(IVirtualBox *a_pVirtualBox, AutoWriteLock *a_pLock, PRTERRINFO pErrInfo)
855{
856 if ( m != NULL
857 && m->hMainMod != NIL_RTLDRMOD)
858 {
859 if (m->pReg->pfnInstalled)
860 {
861 ComPtr<ExtPack> ptrSelfRef = this;
862 a_pLock->release();
863 pErrInfo->rc = m->pReg->pfnInstalled(m->pReg, a_pVirtualBox, pErrInfo);
864 a_pLock->acquire();
865 return true;
866 }
867 }
868 pErrInfo->rc = VINF_SUCCESS;
869 return false;
870}
871
872/**
873 * Calls the uninstall hook and closes the module.
874 *
875 * @returns S_OK or COM error status with error information.
876 * @param a_pVirtualBox The VirtualBox interface.
877 * @param a_fForcedRemoval When set, we'll ignore complaints from the
878 * uninstall hook.
879 * @remarks The caller holds the manager's write lock, not released.
880 */
881HRESULT ExtPack::i_callUninstallHookAndClose(IVirtualBox *a_pVirtualBox, bool a_fForcedRemoval)
882{
883 HRESULT hrc = S_OK;
884
885 if ( m != NULL
886 && m->hMainMod != NIL_RTLDRMOD)
887 {
888 if (m->pReg->pfnUninstall && !a_fForcedRemoval)
889 {
890 int vrc = m->pReg->pfnUninstall(m->pReg, a_pVirtualBox);
891 if (RT_FAILURE(vrc))
892 {
893 LogRel(("ExtPack pfnUninstall returned %Rrc for %s\n", vrc, m->Desc.strName.c_str()));
894 if (!a_fForcedRemoval)
895 hrc = setErrorBoth(E_FAIL, vrc, tr("pfnUninstall returned %Rrc"), vrc);
896 }
897 }
898 if (SUCCEEDED(hrc))
899 {
900 RTLdrClose(m->hMainMod);
901 m->hMainMod = NIL_RTLDRMOD;
902 m->pReg = NULL;
903 }
904 }
905
906 return hrc;
907}
908
909/**
910 * Calls the pfnVirtualBoxReady hook.
911 *
912 * @returns true if we left the lock, false if we didn't.
913 * @param a_pVirtualBox The VirtualBox interface.
914 * @param a_pLock The write lock held by the caller.
915 */
916bool ExtPack::i_callVirtualBoxReadyHook(IVirtualBox *a_pVirtualBox, AutoWriteLock *a_pLock)
917{
918 if ( m != NULL
919 && m->fUsable
920 && m->hMainMod != NIL_RTLDRMOD
921 && !m->fMadeReadyCall)
922 {
923 m->fMadeReadyCall = true;
924 if (m->pReg->pfnVirtualBoxReady)
925 {
926 ComPtr<ExtPack> ptrSelfRef = this;
927 a_pLock->release();
928 m->pReg->pfnVirtualBoxReady(m->pReg, a_pVirtualBox);
929 a_pLock->acquire();
930 return true;
931 }
932 }
933 return false;
934}
935#endif /* !VBOX_COM_INPROC */
936
937#ifdef VBOX_COM_INPROC
938/**
939 * Calls the pfnConsoleReady hook.
940 *
941 * @returns true if we left the lock, false if we didn't.
942 * @param a_pConsole The Console interface.
943 * @param a_pLock The write lock held by the caller.
944 */
945bool ExtPack::i_callConsoleReadyHook(IConsole *a_pConsole, AutoWriteLock *a_pLock)
946{
947 if ( m != NULL
948 && m->fUsable
949 && m->hMainMod != NIL_RTLDRMOD
950 && !m->fMadeReadyCall)
951 {
952 m->fMadeReadyCall = true;
953 if (m->pReg->pfnConsoleReady)
954 {
955 ComPtr<ExtPack> ptrSelfRef = this;
956 a_pLock->release();
957 m->pReg->pfnConsoleReady(m->pReg, a_pConsole);
958 a_pLock->acquire();
959 return true;
960 }
961 }
962 return false;
963}
964#endif /* VBOX_COM_INPROC */
965
966#ifndef VBOX_COM_INPROC
967/**
968 * Calls the pfnVMCreate hook.
969 *
970 * @returns true if we left the lock, false if we didn't.
971 * @param a_pVirtualBox The VirtualBox interface.
972 * @param a_pMachine The machine interface of the new VM.
973 * @param a_pLock The write lock held by the caller.
974 */
975bool ExtPack::i_callVmCreatedHook(IVirtualBox *a_pVirtualBox, IMachine *a_pMachine, AutoWriteLock *a_pLock)
976{
977 if ( m != NULL
978 && m->hMainMod != NIL_RTLDRMOD
979 && m->fUsable)
980 {
981 if (m->pReg->pfnVMCreated)
982 {
983 ComPtr<ExtPack> ptrSelfRef = this;
984 a_pLock->release();
985 m->pReg->pfnVMCreated(m->pReg, a_pVirtualBox, a_pMachine);
986 a_pLock->acquire();
987 return true;
988 }
989 }
990 return false;
991}
992#endif /* !VBOX_COM_INPROC */
993
994#ifdef VBOX_COM_INPROC
995/**
996 * Calls the pfnVMConfigureVMM hook.
997 *
998 * @returns true if we left the lock, false if we didn't.
999 * @param a_pConsole The console interface.
1000 * @param a_pVM The VM handle.
1001 * @param a_pLock The write lock held by the caller.
1002 * @param a_pvrc Where to return the status code of the
1003 * callback. This is always set. LogRel is
1004 * called on if a failure status is returned.
1005 */
1006bool ExtPack::i_callVmConfigureVmmHook(IConsole *a_pConsole, PVM a_pVM, AutoWriteLock *a_pLock, int *a_pvrc)
1007{
1008 *a_pvrc = VINF_SUCCESS;
1009 if ( m != NULL
1010 && m->hMainMod != NIL_RTLDRMOD
1011 && m->fUsable)
1012 {
1013 if (m->pReg->pfnVMConfigureVMM)
1014 {
1015 ComPtr<ExtPack> ptrSelfRef = this;
1016 a_pLock->release();
1017 int vrc = m->pReg->pfnVMConfigureVMM(m->pReg, a_pConsole, a_pVM);
1018 *a_pvrc = vrc;
1019 a_pLock->acquire();
1020 if (RT_FAILURE(vrc))
1021 LogRel(("ExtPack pfnVMConfigureVMM returned %Rrc for %s\n", vrc, m->Desc.strName.c_str()));
1022 return true;
1023 }
1024 }
1025 return false;
1026}
1027
1028/**
1029 * Calls the pfnVMPowerOn hook.
1030 *
1031 * @returns true if we left the lock, false if we didn't.
1032 * @param a_pConsole The console interface.
1033 * @param a_pVM The VM handle.
1034 * @param a_pLock The write lock held by the caller.
1035 * @param a_pvrc Where to return the status code of the
1036 * callback. This is always set. LogRel is
1037 * called on if a failure status is returned.
1038 */
1039bool ExtPack::i_callVmPowerOnHook(IConsole *a_pConsole, PVM a_pVM, AutoWriteLock *a_pLock, int *a_pvrc)
1040{
1041 *a_pvrc = VINF_SUCCESS;
1042 if ( m != NULL
1043 && m->hMainMod != NIL_RTLDRMOD
1044 && m->fUsable)
1045 {
1046 if (m->pReg->pfnVMPowerOn)
1047 {
1048 ComPtr<ExtPack> ptrSelfRef = this;
1049 a_pLock->release();
1050 int vrc = m->pReg->pfnVMPowerOn(m->pReg, a_pConsole, a_pVM);
1051 *a_pvrc = vrc;
1052 a_pLock->acquire();
1053 if (RT_FAILURE(vrc))
1054 LogRel(("ExtPack pfnVMPowerOn returned %Rrc for %s\n", vrc, m->Desc.strName.c_str()));
1055 return true;
1056 }
1057 }
1058 return false;
1059}
1060
1061/**
1062 * Calls the pfnVMPowerOff hook.
1063 *
1064 * @returns true if we left the lock, false if we didn't.
1065 * @param a_pConsole The console interface.
1066 * @param a_pVM The VM handle.
1067 * @param a_pLock The write lock held by the caller.
1068 */
1069bool ExtPack::i_callVmPowerOffHook(IConsole *a_pConsole, PVM a_pVM, AutoWriteLock *a_pLock)
1070{
1071 if ( m != NULL
1072 && m->hMainMod != NIL_RTLDRMOD
1073 && m->fUsable)
1074 {
1075 if (m->pReg->pfnVMPowerOff)
1076 {
1077 ComPtr<ExtPack> ptrSelfRef = this;
1078 a_pLock->release();
1079 m->pReg->pfnVMPowerOff(m->pReg, a_pConsole, a_pVM);
1080 a_pLock->acquire();
1081 return true;
1082 }
1083 }
1084 return false;
1085}
1086#endif /* !VBOX_COM_INPROC */
1087
1088/**
1089 * Check if the extension pack is usable and has an VRDE module.
1090 *
1091 * @returns S_OK or COM error status with error information.
1092 *
1093 * @remarks Caller holds the extension manager lock for reading, no locking
1094 * necessary.
1095 */
1096HRESULT ExtPack::i_checkVrde(void)
1097{
1098 HRESULT hrc;
1099 if ( m != NULL
1100 && m->fUsable)
1101 {
1102 if (m->Desc.strVrdeModule.isNotEmpty())
1103 hrc = S_OK;
1104 else
1105 hrc = setError(E_FAIL, tr("The extension pack '%s' does not include a VRDE module"), m->Desc.strName.c_str());
1106 }
1107 else
1108 hrc = setError(E_FAIL, tr("%s"), m->strWhyUnusable.c_str());
1109 return hrc;
1110}
1111
1112/**
1113 * Same as checkVrde(), except that it also resolves the path to the module.
1114 *
1115 * @returns S_OK or COM error status with error information.
1116 * @param a_pstrVrdeLibrary Where to return the path on success.
1117 *
1118 * @remarks Caller holds the extension manager lock for reading, no locking
1119 * necessary.
1120 */
1121HRESULT ExtPack::i_getVrdpLibraryName(Utf8Str *a_pstrVrdeLibrary)
1122{
1123 HRESULT hrc = i_checkVrde();
1124 if (SUCCEEDED(hrc))
1125 {
1126 if (i_findModule(m->Desc.strVrdeModule.c_str(), NULL, VBOXEXTPACKMODKIND_R3,
1127 a_pstrVrdeLibrary, NULL /*a_pfNative*/, NULL /*a_pObjInfo*/))
1128 hrc = S_OK;
1129 else
1130 hrc = setError(E_FAIL, tr("Failed to locate the VRDE module '%s' in extension pack '%s'"),
1131 m->Desc.strVrdeModule.c_str(), m->Desc.strName.c_str());
1132 }
1133 return hrc;
1134}
1135
1136/**
1137 * Resolves the path to the module.
1138 *
1139 * @returns S_OK or COM error status with error information.
1140 * @param a_pszModuleName The library.
1141 * @param a_pstrLibrary Where to return the path on success.
1142 *
1143 * @remarks Caller holds the extension manager lock for reading, no locking
1144 * necessary.
1145 */
1146HRESULT ExtPack::i_getLibraryName(const char *a_pszModuleName, Utf8Str *a_pstrLibrary)
1147{
1148 HRESULT hrc;
1149 if (i_findModule(a_pszModuleName, NULL, VBOXEXTPACKMODKIND_R3,
1150 a_pstrLibrary, NULL /*a_pfNative*/, NULL /*a_pObjInfo*/))
1151 hrc = S_OK;
1152 else
1153 hrc = setError(E_FAIL, tr("Failed to locate the module '%s' in extension pack '%s'"),
1154 a_pszModuleName, m->Desc.strName.c_str());
1155 return hrc;
1156}
1157
1158/**
1159 * Check if this extension pack wishes to be the default VRDE provider.
1160 *
1161 * @returns @c true if it wants to and it is in a usable state, otherwise
1162 * @c false.
1163 *
1164 * @remarks Caller holds the extension manager lock for reading, no locking
1165 * necessary.
1166 */
1167bool ExtPack::i_wantsToBeDefaultVrde(void) const
1168{
1169 return m->fUsable
1170 && m->Desc.strVrdeModule.isNotEmpty();
1171}
1172
1173/**
1174 * Refreshes the extension pack state.
1175 *
1176 * This is called by the manager so that the on disk changes are picked up.
1177 *
1178 * @returns S_OK or COM error status with error information.
1179 *
1180 * @param a_pfCanDelete Optional can-delete-this-object output indicator.
1181 *
1182 * @remarks Caller holds the extension manager lock for writing.
1183 * @remarks Only called in VBoxSVC.
1184 */
1185HRESULT ExtPack::i_refresh(bool *a_pfCanDelete)
1186{
1187 if (a_pfCanDelete)
1188 *a_pfCanDelete = false;
1189
1190 AutoWriteLock autoLock(this COMMA_LOCKVAL_SRC_POS); /* for the COMGETTERs */
1191
1192 /*
1193 * Has the module been deleted?
1194 */
1195 RTFSOBJINFO ObjInfoExtPack;
1196 int vrc = RTPathQueryInfoEx(m->strExtPackPath.c_str(), &ObjInfoExtPack, RTFSOBJATTRADD_UNIX, RTPATH_F_ON_LINK);
1197 if ( RT_FAILURE(vrc)
1198 || !RTFS_IS_DIRECTORY(ObjInfoExtPack.Attr.fMode))
1199 {
1200 if (a_pfCanDelete)
1201 *a_pfCanDelete = true;
1202 return S_OK;
1203 }
1204
1205 /*
1206 * We've got a directory, so try query file system object info for the
1207 * files we are interested in as well.
1208 */
1209 RTFSOBJINFO ObjInfoDesc;
1210 char szDescFilePath[RTPATH_MAX];
1211 vrc = RTPathJoin(szDescFilePath, sizeof(szDescFilePath), m->strExtPackPath.c_str(), VBOX_EXTPACK_DESCRIPTION_NAME);
1212 if (RT_SUCCESS(vrc))
1213 vrc = RTPathQueryInfoEx(szDescFilePath, &ObjInfoDesc, RTFSOBJATTRADD_UNIX, RTPATH_F_ON_LINK);
1214 if (RT_FAILURE(vrc))
1215 RT_ZERO(ObjInfoDesc);
1216
1217 RTFSOBJINFO ObjInfoMainMod;
1218 if (m->strMainModPath.isNotEmpty())
1219 vrc = RTPathQueryInfoEx(m->strMainModPath.c_str(), &ObjInfoMainMod, RTFSOBJATTRADD_UNIX, RTPATH_F_ON_LINK);
1220 if (m->strMainModPath.isEmpty() || RT_FAILURE(vrc))
1221 RT_ZERO(ObjInfoMainMod);
1222
1223 /*
1224 * If we have a usable module already, just verify that things haven't
1225 * changed since we loaded it.
1226 */
1227 if (m->fUsable)
1228 {
1229 if (m->hMainMod == NIL_RTLDRMOD)
1230 i_probeAndLoad();
1231 else if ( !i_objinfoIsEqual(&ObjInfoDesc, &m->ObjInfoDesc)
1232 || !i_objinfoIsEqual(&ObjInfoMainMod, &m->ObjInfoMainMod)
1233 || !i_objinfoIsEqual(&ObjInfoExtPack, &m->ObjInfoExtPack) )
1234 {
1235 /** @todo not important, so it can wait. */
1236 }
1237 }
1238 /*
1239 * Ok, it is currently not usable. If anything has changed since last time
1240 * reprobe the extension pack.
1241 */
1242 else if ( !i_objinfoIsEqual(&ObjInfoDesc, &m->ObjInfoDesc)
1243 || !i_objinfoIsEqual(&ObjInfoMainMod, &m->ObjInfoMainMod)
1244 || !i_objinfoIsEqual(&ObjInfoExtPack, &m->ObjInfoExtPack) )
1245 i_probeAndLoad();
1246
1247 return S_OK;
1248}
1249
1250/**
1251 * Probes the extension pack, loading the main dll and calling its registration
1252 * entry point.
1253 *
1254 * This updates the state accordingly, the strWhyUnusable and fUnusable members
1255 * being the most important ones.
1256 */
1257void ExtPack::i_probeAndLoad(void)
1258{
1259 m->fUsable = false;
1260 m->fMadeReadyCall = false;
1261
1262 /*
1263 * Query the file system info for the extension pack directory. This and
1264 * all other file system info we save is for the benefit of refresh().
1265 */
1266 int vrc = RTPathQueryInfoEx(m->strExtPackPath.c_str(), &m->ObjInfoExtPack, RTFSOBJATTRADD_UNIX, RTPATH_F_ON_LINK);
1267 if (RT_FAILURE(vrc))
1268 {
1269 m->strWhyUnusable.printf(tr("RTPathQueryInfoEx on '%s' failed: %Rrc"), m->strExtPackPath.c_str(), vrc);
1270 return;
1271 }
1272 if (!RTFS_IS_DIRECTORY(m->ObjInfoExtPack.Attr.fMode))
1273 {
1274 if (RTFS_IS_SYMLINK(m->ObjInfoExtPack.Attr.fMode))
1275 m->strWhyUnusable.printf(tr("'%s' is a symbolic link, this is not allowed"),
1276 m->strExtPackPath.c_str(), vrc);
1277 else if (RTFS_IS_FILE(m->ObjInfoExtPack.Attr.fMode))
1278 m->strWhyUnusable.printf(tr("'%s' is a symbolic file, not a directory"),
1279 m->strExtPackPath.c_str(), vrc);
1280 else
1281 m->strWhyUnusable.printf(tr("'%s' is not a directory (fMode=%#x)"),
1282 m->strExtPackPath.c_str(), m->ObjInfoExtPack.Attr.fMode);
1283 return;
1284 }
1285
1286 RTERRINFOSTATIC ErrInfo;
1287 RTErrInfoInitStatic(&ErrInfo);
1288 vrc = SUPR3HardenedVerifyDir(m->strExtPackPath.c_str(), true /*fRecursive*/, true /*fCheckFiles*/, &ErrInfo.Core);
1289 if (RT_FAILURE(vrc))
1290 {
1291 m->strWhyUnusable.printf(tr("%s (rc=%Rrc)"), ErrInfo.Core.pszMsg, vrc);
1292 return;
1293 }
1294
1295 /*
1296 * Read the description file.
1297 */
1298 RTCString strSavedName(m->Desc.strName);
1299 RTCString *pStrLoadErr = VBoxExtPackLoadDesc(m->strExtPackPath.c_str(), &m->Desc, &m->ObjInfoDesc);
1300 if (pStrLoadErr != NULL)
1301 {
1302 m->strWhyUnusable.printf(tr("Failed to load '%s/%s': %s"),
1303 m->strExtPackPath.c_str(), VBOX_EXTPACK_DESCRIPTION_NAME, pStrLoadErr->c_str());
1304 m->Desc.strName = strSavedName;
1305 delete pStrLoadErr;
1306 return;
1307 }
1308
1309 /*
1310 * Make sure the XML name and directory matches.
1311 */
1312 if (!m->Desc.strName.equalsIgnoreCase(strSavedName))
1313 {
1314 m->strWhyUnusable.printf(tr("The description name ('%s') and directory name ('%s') does not match"),
1315 m->Desc.strName.c_str(), strSavedName.c_str());
1316 m->Desc.strName = strSavedName;
1317 return;
1318 }
1319
1320 /*
1321 * Load the main DLL and call the predefined entry point.
1322 */
1323#ifndef VBOX_COM_INPROC
1324 const char *pszMainModule = m->Desc.strMainModule.c_str();
1325#else
1326 const char *pszMainModule = m->Desc.strMainVMModule.c_str();
1327 if (m->Desc.strMainVMModule.isEmpty())
1328 {
1329 /*
1330 * We're good! The main module for VM processes is optional.
1331 */
1332 m->fUsable = true;
1333 m->strWhyUnusable.setNull();
1334 return;
1335 }
1336#endif
1337 bool fIsNative;
1338 if (!i_findModule(pszMainModule, NULL /* default extension */, VBOXEXTPACKMODKIND_R3,
1339 &m->strMainModPath, &fIsNative, &m->ObjInfoMainMod))
1340 {
1341 m->strWhyUnusable.printf(tr("Failed to locate the main module ('%s')"), pszMainModule);
1342 return;
1343 }
1344
1345 vrc = SUPR3HardenedVerifyPlugIn(m->strMainModPath.c_str(), &ErrInfo.Core);
1346 if (RT_FAILURE(vrc))
1347 {
1348 m->strWhyUnusable.printf(tr("%s"), ErrInfo.Core.pszMsg);
1349 return;
1350 }
1351
1352 if (fIsNative)
1353 {
1354 vrc = SUPR3HardenedLdrLoadPlugIn(m->strMainModPath.c_str(), &m->hMainMod, &ErrInfo.Core);
1355 if (RT_FAILURE(vrc))
1356 {
1357 m->hMainMod = NIL_RTLDRMOD;
1358 m->strWhyUnusable.printf(tr("Failed to load the main module ('%s'): %Rrc - %s"),
1359 m->strMainModPath.c_str(), vrc, ErrInfo.Core.pszMsg);
1360 return;
1361 }
1362 }
1363 else
1364 {
1365 m->strWhyUnusable.printf(tr("Only native main modules are currently supported"));
1366 return;
1367 }
1368
1369 /*
1370 * Resolve the predefined entry point.
1371 */
1372#ifndef VBOX_COM_INPROC
1373 const char *pszMainEntryPoint = VBOX_EXTPACK_MAIN_MOD_ENTRY_POINT;
1374 PFNVBOXEXTPACKREGISTER pfnRegistration;
1375 uint32_t uVersion = VBOXEXTPACKREG_VERSION;
1376#else
1377 const char *pszMainEntryPoint = VBOX_EXTPACK_MAIN_VM_MOD_ENTRY_POINT;
1378 PFNVBOXEXTPACKVMREGISTER pfnRegistration;
1379 uint32_t uVersion = VBOXEXTPACKVMREG_VERSION;
1380#endif
1381 vrc = RTLdrGetSymbol(m->hMainMod, pszMainEntryPoint, (void **)&pfnRegistration);
1382 if (RT_SUCCESS(vrc))
1383 {
1384 RTErrInfoClear(&ErrInfo.Core);
1385 vrc = pfnRegistration(&m->Hlp, &m->pReg, &ErrInfo.Core);
1386 if ( RT_SUCCESS(vrc)
1387 && !RTErrInfoIsSet(&ErrInfo.Core)
1388 && VALID_PTR(m->pReg))
1389 {
1390 if ( VBOXEXTPACK_IS_MAJOR_VER_EQUAL(m->pReg->u32Version, uVersion)
1391 && m->pReg->u32EndMarker == m->pReg->u32Version)
1392 {
1393#ifndef VBOX_COM_INPROC
1394 if ( (!m->pReg->pfnInstalled || RT_VALID_PTR(m->pReg->pfnInstalled))
1395 && (!m->pReg->pfnUninstall || RT_VALID_PTR(m->pReg->pfnUninstall))
1396 && (!m->pReg->pfnVirtualBoxReady || RT_VALID_PTR(m->pReg->pfnVirtualBoxReady))
1397 && (!m->pReg->pfnUnload || RT_VALID_PTR(m->pReg->pfnUnload))
1398 && (!m->pReg->pfnVMCreated || RT_VALID_PTR(m->pReg->pfnVMCreated))
1399 && (!m->pReg->pfnQueryObject || RT_VALID_PTR(m->pReg->pfnQueryObject))
1400 )
1401 {
1402 /*
1403 * We're good!
1404 */
1405 m->fUsable = true;
1406 m->strWhyUnusable.setNull();
1407 return;
1408 }
1409#else
1410 if ( (!m->pReg->pfnConsoleReady || RT_VALID_PTR(m->pReg->pfnConsoleReady))
1411 && (!m->pReg->pfnUnload || RT_VALID_PTR(m->pReg->pfnUnload))
1412 && (!m->pReg->pfnVMConfigureVMM || RT_VALID_PTR(m->pReg->pfnVMConfigureVMM))
1413 && (!m->pReg->pfnVMPowerOn || RT_VALID_PTR(m->pReg->pfnVMPowerOn))
1414 && (!m->pReg->pfnVMPowerOff || RT_VALID_PTR(m->pReg->pfnVMPowerOff))
1415 && (!m->pReg->pfnQueryObject || RT_VALID_PTR(m->pReg->pfnQueryObject))
1416 )
1417 {
1418 /*
1419 * We're good!
1420 */
1421 m->fUsable = true;
1422 m->strWhyUnusable.setNull();
1423 return;
1424 }
1425#endif
1426
1427 m->strWhyUnusable = tr("The registration structure contains one or more invalid function pointers");
1428 }
1429 else
1430 m->strWhyUnusable.printf(tr("Unsupported registration structure version %u.%u"),
1431 RT_HIWORD(m->pReg->u32Version), RT_LOWORD(m->pReg->u32Version));
1432 }
1433 else
1434 m->strWhyUnusable.printf(tr("%s returned %Rrc, pReg=%p ErrInfo='%s'"),
1435 pszMainEntryPoint, vrc, m->pReg, ErrInfo.Core.pszMsg);
1436 m->pReg = NULL;
1437 }
1438 else
1439 m->strWhyUnusable.printf(tr("Failed to resolve exported symbol '%s' in the main module: %Rrc"),
1440 pszMainEntryPoint, vrc);
1441
1442 RTLdrClose(m->hMainMod);
1443 m->hMainMod = NIL_RTLDRMOD;
1444}
1445
1446/**
1447 * Finds a module.
1448 *
1449 * @returns true if found, false if not.
1450 * @param a_pszName The module base name (no extension).
1451 * @param a_pszExt The extension. If NULL we use default
1452 * extensions.
1453 * @param a_enmKind The kind of module to locate.
1454 * @param a_pStrFound Where to return the path to the module we've
1455 * found.
1456 * @param a_pfNative Where to return whether this is a native module
1457 * or an agnostic one. Optional.
1458 * @param a_pObjInfo Where to return the file system object info for
1459 * the module. Optional.
1460 */
1461bool ExtPack::i_findModule(const char *a_pszName, const char *a_pszExt, VBOXEXTPACKMODKIND a_enmKind,
1462 Utf8Str *a_pStrFound, bool *a_pfNative, PRTFSOBJINFO a_pObjInfo) const
1463{
1464 /*
1465 * Try the native path first.
1466 */
1467 char szPath[RTPATH_MAX];
1468 int vrc = RTPathJoin(szPath, sizeof(szPath), m->strExtPackPath.c_str(), RTBldCfgTargetDotArch());
1469 AssertLogRelRCReturn(vrc, false);
1470 vrc = RTPathAppend(szPath, sizeof(szPath), a_pszName);
1471 AssertLogRelRCReturn(vrc, false);
1472 if (!a_pszExt)
1473 {
1474 const char *pszDefExt;
1475 switch (a_enmKind)
1476 {
1477 case VBOXEXTPACKMODKIND_RC: pszDefExt = ".rc"; break;
1478 case VBOXEXTPACKMODKIND_R0: pszDefExt = ".r0"; break;
1479 case VBOXEXTPACKMODKIND_R3: pszDefExt = RTLdrGetSuff(); break;
1480 default:
1481 AssertFailedReturn(false);
1482 }
1483 vrc = RTStrCat(szPath, sizeof(szPath), pszDefExt);
1484 AssertLogRelRCReturn(vrc, false);
1485 }
1486
1487 RTFSOBJINFO ObjInfo;
1488 if (!a_pObjInfo)
1489 a_pObjInfo = &ObjInfo;
1490 vrc = RTPathQueryInfo(szPath, a_pObjInfo, RTFSOBJATTRADD_UNIX);
1491 if (RT_SUCCESS(vrc) && RTFS_IS_FILE(a_pObjInfo->Attr.fMode))
1492 {
1493 if (a_pfNative)
1494 *a_pfNative = true;
1495 *a_pStrFound = szPath;
1496 return true;
1497 }
1498
1499 /*
1500 * Try the platform agnostic modules.
1501 */
1502 /* gcc.x86/module.rel */
1503 char szSubDir[32];
1504 RTStrPrintf(szSubDir, sizeof(szSubDir), "%s.%s", RTBldCfgCompiler(), RTBldCfgTargetArch());
1505 vrc = RTPathJoin(szPath, sizeof(szPath), m->strExtPackPath.c_str(), szSubDir);
1506 AssertLogRelRCReturn(vrc, false);
1507 vrc = RTPathAppend(szPath, sizeof(szPath), a_pszName);
1508 AssertLogRelRCReturn(vrc, false);
1509 if (!a_pszExt)
1510 {
1511 vrc = RTStrCat(szPath, sizeof(szPath), ".rel");
1512 AssertLogRelRCReturn(vrc, false);
1513 }
1514 vrc = RTPathQueryInfo(szPath, a_pObjInfo, RTFSOBJATTRADD_UNIX);
1515 if (RT_SUCCESS(vrc) && RTFS_IS_FILE(a_pObjInfo->Attr.fMode))
1516 {
1517 if (a_pfNative)
1518 *a_pfNative = false;
1519 *a_pStrFound = szPath;
1520 return true;
1521 }
1522
1523 /* x86/module.rel */
1524 vrc = RTPathJoin(szPath, sizeof(szPath), m->strExtPackPath.c_str(), RTBldCfgTargetArch());
1525 AssertLogRelRCReturn(vrc, false);
1526 vrc = RTPathAppend(szPath, sizeof(szPath), a_pszName);
1527 AssertLogRelRCReturn(vrc, false);
1528 if (!a_pszExt)
1529 {
1530 vrc = RTStrCat(szPath, sizeof(szPath), ".rel");
1531 AssertLogRelRCReturn(vrc, false);
1532 }
1533 vrc = RTPathQueryInfo(szPath, a_pObjInfo, RTFSOBJATTRADD_UNIX);
1534 if (RT_SUCCESS(vrc) && RTFS_IS_FILE(a_pObjInfo->Attr.fMode))
1535 {
1536 if (a_pfNative)
1537 *a_pfNative = false;
1538 *a_pStrFound = szPath;
1539 return true;
1540 }
1541
1542 return false;
1543}
1544
1545/**
1546 * Compares two file system object info structures.
1547 *
1548 * @returns true if equal, false if not.
1549 * @param pObjInfo1 The first.
1550 * @param pObjInfo2 The second.
1551 * @todo IPRT should do this, really.
1552 */
1553/* static */ bool ExtPack::i_objinfoIsEqual(PCRTFSOBJINFO pObjInfo1, PCRTFSOBJINFO pObjInfo2)
1554{
1555 if (!RTTimeSpecIsEqual(&pObjInfo1->ModificationTime, &pObjInfo2->ModificationTime))
1556 return false;
1557 if (!RTTimeSpecIsEqual(&pObjInfo1->ChangeTime, &pObjInfo2->ChangeTime))
1558 return false;
1559 if (!RTTimeSpecIsEqual(&pObjInfo1->BirthTime, &pObjInfo2->BirthTime))
1560 return false;
1561 if (pObjInfo1->cbObject != pObjInfo2->cbObject)
1562 return false;
1563 if (pObjInfo1->Attr.fMode != pObjInfo2->Attr.fMode)
1564 return false;
1565 if (pObjInfo1->Attr.enmAdditional == pObjInfo2->Attr.enmAdditional)
1566 {
1567 switch (pObjInfo1->Attr.enmAdditional)
1568 {
1569 case RTFSOBJATTRADD_UNIX:
1570 if (pObjInfo1->Attr.u.Unix.uid != pObjInfo2->Attr.u.Unix.uid)
1571 return false;
1572 if (pObjInfo1->Attr.u.Unix.gid != pObjInfo2->Attr.u.Unix.gid)
1573 return false;
1574 if (pObjInfo1->Attr.u.Unix.INodeIdDevice != pObjInfo2->Attr.u.Unix.INodeIdDevice)
1575 return false;
1576 if (pObjInfo1->Attr.u.Unix.INodeId != pObjInfo2->Attr.u.Unix.INodeId)
1577 return false;
1578 if (pObjInfo1->Attr.u.Unix.GenerationId != pObjInfo2->Attr.u.Unix.GenerationId)
1579 return false;
1580 break;
1581 default:
1582 break;
1583 }
1584 }
1585 return true;
1586}
1587
1588
1589/**
1590 * @interface_method_impl{VBOXEXTPACKHLP,pfnFindModule}
1591 */
1592/*static*/ DECLCALLBACK(int)
1593ExtPack::i_hlpFindModule(PCVBOXEXTPACKHLP pHlp, const char *pszName, const char *pszExt, VBOXEXTPACKMODKIND enmKind,
1594 char *pszFound, size_t cbFound, bool *pfNative)
1595{
1596 /*
1597 * Validate the input and get our bearings.
1598 */
1599 AssertPtrReturn(pszName, VERR_INVALID_POINTER);
1600 AssertPtrNullReturn(pszExt, VERR_INVALID_POINTER);
1601 AssertPtrReturn(pszFound, VERR_INVALID_POINTER);
1602 AssertPtrNullReturn(pfNative, VERR_INVALID_POINTER);
1603 AssertReturn(enmKind > VBOXEXTPACKMODKIND_INVALID && enmKind < VBOXEXTPACKMODKIND_END, VERR_INVALID_PARAMETER);
1604
1605 AssertPtrReturn(pHlp, VERR_INVALID_POINTER);
1606 AssertReturn(pHlp->u32Version == VBOXEXTPACKHLP_VERSION, VERR_INVALID_POINTER);
1607 ExtPack::Data *m = RT_FROM_CPP_MEMBER(pHlp, Data, Hlp);
1608 AssertPtrReturn(m, VERR_INVALID_POINTER);
1609 ExtPack *pThis = m->pThis;
1610 AssertPtrReturn(pThis, VERR_INVALID_POINTER);
1611
1612 /*
1613 * This is just a wrapper around findModule.
1614 */
1615 Utf8Str strFound;
1616 if (pThis->i_findModule(pszName, pszExt, enmKind, &strFound, pfNative, NULL))
1617 return RTStrCopy(pszFound, cbFound, strFound.c_str());
1618 return VERR_FILE_NOT_FOUND;
1619}
1620
1621/*static*/ DECLCALLBACK(int)
1622ExtPack::i_hlpGetFilePath(PCVBOXEXTPACKHLP pHlp, const char *pszFilename, char *pszPath, size_t cbPath)
1623{
1624 /*
1625 * Validate the input and get our bearings.
1626 */
1627 AssertPtrReturn(pszFilename, VERR_INVALID_POINTER);
1628 AssertPtrReturn(pszPath, VERR_INVALID_POINTER);
1629 AssertReturn(cbPath > 0, VERR_BUFFER_OVERFLOW);
1630
1631 AssertPtrReturn(pHlp, VERR_INVALID_POINTER);
1632 AssertReturn(pHlp->u32Version == VBOXEXTPACKHLP_VERSION, VERR_INVALID_POINTER);
1633 ExtPack::Data *m = RT_FROM_CPP_MEMBER(pHlp, Data, Hlp);
1634 AssertPtrReturn(m, VERR_INVALID_POINTER);
1635 ExtPack *pThis = m->pThis;
1636 AssertPtrReturn(pThis, VERR_INVALID_POINTER);
1637
1638 /*
1639 * This is a simple RTPathJoin, no checking if things exists or anything.
1640 */
1641 int vrc = RTPathJoin(pszPath, cbPath, pThis->m->strExtPackPath.c_str(), pszFilename);
1642 if (RT_FAILURE(vrc))
1643 RT_BZERO(pszPath, cbPath);
1644 return vrc;
1645}
1646
1647/*static*/ DECLCALLBACK(VBOXEXTPACKCTX)
1648ExtPack::i_hlpGetContext(PCVBOXEXTPACKHLP pHlp)
1649{
1650 /*
1651 * Validate the input and get our bearings.
1652 */
1653 AssertPtrReturn(pHlp, VBOXEXTPACKCTX_INVALID);
1654 AssertReturn(pHlp->u32Version == VBOXEXTPACKHLP_VERSION, VBOXEXTPACKCTX_INVALID);
1655 ExtPack::Data *m = RT_FROM_CPP_MEMBER(pHlp, Data, Hlp);
1656 AssertPtrReturn(m, VBOXEXTPACKCTX_INVALID);
1657 ExtPack *pThis = m->pThis;
1658 AssertPtrReturn(pThis, VBOXEXTPACKCTX_INVALID);
1659
1660 return pThis->m->enmContext;
1661}
1662
1663/*static*/ DECLCALLBACK(int)
1664ExtPack::i_hlpLoadHGCMService(PCVBOXEXTPACKHLP pHlp, VBOXEXTPACK_IF_CS(IConsole) *pConsole,
1665 const char *pszServiceLibrary, const char *pszServiceName)
1666{
1667#ifdef VBOX_COM_INPROC
1668 /*
1669 * Validate the input and get our bearings.
1670 */
1671 AssertPtrReturn(pszServiceLibrary, VERR_INVALID_POINTER);
1672 AssertPtrReturn(pszServiceName, VERR_INVALID_POINTER);
1673
1674 AssertPtrReturn(pHlp, VERR_INVALID_POINTER);
1675 AssertReturn(pHlp->u32Version == VBOXEXTPACKHLP_VERSION, VERR_INVALID_POINTER);
1676 ExtPack::Data *m = RT_FROM_CPP_MEMBER(pHlp, Data, Hlp);
1677 AssertPtrReturn(m, VERR_INVALID_POINTER);
1678 ExtPack *pThis = m->pThis;
1679 AssertPtrReturn(pThis, VERR_INVALID_POINTER);
1680 AssertPtrReturn(pConsole, VERR_INVALID_POINTER);
1681
1682 Console *pCon = (Console *)pConsole;
1683 return pCon->i_hgcmLoadService(pszServiceLibrary, pszServiceName);
1684#else
1685 NOREF(pHlp); NOREF(pConsole); NOREF(pszServiceLibrary); NOREF(pszServiceName);
1686 return VERR_INVALID_STATE;
1687#endif
1688}
1689
1690/*static*/ DECLCALLBACK(int)
1691ExtPack::i_hlpLoadVDPlugin(PCVBOXEXTPACKHLP pHlp, VBOXEXTPACK_IF_CS(IVirtualBox) *pVirtualBox, const char *pszPluginLibrary)
1692{
1693#ifndef VBOX_COM_INPROC
1694 /*
1695 * Validate the input and get our bearings.
1696 */
1697 AssertPtrReturn(pszPluginLibrary, VERR_INVALID_POINTER);
1698
1699 AssertPtrReturn(pHlp, VERR_INVALID_POINTER);
1700 AssertReturn(pHlp->u32Version == VBOXEXTPACKHLP_VERSION, VERR_INVALID_POINTER);
1701 ExtPack::Data *m = RT_FROM_CPP_MEMBER(pHlp, Data, Hlp);
1702 AssertPtrReturn(m, VERR_INVALID_POINTER);
1703 ExtPack *pThis = m->pThis;
1704 AssertPtrReturn(pThis, VERR_INVALID_POINTER);
1705 AssertPtrReturn(pVirtualBox, VERR_INVALID_POINTER);
1706
1707 VirtualBox *pVBox = (VirtualBox *)pVirtualBox;
1708 return pVBox->i_loadVDPlugin(pszPluginLibrary);
1709#else
1710 NOREF(pHlp); NOREF(pVirtualBox); NOREF(pszPluginLibrary);
1711 return VERR_INVALID_STATE;
1712#endif
1713}
1714
1715/*static*/ DECLCALLBACK(int)
1716ExtPack::i_hlpUnloadVDPlugin(PCVBOXEXTPACKHLP pHlp, VBOXEXTPACK_IF_CS(IVirtualBox) *pVirtualBox, const char *pszPluginLibrary)
1717{
1718#ifndef VBOX_COM_INPROC
1719 /*
1720 * Validate the input and get our bearings.
1721 */
1722 AssertPtrReturn(pszPluginLibrary, VERR_INVALID_POINTER);
1723
1724 AssertPtrReturn(pHlp, VERR_INVALID_POINTER);
1725 AssertReturn(pHlp->u32Version == VBOXEXTPACKHLP_VERSION, VERR_INVALID_POINTER);
1726 ExtPack::Data *m = RT_FROM_CPP_MEMBER(pHlp, Data, Hlp);
1727 AssertPtrReturn(m, VERR_INVALID_POINTER);
1728 ExtPack *pThis = m->pThis;
1729 AssertPtrReturn(pThis, VERR_INVALID_POINTER);
1730 AssertPtrReturn(pVirtualBox, VERR_INVALID_POINTER);
1731
1732 VirtualBox *pVBox = (VirtualBox *)pVirtualBox;
1733 return pVBox->i_unloadVDPlugin(pszPluginLibrary);
1734#else
1735 NOREF(pHlp); NOREF(pVirtualBox); NOREF(pszPluginLibrary);
1736 return VERR_INVALID_STATE;
1737#endif
1738}
1739
1740/*static*/ DECLCALLBACK(uint32_t)
1741ExtPack::i_hlpCreateProgress(PCVBOXEXTPACKHLP pHlp, VBOXEXTPACK_IF_CS(IUnknown) *pInitiator,
1742 const char *pcszDescription, uint32_t cOperations,
1743 uint32_t uTotalOperationsWeight, const char *pcszFirstOperationDescription,
1744 uint32_t uFirstOperationWeight, VBOXEXTPACK_IF_CS(IProgress) **ppProgressOut)
1745{
1746 /*
1747 * Validate the input and get our bearings.
1748 */
1749 AssertPtrReturn(pcszDescription, (uint32_t)E_INVALIDARG);
1750 AssertReturn(cOperations >= 1, (uint32_t)E_INVALIDARG);
1751 AssertReturn(uTotalOperationsWeight >= 1, (uint32_t)E_INVALIDARG);
1752 AssertPtrReturn(pcszFirstOperationDescription, (uint32_t)E_INVALIDARG);
1753 AssertReturn(uFirstOperationWeight >= 1, (uint32_t)E_INVALIDARG);
1754 AssertPtrReturn(ppProgressOut, (uint32_t)E_INVALIDARG);
1755
1756 AssertPtrReturn(pHlp, (uint32_t)E_INVALIDARG);
1757 AssertReturn(pHlp->u32Version == VBOXEXTPACKHLP_VERSION, (uint32_t)E_INVALIDARG);
1758#ifndef VBOX_COM_INPROC
1759 ExtPack::Data *m = RT_FROM_CPP_MEMBER(pHlp, Data, Hlp);
1760#endif
1761
1762 ComObjPtr<Progress> pProgress;
1763 HRESULT hrc = pProgress.createObject();
1764 if (FAILED(hrc))
1765 return hrc;
1766 hrc = pProgress->init(
1767#ifndef VBOX_COM_INPROC
1768 m->pVirtualBox,
1769#endif
1770 pInitiator, pcszDescription, TRUE /* aCancelable */,
1771 cOperations, uTotalOperationsWeight,
1772 pcszFirstOperationDescription, uFirstOperationWeight);
1773 if (FAILED(hrc))
1774 return hrc;
1775
1776 return pProgress.queryInterfaceTo(ppProgressOut);
1777}
1778
1779/*static*/ DECLCALLBACK(uint32_t)
1780ExtPack::i_hlpGetCanceledProgress(PCVBOXEXTPACKHLP pHlp, VBOXEXTPACK_IF_CS(IProgress) *pProgress,
1781 bool *pfCanceled)
1782{
1783 /*
1784 * Validate the input and get our bearings.
1785 */
1786 AssertPtrReturn(pProgress, (uint32_t)E_INVALIDARG);
1787 AssertPtrReturn(pfCanceled, (uint32_t)E_INVALIDARG);
1788
1789 AssertPtrReturn(pHlp, (uint32_t)E_INVALIDARG);
1790 AssertReturn(pHlp->u32Version == VBOXEXTPACKHLP_VERSION, (uint32_t)E_INVALIDARG);
1791
1792 BOOL fCanceled = FALSE;
1793 HRESULT hrc = pProgress->COMGETTER(Canceled)(&fCanceled);
1794 *pfCanceled = !!fCanceled;
1795 return hrc;
1796}
1797
1798/*static*/ DECLCALLBACK(uint32_t)
1799ExtPack::i_hlpUpdateProgress(PCVBOXEXTPACKHLP pHlp, VBOXEXTPACK_IF_CS(IProgress) *pProgress,
1800 uint32_t uPercent)
1801{
1802 /*
1803 * Validate the input and get our bearings.
1804 */
1805 AssertPtrReturn(pProgress, (uint32_t)E_INVALIDARG);
1806 AssertReturn(uPercent <= 100, (uint32_t)E_INVALIDARG);
1807
1808 AssertPtrReturn(pHlp, (uint32_t)E_INVALIDARG);
1809 AssertReturn(pHlp->u32Version == VBOXEXTPACKHLP_VERSION, (uint32_t)E_INVALIDARG);
1810
1811 return pProgress->SetCurrentOperationProgress(uPercent);
1812}
1813
1814/*static*/ DECLCALLBACK(uint32_t)
1815ExtPack::i_hlpNextOperationProgress(PCVBOXEXTPACKHLP pHlp, VBOXEXTPACK_IF_CS(IProgress) *pProgress,
1816 const char *pcszNextOperationDescription,
1817 uint32_t uNextOperationWeight)
1818{
1819 /*
1820 * Validate the input and get our bearings.
1821 */
1822 AssertPtrReturn(pProgress, (uint32_t)E_INVALIDARG);
1823 AssertPtrReturn(pcszNextOperationDescription, (uint32_t)E_INVALIDARG);
1824 AssertReturn(uNextOperationWeight >= 1, (uint32_t)E_INVALIDARG);
1825
1826 AssertPtrReturn(pHlp, (uint32_t)E_INVALIDARG);
1827 AssertReturn(pHlp->u32Version == VBOXEXTPACKHLP_VERSION, (uint32_t)E_INVALIDARG);
1828
1829 return pProgress->SetNextOperation(Bstr(pcszNextOperationDescription).raw(), uNextOperationWeight);
1830}
1831
1832/*static*/ DECLCALLBACK(uint32_t)
1833ExtPack::i_hlpCompleteProgress(PCVBOXEXTPACKHLP pHlp, VBOXEXTPACK_IF_CS(IProgress) *pProgress,
1834 uint32_t uResultCode)
1835{
1836 /*
1837 * Validate the input and get our bearings.
1838 */
1839 AssertPtrReturn(pProgress, (uint32_t)E_INVALIDARG);
1840
1841 AssertPtrReturn(pHlp, (uint32_t)E_INVALIDARG);
1842 AssertReturn(pHlp->u32Version == VBOXEXTPACKHLP_VERSION, (uint32_t)E_INVALIDARG);
1843
1844 Progress *pProgressInt = static_cast<Progress *>(pProgress);
1845 return pProgressInt->i_notifyComplete(uResultCode);
1846}
1847
1848/*static*/ DECLCALLBACK(int)
1849ExtPack::i_hlpReservedN(PCVBOXEXTPACKHLP pHlp)
1850{
1851 /*
1852 * Validate the input and get our bearings.
1853 */
1854 AssertPtrReturn(pHlp, VERR_INVALID_POINTER);
1855 AssertReturn(pHlp->u32Version == VBOXEXTPACKHLP_VERSION, VERR_INVALID_POINTER);
1856 ExtPack::Data *m = RT_FROM_CPP_MEMBER(pHlp, Data, Hlp);
1857 AssertPtrReturn(m, VERR_INVALID_POINTER);
1858 ExtPack *pThis = m->pThis;
1859 AssertPtrReturn(pThis, VERR_INVALID_POINTER);
1860
1861 return VERR_NOT_IMPLEMENTED;
1862}
1863
1864
1865
1866
1867HRESULT ExtPack::getName(com::Utf8Str &aName)
1868{
1869 aName = m->Desc.strName;
1870 return S_OK;
1871}
1872
1873HRESULT ExtPack::getDescription(com::Utf8Str &aDescription)
1874{
1875 aDescription = m->Desc.strDescription;
1876 return S_OK;
1877}
1878
1879HRESULT ExtPack::getVersion(com::Utf8Str &aVersion)
1880{
1881 aVersion = m->Desc.strVersion;
1882 return S_OK;
1883}
1884
1885HRESULT ExtPack::getRevision(ULONG *aRevision)
1886{
1887 *aRevision = m->Desc.uRevision;
1888 return S_OK;
1889}
1890
1891HRESULT ExtPack::getEdition(com::Utf8Str &aEdition)
1892{
1893 aEdition = m->Desc.strEdition;
1894 return S_OK;
1895}
1896
1897HRESULT ExtPack::getVRDEModule(com::Utf8Str &aVRDEModule)
1898{
1899 aVRDEModule = m->Desc.strVrdeModule;
1900 return S_OK;
1901}
1902
1903HRESULT ExtPack::getPlugIns(std::vector<ComPtr<IExtPackPlugIn> > &aPlugIns)
1904{
1905 /** @todo implement plug-ins. */
1906 NOREF(aPlugIns);
1907 ReturnComNotImplemented();
1908}
1909
1910HRESULT ExtPack::getUsable(BOOL *aUsable)
1911{
1912 *aUsable = m->fUsable;
1913 return S_OK;
1914}
1915
1916HRESULT ExtPack::getWhyUnusable(com::Utf8Str &aWhyUnusable)
1917{
1918 aWhyUnusable = m->strWhyUnusable;
1919 return S_OK;
1920}
1921
1922HRESULT ExtPack::getShowLicense(BOOL *aShowLicense)
1923{
1924 *aShowLicense = m->Desc.fShowLicense;
1925 return S_OK;
1926}
1927
1928HRESULT ExtPack::getLicense(com::Utf8Str &aLicense)
1929{
1930 Utf8Str strHtml("html");
1931 Utf8Str str("");
1932 return queryLicense(str, str, strHtml, aLicense);
1933}
1934
1935HRESULT ExtPack::queryLicense(const com::Utf8Str &aPreferredLocale, const com::Utf8Str &aPreferredLanguage,
1936 const com::Utf8Str &aFormat, com::Utf8Str &aLicenseText)
1937{
1938 HRESULT hrc = S_OK;
1939
1940 /*
1941 * Validate input.
1942 */
1943 if (aPreferredLocale.length() != 2 && aPreferredLocale.length() != 0)
1944 return setError(E_FAIL, tr("The preferred locale is a two character string or empty."));
1945
1946 if (aPreferredLanguage.length() != 2 && aPreferredLanguage.length() != 0)
1947 return setError(E_FAIL, tr("The preferred lanuage is a two character string or empty."));
1948
1949 if ( !aFormat.equals("html")
1950 && !aFormat.equals("rtf")
1951 && !aFormat.equals("txt"))
1952 return setError(E_FAIL, tr("The license format can only have the values 'html', 'rtf' and 'txt'."));
1953
1954 /*
1955 * Combine the options to form a file name before locking down anything.
1956 */
1957 char szName[sizeof(VBOX_EXTPACK_LICENSE_NAME_PREFIX "-de_DE.html") + 2];
1958 if (aPreferredLocale.isNotEmpty() && aPreferredLanguage.isNotEmpty())
1959 RTStrPrintf(szName, sizeof(szName), VBOX_EXTPACK_LICENSE_NAME_PREFIX "-%s_%s.%s",
1960 aPreferredLocale.c_str(), aPreferredLanguage.c_str(), aFormat.c_str());
1961 else if (aPreferredLocale.isNotEmpty())
1962 RTStrPrintf(szName, sizeof(szName), VBOX_EXTPACK_LICENSE_NAME_PREFIX "-%s.%s",
1963 aPreferredLocale.c_str(), aFormat.c_str());
1964 else if (aPreferredLanguage.isNotEmpty())
1965 RTStrPrintf(szName, sizeof(szName), VBOX_EXTPACK_LICENSE_NAME_PREFIX "-_%s.%s",
1966 aPreferredLocale.c_str(), aFormat.c_str());
1967 else
1968 RTStrPrintf(szName, sizeof(szName), VBOX_EXTPACK_LICENSE_NAME_PREFIX ".%s",
1969 aFormat.c_str());
1970
1971 /*
1972 * Effectuate the query.
1973 */
1974 AutoReadLock autoLock(this COMMA_LOCKVAL_SRC_POS); /* paranoia */
1975
1976 if (!m->fUsable)
1977 hrc = setError(E_FAIL, tr("%s"), m->strWhyUnusable.c_str());
1978 else
1979 {
1980 char szPath[RTPATH_MAX];
1981 int vrc = RTPathJoin(szPath, sizeof(szPath), m->strExtPackPath.c_str(), szName);
1982 if (RT_SUCCESS(vrc))
1983 {
1984 void *pvFile;
1985 size_t cbFile;
1986 vrc = RTFileReadAllEx(szPath, 0, RTFOFF_MAX, RTFILE_RDALL_O_DENY_READ, &pvFile, &cbFile);
1987 if (RT_SUCCESS(vrc))
1988 {
1989 Bstr bstrLicense((const char *)pvFile, cbFile);
1990 if (bstrLicense.isNotEmpty())
1991 {
1992 aLicenseText = Utf8Str(bstrLicense);
1993 hrc = S_OK;
1994 }
1995 else
1996 hrc = setError(VBOX_E_IPRT_ERROR, tr("The license file '%s' is empty or contains invalid UTF-8 encoding"),
1997 szPath);
1998 RTFileReadAllFree(pvFile, cbFile);
1999 }
2000 else if (vrc == VERR_FILE_NOT_FOUND || vrc == VERR_PATH_NOT_FOUND)
2001 hrc = setErrorBoth(VBOX_E_OBJECT_NOT_FOUND, vrc, tr("The license file '%s' was not found in extension pack '%s'"),
2002 szName, m->Desc.strName.c_str());
2003 else
2004 hrc = setErrorBoth(VBOX_E_FILE_ERROR, vrc, tr("Failed to open the license file '%s': %Rrc"), szPath, vrc);
2005 }
2006 else
2007 hrc = setErrorBoth(VBOX_E_IPRT_ERROR, vrc, tr("RTPathJoin failed: %Rrc"), vrc);
2008 }
2009 return hrc;
2010}
2011
2012HRESULT ExtPack::queryObject(const com::Utf8Str &aObjUuid, ComPtr<IUnknown> &aReturnInterface)
2013{
2014 com::Guid ObjectId;
2015 CheckComArgGuid(aObjUuid, ObjectId);
2016
2017 HRESULT hrc S_OK;
2018
2019 if ( m->pReg
2020 && m->pReg->pfnQueryObject)
2021 {
2022 void *pvUnknown = m->pReg->pfnQueryObject(m->pReg, ObjectId.raw());
2023 if (pvUnknown)
2024 aReturnInterface = (IUnknown *)pvUnknown;
2025 else
2026 hrc = E_NOINTERFACE;
2027 }
2028 else
2029 hrc = E_NOINTERFACE;
2030 return hrc;
2031}
2032
2033DEFINE_EMPTY_CTOR_DTOR(ExtPackManager)
2034
2035/**
2036 * Called by ComObjPtr::createObject when creating the object.
2037 *
2038 * Just initialize the basic object state, do the rest in init().
2039 *
2040 * @returns S_OK.
2041 */
2042HRESULT ExtPackManager::FinalConstruct()
2043{
2044 m = NULL;
2045 return BaseFinalConstruct();
2046}
2047
2048/**
2049 * Initializes the extension pack manager.
2050 *
2051 * @returns COM status code.
2052 * @param a_pVirtualBox Pointer to the VirtualBox object.
2053 * @param a_enmContext The context we're in.
2054 */
2055HRESULT ExtPackManager::initExtPackManager(VirtualBox *a_pVirtualBox, VBOXEXTPACKCTX a_enmContext)
2056{
2057 AutoInitSpan autoInitSpan(this);
2058 AssertReturn(autoInitSpan.isOk(), E_FAIL);
2059
2060 /*
2061 * Figure some stuff out before creating the instance data.
2062 */
2063 char szBaseDir[RTPATH_MAX];
2064 int rc = RTPathAppPrivateArchTop(szBaseDir, sizeof(szBaseDir));
2065 AssertLogRelRCReturn(rc, E_FAIL);
2066 rc = RTPathAppend(szBaseDir, sizeof(szBaseDir), VBOX_EXTPACK_INSTALL_DIR);
2067 AssertLogRelRCReturn(rc, E_FAIL);
2068
2069 char szCertificatDir[RTPATH_MAX];
2070 rc = RTPathAppPrivateNoArch(szCertificatDir, sizeof(szCertificatDir));
2071 AssertLogRelRCReturn(rc, E_FAIL);
2072 rc = RTPathAppend(szCertificatDir, sizeof(szCertificatDir), VBOX_EXTPACK_CERT_DIR);
2073 AssertLogRelRCReturn(rc, E_FAIL);
2074
2075 /*
2076 * Allocate and initialize the instance data.
2077 */
2078 m = new Data;
2079 m->strBaseDir = szBaseDir;
2080 m->strCertificatDirPath = szCertificatDir;
2081 m->enmContext = a_enmContext;
2082#ifndef VBOX_COM_INPROC
2083 m->pVirtualBox = a_pVirtualBox;
2084#else
2085 RT_NOREF_PV(a_pVirtualBox);
2086#endif
2087
2088 /*
2089 * Go looking for extensions. The RTDirOpen may fail if nothing has been
2090 * installed yet, or if root is paranoid and has revoked our access to them.
2091 *
2092 * We ASSUME that there are no files, directories or stuff in the directory
2093 * that exceed the max name length in RTDIRENTRYEX.
2094 */
2095 HRESULT hrc = S_OK;
2096 RTDIR hDir;
2097 int vrc = RTDirOpen(&hDir, szBaseDir);
2098 if (RT_SUCCESS(vrc))
2099 {
2100 for (;;)
2101 {
2102 RTDIRENTRYEX Entry;
2103 vrc = RTDirReadEx(hDir, &Entry, NULL /*pcbDirEntry*/, RTFSOBJATTRADD_NOTHING, RTPATH_F_ON_LINK);
2104 if (RT_FAILURE(vrc))
2105 {
2106 AssertLogRelMsg(vrc == VERR_NO_MORE_FILES, ("%Rrc\n", vrc));
2107 break;
2108 }
2109 if ( RTFS_IS_DIRECTORY(Entry.Info.Attr.fMode)
2110 && strcmp(Entry.szName, ".") != 0
2111 && strcmp(Entry.szName, "..") != 0
2112 && VBoxExtPackIsValidMangledName(Entry.szName) )
2113 {
2114 /*
2115 * All directories are extensions, the shall be nothing but
2116 * extensions in this subdirectory.
2117 */
2118 char szExtPackDir[RTPATH_MAX];
2119 vrc = RTPathJoin(szExtPackDir, sizeof(szExtPackDir), m->strBaseDir.c_str(), Entry.szName);
2120 AssertLogRelRC(vrc);
2121 if (RT_SUCCESS(vrc))
2122 {
2123 RTCString *pstrName = VBoxExtPackUnmangleName(Entry.szName, RTSTR_MAX);
2124 AssertLogRel(pstrName);
2125 if (pstrName)
2126 {
2127 ComObjPtr<ExtPack> NewExtPack;
2128 HRESULT hrc2 = NewExtPack.createObject();
2129 if (SUCCEEDED(hrc2))
2130 hrc2 = NewExtPack->initWithDir(a_pVirtualBox, a_enmContext, pstrName->c_str(), szExtPackDir);
2131 delete pstrName;
2132 if (SUCCEEDED(hrc2))
2133 {
2134 m->llInstalledExtPacks.push_back(NewExtPack);
2135 /* Paranoia, there should be no API clients before this method is finished. */
2136
2137 m->cUpdate++;
2138 }
2139 else if (SUCCEEDED(rc))
2140 hrc = hrc2;
2141 }
2142 else
2143 hrc = E_UNEXPECTED;
2144 }
2145 else
2146 hrc = E_UNEXPECTED;
2147 }
2148 }
2149 RTDirClose(hDir);
2150 }
2151 /* else: ignore, the directory probably does not exist or something. */
2152
2153 if (SUCCEEDED(hrc))
2154 autoInitSpan.setSucceeded();
2155 return hrc;
2156}
2157
2158/**
2159 * COM cruft.
2160 */
2161void ExtPackManager::FinalRelease()
2162{
2163 uninit();
2164 BaseFinalRelease();
2165}
2166
2167/**
2168 * Do the actual cleanup.
2169 */
2170void ExtPackManager::uninit()
2171{
2172 /* Enclose the state transition Ready->InUninit->NotReady */
2173 AutoUninitSpan autoUninitSpan(this);
2174 if (!autoUninitSpan.uninitDone() && m != NULL)
2175 {
2176 delete m;
2177 m = NULL;
2178 }
2179}
2180
2181HRESULT ExtPackManager::getInstalledExtPacks(std::vector<ComPtr<IExtPack> > &aInstalledExtPacks)
2182{
2183 Assert(m->enmContext == VBOXEXTPACKCTX_PER_USER_DAEMON);
2184
2185 AutoReadLock autoLock(this COMMA_LOCKVAL_SRC_POS);
2186
2187
2188 SafeIfaceArray<IExtPack> SaExtPacks(m->llInstalledExtPacks);
2189 aInstalledExtPacks.resize(SaExtPacks.size());
2190 for(size_t i = 0; i < SaExtPacks.size(); ++i)
2191 aInstalledExtPacks[i] = SaExtPacks[i];
2192
2193 return S_OK;
2194}
2195
2196HRESULT ExtPackManager::find(const com::Utf8Str &aName, ComPtr<IExtPack> &aReturnData)
2197{
2198 HRESULT hrc = S_OK;
2199
2200 Assert(m->enmContext == VBOXEXTPACKCTX_PER_USER_DAEMON);
2201
2202 AutoReadLock autoLock(this COMMA_LOCKVAL_SRC_POS);
2203
2204 ComPtr<ExtPack> ptrExtPack = i_findExtPack(aName.c_str());
2205 if (!ptrExtPack.isNull())
2206 ptrExtPack.queryInterfaceTo(aReturnData.asOutParam());
2207 else
2208 hrc = VBOX_E_OBJECT_NOT_FOUND;
2209
2210 return hrc;
2211}
2212
2213HRESULT ExtPackManager::openExtPackFile(const com::Utf8Str &aPath, ComPtr<IExtPackFile> &aFile)
2214{
2215 AssertReturn(m->enmContext == VBOXEXTPACKCTX_PER_USER_DAEMON, E_UNEXPECTED);
2216
2217#ifndef VBOX_COM_INPROC
2218 /* The API can optionally take a ::SHA-256=<hex-digest> attribute at the
2219 end of the file name. This is just a temporary measure for
2220 backporting, in 4.2 we'll add another parameter to the method. */
2221 Utf8Str strTarball;
2222 Utf8Str strDigest;
2223 size_t offSha256 = aPath.find("::SHA-256=");
2224 if (offSha256 == Utf8Str::npos)
2225 strTarball = aPath;
2226 else
2227 {
2228 strTarball = aPath.substr(0, offSha256);
2229 strDigest = aPath.substr(offSha256 + sizeof("::SHA-256=") - 1);
2230 }
2231
2232 ComObjPtr<ExtPackFile> NewExtPackFile;
2233 HRESULT hrc = NewExtPackFile.createObject();
2234 if (SUCCEEDED(hrc))
2235 hrc = NewExtPackFile->initWithFile(strTarball.c_str(), strDigest.c_str(), this, m->pVirtualBox);
2236 if (SUCCEEDED(hrc))
2237 NewExtPackFile.queryInterfaceTo(aFile.asOutParam());
2238
2239 return hrc;
2240#else
2241 RT_NOREF(aPath, aFile);
2242 return E_NOTIMPL;
2243#endif
2244}
2245
2246HRESULT ExtPackManager::uninstall(const com::Utf8Str &aName, BOOL aForcedRemoval,
2247 const com::Utf8Str &aDisplayInfo, ComPtr<IProgress> &aProgress)
2248{
2249 Assert(m->enmContext == VBOXEXTPACKCTX_PER_USER_DAEMON);
2250
2251#ifndef VBOX_COM_INPROC
2252
2253 HRESULT hrc;
2254 ExtPackUninstallTask *pTask = NULL;
2255 try
2256 {
2257 pTask = new ExtPackUninstallTask();
2258 hrc = pTask->Init(this, aName, aForcedRemoval != FALSE, aDisplayInfo);
2259 if (SUCCEEDED(hrc))
2260 {
2261 ComPtr<Progress> ptrProgress = pTask->ptrProgress;
2262 hrc = pTask->createThreadWithType(RTTHREADTYPE_DEFAULT);
2263 pTask = NULL; /* always consumed by createThread */
2264 if (SUCCEEDED(hrc))
2265 hrc = ptrProgress.queryInterfaceTo(aProgress.asOutParam());
2266 else
2267 hrc = setError(VBOX_E_IPRT_ERROR,
2268 tr("Starting thread for an extension pack uninstallation failed with %Rrc"), hrc);
2269 }
2270 else
2271 hrc = setError(hrc, tr("Looks like creating a progress object for ExtraPackUninstallTask object failed"));
2272 }
2273 catch (std::bad_alloc &)
2274 {
2275 hrc = E_OUTOFMEMORY;
2276 }
2277 catch (HRESULT hrcXcpt)
2278 {
2279 LogFlowThisFunc(("Exception was caught in the function ExtPackManager::uninstall()\n"));
2280 hrc = hrcXcpt;
2281 }
2282 if (pTask)
2283 delete pTask;
2284 return hrc;
2285#else
2286 RT_NOREF(aName, aForcedRemoval, aDisplayInfo, aProgress);
2287 return E_NOTIMPL;
2288#endif
2289}
2290
2291HRESULT ExtPackManager::cleanup(void)
2292{
2293 Assert(m->enmContext == VBOXEXTPACKCTX_PER_USER_DAEMON);
2294
2295 AutoCaller autoCaller(this);
2296 HRESULT hrc = autoCaller.rc();
2297 if (SUCCEEDED(hrc))
2298 {
2299 /*
2300 * Run the set-uid-to-root binary that performs the cleanup.
2301 *
2302 * Take the write lock to prevent conflicts with other calls to this
2303 * VBoxSVC instance.
2304 */
2305 AutoWriteLock autoLock(this COMMA_LOCKVAL_SRC_POS);
2306 hrc = i_runSetUidToRootHelper(NULL,
2307 "cleanup",
2308 "--base-dir", m->strBaseDir.c_str(),
2309 (const char *)NULL);
2310 }
2311
2312 return hrc;
2313}
2314
2315HRESULT ExtPackManager::queryAllPlugInsForFrontend(const com::Utf8Str &aFrontendName, std::vector<com::Utf8Str> &aPlugInModules)
2316{
2317 NOREF(aFrontendName);
2318 aPlugInModules.resize(0);
2319 return S_OK;
2320}
2321
2322HRESULT ExtPackManager::isExtPackUsable(const com::Utf8Str &aName, BOOL *aUsable)
2323{
2324 *aUsable = i_isExtPackUsable(aName.c_str());
2325 return S_OK;
2326}
2327
2328/**
2329 * Finds the success indicator string in the stderr output ofr hte helper app.
2330 *
2331 * @returns Pointer to the indicator.
2332 * @param psz The stderr output string. Can be NULL.
2333 * @param cch The size of the string.
2334 */
2335static char *findSuccessIndicator(char *psz, size_t cch)
2336{
2337 static const char s_szSuccessInd[] = "rcExit=RTEXITCODE_SUCCESS";
2338 Assert(!cch || strlen(psz) == cch);
2339 if (cch < sizeof(s_szSuccessInd) - 1)
2340 return NULL;
2341 char *pszInd = &psz[cch - sizeof(s_szSuccessInd) + 1];
2342 if (strcmp(s_szSuccessInd, pszInd))
2343 return NULL;
2344 return pszInd;
2345}
2346
2347/**
2348 * Runs the helper application that does the privileged operations.
2349 *
2350 * @returns S_OK or a failure status with error information set.
2351 * @param a_pstrDisplayInfo Platform specific display info hacks.
2352 * @param a_pszCommand The command to execute.
2353 * @param ... The argument strings that goes along with the
2354 * command. Maximum is about 16. Terminated by a
2355 * NULL.
2356 */
2357HRESULT ExtPackManager::i_runSetUidToRootHelper(Utf8Str const *a_pstrDisplayInfo, const char *a_pszCommand, ...)
2358{
2359 /*
2360 * Calculate the path to the helper application.
2361 */
2362 char szExecName[RTPATH_MAX];
2363 int vrc = RTPathAppPrivateArch(szExecName, sizeof(szExecName));
2364 AssertLogRelRCReturn(vrc, E_UNEXPECTED);
2365
2366 vrc = RTPathAppend(szExecName, sizeof(szExecName), VBOX_EXTPACK_HELPER_NAME);
2367 AssertLogRelRCReturn(vrc, E_UNEXPECTED);
2368
2369 /*
2370 * Convert the variable argument list to a RTProcCreate argument vector.
2371 */
2372 const char *apszArgs[20];
2373 unsigned cArgs = 0;
2374
2375 LogRel(("ExtPack: Executing '%s'", szExecName));
2376 apszArgs[cArgs++] = &szExecName[0];
2377
2378 if ( a_pstrDisplayInfo
2379 && a_pstrDisplayInfo->isNotEmpty())
2380 {
2381 LogRel((" '--display-info-hack' '%s'", a_pstrDisplayInfo->c_str()));
2382 apszArgs[cArgs++] = "--display-info-hack";
2383 apszArgs[cArgs++] = a_pstrDisplayInfo->c_str();
2384 }
2385
2386 LogRel((" '%s'", a_pszCommand));
2387 apszArgs[cArgs++] = a_pszCommand;
2388
2389 va_list va;
2390 va_start(va, a_pszCommand);
2391 const char *pszLastArg;
2392 for (;;)
2393 {
2394 AssertReturn(cArgs < RT_ELEMENTS(apszArgs) - 1, E_UNEXPECTED);
2395 pszLastArg = va_arg(va, const char *);
2396 if (!pszLastArg)
2397 break;
2398 LogRel((" '%s'", pszLastArg));
2399 apszArgs[cArgs++] = pszLastArg;
2400 };
2401 va_end(va);
2402
2403 LogRel(("\n"));
2404 apszArgs[cArgs] = NULL;
2405
2406 /*
2407 * Create a PIPE which we attach to stderr so that we can read the error
2408 * message on failure and report it back to the caller.
2409 */
2410 RTPIPE hPipeR;
2411 RTHANDLE hStdErrPipe;
2412 hStdErrPipe.enmType = RTHANDLETYPE_PIPE;
2413 vrc = RTPipeCreate(&hPipeR, &hStdErrPipe.u.hPipe, RTPIPE_C_INHERIT_WRITE);
2414 AssertLogRelRCReturn(vrc, E_UNEXPECTED);
2415
2416 /*
2417 * Spawn the process.
2418 */
2419 HRESULT hrc;
2420 RTPROCESS hProcess;
2421 vrc = RTProcCreateEx(szExecName,
2422 apszArgs,
2423 RTENV_DEFAULT,
2424 0 /*fFlags*/,
2425 NULL /*phStdIn*/,
2426 NULL /*phStdOut*/,
2427 &hStdErrPipe,
2428 NULL /*pszAsUser*/,
2429 NULL /*pszPassword*/,
2430 &hProcess);
2431 if (RT_SUCCESS(vrc))
2432 {
2433 vrc = RTPipeClose(hStdErrPipe.u.hPipe);
2434 hStdErrPipe.u.hPipe = NIL_RTPIPE;
2435
2436 /*
2437 * Read the pipe output until the process completes.
2438 */
2439 RTPROCSTATUS ProcStatus = { -42, RTPROCEXITREASON_ABEND };
2440 size_t cbStdErrBuf = 0;
2441 size_t offStdErrBuf = 0;
2442 char *pszStdErrBuf = NULL;
2443 do
2444 {
2445 /*
2446 * Service the pipe. Block waiting for output or the pipe breaking
2447 * when the process terminates.
2448 */
2449 if (hPipeR != NIL_RTPIPE)
2450 {
2451 char achBuf[1024];
2452 size_t cbRead;
2453 vrc = RTPipeReadBlocking(hPipeR, achBuf, sizeof(achBuf), &cbRead);
2454 if (RT_SUCCESS(vrc))
2455 {
2456 /* grow the buffer? */
2457 size_t cbBufReq = offStdErrBuf + cbRead + 1;
2458 if ( cbBufReq > cbStdErrBuf
2459 && cbBufReq < _256K)
2460 {
2461 size_t cbNew = RT_ALIGN_Z(cbBufReq, 16); // 1024
2462 void *pvNew = RTMemRealloc(pszStdErrBuf, cbNew);
2463 if (pvNew)
2464 {
2465 pszStdErrBuf = (char *)pvNew;
2466 cbStdErrBuf = cbNew;
2467 }
2468 }
2469
2470 /* append if we've got room. */
2471 if (cbBufReq <= cbStdErrBuf)
2472 {
2473 memcpy(&pszStdErrBuf[offStdErrBuf], achBuf, cbRead);
2474 offStdErrBuf = offStdErrBuf + cbRead;
2475 pszStdErrBuf[offStdErrBuf] = '\0';
2476 }
2477 }
2478 else
2479 {
2480 AssertLogRelMsg(vrc == VERR_BROKEN_PIPE, ("%Rrc\n", vrc));
2481 RTPipeClose(hPipeR);
2482 hPipeR = NIL_RTPIPE;
2483 }
2484 }
2485
2486 /*
2487 * Service the process. Block if we have no pipe.
2488 */
2489 if (hProcess != NIL_RTPROCESS)
2490 {
2491 vrc = RTProcWait(hProcess,
2492 hPipeR == NIL_RTPIPE ? RTPROCWAIT_FLAGS_BLOCK : RTPROCWAIT_FLAGS_NOBLOCK,
2493 &ProcStatus);
2494 if (RT_SUCCESS(vrc))
2495 hProcess = NIL_RTPROCESS;
2496 else
2497 AssertLogRelMsgStmt(vrc == VERR_PROCESS_RUNNING, ("%Rrc\n", vrc), hProcess = NIL_RTPROCESS);
2498 }
2499 } while ( hPipeR != NIL_RTPIPE
2500 || hProcess != NIL_RTPROCESS);
2501
2502 LogRel(("ExtPack: enmReason=%d iStatus=%d stderr='%s'\n",
2503 ProcStatus.enmReason, ProcStatus.iStatus, offStdErrBuf ? pszStdErrBuf : ""));
2504
2505 /*
2506 * Look for rcExit=RTEXITCODE_SUCCESS at the end of the error output,
2507 * cut it as it is only there to attest the success.
2508 */
2509 if (offStdErrBuf > 0)
2510 {
2511 RTStrStripR(pszStdErrBuf);
2512 offStdErrBuf = strlen(pszStdErrBuf);
2513 }
2514
2515 char *pszSuccessInd = findSuccessIndicator(pszStdErrBuf, offStdErrBuf);
2516 if (pszSuccessInd)
2517 {
2518 *pszSuccessInd = '\0';
2519 offStdErrBuf = pszSuccessInd - pszStdErrBuf;
2520 }
2521 else if ( ProcStatus.enmReason == RTPROCEXITREASON_NORMAL
2522 && ProcStatus.iStatus == 0)
2523 ProcStatus.iStatus = offStdErrBuf ? 667 : 666;
2524
2525 /*
2526 * Compose the status code and, on failure, error message.
2527 */
2528 if ( ProcStatus.enmReason == RTPROCEXITREASON_NORMAL
2529 && ProcStatus.iStatus == 0)
2530 hrc = S_OK;
2531 else if (ProcStatus.enmReason == RTPROCEXITREASON_NORMAL)
2532 {
2533 AssertMsg(ProcStatus.iStatus != 0, ("%s\n", pszStdErrBuf));
2534 hrc = setError(E_FAIL, tr("The installer failed with exit code %d: %s"),
2535 ProcStatus.iStatus, offStdErrBuf ? pszStdErrBuf : "");
2536 }
2537 else if (ProcStatus.enmReason == RTPROCEXITREASON_SIGNAL)
2538 hrc = setError(E_UNEXPECTED, tr("The installer was killed by signal #d (stderr: %s)"),
2539 ProcStatus.iStatus, offStdErrBuf ? pszStdErrBuf : "");
2540 else if (ProcStatus.enmReason == RTPROCEXITREASON_ABEND)
2541 hrc = setError(E_UNEXPECTED, tr("The installer aborted abnormally (stderr: %s)"),
2542 offStdErrBuf ? pszStdErrBuf : "");
2543 else
2544 hrc = setError(E_UNEXPECTED, tr("internal error: enmReason=%d iStatus=%d stderr='%s'"),
2545 ProcStatus.enmReason, ProcStatus.iStatus, offStdErrBuf ? pszStdErrBuf : "");
2546
2547 RTMemFree(pszStdErrBuf);
2548 }
2549 else
2550 hrc = setErrorBoth(VBOX_E_IPRT_ERROR, vrc, tr("Failed to launch the helper application '%s' (%Rrc)"), szExecName, vrc);
2551
2552 RTPipeClose(hPipeR);
2553 RTPipeClose(hStdErrPipe.u.hPipe);
2554
2555 return hrc;
2556}
2557
2558/**
2559 * Finds an installed extension pack.
2560 *
2561 * @returns Pointer to the extension pack if found, NULL if not. (No reference
2562 * counting problem here since the caller must be holding the lock.)
2563 * @param a_pszName The name of the extension pack.
2564 */
2565ExtPack *ExtPackManager::i_findExtPack(const char *a_pszName)
2566{
2567 size_t cchName = strlen(a_pszName);
2568
2569 for (ExtPackList::iterator it = m->llInstalledExtPacks.begin();
2570 it != m->llInstalledExtPacks.end();
2571 ++it)
2572 {
2573 ExtPack::Data *pExtPackData = (*it)->m;
2574 if ( pExtPackData
2575 && pExtPackData->Desc.strName.length() == cchName
2576 && pExtPackData->Desc.strName.equalsIgnoreCase(a_pszName))
2577 return (*it);
2578 }
2579 return NULL;
2580}
2581
2582/**
2583 * Removes an installed extension pack from the internal list.
2584 *
2585 * The package is expected to exist!
2586 *
2587 * @param a_pszName The name of the extension pack.
2588 */
2589void ExtPackManager::i_removeExtPack(const char *a_pszName)
2590{
2591 size_t cchName = strlen(a_pszName);
2592
2593 for (ExtPackList::iterator it = m->llInstalledExtPacks.begin();
2594 it != m->llInstalledExtPacks.end();
2595 ++it)
2596 {
2597 ExtPack::Data *pExtPackData = (*it)->m;
2598 if ( pExtPackData
2599 && pExtPackData->Desc.strName.length() == cchName
2600 && pExtPackData->Desc.strName.equalsIgnoreCase(a_pszName))
2601 {
2602 m->llInstalledExtPacks.erase(it);
2603 m->cUpdate++;
2604 return;
2605 }
2606 }
2607 AssertMsgFailed(("%s\n", a_pszName));
2608}
2609
2610#ifndef VBOX_COM_INPROC
2611
2612/**
2613 * Refreshes the specified extension pack.
2614 *
2615 * This may remove the extension pack from the list, so any non-smart pointers
2616 * to the extension pack object may become invalid.
2617 *
2618 * @returns S_OK and *a_ppExtPack on success, COM status code and error
2619 * message on failure. Note that *a_ppExtPack can be NULL.
2620 *
2621 * @param a_pszName The extension to update..
2622 * @param a_fUnusableIsError If @c true, report an unusable extension pack
2623 * as an error.
2624 * @param a_ppExtPack Where to store the pointer to the extension
2625 * pack of it is still around after the refresh.
2626 * This is optional.
2627 *
2628 * @remarks Caller holds the extension manager lock.
2629 * @remarks Only called in VBoxSVC.
2630 */
2631HRESULT ExtPackManager::i_refreshExtPack(const char *a_pszName, bool a_fUnusableIsError, ExtPack **a_ppExtPack)
2632{
2633 Assert(m->pVirtualBox != NULL); /* Only called from VBoxSVC. */
2634
2635 HRESULT hrc;
2636 ExtPack *pExtPack = i_findExtPack(a_pszName);
2637 if (pExtPack)
2638 {
2639 /*
2640 * Refresh existing object.
2641 */
2642 bool fCanDelete;
2643 hrc = pExtPack->i_refresh(&fCanDelete);
2644 if (SUCCEEDED(hrc))
2645 {
2646 if (fCanDelete)
2647 {
2648 i_removeExtPack(a_pszName);
2649 pExtPack = NULL;
2650 }
2651 }
2652 }
2653 else
2654 {
2655 /*
2656 * Do this check here, otherwise VBoxExtPackCalcDir() will fail with a strange
2657 * error.
2658 */
2659 bool fValid = VBoxExtPackIsValidName(a_pszName);
2660 if (!fValid)
2661 return setError(E_FAIL, "Invalid extension pack name specified");
2662
2663 /*
2664 * Does the dir exist? Make some special effort to deal with case
2665 * sensitivie file systems (a_pszName is case insensitive and mangled).
2666 */
2667 char szDir[RTPATH_MAX];
2668 int vrc = VBoxExtPackCalcDir(szDir, sizeof(szDir), m->strBaseDir.c_str(), a_pszName);
2669 AssertLogRelRCReturn(vrc, E_FAIL);
2670
2671 RTDIRENTRYEX Entry;
2672 RTFSOBJINFO ObjInfo;
2673 vrc = RTPathQueryInfoEx(szDir, &ObjInfo, RTFSOBJATTRADD_NOTHING, RTPATH_F_ON_LINK);
2674 bool fExists = RT_SUCCESS(vrc) && RTFS_IS_DIRECTORY(ObjInfo.Attr.fMode);
2675 if (!fExists)
2676 {
2677 RTDIR hDir;
2678 vrc = RTDirOpen(&hDir, m->strBaseDir.c_str());
2679 if (RT_SUCCESS(vrc))
2680 {
2681 const char *pszMangledName = RTPathFilename(szDir);
2682 for (;;)
2683 {
2684 vrc = RTDirReadEx(hDir, &Entry, NULL /*pcbDirEntry*/, RTFSOBJATTRADD_NOTHING, RTPATH_F_ON_LINK);
2685 if (RT_FAILURE(vrc))
2686 {
2687 AssertLogRelMsg(vrc == VERR_NO_MORE_FILES, ("%Rrc\n", vrc));
2688 break;
2689 }
2690 if ( RTFS_IS_DIRECTORY(Entry.Info.Attr.fMode)
2691 && !RTStrICmp(Entry.szName, pszMangledName))
2692 {
2693 /*
2694 * The installed extension pack has a uses different case.
2695 * Update the name and directory variables.
2696 */
2697 vrc = RTPathJoin(szDir, sizeof(szDir), m->strBaseDir.c_str(), Entry.szName); /* not really necessary */
2698 AssertLogRelRCReturnStmt(vrc, RTDirClose(hDir), E_UNEXPECTED);
2699 a_pszName = Entry.szName;
2700 fExists = true;
2701 break;
2702 }
2703 }
2704 RTDirClose(hDir);
2705 }
2706 }
2707 if (fExists)
2708 {
2709 /*
2710 * We've got something, create a new extension pack object for it.
2711 */
2712 ComObjPtr<ExtPack> ptrNewExtPack;
2713 hrc = ptrNewExtPack.createObject();
2714 if (SUCCEEDED(hrc))
2715 hrc = ptrNewExtPack->initWithDir(m->pVirtualBox, m->enmContext, a_pszName, szDir);
2716 if (SUCCEEDED(hrc))
2717 {
2718 m->llInstalledExtPacks.push_back(ptrNewExtPack);
2719 m->cUpdate++;
2720 if (ptrNewExtPack->m->fUsable)
2721 LogRel(("ExtPackManager: Found extension pack '%s'.\n", a_pszName));
2722 else
2723 LogRel(("ExtPackManager: Found bad extension pack '%s': %s\n",
2724 a_pszName, ptrNewExtPack->m->strWhyUnusable.c_str() ));
2725 pExtPack = ptrNewExtPack;
2726 }
2727 }
2728 else
2729 hrc = S_OK;
2730 }
2731
2732 /*
2733 * Report error if not usable, if that is desired.
2734 */
2735 if ( SUCCEEDED(hrc)
2736 && pExtPack
2737 && a_fUnusableIsError
2738 && !pExtPack->m->fUsable)
2739 hrc = setError(E_FAIL, "%s", pExtPack->m->strWhyUnusable.c_str());
2740
2741 if (a_ppExtPack)
2742 *a_ppExtPack = pExtPack;
2743 return hrc;
2744}
2745
2746/**
2747 * Checks if there are any running VMs.
2748 *
2749 * This is called when uninstalling or replacing an extension pack.
2750 *
2751 * @returns true / false
2752 */
2753bool ExtPackManager::i_areThereAnyRunningVMs(void) const
2754{
2755 Assert(m->pVirtualBox != NULL); /* Only called from VBoxSVC. */
2756
2757 /*
2758 * Get list of machines and their states.
2759 */
2760 com::SafeIfaceArray<IMachine> SaMachines;
2761 HRESULT hrc = m->pVirtualBox->COMGETTER(Machines)(ComSafeArrayAsOutParam(SaMachines));
2762 if (SUCCEEDED(hrc))
2763 {
2764 com::SafeArray<MachineState_T> SaStates;
2765 hrc = m->pVirtualBox->GetMachineStates(ComSafeArrayAsInParam(SaMachines), ComSafeArrayAsOutParam(SaStates));
2766 if (SUCCEEDED(hrc))
2767 {
2768 /*
2769 * Scan the two parallel arrays for machines in the running state.
2770 */
2771 Assert(SaStates.size() == SaMachines.size());
2772 for (size_t i = 0; i < SaMachines.size(); ++i)
2773 if (SaMachines[i] && Global::IsOnline(SaStates[i]))
2774 return true;
2775 }
2776 }
2777 return false;
2778}
2779
2780/**
2781 * Worker for IExtPackFile::Install.
2782 *
2783 * Called on a worker thread via doInstallThreadProc.
2784 *
2785 * @returns COM status code.
2786 * @param a_pExtPackFile The extension pack file, caller checks that
2787 * it's usable.
2788 * @param a_fReplace Whether to replace any existing extpack or just
2789 * fail.
2790 * @param a_pstrDisplayInfo Host specific display information hacks.
2791 * be NULL.
2792 */
2793HRESULT ExtPackManager::i_doInstall(ExtPackFile *a_pExtPackFile, bool a_fReplace, Utf8Str const *a_pstrDisplayInfo)
2794{
2795 AssertReturn(m->enmContext == VBOXEXTPACKCTX_PER_USER_DAEMON, E_UNEXPECTED);
2796 RTCString const * const pStrName = &a_pExtPackFile->m->Desc.strName;
2797 RTCString const * const pStrTarball = &a_pExtPackFile->m->strExtPackFile;
2798 RTCString const * const pStrTarballDigest = &a_pExtPackFile->m->strDigest;
2799
2800 AutoCaller autoCaller(this);
2801 HRESULT hrc = autoCaller.rc();
2802 if (SUCCEEDED(hrc))
2803 {
2804 AutoWriteLock autoLock(this COMMA_LOCKVAL_SRC_POS);
2805
2806 /*
2807 * Refresh the data we have on the extension pack as it
2808 * may be made stale by direct meddling or some other user.
2809 */
2810 ExtPack *pExtPack;
2811 hrc = i_refreshExtPack(pStrName->c_str(), false /*a_fUnusableIsError*/, &pExtPack);
2812 if (SUCCEEDED(hrc))
2813 {
2814 if (pExtPack && a_fReplace)
2815 {
2816 /* We must leave the lock when calling i_areThereAnyRunningVMs,
2817 which means we have to redo the refresh call afterwards. */
2818 autoLock.release();
2819 bool fRunningVMs = i_areThereAnyRunningVMs();
2820 autoLock.acquire();
2821 hrc = i_refreshExtPack(pStrName->c_str(), false /*a_fUnusableIsError*/, &pExtPack);
2822 if (fRunningVMs)
2823 {
2824 LogRel(("Install extension pack '%s' failed because at least one VM is still running.", pStrName->c_str()));
2825 hrc = setError(E_FAIL, tr("Install extension pack '%s' failed because at least one VM is still running"),
2826 pStrName->c_str());
2827 }
2828 else if (SUCCEEDED(hrc) && pExtPack)
2829 hrc = pExtPack->i_callUninstallHookAndClose(m->pVirtualBox, false /*a_ForcedRemoval*/);
2830 }
2831 else if (pExtPack)
2832 hrc = setError(E_FAIL,
2833 tr("Extension pack '%s' is already installed."
2834 " In case of a reinstallation, please uninstall it first"),
2835 pStrName->c_str());
2836 }
2837 if (SUCCEEDED(hrc))
2838 {
2839 /*
2840 * Run the privileged helper binary that performs the actual
2841 * installation. Then create an object for the packet (we do this
2842 * even on failure, to be on the safe side).
2843 */
2844 hrc = i_runSetUidToRootHelper(a_pstrDisplayInfo,
2845 "install",
2846 "--base-dir", m->strBaseDir.c_str(),
2847 "--cert-dir", m->strCertificatDirPath.c_str(),
2848 "--name", pStrName->c_str(),
2849 "--tarball", pStrTarball->c_str(),
2850 "--sha-256", pStrTarballDigest->c_str(),
2851 pExtPack ? "--replace" : (const char *)NULL,
2852 (const char *)NULL);
2853 if (SUCCEEDED(hrc))
2854 {
2855 hrc = i_refreshExtPack(pStrName->c_str(), true /*a_fUnusableIsError*/, &pExtPack);
2856 if (SUCCEEDED(hrc) && pExtPack)
2857 {
2858 RTERRINFOSTATIC ErrInfo;
2859 RTErrInfoInitStatic(&ErrInfo);
2860 pExtPack->i_callInstalledHook(m->pVirtualBox, &autoLock, &ErrInfo.Core);
2861 if (RT_SUCCESS(ErrInfo.Core.rc))
2862 LogRel(("ExtPackManager: Successfully installed extension pack '%s'.\n", pStrName->c_str()));
2863 else
2864 {
2865 LogRel(("ExtPackManager: Installed hook for '%s' failed: %Rrc - %s\n",
2866 pStrName->c_str(), ErrInfo.Core.rc, ErrInfo.Core.pszMsg));
2867
2868 /*
2869 * Uninstall the extpack if the error indicates that.
2870 */
2871 if (ErrInfo.Core.rc == VERR_EXTPACK_UNSUPPORTED_HOST_UNINSTALL)
2872 i_runSetUidToRootHelper(a_pstrDisplayInfo,
2873 "uninstall",
2874 "--base-dir", m->strBaseDir.c_str(),
2875 "--name", pStrName->c_str(),
2876 "--forced",
2877 (const char *)NULL);
2878 hrc = setErrorBoth(E_FAIL, ErrInfo.Core.rc, tr("The installation hook failed: %Rrc - %s"),
2879 ErrInfo.Core.rc, ErrInfo.Core.pszMsg);
2880 }
2881 }
2882 else if (SUCCEEDED(hrc))
2883 hrc = setError(E_FAIL, tr("Installing extension pack '%s' failed under mysterious circumstances"),
2884 pStrName->c_str());
2885 }
2886 else
2887 {
2888 ErrorInfoKeeper Eik;
2889 i_refreshExtPack(pStrName->c_str(), false /*a_fUnusableIsError*/, NULL);
2890 }
2891 }
2892
2893 /*
2894 * Do VirtualBoxReady callbacks now for any freshly installed
2895 * extension pack (old ones will not be called).
2896 */
2897 if (m->enmContext == VBOXEXTPACKCTX_PER_USER_DAEMON)
2898 {
2899 autoLock.release();
2900 i_callAllVirtualBoxReadyHooks();
2901 }
2902 }
2903
2904 return hrc;
2905}
2906
2907/**
2908 * Worker for IExtPackManager::Uninstall.
2909 *
2910 * Called on a worker thread via doUninstallThreadProc.
2911 *
2912 * @returns COM status code.
2913 * @param a_pstrName The name of the extension pack to uninstall.
2914 * @param a_fForcedRemoval Whether to be skip and ignore certain bits of
2915 * the extpack feedback. To deal with misbehaving
2916 * extension pack hooks.
2917 * @param a_pstrDisplayInfo Host specific display information hacks.
2918 */
2919HRESULT ExtPackManager::i_doUninstall(Utf8Str const *a_pstrName, bool a_fForcedRemoval, Utf8Str const *a_pstrDisplayInfo)
2920{
2921 Assert(m->enmContext == VBOXEXTPACKCTX_PER_USER_DAEMON);
2922
2923 AutoCaller autoCaller(this);
2924 HRESULT hrc = autoCaller.rc();
2925 if (SUCCEEDED(hrc))
2926 {
2927 if (a_fForcedRemoval || !i_areThereAnyRunningVMs())
2928 {
2929 AutoWriteLock autoLock(this COMMA_LOCKVAL_SRC_POS);
2930
2931 /*
2932 * Refresh the data we have on the extension pack as it may be made
2933 * stale by direct meddling or some other user.
2934 */
2935 ExtPack *pExtPack;
2936 hrc = i_refreshExtPack(a_pstrName->c_str(), false /*a_fUnusableIsError*/, &pExtPack);
2937 if (SUCCEEDED(hrc))
2938 {
2939 if (!pExtPack)
2940 {
2941 LogRel(("ExtPackManager: Extension pack '%s' is not installed, so nothing to uninstall.\n", a_pstrName->c_str()));
2942 hrc = S_OK; /* nothing to uninstall */
2943 }
2944 else
2945 {
2946 /*
2947 * Call the uninstall hook and unload the main dll.
2948 */
2949 hrc = pExtPack->i_callUninstallHookAndClose(m->pVirtualBox, a_fForcedRemoval);
2950 if (SUCCEEDED(hrc))
2951 {
2952 /*
2953 * Run the set-uid-to-root binary that performs the
2954 * uninstallation. Then refresh the object.
2955 *
2956 * This refresh is theorically subject to races, but it's of
2957 * the don't-do-that variety.
2958 */
2959 const char *pszForcedOpt = a_fForcedRemoval ? "--forced" : NULL;
2960 hrc = i_runSetUidToRootHelper(a_pstrDisplayInfo,
2961 "uninstall",
2962 "--base-dir", m->strBaseDir.c_str(),
2963 "--name", a_pstrName->c_str(),
2964 pszForcedOpt, /* Last as it may be NULL. */
2965 (const char *)NULL);
2966 if (SUCCEEDED(hrc))
2967 {
2968 hrc = i_refreshExtPack(a_pstrName->c_str(), false /*a_fUnusableIsError*/, &pExtPack);
2969 if (SUCCEEDED(hrc))
2970 {
2971 if (!pExtPack)
2972 LogRel(("ExtPackManager: Successfully uninstalled extension pack '%s'.\n", a_pstrName->c_str()));
2973 else
2974 hrc = setError(E_FAIL,
2975 tr("Uninstall extension pack '%s' failed under mysterious circumstances"),
2976 a_pstrName->c_str());
2977 }
2978 }
2979 else
2980 {
2981 ErrorInfoKeeper Eik;
2982 i_refreshExtPack(a_pstrName->c_str(), false /*a_fUnusableIsError*/, NULL);
2983 }
2984 }
2985 }
2986 }
2987 }
2988 else
2989 {
2990 LogRel(("Uninstall extension pack '%s' failed because at least one VM is still running.", a_pstrName->c_str()));
2991 hrc = setError(E_FAIL, tr("Uninstall extension pack '%s' failed because at least one VM is still running"),
2992 a_pstrName->c_str());
2993 }
2994
2995 /*
2996 * Do VirtualBoxReady callbacks now for any freshly installed
2997 * extension pack (old ones will not be called).
2998 */
2999 if (m->enmContext == VBOXEXTPACKCTX_PER_USER_DAEMON)
3000 i_callAllVirtualBoxReadyHooks();
3001 }
3002
3003 return hrc;
3004}
3005
3006
3007/**
3008 * Calls the pfnVirtualBoxReady hook for all working extension packs.
3009 *
3010 * @remarks The caller must not hold any locks.
3011 */
3012void ExtPackManager::i_callAllVirtualBoxReadyHooks(void)
3013{
3014 AutoCaller autoCaller(this);
3015 HRESULT hrc = autoCaller.rc();
3016 if (FAILED(hrc))
3017 return;
3018 AutoWriteLock autoLock(this COMMA_LOCKVAL_SRC_POS);
3019 ComPtr<ExtPackManager> ptrSelfRef = this;
3020
3021 for (ExtPackList::iterator it = m->llInstalledExtPacks.begin();
3022 it != m->llInstalledExtPacks.end();
3023 /* advancing below */)
3024 {
3025 if ((*it)->i_callVirtualBoxReadyHook(m->pVirtualBox, &autoLock))
3026 it = m->llInstalledExtPacks.begin();
3027 else
3028 ++it;
3029 }
3030}
3031
3032
3033/**
3034 * Queries objects of type @a aObjUuid from all the extension packs.
3035 *
3036 * @returns COM status code.
3037 * @param aObjUuid The UUID of the kind of objects we're querying.
3038 * @param aObjects Where to return the objects.
3039 * @param a_pstrExtPackNames Where to return the corresponding extpack names (may be NULL).
3040 *
3041 * @remarks The caller must not hold any locks.
3042 */
3043HRESULT ExtPackManager::i_queryObjects(const com::Utf8Str &aObjUuid, std::vector<ComPtr<IUnknown> > &aObjects, std::vector<com::Utf8Str> *a_pstrExtPackNames)
3044{
3045 aObjects.clear();
3046 if (a_pstrExtPackNames)
3047 a_pstrExtPackNames->clear();
3048
3049 AutoCaller autoCaller(this);
3050 HRESULT hrc = autoCaller.rc();
3051 if (SUCCEEDED(hrc))
3052 {
3053 AutoWriteLock autoLock(this COMMA_LOCKVAL_SRC_POS);
3054 ComPtr<ExtPackManager> ptrSelfRef = this;
3055
3056 for (ExtPackList::iterator it = m->llInstalledExtPacks.begin();
3057 it != m->llInstalledExtPacks.end();
3058 ++it)
3059 {
3060 ComPtr<IUnknown> ptrIf;
3061 HRESULT hrc2 = (*it)->queryObject(aObjUuid, ptrIf);
3062 if (SUCCEEDED(hrc2))
3063 {
3064 aObjects.push_back(ptrIf);
3065 if (a_pstrExtPackNames)
3066 a_pstrExtPackNames->push_back((*it)->m->Desc.strName);
3067 }
3068 else if (hrc2 != E_NOINTERFACE)
3069 hrc = hrc2;
3070 }
3071
3072 if (aObjects.size() > 0)
3073 hrc = S_OK;
3074 }
3075 return hrc;
3076}
3077
3078#endif /* !VBOX_COM_INPROC */
3079
3080#ifdef VBOX_COM_INPROC
3081/**
3082 * Calls the pfnConsoleReady hook for all working extension packs.
3083 *
3084 * @param a_pConsole The console interface.
3085 * @remarks The caller must not hold any locks.
3086 */
3087void ExtPackManager::i_callAllConsoleReadyHooks(IConsole *a_pConsole)
3088{
3089 AutoCaller autoCaller(this);
3090 HRESULT hrc = autoCaller.rc();
3091 if (FAILED(hrc))
3092 return;
3093 AutoWriteLock autoLock(this COMMA_LOCKVAL_SRC_POS);
3094 ComPtr<ExtPackManager> ptrSelfRef = this;
3095
3096 for (ExtPackList::iterator it = m->llInstalledExtPacks.begin();
3097 it != m->llInstalledExtPacks.end();
3098 /* advancing below */)
3099 {
3100 if ((*it)->i_callConsoleReadyHook(a_pConsole, &autoLock))
3101 it = m->llInstalledExtPacks.begin();
3102 else
3103 ++it;
3104 }
3105}
3106#endif
3107
3108#ifndef VBOX_COM_INPROC
3109/**
3110 * Calls the pfnVMCreated hook for all working extension packs.
3111 *
3112 * @param a_pMachine The machine interface of the new VM.
3113 */
3114void ExtPackManager::i_callAllVmCreatedHooks(IMachine *a_pMachine)
3115{
3116 AutoCaller autoCaller(this);
3117 HRESULT hrc = autoCaller.rc();
3118 if (FAILED(hrc))
3119 return;
3120 AutoWriteLock autoLock(this COMMA_LOCKVAL_SRC_POS);
3121 ComPtr<ExtPackManager> ptrSelfRef = this; /* paranoia */
3122 ExtPackList llExtPacks = m->llInstalledExtPacks;
3123
3124 for (ExtPackList::iterator it = llExtPacks.begin(); it != llExtPacks.end(); ++it)
3125 (*it)->i_callVmCreatedHook(m->pVirtualBox, a_pMachine, &autoLock);
3126}
3127#endif
3128
3129#ifdef VBOX_COM_INPROC
3130/**
3131 * Calls the pfnVMConfigureVMM hook for all working extension packs.
3132 *
3133 * @returns VBox status code. Stops on the first failure, expecting the caller
3134 * to signal this to the caller of the CFGM constructor.
3135 * @param a_pConsole The console interface for the VM.
3136 * @param a_pVM The VM handle.
3137 */
3138int ExtPackManager::i_callAllVmConfigureVmmHooks(IConsole *a_pConsole, PVM a_pVM)
3139{
3140 AutoCaller autoCaller(this);
3141 HRESULT hrc = autoCaller.rc();
3142 if (FAILED(hrc))
3143 return Global::vboxStatusCodeFromCOM(hrc);
3144 AutoWriteLock autoLock(this COMMA_LOCKVAL_SRC_POS);
3145 ComPtr<ExtPackManager> ptrSelfRef = this; /* paranoia */
3146 ExtPackList llExtPacks = m->llInstalledExtPacks;
3147
3148 for (ExtPackList::iterator it = llExtPacks.begin(); it != llExtPacks.end(); ++it)
3149 {
3150 int vrc;
3151 (*it)->i_callVmConfigureVmmHook(a_pConsole, a_pVM, &autoLock, &vrc);
3152 if (RT_FAILURE(vrc))
3153 return vrc;
3154 }
3155
3156 return VINF_SUCCESS;
3157}
3158
3159/**
3160 * Calls the pfnVMPowerOn hook for all working extension packs.
3161 *
3162 * @returns VBox status code. Stops on the first failure, expecting the caller
3163 * to not power on the VM.
3164 * @param a_pConsole The console interface for the VM.
3165 * @param a_pVM The VM handle.
3166 */
3167int ExtPackManager::i_callAllVmPowerOnHooks(IConsole *a_pConsole, PVM a_pVM)
3168{
3169 AutoCaller autoCaller(this);
3170 HRESULT hrc = autoCaller.rc();
3171 if (FAILED(hrc))
3172 return Global::vboxStatusCodeFromCOM(hrc);
3173 AutoWriteLock autoLock(this COMMA_LOCKVAL_SRC_POS);
3174 ComPtr<ExtPackManager> ptrSelfRef = this; /* paranoia */
3175 ExtPackList llExtPacks = m->llInstalledExtPacks;
3176
3177 for (ExtPackList::iterator it = llExtPacks.begin(); it != llExtPacks.end(); ++it)
3178 {
3179 int vrc;
3180 (*it)->i_callVmPowerOnHook(a_pConsole, a_pVM, &autoLock, &vrc);
3181 if (RT_FAILURE(vrc))
3182 return vrc;
3183 }
3184
3185 return VINF_SUCCESS;
3186}
3187
3188/**
3189 * Calls the pfnVMPowerOff hook for all working extension packs.
3190 *
3191 * @param a_pConsole The console interface for the VM.
3192 * @param a_pVM The VM handle. Can be NULL.
3193 */
3194void ExtPackManager::i_callAllVmPowerOffHooks(IConsole *a_pConsole, PVM a_pVM)
3195{
3196 AutoCaller autoCaller(this);
3197 HRESULT hrc = autoCaller.rc();
3198 if (FAILED(hrc))
3199 return;
3200 AutoWriteLock autoLock(this COMMA_LOCKVAL_SRC_POS);
3201 ComPtr<ExtPackManager> ptrSelfRef = this; /* paranoia */
3202 ExtPackList llExtPacks = m->llInstalledExtPacks;
3203
3204 for (ExtPackList::iterator it = llExtPacks.begin(); it != llExtPacks.end(); ++it)
3205 (*it)->i_callVmPowerOffHook(a_pConsole, a_pVM, &autoLock);
3206}
3207#endif
3208
3209
3210/**
3211 * Checks that the specified extension pack contains a VRDE module and that it
3212 * is shipshape.
3213 *
3214 * @returns S_OK if ok, appropriate failure status code with details.
3215 * @param a_pstrExtPack The name of the extension pack.
3216 */
3217HRESULT ExtPackManager::i_checkVrdeExtPack(Utf8Str const *a_pstrExtPack)
3218{
3219 AutoCaller autoCaller(this);
3220 HRESULT hrc = autoCaller.rc();
3221 if (SUCCEEDED(hrc))
3222 {
3223 AutoReadLock autoLock(this COMMA_LOCKVAL_SRC_POS);
3224
3225 ExtPack *pExtPack = i_findExtPack(a_pstrExtPack->c_str());
3226 if (pExtPack)
3227 hrc = pExtPack->i_checkVrde();
3228 else
3229 hrc = setError(VBOX_E_OBJECT_NOT_FOUND, tr("No extension pack by the name '%s' was found"), a_pstrExtPack->c_str());
3230 }
3231
3232 return hrc;
3233}
3234
3235/**
3236 * Gets the full path to the VRDE library of the specified extension pack.
3237 *
3238 * This will do extacly the same as checkVrdeExtPack and then resolve the
3239 * library path.
3240 *
3241 * @returns S_OK if a path is returned, COM error status and message return if
3242 * not.
3243 * @param a_pstrExtPack The extension pack.
3244 * @param a_pstrVrdeLibrary Where to return the path.
3245 */
3246int ExtPackManager::i_getVrdeLibraryPathForExtPack(Utf8Str const *a_pstrExtPack, Utf8Str *a_pstrVrdeLibrary)
3247{
3248 AutoCaller autoCaller(this);
3249 HRESULT hrc = autoCaller.rc();
3250 if (SUCCEEDED(hrc))
3251 {
3252 AutoReadLock autoLock(this COMMA_LOCKVAL_SRC_POS);
3253
3254 ExtPack *pExtPack = i_findExtPack(a_pstrExtPack->c_str());
3255 if (pExtPack)
3256 hrc = pExtPack->i_getVrdpLibraryName(a_pstrVrdeLibrary);
3257 else
3258 hrc = setError(VBOX_E_OBJECT_NOT_FOUND, tr("No extension pack by the name '%s' was found"),
3259 a_pstrExtPack->c_str());
3260 }
3261
3262 return hrc;
3263}
3264
3265/**
3266 * Gets the full path to the specified library of the specified extension pack.
3267 *
3268 * @returns S_OK if a path is returned, COM error status and message return if
3269 * not.
3270 * @param a_pszModuleName The library.
3271 * @param a_pszExtPack The extension pack.
3272 * @param a_pstrLibrary Where to return the path.
3273 */
3274HRESULT ExtPackManager::i_getLibraryPathForExtPack(const char *a_pszModuleName, const char *a_pszExtPack, Utf8Str *a_pstrLibrary)
3275{
3276 AutoCaller autoCaller(this);
3277 HRESULT hrc = autoCaller.rc();
3278 if (SUCCEEDED(hrc))
3279 {
3280 AutoReadLock autoLock(this COMMA_LOCKVAL_SRC_POS);
3281
3282 ExtPack *pExtPack = i_findExtPack(a_pszExtPack);
3283 if (pExtPack)
3284 hrc = pExtPack->i_getLibraryName(a_pszModuleName, a_pstrLibrary);
3285 else
3286 hrc = setError(VBOX_E_OBJECT_NOT_FOUND, tr("No extension pack by the name '%s' was found"), a_pszExtPack);
3287 }
3288
3289 return hrc;
3290}
3291
3292/**
3293 * Gets the name of the default VRDE extension pack.
3294 *
3295 * @returns S_OK or some COM error status on red tape failure.
3296 * @param a_pstrExtPack Where to return the extension pack name. Returns
3297 * empty if no extension pack wishes to be the default
3298 * VRDP provider.
3299 */
3300HRESULT ExtPackManager::i_getDefaultVrdeExtPack(Utf8Str *a_pstrExtPack)
3301{
3302 a_pstrExtPack->setNull();
3303
3304 AutoCaller autoCaller(this);
3305 HRESULT hrc = autoCaller.rc();
3306 if (SUCCEEDED(hrc))
3307 {
3308 AutoReadLock autoLock(this COMMA_LOCKVAL_SRC_POS);
3309
3310 for (ExtPackList::iterator it = m->llInstalledExtPacks.begin();
3311 it != m->llInstalledExtPacks.end();
3312 ++it)
3313 {
3314 if ((*it)->i_wantsToBeDefaultVrde())
3315 {
3316 *a_pstrExtPack = (*it)->m->Desc.strName;
3317 break;
3318 }
3319 }
3320 }
3321 return hrc;
3322}
3323
3324/**
3325 * Checks if an extension pack is (present and) usable.
3326 *
3327 * @returns @c true if it is, otherwise @c false.
3328 * @param a_pszExtPack The name of the extension pack.
3329 */
3330bool ExtPackManager::i_isExtPackUsable(const char *a_pszExtPack)
3331{
3332 AutoCaller autoCaller(this);
3333 HRESULT hrc = autoCaller.rc();
3334 if (FAILED(hrc))
3335 return false;
3336 AutoReadLock autoLock(this COMMA_LOCKVAL_SRC_POS);
3337
3338 ExtPack *pExtPack = i_findExtPack(a_pszExtPack);
3339 return pExtPack != NULL
3340 && pExtPack->m->fUsable;
3341}
3342
3343/**
3344 * Dumps all extension packs to the release log.
3345 */
3346void ExtPackManager::i_dumpAllToReleaseLog(void)
3347{
3348 AutoCaller autoCaller(this);
3349 HRESULT hrc = autoCaller.rc();
3350 if (FAILED(hrc))
3351 return;
3352 AutoReadLock autoLock(this COMMA_LOCKVAL_SRC_POS);
3353
3354 LogRel(("Installed Extension Packs:\n"));
3355 for (ExtPackList::iterator it = m->llInstalledExtPacks.begin();
3356 it != m->llInstalledExtPacks.end();
3357 ++it)
3358 {
3359 ExtPack::Data *pExtPackData = (*it)->m;
3360 if (pExtPackData)
3361 {
3362 if (pExtPackData->fUsable)
3363 LogRel((" %s (Version: %s r%u%s%s; VRDE Module: %s)\n",
3364 pExtPackData->Desc.strName.c_str(),
3365 pExtPackData->Desc.strVersion.c_str(),
3366 pExtPackData->Desc.uRevision,
3367 pExtPackData->Desc.strEdition.isEmpty() ? "" : " ",
3368 pExtPackData->Desc.strEdition.c_str(),
3369 pExtPackData->Desc.strVrdeModule.c_str() ));
3370 else
3371 LogRel((" %s (Version: %s r%u%s%s; VRDE Module: %s unusable because of '%s')\n",
3372 pExtPackData->Desc.strName.c_str(),
3373 pExtPackData->Desc.strVersion.c_str(),
3374 pExtPackData->Desc.uRevision,
3375 pExtPackData->Desc.strEdition.isEmpty() ? "" : " ",
3376 pExtPackData->Desc.strEdition.c_str(),
3377 pExtPackData->Desc.strVrdeModule.c_str(),
3378 pExtPackData->strWhyUnusable.c_str() ));
3379 }
3380 else
3381 LogRel((" pExtPackData is NULL\n"));
3382 }
3383
3384 if (!m->llInstalledExtPacks.size())
3385 LogRel((" None installed!\n"));
3386}
3387
3388/**
3389 * Gets the update counter (reflecting extpack list updates).
3390 */
3391uint64_t ExtPackManager::i_getUpdateCounter(void)
3392{
3393 AutoCaller autoCaller(this);
3394 HRESULT hrc = autoCaller.rc();
3395 if (FAILED(hrc))
3396 return 0;
3397 AutoReadLock autoLock(this COMMA_LOCKVAL_SRC_POS);
3398 return m->cUpdate;
3399}
3400
3401/* vi: set tabstop=4 shiftwidth=4 expandtab: */
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette