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