1 /* vim:set ts=4 sw=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 saslgssapi
17 * The Initial Developer of the Original Code is Simon Wilkinson
18 * Portions created by the Initial Developer are Copyright (C) 2005
19 * the Initial Developer. All Rights Reserved.
22 * Simon Wilkinson <simon@sxw.org.uk>
24 * Alternatively, the contents of this file may be used under the terms of
25 * either the GNU General Public License Version 2 or later (the "GPL"), or
26 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 * in which case the provisions of the GPL or the LGPL are applicable instead
28 * of those above. If you wish to allow use of your version of this file only
29 * under the terms of either the GPL or the LGPL, and not to allow others to
30 * use your version of this file under the terms of the MPL, indicate your
31 * decision by deleting the provisions above and replace them with the notice
32 * and other provisions required by the GPL or the LGPL. If you do not delete
33 * the provisions above, a recipient may use your version of this file under
34 * the terms of any one of the MPL, the GPL or the LGPL.
36 * ***** END LICENSE BLOCK ***** */
38 #include "nsComponentManagerUtils.h"
39 #include "nsNativeCharsetUtils.h"
40 #include "nsIServiceManager.h"
41 #include "nsIPrefService.h"
43 #include "nsAuthSASL.h"
45 static const char kNegotiateAuthSSPI
[] = "network.auth.use-sspi";
47 nsAuthSASL::nsAuthSASL()
52 void nsAuthSASL::Reset()
57 /* Limitations apply to this class's thread safety. See the header file */
58 NS_IMPL_THREADSAFE_ISUPPORTS1(nsAuthSASL
, nsIAuthModule
)
61 nsAuthSASL::Init(const char *serviceName
,
62 PRUint32 serviceFlags
,
63 const PRUnichar
*domain
,
64 const PRUnichar
*username
,
65 const PRUnichar
*password
)
69 NS_ASSERTION(username
, "SASL requires a username");
70 NS_ASSERTION(!domain
&& !password
, "unexpected credentials");
74 // If we're doing SASL, we should do mutual auth
75 serviceFlags
|= REQ_MUTUAL_AUTH
;
77 // Find out whether we should be trying SSPI or not
78 const char *contractID
= NS_AUTH_MODULE_CONTRACTID_PREFIX
"kerb-gss";
80 nsCOMPtr
<nsIPrefBranch
> prefs
= do_GetService(NS_PREFSERVICE_CONTRACTID
);
83 rv
= prefs
->GetBoolPref(kNegotiateAuthSSPI
, &val
);
84 if (NS_SUCCEEDED(rv
) && val
)
85 contractID
= NS_AUTH_MODULE_CONTRACTID_PREFIX
"kerb-sspi";
88 mInnerModule
= do_CreateInstance(contractID
, &rv
);
89 // if we can't create the GSSAPI module, then bail
90 NS_ENSURE_SUCCESS(rv
, rv
);
92 mInnerModule
->Init(serviceName
, serviceFlags
, nsnull
, nsnull
, nsnull
);
98 nsAuthSASL::GetNextToken(const void *inToken
,
101 PRUint32
*outTokenLen
)
104 void *unwrappedToken
;
106 PRUint32 unwrappedTokenLen
, messageLen
;
107 nsCAutoString userbuf
;
110 return NS_ERROR_NOT_INITIALIZED
;
113 // If the server COMPLETEs with an empty token, Cyrus sends us that token.
114 // I don't think this is correct, but we need to handle that behaviour.
115 // Cyrus ignores the contents of our reply token.
116 if (inTokenLen
== 0) {
121 // We've completed the GSSAPI portion of the handshake, and are
122 // now ready to do the SASL security layer and authzid negotiation
124 // Input packet from the server needs to be unwrapped.
125 rv
= mInnerModule
->Unwrap(inToken
, inTokenLen
, &unwrappedToken
,
132 // If we were doing security layers then we'd care what the
133 // server had sent us. We're not, so all we had to do was make
134 // sure that the signature was correct with the above unwrap()
135 nsMemory::Free(unwrappedToken
);
137 NS_CopyUnicodeToNative(mUsername
, userbuf
);
138 messageLen
= userbuf
.Length() + 4 + 1;
139 message
= (char *)nsMemory::Alloc(messageLen
);
142 return NS_ERROR_OUT_OF_MEMORY
;
144 message
[0] = 0x01; // No security layer
147 message
[3] = 0x00; // Maxbuf must be zero if we've got no sec layer
148 strcpy(message
+4, userbuf
.get());
149 // Userbuf should not be NULL terminated, so trim the trailing NULL
150 // when wrapping the message
151 rv
= mInnerModule
->Wrap((void *) message
, messageLen
-1, PR_FALSE
,
152 outToken
, outTokenLen
);
153 nsMemory::Free(message
);
155 return NS_SUCCEEDED(rv
) ? NS_SUCCESS_AUTH_FINISHED
: rv
;
157 rv
= mInnerModule
->GetNextToken(inToken
, inTokenLen
, outToken
,
159 if (rv
== NS_SUCCESS_AUTH_FINISHED
) {
167 nsAuthSASL::Unwrap(const void *inToken
,
170 PRUint32
*outTokenLen
)
172 return NS_ERROR_NOT_IMPLEMENTED
;
176 nsAuthSASL::Wrap(const void *inToken
,
180 PRUint32
*outTokenLen
)
182 return NS_ERROR_NOT_IMPLEMENTED
;