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