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