diff options
Diffstat (limited to 'OpenSim/Framework/Data.MySQL/MySQLGridData.cs')
-rw-r--r-- | OpenSim/Framework/Data.MySQL/MySQLGridData.cs | 287 |
1 files changed, 287 insertions, 0 deletions
diff --git a/OpenSim/Framework/Data.MySQL/MySQLGridData.cs b/OpenSim/Framework/Data.MySQL/MySQLGridData.cs new file mode 100644 index 0000000..ef643d2 --- /dev/null +++ b/OpenSim/Framework/Data.MySQL/MySQLGridData.cs | |||
@@ -0,0 +1,287 @@ | |||
1 | /* | ||
2 | * Copyright (c) Contributors, http://www.openmetaverse.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 OpenSim 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.Generic; | ||
30 | using System.Data; | ||
31 | using System.Security.Cryptography; | ||
32 | using System.Text; | ||
33 | using libsecondlife; | ||
34 | |||
35 | namespace OpenSim.Framework.Data.MySQL | ||
36 | { | ||
37 | /// <summary> | ||
38 | /// A MySQL Interface for the Grid Server | ||
39 | /// </summary> | ||
40 | public class MySQLGridData : IGridData | ||
41 | { | ||
42 | /// <summary> | ||
43 | /// MySQL Database Manager | ||
44 | /// </summary> | ||
45 | private MySQLManager database; | ||
46 | |||
47 | /// <summary> | ||
48 | /// Initialises the Grid Interface | ||
49 | /// </summary> | ||
50 | public void Initialise() | ||
51 | { | ||
52 | IniFile GridDataMySqlFile = new IniFile("mysql_connection.ini"); | ||
53 | string settingHostname = GridDataMySqlFile.ParseFileReadValue("hostname"); | ||
54 | string settingDatabase = GridDataMySqlFile.ParseFileReadValue("database"); | ||
55 | string settingUsername = GridDataMySqlFile.ParseFileReadValue("username"); | ||
56 | string settingPassword = GridDataMySqlFile.ParseFileReadValue("password"); | ||
57 | string settingPooling = GridDataMySqlFile.ParseFileReadValue("pooling"); | ||
58 | string settingPort = GridDataMySqlFile.ParseFileReadValue("port"); | ||
59 | |||
60 | database = new MySQLManager(settingHostname, settingDatabase, settingUsername, settingPassword, settingPooling, settingPort); | ||
61 | } | ||
62 | |||
63 | /// <summary> | ||
64 | /// Shuts down the grid interface | ||
65 | /// </summary> | ||
66 | public void Close() | ||
67 | { | ||
68 | database.Close(); | ||
69 | } | ||
70 | |||
71 | /// <summary> | ||
72 | /// Returns the plugin name | ||
73 | /// </summary> | ||
74 | /// <returns>Plugin name</returns> | ||
75 | public string getName() | ||
76 | { | ||
77 | return "MySql OpenGridData"; | ||
78 | } | ||
79 | |||
80 | /// <summary> | ||
81 | /// Returns the plugin version | ||
82 | /// </summary> | ||
83 | /// <returns>Plugin version</returns> | ||
84 | public string getVersion() | ||
85 | { | ||
86 | return "0.1"; | ||
87 | } | ||
88 | |||
89 | /// <summary> | ||
90 | /// Returns all the specified region profiles within coordates -- coordinates are inclusive | ||
91 | /// </summary> | ||
92 | /// <param name="xmin">Minimum X coordinate</param> | ||
93 | /// <param name="ymin">Minimum Y coordinate</param> | ||
94 | /// <param name="xmax">Maximum X coordinate</param> | ||
95 | /// <param name="ymax">Maximum Y coordinate</param> | ||
96 | /// <returns></returns> | ||
97 | public SimProfileData[] GetProfilesInRange(uint xmin, uint ymin, uint xmax, uint ymax) | ||
98 | { | ||
99 | try | ||
100 | { | ||
101 | lock (database) | ||
102 | { | ||
103 | Dictionary<string, string> param = new Dictionary<string, string>(); | ||
104 | param["?xmin"] = xmin.ToString(); | ||
105 | param["?ymin"] = ymin.ToString(); | ||
106 | param["?xmax"] = xmax.ToString(); | ||
107 | param["?ymax"] = ymax.ToString(); | ||
108 | |||
109 | IDbCommand result = database.Query("SELECT * FROM regions WHERE locX >= ?xmin AND locX <= ?xmax AND locY >= ?ymin AND locY <= ?ymax", param); | ||
110 | IDataReader reader = result.ExecuteReader(); | ||
111 | |||
112 | SimProfileData row; | ||
113 | |||
114 | List<SimProfileData> rows = new List<SimProfileData>(); | ||
115 | |||
116 | while ((row = database.readSimRow(reader)) != null) | ||
117 | { | ||
118 | rows.Add(row); | ||
119 | } | ||
120 | reader.Close(); | ||
121 | result.Dispose(); | ||
122 | |||
123 | return rows.ToArray(); | ||
124 | |||
125 | } | ||
126 | } | ||
127 | catch (Exception e) | ||
128 | { | ||
129 | database.Reconnect(); | ||
130 | Console.WriteLine(e.ToString()); | ||
131 | return null; | ||
132 | } | ||
133 | } | ||
134 | |||
135 | /// <summary> | ||
136 | /// Returns a sim profile from it's location | ||
137 | /// </summary> | ||
138 | /// <param name="handle">Region location handle</param> | ||
139 | /// <returns>Sim profile</returns> | ||
140 | public SimProfileData GetProfileByHandle(ulong handle) | ||
141 | { | ||
142 | try | ||
143 | { | ||
144 | lock (database) | ||
145 | { | ||
146 | Dictionary<string, string> param = new Dictionary<string, string>(); | ||
147 | param["?handle"] = handle.ToString(); | ||
148 | |||
149 | IDbCommand result = database.Query("SELECT * FROM regions WHERE regionHandle = ?handle", param); | ||
150 | IDataReader reader = result.ExecuteReader(); | ||
151 | |||
152 | SimProfileData row = database.readSimRow(reader); | ||
153 | reader.Close(); | ||
154 | result.Dispose(); | ||
155 | |||
156 | return row; | ||
157 | } | ||
158 | } | ||
159 | catch (Exception e) | ||
160 | { | ||
161 | database.Reconnect(); | ||
162 | Console.WriteLine(e.ToString()); | ||
163 | return null; | ||
164 | } | ||
165 | } | ||
166 | |||
167 | /// <summary> | ||
168 | /// Returns a sim profile from it's UUID | ||
169 | /// </summary> | ||
170 | /// <param name="uuid">The region UUID</param> | ||
171 | /// <returns>The sim profile</returns> | ||
172 | public SimProfileData GetProfileByLLUUID(LLUUID uuid) | ||
173 | { | ||
174 | try | ||
175 | { | ||
176 | lock (database) | ||
177 | { | ||
178 | Dictionary<string, string> param = new Dictionary<string, string>(); | ||
179 | param["?uuid"] = uuid.ToStringHyphenated(); | ||
180 | |||
181 | IDbCommand result = database.Query("SELECT * FROM regions WHERE uuid = ?uuid", param); | ||
182 | IDataReader reader = result.ExecuteReader(); | ||
183 | |||
184 | SimProfileData row = database.readSimRow(reader); | ||
185 | reader.Close(); | ||
186 | result.Dispose(); | ||
187 | |||
188 | return row; | ||
189 | } | ||
190 | } | ||
191 | catch (Exception e) | ||
192 | { | ||
193 | database.Reconnect(); | ||
194 | Console.WriteLine(e.ToString()); | ||
195 | return null; | ||
196 | } | ||
197 | } | ||
198 | |||
199 | /// <summary> | ||
200 | /// Adds a new profile to the database | ||
201 | /// </summary> | ||
202 | /// <param name="profile">The profile to add</param> | ||
203 | /// <returns>Successful?</returns> | ||
204 | public DataResponse AddProfile(SimProfileData profile) | ||
205 | { | ||
206 | lock (database) | ||
207 | { | ||
208 | if (database.insertRegion(profile)) | ||
209 | { | ||
210 | return DataResponse.RESPONSE_OK; | ||
211 | } | ||
212 | else | ||
213 | { | ||
214 | return DataResponse.RESPONSE_ERROR; | ||
215 | } | ||
216 | } | ||
217 | } | ||
218 | |||
219 | /// <summary> | ||
220 | /// DEPRECIATED. Attempts to authenticate a region by comparing a shared secret. | ||
221 | /// </summary> | ||
222 | /// <param name="uuid">The UUID of the challenger</param> | ||
223 | /// <param name="handle">The attempted regionHandle of the challenger</param> | ||
224 | /// <param name="authkey">The secret</param> | ||
225 | /// <returns>Whether the secret and regionhandle match the database entry for UUID</returns> | ||
226 | public bool AuthenticateSim(LLUUID uuid, ulong handle, string authkey) | ||
227 | { | ||
228 | bool throwHissyFit = false; // Should be true by 1.0 | ||
229 | |||
230 | if (throwHissyFit) | ||
231 | throw new Exception("CRYPTOWEAK AUTHENTICATE: Refusing to authenticate due to replay potential."); | ||
232 | |||
233 | SimProfileData data = GetProfileByLLUUID(uuid); | ||
234 | |||
235 | return (handle == data.regionHandle && authkey == data.regionSecret); | ||
236 | } | ||
237 | |||
238 | /// <summary> | ||
239 | /// NOT YET FUNCTIONAL. Provides a cryptographic authentication of a region | ||
240 | /// </summary> | ||
241 | /// <remarks>This requires a security audit.</remarks> | ||
242 | /// <param name="uuid"></param> | ||
243 | /// <param name="handle"></param> | ||
244 | /// <param name="authhash"></param> | ||
245 | /// <param name="challenge"></param> | ||
246 | /// <returns></returns> | ||
247 | public bool AuthenticateSim(LLUUID uuid, ulong handle, string authhash, string challenge) | ||
248 | { | ||
249 | SHA512Managed HashProvider = new SHA512Managed(); | ||
250 | ASCIIEncoding TextProvider = new ASCIIEncoding(); | ||
251 | |||
252 | byte[] stream = TextProvider.GetBytes(uuid.ToStringHyphenated() + ":" + handle.ToString() + ":" + challenge); | ||
253 | byte[] hash = HashProvider.ComputeHash(stream); | ||
254 | |||
255 | return false; | ||
256 | } | ||
257 | |||
258 | public ReservationData GetReservationAtPoint(uint x, uint y) | ||
259 | { | ||
260 | try | ||
261 | { | ||
262 | lock (database) | ||
263 | { | ||
264 | Dictionary<string, string> param = new Dictionary<string, string>(); | ||
265 | param["?x"] = x.ToString(); | ||
266 | param["?y"] = y.ToString(); | ||
267 | IDbCommand result = database.Query("SELECT * FROM reservations WHERE resXMin <= ?x AND resXMax >= ?x AND resYMin <= ?y AND resYMax >= ?y", param); | ||
268 | IDataReader reader = result.ExecuteReader(); | ||
269 | |||
270 | ReservationData row = database.readReservationRow(reader); | ||
271 | reader.Close(); | ||
272 | result.Dispose(); | ||
273 | |||
274 | return row; | ||
275 | } | ||
276 | } | ||
277 | catch (Exception e) | ||
278 | { | ||
279 | database.Reconnect(); | ||
280 | Console.WriteLine(e.ToString()); | ||
281 | return null; | ||
282 | } | ||
283 | } | ||
284 | } | ||
285 | |||
286 | |||
287 | } | ||