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