OpenSim
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros
PasswordAuthenticationService.cs
Go to the documentation of this file.
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.Generic;
30 using OpenMetaverse;
31 using OpenSim.Services.Interfaces;
32 using log4net;
33 using Nini.Config;
34 using System.Reflection;
35 using OpenSim.Data;
36 using OpenSim.Framework;
37 using OpenSim.Framework.Console;
38 
39 namespace OpenSim.Services.AuthenticationService
40 {
41  // Generic Authentication service used for identifying
42  // and authenticating principals.
43  // Principals may be clients acting on users' behalf,
44  // or any other components that need
45  // verifiable identification.
46  //
49  {
50  private static readonly ILog m_log =
51  LogManager.GetLogger(
52  MethodBase.GetCurrentMethod().DeclaringType);
53 
54  public PasswordAuthenticationService(IConfigSource config, IUserAccountService userService) :
55  base(config, userService)
56  {
57  m_log.Debug("[AUTH SERVICE]: Started with User Account access");
58  }
59 
60  public PasswordAuthenticationService(IConfigSource config) :
61  base(config)
62  {
63  }
64 
65  public string Authenticate(UUID principalID, string password, int lifetime)
66  {
67  UUID realID;
68  return Authenticate(principalID, password, lifetime, out realID);
69  }
70 
71  public string Authenticate(UUID principalID, string password, int lifetime, out UUID realID)
72  {
73  realID = UUID.Zero;
74 
75  m_log.DebugFormat("[AUTH SERVICE]: Authenticating for {0}, user account service present: {1}", principalID, m_UserAccountService != null);
76  AuthenticationData data = m_Database.Get(principalID);
77  UserAccount user = null;
78  if (m_UserAccountService != null)
79  user = m_UserAccountService.GetUserAccount(UUID.Zero, principalID);
80 
81  if (data == null || data.Data == null)
82  {
83  m_log.DebugFormat("[AUTH SERVICE]: PrincipalID {0} or its data not found", principalID);
84  return String.Empty;
85  }
86 
87  if (!data.Data.ContainsKey("passwordHash") ||
88  !data.Data.ContainsKey("passwordSalt"))
89  {
90  return String.Empty;
91  }
92 
93  string hashed = Util.Md5Hash(password + ":" +
94  data.Data["passwordSalt"].ToString());
95 
96 // m_log.DebugFormat("[PASS AUTH]: got {0}; hashed = {1}; stored = {2}", password, hashed, data.Data["passwordHash"].ToString());
97 
98  if (data.Data["passwordHash"].ToString() == hashed)
99  {
100  return GetToken(principalID, lifetime);
101  }
102 
103  if (user == null)
104  {
105  m_log.DebugFormat("[PASS AUTH]: No user record for {0}", principalID);
106  return String.Empty;
107  }
108 
109  int impersonateFlag = 1 << 6;
110 
111  if ((user.UserFlags & impersonateFlag) == 0)
112  return String.Empty;
113 
114  m_log.DebugFormat("[PASS AUTH]: Attempting impersonation");
115 
116  List<UserAccount> accounts = m_UserAccountService.GetUserAccountsWhere(UUID.Zero, "UserLevel >= 200");
117  if (accounts == null || accounts.Count == 0)
118  return String.Empty;
119 
120  foreach (UserAccount a in accounts)
121  {
122  data = m_Database.Get(a.PrincipalID);
123  if (data == null || data.Data == null ||
124  !data.Data.ContainsKey("passwordHash") ||
125  !data.Data.ContainsKey("passwordSalt"))
126  {
127  continue;
128  }
129 
130 // m_log.DebugFormat("[PASS AUTH]: Trying {0}", data.PrincipalID);
131 
132  hashed = Util.Md5Hash(password + ":" +
133  data.Data["passwordSalt"].ToString());
134 
135  if (data.Data["passwordHash"].ToString() == hashed)
136  {
137  m_log.DebugFormat("[PASS AUTH]: {0} {1} impersonating {2}, proceeding with login", a.FirstName, a.LastName, principalID);
138  realID = a.PrincipalID;
139  return GetToken(principalID, lifetime);
140  }
141 // else
142 // {
143 // m_log.DebugFormat(
144 // "[AUTH SERVICE]: Salted hash {0} of given password did not match salted hash of {1} for PrincipalID {2}. Authentication failure.",
145 // hashed, data.Data["passwordHash"], data.PrincipalID);
146 // }
147  }
148 
149  m_log.DebugFormat("[PASS AUTH]: Impersonation of {0} failed", principalID);
150  return String.Empty;
151  }
152  }
153 }
string Authenticate(UUID principalID, string password, int lifetime)
string Authenticate(UUID principalID, string password, int lifetime, out UUID realID)
PasswordAuthenticationService(IConfigSource config, IUserAccountService userService)