VirtualBox

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

Last change on this file since 45733 was 44529, checked in by vboxsync, 12 years ago

header (C) fixes

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 106.2 KB
Line 
1/* $Id: ExtPackManagerImpl.cpp 44529 2013-02-04 15:54:15Z vboxsync $ */
2/** @file
3 * VirtualBox Main - interface for Extension Packs, VBoxSVC & VBoxC.
4 */
5
6/*
7 * Copyright (C) 2010-2013 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 */
17
18
19/*******************************************************************************
20* Header Files *
21*******************************************************************************/
22#include "ExtPackManagerImpl.h"
23#include "ExtPackUtil.h"
24
25#include <iprt/buildconfig.h>
26#include <iprt/ctype.h>
27#include <iprt/dir.h>
28#include <iprt/env.h>
29#include <iprt/file.h>
30#include <iprt/ldr.h>
31#include <iprt/manifest.h>
32#include <iprt/param.h>
33#include <iprt/path.h>
34#include <iprt/pipe.h>
35#include <iprt/process.h>
36#include <iprt/string.h>
37
38#include <VBox/com/array.h>
39#include <VBox/com/ErrorInfo.h>
40#include <VBox/err.h>
41#include <VBox/log.h>
42#include <VBox/sup.h>
43#include <VBox/version.h>
44#include "AutoCaller.h"
45#include "Global.h"
46#include "ProgressImpl.h"
47#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 = strTarballAndDigest.find("::SHA-256=");
2044 if (offSha256 == Utf8Str::npos)
2045 strTarball = strTarballAndDigest;
2046 else
2047 {
2048 strTarball = strTarballAndDigest.substr(0, offSha256);
2049 strDigest = strTarballAndDigest.substr(offSha256 + sizeof("::SHA-256=") - 1);
2050 }
2051
2052 ComObjPtr<ExtPackFile> NewExtPackFile;
2053 HRESULT hrc = NewExtPackFile.createObject();
2054 if (SUCCEEDED(hrc))
2055 hrc = NewExtPackFile->initWithFile(strTarball.c_str(), strDigest.c_str(), this, m->pVirtualBox);
2056 if (SUCCEEDED(hrc))
2057 NewExtPackFile.queryInterfaceTo(a_ppExtPackFile);
2058
2059 return hrc;
2060}
2061
2062STDMETHODIMP ExtPackManager::Uninstall(IN_BSTR a_bstrName, BOOL a_fForcedRemoval, IN_BSTR a_bstrDisplayInfo,
2063 IProgress **a_ppProgress)
2064{
2065 CheckComArgNotNull(a_bstrName);
2066 if (a_ppProgress)
2067 *a_ppProgress = NULL;
2068 Assert(m->enmContext == VBOXEXTPACKCTX_PER_USER_DAEMON);
2069
2070 AutoCaller autoCaller(this);
2071 HRESULT hrc = autoCaller.rc();
2072 if (SUCCEEDED(hrc))
2073 {
2074 PEXTPACKUNINSTALLJOB pJob = NULL;
2075 try
2076 {
2077 pJob = new EXTPACKUNINSTALLJOB;
2078 pJob->ptrExtPackMgr = this;
2079 pJob->strName = a_bstrName;
2080 pJob->fForcedRemoval = a_fForcedRemoval != FALSE;
2081 pJob->strDisplayInfo = a_bstrDisplayInfo;
2082 hrc = pJob->ptrProgress.createObject();
2083 if (SUCCEEDED(hrc))
2084 {
2085 Bstr bstrDescription = tr("Uninstalling extension pack");
2086 hrc = pJob->ptrProgress->init(
2087#ifndef VBOX_COM_INPROC
2088 m->pVirtualBox,
2089#endif
2090 static_cast<IExtPackManager *>(this),
2091 bstrDescription.raw(),
2092 FALSE /*aCancelable*/,
2093 NULL /*aId*/);
2094 }
2095 if (SUCCEEDED(hrc))
2096 {
2097 ComPtr<Progress> ptrProgress = pJob->ptrProgress;
2098 int vrc = RTThreadCreate(NULL /*phThread*/, ExtPackManager::doUninstallThreadProc, pJob, 0,
2099 RTTHREADTYPE_DEFAULT, 0 /*fFlags*/, "ExtPackUninst");
2100 if (RT_SUCCESS(vrc))
2101 {
2102 pJob = NULL; /* the thread deletes it */
2103 ptrProgress.queryInterfaceTo(a_ppProgress);
2104 }
2105 else
2106 hrc = setError(VBOX_E_IPRT_ERROR, tr("RTThreadCreate failed with %Rrc"), vrc);
2107 }
2108 }
2109 catch (std::bad_alloc)
2110 {
2111 hrc = E_OUTOFMEMORY;
2112 }
2113 if (pJob)
2114 delete pJob;
2115 }
2116
2117 return hrc;
2118}
2119
2120STDMETHODIMP ExtPackManager::Cleanup(void)
2121{
2122 Assert(m->enmContext == VBOXEXTPACKCTX_PER_USER_DAEMON);
2123
2124 AutoCaller autoCaller(this);
2125 HRESULT hrc = autoCaller.rc();
2126 if (SUCCEEDED(hrc))
2127 {
2128 /*
2129 * Run the set-uid-to-root binary that performs the cleanup.
2130 *
2131 * Take the write lock to prevent conflicts with other calls to this
2132 * VBoxSVC instance.
2133 */
2134 AutoWriteLock autoLock(this COMMA_LOCKVAL_SRC_POS);
2135 hrc = runSetUidToRootHelper(NULL,
2136 "cleanup",
2137 "--base-dir", m->strBaseDir.c_str(),
2138 (const char *)NULL);
2139 }
2140
2141 return hrc;
2142}
2143
2144STDMETHODIMP ExtPackManager::QueryAllPlugInsForFrontend(IN_BSTR a_bstrFrontend, ComSafeArrayOut(BSTR, a_pabstrPlugInModules))
2145{
2146 CheckComArgNotNull(a_bstrFrontend);
2147 Utf8Str strName(a_bstrFrontend);
2148 CheckComArgOutSafeArrayPointerValid(a_pabstrPlugInModules);
2149 Assert(m->enmContext == VBOXEXTPACKCTX_PER_USER_DAEMON);
2150
2151 AutoCaller autoCaller(this);
2152 HRESULT hrc = autoCaller.rc();
2153 if (SUCCEEDED(hrc))
2154 {
2155 com::SafeArray<BSTR> saPaths((size_t)0);
2156 /** @todo implement plug-ins */
2157 saPaths.detachTo(ComSafeArrayOutArg(a_pabstrPlugInModules));
2158 }
2159 return hrc;
2160}
2161
2162STDMETHODIMP ExtPackManager::IsExtPackUsable(IN_BSTR a_bstrExtPack, BOOL *aUsable)
2163{
2164 CheckComArgNotNull(a_bstrExtPack);
2165 Utf8Str strExtPack(a_bstrExtPack);
2166 *aUsable = isExtPackUsable(strExtPack.c_str());
2167 return S_OK;
2168}
2169
2170/**
2171 * Finds the success indicator string in the stderr output ofr hte helper app.
2172 *
2173 * @returns Pointer to the indicator.
2174 * @param psz The stderr output string. Can be NULL.
2175 * @param cch The size of the string.
2176 */
2177static char *findSuccessIndicator(char *psz, size_t cch)
2178{
2179 static const char s_szSuccessInd[] = "rcExit=RTEXITCODE_SUCCESS";
2180 Assert(!cch || strlen(psz) == cch);
2181 if (cch < sizeof(s_szSuccessInd) - 1)
2182 return NULL;
2183 char *pszInd = &psz[cch - sizeof(s_szSuccessInd) + 1];
2184 if (strcmp(s_szSuccessInd, pszInd))
2185 return NULL;
2186 return pszInd;
2187}
2188
2189/**
2190 * Runs the helper application that does the privileged operations.
2191 *
2192 * @returns S_OK or a failure status with error information set.
2193 * @param a_pstrDisplayInfo Platform specific display info hacks.
2194 * @param a_pszCommand The command to execute.
2195 * @param ... The argument strings that goes along with the
2196 * command. Maximum is about 16. Terminated by a
2197 * NULL.
2198 */
2199HRESULT ExtPackManager::runSetUidToRootHelper(Utf8Str const *a_pstrDisplayInfo, const char *a_pszCommand, ...)
2200{
2201 /*
2202 * Calculate the path to the helper application.
2203 */
2204 char szExecName[RTPATH_MAX];
2205 int vrc = RTPathAppPrivateArch(szExecName, sizeof(szExecName));
2206 AssertLogRelRCReturn(vrc, E_UNEXPECTED);
2207
2208 vrc = RTPathAppend(szExecName, sizeof(szExecName), VBOX_EXTPACK_HELPER_NAME);
2209 AssertLogRelRCReturn(vrc, E_UNEXPECTED);
2210
2211 /*
2212 * Convert the variable argument list to a RTProcCreate argument vector.
2213 */
2214 const char *apszArgs[20];
2215 unsigned cArgs = 0;
2216
2217 LogRel(("ExtPack: Executing '%s'", szExecName));
2218 apszArgs[cArgs++] = &szExecName[0];
2219
2220 if ( a_pstrDisplayInfo
2221 && a_pstrDisplayInfo->isNotEmpty())
2222 {
2223 LogRel((" '--display-info-hack' '%s'", a_pstrDisplayInfo->c_str()));
2224 apszArgs[cArgs++] = "--display-info-hack";
2225 apszArgs[cArgs++] = a_pstrDisplayInfo->c_str();
2226 }
2227
2228 LogRel(("'%s'", a_pszCommand));
2229 apszArgs[cArgs++] = a_pszCommand;
2230
2231 va_list va;
2232 va_start(va, a_pszCommand);
2233 const char *pszLastArg;
2234 for (;;)
2235 {
2236 AssertReturn(cArgs < RT_ELEMENTS(apszArgs) - 1, E_UNEXPECTED);
2237 pszLastArg = va_arg(va, const char *);
2238 if (!pszLastArg)
2239 break;
2240 LogRel((" '%s'", pszLastArg));
2241 apszArgs[cArgs++] = pszLastArg;
2242 };
2243 va_end(va);
2244
2245 LogRel(("\n"));
2246 apszArgs[cArgs] = NULL;
2247
2248 /*
2249 * Create a PIPE which we attach to stderr so that we can read the error
2250 * message on failure and report it back to the caller.
2251 */
2252 RTPIPE hPipeR;
2253 RTHANDLE hStdErrPipe;
2254 hStdErrPipe.enmType = RTHANDLETYPE_PIPE;
2255 vrc = RTPipeCreate(&hPipeR, &hStdErrPipe.u.hPipe, RTPIPE_C_INHERIT_WRITE);
2256 AssertLogRelRCReturn(vrc, E_UNEXPECTED);
2257
2258 /*
2259 * Spawn the process.
2260 */
2261 HRESULT hrc;
2262 RTPROCESS hProcess;
2263 vrc = RTProcCreateEx(szExecName,
2264 apszArgs,
2265 RTENV_DEFAULT,
2266 0 /*fFlags*/,
2267 NULL /*phStdIn*/,
2268 NULL /*phStdOut*/,
2269 &hStdErrPipe,
2270 NULL /*pszAsUser*/,
2271 NULL /*pszPassword*/,
2272 &hProcess);
2273 if (RT_SUCCESS(vrc))
2274 {
2275 vrc = RTPipeClose(hStdErrPipe.u.hPipe);
2276 hStdErrPipe.u.hPipe = NIL_RTPIPE;
2277
2278 /*
2279 * Read the pipe output until the process completes.
2280 */
2281 RTPROCSTATUS ProcStatus = { -42, RTPROCEXITREASON_ABEND };
2282 size_t cbStdErrBuf = 0;
2283 size_t offStdErrBuf = 0;
2284 char *pszStdErrBuf = NULL;
2285 do
2286 {
2287 /*
2288 * Service the pipe. Block waiting for output or the pipe breaking
2289 * when the process terminates.
2290 */
2291 if (hPipeR != NIL_RTPIPE)
2292 {
2293 char achBuf[1024];
2294 size_t cbRead;
2295 vrc = RTPipeReadBlocking(hPipeR, achBuf, sizeof(achBuf), &cbRead);
2296 if (RT_SUCCESS(vrc))
2297 {
2298 /* grow the buffer? */
2299 size_t cbBufReq = offStdErrBuf + cbRead + 1;
2300 if ( cbBufReq > cbStdErrBuf
2301 && cbBufReq < _256K)
2302 {
2303 size_t cbNew = RT_ALIGN_Z(cbBufReq, 16); // 1024
2304 void *pvNew = RTMemRealloc(pszStdErrBuf, cbNew);
2305 if (pvNew)
2306 {
2307 pszStdErrBuf = (char *)pvNew;
2308 cbStdErrBuf = cbNew;
2309 }
2310 }
2311
2312 /* append if we've got room. */
2313 if (cbBufReq <= cbStdErrBuf)
2314 {
2315 memcpy(&pszStdErrBuf[offStdErrBuf], achBuf, cbRead);
2316 offStdErrBuf = offStdErrBuf + cbRead;
2317 pszStdErrBuf[offStdErrBuf] = '\0';
2318 }
2319 }
2320 else
2321 {
2322 AssertLogRelMsg(vrc == VERR_BROKEN_PIPE, ("%Rrc\n", vrc));
2323 RTPipeClose(hPipeR);
2324 hPipeR = NIL_RTPIPE;
2325 }
2326 }
2327
2328 /*
2329 * Service the process. Block if we have no pipe.
2330 */
2331 if (hProcess != NIL_RTPROCESS)
2332 {
2333 vrc = RTProcWait(hProcess,
2334 hPipeR == NIL_RTPIPE ? RTPROCWAIT_FLAGS_BLOCK : RTPROCWAIT_FLAGS_NOBLOCK,
2335 &ProcStatus);
2336 if (RT_SUCCESS(vrc))
2337 hProcess = NIL_RTPROCESS;
2338 else
2339 AssertLogRelMsgStmt(vrc == VERR_PROCESS_RUNNING, ("%Rrc\n", vrc), hProcess = NIL_RTPROCESS);
2340 }
2341 } while ( hPipeR != NIL_RTPIPE
2342 || hProcess != NIL_RTPROCESS);
2343
2344 LogRel(("ExtPack: enmReason=%d iStatus=%d stderr='%s'\n",
2345 ProcStatus.enmReason, ProcStatus.iStatus, offStdErrBuf ? pszStdErrBuf : ""));
2346
2347 /*
2348 * Look for rcExit=RTEXITCODE_SUCCESS at the end of the error output,
2349 * cut it as it is only there to attest the success.
2350 */
2351 if (offStdErrBuf > 0)
2352 {
2353 RTStrStripR(pszStdErrBuf);
2354 offStdErrBuf = strlen(pszStdErrBuf);
2355 }
2356
2357 char *pszSuccessInd = findSuccessIndicator(pszStdErrBuf, offStdErrBuf);
2358 if (pszSuccessInd)
2359 {
2360 *pszSuccessInd = '\0';
2361 offStdErrBuf = pszSuccessInd - pszStdErrBuf;
2362 }
2363 else if ( ProcStatus.enmReason == RTPROCEXITREASON_NORMAL
2364 && ProcStatus.iStatus == 0)
2365 ProcStatus.iStatus = offStdErrBuf ? 667 : 666;
2366
2367 /*
2368 * Compose the status code and, on failure, error message.
2369 */
2370 if ( ProcStatus.enmReason == RTPROCEXITREASON_NORMAL
2371 && ProcStatus.iStatus == 0)
2372 hrc = S_OK;
2373 else if (ProcStatus.enmReason == RTPROCEXITREASON_NORMAL)
2374 {
2375 AssertMsg(ProcStatus.iStatus != 0, ("%s\n", pszStdErrBuf));
2376 hrc = setError(E_FAIL, tr("The installer failed with exit code %d: %s"),
2377 ProcStatus.iStatus, offStdErrBuf ? pszStdErrBuf : "");
2378 }
2379 else if (ProcStatus.enmReason == RTPROCEXITREASON_SIGNAL)
2380 hrc = setError(E_UNEXPECTED, tr("The installer was killed by signal #d (stderr: %s)"),
2381 ProcStatus.iStatus, offStdErrBuf ? pszStdErrBuf : "");
2382 else if (ProcStatus.enmReason == RTPROCEXITREASON_ABEND)
2383 hrc = setError(E_UNEXPECTED, tr("The installer aborted abnormally (stderr: %s)"),
2384 offStdErrBuf ? pszStdErrBuf : "");
2385 else
2386 hrc = setError(E_UNEXPECTED, tr("internal error: enmReason=%d iStatus=%d stderr='%s'"),
2387 ProcStatus.enmReason, ProcStatus.iStatus, offStdErrBuf ? pszStdErrBuf : "");
2388
2389 RTMemFree(pszStdErrBuf);
2390 }
2391 else
2392 hrc = setError(VBOX_E_IPRT_ERROR, tr("Failed to launch the helper application '%s' (%Rrc)"), szExecName, vrc);
2393
2394 RTPipeClose(hPipeR);
2395 RTPipeClose(hStdErrPipe.u.hPipe);
2396
2397 return hrc;
2398}
2399
2400/**
2401 * Finds an installed extension pack.
2402 *
2403 * @returns Pointer to the extension pack if found, NULL if not. (No reference
2404 * counting problem here since the caller must be holding the lock.)
2405 * @param a_pszName The name of the extension pack.
2406 */
2407ExtPack *ExtPackManager::findExtPack(const char *a_pszName)
2408{
2409 size_t cchName = strlen(a_pszName);
2410
2411 for (ExtPackList::iterator it = m->llInstalledExtPacks.begin();
2412 it != m->llInstalledExtPacks.end();
2413 it++)
2414 {
2415 ExtPack::Data *pExtPackData = (*it)->m;
2416 if ( pExtPackData
2417 && pExtPackData->Desc.strName.length() == cchName
2418 && pExtPackData->Desc.strName.equalsIgnoreCase(a_pszName))
2419 return (*it);
2420 }
2421 return NULL;
2422}
2423
2424/**
2425 * Removes an installed extension pack from the internal list.
2426 *
2427 * The package is expected to exist!
2428 *
2429 * @param a_pszName The name of the extension pack.
2430 */
2431void ExtPackManager::removeExtPack(const char *a_pszName)
2432{
2433 size_t cchName = strlen(a_pszName);
2434
2435 for (ExtPackList::iterator it = m->llInstalledExtPacks.begin();
2436 it != m->llInstalledExtPacks.end();
2437 it++)
2438 {
2439 ExtPack::Data *pExtPackData = (*it)->m;
2440 if ( pExtPackData
2441 && pExtPackData->Desc.strName.length() == cchName
2442 && pExtPackData->Desc.strName.equalsIgnoreCase(a_pszName))
2443 {
2444 m->llInstalledExtPacks.erase(it);
2445 return;
2446 }
2447 }
2448 AssertMsgFailed(("%s\n", a_pszName));
2449}
2450
2451/**
2452 * Refreshes the specified extension pack.
2453 *
2454 * This may remove the extension pack from the list, so any non-smart pointers
2455 * to the extension pack object may become invalid.
2456 *
2457 * @returns S_OK and *a_ppExtPack on success, COM status code and error
2458 * message on failure. Note that *a_ppExtPack can be NULL.
2459 *
2460 * @param a_pszName The extension to update..
2461 * @param a_fUnusableIsError If @c true, report an unusable extension pack
2462 * as an error.
2463 * @param a_ppExtPack Where to store the pointer to the extension
2464 * pack of it is still around after the refresh.
2465 * This is optional.
2466 *
2467 * @remarks Caller holds the extension manager lock.
2468 * @remarks Only called in VBoxSVC.
2469 */
2470HRESULT ExtPackManager::refreshExtPack(const char *a_pszName, bool a_fUnusableIsError, ExtPack **a_ppExtPack)
2471{
2472 Assert(m->pVirtualBox != NULL); /* Only called from VBoxSVC. */
2473
2474 HRESULT hrc;
2475 ExtPack *pExtPack = findExtPack(a_pszName);
2476 if (pExtPack)
2477 {
2478 /*
2479 * Refresh existing object.
2480 */
2481 bool fCanDelete;
2482 hrc = pExtPack->refresh(&fCanDelete);
2483 if (SUCCEEDED(hrc))
2484 {
2485 if (fCanDelete)
2486 {
2487 removeExtPack(a_pszName);
2488 pExtPack = NULL;
2489 }
2490 }
2491 }
2492 else
2493 {
2494 /*
2495 * Do this check here, otherwise VBoxExtPackCalcDir() will fail with a strange
2496 * error.
2497 */
2498 bool fValid = VBoxExtPackIsValidName(a_pszName);
2499 if (!fValid)
2500 return setError(E_FAIL, "Invalid extension pack name specified");
2501
2502 /*
2503 * Does the dir exist? Make some special effort to deal with case
2504 * sensitivie file systems (a_pszName is case insensitive and mangled).
2505 */
2506 char szDir[RTPATH_MAX];
2507 int vrc = VBoxExtPackCalcDir(szDir, sizeof(szDir), m->strBaseDir.c_str(), a_pszName);
2508 AssertLogRelRCReturn(vrc, E_FAIL);
2509
2510 RTDIRENTRYEX Entry;
2511 RTFSOBJINFO ObjInfo;
2512 vrc = RTPathQueryInfoEx(szDir, &ObjInfo, RTFSOBJATTRADD_NOTHING, RTPATH_F_ON_LINK);
2513 bool fExists = RT_SUCCESS(vrc) && RTFS_IS_DIRECTORY(ObjInfo.Attr.fMode);
2514 if (!fExists)
2515 {
2516 PRTDIR pDir;
2517 vrc = RTDirOpen(&pDir, m->strBaseDir.c_str());
2518 if (RT_SUCCESS(vrc))
2519 {
2520 const char *pszMangledName = RTPathFilename(szDir);
2521 for (;;)
2522 {
2523 vrc = RTDirReadEx(pDir, &Entry, NULL /*pcbDirEntry*/, RTFSOBJATTRADD_NOTHING, RTPATH_F_ON_LINK);
2524 if (RT_FAILURE(vrc))
2525 {
2526 AssertLogRelMsg(vrc == VERR_NO_MORE_FILES, ("%Rrc\n", vrc));
2527 break;
2528 }
2529 if ( RTFS_IS_DIRECTORY(Entry.Info.Attr.fMode)
2530 && !RTStrICmp(Entry.szName, pszMangledName))
2531 {
2532 /*
2533 * The installed extension pack has a uses different case.
2534 * Update the name and directory variables.
2535 */
2536 vrc = RTPathJoin(szDir, sizeof(szDir), m->strBaseDir.c_str(), Entry.szName); /* not really necessary */
2537 AssertLogRelRCReturnStmt(vrc, RTDirClose(pDir), E_UNEXPECTED);
2538 a_pszName = Entry.szName;
2539 fExists = true;
2540 break;
2541 }
2542 }
2543 RTDirClose(pDir);
2544 }
2545 }
2546 if (fExists)
2547 {
2548 /*
2549 * We've got something, create a new extension pack object for it.
2550 */
2551 ComObjPtr<ExtPack> ptrNewExtPack;
2552 hrc = ptrNewExtPack.createObject();
2553 if (SUCCEEDED(hrc))
2554 hrc = ptrNewExtPack->initWithDir(m->enmContext, a_pszName, szDir);
2555 if (SUCCEEDED(hrc))
2556 {
2557 m->llInstalledExtPacks.push_back(ptrNewExtPack);
2558 if (ptrNewExtPack->m->fUsable)
2559 LogRel(("ExtPackManager: Found extension pack '%s'.\n", a_pszName));
2560 else
2561 LogRel(("ExtPackManager: Found bad extension pack '%s': %s\n",
2562 a_pszName, ptrNewExtPack->m->strWhyUnusable.c_str() ));
2563 pExtPack = ptrNewExtPack;
2564 }
2565 }
2566 else
2567 hrc = S_OK;
2568 }
2569
2570 /*
2571 * Report error if not usable, if that is desired.
2572 */
2573 if ( SUCCEEDED(hrc)
2574 && pExtPack
2575 && a_fUnusableIsError
2576 && !pExtPack->m->fUsable)
2577 hrc = setError(E_FAIL, "%s", pExtPack->m->strWhyUnusable.c_str());
2578
2579 if (a_ppExtPack)
2580 *a_ppExtPack = pExtPack;
2581 return hrc;
2582}
2583
2584/**
2585 * Thread wrapper around doInstall.
2586 *
2587 * @returns VINF_SUCCESS (ignored)
2588 * @param hThread The thread handle (ignored).
2589 * @param pvJob The job structure.
2590 */
2591/*static*/ DECLCALLBACK(int) ExtPackManager::doInstallThreadProc(RTTHREAD hThread, void *pvJob)
2592{
2593 PEXTPACKINSTALLJOB pJob = (PEXTPACKINSTALLJOB)pvJob;
2594 HRESULT hrc = pJob->ptrExtPackMgr->doInstall(pJob->ptrExtPackFile, pJob->fReplace, &pJob->strDisplayInfo);
2595 pJob->ptrProgress->notifyComplete(hrc);
2596 delete pJob;
2597
2598 NOREF(hThread);
2599 return VINF_SUCCESS;
2600}
2601
2602/**
2603 * Worker for IExtPackFile::Install.
2604 *
2605 * Called on a worker thread via doInstallThreadProc.
2606 *
2607 * @returns COM status code.
2608 * @param a_pExtPackFile The extension pack file, caller checks that
2609 * it's usable.
2610 * @param a_fReplace Whether to replace any existing extpack or just
2611 * fail.
2612 * @param a_pstrDisplayInfo Host specific display information hacks.
2613 * @param a_ppProgress Where to return a progress object some day. Can
2614 * be NULL.
2615 */
2616HRESULT ExtPackManager::doInstall(ExtPackFile *a_pExtPackFile, bool a_fReplace, Utf8Str const *a_pstrDisplayInfo)
2617{
2618 AssertReturn(m->enmContext == VBOXEXTPACKCTX_PER_USER_DAEMON, E_UNEXPECTED);
2619 RTCString const * const pStrName = &a_pExtPackFile->m->Desc.strName;
2620 RTCString const * const pStrTarball = &a_pExtPackFile->m->strExtPackFile;
2621 RTCString const * const pStrTarballDigest = &a_pExtPackFile->m->strDigest;
2622
2623 AutoCaller autoCaller(this);
2624 HRESULT hrc = autoCaller.rc();
2625 if (SUCCEEDED(hrc))
2626 {
2627 AutoWriteLock autoLock(this COMMA_LOCKVAL_SRC_POS);
2628
2629 /*
2630 * Refresh the data we have on the extension pack as it
2631 * may be made stale by direct meddling or some other user.
2632 */
2633 ExtPack *pExtPack;
2634 hrc = refreshExtPack(pStrName->c_str(), false /*a_fUnusableIsError*/, &pExtPack);
2635 if (SUCCEEDED(hrc))
2636 {
2637 if (pExtPack && a_fReplace)
2638 hrc = pExtPack->callUninstallHookAndClose(m->pVirtualBox, false /*a_ForcedRemoval*/);
2639 else if (pExtPack)
2640 hrc = setError(E_FAIL,
2641 tr("Extension pack '%s' is already installed."
2642 " In case of a reinstallation, please uninstall it first"),
2643 pStrName->c_str());
2644 }
2645 if (SUCCEEDED(hrc))
2646 {
2647 /*
2648 * Run the privileged helper binary that performs the actual
2649 * installation. Then create an object for the packet (we do this
2650 * even on failure, to be on the safe side).
2651 */
2652 hrc = runSetUidToRootHelper(a_pstrDisplayInfo,
2653 "install",
2654 "--base-dir", m->strBaseDir.c_str(),
2655 "--cert-dir", m->strCertificatDirPath.c_str(),
2656 "--name", pStrName->c_str(),
2657 "--tarball", pStrTarball->c_str(),
2658 "--sha-256", pStrTarballDigest->c_str(),
2659 pExtPack ? "--replace" : (const char *)NULL,
2660 (const char *)NULL);
2661 if (SUCCEEDED(hrc))
2662 {
2663 hrc = refreshExtPack(pStrName->c_str(), true /*a_fUnusableIsError*/, &pExtPack);
2664 if (SUCCEEDED(hrc) && pExtPack)
2665 {
2666 RTERRINFOSTATIC ErrInfo;
2667 RTErrInfoInitStatic(&ErrInfo);
2668 pExtPack->callInstalledHook(m->pVirtualBox, &autoLock, &ErrInfo.Core);
2669 if (RT_SUCCESS(ErrInfo.Core.rc))
2670 LogRel(("ExtPackManager: Successfully installed extension pack '%s'.\n", pStrName->c_str()));
2671 else
2672 {
2673 LogRel(("ExtPackManager: Installed hook for '%s' failed: %Rrc - %s\n",
2674 pStrName->c_str(), ErrInfo.Core.rc, ErrInfo.Core.pszMsg));
2675
2676 /*
2677 * Uninstall the extpack if the error indicates that.
2678 */
2679 if (ErrInfo.Core.rc == VERR_EXTPACK_UNSUPPORTED_HOST_UNINSTALL)
2680 runSetUidToRootHelper(a_pstrDisplayInfo,
2681 "uninstall",
2682 "--base-dir", m->strBaseDir.c_str(),
2683 "--name", pStrName->c_str(),
2684 "--forced",
2685 (const char *)NULL);
2686 hrc = setError(E_FAIL, tr("The installation hook failed: %Rrc - %s"),
2687 ErrInfo.Core.rc, ErrInfo.Core.pszMsg);
2688 }
2689 }
2690 else if (SUCCEEDED(hrc))
2691 hrc = setError(E_FAIL, tr("Installing extension pack '%s' failed under mysterious circumstances"),
2692 pStrName->c_str());
2693 }
2694 else
2695 {
2696 ErrorInfoKeeper Eik;
2697 refreshExtPack(pStrName->c_str(), false /*a_fUnusableIsError*/, NULL);
2698 }
2699 }
2700
2701 /*
2702 * Do VirtualBoxReady callbacks now for any freshly installed
2703 * extension pack (old ones will not be called).
2704 */
2705 if (m->enmContext == VBOXEXTPACKCTX_PER_USER_DAEMON)
2706 {
2707 autoLock.release();
2708 callAllVirtualBoxReadyHooks();
2709 }
2710 }
2711
2712 return hrc;
2713}
2714
2715/**
2716 * Thread wrapper around doUninstall.
2717 *
2718 * @returns VINF_SUCCESS (ignored)
2719 * @param hThread The thread handle (ignored).
2720 * @param pvJob The job structure.
2721 */
2722/*static*/ DECLCALLBACK(int) ExtPackManager::doUninstallThreadProc(RTTHREAD hThread, void *pvJob)
2723{
2724 PEXTPACKUNINSTALLJOB pJob = (PEXTPACKUNINSTALLJOB)pvJob;
2725 HRESULT hrc = pJob->ptrExtPackMgr->doUninstall(&pJob->strName, pJob->fForcedRemoval, &pJob->strDisplayInfo);
2726 pJob->ptrProgress->notifyComplete(hrc);
2727 delete pJob;
2728
2729 NOREF(hThread);
2730 return VINF_SUCCESS;
2731}
2732
2733/**
2734 * Worker for IExtPackManager::Uninstall.
2735 *
2736 * Called on a worker thread via doUninstallThreadProc.
2737 *
2738 * @returns COM status code.
2739 * @param a_pstrName The name of the extension pack to uninstall.
2740 * @param a_fForcedRemoval Whether to be skip and ignore certain bits of
2741 * the extpack feedback. To deal with misbehaving
2742 * extension pack hooks.
2743 * @param a_pstrDisplayInfo Host specific display information hacks.
2744 */
2745HRESULT ExtPackManager::doUninstall(Utf8Str const *a_pstrName, bool a_fForcedRemoval, Utf8Str const *a_pstrDisplayInfo)
2746{
2747 Assert(m->enmContext == VBOXEXTPACKCTX_PER_USER_DAEMON);
2748
2749 AutoCaller autoCaller(this);
2750 HRESULT hrc = autoCaller.rc();
2751 if (SUCCEEDED(hrc))
2752 {
2753 AutoWriteLock autoLock(this COMMA_LOCKVAL_SRC_POS);
2754
2755 /*
2756 * Refresh the data we have on the extension pack as it may be made
2757 * stale by direct meddling or some other user.
2758 */
2759 ExtPack *pExtPack;
2760 hrc = refreshExtPack(a_pstrName->c_str(), false /*a_fUnusableIsError*/, &pExtPack);
2761 if (SUCCEEDED(hrc))
2762 {
2763 if (!pExtPack)
2764 {
2765 LogRel(("ExtPackManager: Extension pack '%s' is not installed, so nothing to uninstall.\n", a_pstrName->c_str()));
2766 hrc = S_OK; /* nothing to uninstall */
2767 }
2768 else
2769 {
2770 /*
2771 * Call the uninstall hook and unload the main dll.
2772 */
2773 hrc = pExtPack->callUninstallHookAndClose(m->pVirtualBox, a_fForcedRemoval);
2774 if (SUCCEEDED(hrc))
2775 {
2776 /*
2777 * Run the set-uid-to-root binary that performs the
2778 * uninstallation. Then refresh the object.
2779 *
2780 * This refresh is theorically subject to races, but it's of
2781 * the don't-do-that variety.
2782 */
2783 const char *pszForcedOpt = a_fForcedRemoval ? "--forced" : NULL;
2784 hrc = runSetUidToRootHelper(a_pstrDisplayInfo,
2785 "uninstall",
2786 "--base-dir", m->strBaseDir.c_str(),
2787 "--name", a_pstrName->c_str(),
2788 pszForcedOpt, /* Last as it may be NULL. */
2789 (const char *)NULL);
2790 if (SUCCEEDED(hrc))
2791 {
2792 hrc = refreshExtPack(a_pstrName->c_str(), false /*a_fUnusableIsError*/, &pExtPack);
2793 if (SUCCEEDED(hrc))
2794 {
2795 if (!pExtPack)
2796 LogRel(("ExtPackManager: Successfully uninstalled extension pack '%s'.\n", a_pstrName->c_str()));
2797 else
2798 hrc = setError(E_FAIL,
2799 tr("Uninstall extension pack '%s' failed under mysterious circumstances"),
2800 a_pstrName->c_str());
2801 }
2802 }
2803 else
2804 {
2805 ErrorInfoKeeper Eik;
2806 refreshExtPack(a_pstrName->c_str(), false /*a_fUnusableIsError*/, NULL);
2807 }
2808 }
2809 }
2810 }
2811
2812 /*
2813 * Do VirtualBoxReady callbacks now for any freshly installed
2814 * extension pack (old ones will not be called).
2815 */
2816 if (m->enmContext == VBOXEXTPACKCTX_PER_USER_DAEMON)
2817 {
2818 autoLock.release();
2819 callAllVirtualBoxReadyHooks();
2820 }
2821 }
2822
2823 return hrc;
2824}
2825
2826
2827/**
2828 * Calls the pfnVirtualBoxReady hook for all working extension packs.
2829 *
2830 * @remarks The caller must not hold any locks.
2831 */
2832void ExtPackManager::callAllVirtualBoxReadyHooks(void)
2833{
2834 AutoCaller autoCaller(this);
2835 HRESULT hrc = autoCaller.rc();
2836 if (FAILED(hrc))
2837 return;
2838 AutoWriteLock autoLock(this COMMA_LOCKVAL_SRC_POS);
2839 ComPtr<ExtPackManager> ptrSelfRef = this;
2840
2841 for (ExtPackList::iterator it = m->llInstalledExtPacks.begin();
2842 it != m->llInstalledExtPacks.end();
2843 /* advancing below */)
2844 {
2845 if ((*it)->callVirtualBoxReadyHook(m->pVirtualBox, &autoLock))
2846 it = m->llInstalledExtPacks.begin();
2847 else
2848 it++;
2849 }
2850}
2851
2852/**
2853 * Calls the pfnConsoleReady hook for all working extension packs.
2854 *
2855 * @param a_pConsole The console interface.
2856 * @remarks The caller must not hold any locks.
2857 */
2858void ExtPackManager::callAllConsoleReadyHooks(IConsole *a_pConsole)
2859{
2860 AutoCaller autoCaller(this);
2861 HRESULT hrc = autoCaller.rc();
2862 if (FAILED(hrc))
2863 return;
2864 AutoWriteLock autoLock(this COMMA_LOCKVAL_SRC_POS);
2865 ComPtr<ExtPackManager> ptrSelfRef = this;
2866
2867 for (ExtPackList::iterator it = m->llInstalledExtPacks.begin();
2868 it != m->llInstalledExtPacks.end();
2869 /* advancing below */)
2870 {
2871 if ((*it)->callConsoleReadyHook(a_pConsole, &autoLock))
2872 it = m->llInstalledExtPacks.begin();
2873 else
2874 it++;
2875 }
2876}
2877
2878/**
2879 * Calls the pfnVMCreated hook for all working extension packs.
2880 *
2881 * @param a_pMachine The machine interface of the new VM.
2882 */
2883void ExtPackManager::callAllVmCreatedHooks(IMachine *a_pMachine)
2884{
2885 AutoCaller autoCaller(this);
2886 HRESULT hrc = autoCaller.rc();
2887 if (FAILED(hrc))
2888 return;
2889 AutoWriteLock autoLock(this COMMA_LOCKVAL_SRC_POS);
2890 ComPtr<ExtPackManager> ptrSelfRef = this; /* paranoia */
2891 ExtPackList llExtPacks = m->llInstalledExtPacks;
2892
2893 for (ExtPackList::iterator it = llExtPacks.begin(); it != llExtPacks.end(); it++)
2894 (*it)->callVmCreatedHook(m->pVirtualBox, a_pMachine, &autoLock);
2895}
2896
2897/**
2898 * Calls the pfnVMConfigureVMM hook for all working extension packs.
2899 *
2900 * @returns VBox status code. Stops on the first failure, expecting the caller
2901 * to signal this to the caller of the CFGM constructor.
2902 * @param a_pConsole The console interface for the VM.
2903 * @param a_pVM The VM handle.
2904 */
2905int ExtPackManager::callAllVmConfigureVmmHooks(IConsole *a_pConsole, PVM a_pVM)
2906{
2907 AutoCaller autoCaller(this);
2908 HRESULT hrc = autoCaller.rc();
2909 if (FAILED(hrc))
2910 return Global::vboxStatusCodeFromCOM(hrc);
2911 AutoWriteLock autoLock(this COMMA_LOCKVAL_SRC_POS);
2912 ComPtr<ExtPackManager> ptrSelfRef = this; /* paranoia */
2913 ExtPackList llExtPacks = m->llInstalledExtPacks;
2914
2915 for (ExtPackList::iterator it = llExtPacks.begin(); it != llExtPacks.end(); it++)
2916 {
2917 int vrc;
2918 (*it)->callVmConfigureVmmHook(a_pConsole, a_pVM, &autoLock, &vrc);
2919 if (RT_FAILURE(vrc))
2920 return vrc;
2921 }
2922
2923 return VINF_SUCCESS;
2924}
2925
2926/**
2927 * Calls the pfnVMPowerOn hook for all working extension packs.
2928 *
2929 * @returns VBox status code. Stops on the first failure, expecting the caller
2930 * to not power on the VM.
2931 * @param a_pConsole The console interface for the VM.
2932 * @param a_pVM The VM handle.
2933 */
2934int ExtPackManager::callAllVmPowerOnHooks(IConsole *a_pConsole, PVM a_pVM)
2935{
2936 AutoCaller autoCaller(this);
2937 HRESULT hrc = autoCaller.rc();
2938 if (FAILED(hrc))
2939 return Global::vboxStatusCodeFromCOM(hrc);
2940 AutoWriteLock autoLock(this COMMA_LOCKVAL_SRC_POS);
2941 ComPtr<ExtPackManager> ptrSelfRef = this; /* paranoia */
2942 ExtPackList llExtPacks = m->llInstalledExtPacks;
2943
2944 for (ExtPackList::iterator it = llExtPacks.begin(); it != llExtPacks.end(); it++)
2945 {
2946 int vrc;
2947 (*it)->callVmPowerOnHook(a_pConsole, a_pVM, &autoLock, &vrc);
2948 if (RT_FAILURE(vrc))
2949 return vrc;
2950 }
2951
2952 return VINF_SUCCESS;
2953}
2954
2955/**
2956 * Calls the pfnVMPowerOff hook for all working extension packs.
2957 *
2958 * @param a_pConsole The console interface for the VM.
2959 * @param a_pVM The VM handle. Can be NULL.
2960 */
2961void ExtPackManager::callAllVmPowerOffHooks(IConsole *a_pConsole, PVM a_pVM)
2962{
2963 AutoCaller autoCaller(this);
2964 HRESULT hrc = autoCaller.rc();
2965 if (FAILED(hrc))
2966 return;
2967 AutoWriteLock autoLock(this COMMA_LOCKVAL_SRC_POS);
2968 ComPtr<ExtPackManager> ptrSelfRef = this; /* paranoia */
2969 ExtPackList llExtPacks = m->llInstalledExtPacks;
2970
2971 for (ExtPackList::iterator it = llExtPacks.begin(); it != llExtPacks.end(); it++)
2972 (*it)->callVmPowerOffHook(a_pConsole, a_pVM, &autoLock);
2973}
2974
2975
2976/**
2977 * Checks that the specified extension pack contains a VRDE module and that it
2978 * is shipshape.
2979 *
2980 * @returns S_OK if ok, appropriate failure status code with details.
2981 * @param a_pstrExtPack The name of the extension pack.
2982 */
2983HRESULT ExtPackManager::checkVrdeExtPack(Utf8Str const *a_pstrExtPack)
2984{
2985 AutoCaller autoCaller(this);
2986 HRESULT hrc = autoCaller.rc();
2987 if (SUCCEEDED(hrc))
2988 {
2989 AutoReadLock autoLock(this COMMA_LOCKVAL_SRC_POS);
2990
2991 ExtPack *pExtPack = findExtPack(a_pstrExtPack->c_str());
2992 if (pExtPack)
2993 hrc = pExtPack->checkVrde();
2994 else
2995 hrc = setError(VBOX_E_OBJECT_NOT_FOUND, tr("No extension pack by the name '%s' was found"), a_pstrExtPack->c_str());
2996 }
2997
2998 return hrc;
2999}
3000
3001/**
3002 * Gets the full path to the VRDE library of the specified extension pack.
3003 *
3004 * This will do extacly the same as checkVrdeExtPack and then resolve the
3005 * library path.
3006 *
3007 * @returns S_OK if a path is returned, COM error status and message return if
3008 * not.
3009 * @param a_pstrExtPack The extension pack.
3010 * @param a_pstrVrdeLibrary Where to return the path.
3011 */
3012int ExtPackManager::getVrdeLibraryPathForExtPack(Utf8Str const *a_pstrExtPack, Utf8Str *a_pstrVrdeLibrary)
3013{
3014 AutoCaller autoCaller(this);
3015 HRESULT hrc = autoCaller.rc();
3016 if (SUCCEEDED(hrc))
3017 {
3018 AutoReadLock autoLock(this COMMA_LOCKVAL_SRC_POS);
3019
3020 ExtPack *pExtPack = findExtPack(a_pstrExtPack->c_str());
3021 if (pExtPack)
3022 hrc = pExtPack->getVrdpLibraryName(a_pstrVrdeLibrary);
3023 else
3024 hrc = setError(VBOX_E_OBJECT_NOT_FOUND, tr("No extension pack by the name '%s' was found"), a_pstrExtPack->c_str());
3025 }
3026
3027 return hrc;
3028}
3029
3030/**
3031 * Gets the name of the default VRDE extension pack.
3032 *
3033 * @returns S_OK or some COM error status on red tape failure.
3034 * @param a_pstrExtPack Where to return the extension pack name. Returns
3035 * empty if no extension pack wishes to be the default
3036 * VRDP provider.
3037 */
3038HRESULT ExtPackManager::getDefaultVrdeExtPack(Utf8Str *a_pstrExtPack)
3039{
3040 a_pstrExtPack->setNull();
3041
3042 AutoCaller autoCaller(this);
3043 HRESULT hrc = autoCaller.rc();
3044 if (SUCCEEDED(hrc))
3045 {
3046 AutoReadLock autoLock(this COMMA_LOCKVAL_SRC_POS);
3047
3048 for (ExtPackList::iterator it = m->llInstalledExtPacks.begin();
3049 it != m->llInstalledExtPacks.end();
3050 it++)
3051 {
3052 if ((*it)->wantsToBeDefaultVrde())
3053 {
3054 *a_pstrExtPack = (*it)->m->Desc.strName;
3055 break;
3056 }
3057 }
3058 }
3059 return hrc;
3060}
3061
3062/**
3063 * Checks if an extension pack is (present and) usable.
3064 *
3065 * @returns @c true if it is, otherwise @c false.
3066 * @param a_pszExtPack The name of the extension pack.
3067 */
3068bool ExtPackManager::isExtPackUsable(const char *a_pszExtPack)
3069{
3070 AutoCaller autoCaller(this);
3071 HRESULT hrc = autoCaller.rc();
3072 if (FAILED(hrc))
3073 return false;
3074 AutoReadLock autoLock(this COMMA_LOCKVAL_SRC_POS);
3075
3076 ExtPack *pExtPack = findExtPack(a_pszExtPack);
3077 return pExtPack != NULL
3078 && pExtPack->m->fUsable;
3079}
3080
3081/**
3082 * Dumps all extension packs to the release log.
3083 */
3084void ExtPackManager::dumpAllToReleaseLog(void)
3085{
3086 AutoCaller autoCaller(this);
3087 HRESULT hrc = autoCaller.rc();
3088 if (FAILED(hrc))
3089 return;
3090 AutoReadLock autoLock(this COMMA_LOCKVAL_SRC_POS);
3091
3092 LogRel(("Installed Extension Packs:\n"));
3093 for (ExtPackList::iterator it = m->llInstalledExtPacks.begin();
3094 it != m->llInstalledExtPacks.end();
3095 it++)
3096 {
3097 ExtPack::Data *pExtPackData = (*it)->m;
3098 if (pExtPackData)
3099 {
3100 if (pExtPackData->fUsable)
3101 LogRel((" %s (Version: %s r%u%s%s; VRDE Module: %s)\n",
3102 pExtPackData->Desc.strName.c_str(),
3103 pExtPackData->Desc.strVersion.c_str(),
3104 pExtPackData->Desc.uRevision,
3105 pExtPackData->Desc.strEdition.isEmpty() ? "" : " ",
3106 pExtPackData->Desc.strEdition.c_str(),
3107 pExtPackData->Desc.strVrdeModule.c_str() ));
3108 else
3109 LogRel((" %s (Version: %s r%u%s%s; VRDE Module: %s unusable because of '%s')\n",
3110 pExtPackData->Desc.strName.c_str(),
3111 pExtPackData->Desc.strVersion.c_str(),
3112 pExtPackData->Desc.uRevision,
3113 pExtPackData->Desc.strEdition.isEmpty() ? "" : " ",
3114 pExtPackData->Desc.strEdition.c_str(),
3115 pExtPackData->Desc.strVrdeModule.c_str(),
3116 pExtPackData->strWhyUnusable.c_str() ));
3117 }
3118 else
3119 LogRel((" pExtPackData is NULL\n"));
3120 }
3121
3122 if (!m->llInstalledExtPacks.size())
3123 LogRel((" None installed!\n"));
3124}
3125
3126/* vi: set tabstop=4 shiftwidth=4 expandtab: */
Note: See TracBrowser for help on using the repository browser.

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