xref: /titanic_54/usr/src/cmd/krb5/kdestroy/kdestroy.c (revision 56a424cca6b3f91f31bdab72a4626c48c779fe8b)
17c478bd9Sstevel@tonic-gate /*
2*56a424ccSmp153739  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
37c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
47c478bd9Sstevel@tonic-gate  */
57c478bd9Sstevel@tonic-gate 
67c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
77c478bd9Sstevel@tonic-gate 
87c478bd9Sstevel@tonic-gate /*
97c478bd9Sstevel@tonic-gate  * clients/kdestroy/kdestroy.c
107c478bd9Sstevel@tonic-gate  *
117c478bd9Sstevel@tonic-gate  * Copyright 1990 by the Massachusetts Institute of Technology.
127c478bd9Sstevel@tonic-gate  * All Rights Reserved.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * Export of this software from the United States of America may
157c478bd9Sstevel@tonic-gate  *   require a specific license from the United States Government.
167c478bd9Sstevel@tonic-gate  *   It is the responsibility of any person or organization contemplating
177c478bd9Sstevel@tonic-gate  *   export to obtain such a license before exporting.
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
207c478bd9Sstevel@tonic-gate  * distribute this software and its documentation for any purpose and
217c478bd9Sstevel@tonic-gate  * without fee is hereby granted, provided that the above copyright
227c478bd9Sstevel@tonic-gate  * notice appear in all copies and that both that copyright notice and
237c478bd9Sstevel@tonic-gate  * this permission notice appear in supporting documentation, and that
247c478bd9Sstevel@tonic-gate  * the name of M.I.T. not be used in advertising or publicity pertaining
257c478bd9Sstevel@tonic-gate  * to distribution of the software without specific, written prior
267c478bd9Sstevel@tonic-gate  * permission.  Furthermore if you modify this software you must label
277c478bd9Sstevel@tonic-gate  * your software as modified software and not distribute it in such a
287c478bd9Sstevel@tonic-gate  * fashion that it might be confused with the original M.I.T. software.
297c478bd9Sstevel@tonic-gate  * M.I.T. makes no representations about the suitability of
307c478bd9Sstevel@tonic-gate  * this software for any purpose.  It is provided "as is" without express
317c478bd9Sstevel@tonic-gate  * or implied warranty.
327c478bd9Sstevel@tonic-gate  *
337c478bd9Sstevel@tonic-gate  *
347c478bd9Sstevel@tonic-gate  * Destroy the contents of your credential cache.
357c478bd9Sstevel@tonic-gate  */
367c478bd9Sstevel@tonic-gate 
377c478bd9Sstevel@tonic-gate #include <krb5.h>
387c478bd9Sstevel@tonic-gate #include <com_err.h>
397c478bd9Sstevel@tonic-gate #include <string.h>
407c478bd9Sstevel@tonic-gate #include <stdio.h>
41*56a424ccSmp153739 #ifdef HAVE_UNISTD_H
42*56a424ccSmp153739 #include <unistd.h>
43*56a424ccSmp153739 #endif
447c478bd9Sstevel@tonic-gate #include <locale.h>
457c478bd9Sstevel@tonic-gate #include <rpc/types.h>
467c478bd9Sstevel@tonic-gate #include <rpc/rpcsys.h>
477c478bd9Sstevel@tonic-gate #include <rpc/rpcsec_gss.h>
487c478bd9Sstevel@tonic-gate #include <syslog.h>
497c478bd9Sstevel@tonic-gate #include <libintl.h>
507c478bd9Sstevel@tonic-gate 
517c478bd9Sstevel@tonic-gate #ifdef KRB5_KRB4_COMPAT
527c478bd9Sstevel@tonic-gate #include <kerberosIV/krb.h>
537c478bd9Sstevel@tonic-gate #endif
547c478bd9Sstevel@tonic-gate 
557c478bd9Sstevel@tonic-gate #ifdef __STDC__
567c478bd9Sstevel@tonic-gate #define BELL_CHAR '\a'
577c478bd9Sstevel@tonic-gate #else
587c478bd9Sstevel@tonic-gate #define BELL_CHAR '\007'
597c478bd9Sstevel@tonic-gate #endif
607c478bd9Sstevel@tonic-gate 
617c478bd9Sstevel@tonic-gate extern int optind;
627c478bd9Sstevel@tonic-gate extern char *optarg;
637c478bd9Sstevel@tonic-gate 
647c478bd9Sstevel@tonic-gate #ifndef _WIN32
657c478bd9Sstevel@tonic-gate #define GET_PROGNAME(x) (strrchr((x), '/') ? strrchr((x), '/')+1 : (x))
667c478bd9Sstevel@tonic-gate #else
677c478bd9Sstevel@tonic-gate #define GET_PROGNAME(x) max(max(strrchr((x), '/'), strrchr((x), '\\')) + 1,(x))
687c478bd9Sstevel@tonic-gate #endif
697c478bd9Sstevel@tonic-gate 
707c478bd9Sstevel@tonic-gate char *progname;
717c478bd9Sstevel@tonic-gate 
727c478bd9Sstevel@tonic-gate int got_k5 = 0;
737c478bd9Sstevel@tonic-gate int got_k4 = 0;
747c478bd9Sstevel@tonic-gate 
757c478bd9Sstevel@tonic-gate int default_k5 = 1;
767c478bd9Sstevel@tonic-gate #ifdef KRB5_KRB4_COMPAT
777c478bd9Sstevel@tonic-gate int default_k4 = 1;
787c478bd9Sstevel@tonic-gate #else
797c478bd9Sstevel@tonic-gate int default_k4 = 0;
807c478bd9Sstevel@tonic-gate #endif
817c478bd9Sstevel@tonic-gate 
827c478bd9Sstevel@tonic-gate 
83*56a424ccSmp153739 static void usage()
847c478bd9Sstevel@tonic-gate {
857c478bd9Sstevel@tonic-gate #define KRB_AVAIL_STRING(x) ((x)?gettext("available"):gettext("not available"))
867c478bd9Sstevel@tonic-gate 
877c478bd9Sstevel@tonic-gate     fprintf(stderr, gettext("Usage"), ": %s [-5] [-4] [-q] [-c cache_name]\n",
887c478bd9Sstevel@tonic-gate             progname);
897c478bd9Sstevel@tonic-gate     fprintf(stderr, "\t-5 Kerberos 5 (%s)\n", KRB_AVAIL_STRING(got_k5));
907c478bd9Sstevel@tonic-gate     fprintf(stderr, "\t-4 Kerberos 4 (%s)\n", KRB_AVAIL_STRING(got_k4));
917c478bd9Sstevel@tonic-gate     fprintf(stderr, gettext("\t   (Default is %s%s%s%s)\n"),
927c478bd9Sstevel@tonic-gate 	    default_k5?"Kerberos 5":"",
937c478bd9Sstevel@tonic-gate 	    (default_k5 && default_k4)?gettext(" and "):"",
947c478bd9Sstevel@tonic-gate 	    default_k4?"Kerberos 4":"",
957c478bd9Sstevel@tonic-gate 	    (!default_k5 && !default_k4)?gettext("neither"):"");
967c478bd9Sstevel@tonic-gate     fprintf(stderr, gettext("\t-q quiet mode\n"));
977c478bd9Sstevel@tonic-gate     fprintf(stderr, gettext("\t-c specify name of credentials cache\n"));
987c478bd9Sstevel@tonic-gate     exit(2);
997c478bd9Sstevel@tonic-gate }
1007c478bd9Sstevel@tonic-gate 
1017c478bd9Sstevel@tonic-gate int
1027c478bd9Sstevel@tonic-gate main(argc, argv)
1037c478bd9Sstevel@tonic-gate     int argc;
1047c478bd9Sstevel@tonic-gate     char **argv;
1057c478bd9Sstevel@tonic-gate {
1067c478bd9Sstevel@tonic-gate     krb5_context kcontext;
1077c478bd9Sstevel@tonic-gate     krb5_error_code retval;
1087c478bd9Sstevel@tonic-gate     int c;
1097c478bd9Sstevel@tonic-gate     krb5_ccache cache = NULL;
1107c478bd9Sstevel@tonic-gate     char *cache_name = NULL;
1117c478bd9Sstevel@tonic-gate     char *client_name = NULL;
1127c478bd9Sstevel@tonic-gate     krb5_principal me;
1137c478bd9Sstevel@tonic-gate     int code = 0;
1147c478bd9Sstevel@tonic-gate #ifdef KRB5_KRB4_COMPAT
1157c478bd9Sstevel@tonic-gate     int v4code = 0;
1167c478bd9Sstevel@tonic-gate     int v4 = 1;
1177c478bd9Sstevel@tonic-gate #endif
1187c478bd9Sstevel@tonic-gate     int errflg = 0;
1197c478bd9Sstevel@tonic-gate     int quiet = 0;
1207c478bd9Sstevel@tonic-gate     struct krpc_revauth desarg;
1217c478bd9Sstevel@tonic-gate     static  rpc_gss_OID_desc oid=
1227c478bd9Sstevel@tonic-gate 	{9, "\052\206\110\206\367\022\001\002\002"};
1237c478bd9Sstevel@tonic-gate 
1247c478bd9Sstevel@tonic-gate     static  rpc_gss_OID krb5_mech_type = &oid;
1257c478bd9Sstevel@tonic-gate 
1267c478bd9Sstevel@tonic-gate     int use_k5 = 0;
1277c478bd9Sstevel@tonic-gate     int use_k4 = 0;
1287c478bd9Sstevel@tonic-gate 
1297c478bd9Sstevel@tonic-gate     /* set locale and domain for internationalization */
1307c478bd9Sstevel@tonic-gate     (void) setlocale(LC_ALL, "");
1317c478bd9Sstevel@tonic-gate 
1327c478bd9Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)
1337c478bd9Sstevel@tonic-gate #define TEXT_DOMAIN "SYS_TEST"
1347c478bd9Sstevel@tonic-gate #endif /* !TEXT_DOMAIN */
1357c478bd9Sstevel@tonic-gate 
1367c478bd9Sstevel@tonic-gate     (void) textdomain(TEXT_DOMAIN);
1377c478bd9Sstevel@tonic-gate 
1387c478bd9Sstevel@tonic-gate     got_k5 = 1;
1397c478bd9Sstevel@tonic-gate #ifdef KRB5_KRB4_COMPAT
1407c478bd9Sstevel@tonic-gate     got_k4 = 1;
1417c478bd9Sstevel@tonic-gate #endif
1427c478bd9Sstevel@tonic-gate 
1437c478bd9Sstevel@tonic-gate     progname = (strrchr(*argv, '/') ? strrchr(*argv, '/')+1 : argv[0]);
1447c478bd9Sstevel@tonic-gate 
1457c478bd9Sstevel@tonic-gate     while ((c = getopt(argc, argv, "54qc:")) != -1) {	switch (c) {
1467c478bd9Sstevel@tonic-gate 	case 'q':
1477c478bd9Sstevel@tonic-gate 	    quiet = 1;
1487c478bd9Sstevel@tonic-gate 	    break;
1497c478bd9Sstevel@tonic-gate 	case 'c':
1507c478bd9Sstevel@tonic-gate 	    if (cache_name) {
1517c478bd9Sstevel@tonic-gate 		fprintf(stderr, gettext("Only one -c option allowed\n"));
1527c478bd9Sstevel@tonic-gate 		errflg++;
1537c478bd9Sstevel@tonic-gate 	    } else {
1547c478bd9Sstevel@tonic-gate 		cache_name = optarg;
1557c478bd9Sstevel@tonic-gate 	    }
1567c478bd9Sstevel@tonic-gate 	    break;
1577c478bd9Sstevel@tonic-gate 	case '4':
1587c478bd9Sstevel@tonic-gate 	    if (!got_k4)
1597c478bd9Sstevel@tonic-gate 	    {
1607c478bd9Sstevel@tonic-gate #ifdef KRB5_KRB4_COMPAT
1617c478bd9Sstevel@tonic-gate 		fprintf(stderr, "Kerberos 4 support could not be loaded\n");
1627c478bd9Sstevel@tonic-gate #else
1637c478bd9Sstevel@tonic-gate 		fprintf(stderr, gettext("This was not built with Kerberos 4 support\n"));
1647c478bd9Sstevel@tonic-gate #endif
1657c478bd9Sstevel@tonic-gate 		exit(3);
1667c478bd9Sstevel@tonic-gate 	    }
1677c478bd9Sstevel@tonic-gate 	    use_k4 = 1;
1687c478bd9Sstevel@tonic-gate 	    break;
1697c478bd9Sstevel@tonic-gate 	case '5':
1707c478bd9Sstevel@tonic-gate 	    if (!got_k5)
1717c478bd9Sstevel@tonic-gate 	    {
1727c478bd9Sstevel@tonic-gate 		fprintf(stderr, gettext("Kerberos 5 support could not be loaded\n"));
1737c478bd9Sstevel@tonic-gate 		exit(3);
1747c478bd9Sstevel@tonic-gate 	    }
1757c478bd9Sstevel@tonic-gate 	    use_k5 = 1;
1767c478bd9Sstevel@tonic-gate 	    break;
1777c478bd9Sstevel@tonic-gate 	case '?':
1787c478bd9Sstevel@tonic-gate 	default:
1797c478bd9Sstevel@tonic-gate 	    errflg++;
1807c478bd9Sstevel@tonic-gate 	    break;
1817c478bd9Sstevel@tonic-gate 	}
1827c478bd9Sstevel@tonic-gate     }
1837c478bd9Sstevel@tonic-gate 
1847c478bd9Sstevel@tonic-gate     if (optind != argc)
1857c478bd9Sstevel@tonic-gate 	errflg++;
1867c478bd9Sstevel@tonic-gate 
1877c478bd9Sstevel@tonic-gate     if (errflg) {
1887c478bd9Sstevel@tonic-gate 	usage();
1897c478bd9Sstevel@tonic-gate     }
1907c478bd9Sstevel@tonic-gate 
1917c478bd9Sstevel@tonic-gate     if (!use_k5 && !use_k4)
1927c478bd9Sstevel@tonic-gate     {
1937c478bd9Sstevel@tonic-gate 	use_k5 = default_k5;
1947c478bd9Sstevel@tonic-gate 	use_k4 = default_k4;
1957c478bd9Sstevel@tonic-gate     }
1967c478bd9Sstevel@tonic-gate 
1977c478bd9Sstevel@tonic-gate     if (!use_k5)
1987c478bd9Sstevel@tonic-gate 	got_k5 = 0;
1997c478bd9Sstevel@tonic-gate     if (!use_k4)
2007c478bd9Sstevel@tonic-gate 	got_k4 = 0;
2017c478bd9Sstevel@tonic-gate 
2027c478bd9Sstevel@tonic-gate     if (got_k5) {
2037c478bd9Sstevel@tonic-gate 	retval = krb5_init_context(&kcontext);
2047c478bd9Sstevel@tonic-gate 	if (retval) {
2057c478bd9Sstevel@tonic-gate 	    com_err(progname, retval, gettext("while initializing krb5"));
2067c478bd9Sstevel@tonic-gate 	    exit(1);
2077c478bd9Sstevel@tonic-gate 	}
2087c478bd9Sstevel@tonic-gate 
2097c478bd9Sstevel@tonic-gate     	/*
2107c478bd9Sstevel@tonic-gate      	 *  Solaris Kerberos
2117c478bd9Sstevel@tonic-gate      	 *  Let us destroy the kernel cache first
2127c478bd9Sstevel@tonic-gate      	 */
2137c478bd9Sstevel@tonic-gate     	desarg.version = 1;
2147c478bd9Sstevel@tonic-gate     	desarg.uid_1 = geteuid();
2157c478bd9Sstevel@tonic-gate     	desarg.rpcsec_flavor_1 = RPCSEC_GSS;
2167c478bd9Sstevel@tonic-gate     	desarg.flavor_data_1 = (void *) krb5_mech_type;
2177c478bd9Sstevel@tonic-gate     	code = krpc_sys(KRPC_REVAUTH, (void *)&desarg);
2187c478bd9Sstevel@tonic-gate 
2197c478bd9Sstevel@tonic-gate     	if (code != 0) {
2207c478bd9Sstevel@tonic-gate         	fprintf(stderr,
2217c478bd9Sstevel@tonic-gate             		gettext("%s: kernel creds cache error %d \n"),
2227c478bd9Sstevel@tonic-gate             		progname, code);
2237c478bd9Sstevel@tonic-gate     	}
2247c478bd9Sstevel@tonic-gate 
2257c478bd9Sstevel@tonic-gate     	if (cache == NULL) {
2267c478bd9Sstevel@tonic-gate         	if (code = krb5_cc_default(kcontext, &cache)) {
2277c478bd9Sstevel@tonic-gate             	com_err(progname, code,
2287c478bd9Sstevel@tonic-gate                 	gettext("while getting default ccache"));
2297c478bd9Sstevel@tonic-gate             	exit(1);
2307c478bd9Sstevel@tonic-gate         	}
2317c478bd9Sstevel@tonic-gate     	}
2327c478bd9Sstevel@tonic-gate 
2337c478bd9Sstevel@tonic-gate 	if (cache_name) {
2347c478bd9Sstevel@tonic-gate 
2357c478bd9Sstevel@tonic-gate 
2367c478bd9Sstevel@tonic-gate 
2377c478bd9Sstevel@tonic-gate #ifdef KRB5_KRB4_COMPAT
2387c478bd9Sstevel@tonic-gate 	    v4 = 0;	/* Don't do v4 if doing v5 and cache name given. */
2397c478bd9Sstevel@tonic-gate #endif
2407c478bd9Sstevel@tonic-gate 	    code = krb5_cc_resolve (kcontext, cache_name, &cache);
2417c478bd9Sstevel@tonic-gate 	    if (code != 0) {
2427c478bd9Sstevel@tonic-gate 		com_err (progname, code, gettext("while resolving %s"), cache_name);
2437c478bd9Sstevel@tonic-gate 		exit(1);
2447c478bd9Sstevel@tonic-gate 	    }
2457c478bd9Sstevel@tonic-gate 	} else {
246*56a424ccSmp153739 	    code = krb5_cc_default(kcontext, &cache);
247*56a424ccSmp153739 	    if (code) {
2487c478bd9Sstevel@tonic-gate 		com_err(progname, code, gettext("while getting default ccache"));
2497c478bd9Sstevel@tonic-gate 		exit(1);
2507c478bd9Sstevel@tonic-gate 	    }
2517c478bd9Sstevel@tonic-gate 	}
2527c478bd9Sstevel@tonic-gate 
2537c478bd9Sstevel@tonic-gate 	/*
2547c478bd9Sstevel@tonic-gate 	 * Solaris Kerberos
2557c478bd9Sstevel@tonic-gate          * Get client name for kwarn_del_warning.
2567c478bd9Sstevel@tonic-gate 	 */
2577c478bd9Sstevel@tonic-gate         code = krb5_cc_get_principal(kcontext, cache, &me);
2587c478bd9Sstevel@tonic-gate         if (code != 0)
2597c478bd9Sstevel@tonic-gate             fprintf(stderr, gettext
2607c478bd9Sstevel@tonic-gate                 ("%s: Could not obtain principal name from cache\n"), progname);
2617c478bd9Sstevel@tonic-gate         else
2627c478bd9Sstevel@tonic-gate             if ((code = krb5_unparse_name(kcontext, me, &client_name)))
2637c478bd9Sstevel@tonic-gate                 fprintf(stderr, gettext
2647c478bd9Sstevel@tonic-gate                     ("%s: Could not unparse principal name found in cache\n"), progname);
2657c478bd9Sstevel@tonic-gate 
2667c478bd9Sstevel@tonic-gate 	code = krb5_cc_destroy (kcontext, cache);
2677c478bd9Sstevel@tonic-gate 	if (code != 0) {
2687c478bd9Sstevel@tonic-gate 	    com_err (progname, code, gettext("while destroying cache"));
2697c478bd9Sstevel@tonic-gate 	    if (code != KRB5_FCC_NOFILE) {
2707c478bd9Sstevel@tonic-gate 		if (quiet)
2717c478bd9Sstevel@tonic-gate 		    fprintf(stderr, gettext("Ticket cache NOT destroyed!\n"));
2727c478bd9Sstevel@tonic-gate 		else {
2737c478bd9Sstevel@tonic-gate 		    fprintf(stderr, gettext("Ticket cache %cNOT%c destroyed!\n"),
2747c478bd9Sstevel@tonic-gate 			    BELL_CHAR, BELL_CHAR);
2757c478bd9Sstevel@tonic-gate 		}
2767c478bd9Sstevel@tonic-gate 		errflg = 1;
2777c478bd9Sstevel@tonic-gate 	    }
2787c478bd9Sstevel@tonic-gate 	}
2797c478bd9Sstevel@tonic-gate     }
2807c478bd9Sstevel@tonic-gate #ifdef KRB5_KRB4_COMPAT
2817c478bd9Sstevel@tonic-gate     if (got_k4 && v4) {
2827c478bd9Sstevel@tonic-gate 	v4code = dest_tkt();
2837c478bd9Sstevel@tonic-gate 	if (v4code == KSUCCESS && code != 0)
2847c478bd9Sstevel@tonic-gate 	    fprintf(stderr, "Kerberos 4 ticket cache destroyed.\n");
2857c478bd9Sstevel@tonic-gate 	if (v4code != KSUCCESS && v4code != RET_TKFIL) {
2867c478bd9Sstevel@tonic-gate 	    if (quiet)
2877c478bd9Sstevel@tonic-gate 		fprintf(stderr, "Kerberos 4 ticket cache NOT destroyed!\n");
2887c478bd9Sstevel@tonic-gate 	    else
2897c478bd9Sstevel@tonic-gate 		fprintf(stderr, "Kerberos 4 ticket cache %cNOT%c destroyed!\n",
2907c478bd9Sstevel@tonic-gate 			BELL_CHAR, BELL_CHAR);
2917c478bd9Sstevel@tonic-gate 	    errflg = 1;
2927c478bd9Sstevel@tonic-gate 	}
2937c478bd9Sstevel@tonic-gate     }
2947c478bd9Sstevel@tonic-gate #endif
2957c478bd9Sstevel@tonic-gate 
2967c478bd9Sstevel@tonic-gate     /* Solaris Kerberos */
2977c478bd9Sstevel@tonic-gate     if (!errflg && client_name)
2987c478bd9Sstevel@tonic-gate         kwarn_del_warning(client_name);
2997c478bd9Sstevel@tonic-gate     else
3007c478bd9Sstevel@tonic-gate         fprintf(stderr, gettext
3017c478bd9Sstevel@tonic-gate             ("%s: TGT expire warning NOT deleted\n"), progname);
3027c478bd9Sstevel@tonic-gate 
3037c478bd9Sstevel@tonic-gate     return errflg;
3047c478bd9Sstevel@tonic-gate }
305