]> andersk Git - gssapi-openssh.git/blob - openssh/auth2-gss.c
openssh-4.4p1-gsskex-20061002.patch
[gssapi-openssh.git] / openssh / auth2-gss.c
1 /* $OpenBSD: auth2-gss.c,v 1.15 2006/08/03 03:34:41 deraadt 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 <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 input_gssapi_token(int type, u_int32_t plen, void *ctxt);
51 static void input_gssapi_mic(int type, u_int32_t plen, void *ctxt);
52 static void input_gssapi_exchange_complete(int type, u_int32_t plen, void *ctxt);
53 static void input_gssapi_errtok(int, u_int32_t, void *);
54
55 /* 
56  * The 'gssapi_keyex' userauth mechanism.
57  */
58 static int
59 userauth_gsskeyex(Authctxt *authctxt)
60 {
61         int authenticated = 0;
62         Buffer b;
63         gss_buffer_desc mic, gssbuf;
64         u_int len;
65
66         mic.value = packet_get_string(&len);
67         mic.length = len;
68
69         packet_check_eom();
70
71         ssh_gssapi_buildmic(&b, authctxt->user, authctxt->service,
72             "gssapi-keyex");
73
74         gssbuf.value = buffer_ptr(&b);
75         gssbuf.length = buffer_len(&b);
76
77         /* gss_kex_context is NULL with privsep, so we can't check it here */
78         if (!GSS_ERROR(PRIVSEP(ssh_gssapi_checkmic(gss_kex_context, 
79             &gssbuf, &mic))))
80                 authenticated = PRIVSEP(ssh_gssapi_userok(authctxt->user));
81         
82         buffer_free(&b);
83         xfree(mic.value);
84
85         return (authenticated);
86 }
87
88 /*
89  * We only support those mechanisms that we know about (ie ones that we know
90  * how to check local user kuserok and the like)
91  */
92 static int
93 userauth_gssapi(Authctxt *authctxt)
94 {
95         gss_OID_desc goid = {0, NULL};
96         Gssctxt *ctxt = NULL;
97         int mechs;
98         gss_OID_set supported;
99         int present;
100         OM_uint32 ms;
101         u_int len;
102         u_char *doid = NULL;
103
104         if (!authctxt->valid || authctxt->user == NULL)
105                 return (0);
106
107         mechs = packet_get_int();
108         if (mechs == 0) {
109                 debug("Mechanism negotiation is not supported");
110                 return (0);
111         }
112
113         ssh_gssapi_supported_oids(&supported);
114         do {
115                 mechs--;
116
117                 if (doid)
118                         xfree(doid);
119
120                 present = 0;
121                 doid = packet_get_string(&len);
122
123                 if (len > 2 && doid[0] == SSH_GSS_OIDTYPE &&
124                     doid[1] == len - 2) {
125                         goid.elements = doid + 2;
126                         goid.length   = len - 2;
127                         gss_test_oid_set_member(&ms, &goid, supported,
128                             &present);
129                 } else {
130                         logit("Badly formed OID received");
131                 }
132         } while (mechs > 0 && !present);
133
134         gss_release_oid_set(&ms, &supported);
135
136         if (!present) {
137                 xfree(doid);
138                 authctxt->server_caused_failure = 1;
139                 return (0);
140         }
141
142         if (GSS_ERROR(PRIVSEP(ssh_gssapi_server_ctx(&ctxt, &goid)))) {
143                 if (ctxt != NULL)
144                         ssh_gssapi_delete_ctx(&ctxt);
145                 xfree(doid);
146                 authctxt->server_caused_failure = 1;
147                 return (0);
148         }
149
150         authctxt->methoddata = (void *)ctxt;
151
152         packet_start(SSH2_MSG_USERAUTH_GSSAPI_RESPONSE);
153
154         /* Return the OID that we received */
155         packet_put_string(doid, len);
156
157         packet_send();
158         xfree(doid);
159
160         dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN, &input_gssapi_token);
161         dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_ERRTOK, &input_gssapi_errtok);
162         authctxt->postponed = 1;
163
164         return (0);
165 }
166
167 static void
168 input_gssapi_token(int type, u_int32_t plen, void *ctxt)
169 {
170         Authctxt *authctxt = ctxt;
171         Gssctxt *gssctxt;
172         gss_buffer_desc send_tok = GSS_C_EMPTY_BUFFER;
173         gss_buffer_desc recv_tok;
174         OM_uint32 maj_status, min_status, flags;
175         u_int len;
176
177         if (authctxt == NULL || (authctxt->methoddata == NULL && !use_privsep))
178                 fatal("No authentication or GSSAPI context");
179
180         gssctxt = authctxt->methoddata;
181         recv_tok.value = packet_get_string(&len);
182         recv_tok.length = len; /* u_int vs. size_t */
183
184         packet_check_eom();
185
186         maj_status = PRIVSEP(ssh_gssapi_accept_ctx(gssctxt, &recv_tok,
187             &send_tok, &flags));
188
189         xfree(recv_tok.value);
190
191         if (GSS_ERROR(maj_status)) {
192                 if (send_tok.length != 0) {
193                         packet_start(SSH2_MSG_USERAUTH_GSSAPI_ERRTOK);
194                         packet_put_string(send_tok.value, send_tok.length);
195                         packet_send();
196                 }
197                 authctxt->postponed = 0;
198                 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN, NULL);
199                 userauth_finish(authctxt, 0, "gssapi-with-mic");
200         } else {
201                 if (send_tok.length != 0) {
202                         packet_start(SSH2_MSG_USERAUTH_GSSAPI_TOKEN);
203                         packet_put_string(send_tok.value, send_tok.length);
204                         packet_send();
205                 }
206                 if (maj_status == GSS_S_COMPLETE) {
207                         dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN, NULL);
208                         if (flags & GSS_C_INTEG_FLAG)
209                                 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_MIC,
210                                     &input_gssapi_mic);
211                         else
212                                 dispatch_set(
213                                     SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE,
214                                     &input_gssapi_exchange_complete);
215                 }
216         }
217
218         gss_release_buffer(&min_status, &send_tok);
219 }
220
221 static void
222 input_gssapi_errtok(int type, u_int32_t plen, void *ctxt)
223 {
224         Authctxt *authctxt = ctxt;
225         Gssctxt *gssctxt;
226         gss_buffer_desc send_tok = GSS_C_EMPTY_BUFFER;
227         gss_buffer_desc recv_tok;
228         OM_uint32 maj_status;
229         u_int len;
230
231         if (authctxt == NULL || (authctxt->methoddata == NULL && !use_privsep))
232                 fatal("No authentication or GSSAPI context");
233
234         gssctxt = authctxt->methoddata;
235         recv_tok.value = packet_get_string(&len);
236         recv_tok.length = len;
237
238         packet_check_eom();
239
240         /* Push the error token into GSSAPI to see what it says */
241         maj_status = PRIVSEP(ssh_gssapi_accept_ctx(gssctxt, &recv_tok,
242             &send_tok, NULL));
243
244         xfree(recv_tok.value);
245
246         /* We can't return anything to the client, even if we wanted to */
247         dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN, NULL);
248         dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_ERRTOK, NULL);
249
250         /* The client will have already moved on to the next auth */
251
252         gss_release_buffer(&maj_status, &send_tok);
253 }
254
255 /*
256  * This is called when the client thinks we've completed authentication.
257  * It should only be enabled in the dispatch handler by the function above,
258  * which only enables it once the GSSAPI exchange is complete.
259  */
260
261 static void
262 input_gssapi_exchange_complete(int type, u_int32_t plen, void *ctxt)
263 {
264         Authctxt *authctxt = ctxt;
265         Gssctxt *gssctxt;
266         int authenticated;
267
268         if (authctxt == NULL || (authctxt->methoddata == NULL && !use_privsep))
269                 fatal("No authentication or GSSAPI context");
270
271         gssctxt = authctxt->methoddata;
272
273         /*
274          * We don't need to check the status, because we're only enabled in
275          * the dispatcher once the exchange is complete
276          */
277
278         packet_check_eom();
279
280         authenticated = PRIVSEP(ssh_gssapi_userok(authctxt->user));
281
282         authctxt->postponed = 0;
283         dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN, NULL);
284         dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_ERRTOK, NULL);
285         dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_MIC, NULL);
286         dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE, NULL);
287         userauth_finish(authctxt, authenticated, "gssapi-with-mic");
288 }
289
290 static void
291 input_gssapi_mic(int type, u_int32_t plen, void *ctxt)
292 {
293         Authctxt *authctxt = ctxt;
294         Gssctxt *gssctxt;
295         int authenticated = 0;
296         Buffer b;
297         gss_buffer_desc mic, gssbuf;
298         u_int len;
299
300         if (authctxt == NULL || (authctxt->methoddata == NULL && !use_privsep))
301                 fatal("No authentication or GSSAPI context");
302
303         gssctxt = authctxt->methoddata;
304
305         mic.value = packet_get_string(&len);
306         mic.length = len;
307
308         ssh_gssapi_buildmic(&b, authctxt->user, authctxt->service,
309             "gssapi-with-mic");
310
311         gssbuf.value = buffer_ptr(&b);
312         gssbuf.length = buffer_len(&b);
313
314         if (!GSS_ERROR(PRIVSEP(ssh_gssapi_checkmic(gssctxt, &gssbuf, &mic))))
315                 authenticated = PRIVSEP(ssh_gssapi_userok(authctxt->user));
316         else
317                 logit("GSSAPI MIC check failed");
318
319         buffer_free(&b);
320         xfree(mic.value);
321
322         authctxt->postponed = 0;
323         dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN, NULL);
324         dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_ERRTOK, NULL);
325         dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_MIC, NULL);
326         dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE, NULL);
327         userauth_finish(authctxt, authenticated, "gssapi-with-mic");
328 }
329
330 Authmethod method_gsskeyex = {
331         "gssapi-keyex",
332         userauth_gsskeyex,
333         &options.gss_authentication
334 };
335
336 Authmethod method_gssapi = {
337         "gssapi-with-mic",
338         userauth_gssapi,
339         &options.gss_authentication
340 };
341
342 #endif /* GSSAPI */
This page took 0.064735 seconds and 5 git commands to generate.