Imported Upstream version 1.15.1
[platform/upstream/krb5.git] / src / clients / kdestroy / kdestroy.c
1 /* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2 /* clients/kdestroy/kdestroy.c - Destroy contents of credential cache */
3 /*
4  * Copyright 1990 by the Massachusetts Institute of Technology.
5  * All Rights Reserved.
6  *
7  * Export of this software from the United States of America may
8  *   require a specific license from the United States Government.
9  *   It is the responsibility of any person or organization contemplating
10  *   export to obtain such a license before exporting.
11  *
12  * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
13  * distribute this software and its documentation for any purpose and
14  * without fee is hereby granted, provided that the above copyright
15  * notice appear in all copies and that both that copyright notice and
16  * this permission notice appear in supporting documentation, and that
17  * the name of M.I.T. not be used in advertising or publicity pertaining
18  * to distribution of the software without specific, written prior
19  * permission.  Furthermore if you modify this software you must label
20  * your software as modified software and not distribute it in such a
21  * fashion that it might be confused with the original M.I.T. software.
22  * M.I.T. makes no representations about the suitability of
23  * this software for any purpose.  It is provided "as is" without express
24  * or implied warranty.
25  */
26
27 #include "k5-platform.h"
28 #include <krb5.h>
29 #include <com_err.h>
30 #include <locale.h>
31 #include <string.h>
32 #include <stdio.h>
33
34 #ifdef __STDC__
35 #define BELL_CHAR '\a'
36 #else
37 #define BELL_CHAR '\007'
38 #endif
39
40 extern int optind;
41 extern char *optarg;
42
43 #ifndef _WIN32
44 #define GET_PROGNAME(x) (strrchr((x), '/') ? strrchr((x), '/')+1 : (x))
45 #else
46 #define GET_PROGNAME(x) max(max(strrchr((x), '/'), strrchr((x), '\\')) + 1,(x))
47 #endif
48
49 char *progname;
50
51
52 static void usage()
53 {
54 #define KRB_AVAIL_STRING(x) ((x)?"available":"not available")
55
56     fprintf(stderr, _("Usage: %s [-A] [-q] [-c cache_name]\n"), progname);
57     fprintf(stderr, _("\t-A destroy all credential caches in collection\n"));
58     fprintf(stderr, _("\t-q quiet mode\n"));
59     fprintf(stderr, _("\t-c specify name of credentials cache\n"));
60     exit(2);
61 }
62
63 /* Print a warning if there are still un-destroyed caches in the collection. */
64 static void
65 print_remaining_cc_warning(krb5_context context)
66 {
67     krb5_error_code retval;
68     krb5_ccache cache;
69     krb5_cccol_cursor cursor;
70
71     retval = krb5_cccol_cursor_new(context, &cursor);
72     if (retval) {
73         com_err(progname, retval, _("while listing credential caches"));
74         exit(1);
75     }
76
77     retval = krb5_cccol_cursor_next(context, cursor, &cache);
78     if (retval == 0 && cache != NULL) {
79         fprintf(stderr,
80                 _("Other credential caches present, use -A to destroy all\n"));
81         krb5_cc_close(context, cache);
82     }
83
84     krb5_cccol_cursor_free(context, &cursor);
85 }
86
87 int
88 main(argc, argv)
89     int argc;
90     char **argv;
91 {
92     krb5_context kcontext;
93     krb5_error_code retval;
94     int c;
95     krb5_ccache cache = NULL;
96     krb5_cccol_cursor cursor;
97     char *cache_name = NULL;
98     int code = 0;
99     int errflg = 0;
100     int quiet = 0;
101     int all = 0;
102
103     setlocale(LC_ALL, "");
104     progname = GET_PROGNAME(argv[0]);
105
106     while ((c = getopt(argc, argv, "54Aqc:")) != -1) {
107         switch (c) {
108         case 'A':
109             all = 1;
110             break;
111         case 'q':
112             quiet = 1;
113             break;
114         case 'c':
115             if (cache_name) {
116                 fprintf(stderr, _("Only one -c option allowed\n"));
117                 errflg++;
118             } else {
119                 cache_name = optarg;
120             }
121             break;
122         case '4':
123             fprintf(stderr, _("Kerberos 4 is no longer supported\n"));
124             exit(3);
125             break;
126         case '5':
127             break;
128         case '?':
129         default:
130             errflg++;
131             break;
132         }
133     }
134
135     if (optind != argc)
136         errflg++;
137
138     if (errflg) {
139         usage();
140     }
141
142     retval = krb5_init_context(&kcontext);
143     if (retval) {
144         com_err(progname, retval, _("while initializing krb5"));
145         exit(1);
146     }
147
148     if (all) {
149         code = krb5_cccol_cursor_new(kcontext, &cursor);
150         if (code) {
151             com_err(progname, code, _("while listing credential caches"));
152             exit(1);
153         }
154         while ((code = krb5_cccol_cursor_next(kcontext, cursor,
155                                               &cache)) == 0 && cache != NULL) {
156             code = krb5_cc_get_full_name(kcontext, cache, &cache_name);
157             if (code) {
158                 com_err(progname, code, _("composing ccache name"));
159                 exit(1);
160             }
161             code = krb5_cc_destroy(kcontext, cache);
162             if (code && code != KRB5_FCC_NOFILE) {
163                 com_err(progname, code, _("while destroying cache %s"),
164                         cache_name);
165             }
166             krb5_free_string(kcontext, cache_name);
167         }
168         krb5_cccol_cursor_free(kcontext, &cursor);
169         krb5_free_context(kcontext);
170         return 0;
171     }
172
173     if (cache_name) {
174         code = krb5_cc_resolve (kcontext, cache_name, &cache);
175         if (code != 0) {
176             com_err(progname, code, _("while resolving %s"), cache_name);
177             exit(1);
178         }
179     } else {
180         code = krb5_cc_default(kcontext, &cache);
181         if (code) {
182             com_err(progname, code, _("while getting default ccache"));
183             exit(1);
184         }
185     }
186
187     code = krb5_cc_destroy (kcontext, cache);
188     if (code != 0) {
189         com_err (progname, code, _("while destroying cache"));
190         if (code != KRB5_FCC_NOFILE) {
191             if (quiet)
192                 fprintf(stderr, _("Ticket cache NOT destroyed!\n"));
193             else {
194                 fprintf(stderr, _("Ticket cache %cNOT%c destroyed!\n"),
195                         BELL_CHAR, BELL_CHAR);
196             }
197             errflg = 1;
198         }
199     }
200
201     if (!quiet && !errflg)
202         print_remaining_cc_warning(kcontext);
203
204     krb5_free_context(kcontext);
205     return errflg;
206 }