1 | /* $Id: VBoxGuestR3LibCredentials.cpp 82968 2020-02-04 10:35:17Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * VBoxGuestR3Lib - Ring-3 Support Library for VirtualBox guest additions, user credentials.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2009-2020 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 | * The contents of this file may alternatively be used under the terms
|
---|
18 | * of the Common Development and Distribution License Version 1.0
|
---|
19 | * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
|
---|
20 | * VirtualBox OSE distribution, in which case the provisions of the
|
---|
21 | * CDDL are applicable instead of those of the GPL.
|
---|
22 | *
|
---|
23 | * You may elect to license modified versions of this file under the
|
---|
24 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
25 | */
|
---|
26 |
|
---|
27 |
|
---|
28 | /*********************************************************************************************************************************
|
---|
29 | * Header Files *
|
---|
30 | *********************************************************************************************************************************/
|
---|
31 | #include <iprt/asm.h>
|
---|
32 | #include <iprt/mem.h>
|
---|
33 | #include <iprt/rand.h>
|
---|
34 | #include <iprt/string.h>
|
---|
35 | #include <iprt/utf16.h>
|
---|
36 | #include <VBox/log.h>
|
---|
37 |
|
---|
38 | #include "VBoxGuestR3LibInternal.h"
|
---|
39 |
|
---|
40 |
|
---|
41 | /**
|
---|
42 | * Checks whether user credentials are available to the guest or not.
|
---|
43 | *
|
---|
44 | * @returns IPRT status value; VINF_SUCCESS if credentials are available,
|
---|
45 | * VERR_NOT_FOUND if not. Otherwise an error is occurred.
|
---|
46 | */
|
---|
47 | VBGLR3DECL(int) VbglR3CredentialsQueryAvailability(void)
|
---|
48 | {
|
---|
49 | VMMDevCredentials Req;
|
---|
50 | RT_ZERO(Req);
|
---|
51 | vmmdevInitRequest((VMMDevRequestHeader*)&Req, VMMDevReq_QueryCredentials);
|
---|
52 | Req.u32Flags |= VMMDEV_CREDENTIALS_QUERYPRESENCE;
|
---|
53 |
|
---|
54 | int rc = vbglR3GRPerform(&Req.header);
|
---|
55 | if (RT_SUCCESS(rc))
|
---|
56 | {
|
---|
57 | if ((Req.u32Flags & VMMDEV_CREDENTIALS_PRESENT) == 0)
|
---|
58 | rc = VERR_NOT_FOUND;
|
---|
59 | }
|
---|
60 | return rc;
|
---|
61 | }
|
---|
62 |
|
---|
63 |
|
---|
64 | /**
|
---|
65 | * Retrieves and clears the user credentials for logging into the guest OS.
|
---|
66 | *
|
---|
67 | * @returns IPRT status value
|
---|
68 | * @param ppszUser Receives pointer of allocated user name string.
|
---|
69 | * The returned pointer must be freed using VbglR3CredentialsDestroy().
|
---|
70 | * @param ppszPassword Receives pointer of allocated user password string.
|
---|
71 | * The returned pointer must be freed using VbglR3CredentialsDestroy().
|
---|
72 | * @param ppszDomain Receives pointer of allocated domain name string.
|
---|
73 | * The returned pointer must be freed using VbglR3CredentialsDestroy().
|
---|
74 | */
|
---|
75 | VBGLR3DECL(int) VbglR3CredentialsRetrieve(char **ppszUser, char **ppszPassword, char **ppszDomain)
|
---|
76 | {
|
---|
77 | AssertPtrReturn(ppszUser, VERR_INVALID_POINTER);
|
---|
78 | AssertPtrReturn(ppszPassword, VERR_INVALID_POINTER);
|
---|
79 | AssertPtrReturn(ppszDomain, VERR_INVALID_POINTER);
|
---|
80 |
|
---|
81 | VMMDevCredentials Req;
|
---|
82 | RT_ZERO(Req);
|
---|
83 | vmmdevInitRequest((VMMDevRequestHeader*)&Req, VMMDevReq_QueryCredentials);
|
---|
84 | Req.u32Flags |= VMMDEV_CREDENTIALS_READ | VMMDEV_CREDENTIALS_CLEAR;
|
---|
85 |
|
---|
86 | int rc = vbglR3GRPerform(&Req.header);
|
---|
87 | if (RT_SUCCESS(rc))
|
---|
88 | {
|
---|
89 | rc = RTStrDupEx(ppszUser, Req.szUserName);
|
---|
90 | if (RT_SUCCESS(rc))
|
---|
91 | {
|
---|
92 | rc = RTStrDupEx(ppszPassword, Req.szPassword);
|
---|
93 | if (RT_SUCCESS(rc))
|
---|
94 | {
|
---|
95 | rc = RTStrDupEx(ppszDomain, Req.szDomain);
|
---|
96 | if (RT_SUCCESS(rc))
|
---|
97 | return VINF_SUCCESS;
|
---|
98 |
|
---|
99 | RTStrFree(*ppszPassword);
|
---|
100 | }
|
---|
101 | RTStrFree(*ppszUser);
|
---|
102 | }
|
---|
103 | }
|
---|
104 | return rc;
|
---|
105 | }
|
---|
106 |
|
---|
107 |
|
---|
108 | /**
|
---|
109 | * Retrieves and clears the user credentials for logging into the guest OS.
|
---|
110 | * UTF-16 version.
|
---|
111 | *
|
---|
112 | * @returns IPRT status value
|
---|
113 | * @param ppwszUser Receives pointer of allocated user name string.
|
---|
114 | * The returned pointer must be freed using VbglR3CredentialsDestroyUtf16().
|
---|
115 | * @param ppwszPassword Receives pointer of allocated user password string.
|
---|
116 | * The returned pointer must be freed using VbglR3CredentialsDestroyUtf16().
|
---|
117 | * @param ppwszDomain Receives pointer of allocated domain name string.
|
---|
118 | * The returned pointer must be freed using VbglR3CredentialsDestroyUtf16().
|
---|
119 | */
|
---|
120 | VBGLR3DECL(int) VbglR3CredentialsRetrieveUtf16(PRTUTF16 *ppwszUser, PRTUTF16 *ppwszPassword, PRTUTF16 *ppwszDomain)
|
---|
121 | {
|
---|
122 | AssertPtrReturn(ppwszUser, VERR_INVALID_POINTER);
|
---|
123 | AssertPtrReturn(ppwszPassword, VERR_INVALID_POINTER);
|
---|
124 | AssertPtrReturn(ppwszDomain, VERR_INVALID_POINTER);
|
---|
125 |
|
---|
126 | char *pszUser, *pszPassword, *pszDomain;
|
---|
127 | int rc = VbglR3CredentialsRetrieve(&pszUser, &pszPassword, &pszDomain);
|
---|
128 | if (RT_SUCCESS(rc))
|
---|
129 | {
|
---|
130 | PRTUTF16 pwszUser = NULL;
|
---|
131 | PRTUTF16 pwszPassword = NULL;
|
---|
132 | PRTUTF16 pwszDomain = NULL;
|
---|
133 |
|
---|
134 | rc = RTStrToUtf16(pszUser, &pwszUser);
|
---|
135 | if (RT_SUCCESS(rc))
|
---|
136 | {
|
---|
137 | rc = RTStrToUtf16(pszPassword, &pwszPassword);
|
---|
138 | if (RT_SUCCESS(rc))
|
---|
139 | rc = RTStrToUtf16(pszDomain, &pwszDomain);
|
---|
140 | }
|
---|
141 |
|
---|
142 | if (RT_SUCCESS(rc))
|
---|
143 | {
|
---|
144 | *ppwszUser = pwszUser;
|
---|
145 | *ppwszPassword = pwszPassword;
|
---|
146 | *ppwszDomain = pwszDomain;
|
---|
147 | }
|
---|
148 | else
|
---|
149 | VbglR3CredentialsDestroyUtf16(pwszUser, pwszPassword, pwszDomain, 3 /* Passes */);
|
---|
150 | VbglR3CredentialsDestroy(pszUser, pszPassword, pszDomain, 3 /* Passes */);
|
---|
151 | }
|
---|
152 |
|
---|
153 | return rc;
|
---|
154 | }
|
---|
155 |
|
---|
156 |
|
---|
157 | /**
|
---|
158 | * Clears and frees the three strings.
|
---|
159 | *
|
---|
160 | * @param pszUser Receives pointer of the user name string to destroy.
|
---|
161 | * Optional.
|
---|
162 | * @param pszPassword Receives pointer of the password string to destroy.
|
---|
163 | * Optional.
|
---|
164 | * @param pszDomain Receives pointer of allocated domain name string.
|
---|
165 | * Optional.
|
---|
166 | * @param cPasses Number of wipe passes. The more the better + slower.
|
---|
167 | */
|
---|
168 | VBGLR3DECL(void) VbglR3CredentialsDestroy(char *pszUser, char *pszPassword, char *pszDomain, uint32_t cPasses)
|
---|
169 | {
|
---|
170 | /* wipe first */
|
---|
171 | if (pszUser)
|
---|
172 | RTMemWipeThoroughly(pszUser, strlen(pszUser) + 1, cPasses);
|
---|
173 | if (pszPassword)
|
---|
174 | RTMemWipeThoroughly(pszPassword, strlen(pszPassword) + 1, cPasses);
|
---|
175 | if (pszDomain)
|
---|
176 | RTMemWipeThoroughly(pszDomain, strlen(pszDomain) + 1, cPasses);
|
---|
177 |
|
---|
178 | /* then free. */
|
---|
179 | RTStrFree(pszUser);
|
---|
180 | RTStrFree(pszPassword);
|
---|
181 | RTStrFree(pszDomain);
|
---|
182 | }
|
---|
183 |
|
---|
184 |
|
---|
185 | /**
|
---|
186 | * Clears and frees the three strings. UTF-16 version.
|
---|
187 | *
|
---|
188 | * @param pwszUser Receives pointer of the user name string to destroy.
|
---|
189 | * Optional.
|
---|
190 | * @param pwszPassword Receives pointer of the password string to destroy.
|
---|
191 | * Optional.
|
---|
192 | * @param pwszDomain Receives pointer of allocated domain name string.
|
---|
193 | * Optional.
|
---|
194 | * @param cPasses Number of wipe passes. The more the better + slower.
|
---|
195 | */
|
---|
196 | VBGLR3DECL(void) VbglR3CredentialsDestroyUtf16(PRTUTF16 pwszUser, PRTUTF16 pwszPassword, PRTUTF16 pwszDomain,
|
---|
197 | uint32_t cPasses)
|
---|
198 | {
|
---|
199 | /* wipe first */
|
---|
200 | if (pwszUser)
|
---|
201 | RTMemWipeThoroughly(pwszUser, (RTUtf16Len(pwszUser) + 1) * sizeof(RTUTF16), cPasses);
|
---|
202 | if (pwszPassword)
|
---|
203 | RTMemWipeThoroughly(pwszPassword, (RTUtf16Len(pwszPassword) + 1) * sizeof(RTUTF16), cPasses);
|
---|
204 | if (pwszDomain)
|
---|
205 | RTMemWipeThoroughly(pwszDomain, (RTUtf16Len(pwszDomain) + 1) * sizeof(RTUTF16), cPasses);
|
---|
206 |
|
---|
207 | /* then free. */
|
---|
208 | RTUtf16Free(pwszUser);
|
---|
209 | RTUtf16Free(pwszPassword);
|
---|
210 | RTUtf16Free(pwszDomain);
|
---|
211 | }
|
---|
212 |
|
---|