1 | /** @file
|
---|
2 | *
|
---|
3 | * VBox Remote Desktop Protocol:
|
---|
4 | * External Authentication Library:
|
---|
5 | * Simple Authentication.
|
---|
6 | */
|
---|
7 |
|
---|
8 | /*
|
---|
9 | * Copyright (C) 2006-2010 Oracle Corporation
|
---|
10 | *
|
---|
11 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
12 | * available from http://www.virtualbox.org. This file is free software;
|
---|
13 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
14 | * General Public License (GPL) as published by the Free Software
|
---|
15 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
16 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
17 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
18 | */
|
---|
19 |
|
---|
20 | #include <stdlib.h>
|
---|
21 | #include <stdio.h>
|
---|
22 | #include <string.h>
|
---|
23 |
|
---|
24 | #include <iprt/cdefs.h>
|
---|
25 | #include <iprt/uuid.h>
|
---|
26 | #include <iprt/sha.h>
|
---|
27 |
|
---|
28 | #include <VBox/VRDPAuth.h>
|
---|
29 |
|
---|
30 | #include <VBox/com/com.h>
|
---|
31 | #include <VBox/com/string.h>
|
---|
32 | #include <VBox/com/Guid.h>
|
---|
33 | #include <VBox/com/VirtualBox.h>
|
---|
34 |
|
---|
35 | using namespace com;
|
---|
36 |
|
---|
37 | /* If defined, debug messages will be written to the specified file. */
|
---|
38 | //#define VRDPAUTH_DEBUG_FILE_NAME "/tmp/VRDPAuth.log"
|
---|
39 |
|
---|
40 |
|
---|
41 | static void dprintf(const char *fmt, ...)
|
---|
42 | {
|
---|
43 | #ifdef VRDPAUTH_DEBUG_FILE_NAME
|
---|
44 | va_list va;
|
---|
45 |
|
---|
46 | va_start(va, fmt);
|
---|
47 |
|
---|
48 | char buffer[1024];
|
---|
49 |
|
---|
50 | vsnprintf(buffer, sizeof(buffer), fmt, va);
|
---|
51 |
|
---|
52 | FILE *f = fopen(VRDPAUTH_DEBUG_FILE_NAME, "ab");
|
---|
53 | fprintf(f, "%s", buffer);
|
---|
54 | fclose(f);
|
---|
55 |
|
---|
56 | va_end (va);
|
---|
57 | #endif
|
---|
58 | }
|
---|
59 |
|
---|
60 | RT_C_DECLS_BEGIN
|
---|
61 | DECLEXPORT(VRDPAuthResult) VRDPAUTHCALL VRDPAuth2(PVRDPAUTHUUID pUuid,
|
---|
62 | VRDPAuthGuestJudgement guestJudgement,
|
---|
63 | const char *szUser,
|
---|
64 | const char *szPassword,
|
---|
65 | const char *szDomain,
|
---|
66 | int fLogon,
|
---|
67 | unsigned clientId)
|
---|
68 | {
|
---|
69 | /* default is failed */
|
---|
70 | VRDPAuthResult result = VRDPAuthAccessDenied;
|
---|
71 |
|
---|
72 | /* only interested in logon */
|
---|
73 | if (!fLogon)
|
---|
74 | /* return value ignored */
|
---|
75 | return result;
|
---|
76 |
|
---|
77 | char uuid[RTUUID_STR_LENGTH] = {0};
|
---|
78 | if (pUuid)
|
---|
79 | RTUuidToStr((PCRTUUID)pUuid, (char*)uuid, RTUUID_STR_LENGTH);
|
---|
80 |
|
---|
81 | /* the user might contain a domain name, split it */
|
---|
82 | char *user = strchr((char*)szUser, '\\');
|
---|
83 | if (user)
|
---|
84 | user++;
|
---|
85 | else
|
---|
86 | user = (char*)szUser;
|
---|
87 |
|
---|
88 | dprintf("VBoxAuth: uuid: %s, user: %s, szPassword: %s\n", uuid, user, szPassword);
|
---|
89 |
|
---|
90 | ComPtr<IVirtualBox> virtualBox;
|
---|
91 | HRESULT rc;
|
---|
92 |
|
---|
93 | rc = virtualBox.createLocalObject(CLSID_VirtualBox);
|
---|
94 | if (SUCCEEDED(rc))
|
---|
95 | {
|
---|
96 | Bstr key = BstrFmt("VBoxAuthSimple/users/%s", user);
|
---|
97 | Bstr password;
|
---|
98 |
|
---|
99 | /* lookup in VM's extra data? */
|
---|
100 | if (pUuid)
|
---|
101 | {
|
---|
102 | ComPtr<IMachine> machine;
|
---|
103 | virtualBox->GetMachine(Bstr(uuid).raw(), machine.asOutParam());
|
---|
104 | if (machine)
|
---|
105 | machine->GetExtraData(key.raw(), password.asOutParam());
|
---|
106 | } else
|
---|
107 | /* lookup global extra data */
|
---|
108 | virtualBox->GetExtraData(key.raw(), password.asOutParam());
|
---|
109 |
|
---|
110 | if (!password.isEmpty())
|
---|
111 | {
|
---|
112 | /* calculate hash */
|
---|
113 | uint8_t abDigest[RTSHA256_HASH_SIZE];
|
---|
114 | RTSha256(szPassword, strlen(szPassword), abDigest);
|
---|
115 | char pszDigest[RTSHA256_DIGEST_LEN + 1];
|
---|
116 | RTSha256ToString(abDigest, pszDigest, sizeof(pszDigest));
|
---|
117 |
|
---|
118 | if (password == pszDigest)
|
---|
119 | result = VRDPAuthAccessGranted;
|
---|
120 | }
|
---|
121 | }
|
---|
122 |
|
---|
123 | return result;
|
---|
124 | }
|
---|
125 | RT_C_DECLS_END
|
---|
126 |
|
---|
127 | /* Verify the function prototype. */
|
---|
128 | static PVRDPAUTHENTRY2 gpfnAuthEntry = VRDPAuth2;
|
---|