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