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