]> andersk Git - gssapi-openssh.git/blame - openssh/auth2-gss.c
OPENSSH_3_8_1P1_GSSAPI_20040629 merged to gpt-branch
[gssapi-openssh.git] / openssh / auth2-gss.c
CommitLineData
416fd2a8 1/* $OpenBSD: auth2-gss.c,v 1.7 2003/11/21 11:57:03 djm Exp $ */
70791e56 2
ff2d7a98 3/*
88928908 4 * Copyright (c) 2001-2003 Simon Wilkinson. All rights reserved.
ff2d7a98 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
70791e56 30
ff2d7a98 31#include "auth.h"
32#include "ssh2.h"
ff2d7a98 33#include "xmalloc.h"
34#include "log.h"
35#include "dispatch.h"
36#include "servconf.h"
37#include "compat.h"
38#include "packet.h"
39#include "monitor_wrap.h"
40
41#include "ssh-gss.h"
42
43extern ServerOptions options;
ff2d7a98 44
45static int
46userauth_external(Authctxt *authctxt)
47{
48 packet_check_eom();
49
50 return(PRIVSEP(ssh_gssapi_userok(authctxt->user)));
51}
52
88928908 53static void ssh_gssapi_userauth_error(Gssctxt *ctxt);
ff2d7a98 54static void input_gssapi_token(int type, u_int32_t plen, void *ctxt);
416fd2a8 55static void input_gssapi_mic(int type, u_int32_t plen, void *ctxt);
ff2d7a98 56static void input_gssapi_exchange_complete(int type, u_int32_t plen, void *ctxt);
88928908 57static void input_gssapi_errtok(int, u_int32_t, void *);
ff2d7a98 58
416fd2a8 59static int gssapi_with_mic = 1; /* flag to toggle "gssapi-with-mic" vs.
60 "gssapi" */
61
70791e56 62/*
63 * We only support those mechanisms that we know about (ie ones that we know
ff2d7a98 64 * how to check local user kuserok and the like
65 */
66static int
67userauth_gssapi(Authctxt *authctxt)
68{
70791e56 69 gss_OID_desc oid = {0, NULL};
70 Gssctxt *ctxt = NULL;
71 int mechs;
72 gss_OID_set supported;
73 int present;
74 OM_uint32 ms;
75 u_int len;
76 char *doid = NULL;
77
78 if (!authctxt->valid || authctxt->user == NULL)
79 return (0);
80
81 mechs = packet_get_int();
82 if (mechs == 0) {
83 debug("Mechanism negotiation is not supported");
84 return (0);
88928908 85 }
ff2d7a98 86
70791e56 87 ssh_gssapi_supported_oids(&supported);
88 do {
89 mechs--;
ff2d7a98 90
70791e56 91 if (doid)
92 xfree(doid);
88928908 93
416fd2a8 94 present = 0;
70791e56 95 doid = packet_get_string(&len);
88928908 96
70791e56 97 if (doid[0] != SSH_GSS_OIDTYPE || doid[1] != len-2) {
98 logit("Mechanism OID received using the old encoding form");
99 oid.elements = doid;
100 oid.length = len;
101 } else {
102 oid.elements = doid + 2;
103 oid.length = len - 2;
104 }
105 gss_test_oid_set_member(&ms, &oid, supported, &present);
106 } while (mechs > 0 && !present);
107
108 gss_release_oid_set(&ms, &supported);
109
110 if (!present) {
111 xfree(doid);
112 return (0);
113 }
114
115 if (GSS_ERROR(PRIVSEP(ssh_gssapi_server_ctx(&ctxt, &oid)))) {
116 xfree(doid);
117 return (0);
118 }
119
120 authctxt->methoddata=(void *)ctxt;
121
122 packet_start(SSH2_MSG_USERAUTH_GSSAPI_RESPONSE);
123
416fd2a8 124 /* Return the OID that we received */
70791e56 125 packet_put_string(doid, len);
88928908 126
70791e56 127 packet_send();
128 xfree(doid);
129
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;
133
134 return (0);
ff2d7a98 135}
136
137static void
138input_gssapi_token(int type, u_int32_t plen, void *ctxt)
139{
70791e56 140 Authctxt *authctxt = ctxt;
141 Gssctxt *gssctxt;
142 gss_buffer_desc send_tok = GSS_C_EMPTY_BUFFER;
143 gss_buffer_desc recv_tok;
2a304a95 144 OM_uint32 maj_status, min_status, flags=0;
70791e56 145 u_int len;
146
147 if (authctxt == NULL || (authctxt->methoddata == NULL && !use_privsep))
148 fatal("No authentication or GSSAPI context");
149
150 gssctxt = authctxt->methoddata;
151 recv_tok.value = packet_get_string(&len);
152 recv_tok.length = len; /* u_int vs. size_t */
153
154 packet_check_eom();
155
156 maj_status = PRIVSEP(ssh_gssapi_accept_ctx(gssctxt, &recv_tok,
416fd2a8 157 &send_tok, &flags));
70791e56 158
159 xfree(recv_tok.value);
160
161 if (GSS_ERROR(maj_status)) {
88928908 162 ssh_gssapi_userauth_error(gssctxt);
163 if (send_tok.length != 0) {
88928908 164 packet_start(SSH2_MSG_USERAUTH_GSSAPI_ERRTOK);
70791e56 165 packet_put_string(send_tok.value, send_tok.length);
166 packet_send();
167 }
168 authctxt->postponed = 0;
169 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN, NULL);
416fd2a8 170 userauth_finish(authctxt, 0,
171 gssapi_with_mic ? "gssapi-with-mic" :
172 "gssapi");
70791e56 173 } else {
174 if (send_tok.length != 0) {
175 packet_start(SSH2_MSG_USERAUTH_GSSAPI_TOKEN);
176 packet_put_string(send_tok.value, send_tok.length);
177 packet_send();
178 }
179 if (maj_status == GSS_S_COMPLETE) {
180 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN, NULL);
416fd2a8 181 if (flags & GSS_C_INTEG_FLAG && gssapi_with_mic)
182 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_MIC,
183 &input_gssapi_mic);
184 else
185 dispatch_set(
186 SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE,
187 &input_gssapi_exchange_complete);
70791e56 188 }
189 }
190
191 gss_release_buffer(&min_status, &send_tok);
88928908 192}
193
194static void
195input_gssapi_errtok(int type, u_int32_t plen, void *ctxt)
196{
70791e56 197 Authctxt *authctxt = ctxt;
198 Gssctxt *gssctxt;
199 gss_buffer_desc send_tok = GSS_C_EMPTY_BUFFER;
200 gss_buffer_desc recv_tok;
201 OM_uint32 maj_status;
202 u_int len;
203
204 if (authctxt == NULL || (authctxt->methoddata == NULL && !use_privsep))
205 fatal("No authentication or GSSAPI context");
206
207 gssctxt = authctxt->methoddata;
208 recv_tok.value = packet_get_string(&len);
209 recv_tok.length = len;
210
211 packet_check_eom();
212
213 /* Push the error token into GSSAPI to see what it says */
214 maj_status = PRIVSEP(ssh_gssapi_accept_ctx(gssctxt, &recv_tok,
215 &send_tok, NULL));
216
217 xfree(recv_tok.value);
88928908 218
219 /* We can't return anything to the client, even if we wanted to */
88928908 220 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN, NULL);
70791e56 221 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_ERRTOK, NULL);
88928908 222
223 /* The client will have already moved on to the next auth */
70791e56 224
225 gss_release_buffer(&maj_status, &send_tok);
ff2d7a98 226}
227
416fd2a8 228static void
229gssapi_set_implicit_username(Authctxt *authctxt)
230{
231 if ((strcmp(authctxt->user, "") == 0) && (authctxt->pw == NULL)) {
232 char *lname = 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));
239 } else {
240 debug("failed to set username from gssapi context");
241 }
242 }
243 if (authctxt->pw) {
244#ifdef USE_PAM
2a304a95 245 if (options.use_pam)
246 PRIVSEP(start_pam(authctxt->pw->pw_name));
416fd2a8 247#endif
248 }
249}
250
70791e56 251/*
252 * This is called when the client thinks we've completed authentication.
ff2d7a98 253 * It should only be enabled in the dispatch handler by the function above,
254 * which only enables it once the GSSAPI exchange is complete.
255 */
70791e56 256
ff2d7a98 257static void
258input_gssapi_exchange_complete(int type, u_int32_t plen, void *ctxt)
259{
70791e56 260 Authctxt *authctxt = ctxt;
261 Gssctxt *gssctxt;
262 int authenticated;
263
ff2d7a98 264 if (authctxt == NULL || (authctxt->methoddata == NULL && !use_privsep))
70791e56 265 fatal("No authentication or GSSAPI context");
266
416fd2a8 267 gssapi_set_implicit_username(authctxt);
c2397a66 268
70791e56 269 gssctxt = authctxt->methoddata;
ff2d7a98 270
70791e56 271 /*
416fd2a8 272 * We don't need to check the status, because we're only enabled in
273 * the dispatcher once the exchange is complete
ff2d7a98 274 */
275
70791e56 276 packet_check_eom();
277
416fd2a8 278 if (strcmp(authctxt->user, "") != 0) {
279 authenticated = PRIVSEP(ssh_gssapi_userok(authctxt->user));
280 } else {
281 authenticated = 0;
282 }
ff2d7a98 283
70791e56 284 authctxt->postponed = 0;
285 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN, NULL);
286 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_ERRTOK, NULL);
416fd2a8 287 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_MIC, NULL);
70791e56 288 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE, NULL);
416fd2a8 289 userauth_finish(authctxt, authenticated,
290 gssapi_with_mic ? "gssapi-with-mic" : "gssapi");
291}
292
293static int
294userauth_gssapi_with_mic(Authctxt *authctxt)
295{
296 gssapi_with_mic = 1;
297 return userauth_gssapi(authctxt);
298}
299
300static int
301userauth_gssapi_without_mic(Authctxt *authctxt)
302{
303 gssapi_with_mic = 0;
304 return userauth_gssapi(authctxt);
305}
306
307static void
308input_gssapi_mic(int type, u_int32_t plen, void *ctxt)
309{
310 Authctxt *authctxt = ctxt;
311 Gssctxt *gssctxt;
312 int authenticated = 0;
313 Buffer b;
314 gss_buffer_desc mic, gssbuf;
315 u_int len;
316
317 if (authctxt == NULL || (authctxt->methoddata == NULL && !use_privsep))
318 fatal("No authentication or GSSAPI context");
319
320 gssapi_set_implicit_username(authctxt);
321
322 gssctxt = authctxt->methoddata;
323
324 mic.value = packet_get_string(&len);
325 mic.length = len;
326
327 ssh_gssapi_buildmic(&b, authctxt->user, authctxt->service,
328 "gssapi-with-mic");
329
330 gssbuf.value = buffer_ptr(&b);
331 gssbuf.length = buffer_len(&b);
332
333 if (!GSS_ERROR(PRIVSEP(ssh_gssapi_checkmic(gssctxt, &gssbuf, &mic))))
334 if (strcmp(authctxt->user, "") != 0) {
335 authenticated = PRIVSEP(ssh_gssapi_userok(authctxt->user));
336 } else {
337 authenticated = 0;
338 }
339 else
340 logit("GSSAPI MIC check failed");
341
342 buffer_free(&b);
343 xfree(mic.value);
344
345 authctxt->postponed = 0;
346 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN, NULL);
347 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_ERRTOK, NULL);
348 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_MIC, NULL);
349 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE, NULL);
350 userauth_finish(authctxt, authenticated, "gssapi-with-mic");
ff2d7a98 351}
352
88928908 353static void ssh_gssapi_userauth_error(Gssctxt *ctxt) {
354 char *errstr;
355 OM_uint32 maj,min;
356
357 errstr=PRIVSEP(ssh_gssapi_last_error(ctxt,&maj,&min));
358 if (errstr) {
88928908 359 packet_start(SSH2_MSG_USERAUTH_GSSAPI_ERROR);
360 packet_put_int(maj);
361 packet_put_int(min);
362 packet_put_cstring(errstr);
363 packet_put_cstring("");
364 packet_send();
365 packet_write_wait();
366 xfree(errstr);
88928908 367 }
368}
369
ff2d7a98 370Authmethod method_external = {
371 "external-keyx",
372 userauth_external,
373 &options.gss_authentication
374};
375
376Authmethod method_gssapi = {
416fd2a8 377 "gssapi-with-mic",
378 userauth_gssapi_with_mic,
379 &options.gss_authentication
380};
381
382Authmethod method_gssapi_compat = {
70791e56 383 "gssapi",
416fd2a8 384 userauth_gssapi_without_mic,
70791e56 385 &options.gss_authentication
ff2d7a98 386};
387
388#endif /* GSSAPI */
This page took 0.132537 seconds and 5 git commands to generate.