VirtualBox

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

Last change on this file since 40188 was 39878, checked in by vboxsync, 13 years ago

Main,QtGui: Implemented that hashing todo, extending it all the way to the gui for good measure.

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

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