]> andersk Git - gssapi-openssh.git/blame - openssh/auth2-gss.c
This fixes an issue with determining the auth method, for reporting
[gssapi-openssh.git] / openssh / auth2-gss.c
CommitLineData
e74dc197 1/* $OpenBSD: auth2-gss.c,v 1.16 2007/10/29 00:52:45 dtucker Exp $ */
7cac2b65 2
b59afbfe 3/*
f97edba6 4 * Copyright (c) 2001-2007 Simon Wilkinson. All rights reserved.
b59afbfe 5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
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.
14 *
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.
25 */
26
27#include "includes.h"
28
29#ifdef GSSAPI
7cac2b65 30
30460aeb 31#include <sys/types.h>
32
33#include <stdarg.h>
34
35#include "xmalloc.h"
36#include "key.h"
37#include "hostfile.h"
b59afbfe 38#include "auth.h"
39#include "ssh2.h"
b59afbfe 40#include "log.h"
41#include "dispatch.h"
30460aeb 42#include "buffer.h"
b59afbfe 43#include "servconf.h"
b59afbfe 44#include "packet.h"
b59afbfe 45#include "ssh-gss.h"
30460aeb 46#include "monitor_wrap.h"
b59afbfe 47
48extern ServerOptions options;
b59afbfe 49
23987cb8 50static void ssh_gssapi_userauth_error(Gssctxt *ctxt);
b59afbfe 51static void input_gssapi_token(int type, u_int32_t plen, void *ctxt);
540d72c3 52static void input_gssapi_mic(int type, u_int32_t plen, void *ctxt);
b59afbfe 53static void input_gssapi_exchange_complete(int type, u_int32_t plen, void *ctxt);
e23e524c 54static void input_gssapi_errtok(int, u_int32_t, void *);
b59afbfe 55
fe4ad273 56/*
57 * The 'gssapi_keyex' userauth mechanism.
58 */
59static int
60userauth_gsskeyex(Authctxt *authctxt)
61{
62 int authenticated = 0;
63 Buffer b, b2;
64 gss_buffer_desc mic, gssbuf, gssbuf2;
65 u_int len;
66
67 mic.value = packet_get_string(&len);
68 mic.length = len;
69
70 packet_check_eom();
71
72 ssh_gssapi_buildmic(&b, authctxt->user, authctxt->service,
73 "gssapi-keyex");
74
75 gssbuf.value = buffer_ptr(&b);
76 gssbuf.length = buffer_len(&b);
77
78 /* client may have used empty username to determine target
79 name from GSSAPI context */
80 ssh_gssapi_buildmic(&b2, "", authctxt->service, "gssapi-keyex");
81
82 gssbuf2.value = buffer_ptr(&b2);
83 gssbuf2.length = buffer_len(&b2);
84
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,
87 &gssbuf, &mic))) ||
88 !GSS_ERROR(PRIVSEP(ssh_gssapi_checkmic(gss_kex_context,
89 &gssbuf2, &mic)))) {
90 if (authctxt->valid && authctxt->user && authctxt->user[0]) {
c7931c9a 91 authenticated = PRIVSEP(ssh_gssapi_userok(authctxt->user,
09dcbb73 92 authctxt->pw,
93 1 /* gssapi-keyex */));
fe4ad273 94 }
95 }
96
97 buffer_free(&b);
98 buffer_free(&b2);
99 xfree(mic.value);
100
101 return (authenticated);
102}
103
7cac2b65 104/*
105 * We only support those mechanisms that we know about (ie ones that we know
08822d99 106 * how to check local user kuserok and the like)
b59afbfe 107 */
108static int
109userauth_gssapi(Authctxt *authctxt)
110{
7e82606e 111 gss_OID_desc goid = {0, NULL};
7cac2b65 112 Gssctxt *ctxt = NULL;
113 int mechs;
114 gss_OID_set supported;
115 int present;
116 OM_uint32 ms;
117 u_int len;
2ce0bfe4 118 u_char *doid = NULL;
7cac2b65 119
941f3f29 120 /* authctxt->valid may be 0 if we haven't yet determined
121 username from gssapi context. */
122
5217e987 123 if (authctxt->user == NULL)
7cac2b65 124 return (0);
125
126 mechs = packet_get_int();
127 if (mechs == 0) {
128 debug("Mechanism negotiation is not supported");
129 return (0);
130 }
131
132 ssh_gssapi_supported_oids(&supported);
133 do {
134 mechs--;
135
136 if (doid)
137 xfree(doid);
138
540d72c3 139 present = 0;
7cac2b65 140 doid = packet_get_string(&len);
141
2ce0bfe4 142 if (len > 2 && doid[0] == SSH_GSS_OIDTYPE &&
143 doid[1] == len - 2) {
7e82606e 144 goid.elements = doid + 2;
145 goid.length = len - 2;
6fc2ad02 146 gss_test_oid_set_member(&ms, &goid, supported,
147 &present);
148 } else {
149 logit("Badly formed OID received");
7cac2b65 150 }
7cac2b65 151 } while (mechs > 0 && !present);
152
153 gss_release_oid_set(&ms, &supported);
154
155 if (!present) {
156 xfree(doid);
fe4ad273 157 authctxt->server_caused_failure = 1;
7cac2b65 158 return (0);
159 }
160
7e82606e 161 if (GSS_ERROR(PRIVSEP(ssh_gssapi_server_ctx(&ctxt, &goid)))) {
30460aeb 162 if (ctxt != NULL)
163 ssh_gssapi_delete_ctx(&ctxt);
7cac2b65 164 xfree(doid);
fe4ad273 165 authctxt->server_caused_failure = 1;
7cac2b65 166 return (0);
23987cb8 167 }
b59afbfe 168
08822d99 169 authctxt->methoddata = (void *)ctxt;
b59afbfe 170
7cac2b65 171 packet_start(SSH2_MSG_USERAUTH_GSSAPI_RESPONSE);
23987cb8 172
540d72c3 173 /* Return the OID that we received */
7cac2b65 174 packet_put_string(doid, len);
23987cb8 175
7cac2b65 176 packet_send();
177 xfree(doid);
b59afbfe 178
7cac2b65 179 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN, &input_gssapi_token);
180 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_ERRTOK, &input_gssapi_errtok);
181 authctxt->postponed = 1;
182
183 return (0);
b59afbfe 184}
185
186static void
187input_gssapi_token(int type, u_int32_t plen, void *ctxt)
188{
7cac2b65 189 Authctxt *authctxt = ctxt;
190 Gssctxt *gssctxt;
191 gss_buffer_desc send_tok = GSS_C_EMPTY_BUFFER;
192 gss_buffer_desc recv_tok;
147c00cc 193 OM_uint32 maj_status, min_status, flags=0;
7cac2b65 194 u_int len;
195
196 if (authctxt == NULL || (authctxt->methoddata == NULL && !use_privsep))
197 fatal("No authentication or GSSAPI context");
198
199 gssctxt = authctxt->methoddata;
200 recv_tok.value = packet_get_string(&len);
201 recv_tok.length = len; /* u_int vs. size_t */
202
203 packet_check_eom();
204
205 maj_status = PRIVSEP(ssh_gssapi_accept_ctx(gssctxt, &recv_tok,
540d72c3 206 &send_tok, &flags));
7cac2b65 207
208 xfree(recv_tok.value);
209
210 if (GSS_ERROR(maj_status)) {
e23e524c 211 ssh_gssapi_userauth_error(gssctxt);
212 if (send_tok.length != 0) {
e23e524c 213 packet_start(SSH2_MSG_USERAUTH_GSSAPI_ERRTOK);
7cac2b65 214 packet_put_string(send_tok.value, send_tok.length);
215 packet_send();
216 }
217 authctxt->postponed = 0;
7cac2b65 218 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN, NULL);
22ce3a3b 219 userauth_finish(authctxt, 0, "gssapi-with-mic");
7cac2b65 220 } else {
221 if (send_tok.length != 0) {
7cac2b65 222 packet_start(SSH2_MSG_USERAUTH_GSSAPI_TOKEN);
223 packet_put_string(send_tok.value, send_tok.length);
224 packet_send();
225 }
226 if (maj_status == GSS_S_COMPLETE) {
7cac2b65 227 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN, NULL);
22ce3a3b 228 if (flags & GSS_C_INTEG_FLAG)
540d72c3 229 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_MIC,
230 &input_gssapi_mic);
231 else
232 dispatch_set(
233 SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE,
234 &input_gssapi_exchange_complete);
7cac2b65 235 }
236 }
237
238 gss_release_buffer(&min_status, &send_tok);
e23e524c 239}
23987cb8 240
e23e524c 241static void
242input_gssapi_errtok(int type, u_int32_t plen, void *ctxt)
243{
7cac2b65 244 Authctxt *authctxt = ctxt;
245 Gssctxt *gssctxt;
246 gss_buffer_desc send_tok = GSS_C_EMPTY_BUFFER;
247 gss_buffer_desc recv_tok;
248 OM_uint32 maj_status;
249 u_int len;
250
251 if (authctxt == NULL || (authctxt->methoddata == NULL && !use_privsep))
252 fatal("No authentication or GSSAPI context");
253
254 gssctxt = authctxt->methoddata;
255 recv_tok.value = packet_get_string(&len);
256 recv_tok.length = len;
257
258 packet_check_eom();
259
260 /* Push the error token into GSSAPI to see what it says */
261 maj_status = PRIVSEP(ssh_gssapi_accept_ctx(gssctxt, &recv_tok,
262 &send_tok, NULL));
263
264 xfree(recv_tok.value);
e23e524c 265
266 /* We can't return anything to the client, even if we wanted to */
e23e524c 267 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN, NULL);
7cac2b65 268 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_ERRTOK, NULL);
e23e524c 269
270 /* The client will have already moved on to the next auth */
7cac2b65 271
272 gss_release_buffer(&maj_status, &send_tok);
b59afbfe 273}
274
540d72c3 275static void
86a22a0a 276gssapi_set_username(Authctxt *authctxt)
540d72c3 277{
86a22a0a 278 char *lname = NULL;
279
4f1e213f 280 if ((authctxt->user == NULL) || (authctxt->user[0] == '\0')) {
86a22a0a 281 PRIVSEP(ssh_gssapi_localname(&lname));
282 if (lname && lname[0] != '\0') {
283 if (authctxt->user) xfree(authctxt->user);
284 authctxt->user = lname;
285 debug("set username to %s from gssapi context", lname);
286 authctxt->pw = PRIVSEP(getpwnamallow(authctxt->user));
287 if (authctxt->pw) {
288 authctxt->valid = 1;
540d72c3 289#ifdef USE_PAM
86a22a0a 290 if (options.use_pam)
291 PRIVSEP(start_pam(authctxt));
540d72c3 292#endif
86a22a0a 293 }
294 } else {
295 debug("failed to set username from gssapi context");
296 packet_send_debug("failed to set username from gssapi context");
297 }
540d72c3 298 }
299}
300
7cac2b65 301/*
302 * This is called when the client thinks we've completed authentication.
b59afbfe 303 * It should only be enabled in the dispatch handler by the function above,
304 * which only enables it once the GSSAPI exchange is complete.
305 */
7cac2b65 306
b59afbfe 307static void
308input_gssapi_exchange_complete(int type, u_int32_t plen, void *ctxt)
309{
7cac2b65 310 Authctxt *authctxt = ctxt;
311 Gssctxt *gssctxt;
312 int authenticated;
313
b59afbfe 314 if (authctxt == NULL || (authctxt->methoddata == NULL && !use_privsep))
7cac2b65 315 fatal("No authentication or GSSAPI context");
316
3ea093c4 317 gssapi_set_username(authctxt);
d13dfff4 318
7cac2b65 319 gssctxt = authctxt->methoddata;
320
7cac2b65 321 /*
540d72c3 322 * We don't need to check the status, because we're only enabled in
323 * the dispatcher once the exchange is complete
b59afbfe 324 */
325
7cac2b65 326 packet_check_eom();
327
941f3f29 328 /* user should be set if valid but we double-check here */
329 if (authctxt->valid && authctxt->user && authctxt->user[0]) {
c7931c9a 330 authenticated = PRIVSEP(ssh_gssapi_userok(authctxt->user,
09dcbb73 331 authctxt->pw, 0 /* !gssapi-keyex */));
540d72c3 332 } else {
333 authenticated = 0;
334 }
b59afbfe 335
7cac2b65 336 authctxt->postponed = 0;
337 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN, NULL);
338 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_ERRTOK, NULL);
540d72c3 339 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_MIC, NULL);
7cac2b65 340 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE, NULL);
22ce3a3b 341 userauth_finish(authctxt, authenticated, "gssapi-with-mic");
540d72c3 342}
343
344static void
345input_gssapi_mic(int type, u_int32_t plen, void *ctxt)
346{
347 Authctxt *authctxt = ctxt;
348 Gssctxt *gssctxt;
349 int authenticated = 0;
350 Buffer b;
351 gss_buffer_desc mic, gssbuf;
352 u_int len;
353
354 if (authctxt == NULL || (authctxt->methoddata == NULL && !use_privsep))
355 fatal("No authentication or GSSAPI context");
356
540d72c3 357 gssctxt = authctxt->methoddata;
358
359 mic.value = packet_get_string(&len);
360 mic.length = len;
361
362 ssh_gssapi_buildmic(&b, authctxt->user, authctxt->service,
363 "gssapi-with-mic");
364
365 gssbuf.value = buffer_ptr(&b);
366 gssbuf.length = buffer_len(&b);
367
86a22a0a 368 gssapi_set_username(authctxt);
369
540d72c3 370 if (!GSS_ERROR(PRIVSEP(ssh_gssapi_checkmic(gssctxt, &gssbuf, &mic))))
941f3f29 371 if (authctxt->valid && authctxt->user && authctxt->user[0]) {
c7931c9a 372 authenticated =
09dcbb73 373 PRIVSEP(ssh_gssapi_userok(authctxt->user, authctxt->pw,
374 0 /* !gssapi-keyex */));
540d72c3 375 } else {
c7931c9a 376 authenticated = 0;
540d72c3 377 }
378 else
379 logit("GSSAPI MIC check failed");
380
381 buffer_free(&b);
382 xfree(mic.value);
383
384 authctxt->postponed = 0;
385 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN, NULL);
386 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_ERRTOK, NULL);
387 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_MIC, NULL);
388 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE, NULL);
389 userauth_finish(authctxt, authenticated, "gssapi-with-mic");
b59afbfe 390}
391
23987cb8 392static void ssh_gssapi_userauth_error(Gssctxt *ctxt) {
393 char *errstr;
394 OM_uint32 maj,min;
395
396 errstr=PRIVSEP(ssh_gssapi_last_error(ctxt,&maj,&min));
397 if (errstr) {
398 packet_start(SSH2_MSG_USERAUTH_GSSAPI_ERROR);
399 packet_put_int(maj);
400 packet_put_int(min);
401 packet_put_cstring(errstr);
402 packet_put_cstring("");
403 packet_send();
404 packet_write_wait();
405 xfree(errstr);
406 }
407}
408
fe4ad273 409Authmethod method_gsskeyex = {
410 "gssapi-keyex",
411 userauth_gsskeyex,
412 &options.gss_authentication
413};
414
b59afbfe 415Authmethod method_gssapi = {
540d72c3 416 "gssapi-with-mic",
22ce3a3b 417 userauth_gssapi,
7cac2b65 418 &options.gss_authentication
b59afbfe 419};
420
421#endif /* GSSAPI */
This page took 0.152583 seconds and 5 git commands to generate.