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 //-----------------------------------------------------------------------------
77 // We define GSS_C_NT_HOSTBASED_SERVICE explicitly since it may be referenced
78 // by by a different name depending on the implementation of gss but always
81 static gss_OID_desc gss_c_nt_hostbased_service
=
82 { 10, (void *) "\x2a\x86\x48\x86\xf7\x12\x01\x02\x01\x04" };
84 static const char kNegotiateAuthGssLib
[] =
85 "network.negotiate-auth.gsslib";
86 static const char kNegotiateAuthNativeImp
[] =
87 "network.negotiate-auth.using-native-gsslib";
89 static const char *gssFuncStr
[] = {
91 "gss_init_sec_context",
93 "gss_release_oid_set",
94 "gss_delete_sec_context",
102 #define gssFuncItems NS_ARRAY_LENGTH(gssFuncStr)
104 static PRFuncPtr gssFunPtr
[gssFuncItems
];
105 static PRBool gssNativeImp
= PR_TRUE
;
106 static PRLibrary
* gssLibrary
= nsnull
;
108 #define gss_display_status_ptr ((gss_display_status_type)*gssFunPtr[0])
109 #define gss_init_sec_context_ptr ((gss_init_sec_context_type)*gssFunPtr[1])
110 #define gss_indicate_mechs_ptr ((gss_indicate_mechs_type)*gssFunPtr[2])
111 #define gss_release_oid_set_ptr ((gss_release_oid_set_type)*gssFunPtr[3])
112 #define gss_delete_sec_context_ptr ((gss_delete_sec_context_type)*gssFunPtr[4])
113 #define gss_import_name_ptr ((gss_import_name_type)*gssFunPtr[5])
114 #define gss_release_buffer_ptr ((gss_release_buffer_type)*gssFunPtr[6])
115 #define gss_release_name_ptr ((gss_release_name_type)*gssFunPtr[7])
116 #define gss_wrap_ptr ((gss_wrap_type)*gssFunPtr[8])
117 #define gss_unwrap_ptr ((gss_unwrap_type)*gssFunPtr[9])
120 static PRFuncPtr KLCacheHasValidTicketsPtr
;
121 #define KLCacheHasValidTickets_ptr \
122 ((KLCacheHasValidTickets_type)*KLCacheHasValidTicketsPtr)
128 nsXPIDLCString libPath
;
129 nsCOMPtr
<nsIPrefBranch
> prefs
= do_GetService(NS_PREFSERVICE_CONTRACTID
);
131 prefs
->GetCharPref(kNegotiateAuthGssLib
, getter_Copies(libPath
));
132 prefs
->GetBoolPref(kNegotiateAuthNativeImp
, &gssNativeImp
);
135 PRLibrary
*lib
= NULL
;
137 if (!libPath
.IsEmpty()) {
138 LOG(("Attempting to load user specified library [%s]\n", libPath
.get()));
139 gssNativeImp
= PR_FALSE
;
140 lib
= PR_LoadLibrary(libPath
.get());
144 char *libName
= PR_GetLibraryName(NULL
, "gssapi32");
146 lib
= PR_LoadLibrary("gssapi32");
147 PR_FreeLibraryName(libName
);
151 const char *const libNames
[] = {
157 const char *const verLibNames
[] = {
158 "libgssapi_krb5.so.2", /* MIT - FC, Suse10, Debian */
159 "libgssapi.so.4", /* Heimdal - Suse10, MDK */
160 "libgssapi.so.1" /* Heimdal - Suse9, CITI - FC, MDK, Suse10*/
163 for (size_t i
= 0; i
< NS_ARRAY_LENGTH(verLibNames
) && !lib
; ++i
) {
164 lib
= PR_LoadLibrary(verLibNames
[i
]);
166 /* The CITI libgssapi library calls exit() during
167 * initialization if it's not correctly configured. Try to
168 * ensure that we never use this library for our GSSAPI
169 * support, as its just a wrapper library, anyway.
170 * See Bugzilla #325433
173 PR_FindFunctionSymbol(lib
,
174 "internal_krb5_gss_initialize") &&
175 PR_FindFunctionSymbol(lib
, "gssd_pname_to_uid")) {
176 LOG(("CITI libgssapi found, which calls exit(). Skipping\n"));
177 PR_UnloadLibrary(lib
);
182 for (size_t i
= 0; i
< NS_ARRAY_LENGTH(libNames
) && !lib
; ++i
) {
183 char *libName
= PR_GetLibraryName(NULL
, libNames
[i
]);
185 lib
= PR_LoadLibrary(libName
);
186 PR_FreeLibraryName(libName
);
189 PR_FindFunctionSymbol(lib
,
190 "internal_krb5_gss_initialize") &&
191 PR_FindFunctionSymbol(lib
, "gssd_pname_to_uid")) {
192 LOG(("CITI libgssapi found, which calls exit(). Skipping\n"));
193 PR_UnloadLibrary(lib
);
202 LOG(("Fail to load gssapi library\n"));
203 return NS_ERROR_FAILURE
;
206 LOG(("Attempting to load gss functions\n"));
208 for (size_t i
= 0; i
< gssFuncItems
; ++i
) {
209 gssFunPtr
[i
] = PR_FindFunctionSymbol(lib
, gssFuncStr
[i
]);
211 LOG(("Fail to load %s function from gssapi library\n", gssFuncStr
[i
]));
212 PR_UnloadLibrary(lib
);
213 return NS_ERROR_FAILURE
;
218 !(KLCacheHasValidTicketsPtr
=
219 PR_FindFunctionSymbol(lib
, "KLCacheHasValidTickets"))) {
220 LOG(("Fail to load KLCacheHasValidTickets function from gssapi library\n"));
221 PR_UnloadLibrary(lib
);
222 return NS_ERROR_FAILURE
;
230 #if defined( PR_LOGGING )
232 // Generate proper GSSAPI error messages from the major and
233 // minor status codes.
235 LogGssError(OM_uint32 maj_stat
, OM_uint32 min_stat
, const char *prefix
)
238 OM_uint32 msg_ctx
= 0;
239 gss_buffer_desc status1_string
;
240 gss_buffer_desc status2_string
;
242 nsCAutoString errorStr
;
243 errorStr
.Assign(prefix
);
250 ret
= gss_display_status_ptr(&new_stat
,
256 errorStr
.Append((const char *) status1_string
.value
, status1_string
.length
);
257 gss_release_buffer_ptr(&new_stat
, &status1_string
);
260 ret
= gss_display_status_ptr(&new_stat
,
266 errorStr
.Append((const char *) status2_string
.value
, status2_string
.length
);
268 } while (!GSS_ERROR(ret
) && msg_ctx
!= 0);
270 LOG(("%s\n", errorStr
.get()));
273 #else /* PR_LOGGING */
275 #define LogGssError(x,y,z)
277 #endif /* PR_LOGGING */
279 //-----------------------------------------------------------------------------
281 nsAuthGSSAPI::nsAuthGSSAPI(pType package
)
282 : mServiceFlags(REQ_DEFAULT
)
286 gss_OID_set mech_set
;
290 static gss_OID_desc gss_krb5_mech_oid_desc
=
291 { 9, (void *) "\x2a\x86\x48\x86\xf7\x12\x01\x02\x02" };
292 static gss_OID_desc gss_spnego_mech_oid_desc
=
293 { 6, (void *) "\x2b\x06\x01\x05\x05\x02" };
295 LOG(("entering nsAuthGSSAPI::nsAuthGSSAPI()\n"));
297 mComplete
= PR_FALSE
;
299 if (!gssLibrary
&& NS_FAILED(gssInit()))
302 mCtx
= GSS_C_NO_CONTEXT
;
303 mMechOID
= &gss_krb5_mech_oid_desc
;
305 // if the type is kerberos we accept it as default
308 if (package
== PACKAGE_TYPE_KERBEROS
)
311 // Now, look at the list of supported mechanisms,
312 // if SPNEGO is found, then use it.
313 // Otherwise, set the desired mechanism to
314 // GSS_C_NO_OID and let the system try to use
315 // the default mechanism.
317 // Using Kerberos directly (instead of negotiating
318 // with SPNEGO) may work in some cases depending
319 // on how smart the server side is.
321 majstat
= gss_indicate_mechs_ptr(&minstat
, &mech_set
);
322 if (GSS_ERROR(majstat
))
326 for (i
=0; i
<mech_set
->count
; i
++) {
327 item
= &mech_set
->elements
[i
];
328 if (item
->length
== gss_spnego_mech_oid_desc
.length
&&
329 !memcmp(item
->elements
, gss_spnego_mech_oid_desc
.elements
,
332 mMechOID
= &gss_spnego_mech_oid_desc
;
336 gss_release_oid_set_ptr(&minstat
, &mech_set
);
341 nsAuthGSSAPI::Reset()
343 if (gssLibrary
&& mCtx
!= GSS_C_NO_CONTEXT
) {
344 OM_uint32 minor_status
;
345 gss_delete_sec_context_ptr(&minor_status
, &mCtx
, GSS_C_NO_BUFFER
);
347 mCtx
= GSS_C_NO_CONTEXT
;
348 mComplete
= PR_FALSE
;
352 nsAuthGSSAPI::Shutdown()
355 PR_UnloadLibrary(gssLibrary
);
360 /* Limitations apply to this class's thread safety. See the header file */
361 NS_IMPL_THREADSAFE_ISUPPORTS1(nsAuthGSSAPI
, nsIAuthModule
)
364 nsAuthGSSAPI::Init(const char *serviceName
,
365 PRUint32 serviceFlags
,
366 const PRUnichar
*domain
,
367 const PRUnichar
*username
,
368 const PRUnichar
*password
)
370 // we don't expect to be passed any user credentials
371 NS_ASSERTION(!domain
&& !username
&& !password
, "unexpected credentials");
373 // it's critial that the caller supply a service name to be used
374 NS_ENSURE_TRUE(serviceName
&& *serviceName
, NS_ERROR_INVALID_ARG
);
376 LOG(("entering nsAuthGSSAPI::Init()\n"));
379 return NS_ERROR_NOT_INITIALIZED
;
381 mServiceName
= serviceName
;
382 mServiceFlags
= serviceFlags
;
387 nsAuthGSSAPI::GetNextToken(const void *inToken
,
390 PRUint32
*outTokenLen
)
392 OM_uint32 major_status
, minor_status
;
393 OM_uint32 req_flags
= 0;
394 gss_buffer_desc input_token
= GSS_C_EMPTY_BUFFER
;
395 gss_buffer_desc output_token
= GSS_C_EMPTY_BUFFER
;
396 gss_buffer_t in_token_ptr
= GSS_C_NO_BUFFER
;
398 nsCAutoString userbuf
;
401 LOG(("entering nsAuthGSSAPI::GetNextToken()\n"));
404 return NS_ERROR_NOT_INITIALIZED
;
406 // If they've called us again after we're complete, reset to start afresh.
410 if (mServiceFlags
& REQ_DELEGATE
)
411 req_flags
|= GSS_C_DELEG_FLAG
;
413 if (mServiceFlags
& REQ_MUTUAL_AUTH
)
414 req_flags
|= GSS_C_MUTUAL_FLAG
;
416 input_token
.value
= (void *)mServiceName
.get();
417 input_token
.length
= mServiceName
.Length() + 1;
419 major_status
= gss_import_name_ptr(&minor_status
,
421 &gss_c_nt_hostbased_service
,
423 input_token
.value
= NULL
;
424 input_token
.length
= 0;
425 if (GSS_ERROR(major_status
)) {
426 LogGssError(major_status
, minor_status
, "gss_import_name() failed");
427 return NS_ERROR_FAILURE
;
431 input_token
.length
= inTokenLen
;
432 input_token
.value
= (void *) inToken
;
433 in_token_ptr
= &input_token
;
435 else if (mCtx
!= GSS_C_NO_CONTEXT
) {
436 // If there is no input token, then we are starting a new
437 // authentication sequence. If we have already initialized our
438 // security context, then we're in trouble because it means that the
439 // first sequence failed. We need to bail or else we might end up in
441 LOG(("Cannot restart authentication sequence!"));
442 return NS_ERROR_UNEXPECTED
;
445 #if defined(XP_MACOSX)
446 // Suppress Kerberos prompts to get credentials. See bug 240643.
447 // We can only use Mac OS X specific kerb functions if we are using
450 PRBool doingMailTask
= mServiceName
.Find("imap@") ||
451 mServiceName
.Find("pop@") ||
452 mServiceName
.Find("smtp@") ||
453 mServiceName
.Find("ldap@");
455 if (!doingMailTask
&& (gssNativeImp
&&
456 (KLCacheHasValidTickets_ptr(NULL
, kerberosVersion_V5
, &found
, NULL
, NULL
) != klNoErr
|| !found
)))
458 major_status
= GSS_S_FAILURE
;
462 #endif /* XP_MACOSX */
463 major_status
= gss_init_sec_context_ptr(&minor_status
,
470 GSS_C_NO_CHANNEL_BINDINGS
,
477 if (GSS_ERROR(major_status
)) {
478 LogGssError(major_status
, minor_status
, "gss_init_sec_context() failed");
480 rv
= NS_ERROR_FAILURE
;
483 if (major_status
== GSS_S_COMPLETE
) {
484 // Mark ourselves as being complete, so that if we're called again
485 // we know to start afresh.
488 else if (major_status
== GSS_S_CONTINUE_NEEDED
) {
490 // The important thing is that we do NOT reset the
491 // context here because it will be needed on the
496 *outTokenLen
= output_token
.length
;
497 if (output_token
.length
!= 0)
498 *outToken
= nsMemory::Clone(output_token
.value
, output_token
.length
);
502 gss_release_buffer_ptr(&minor_status
, &output_token
);
504 if (major_status
== GSS_S_COMPLETE
)
505 rv
= NS_SUCCESS_AUTH_FINISHED
;
510 gss_release_name_ptr(&minor_status
, &server
);
512 LOG((" leaving nsAuthGSSAPI::GetNextToken [rv=%x]", rv
));
517 nsAuthGSSAPI::Unwrap(const void *inToken
,
520 PRUint32
*outTokenLen
)
522 OM_uint32 major_status
, minor_status
;
524 gss_buffer_desc input_token
;
525 gss_buffer_desc output_token
= GSS_C_EMPTY_BUFFER
;
527 input_token
.value
= (void *) inToken
;
528 input_token
.length
= inTokenLen
;
530 major_status
= gss_unwrap_ptr(&minor_status
,
536 if (GSS_ERROR(major_status
)) {
537 LogGssError(major_status
, minor_status
, "gss_unwrap() failed");
539 gss_release_buffer_ptr(&minor_status
, &output_token
);
540 return NS_ERROR_FAILURE
;
543 *outTokenLen
= output_token
.length
;
545 if (output_token
.length
)
546 *outToken
= nsMemory::Clone(output_token
.value
, output_token
.length
);
550 gss_release_buffer_ptr(&minor_status
, &output_token
);
556 nsAuthGSSAPI::Wrap(const void *inToken
,
560 PRUint32
*outTokenLen
)
562 OM_uint32 major_status
, minor_status
;
564 gss_buffer_desc input_token
;
565 gss_buffer_desc output_token
= GSS_C_EMPTY_BUFFER
;
567 input_token
.value
= (void *) inToken
;
568 input_token
.length
= inTokenLen
;
570 major_status
= gss_wrap_ptr(&minor_status
,
578 if (GSS_ERROR(major_status
)) {
579 LogGssError(major_status
, minor_status
, "gss_wrap() failed");
581 gss_release_buffer_ptr(&minor_status
, &output_token
);
582 return NS_ERROR_FAILURE
;
585 *outTokenLen
= output_token
.length
;
587 /* it is not possible for output_token.length to be zero */
588 *outToken
= nsMemory::Clone(output_token
.value
, output_token
.length
);
589 gss_release_buffer_ptr(&minor_status
, &output_token
);