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