/* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */1/* tests/forward.c - test harness for getting forwarded creds */2/*3* Copyright (C) 2016 by the Massachusetts Institute of Technology.4* All rights reserved.5*6* Redistribution and use in source and binary forms, with or without7* modification, are permitted provided that the following conditions8* are met:9*10* * Redistributions of source code must retain the above copyright11* notice, this list of conditions and the following disclaimer.12*13* * Redistributions in binary form must reproduce the above copyright14* notice, this list of conditions and the following disclaimer in15* the documentation and/or other materials provided with the16* distribution.17*18* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS19* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT20* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS21* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE22* COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,23* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES24* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR25* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)26* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,27* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)28* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED29* OF THE POSSIBILITY OF SUCH DAMAGE.30*/3132/* This test program overwrites the default credential cache with a forwarded33* TGT obtained using the TGT presently in the cache. */3435#include "k5-int.h"3637static krb5_context ctx;3839static void40check(krb5_error_code code)41{42const char *errmsg;4344if (code) {45errmsg = krb5_get_error_message(ctx, code);46fprintf(stderr, "%s\n", errmsg);47krb5_free_error_message(ctx, errmsg);48exit(1);49}50}5152int53main(void)54{55krb5_ccache cc;56krb5_creds mcred, tgt, *fcred;57krb5_principal client, tgtprinc;58krb5_flags options;5960/* Open the default ccache and get the client and TGT principal names. */61check(krb5_init_context(&ctx));62check(krb5_cc_default(ctx, &cc));63check(krb5_cc_get_principal(ctx, cc, &client));64check(krb5_build_principal_ext(ctx, &tgtprinc, client->realm.length,65client->realm.data, KRB5_TGS_NAME_SIZE,66KRB5_TGS_NAME, client->realm.length,67client->realm.data, 0));6869/* Fetch the TGT credential. */70memset(&mcred, 0, sizeof(mcred));71mcred.client = client;72mcred.server = tgtprinc;73check(krb5_cc_retrieve_cred(ctx, cc, 0, &mcred, &tgt));7475/* Get a forwarded TGT. */76mcred.times = tgt.times;77mcred.times.starttime = 0;78options = (tgt.ticket_flags & KDC_TKT_COMMON_MASK) | KDC_OPT_FORWARDED;79check(krb5_get_cred_via_tkt(ctx, &tgt, options, NULL, &mcred, &fcred));8081/* Reinitialize the default ccache with the forwarded TGT. */82check(krb5_cc_initialize(ctx, cc, client));83check(krb5_cc_store_cred(ctx, cc, fcred));8485krb5_free_creds(ctx, fcred);86krb5_free_cred_contents(ctx, &tgt);87krb5_free_principal(ctx, tgtprinc);88krb5_free_principal(ctx, client);89krb5_cc_close(ctx, cc);90krb5_free_context(ctx);91return 0;92}939495