1178825Sdfr/*
2178825Sdfr * Copyright (c) 2008 Kungliga Tekniska H�gskolan
3178825Sdfr * (Royal Institute of Technology, Stockholm, Sweden).
4178825Sdfr * All rights reserved.
5178825Sdfr *
6178825Sdfr * Redistribution and use in source and binary forms, with or without
7178825Sdfr * modification, are permitted provided that the following conditions
8178825Sdfr * are met:
9178825Sdfr *
10178825Sdfr * 1. Redistributions of source code must retain the above copyright
11178825Sdfr *    notice, this list of conditions and the following disclaimer.
12178825Sdfr *
13178825Sdfr * 2. Redistributions in binary form must reproduce the above copyright
14178825Sdfr *    notice, this list of conditions and the following disclaimer in the
15178825Sdfr *    documentation and/or other materials provided with the distribution.
16178825Sdfr *
17178825Sdfr * 3. Neither the name of KTH nor the names of its contributors may be
18178825Sdfr *    used to endorse or promote products derived from this software without
19178825Sdfr *    specific prior written permission.
20178825Sdfr *
21178825Sdfr * THIS SOFTWARE IS PROVIDED BY KTH AND ITS CONTRIBUTORS ``AS IS'' AND ANY
22178825Sdfr * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23178825Sdfr * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24178825Sdfr * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL KTH OR ITS CONTRIBUTORS BE
25178825Sdfr * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26178825Sdfr * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27178825Sdfr * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
28178825Sdfr * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
29178825Sdfr * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
30178825Sdfr * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
31178825Sdfr * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
32178825Sdfr
33178825Sdfr#include "krb5_locl.h"
34178825Sdfr#include <err.h>
35178825Sdfr#include <getarg.h>
36178825Sdfr
37178825SdfrRCSID("$Id$");
38178825Sdfr
39178825Sdfrstatic int version_flag = 0;
40178825Sdfrstatic int help_flag	= 0;
41178825Sdfr
42178825Sdfrstatic struct getargs args[] = {
43178825Sdfr    {"version",	0,	arg_flag,	&version_flag,
44178825Sdfr     "print version", NULL },
45178825Sdfr    {"help",	0,	arg_flag,	&help_flag,
46178825Sdfr     NULL, NULL }
47178825Sdfr};
48178825Sdfr
49178825Sdfrstatic void
50178825Sdfrusage (int ret)
51178825Sdfr{
52178825Sdfr    arg_printusage (args,
53178825Sdfr		    sizeof(args)/sizeof(*args),
54178825Sdfr		    NULL,
55178825Sdfr		    "hostname");
56178825Sdfr    exit (ret);
57178825Sdfr}
58178825Sdfr
59178825Sdfrint
60178825Sdfrmain(int argc, char **argv)
61178825Sdfr{
62178825Sdfr    const char *hostname;
63178825Sdfr    krb5_context context;
64178825Sdfr    krb5_auth_context ac;
65178825Sdfr    krb5_error_code ret;
66178825Sdfr    krb5_creds cred;
67178825Sdfr    krb5_ccache id;
68178825Sdfr    krb5_data data;
69178825Sdfr    int optidx = 0;
70178825Sdfr
71178825Sdfr    setprogname (argv[0]);
72178825Sdfr
73178825Sdfr    if(getarg(args, sizeof(args) / sizeof(args[0]), argc, argv, &optidx))
74178825Sdfr	usage(1);
75178825Sdfr
76178825Sdfr    if (help_flag)
77178825Sdfr	usage (0);
78178825Sdfr
79178825Sdfr    if(version_flag){
80178825Sdfr	print_version(NULL);
81178825Sdfr	exit(0);
82178825Sdfr    }
83178825Sdfr
84178825Sdfr    argc -= optidx;
85178825Sdfr    argv += optidx;
86178825Sdfr
87178825Sdfr    if (argc < 1)
88178825Sdfr	usage(1);
89178825Sdfr
90178825Sdfr    hostname = argv[0];
91178825Sdfr
92178825Sdfr    memset(&cred, 0, sizeof(cred));
93178825Sdfr
94178825Sdfr    ret = krb5_init_context(&context);
95178825Sdfr    if (ret)
96178825Sdfr	errx (1, "krb5_init_context failed: %d", ret);
97178825Sdfr
98178825Sdfr    ret = krb5_cc_default(context, &id);
99178825Sdfr    if (ret)
100178825Sdfr	krb5_err(context, 1, ret, "krb5_cc_default failed: %d", ret);
101178825Sdfr
102178825Sdfr    ret = krb5_auth_con_init(context, &ac);
103178825Sdfr    if (ret)
104178825Sdfr	krb5_err(context, 1, ret, "krb5_auth_con_init failed: %d", ret);
105178825Sdfr
106178825Sdfr    krb5_auth_con_addflags(context, ac,
107178825Sdfr			   KRB5_AUTH_CONTEXT_CLEAR_FORWARDED_CRED, NULL);
108178825Sdfr
109178825Sdfr    ret = krb5_cc_get_principal(context, id, &cred.client);
110178825Sdfr    if (ret)
111178825Sdfr	krb5_err(context, 1, ret, "krb5_cc_get_principal");
112178825Sdfr
113178825Sdfr    ret = krb5_make_principal(context,
114178825Sdfr			      &cred.server,
115178825Sdfr			      krb5_principal_get_realm(context, cred.client),
116178825Sdfr			      KRB5_TGS_NAME,
117178825Sdfr			      krb5_principal_get_realm(context, cred.client),
118178825Sdfr			      NULL);
119178825Sdfr    if (ret)
120178825Sdfr	krb5_err(context, 1, ret, "krb5_make_principal(server)");
121178825Sdfr
122178825Sdfr    ret = krb5_get_forwarded_creds (context,
123178825Sdfr				    ac,
124178825Sdfr				    id,
125178825Sdfr				    KDC_OPT_FORWARDABLE,
126178825Sdfr				    hostname,
127178825Sdfr				    &cred,
128178825Sdfr				    &data);
129178825Sdfr    if (ret)
130178825Sdfr	krb5_err (context, 1, ret, "krb5_get_forwarded_creds");
131178825Sdfr
132178825Sdfr    krb5_data_free(&data);
133178825Sdfr    krb5_free_context(context);
134178825Sdfr
135178825Sdfr    return 0;
136178825Sdfr}
137