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