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 input_gssapi_token(int type, u_int32_t plen, void *ctxt);
51 static void input_gssapi_mic(int type, u_int32_t plen, void *ctxt);
52 static void input_gssapi_exchange_complete(int type, u_int32_t plen, void *ctxt);
53 static void input_gssapi_errtok(int, u_int32_t, void *);
54 static void gssapi_set_username(Authctxt *authctxt);
57 * The 'gssapi_keyex' userauth mechanism.
60 userauth_gsskeyex(Authctxt *authctxt)
62 int authenticated = 0;
64 gss_buffer_desc mic, gssbuf;
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 gssapi_set_username(authctxt);
80 /* gss_kex_context is NULL with privsep, so we can't check it here */
81 if (!GSS_ERROR(PRIVSEP(ssh_gssapi_checkmic(gss_kex_context,
83 authenticated = PRIVSEP(ssh_gssapi_userok(authctxt->user,
89 return (authenticated);
93 * We only support those mechanisms that we know about (ie ones that we know
94 * how to check local user kuserok and the like)
97 userauth_gssapi(Authctxt *authctxt)
99 gss_OID_desc goid = {0, NULL};
100 Gssctxt *ctxt = NULL;
102 gss_OID_set supported;
108 /* authctxt->valid may be 0 if we haven't yet determined
109 username from gssapi context. */
111 if (authctxt->user == NULL)
114 mechs = packet_get_int();
116 debug("Mechanism negotiation is not supported");
120 ssh_gssapi_supported_oids(&supported);
128 doid = packet_get_string(&len);
130 if (len > 2 && doid[0] == SSH_GSS_OIDTYPE &&
131 doid[1] == len - 2) {
132 goid.elements = doid + 2;
133 goid.length = len - 2;
134 gss_test_oid_set_member(&ms, &goid, supported,
137 logit("Badly formed OID received");
139 } while (mechs > 0 && !present);
141 gss_release_oid_set(&ms, &supported);
145 authctxt->server_caused_failure = 1;
149 if (GSS_ERROR(PRIVSEP(ssh_gssapi_server_ctx(&ctxt, &goid)))) {
151 ssh_gssapi_delete_ctx(&ctxt);
153 authctxt->server_caused_failure = 1;
157 authctxt->methoddata = (void *)ctxt;
159 packet_start(SSH2_MSG_USERAUTH_GSSAPI_RESPONSE);
161 /* Return the OID that we received */
162 packet_put_string(doid, len);
167 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN, &input_gssapi_token);
168 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_ERRTOK, &input_gssapi_errtok);
169 authctxt->postponed = 1;
175 input_gssapi_token(int type, u_int32_t plen, void *ctxt)
177 Authctxt *authctxt = ctxt;
179 gss_buffer_desc send_tok = GSS_C_EMPTY_BUFFER;
180 gss_buffer_desc recv_tok;
181 OM_uint32 maj_status, min_status, flags;
184 if (authctxt == NULL || (authctxt->methoddata == NULL && !use_privsep))
185 fatal("No authentication or GSSAPI context");
187 gssctxt = authctxt->methoddata;
188 recv_tok.value = packet_get_string(&len);
189 recv_tok.length = len; /* u_int vs. size_t */
193 maj_status = PRIVSEP(ssh_gssapi_accept_ctx(gssctxt, &recv_tok,
196 xfree(recv_tok.value);
198 if (GSS_ERROR(maj_status)) {
199 if (send_tok.length != 0) {
200 packet_start(SSH2_MSG_USERAUTH_GSSAPI_ERRTOK);
201 packet_put_string(send_tok.value, send_tok.length);
204 authctxt->postponed = 0;
205 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN, NULL);
206 userauth_finish(authctxt, 0, "gssapi-with-mic");
208 if (send_tok.length != 0) {
209 packet_start(SSH2_MSG_USERAUTH_GSSAPI_TOKEN);
210 packet_put_string(send_tok.value, send_tok.length);
213 if (maj_status == GSS_S_COMPLETE) {
214 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN, NULL);
215 if (flags & GSS_C_INTEG_FLAG)
216 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_MIC,
220 SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE,
221 &input_gssapi_exchange_complete);
225 gss_release_buffer(&min_status, &send_tok);
229 input_gssapi_errtok(int type, u_int32_t plen, void *ctxt)
231 Authctxt *authctxt = ctxt;
233 gss_buffer_desc send_tok = GSS_C_EMPTY_BUFFER;
234 gss_buffer_desc recv_tok;
235 OM_uint32 maj_status;
238 if (authctxt == NULL || (authctxt->methoddata == NULL && !use_privsep))
239 fatal("No authentication or GSSAPI context");
241 gssctxt = authctxt->methoddata;
242 recv_tok.value = packet_get_string(&len);
243 recv_tok.length = len;
247 /* Push the error token into GSSAPI to see what it says */
248 maj_status = PRIVSEP(ssh_gssapi_accept_ctx(gssctxt, &recv_tok,
251 xfree(recv_tok.value);
253 /* We can't return anything to the client, even if we wanted to */
254 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN, NULL);
255 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_ERRTOK, NULL);
257 /* The client will have already moved on to the next auth */
259 gss_release_buffer(&maj_status, &send_tok);
263 gssapi_set_username(Authctxt *authctxt)
267 if ((authctxt->user == NULL) || (authctxt->user[0] == '\0')) {
268 PRIVSEP(ssh_gssapi_localname(&lname));
269 if (lname && lname[0] != '\0') {
270 if (authctxt->user) xfree(authctxt->user);
271 authctxt->user = lname;
272 debug("set username to %s from gssapi context", lname);
273 authctxt->pw = PRIVSEP(getpwnamallow(authctxt->user));
278 PRIVSEP(start_pam(authctxt));
282 debug("failed to set username from gssapi context");
283 packet_send_debug("failed to set username from gssapi context");
289 * This is called when the client thinks we've completed authentication.
290 * It should only be enabled in the dispatch handler by the function above,
291 * which only enables it once the GSSAPI exchange is complete.
295 input_gssapi_exchange_complete(int type, u_int32_t plen, void *ctxt)
297 Authctxt *authctxt = ctxt;
301 if (authctxt == NULL || (authctxt->methoddata == NULL && !use_privsep))
302 fatal("No authentication or GSSAPI context");
304 gssctxt = authctxt->methoddata;
307 * We don't need to check the status, because we're only enabled in
308 * the dispatcher once the exchange is complete
313 authenticated = PRIVSEP(ssh_gssapi_userok(authctxt->user,
316 authctxt->postponed = 0;
317 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN, NULL);
318 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_ERRTOK, NULL);
319 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_MIC, NULL);
320 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE, NULL);
321 userauth_finish(authctxt, authenticated, "gssapi-with-mic");
325 input_gssapi_mic(int type, u_int32_t plen, void *ctxt)
327 Authctxt *authctxt = ctxt;
329 int authenticated = 0;
331 gss_buffer_desc mic, gssbuf;
334 if (authctxt == NULL || (authctxt->methoddata == NULL && !use_privsep))
335 fatal("No authentication or GSSAPI context");
337 gssctxt = authctxt->methoddata;
339 mic.value = packet_get_string(&len);
342 ssh_gssapi_buildmic(&b, authctxt->user, authctxt->service,
345 gssbuf.value = buffer_ptr(&b);
346 gssbuf.length = buffer_len(&b);
348 gssapi_set_username(authctxt);
350 if (!GSS_ERROR(PRIVSEP(ssh_gssapi_checkmic(gssctxt, &gssbuf, &mic))))
352 PRIVSEP(ssh_gssapi_userok(authctxt->user, authctxt->pw));
354 logit("GSSAPI MIC check failed");
359 authctxt->postponed = 0;
360 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN, NULL);
361 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_ERRTOK, NULL);
362 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_MIC, NULL);
363 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE, NULL);
364 userauth_finish(authctxt, authenticated, "gssapi-with-mic");
367 Authmethod method_gsskeyex = {
370 &options.gss_authentication
373 Authmethod method_gssapi = {
376 &options.gss_authentication