Path: blob/main/crypto/krb5/src/clients/kdestroy/kdestroy.c
34889 views
/* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */1/* clients/kdestroy/kdestroy.c - Destroy contents of credential cache */2/*3* Copyright 1990 by the Massachusetts Institute of Technology.4* All Rights Reserved.5*6* Export of this software from the United States of America may7* require a specific license from the United States Government.8* It is the responsibility of any person or organization contemplating9* export to obtain such a license before exporting.10*11* WITHIN THAT CONSTRAINT, permission to use, copy, modify, and12* distribute this software and its documentation for any purpose and13* without fee is hereby granted, provided that the above copyright14* notice appear in all copies and that both that copyright notice and15* this permission notice appear in supporting documentation, and that16* the name of M.I.T. not be used in advertising or publicity pertaining17* to distribution of the software without specific, written prior18* permission. Furthermore if you modify this software you must label19* your software as modified software and not distribute it in such a20* fashion that it might be confused with the original M.I.T. software.21* M.I.T. makes no representations about the suitability of22* this software for any purpose. It is provided "as is" without express23* or implied warranty.24*/2526#include "k5-platform.h"27#include <krb5.h>28#include <com_err.h>29#include <locale.h>30#include <string.h>31#include <stdio.h>3233#ifdef __STDC__34#define BELL_CHAR '\a'35#else36#define BELL_CHAR '\007'37#endif3839#ifndef _WIN3240#define GET_PROGNAME(x) (strrchr((x), '/') ? strrchr((x), '/') + 1 : (x))41#else42#define GET_PROGNAME(x) max(max(strrchr((x), '/'), strrchr((x), '\\')) + 1,(x))43#endif4445char *progname;464748static void49usage(void)50{51fprintf(stderr, _("Usage: %s [-A] [-q] [-c cache_name] [-p princ_name]\n"),52progname);53fprintf(stderr, _("\t-A destroy all credential caches in collection\n"));54fprintf(stderr, _("\t-q quiet mode\n"));55fprintf(stderr, _("\t-c specify name of credentials cache\n"));56fprintf(stderr, _("\t-p specify principal name within collection\n"));57exit(2);58}5960/* Print a warning if there are still un-destroyed caches in the collection. */61static void62print_remaining_cc_warning(krb5_context context)63{64krb5_error_code ret;65krb5_ccache cache;66krb5_cccol_cursor cursor;6768ret = krb5_cccol_cursor_new(context, &cursor);69if (ret) {70com_err(progname, ret, _("while listing credential caches"));71exit(1);72}7374ret = krb5_cccol_cursor_next(context, cursor, &cache);75if (ret == 0 && cache != NULL) {76fprintf(stderr,77_("Other credential caches present, use -A to destroy all\n"));78krb5_cc_close(context, cache);79}8081krb5_cccol_cursor_free(context, &cursor);82}8384int85main(int argc, char *argv[])86{87krb5_context context;88krb5_error_code ret;89krb5_ccache cache = NULL;90krb5_cccol_cursor cursor;91krb5_principal princ;92char *cache_name = NULL;93const char *princ_name = NULL;94int code = 0, errflg = 0, quiet = 0, all = 0, c;9596setlocale(LC_ALL, "");97progname = GET_PROGNAME(argv[0]);9899while ((c = getopt(argc, argv, "54Aqc:p:")) != -1) {100switch (c) {101case 'A':102all = 1;103break;104case 'q':105quiet = 1;106break;107case 'c':108if (cache_name) {109fprintf(stderr, _("Only one -c option allowed\n"));110errflg++;111} else {112cache_name = optarg;113}114break;115case 'p':116if (princ_name != NULL) {117fprintf(stderr, _("Only one -p option allowed\n"));118errflg++;119} else {120princ_name = optarg;121}122break;123case '4':124fprintf(stderr, _("Kerberos 4 is no longer supported\n"));125exit(3);126break;127case '5':128break;129case '?':130default:131errflg++;132break;133}134}135136if (all && princ_name != NULL) {137fprintf(stderr, _("-A option is exclusive with -p option\n"));138errflg++;139}140141if (optind != argc)142errflg++;143144if (errflg)145usage();146147ret = krb5_init_context(&context);148if (ret) {149com_err(progname, ret, _("while initializing krb5"));150exit(1);151}152153if (cache_name != NULL) {154code = krb5_cc_set_default_name(context, cache_name);155if (code) {156com_err(progname, code, _("while setting default cache name"));157exit(1);158}159}160161if (all) {162code = krb5_cccol_cursor_new(context, &cursor);163if (code) {164com_err(progname, code, _("while listing credential caches"));165exit(1);166}167while (krb5_cccol_cursor_next(context, cursor, &cache) == 0 &&168cache != NULL) {169code = krb5_cc_get_full_name(context, cache, &cache_name);170if (code) {171com_err(progname, code, _("composing ccache name"));172exit(1);173}174code = krb5_cc_destroy(context, cache);175if (code && code != KRB5_FCC_NOFILE) {176com_err(progname, code, _("while destroying cache %s"),177cache_name);178}179krb5_free_string(context, cache_name);180}181krb5_cccol_cursor_free(context, &cursor);182krb5_free_context(context);183return 0;184}185186if (princ_name != NULL) {187code = krb5_parse_name(context, princ_name, &princ);188if (code) {189com_err(progname, code, _("while parsing principal name %s"),190princ_name);191exit(1);192}193code = krb5_cc_cache_match(context, princ, &cache);194if (code) {195com_err(progname, code, _("while finding cache for %s"),196princ_name);197exit(1);198}199krb5_free_principal(context, princ);200} else {201code = krb5_cc_default(context, &cache);202if (code) {203com_err(progname, code, _("while resolving ccache"));204exit(1);205}206}207208code = krb5_cc_destroy(context, cache);209if (code != 0) {210com_err(progname, code, _("while destroying cache"));211if (code != KRB5_FCC_NOFILE) {212if (quiet) {213fprintf(stderr, _("Ticket cache NOT destroyed!\n"));214} else {215fprintf(stderr, _("Ticket cache %cNOT%c destroyed!\n"),216BELL_CHAR, BELL_CHAR);217}218errflg = 1;219}220}221222if (!quiet && !errflg && princ_name == NULL)223print_remaining_cc_warning(context);224225krb5_free_context(context);226227return errflg;228}229230231