1 /* vim:set ts=4 sw=4 sts=4 et cindent: */
2 /* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
15 * The Original Code is the Negotiateauth
17 * The Initial Developer of the Original Code is Daniel Kouril.
18 * Portions created by the Initial Developer are Copyright (C) 2003
19 * the Initial Developer. All Rights Reserved.
22 * Daniel Kouril <kouril@ics.muni.cz> (original author)
23 * Wyllys Ingersoll <wyllys.ingersoll@sun.com>
24 * Christopher Nebergall <cneberg@sandia.gov>
25 * Darin Fisher <darin@meer.net>
26 * Mark Mentovai <mark@moxienet.com>
28 * Alternatively, the contents of this file may be used under the terms of
29 * either the GNU General Public License Version 2 or later (the "GPL"), or
30 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
31 * in which case the provisions of the GPL or the LGPL are applicable instead
32 * of those above. If you wish to allow use of your version of this file only
33 * under the terms of either the GPL or the LGPL, and not to allow others to
34 * use your version of this file under the terms of the MPL, indicate your
35 * decision by deleting the provisions above and replace them with the notice
36 * and other provisions required by the GPL or the LGPL. If you do not delete
37 * the provisions above, a recipient may use your version of this file under
38 * the terms of any one of the MPL, the GPL or the LGPL.
40 * ***** END LICENSE BLOCK ***** */
43 // GSSAPI Authentication Support Module
45 // Described by IETF Internet draft: draft-brezak-kerberos-http-00.txt
46 // (formerly draft-brezak-spnego-http-04.txt)
48 // Also described here:
49 // http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnsecure/html/http-sso-1.asp
55 #include "nsIPrefService.h"
56 #include "nsIPrefBranch.h"
57 #include "nsIServiceManager.h"
58 #include "nsNativeCharsetUtils.h"
60 #include "nsAuthGSSAPI.h"
63 #include <Kerberos/Kerberos.h>
67 typedef KLStatus (*KLCacheHasValidTickets_type
)(
75 #if defined(HAVE_RES_NINIT)
79 //-----------------------------------------------------------------------------
81 // We define GSS_C_NT_HOSTBASED_SERVICE explicitly since it may be referenced
82 // by by a different name depending on the implementation of gss but always
85 static gss_OID_desc gss_c_nt_hostbased_service
=
86 { 10, (void *) "\x2a\x86\x48\x86\xf7\x12\x01\x02\x01\x04" };
88 static const char kNegotiateAuthGssLib
[] =
89 "network.negotiate-auth.gsslib";
90 static const char kNegotiateAuthNativeImp
[] =
91 "network.negotiate-auth.using-native-gsslib";
93 static const char *gssFuncStr
[] = {
95 "gss_init_sec_context",
97 "gss_release_oid_set",
98 "gss_delete_sec_context",
100 "gss_release_buffer",
106 #define gssFuncItems NS_ARRAY_LENGTH(gssFuncStr)
108 static PRFuncPtr gssFunPtr
[gssFuncItems
];
109 static PRBool gssNativeImp
= PR_TRUE
;
110 static PRLibrary
* gssLibrary
= nsnull
;
112 #define gss_display_status_ptr ((gss_display_status_type)*gssFunPtr[0])
113 #define gss_init_sec_context_ptr ((gss_init_sec_context_type)*gssFunPtr[1])
114 #define gss_indicate_mechs_ptr ((gss_indicate_mechs_type)*gssFunPtr[2])
115 #define gss_release_oid_set_ptr ((gss_release_oid_set_type)*gssFunPtr[3])
116 #define gss_delete_sec_context_ptr ((gss_delete_sec_context_type)*gssFunPtr[4])
117 #define gss_import_name_ptr ((gss_import_name_type)*gssFunPtr[5])
118 #define gss_release_buffer_ptr ((gss_release_buffer_type)*gssFunPtr[6])
119 #define gss_release_name_ptr ((gss_release_name_type)*gssFunPtr[7])
120 #define gss_wrap_ptr ((gss_wrap_type)*gssFunPtr[8])
121 #define gss_unwrap_ptr ((gss_unwrap_type)*gssFunPtr[9])
124 static PRFuncPtr KLCacheHasValidTicketsPtr
;
125 #define KLCacheHasValidTickets_ptr \
126 ((KLCacheHasValidTickets_type)*KLCacheHasValidTicketsPtr)
132 nsXPIDLCString libPath
;
133 nsCOMPtr
<nsIPrefBranch
> prefs
= do_GetService(NS_PREFSERVICE_CONTRACTID
);
135 prefs
->GetCharPref(kNegotiateAuthGssLib
, getter_Copies(libPath
));
136 prefs
->GetBoolPref(kNegotiateAuthNativeImp
, &gssNativeImp
);
139 PRLibrary
*lib
= NULL
;
141 if (!libPath
.IsEmpty()) {
142 LOG(("Attempting to load user specified library [%s]\n", libPath
.get()));
143 gssNativeImp
= PR_FALSE
;
144 lib
= PR_LoadLibrary(libPath
.get());
148 char *libName
= PR_GetLibraryName(NULL
, "gssapi32");
150 lib
= PR_LoadLibrary("gssapi32");
151 PR_FreeLibraryName(libName
);
155 const char *const libNames
[] = {
161 const char *const verLibNames
[] = {
162 "libgssapi_krb5.so.2", /* MIT - FC, Suse10, Debian */
163 "libgssapi.so.4", /* Heimdal - Suse10, MDK */
164 "libgssapi.so.1" /* Heimdal - Suse9, CITI - FC, MDK, Suse10*/
167 for (size_t i
= 0; i
< NS_ARRAY_LENGTH(verLibNames
) && !lib
; ++i
) {
168 lib
= PR_LoadLibrary(verLibNames
[i
]);
170 /* The CITI libgssapi library calls exit() during
171 * initialization if it's not correctly configured. Try to
172 * ensure that we never use this library for our GSSAPI
173 * support, as its just a wrapper library, anyway.
174 * See Bugzilla #325433
177 PR_FindFunctionSymbol(lib
,
178 "internal_krb5_gss_initialize") &&
179 PR_FindFunctionSymbol(lib
, "gssd_pname_to_uid")) {
180 LOG(("CITI libgssapi found, which calls exit(). Skipping\n"));
181 PR_UnloadLibrary(lib
);
186 for (size_t i
= 0; i
< NS_ARRAY_LENGTH(libNames
) && !lib
; ++i
) {
187 char *libName
= PR_GetLibraryName(NULL
, libNames
[i
]);
189 lib
= PR_LoadLibrary(libName
);
190 PR_FreeLibraryName(libName
);
193 PR_FindFunctionSymbol(lib
,
194 "internal_krb5_gss_initialize") &&
195 PR_FindFunctionSymbol(lib
, "gssd_pname_to_uid")) {
196 LOG(("CITI libgssapi found, which calls exit(). Skipping\n"));
197 PR_UnloadLibrary(lib
);
206 LOG(("Fail to load gssapi library\n"));
207 return NS_ERROR_FAILURE
;
210 LOG(("Attempting to load gss functions\n"));
212 for (size_t i
= 0; i
< gssFuncItems
; ++i
) {
213 gssFunPtr
[i
] = PR_FindFunctionSymbol(lib
, gssFuncStr
[i
]);
215 LOG(("Fail to load %s function from gssapi library\n", gssFuncStr
[i
]));
216 PR_UnloadLibrary(lib
);
217 return NS_ERROR_FAILURE
;
222 !(KLCacheHasValidTicketsPtr
=
223 PR_FindFunctionSymbol(lib
, "KLCacheHasValidTickets"))) {
224 LOG(("Fail to load KLCacheHasValidTickets function from gssapi library\n"));
225 PR_UnloadLibrary(lib
);
226 return NS_ERROR_FAILURE
;
234 #if defined( PR_LOGGING )
236 // Generate proper GSSAPI error messages from the major and
237 // minor status codes.
239 LogGssError(OM_uint32 maj_stat
, OM_uint32 min_stat
, const char *prefix
)
242 OM_uint32 msg_ctx
= 0;
243 gss_buffer_desc status1_string
;
244 gss_buffer_desc status2_string
;
246 nsCAutoString errorStr
;
247 errorStr
.Assign(prefix
);
254 ret
= gss_display_status_ptr(&new_stat
,
260 errorStr
.Append((const char *) status1_string
.value
, status1_string
.length
);
261 gss_release_buffer_ptr(&new_stat
, &status1_string
);
264 ret
= gss_display_status_ptr(&new_stat
,
270 errorStr
.Append((const char *) status2_string
.value
, status2_string
.length
);
272 } while (!GSS_ERROR(ret
) && msg_ctx
!= 0);
274 LOG(("%s\n", errorStr
.get()));
277 #else /* PR_LOGGING */
279 #define LogGssError(x,y,z)
281 #endif /* PR_LOGGING */
283 //-----------------------------------------------------------------------------
285 nsAuthGSSAPI::nsAuthGSSAPI(pType package
)
286 : mServiceFlags(REQ_DEFAULT
)
290 gss_OID_set mech_set
;
294 static gss_OID_desc gss_krb5_mech_oid_desc
=
295 { 9, (void *) "\x2a\x86\x48\x86\xf7\x12\x01\x02\x02" };
296 static gss_OID_desc gss_spnego_mech_oid_desc
=
297 { 6, (void *) "\x2b\x06\x01\x05\x05\x02" };
299 LOG(("entering nsAuthGSSAPI::nsAuthGSSAPI()\n"));
301 mComplete
= PR_FALSE
;
303 if (!gssLibrary
&& NS_FAILED(gssInit()))
306 mCtx
= GSS_C_NO_CONTEXT
;
307 mMechOID
= &gss_krb5_mech_oid_desc
;
309 // if the type is kerberos we accept it as default
312 if (package
== PACKAGE_TYPE_KERBEROS
)
315 // Now, look at the list of supported mechanisms,
316 // if SPNEGO is found, then use it.
317 // Otherwise, set the desired mechanism to
318 // GSS_C_NO_OID and let the system try to use
319 // the default mechanism.
321 // Using Kerberos directly (instead of negotiating
322 // with SPNEGO) may work in some cases depending
323 // on how smart the server side is.
325 majstat
= gss_indicate_mechs_ptr(&minstat
, &mech_set
);
326 if (GSS_ERROR(majstat
))
330 for (i
=0; i
<mech_set
->count
; i
++) {
331 item
= &mech_set
->elements
[i
];
332 if (item
->length
== gss_spnego_mech_oid_desc
.length
&&
333 !memcmp(item
->elements
, gss_spnego_mech_oid_desc
.elements
,
336 mMechOID
= &gss_spnego_mech_oid_desc
;
340 gss_release_oid_set_ptr(&minstat
, &mech_set
);
345 nsAuthGSSAPI::Reset()
347 if (gssLibrary
&& mCtx
!= GSS_C_NO_CONTEXT
) {
348 OM_uint32 minor_status
;
349 gss_delete_sec_context_ptr(&minor_status
, &mCtx
, GSS_C_NO_BUFFER
);
351 mCtx
= GSS_C_NO_CONTEXT
;
352 mComplete
= PR_FALSE
;
356 nsAuthGSSAPI::Shutdown()
359 PR_UnloadLibrary(gssLibrary
);
364 /* Limitations apply to this class's thread safety. See the header file */
365 NS_IMPL_THREADSAFE_ISUPPORTS1(nsAuthGSSAPI
, nsIAuthModule
)
368 nsAuthGSSAPI::Init(const char *serviceName
,
369 PRUint32 serviceFlags
,
370 const PRUnichar
*domain
,
371 const PRUnichar
*username
,
372 const PRUnichar
*password
)
374 // we don't expect to be passed any user credentials
375 NS_ASSERTION(!domain
&& !username
&& !password
, "unexpected credentials");
377 // it's critial that the caller supply a service name to be used
378 NS_ENSURE_TRUE(serviceName
&& *serviceName
, NS_ERROR_INVALID_ARG
);
380 LOG(("entering nsAuthGSSAPI::Init()\n"));
383 return NS_ERROR_NOT_INITIALIZED
;
385 mServiceName
= serviceName
;
386 mServiceFlags
= serviceFlags
;
391 nsAuthGSSAPI::GetNextToken(const void *inToken
,
394 PRUint32
*outTokenLen
)
396 OM_uint32 major_status
, minor_status
;
397 OM_uint32 req_flags
= 0;
398 gss_buffer_desc input_token
= GSS_C_EMPTY_BUFFER
;
399 gss_buffer_desc output_token
= GSS_C_EMPTY_BUFFER
;
400 gss_buffer_t in_token_ptr
= GSS_C_NO_BUFFER
;
402 nsCAutoString userbuf
;
405 LOG(("entering nsAuthGSSAPI::GetNextToken()\n"));
408 return NS_ERROR_NOT_INITIALIZED
;
410 // If they've called us again after we're complete, reset to start afresh.
414 if (mServiceFlags
& REQ_DELEGATE
)
415 req_flags
|= GSS_C_DELEG_FLAG
;
417 if (mServiceFlags
& REQ_MUTUAL_AUTH
)
418 req_flags
|= GSS_C_MUTUAL_FLAG
;
420 input_token
.value
= (void *)mServiceName
.get();
421 input_token
.length
= mServiceName
.Length() + 1;
423 #if defined(HAVE_RES_NINIT)
426 major_status
= gss_import_name_ptr(&minor_status
,
428 &gss_c_nt_hostbased_service
,
430 input_token
.value
= NULL
;
431 input_token
.length
= 0;
432 if (GSS_ERROR(major_status
)) {
433 LogGssError(major_status
, minor_status
, "gss_import_name() failed");
434 return NS_ERROR_FAILURE
;
438 input_token
.length
= inTokenLen
;
439 input_token
.value
= (void *) inToken
;
440 in_token_ptr
= &input_token
;
442 else if (mCtx
!= GSS_C_NO_CONTEXT
) {
443 // If there is no input token, then we are starting a new
444 // authentication sequence. If we have already initialized our
445 // security context, then we're in trouble because it means that the
446 // first sequence failed. We need to bail or else we might end up in
448 LOG(("Cannot restart authentication sequence!"));
449 return NS_ERROR_UNEXPECTED
;
452 #if defined(XP_MACOSX)
453 // Suppress Kerberos prompts to get credentials. See bug 240643.
454 // We can only use Mac OS X specific kerb functions if we are using
457 PRBool doingMailTask
= mServiceName
.Find("imap@") ||
458 mServiceName
.Find("pop@") ||
459 mServiceName
.Find("smtp@") ||
460 mServiceName
.Find("ldap@");
462 if (!doingMailTask
&& (gssNativeImp
&&
463 (KLCacheHasValidTickets_ptr(NULL
, kerberosVersion_V5
, &found
, NULL
, NULL
) != klNoErr
|| !found
)))
465 major_status
= GSS_S_FAILURE
;
469 #endif /* XP_MACOSX */
470 major_status
= gss_init_sec_context_ptr(&minor_status
,
477 GSS_C_NO_CHANNEL_BINDINGS
,
484 if (GSS_ERROR(major_status
)) {
485 LogGssError(major_status
, minor_status
, "gss_init_sec_context() failed");
487 rv
= NS_ERROR_FAILURE
;
490 if (major_status
== GSS_S_COMPLETE
) {
491 // Mark ourselves as being complete, so that if we're called again
492 // we know to start afresh.
495 else if (major_status
== GSS_S_CONTINUE_NEEDED
) {
497 // The important thing is that we do NOT reset the
498 // context here because it will be needed on the
503 *outTokenLen
= output_token
.length
;
504 if (output_token
.length
!= 0)
505 *outToken
= nsMemory::Clone(output_token
.value
, output_token
.length
);
509 gss_release_buffer_ptr(&minor_status
, &output_token
);
511 if (major_status
== GSS_S_COMPLETE
)
512 rv
= NS_SUCCESS_AUTH_FINISHED
;
517 gss_release_name_ptr(&minor_status
, &server
);
519 LOG((" leaving nsAuthGSSAPI::GetNextToken [rv=%x]", rv
));
524 nsAuthGSSAPI::Unwrap(const void *inToken
,
527 PRUint32
*outTokenLen
)
529 OM_uint32 major_status
, minor_status
;
531 gss_buffer_desc input_token
;
532 gss_buffer_desc output_token
= GSS_C_EMPTY_BUFFER
;
534 input_token
.value
= (void *) inToken
;
535 input_token
.length
= inTokenLen
;
537 major_status
= gss_unwrap_ptr(&minor_status
,
543 if (GSS_ERROR(major_status
)) {
544 LogGssError(major_status
, minor_status
, "gss_unwrap() failed");
546 gss_release_buffer_ptr(&minor_status
, &output_token
);
547 return NS_ERROR_FAILURE
;
550 *outTokenLen
= output_token
.length
;
552 if (output_token
.length
)
553 *outToken
= nsMemory::Clone(output_token
.value
, output_token
.length
);
557 gss_release_buffer_ptr(&minor_status
, &output_token
);
563 nsAuthGSSAPI::Wrap(const void *inToken
,
567 PRUint32
*outTokenLen
)
569 OM_uint32 major_status
, minor_status
;
571 gss_buffer_desc input_token
;
572 gss_buffer_desc output_token
= GSS_C_EMPTY_BUFFER
;
574 input_token
.value
= (void *) inToken
;
575 input_token
.length
= inTokenLen
;
577 major_status
= gss_wrap_ptr(&minor_status
,
585 if (GSS_ERROR(major_status
)) {
586 LogGssError(major_status
, minor_status
, "gss_wrap() failed");
588 gss_release_buffer_ptr(&minor_status
, &output_token
);
589 return NS_ERROR_FAILURE
;
592 *outTokenLen
= output_token
.length
;
594 /* it is not possible for output_token.length to be zero */
595 *outToken
= nsMemory::Clone(output_token
.value
, output_token
.length
);
596 gss_release_buffer_ptr(&minor_status
, &output_token
);