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);
104 doid[0] == SSH_GSS_OIDTYPE &&
105 doid[1] == len - 2) {
106 goid.elements = doid + 2;
107 goid.length = len - 2;
108 gss_test_oid_set_member(&ms, &goid, supported,
111 logit("Badly formed OID received");
113 } while (mechs > 0 && !present);
115 gss_release_oid_set(&ms, &supported);
122 if (GSS_ERROR(PRIVSEP(ssh_gssapi_server_ctx(&ctxt, &goid)))) {
127 authctxt->methoddata=(void *)ctxt;
129 packet_start(SSH2_MSG_USERAUTH_GSSAPI_RESPONSE);
131 /* Return the OID that we received */
132 packet_put_string(doid, len);
137 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN, &input_gssapi_token);
138 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_ERRTOK, &input_gssapi_errtok);
139 authctxt->postponed = 1;
145 input_gssapi_token(int type, u_int32_t plen, void *ctxt)
147 Authctxt *authctxt = ctxt;
149 gss_buffer_desc send_tok = GSS_C_EMPTY_BUFFER;
150 gss_buffer_desc recv_tok;
151 OM_uint32 maj_status, min_status, flags=0;
154 if (authctxt == NULL || (authctxt->methoddata == NULL && !use_privsep))
155 fatal("No authentication or GSSAPI context");
157 gssctxt = authctxt->methoddata;
158 recv_tok.value = packet_get_string(&len);
159 recv_tok.length = len; /* u_int vs. size_t */
163 maj_status = PRIVSEP(ssh_gssapi_accept_ctx(gssctxt, &recv_tok,
166 xfree(recv_tok.value);
168 if (GSS_ERROR(maj_status)) {
169 ssh_gssapi_userauth_error(gssctxt);
170 if (send_tok.length != 0) {
171 packet_start(SSH2_MSG_USERAUTH_GSSAPI_ERRTOK);
172 packet_put_string(send_tok.value, send_tok.length);
175 authctxt->postponed = 0;
176 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN, NULL);
177 userauth_finish(authctxt, 0,
178 gssapi_with_mic ? "gssapi-with-mic" :
181 if (send_tok.length != 0) {
182 packet_start(SSH2_MSG_USERAUTH_GSSAPI_TOKEN);
183 packet_put_string(send_tok.value, send_tok.length);
186 if (maj_status == GSS_S_COMPLETE) {
187 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN, NULL);
188 if (flags & GSS_C_INTEG_FLAG && gssapi_with_mic)
189 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_MIC,
193 SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE,
194 &input_gssapi_exchange_complete);
198 gss_release_buffer(&min_status, &send_tok);
202 input_gssapi_errtok(int type, u_int32_t plen, void *ctxt)
204 Authctxt *authctxt = ctxt;
206 gss_buffer_desc send_tok = GSS_C_EMPTY_BUFFER;
207 gss_buffer_desc recv_tok;
208 OM_uint32 maj_status;
211 if (authctxt == NULL || (authctxt->methoddata == NULL && !use_privsep))
212 fatal("No authentication or GSSAPI context");
214 gssctxt = authctxt->methoddata;
215 recv_tok.value = packet_get_string(&len);
216 recv_tok.length = len;
220 /* Push the error token into GSSAPI to see what it says */
221 maj_status = PRIVSEP(ssh_gssapi_accept_ctx(gssctxt, &recv_tok,
224 xfree(recv_tok.value);
226 /* We can't return anything to the client, even if we wanted to */
227 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN, NULL);
228 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_ERRTOK, NULL);
230 /* The client will have already moved on to the next auth */
232 gss_release_buffer(&maj_status, &send_tok);
236 gssapi_set_implicit_username(Authctxt *authctxt)
238 if ((authctxt->user == NULL) || (authctxt->user[0] == '\0')) {
240 PRIVSEP(ssh_gssapi_localname(&lname));
241 if (lname && lname[0] != '\0') {
242 if (authctxt->user) xfree(authctxt->user);
243 authctxt->user = lname;
244 debug("set username to %s from gssapi context", lname);
245 authctxt->pw = PRIVSEP(getpwnamallow(authctxt->user));
250 debug("failed to set username from gssapi context");
256 PRIVSEP(start_pam(authctxt));
262 * This is called when the client thinks we've completed authentication.
263 * It should only be enabled in the dispatch handler by the function above,
264 * which only enables it once the GSSAPI exchange is complete.
268 input_gssapi_exchange_complete(int type, u_int32_t plen, void *ctxt)
270 Authctxt *authctxt = ctxt;
274 if (authctxt == NULL || (authctxt->methoddata == NULL && !use_privsep))
275 fatal("No authentication or GSSAPI context");
277 gssapi_set_implicit_username(authctxt);
279 gssctxt = authctxt->methoddata;
282 * We don't need to check the status, because we're only enabled in
283 * the dispatcher once the exchange is complete
288 /* user should be set if valid but we double-check here */
289 if (authctxt->valid && authctxt->user && authctxt->user[0]) {
290 authenticated = PRIVSEP(ssh_gssapi_userok(authctxt->user));
295 authctxt->postponed = 0;
296 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN, NULL);
297 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_ERRTOK, NULL);
298 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_MIC, NULL);
299 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE, NULL);
300 userauth_finish(authctxt, authenticated,
301 gssapi_with_mic ? "gssapi-with-mic" : "gssapi");
305 userauth_gssapi_with_mic(Authctxt *authctxt)
308 return userauth_gssapi(authctxt);
312 userauth_gssapi_without_mic(Authctxt *authctxt)
315 return userauth_gssapi(authctxt);
319 input_gssapi_mic(int type, u_int32_t plen, void *ctxt)
321 Authctxt *authctxt = ctxt;
323 int authenticated = 0;
325 gss_buffer_desc mic, gssbuf;
328 if (authctxt == NULL || (authctxt->methoddata == NULL && !use_privsep))
329 fatal("No authentication or GSSAPI context");
331 gssapi_set_implicit_username(authctxt);
333 gssctxt = authctxt->methoddata;
335 mic.value = packet_get_string(&len);
338 ssh_gssapi_buildmic(&b, authctxt->user, authctxt->service,
341 gssbuf.value = buffer_ptr(&b);
342 gssbuf.length = buffer_len(&b);
344 if (!GSS_ERROR(PRIVSEP(ssh_gssapi_checkmic(gssctxt, &gssbuf, &mic))))
345 if (authctxt->valid && authctxt->user && authctxt->user[0]) {
346 authenticated = PRIVSEP(ssh_gssapi_userok(authctxt->user));
351 logit("GSSAPI MIC check failed");
356 authctxt->postponed = 0;
357 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN, NULL);
358 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_ERRTOK, NULL);
359 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_MIC, NULL);
360 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE, NULL);
361 userauth_finish(authctxt, authenticated, "gssapi-with-mic");
364 static void ssh_gssapi_userauth_error(Gssctxt *ctxt) {
368 errstr=PRIVSEP(ssh_gssapi_last_error(ctxt,&maj,&min));
370 packet_start(SSH2_MSG_USERAUTH_GSSAPI_ERROR);
373 packet_put_cstring(errstr);
374 packet_put_cstring("");
381 Authmethod method_external = {
384 &options.gss_authentication
387 Authmethod method_gssapi = {
389 userauth_gssapi_with_mic,
390 &options.gss_authentication
393 Authmethod method_gssapi_compat = {
395 userauth_gssapi_without_mic,
396 &options.gss_authentication