1 | /*
|
---|
2 | * vboxweb.h:
|
---|
3 | * header file for "real" web server code.
|
---|
4 | *
|
---|
5 | * Copyright (C) 2006-2007 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 |
|
---|
36 | #include <VBox/err.h>
|
---|
37 |
|
---|
38 | #include <iprt/stream.h>
|
---|
39 |
|
---|
40 | #include <string>
|
---|
41 |
|
---|
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 | /****************************************************************************
|
---|
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 | int rc;
|
---|
227 | ManagedObjectRef *pRef;
|
---|
228 | if ((rc = ManagedObjectRef::findRefFromId(id, &pRef, fNullAllowed)))
|
---|
229 | RaiseSoapInvalidObjectFault(soap, id);
|
---|
230 | else
|
---|
231 | {
|
---|
232 | if (fNullAllowed && pRef == NULL)
|
---|
233 | {
|
---|
234 | pComPtr.setNull();
|
---|
235 | return 0;
|
---|
236 | }
|
---|
237 |
|
---|
238 | // pRef->getComPtr returns a ComPtr<IUnknown>; by casting it to
|
---|
239 | // ComPtr<T>, we implicitly do a COM queryInterface() call
|
---|
240 | if (pComPtr = pRef->getComPtr())
|
---|
241 | return 0;
|
---|
242 |
|
---|
243 | WEBDEBUG((" Interface not supported for object reference %s, which is of class %s\n", id.c_str(), pRef->getInterfaceName()));
|
---|
244 | rc = VERR_WEB_UNSUPPORTED_INTERFACE;
|
---|
245 | RaiseSoapInvalidObjectFault(soap, id); // @todo better message
|
---|
246 | }
|
---|
247 |
|
---|
248 | return rc;
|
---|
249 | }
|
---|
250 |
|
---|
251 | /**
|
---|
252 | * Template function that creates a new managed object for the given COM
|
---|
253 | * pointer of the template class T. If a reference already exists for the
|
---|
254 | * given pointer, then that reference's ID is returned instead.
|
---|
255 | *
|
---|
256 | * @param idParent managed object reference of calling object; used to extract session ID
|
---|
257 | * @param pc COM object for which to create a reference
|
---|
258 | * @return existing or new managed object reference
|
---|
259 | */
|
---|
260 | template <class T>
|
---|
261 | WSDLT_ID createOrFindRefFromComPtr(const WSDLT_ID &idParent,
|
---|
262 | const char *pcszInterface,
|
---|
263 | const ComPtr<T> &pc)
|
---|
264 | {
|
---|
265 | // NULL comptr should return NULL MOR
|
---|
266 | if (pc.isNull())
|
---|
267 | {
|
---|
268 | WEBDEBUG((" createOrFindRefFromComPtr(): returning empty MOR for NULL COM pointer\n"));
|
---|
269 | return "";
|
---|
270 | }
|
---|
271 |
|
---|
272 | WebServiceSession *pSession;
|
---|
273 | if ((pSession = WebServiceSession::findSessionFromRef(idParent)))
|
---|
274 | {
|
---|
275 | // WEBDEBUG(("\n-- found session for %s\n", idParent.c_str()));
|
---|
276 | ManagedObjectRef *pRef;
|
---|
277 | if ( ((pRef = pSession->findRefFromPtr(pc)))
|
---|
278 | || ((pRef = new ManagedObjectRef(*pSession, pcszInterface, pc)))
|
---|
279 | )
|
---|
280 | return pRef->toWSDL();
|
---|
281 | }
|
---|
282 |
|
---|
283 | return 0;
|
---|
284 | }
|
---|
285 |
|
---|