VirtualBox

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

Last change on this file since 37952 was 37843, checked in by vboxsync, 13 years ago

Moved the extension pack release logging from ConsoleImpl.cpp to ExtPackManagerImpl.cpp.

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