1 | /* $Id: VBoxGuestR3LibCredentials.cpp 26243 2010-02-04 16:39:26Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * VBoxGuestR3Lib - Ring-3 Support Library for VirtualBox guest additions, user credentials.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2009 Sun Microsystems, Inc.
|
---|
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 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
|
---|
18 | * Clara, CA 95054 USA or visit http://www.sun.com if you need
|
---|
19 | * additional information or have any questions.
|
---|
20 | */
|
---|
21 |
|
---|
22 |
|
---|
23 | /*******************************************************************************
|
---|
24 | * Header Files *
|
---|
25 | *******************************************************************************/
|
---|
26 | #include <iprt/asm.h>
|
---|
27 | #include <iprt/string.h>
|
---|
28 | #include <iprt/rand.h>
|
---|
29 | #include <VBox/log.h>
|
---|
30 |
|
---|
31 | #include "VBGLR3Internal.h"
|
---|
32 |
|
---|
33 |
|
---|
34 | /**
|
---|
35 | * Checks whether user credentials are available to the guest or not.
|
---|
36 | *
|
---|
37 | * @returns true if credentials are available, false if not (or error occured).
|
---|
38 | */
|
---|
39 | VBGLR3DECL(bool) VbglR3CredentialsAreAvailable(void)
|
---|
40 | {
|
---|
41 | VMMDevCredentials Req;
|
---|
42 | RT_ZERO(Req);
|
---|
43 | vmmdevInitRequest((VMMDevRequestHeader*)&Req, VMMDevReq_QueryCredentials);
|
---|
44 | Req.u32Flags |= VMMDEV_CREDENTIALS_QUERYPRESENCE;
|
---|
45 |
|
---|
46 | int rc = vbglR3GRPerform(&Req.header);
|
---|
47 | return RT_SUCCESS(rc)
|
---|
48 | && (Req.u32Flags & VMMDEV_CREDENTIALS_PRESENT) != 0;
|
---|
49 | }
|
---|
50 |
|
---|
51 |
|
---|
52 | /**
|
---|
53 | * Retrieves and clears the user credentials for logging into the guest OS.
|
---|
54 | *
|
---|
55 | * @returns IPRT status value
|
---|
56 | * @param ppszUser Receives pointer of allocated user name string.
|
---|
57 | * The returned pointer must be freed using VbglR3CredentialsDestroy().
|
---|
58 | * @param ppszPassword Receives pointer of allocated user password string.
|
---|
59 | * The returned pointer must be freed using VbglR3CredentialsDestroy().
|
---|
60 | * @param ppszDomain Receives pointer of allocated domain name string.
|
---|
61 | * The returned pointer must be freed using VbglR3CredentialsDestroy().
|
---|
62 | */
|
---|
63 | VBGLR3DECL(int) VbglR3CredentialsRetrieve(char **ppszUser, char **ppszPassword, char **ppszDomain)
|
---|
64 | {
|
---|
65 | VMMDevCredentials Req;
|
---|
66 | RT_ZERO(Req);
|
---|
67 | vmmdevInitRequest((VMMDevRequestHeader*)&Req, VMMDevReq_QueryCredentials);
|
---|
68 | Req.u32Flags |= VMMDEV_CREDENTIALS_READ | VMMDEV_CREDENTIALS_CLEAR;
|
---|
69 |
|
---|
70 | int rc = vbglR3GRPerform(&Req.header);
|
---|
71 | if (RT_SUCCESS(rc))
|
---|
72 | {
|
---|
73 | rc = RTStrDupEx(ppszUser, Req.szUserName);
|
---|
74 | if (RT_SUCCESS(rc))
|
---|
75 | {
|
---|
76 | rc = RTStrDupEx(ppszPassword, Req.szPassword);
|
---|
77 | if (RT_SUCCESS(rc))
|
---|
78 | {
|
---|
79 | rc = RTStrDupEx(ppszDomain, Req.szDomain);
|
---|
80 | if (RT_SUCCESS(rc))
|
---|
81 | return VINF_SUCCESS;
|
---|
82 |
|
---|
83 | RTStrFree(*ppszPassword);
|
---|
84 | }
|
---|
85 | RTStrFree(*ppszUser);
|
---|
86 | }
|
---|
87 | }
|
---|
88 | return rc;
|
---|
89 | }
|
---|
90 |
|
---|
91 |
|
---|
92 | /**
|
---|
93 | * Clears and frees the three strings.
|
---|
94 | *
|
---|
95 | * @param pszUser Receives pointer of the user name string to destroy.
|
---|
96 | * Optional.
|
---|
97 | * @param pszPassword Receives pointer of the password string to destroy.
|
---|
98 | * Optional.
|
---|
99 | * @param pszDomain Receives pointer of allocated domain name string.
|
---|
100 | * Optional.
|
---|
101 | * @param cPasses Number of wipe passes. The more the better + slower.
|
---|
102 | */
|
---|
103 | VBGLR3DECL(void) VbglR3CredentialsDestroy(char *pszUser, char *pszPassword, char *pszDomain, uint32_t cPasses)
|
---|
104 | {
|
---|
105 | size_t const cchUser = pszUser ? strlen(pszUser) : 0;
|
---|
106 | size_t const cchPassword = pszPassword ? strlen(pszPassword) : 0;
|
---|
107 | size_t const cchDomain = pszDomain ? strlen(pszDomain) : 0;
|
---|
108 |
|
---|
109 | do
|
---|
110 | {
|
---|
111 | if (cchUser)
|
---|
112 | memset(pszUser, 0xff, cchUser);
|
---|
113 | if (cchPassword)
|
---|
114 | memset(pszPassword, 0xff, cchPassword);
|
---|
115 | if (cchDomain)
|
---|
116 | memset(pszDomain, 0xff, cchDomain);
|
---|
117 | ASMMemoryFence();
|
---|
118 |
|
---|
119 | if (cchUser)
|
---|
120 | memset(pszUser, 0x00, cchUser);
|
---|
121 | if (cchPassword)
|
---|
122 | memset(pszPassword, 0x00, cchPassword);
|
---|
123 | if (cchDomain)
|
---|
124 | memset(pszDomain, 0x00, cchDomain);
|
---|
125 | ASMMemoryFence();
|
---|
126 |
|
---|
127 | if (cchUser)
|
---|
128 | RTRandBytes(pszUser, cchUser);
|
---|
129 | if (cchPassword)
|
---|
130 | RTRandBytes(pszPassword, cchPassword);
|
---|
131 | if (cchDomain)
|
---|
132 | RTRandBytes(pszDomain, cchDomain);
|
---|
133 | ASMMemoryFence();
|
---|
134 |
|
---|
135 | } while (cPasses-- > 0);
|
---|
136 |
|
---|
137 | RTStrFree(pszUser);
|
---|
138 | RTStrFree(pszPassword);
|
---|
139 | RTStrFree(pszDomain);
|
---|
140 | }
|
---|
141 |
|
---|