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