1130812Smarcel/*
2130812Smarcel * Copyright (c) 2010 Kungliga Tekniska H��gskolan
3130812Smarcel * (Royal Institute of Technology, Stockholm, Sweden).
4130812Smarcel * All rights reserved.
5130812Smarcel *
6130812Smarcel * Redistribution and use in source and binary forms, with or without
7130812Smarcel * modification, are permitted provided that the following conditions
8130812Smarcel * are met:
9130812Smarcel *
10130812Smarcel * 1. Redistributions of source code must retain the above copyright
11130812Smarcel *    notice, this list of conditions and the following disclaimer.
12130812Smarcel *
13130812Smarcel * 2. Redistributions in binary form must reproduce the above copyright
14130812Smarcel *    notice, this list of conditions and the following disclaimer in the
15130812Smarcel *    documentation and/or other materials provided with the distribution.
16130812Smarcel *
17130812Smarcel * 3. Neither the name of the Institute nor the names of its contributors
18130812Smarcel *    may be used to endorse or promote products derived from this software
19130812Smarcel *    without specific prior written permission.
20130812Smarcel *
21130812Smarcel * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
22130812Smarcel * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23130812Smarcel * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24130812Smarcel * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
25130812Smarcel * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26130812Smarcel * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27130812Smarcel * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28130812Smarcel * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29130812Smarcel * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30130812Smarcel * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31130812Smarcel * SUCH DAMAGE.
32130812Smarcel */
33130812Smarcel
34130812Smarcel#include "kuser_locl.h"
35130812Smarcel#include <sl.h>
36130812Smarcel#include "kcc-commands.h"
37130812Smarcel
38130812Smarcelkrb5_context kcc_context;
39130812Smarcelstatic int version_flag;
40130812Smarcelstatic int help_flag;
41130812Smarcel
42130812Smarcelstatic struct getargs args[] = {
43130812Smarcel    { "version", 	0,   arg_flag, &version_flag, NULL, NULL },
44130812Smarcel    { "help",		0,   arg_flag, &help_flag, NULL, NULL }
45130812Smarcel};
46130812Smarcel
47130812Smarcelstatic void
48130812Smarcelusage(int ret)
49130812Smarcel{
50130812Smarcel    arg_printusage_i18n(args,
51130812Smarcel			sizeof(args)/sizeof(*args),
52130812Smarcel			N_("Usage: ", ""),
53130812Smarcel			NULL,
54130812Smarcel			"command ..",
55130812Smarcel			getarg_i18n);
56130812Smarcel    exit (ret);
57130812Smarcel}
58130812Smarcel
59130812Smarcelint
60130812Smarcelhelp(void *opt, int argc, char **argv)
61130812Smarcel{
62130812Smarcel    sl_slc_help(commands, argc, argv);
63130812Smarcel    return 0;
64130812Smarcel}
65130812Smarcel
66130812Smarcelint
67130812Smarcelkgetcred(struct kgetcred_options *opt, int argc, char **argv)
68130812Smarcel{
69130812Smarcel    return 0;
70130812Smarcel}
71130812Smarcel
72130812Smarcel/*
73130812Smarcel * Wrapper for command line compatiblity
74130812Smarcel */
75130812Smarcel
76130812Smarcelint
77130812Smarcelkvno(struct kvno_options *opt, int argc, char **argv)
78130812Smarcel{
79130812Smarcel    struct kgetcred_options k;
80130812Smarcel    memset(&k, 0, sizeof(k));
81130812Smarcel
82130812Smarcel    k.cache_string = opt->cache_string;
83130812Smarcel    k.enctype_string = opt->enctype_string;
84130812Smarcel
85130812Smarcel    return kgetcred(&k, argc, argv);
86130812Smarcel}
87130812Smarcel
88130812Smarcelstatic int
89130812Smarcelcommand_alias(const char *name)
90130812Smarcel{
91130812Smarcel    const char *aliases[] = {
92130812Smarcel	"kinit", "klist", "kswitch", "kgetcred", "kvno", "kdeltkt",
93130812Smarcel	"kdestroy", "kcpytkt", NULL
94130812Smarcel    }, **p = aliases;
95130812Smarcel
96130812Smarcel    while (*p && strcmp(name, *p) != 0)
97130812Smarcel	p++;
98130812Smarcel    return *p != NULL;
99130812Smarcel}
100130812Smarcel
101130812Smarcel
102130812Smarcelint
103130812Smarcelmain(int argc, char **argv)
104130812Smarcel{
105130812Smarcel    krb5_error_code ret;
106130812Smarcel    int optidx = 0;
107130812Smarcel    int exit_status = 0;
108130812Smarcel
109130812Smarcel    setprogname (argv[0]);
110130812Smarcel
111130812Smarcel    setlocale (LC_ALL, "");
112130812Smarcel    bindtextdomain ("heimdal_kuser", HEIMDAL_LOCALEDIR);
113130812Smarcel    textdomain("heimdal_kuser");
114130812Smarcel
115130812Smarcel    ret = krb5_init_context(&kcc_context);
116130812Smarcel    if (ret == KRB5_CONFIG_BADFORMAT)
117130812Smarcel	errx (1, "krb5_init_context failed to parse configuration file");
118130812Smarcel    else if (ret)
119130812Smarcel	errx(1, "krb5_init_context failed: %d", ret);
120130812Smarcel
121130812Smarcel    /*
122130812Smarcel     * Support linking of kcc to commands
123130812Smarcel     */
124130812Smarcel
125130812Smarcel    if (!command_alias(getprogname())) {
126130812Smarcel
127130812Smarcel	if (argc == 1) {
128130812Smarcel	    sl_slc_help(commands, 0, NULL);
129130812Smarcel	    return 1;
130130812Smarcel	}
131130812Smarcel
132130812Smarcel	if(getarg(args, sizeof(args) / sizeof(args[0]), argc, argv, &optidx))
133130812Smarcel	    usage(1);
134130812Smarcel
135130812Smarcel	if (help_flag)
136130812Smarcel	    usage (0);
137130812Smarcel
138130812Smarcel	if(version_flag) {
139130812Smarcel	    print_version(NULL);
140130812Smarcel	    exit(0);
141130812Smarcel	}
142130812Smarcel
143130812Smarcel    } else {
144130812Smarcel	argv[0] = rk_UNCONST(getprogname());
145130812Smarcel    }
146130812Smarcel
147130812Smarcel    argc -= optidx;
148130812Smarcel    argv += optidx;
149130812Smarcel
150130812Smarcel    if (argc != 0) {
151130812Smarcel	ret = sl_command(commands, argc, argv);
152130812Smarcel	if(ret == -1)
153130812Smarcel	    krb5_warnx(kcc_context, "unrecognized command: %s", argv[0]);
154130812Smarcel	else if (ret == -2)
155130812Smarcel	    ret = 0;
156130812Smarcel	if(ret != 0)
157130812Smarcel	    exit_status = 1;
158130812Smarcel    } else {
159130812Smarcel	sl_slc_help(commands, argc, argv);
160130812Smarcel	exit_status = 1;
161130812Smarcel    }
162130812Smarcel
163130812Smarcel    krb5_free_context(kcc_context);
164130812Smarcel    return exit_status;
165130812Smarcel}
166130812Smarcel