1226031Sstas/*
2226031Sstas * Copyright (c) 2010 Kungliga Tekniska H��gskolan
3226031Sstas * (Royal Institute of Technology, Stockholm, Sweden).
4226031Sstas * All rights reserved.
5226031Sstas *
6226031Sstas * Redistribution and use in source and binary forms, with or without
7226031Sstas * modification, are permitted provided that the following conditions
8226031Sstas * are met:
9226031Sstas *
10226031Sstas * 1. Redistributions of source code must retain the above copyright
11226031Sstas *    notice, this list of conditions and the following disclaimer.
12226031Sstas *
13226031Sstas * 2. Redistributions in binary form must reproduce the above copyright
14226031Sstas *    notice, this list of conditions and the following disclaimer in the
15226031Sstas *    documentation and/or other materials provided with the distribution.
16226031Sstas *
17226031Sstas * 3. Neither the name of the Institute nor the names of its contributors
18226031Sstas *    may be used to endorse or promote products derived from this software
19226031Sstas *    without specific prior written permission.
20226031Sstas *
21226031Sstas * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
22226031Sstas * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23226031Sstas * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24226031Sstas * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
25226031Sstas * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26226031Sstas * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27226031Sstas * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28226031Sstas * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29226031Sstas * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30226031Sstas * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31226031Sstas * SUCH DAMAGE.
32226031Sstas */
33226031Sstas
34226031Sstas#include "kuser_locl.h"
35226031Sstas#include <sl.h>
36226031Sstas#include "kcc-commands.h"
37226031Sstas
38226031Sstaskrb5_context kcc_context;
39226031Sstasstatic int version_flag;
40226031Sstasstatic int help_flag;
41226031Sstas
42226031Sstasstatic struct getargs args[] = {
43226031Sstas    { "version", 	0,   arg_flag, &version_flag, NULL, NULL },
44226031Sstas    { "help",		0,   arg_flag, &help_flag, NULL, NULL }
45226031Sstas};
46226031Sstas
47226031Sstasstatic void
48226031Sstasusage(int ret)
49226031Sstas{
50226031Sstas    arg_printusage_i18n(args,
51226031Sstas			sizeof(args)/sizeof(*args),
52226031Sstas			N_("Usage: ", ""),
53226031Sstas			NULL,
54226031Sstas			"command ..",
55226031Sstas			getarg_i18n);
56226031Sstas    exit (ret);
57226031Sstas}
58226031Sstas
59226031Sstasint
60226031Sstashelp(void *opt, int argc, char **argv)
61226031Sstas{
62226031Sstas    sl_slc_help(commands, argc, argv);
63226031Sstas    return 0;
64226031Sstas}
65226031Sstas
66226031Sstasint
67226031Sstaskgetcred(struct kgetcred_options *opt, int argc, char **argv)
68226031Sstas{
69226031Sstas    return 0;
70226031Sstas}
71226031Sstas
72226031Sstas/*
73226031Sstas * Wrapper for command line compatiblity
74226031Sstas */
75226031Sstas
76226031Sstasint
77226031Sstaskvno(struct kvno_options *opt, int argc, char **argv)
78226031Sstas{
79226031Sstas    struct kgetcred_options k;
80226031Sstas    memset(&k, 0, sizeof(k));
81226031Sstas
82226031Sstas    k.cache_string = opt->cache_string;
83226031Sstas    k.enctype_string = opt->enctype_string;
84226031Sstas
85226031Sstas    return kgetcred(&k, argc, argv);
86226031Sstas}
87226031Sstas
88226031Sstasstatic int
89226031Sstascommand_alias(const char *name)
90226031Sstas{
91226031Sstas    const char *aliases[] = {
92226031Sstas	"kinit", "klist", "kswitch", "kgetcred", "kvno", "kdeltkt",
93226031Sstas	"kdestroy", "kcpytkt", NULL
94226031Sstas    }, **p = aliases;
95226031Sstas
96226031Sstas    while (*p && strcmp(name, *p) != 0)
97226031Sstas	p++;
98226031Sstas    return *p != NULL;
99226031Sstas}
100226031Sstas
101226031Sstas
102226031Sstasint
103226031Sstasmain(int argc, char **argv)
104226031Sstas{
105226031Sstas    krb5_error_code ret;
106226031Sstas    int optidx = 0;
107226031Sstas    int exit_status = 0;
108226031Sstas
109226031Sstas    setprogname (argv[0]);
110226031Sstas
111226031Sstas    setlocale (LC_ALL, "");
112226031Sstas    bindtextdomain ("heimdal_kuser", HEIMDAL_LOCALEDIR);
113226031Sstas    textdomain("heimdal_kuser");
114226031Sstas
115226031Sstas    ret = krb5_init_context(&kcc_context);
116226031Sstas    if (ret == KRB5_CONFIG_BADFORMAT)
117226031Sstas	errx (1, "krb5_init_context failed to parse configuration file");
118226031Sstas    else if (ret)
119226031Sstas	errx(1, "krb5_init_context failed: %d", ret);
120226031Sstas
121226031Sstas    /*
122226031Sstas     * Support linking of kcc to commands
123226031Sstas     */
124226031Sstas
125226031Sstas    if (!command_alias(getprogname())) {
126226031Sstas
127226031Sstas	if (argc == 1) {
128226031Sstas	    sl_slc_help(commands, 0, NULL);
129226031Sstas	    return 1;
130226031Sstas	}
131226031Sstas
132226031Sstas	if(getarg(args, sizeof(args) / sizeof(args[0]), argc, argv, &optidx))
133226031Sstas	    usage(1);
134226031Sstas
135226031Sstas	if (help_flag)
136226031Sstas	    usage (0);
137226031Sstas
138226031Sstas	if(version_flag) {
139226031Sstas	    print_version(NULL);
140226031Sstas	    exit(0);
141226031Sstas	}
142226031Sstas
143226031Sstas    } else {
144226031Sstas	argv[0] = rk_UNCONST(getprogname());
145226031Sstas    }
146226031Sstas
147226031Sstas    argc -= optidx;
148226031Sstas    argv += optidx;
149226031Sstas
150226031Sstas    if (argc != 0) {
151226031Sstas	ret = sl_command(commands, argc, argv);
152226031Sstas	if(ret == -1)
153226031Sstas	    krb5_warnx(kcc_context, "unrecognized command: %s", argv[0]);
154226031Sstas	else if (ret == -2)
155226031Sstas	    ret = 0;
156226031Sstas	if(ret != 0)
157226031Sstas	    exit_status = 1;
158226031Sstas    } else {
159226031Sstas	sl_slc_help(commands, argc, argv);
160226031Sstas	exit_status = 1;
161226031Sstas    }
162226031Sstas
163226031Sstas    krb5_free_context(kcc_context);
164226031Sstas    return exit_status;
165226031Sstas}
166