diff options
Diffstat (limited to 'OpenSim/Services/Connectors/SimianGrid/SimianAuthenticationServiceConnector.cs')
-rw-r--r-- | OpenSim/Services/Connectors/SimianGrid/SimianAuthenticationServiceConnector.cs | 246 |
1 files changed, 246 insertions, 0 deletions
diff --git a/OpenSim/Services/Connectors/SimianGrid/SimianAuthenticationServiceConnector.cs b/OpenSim/Services/Connectors/SimianGrid/SimianAuthenticationServiceConnector.cs new file mode 100644 index 0000000..b19135e --- /dev/null +++ b/OpenSim/Services/Connectors/SimianGrid/SimianAuthenticationServiceConnector.cs | |||
@@ -0,0 +1,246 @@ | |||
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 System.Collections.Specialized; | ||
30 | using System.Reflection; | ||
31 | using log4net; | ||
32 | using Mono.Addins; | ||
33 | using Nini.Config; | ||
34 | using OpenMetaverse; | ||
35 | using OpenMetaverse.StructuredData; | ||
36 | using OpenSim.Framework; | ||
37 | using OpenSim.Region.Framework.Interfaces; | ||
38 | using OpenSim.Region.Framework.Scenes; | ||
39 | using OpenSim.Services.Interfaces; | ||
40 | |||
41 | namespace OpenSim.Services.Connectors.SimianGrid | ||
42 | { | ||
43 | /// <summary> | ||
44 | /// Connects authentication/authorization to the SimianGrid backend | ||
45 | /// </summary> | ||
46 | [Extension(Path = "/OpenSim/RegionModules", NodeName = "RegionModule")] | ||
47 | public class SimianAuthenticationServiceConnector : IAuthenticationService, ISharedRegionModule | ||
48 | { | ||
49 | private static readonly ILog m_log = | ||
50 | LogManager.GetLogger( | ||
51 | MethodBase.GetCurrentMethod().DeclaringType); | ||
52 | |||
53 | private string m_serverUrl = String.Empty; | ||
54 | |||
55 | #region ISharedRegionModule | ||
56 | |||
57 | public Type ReplaceableInterface { get { return null; } } | ||
58 | public void RegionLoaded(Scene scene) { } | ||
59 | public void PostInitialise() { } | ||
60 | public void Close() { } | ||
61 | |||
62 | public SimianAuthenticationServiceConnector() { } | ||
63 | public string Name { get { return "SimianAuthenticationServiceConnector"; } } | ||
64 | public void AddRegion(Scene scene) { if (!String.IsNullOrEmpty(m_serverUrl)) { scene.RegisterModuleInterface<IAuthenticationService>(this); } } | ||
65 | public void RemoveRegion(Scene scene) { if (!String.IsNullOrEmpty(m_serverUrl)) { scene.UnregisterModuleInterface<IAuthenticationService>(this); } } | ||
66 | |||
67 | #endregion ISharedRegionModule | ||
68 | |||
69 | public SimianAuthenticationServiceConnector(IConfigSource source) | ||
70 | { | ||
71 | Initialise(source); | ||
72 | } | ||
73 | |||
74 | public void Initialise(IConfigSource source) | ||
75 | { | ||
76 | if (Simian.IsSimianEnabled(source, "AuthenticationServices", this.Name)) | ||
77 | { | ||
78 | IConfig assetConfig = source.Configs["AuthenticationService"]; | ||
79 | if (assetConfig == null) | ||
80 | { | ||
81 | m_log.Error("[SIMIAN AUTH CONNECTOR]: AuthenticationService missing from OpenSim.ini"); | ||
82 | throw new Exception("Authentication connector init error"); | ||
83 | } | ||
84 | |||
85 | string serviceURI = assetConfig.GetString("AuthenticationServerURI"); | ||
86 | if (String.IsNullOrEmpty(serviceURI)) | ||
87 | { | ||
88 | m_log.Error("[SIMIAN AUTH CONNECTOR]: No Server URI named in section AuthenticationService"); | ||
89 | throw new Exception("Authentication connector init error"); | ||
90 | } | ||
91 | |||
92 | m_serverUrl = serviceURI; | ||
93 | } | ||
94 | } | ||
95 | |||
96 | public string Authenticate(UUID principalID, string password, int lifetime) | ||
97 | { | ||
98 | NameValueCollection requestArgs = new NameValueCollection | ||
99 | { | ||
100 | { "RequestMethod", "GetIdentities" }, | ||
101 | { "UserID", principalID.ToString() } | ||
102 | }; | ||
103 | |||
104 | OSDMap response = WebUtil.PostToService(m_serverUrl, requestArgs); | ||
105 | if (response["Success"].AsBoolean() && response["Identities"] is OSDArray) | ||
106 | { | ||
107 | bool md5hashFound = false; | ||
108 | |||
109 | OSDArray identities = (OSDArray)response["Identities"]; | ||
110 | for (int i = 0; i < identities.Count; i++) | ||
111 | { | ||
112 | OSDMap identity = identities[i] as OSDMap; | ||
113 | if (identity != null) | ||
114 | { | ||
115 | if (identity["Type"].AsString() == "md5hash") | ||
116 | { | ||
117 | string credential = identity["Credential"].AsString(); | ||
118 | |||
119 | if (password == credential || "$1$" + Utils.MD5String(password) == credential || Utils.MD5String(password) == credential) | ||
120 | return Authorize(principalID); | ||
121 | |||
122 | md5hashFound = true; | ||
123 | break; | ||
124 | } | ||
125 | } | ||
126 | } | ||
127 | |||
128 | if (md5hashFound) | ||
129 | m_log.Warn("[SIMIAN AUTH CONNECTOR]: Authentication failed for " + principalID + " using md5hash $1$" + Utils.MD5String(password)); | ||
130 | else | ||
131 | m_log.Warn("[SIMIAN AUTH CONNECTOR]: Authentication failed for " + principalID + ", no md5hash identity found"); | ||
132 | } | ||
133 | else | ||
134 | { | ||
135 | m_log.Warn("[SIMIAN AUTH CONNECTOR]: Failed to retrieve identities for " + principalID + ": " + | ||
136 | response["Message"].AsString()); | ||
137 | } | ||
138 | |||
139 | return String.Empty; | ||
140 | } | ||
141 | |||
142 | public bool Verify(UUID principalID, string token, int lifetime) | ||
143 | { | ||
144 | NameValueCollection requestArgs = new NameValueCollection | ||
145 | { | ||
146 | { "RequestMethod", "GetSession" }, | ||
147 | { "SessionID", token } | ||
148 | }; | ||
149 | |||
150 | OSDMap response = WebUtil.PostToService(m_serverUrl, requestArgs); | ||
151 | if (response["Success"].AsBoolean()) | ||
152 | { | ||
153 | return true; | ||
154 | } | ||
155 | else | ||
156 | { | ||
157 | m_log.Warn("[SIMIAN AUTH CONNECTOR]: Could not verify session for " + principalID + ": " + | ||
158 | response["Message"].AsString()); | ||
159 | } | ||
160 | |||
161 | return false; | ||
162 | } | ||
163 | |||
164 | public bool Release(UUID principalID, string token) | ||
165 | { | ||
166 | NameValueCollection requestArgs = new NameValueCollection | ||
167 | { | ||
168 | { "RequestMethod", "RemoveSession" }, | ||
169 | { "UserID", principalID.ToString() } | ||
170 | }; | ||
171 | |||
172 | OSDMap response = WebUtil.PostToService(m_serverUrl, requestArgs); | ||
173 | if (response["Success"].AsBoolean()) | ||
174 | { | ||
175 | return true; | ||
176 | } | ||
177 | else | ||
178 | { | ||
179 | m_log.Warn("[SIMIAN AUTH CONNECTOR]: Failed to remove session for " + principalID + ": " + | ||
180 | response["Message"].AsString()); | ||
181 | } | ||
182 | |||
183 | return false; | ||
184 | } | ||
185 | |||
186 | public bool SetPassword(UUID principalID, string passwd) | ||
187 | { | ||
188 | // Fetch the user name first | ||
189 | NameValueCollection requestArgs = new NameValueCollection | ||
190 | { | ||
191 | { "RequestMethod", "GetUser" }, | ||
192 | { "UserID", principalID.ToString() } | ||
193 | }; | ||
194 | |||
195 | OSDMap response = WebUtil.PostToService(m_serverUrl, requestArgs); | ||
196 | if (response["Success"].AsBoolean() && response["User"] is OSDMap) | ||
197 | { | ||
198 | OSDMap userMap = (OSDMap)response["User"]; | ||
199 | string identifier = userMap["Name"].AsString(); | ||
200 | |||
201 | if (!String.IsNullOrEmpty(identifier)) | ||
202 | { | ||
203 | // Add/update the md5hash identity | ||
204 | requestArgs = new NameValueCollection | ||
205 | { | ||
206 | { "RequestMethod", "AddIdentity" }, | ||
207 | { "Identifier", identifier }, | ||
208 | { "Credential", "$1$" + Utils.MD5String(passwd) }, | ||
209 | { "Type", "md5hash" }, | ||
210 | { "UserID", principalID.ToString() } | ||
211 | }; | ||
212 | |||
213 | response = WebUtil.PostToService(m_serverUrl, requestArgs); | ||
214 | bool success = response["Success"].AsBoolean(); | ||
215 | |||
216 | if (!success) | ||
217 | m_log.WarnFormat("[SIMIAN AUTH CONNECTOR]: Failed to set password for {0} ({1})", identifier, principalID); | ||
218 | |||
219 | return success; | ||
220 | } | ||
221 | } | ||
222 | else | ||
223 | { | ||
224 | m_log.Warn("[SIMIAN AUTH CONNECTOR]: Failed to retrieve identities for " + principalID + ": " + | ||
225 | response["Message"].AsString()); | ||
226 | } | ||
227 | |||
228 | return false; | ||
229 | } | ||
230 | |||
231 | private string Authorize(UUID userID) | ||
232 | { | ||
233 | NameValueCollection requestArgs = new NameValueCollection | ||
234 | { | ||
235 | { "RequestMethod", "AddSession" }, | ||
236 | { "UserID", userID.ToString() } | ||
237 | }; | ||
238 | |||
239 | OSDMap response = WebUtil.PostToService(m_serverUrl, requestArgs); | ||
240 | if (response["Success"].AsBoolean()) | ||
241 | return response["SessionID"].AsUUID().ToString(); | ||
242 | else | ||
243 | return String.Empty; | ||
244 | } | ||
245 | } | ||
246 | } | ||