]> andersk Git - gssapi-openssh.git/blame - openssh/gss-serv.c
Added support for reporting usage metrics.
[gssapi-openssh.git] / openssh / gss-serv.c
CommitLineData
5156b1a1 1/* $OpenBSD: gss-serv.c,v 1.22 2008/05/08 12:02:23 djm Exp $ */
7cac2b65 2
5598e598 3/*
f97edba6 4 * Copyright (c) 2001-2009 Simon Wilkinson. All rights reserved.
5598e598 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
30460aeb 31#include <sys/types.h>
fa0f0f45 32#include <sys/param.h>
30460aeb 33
34#include <stdarg.h>
35#include <string.h>
36#include <unistd.h>
37
5156b1a1 38#include "openbsd-compat/sys-queue.h"
30460aeb 39#include "xmalloc.h"
40#include "buffer.h"
41#include "key.h"
42#include "hostfile.h"
5598e598 43#include "auth.h"
44#include "log.h"
e5affddc 45#include "channels.h"
5598e598 46#include "session.h"
f713db99 47#include "misc.h"
5598e598 48#include "servconf.h"
f97edba6 49#include "uidswap.h"
f713db99 50
7cac2b65 51#include "xmalloc.h"
5598e598 52#include "ssh-gss.h"
30460aeb 53#include "monitor_wrap.h"
5598e598 54
bd3336ab 55extern ServerOptions options;
ae82558b 56extern Authctxt *the_authctxt;
bd3336ab 57
23987cb8 58static ssh_gssapi_client gssapi_client =
7cac2b65 59 { GSS_C_EMPTY_BUFFER, GSS_C_EMPTY_BUFFER,
f97edba6 60 GSS_C_NO_CREDENTIAL, GSS_C_NO_NAME, NULL, {NULL, NULL, NULL}, 0, 0};
5598e598 61
7cac2b65 62ssh_gssapi_mech gssapi_null_mech =
f97edba6 63 { NULL, NULL, {0, NULL}, NULL, NULL, NULL, NULL, NULL};
44a053a3 64
5598e598 65#ifdef KRB5
23987cb8 66extern ssh_gssapi_mech gssapi_kerberos_mech;
54425c7a 67#endif
5598e598 68#ifdef GSI
23987cb8 69extern ssh_gssapi_mech gssapi_gsi_mech;
b311f64e 70#endif
71
23987cb8 72ssh_gssapi_mech* supported_mechs[]= {
5598e598 73#ifdef KRB5
7cac2b65 74 &gssapi_kerberos_mech,
5598e598 75#endif
76#ifdef GSI
7cac2b65 77 &gssapi_gsi_mech,
7b5a625b 78#endif
7cac2b65 79 &gssapi_null_mech,
23987cb8 80};
5598e598 81
b4cfa386 82#ifdef GSS_C_GLOBUS_LIMITED_PROXY_FLAG
83static int limited = 0;
84#endif
85
fa0f0f45 86/*
87 * Acquire credentials for a server running on the current host.
f713db99 88 * Requires that the context structure contains a valid OID
89 */
90
91/* Returns a GSSAPI error code */
fa0f0f45 92/* Privileged (called from ssh_gssapi_server_ctx) */
93static OM_uint32
f713db99 94ssh_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
fa0f0f45 128/* Privileged */
129OM_uint32
130ssh_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
f97edba6 139/* Unprivileged */
140char *
141ssh_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 */
150int
151ssh_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
c5b7701b 162/* Unprivileged */
163void
164ssh_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);
f97edba6 172
c7931c9a 173 /* Ask privileged process what mechanisms it supports. */
174 if (GSS_ERROR(PRIVSEP(gss_indicate_mechs(&min_status, &supported))))
f97edba6 175 return;
c5b7701b 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
7cac2b65 191/* Wrapper around accept_sec_context
192 * Requires that the context contains:
193 * oid
194 * credentials (from ssh_gssapi_acquire_cred)
195 */
08822d99 196/* Privileged */
7cac2b65 197OM_uint32
198ssh_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");
b4cfa386 227#ifdef GSS_C_GLOBUS_LIMITED_PROXY_FLAG
228 if (flags && (*flags & GSS_C_GLOBUS_LIMITED_PROXY_FLAG))
229 limited=1;
230#endif
7cac2b65 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 */
241static OM_uint32
242ssh_gssapi_parse_ename(Gssctxt *ctx, gss_buffer_t ename, gss_buffer_t name)
243{
2ce0bfe4 244 u_char *tok;
7cac2b65 245 OM_uint32 offset;
246 OM_uint32 oidl;
247
08822d99 248 tok = ename->value;
7cac2b65 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
08822d99 266 if (ename->length < 6 || memcmp(tok, "\x04\x01", 2) != 0)
7cac2b65 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
30460aeb 276 oidl = get_u16(tok+2); /* length including next two bytes */
7cac2b65 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 ||
08822d99 285 !ssh_gssapi_check_oid(ctx, tok+6, oidl))
7cac2b65 286 return GSS_S_FAILURE;
287
288 offset = oidl+6;
289
290 if (ename->length < offset+4)
291 return GSS_S_FAILURE;
292
30460aeb 293 name->length = get_u32(tok+offset);
7cac2b65 294 offset += 4;
295
296 if (ename->length < offset+name->length)
297 return GSS_S_FAILURE;
298
299 name->value = xmalloc(name->length+1);
30460aeb 300 memcpy(name->value, tok+offset, name->length);
7cac2b65 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
08822d99 309/* Privileged (called from accept_secure_ctx) */
7cac2b65 310OM_uint32
311ssh_gssapi_getclient(Gssctxt *ctx, ssh_gssapi_client *client)
312{
313 int i = 0;
f97edba6 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
c7931c9a 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,
f97edba6 331 NULL, NULL, NULL))) {
332 ssh_gssapi_error(ctx);
333 return (ctx->major);
334 }
7cac2b65 335
f97edba6 336 ctx->major = gss_compare_name(&ctx->minor, client->name,
337 new_name, &equal);
0fff78ff 338
f97edba6 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 }
7cac2b65 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
c7931c9a 373 /* Call gss_inquire_cred rather than gss_inquire_cred_by_mech
374 because GSI doesn't support the latter. -jbasney */
375
f97edba6 376 if (ctx->client_creds &&
c7931c9a 377 (ctx->major = gss_inquire_cred(&ctx->minor,
378 ctx->client_creds, &client->name, NULL, NULL, NULL))) {
f97edba6 379 ssh_gssapi_error(ctx);
380 return (ctx->major);
381 }
382
7cac2b65 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
f97edba6 400 gss_release_buffer(&ctx->minor, &ename);
401
7cac2b65 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;
682b256d 405
406 /* needed for globus_gss_assist_map_and_authorize() */
407 client->context = ctx->context;
408
7cac2b65 409 return (ctx->major);
410}
411
540d72c3 412/* As user - called on fatal/exit */
7cac2b65 413void
540d72c3 414ssh_gssapi_cleanup_creds(void)
7cac2b65 415{
416 if (gssapi_client.store.filename != NULL) {
417 /* Unlink probably isn't sufficient */
30460aeb 418 debug("removing gssapi cred file\"%s\"",
419 gssapi_client.store.filename);
7cac2b65 420 unlink(gssapi_client.store.filename);
421 }
422}
423
424/* As user */
425void
426ssh_gssapi_storecreds(void)
427{
428 if (gssapi_client.mech && gssapi_client.mech->storecreds) {
ae82558b 429 if (options.gss_creds_path) {
430 gssapi_client.store.filename =
431 expand_authorized_keys(options.gss_creds_path,
432 the_authctxt->pw);
433 }
7cac2b65 434 (*gssapi_client.mech->storecreds)(&gssapi_client);
7cac2b65 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 */
443void
444ssh_gssapi_do_child(char ***envp, u_int *envsizep)
445{
446
447 if (gssapi_client.store.envvar != NULL &&
448 gssapi_client.store.envval != NULL) {
7cac2b65 449 debug("Setting %s to %s", gssapi_client.store.envvar,
08822d99 450 gssapi_client.store.envval);
7cac2b65 451 child_set_env(envp, envsizep, gssapi_client.store.envvar,
2ce0bfe4 452 gssapi_client.store.envval);
7cac2b65 453 }
454}
455
08822d99 456/* Privileged */
7cac2b65 457int
f97edba6 458ssh_gssapi_userok(char *user, struct passwd *pw)
7cac2b65 459{
2ce0bfe4 460 OM_uint32 lmin;
461
7cac2b65 462 if (gssapi_client.exportedname.length == 0 ||
463 gssapi_client.exportedname.value == NULL) {
464 debug("No suitable client data");
465 return 0;
466 }
b4cfa386 467#ifdef GSS_C_GLOBUS_LIMITED_PROXY_FLAG
826a9049 468 if (limited && options.gsi_allow_limited_proxy != 1) {
b4cfa386 469 debug("limited proxy not acceptable for remote login");
470 return 0;
471 }
472#endif
7cac2b65 473 if (gssapi_client.mech && gssapi_client.mech->userok)
f97edba6 474 if ((*gssapi_client.mech->userok)(&gssapi_client, user)) {
475 gssapi_client.used = 1;
476 gssapi_client.store.owner = pw;
2ce0bfe4 477 return 1;
f97edba6 478 } else {
2ce0bfe4 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 }
7cac2b65 486 else
487 debug("ssh_gssapi_userok: Unknown GSSAPI mechanism");
488 return (0);
489}
490
c5b7701b 491/* ssh_gssapi_checkmic() moved to gss-genr.c so it can be called by
492 kexgss_client(). */
493
e23e524c 494/* Priviledged */
23987cb8 495int
496ssh_gssapi_localname(char **user)
497{
498 *user = NULL;
7cac2b65 499 if (gssapi_client.displayname.length==0 ||
500 gssapi_client.displayname.value==NULL) {
23987cb8 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}
540d72c3 511
f97edba6 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
523static int ssh_gssapi_simple_conv(int n, const struct pam_message **msg,
524 struct pam_response **resp, void *data)
cdd66111 525{
f97edba6 526 return (PAM_CONV_ERR);
527}
528#endif
cdd66111 529
f97edba6 530void
531ssh_gssapi_rekey_creds() {
532 int ok;
f97edba6 533#ifdef USE_PAM
bed61975 534 int ret;
f97edba6 535 pam_handle_t *pamh = NULL;
536 struct pam_conv pamconv = {ssh_gssapi_simple_conv, NULL};
537 char *envstr;
77fa2ab4 538 char **p;char **pw;
f97edba6 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
77fa2ab4 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
f97edba6 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
590int
591ssh_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;
cdd66111 609}
610
9f2c8cb9 611void
612ssh_gssapi_get_client_info(char **userdn, char **mech) {
613 *userdn = gssapi_client.displayname.value;
614 *mech = gssapi_client.mech->name;
615}
616
23987cb8 617#endif
This page took 0.171736 seconds and 5 git commands to generate.