VirtualBox

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

Last change on this file since 49016 was 48390, checked in by vboxsync, 11 years ago

Main/ExtPackManagerImpl: warning.

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