1 /* $NetBSD: kdestroy.c,v 1.1.1.2 2014/04/24 12:45:28 pettai Exp $ */ 2 3 /* 4 * Copyright (c) 1997 - 2000, 2003 Kungliga Tekniska Högskolan 5 * (Royal Institute of Technology, Stockholm, Sweden). 6 * All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 12 * 1. Redistributions of source code must retain the above copyright 13 * notice, this list of conditions and the following disclaimer. 14 * 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 19 * 3. Neither the name of the Institute nor the names of its contributors 20 * may be used to endorse or promote products derived from this software 21 * without specific prior written permission. 22 * 23 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND 24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE 27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33 * SUCH DAMAGE. 34 */ 35 36 #include "kuser_locl.h" 37 38 static const char *cache; 39 static const char *credential; 40 static int help_flag; 41 static int version_flag; 42 #ifndef NO_AFS 43 static int unlog_flag = 1; 44 #endif 45 static int dest_tkt_flag = 1; 46 static int all_flag = 0; 47 48 struct getargs args[] = { 49 { "credential", 0, arg_string, rk_UNCONST(&credential), 50 "remove one credential", "principal" }, 51 { "cache", 'c', arg_string, rk_UNCONST(&cache), "cache to destroy", "cache" }, 52 { "all", 'A', arg_flag, &all_flag, "destroy all caches", NULL }, 53 #ifndef NO_AFS 54 { "unlog", 0, arg_negative_flag, &unlog_flag, 55 "do not destroy tokens", NULL }, 56 #endif 57 { "delete-v4", 0, arg_negative_flag, &dest_tkt_flag, 58 "do not destroy v4 tickets", NULL }, 59 { "version", 0, arg_flag, &version_flag, NULL, NULL }, 60 { "help", 'h', arg_flag, &help_flag, NULL, NULL} 61 }; 62 63 int num_args = sizeof(args) / sizeof(args[0]); 64 65 static void 66 usage (int status) 67 { 68 arg_printusage (args, num_args, NULL, ""); 69 exit (status); 70 } 71 72 int 73 main (int argc, char **argv) 74 { 75 krb5_error_code ret; 76 krb5_context context; 77 krb5_ccache ccache; 78 int optidx = 0; 79 int exit_val = 0; 80 81 setprogname (argv[0]); 82 83 if(getarg(args, num_args, argc, argv, &optidx)) 84 usage(1); 85 86 if (help_flag) 87 usage (0); 88 89 if(version_flag){ 90 print_version(NULL); 91 exit(0); 92 } 93 94 argc -= optidx; 95 argv += optidx; 96 97 if (argc != 0) 98 usage (1); 99 100 ret = krb5_init_context (&context); 101 if (ret) 102 errx (1, "krb5_init_context failed: %d", ret); 103 104 if (all_flag) { 105 krb5_cccol_cursor cursor; 106 107 ret = krb5_cccol_cursor_new (context, &cursor); 108 if (ret) 109 krb5_err(context, 1, ret, "krb5_cccol_cursor_new"); 110 111 while (krb5_cccol_cursor_next (context, cursor, &ccache) == 0 && ccache != NULL) { 112 113 ret = krb5_cc_destroy (context, ccache); 114 if (ret) { 115 krb5_warn(context, ret, "krb5_cc_destroy"); 116 exit_val = 1; 117 } 118 } 119 krb5_cccol_cursor_free(context, &cursor); 120 121 } else { 122 if(cache == NULL) { 123 ret = krb5_cc_default(context, &ccache); 124 if (ret) 125 krb5_err(context, 1, ret, "krb5_cc_default"); 126 } else { 127 ret = krb5_cc_resolve(context, 128 cache, 129 &ccache); 130 if (ret) 131 krb5_err(context, 1, ret, "krb5_cc_resolve"); 132 } 133 134 if (ret == 0) { 135 if (credential) { 136 krb5_creds mcred; 137 138 krb5_cc_clear_mcred(&mcred); 139 140 ret = krb5_parse_name(context, credential, &mcred.server); 141 if (ret) 142 krb5_err(context, 1, ret, 143 "Can't parse principal %s", credential); 144 145 ret = krb5_cc_remove_cred(context, ccache, 0, &mcred); 146 if (ret) 147 krb5_err(context, 1, ret, 148 "Failed to remove principal %s", credential); 149 150 krb5_cc_close(context, ccache); 151 krb5_free_principal(context, mcred.server); 152 krb5_free_context(context); 153 return 0; 154 } 155 156 ret = krb5_cc_destroy (context, ccache); 157 if (ret) { 158 krb5_warn(context, ret, "krb5_cc_destroy"); 159 exit_val = 1; 160 } 161 } 162 } 163 164 krb5_free_context (context); 165 166 #ifndef NO_AFS 167 if (unlog_flag && k_hasafs ()) { 168 if (k_unlog ()) 169 exit_val = 1; 170 } 171 #endif 172 173 return exit_val; 174 } 175