1 | /*
|
---|
2 | * vboxweb.h:
|
---|
3 | * header file for "real" web server code.
|
---|
4 | *
|
---|
5 | * Copyright (C) 2006-2010 Oracle Corporation
|
---|
6 | *
|
---|
7 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
8 | * available from http://www.virtualbox.org. This file is free software;
|
---|
9 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
10 | * General Public License (GPL) as published by the Free Software
|
---|
11 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
12 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
13 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
14 | */
|
---|
15 |
|
---|
16 | /****************************************************************************
|
---|
17 | *
|
---|
18 | * debug macro
|
---|
19 | *
|
---|
20 | ****************************************************************************/
|
---|
21 |
|
---|
22 | void WebLog(const char *pszFormat, ...);
|
---|
23 |
|
---|
24 | #define WEBDEBUG(a) if (g_fVerbose) { WebLog a; }
|
---|
25 |
|
---|
26 | #define LOG_GROUP LOG_GROUP_WEBSERVICE
|
---|
27 | #include <VBox/log.h>
|
---|
28 |
|
---|
29 | #include <VBox/com/VirtualBox.h>
|
---|
30 | #include <VBox/com/Guid.h>
|
---|
31 | #include <VBox/com/AutoLock.h>
|
---|
32 |
|
---|
33 | #include <VBox/err.h>
|
---|
34 |
|
---|
35 | #include <iprt/stream.h>
|
---|
36 |
|
---|
37 | #include <string>
|
---|
38 |
|
---|
39 | /****************************************************************************
|
---|
40 | *
|
---|
41 | * global variables
|
---|
42 | *
|
---|
43 | ****************************************************************************/
|
---|
44 |
|
---|
45 | extern ComPtr<IVirtualBox> g_pVirtualBox;
|
---|
46 | extern bool g_fVerbose;
|
---|
47 |
|
---|
48 | extern PRTSTREAM g_pstrLog;
|
---|
49 |
|
---|
50 | extern util::RWLockHandle *g_pAuthLibLockHandle;
|
---|
51 |
|
---|
52 | extern util::RWLockHandle *g_pSessionsLockHandle;
|
---|
53 |
|
---|
54 | /****************************************************************************
|
---|
55 | *
|
---|
56 | * typedefs
|
---|
57 | *
|
---|
58 | ****************************************************************************/
|
---|
59 |
|
---|
60 | // type used by gSOAP-generated code
|
---|
61 | typedef std::string WSDLT_ID; // combined managed object ref (session ID plus object ID)
|
---|
62 | typedef std::string vbox__uuid;
|
---|
63 |
|
---|
64 | /****************************************************************************
|
---|
65 | *
|
---|
66 | * SOAP exceptions
|
---|
67 | *
|
---|
68 | ****************************************************************************/
|
---|
69 |
|
---|
70 | void RaiseSoapInvalidObjectFault(struct soap *soap, WSDLT_ID obj);
|
---|
71 |
|
---|
72 | void RaiseSoapRuntimeFault(struct soap *soap, HRESULT apirc, IUnknown *pObj);
|
---|
73 |
|
---|
74 | /****************************************************************************
|
---|
75 | *
|
---|
76 | * conversion helpers
|
---|
77 | *
|
---|
78 | ****************************************************************************/
|
---|
79 |
|
---|
80 | std::string ConvertComString(const com::Bstr &bstr);
|
---|
81 |
|
---|
82 | std::string ConvertComString(const com::Guid &bstr);
|
---|
83 |
|
---|
84 | /****************************************************************************
|
---|
85 | *
|
---|
86 | * managed object reference classes
|
---|
87 | *
|
---|
88 | ****************************************************************************/
|
---|
89 |
|
---|
90 | class WebServiceSessionPrivate;
|
---|
91 | class ManagedObjectRef;
|
---|
92 |
|
---|
93 | /**
|
---|
94 | * An instance of this gets created for every client that logs onto the
|
---|
95 | * webservice (via the special IWebsessionManager::logon() SOAP API) and
|
---|
96 | * maintains the managed object references for that session.
|
---|
97 | */
|
---|
98 | class WebServiceSession
|
---|
99 | {
|
---|
100 | friend class ManagedObjectRef;
|
---|
101 |
|
---|
102 | private:
|
---|
103 | uint64_t _uSessionID;
|
---|
104 | WebServiceSessionPrivate *_pp; // opaque data struct (defined in vboxweb.cpp)
|
---|
105 | bool _fDestructing;
|
---|
106 |
|
---|
107 | ManagedObjectRef *_pISession;
|
---|
108 |
|
---|
109 | time_t _tLastObjectLookup;
|
---|
110 |
|
---|
111 | // hide the copy constructor because we're not copyable
|
---|
112 | WebServiceSession(const WebServiceSession ©From);
|
---|
113 |
|
---|
114 | public:
|
---|
115 | WebServiceSession();
|
---|
116 |
|
---|
117 | ~WebServiceSession();
|
---|
118 |
|
---|
119 | int authenticate(const char *pcszUsername,
|
---|
120 | const char *pcszPassword);
|
---|
121 |
|
---|
122 | ManagedObjectRef* findRefFromPtr(const ComPtr<IUnknown> &pcu);
|
---|
123 |
|
---|
124 | uint64_t getID() const
|
---|
125 | {
|
---|
126 | return _uSessionID;
|
---|
127 | }
|
---|
128 |
|
---|
129 | WSDLT_ID getSessionObject() const;
|
---|
130 |
|
---|
131 | void touch();
|
---|
132 |
|
---|
133 | time_t getLastObjectLookup() const
|
---|
134 | {
|
---|
135 | return _tLastObjectLookup;
|
---|
136 | }
|
---|
137 |
|
---|
138 | static WebServiceSession* findSessionFromRef(const WSDLT_ID &id);
|
---|
139 |
|
---|
140 | void DumpRefs();
|
---|
141 | };
|
---|
142 |
|
---|
143 | /**
|
---|
144 | * ManagedObjectRef is used to map COM pointers to object IDs
|
---|
145 | * within a session. Such object IDs are 64-bit integers.
|
---|
146 | *
|
---|
147 | * When a webservice method call is invoked on an object, it
|
---|
148 | * has an opaque string called a "managed object reference". Such
|
---|
149 | * a string consists of a session ID combined with an object ID.
|
---|
150 | *
|
---|
151 | */
|
---|
152 | class ManagedObjectRef
|
---|
153 | {
|
---|
154 | protected:
|
---|
155 | // owning session:
|
---|
156 | WebServiceSession &_session;
|
---|
157 |
|
---|
158 | // value:
|
---|
159 | ComPtr<IUnknown> _pObj;
|
---|
160 | const char *_pcszInterface;
|
---|
161 |
|
---|
162 | // keys:
|
---|
163 | uint64_t _id;
|
---|
164 | uintptr_t _ulp;
|
---|
165 |
|
---|
166 | // long ID as string
|
---|
167 | WSDLT_ID _strID;
|
---|
168 |
|
---|
169 | public:
|
---|
170 | ManagedObjectRef(WebServiceSession &session,
|
---|
171 | const char *pcszInterface,
|
---|
172 | const ComPtr<IUnknown> &obj);
|
---|
173 | ~ManagedObjectRef();
|
---|
174 |
|
---|
175 | uint64_t getID()
|
---|
176 | {
|
---|
177 | return _id;
|
---|
178 | }
|
---|
179 |
|
---|
180 | ComPtr<IUnknown> getComPtr()
|
---|
181 | {
|
---|
182 | return _pObj;
|
---|
183 | }
|
---|
184 |
|
---|
185 | WSDLT_ID toWSDL() const;
|
---|
186 | const char* getInterfaceName() const
|
---|
187 | {
|
---|
188 | return _pcszInterface;
|
---|
189 | }
|
---|
190 |
|
---|
191 | static int findRefFromId(const WSDLT_ID &id,
|
---|
192 | ManagedObjectRef **pRef,
|
---|
193 | bool fNullAllowed);
|
---|
194 |
|
---|
195 | static ManagedObjectRef* findFromPtr(ComPtr<IUnknown> pcu);
|
---|
196 | static ManagedObjectRef* create(const WSDLT_ID &idParent,
|
---|
197 | ComPtr<IUnknown> pcu);
|
---|
198 |
|
---|
199 | };
|
---|
200 |
|
---|
201 | /**
|
---|
202 | * Template function that resolves a managed object reference to a COM pointer
|
---|
203 | * of the template class T. Gets called from tons of generated code in
|
---|
204 | * methodmaps.cpp.
|
---|
205 | *
|
---|
206 | * This is a template function so that we can support ComPtr's for arbitrary
|
---|
207 | * interfaces and automatically verify that the managed object reference on
|
---|
208 | * the internal stack actually is of the expected interface.
|
---|
209 | *
|
---|
210 | * @param soap
|
---|
211 | * @param id in: integer managed object reference, as passed in by web service client
|
---|
212 | * @param pComPtr out: reference to COM pointer object that receives the com pointer,
|
---|
213 | * if SOAP_OK is returned
|
---|
214 | * @param fNullAllowed in: if true, then this func returns a NULL COM pointer if an
|
---|
215 | * empty MOR is passed in (i.e. NULL pointers are allowed). If false,
|
---|
216 | * then this fails; this will be false when called for the "this"
|
---|
217 | * argument of method calls, which really shouldn't be NULL.
|
---|
218 | * @return error code or SOAP_OK if no error
|
---|
219 | */
|
---|
220 | template <class T>
|
---|
221 | int findComPtrFromId(struct soap *soap,
|
---|
222 | const WSDLT_ID &id,
|
---|
223 | ComPtr<T> &pComPtr,
|
---|
224 | bool fNullAllowed)
|
---|
225 | {
|
---|
226 | // we're only reading the MOR maps, not modifying them, so a readlock is good enough
|
---|
227 | // (allow concurrency, this code gets called from everywhere in methodmaps.cpp)
|
---|
228 | util::AutoReadLock lock(g_pSessionsLockHandle COMMA_LOCKVAL_SRC_POS);
|
---|
229 |
|
---|
230 | int rc;
|
---|
231 | ManagedObjectRef *pRef;
|
---|
232 | if ((rc = ManagedObjectRef::findRefFromId(id, &pRef, fNullAllowed)))
|
---|
233 | RaiseSoapInvalidObjectFault(soap, id);
|
---|
234 | else
|
---|
235 | {
|
---|
236 | if (fNullAllowed && pRef == NULL)
|
---|
237 | {
|
---|
238 | pComPtr.setNull();
|
---|
239 | return 0;
|
---|
240 | }
|
---|
241 |
|
---|
242 | // pRef->getComPtr returns a ComPtr<IUnknown>; by casting it to
|
---|
243 | // ComPtr<T>, we implicitly do a COM queryInterface() call
|
---|
244 | if (pComPtr = pRef->getComPtr())
|
---|
245 | return 0;
|
---|
246 |
|
---|
247 | WEBDEBUG((" Interface not supported for object reference %s, which is of class %s\n", id.c_str(), pRef->getInterfaceName()));
|
---|
248 | rc = VERR_WEB_UNSUPPORTED_INTERFACE;
|
---|
249 | RaiseSoapInvalidObjectFault(soap, id); // @todo better message
|
---|
250 | }
|
---|
251 |
|
---|
252 | return rc;
|
---|
253 | }
|
---|
254 |
|
---|
255 | /**
|
---|
256 | * Template function that creates a new managed object for the given COM
|
---|
257 | * pointer of the template class T. If a reference already exists for the
|
---|
258 | * given pointer, then that reference's ID is returned instead.
|
---|
259 | *
|
---|
260 | * @param idParent managed object reference of calling object; used to extract session ID
|
---|
261 | * @param pc COM object for which to create a reference
|
---|
262 | * @return existing or new managed object reference
|
---|
263 | */
|
---|
264 | template <class T>
|
---|
265 | WSDLT_ID createOrFindRefFromComPtr(const WSDLT_ID &idParent,
|
---|
266 | const char *pcszInterface,
|
---|
267 | const ComPtr<T> &pc)
|
---|
268 | {
|
---|
269 | // NULL comptr should return NULL MOR
|
---|
270 | if (pc.isNull())
|
---|
271 | {
|
---|
272 | WEBDEBUG((" createOrFindRefFromComPtr(): returning empty MOR for NULL COM pointer\n"));
|
---|
273 | return "";
|
---|
274 | }
|
---|
275 |
|
---|
276 | // we might be modifying the MOR maps below, so request write lock now
|
---|
277 | util::AutoWriteLock lock(g_pSessionsLockHandle COMMA_LOCKVAL_SRC_POS);
|
---|
278 | WebServiceSession *pSession;
|
---|
279 | if ((pSession = WebServiceSession::findSessionFromRef(idParent)))
|
---|
280 | {
|
---|
281 | // WEBDEBUG(("\n-- found session for %s\n", idParent.c_str()));
|
---|
282 | ManagedObjectRef *pRef;
|
---|
283 | if ( ((pRef = pSession->findRefFromPtr(pc)))
|
---|
284 | || ((pRef = new ManagedObjectRef(*pSession, pcszInterface, pc)))
|
---|
285 | )
|
---|
286 | return pRef->toWSDL();
|
---|
287 | }
|
---|
288 |
|
---|
289 | // session has expired, return an empty MOR instead of allocating a
|
---|
290 | // new reference which couldn't be used anyway.
|
---|
291 | return "";
|
---|
292 | }
|
---|
293 |
|
---|