diff options
Diffstat (limited to 'OpenSim/Server/Handlers/UserAccounts')
-rw-r--r-- | OpenSim/Server/Handlers/UserAccounts/UserAccountServerConnector.cs | 64 | ||||
-rw-r--r-- | OpenSim/Server/Handlers/UserAccounts/UserAccountServerPostHandler.cs | 348 |
2 files changed, 412 insertions, 0 deletions
diff --git a/OpenSim/Server/Handlers/UserAccounts/UserAccountServerConnector.cs b/OpenSim/Server/Handlers/UserAccounts/UserAccountServerConnector.cs new file mode 100644 index 0000000..e95e3dc --- /dev/null +++ b/OpenSim/Server/Handlers/UserAccounts/UserAccountServerConnector.cs | |||
@@ -0,0 +1,64 @@ | |||
1 | /* | ||
2 | * Copyright (c) Contributors, http://opensimulator.org/ | ||
3 | * See CONTRIBUTORS.TXT for a full list of copyright holders. | ||
4 | * | ||
5 | * Redistribution and use in source and binary forms, with or without | ||
6 | * modification, are permitted provided that the following conditions are met: | ||
7 | * * Redistributions of source code must retain the above copyright | ||
8 | * notice, this list of conditions and the following disclaimer. | ||
9 | * * Redistributions in binary form must reproduce the above copyright | ||
10 | * notice, this list of conditions and the following disclaimer in the | ||
11 | * documentation and/or other materials provided with the distribution. | ||
12 | * * Neither the name of the OpenSimulator Project nor the | ||
13 | * names of its contributors may be used to endorse or promote products | ||
14 | * derived from this software without specific prior written permission. | ||
15 | * | ||
16 | * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY | ||
17 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | ||
18 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | ||
19 | * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY | ||
20 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | ||
21 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | ||
22 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND | ||
23 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||
24 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | ||
25 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
26 | */ | ||
27 | |||
28 | using System; | ||
29 | using Nini.Config; | ||
30 | using OpenSim.Server.Base; | ||
31 | using OpenSim.Services.Interfaces; | ||
32 | using OpenSim.Framework.Servers.HttpServer; | ||
33 | using OpenSim.Framework.ServiceAuth; | ||
34 | using OpenSim.Server.Handlers.Base; | ||
35 | |||
36 | namespace OpenSim.Server.Handlers.UserAccounts | ||
37 | { | ||
38 | public class UserAccountServiceConnector : ServiceConnector | ||
39 | { | ||
40 | private IUserAccountService m_UserAccountService; | ||
41 | private string m_ConfigName = "UserAccountService"; | ||
42 | |||
43 | public UserAccountServiceConnector(IConfigSource config, IHttpServer server, string configName) : | ||
44 | base(config, server, configName) | ||
45 | { | ||
46 | IConfig serverConfig = config.Configs[m_ConfigName]; | ||
47 | if (serverConfig == null) | ||
48 | throw new Exception(String.Format("No section {0} in config file", m_ConfigName)); | ||
49 | |||
50 | string service = serverConfig.GetString("LocalServiceModule", | ||
51 | String.Empty); | ||
52 | |||
53 | if (service == String.Empty) | ||
54 | throw new Exception("No LocalServiceModule in config file"); | ||
55 | |||
56 | Object[] args = new Object[] { config }; | ||
57 | m_UserAccountService = ServerUtils.LoadPlugin<IUserAccountService>(service, args); | ||
58 | |||
59 | IServiceAuth auth = ServiceAuth.Create(config, m_ConfigName); | ||
60 | |||
61 | server.AddStreamHandler(new UserAccountServerPostHandler(m_UserAccountService, serverConfig, auth)); | ||
62 | } | ||
63 | } | ||
64 | } | ||
diff --git a/OpenSim/Server/Handlers/UserAccounts/UserAccountServerPostHandler.cs b/OpenSim/Server/Handlers/UserAccounts/UserAccountServerPostHandler.cs new file mode 100644 index 0000000..21eb790 --- /dev/null +++ b/OpenSim/Server/Handlers/UserAccounts/UserAccountServerPostHandler.cs | |||
@@ -0,0 +1,348 @@ | |||
1 | /* | ||
2 | * Copyright (c) Contributors, http://opensimulator.org/ | ||
3 | * See CONTRIBUTORS.TXT for a full list of copyright holders. | ||
4 | * | ||
5 | * Redistribution and use in source and binary forms, with or without | ||
6 | * modification, are permitted provided that the following conditions are met: | ||
7 | * * Redistributions of source code must retain the above copyright | ||
8 | * notice, this list of conditions and the following disclaimer. | ||
9 | * * Redistributions in binary form must reproduce the above copyright | ||
10 | * notice, this list of conditions and the following disclaimer in the | ||
11 | * documentation and/or other materials provided with the distribution. | ||
12 | * * Neither the name of the OpenSimulator Project nor the | ||
13 | * names of its contributors may be used to endorse or promote products | ||
14 | * derived from this software without specific prior written permission. | ||
15 | * | ||
16 | * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY | ||
17 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | ||
18 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | ||
19 | * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY | ||
20 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | ||
21 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | ||
22 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND | ||
23 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||
24 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | ||
25 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
26 | */ | ||
27 | |||
28 | using Nini.Config; | ||
29 | using log4net; | ||
30 | using System; | ||
31 | using System.Reflection; | ||
32 | using System.IO; | ||
33 | using System.Net; | ||
34 | using System.Text; | ||
35 | using System.Text.RegularExpressions; | ||
36 | using System.Xml; | ||
37 | using System.Xml.Serialization; | ||
38 | using System.Collections.Generic; | ||
39 | using OpenSim.Server.Base; | ||
40 | using OpenSim.Services.Interfaces; | ||
41 | using OpenSim.Services.UserAccountService; | ||
42 | using OpenSim.Framework; | ||
43 | using OpenSim.Framework.Servers.HttpServer; | ||
44 | using OpenSim.Framework.ServiceAuth; | ||
45 | using OpenMetaverse; | ||
46 | |||
47 | namespace OpenSim.Server.Handlers.UserAccounts | ||
48 | { | ||
49 | public class UserAccountServerPostHandler : BaseStreamHandler | ||
50 | { | ||
51 | private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); | ||
52 | |||
53 | private IUserAccountService m_UserAccountService; | ||
54 | private bool m_AllowCreateUser = false; | ||
55 | private bool m_AllowSetAccount = false; | ||
56 | |||
57 | public UserAccountServerPostHandler(IUserAccountService service) | ||
58 | : this(service, null, null) {} | ||
59 | |||
60 | public UserAccountServerPostHandler(IUserAccountService service, IConfig config, IServiceAuth auth) : | ||
61 | base("POST", "/accounts", auth) | ||
62 | { | ||
63 | m_UserAccountService = service; | ||
64 | |||
65 | if (config != null) | ||
66 | { | ||
67 | m_AllowCreateUser = config.GetBoolean("AllowCreateUser", m_AllowCreateUser); | ||
68 | m_AllowSetAccount = config.GetBoolean("AllowSetAccount", m_AllowSetAccount); | ||
69 | } | ||
70 | } | ||
71 | |||
72 | protected override byte[] ProcessRequest(string path, Stream requestData, | ||
73 | IOSHttpRequest httpRequest, IOSHttpResponse httpResponse) | ||
74 | { | ||
75 | StreamReader sr = new StreamReader(requestData); | ||
76 | string body = sr.ReadToEnd(); | ||
77 | sr.Close(); | ||
78 | body = body.Trim(); | ||
79 | |||
80 | // We need to check the authorization header | ||
81 | //httpRequest.Headers["authorization"] ... | ||
82 | |||
83 | //m_log.DebugFormat("[XXX]: query String: {0}", body); | ||
84 | string method = string.Empty; | ||
85 | try | ||
86 | { | ||
87 | Dictionary<string, object> request = | ||
88 | ServerUtils.ParseQueryString(body); | ||
89 | |||
90 | if (!request.ContainsKey("METHOD")) | ||
91 | return FailureResult(); | ||
92 | |||
93 | method = request["METHOD"].ToString(); | ||
94 | |||
95 | switch (method) | ||
96 | { | ||
97 | case "createuser": | ||
98 | if (m_AllowCreateUser) | ||
99 | return CreateUser(request); | ||
100 | else | ||
101 | break; | ||
102 | case "getaccount": | ||
103 | return GetAccount(request); | ||
104 | case "getaccounts": | ||
105 | return GetAccounts(request); | ||
106 | case "setaccount": | ||
107 | if (m_AllowSetAccount) | ||
108 | return StoreAccount(request); | ||
109 | else | ||
110 | break; | ||
111 | } | ||
112 | |||
113 | m_log.DebugFormat("[USER SERVICE HANDLER]: unknown method request: {0}", method); | ||
114 | } | ||
115 | catch (Exception e) | ||
116 | { | ||
117 | m_log.DebugFormat("[USER SERVICE HANDLER]: Exception in method {0}: {1}", method, e); | ||
118 | } | ||
119 | |||
120 | return FailureResult(); | ||
121 | } | ||
122 | |||
123 | byte[] GetAccount(Dictionary<string, object> request) | ||
124 | { | ||
125 | UserAccount account = null; | ||
126 | UUID scopeID = UUID.Zero; | ||
127 | Dictionary<string, object> result = new Dictionary<string, object>(); | ||
128 | |||
129 | if (request.ContainsKey("ScopeID") && !UUID.TryParse(request["ScopeID"].ToString(), out scopeID)) | ||
130 | { | ||
131 | result["result"] = "null"; | ||
132 | return ResultToBytes(result); | ||
133 | } | ||
134 | |||
135 | if (request.ContainsKey("UserID") && request["UserID"] != null) | ||
136 | { | ||
137 | UUID userID; | ||
138 | if (UUID.TryParse(request["UserID"].ToString(), out userID)) | ||
139 | account = m_UserAccountService.GetUserAccount(scopeID, userID); | ||
140 | } | ||
141 | else if (request.ContainsKey("PrincipalID") && request["PrincipalID"] != null) | ||
142 | { | ||
143 | UUID userID; | ||
144 | if (UUID.TryParse(request["PrincipalID"].ToString(), out userID)) | ||
145 | account = m_UserAccountService.GetUserAccount(scopeID, userID); | ||
146 | } | ||
147 | else if (request.ContainsKey("Email") && request["Email"] != null) | ||
148 | { | ||
149 | account = m_UserAccountService.GetUserAccount(scopeID, request["Email"].ToString()); | ||
150 | } | ||
151 | else if (request.ContainsKey("FirstName") && request.ContainsKey("LastName") && | ||
152 | request["FirstName"] != null && request["LastName"] != null) | ||
153 | { | ||
154 | account = m_UserAccountService.GetUserAccount(scopeID, request["FirstName"].ToString(), request["LastName"].ToString()); | ||
155 | } | ||
156 | |||
157 | if (account == null) | ||
158 | { | ||
159 | result["result"] = "null"; | ||
160 | } | ||
161 | else | ||
162 | { | ||
163 | result["result"] = account.ToKeyValuePairs(); | ||
164 | } | ||
165 | |||
166 | return ResultToBytes(result); | ||
167 | } | ||
168 | |||
169 | byte[] GetAccounts(Dictionary<string, object> request) | ||
170 | { | ||
171 | if (!request.ContainsKey("query")) | ||
172 | return FailureResult(); | ||
173 | |||
174 | UUID scopeID = UUID.Zero; | ||
175 | if (request.ContainsKey("ScopeID") && !UUID.TryParse(request["ScopeID"].ToString(), out scopeID)) | ||
176 | return FailureResult(); | ||
177 | |||
178 | string query = request["query"].ToString(); | ||
179 | |||
180 | List<UserAccount> accounts = m_UserAccountService.GetUserAccounts(scopeID, query); | ||
181 | |||
182 | Dictionary<string, object> result = new Dictionary<string, object>(); | ||
183 | if ((accounts == null) || ((accounts != null) && (accounts.Count == 0))) | ||
184 | { | ||
185 | result["result"] = "null"; | ||
186 | } | ||
187 | else | ||
188 | { | ||
189 | int i = 0; | ||
190 | foreach (UserAccount acc in accounts) | ||
191 | { | ||
192 | Dictionary<string, object> rinfoDict = acc.ToKeyValuePairs(); | ||
193 | result["account" + i] = rinfoDict; | ||
194 | i++; | ||
195 | } | ||
196 | } | ||
197 | |||
198 | string xmlString = ServerUtils.BuildXmlResponse(result); | ||
199 | |||
200 | //m_log.DebugFormat("[GRID HANDLER]: resp string: {0}", xmlString); | ||
201 | return Util.UTF8NoBomEncoding.GetBytes(xmlString); | ||
202 | } | ||
203 | |||
204 | byte[] StoreAccount(Dictionary<string, object> request) | ||
205 | { | ||
206 | UUID principalID = UUID.Zero; | ||
207 | if (request.ContainsKey("PrincipalID") && !UUID.TryParse(request["PrincipalID"].ToString(), out principalID)) | ||
208 | return FailureResult(); | ||
209 | |||
210 | UUID scopeID = UUID.Zero; | ||
211 | if (request.ContainsKey("ScopeID") && !UUID.TryParse(request["ScopeID"].ToString(), out scopeID)) | ||
212 | return FailureResult(); | ||
213 | |||
214 | UserAccount existingAccount = m_UserAccountService.GetUserAccount(scopeID, principalID); | ||
215 | if (existingAccount == null) | ||
216 | return FailureResult(); | ||
217 | |||
218 | Dictionary<string, object> result = new Dictionary<string, object>(); | ||
219 | |||
220 | if (request.ContainsKey("FirstName")) | ||
221 | existingAccount.FirstName = request["FirstName"].ToString(); | ||
222 | |||
223 | if (request.ContainsKey("LastName")) | ||
224 | existingAccount.LastName = request["LastName"].ToString(); | ||
225 | |||
226 | if (request.ContainsKey("Email")) | ||
227 | existingAccount.Email = request["Email"].ToString(); | ||
228 | |||
229 | int created = 0; | ||
230 | if (request.ContainsKey("Created") && int.TryParse(request["Created"].ToString(), out created)) | ||
231 | existingAccount.Created = created; | ||
232 | |||
233 | int userLevel = 0; | ||
234 | if (request.ContainsKey("UserLevel") && int.TryParse(request["UserLevel"].ToString(), out userLevel)) | ||
235 | existingAccount.UserLevel = userLevel; | ||
236 | |||
237 | int userFlags = 0; | ||
238 | if (request.ContainsKey("UserFlags") && int.TryParse(request["UserFlags"].ToString(), out userFlags)) | ||
239 | existingAccount.UserFlags = userFlags; | ||
240 | |||
241 | if (request.ContainsKey("UserTitle")) | ||
242 | existingAccount.UserTitle = request["UserTitle"].ToString(); | ||
243 | |||
244 | if (!m_UserAccountService.StoreUserAccount(existingAccount)) | ||
245 | { | ||
246 | m_log.ErrorFormat( | ||
247 | "[USER ACCOUNT SERVER POST HANDLER]: Account store failed for account {0} {1} {2}", | ||
248 | existingAccount.FirstName, existingAccount.LastName, existingAccount.PrincipalID); | ||
249 | |||
250 | return FailureResult(); | ||
251 | } | ||
252 | |||
253 | result["result"] = existingAccount.ToKeyValuePairs(); | ||
254 | |||
255 | return ResultToBytes(result); | ||
256 | } | ||
257 | |||
258 | byte[] CreateUser(Dictionary<string, object> request) | ||
259 | { | ||
260 | if (! request.ContainsKey("FirstName") | ||
261 | && request.ContainsKey("LastName") | ||
262 | && request.ContainsKey("Password")) | ||
263 | return FailureResult(); | ||
264 | |||
265 | Dictionary<string, object> result = new Dictionary<string, object>(); | ||
266 | |||
267 | UUID scopeID = UUID.Zero; | ||
268 | if (request.ContainsKey("ScopeID") && !UUID.TryParse(request["ScopeID"].ToString(), out scopeID)) | ||
269 | return FailureResult(); | ||
270 | |||
271 | UUID principalID = UUID.Random(); | ||
272 | if (request.ContainsKey("PrincipalID") && !UUID.TryParse(request["PrincipalID"].ToString(), out principalID)) | ||
273 | return FailureResult(); | ||
274 | |||
275 | string firstName = request["FirstName"].ToString(); | ||
276 | string lastName = request["LastName"].ToString(); | ||
277 | string password = request["Password"].ToString(); | ||
278 | |||
279 | string email = ""; | ||
280 | if (request.ContainsKey("Email")) | ||
281 | email = request["Email"].ToString(); | ||
282 | |||
283 | UserAccount createdUserAccount = null; | ||
284 | |||
285 | if (m_UserAccountService is UserAccountService) | ||
286 | createdUserAccount | ||
287 | = ((UserAccountService)m_UserAccountService).CreateUser( | ||
288 | scopeID, principalID, firstName, lastName, password, email); | ||
289 | |||
290 | if (createdUserAccount == null) | ||
291 | return FailureResult(); | ||
292 | |||
293 | result["result"] = createdUserAccount.ToKeyValuePairs(); | ||
294 | |||
295 | return ResultToBytes(result); | ||
296 | } | ||
297 | |||
298 | private byte[] SuccessResult() | ||
299 | { | ||
300 | XmlDocument doc = new XmlDocument(); | ||
301 | |||
302 | XmlNode xmlnode = doc.CreateNode(XmlNodeType.XmlDeclaration, | ||
303 | "", ""); | ||
304 | |||
305 | doc.AppendChild(xmlnode); | ||
306 | |||
307 | XmlElement rootElement = doc.CreateElement("", "ServerResponse", | ||
308 | ""); | ||
309 | |||
310 | doc.AppendChild(rootElement); | ||
311 | |||
312 | XmlElement result = doc.CreateElement("", "result", ""); | ||
313 | result.AppendChild(doc.CreateTextNode("Success")); | ||
314 | |||
315 | rootElement.AppendChild(result); | ||
316 | |||
317 | return Util.DocToBytes(doc); | ||
318 | } | ||
319 | |||
320 | private byte[] FailureResult() | ||
321 | { | ||
322 | XmlDocument doc = new XmlDocument(); | ||
323 | |||
324 | XmlNode xmlnode = doc.CreateNode(XmlNodeType.XmlDeclaration, | ||
325 | "", ""); | ||
326 | |||
327 | doc.AppendChild(xmlnode); | ||
328 | |||
329 | XmlElement rootElement = doc.CreateElement("", "ServerResponse", | ||
330 | ""); | ||
331 | |||
332 | doc.AppendChild(rootElement); | ||
333 | |||
334 | XmlElement result = doc.CreateElement("", "result", ""); | ||
335 | result.AppendChild(doc.CreateTextNode("Failure")); | ||
336 | |||
337 | rootElement.AppendChild(result); | ||
338 | |||
339 | return Util.DocToBytes(doc); | ||
340 | } | ||
341 | |||
342 | private byte[] ResultToBytes(Dictionary<string, object> result) | ||
343 | { | ||
344 | string xmlString = ServerUtils.BuildXmlResponse(result); | ||
345 | return Util.UTF8NoBomEncoding.GetBytes(xmlString); | ||
346 | } | ||
347 | } | ||
348 | } \ No newline at end of file | ||