]> andersk Git - gssapi-openssh.git/blob - openssh/auth2-gss.c
fix for http://grid.ncsa.uiuc.edu/ssh/implicitlogin.adv vulnerability:
[gssapi-openssh.git] / openssh / auth2-gss.c
1 /*      $OpenBSD: auth2-gss.c,v 1.7 2003/11/21 11:57:03 djm 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 "compat.h"
38 #include "packet.h"
39 #include "monitor_wrap.h"
40
41 #include "ssh-gss.h"
42
43 extern ServerOptions options;
44
45 static void ssh_gssapi_userauth_error(Gssctxt *ctxt);
46 static void input_gssapi_token(int type, u_int32_t plen, void *ctxt);
47 static void input_gssapi_mic(int type, u_int32_t plen, void *ctxt);
48 static void input_gssapi_exchange_complete(int type, u_int32_t plen, void *ctxt);
49 static void input_gssapi_errtok(int, u_int32_t, void *);
50
51 static int gssapi_with_mic = 1; /* flag to toggle "gssapi-with-mic" vs.
52                                    "gssapi" */
53
54 static int
55 userauth_external(Authctxt *authctxt)
56 {
57         packet_check_eom();
58
59         if (authctxt->valid && strcmp(authctxt->user, "") != 0) {
60             return(PRIVSEP(ssh_gssapi_userok(authctxt->user)));
61         }
62         return 0;
63 }
64
65 /*
66  * We only support those mechanisms that we know about (ie ones that we know
67  * how to check local user kuserok and the like
68  */
69 static int
70 userauth_gssapi(Authctxt *authctxt)
71 {
72         gss_OID_desc oid = {0, NULL};
73         Gssctxt *ctxt = NULL;
74         int mechs;
75         gss_OID_set supported;
76         int present;
77         OM_uint32 ms;
78         u_int len;
79         char *doid = NULL;
80
81         if (authctxt->user == NULL)
82                 return (0);
83
84         mechs = packet_get_int();
85         if (mechs == 0) {
86                 debug("Mechanism negotiation is not supported");
87                 return (0);
88         }
89
90         ssh_gssapi_supported_oids(&supported);
91         do {
92                 mechs--;
93
94                 if (doid)
95                         xfree(doid);
96
97                 present = 0;
98                 doid = packet_get_string(&len);
99
100                 if (doid[0] != SSH_GSS_OIDTYPE || doid[1] != len-2) {
101                         logit("Mechanism OID received using the old encoding form");
102                         oid.elements = doid;
103                         oid.length = len;
104                 } else {
105                         oid.elements = doid + 2;
106                         oid.length   = len - 2;
107                 }
108                 gss_test_oid_set_member(&ms, &oid, supported, &present);
109         } while (mechs > 0 && !present);
110
111         gss_release_oid_set(&ms, &supported);
112
113         if (!present) {
114                 xfree(doid);
115                 return (0);
116         }
117
118         if (GSS_ERROR(PRIVSEP(ssh_gssapi_server_ctx(&ctxt, &oid)))) {
119                 xfree(doid);
120                 return (0);
121         }
122
123         authctxt->methoddata=(void *)ctxt;
124
125         packet_start(SSH2_MSG_USERAUTH_GSSAPI_RESPONSE);
126
127         /* Return the OID that we received */
128         packet_put_string(doid, len);
129
130         packet_send();
131         xfree(doid);
132
133         dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN, &input_gssapi_token);
134         dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_ERRTOK, &input_gssapi_errtok);
135         authctxt->postponed = 1;
136
137         return (0);
138 }
139
140 static void
141 input_gssapi_token(int type, u_int32_t plen, void *ctxt)
142 {
143         Authctxt *authctxt = ctxt;
144         Gssctxt *gssctxt;
145         gss_buffer_desc send_tok = GSS_C_EMPTY_BUFFER;
146         gss_buffer_desc recv_tok;
147         OM_uint32 maj_status, min_status, flags=0;
148         u_int len;
149
150         if (authctxt == NULL || (authctxt->methoddata == NULL && !use_privsep))
151                 fatal("No authentication or GSSAPI context");
152
153         gssctxt = authctxt->methoddata;
154         recv_tok.value = packet_get_string(&len);
155         recv_tok.length = len; /* u_int vs. size_t */
156
157         packet_check_eom();
158
159         maj_status = PRIVSEP(ssh_gssapi_accept_ctx(gssctxt, &recv_tok,
160             &send_tok, &flags));
161
162         xfree(recv_tok.value);
163
164         if (GSS_ERROR(maj_status)) {
165                 ssh_gssapi_userauth_error(gssctxt);
166                 if (send_tok.length != 0) {
167                         packet_start(SSH2_MSG_USERAUTH_GSSAPI_ERRTOK);
168                         packet_put_string(send_tok.value, send_tok.length);
169                         packet_send();
170                 }
171                 authctxt->postponed = 0;
172                 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN, NULL);
173                 userauth_finish(authctxt, 0,
174                                 gssapi_with_mic ? "gssapi-with-mic" :
175                                                   "gssapi");
176         } else {
177                 if (send_tok.length != 0) {
178                         packet_start(SSH2_MSG_USERAUTH_GSSAPI_TOKEN);
179                         packet_put_string(send_tok.value, send_tok.length);
180                         packet_send();
181                 }
182                 if (maj_status == GSS_S_COMPLETE) {
183                         dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN, NULL);
184                         if (flags & GSS_C_INTEG_FLAG && gssapi_with_mic)
185                                 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_MIC,
186                                     &input_gssapi_mic);
187                         else
188                                 dispatch_set(
189                                     SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE,
190                                     &input_gssapi_exchange_complete);
191                 }
192         }
193
194         gss_release_buffer(&min_status, &send_tok);
195 }
196
197 static void
198 input_gssapi_errtok(int type, u_int32_t plen, void *ctxt)
199 {
200         Authctxt *authctxt = ctxt;
201         Gssctxt *gssctxt;
202         gss_buffer_desc send_tok = GSS_C_EMPTY_BUFFER;
203         gss_buffer_desc recv_tok;
204         OM_uint32 maj_status;
205         u_int len;
206
207         if (authctxt == NULL || (authctxt->methoddata == NULL && !use_privsep))
208                 fatal("No authentication or GSSAPI context");
209
210         gssctxt = authctxt->methoddata;
211         recv_tok.value = packet_get_string(&len);
212         recv_tok.length = len;
213
214         packet_check_eom();
215
216         /* Push the error token into GSSAPI to see what it says */
217         maj_status = PRIVSEP(ssh_gssapi_accept_ctx(gssctxt, &recv_tok,
218             &send_tok, NULL));
219
220         xfree(recv_tok.value);
221
222         /* We can't return anything to the client, even if we wanted to */
223         dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN, NULL);
224         dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_ERRTOK, NULL);
225
226         /* The client will have already moved on to the next auth */
227
228         gss_release_buffer(&maj_status, &send_tok);
229 }
230
231 static void
232 gssapi_set_implicit_username(Authctxt *authctxt)
233 {
234     if ((strcmp(authctxt->user, "") == 0) && (authctxt->pw == NULL)) {
235         char *lname = NULL;
236         PRIVSEP(ssh_gssapi_localname(&lname));
237         if (lname && lname[0] != '\0') {
238             xfree(authctxt->user);
239             authctxt->user = lname;
240             debug("set username to %s from gssapi context", lname);
241             authctxt->pw = PRIVSEP(getpwnamallow(authctxt->user));
242             if (authctxt->pw) {
243                 authctxt->valid = 1;
244             }
245         } else {
246             debug("failed to set username from gssapi context");
247         }
248     }
249     if (authctxt->pw) {
250 #ifdef USE_PAM
251         if (options.use_pam)
252                 PRIVSEP(start_pam(authctxt));
253 #endif
254     }
255 }
256
257 /*
258  * This is called when the client thinks we've completed authentication.
259  * It should only be enabled in the dispatch handler by the function above,
260  * which only enables it once the GSSAPI exchange is complete.
261  */
262
263 static void
264 input_gssapi_exchange_complete(int type, u_int32_t plen, void *ctxt)
265 {
266         Authctxt *authctxt = ctxt;
267         Gssctxt *gssctxt;
268         int authenticated;
269
270         if (authctxt == NULL || (authctxt->methoddata == NULL && !use_privsep))
271                 fatal("No authentication or GSSAPI context");
272
273         gssapi_set_implicit_username(authctxt);
274
275         gssctxt = authctxt->methoddata;
276
277         /*
278          * We don't need to check the status, because we're only enabled in
279          * the dispatcher once the exchange is complete
280          */
281
282         packet_check_eom();
283
284         if (authctxt->valid && strcmp(authctxt->user, "") != 0) {
285             authenticated = PRIVSEP(ssh_gssapi_userok(authctxt->user));
286         } else {
287             authenticated = 0;
288         }
289
290         authctxt->postponed = 0;
291         dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN, NULL);
292         dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_ERRTOK, NULL);
293         dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_MIC, NULL);
294         dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE, NULL);
295         userauth_finish(authctxt, authenticated,
296                         gssapi_with_mic ? "gssapi-with-mic" : "gssapi");
297 }
298
299 static int
300 userauth_gssapi_with_mic(Authctxt *authctxt)
301 {
302     gssapi_with_mic = 1;
303     return userauth_gssapi(authctxt);
304 }
305
306 static int
307 userauth_gssapi_without_mic(Authctxt *authctxt)
308 {
309     gssapi_with_mic = 0;
310     return userauth_gssapi(authctxt);
311 }
312
313 static void
314 input_gssapi_mic(int type, u_int32_t plen, void *ctxt)
315 {
316         Authctxt *authctxt = ctxt;
317         Gssctxt *gssctxt;
318         int authenticated = 0;
319         Buffer b;
320         gss_buffer_desc mic, gssbuf;
321         u_int len;
322
323         if (authctxt == NULL || (authctxt->methoddata == NULL && !use_privsep))
324                 fatal("No authentication or GSSAPI context");
325
326         gssapi_set_implicit_username(authctxt);
327
328         gssctxt = authctxt->methoddata;
329
330         mic.value = packet_get_string(&len);
331         mic.length = len;
332
333         ssh_gssapi_buildmic(&b, authctxt->user, authctxt->service,
334             "gssapi-with-mic");
335
336         gssbuf.value = buffer_ptr(&b);
337         gssbuf.length = buffer_len(&b);
338
339         if (!GSS_ERROR(PRIVSEP(ssh_gssapi_checkmic(gssctxt, &gssbuf, &mic))))
340             if (authctxt->valid && strcmp(authctxt->user, "") != 0) {
341                 authenticated = PRIVSEP(ssh_gssapi_userok(authctxt->user));
342             } else {
343                 authenticated = 0;
344             }
345         else
346                 logit("GSSAPI MIC check failed");
347
348         buffer_free(&b);
349         xfree(mic.value);
350
351         authctxt->postponed = 0;
352         dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN, NULL);
353         dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_ERRTOK, NULL);
354         dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_MIC, NULL);
355         dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE, NULL);
356         userauth_finish(authctxt, authenticated, "gssapi-with-mic");
357 }
358
359 static void ssh_gssapi_userauth_error(Gssctxt *ctxt) {
360         char *errstr;
361         OM_uint32 maj,min;
362         
363         errstr=PRIVSEP(ssh_gssapi_last_error(ctxt,&maj,&min));
364         if (errstr) {
365                 packet_start(SSH2_MSG_USERAUTH_GSSAPI_ERROR);
366                 packet_put_int(maj);
367                 packet_put_int(min);
368                 packet_put_cstring(errstr);
369                 packet_put_cstring("");
370                 packet_send();
371                 packet_write_wait();
372                 xfree(errstr);
373         }
374 }
375
376 Authmethod method_external = {
377         "external-keyx",
378         userauth_external,
379         &options.gss_authentication
380 };
381         
382 Authmethod method_gssapi = {
383         "gssapi-with-mic",
384         userauth_gssapi_with_mic,
385         &options.gss_authentication
386 };
387
388 Authmethod method_gssapi_compat = {
389         "gssapi",
390         userauth_gssapi_without_mic,
391         &options.gss_authentication
392 };
393
394 #endif /* GSSAPI */
This page took 0.187895 seconds and 5 git commands to generate.