clockwerk-opensim – Blame information for rev 1

Subversion Repositories:
Rev:
Rev Author Line No. Line
1 vero 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 OpenMetaverse;
30 using log4net;
31 using Nini.Config;
32 using System.Reflection;
33 using OpenSim.Data;
34 using OpenSim.Framework;
35 using OpenSim.Services.Base;
36 using OpenSim.Services.Interfaces;
37  
38 namespace OpenSim.Services.AuthenticationService
39 {
40 // Generic Authentication service used for identifying
41 // and authenticating principals.
42 // Principals may be clients acting on users' behalf,
43 // or any other components that need
44 // verifiable identification.
45 //
46 public class AuthenticationServiceBase : ServiceBase
47 {
48 private static readonly ILog m_log =
49 LogManager.GetLogger(
50 MethodBase.GetCurrentMethod().DeclaringType);
51  
52 protected IAuthenticationData m_Database;
53  
54 public AuthenticationServiceBase(IConfigSource config) : base(config)
55 {
56 string dllName = String.Empty;
57 string connString = String.Empty;
58 string realm = "auth";
59  
60 //
61 // Try reading the [AuthenticationService] section first, if it exists
62 //
63 IConfig authConfig = config.Configs["AuthenticationService"];
64 if (authConfig != null)
65 {
66 dllName = authConfig.GetString("StorageProvider", dllName);
67 connString = authConfig.GetString("ConnectionString", connString);
68 realm = authConfig.GetString("Realm", realm);
69 }
70  
71 //
72 // Try reading the [DatabaseService] section, if it exists
73 //
74 IConfig dbConfig = config.Configs["DatabaseService"];
75 if (dbConfig != null)
76 {
77 if (dllName == String.Empty)
78 dllName = dbConfig.GetString("StorageProvider", String.Empty);
79 if (connString == String.Empty)
80 connString = dbConfig.GetString("ConnectionString", String.Empty);
81 }
82  
83 //
84 // We tried, but this doesn't exist. We can't proceed.
85 //
86 if (dllName == String.Empty || realm == String.Empty)
87 throw new Exception("No StorageProvider configured");
88  
89 m_Database = LoadPlugin<IAuthenticationData>(dllName,
90 new Object[] {connString, realm});
91 if (m_Database == null)
92 throw new Exception(string.Format("Could not find a storage interface in module {0}", dllName));
93 }
94  
95 public bool Verify(UUID principalID, string token, int lifetime)
96 {
97 return m_Database.CheckToken(principalID, token, lifetime);
98 }
99  
100 public virtual bool Release(UUID principalID, string token)
101 {
102 return m_Database.CheckToken(principalID, token, 0);
103 }
104  
105 public virtual bool SetPassword(UUID principalID, string password)
106 {
107 string passwordSalt = Util.Md5Hash(UUID.Random().ToString());
108 string md5PasswdHash = Util.Md5Hash(Util.Md5Hash(password) + ":" + passwordSalt);
109  
110 AuthenticationData auth = m_Database.Get(principalID);
111 if (auth == null)
112 {
113 auth = new AuthenticationData();
114 auth.PrincipalID = principalID;
115 auth.Data = new System.Collections.Generic.Dictionary<string, object>();
116 auth.Data["accountType"] = "UserAccount";
117 auth.Data["webLoginKey"] = UUID.Zero.ToString();
118 }
119 auth.Data["passwordHash"] = md5PasswdHash;
120 auth.Data["passwordSalt"] = passwordSalt;
121 if (!m_Database.Store(auth))
122 {
123 m_log.DebugFormat("[AUTHENTICATION DB]: Failed to store authentication data");
124 return false;
125 }
126  
127 m_log.InfoFormat("[AUTHENTICATION DB]: Set password for principalID {0}", principalID);
128 return true;
129 }
130  
131 public virtual AuthInfo GetAuthInfo(UUID principalID)
132 {
133 AuthenticationData data = m_Database.Get(principalID);
134  
135 if (data == null)
136 {
137 return null;
138 }
139 else
140 {
141 AuthInfo info
142 = new AuthInfo()
143 {
144 PrincipalID = data.PrincipalID,
145 AccountType = data.Data["accountType"] as string,
146 PasswordHash = data.Data["passwordHash"] as string,
147 PasswordSalt = data.Data["passwordSalt"] as string,
148 WebLoginKey = data.Data["webLoginKey"] as string
149 };
150  
151 return info;
152 }
153 }
154  
155 public virtual bool SetAuthInfo(AuthInfo info)
156 {
157 AuthenticationData auth = new AuthenticationData();
158 auth.PrincipalID = info.PrincipalID;
159 auth.Data = new System.Collections.Generic.Dictionary<string, object>();
160 auth.Data["accountType"] = info.AccountType;
161 auth.Data["webLoginKey"] = info.WebLoginKey;
162 auth.Data["passwordHash"] = info.PasswordHash;
163 auth.Data["passwordSalt"] = info.PasswordSalt;
164  
165 if (!m_Database.Store(auth))
166 {
167 m_log.ErrorFormat("[AUTHENTICATION DB]: Failed to store authentication info.");
168 return false;
169 }
170  
171 m_log.DebugFormat("[AUTHENTICATION DB]: Set authentication info for principalID {0}", info.PrincipalID);
172 return true;
173 }
174  
175 protected string GetToken(UUID principalID, int lifetime)
176 {
177 UUID token = UUID.Random();
178  
179 if (m_Database.SetToken(principalID, token.ToString(), lifetime))
180 return token.ToString();
181  
182 return String.Empty;
183 }
184  
185 }
186 }