1 /* $OpenBSD: auth2-gss.c,v 1.7 2003/11/21 11:57:03 djm 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;
46 userauth_external(Authctxt *authctxt)
50 return(PRIVSEP(ssh_gssapi_userok(authctxt->user)));
53 static void ssh_gssapi_userauth_error(Gssctxt *ctxt);
54 static void input_gssapi_token(int type, u_int32_t plen, void *ctxt);
55 static void input_gssapi_mic(int type, u_int32_t plen, void *ctxt);
56 static void input_gssapi_exchange_complete(int type, u_int32_t plen, void *ctxt);
57 static void input_gssapi_errtok(int, u_int32_t, void *);
59 static int gssapi_with_mic = 1; /* flag to toggle "gssapi-with-mic" vs.
63 * We only support those mechanisms that we know about (ie ones that we know
64 * how to check local user kuserok and the like
67 userauth_gssapi(Authctxt *authctxt)
69 gss_OID_desc oid = {0, NULL};
72 gss_OID_set supported;
78 if (!authctxt->valid || authctxt->user == NULL)
81 mechs = packet_get_int();
83 debug("Mechanism negotiation is not supported");
87 ssh_gssapi_supported_oids(&supported);
95 doid = packet_get_string(&len);
97 if (doid[0] != SSH_GSS_OIDTYPE || doid[1] != len-2) {
98 logit("Mechanism OID received using the old encoding form");
102 oid.elements = doid + 2;
103 oid.length = len - 2;
105 gss_test_oid_set_member(&ms, &oid, supported, &present);
106 } while (mechs > 0 && !present);
108 gss_release_oid_set(&ms, &supported);
115 if (GSS_ERROR(PRIVSEP(ssh_gssapi_server_ctx(&ctxt, &oid)))) {
120 authctxt->methoddata=(void *)ctxt;
122 packet_start(SSH2_MSG_USERAUTH_GSSAPI_RESPONSE);
124 /* Return the OID that we received */
125 packet_put_string(doid, len);
130 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN, &input_gssapi_token);
131 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_ERRTOK, &input_gssapi_errtok);
132 authctxt->postponed = 1;
138 input_gssapi_token(int type, u_int32_t plen, void *ctxt)
140 Authctxt *authctxt = ctxt;
142 gss_buffer_desc send_tok = GSS_C_EMPTY_BUFFER;
143 gss_buffer_desc recv_tok;
144 OM_uint32 maj_status, min_status, flags;
147 if (authctxt == NULL || (authctxt->methoddata == NULL && !use_privsep))
148 fatal("No authentication or GSSAPI context");
150 gssctxt = authctxt->methoddata;
151 recv_tok.value = packet_get_string(&len);
152 recv_tok.length = len; /* u_int vs. size_t */
156 maj_status = PRIVSEP(ssh_gssapi_accept_ctx(gssctxt, &recv_tok,
159 xfree(recv_tok.value);
161 if (GSS_ERROR(maj_status)) {
162 ssh_gssapi_userauth_error(gssctxt);
163 if (send_tok.length != 0) {
164 packet_start(SSH2_MSG_USERAUTH_GSSAPI_ERRTOK);
165 packet_put_string(send_tok.value, send_tok.length);
168 authctxt->postponed = 0;
169 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN, NULL);
170 userauth_finish(authctxt, 0,
171 gssapi_with_mic ? "gssapi-with-mic" :
174 if (send_tok.length != 0) {
175 packet_start(SSH2_MSG_USERAUTH_GSSAPI_TOKEN);
176 packet_put_string(send_tok.value, send_tok.length);
179 if (maj_status == GSS_S_COMPLETE) {
180 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN, NULL);
181 if (flags & GSS_C_INTEG_FLAG && gssapi_with_mic)
182 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_MIC,
186 SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE,
187 &input_gssapi_exchange_complete);
191 gss_release_buffer(&min_status, &send_tok);
195 input_gssapi_errtok(int type, u_int32_t plen, void *ctxt)
197 Authctxt *authctxt = ctxt;
199 gss_buffer_desc send_tok = GSS_C_EMPTY_BUFFER;
200 gss_buffer_desc recv_tok;
201 OM_uint32 maj_status;
204 if (authctxt == NULL || (authctxt->methoddata == NULL && !use_privsep))
205 fatal("No authentication or GSSAPI context");
207 gssctxt = authctxt->methoddata;
208 recv_tok.value = packet_get_string(&len);
209 recv_tok.length = len;
213 /* Push the error token into GSSAPI to see what it says */
214 maj_status = PRIVSEP(ssh_gssapi_accept_ctx(gssctxt, &recv_tok,
217 xfree(recv_tok.value);
219 /* We can't return anything to the client, even if we wanted to */
220 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN, NULL);
221 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_ERRTOK, NULL);
223 /* The client will have already moved on to the next auth */
225 gss_release_buffer(&maj_status, &send_tok);
229 gssapi_set_implicit_username(Authctxt *authctxt)
231 if ((strcmp(authctxt->user, "") == 0) && (authctxt->pw == NULL)) {
233 PRIVSEP(ssh_gssapi_localname(&lname));
234 if (lname && lname[0] != '\0') {
235 xfree(authctxt->user);
236 authctxt->user = lname;
237 debug("set username to %s from gssapi context", lname);
238 authctxt->pw = PRIVSEP(getpwnamallow(authctxt->user));
240 debug("failed to set username from gssapi context");
245 PRIVSEP(start_pam(authctxt->pw->pw_name));
251 * This is called when the client thinks we've completed authentication.
252 * It should only be enabled in the dispatch handler by the function above,
253 * which only enables it once the GSSAPI exchange is complete.
257 input_gssapi_exchange_complete(int type, u_int32_t plen, void *ctxt)
259 Authctxt *authctxt = ctxt;
263 if (authctxt == NULL || (authctxt->methoddata == NULL && !use_privsep))
264 fatal("No authentication or GSSAPI context");
266 gssapi_set_implicit_username(authctxt);
268 gssctxt = authctxt->methoddata;
271 * We don't need to check the status, because we're only enabled in
272 * the dispatcher once the exchange is complete
277 if (strcmp(authctxt->user, "") != 0) {
278 authenticated = PRIVSEP(ssh_gssapi_userok(authctxt->user));
283 authctxt->postponed = 0;
284 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN, NULL);
285 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_ERRTOK, NULL);
286 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_MIC, NULL);
287 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE, NULL);
288 userauth_finish(authctxt, authenticated,
289 gssapi_with_mic ? "gssapi-with-mic" : "gssapi");
293 userauth_gssapi_with_mic(Authctxt *authctxt)
296 return userauth_gssapi(authctxt);
300 userauth_gssapi_without_mic(Authctxt *authctxt)
303 return userauth_gssapi(authctxt);
307 input_gssapi_mic(int type, u_int32_t plen, void *ctxt)
309 Authctxt *authctxt = ctxt;
311 int authenticated = 0;
313 gss_buffer_desc mic, gssbuf;
316 if (authctxt == NULL || (authctxt->methoddata == NULL && !use_privsep))
317 fatal("No authentication or GSSAPI context");
319 gssapi_set_implicit_username(authctxt);
321 gssctxt = authctxt->methoddata;
323 mic.value = packet_get_string(&len);
326 ssh_gssapi_buildmic(&b, authctxt->user, authctxt->service,
329 gssbuf.value = buffer_ptr(&b);
330 gssbuf.length = buffer_len(&b);
332 if (!GSS_ERROR(PRIVSEP(ssh_gssapi_checkmic(gssctxt, &gssbuf, &mic))))
333 if (strcmp(authctxt->user, "") != 0) {
334 authenticated = PRIVSEP(ssh_gssapi_userok(authctxt->user));
339 logit("GSSAPI MIC check failed");
344 authctxt->postponed = 0;
345 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN, NULL);
346 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_ERRTOK, NULL);
347 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_MIC, NULL);
348 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE, NULL);
349 userauth_finish(authctxt, authenticated, "gssapi-with-mic");
352 static void ssh_gssapi_userauth_error(Gssctxt *ctxt) {
356 errstr=PRIVSEP(ssh_gssapi_last_error(ctxt,&maj,&min));
358 packet_start(SSH2_MSG_USERAUTH_GSSAPI_ERROR);
361 packet_put_cstring(errstr);
362 packet_put_cstring("");
369 Authmethod method_external = {
372 &options.gss_authentication
375 Authmethod method_gssapi = {
377 userauth_gssapi_with_mic,
378 &options.gss_authentication
381 Authmethod method_gssapi_compat = {
383 userauth_gssapi_without_mic,
384 &options.gss_authentication