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