aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/OpenSim/Data/MSSQL/MSSQLAuthenticationData.cs
diff options
context:
space:
mode:
Diffstat (limited to 'OpenSim/Data/MSSQL/MSSQLAuthenticationData.cs')
-rw-r--r--OpenSim/Data/MSSQL/MSSQLAuthenticationData.cs222
1 files changed, 222 insertions, 0 deletions
diff --git a/OpenSim/Data/MSSQL/MSSQLAuthenticationData.cs b/OpenSim/Data/MSSQL/MSSQLAuthenticationData.cs
new file mode 100644
index 0000000..78fc22c
--- /dev/null
+++ b/OpenSim/Data/MSSQL/MSSQLAuthenticationData.cs
@@ -0,0 +1,222 @@
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 System.Data.SqlClient;
35using System.Reflection;
36using System.Text;
37
38namespace OpenSim.Data.MSSQL
39{
40 public class MSSQLAuthenticationData : IAuthenticationData
41 {
42 private string m_Realm;
43 private List<string> m_ColumnNames = null;
44 private int m_LastExpire = 0;
45 private string m_ConnectionString;
46
47 public MSSQLAuthenticationData(string connectionString, string realm)
48 {
49 m_Realm = realm;
50 m_ConnectionString = connectionString;
51 using (SqlConnection conn = new SqlConnection(m_ConnectionString))
52 {
53 conn.Open();
54 Migration m = new Migration(conn, GetType().Assembly, "AuthStore");
55 m.Update();
56 }
57 }
58
59 public AuthenticationData Get(UUID principalID)
60 {
61 AuthenticationData ret = new AuthenticationData();
62 ret.Data = new Dictionary<string, object>();
63
64 string sql = string.Format("select * from '{0}' where UUID = @principalID", m_Realm);
65
66 using (SqlConnection conn = new SqlConnection(m_ConnectionString))
67 using (SqlCommand cmd = new SqlCommand(sql, conn))
68 {
69 cmd.Parameters.AddWithValue("@principalID", principalID.ToString());
70 conn.Open();
71 using (SqlDataReader result = cmd.ExecuteReader())
72 {
73 if (result.Read())
74 {
75 ret.PrincipalID = principalID;
76
77 if (m_ColumnNames == null)
78 {
79 m_ColumnNames = new List<string>();
80
81 DataTable schemaTable = result.GetSchemaTable();
82 foreach (DataRow row in schemaTable.Rows)
83 m_ColumnNames.Add(row["ColumnName"].ToString());
84 }
85
86 foreach (string s in m_ColumnNames)
87 {
88 if (s == "UUID")
89 continue;
90
91 ret.Data[s] = result[s].ToString();
92 }
93 return ret;
94 }
95 }
96 }
97 return null;
98 }
99
100 public bool Store(AuthenticationData data)
101 {
102 if (data.Data.ContainsKey("UUID"))
103 data.Data.Remove("UUID");
104
105 string[] fields = new List<string>(data.Data.Keys).ToArray();
106 StringBuilder updateBuilder = new StringBuilder();
107
108 using (SqlConnection conn = new SqlConnection(m_ConnectionString))
109 using (SqlCommand cmd = new SqlCommand())
110 {
111 updateBuilder.AppendFormat("update '{0}' set ", m_Realm);
112
113 bool first = true;
114 foreach (string field in fields)
115 {
116 if (!first)
117 updateBuilder.Append(", ");
118 updateBuilder.AppendFormat("'{0}' = @{0}",field);
119
120 first = false;
121
122 cmd.Parameters.AddWithValue("@" + field, data.Data[field]);
123 }
124
125 updateBuilder.Append(" where UUID = @principalID");
126
127 cmd.CommandText = updateBuilder.ToString();
128 cmd.Connection = conn;
129
130 cmd.Parameters.AddWithValue("@principalID", data.PrincipalID.ToString());
131 conn.Open();
132 if (cmd.ExecuteNonQuery() < 1)
133 {
134 StringBuilder insertBuilder = new StringBuilder();
135
136 insertBuilder.AppendFormat("insert into '{0}' ('UUID', '", m_Realm);
137 insertBuilder.Append(String.Join("', '", fields));
138 insertBuilder.Append("') values ( @principalID, @");
139 insertBuilder.Append(String.Join(", @", fields));
140 insertBuilder.Append(")");
141
142 cmd.CommandText = insertBuilder.ToString();
143
144 if (cmd.ExecuteNonQuery() < 1)
145 {
146 return false;
147 }
148 }
149 }
150 return true;
151 }
152
153 public bool SetDataItem(UUID principalID, string item, string value)
154 {
155 string sql = string.Format("update '{0}' set '{1}' = @{1} where UUID = @UUID", m_Realm, item);
156 using (SqlConnection conn = new SqlConnection(m_ConnectionString))
157 using (SqlCommand cmd = new SqlCommand(sql, conn))
158 {
159 cmd.Parameters.AddWithValue("@" + item, value);
160 cmd.Parameters.AddWithValue("@UUID", principalID.ToString());
161 conn.Open();
162 if (cmd.ExecuteNonQuery() > 0)
163 return true;
164 }
165 return false;
166 }
167
168 public bool SetToken(UUID principalID, string token, int lifetime)
169 {
170 if (System.Environment.TickCount - m_LastExpire > 30000)
171 DoExpire();
172 string sql = "insert into tokens (UUID, token, validity) values (@principalID, @token, date_add(now(), interval @lifetime minute))";
173 using (SqlConnection conn = new SqlConnection(m_ConnectionString))
174 using (SqlCommand cmd = new SqlCommand(sql, conn))
175 {
176 cmd.Parameters.AddWithValue("@principalID", principalID.ToString());
177 cmd.Parameters.AddWithValue("@token", token);
178 cmd.Parameters.AddWithValue("@lifetime", lifetime.ToString());
179 conn.Open();
180
181 if (cmd.ExecuteNonQuery() > 0)
182 {
183 return true;
184 }
185 }
186 return false;
187 }
188
189 public bool CheckToken(UUID principalID, string token, int lifetime)
190 {
191 if (System.Environment.TickCount - m_LastExpire > 30000)
192 DoExpire();
193 string sql = "update tokens set validity = date_add(now(), interval @lifetime minute) where UUID = @principalID and token = @token and validity > now()";
194 using (SqlConnection conn = new SqlConnection(m_ConnectionString))
195 using (SqlCommand cmd = new SqlCommand(sql, conn))
196 {
197 cmd.Parameters.AddWithValue("@principalID", principalID.ToString());
198 cmd.Parameters.AddWithValue("@token", token);
199 cmd.Parameters.AddWithValue("@lifetime", lifetime.ToString());
200 conn.Open();
201
202 if (cmd.ExecuteNonQuery() > 0)
203 {
204 return true;
205 }
206 }
207 return false;
208 }
209
210 private void DoExpire()
211 {
212 string sql = "delete from tokens where validity < now()";
213 using (SqlConnection conn = new SqlConnection(m_ConnectionString))
214 using (SqlCommand cmd = new SqlCommand(sql, conn))
215 {
216 conn.Open();
217 cmd.ExecuteNonQuery();
218 }
219 m_LastExpire = System.Environment.TickCount;
220 }
221 }
222}