[63197] | 1 | /* $Id: VBoxAuthSimple.cpp 93115 2022-01-01 11:31:46Z vboxsync $ */
|
---|
[33185] | 2 | /** @file
|
---|
[63197] | 3 | * VirtualBox External Authentication Library - Simple Authentication.
|
---|
[33185] | 4 | */
|
---|
| 5 |
|
---|
| 6 | /*
|
---|
[93115] | 7 | * Copyright (C) 2006-2022 Oracle Corporation
|
---|
[33185] | 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 | #include <stdlib.h>
|
---|
| 19 | #include <stdio.h>
|
---|
| 20 | #include <string.h>
|
---|
| 21 |
|
---|
| 22 | #include <iprt/cdefs.h>
|
---|
| 23 | #include <iprt/uuid.h>
|
---|
[33228] | 24 | #include <iprt/sha.h>
|
---|
[33185] | 25 |
|
---|
[34563] | 26 | #include <VBox/VBoxAuth.h>
|
---|
[33185] | 27 |
|
---|
| 28 | #include <VBox/com/com.h>
|
---|
| 29 | #include <VBox/com/string.h>
|
---|
| 30 | #include <VBox/com/Guid.h>
|
---|
| 31 | #include <VBox/com/VirtualBox.h>
|
---|
| 32 |
|
---|
| 33 | using namespace com;
|
---|
| 34 |
|
---|
| 35 | /* If defined, debug messages will be written to the specified file. */
|
---|
[34563] | 36 | //#define AUTH_DEBUG_FILE_NAME "/tmp/VBoxAuth.log"
|
---|
[33185] | 37 |
|
---|
| 38 |
|
---|
[63197] | 39 | static void dprintf(const char *pszFormat, ...)
|
---|
[33185] | 40 | {
|
---|
[34563] | 41 | #ifdef AUTH_DEBUG_FILE_NAME
|
---|
| 42 | FILE *f = fopen(AUTH_DEBUG_FILE_NAME, "ab");
|
---|
[34558] | 43 | if (f)
|
---|
| 44 | {
|
---|
[63197] | 45 | va_list va;
|
---|
| 46 | va_start(va, pszFormat);
|
---|
| 47 | vfprintf(f, pszFormat, va);
|
---|
| 48 | va_end(va);
|
---|
[34558] | 49 | fclose(f);
|
---|
| 50 | }
|
---|
[63197] | 51 | #else
|
---|
| 52 | RT_NOREF(pszFormat);
|
---|
[33185] | 53 | #endif
|
---|
| 54 | }
|
---|
| 55 |
|
---|
| 56 | RT_C_DECLS_BEGIN
|
---|
[63197] | 57 | DECLEXPORT(FNAUTHENTRY3) AuthEntry;
|
---|
| 58 | RT_C_DECLS_END
|
---|
| 59 |
|
---|
| 60 | DECLEXPORT(AuthResult) AUTHCALL AuthEntry(const char *pszCaller,
|
---|
[34558] | 61 | PAUTHUUID pUuid,
|
---|
| 62 | AuthGuestJudgement guestJudgement,
|
---|
[63197] | 63 | const char *pszUser,
|
---|
| 64 | const char *pszPassword,
|
---|
| 65 | const char *pszDomain,
|
---|
[34558] | 66 | int fLogon,
|
---|
| 67 | unsigned clientId)
|
---|
[33185] | 68 | {
|
---|
[63197] | 69 | RT_NOREF(pszCaller, guestJudgement, pszDomain, clientId);
|
---|
| 70 |
|
---|
[33185] | 71 | /* default is failed */
|
---|
[34558] | 72 | AuthResult result = AuthResultAccessDenied;
|
---|
[33185] | 73 |
|
---|
| 74 | /* only interested in logon */
|
---|
| 75 | if (!fLogon)
|
---|
| 76 | /* return value ignored */
|
---|
| 77 | return result;
|
---|
| 78 |
|
---|
| 79 | char uuid[RTUUID_STR_LENGTH] = {0};
|
---|
| 80 | if (pUuid)
|
---|
| 81 | RTUuidToStr((PCRTUUID)pUuid, (char*)uuid, RTUUID_STR_LENGTH);
|
---|
| 82 |
|
---|
| 83 | /* the user might contain a domain name, split it */
|
---|
[63197] | 84 | const char *user = strchr(pszUser, '\\');
|
---|
[33185] | 85 | if (user)
|
---|
| 86 | user++;
|
---|
| 87 | else
|
---|
[63197] | 88 | user = (char*)pszUser;
|
---|
[33185] | 89 |
|
---|
[63197] | 90 | dprintf("VBoxAuth: uuid: %s, user: %s, pszPassword: %s\n", uuid, user, pszPassword);
|
---|
[33185] | 91 |
|
---|
[60063] | 92 | ComPtr<IVirtualBoxClient> virtualBoxClient;
|
---|
[33185] | 93 | ComPtr<IVirtualBox> virtualBox;
|
---|
| 94 | HRESULT rc;
|
---|
| 95 |
|
---|
[60063] | 96 | rc = virtualBoxClient.createInprocObject(CLSID_VirtualBoxClient);
|
---|
[33185] | 97 | if (SUCCEEDED(rc))
|
---|
[63197] | 98 | {
|
---|
[60063] | 99 | rc = virtualBoxClient->COMGETTER(VirtualBox)(virtualBox.asOutParam());
|
---|
[63197] | 100 | if (SUCCEEDED(rc))
|
---|
[33185] | 101 | {
|
---|
[63197] | 102 | Bstr key = BstrFmt("VBoxAuthSimple/users/%s", user);
|
---|
| 103 | Bstr password;
|
---|
[33185] | 104 |
|
---|
[63197] | 105 | /* lookup in VM's extra data? */
|
---|
| 106 | if (pUuid)
|
---|
| 107 | {
|
---|
| 108 | ComPtr<IMachine> machine;
|
---|
| 109 | virtualBox->FindMachine(Bstr(uuid).raw(), machine.asOutParam());
|
---|
| 110 | if (machine)
|
---|
| 111 | machine->GetExtraData(key.raw(), password.asOutParam());
|
---|
| 112 | }
|
---|
| 113 | else
|
---|
| 114 | /* lookup global extra data */
|
---|
| 115 | virtualBox->GetExtraData(key.raw(), password.asOutParam());
|
---|
[33294] | 116 |
|
---|
[63197] | 117 | if (!password.isEmpty())
|
---|
| 118 | {
|
---|
| 119 | /* calculate hash */
|
---|
| 120 | uint8_t abDigest[RTSHA256_HASH_SIZE];
|
---|
| 121 | RTSha256(pszPassword, strlen(pszPassword), abDigest);
|
---|
| 122 | char pszDigest[RTSHA256_DIGEST_LEN + 1];
|
---|
| 123 | RTSha256ToString(abDigest, pszDigest, sizeof(pszDigest));
|
---|
| 124 |
|
---|
| 125 | if (password == pszDigest)
|
---|
| 126 | result = AuthResultAccessGranted;
|
---|
| 127 | }
|
---|
[33185] | 128 | }
|
---|
[63197] | 129 | else
|
---|
| 130 | dprintf("VBoxAuth: failed to get VirtualBox object reference: %#x\n", rc);
|
---|
[33185] | 131 | }
|
---|
[60063] | 132 | else
|
---|
[63197] | 133 | dprintf("VBoxAuth: failed to get VirtualBoxClient object reference: %#x\n", rc);
|
---|
[33185] | 134 |
|
---|
| 135 | return result;
|
---|
| 136 | }
|
---|
| 137 |
|
---|