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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "nsAbLDAPListenerBase.h"
#include "nsIWindowWatcher.h"
#include "nsIWindowMediator.h"
#include "mozIDOMWindow.h"
#include "nsIAuthPrompt.h"
#include "nsIStringBundle.h"
#include "nsILDAPMessage.h"
#include "nsILDAPErrors.h"
#include "nsILoginManager.h"
#include "nsILoginInfo.h"
#include "nsServiceManagerUtils.h"
#include "nsComponentManagerUtils.h"
#include "nsMemory.h"
#include "mozilla/Services.h"
using namespace mozilla;
nsAbLDAPListenerBase::nsAbLDAPListenerBase(nsILDAPURL* url,
nsILDAPConnection* connection,
const nsACString &login,
const int32_t timeOut) :
mDirectoryUrl(url), mConnection(connection), mLogin(login),
mTimeOut(timeOut), mBound(false), mInitialized(false),
mLock("nsAbLDAPListenerBase.mLock")
{
}
nsAbLDAPListenerBase::~nsAbLDAPListenerBase()
{
}
nsresult nsAbLDAPListenerBase::Initiate()
{
if (!mConnection || !mDirectoryUrl)
return NS_ERROR_NULL_POINTER;
if (mInitialized)
return NS_OK;
mInitialized = true;
return NS_OK;
}
// If something fails in this function, we must call InitFailed() so that the
// derived class (and listener) knows to cancel what its doing as there is
// a problem.
NS_IMETHODIMP nsAbLDAPListenerBase::OnLDAPInit(nsILDAPConnection *aConn, nsresult aStatus)
{
if (!mConnection || !mDirectoryUrl)
{
InitFailed();
return NS_ERROR_NULL_POINTER;
}
nsresult rv;
nsString passwd;
// Make sure that the Init() worked properly
if (NS_FAILED(aStatus))
{
InitFailed();
return NS_OK;
}
// If mLogin is set, we're expected to use it to get a password.
//
if (!mLogin.IsEmpty() && !mSaslMechanism.EqualsLiteral("GSSAPI"))
{
// get the string bundle service
//
nsCOMPtr<nsIStringBundleService> stringBundleSvc =
mozilla::services::GetStringBundleService();
if (!stringBundleSvc)
{
NS_ERROR("nsAbLDAPListenerBase::OnLDAPInit():"
" error getting string bundle service");
InitFailed();
return NS_ERROR_UNEXPECTED;
}
// get the LDAP string bundle
//
nsCOMPtr<nsIStringBundle> ldapBundle;
rv = stringBundleSvc->CreateBundle("chrome://mozldap/locale/ldap.properties",
getter_AddRefs(ldapBundle));
if (NS_FAILED(rv))
{
NS_ERROR("nsAbLDAPListenerBase::OnLDAPInit(): error creating string"
"bundle chrome://mozldap/locale/ldap.properties");
InitFailed();
return rv;
}
// get the title for the authentication prompt
//
nsString authPromptTitle;
rv = ldapBundle->GetStringFromName(u"authPromptTitle",
getter_Copies(authPromptTitle));
if (NS_FAILED(rv))
{
NS_ERROR("nsAbLDAPListenerBase::OnLDAPInit(): error getting"
"'authPromptTitle' string from bundle "
"chrome://mozldap/locale/ldap.properties");
InitFailed();
return rv;
}
// get the host name for the auth prompt
//
nsAutoCString host;
rv = mDirectoryUrl->GetAsciiHost(host);
if (NS_FAILED(rv))
{
NS_ERROR("nsAbLDAPListenerBase::OnLDAPInit(): error getting ascii host"
"name from directory url");
InitFailed();
return rv;
}
// hostTemp is only necessary to work around a code-generation
// bug in egcs 1.1.2 (the version of gcc that comes with Red Hat 6.2),
// which is the default compiler for Mozilla on linux at the moment.
//
NS_ConvertASCIItoUTF16 hostTemp(host);
const char16_t *hostArray[1] = { hostTemp.get() };
// format the hostname into the authprompt text string
//
nsString authPromptText;
rv = ldapBundle->FormatStringFromName(u"authPromptText",
hostArray,
sizeof(hostArray) / sizeof(const char16_t *),
getter_Copies(authPromptText));
if (NS_FAILED(rv))
{
NS_ERROR("nsAbLDAPListenerBase::OnLDAPInit():"
"error getting 'authPromptText' string from bundle "
"chrome://mozldap/locale/ldap.properties");
InitFailed();
return rv;
}
// get the window mediator service, so we can get an auth prompter
//
nsCOMPtr<nsIWindowMediator> windowMediator =
do_GetService(NS_WINDOWMEDIATOR_CONTRACTID, &rv);
if (NS_FAILED(rv))
{
NS_ERROR("nsAbLDAPListenerBase::OnLDAPInit():"
" couldn't get window mediator service.");
InitFailed();
return rv;
}
// get the addressbook window, as it will be used to parent the auth
// prompter dialog
//
nsCOMPtr<mozIDOMWindowProxy> window;
rv = windowMediator->GetMostRecentWindow(nullptr,
getter_AddRefs(window));
if (NS_FAILED(rv))
{
NS_ERROR("nsAbLDAPListenerBase::OnLDAPInit():"
" error getting most recent window");
InitFailed();
return rv;
}
// get the window watcher service, so we can get an auth prompter
//
nsCOMPtr<nsIWindowWatcher> windowWatcherSvc =
do_GetService(NS_WINDOWWATCHER_CONTRACTID, &rv);
if (NS_FAILED(rv))
{
NS_ERROR("nsAbLDAPListenerBase::OnLDAPInit():"
" couldn't get window watcher service.");
InitFailed();
return rv;
}
// get the auth prompter itself
//
nsCOMPtr<nsIAuthPrompt> authPrompter;
rv = windowWatcherSvc->GetNewAuthPrompter(window,
getter_AddRefs(authPrompter));
if (NS_FAILED(rv))
{
NS_ERROR("nsAbLDAPMessageBase::OnLDAPInit():"
" error getting auth prompter");
InitFailed();
return rv;
}
// get authentication password, prompting the user if necessary
//
// we're going to use the URL spec of the server as the "realm" for
// wallet to remember the password by / for.
// Get the specification
nsCString spec;
rv = mDirectoryUrl->GetSpec(spec);
if (NS_FAILED(rv))
{
NS_ERROR("nsAbLDAPMessageBase::OnLDAPInit():"
" error getting directory url spec");
InitFailed();
return rv;
}
bool status;
rv = authPrompter->PromptPassword(authPromptTitle.get(),
authPromptText.get(),
NS_ConvertUTF8toUTF16(spec).get(),
nsIAuthPrompt::SAVE_PASSWORD_PERMANENTLY,
getter_Copies(passwd),
&status);
if (NS_FAILED(rv))
{
NS_ERROR("nsAbLDAPMessageBase::OnLDAPInit(): failed to prompt for"
" password");
InitFailed();
return rv;
}
else if (!status)
{
InitFailed(true);
return NS_OK;
}
}
// Initiate the LDAP operation
mOperation = do_CreateInstance(NS_LDAPOPERATION_CONTRACTID, &rv);
if (NS_FAILED(rv))
{
NS_ERROR("nsAbLDAPMessageBase::OnLDAPInit(): failed to create ldap operation");
InitFailed();
return rv;
}
rv = mOperation->Init(mConnection, this, nullptr);
if (NS_FAILED(rv))
{
NS_ERROR("nsAbLDAPMessageBase::OnLDAPInit(): failed to Initialise operation");
InitFailed();
return rv;
}
// Try non-password mechanisms first
if (mSaslMechanism.EqualsLiteral("GSSAPI"))
{
nsAutoCString service;
rv = mDirectoryUrl->GetAsciiHost(service);
NS_ENSURE_SUCCESS(rv, rv);
service.Insert(NS_LITERAL_CSTRING("ldap@"), 0);
nsCOMPtr<nsIAuthModule> authModule =
do_CreateInstance(NS_AUTH_MODULE_CONTRACTID_PREFIX "sasl-gssapi", &rv);
NS_ENSURE_SUCCESS(rv, rv);
rv = mOperation->SaslBind(service, mSaslMechanism, authModule);
if (NS_FAILED(rv))
{
NS_ERROR("nsAbLDAPMessageBase::OnLDAPInit(): "
"failed to perform GSSAPI bind");
mOperation = nullptr; // Break Listener -> Operation -> Listener ref cycle
InitFailed();
}
return rv;
}
// Bind
rv = mOperation->SimpleBind(NS_ConvertUTF16toUTF8(passwd));
if (NS_FAILED(rv))
{
NS_ERROR("nsAbLDAPMessageBase::OnLDAPInit(): failed to perform bind operation");
mOperation = nullptr; // Break Listener->Operation->Listener reference cycle
InitFailed();
}
return rv;
}
nsresult nsAbLDAPListenerBase::OnLDAPMessageBind(nsILDAPMessage *aMessage)
{
if (mBound)
return NS_OK;
// see whether the bind actually succeeded
//
int32_t errCode;
nsresult rv = aMessage->GetErrorCode(&errCode);
NS_ENSURE_SUCCESS(rv, rv);
if (errCode != nsILDAPErrors::SUCCESS)
{
// if the login failed, tell the wallet to forget this password
//
if (errCode == nsILDAPErrors::INAPPROPRIATE_AUTH ||
errCode == nsILDAPErrors::INVALID_CREDENTIALS)
{
// Login failed, so try again - but first remove the existing login(s)
// so that the user gets prompted. This may not be the best way of doing
// things, we need to review that later.
nsCOMPtr<nsILoginManager> loginMgr =
do_GetService(NS_LOGINMANAGER_CONTRACTID, &rv);
NS_ENSURE_SUCCESS(rv, rv);
nsCString spec;
rv = mDirectoryUrl->GetSpec(spec);
NS_ENSURE_SUCCESS(rv, rv);
nsCString prePath;
rv = mDirectoryUrl->GetPrePath(prePath);
NS_ENSURE_SUCCESS(rv, rv);
uint32_t count;
nsILoginInfo** logins;
rv = loginMgr->FindLogins(&count, NS_ConvertUTF8toUTF16(prePath),
EmptyString(),
NS_ConvertUTF8toUTF16(spec), &logins);
NS_ENSURE_SUCCESS(rv, rv);
// Typically there should only be one-login stored for this url, however,
// just in case there isn't.
for (uint32_t i = 0; i < count; ++i)
{
rv = loginMgr->RemoveLogin(logins[i]);
if (NS_FAILED(rv))
{
NS_FREE_XPCOM_ISUPPORTS_POINTER_ARRAY(count, logins);
return rv;
}
}
NS_FREE_XPCOM_ISUPPORTS_POINTER_ARRAY(count, logins);
// XXX We should probably pop up an error dialog telling
// the user that the login failed here, rather than just bringing
// up the password dialog again, which is what calling OnLDAPInit()
// does.
return OnLDAPInit(nullptr, NS_OK);
}
// Don't know how to handle this, so use the message error code in
// the failure return value so we hopefully get it back to the UI.
return NS_ERROR_GENERATE_FAILURE(NS_ERROR_MODULE_LDAP, errCode);
}
mBound = true;
return DoTask();
}
|