etc/services - sync with NetBSD-8
[minix.git] / crypto / external / bsd / heimdal / dist / kuser / kdestroy.c
blob6ffb39549e857c115e938cf7a38b26ce73acf2b1
1 /* $NetBSD: kdestroy.c,v 1.1.1.2 2014/04/24 12:45:28 pettai Exp $ */
3 /*
4 * Copyright (c) 1997 - 2000, 2003 Kungliga Tekniska Högskolan
5 * (Royal Institute of Technology, Stockholm, Sweden).
6 * All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
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.
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.
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.
36 #include "kuser_locl.h"
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;
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}
63 int num_args = sizeof(args) / sizeof(args[0]);
65 static void
66 usage (int status)
68 arg_printusage (args, num_args, NULL, "");
69 exit (status);
72 int
73 main (int argc, char **argv)
75 krb5_error_code ret;
76 krb5_context context;
77 krb5_ccache ccache;
78 int optidx = 0;
79 int exit_val = 0;
81 setprogname (argv[0]);
83 if(getarg(args, num_args, argc, argv, &optidx))
84 usage(1);
86 if (help_flag)
87 usage (0);
89 if(version_flag){
90 print_version(NULL);
91 exit(0);
94 argc -= optidx;
95 argv += optidx;
97 if (argc != 0)
98 usage (1);
100 ret = krb5_init_context (&context);
101 if (ret)
102 errx (1, "krb5_init_context failed: %d", ret);
104 if (all_flag) {
105 krb5_cccol_cursor cursor;
107 ret = krb5_cccol_cursor_new (context, &cursor);
108 if (ret)
109 krb5_err(context, 1, ret, "krb5_cccol_cursor_new");
111 while (krb5_cccol_cursor_next (context, cursor, &ccache) == 0 && ccache != NULL) {
113 ret = krb5_cc_destroy (context, ccache);
114 if (ret) {
115 krb5_warn(context, ret, "krb5_cc_destroy");
116 exit_val = 1;
119 krb5_cccol_cursor_free(context, &cursor);
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");
134 if (ret == 0) {
135 if (credential) {
136 krb5_creds mcred;
138 krb5_cc_clear_mcred(&mcred);
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);
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);
150 krb5_cc_close(context, ccache);
151 krb5_free_principal(context, mcred.server);
152 krb5_free_context(context);
153 return 0;
156 ret = krb5_cc_destroy (context, ccache);
157 if (ret) {
158 krb5_warn(context, ret, "krb5_cc_destroy");
159 exit_val = 1;
164 krb5_free_context (context);
166 #ifndef NO_AFS
167 if (unlog_flag && k_hasafs ()) {
168 if (k_unlog ())
169 exit_val = 1;
171 #endif
173 return exit_val;