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