2013-06-01 21:31:17 +00:00
|
|
|
/* $OpenBSD: auth2-gss.c,v 1.20 2013/05/17 00:13:13 djm Exp $ */
|
2003-08-26 01:49:55 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Copyright (c) 2001-2003 Simon Wilkinson. All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AS IS'' AND ANY EXPRESS OR
|
|
|
|
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|
|
|
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|
|
|
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|
|
|
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|
|
|
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "includes.h"
|
|
|
|
|
2006-08-05 05:24:59 +00:00
|
|
|
#ifdef GSSAPI
|
|
|
|
|
2006-08-05 02:39:39 +00:00
|
|
|
#include <sys/types.h>
|
2003-08-26 01:49:55 +00:00
|
|
|
|
2006-09-01 05:38:36 +00:00
|
|
|
#include <stdarg.h>
|
|
|
|
|
2006-08-05 02:39:39 +00:00
|
|
|
#include "xmalloc.h"
|
|
|
|
#include "key.h"
|
|
|
|
#include "hostfile.h"
|
2003-08-26 01:49:55 +00:00
|
|
|
#include "auth.h"
|
|
|
|
#include "ssh2.h"
|
|
|
|
#include "log.h"
|
|
|
|
#include "dispatch.h"
|
2006-08-05 02:39:39 +00:00
|
|
|
#include "buffer.h"
|
2003-08-26 01:49:55 +00:00
|
|
|
#include "servconf.h"
|
|
|
|
#include "packet.h"
|
|
|
|
#include "ssh-gss.h"
|
2006-08-05 02:39:39 +00:00
|
|
|
#include "monitor_wrap.h"
|
2003-08-26 01:49:55 +00:00
|
|
|
|
|
|
|
extern ServerOptions options;
|
|
|
|
|
|
|
|
static void input_gssapi_token(int type, u_int32_t plen, void *ctxt);
|
2003-11-17 11:18:21 +00:00
|
|
|
static void input_gssapi_mic(int type, u_int32_t plen, void *ctxt);
|
2003-08-26 01:49:55 +00:00
|
|
|
static void input_gssapi_exchange_complete(int type, u_int32_t plen, void *ctxt);
|
|
|
|
static void input_gssapi_errtok(int, u_int32_t, void *);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* We only support those mechanisms that we know about (ie ones that we know
|
2005-11-05 04:07:05 +00:00
|
|
|
* how to check local user kuserok and the like)
|
2003-08-26 01:49:55 +00:00
|
|
|
*/
|
|
|
|
static int
|
|
|
|
userauth_gssapi(Authctxt *authctxt)
|
|
|
|
{
|
2004-06-22 02:56:01 +00:00
|
|
|
gss_OID_desc goid = {0, NULL};
|
2003-08-26 01:49:55 +00:00
|
|
|
Gssctxt *ctxt = NULL;
|
|
|
|
int mechs;
|
|
|
|
gss_OID_set supported;
|
|
|
|
int present;
|
|
|
|
OM_uint32 ms;
|
|
|
|
u_int len;
|
2005-06-17 02:59:34 +00:00
|
|
|
u_char *doid = NULL;
|
2003-08-26 01:49:55 +00:00
|
|
|
|
|
|
|
if (!authctxt->valid || authctxt->user == NULL)
|
|
|
|
return (0);
|
|
|
|
|
|
|
|
mechs = packet_get_int();
|
|
|
|
if (mechs == 0) {
|
|
|
|
debug("Mechanism negotiation is not supported");
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
ssh_gssapi_supported_oids(&supported);
|
|
|
|
do {
|
|
|
|
mechs--;
|
|
|
|
|
2013-06-01 21:31:17 +00:00
|
|
|
free(doid);
|
2003-08-26 01:49:55 +00:00
|
|
|
|
2003-11-03 09:09:03 +00:00
|
|
|
present = 0;
|
2003-08-26 01:49:55 +00:00
|
|
|
doid = packet_get_string(&len);
|
|
|
|
|
2005-07-17 07:22:45 +00:00
|
|
|
if (len > 2 && doid[0] == SSH_GSS_OIDTYPE &&
|
|
|
|
doid[1] == len - 2) {
|
2004-06-22 02:56:01 +00:00
|
|
|
goid.elements = doid + 2;
|
|
|
|
goid.length = len - 2;
|
|
|
|
gss_test_oid_set_member(&ms, &goid, supported,
|
2003-11-03 09:09:03 +00:00
|
|
|
&present);
|
2003-08-26 01:49:55 +00:00
|
|
|
} else {
|
2003-11-03 09:09:03 +00:00
|
|
|
logit("Badly formed OID received");
|
2003-08-26 01:49:55 +00:00
|
|
|
}
|
|
|
|
} while (mechs > 0 && !present);
|
|
|
|
|
|
|
|
gss_release_oid_set(&ms, &supported);
|
|
|
|
|
|
|
|
if (!present) {
|
2013-06-01 21:31:17 +00:00
|
|
|
free(doid);
|
2011-05-05 04:04:11 +00:00
|
|
|
authctxt->server_caused_failure = 1;
|
2003-08-26 01:49:55 +00:00
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
2004-06-22 02:56:01 +00:00
|
|
|
if (GSS_ERROR(PRIVSEP(ssh_gssapi_server_ctx(&ctxt, &goid)))) {
|
2006-03-25 13:04:53 +00:00
|
|
|
if (ctxt != NULL)
|
|
|
|
ssh_gssapi_delete_ctx(&ctxt);
|
2013-06-01 21:31:17 +00:00
|
|
|
free(doid);
|
2011-05-05 04:04:11 +00:00
|
|
|
authctxt->server_caused_failure = 1;
|
2003-08-26 01:49:55 +00:00
|
|
|
return (0);
|
2003-09-02 12:59:01 +00:00
|
|
|
}
|
2003-08-26 01:49:55 +00:00
|
|
|
|
2005-11-05 04:07:05 +00:00
|
|
|
authctxt->methoddata = (void *)ctxt;
|
2003-08-26 01:49:55 +00:00
|
|
|
|
|
|
|
packet_start(SSH2_MSG_USERAUTH_GSSAPI_RESPONSE);
|
|
|
|
|
2003-11-03 09:09:03 +00:00
|
|
|
/* Return the OID that we received */
|
2003-08-26 01:49:55 +00:00
|
|
|
packet_put_string(doid, len);
|
|
|
|
|
|
|
|
packet_send();
|
2013-06-01 21:31:17 +00:00
|
|
|
free(doid);
|
2003-08-26 01:49:55 +00:00
|
|
|
|
|
|
|
dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN, &input_gssapi_token);
|
|
|
|
dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_ERRTOK, &input_gssapi_errtok);
|
|
|
|
authctxt->postponed = 1;
|
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
input_gssapi_token(int type, u_int32_t plen, void *ctxt)
|
|
|
|
{
|
|
|
|
Authctxt *authctxt = ctxt;
|
|
|
|
Gssctxt *gssctxt;
|
|
|
|
gss_buffer_desc send_tok = GSS_C_EMPTY_BUFFER;
|
|
|
|
gss_buffer_desc recv_tok;
|
2003-11-17 11:18:21 +00:00
|
|
|
OM_uint32 maj_status, min_status, flags;
|
2003-08-26 01:49:55 +00:00
|
|
|
u_int len;
|
|
|
|
|
|
|
|
if (authctxt == NULL || (authctxt->methoddata == NULL && !use_privsep))
|
|
|
|
fatal("No authentication or GSSAPI context");
|
|
|
|
|
|
|
|
gssctxt = authctxt->methoddata;
|
|
|
|
recv_tok.value = packet_get_string(&len);
|
|
|
|
recv_tok.length = len; /* u_int vs. size_t */
|
|
|
|
|
|
|
|
packet_check_eom();
|
|
|
|
|
|
|
|
maj_status = PRIVSEP(ssh_gssapi_accept_ctx(gssctxt, &recv_tok,
|
2003-11-17 11:18:21 +00:00
|
|
|
&send_tok, &flags));
|
2003-08-26 01:49:55 +00:00
|
|
|
|
2013-06-01 21:31:17 +00:00
|
|
|
free(recv_tok.value);
|
2003-08-26 01:49:55 +00:00
|
|
|
|
|
|
|
if (GSS_ERROR(maj_status)) {
|
|
|
|
if (send_tok.length != 0) {
|
|
|
|
packet_start(SSH2_MSG_USERAUTH_GSSAPI_ERRTOK);
|
|
|
|
packet_put_string(send_tok.value, send_tok.length);
|
|
|
|
packet_send();
|
|
|
|
}
|
|
|
|
authctxt->postponed = 0;
|
|
|
|
dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN, NULL);
|
2012-12-02 22:53:20 +00:00
|
|
|
userauth_finish(authctxt, 0, "gssapi-with-mic", NULL);
|
2003-08-26 01:49:55 +00:00
|
|
|
} else {
|
|
|
|
if (send_tok.length != 0) {
|
|
|
|
packet_start(SSH2_MSG_USERAUTH_GSSAPI_TOKEN);
|
|
|
|
packet_put_string(send_tok.value, send_tok.length);
|
|
|
|
packet_send();
|
|
|
|
}
|
|
|
|
if (maj_status == GSS_S_COMPLETE) {
|
|
|
|
dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN, NULL);
|
2003-11-17 11:18:21 +00:00
|
|
|
if (flags & GSS_C_INTEG_FLAG)
|
|
|
|
dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_MIC,
|
|
|
|
&input_gssapi_mic);
|
|
|
|
else
|
|
|
|
dispatch_set(
|
|
|
|
SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE,
|
|
|
|
&input_gssapi_exchange_complete);
|
2003-08-26 01:49:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
gss_release_buffer(&min_status, &send_tok);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
input_gssapi_errtok(int type, u_int32_t plen, void *ctxt)
|
|
|
|
{
|
|
|
|
Authctxt *authctxt = ctxt;
|
|
|
|
Gssctxt *gssctxt;
|
|
|
|
gss_buffer_desc send_tok = GSS_C_EMPTY_BUFFER;
|
|
|
|
gss_buffer_desc recv_tok;
|
|
|
|
OM_uint32 maj_status;
|
2003-09-02 12:14:07 +00:00
|
|
|
u_int len;
|
2003-08-26 01:49:55 +00:00
|
|
|
|
|
|
|
if (authctxt == NULL || (authctxt->methoddata == NULL && !use_privsep))
|
|
|
|
fatal("No authentication or GSSAPI context");
|
|
|
|
|
|
|
|
gssctxt = authctxt->methoddata;
|
2003-09-02 12:14:07 +00:00
|
|
|
recv_tok.value = packet_get_string(&len);
|
|
|
|
recv_tok.length = len;
|
2003-08-26 01:49:55 +00:00
|
|
|
|
|
|
|
packet_check_eom();
|
|
|
|
|
|
|
|
/* Push the error token into GSSAPI to see what it says */
|
|
|
|
maj_status = PRIVSEP(ssh_gssapi_accept_ctx(gssctxt, &recv_tok,
|
|
|
|
&send_tok, NULL));
|
|
|
|
|
2013-06-01 21:31:17 +00:00
|
|
|
free(recv_tok.value);
|
2003-08-26 01:49:55 +00:00
|
|
|
|
|
|
|
/* We can't return anything to the client, even if we wanted to */
|
|
|
|
dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN, NULL);
|
|
|
|
dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_ERRTOK, NULL);
|
|
|
|
|
|
|
|
/* The client will have already moved on to the next auth */
|
|
|
|
|
|
|
|
gss_release_buffer(&maj_status, &send_tok);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This is called when the client thinks we've completed authentication.
|
|
|
|
* It should only be enabled in the dispatch handler by the function above,
|
|
|
|
* which only enables it once the GSSAPI exchange is complete.
|
|
|
|
*/
|
|
|
|
|
|
|
|
static void
|
|
|
|
input_gssapi_exchange_complete(int type, u_int32_t plen, void *ctxt)
|
|
|
|
{
|
|
|
|
Authctxt *authctxt = ctxt;
|
|
|
|
int authenticated;
|
|
|
|
|
|
|
|
if (authctxt == NULL || (authctxt->methoddata == NULL && !use_privsep))
|
|
|
|
fatal("No authentication or GSSAPI context");
|
|
|
|
|
|
|
|
/*
|
2003-11-17 11:18:21 +00:00
|
|
|
* We don't need to check the status, because we're only enabled in
|
|
|
|
* the dispatcher once the exchange is complete
|
2003-08-26 01:49:55 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
packet_check_eom();
|
|
|
|
|
|
|
|
authenticated = PRIVSEP(ssh_gssapi_userok(authctxt->user));
|
|
|
|
|
|
|
|
authctxt->postponed = 0;
|
|
|
|
dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN, NULL);
|
|
|
|
dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_ERRTOK, NULL);
|
2003-11-17 11:18:21 +00:00
|
|
|
dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_MIC, NULL);
|
|
|
|
dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE, NULL);
|
2012-12-02 22:53:20 +00:00
|
|
|
userauth_finish(authctxt, authenticated, "gssapi-with-mic", NULL);
|
2003-11-17 11:18:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
input_gssapi_mic(int type, u_int32_t plen, void *ctxt)
|
|
|
|
{
|
|
|
|
Authctxt *authctxt = ctxt;
|
|
|
|
Gssctxt *gssctxt;
|
|
|
|
int authenticated = 0;
|
|
|
|
Buffer b;
|
|
|
|
gss_buffer_desc mic, gssbuf;
|
|
|
|
u_int len;
|
2003-11-21 12:56:47 +00:00
|
|
|
|
2003-11-17 11:18:21 +00:00
|
|
|
if (authctxt == NULL || (authctxt->methoddata == NULL && !use_privsep))
|
|
|
|
fatal("No authentication or GSSAPI context");
|
2003-11-21 12:56:47 +00:00
|
|
|
|
2003-11-17 11:18:21 +00:00
|
|
|
gssctxt = authctxt->methoddata;
|
2003-11-21 12:56:47 +00:00
|
|
|
|
2003-11-17 11:18:21 +00:00
|
|
|
mic.value = packet_get_string(&len);
|
|
|
|
mic.length = len;
|
2003-11-21 12:56:47 +00:00
|
|
|
|
2003-11-17 11:18:21 +00:00
|
|
|
ssh_gssapi_buildmic(&b, authctxt->user, authctxt->service,
|
|
|
|
"gssapi-with-mic");
|
2003-11-21 12:56:47 +00:00
|
|
|
|
2003-11-17 11:18:21 +00:00
|
|
|
gssbuf.value = buffer_ptr(&b);
|
|
|
|
gssbuf.length = buffer_len(&b);
|
2003-11-21 12:56:47 +00:00
|
|
|
|
2003-11-17 11:18:21 +00:00
|
|
|
if (!GSS_ERROR(PRIVSEP(ssh_gssapi_checkmic(gssctxt, &gssbuf, &mic))))
|
|
|
|
authenticated = PRIVSEP(ssh_gssapi_userok(authctxt->user));
|
|
|
|
else
|
|
|
|
logit("GSSAPI MIC check failed");
|
|
|
|
|
|
|
|
buffer_free(&b);
|
2013-06-01 21:31:17 +00:00
|
|
|
free(mic.value);
|
2003-11-21 12:56:47 +00:00
|
|
|
|
2003-11-17 11:18:21 +00:00
|
|
|
authctxt->postponed = 0;
|
|
|
|
dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN, NULL);
|
|
|
|
dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_ERRTOK, NULL);
|
|
|
|
dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_MIC, NULL);
|
2003-08-26 01:49:55 +00:00
|
|
|
dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE, NULL);
|
2012-12-02 22:53:20 +00:00
|
|
|
userauth_finish(authctxt, authenticated, "gssapi-with-mic", NULL);
|
2003-08-26 01:49:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Authmethod method_gssapi = {
|
2003-11-17 11:18:21 +00:00
|
|
|
"gssapi-with-mic",
|
2003-08-26 01:49:55 +00:00
|
|
|
userauth_gssapi,
|
|
|
|
&options.gss_authentication
|
|
|
|
};
|
2006-08-05 05:24:59 +00:00
|
|
|
|
|
|
|
#endif /* GSSAPI */
|