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