]> andersk Git - gssapi-openssh.git/blob - openssh/auth2-gss.c
add support for GsiAllowLimitedProxy option
[gssapi-openssh.git] / openssh / auth2-gss.c
1 /*      $OpenBSD: auth2-gss.c,v 1.12 2005/10/13 22:24:31 stevesk Exp $  */
2
3 /*
4  * Copyright (c) 2001-2003 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 "auth.h"
32 #include "ssh2.h"
33 #include "xmalloc.h"
34 #include "log.h"
35 #include "dispatch.h"
36 #include "servconf.h"
37 #include "packet.h"
38 #include "monitor_wrap.h"
39
40 #include "ssh-gss.h"
41
42 extern ServerOptions options;
43
44 static void ssh_gssapi_userauth_error(Gssctxt *ctxt);
45 static void input_gssapi_token(int type, u_int32_t plen, void *ctxt);
46 static void input_gssapi_mic(int type, u_int32_t plen, void *ctxt);
47 static void input_gssapi_exchange_complete(int type, u_int32_t plen, void *ctxt);
48 static void input_gssapi_errtok(int, u_int32_t, void *);
49
50 static int gssapi_with_mic = 1; /* flag to toggle "gssapi-with-mic" vs.
51                                    "gssapi" */
52
53 static int
54 userauth_external(Authctxt *authctxt)
55 {
56         packet_check_eom();
57
58         if (authctxt->valid && authctxt->user && authctxt->user[0]) {
59                 return(PRIVSEP(ssh_gssapi_userok(authctxt->user)));
60         }
61         return 0;
62 }
63
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
110 /*
111  * We only support those mechanisms that we know about (ie ones that we know
112  * how to check local user kuserok and the like)
113  */
114 static int
115 userauth_gssapi(Authctxt *authctxt)
116 {
117         gss_OID_desc goid = {0, NULL};
118         Gssctxt *ctxt = NULL;
119         int mechs;
120         gss_OID_set supported;
121         int present;
122         OM_uint32 ms;
123         u_int len;
124         u_char *doid = NULL;
125
126         /* authctxt->valid may be 0 if we haven't yet determined
127            username from gssapi context. */
128
129         if (authctxt->user == NULL)
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
145                 present = 0;
146                 doid = packet_get_string(&len);
147
148                 if (len > 2 && doid[0] == SSH_GSS_OIDTYPE &&
149                     doid[1] == len - 2) {
150                         goid.elements = doid + 2;
151                         goid.length   = len - 2;
152                         gss_test_oid_set_member(&ms, &goid, supported,
153                             &present);
154                 } else {
155                         logit("Badly formed OID received");
156                 }
157         } while (mechs > 0 && !present);
158
159         gss_release_oid_set(&ms, &supported);
160
161         if (!present) {
162                 xfree(doid);
163                 authctxt->server_caused_failure = 1;
164                 return (0);
165         }
166
167         if (GSS_ERROR(PRIVSEP(ssh_gssapi_server_ctx(&ctxt, &goid)))) {
168                 xfree(doid);
169                 authctxt->server_caused_failure = 1;
170                 return (0);
171         }
172
173         authctxt->methoddata = (void *)ctxt;
174
175         packet_start(SSH2_MSG_USERAUTH_GSSAPI_RESPONSE);
176
177         /* Return the OID that we received */
178         packet_put_string(doid, len);
179
180         packet_send();
181         xfree(doid);
182
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);
188 }
189
190 static void
191 input_gssapi_token(int type, u_int32_t plen, void *ctxt)
192 {
193         Authctxt *authctxt = ctxt;
194         Gssctxt *gssctxt;
195         gss_buffer_desc send_tok = GSS_C_EMPTY_BUFFER;
196         gss_buffer_desc recv_tok;
197         OM_uint32 maj_status, min_status, flags=0;
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,
210             &send_tok, &flags));
211
212         xfree(recv_tok.value);
213
214         if (GSS_ERROR(maj_status)) {
215                 ssh_gssapi_userauth_error(gssctxt);
216                 if (send_tok.length != 0) {
217                         packet_start(SSH2_MSG_USERAUTH_GSSAPI_ERRTOK);
218                         packet_put_string(send_tok.value, send_tok.length);
219                         packet_send();
220                 }
221                 authctxt->postponed = 0;
222                 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN, NULL);
223                 userauth_finish(authctxt, 0,
224                                 gssapi_with_mic ? "gssapi-with-mic" :
225                                                   "gssapi");
226         } else {
227                 if (send_tok.length != 0) {
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) {
233                         dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN, NULL);
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);
241                 }
242         }
243
244         gss_release_buffer(&min_status, &send_tok);
245 }
246
247 static void
248 input_gssapi_errtok(int type, u_int32_t plen, void *ctxt)
249 {
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);
271
272         /* We can't return anything to the client, even if we wanted to */
273         dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN, NULL);
274         dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_ERRTOK, NULL);
275
276         /* The client will have already moved on to the next auth */
277
278         gss_release_buffer(&maj_status, &send_tok);
279 }
280
281 static void
282 gssapi_set_implicit_username(Authctxt *authctxt)
283 {
284     if ((authctxt->user == NULL) || (authctxt->user[0] == '\0')) {
285         char *lname = NULL;
286         PRIVSEP(ssh_gssapi_localname(&lname));
287         if (lname && lname[0] != '\0') {
288             if (authctxt->user) xfree(authctxt->user);
289             authctxt->user = lname;
290             debug("set username to %s from gssapi context", lname);
291             authctxt->pw = PRIVSEP(getpwnamallow(authctxt->user));
292             if (authctxt->pw) {
293                 authctxt->valid = 1;
294             }
295         } else {
296             debug("failed to set username from gssapi context");
297         }
298     }
299     if (authctxt->pw) {
300 #ifdef USE_PAM
301         if (options.use_pam)
302                 PRIVSEP(start_pam(authctxt));
303 #endif
304     }
305 }
306
307 /*
308  * This is called when the client thinks we've completed authentication.
309  * It should only be enabled in the dispatch handler by the function above,
310  * which only enables it once the GSSAPI exchange is complete.
311  */
312
313 static void
314 input_gssapi_exchange_complete(int type, u_int32_t plen, void *ctxt)
315 {
316         Authctxt *authctxt = ctxt;
317         Gssctxt *gssctxt;
318         int authenticated;
319
320         if (authctxt == NULL || (authctxt->methoddata == NULL && !use_privsep))
321                 fatal("No authentication or GSSAPI context");
322
323         gssapi_set_implicit_username(authctxt);
324
325         gssctxt = authctxt->methoddata;
326
327         /*
328          * We don't need to check the status, because we're only enabled in
329          * the dispatcher once the exchange is complete
330          */
331
332         packet_check_eom();
333
334         /* user should be set if valid but we double-check here */
335         if (authctxt->valid && authctxt->user && authctxt->user[0]) {
336             authenticated = PRIVSEP(ssh_gssapi_userok(authctxt->user));
337         } else {
338             authenticated = 0;
339         }
340
341         authctxt->postponed = 0;
342         dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN, NULL);
343         dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_ERRTOK, NULL);
344         dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_MIC, NULL);
345         dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE, NULL);
346         userauth_finish(authctxt, authenticated,
347                         gssapi_with_mic ? "gssapi-with-mic" : "gssapi");
348 }
349
350 static int
351 userauth_gssapi_with_mic(Authctxt *authctxt)
352 {
353     gssapi_with_mic = 1;
354     return userauth_gssapi(authctxt);
355 }
356
357 static int
358 userauth_gssapi_without_mic(Authctxt *authctxt)
359 {
360     gssapi_with_mic = 0;
361     return userauth_gssapi(authctxt);
362 }
363
364 static void
365 input_gssapi_mic(int type, u_int32_t plen, void *ctxt)
366 {
367         Authctxt *authctxt = ctxt;
368         Gssctxt *gssctxt;
369         int authenticated = 0;
370         Buffer b;
371         gss_buffer_desc mic, gssbuf;
372         u_int len;
373
374         if (authctxt == NULL || (authctxt->methoddata == NULL && !use_privsep))
375                 fatal("No authentication or GSSAPI context");
376
377         gssapi_set_implicit_username(authctxt);
378
379         gssctxt = authctxt->methoddata;
380
381         mic.value = packet_get_string(&len);
382         mic.length = len;
383
384         ssh_gssapi_buildmic(&b, authctxt->user, authctxt->service,
385             "gssapi-with-mic");
386
387         gssbuf.value = buffer_ptr(&b);
388         gssbuf.length = buffer_len(&b);
389
390         if (!GSS_ERROR(PRIVSEP(ssh_gssapi_checkmic(gssctxt, &gssbuf, &mic))))
391             if (authctxt->valid && authctxt->user && authctxt->user[0]) {
392                 authenticated = PRIVSEP(ssh_gssapi_userok(authctxt->user));
393             } else {
394                 authenticated = 0;
395             }
396         else
397                 logit("GSSAPI MIC check failed");
398
399         buffer_free(&b);
400         xfree(mic.value);
401
402         authctxt->postponed = 0;
403         dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN, NULL);
404         dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_ERRTOK, NULL);
405         dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_MIC, NULL);
406         dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE, NULL);
407         userauth_finish(authctxt, authenticated, "gssapi-with-mic");
408 }
409
410 static void ssh_gssapi_userauth_error(Gssctxt *ctxt) {
411         char *errstr;
412         OM_uint32 maj,min;
413         
414         errstr=PRIVSEP(ssh_gssapi_last_error(ctxt,&maj,&min));
415         if (errstr) {
416                 packet_start(SSH2_MSG_USERAUTH_GSSAPI_ERROR);
417                 packet_put_int(maj);
418                 packet_put_int(min);
419                 packet_put_cstring(errstr);
420                 packet_put_cstring("");
421                 packet_send();
422                 packet_write_wait();
423                 xfree(errstr);
424         }
425 }
426
427 Authmethod method_external = {
428         "external-keyx",
429         userauth_external,
430         &options.gss_authentication
431 };
432         
433 Authmethod method_gsskeyex = {
434         "gssapi-keyex",
435         userauth_gsskeyex,
436         &options.gss_authentication
437 };
438
439 Authmethod method_gssapi = {
440         "gssapi-with-mic",
441         userauth_gssapi_with_mic,
442         &options.gss_authentication
443 };
444
445 Authmethod method_gssapi_compat = {
446         "gssapi",
447         userauth_gssapi_without_mic,
448         &options.gss_authentication
449 };
450
451 #endif /* GSSAPI */
This page took 0.082912 seconds and 5 git commands to generate.