]> andersk Git - gssapi-openssh.git/blame - openssh/auth2-gss.c
update for 3.1 release
[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;
416fd2a8 144 OM_uint32 maj_status, min_status, flags;
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
245 PRIVSEP(start_pam(authctxt->pw->pw_name));
246#endif
247 }
248}
249
70791e56 250/*
251 * This is called when the client thinks we've completed authentication.
ff2d7a98 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.
254 */
70791e56 255
ff2d7a98 256static void
257input_gssapi_exchange_complete(int type, u_int32_t plen, void *ctxt)
258{
70791e56 259 Authctxt *authctxt = ctxt;
260 Gssctxt *gssctxt;
261 int authenticated;
262
ff2d7a98 263 if (authctxt == NULL || (authctxt->methoddata == NULL && !use_privsep))
70791e56 264 fatal("No authentication or GSSAPI context");
265
416fd2a8 266 gssapi_set_implicit_username(authctxt);
c2397a66 267
70791e56 268 gssctxt = authctxt->methoddata;
ff2d7a98 269
70791e56 270 /*
416fd2a8 271 * We don't need to check the status, because we're only enabled in
272 * the dispatcher once the exchange is complete
ff2d7a98 273 */
274
70791e56 275 packet_check_eom();
276
416fd2a8 277 if (strcmp(authctxt->user, "") != 0) {
278 authenticated = PRIVSEP(ssh_gssapi_userok(authctxt->user));
279 } else {
280 authenticated = 0;
281 }
ff2d7a98 282
70791e56 283 authctxt->postponed = 0;
284 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN, NULL);
285 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_ERRTOK, NULL);
416fd2a8 286 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_MIC, NULL);
70791e56 287 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE, NULL);
416fd2a8 288 userauth_finish(authctxt, authenticated,
289 gssapi_with_mic ? "gssapi-with-mic" : "gssapi");
290}
291
292static int
293userauth_gssapi_with_mic(Authctxt *authctxt)
294{
295 gssapi_with_mic = 1;
296 return userauth_gssapi(authctxt);
297}
298
299static int
300userauth_gssapi_without_mic(Authctxt *authctxt)
301{
302 gssapi_with_mic = 0;
303 return userauth_gssapi(authctxt);
304}
305
306static void
307input_gssapi_mic(int type, u_int32_t plen, void *ctxt)
308{
309 Authctxt *authctxt = ctxt;
310 Gssctxt *gssctxt;
311 int authenticated = 0;
312 Buffer b;
313 gss_buffer_desc mic, gssbuf;
314 u_int len;
315
316 if (authctxt == NULL || (authctxt->methoddata == NULL && !use_privsep))
317 fatal("No authentication or GSSAPI context");
318
319 gssapi_set_implicit_username(authctxt);
320
321 gssctxt = authctxt->methoddata;
322
323 mic.value = packet_get_string(&len);
324 mic.length = len;
325
326 ssh_gssapi_buildmic(&b, authctxt->user, authctxt->service,
327 "gssapi-with-mic");
328
329 gssbuf.value = buffer_ptr(&b);
330 gssbuf.length = buffer_len(&b);
331
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));
335 } else {
336 authenticated = 0;
337 }
338 else
339 logit("GSSAPI MIC check failed");
340
341 buffer_free(&b);
342 xfree(mic.value);
343
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");
ff2d7a98 350}
351
88928908 352static void ssh_gssapi_userauth_error(Gssctxt *ctxt) {
353 char *errstr;
354 OM_uint32 maj,min;
355
356 errstr=PRIVSEP(ssh_gssapi_last_error(ctxt,&maj,&min));
357 if (errstr) {
88928908 358 packet_start(SSH2_MSG_USERAUTH_GSSAPI_ERROR);
359 packet_put_int(maj);
360 packet_put_int(min);
361 packet_put_cstring(errstr);
362 packet_put_cstring("");
363 packet_send();
364 packet_write_wait();
365 xfree(errstr);
88928908 366 }
367}
368
ff2d7a98 369Authmethod method_external = {
370 "external-keyx",
371 userauth_external,
372 &options.gss_authentication
373};
374
375Authmethod method_gssapi = {
416fd2a8 376 "gssapi-with-mic",
377 userauth_gssapi_with_mic,
378 &options.gss_authentication
379};
380
381Authmethod method_gssapi_compat = {
70791e56 382 "gssapi",
416fd2a8 383 userauth_gssapi_without_mic,
70791e56 384 &options.gss_authentication
ff2d7a98 385};
386
387#endif /* GSSAPI */
This page took 0.148743 seconds and 5 git commands to generate.