1 /* $OpenBSD: auth2-gss.c,v 1.8 2004/06/21 17:36:31 avsm 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.
39 #include "monitor_wrap.h"
43 extern ServerOptions options;
45 static void ssh_gssapi_userauth_error(Gssctxt *ctxt);
46 static void input_gssapi_token(int type, u_int32_t plen, void *ctxt);
47 static void input_gssapi_mic(int type, u_int32_t plen, void *ctxt);
48 static void input_gssapi_exchange_complete(int type, u_int32_t plen, void *ctxt);
49 static void input_gssapi_errtok(int, u_int32_t, void *);
51 static int gssapi_with_mic = 1; /* flag to toggle "gssapi-with-mic" vs.
55 userauth_external(Authctxt *authctxt)
59 if (authctxt->valid && authctxt->user && authctxt->user[0]) {
60 return(PRIVSEP(ssh_gssapi_userok(authctxt->user)));
66 * We only support those mechanisms that we know about (ie ones that we know
67 * how to check local user kuserok and the like
70 userauth_gssapi(Authctxt *authctxt)
72 gss_OID_desc goid = {0, NULL};
75 gss_OID_set supported;
81 /* authctxt->valid may be 0 if we haven't yet determined
82 username from gssapi context. */
84 if (authctxt->user == NULL)
87 mechs = packet_get_int();
89 debug("Mechanism negotiation is not supported");
93 ssh_gssapi_supported_oids(&supported);
101 doid = packet_get_string(&len);
103 if (doid[0] != SSH_GSS_OIDTYPE || doid[1] != len-2) {
104 logit("Mechanism OID received using the old encoding form");
105 goid.elements = doid;
108 goid.elements = doid + 2;
109 goid.length = len - 2;
111 gss_test_oid_set_member(&ms, &goid, supported, &present);
112 } while (mechs > 0 && !present);
114 gss_release_oid_set(&ms, &supported);
121 if (GSS_ERROR(PRIVSEP(ssh_gssapi_server_ctx(&ctxt, &goid)))) {
126 authctxt->methoddata=(void *)ctxt;
128 packet_start(SSH2_MSG_USERAUTH_GSSAPI_RESPONSE);
130 /* Return the OID that we received */
131 packet_put_string(doid, len);
136 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN, &input_gssapi_token);
137 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_ERRTOK, &input_gssapi_errtok);
138 authctxt->postponed = 1;
144 input_gssapi_token(int type, u_int32_t plen, void *ctxt)
146 Authctxt *authctxt = ctxt;
148 gss_buffer_desc send_tok = GSS_C_EMPTY_BUFFER;
149 gss_buffer_desc recv_tok;
150 OM_uint32 maj_status, min_status, flags=0;
153 if (authctxt == NULL || (authctxt->methoddata == NULL && !use_privsep))
154 fatal("No authentication or GSSAPI context");
156 gssctxt = authctxt->methoddata;
157 recv_tok.value = packet_get_string(&len);
158 recv_tok.length = len; /* u_int vs. size_t */
162 maj_status = PRIVSEP(ssh_gssapi_accept_ctx(gssctxt, &recv_tok,
165 xfree(recv_tok.value);
167 if (GSS_ERROR(maj_status)) {
168 ssh_gssapi_userauth_error(gssctxt);
169 if (send_tok.length != 0) {
170 packet_start(SSH2_MSG_USERAUTH_GSSAPI_ERRTOK);
171 packet_put_string(send_tok.value, send_tok.length);
174 authctxt->postponed = 0;
175 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN, NULL);
176 userauth_finish(authctxt, 0,
177 gssapi_with_mic ? "gssapi-with-mic" :
180 if (send_tok.length != 0) {
181 packet_start(SSH2_MSG_USERAUTH_GSSAPI_TOKEN);
182 packet_put_string(send_tok.value, send_tok.length);
185 if (maj_status == GSS_S_COMPLETE) {
186 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN, NULL);
187 if (flags & GSS_C_INTEG_FLAG && gssapi_with_mic)
188 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_MIC,
192 SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE,
193 &input_gssapi_exchange_complete);
197 gss_release_buffer(&min_status, &send_tok);
201 input_gssapi_errtok(int type, u_int32_t plen, void *ctxt)
203 Authctxt *authctxt = ctxt;
205 gss_buffer_desc send_tok = GSS_C_EMPTY_BUFFER;
206 gss_buffer_desc recv_tok;
207 OM_uint32 maj_status;
210 if (authctxt == NULL || (authctxt->methoddata == NULL && !use_privsep))
211 fatal("No authentication or GSSAPI context");
213 gssctxt = authctxt->methoddata;
214 recv_tok.value = packet_get_string(&len);
215 recv_tok.length = len;
219 /* Push the error token into GSSAPI to see what it says */
220 maj_status = PRIVSEP(ssh_gssapi_accept_ctx(gssctxt, &recv_tok,
223 xfree(recv_tok.value);
225 /* We can't return anything to the client, even if we wanted to */
226 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN, NULL);
227 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_ERRTOK, NULL);
229 /* The client will have already moved on to the next auth */
231 gss_release_buffer(&maj_status, &send_tok);
235 gssapi_set_implicit_username(Authctxt *authctxt)
237 if ((authctxt->user == NULL) || (strcmp(authctxt->user, "") == 0)) {
239 PRIVSEP(ssh_gssapi_localname(&lname));
240 if (lname && lname[0] != '\0') {
241 xfree(authctxt->user);
242 authctxt->user = lname;
243 debug("set username to %s from gssapi context", lname);
244 authctxt->pw = PRIVSEP(getpwnamallow(authctxt->user));
249 debug("failed to set username from gssapi context");
255 PRIVSEP(start_pam(authctxt));
261 * This is called when the client thinks we've completed authentication.
262 * It should only be enabled in the dispatch handler by the function above,
263 * which only enables it once the GSSAPI exchange is complete.
267 input_gssapi_exchange_complete(int type, u_int32_t plen, void *ctxt)
269 Authctxt *authctxt = ctxt;
273 if (authctxt == NULL || (authctxt->methoddata == NULL && !use_privsep))
274 fatal("No authentication or GSSAPI context");
276 gssapi_set_implicit_username(authctxt);
278 gssctxt = authctxt->methoddata;
281 * We don't need to check the status, because we're only enabled in
282 * the dispatcher once the exchange is complete
287 /* user should be set if valid but we double-check here */
288 if (authctxt->valid && authctxt->user && authctxt->user[0]) {
289 authenticated = PRIVSEP(ssh_gssapi_userok(authctxt->user));
294 authctxt->postponed = 0;
295 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN, NULL);
296 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_ERRTOK, NULL);
297 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_MIC, NULL);
298 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE, NULL);
299 userauth_finish(authctxt, authenticated,
300 gssapi_with_mic ? "gssapi-with-mic" : "gssapi");
304 userauth_gssapi_with_mic(Authctxt *authctxt)
307 return userauth_gssapi(authctxt);
311 userauth_gssapi_without_mic(Authctxt *authctxt)
314 return userauth_gssapi(authctxt);
318 input_gssapi_mic(int type, u_int32_t plen, void *ctxt)
320 Authctxt *authctxt = ctxt;
322 int authenticated = 0;
324 gss_buffer_desc mic, gssbuf;
327 if (authctxt == NULL || (authctxt->methoddata == NULL && !use_privsep))
328 fatal("No authentication or GSSAPI context");
330 gssapi_set_implicit_username(authctxt);
332 gssctxt = authctxt->methoddata;
334 mic.value = packet_get_string(&len);
337 ssh_gssapi_buildmic(&b, authctxt->user, authctxt->service,
340 gssbuf.value = buffer_ptr(&b);
341 gssbuf.length = buffer_len(&b);
343 if (!GSS_ERROR(PRIVSEP(ssh_gssapi_checkmic(gssctxt, &gssbuf, &mic))))
344 if (authctxt->valid && authctxt->user && authctxt->user[0]) {
345 authenticated = PRIVSEP(ssh_gssapi_userok(authctxt->user));
350 logit("GSSAPI MIC check failed");
355 authctxt->postponed = 0;
356 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN, NULL);
357 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_ERRTOK, NULL);
358 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_MIC, NULL);
359 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE, NULL);
360 userauth_finish(authctxt, authenticated, "gssapi-with-mic");
363 static void ssh_gssapi_userauth_error(Gssctxt *ctxt) {
367 errstr=PRIVSEP(ssh_gssapi_last_error(ctxt,&maj,&min));
369 packet_start(SSH2_MSG_USERAUTH_GSSAPI_ERROR);
372 packet_put_cstring(errstr);
373 packet_put_cstring("");
380 Authmethod method_external = {
383 &options.gss_authentication
386 Authmethod method_gssapi = {
388 userauth_gssapi_with_mic,
389 &options.gss_authentication
392 Authmethod method_gssapi_compat = {
394 userauth_gssapi_without_mic,
395 &options.gss_authentication