1 /* $OpenBSD: auth2-gss.c,v 1.17 2011/03/10 02:52:57 djm 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,
100 return (authenticated);
104 * We only support those mechanisms that we know about (ie ones that we know
105 * how to check local user kuserok and the like)
108 userauth_gssapi(Authctxt *authctxt)
110 gss_OID_desc goid = {0, NULL};
111 Gssctxt *ctxt = NULL;
113 gss_OID_set supported;
119 /* authctxt->valid may be 0 if we haven't yet determined
120 username from gssapi context. */
122 if (authctxt->user == NULL)
125 mechs = packet_get_int();
127 debug("Mechanism negotiation is not supported");
131 ssh_gssapi_supported_oids(&supported);
139 doid = packet_get_string(&len);
141 if (len > 2 && doid[0] == SSH_GSS_OIDTYPE &&
142 doid[1] == len - 2) {
143 goid.elements = doid + 2;
144 goid.length = len - 2;
145 gss_test_oid_set_member(&ms, &goid, supported,
148 logit("Badly formed OID received");
150 } while (mechs > 0 && !present);
152 gss_release_oid_set(&ms, &supported);
156 authctxt->server_caused_failure = 1;
160 if (GSS_ERROR(PRIVSEP(ssh_gssapi_server_ctx(&ctxt, &goid)))) {
162 ssh_gssapi_delete_ctx(&ctxt);
164 authctxt->server_caused_failure = 1;
168 authctxt->methoddata = (void *)ctxt;
170 packet_start(SSH2_MSG_USERAUTH_GSSAPI_RESPONSE);
172 /* Return the OID that we received */
173 packet_put_string(doid, len);
178 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN, &input_gssapi_token);
179 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_ERRTOK, &input_gssapi_errtok);
180 authctxt->postponed = 1;
186 input_gssapi_token(int type, u_int32_t plen, void *ctxt)
188 Authctxt *authctxt = ctxt;
190 gss_buffer_desc send_tok = GSS_C_EMPTY_BUFFER;
191 gss_buffer_desc recv_tok;
192 OM_uint32 maj_status, min_status, flags=0;
195 if (authctxt == NULL || (authctxt->methoddata == NULL && !use_privsep))
196 fatal("No authentication or GSSAPI context");
198 gssctxt = authctxt->methoddata;
199 recv_tok.value = packet_get_string(&len);
200 recv_tok.length = len; /* u_int vs. size_t */
204 maj_status = PRIVSEP(ssh_gssapi_accept_ctx(gssctxt, &recv_tok,
207 xfree(recv_tok.value);
209 if (GSS_ERROR(maj_status)) {
210 ssh_gssapi_userauth_error(gssctxt);
211 if (send_tok.length != 0) {
212 packet_start(SSH2_MSG_USERAUTH_GSSAPI_ERRTOK);
213 packet_put_string(send_tok.value, send_tok.length);
216 authctxt->postponed = 0;
217 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN, NULL);
218 userauth_finish(authctxt, 0, "gssapi-with-mic");
220 if (send_tok.length != 0) {
221 packet_start(SSH2_MSG_USERAUTH_GSSAPI_TOKEN);
222 packet_put_string(send_tok.value, send_tok.length);
225 if (maj_status == GSS_S_COMPLETE) {
226 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN, NULL);
227 if (flags & GSS_C_INTEG_FLAG)
228 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_MIC,
232 SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE,
233 &input_gssapi_exchange_complete);
237 gss_release_buffer(&min_status, &send_tok);
241 input_gssapi_errtok(int type, u_int32_t plen, void *ctxt)
243 Authctxt *authctxt = ctxt;
245 gss_buffer_desc send_tok = GSS_C_EMPTY_BUFFER;
246 gss_buffer_desc recv_tok;
247 OM_uint32 maj_status;
250 if (authctxt == NULL || (authctxt->methoddata == NULL && !use_privsep))
251 fatal("No authentication or GSSAPI context");
253 gssctxt = authctxt->methoddata;
254 recv_tok.value = packet_get_string(&len);
255 recv_tok.length = len;
259 /* Push the error token into GSSAPI to see what it says */
260 maj_status = PRIVSEP(ssh_gssapi_accept_ctx(gssctxt, &recv_tok,
263 xfree(recv_tok.value);
265 /* We can't return anything to the client, even if we wanted to */
266 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN, NULL);
267 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_ERRTOK, NULL);
269 /* The client will have already moved on to the next auth */
271 gss_release_buffer(&maj_status, &send_tok);
275 gssapi_set_username(Authctxt *authctxt)
279 if ((authctxt->user == NULL) || (authctxt->user[0] == '\0')) {
280 PRIVSEP(ssh_gssapi_localname(&lname));
281 if (lname && lname[0] != '\0') {
282 if (authctxt->user) xfree(authctxt->user);
283 authctxt->user = lname;
284 debug("set username to %s from gssapi context", lname);
285 authctxt->pw = PRIVSEP(getpwnamallow(authctxt->user));
290 PRIVSEP(start_pam(authctxt));
294 debug("failed to set username from gssapi context");
295 packet_send_debug("failed to set username from gssapi context");
301 * This is called when the client thinks we've completed authentication.
302 * It should only be enabled in the dispatch handler by the function above,
303 * which only enables it once the GSSAPI exchange is complete.
307 input_gssapi_exchange_complete(int type, u_int32_t plen, void *ctxt)
309 Authctxt *authctxt = ctxt;
313 if (authctxt == NULL || (authctxt->methoddata == NULL && !use_privsep))
314 fatal("No authentication or GSSAPI context");
316 gssapi_set_username(authctxt);
318 gssctxt = authctxt->methoddata;
321 * We don't need to check the status, because we're only enabled in
322 * the dispatcher once the exchange is complete
327 /* user should be set if valid but we double-check here */
328 if (authctxt->valid && authctxt->user && authctxt->user[0]) {
329 authenticated = PRIVSEP(ssh_gssapi_userok(authctxt->user,
335 authctxt->postponed = 0;
336 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN, NULL);
337 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_ERRTOK, NULL);
338 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_MIC, NULL);
339 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE, NULL);
340 userauth_finish(authctxt, authenticated, "gssapi-with-mic");
344 input_gssapi_mic(int type, u_int32_t plen, void *ctxt)
346 Authctxt *authctxt = ctxt;
348 int authenticated = 0;
350 gss_buffer_desc mic, gssbuf;
353 if (authctxt == NULL || (authctxt->methoddata == NULL && !use_privsep))
354 fatal("No authentication or GSSAPI context");
356 gssctxt = authctxt->methoddata;
358 mic.value = packet_get_string(&len);
361 ssh_gssapi_buildmic(&b, authctxt->user, authctxt->service,
364 gssbuf.value = buffer_ptr(&b);
365 gssbuf.length = buffer_len(&b);
367 gssapi_set_username(authctxt);
369 if (!GSS_ERROR(PRIVSEP(ssh_gssapi_checkmic(gssctxt, &gssbuf, &mic))))
370 if (authctxt->valid && authctxt->user && authctxt->user[0]) {
372 PRIVSEP(ssh_gssapi_userok(authctxt->user, authctxt->pw));
377 logit("GSSAPI MIC check failed");
382 authctxt->postponed = 0;
383 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN, NULL);
384 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_ERRTOK, NULL);
385 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_MIC, NULL);
386 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE, NULL);
387 userauth_finish(authctxt, authenticated, "gssapi-with-mic");
390 static void ssh_gssapi_userauth_error(Gssctxt *ctxt) {
394 errstr=PRIVSEP(ssh_gssapi_last_error(ctxt,&maj,&min));
396 packet_start(SSH2_MSG_USERAUTH_GSSAPI_ERROR);
399 packet_put_cstring(errstr);
400 packet_put_cstring("");
407 Authmethod method_gsskeyex = {
410 &options.gss_authentication
413 Authmethod method_gssapi = {
416 &options.gss_authentication