155682Smarkm/*
2233294Sstas * Copyright (c) 1997 - 2000, 2003 Kungliga Tekniska H��gskolan
3233294Sstas * (Royal Institute of Technology, Stockholm, Sweden).
4233294Sstas * All rights reserved.
555682Smarkm *
6233294Sstas * Redistribution and use in source and binary forms, with or without
7233294Sstas * modification, are permitted provided that the following conditions
8233294Sstas * are met:
955682Smarkm *
10233294Sstas * 1. Redistributions of source code must retain the above copyright
11233294Sstas *    notice, this list of conditions and the following disclaimer.
1255682Smarkm *
13233294Sstas * 2. Redistributions in binary form must reproduce the above copyright
14233294Sstas *    notice, this list of conditions and the following disclaimer in the
15233294Sstas *    documentation and/or other materials provided with the distribution.
1655682Smarkm *
17233294Sstas * 3. Neither the name of the Institute nor the names of its contributors
18233294Sstas *    may be used to endorse or promote products derived from this software
19233294Sstas *    without specific prior written permission.
2055682Smarkm *
21233294Sstas * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
22233294Sstas * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23233294Sstas * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24233294Sstas * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
25233294Sstas * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26233294Sstas * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27233294Sstas * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28233294Sstas * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29233294Sstas * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30233294Sstas * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31233294Sstas * SUCH DAMAGE.
3255682Smarkm */
3355682Smarkm
3455682Smarkm#include "kuser_locl.h"
3555682Smarkm
3655682Smarkmstatic const char *cache;
37178825Sdfrstatic const char *credential;
3855682Smarkmstatic int help_flag;
3955682Smarkmstatic int version_flag;
40233294Sstas#ifndef NO_AFS
4155682Smarkmstatic int unlog_flag = 1;
42233294Sstas#endif
4355682Smarkmstatic int dest_tkt_flag = 1;
44233294Sstasstatic int all_flag = 0;
4555682Smarkm
4655682Smarkmstruct getargs args[] = {
47233294Sstas    { "credential",	0,   arg_string, rk_UNCONST(&credential),
48178825Sdfr      "remove one credential", "principal" },
49233294Sstas    { "cache",		'c', arg_string, rk_UNCONST(&cache), "cache to destroy", "cache" },
50233294Sstas    { "all",		'A', arg_flag, &all_flag, "destroy all caches", NULL },
51233294Sstas#ifndef NO_AFS
5255682Smarkm    { "unlog",		0,   arg_negative_flag, &unlog_flag,
5355682Smarkm      "do not destroy tokens", NULL },
54233294Sstas#endif
5555682Smarkm    { "delete-v4",	0,   arg_negative_flag, &dest_tkt_flag,
5655682Smarkm      "do not destroy v4 tickets", NULL },
5755682Smarkm    { "version", 	0,   arg_flag, &version_flag, NULL, NULL },
5855682Smarkm    { "help",		'h', arg_flag, &help_flag, NULL, NULL}
5955682Smarkm};
6055682Smarkm
6155682Smarkmint num_args = sizeof(args) / sizeof(args[0]);
6255682Smarkm
6355682Smarkmstatic void
6455682Smarkmusage (int status)
6555682Smarkm{
6655682Smarkm    arg_printusage (args, num_args, NULL, "");
6755682Smarkm    exit (status);
6855682Smarkm}
6955682Smarkm
7055682Smarkmint
7155682Smarkmmain (int argc, char **argv)
7255682Smarkm{
7355682Smarkm    krb5_error_code ret;
7455682Smarkm    krb5_context context;
7555682Smarkm    krb5_ccache  ccache;
76178825Sdfr    int optidx = 0;
7755682Smarkm    int exit_val = 0;
7855682Smarkm
7978527Sassar    setprogname (argv[0]);
8055682Smarkm
81178825Sdfr    if(getarg(args, num_args, argc, argv, &optidx))
8255682Smarkm	usage(1);
83233294Sstas
8455682Smarkm    if (help_flag)
8555682Smarkm	usage (0);
86233294Sstas
8755682Smarkm    if(version_flag){
8855682Smarkm	print_version(NULL);
8955682Smarkm	exit(0);
9055682Smarkm    }
91233294Sstas
92178825Sdfr    argc -= optidx;
93178825Sdfr    argv += optidx;
9455682Smarkm
9555682Smarkm    if (argc != 0)
9655682Smarkm	usage (1);
9755682Smarkm
9855682Smarkm    ret = krb5_init_context (&context);
9955682Smarkm    if (ret)
10072445Sassar	errx (1, "krb5_init_context failed: %d", ret);
10155682Smarkm
102233294Sstas    if (all_flag) {
103233294Sstas	krb5_cccol_cursor cursor;
10455682Smarkm
105233294Sstas	ret = krb5_cccol_cursor_new (context, &cursor);
106233294Sstas	if (ret)
107233294Sstas	    krb5_err(context, 1, ret, "krb5_cccol_cursor_new");
108178825Sdfr
109233294Sstas	while (krb5_cccol_cursor_next (context, cursor, &ccache) == 0 && ccache != NULL) {
110178825Sdfr
111233294Sstas	    ret = krb5_cc_destroy (context, ccache);
112233294Sstas	    if (ret) {
113233294Sstas		krb5_warn(context, ret, "krb5_cc_destroy");
114233294Sstas		exit_val = 1;
115233294Sstas	    }
116233294Sstas	}
117233294Sstas	krb5_cccol_cursor_free(context, &cursor);
118233294Sstas
119233294Sstas    } else {
120233294Sstas	if(cache == NULL) {
121233294Sstas	    ret = krb5_cc_default(context, &ccache);
122178825Sdfr	    if (ret)
123233294Sstas		krb5_err(context, 1, ret, "krb5_cc_default");
124233294Sstas	} else {
125233294Sstas	    ret =  krb5_cc_resolve(context,
126233294Sstas				   cache,
127233294Sstas				   &ccache);
128233294Sstas	    if (ret)
129233294Sstas		krb5_err(context, 1, ret, "krb5_cc_resolve");
130178825Sdfr	}
131178825Sdfr
132233294Sstas	if (ret == 0) {
133233294Sstas	    if (credential) {
134233294Sstas		krb5_creds mcred;
135233294Sstas
136233294Sstas		krb5_cc_clear_mcred(&mcred);
137233294Sstas
138233294Sstas		ret = krb5_parse_name(context, credential, &mcred.server);
139233294Sstas		if (ret)
140233294Sstas		    krb5_err(context, 1, ret,
141233294Sstas			     "Can't parse principal %s", credential);
142233294Sstas
143233294Sstas		ret = krb5_cc_remove_cred(context, ccache, 0, &mcred);
144233294Sstas		if (ret)
145233294Sstas		    krb5_err(context, 1, ret,
146233294Sstas			     "Failed to remove principal %s", credential);
147233294Sstas
148233294Sstas		krb5_cc_close(context, ccache);
149233294Sstas		krb5_free_principal(context, mcred.server);
150233294Sstas		krb5_free_context(context);
151233294Sstas		return 0;
152233294Sstas	    }
153233294Sstas
154233294Sstas	    ret = krb5_cc_destroy (context, ccache);
155233294Sstas	    if (ret) {
156233294Sstas		krb5_warn(context, ret, "krb5_cc_destroy");
157233294Sstas		exit_val = 1;
158233294Sstas	    }
15955682Smarkm	}
16055682Smarkm    }
16155682Smarkm
16255682Smarkm    krb5_free_context (context);
16355682Smarkm
164233294Sstas#ifndef NO_AFS
16555682Smarkm    if (unlog_flag && k_hasafs ()) {
16655682Smarkm	if (k_unlog ())
16755682Smarkm	    exit_val = 1;
16855682Smarkm    }
169233294Sstas#endif
17055682Smarkm
17155682Smarkm    return exit_val;
17255682Smarkm}
173