1 /* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2 /* tests/forward.c - test harness for getting forwarded creds */
3 /*
4  * Copyright (C) 2016 by the Massachusetts Institute of Technology.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * * Redistributions of source code must retain the above copyright
12  *   notice, this list of conditions and the following disclaimer.
13  *
14  * * Redistributions in binary form must reproduce the above copyright
15  *   notice, this list of conditions and the following disclaimer in
16  *   the documentation and/or other materials provided with the
17  *   distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
22  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
23  * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
24  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
25  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
26  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
28  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
30  * OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32 
33 /* This test program overwrites the default credential cache with a forwarded
34  * TGT obtained using the TGT presently in the cache. */
35 
36 #include "k5-int.h"
37 
38 static krb5_context ctx;
39 
40 static void
check(krb5_error_code code)41 check(krb5_error_code code)
42 {
43     const char *errmsg;
44 
45     if (code) {
46         errmsg = krb5_get_error_message(ctx, code);
47         fprintf(stderr, "%s\n", errmsg);
48         krb5_free_error_message(ctx, errmsg);
49         exit(1);
50     }
51 }
52 
53 int
main()54 main()
55 {
56     krb5_ccache cc;
57     krb5_creds mcred, tgt, *fcred;
58     krb5_principal client, tgtprinc;
59     krb5_flags options;
60 
61     /* Open the default ccache and get the client and TGT principal names. */
62     check(krb5_init_context(&ctx));
63     check(krb5_cc_default(ctx, &cc));
64     check(krb5_cc_get_principal(ctx, cc, &client));
65     check(krb5_build_principal_ext(ctx, &tgtprinc, client->realm.length,
66                                    client->realm.data, KRB5_TGS_NAME_SIZE,
67                                    KRB5_TGS_NAME, client->realm.length,
68                                    client->realm.data, 0));
69 
70     /* Fetch the TGT credential. */
71     memset(&mcred, 0, sizeof(mcred));
72     mcred.client = client;
73     mcred.server = tgtprinc;
74     check(krb5_cc_retrieve_cred(ctx, cc, 0, &mcred, &tgt));
75 
76     /* Get a forwarded TGT. */
77     mcred.times = tgt.times;
78     mcred.times.starttime = 0;
79     options = (tgt.ticket_flags & KDC_TKT_COMMON_MASK) | KDC_OPT_FORWARDED;
80     check(krb5_get_cred_via_tkt(ctx, &tgt, options, NULL, &mcred, &fcred));
81 
82     /* Reinitialize the default ccache with the forwarded TGT. */
83     check(krb5_cc_initialize(ctx, cc, client));
84     check(krb5_cc_store_cred(ctx, cc, fcred));
85 
86     krb5_free_creds(ctx, fcred);
87     krb5_free_cred_contents(ctx, &tgt);
88     krb5_free_principal(ctx, tgtprinc);
89     krb5_free_principal(ctx, client);
90     krb5_cc_close(ctx, cc);
91     krb5_free_context(ctx);
92     return 0;
93 }
94