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