diff options
Diffstat (limited to '')
-rw-r--r-- | OpenSim/Data/SQLiteLegacy/SQLiteAuthenticationData.cs | 266 |
1 files changed, 266 insertions, 0 deletions
diff --git a/OpenSim/Data/SQLiteLegacy/SQLiteAuthenticationData.cs b/OpenSim/Data/SQLiteLegacy/SQLiteAuthenticationData.cs new file mode 100644 index 0000000..760221d --- /dev/null +++ b/OpenSim/Data/SQLiteLegacy/SQLiteAuthenticationData.cs | |||
@@ -0,0 +1,266 @@ | |||
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; | ||
30 | using System.Collections.Generic; | ||
31 | using System.Data; | ||
32 | using System.Reflection; | ||
33 | using log4net; | ||
34 | using OpenMetaverse; | ||
35 | using OpenSim.Framework; | ||
36 | using Mono.Data.SqliteClient; | ||
37 | |||
38 | namespace OpenSim.Data.SQLiteLegacy | ||
39 | { | ||
40 | public class SQLiteAuthenticationData : SQLiteFramework, IAuthenticationData | ||
41 | { | ||
42 | private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); | ||
43 | |||
44 | private string m_Realm; | ||
45 | private List<string> m_ColumnNames; | ||
46 | private int m_LastExpire; | ||
47 | private string m_connectionString; | ||
48 | |||
49 | protected static SqliteConnection m_Connection; | ||
50 | private static bool m_initialized = false; | ||
51 | |||
52 | public SQLiteAuthenticationData(string connectionString, string realm) | ||
53 | : base(connectionString) | ||
54 | { | ||
55 | m_Realm = realm; | ||
56 | m_connectionString = connectionString; | ||
57 | |||
58 | if (!m_initialized) | ||
59 | { | ||
60 | m_Connection = new SqliteConnection(connectionString); | ||
61 | m_Connection.Open(); | ||
62 | |||
63 | using (SqliteConnection dbcon = (SqliteConnection)((ICloneable)m_Connection).Clone()) | ||
64 | { | ||
65 | dbcon.Open(); | ||
66 | Migration m = new Migration(dbcon, GetType().Assembly, "AuthStore"); | ||
67 | m.Update(); | ||
68 | dbcon.Close(); | ||
69 | } | ||
70 | |||
71 | m_initialized = true; | ||
72 | } | ||
73 | } | ||
74 | |||
75 | public AuthenticationData Get(UUID principalID) | ||
76 | { | ||
77 | AuthenticationData ret = new AuthenticationData(); | ||
78 | ret.Data = new Dictionary<string, object>(); | ||
79 | |||
80 | SqliteCommand cmd = new SqliteCommand("select * from `" + m_Realm + "` where UUID = :PrincipalID"); | ||
81 | cmd.Parameters.Add(new SqliteParameter(":PrincipalID", principalID.ToString())); | ||
82 | |||
83 | IDataReader result = ExecuteReader(cmd, m_Connection); | ||
84 | |||
85 | try | ||
86 | { | ||
87 | if (result.Read()) | ||
88 | { | ||
89 | ret.PrincipalID = principalID; | ||
90 | |||
91 | if (m_ColumnNames == null) | ||
92 | { | ||
93 | m_ColumnNames = new List<string>(); | ||
94 | |||
95 | DataTable schemaTable = result.GetSchemaTable(); | ||
96 | foreach (DataRow row in schemaTable.Rows) | ||
97 | m_ColumnNames.Add(row["ColumnName"].ToString()); | ||
98 | } | ||
99 | |||
100 | foreach (string s in m_ColumnNames) | ||
101 | { | ||
102 | if (s == "UUID") | ||
103 | continue; | ||
104 | |||
105 | ret.Data[s] = result[s].ToString(); | ||
106 | } | ||
107 | |||
108 | return ret; | ||
109 | } | ||
110 | else | ||
111 | { | ||
112 | return null; | ||
113 | } | ||
114 | } | ||
115 | catch | ||
116 | { | ||
117 | } | ||
118 | finally | ||
119 | { | ||
120 | CloseCommand(cmd); | ||
121 | } | ||
122 | |||
123 | return null; | ||
124 | } | ||
125 | |||
126 | public bool Store(AuthenticationData data) | ||
127 | { | ||
128 | if (data.Data.ContainsKey("UUID")) | ||
129 | data.Data.Remove("UUID"); | ||
130 | |||
131 | string[] fields = new List<string>(data.Data.Keys).ToArray(); | ||
132 | string[] values = new string[data.Data.Count]; | ||
133 | int i = 0; | ||
134 | foreach (object o in data.Data.Values) | ||
135 | values[i++] = o.ToString(); | ||
136 | |||
137 | SqliteCommand cmd = new SqliteCommand(); | ||
138 | |||
139 | if (Get(data.PrincipalID) != null) | ||
140 | { | ||
141 | |||
142 | |||
143 | string update = "update `" + m_Realm + "` set "; | ||
144 | bool first = true; | ||
145 | foreach (string field in fields) | ||
146 | { | ||
147 | if (!first) | ||
148 | update += ", "; | ||
149 | update += "`" + field + "` = :" + field; | ||
150 | cmd.Parameters.Add(new SqliteParameter(":" + field, data.Data[field])); | ||
151 | |||
152 | first = false; | ||
153 | } | ||
154 | |||
155 | update += " where UUID = :UUID"; | ||
156 | cmd.Parameters.Add(new SqliteParameter(":UUID", data.PrincipalID.ToString())); | ||
157 | |||
158 | cmd.CommandText = update; | ||
159 | try | ||
160 | { | ||
161 | if (ExecuteNonQuery(cmd, m_Connection) < 1) | ||
162 | { | ||
163 | CloseCommand(cmd); | ||
164 | return false; | ||
165 | } | ||
166 | } | ||
167 | catch (Exception e) | ||
168 | { | ||
169 | m_log.Error("[SQLITE]: Exception storing authentication data", e); | ||
170 | CloseCommand(cmd); | ||
171 | return false; | ||
172 | } | ||
173 | } | ||
174 | |||
175 | else | ||
176 | { | ||
177 | string insert = "insert into `" + m_Realm + "` (`UUID`, `" + | ||
178 | String.Join("`, `", fields) + | ||
179 | "`) values (:UUID, :" + String.Join(", :", fields) + ")"; | ||
180 | |||
181 | cmd.Parameters.Add(new SqliteParameter(":UUID", data.PrincipalID.ToString())); | ||
182 | foreach (string field in fields) | ||
183 | cmd.Parameters.Add(new SqliteParameter(":" + field, data.Data[field])); | ||
184 | |||
185 | cmd.CommandText = insert; | ||
186 | |||
187 | try | ||
188 | { | ||
189 | if (ExecuteNonQuery(cmd, m_Connection) < 1) | ||
190 | { | ||
191 | CloseCommand(cmd); | ||
192 | return false; | ||
193 | } | ||
194 | } | ||
195 | catch (Exception e) | ||
196 | { | ||
197 | Console.WriteLine(e.ToString()); | ||
198 | CloseCommand(cmd); | ||
199 | return false; | ||
200 | } | ||
201 | } | ||
202 | |||
203 | CloseCommand(cmd); | ||
204 | |||
205 | return true; | ||
206 | } | ||
207 | |||
208 | public bool SetDataItem(UUID principalID, string item, string value) | ||
209 | { | ||
210 | SqliteCommand cmd = new SqliteCommand("update `" + m_Realm + | ||
211 | "` set `" + item + "` = " + value + " where UUID = '" + principalID.ToString() + "'"); | ||
212 | |||
213 | if (ExecuteNonQuery(cmd, m_Connection) > 0) | ||
214 | return true; | ||
215 | |||
216 | return false; | ||
217 | } | ||
218 | |||
219 | public bool SetToken(UUID principalID, string token, int lifetime) | ||
220 | { | ||
221 | if (System.Environment.TickCount - m_LastExpire > 30000) | ||
222 | DoExpire(); | ||
223 | |||
224 | SqliteCommand cmd = new SqliteCommand("insert into tokens (UUID, token, validity) values ('" + principalID.ToString() + | ||
225 | "', '" + token + "', datetime('now', 'localtime', '+" + lifetime.ToString() + " minutes'))"); | ||
226 | |||
227 | if (ExecuteNonQuery(cmd, m_Connection) > 0) | ||
228 | { | ||
229 | cmd.Dispose(); | ||
230 | return true; | ||
231 | } | ||
232 | |||
233 | cmd.Dispose(); | ||
234 | return false; | ||
235 | } | ||
236 | |||
237 | public bool CheckToken(UUID principalID, string token, int lifetime) | ||
238 | { | ||
239 | if (System.Environment.TickCount - m_LastExpire > 30000) | ||
240 | DoExpire(); | ||
241 | |||
242 | SqliteCommand cmd = new SqliteCommand("update tokens set validity = datetime('now', 'localtime', '+" + lifetime.ToString() + | ||
243 | " minutes') where UUID = '" + principalID.ToString() + "' and token = '" + token + "' and validity > datetime('now', 'localtime')"); | ||
244 | |||
245 | if (ExecuteNonQuery(cmd, m_Connection) > 0) | ||
246 | { | ||
247 | cmd.Dispose(); | ||
248 | return true; | ||
249 | } | ||
250 | |||
251 | cmd.Dispose(); | ||
252 | |||
253 | return false; | ||
254 | } | ||
255 | |||
256 | private void DoExpire() | ||
257 | { | ||
258 | SqliteCommand cmd = new SqliteCommand("delete from tokens where validity < datetime('now', 'localtime')"); | ||
259 | ExecuteNonQuery(cmd, m_Connection); | ||
260 | |||
261 | cmd.Dispose(); | ||
262 | |||
263 | m_LastExpire = System.Environment.TickCount; | ||
264 | } | ||
265 | } | ||
266 | } | ||