kdestroy.c revision 178825
198944Sobrien/*
2130803Smarcel * Copyright (c) 1997 - 2000, 2003 Kungliga Tekniska H�gskolan
398944Sobrien * (Royal Institute of Technology, Stockholm, Sweden).
498944Sobrien * All rights reserved.
598944Sobrien *
698944Sobrien * Redistribution and use in source and binary forms, with or without
798944Sobrien * modification, are permitted provided that the following conditions
898944Sobrien * are met:
998944Sobrien *
1098944Sobrien * 1. Redistributions of source code must retain the above copyright
1198944Sobrien *    notice, this list of conditions and the following disclaimer.
1298944Sobrien *
1398944Sobrien * 2. Redistributions in binary form must reproduce the above copyright
1498944Sobrien *    notice, this list of conditions and the following disclaimer in the
1598944Sobrien *    documentation and/or other materials provided with the distribution.
1698944Sobrien *
1798944Sobrien * 3. Neither the name of the Institute nor the names of its contributors
1898944Sobrien *    may be used to endorse or promote products derived from this software
1998944Sobrien *    without specific prior written permission.
2098944Sobrien *
2198944Sobrien * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
2298944Sobrien * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2398944Sobrien * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24130803Smarcel * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
2598944Sobrien * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2698944Sobrien * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2798944Sobrien * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2898944Sobrien * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2998944Sobrien * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3098944Sobrien * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3198944Sobrien * SUCH DAMAGE.
3298944Sobrien */
3398944Sobrien
3498944Sobrien#include "kuser_locl.h"
3598944SobrienRCSID("$Id: kdestroy.c 20458 2007-04-19 20:41:27Z lha $");
3698944Sobrien
3798944Sobrienstatic const char *cache;
3898944Sobrienstatic const char *credential;
3998944Sobrienstatic int help_flag;
4098944Sobrienstatic int version_flag;
4198944Sobrienstatic int unlog_flag = 1;
4298944Sobrienstatic int dest_tkt_flag = 1;
4398944Sobrien
4498944Sobrienstruct getargs args[] = {
4598944Sobrien    { "credential",	0,   arg_string, &credential,
4698944Sobrien      "remove one credential", "principal" },
4798944Sobrien    { "cache",		'c', arg_string, &cache, "cache to destroy", "cache" },
4898944Sobrien    { "unlog",		0,   arg_negative_flag, &unlog_flag,
4998944Sobrien      "do not destroy tokens", NULL },
5098944Sobrien    { "delete-v4",	0,   arg_negative_flag, &dest_tkt_flag,
5198944Sobrien      "do not destroy v4 tickets", NULL },
5298944Sobrien    { "version", 	0,   arg_flag, &version_flag, NULL, NULL },
5398944Sobrien    { "help",		'h', arg_flag, &help_flag, NULL, NULL}
5498944Sobrien};
5598944Sobrien
5698944Sobrienint num_args = sizeof(args) / sizeof(args[0]);
5798944Sobrien
5898944Sobrienstatic void
5998944Sobrienusage (int status)
6098944Sobrien{
6198944Sobrien    arg_printusage (args, num_args, NULL, "");
6298944Sobrien    exit (status);
63130803Smarcel}
6498944Sobrien
6598944Sobrienint
6698944Sobrienmain (int argc, char **argv)
6798944Sobrien{
6898944Sobrien    krb5_error_code ret;
6998944Sobrien    krb5_context context;
7098944Sobrien    krb5_ccache  ccache;
7198944Sobrien    int optidx = 0;
7298944Sobrien    int exit_val = 0;
7398944Sobrien
7498944Sobrien    setprogname (argv[0]);
7598944Sobrien
7698944Sobrien    if(getarg(args, num_args, argc, argv, &optidx))
7798944Sobrien	usage(1);
7898944Sobrien
7998944Sobrien    if (help_flag)
8098944Sobrien	usage (0);
8198944Sobrien
8298944Sobrien    if(version_flag){
8398944Sobrien	print_version(NULL);
8498944Sobrien	exit(0);
8598944Sobrien    }
8698944Sobrien
8798944Sobrien    argc -= optidx;
8898944Sobrien    argv += optidx;
8998944Sobrien
9098944Sobrien    if (argc != 0)
9198944Sobrien	usage (1);
9298944Sobrien
9398944Sobrien    ret = krb5_init_context (&context);
9498944Sobrien    if (ret)
9598944Sobrien	errx (1, "krb5_init_context failed: %d", ret);
9698944Sobrien
9798944Sobrien    if(cache == NULL) {
9898944Sobrien	cache = krb5_cc_default_name(context);
9998944Sobrien	if (cache == NULL) {
10098944Sobrien	    warnx ("krb5_cc_default_name: %s", krb5_get_err_text(context, ret));
10198944Sobrien	    exit(1);
10298944Sobrien	}
10398944Sobrien    }
10498944Sobrien
10598944Sobrien    ret =  krb5_cc_resolve(context,
10698944Sobrien			   cache,
10798944Sobrien			   &ccache);
10898944Sobrien
10998944Sobrien    if (ret == 0) {
11098944Sobrien	if (credential) {
11198944Sobrien	    krb5_creds mcred;
11298944Sobrien
11398944Sobrien	    krb5_cc_clear_mcred(&mcred);
11498944Sobrien
11598944Sobrien	    ret = krb5_parse_name(context, credential, &mcred.server);
11698944Sobrien	    if (ret)
11798944Sobrien		krb5_err(context, 1, ret,
11898944Sobrien			 "Can't parse principal %s", credential);
11998944Sobrien
12098944Sobrien	    ret = krb5_cc_remove_cred(context, ccache, 0, &mcred);
12198944Sobrien	    if (ret)
12298944Sobrien		krb5_err(context, 1, ret,
12398944Sobrien			 "Failed to remove principal %s", credential);
12498944Sobrien
12598944Sobrien	    krb5_cc_close(context, ccache);
12698944Sobrien	    krb5_free_principal(context, mcred.server);
12798944Sobrien	    krb5_free_context(context);
12898944Sobrien	    return 0;
12998944Sobrien	}
13098944Sobrien
13198944Sobrien	ret = krb5_cc_destroy (context, ccache);
13298944Sobrien	if (ret) {
13398944Sobrien	    warnx ("krb5_cc_destroy: %s", krb5_get_err_text(context, ret));
13498944Sobrien	    exit_val = 1;
13598944Sobrien	}
13698944Sobrien    } else {
13798944Sobrien	warnx ("krb5_cc_resolve(%s): %s", cache,
13898944Sobrien	       krb5_get_err_text(context, ret));
13998944Sobrien	exit_val = 1;
14098944Sobrien    }
14198944Sobrien
14298944Sobrien    krb5_free_context (context);
14398944Sobrien
144130803Smarcel    if (unlog_flag && k_hasafs ()) {
145130803Smarcel	if (k_unlog ())
14698944Sobrien	    exit_val = 1;
14798944Sobrien    }
14898944Sobrien
14998944Sobrien    return exit_val;
15098944Sobrien}
15198944Sobrien