1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
|
/*
* Copyright (C) 2005-2013 Team XBMC
* http://xbmc.org
*
* This Program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* This Program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with XBMC; see the file COPYING. If not, see
* <http://www.gnu.org/licenses/>.
*
*/
#include "PasswordManager.h"
#include "profiles/ProfilesManager.h"
#include "profiles/dialogs/GUIDialogLockSettings.h"
#include "URL.h"
#include "utils/XMLUtils.h"
#include "threads/SingleLock.h"
#include "utils/log.h"
#include "filesystem/File.h"
using namespace std;
CPasswordManager &CPasswordManager::GetInstance()
{
static CPasswordManager sPasswordManager;
return sPasswordManager;
}
CPasswordManager::CPasswordManager()
{
m_loaded = false;
}
bool CPasswordManager::AuthenticateURL(CURL &url)
{
CSingleLock lock(m_critSection);
if (!m_loaded)
Load();
CStdString lookup(GetLookupPath(url));
map<CStdString, CStdString>::const_iterator it = m_temporaryCache.find(lookup);
if (it == m_temporaryCache.end())
{ // second step, try something that doesn't quite match
it = m_temporaryCache.find(GetServerLookup(lookup));
}
if (it != m_temporaryCache.end())
{
CURL auth(it->second);
url.SetPassword(auth.GetPassWord());
url.SetUserName(auth.GetUserName());
return true;
}
return false;
}
bool CPasswordManager::PromptToAuthenticateURL(CURL &url)
{
CSingleLock lock(m_critSection);
std::string passcode;
std::string username = url.GetUserName();
bool saveDetails = false;
if (!CGUIDialogLockSettings::ShowAndGetUserAndPassword(username, passcode, url.GetWithoutUserDetails(), &saveDetails))
return false;
url.SetPassword(passcode);
url.SetUserName(username);
// save the information for later
SaveAuthenticatedURL(url, saveDetails);
return true;
}
void CPasswordManager::SaveAuthenticatedURL(const CURL &url, bool saveToProfile)
{
// don't store/save authenticated url if it doesn't contain username
if (url.GetUserName().empty())
return;
CSingleLock lock(m_critSection);
CStdString path = GetLookupPath(url);
CStdString authenticatedPath = url.Get();
if (!m_loaded)
Load();
if (saveToProfile)
{ // write to some random XML file...
m_permanentCache[path] = authenticatedPath;
Save();
}
// save for both this path and more generally the server as a whole.
m_temporaryCache[path] = authenticatedPath;
m_temporaryCache[GetServerLookup(path)] = authenticatedPath;
}
void CPasswordManager::Clear()
{
m_temporaryCache.clear();
m_permanentCache.clear();
m_loaded = false;
}
void CPasswordManager::Load()
{
Clear();
CStdString passwordsFile = CProfilesManager::Get().GetUserDataItem("passwords.xml");
if (XFILE::CFile::Exists(passwordsFile))
{
CXBMCTinyXML doc;
if (!doc.LoadFile(passwordsFile))
{
CLog::Log(LOGERROR, "%s - Unable to load: %s, Line %d\n%s",
__FUNCTION__, passwordsFile.c_str(), doc.ErrorRow(), doc.ErrorDesc());
return;
}
const TiXmlElement *root = doc.RootElement();
if (root->ValueStr() != "passwords")
return;
// read in our passwords
const TiXmlElement *path = root->FirstChildElement("path");
while (path)
{
CStdString from, to;
if (XMLUtils::GetPath(path, "from", from) && XMLUtils::GetPath(path, "to", to))
{
m_permanentCache[from] = to;
m_temporaryCache[from] = to;
m_temporaryCache[GetServerLookup(from)] = to;
}
path = path->NextSiblingElement("path");
}
}
m_loaded = true;
}
void CPasswordManager::Save() const
{
if (m_permanentCache.empty())
return;
CXBMCTinyXML doc;
TiXmlElement rootElement("passwords");
TiXmlNode *root = doc.InsertEndChild(rootElement);
if (!root)
return;
for (map<CStdString, CStdString>::const_iterator i = m_permanentCache.begin(); i != m_permanentCache.end(); ++i)
{
TiXmlElement pathElement("path");
TiXmlNode *path = root->InsertEndChild(pathElement);
XMLUtils::SetPath(path, "from", i->first);
XMLUtils::SetPath(path, "to", i->second);
}
doc.SaveFile(CProfilesManager::Get().GetUserDataItem("passwords.xml"));
}
CStdString CPasswordManager::GetLookupPath(const CURL &url) const
{
return "smb://" + url.GetHostName() + "/" + url.GetShareName();
}
CStdString CPasswordManager::GetServerLookup(const CStdString &path) const
{
CURL url(path);
return "smb://" + url.GetHostName() + "/";
}
|