1 /* $OpenBSD: auth2-gss.c,v 1.16 2007/10/29 00:52:45 dtucker Exp $ */
4 * Copyright (c) 2001-2007 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 *);
57 * The 'gssapi_keyex' userauth mechanism.
60 userauth_gsskeyex(Authctxt *authctxt)
62 int authenticated = 0;
64 gss_buffer_desc mic, gssbuf, gssbuf2;
67 mic.value = packet_get_string(&len);
72 ssh_gssapi_buildmic(&b, authctxt->user, authctxt->service,
75 gssbuf.value = buffer_ptr(&b);
76 gssbuf.length = buffer_len(&b);
78 /* client may have used empty username to determine target
79 name from GSSAPI context */
80 ssh_gssapi_buildmic(&b2, "", authctxt->service, "gssapi-keyex");
82 gssbuf2.value = buffer_ptr(&b2);
83 gssbuf2.length = buffer_len(&b2);
85 /* gss_kex_context is NULL with privsep, so we can't check it here */
86 if (!GSS_ERROR(PRIVSEP(ssh_gssapi_checkmic(gss_kex_context,
88 !GSS_ERROR(PRIVSEP(ssh_gssapi_checkmic(gss_kex_context,
90 if (authctxt->valid && authctxt->user && authctxt->user[0]) {
91 authenticated = PRIVSEP(ssh_gssapi_userok(authctxt->user,
93 1 /* gssapi-keyex */));
101 return (authenticated);
105 * We only support those mechanisms that we know about (ie ones that we know
106 * how to check local user kuserok and the like)
109 userauth_gssapi(Authctxt *authctxt)
111 gss_OID_desc goid = {0, NULL};
112 Gssctxt *ctxt = NULL;
114 gss_OID_set supported;
120 /* authctxt->valid may be 0 if we haven't yet determined
121 username from gssapi context. */
123 if (authctxt->user == NULL)
126 mechs = packet_get_int();
128 debug("Mechanism negotiation is not supported");
132 ssh_gssapi_supported_oids(&supported);
140 doid = packet_get_string(&len);
142 if (len > 2 && doid[0] == SSH_GSS_OIDTYPE &&
143 doid[1] == len - 2) {
144 goid.elements = doid + 2;
145 goid.length = len - 2;
146 gss_test_oid_set_member(&ms, &goid, supported,
149 logit("Badly formed OID received");
151 } while (mechs > 0 && !present);
153 gss_release_oid_set(&ms, &supported);
157 authctxt->server_caused_failure = 1;
161 if (GSS_ERROR(PRIVSEP(ssh_gssapi_server_ctx(&ctxt, &goid)))) {
163 ssh_gssapi_delete_ctx(&ctxt);
165 authctxt->server_caused_failure = 1;
169 authctxt->methoddata = (void *)ctxt;
171 packet_start(SSH2_MSG_USERAUTH_GSSAPI_RESPONSE);
173 /* Return the OID that we received */
174 packet_put_string(doid, len);
179 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN, &input_gssapi_token);
180 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_ERRTOK, &input_gssapi_errtok);
181 authctxt->postponed = 1;
187 input_gssapi_token(int type, u_int32_t plen, void *ctxt)
189 Authctxt *authctxt = ctxt;
191 gss_buffer_desc send_tok = GSS_C_EMPTY_BUFFER;
192 gss_buffer_desc recv_tok;
193 OM_uint32 maj_status, min_status, flags=0;
196 if (authctxt == NULL || (authctxt->methoddata == NULL && !use_privsep))
197 fatal("No authentication or GSSAPI context");
199 gssctxt = authctxt->methoddata;
200 recv_tok.value = packet_get_string(&len);
201 recv_tok.length = len; /* u_int vs. size_t */
205 maj_status = PRIVSEP(ssh_gssapi_accept_ctx(gssctxt, &recv_tok,
208 xfree(recv_tok.value);
210 if (GSS_ERROR(maj_status)) {
211 ssh_gssapi_userauth_error(gssctxt);
212 if (send_tok.length != 0) {
213 packet_start(SSH2_MSG_USERAUTH_GSSAPI_ERRTOK);
214 packet_put_string(send_tok.value, send_tok.length);
217 authctxt->postponed = 0;
218 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN, NULL);
219 userauth_finish(authctxt, 0, "gssapi-with-mic");
221 if (send_tok.length != 0) {
222 packet_start(SSH2_MSG_USERAUTH_GSSAPI_TOKEN);
223 packet_put_string(send_tok.value, send_tok.length);
226 if (maj_status == GSS_S_COMPLETE) {
227 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN, NULL);
228 if (flags & GSS_C_INTEG_FLAG)
229 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_MIC,
233 SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE,
234 &input_gssapi_exchange_complete);
238 gss_release_buffer(&min_status, &send_tok);
242 input_gssapi_errtok(int type, u_int32_t plen, void *ctxt)
244 Authctxt *authctxt = ctxt;
246 gss_buffer_desc send_tok = GSS_C_EMPTY_BUFFER;
247 gss_buffer_desc recv_tok;
248 OM_uint32 maj_status;
251 if (authctxt == NULL || (authctxt->methoddata == NULL && !use_privsep))
252 fatal("No authentication or GSSAPI context");
254 gssctxt = authctxt->methoddata;
255 recv_tok.value = packet_get_string(&len);
256 recv_tok.length = len;
260 /* Push the error token into GSSAPI to see what it says */
261 maj_status = PRIVSEP(ssh_gssapi_accept_ctx(gssctxt, &recv_tok,
264 xfree(recv_tok.value);
266 /* We can't return anything to the client, even if we wanted to */
267 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN, NULL);
268 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_ERRTOK, NULL);
270 /* The client will have already moved on to the next auth */
272 gss_release_buffer(&maj_status, &send_tok);
276 gssapi_set_username(Authctxt *authctxt)
280 if ((authctxt->user == NULL) || (authctxt->user[0] == '\0')) {
281 PRIVSEP(ssh_gssapi_localname(&lname));
282 if (lname && lname[0] != '\0') {
283 if (authctxt->user) xfree(authctxt->user);
284 authctxt->user = lname;
285 debug("set username to %s from gssapi context", lname);
286 authctxt->pw = PRIVSEP(getpwnamallow(authctxt->user));
291 PRIVSEP(start_pam(authctxt));
295 debug("failed to set username from gssapi context");
296 packet_send_debug("failed to set username from gssapi context");
302 * This is called when the client thinks we've completed authentication.
303 * It should only be enabled in the dispatch handler by the function above,
304 * which only enables it once the GSSAPI exchange is complete.
308 input_gssapi_exchange_complete(int type, u_int32_t plen, void *ctxt)
310 Authctxt *authctxt = ctxt;
314 if (authctxt == NULL || (authctxt->methoddata == NULL && !use_privsep))
315 fatal("No authentication or GSSAPI context");
317 gssapi_set_username(authctxt);
319 gssctxt = authctxt->methoddata;
322 * We don't need to check the status, because we're only enabled in
323 * the dispatcher once the exchange is complete
328 /* user should be set if valid but we double-check here */
329 if (authctxt->valid && authctxt->user && authctxt->user[0]) {
330 authenticated = PRIVSEP(ssh_gssapi_userok(authctxt->user,
331 authctxt->pw, 0 /* !gssapi-keyex */));
336 authctxt->postponed = 0;
337 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN, NULL);
338 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_ERRTOK, NULL);
339 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_MIC, NULL);
340 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE, NULL);
341 userauth_finish(authctxt, authenticated, "gssapi-with-mic");
345 input_gssapi_mic(int type, u_int32_t plen, void *ctxt)
347 Authctxt *authctxt = ctxt;
349 int authenticated = 0;
351 gss_buffer_desc mic, gssbuf;
354 if (authctxt == NULL || (authctxt->methoddata == NULL && !use_privsep))
355 fatal("No authentication or GSSAPI context");
357 gssctxt = authctxt->methoddata;
359 mic.value = packet_get_string(&len);
362 ssh_gssapi_buildmic(&b, authctxt->user, authctxt->service,
365 gssbuf.value = buffer_ptr(&b);
366 gssbuf.length = buffer_len(&b);
368 gssapi_set_username(authctxt);
370 if (!GSS_ERROR(PRIVSEP(ssh_gssapi_checkmic(gssctxt, &gssbuf, &mic))))
371 if (authctxt->valid && authctxt->user && authctxt->user[0]) {
373 PRIVSEP(ssh_gssapi_userok(authctxt->user, authctxt->pw,
374 0 /* !gssapi-keyex */));
379 logit("GSSAPI MIC check failed");
384 authctxt->postponed = 0;
385 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN, NULL);
386 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_ERRTOK, NULL);
387 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_MIC, NULL);
388 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE, NULL);
389 userauth_finish(authctxt, authenticated, "gssapi-with-mic");
392 static void ssh_gssapi_userauth_error(Gssctxt *ctxt) {
396 errstr=PRIVSEP(ssh_gssapi_last_error(ctxt,&maj,&min));
398 packet_start(SSH2_MSG_USERAUTH_GSSAPI_ERROR);
401 packet_put_cstring(errstr);
402 packet_put_cstring("");
409 Authmethod method_gsskeyex = {
412 &options.gss_authentication
415 Authmethod method_gssapi = {
418 &options.gss_authentication