VirtualBox

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

Last change on this file since 35242 was 35237, checked in by vboxsync, 14 years ago

ExtPack: Don't suppress gksu and kdesudo output, just ignore extra the output on success.

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