VirtualBox

source: vbox/trunk/src/VBox/Main/ExtPackManagerImpl.cpp@ 35128

Last change on this file since 35128 was 35100, checked in by vboxsync, 14 years ago

IExtPackFile::Install: Added a 'replace' parameter for automatically trying to uninstall an existing extension pack during upgrade.

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