1 /* $OpenBSD: auth2-gss.c,v 1.15 2006/08/03 03:34:41 deraadt Exp $ */
4 * Copyright (c) 2001-2003 Simon Wilkinson. All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 #include <sys/types.h>
46 #include "monitor_wrap.h"
48 extern ServerOptions options;
50 static void ssh_gssapi_userauth_error(Gssctxt *ctxt);
51 static void input_gssapi_token(int type, u_int32_t plen, void *ctxt);
52 static void input_gssapi_mic(int type, u_int32_t plen, void *ctxt);
53 static void input_gssapi_exchange_complete(int type, u_int32_t plen, void *ctxt);
54 static void input_gssapi_errtok(int, u_int32_t, void *);
56 static int gssapi_with_mic = 1; /* flag to toggle "gssapi-with-mic" vs.
60 userauth_external(Authctxt *authctxt)
64 if (authctxt->valid && authctxt->user && authctxt->user[0]) {
65 return(PRIVSEP(ssh_gssapi_userok(authctxt->user)));
71 * The 'gssapi_keyex' userauth mechanism.
74 userauth_gsskeyex(Authctxt *authctxt)
76 int authenticated = 0;
78 gss_buffer_desc mic, gssbuf, gssbuf2;
81 mic.value = packet_get_string(&len);
86 ssh_gssapi_buildmic(&b, authctxt->user, authctxt->service,
89 gssbuf.value = buffer_ptr(&b);
90 gssbuf.length = buffer_len(&b);
92 /* client may have used empty username to determine target
93 name from GSSAPI context */
94 ssh_gssapi_buildmic(&b2, "", authctxt->service, "gssapi-keyex");
96 gssbuf2.value = buffer_ptr(&b2);
97 gssbuf2.length = buffer_len(&b2);
99 /* gss_kex_context is NULL with privsep, so we can't check it here */
100 if (!GSS_ERROR(PRIVSEP(ssh_gssapi_checkmic(gss_kex_context,
102 !GSS_ERROR(PRIVSEP(ssh_gssapi_checkmic(gss_kex_context,
104 if (authctxt->valid && authctxt->user && authctxt->user[0]) {
105 authenticated = PRIVSEP(ssh_gssapi_userok(authctxt->user));
113 return (authenticated);
117 * We only support those mechanisms that we know about (ie ones that we know
118 * how to check local user kuserok and the like)
121 userauth_gssapi(Authctxt *authctxt)
123 gss_OID_desc goid = {0, NULL};
124 Gssctxt *ctxt = NULL;
126 gss_OID_set supported;
132 /* authctxt->valid may be 0 if we haven't yet determined
133 username from gssapi context. */
135 if (authctxt->user == NULL)
138 mechs = packet_get_int();
140 debug("Mechanism negotiation is not supported");
144 ssh_gssapi_supported_oids(&supported);
152 doid = packet_get_string(&len);
154 if (len > 2 && doid[0] == SSH_GSS_OIDTYPE &&
155 doid[1] == len - 2) {
156 goid.elements = doid + 2;
157 goid.length = len - 2;
158 gss_test_oid_set_member(&ms, &goid, supported,
161 logit("Badly formed OID received");
163 } while (mechs > 0 && !present);
165 gss_release_oid_set(&ms, &supported);
169 authctxt->server_caused_failure = 1;
173 if (GSS_ERROR(PRIVSEP(ssh_gssapi_server_ctx(&ctxt, &goid)))) {
175 ssh_gssapi_delete_ctx(&ctxt);
177 authctxt->server_caused_failure = 1;
181 authctxt->methoddata = (void *)ctxt;
183 packet_start(SSH2_MSG_USERAUTH_GSSAPI_RESPONSE);
185 /* Return the OID that we received */
186 packet_put_string(doid, len);
191 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN, &input_gssapi_token);
192 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_ERRTOK, &input_gssapi_errtok);
193 authctxt->postponed = 1;
199 input_gssapi_token(int type, u_int32_t plen, void *ctxt)
201 Authctxt *authctxt = ctxt;
203 gss_buffer_desc send_tok = GSS_C_EMPTY_BUFFER;
204 gss_buffer_desc recv_tok;
205 OM_uint32 maj_status, min_status, flags=0;
208 if (authctxt == NULL || (authctxt->methoddata == NULL && !use_privsep))
209 fatal("No authentication or GSSAPI context");
211 gssctxt = authctxt->methoddata;
212 recv_tok.value = packet_get_string(&len);
213 recv_tok.length = len; /* u_int vs. size_t */
217 maj_status = PRIVSEP(ssh_gssapi_accept_ctx(gssctxt, &recv_tok,
220 xfree(recv_tok.value);
222 if (GSS_ERROR(maj_status)) {
223 ssh_gssapi_userauth_error(gssctxt);
224 if (send_tok.length != 0) {
225 packet_start(SSH2_MSG_USERAUTH_GSSAPI_ERRTOK);
226 packet_put_string(send_tok.value, send_tok.length);
229 authctxt->postponed = 0;
230 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN, NULL);
231 userauth_finish(authctxt, 0,
232 gssapi_with_mic ? "gssapi-with-mic" :
235 if (send_tok.length != 0) {
236 packet_start(SSH2_MSG_USERAUTH_GSSAPI_TOKEN);
237 packet_put_string(send_tok.value, send_tok.length);
240 if (maj_status == GSS_S_COMPLETE) {
241 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN, NULL);
242 if (flags & GSS_C_INTEG_FLAG && gssapi_with_mic)
243 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_MIC,
247 SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE,
248 &input_gssapi_exchange_complete);
252 gss_release_buffer(&min_status, &send_tok);
256 input_gssapi_errtok(int type, u_int32_t plen, void *ctxt)
258 Authctxt *authctxt = ctxt;
260 gss_buffer_desc send_tok = GSS_C_EMPTY_BUFFER;
261 gss_buffer_desc recv_tok;
262 OM_uint32 maj_status;
265 if (authctxt == NULL || (authctxt->methoddata == NULL && !use_privsep))
266 fatal("No authentication or GSSAPI context");
268 gssctxt = authctxt->methoddata;
269 recv_tok.value = packet_get_string(&len);
270 recv_tok.length = len;
274 /* Push the error token into GSSAPI to see what it says */
275 maj_status = PRIVSEP(ssh_gssapi_accept_ctx(gssctxt, &recv_tok,
278 xfree(recv_tok.value);
280 /* We can't return anything to the client, even if we wanted to */
281 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN, NULL);
282 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_ERRTOK, NULL);
284 /* The client will have already moved on to the next auth */
286 gss_release_buffer(&maj_status, &send_tok);
290 gssapi_set_implicit_username(Authctxt *authctxt)
292 if ((authctxt->user == NULL) || (authctxt->user[0] == '\0')) {
294 PRIVSEP(ssh_gssapi_localname(&lname));
295 if (lname && lname[0] != '\0') {
296 if (authctxt->user) xfree(authctxt->user);
297 authctxt->user = lname;
298 debug("set username to %s from gssapi context", lname);
299 authctxt->pw = PRIVSEP(getpwnamallow(authctxt->user));
304 debug("failed to set username from gssapi context");
305 packet_send_debug("failed to set username from gssapi context");
311 PRIVSEP(start_pam(authctxt));
317 * This is called when the client thinks we've completed authentication.
318 * It should only be enabled in the dispatch handler by the function above,
319 * which only enables it once the GSSAPI exchange is complete.
323 input_gssapi_exchange_complete(int type, u_int32_t plen, void *ctxt)
325 Authctxt *authctxt = ctxt;
329 if (authctxt == NULL || (authctxt->methoddata == NULL && !use_privsep))
330 fatal("No authentication or GSSAPI context");
332 gssapi_set_implicit_username(authctxt);
334 gssctxt = authctxt->methoddata;
337 * We don't need to check the status, because we're only enabled in
338 * the dispatcher once the exchange is complete
343 /* user should be set if valid but we double-check here */
344 if (authctxt->valid && authctxt->user && authctxt->user[0]) {
345 authenticated = PRIVSEP(ssh_gssapi_userok(authctxt->user));
350 authctxt->postponed = 0;
351 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN, NULL);
352 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_ERRTOK, NULL);
353 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_MIC, NULL);
354 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE, NULL);
355 userauth_finish(authctxt, authenticated,
356 gssapi_with_mic ? "gssapi-with-mic" : "gssapi");
360 userauth_gssapi_with_mic(Authctxt *authctxt)
363 return userauth_gssapi(authctxt);
367 userauth_gssapi_without_mic(Authctxt *authctxt)
370 return userauth_gssapi(authctxt);
374 input_gssapi_mic(int type, u_int32_t plen, void *ctxt)
376 Authctxt *authctxt = ctxt;
378 int authenticated = 0;
380 gss_buffer_desc mic, gssbuf;
383 if (authctxt == NULL || (authctxt->methoddata == NULL && !use_privsep))
384 fatal("No authentication or GSSAPI context");
386 gssapi_set_implicit_username(authctxt);
388 gssctxt = authctxt->methoddata;
390 mic.value = packet_get_string(&len);
393 ssh_gssapi_buildmic(&b, authctxt->user, authctxt->service,
396 gssbuf.value = buffer_ptr(&b);
397 gssbuf.length = buffer_len(&b);
399 if (!GSS_ERROR(PRIVSEP(ssh_gssapi_checkmic(gssctxt, &gssbuf, &mic))))
400 if (authctxt->valid && authctxt->user && authctxt->user[0]) {
401 authenticated = PRIVSEP(ssh_gssapi_userok(authctxt->user));
406 logit("GSSAPI MIC check failed");
411 authctxt->postponed = 0;
412 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN, NULL);
413 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_ERRTOK, NULL);
414 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_MIC, NULL);
415 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE, NULL);
416 userauth_finish(authctxt, authenticated, "gssapi-with-mic");
419 static void ssh_gssapi_userauth_error(Gssctxt *ctxt) {
423 errstr=PRIVSEP(ssh_gssapi_last_error(ctxt,&maj,&min));
425 packet_start(SSH2_MSG_USERAUTH_GSSAPI_ERROR);
428 packet_put_cstring(errstr);
429 packet_put_cstring("");
436 Authmethod method_external = {
439 &options.gss_authentication
442 Authmethod method_gsskeyex = {
445 &options.gss_authentication
448 Authmethod method_gssapi = {
450 userauth_gssapi_with_mic,
451 &options.gss_authentication
454 Authmethod method_gssapi_compat = {
456 userauth_gssapi_without_mic,
457 &options.gss_authentication