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