xref: /titanic_50/usr/src/cmd/krb5/kdestroy/kdestroy.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * Copyright 2002-2003 Sun Microsystems, Inc.  All rights reserved.
3*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
4*7c478bd9Sstevel@tonic-gate  */
5*7c478bd9Sstevel@tonic-gate 
6*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
7*7c478bd9Sstevel@tonic-gate 
8*7c478bd9Sstevel@tonic-gate /*
9*7c478bd9Sstevel@tonic-gate  * clients/kdestroy/kdestroy.c
10*7c478bd9Sstevel@tonic-gate  *
11*7c478bd9Sstevel@tonic-gate  * Copyright 1990 by the Massachusetts Institute of Technology.
12*7c478bd9Sstevel@tonic-gate  * All Rights Reserved.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * Export of this software from the United States of America may
15*7c478bd9Sstevel@tonic-gate  *   require a specific license from the United States Government.
16*7c478bd9Sstevel@tonic-gate  *   It is the responsibility of any person or organization contemplating
17*7c478bd9Sstevel@tonic-gate  *   export to obtain such a license before exporting.
18*7c478bd9Sstevel@tonic-gate  *
19*7c478bd9Sstevel@tonic-gate  * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
20*7c478bd9Sstevel@tonic-gate  * distribute this software and its documentation for any purpose and
21*7c478bd9Sstevel@tonic-gate  * without fee is hereby granted, provided that the above copyright
22*7c478bd9Sstevel@tonic-gate  * notice appear in all copies and that both that copyright notice and
23*7c478bd9Sstevel@tonic-gate  * this permission notice appear in supporting documentation, and that
24*7c478bd9Sstevel@tonic-gate  * the name of M.I.T. not be used in advertising or publicity pertaining
25*7c478bd9Sstevel@tonic-gate  * to distribution of the software without specific, written prior
26*7c478bd9Sstevel@tonic-gate  * permission.  Furthermore if you modify this software you must label
27*7c478bd9Sstevel@tonic-gate  * your software as modified software and not distribute it in such a
28*7c478bd9Sstevel@tonic-gate  * fashion that it might be confused with the original M.I.T. software.
29*7c478bd9Sstevel@tonic-gate  * M.I.T. makes no representations about the suitability of
30*7c478bd9Sstevel@tonic-gate  * this software for any purpose.  It is provided "as is" without express
31*7c478bd9Sstevel@tonic-gate  * or implied warranty.
32*7c478bd9Sstevel@tonic-gate  *
33*7c478bd9Sstevel@tonic-gate  *
34*7c478bd9Sstevel@tonic-gate  * Destroy the contents of your credential cache.
35*7c478bd9Sstevel@tonic-gate  */
36*7c478bd9Sstevel@tonic-gate 
37*7c478bd9Sstevel@tonic-gate #include <krb5.h>
38*7c478bd9Sstevel@tonic-gate #include <com_err.h>
39*7c478bd9Sstevel@tonic-gate #include <string.h>
40*7c478bd9Sstevel@tonic-gate #include <stdio.h>
41*7c478bd9Sstevel@tonic-gate #include <locale.h>
42*7c478bd9Sstevel@tonic-gate #include <rpc/types.h>
43*7c478bd9Sstevel@tonic-gate #include <rpc/rpcsys.h>
44*7c478bd9Sstevel@tonic-gate #include <rpc/rpcsec_gss.h>
45*7c478bd9Sstevel@tonic-gate #include <syslog.h>
46*7c478bd9Sstevel@tonic-gate #include <libintl.h>
47*7c478bd9Sstevel@tonic-gate 
48*7c478bd9Sstevel@tonic-gate #ifdef KRB5_KRB4_COMPAT
49*7c478bd9Sstevel@tonic-gate #include <kerberosIV/krb.h>
50*7c478bd9Sstevel@tonic-gate #endif
51*7c478bd9Sstevel@tonic-gate 
52*7c478bd9Sstevel@tonic-gate #ifdef __STDC__
53*7c478bd9Sstevel@tonic-gate #define BELL_CHAR '\a'
54*7c478bd9Sstevel@tonic-gate #else
55*7c478bd9Sstevel@tonic-gate #define BELL_CHAR '\007'
56*7c478bd9Sstevel@tonic-gate #endif
57*7c478bd9Sstevel@tonic-gate 
58*7c478bd9Sstevel@tonic-gate extern int optind;
59*7c478bd9Sstevel@tonic-gate extern char *optarg;
60*7c478bd9Sstevel@tonic-gate 
61*7c478bd9Sstevel@tonic-gate #ifndef _WIN32
62*7c478bd9Sstevel@tonic-gate #define GET_PROGNAME(x) (strrchr((x), '/') ? strrchr((x), '/')+1 : (x))
63*7c478bd9Sstevel@tonic-gate #else
64*7c478bd9Sstevel@tonic-gate #define GET_PROGNAME(x) max(max(strrchr((x), '/'), strrchr((x), '\\')) + 1,(x))
65*7c478bd9Sstevel@tonic-gate #endif
66*7c478bd9Sstevel@tonic-gate 
67*7c478bd9Sstevel@tonic-gate char *progname;
68*7c478bd9Sstevel@tonic-gate 
69*7c478bd9Sstevel@tonic-gate int got_k5 = 0;
70*7c478bd9Sstevel@tonic-gate int got_k4 = 0;
71*7c478bd9Sstevel@tonic-gate 
72*7c478bd9Sstevel@tonic-gate int default_k5 = 1;
73*7c478bd9Sstevel@tonic-gate #ifdef KRB5_KRB4_COMPAT
74*7c478bd9Sstevel@tonic-gate int default_k4 = 1;
75*7c478bd9Sstevel@tonic-gate #else
76*7c478bd9Sstevel@tonic-gate int default_k4 = 0;
77*7c478bd9Sstevel@tonic-gate #endif
78*7c478bd9Sstevel@tonic-gate 
79*7c478bd9Sstevel@tonic-gate 
80*7c478bd9Sstevel@tonic-gate void usage()
81*7c478bd9Sstevel@tonic-gate {
82*7c478bd9Sstevel@tonic-gate #define KRB_AVAIL_STRING(x) ((x)?gettext("available"):gettext("not available"))
83*7c478bd9Sstevel@tonic-gate 
84*7c478bd9Sstevel@tonic-gate     fprintf(stderr, gettext("Usage"), ": %s [-5] [-4] [-q] [-c cache_name]\n",
85*7c478bd9Sstevel@tonic-gate             progname);
86*7c478bd9Sstevel@tonic-gate     fprintf(stderr, "\t-5 Kerberos 5 (%s)\n", KRB_AVAIL_STRING(got_k5));
87*7c478bd9Sstevel@tonic-gate     fprintf(stderr, "\t-4 Kerberos 4 (%s)\n", KRB_AVAIL_STRING(got_k4));
88*7c478bd9Sstevel@tonic-gate     fprintf(stderr, gettext("\t   (Default is %s%s%s%s)\n"),
89*7c478bd9Sstevel@tonic-gate 	    default_k5?"Kerberos 5":"",
90*7c478bd9Sstevel@tonic-gate 	    (default_k5 && default_k4)?gettext(" and "):"",
91*7c478bd9Sstevel@tonic-gate 	    default_k4?"Kerberos 4":"",
92*7c478bd9Sstevel@tonic-gate 	    (!default_k5 && !default_k4)?gettext("neither"):"");
93*7c478bd9Sstevel@tonic-gate     fprintf(stderr, gettext("\t-q quiet mode\n"));
94*7c478bd9Sstevel@tonic-gate     fprintf(stderr, gettext("\t-c specify name of credentials cache\n"));
95*7c478bd9Sstevel@tonic-gate     exit(2);
96*7c478bd9Sstevel@tonic-gate }
97*7c478bd9Sstevel@tonic-gate 
98*7c478bd9Sstevel@tonic-gate int
99*7c478bd9Sstevel@tonic-gate main(argc, argv)
100*7c478bd9Sstevel@tonic-gate     int argc;
101*7c478bd9Sstevel@tonic-gate     char **argv;
102*7c478bd9Sstevel@tonic-gate {
103*7c478bd9Sstevel@tonic-gate     krb5_context kcontext;
104*7c478bd9Sstevel@tonic-gate     krb5_error_code retval;
105*7c478bd9Sstevel@tonic-gate     int c;
106*7c478bd9Sstevel@tonic-gate     krb5_ccache cache = NULL;
107*7c478bd9Sstevel@tonic-gate     char *cache_name = NULL;
108*7c478bd9Sstevel@tonic-gate     char *client_name = NULL;
109*7c478bd9Sstevel@tonic-gate     krb5_principal me;
110*7c478bd9Sstevel@tonic-gate     int code = 0;
111*7c478bd9Sstevel@tonic-gate #ifdef KRB5_KRB4_COMPAT
112*7c478bd9Sstevel@tonic-gate     int v4code = 0;
113*7c478bd9Sstevel@tonic-gate     int v4 = 1;
114*7c478bd9Sstevel@tonic-gate #endif
115*7c478bd9Sstevel@tonic-gate     int errflg = 0;
116*7c478bd9Sstevel@tonic-gate     int quiet = 0;
117*7c478bd9Sstevel@tonic-gate     struct krpc_revauth desarg;
118*7c478bd9Sstevel@tonic-gate     static  rpc_gss_OID_desc oid=
119*7c478bd9Sstevel@tonic-gate 	{9, "\052\206\110\206\367\022\001\002\002"};
120*7c478bd9Sstevel@tonic-gate 
121*7c478bd9Sstevel@tonic-gate     static  rpc_gss_OID krb5_mech_type = &oid;
122*7c478bd9Sstevel@tonic-gate 
123*7c478bd9Sstevel@tonic-gate     int use_k5 = 0;
124*7c478bd9Sstevel@tonic-gate     int use_k4 = 0;
125*7c478bd9Sstevel@tonic-gate 
126*7c478bd9Sstevel@tonic-gate     /* set locale and domain for internationalization */
127*7c478bd9Sstevel@tonic-gate     (void) setlocale(LC_ALL, "");
128*7c478bd9Sstevel@tonic-gate 
129*7c478bd9Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)
130*7c478bd9Sstevel@tonic-gate #define TEXT_DOMAIN "SYS_TEST"
131*7c478bd9Sstevel@tonic-gate #endif /* !TEXT_DOMAIN */
132*7c478bd9Sstevel@tonic-gate 
133*7c478bd9Sstevel@tonic-gate     (void) textdomain(TEXT_DOMAIN);
134*7c478bd9Sstevel@tonic-gate 
135*7c478bd9Sstevel@tonic-gate     got_k5 = 1;
136*7c478bd9Sstevel@tonic-gate #ifdef KRB5_KRB4_COMPAT
137*7c478bd9Sstevel@tonic-gate     got_k4 = 1;
138*7c478bd9Sstevel@tonic-gate #endif
139*7c478bd9Sstevel@tonic-gate 
140*7c478bd9Sstevel@tonic-gate     progname = (strrchr(*argv, '/') ? strrchr(*argv, '/')+1 : argv[0]);
141*7c478bd9Sstevel@tonic-gate 
142*7c478bd9Sstevel@tonic-gate     while ((c = getopt(argc, argv, "54qc:")) != -1) {	switch (c) {
143*7c478bd9Sstevel@tonic-gate 	case 'q':
144*7c478bd9Sstevel@tonic-gate 	    quiet = 1;
145*7c478bd9Sstevel@tonic-gate 	    break;
146*7c478bd9Sstevel@tonic-gate 	case 'c':
147*7c478bd9Sstevel@tonic-gate 	    if (cache_name) {
148*7c478bd9Sstevel@tonic-gate 		fprintf(stderr, gettext("Only one -c option allowed\n"));
149*7c478bd9Sstevel@tonic-gate 		errflg++;
150*7c478bd9Sstevel@tonic-gate 	    } else {
151*7c478bd9Sstevel@tonic-gate 		cache_name = optarg;
152*7c478bd9Sstevel@tonic-gate 	    }
153*7c478bd9Sstevel@tonic-gate 	    break;
154*7c478bd9Sstevel@tonic-gate 	case '4':
155*7c478bd9Sstevel@tonic-gate 	    if (!got_k4)
156*7c478bd9Sstevel@tonic-gate 	    {
157*7c478bd9Sstevel@tonic-gate #ifdef KRB5_KRB4_COMPAT
158*7c478bd9Sstevel@tonic-gate 		fprintf(stderr, "Kerberos 4 support could not be loaded\n");
159*7c478bd9Sstevel@tonic-gate #else
160*7c478bd9Sstevel@tonic-gate 		fprintf(stderr, gettext("This was not built with Kerberos 4 support\n"));
161*7c478bd9Sstevel@tonic-gate #endif
162*7c478bd9Sstevel@tonic-gate 		exit(3);
163*7c478bd9Sstevel@tonic-gate 	    }
164*7c478bd9Sstevel@tonic-gate 	    use_k4 = 1;
165*7c478bd9Sstevel@tonic-gate 	    break;
166*7c478bd9Sstevel@tonic-gate 	case '5':
167*7c478bd9Sstevel@tonic-gate 	    if (!got_k5)
168*7c478bd9Sstevel@tonic-gate 	    {
169*7c478bd9Sstevel@tonic-gate 		fprintf(stderr, gettext("Kerberos 5 support could not be loaded\n"));
170*7c478bd9Sstevel@tonic-gate 		exit(3);
171*7c478bd9Sstevel@tonic-gate 	    }
172*7c478bd9Sstevel@tonic-gate 	    use_k5 = 1;
173*7c478bd9Sstevel@tonic-gate 	    break;
174*7c478bd9Sstevel@tonic-gate 	case '?':
175*7c478bd9Sstevel@tonic-gate 	default:
176*7c478bd9Sstevel@tonic-gate 	    errflg++;
177*7c478bd9Sstevel@tonic-gate 	    break;
178*7c478bd9Sstevel@tonic-gate 	}
179*7c478bd9Sstevel@tonic-gate     }
180*7c478bd9Sstevel@tonic-gate 
181*7c478bd9Sstevel@tonic-gate     if (optind != argc)
182*7c478bd9Sstevel@tonic-gate 	errflg++;
183*7c478bd9Sstevel@tonic-gate 
184*7c478bd9Sstevel@tonic-gate     if (errflg) {
185*7c478bd9Sstevel@tonic-gate 	usage();
186*7c478bd9Sstevel@tonic-gate     }
187*7c478bd9Sstevel@tonic-gate 
188*7c478bd9Sstevel@tonic-gate     if (!use_k5 && !use_k4)
189*7c478bd9Sstevel@tonic-gate     {
190*7c478bd9Sstevel@tonic-gate 	use_k5 = default_k5;
191*7c478bd9Sstevel@tonic-gate 	use_k4 = default_k4;
192*7c478bd9Sstevel@tonic-gate     }
193*7c478bd9Sstevel@tonic-gate 
194*7c478bd9Sstevel@tonic-gate     if (!use_k5)
195*7c478bd9Sstevel@tonic-gate 	got_k5 = 0;
196*7c478bd9Sstevel@tonic-gate     if (!use_k4)
197*7c478bd9Sstevel@tonic-gate 	got_k4 = 0;
198*7c478bd9Sstevel@tonic-gate 
199*7c478bd9Sstevel@tonic-gate     if (got_k5) {
200*7c478bd9Sstevel@tonic-gate 	retval = krb5_init_context(&kcontext);
201*7c478bd9Sstevel@tonic-gate 	if (retval) {
202*7c478bd9Sstevel@tonic-gate 	    com_err(progname, retval, gettext("while initializing krb5"));
203*7c478bd9Sstevel@tonic-gate 	    exit(1);
204*7c478bd9Sstevel@tonic-gate 	}
205*7c478bd9Sstevel@tonic-gate 
206*7c478bd9Sstevel@tonic-gate     	/*
207*7c478bd9Sstevel@tonic-gate      	 *  Solaris Kerberos
208*7c478bd9Sstevel@tonic-gate      	 *  Let us destroy the kernel cache first
209*7c478bd9Sstevel@tonic-gate      	 */
210*7c478bd9Sstevel@tonic-gate     	desarg.version = 1;
211*7c478bd9Sstevel@tonic-gate     	desarg.uid_1 = geteuid();
212*7c478bd9Sstevel@tonic-gate     	desarg.rpcsec_flavor_1 = RPCSEC_GSS;
213*7c478bd9Sstevel@tonic-gate     	desarg.flavor_data_1 = (void *) krb5_mech_type;
214*7c478bd9Sstevel@tonic-gate     	code = krpc_sys(KRPC_REVAUTH, (void *)&desarg);
215*7c478bd9Sstevel@tonic-gate 
216*7c478bd9Sstevel@tonic-gate     	if (code != 0) {
217*7c478bd9Sstevel@tonic-gate         	fprintf(stderr,
218*7c478bd9Sstevel@tonic-gate             		gettext("%s: kernel creds cache error %d \n"),
219*7c478bd9Sstevel@tonic-gate             		progname, code);
220*7c478bd9Sstevel@tonic-gate     	}
221*7c478bd9Sstevel@tonic-gate 
222*7c478bd9Sstevel@tonic-gate     	if (cache == NULL) {
223*7c478bd9Sstevel@tonic-gate         	if (code = krb5_cc_default(kcontext, &cache)) {
224*7c478bd9Sstevel@tonic-gate             	com_err(progname, code,
225*7c478bd9Sstevel@tonic-gate                 	gettext("while getting default ccache"));
226*7c478bd9Sstevel@tonic-gate             	exit(1);
227*7c478bd9Sstevel@tonic-gate         	}
228*7c478bd9Sstevel@tonic-gate     	}
229*7c478bd9Sstevel@tonic-gate 
230*7c478bd9Sstevel@tonic-gate 	if (cache_name) {
231*7c478bd9Sstevel@tonic-gate 
232*7c478bd9Sstevel@tonic-gate 
233*7c478bd9Sstevel@tonic-gate 
234*7c478bd9Sstevel@tonic-gate #ifdef KRB5_KRB4_COMPAT
235*7c478bd9Sstevel@tonic-gate 	    v4 = 0;	/* Don't do v4 if doing v5 and cache name given. */
236*7c478bd9Sstevel@tonic-gate #endif
237*7c478bd9Sstevel@tonic-gate 	    code = krb5_cc_resolve (kcontext, cache_name, &cache);
238*7c478bd9Sstevel@tonic-gate 	    if (code != 0) {
239*7c478bd9Sstevel@tonic-gate 		com_err (progname, code, gettext("while resolving %s"), cache_name);
240*7c478bd9Sstevel@tonic-gate 		exit(1);
241*7c478bd9Sstevel@tonic-gate 	    }
242*7c478bd9Sstevel@tonic-gate 	} else {
243*7c478bd9Sstevel@tonic-gate 	    if (code = krb5_cc_default(kcontext, &cache)) {
244*7c478bd9Sstevel@tonic-gate 		com_err(progname, code, gettext("while getting default ccache"));
245*7c478bd9Sstevel@tonic-gate 		exit(1);
246*7c478bd9Sstevel@tonic-gate 	    }
247*7c478bd9Sstevel@tonic-gate 	}
248*7c478bd9Sstevel@tonic-gate 
249*7c478bd9Sstevel@tonic-gate 	/*
250*7c478bd9Sstevel@tonic-gate 	 * Solaris Kerberos
251*7c478bd9Sstevel@tonic-gate          * Get client name for kwarn_del_warning.
252*7c478bd9Sstevel@tonic-gate 	 */
253*7c478bd9Sstevel@tonic-gate         code = krb5_cc_get_principal(kcontext, cache, &me);
254*7c478bd9Sstevel@tonic-gate         if (code != 0)
255*7c478bd9Sstevel@tonic-gate             fprintf(stderr, gettext
256*7c478bd9Sstevel@tonic-gate                 ("%s: Could not obtain principal name from cache\n"), progname);
257*7c478bd9Sstevel@tonic-gate         else
258*7c478bd9Sstevel@tonic-gate             if ((code = krb5_unparse_name(kcontext, me, &client_name)))
259*7c478bd9Sstevel@tonic-gate                 fprintf(stderr, gettext
260*7c478bd9Sstevel@tonic-gate                     ("%s: Could not unparse principal name found in cache\n"), progname);
261*7c478bd9Sstevel@tonic-gate 
262*7c478bd9Sstevel@tonic-gate 	code = krb5_cc_destroy (kcontext, cache);
263*7c478bd9Sstevel@tonic-gate 	if (code != 0) {
264*7c478bd9Sstevel@tonic-gate 	    com_err (progname, code, gettext("while destroying cache"));
265*7c478bd9Sstevel@tonic-gate 	    if (code != KRB5_FCC_NOFILE) {
266*7c478bd9Sstevel@tonic-gate 		if (quiet)
267*7c478bd9Sstevel@tonic-gate 		    fprintf(stderr, gettext("Ticket cache NOT destroyed!\n"));
268*7c478bd9Sstevel@tonic-gate 		else {
269*7c478bd9Sstevel@tonic-gate 		    fprintf(stderr, gettext("Ticket cache %cNOT%c destroyed!\n"),
270*7c478bd9Sstevel@tonic-gate 			    BELL_CHAR, BELL_CHAR);
271*7c478bd9Sstevel@tonic-gate 		}
272*7c478bd9Sstevel@tonic-gate 		errflg = 1;
273*7c478bd9Sstevel@tonic-gate 	    }
274*7c478bd9Sstevel@tonic-gate 	}
275*7c478bd9Sstevel@tonic-gate     }
276*7c478bd9Sstevel@tonic-gate #ifdef KRB5_KRB4_COMPAT
277*7c478bd9Sstevel@tonic-gate     if (got_k4 && v4) {
278*7c478bd9Sstevel@tonic-gate 	v4code = dest_tkt();
279*7c478bd9Sstevel@tonic-gate 	if (v4code == KSUCCESS && code != 0)
280*7c478bd9Sstevel@tonic-gate 	    fprintf(stderr, "Kerberos 4 ticket cache destroyed.\n");
281*7c478bd9Sstevel@tonic-gate 	if (v4code != KSUCCESS && v4code != RET_TKFIL) {
282*7c478bd9Sstevel@tonic-gate 	    if (quiet)
283*7c478bd9Sstevel@tonic-gate 		fprintf(stderr, "Kerberos 4 ticket cache NOT destroyed!\n");
284*7c478bd9Sstevel@tonic-gate 	    else
285*7c478bd9Sstevel@tonic-gate 		fprintf(stderr, "Kerberos 4 ticket cache %cNOT%c destroyed!\n",
286*7c478bd9Sstevel@tonic-gate 			BELL_CHAR, BELL_CHAR);
287*7c478bd9Sstevel@tonic-gate 	    errflg = 1;
288*7c478bd9Sstevel@tonic-gate 	}
289*7c478bd9Sstevel@tonic-gate     }
290*7c478bd9Sstevel@tonic-gate #endif
291*7c478bd9Sstevel@tonic-gate 
292*7c478bd9Sstevel@tonic-gate     /* Solaris Kerberos */
293*7c478bd9Sstevel@tonic-gate     if (!errflg && client_name)
294*7c478bd9Sstevel@tonic-gate         kwarn_del_warning(client_name);
295*7c478bd9Sstevel@tonic-gate     else
296*7c478bd9Sstevel@tonic-gate         fprintf(stderr, gettext
297*7c478bd9Sstevel@tonic-gate             ("%s: TGT expire warning NOT deleted\n"), progname);
298*7c478bd9Sstevel@tonic-gate 
299*7c478bd9Sstevel@tonic-gate     return errflg;
300*7c478bd9Sstevel@tonic-gate }
301