VirtualBox

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

Last change on this file since 35061 was 35013, checked in by vboxsync, 14 years ago

Extpack: fix error reporting

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 91.0 KB
Line 
1/* $Id: ExtPackManagerImpl.cpp 35013 2010-12-13 14:05:40Z 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(void)
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);
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 /** @todo not important, so it can wait. */
1065 }
1066 /*
1067 * Ok, it is currently not usable. If anything has changed since last time
1068 * reprobe the extension pack.
1069 */
1070 else if ( !objinfoIsEqual(&ObjInfoDesc, &m->ObjInfoDesc)
1071 || !objinfoIsEqual(&ObjInfoMainMod, &m->ObjInfoMainMod)
1072 || !objinfoIsEqual(&ObjInfoExtPack, &m->ObjInfoExtPack) )
1073 probeAndLoad();
1074
1075 return S_OK;
1076}
1077
1078/**
1079 * Probes the extension pack, loading the main dll and calling its registration
1080 * entry point.
1081 *
1082 * This updates the state accordingly, the strWhyUnusable and fUnusable members
1083 * being the most important ones.
1084 */
1085void ExtPack::probeAndLoad(void)
1086{
1087 m->fUsable = false;
1088 m->fMadeReadyCall = false;
1089
1090 /*
1091 * Query the file system info for the extension pack directory. This and
1092 * all other file system info we save is for the benefit of refresh().
1093 */
1094 int vrc = RTPathQueryInfoEx(m->strExtPackPath.c_str(), &m->ObjInfoExtPack, RTFSOBJATTRADD_UNIX, RTPATH_F_ON_LINK);
1095 if (RT_FAILURE(vrc))
1096 {
1097 m->strWhyUnusable.printf(tr("RTPathQueryInfoEx on '%s' failed: %Rrc"), m->strExtPackPath.c_str(), vrc);
1098 return;
1099 }
1100 if (!RTFS_IS_DIRECTORY(m->ObjInfoExtPack.Attr.fMode))
1101 {
1102 if (RTFS_IS_SYMLINK(m->ObjInfoExtPack.Attr.fMode))
1103 m->strWhyUnusable.printf(tr("'%s' is a symbolic link, this is not allowed"), m->strExtPackPath.c_str(), vrc);
1104 else if (RTFS_IS_FILE(m->ObjInfoExtPack.Attr.fMode))
1105 m->strWhyUnusable.printf(tr("'%s' is a symbolic file, not a directory"), m->strExtPackPath.c_str(), vrc);
1106 else
1107 m->strWhyUnusable.printf(tr("'%s' is not a directory (fMode=%#x)"), m->strExtPackPath.c_str(), m->ObjInfoExtPack.Attr.fMode);
1108 return;
1109 }
1110
1111 char szErr[2048];
1112 RT_ZERO(szErr);
1113 vrc = SUPR3HardenedVerifyDir(m->strExtPackPath.c_str(), true /*fRecursive*/, true /*fCheckFiles*/, szErr, sizeof(szErr));
1114 if (RT_FAILURE(vrc))
1115 {
1116 m->strWhyUnusable.printf(tr("%s (rc=%Rrc)"), szErr, vrc);
1117 return;
1118 }
1119
1120 /*
1121 * Read the description file.
1122 */
1123 iprt::MiniString strSavedName(m->Desc.strName);
1124 iprt::MiniString *pStrLoadErr = VBoxExtPackLoadDesc(m->strExtPackPath.c_str(), &m->Desc, &m->ObjInfoDesc);
1125 if (pStrLoadErr != NULL)
1126 {
1127 m->strWhyUnusable.printf(tr("Failed to load '%s/%s': %s"),
1128 m->strExtPackPath.c_str(), VBOX_EXTPACK_DESCRIPTION_NAME, pStrLoadErr->c_str());
1129 m->Desc.strName = strSavedName;
1130 delete pStrLoadErr;
1131 return;
1132 }
1133
1134 /*
1135 * Make sure the XML name and directory matches.
1136 */
1137 if (!m->Desc.strName.equalsIgnoreCase(strSavedName))
1138 {
1139 m->strWhyUnusable.printf(tr("The description name ('%s') and directory name ('%s') does not match"),
1140 m->Desc.strName.c_str(), strSavedName.c_str());
1141 m->Desc.strName = strSavedName;
1142 return;
1143 }
1144
1145 /*
1146 * Load the main DLL and call the predefined entry point.
1147 */
1148 bool fIsNative;
1149 if (!findModule(m->Desc.strMainModule.c_str(), NULL /* default extension */, VBOXEXTPACKMODKIND_R3,
1150 &m->strMainModPath, &fIsNative, &m->ObjInfoMainMod))
1151 {
1152 m->strWhyUnusable.printf(tr("Failed to locate the main module ('%s')"), m->Desc.strMainModule.c_str());
1153 return;
1154 }
1155
1156 vrc = SUPR3HardenedVerifyPlugIn(m->strMainModPath.c_str(), szErr, sizeof(szErr));
1157 if (RT_FAILURE(vrc))
1158 {
1159 m->strWhyUnusable.printf(tr("%s"), szErr);
1160 return;
1161 }
1162
1163 if (fIsNative)
1164 {
1165 char szError[8192];
1166 vrc = RTLdrLoadEx(m->strMainModPath.c_str(), &m->hMainMod, szError, sizeof(szError));
1167 if (RT_FAILURE(vrc))
1168 {
1169 m->hMainMod = NIL_RTLDRMOD;
1170 m->strWhyUnusable.printf(tr("Failed to locate load the main module ('%s'): %Rrc - %s"),
1171 m->strMainModPath.c_str(), vrc, szError);
1172 return;
1173 }
1174 }
1175 else
1176 {
1177 m->strWhyUnusable.printf(tr("Only native main modules are currently supported"));
1178 return;
1179 }
1180
1181 /*
1182 * Resolve the predefined entry point.
1183 */
1184 PFNVBOXEXTPACKREGISTER pfnRegistration;
1185 vrc = RTLdrGetSymbol(m->hMainMod, VBOX_EXTPACK_MAIN_MOD_ENTRY_POINT, (void **)&pfnRegistration);
1186 if (RT_SUCCESS(vrc))
1187 {
1188 RT_ZERO(szErr);
1189 vrc = pfnRegistration(&m->Hlp, &m->pReg, szErr, sizeof(szErr) - 16);
1190 if ( RT_SUCCESS(vrc)
1191 && szErr[0] == '\0'
1192 && VALID_PTR(m->pReg))
1193 {
1194 if ( VBOXEXTPACK_IS_MAJOR_VER_EQUAL(m->pReg->u32Version, VBOXEXTPACKREG_VERSION)
1195 && m->pReg->u32EndMarker == m->pReg->u32Version)
1196 {
1197 if ( (!m->pReg->pfnInstalled || RT_VALID_PTR(m->pReg->pfnInstalled))
1198 && (!m->pReg->pfnUninstall || RT_VALID_PTR(m->pReg->pfnUninstall))
1199 && (!m->pReg->pfnVirtualBoxReady || RT_VALID_PTR(m->pReg->pfnVirtualBoxReady))
1200 && (!m->pReg->pfnConsoleReady || RT_VALID_PTR(m->pReg->pfnConsoleReady))
1201 && (!m->pReg->pfnUnload || RT_VALID_PTR(m->pReg->pfnUnload))
1202 && (!m->pReg->pfnVMCreated || RT_VALID_PTR(m->pReg->pfnVMCreated))
1203 && (!m->pReg->pfnVMConfigureVMM || RT_VALID_PTR(m->pReg->pfnVMConfigureVMM))
1204 && (!m->pReg->pfnVMPowerOn || RT_VALID_PTR(m->pReg->pfnVMPowerOn))
1205 && (!m->pReg->pfnVMPowerOff || RT_VALID_PTR(m->pReg->pfnVMPowerOff))
1206 && (!m->pReg->pfnQueryObject || RT_VALID_PTR(m->pReg->pfnQueryObject))
1207 )
1208 {
1209 /*
1210 * We're good!
1211 */
1212 m->fUsable = true;
1213 m->strWhyUnusable.setNull();
1214 return;
1215 }
1216
1217 m->strWhyUnusable = tr("The registration structure contains on or more invalid function pointers");
1218 }
1219 else
1220 m->strWhyUnusable.printf(tr("Unsupported registration structure version %u.%u"),
1221 RT_HIWORD(m->pReg->u32Version), RT_LOWORD(m->pReg->u32Version));
1222 }
1223 else
1224 {
1225 szErr[sizeof(szErr) - 1] = '\0';
1226 m->strWhyUnusable.printf(tr("%s returned %Rrc, pReg=%p szErr='%s'"),
1227 VBOX_EXTPACK_MAIN_MOD_ENTRY_POINT, vrc, m->pReg, szErr);
1228 }
1229 m->pReg = NULL;
1230 }
1231 else
1232 m->strWhyUnusable.printf(tr("Failed to resolve exported symbol '%s' in the main module: %Rrc"),
1233 VBOX_EXTPACK_MAIN_MOD_ENTRY_POINT, vrc);
1234
1235 RTLdrClose(m->hMainMod);
1236 m->hMainMod = NIL_RTLDRMOD;
1237}
1238
1239/**
1240 * Finds a module.
1241 *
1242 * @returns true if found, false if not.
1243 * @param a_pszName The module base name (no extension).
1244 * @param a_pszExt The extension. If NULL we use default
1245 * extensions.
1246 * @param a_enmKind The kind of module to locate.
1247 * @param a_pStrFound Where to return the path to the module we've
1248 * found.
1249 * @param a_pfNative Where to return whether this is a native module
1250 * or an agnostic one. Optional.
1251 * @param a_pObjInfo Where to return the file system object info for
1252 * the module. Optional.
1253 */
1254bool ExtPack::findModule(const char *a_pszName, const char *a_pszExt, VBOXEXTPACKMODKIND a_enmKind,
1255 Utf8Str *a_pStrFound, bool *a_pfNative, PRTFSOBJINFO a_pObjInfo) const
1256{
1257 /*
1258 * Try the native path first.
1259 */
1260 char szPath[RTPATH_MAX];
1261 int vrc = RTPathJoin(szPath, sizeof(szPath), m->strExtPackPath.c_str(), RTBldCfgTargetDotArch());
1262 AssertLogRelRCReturn(vrc, false);
1263 vrc = RTPathAppend(szPath, sizeof(szPath), a_pszName);
1264 AssertLogRelRCReturn(vrc, false);
1265 if (!a_pszExt)
1266 {
1267 const char *pszDefExt;
1268 switch (a_enmKind)
1269 {
1270 case VBOXEXTPACKMODKIND_RC: pszDefExt = ".rc"; break;
1271 case VBOXEXTPACKMODKIND_R0: pszDefExt = ".r0"; break;
1272 case VBOXEXTPACKMODKIND_R3: pszDefExt = RTLdrGetSuff(); break;
1273 default:
1274 AssertFailedReturn(false);
1275 }
1276 vrc = RTStrCat(szPath, sizeof(szPath), pszDefExt);
1277 AssertLogRelRCReturn(vrc, false);
1278 }
1279
1280 RTFSOBJINFO ObjInfo;
1281 if (!a_pObjInfo)
1282 a_pObjInfo = &ObjInfo;
1283 vrc = RTPathQueryInfo(szPath, a_pObjInfo, RTFSOBJATTRADD_UNIX);
1284 if (RT_SUCCESS(vrc) && RTFS_IS_FILE(a_pObjInfo->Attr.fMode))
1285 {
1286 if (a_pfNative)
1287 *a_pfNative = true;
1288 *a_pStrFound = szPath;
1289 return true;
1290 }
1291
1292 /*
1293 * Try the platform agnostic modules.
1294 */
1295 /* gcc.x86/module.rel */
1296 char szSubDir[32];
1297 RTStrPrintf(szSubDir, sizeof(szSubDir), "%s.%s", RTBldCfgCompiler(), RTBldCfgTargetArch());
1298 vrc = RTPathJoin(szPath, sizeof(szPath), m->strExtPackPath.c_str(), szSubDir);
1299 AssertLogRelRCReturn(vrc, false);
1300 vrc = RTPathAppend(szPath, sizeof(szPath), a_pszName);
1301 AssertLogRelRCReturn(vrc, false);
1302 if (!a_pszExt)
1303 {
1304 vrc = RTStrCat(szPath, sizeof(szPath), ".rel");
1305 AssertLogRelRCReturn(vrc, false);
1306 }
1307 vrc = RTPathQueryInfo(szPath, a_pObjInfo, RTFSOBJATTRADD_UNIX);
1308 if (RT_SUCCESS(vrc) && RTFS_IS_FILE(a_pObjInfo->Attr.fMode))
1309 {
1310 if (a_pfNative)
1311 *a_pfNative = false;
1312 *a_pStrFound = szPath;
1313 return true;
1314 }
1315
1316 /* x86/module.rel */
1317 vrc = RTPathJoin(szPath, sizeof(szPath), m->strExtPackPath.c_str(), RTBldCfgTargetArch());
1318 AssertLogRelRCReturn(vrc, false);
1319 vrc = RTPathAppend(szPath, sizeof(szPath), a_pszName);
1320 AssertLogRelRCReturn(vrc, false);
1321 if (!a_pszExt)
1322 {
1323 vrc = RTStrCat(szPath, sizeof(szPath), ".rel");
1324 AssertLogRelRCReturn(vrc, false);
1325 }
1326 vrc = RTPathQueryInfo(szPath, a_pObjInfo, RTFSOBJATTRADD_UNIX);
1327 if (RT_SUCCESS(vrc) && RTFS_IS_FILE(a_pObjInfo->Attr.fMode))
1328 {
1329 if (a_pfNative)
1330 *a_pfNative = false;
1331 *a_pStrFound = szPath;
1332 return true;
1333 }
1334
1335 return false;
1336}
1337
1338/**
1339 * Compares two file system object info structures.
1340 *
1341 * @returns true if equal, false if not.
1342 * @param pObjInfo1 The first.
1343 * @param pObjInfo2 The second.
1344 * @todo IPRT should do this, really.
1345 */
1346/* static */ bool ExtPack::objinfoIsEqual(PCRTFSOBJINFO pObjInfo1, PCRTFSOBJINFO pObjInfo2)
1347{
1348 if (!RTTimeSpecIsEqual(&pObjInfo1->ModificationTime, &pObjInfo2->ModificationTime))
1349 return false;
1350 if (!RTTimeSpecIsEqual(&pObjInfo1->ChangeTime, &pObjInfo2->ChangeTime))
1351 return false;
1352 if (!RTTimeSpecIsEqual(&pObjInfo1->BirthTime, &pObjInfo2->BirthTime))
1353 return false;
1354 if (pObjInfo1->cbObject != pObjInfo2->cbObject)
1355 return false;
1356 if (pObjInfo1->Attr.fMode != pObjInfo2->Attr.fMode)
1357 return false;
1358 if (pObjInfo1->Attr.enmAdditional == pObjInfo2->Attr.enmAdditional)
1359 {
1360 switch (pObjInfo1->Attr.enmAdditional)
1361 {
1362 case RTFSOBJATTRADD_UNIX:
1363 if (pObjInfo1->Attr.u.Unix.uid != pObjInfo2->Attr.u.Unix.uid)
1364 return false;
1365 if (pObjInfo1->Attr.u.Unix.gid != pObjInfo2->Attr.u.Unix.gid)
1366 return false;
1367 if (pObjInfo1->Attr.u.Unix.INodeIdDevice != pObjInfo2->Attr.u.Unix.INodeIdDevice)
1368 return false;
1369 if (pObjInfo1->Attr.u.Unix.INodeId != pObjInfo2->Attr.u.Unix.INodeId)
1370 return false;
1371 if (pObjInfo1->Attr.u.Unix.GenerationId != pObjInfo2->Attr.u.Unix.GenerationId)
1372 return false;
1373 break;
1374 default:
1375 break;
1376 }
1377 }
1378 return true;
1379}
1380
1381
1382/**
1383 * @interface_method_impl{VBOXEXTPACKHLP,pfnFindModule}
1384 */
1385/*static*/ DECLCALLBACK(int)
1386ExtPack::hlpFindModule(PCVBOXEXTPACKHLP pHlp, const char *pszName, const char *pszExt, VBOXEXTPACKMODKIND enmKind,
1387 char *pszFound, size_t cbFound, bool *pfNative)
1388{
1389 /*
1390 * Validate the input and get our bearings.
1391 */
1392 AssertPtrReturn(pszName, VERR_INVALID_POINTER);
1393 AssertPtrNullReturn(pszExt, VERR_INVALID_POINTER);
1394 AssertPtrReturn(pszFound, VERR_INVALID_POINTER);
1395 AssertPtrNullReturn(pfNative, VERR_INVALID_POINTER);
1396 AssertReturn(enmKind > VBOXEXTPACKMODKIND_INVALID && enmKind < VBOXEXTPACKMODKIND_END, VERR_INVALID_PARAMETER);
1397
1398 AssertPtrReturn(pHlp, VERR_INVALID_POINTER);
1399 AssertReturn(pHlp->u32Version == VBOXEXTPACKHLP_VERSION, VERR_INVALID_POINTER);
1400 ExtPack::Data *m = RT_FROM_CPP_MEMBER(pHlp, Data, Hlp);
1401 AssertPtrReturn(m, VERR_INVALID_POINTER);
1402 ExtPack *pThis = m->pThis;
1403 AssertPtrReturn(pThis, VERR_INVALID_POINTER);
1404
1405 /*
1406 * This is just a wrapper around findModule.
1407 */
1408 Utf8Str strFound;
1409 if (pThis->findModule(pszName, pszExt, enmKind, &strFound, pfNative, NULL))
1410 return RTStrCopy(pszFound, cbFound, strFound.c_str());
1411 return VERR_FILE_NOT_FOUND;
1412}
1413
1414/*static*/ DECLCALLBACK(int)
1415ExtPack::hlpGetFilePath(PCVBOXEXTPACKHLP pHlp, const char *pszFilename, char *pszPath, size_t cbPath)
1416{
1417 /*
1418 * Validate the input and get our bearings.
1419 */
1420 AssertPtrReturn(pszFilename, VERR_INVALID_POINTER);
1421 AssertPtrReturn(pszPath, VERR_INVALID_POINTER);
1422 AssertReturn(cbPath > 0, VERR_BUFFER_OVERFLOW);
1423
1424 AssertPtrReturn(pHlp, VERR_INVALID_POINTER);
1425 AssertReturn(pHlp->u32Version == VBOXEXTPACKHLP_VERSION, VERR_INVALID_POINTER);
1426 ExtPack::Data *m = RT_FROM_CPP_MEMBER(pHlp, Data, Hlp);
1427 AssertPtrReturn(m, VERR_INVALID_POINTER);
1428 ExtPack *pThis = m->pThis;
1429 AssertPtrReturn(pThis, VERR_INVALID_POINTER);
1430
1431 /*
1432 * This is a simple RTPathJoin, no checking if things exists or anything.
1433 */
1434 int vrc = RTPathJoin(pszPath, cbPath, pThis->m->strExtPackPath.c_str(), pszFilename);
1435 if (RT_FAILURE(vrc))
1436 RT_BZERO(pszPath, cbPath);
1437 return vrc;
1438}
1439
1440/*static*/ DECLCALLBACK(VBOXEXTPACKCTX)
1441ExtPack::hlpGetContext(PCVBOXEXTPACKHLP pHlp)
1442{
1443 /*
1444 * Validate the input and get our bearings.
1445 */
1446 AssertPtrReturn(pHlp, VBOXEXTPACKCTX_INVALID);
1447 AssertReturn(pHlp->u32Version == VBOXEXTPACKHLP_VERSION, VBOXEXTPACKCTX_INVALID);
1448 ExtPack::Data *m = RT_FROM_CPP_MEMBER(pHlp, Data, Hlp);
1449 AssertPtrReturn(m, VBOXEXTPACKCTX_INVALID);
1450 ExtPack *pThis = m->pThis;
1451 AssertPtrReturn(pThis, VBOXEXTPACKCTX_INVALID);
1452
1453 return pThis->m->enmContext;
1454}
1455
1456/*static*/ DECLCALLBACK(int)
1457ExtPack::hlpReservedN(PCVBOXEXTPACKHLP pHlp)
1458{
1459 /*
1460 * Validate the input and get our bearings.
1461 */
1462 AssertPtrReturn(pHlp, VERR_INVALID_POINTER);
1463 AssertReturn(pHlp->u32Version == VBOXEXTPACKHLP_VERSION, VERR_INVALID_POINTER);
1464 ExtPack::Data *m = RT_FROM_CPP_MEMBER(pHlp, Data, Hlp);
1465 AssertPtrReturn(m, VERR_INVALID_POINTER);
1466 ExtPack *pThis = m->pThis;
1467 AssertPtrReturn(pThis, VERR_INVALID_POINTER);
1468
1469 return VERR_NOT_IMPLEMENTED;
1470}
1471
1472
1473
1474
1475
1476STDMETHODIMP ExtPack::COMGETTER(Name)(BSTR *a_pbstrName)
1477{
1478 CheckComArgOutPointerValid(a_pbstrName);
1479
1480 AutoCaller autoCaller(this);
1481 HRESULT hrc = autoCaller.rc();
1482 if (SUCCEEDED(hrc))
1483 {
1484 Bstr str(m->Desc.strName);
1485 str.cloneTo(a_pbstrName);
1486 }
1487 return hrc;
1488}
1489
1490STDMETHODIMP ExtPack::COMGETTER(Description)(BSTR *a_pbstrDescription)
1491{
1492 CheckComArgOutPointerValid(a_pbstrDescription);
1493
1494 AutoCaller autoCaller(this);
1495 HRESULT hrc = autoCaller.rc();
1496 if (SUCCEEDED(hrc))
1497 {
1498 Bstr str(m->Desc.strDescription);
1499 str.cloneTo(a_pbstrDescription);
1500 }
1501 return hrc;
1502}
1503
1504STDMETHODIMP ExtPack::COMGETTER(Version)(BSTR *a_pbstrVersion)
1505{
1506 CheckComArgOutPointerValid(a_pbstrVersion);
1507
1508 AutoCaller autoCaller(this);
1509 HRESULT hrc = autoCaller.rc();
1510 if (SUCCEEDED(hrc))
1511 {
1512 Bstr str(m->Desc.strVersion);
1513 str.cloneTo(a_pbstrVersion);
1514 }
1515 return hrc;
1516}
1517
1518STDMETHODIMP ExtPack::COMGETTER(Revision)(ULONG *a_puRevision)
1519{
1520 CheckComArgOutPointerValid(a_puRevision);
1521
1522 AutoCaller autoCaller(this);
1523 HRESULT hrc = autoCaller.rc();
1524 if (SUCCEEDED(hrc))
1525 *a_puRevision = m->Desc.uRevision;
1526 return hrc;
1527}
1528
1529STDMETHODIMP ExtPack::COMGETTER(VRDEModule)(BSTR *a_pbstrVrdeModule)
1530{
1531 CheckComArgOutPointerValid(a_pbstrVrdeModule);
1532
1533 AutoCaller autoCaller(this);
1534 HRESULT hrc = autoCaller.rc();
1535 if (SUCCEEDED(hrc))
1536 {
1537 Bstr str(m->Desc.strVrdeModule);
1538 str.cloneTo(a_pbstrVrdeModule);
1539 }
1540 return hrc;
1541}
1542
1543STDMETHODIMP ExtPack::COMGETTER(PlugIns)(ComSafeArrayOut(IExtPackPlugIn *, a_paPlugIns))
1544{
1545 /** @todo implement plug-ins. */
1546#ifdef VBOX_WITH_XPCOM
1547 NOREF(a_paPlugIns);
1548 NOREF(a_paPlugInsSize);
1549#endif
1550 ReturnComNotImplemented();
1551}
1552
1553STDMETHODIMP ExtPack::COMGETTER(Usable)(BOOL *a_pfUsable)
1554{
1555 CheckComArgOutPointerValid(a_pfUsable);
1556
1557 AutoCaller autoCaller(this);
1558 HRESULT hrc = autoCaller.rc();
1559 if (SUCCEEDED(hrc))
1560 *a_pfUsable = m->fUsable;
1561 return hrc;
1562}
1563
1564STDMETHODIMP ExtPack::COMGETTER(WhyUnusable)(BSTR *a_pbstrWhy)
1565{
1566 CheckComArgOutPointerValid(a_pbstrWhy);
1567
1568 AutoCaller autoCaller(this);
1569 HRESULT hrc = autoCaller.rc();
1570 if (SUCCEEDED(hrc))
1571 m->strWhyUnusable.cloneTo(a_pbstrWhy);
1572 return hrc;
1573}
1574
1575STDMETHODIMP ExtPack::COMGETTER(ShowLicense)(BOOL *a_pfShowIt)
1576{
1577 CheckComArgOutPointerValid(a_pfShowIt);
1578
1579 AutoCaller autoCaller(this);
1580 HRESULT hrc = autoCaller.rc();
1581 if (SUCCEEDED(hrc))
1582 *a_pfShowIt = m->Desc.fShowLicense;
1583 return hrc;
1584}
1585
1586STDMETHODIMP ExtPack::COMGETTER(License)(BSTR *a_pbstrHtmlLicense)
1587{
1588 Bstr bstrHtml("html");
1589 return QueryLicense(Bstr::Empty.raw(), Bstr::Empty.raw(), bstrHtml.raw(), a_pbstrHtmlLicense);
1590}
1591
1592STDMETHODIMP ExtPack::QueryLicense(IN_BSTR a_bstrPreferredLocale, IN_BSTR a_bstrPreferredLanguage, IN_BSTR a_bstrFormat,
1593 BSTR *a_pbstrLicense)
1594{
1595 /*
1596 * Validate input.
1597 */
1598 CheckComArgOutPointerValid(a_pbstrLicense);
1599 CheckComArgNotNull(a_bstrPreferredLocale);
1600 CheckComArgNotNull(a_bstrPreferredLanguage);
1601 CheckComArgNotNull(a_bstrFormat);
1602
1603 Utf8Str strPreferredLocale(a_bstrPreferredLocale);
1604 if (strPreferredLocale.length() != 2 && strPreferredLocale.length() != 0)
1605 return setError(E_FAIL, tr("The preferred locale is a two character string or empty."));
1606
1607 Utf8Str strPreferredLanguage(a_bstrPreferredLanguage);
1608 if (strPreferredLanguage.length() != 2 && strPreferredLanguage.length() != 0)
1609 return setError(E_FAIL, tr("The preferred lanuage is a two character string or empty."));
1610
1611 Utf8Str strFormat(a_bstrFormat);
1612 if ( !strFormat.equals("html")
1613 && !strFormat.equals("rtf")
1614 && !strFormat.equals("txt"))
1615 return setError(E_FAIL, tr("The license format can only have the values 'html', 'rtf' and 'txt'."));
1616
1617 /*
1618 * Combine the options to form a file name before locking down anything.
1619 */
1620 char szName[sizeof(VBOX_EXTPACK_LICENSE_NAME_PREFIX "-de_DE.html") + 2];
1621 if (strPreferredLocale.isNotEmpty() && strPreferredLanguage.isNotEmpty())
1622 RTStrPrintf(szName, sizeof(szName), VBOX_EXTPACK_LICENSE_NAME_PREFIX "-%s_%s.%s",
1623 strPreferredLocale.c_str(), strPreferredLanguage.c_str(), strFormat.c_str());
1624 else if (strPreferredLocale.isNotEmpty())
1625 RTStrPrintf(szName, sizeof(szName), VBOX_EXTPACK_LICENSE_NAME_PREFIX "-%s.%s", strPreferredLocale.c_str(), strFormat.c_str());
1626 else if (strPreferredLanguage.isNotEmpty())
1627 RTStrPrintf(szName, sizeof(szName), VBOX_EXTPACK_LICENSE_NAME_PREFIX "-_%s.%s", strPreferredLocale.c_str(), strFormat.c_str());
1628 else
1629 RTStrPrintf(szName, sizeof(szName), VBOX_EXTPACK_LICENSE_NAME_PREFIX ".%s", strFormat.c_str());
1630
1631 /*
1632 * Effectuate the query.
1633 */
1634 AutoCaller autoCaller(this);
1635 HRESULT hrc = autoCaller.rc();
1636 if (SUCCEEDED(hrc))
1637 {
1638 AutoReadLock autoLock(this COMMA_LOCKVAL_SRC_POS); /* paranoia */
1639
1640 if (!m->fUsable)
1641 hrc = setError(E_FAIL, tr("%s"), m->strWhyUnusable.c_str());
1642 else
1643 {
1644 char szPath[RTPATH_MAX];
1645 int vrc = RTPathJoin(szPath, sizeof(szPath), m->strExtPackPath.c_str(), szName);
1646 if (RT_SUCCESS(vrc))
1647 {
1648 void *pvFile;
1649 size_t cbFile;
1650 vrc = RTFileReadAllEx(szPath, 0, RTFOFF_MAX, RTFILE_RDALL_O_DENY_READ, &pvFile, &cbFile);
1651 if (RT_SUCCESS(vrc))
1652 {
1653 Bstr bstrLicense((const char *)pvFile, cbFile);
1654 if (bstrLicense.isNotEmpty())
1655 {
1656 bstrLicense.detachTo(a_pbstrLicense);
1657 hrc = S_OK;
1658 }
1659 else
1660 hrc = setError(VBOX_E_IPRT_ERROR, tr("The license file '%s' is empty or contains invalid UTF-8 encoding"),
1661 szPath);
1662 RTFileReadAllFree(pvFile, cbFile);
1663 }
1664 else if (vrc == VERR_FILE_NOT_FOUND || vrc == VERR_PATH_NOT_FOUND)
1665 hrc = setError(VBOX_E_OBJECT_NOT_FOUND, tr("The license file '%s' was not found in extension pack '%s'"),
1666 szName, m->Desc.strName.c_str());
1667 else
1668 hrc = setError(VBOX_E_FILE_ERROR, tr("Failed to open the license file '%s': %Rrc"), szPath, vrc);
1669 }
1670 else
1671 hrc = setError(VBOX_E_IPRT_ERROR, tr("RTPathJoin failed: %Rrc"), vrc);
1672 }
1673 }
1674 return hrc;
1675}
1676
1677
1678STDMETHODIMP ExtPack::QueryObject(IN_BSTR a_bstrObjectId, IUnknown **a_ppUnknown)
1679{
1680 com::Guid ObjectId;
1681 CheckComArgGuid(a_bstrObjectId, ObjectId);
1682 CheckComArgOutPointerValid(a_ppUnknown);
1683
1684 AutoCaller autoCaller(this);
1685 HRESULT hrc = autoCaller.rc();
1686 if (SUCCEEDED(hrc))
1687 {
1688 if ( m->pReg
1689 && m->pReg->pfnQueryObject)
1690 {
1691 void *pvUnknown = m->pReg->pfnQueryObject(m->pReg, ObjectId.raw());
1692 if (pvUnknown)
1693 *a_ppUnknown = (IUnknown *)pvUnknown;
1694 else
1695 hrc = E_NOINTERFACE;
1696 }
1697 else
1698 hrc = E_NOINTERFACE;
1699 }
1700 return hrc;
1701}
1702
1703
1704
1705
1706DEFINE_EMPTY_CTOR_DTOR(ExtPackManager)
1707
1708/**
1709 * Called by ComObjPtr::createObject when creating the object.
1710 *
1711 * Just initialize the basic object state, do the rest in init().
1712 *
1713 * @returns S_OK.
1714 */
1715HRESULT ExtPackManager::FinalConstruct()
1716{
1717 m = NULL;
1718 return S_OK;
1719}
1720
1721/**
1722 * Initializes the extension pack manager.
1723 *
1724 * @returns COM status code.
1725 * @param a_pVirtualBox Pointer to the VirtualBox object.
1726 * @param a_enmContext The context we're in.
1727 */
1728HRESULT ExtPackManager::initExtPackManager(VirtualBox *a_pVirtualBox, VBOXEXTPACKCTX a_enmContext)
1729{
1730 AutoInitSpan autoInitSpan(this);
1731 AssertReturn(autoInitSpan.isOk(), E_FAIL);
1732
1733 /*
1734 * Figure some stuff out before creating the instance data.
1735 */
1736 char szBaseDir[RTPATH_MAX];
1737 int rc = RTPathAppPrivateArch(szBaseDir, sizeof(szBaseDir));
1738 AssertLogRelRCReturn(rc, E_FAIL);
1739 rc = RTPathAppend(szBaseDir, sizeof(szBaseDir), VBOX_EXTPACK_INSTALL_DIR);
1740 AssertLogRelRCReturn(rc, E_FAIL);
1741
1742 char szCertificatDir[RTPATH_MAX];
1743 rc = RTPathAppPrivateNoArch(szCertificatDir, sizeof(szCertificatDir));
1744 AssertLogRelRCReturn(rc, E_FAIL);
1745 rc = RTPathAppend(szCertificatDir, sizeof(szCertificatDir), VBOX_EXTPACK_CERT_DIR);
1746 AssertLogRelRCReturn(rc, E_FAIL);
1747
1748 /*
1749 * Allocate and initialize the instance data.
1750 */
1751 m = new Data;
1752 m->strBaseDir = szBaseDir;
1753 m->strCertificatDirPath = szCertificatDir;
1754 m->pVirtualBox = a_pVirtualBox;
1755 m->enmContext = a_enmContext;
1756
1757 /*
1758 * Go looking for extensions. The RTDirOpen may fail if nothing has been
1759 * installed yet, or if root is paranoid and has revoked our access to them.
1760 *
1761 * We ASSUME that there are no files, directories or stuff in the directory
1762 * that exceed the max name length in RTDIRENTRYEX.
1763 */
1764 HRESULT hrc = S_OK;
1765 PRTDIR pDir;
1766 int vrc = RTDirOpen(&pDir, szBaseDir);
1767 if (RT_SUCCESS(vrc))
1768 {
1769 for (;;)
1770 {
1771 RTDIRENTRYEX Entry;
1772 vrc = RTDirReadEx(pDir, &Entry, NULL /*pcbDirEntry*/, RTFSOBJATTRADD_NOTHING, RTPATH_F_ON_LINK);
1773 if (RT_FAILURE(vrc))
1774 {
1775 AssertLogRelMsg(vrc == VERR_NO_MORE_FILES, ("%Rrc\n", vrc));
1776 break;
1777 }
1778 if ( RTFS_IS_DIRECTORY(Entry.Info.Attr.fMode)
1779 && strcmp(Entry.szName, ".") != 0
1780 && strcmp(Entry.szName, "..") != 0
1781 && VBoxExtPackIsValidMangledName(Entry.szName) )
1782 {
1783 /*
1784 * All directories are extensions, the shall be nothing but
1785 * extensions in this subdirectory.
1786 */
1787 char szExtPackDir[RTPATH_MAX];
1788 vrc = RTPathJoin(szExtPackDir, sizeof(szExtPackDir), m->strBaseDir.c_str(), Entry.szName);
1789 AssertLogRelRC(vrc);
1790 if (RT_SUCCESS(vrc))
1791 {
1792 iprt::MiniString *pstrName = VBoxExtPackUnmangleName(Entry.szName, RTSTR_MAX);
1793 AssertLogRel(pstrName);
1794 if (pstrName)
1795 {
1796 ComObjPtr<ExtPack> NewExtPack;
1797 HRESULT hrc2 = NewExtPack.createObject();
1798 if (SUCCEEDED(hrc2))
1799 hrc2 = NewExtPack->initWithDir(a_enmContext, pstrName->c_str(), szExtPackDir);
1800 delete pstrName;
1801 if (SUCCEEDED(hrc2))
1802 m->llInstalledExtPacks.push_back(NewExtPack);
1803 else if (SUCCEEDED(rc))
1804 hrc = hrc2;
1805 }
1806 else
1807 hrc = E_UNEXPECTED;
1808 }
1809 else
1810 hrc = E_UNEXPECTED;
1811 }
1812 }
1813 RTDirClose(pDir);
1814 }
1815 /* else: ignore, the directory probably does not exist or something. */
1816
1817 if (SUCCEEDED(hrc))
1818 autoInitSpan.setSucceeded();
1819 return hrc;
1820}
1821
1822/**
1823 * COM cruft.
1824 */
1825void ExtPackManager::FinalRelease()
1826{
1827 uninit();
1828}
1829
1830/**
1831 * Do the actual cleanup.
1832 */
1833void ExtPackManager::uninit()
1834{
1835 /* Enclose the state transition Ready->InUninit->NotReady */
1836 AutoUninitSpan autoUninitSpan(this);
1837 if (!autoUninitSpan.uninitDone() && m != NULL)
1838 {
1839 delete m;
1840 m = NULL;
1841 }
1842}
1843
1844
1845STDMETHODIMP ExtPackManager::COMGETTER(InstalledExtPacks)(ComSafeArrayOut(IExtPack *, a_paExtPacks))
1846{
1847 CheckComArgOutSafeArrayPointerValid(a_paExtPacks);
1848 Assert(m->enmContext == VBOXEXTPACKCTX_PER_USER_DAEMON);
1849
1850 AutoCaller autoCaller(this);
1851 HRESULT hrc = autoCaller.rc();
1852 if (SUCCEEDED(hrc))
1853 {
1854 AutoReadLock autoLock(this COMMA_LOCKVAL_SRC_POS);
1855
1856 SafeIfaceArray<IExtPack> SaExtPacks(m->llInstalledExtPacks);
1857 SaExtPacks.detachTo(ComSafeArrayOutArg(a_paExtPacks));
1858 }
1859
1860 return hrc;
1861}
1862
1863STDMETHODIMP ExtPackManager::Find(IN_BSTR a_bstrName, IExtPack **a_pExtPack)
1864{
1865 CheckComArgNotNull(a_bstrName);
1866 CheckComArgOutPointerValid(a_pExtPack);
1867 Utf8Str strName(a_bstrName);
1868 Assert(m->enmContext == VBOXEXTPACKCTX_PER_USER_DAEMON);
1869
1870 AutoCaller autoCaller(this);
1871 HRESULT hrc = autoCaller.rc();
1872 if (SUCCEEDED(hrc))
1873 {
1874 AutoReadLock autoLock(this COMMA_LOCKVAL_SRC_POS);
1875
1876 ComPtr<ExtPack> ptrExtPack = findExtPack(strName.c_str());
1877 if (!ptrExtPack.isNull())
1878 ptrExtPack.queryInterfaceTo(a_pExtPack);
1879 else
1880 hrc = VBOX_E_OBJECT_NOT_FOUND;
1881 }
1882
1883 return hrc;
1884}
1885
1886STDMETHODIMP ExtPackManager::OpenExtPackFile(IN_BSTR a_bstrTarball, IExtPackFile **a_ppExtPackFile)
1887{
1888 CheckComArgNotNull(a_bstrTarball);
1889 CheckComArgOutPointerValid(a_ppExtPackFile);
1890 Utf8Str strTarball(a_bstrTarball);
1891 AssertReturn(m->enmContext == VBOXEXTPACKCTX_PER_USER_DAEMON, E_UNEXPECTED);
1892
1893 ComObjPtr<ExtPackFile> NewExtPackFile;
1894 HRESULT hrc = NewExtPackFile.createObject();
1895 if (SUCCEEDED(hrc))
1896 hrc = NewExtPackFile->initWithFile(strTarball.c_str(), this);
1897 if (SUCCEEDED(hrc))
1898 NewExtPackFile.queryInterfaceTo(a_ppExtPackFile);
1899
1900 return hrc;
1901}
1902
1903STDMETHODIMP ExtPackManager::Uninstall(IN_BSTR a_bstrName, BOOL a_fForcedRemoval)
1904{
1905 CheckComArgNotNull(a_bstrName);
1906 Utf8Str strName(a_bstrName);
1907 Assert(m->enmContext == VBOXEXTPACKCTX_PER_USER_DAEMON);
1908
1909 AutoCaller autoCaller(this);
1910 HRESULT hrc = autoCaller.rc();
1911 if (SUCCEEDED(hrc))
1912 {
1913 AutoWriteLock autoLock(this COMMA_LOCKVAL_SRC_POS);
1914
1915 /*
1916 * Refresh the data we have on the extension pack as it may be made
1917 * stale by direct meddling or some other user.
1918 */
1919 ExtPack *pExtPack;
1920 hrc = refreshExtPack(strName.c_str(), false /*a_fUnsuableIsError*/, &pExtPack);
1921 if (SUCCEEDED(hrc))
1922 {
1923 if (!pExtPack)
1924 {
1925 LogRel(("ExtPackManager: Extension pack '%s' is not installed, so nothing to uninstall.\n", strName.c_str()));
1926 hrc = S_OK; /* nothing to uninstall */
1927 }
1928 else
1929 {
1930 /*
1931 * Call the uninstall hook and unload the main dll.
1932 */
1933 hrc = pExtPack->callUninstallHookAndClose(m->pVirtualBox, a_fForcedRemoval != FALSE);
1934 if (SUCCEEDED(hrc))
1935 {
1936 /*
1937 * Run the set-uid-to-root binary that performs the
1938 * uninstallation. Then refresh the object.
1939 *
1940 * This refresh is theorically subject to races, but it's of
1941 * the don't-do-that variety.
1942 */
1943 const char *pszForcedOpt = a_fForcedRemoval ? "--forced" : NULL;
1944 hrc = runSetUidToRootHelper("uninstall",
1945 "--base-dir", m->strBaseDir.c_str(),
1946 "--name", strName.c_str(),
1947 pszForcedOpt, /* Last as it may be NULL. */
1948 (const char *)NULL);
1949 if (SUCCEEDED(hrc))
1950 {
1951 hrc = refreshExtPack(strName.c_str(), false /*a_fUnsuableIsError*/, &pExtPack);
1952 if (SUCCEEDED(hrc))
1953 {
1954 if (!pExtPack)
1955 LogRel(("ExtPackManager: Successfully uninstalled extension pack '%s'.\n", strName.c_str()));
1956 else
1957 hrc = setError(E_FAIL,
1958 tr("Uninstall extension pack '%s' failed under mysterious circumstances"),
1959 strName.c_str());
1960 }
1961 }
1962 else
1963 {
1964 ErrorInfoKeeper Eik;
1965 refreshExtPack(strName.c_str(), false /*a_fUnsuableIsError*/, NULL);
1966 }
1967 }
1968 }
1969 }
1970
1971 /*
1972 * Do VirtualBoxReady callbacks now for any freshly installed
1973 * extension pack (old ones will not be called).
1974 */
1975 if (m->enmContext == VBOXEXTPACKCTX_PER_USER_DAEMON)
1976 {
1977 autoLock.release();
1978 callAllVirtualBoxReadyHooks();
1979 }
1980 }
1981
1982 return hrc;
1983}
1984
1985STDMETHODIMP ExtPackManager::Cleanup(void)
1986{
1987 Assert(m->enmContext == VBOXEXTPACKCTX_PER_USER_DAEMON);
1988
1989 AutoCaller autoCaller(this);
1990 HRESULT hrc = autoCaller.rc();
1991 if (SUCCEEDED(hrc))
1992 {
1993 /*
1994 * Run the set-uid-to-root binary that performs the cleanup.
1995 *
1996 * Take the write lock to prevent conflicts with other calls to this
1997 * VBoxSVC instance.
1998 */
1999 AutoWriteLock autoLock(this COMMA_LOCKVAL_SRC_POS);
2000 hrc = runSetUidToRootHelper("cleanup",
2001 "--base-dir", m->strBaseDir.c_str(),
2002 (const char *)NULL);
2003 }
2004
2005 return hrc;
2006}
2007
2008STDMETHODIMP ExtPackManager::QueryAllPlugInsForFrontend(IN_BSTR a_bstrFrontend, ComSafeArrayOut(BSTR, a_pabstrPlugInModules))
2009{
2010 CheckComArgNotNull(a_bstrFrontend);
2011 Utf8Str strName(a_bstrFrontend);
2012 CheckComArgOutSafeArrayPointerValid(a_pabstrPlugInModules);
2013 Assert(m->enmContext == VBOXEXTPACKCTX_PER_USER_DAEMON);
2014
2015 AutoCaller autoCaller(this);
2016 HRESULT hrc = autoCaller.rc();
2017 if (SUCCEEDED(hrc))
2018 {
2019 com::SafeArray<BSTR> saPaths((size_t)0);
2020 /** @todo implement plug-ins */
2021 saPaths.detachTo(ComSafeArrayOutArg(a_pabstrPlugInModules));
2022 }
2023 return hrc;
2024}
2025
2026STDMETHODIMP ExtPackManager::IsExtPackUsable(IN_BSTR a_bstrExtPack, BOOL *aUsable)
2027{
2028 CheckComArgNotNull(a_bstrExtPack);
2029 Utf8Str strExtPack(a_bstrExtPack);
2030 *aUsable = isExtPackUsable(strExtPack.c_str());
2031 return S_OK;
2032}
2033
2034
2035/**
2036 * Runs the helper application that does the privileged operations.
2037 *
2038 * @returns S_OK or a failure status with error information set.
2039 * @param a_pszCommand The command to execute.
2040 * @param ... The argument strings that goes along with the
2041 * command. Maximum is about 16. Terminated by a
2042 * NULL.
2043 */
2044HRESULT ExtPackManager::runSetUidToRootHelper(const char *a_pszCommand, ...)
2045{
2046 /*
2047 * Calculate the path to the helper application.
2048 */
2049 char szExecName[RTPATH_MAX];
2050 int vrc = RTPathAppPrivateArch(szExecName, sizeof(szExecName));
2051 AssertLogRelRCReturn(vrc, E_UNEXPECTED);
2052
2053 vrc = RTPathAppend(szExecName, sizeof(szExecName), VBOX_EXTPACK_HELPER_NAME);
2054 AssertLogRelRCReturn(vrc, E_UNEXPECTED);
2055
2056 /*
2057 * Convert the variable argument list to a RTProcCreate argument vector.
2058 */
2059 const char *apszArgs[20];
2060 unsigned cArgs = 0;
2061 apszArgs[cArgs++] = &szExecName[0];
2062 apszArgs[cArgs++] = a_pszCommand;
2063
2064 va_list va;
2065 va_start(va, a_pszCommand);
2066 const char *pszLastArg;
2067 LogRel(("ExtPack: Executing '%s'", szExecName));
2068 do
2069 {
2070 LogRel((" '%s'", apszArgs[cArgs - 1]));
2071 AssertReturn(cArgs < RT_ELEMENTS(apszArgs) - 1, E_UNEXPECTED);
2072 pszLastArg = va_arg(va, const char *);
2073 apszArgs[cArgs++] = pszLastArg;
2074 } while (pszLastArg != NULL);
2075 LogRel(("\n"));
2076 va_end(va);
2077 apszArgs[cArgs] = NULL;
2078
2079 /*
2080 * Create a PIPE which we attach to stderr so that we can read the error
2081 * message on failure and report it back to the caller.
2082 */
2083 RTPIPE hPipeR;
2084 RTHANDLE hStdErrPipe;
2085 hStdErrPipe.enmType = RTHANDLETYPE_PIPE;
2086 vrc = RTPipeCreate(&hPipeR, &hStdErrPipe.u.hPipe, RTPIPE_C_INHERIT_WRITE);
2087 AssertLogRelRCReturn(vrc, E_UNEXPECTED);
2088
2089 /*
2090 * Spawn the process.
2091 */
2092 HRESULT hrc;
2093 RTPROCESS hProcess;
2094 vrc = RTProcCreateEx(szExecName,
2095 apszArgs,
2096 RTENV_DEFAULT,
2097 0 /*fFlags*/,
2098 NULL /*phStdIn*/,
2099 NULL /*phStdOut*/,
2100 &hStdErrPipe,
2101 NULL /*pszAsUser*/,
2102 NULL /*pszPassword*/,
2103 &hProcess);
2104 if (RT_SUCCESS(vrc))
2105 {
2106 vrc = RTPipeClose(hStdErrPipe.u.hPipe);
2107 hStdErrPipe.u.hPipe = NIL_RTPIPE;
2108
2109 /*
2110 * Read the pipe output until the process completes.
2111 */
2112 RTPROCSTATUS ProcStatus = { -42, RTPROCEXITREASON_ABEND };
2113 size_t cbStdErrBuf = 0;
2114 size_t offStdErrBuf = 0;
2115 char *pszStdErrBuf = NULL;
2116 do
2117 {
2118 /*
2119 * Service the pipe. Block waiting for output or the pipe breaking
2120 * when the process terminates.
2121 */
2122 if (hPipeR != NIL_RTPIPE)
2123 {
2124 char achBuf[1024];
2125 size_t cbRead;
2126 vrc = RTPipeReadBlocking(hPipeR, achBuf, sizeof(achBuf), &cbRead);
2127 if (RT_SUCCESS(vrc))
2128 {
2129 /* grow the buffer? */
2130 size_t cbBufReq = offStdErrBuf + cbRead + 1;
2131 if ( cbBufReq > cbStdErrBuf
2132 && cbBufReq < _256K)
2133 {
2134 size_t cbNew = RT_ALIGN_Z(cbBufReq, 16); // 1024
2135 void *pvNew = RTMemRealloc(pszStdErrBuf, cbNew);
2136 if (pvNew)
2137 {
2138 pszStdErrBuf = (char *)pvNew;
2139 cbStdErrBuf = cbNew;
2140 }
2141 }
2142
2143 /* append if we've got room. */
2144 if (cbBufReq <= cbStdErrBuf)
2145 {
2146 memcpy(&pszStdErrBuf[offStdErrBuf], achBuf, cbRead);
2147 offStdErrBuf = offStdErrBuf + cbRead;
2148 pszStdErrBuf[offStdErrBuf] = '\0';
2149 }
2150 }
2151 else
2152 {
2153 AssertLogRelMsg(vrc == VERR_BROKEN_PIPE, ("%Rrc\n", vrc));
2154 RTPipeClose(hPipeR);
2155 hPipeR = NIL_RTPIPE;
2156 }
2157 }
2158
2159 /*
2160 * Service the process. Block if we have no pipe.
2161 */
2162 if (hProcess != NIL_RTPROCESS)
2163 {
2164 vrc = RTProcWait(hProcess,
2165 hPipeR == NIL_RTPIPE ? RTPROCWAIT_FLAGS_BLOCK : RTPROCWAIT_FLAGS_NOBLOCK,
2166 &ProcStatus);
2167 if (RT_SUCCESS(vrc))
2168 hProcess = NIL_RTPROCESS;
2169 else
2170 AssertLogRelMsgStmt(vrc == VERR_PROCESS_RUNNING, ("%Rrc\n", vrc), hProcess = NIL_RTPROCESS);
2171 }
2172 } while ( hPipeR != NIL_RTPIPE
2173 || hProcess != NIL_RTPROCESS);
2174
2175 LogRel(("ExtPack: enmReason=%d iStatus=%d stderr='%s'\n",
2176 ProcStatus.enmReason, ProcStatus.iStatus, offStdErrBuf ? pszStdErrBuf : ""));
2177
2178 /*
2179 * Look for rcExit=RTEXITCODE_SUCCESS at the end of the error output,
2180 * cut it as it is only there to attest the success.
2181 */
2182 if (offStdErrBuf > 0)
2183 {
2184 RTStrStripR(pszStdErrBuf);
2185 offStdErrBuf = strlen(pszStdErrBuf);
2186 }
2187
2188 if ( offStdErrBuf > 0
2189 && !strcmp(pszStdErrBuf, "rcExit=RTEXITCODE_SUCCESS"))
2190 {
2191 *pszStdErrBuf = '\0';
2192 offStdErrBuf = 0;
2193 }
2194 else if ( ProcStatus.enmReason == RTPROCEXITREASON_NORMAL
2195 && ProcStatus.iStatus == 0)
2196 ProcStatus.iStatus = 666;
2197
2198 /*
2199 * Compose the status code and, on failure, error message.
2200 */
2201 if ( ProcStatus.enmReason == RTPROCEXITREASON_NORMAL
2202 && ProcStatus.iStatus == 0
2203 && offStdErrBuf == 0)
2204 hrc = S_OK;
2205 else if (ProcStatus.enmReason == RTPROCEXITREASON_NORMAL)
2206 {
2207 AssertMsg(ProcStatus.iStatus != 0, ("%s\n", pszStdErrBuf));
2208 hrc = setError(E_FAIL, tr("The installer failed with exit code %d: %s"),
2209 ProcStatus.iStatus, offStdErrBuf ? pszStdErrBuf : "");
2210 }
2211 else if (ProcStatus.enmReason == RTPROCEXITREASON_SIGNAL)
2212 hrc = setError(E_UNEXPECTED, tr("The installer was killed by signal #d (stderr: %s)"),
2213 ProcStatus.iStatus, offStdErrBuf ? pszStdErrBuf : "");
2214 else if (ProcStatus.enmReason == RTPROCEXITREASON_ABEND)
2215 hrc = setError(E_UNEXPECTED, tr("The installer aborted abnormally (stderr: %s)"),
2216 offStdErrBuf ? pszStdErrBuf : "");
2217 else
2218 hrc = setError(E_UNEXPECTED, tr("internal error: enmReason=%d iStatus=%d stderr='%s'"),
2219 ProcStatus.enmReason, ProcStatus.iStatus, offStdErrBuf ? pszStdErrBuf : "");
2220
2221 RTMemFree(pszStdErrBuf);
2222 }
2223 else
2224 hrc = setError(VBOX_E_IPRT_ERROR, tr("Failed to launch the helper application '%s' (%Rrc)"), szExecName, vrc);
2225
2226 RTPipeClose(hPipeR);
2227 RTPipeClose(hStdErrPipe.u.hPipe);
2228
2229 return hrc;
2230}
2231
2232/**
2233 * Finds an installed extension pack.
2234 *
2235 * @returns Pointer to the extension pack if found, NULL if not. (No reference
2236 * counting problem here since the caller must be holding the lock.)
2237 * @param a_pszName The name of the extension pack.
2238 */
2239ExtPack *ExtPackManager::findExtPack(const char *a_pszName)
2240{
2241 size_t cchName = strlen(a_pszName);
2242
2243 for (ExtPackList::iterator it = m->llInstalledExtPacks.begin();
2244 it != m->llInstalledExtPacks.end();
2245 it++)
2246 {
2247 ExtPack::Data *pExtPackData = (*it)->m;
2248 if ( pExtPackData
2249 && pExtPackData->Desc.strName.length() == cchName
2250 && pExtPackData->Desc.strName.equalsIgnoreCase(a_pszName))
2251 return (*it);
2252 }
2253 return NULL;
2254}
2255
2256/**
2257 * Removes an installed extension pack from the internal list.
2258 *
2259 * The package is expected to exist!
2260 *
2261 * @param a_pszName The name of the extension pack.
2262 */
2263void ExtPackManager::removeExtPack(const char *a_pszName)
2264{
2265 size_t cchName = strlen(a_pszName);
2266
2267 for (ExtPackList::iterator it = m->llInstalledExtPacks.begin();
2268 it != m->llInstalledExtPacks.end();
2269 it++)
2270 {
2271 ExtPack::Data *pExtPackData = (*it)->m;
2272 if ( pExtPackData
2273 && pExtPackData->Desc.strName.length() == cchName
2274 && pExtPackData->Desc.strName.equalsIgnoreCase(a_pszName))
2275 {
2276 m->llInstalledExtPacks.erase(it);
2277 return;
2278 }
2279 }
2280 AssertMsgFailed(("%s\n", a_pszName));
2281}
2282
2283/**
2284 * Refreshes the specified extension pack.
2285 *
2286 * This may remove the extension pack from the list, so any non-smart pointers
2287 * to the extension pack object may become invalid.
2288 *
2289 * @returns S_OK and *ppExtPack on success, COM status code and error message
2290 * on failure.
2291 *
2292 * @param a_pszName The extension to update..
2293 * @param a_fUnsuableIsError If @c true, report an unusable extension pack
2294 * as an error.
2295 * @param a_ppExtPack Where to store the pointer to the extension
2296 * pack of it is still around after the refresh.
2297 * This is optional.
2298 *
2299 * @remarks Caller holds the extension manager lock.
2300 * @remarks Only called in VBoxSVC.
2301 */
2302HRESULT ExtPackManager::refreshExtPack(const char *a_pszName, bool a_fUnsuableIsError, ExtPack **a_ppExtPack)
2303{
2304 Assert(m->pVirtualBox != NULL); /* Only called from VBoxSVC. */
2305
2306 HRESULT hrc;
2307 ExtPack *pExtPack = findExtPack(a_pszName);
2308 if (pExtPack)
2309 {
2310 /*
2311 * Refresh existing object.
2312 */
2313 bool fCanDelete;
2314 hrc = pExtPack->refresh(&fCanDelete);
2315 if (SUCCEEDED(hrc))
2316 {
2317 if (fCanDelete)
2318 {
2319 removeExtPack(a_pszName);
2320 pExtPack = NULL;
2321 }
2322 }
2323 }
2324 else
2325 {
2326 /*
2327 * Does the dir exist? Make some special effort to deal with case
2328 * sensitivie file systems (a_pszName is case insensitive and mangled).
2329 */
2330 char szDir[RTPATH_MAX];
2331 int vrc = VBoxExtPackCalcDir(szDir, sizeof(szDir), m->strBaseDir.c_str(), a_pszName);
2332 AssertLogRelRCReturn(vrc, E_FAIL);
2333
2334 RTDIRENTRYEX Entry;
2335 RTFSOBJINFO ObjInfo;
2336 vrc = RTPathQueryInfoEx(szDir, &ObjInfo, RTFSOBJATTRADD_NOTHING, RTPATH_F_ON_LINK);
2337 bool fExists = RT_SUCCESS(vrc) && RTFS_IS_DIRECTORY(ObjInfo.Attr.fMode);
2338 if (!fExists)
2339 {
2340 PRTDIR pDir;
2341 vrc = RTDirOpen(&pDir, m->strBaseDir.c_str());
2342 if (RT_SUCCESS(vrc))
2343 {
2344 const char *pszMangledName = RTPathFilename(szDir);
2345 for (;;)
2346 {
2347 vrc = RTDirReadEx(pDir, &Entry, NULL /*pcbDirEntry*/, RTFSOBJATTRADD_NOTHING, RTPATH_F_ON_LINK);
2348 if (RT_FAILURE(vrc))
2349 {
2350 AssertLogRelMsg(vrc == VERR_NO_MORE_FILES, ("%Rrc\n", vrc));
2351 break;
2352 }
2353 if ( RTFS_IS_DIRECTORY(Entry.Info.Attr.fMode)
2354 && !RTStrICmp(Entry.szName, pszMangledName))
2355 {
2356 /*
2357 * The installed extension pack has a uses different case.
2358 * Update the name and directory variables.
2359 */
2360 vrc = RTPathJoin(szDir, sizeof(szDir), m->strBaseDir.c_str(), Entry.szName); /* not really necessary */
2361 AssertLogRelRCReturnStmt(vrc, E_UNEXPECTED, RTDirClose(pDir));
2362 a_pszName = Entry.szName;
2363 fExists = true;
2364 break;
2365 }
2366 }
2367 RTDirClose(pDir);
2368 }
2369 }
2370 if (fExists)
2371 {
2372 /*
2373 * We've got something, create a new extension pack object for it.
2374 */
2375 ComObjPtr<ExtPack> ptrNewExtPack;
2376 hrc = ptrNewExtPack.createObject();
2377 if (SUCCEEDED(hrc))
2378 hrc = ptrNewExtPack->initWithDir(m->enmContext, a_pszName, szDir);
2379 if (SUCCEEDED(hrc))
2380 {
2381 m->llInstalledExtPacks.push_back(ptrNewExtPack);
2382 if (ptrNewExtPack->m->fUsable)
2383 LogRel(("ExtPackManager: Found extension pack '%s'.\n", a_pszName));
2384 else
2385 LogRel(("ExtPackManager: Found bad extension pack '%s': %s\n",
2386 a_pszName, ptrNewExtPack->m->strWhyUnusable.c_str() ));
2387 pExtPack = ptrNewExtPack;
2388 }
2389 }
2390 else
2391 hrc = S_OK;
2392 }
2393
2394 /*
2395 * Report error if not usable, if that is desired.
2396 */
2397 if ( SUCCEEDED(hrc)
2398 && pExtPack
2399 && a_fUnsuableIsError
2400 && !pExtPack->m->fUsable)
2401 hrc = setError(E_FAIL, "%s", pExtPack->m->strWhyUnusable.c_str());
2402
2403 if (a_ppExtPack)
2404 *a_ppExtPack = pExtPack;
2405 return hrc;
2406}
2407
2408/**
2409 * Worker for IExtPackFile::Install.
2410 *
2411 * @returns COM status code.
2412 * @param a_pExtPackFile The extension pack file, caller checks that it's
2413 * usable.
2414 */
2415HRESULT ExtPackManager::doInstall(ExtPackFile *a_pExtPackFile)
2416{
2417 AssertReturn(m->enmContext == VBOXEXTPACKCTX_PER_USER_DAEMON, E_UNEXPECTED);
2418 iprt::MiniString const * const pStrName = &a_pExtPackFile->m->Desc.strName;
2419 iprt::MiniString const * const pStrTarball = &a_pExtPackFile->m->strExtPackFile;
2420
2421 AutoCaller autoCaller(this);
2422 HRESULT hrc = autoCaller.rc();
2423 if (SUCCEEDED(hrc))
2424 {
2425 AutoWriteLock autoLock(this COMMA_LOCKVAL_SRC_POS);
2426
2427 /*
2428 * Refresh the data we have on the extension pack as it
2429 * may be made stale by direct meddling or some other user.
2430 */
2431 ExtPack *pExtPack;
2432 hrc = refreshExtPack(pStrName->c_str(), false /*a_fUnsuableIsError*/, &pExtPack);
2433 if (SUCCEEDED(hrc) && !pExtPack)
2434 {
2435 /*
2436 * Run the privileged helper binary that performs the actual
2437 * installation. Then create an object for the packet (we do this
2438 * even on failure, to be on the safe side).
2439 */
2440/** @todo add a hash (SHA-256) of the tarball or maybe just the manifest. */
2441 hrc = runSetUidToRootHelper("install",
2442 "--base-dir", m->strBaseDir.c_str(),
2443 "--cert-dir", m->strCertificatDirPath.c_str(),
2444 "--name", pStrName->c_str(),
2445 "--tarball", pStrTarball->c_str(),
2446 (const char *)NULL);
2447 if (SUCCEEDED(hrc))
2448 {
2449 hrc = refreshExtPack(pStrName->c_str(), true /*a_fUnsuableIsError*/, &pExtPack);
2450 if (SUCCEEDED(hrc))
2451 {
2452 LogRel(("ExtPackManager: Successfully installed extension pack '%s'.\n", pStrName->c_str()));
2453 pExtPack->callInstalledHook(m->pVirtualBox, &autoLock);
2454 }
2455 }
2456 else
2457 {
2458 ErrorInfoKeeper Eik;
2459 refreshExtPack(pStrName->c_str(), false /*a_fUnsuableIsError*/, NULL);
2460 }
2461 }
2462 else if (SUCCEEDED(hrc))
2463 hrc = setError(E_FAIL,
2464 tr("Extension pack '%s' is already installed."
2465 " In case of a reinstallation, please uninstall it first"),
2466 pStrName->c_str());
2467
2468 /*
2469 * Do VirtualBoxReady callbacks now for any freshly installed
2470 * extension pack (old ones will not be called).
2471 */
2472 if (m->enmContext == VBOXEXTPACKCTX_PER_USER_DAEMON)
2473 {
2474 autoLock.release();
2475 callAllVirtualBoxReadyHooks();
2476 }
2477 }
2478
2479 return hrc;
2480}
2481
2482
2483/**
2484 * Calls the pfnVirtualBoxReady hook for all working extension packs.
2485 *
2486 * @remarks The caller must not hold any locks.
2487 */
2488void ExtPackManager::callAllVirtualBoxReadyHooks(void)
2489{
2490 AutoCaller autoCaller(this);
2491 HRESULT hrc = autoCaller.rc();
2492 if (FAILED(hrc))
2493 return;
2494 AutoWriteLock autoLock(this COMMA_LOCKVAL_SRC_POS);
2495 ComPtr<ExtPackManager> ptrSelfRef = this;
2496
2497 for (ExtPackList::iterator it = m->llInstalledExtPacks.begin();
2498 it != m->llInstalledExtPacks.end();
2499 /* advancing below */)
2500 {
2501 if ((*it)->callVirtualBoxReadyHook(m->pVirtualBox, &autoLock))
2502 it = m->llInstalledExtPacks.begin();
2503 else
2504 it++;
2505 }
2506}
2507
2508/**
2509 * Calls the pfnConsoleReady hook for all working extension packs.
2510 *
2511 * @param a_pConsole The console interface.
2512 * @remarks The caller must not hold any locks.
2513 */
2514void ExtPackManager::callAllConsoleReadyHooks(IConsole *a_pConsole)
2515{
2516 AutoCaller autoCaller(this);
2517 HRESULT hrc = autoCaller.rc();
2518 if (FAILED(hrc))
2519 return;
2520 AutoWriteLock autoLock(this COMMA_LOCKVAL_SRC_POS);
2521 ComPtr<ExtPackManager> ptrSelfRef = this;
2522
2523 for (ExtPackList::iterator it = m->llInstalledExtPacks.begin();
2524 it != m->llInstalledExtPacks.end();
2525 /* advancing below */)
2526 {
2527 if ((*it)->callConsoleReadyHook(a_pConsole, &autoLock))
2528 it = m->llInstalledExtPacks.begin();
2529 else
2530 it++;
2531 }
2532}
2533
2534/**
2535 * Calls the pfnVMCreated hook for all working extension packs.
2536 *
2537 * @param a_pMachine The machine interface of the new VM.
2538 */
2539void ExtPackManager::callAllVmCreatedHooks(IMachine *a_pMachine)
2540{
2541 AutoCaller autoCaller(this);
2542 HRESULT hrc = autoCaller.rc();
2543 if (FAILED(hrc))
2544 return;
2545 AutoWriteLock autoLock(this COMMA_LOCKVAL_SRC_POS);
2546 ComPtr<ExtPackManager> ptrSelfRef = this; /* paranoia */
2547 ExtPackList llExtPacks = m->llInstalledExtPacks;
2548
2549 for (ExtPackList::iterator it = llExtPacks.begin(); it != llExtPacks.end(); it++)
2550 (*it)->callVmCreatedHook(m->pVirtualBox, a_pMachine, &autoLock);
2551}
2552
2553/**
2554 * Calls the pfnVMConfigureVMM hook for all working extension packs.
2555 *
2556 * @returns VBox status code. Stops on the first failure, expecting the caller
2557 * to signal this to the caller of the CFGM constructor.
2558 * @param a_pConsole The console interface for the VM.
2559 * @param a_pVM The VM handle.
2560 */
2561int ExtPackManager::callAllVmConfigureVmmHooks(IConsole *a_pConsole, PVM a_pVM)
2562{
2563 AutoCaller autoCaller(this);
2564 HRESULT hrc = autoCaller.rc();
2565 if (FAILED(hrc))
2566 return Global::vboxStatusCodeFromCOM(hrc);
2567 AutoWriteLock autoLock(this COMMA_LOCKVAL_SRC_POS);
2568 ComPtr<ExtPackManager> ptrSelfRef = this; /* paranoia */
2569 ExtPackList llExtPacks = m->llInstalledExtPacks;
2570
2571 for (ExtPackList::iterator it = llExtPacks.begin(); it != llExtPacks.end(); it++)
2572 {
2573 int vrc;
2574 (*it)->callVmConfigureVmmHook(a_pConsole, a_pVM, &autoLock, &vrc);
2575 if (RT_FAILURE(vrc))
2576 return vrc;
2577 }
2578
2579 return VINF_SUCCESS;
2580}
2581
2582/**
2583 * Calls the pfnVMPowerOn hook for all working extension packs.
2584 *
2585 * @returns VBox status code. Stops on the first failure, expecting the caller
2586 * to not power on the VM.
2587 * @param a_pConsole The console interface for the VM.
2588 * @param a_pVM The VM handle.
2589 */
2590int ExtPackManager::callAllVmPowerOnHooks(IConsole *a_pConsole, PVM a_pVM)
2591{
2592 AutoCaller autoCaller(this);
2593 HRESULT hrc = autoCaller.rc();
2594 if (FAILED(hrc))
2595 return Global::vboxStatusCodeFromCOM(hrc);
2596 AutoWriteLock autoLock(this COMMA_LOCKVAL_SRC_POS);
2597 ComPtr<ExtPackManager> ptrSelfRef = this; /* paranoia */
2598 ExtPackList llExtPacks = m->llInstalledExtPacks;
2599
2600 for (ExtPackList::iterator it = llExtPacks.begin(); it != llExtPacks.end(); it++)
2601 {
2602 int vrc;
2603 (*it)->callVmPowerOnHook(a_pConsole, a_pVM, &autoLock, &vrc);
2604 if (RT_FAILURE(vrc))
2605 return vrc;
2606 }
2607
2608 return VINF_SUCCESS;
2609}
2610
2611/**
2612 * Calls the pfnVMPowerOff hook for all working extension packs.
2613 *
2614 * @param a_pConsole The console interface for the VM.
2615 * @param a_pVM The VM handle. Can be NULL.
2616 */
2617void ExtPackManager::callAllVmPowerOffHooks(IConsole *a_pConsole, PVM a_pVM)
2618{
2619 AutoCaller autoCaller(this);
2620 HRESULT hrc = autoCaller.rc();
2621 if (FAILED(hrc))
2622 return;
2623 AutoWriteLock autoLock(this COMMA_LOCKVAL_SRC_POS);
2624 ComPtr<ExtPackManager> ptrSelfRef = this; /* paranoia */
2625 ExtPackList llExtPacks = m->llInstalledExtPacks;
2626
2627 for (ExtPackList::iterator it = llExtPacks.begin(); it != llExtPacks.end(); it++)
2628 (*it)->callVmPowerOffHook(a_pConsole, a_pVM, &autoLock);
2629}
2630
2631
2632/**
2633 * Checks that the specified extension pack contains a VRDE module and that it
2634 * is shipshape.
2635 *
2636 * @returns S_OK if ok, appropriate failure status code with details.
2637 * @param a_pstrExtPack The name of the extension pack.
2638 */
2639HRESULT ExtPackManager::checkVrdeExtPack(Utf8Str const *a_pstrExtPack)
2640{
2641 AutoCaller autoCaller(this);
2642 HRESULT hrc = autoCaller.rc();
2643 if (SUCCEEDED(hrc))
2644 {
2645 AutoReadLock autoLock(this COMMA_LOCKVAL_SRC_POS);
2646
2647 ExtPack *pExtPack = findExtPack(a_pstrExtPack->c_str());
2648 if (pExtPack)
2649 hrc = pExtPack->checkVrde();
2650 else
2651 hrc = setError(VBOX_E_OBJECT_NOT_FOUND, tr("No extension pack by the name '%s' was found"), a_pstrExtPack->c_str());
2652 }
2653
2654 return hrc;
2655}
2656
2657/**
2658 * Gets the full path to the VRDE library of the specified extension pack.
2659 *
2660 * This will do extacly the same as checkVrdeExtPack and then resolve the
2661 * library path.
2662 *
2663 * @returns S_OK if a path is returned, COM error status and message return if
2664 * not.
2665 * @param a_pstrExtPack The extension pack.
2666 * @param a_pstrVrdeLibrary Where to return the path.
2667 */
2668int ExtPackManager::getVrdeLibraryPathForExtPack(Utf8Str const *a_pstrExtPack, Utf8Str *a_pstrVrdeLibrary)
2669{
2670 AutoCaller autoCaller(this);
2671 HRESULT hrc = autoCaller.rc();
2672 if (SUCCEEDED(hrc))
2673 {
2674 AutoReadLock autoLock(this COMMA_LOCKVAL_SRC_POS);
2675
2676 ExtPack *pExtPack = findExtPack(a_pstrExtPack->c_str());
2677 if (pExtPack)
2678 hrc = pExtPack->getVrdpLibraryName(a_pstrVrdeLibrary);
2679 else
2680 hrc = setError(VBOX_E_OBJECT_NOT_FOUND, tr("No extension pack by the name '%s' was found"), a_pstrExtPack->c_str());
2681 }
2682
2683 return hrc;
2684}
2685
2686/**
2687 * Gets the name of the default VRDE extension pack.
2688 *
2689 * @returns S_OK or some COM error status on red tape failure.
2690 * @param a_pstrExtPack Where to return the extension pack name. Returns
2691 * empty if no extension pack wishes to be the default
2692 * VRDP provider.
2693 */
2694HRESULT ExtPackManager::getDefaultVrdeExtPack(Utf8Str *a_pstrExtPack)
2695{
2696 a_pstrExtPack->setNull();
2697
2698 AutoCaller autoCaller(this);
2699 HRESULT hrc = autoCaller.rc();
2700 if (SUCCEEDED(hrc))
2701 {
2702 AutoReadLock autoLock(this COMMA_LOCKVAL_SRC_POS);
2703
2704 for (ExtPackList::iterator it = m->llInstalledExtPacks.begin();
2705 it != m->llInstalledExtPacks.end();
2706 it++)
2707 {
2708 if ((*it)->wantsToBeDefaultVrde())
2709 {
2710 *a_pstrExtPack = (*it)->m->Desc.strName;
2711 break;
2712 }
2713 }
2714 }
2715 return hrc;
2716}
2717
2718/**
2719 * Checks if an extension pack is (present and) usable.
2720 *
2721 * @returns @c true if it is, otherwise @c false.
2722 * @param a_pszExtPack The name of the extension pack.
2723 */
2724bool ExtPackManager::isExtPackUsable(const char *a_pszExtPack)
2725{
2726 AutoCaller autoCaller(this);
2727 HRESULT hrc = autoCaller.rc();
2728 if (FAILED(hrc))
2729 return false;
2730 AutoReadLock autoLock(this COMMA_LOCKVAL_SRC_POS);
2731
2732 ExtPack *pExtPack = findExtPack(a_pszExtPack);
2733 return pExtPack != NULL
2734 && pExtPack->m->fUsable;
2735}
2736
2737/* 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