1 | /* $Id: VBoxCredPoller.cpp 25811 2010-01-13 17:10:01Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * VBoxCredPoller - Thread for retrieving user credentials.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2009 Sun Microsystems, Inc.
|
---|
8 | *
|
---|
9 | * Sun Microsystems, Inc. confidential
|
---|
10 | * All rights reserved
|
---|
11 | */
|
---|
12 |
|
---|
13 |
|
---|
14 | #include <windows.h>
|
---|
15 |
|
---|
16 | #include <VBox/VBoxGuest.h>
|
---|
17 | #include <VBox/VBoxGuestLib.h>
|
---|
18 | #include <VBox/VMMDev.h>
|
---|
19 | #include <iprt/string.h>
|
---|
20 |
|
---|
21 | #include "dll.h"
|
---|
22 | #include "VBoxCredProv.h"
|
---|
23 | #include "VBoxCredential.h"
|
---|
24 | #include "VBoxCredPoller.h"
|
---|
25 |
|
---|
26 |
|
---|
27 | VBoxCredPoller::VBoxCredPoller()
|
---|
28 | {
|
---|
29 | m_hThreadPoller = NIL_RTTHREAD;
|
---|
30 | m_pProv = NULL;
|
---|
31 |
|
---|
32 | m_pszUser = NULL;
|
---|
33 | m_pszPw = NULL;
|
---|
34 | m_pszDomain = NULL;
|
---|
35 | }
|
---|
36 |
|
---|
37 |
|
---|
38 | VBoxCredPoller::~VBoxCredPoller()
|
---|
39 | {
|
---|
40 | Shutdown();
|
---|
41 | }
|
---|
42 |
|
---|
43 |
|
---|
44 | bool VBoxCredPoller::Initialize(VBoxCredProv *pProvider)
|
---|
45 | {
|
---|
46 | Log(("VBoxCredPoller::Initialize\n"));
|
---|
47 |
|
---|
48 | if (m_pProv != NULL)
|
---|
49 | m_pProv->Release();
|
---|
50 | m_pProv = pProvider;
|
---|
51 | Assert(m_pProv);
|
---|
52 | m_pProv->AddRef();
|
---|
53 |
|
---|
54 | /* don't create more than one of them */
|
---|
55 | if (m_hThreadPoller != NIL_RTTHREAD)
|
---|
56 | {
|
---|
57 | Log(("VBoxCredPoller::Initialize: Thread already running, returning!\n"));
|
---|
58 | return false;
|
---|
59 | }
|
---|
60 |
|
---|
61 | int rc = RTCritSectInit(&m_csCredUpate);
|
---|
62 | if (RT_FAILURE(rc))
|
---|
63 | Log(("VBoxCredPoller: Could not init critical section! rc = %Rrc\n", rc));
|
---|
64 |
|
---|
65 | /* create the poller thread */
|
---|
66 | rc = RTThreadCreate(&m_hThreadPoller, VBoxCredPoller::threadPoller, this, 0, RTTHREADTYPE_INFREQUENT_POLLER,
|
---|
67 | RTTHREADFLAGS_WAITABLE, "creds");
|
---|
68 | if (RT_FAILURE(rc))
|
---|
69 | {
|
---|
70 | Log(("VBoxCredPoller::Initialize: Failed to create thread, rc = %Rrc\n", rc));
|
---|
71 | return false;
|
---|
72 | }
|
---|
73 | return true;
|
---|
74 | }
|
---|
75 |
|
---|
76 |
|
---|
77 | bool VBoxCredPoller::Shutdown(void)
|
---|
78 | {
|
---|
79 | Log(("VBoxCredPoller::Shutdown\n"));
|
---|
80 |
|
---|
81 | if (m_hThreadPoller == NIL_RTTHREAD)
|
---|
82 | {
|
---|
83 | Log(("VBoxCredPoller::Shutdown: Either thread or exit sem is NULL!\n"));
|
---|
84 | return false;
|
---|
85 | }
|
---|
86 |
|
---|
87 | /* Post termination event semaphore */
|
---|
88 | int rc = RTThreadUserSignal(m_hThreadPoller);
|
---|
89 | if (RT_SUCCESS(rc))
|
---|
90 | {
|
---|
91 | Log(("VBoxCredPoller::Shutdown: Waiting for thread to terminate\n"));
|
---|
92 | /* wait until the thread has terminated */
|
---|
93 | rc = RTThreadWait(m_hThreadPoller, RT_INDEFINITE_WAIT, NULL);
|
---|
94 | Log(("VBoxCredPoller::Shutdown: Thread has (probably) terminated (rc = %Rrc)\n", rc));
|
---|
95 | }
|
---|
96 | else
|
---|
97 | {
|
---|
98 | /* failed to signal the thread - very unlikely - so no point in waiting long. */
|
---|
99 | Log(("VBoxCredPoller::Shutdown: Failed to signal semaphore, rc = %Rrc\n", rc));
|
---|
100 | rc = RTThreadWait(m_hThreadPoller, 100, NULL);
|
---|
101 | Log(("VBoxCredPoller::Shutdown: Thread has terminated? wait rc = %Rrc\n", rc));
|
---|
102 | }
|
---|
103 |
|
---|
104 | if (m_pProv != NULL)
|
---|
105 | {
|
---|
106 | m_pProv->Release();
|
---|
107 | m_pProv = NULL;
|
---|
108 | }
|
---|
109 |
|
---|
110 | credentialsReset();
|
---|
111 | RTCritSectDelete(&m_csCredUpate);
|
---|
112 |
|
---|
113 | m_hThreadPoller = NIL_RTTHREAD;
|
---|
114 | return true;
|
---|
115 | }
|
---|
116 |
|
---|
117 |
|
---|
118 | int VBoxCredPoller::credentialsRetrieve(void)
|
---|
119 | {
|
---|
120 | credentialsReset();
|
---|
121 |
|
---|
122 | /* get credentials */
|
---|
123 | RTCritSectEnter(&m_csCredUpate);
|
---|
124 | int rc = VbglR3CredentialsRetrieve(&m_pszUser, &m_pszPw, &m_pszDomain);
|
---|
125 | if (RT_SUCCESS(rc))
|
---|
126 | {
|
---|
127 | Log(("VBoxCredPoller::credentialsRetrieve: Credentials retrieved (user=%s, pw=%s, domain=%s)\n",
|
---|
128 | m_pszUser ? m_pszUser : "<empty>",
|
---|
129 | m_pszPw ? m_pszPw : "<empty>",
|
---|
130 | m_pszDomain ? m_pszDomain : "<empty>"));
|
---|
131 |
|
---|
132 | /* allocated but empty? delete and re-fill with default value in block below. */
|
---|
133 | if (strlen(m_pszDomain) == 0)
|
---|
134 | {
|
---|
135 | RTStrFree(m_pszDomain);
|
---|
136 | m_pszDomain = NULL;
|
---|
137 | }
|
---|
138 |
|
---|
139 | /* if we don't have a domain specified, fill in a dot (".") specifying the
|
---|
140 | * local computer. */
|
---|
141 | if (m_pszDomain == NULL)
|
---|
142 | {
|
---|
143 | rc = RTStrAPrintf(&m_pszDomain, ".");
|
---|
144 | if (RT_FAILURE(rc))
|
---|
145 | Log(("VBoxCredPoller::credentialsRetrieve: Could not set default domain name, rc = %Rrc", rc));
|
---|
146 | else
|
---|
147 | Log(("VBoxCredPoller::credentialsRetrieve: No domain name given, set default value to: %s\n", m_pszDomain));
|
---|
148 | }
|
---|
149 | }
|
---|
150 |
|
---|
151 | /* if all went fine, notify parent */
|
---|
152 | if (RT_SUCCESS(rc))
|
---|
153 | {
|
---|
154 | Assert(m_pProv);
|
---|
155 | m_pProv->OnCredentialsProvided(m_pszUser,
|
---|
156 | m_pszPw,
|
---|
157 | m_pszDomain);
|
---|
158 | }
|
---|
159 | RTCritSectLeave(&m_csCredUpate);
|
---|
160 | return rc;
|
---|
161 | }
|
---|
162 |
|
---|
163 |
|
---|
164 | bool VBoxCredPoller::QueryCredentials(VBoxCredential *pCred)
|
---|
165 | {
|
---|
166 | Assert (pCred);
|
---|
167 | RTCritSectEnter(&m_csCredUpate);
|
---|
168 | pCred->Update(m_pszUser, m_pszPw, m_pszDomain);
|
---|
169 | RTCritSectLeave(&m_csCredUpate);
|
---|
170 |
|
---|
171 | bool bRet = ( (m_pszUser && strlen(m_pszUser))
|
---|
172 | || (m_pszPw && strlen(m_pszPw))
|
---|
173 | || (m_pszDomain && strlen(m_pszDomain)));
|
---|
174 | credentialsReset();
|
---|
175 | return bRet;
|
---|
176 | }
|
---|
177 |
|
---|
178 |
|
---|
179 | void VBoxCredPoller::credentialsReset(void)
|
---|
180 | {
|
---|
181 | RTCritSectEnter(&m_csCredUpate);
|
---|
182 |
|
---|
183 | if (m_pszUser)
|
---|
184 | SecureZeroMemory(m_pszUser, strlen(m_pszUser) * sizeof(char));
|
---|
185 | if (m_pszPw)
|
---|
186 | SecureZeroMemory(m_pszPw, strlen(m_pszPw) * sizeof(char));
|
---|
187 | if (m_pszDomain)
|
---|
188 | SecureZeroMemory(m_pszDomain, strlen(m_pszDomain) * sizeof(char));
|
---|
189 |
|
---|
190 | RTStrFree(m_pszUser);
|
---|
191 | m_pszUser = NULL;
|
---|
192 | RTStrFree(m_pszPw);
|
---|
193 | m_pszPw = NULL;
|
---|
194 | RTStrFree(m_pszDomain);
|
---|
195 | m_pszDomain = NULL;
|
---|
196 |
|
---|
197 | RTCritSectLeave(&m_csCredUpate);
|
---|
198 | }
|
---|
199 |
|
---|
200 |
|
---|
201 | DECLCALLBACK(int) VBoxCredPoller::threadPoller(RTTHREAD ThreadSelf, void *pvUser)
|
---|
202 | {
|
---|
203 | Log(("VBoxCredPoller::threadPoller\n"));
|
---|
204 |
|
---|
205 | VBoxCredPoller *pThis = (VBoxCredPoller *)pvUser;
|
---|
206 |
|
---|
207 | do
|
---|
208 | {
|
---|
209 | int rc;
|
---|
210 | if (VbglR3CredentialsAreAvailable())
|
---|
211 | {
|
---|
212 | Log(("VBoxCredPoller::threadPoller: Credentials available.\n"));
|
---|
213 | Assert(pThis);
|
---|
214 | rc = pThis->credentialsRetrieve();
|
---|
215 | }
|
---|
216 |
|
---|
217 | /* wait a bit */
|
---|
218 | if (RTThreadUserWait(ThreadSelf, 500) == VINF_SUCCESS)
|
---|
219 | {
|
---|
220 | Log(("VBoxCredPoller::threadPoller: Terminating\n"));
|
---|
221 | /* we were asked to terminate, do that instantly! */
|
---|
222 | return 0;
|
---|
223 | }
|
---|
224 | }
|
---|
225 | while (1);
|
---|
226 |
|
---|
227 | return 0;
|
---|
228 | }
|
---|
229 |
|
---|