]> andersk Git - gssapi-openssh.git/blob - openssh/gss-serv.c
The man2html from jbasney on pkilab2 works whereas the standard one doesn't.
[gssapi-openssh.git] / openssh / gss-serv.c
1 /* $OpenBSD: gss-serv.c,v 1.22 2008/05/08 12:02:23 djm Exp $ */
2
3 /*
4  * Copyright (c) 2001-2009 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 #include <sys/param.h>
33
34 #include <stdarg.h>
35 #include <string.h>
36 #include <unistd.h>
37
38 #include "openbsd-compat/sys-queue.h"
39 #include "xmalloc.h"
40 #include "buffer.h"
41 #include "key.h"
42 #include "hostfile.h"
43 #include "auth.h"
44 #include "log.h"
45 #include "channels.h"
46 #include "session.h"
47 #include "misc.h"
48 #include "servconf.h"
49 #include "uidswap.h"
50
51 #include "xmalloc.h"
52 #include "ssh-gss.h"
53 #include "monitor_wrap.h"
54
55 extern ServerOptions options;
56 extern Authctxt *the_authctxt;
57
58 static ssh_gssapi_client gssapi_client =
59     { GSS_C_EMPTY_BUFFER, GSS_C_EMPTY_BUFFER,
60     GSS_C_NO_CREDENTIAL, GSS_C_NO_NAME,  NULL, {NULL, NULL, NULL}, 0, 0};
61
62 ssh_gssapi_mech gssapi_null_mech =
63     { NULL, NULL, {0, NULL}, NULL, NULL, NULL, NULL, NULL};
64
65 #ifdef KRB5
66 extern ssh_gssapi_mech gssapi_kerberos_mech;
67 #endif
68 #ifdef GSI
69 extern ssh_gssapi_mech gssapi_gsi_mech;
70 #endif
71
72 ssh_gssapi_mech* supported_mechs[]= {
73 #ifdef KRB5
74         &gssapi_kerberos_mech,
75 #endif
76 #ifdef GSI
77         &gssapi_gsi_mech,
78 #endif
79         &gssapi_null_mech,
80 };
81
82 #ifdef GSS_C_GLOBUS_LIMITED_PROXY_FLAG
83 static int limited = 0;
84 #endif
85
86 /*
87  * Acquire credentials for a server running on the current host.
88  * Requires that the context structure contains a valid OID
89  */
90
91 /* Returns a GSSAPI error code */
92 /* Privileged (called from ssh_gssapi_server_ctx) */
93 static OM_uint32
94 ssh_gssapi_acquire_cred(Gssctxt *ctx)
95 {
96         OM_uint32 status;
97         char lname[MAXHOSTNAMELEN];
98         gss_OID_set oidset;
99
100         if (options.gss_strict_acceptor) {
101                 gss_create_empty_oid_set(&status, &oidset);
102                 gss_add_oid_set_member(&status, ctx->oid, &oidset);
103
104                 if (gethostname(lname, MAXHOSTNAMELEN)) {
105                         gss_release_oid_set(&status, &oidset);
106                         return (-1);
107                 }
108
109                 if (GSS_ERROR(ssh_gssapi_import_name(ctx, lname))) {
110                         gss_release_oid_set(&status, &oidset);
111                         return (ctx->major);
112                 }
113
114                 if ((ctx->major = gss_acquire_cred(&ctx->minor,
115                     ctx->name, 0, oidset, GSS_C_ACCEPT, &ctx->creds, 
116                     NULL, NULL)))
117                         ssh_gssapi_error(ctx);
118
119                 gss_release_oid_set(&status, &oidset);
120                 return (ctx->major);
121         } else {
122                 ctx->name = GSS_C_NO_NAME;
123                 ctx->creds = GSS_C_NO_CREDENTIAL;
124         }
125         return GSS_S_COMPLETE;
126 }
127
128 /* Privileged */
129 OM_uint32
130 ssh_gssapi_server_ctx(Gssctxt **ctx, gss_OID oid)
131 {
132         if (*ctx)
133                 ssh_gssapi_delete_ctx(ctx);
134         ssh_gssapi_build_ctx(ctx);
135         ssh_gssapi_set_oid(*ctx, oid);
136         return (ssh_gssapi_acquire_cred(*ctx));
137 }
138
139 /* Unprivileged */
140 char *
141 ssh_gssapi_server_mechanisms() {
142         gss_OID_set     supported;
143
144         ssh_gssapi_supported_oids(&supported);
145         return (ssh_gssapi_kex_mechs(supported, &ssh_gssapi_server_check_mech,
146             NULL, NULL));
147 }
148
149 /* Unprivileged */
150 int
151 ssh_gssapi_server_check_mech(Gssctxt **dum, gss_OID oid, const char *data,
152     const char *dummy) {
153         Gssctxt *ctx = NULL;
154         int res;
155  
156         res = !GSS_ERROR(PRIVSEP(ssh_gssapi_server_ctx(&ctx, oid)));
157         ssh_gssapi_delete_ctx(&ctx);
158
159         return (res);
160 }
161
162 /* Unprivileged */
163 void
164 ssh_gssapi_supported_oids(gss_OID_set *oidset)
165 {
166         int i = 0;
167         OM_uint32 min_status;
168         int present;
169         gss_OID_set supported;
170
171         gss_create_empty_oid_set(&min_status, oidset);
172
173         /* Ask privileged process what mechanisms it supports. */
174         if (GSS_ERROR(PRIVSEP(gss_indicate_mechs(&min_status, &supported))))
175                 return;
176
177         while (supported_mechs[i]->name != NULL) {
178                 if (GSS_ERROR(gss_test_oid_set_member(&min_status,
179                     &supported_mechs[i]->oid, supported, &present)))
180                         present = 0;
181                 if (present)
182                         gss_add_oid_set_member(&min_status,
183                             &supported_mechs[i]->oid, oidset);
184                 i++;
185         }
186
187         gss_release_oid_set(&min_status, &supported);
188 }
189
190
191 /* Wrapper around accept_sec_context
192  * Requires that the context contains:
193  *    oid
194  *    credentials       (from ssh_gssapi_acquire_cred)
195  */
196 /* Privileged */
197 OM_uint32
198 ssh_gssapi_accept_ctx(Gssctxt *ctx, gss_buffer_desc *recv_tok,
199     gss_buffer_desc *send_tok, OM_uint32 *flags)
200 {
201         OM_uint32 status;
202         gss_OID mech;
203
204         ctx->major = gss_accept_sec_context(&ctx->minor,
205             &ctx->context, ctx->creds, recv_tok,
206             GSS_C_NO_CHANNEL_BINDINGS, &ctx->client, &mech,
207             send_tok, flags, NULL, &ctx->client_creds);
208
209         if (GSS_ERROR(ctx->major))
210                 ssh_gssapi_error(ctx);
211
212         if (ctx->client_creds)
213                 debug("Received some client credentials");
214         else
215                 debug("Got no client credentials");
216
217         status = ctx->major;
218
219         /* Now, if we're complete and we have the right flags, then
220          * we flag the user as also having been authenticated
221          */
222
223         if (((flags == NULL) || ((*flags & GSS_C_MUTUAL_FLAG) &&
224             (*flags & GSS_C_INTEG_FLAG))) && (ctx->major == GSS_S_COMPLETE)) {
225                 if (ssh_gssapi_getclient(ctx, &gssapi_client))
226                         fatal("Couldn't convert client name");
227 #ifdef GSS_C_GLOBUS_LIMITED_PROXY_FLAG
228                 if (flags && (*flags & GSS_C_GLOBUS_LIMITED_PROXY_FLAG))
229                         limited=1;
230 #endif
231         }
232
233         return (status);
234 }
235
236 /*
237  * This parses an exported name, extracting the mechanism specific portion
238  * to use for ACL checking. It verifies that the name belongs the mechanism
239  * originally selected.
240  */
241 static OM_uint32
242 ssh_gssapi_parse_ename(Gssctxt *ctx, gss_buffer_t ename, gss_buffer_t name)
243 {
244         u_char *tok;
245         OM_uint32 offset;
246         OM_uint32 oidl;
247
248         tok = ename->value;
249
250 #ifdef GSI /* GSI gss_export_name() is broken. */
251         if ((ctx->oid->length == gssapi_gsi_mech.oid.length) &&
252             (memcmp(ctx->oid->elements, gssapi_gsi_mech.oid.elements,
253                     gssapi_gsi_mech.oid.length) == 0)) {
254             name->length = ename->length;
255             name->value = xmalloc(ename->length+1);
256             memcpy(name->value, ename->value, ename->length);
257             return GSS_S_COMPLETE;
258         }
259 #endif
260
261         /*
262          * Check that ename is long enough for all of the fixed length
263          * header, and that the initial ID bytes are correct
264          */
265
266         if (ename->length < 6 || memcmp(tok, "\x04\x01", 2) != 0)
267                 return GSS_S_FAILURE;
268
269         /*
270          * Extract the OID, and check it. Here GSSAPI breaks with tradition
271          * and does use the OID type and length bytes. To confuse things
272          * there are two lengths - the first including these, and the
273          * second without.
274          */
275
276         oidl = get_u16(tok+2); /* length including next two bytes */
277         oidl = oidl-2; /* turn it into the _real_ length of the variable OID */
278
279         /*
280          * Check the BER encoding for correct type and length, that the
281          * string is long enough and that the OID matches that in our context
282          */
283         if (tok[4] != 0x06 || tok[5] != oidl ||
284             ename->length < oidl+6 ||
285             !ssh_gssapi_check_oid(ctx, tok+6, oidl))
286                 return GSS_S_FAILURE;
287
288         offset = oidl+6;
289
290         if (ename->length < offset+4)
291                 return GSS_S_FAILURE;
292
293         name->length = get_u32(tok+offset);
294         offset += 4;
295
296         if (ename->length < offset+name->length)
297                 return GSS_S_FAILURE;
298
299         name->value = xmalloc(name->length+1);
300         memcpy(name->value, tok+offset, name->length);
301         ((char *)name->value)[name->length] = 0;
302
303         return GSS_S_COMPLETE;
304 }
305
306 /* Extract the client details from a given context. This can only reliably
307  * be called once for a context */
308
309 /* Privileged (called from accept_secure_ctx) */
310 OM_uint32
311 ssh_gssapi_getclient(Gssctxt *ctx, ssh_gssapi_client *client)
312 {
313         int i = 0;
314         int equal = 0;
315         gss_name_t new_name = GSS_C_NO_NAME;
316         gss_buffer_desc ename = GSS_C_EMPTY_BUFFER;
317
318         if (options.gss_store_rekey && client->used && ctx->client_creds) {
319                 if (client->mech->oid.length != ctx->oid->length ||
320                     (memcmp(client->mech->oid.elements,
321                      ctx->oid->elements, ctx->oid->length) !=0)) {
322                         debug("Rekeyed credentials have different mechanism");
323                         return GSS_S_COMPLETE;
324                 }
325
326         /* Call gss_inquire_cred rather than gss_inquire_cred_by_mech
327            because GSI doesn't support the latter. -jbasney */
328
329                 if ((ctx->major = gss_inquire_cred(&ctx->minor, 
330                     ctx->client_creds, &new_name, 
331                     NULL, NULL, NULL))) {
332                         ssh_gssapi_error(ctx);
333                         return (ctx->major);
334                 }
335
336                 ctx->major = gss_compare_name(&ctx->minor, client->name, 
337                     new_name, &equal);
338
339                 if (GSS_ERROR(ctx->major)) {
340                         ssh_gssapi_error(ctx);
341                         return (ctx->major);
342                 }
343  
344                 if (!equal) {
345                         debug("Rekeyed credentials have different name");
346                         return GSS_S_COMPLETE;
347                 }
348
349                 debug("Marking rekeyed credentials for export");
350
351                 gss_release_name(&ctx->minor, &client->name);
352                 gss_release_cred(&ctx->minor, &client->creds);
353                 client->name = new_name;
354                 client->creds = ctx->client_creds;
355                 ctx->client_creds = GSS_C_NO_CREDENTIAL;
356                 client->updated = 1;
357                 return GSS_S_COMPLETE;
358         }
359
360         client->mech = NULL;
361
362         while (supported_mechs[i]->name != NULL) {
363                 if (supported_mechs[i]->oid.length == ctx->oid->length &&
364                     (memcmp(supported_mechs[i]->oid.elements,
365                     ctx->oid->elements, ctx->oid->length) == 0))
366                         client->mech = supported_mechs[i];
367                 i++;
368         }
369
370         if (client->mech == NULL)
371                 return GSS_S_FAILURE;
372
373     /* Call gss_inquire_cred rather than gss_inquire_cred_by_mech
374        because GSI doesn't support the latter. -jbasney */
375
376         if (ctx->client_creds &&
377             (ctx->major = gss_inquire_cred(&ctx->minor,
378              ctx->client_creds, &client->name, NULL, NULL, NULL))) {
379                 ssh_gssapi_error(ctx);
380                 return (ctx->major);
381         }
382
383         if ((ctx->major = gss_display_name(&ctx->minor, ctx->client,
384             &client->displayname, NULL))) {
385                 ssh_gssapi_error(ctx);
386                 return (ctx->major);
387         }
388
389         if ((ctx->major = gss_export_name(&ctx->minor, ctx->client,
390             &ename))) {
391                 ssh_gssapi_error(ctx);
392                 return (ctx->major);
393         }
394
395         if ((ctx->major = ssh_gssapi_parse_ename(ctx,&ename,
396             &client->exportedname))) {
397                 return (ctx->major);
398         }
399
400         gss_release_buffer(&ctx->minor, &ename);
401
402         /* We can't copy this structure, so we just move the pointer to it */
403         client->creds = ctx->client_creds;
404         ctx->client_creds = GSS_C_NO_CREDENTIAL;
405
406     /* needed for globus_gss_assist_map_and_authorize() */
407     client->context = ctx->context;
408
409         return (ctx->major);
410 }
411
412 /* As user - called on fatal/exit */
413 void
414 ssh_gssapi_cleanup_creds(void)
415 {
416         if (gssapi_client.store.filename != NULL) {
417                 /* Unlink probably isn't sufficient */
418                 debug("removing gssapi cred file\"%s\"",
419                     gssapi_client.store.filename);
420                 unlink(gssapi_client.store.filename);
421         }
422 }
423
424 /* As user */
425 void
426 ssh_gssapi_storecreds(void)
427 {
428         if (gssapi_client.mech && gssapi_client.mech->storecreds) {
429         if (options.gss_creds_path) {
430             gssapi_client.store.filename =
431                 expand_authorized_keys(options.gss_creds_path,
432                                        the_authctxt->pw);
433         }
434                 (*gssapi_client.mech->storecreds)(&gssapi_client);
435         } else
436                 debug("ssh_gssapi_storecreds: Not a GSSAPI mechanism");
437 }
438
439 /* This allows GSSAPI methods to do things to the childs environment based
440  * on the passed authentication process and credentials.
441  */
442 /* As user */
443 void
444 ssh_gssapi_do_child(char ***envp, u_int *envsizep)
445 {
446
447         if (gssapi_client.store.envvar != NULL &&
448             gssapi_client.store.envval != NULL) {
449                 debug("Setting %s to %s", gssapi_client.store.envvar,
450                     gssapi_client.store.envval);
451                 child_set_env(envp, envsizep, gssapi_client.store.envvar,
452                     gssapi_client.store.envval);
453         }
454 }
455
456 /* Privileged */
457 int
458 ssh_gssapi_userok(char *user, struct passwd *pw, int gssapi_keyex)
459 {
460         OM_uint32 lmin;
461
462         if (gssapi_client.exportedname.length == 0 ||
463             gssapi_client.exportedname.value == NULL) {
464                 debug("No suitable client data");
465                 return 0;
466         }
467 #ifdef GSS_C_GLOBUS_LIMITED_PROXY_FLAG
468         if (limited && options.gsi_allow_limited_proxy != 1) {
469                 debug("limited proxy not acceptable for remote login");
470                 return 0;
471         }
472 #endif
473         if (gssapi_client.mech && gssapi_client.mech->userok)
474                 if ((*gssapi_client.mech->userok)(&gssapi_client, user)) {
475                         gssapi_client.used = 1;
476                         gssapi_client.store.owner = pw;
477                         return 1;
478                 } else {
479                         /* Destroy delegated credentials if userok fails */
480                         gss_release_buffer(&lmin, &gssapi_client.displayname);
481                         gss_release_buffer(&lmin, &gssapi_client.exportedname);
482                         gss_release_cred(&lmin, &gssapi_client.creds);
483                         memset(&gssapi_client, 0, sizeof(ssh_gssapi_client));
484                         return 0;
485                 }
486         else
487                 debug("ssh_gssapi_userok: Unknown GSSAPI mechanism");
488         return (0);
489 }
490
491 /* ssh_gssapi_checkmic() moved to gss-genr.c so it can be called by
492    kexgss_client(). */
493
494 /* Priviledged */
495 int
496 ssh_gssapi_localname(char **user)
497 {
498         *user = NULL;
499         if (gssapi_client.displayname.length==0 || 
500             gssapi_client.displayname.value==NULL) {
501                 debug("No suitable client data");
502                 return(0);;
503         }
504         if (gssapi_client.mech && gssapi_client.mech->localname) {
505                 return((*gssapi_client.mech->localname)(&gssapi_client,user));
506         } else {
507                 debug("Unknown client authentication type");
508         }
509         return(0);
510 }
511
512 /* These bits are only used for rekeying. The unpriviledged child is running 
513  * as the user, the monitor is root.
514  *
515  * In the child, we want to :
516  *    *) Ask the monitor to store our credentials into the store we specify
517  *    *) If it succeeds, maybe do a PAM update
518  */
519
520 /* Stuff for PAM */
521
522 #ifdef USE_PAM
523 static int ssh_gssapi_simple_conv(int n, const struct pam_message **msg, 
524     struct pam_response **resp, void *data)
525 {
526         return (PAM_CONV_ERR);
527 }
528 #endif
529
530 void
531 ssh_gssapi_rekey_creds() {
532         int ok;
533 #ifdef USE_PAM
534         int ret;
535         pam_handle_t *pamh = NULL;
536         struct pam_conv pamconv = {ssh_gssapi_simple_conv, NULL};
537         char *envstr;
538         char **p;char **pw;
539 #endif
540
541         if (gssapi_client.store.filename == NULL && 
542             gssapi_client.store.envval == NULL &&
543             gssapi_client.store.envvar == NULL)
544                 return;
545  
546         ok = PRIVSEP(ssh_gssapi_update_creds(&gssapi_client.store));
547
548         if (!ok)
549                 return;
550
551         debug("Rekeyed credentials stored successfully");
552
553         /* Actually managing to play with the ssh pam stack from here will
554          * be next to impossible. In any case, we may want different options
555          * for rekeying. So, use our own :)
556          */
557 #ifdef USE_PAM  
558         if (!use_privsep) {
559                 debug("Not even going to try and do PAM with privsep disabled");
560                 return;
561         }
562
563         ret = pam_start("sshd-rekey", gssapi_client.store.owner->pw_name,
564             &pamconv, &pamh);
565         if (ret)
566                 return;
567
568         /* Put ssh pam stack env variables in this new pam stack env 
569          * Using pam-pkinit, KRB5CCNAME is set during do_pam_session
570          * this addition enables pam-pkinit to access KRB5CCNAME if used 
571          * in sshd-rekey stack too
572          */
573         pw = p = fetch_pam_environment();
574         while ( *pw != NULL ) {
575                 pam_putenv(pamh,*pw);
576                 pw++;
577         }
578         free_pam_environment(p);
579
580         xasprintf(&envstr, "%s=%s", gssapi_client.store.envvar, 
581             gssapi_client.store.envval);
582
583         ret = pam_putenv(pamh, envstr);
584         if (!ret)
585                 pam_setcred(pamh, PAM_REINITIALIZE_CRED);
586         pam_end(pamh, PAM_SUCCESS);
587 #endif
588 }
589
590 int 
591 ssh_gssapi_update_creds(ssh_gssapi_ccache *store) {
592         int ok = 0;
593
594         /* Check we've got credentials to store */
595         if (!gssapi_client.updated)
596                 return 0;
597
598         gssapi_client.updated = 0;
599
600         temporarily_use_uid(gssapi_client.store.owner);
601         if (gssapi_client.mech && gssapi_client.mech->updatecreds)
602                 ok = (*gssapi_client.mech->updatecreds)(store, &gssapi_client);
603         else
604                 debug("No update function for this mechanism");
605
606         restore_uid();
607
608         return ok;
609 }
610
611 void
612 ssh_gssapi_get_client_info(char **userdn, char **mech) {
613         *userdn = gssapi_client.displayname.value;
614
615         if (gssapi_client.mech)
616                 *mech = gssapi_client.mech->name;
617 }
618
619 #endif
This page took 0.095198 seconds and 5 git commands to generate.