diff options
author | Justin Clark-Casey (justincc) | 2010-04-30 17:45:00 +0100 |
---|---|---|
committer | Justin Clark-Casey (justincc) | 2010-04-30 19:28:28 +0100 |
commit | 56fe4c24b8c67ec3b6a5a897c35ab19507bd1077 (patch) | |
tree | 8feccd68058c70da1b96d8b6e804aa4afa6dc723 /OpenSim/Data/SQLite/SQLiteAuthenticationData.cs | |
parent | add Mono.Data.Sqlite.dll (diff) | |
download | opensim-SC-56fe4c24b8c67ec3b6a5a897c35ab19507bd1077.zip opensim-SC-56fe4c24b8c67ec3b6a5a897c35ab19507bd1077.tar.gz opensim-SC-56fe4c24b8c67ec3b6a5a897c35ab19507bd1077.tar.bz2 opensim-SC-56fe4c24b8c67ec3b6a5a897c35ab19507bd1077.tar.xz |
rename SQLiteNG to SQLite and SQLite to SQLiteLegacy this seems the least evil way forward since mono 2.6 and later will see increasing usage, and this only works with what was
SQLiteNG MAC USERS WILL NEED TO CHANGE REFERENCES TO "OpenSim.Data.SQLite.dll" to "OpenSim.Data.SQLiteLegacy.dll" in OpenSim.ini and config-include/StandaloneCommon.ini (if
using standalone) See the OpenSim.ini.example and StandaloneCommon.ini.example files for more details This commit also temporarily changes unsigned ParentEstateID values in the
OpenSim.Data.Tests to signed temporarily, since the new plugin enforces creation of signed fields in the database (which is what the SQL actually specifies). And change data
columns in sqlite is a pita.
Diffstat (limited to 'OpenSim/Data/SQLite/SQLiteAuthenticationData.cs')
-rw-r--r-- | OpenSim/Data/SQLite/SQLiteAuthenticationData.cs | 257 |
1 files changed, 257 insertions, 0 deletions
diff --git a/OpenSim/Data/SQLite/SQLiteAuthenticationData.cs b/OpenSim/Data/SQLite/SQLiteAuthenticationData.cs new file mode 100644 index 0000000..086ac0a --- /dev/null +++ b/OpenSim/Data/SQLite/SQLiteAuthenticationData.cs | |||
@@ -0,0 +1,257 @@ | |||
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 OpenMetaverse; | ||
33 | using OpenSim.Framework; | ||
34 | using Mono.Data.Sqlite; | ||
35 | |||
36 | namespace OpenSim.Data.SQLite | ||
37 | { | ||
38 | public class SQLiteAuthenticationData : SQLiteFramework, IAuthenticationData | ||
39 | { | ||
40 | private string m_Realm; | ||
41 | private List<string> m_ColumnNames; | ||
42 | private int m_LastExpire; | ||
43 | private string m_connectionString; | ||
44 | |||
45 | protected static SqliteConnection m_Connection; | ||
46 | private static bool m_initialized = false; | ||
47 | |||
48 | public SQLiteAuthenticationData(string connectionString, string realm) | ||
49 | : base(connectionString) | ||
50 | { | ||
51 | m_Realm = realm; | ||
52 | m_connectionString = connectionString; | ||
53 | |||
54 | if (!m_initialized) | ||
55 | { | ||
56 | m_Connection = new SqliteConnection(connectionString); | ||
57 | m_Connection.Open(); | ||
58 | |||
59 | Migration m = new Migration(m_Connection, GetType().Assembly, "AuthStore"); | ||
60 | m.Update(); | ||
61 | |||
62 | m_initialized = true; | ||
63 | } | ||
64 | } | ||
65 | |||
66 | public AuthenticationData Get(UUID principalID) | ||
67 | { | ||
68 | AuthenticationData ret = new AuthenticationData(); | ||
69 | ret.Data = new Dictionary<string, object>(); | ||
70 | |||
71 | SqliteCommand cmd = new SqliteCommand("select * from `" + m_Realm + "` where UUID = :PrincipalID"); | ||
72 | cmd.Parameters.Add(new SqliteParameter(":PrincipalID", principalID.ToString())); | ||
73 | |||
74 | IDataReader result = ExecuteReader(cmd, m_Connection); | ||
75 | |||
76 | try | ||
77 | { | ||
78 | if (result.Read()) | ||
79 | { | ||
80 | ret.PrincipalID = principalID; | ||
81 | |||
82 | if (m_ColumnNames == null) | ||
83 | { | ||
84 | m_ColumnNames = new List<string>(); | ||
85 | |||
86 | DataTable schemaTable = result.GetSchemaTable(); | ||
87 | foreach (DataRow row in schemaTable.Rows) | ||
88 | m_ColumnNames.Add(row["ColumnName"].ToString()); | ||
89 | } | ||
90 | |||
91 | foreach (string s in m_ColumnNames) | ||
92 | { | ||
93 | if (s == "UUID") | ||
94 | continue; | ||
95 | |||
96 | ret.Data[s] = result[s].ToString(); | ||
97 | } | ||
98 | |||
99 | return ret; | ||
100 | } | ||
101 | else | ||
102 | { | ||
103 | return null; | ||
104 | } | ||
105 | } | ||
106 | catch | ||
107 | { | ||
108 | } | ||
109 | finally | ||
110 | { | ||
111 | //CloseCommand(cmd); | ||
112 | } | ||
113 | |||
114 | return null; | ||
115 | } | ||
116 | |||
117 | public bool Store(AuthenticationData data) | ||
118 | { | ||
119 | if (data.Data.ContainsKey("UUID")) | ||
120 | data.Data.Remove("UUID"); | ||
121 | |||
122 | string[] fields = new List<string>(data.Data.Keys).ToArray(); | ||
123 | string[] values = new string[data.Data.Count]; | ||
124 | int i = 0; | ||
125 | foreach (object o in data.Data.Values) | ||
126 | values[i++] = o.ToString(); | ||
127 | |||
128 | SqliteCommand cmd = new SqliteCommand(); | ||
129 | |||
130 | if (Get(data.PrincipalID) != null) | ||
131 | { | ||
132 | |||
133 | |||
134 | string update = "update `" + m_Realm + "` set "; | ||
135 | bool first = true; | ||
136 | foreach (string field in fields) | ||
137 | { | ||
138 | if (!first) | ||
139 | update += ", "; | ||
140 | update += "`" + field + "` = :" + field; | ||
141 | cmd.Parameters.Add(new SqliteParameter(":" + field, data.Data[field])); | ||
142 | |||
143 | first = false; | ||
144 | } | ||
145 | |||
146 | update += " where UUID = :UUID"; | ||
147 | cmd.Parameters.Add(new SqliteParameter(":UUID", data.PrincipalID.ToString())); | ||
148 | |||
149 | cmd.CommandText = update; | ||
150 | try | ||
151 | { | ||
152 | if (ExecuteNonQuery(cmd, m_Connection) < 1) | ||
153 | { | ||
154 | //CloseCommand(cmd); | ||
155 | return false; | ||
156 | } | ||
157 | } | ||
158 | catch (Exception e) | ||
159 | { | ||
160 | Console.WriteLine(e.ToString()); | ||
161 | //CloseCommand(cmd); | ||
162 | return false; | ||
163 | } | ||
164 | } | ||
165 | |||
166 | else | ||
167 | { | ||
168 | string insert = "insert into `" + m_Realm + "` (`UUID`, `" + | ||
169 | String.Join("`, `", fields) + | ||
170 | "`) values (:UUID, :" + String.Join(", :", fields) + ")"; | ||
171 | |||
172 | cmd.Parameters.Add(new SqliteParameter(":UUID", data.PrincipalID.ToString())); | ||
173 | foreach (string field in fields) | ||
174 | cmd.Parameters.Add(new SqliteParameter(":" + field, data.Data[field])); | ||
175 | |||
176 | cmd.CommandText = insert; | ||
177 | |||
178 | try | ||
179 | { | ||
180 | if (ExecuteNonQuery(cmd, m_Connection) < 1) | ||
181 | { | ||
182 | //CloseCommand(cmd); | ||
183 | return false; | ||
184 | } | ||
185 | } | ||
186 | catch (Exception e) | ||
187 | { | ||
188 | Console.WriteLine(e.ToString()); | ||
189 | //CloseCommand(cmd); | ||
190 | return false; | ||
191 | } | ||
192 | } | ||
193 | |||
194 | //CloseCommand(cmd); | ||
195 | |||
196 | return true; | ||
197 | } | ||
198 | |||
199 | public bool SetDataItem(UUID principalID, string item, string value) | ||
200 | { | ||
201 | SqliteCommand cmd = new SqliteCommand("update `" + m_Realm + | ||
202 | "` set `" + item + "` = " + value + " where UUID = '" + principalID.ToString() + "'"); | ||
203 | |||
204 | if (ExecuteNonQuery(cmd, m_Connection) > 0) | ||
205 | return true; | ||
206 | |||
207 | return false; | ||
208 | } | ||
209 | |||
210 | public bool SetToken(UUID principalID, string token, int lifetime) | ||
211 | { | ||
212 | if (System.Environment.TickCount - m_LastExpire > 30000) | ||
213 | DoExpire(); | ||
214 | |||
215 | SqliteCommand cmd = new SqliteCommand("insert into tokens (UUID, token, validity) values ('" + principalID.ToString() + | ||
216 | "', '" + token + "', datetime('now', 'localtime', '+" + lifetime.ToString() + " minutes'))"); | ||
217 | |||
218 | if (ExecuteNonQuery(cmd, m_Connection) > 0) | ||
219 | { | ||
220 | cmd.Dispose(); | ||
221 | return true; | ||
222 | } | ||
223 | |||
224 | cmd.Dispose(); | ||
225 | return false; | ||
226 | } | ||
227 | |||
228 | public bool CheckToken(UUID principalID, string token, int lifetime) | ||
229 | { | ||
230 | if (System.Environment.TickCount - m_LastExpire > 30000) | ||
231 | DoExpire(); | ||
232 | |||
233 | SqliteCommand cmd = new SqliteCommand("update tokens set validity = datetime('now', 'localtime', '+" + lifetime.ToString() + | ||
234 | " minutes') where UUID = '" + principalID.ToString() + "' and token = '" + token + "' and validity > datetime('now', 'localtime')"); | ||
235 | |||
236 | if (ExecuteNonQuery(cmd, m_Connection) > 0) | ||
237 | { | ||
238 | cmd.Dispose(); | ||
239 | return true; | ||
240 | } | ||
241 | |||
242 | cmd.Dispose(); | ||
243 | |||
244 | return false; | ||
245 | } | ||
246 | |||
247 | private void DoExpire() | ||
248 | { | ||
249 | SqliteCommand cmd = new SqliteCommand("delete from tokens where validity < datetime('now', 'localtime')"); | ||
250 | ExecuteNonQuery(cmd, m_Connection); | ||
251 | |||
252 | cmd.Dispose(); | ||
253 | |||
254 | m_LastExpire = System.Environment.TickCount; | ||
255 | } | ||
256 | } | ||
257 | } | ||