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