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