VirtualBox

source: vbox/trunk/src/VBox/Additions/WINNT/VBoxCredProv/VBoxCredPoller.cpp@ 25883

Last change on this file since 25883 was 25883, checked in by vboxsync, 15 years ago

Additions: more header fixes

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

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette