opensim – Blame information for rev 1
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
1 | eva | 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 System.Data.SqlClient; |
||
35 | using System.Reflection; |
||
36 | using System.Text; |
||
37 | |||
38 | namespace 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 | private MSSQLManager m_database; |
||
47 | |||
48 | public MSSQLAuthenticationData(string connectionString, string realm) |
||
49 | { |
||
50 | m_Realm = realm; |
||
51 | m_ConnectionString = connectionString; |
||
52 | using (SqlConnection conn = new SqlConnection(m_ConnectionString)) |
||
53 | { |
||
54 | conn.Open(); |
||
55 | Migration m = new Migration(conn, GetType().Assembly, "AuthStore"); |
||
56 | m_database = new MSSQLManager(m_ConnectionString); |
||
57 | m.Update(); |
||
58 | } |
||
59 | } |
||
60 | |||
61 | public AuthenticationData Get(UUID principalID) |
||
62 | { |
||
63 | AuthenticationData ret = new AuthenticationData(); |
||
64 | ret.Data = new Dictionary<string, object>(); |
||
65 | |||
66 | string sql = string.Format("select * from {0} where UUID = @principalID", m_Realm); |
||
67 | |||
68 | using (SqlConnection conn = new SqlConnection(m_ConnectionString)) |
||
69 | using (SqlCommand cmd = new SqlCommand(sql, conn)) |
||
70 | { |
||
71 | cmd.Parameters.Add(m_database.CreateParameter("@principalID", principalID)); |
||
72 | conn.Open(); |
||
73 | using (SqlDataReader result = cmd.ExecuteReader()) |
||
74 | { |
||
75 | if (result.Read()) |
||
76 | { |
||
77 | ret.PrincipalID = principalID; |
||
78 | |||
79 | if (m_ColumnNames == null) |
||
80 | { |
||
81 | m_ColumnNames = new List<string>(); |
||
82 | |||
83 | DataTable schemaTable = result.GetSchemaTable(); |
||
84 | foreach (DataRow row in schemaTable.Rows) |
||
85 | m_ColumnNames.Add(row["ColumnName"].ToString()); |
||
86 | } |
||
87 | |||
88 | foreach (string s in m_ColumnNames) |
||
89 | { |
||
90 | if (s == "UUID") |
||
91 | continue; |
||
92 | |||
93 | ret.Data[s] = result[s].ToString(); |
||
94 | } |
||
95 | return ret; |
||
96 | } |
||
97 | } |
||
98 | } |
||
99 | return null; |
||
100 | } |
||
101 | |||
102 | public bool Store(AuthenticationData data) |
||
103 | { |
||
104 | if (data.Data.ContainsKey("UUID")) |
||
105 | data.Data.Remove("UUID"); |
||
106 | |||
107 | string[] fields = new List<string>(data.Data.Keys).ToArray(); |
||
108 | StringBuilder updateBuilder = new StringBuilder(); |
||
109 | |||
110 | using (SqlConnection conn = new SqlConnection(m_ConnectionString)) |
||
111 | using (SqlCommand cmd = new SqlCommand()) |
||
112 | { |
||
113 | updateBuilder.AppendFormat("update {0} set ", m_Realm); |
||
114 | |||
115 | bool first = true; |
||
116 | foreach (string field in fields) |
||
117 | { |
||
118 | if (!first) |
||
119 | updateBuilder.Append(", "); |
||
120 | updateBuilder.AppendFormat("{0} = @{0}",field); |
||
121 | |||
122 | first = false; |
||
123 | cmd.Parameters.Add(m_database.CreateParameter("@" + field, data.Data[field])); |
||
124 | } |
||
125 | |||
126 | updateBuilder.Append(" where UUID = @principalID"); |
||
127 | |||
128 | cmd.CommandText = updateBuilder.ToString(); |
||
129 | cmd.Connection = conn; |
||
130 | cmd.Parameters.Add(m_database.CreateParameter("@principalID", data.PrincipalID)); |
||
131 | |||
132 | conn.Open(); |
||
133 | if (cmd.ExecuteNonQuery() < 1) |
||
134 | { |
||
135 | StringBuilder insertBuilder = new StringBuilder(); |
||
136 | |||
137 | insertBuilder.AppendFormat("insert into {0} (UUID, ", m_Realm); |
||
138 | insertBuilder.Append(String.Join(", ", fields)); |
||
139 | insertBuilder.Append(") values (@principalID, @"); |
||
140 | insertBuilder.Append(String.Join(", @", fields)); |
||
141 | insertBuilder.Append(")"); |
||
142 | |||
143 | cmd.CommandText = insertBuilder.ToString(); |
||
144 | |||
145 | if (cmd.ExecuteNonQuery() < 1) |
||
146 | { |
||
147 | return false; |
||
148 | } |
||
149 | } |
||
150 | } |
||
151 | return true; |
||
152 | } |
||
153 | |||
154 | public bool SetDataItem(UUID principalID, string item, string value) |
||
155 | { |
||
156 | string sql = string.Format("update {0} set {1} = @{1} where UUID = @UUID", m_Realm, item); |
||
157 | using (SqlConnection conn = new SqlConnection(m_ConnectionString)) |
||
158 | using (SqlCommand cmd = new SqlCommand(sql, conn)) |
||
159 | { |
||
160 | cmd.Parameters.Add(m_database.CreateParameter("@" + item, value)); |
||
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 | |||
173 | string sql = "insert into tokens (UUID, token, validity) values (@principalID, @token, @lifetime)"; |
||
174 | using (SqlConnection conn = new SqlConnection(m_ConnectionString)) |
||
175 | using (SqlCommand cmd = new SqlCommand(sql, conn)) |
||
176 | { |
||
177 | cmd.Parameters.Add(m_database.CreateParameter("@principalID", principalID)); |
||
178 | cmd.Parameters.Add(m_database.CreateParameter("@token", token)); |
||
179 | cmd.Parameters.Add(m_database.CreateParameter("@lifetime", DateTime.Now.AddMinutes(lifetime))); |
||
180 | conn.Open(); |
||
181 | |||
182 | if (cmd.ExecuteNonQuery() > 0) |
||
183 | { |
||
184 | return true; |
||
185 | } |
||
186 | } |
||
187 | return false; |
||
188 | } |
||
189 | |||
190 | public bool CheckToken(UUID principalID, string token, int lifetime) |
||
191 | { |
||
192 | if (System.Environment.TickCount - m_LastExpire > 30000) |
||
193 | DoExpire(); |
||
194 | |||
195 | DateTime validDate = DateTime.Now.AddMinutes(lifetime); |
||
196 | string sql = "update tokens set validity = @validDate where UUID = @principalID and token = @token and validity > GetDate()"; |
||
197 | using (SqlConnection conn = new SqlConnection(m_ConnectionString)) |
||
198 | using (SqlCommand cmd = new SqlCommand(sql, conn)) |
||
199 | { |
||
200 | cmd.Parameters.Add(m_database.CreateParameter("@principalID", principalID)); |
||
201 | cmd.Parameters.Add(m_database.CreateParameter("@token", token)); |
||
202 | cmd.Parameters.Add(m_database.CreateParameter("@validDate", validDate)); |
||
203 | conn.Open(); |
||
204 | |||
205 | if (cmd.ExecuteNonQuery() > 0) |
||
206 | { |
||
207 | return true; |
||
208 | } |
||
209 | } |
||
210 | return false; |
||
211 | } |
||
212 | |||
213 | private void DoExpire() |
||
214 | { |
||
215 | DateTime currentDateTime = DateTime.Now; |
||
216 | string sql = "delete from tokens where validity < @currentDateTime"; |
||
217 | using (SqlConnection conn = new SqlConnection(m_ConnectionString)) |
||
218 | using (SqlCommand cmd = new SqlCommand(sql, conn)) |
||
219 | { |
||
220 | conn.Open(); |
||
221 | cmd.Parameters.Add(m_database.CreateParameter("@currentDateTime", currentDateTime)); |
||
222 | cmd.ExecuteNonQuery(); |
||
223 | } |
||
224 | m_LastExpire = System.Environment.TickCount; |
||
225 | } |
||
226 | } |
||
227 | } |