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