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