xref: /onnv-gate/usr/src/cmd/krb5/kdestroy/kdestroy.c (revision 7934:6aeeafc994de)
10Sstevel@tonic-gate /*
2*7934SMark.Phalan@Sun.COM  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
30Sstevel@tonic-gate  * Use is subject to license terms.
40Sstevel@tonic-gate  */
50Sstevel@tonic-gate 
60Sstevel@tonic-gate 
70Sstevel@tonic-gate /*
80Sstevel@tonic-gate  * clients/kdestroy/kdestroy.c
90Sstevel@tonic-gate  *
100Sstevel@tonic-gate  * Copyright 1990 by the Massachusetts Institute of Technology.
110Sstevel@tonic-gate  * All Rights Reserved.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * Export of this software from the United States of America may
140Sstevel@tonic-gate  *   require a specific license from the United States Government.
150Sstevel@tonic-gate  *   It is the responsibility of any person or organization contemplating
160Sstevel@tonic-gate  *   export to obtain such a license before exporting.
170Sstevel@tonic-gate  *
180Sstevel@tonic-gate  * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
190Sstevel@tonic-gate  * distribute this software and its documentation for any purpose and
200Sstevel@tonic-gate  * without fee is hereby granted, provided that the above copyright
210Sstevel@tonic-gate  * notice appear in all copies and that both that copyright notice and
220Sstevel@tonic-gate  * this permission notice appear in supporting documentation, and that
230Sstevel@tonic-gate  * the name of M.I.T. not be used in advertising or publicity pertaining
240Sstevel@tonic-gate  * to distribution of the software without specific, written prior
250Sstevel@tonic-gate  * permission.  Furthermore if you modify this software you must label
260Sstevel@tonic-gate  * your software as modified software and not distribute it in such a
270Sstevel@tonic-gate  * fashion that it might be confused with the original M.I.T. software.
280Sstevel@tonic-gate  * M.I.T. makes no representations about the suitability of
290Sstevel@tonic-gate  * this software for any purpose.  It is provided "as is" without express
300Sstevel@tonic-gate  * or implied warranty.
310Sstevel@tonic-gate  *
320Sstevel@tonic-gate  *
330Sstevel@tonic-gate  * Destroy the contents of your credential cache.
340Sstevel@tonic-gate  */
350Sstevel@tonic-gate 
360Sstevel@tonic-gate #include <krb5.h>
370Sstevel@tonic-gate #include <com_err.h>
380Sstevel@tonic-gate #include <string.h>
390Sstevel@tonic-gate #include <stdio.h>
402881Smp153739 #ifdef HAVE_UNISTD_H
412881Smp153739 #include <unistd.h>
422881Smp153739 #endif
430Sstevel@tonic-gate #include <locale.h>
440Sstevel@tonic-gate #include <rpc/types.h>
450Sstevel@tonic-gate #include <rpc/rpcsys.h>
460Sstevel@tonic-gate #include <rpc/rpcsec_gss.h>
470Sstevel@tonic-gate #include <syslog.h>
480Sstevel@tonic-gate #include <libintl.h>
490Sstevel@tonic-gate 
500Sstevel@tonic-gate #ifdef KRB5_KRB4_COMPAT
510Sstevel@tonic-gate #include <kerberosIV/krb.h>
520Sstevel@tonic-gate #endif
530Sstevel@tonic-gate 
540Sstevel@tonic-gate #ifdef __STDC__
550Sstevel@tonic-gate #define BELL_CHAR '\a'
560Sstevel@tonic-gate #else
570Sstevel@tonic-gate #define BELL_CHAR '\007'
580Sstevel@tonic-gate #endif
590Sstevel@tonic-gate 
600Sstevel@tonic-gate extern int optind;
610Sstevel@tonic-gate extern char *optarg;
620Sstevel@tonic-gate 
630Sstevel@tonic-gate #ifndef _WIN32
640Sstevel@tonic-gate #define GET_PROGNAME(x) (strrchr((x), '/') ? strrchr((x), '/')+1 : (x))
650Sstevel@tonic-gate #else
660Sstevel@tonic-gate #define GET_PROGNAME(x) max(max(strrchr((x), '/'), strrchr((x), '\\')) + 1,(x))
670Sstevel@tonic-gate #endif
680Sstevel@tonic-gate 
690Sstevel@tonic-gate char *progname;
700Sstevel@tonic-gate 
710Sstevel@tonic-gate int got_k5 = 0;
720Sstevel@tonic-gate int got_k4 = 0;
730Sstevel@tonic-gate 
740Sstevel@tonic-gate int default_k5 = 1;
750Sstevel@tonic-gate #ifdef KRB5_KRB4_COMPAT
760Sstevel@tonic-gate int default_k4 = 1;
770Sstevel@tonic-gate #else
780Sstevel@tonic-gate int default_k4 = 0;
790Sstevel@tonic-gate #endif
800Sstevel@tonic-gate 
810Sstevel@tonic-gate 
usage()822881Smp153739 static void usage()
830Sstevel@tonic-gate {
840Sstevel@tonic-gate #define KRB_AVAIL_STRING(x) ((x)?gettext("available"):gettext("not available"))
850Sstevel@tonic-gate 
860Sstevel@tonic-gate     fprintf(stderr, gettext("Usage"), ": %s [-5] [-4] [-q] [-c cache_name]\n",
870Sstevel@tonic-gate             progname);
880Sstevel@tonic-gate     fprintf(stderr, "\t-5 Kerberos 5 (%s)\n", KRB_AVAIL_STRING(got_k5));
890Sstevel@tonic-gate     fprintf(stderr, "\t-4 Kerberos 4 (%s)\n", KRB_AVAIL_STRING(got_k4));
900Sstevel@tonic-gate     fprintf(stderr, gettext("\t   (Default is %s%s%s%s)\n"),
910Sstevel@tonic-gate 	    default_k5?"Kerberos 5":"",
920Sstevel@tonic-gate 	    (default_k5 && default_k4)?gettext(" and "):"",
930Sstevel@tonic-gate 	    default_k4?"Kerberos 4":"",
940Sstevel@tonic-gate 	    (!default_k5 && !default_k4)?gettext("neither"):"");
950Sstevel@tonic-gate     fprintf(stderr, gettext("\t-q quiet mode\n"));
960Sstevel@tonic-gate     fprintf(stderr, gettext("\t-c specify name of credentials cache\n"));
970Sstevel@tonic-gate     exit(2);
980Sstevel@tonic-gate }
990Sstevel@tonic-gate 
1000Sstevel@tonic-gate int
main(argc,argv)1010Sstevel@tonic-gate main(argc, argv)
1020Sstevel@tonic-gate     int argc;
1030Sstevel@tonic-gate     char **argv;
1040Sstevel@tonic-gate {
1050Sstevel@tonic-gate     krb5_context kcontext;
1060Sstevel@tonic-gate     krb5_error_code retval;
1070Sstevel@tonic-gate     int c;
1080Sstevel@tonic-gate     krb5_ccache cache = NULL;
1090Sstevel@tonic-gate     char *cache_name = NULL;
1100Sstevel@tonic-gate     char *client_name = NULL;
1110Sstevel@tonic-gate     krb5_principal me;
1120Sstevel@tonic-gate     int code = 0;
1130Sstevel@tonic-gate #ifdef KRB5_KRB4_COMPAT
1140Sstevel@tonic-gate     int v4code = 0;
1150Sstevel@tonic-gate     int v4 = 1;
1160Sstevel@tonic-gate #endif
1170Sstevel@tonic-gate     int errflg = 0;
1180Sstevel@tonic-gate     int quiet = 0;
1190Sstevel@tonic-gate     struct krpc_revauth desarg;
1200Sstevel@tonic-gate     static  rpc_gss_OID_desc oid=
1210Sstevel@tonic-gate 	{9, "\052\206\110\206\367\022\001\002\002"};
1220Sstevel@tonic-gate 
1230Sstevel@tonic-gate     static  rpc_gss_OID krb5_mech_type = &oid;
1240Sstevel@tonic-gate 
1250Sstevel@tonic-gate     int use_k5 = 0;
1260Sstevel@tonic-gate     int use_k4 = 0;
1270Sstevel@tonic-gate 
128*7934SMark.Phalan@Sun.COM     progname = GET_PROGNAME(argv[0]);
1290Sstevel@tonic-gate     /* set locale and domain for internationalization */
1300Sstevel@tonic-gate     (void) setlocale(LC_ALL, "");
1310Sstevel@tonic-gate 
1320Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)
1330Sstevel@tonic-gate #define TEXT_DOMAIN "SYS_TEST"
1340Sstevel@tonic-gate #endif /* !TEXT_DOMAIN */
1350Sstevel@tonic-gate 
1360Sstevel@tonic-gate     (void) textdomain(TEXT_DOMAIN);
1370Sstevel@tonic-gate 
1380Sstevel@tonic-gate     got_k5 = 1;
1390Sstevel@tonic-gate #ifdef KRB5_KRB4_COMPAT
1400Sstevel@tonic-gate     got_k4 = 1;
1410Sstevel@tonic-gate #endif
1420Sstevel@tonic-gate 
143*7934SMark.Phalan@Sun.COM     while ((c = getopt(argc, argv, "54qc:")) != -1) {
144*7934SMark.Phalan@Sun.COM 	switch (c) {
1450Sstevel@tonic-gate 	case 'q':
1460Sstevel@tonic-gate 	    quiet = 1;
1470Sstevel@tonic-gate 	    break;
1480Sstevel@tonic-gate 	case 'c':
1490Sstevel@tonic-gate 	    if (cache_name) {
1500Sstevel@tonic-gate 		fprintf(stderr, gettext("Only one -c option allowed\n"));
1510Sstevel@tonic-gate 		errflg++;
1520Sstevel@tonic-gate 	    } else {
1530Sstevel@tonic-gate 		cache_name = optarg;
1540Sstevel@tonic-gate 	    }
1550Sstevel@tonic-gate 	    break;
1560Sstevel@tonic-gate 	case '4':
1570Sstevel@tonic-gate 	    if (!got_k4)
1580Sstevel@tonic-gate 	    {
1590Sstevel@tonic-gate #ifdef KRB5_KRB4_COMPAT
1600Sstevel@tonic-gate 		fprintf(stderr, "Kerberos 4 support could not be loaded\n");
1610Sstevel@tonic-gate #else
1620Sstevel@tonic-gate 		fprintf(stderr, gettext("This was not built with Kerberos 4 support\n"));
1630Sstevel@tonic-gate #endif
1640Sstevel@tonic-gate 		exit(3);
1650Sstevel@tonic-gate 	    }
1660Sstevel@tonic-gate 	    use_k4 = 1;
1670Sstevel@tonic-gate 	    break;
1680Sstevel@tonic-gate 	case '5':
1690Sstevel@tonic-gate 	    if (!got_k5)
1700Sstevel@tonic-gate 	    {
1710Sstevel@tonic-gate 		fprintf(stderr, gettext("Kerberos 5 support could not be loaded\n"));
1720Sstevel@tonic-gate 		exit(3);
1730Sstevel@tonic-gate 	    }
1740Sstevel@tonic-gate 	    use_k5 = 1;
1750Sstevel@tonic-gate 	    break;
1760Sstevel@tonic-gate 	case '?':
1770Sstevel@tonic-gate 	default:
1780Sstevel@tonic-gate 	    errflg++;
1790Sstevel@tonic-gate 	    break;
1800Sstevel@tonic-gate 	}
1810Sstevel@tonic-gate     }
1820Sstevel@tonic-gate 
1830Sstevel@tonic-gate     if (optind != argc)
1840Sstevel@tonic-gate 	errflg++;
1850Sstevel@tonic-gate 
1860Sstevel@tonic-gate     if (errflg) {
1870Sstevel@tonic-gate 	usage();
1880Sstevel@tonic-gate     }
1890Sstevel@tonic-gate 
1900Sstevel@tonic-gate     if (!use_k5 && !use_k4)
1910Sstevel@tonic-gate     {
1920Sstevel@tonic-gate 	use_k5 = default_k5;
1930Sstevel@tonic-gate 	use_k4 = default_k4;
1940Sstevel@tonic-gate     }
1950Sstevel@tonic-gate 
1960Sstevel@tonic-gate     if (!use_k5)
1970Sstevel@tonic-gate 	got_k5 = 0;
1980Sstevel@tonic-gate     if (!use_k4)
1990Sstevel@tonic-gate 	got_k4 = 0;
2000Sstevel@tonic-gate 
2010Sstevel@tonic-gate     if (got_k5) {
2020Sstevel@tonic-gate 	retval = krb5_init_context(&kcontext);
2030Sstevel@tonic-gate 	if (retval) {
2040Sstevel@tonic-gate 	    com_err(progname, retval, gettext("while initializing krb5"));
2050Sstevel@tonic-gate 	    exit(1);
2060Sstevel@tonic-gate 	}
2070Sstevel@tonic-gate 
2080Sstevel@tonic-gate     	/*
2090Sstevel@tonic-gate      	 *  Solaris Kerberos
2100Sstevel@tonic-gate      	 *  Let us destroy the kernel cache first
2110Sstevel@tonic-gate      	 */
2120Sstevel@tonic-gate     	desarg.version = 1;
2130Sstevel@tonic-gate     	desarg.uid_1 = geteuid();
2140Sstevel@tonic-gate     	desarg.rpcsec_flavor_1 = RPCSEC_GSS;
2150Sstevel@tonic-gate     	desarg.flavor_data_1 = (void *) krb5_mech_type;
2160Sstevel@tonic-gate     	code = krpc_sys(KRPC_REVAUTH, (void *)&desarg);
2170Sstevel@tonic-gate 
2180Sstevel@tonic-gate     	if (code != 0) {
2190Sstevel@tonic-gate         	fprintf(stderr,
2200Sstevel@tonic-gate             		gettext("%s: kernel creds cache error %d \n"),
2210Sstevel@tonic-gate             		progname, code);
2220Sstevel@tonic-gate     	}
2230Sstevel@tonic-gate 
2240Sstevel@tonic-gate     	if (cache == NULL) {
2250Sstevel@tonic-gate         	if (code = krb5_cc_default(kcontext, &cache)) {
2260Sstevel@tonic-gate             	com_err(progname, code,
2270Sstevel@tonic-gate                 	gettext("while getting default ccache"));
2280Sstevel@tonic-gate             	exit(1);
2290Sstevel@tonic-gate         	}
2300Sstevel@tonic-gate     	}
2310Sstevel@tonic-gate 
2320Sstevel@tonic-gate 	if (cache_name) {
2330Sstevel@tonic-gate #ifdef KRB5_KRB4_COMPAT
2340Sstevel@tonic-gate 	    v4 = 0;	/* Don't do v4 if doing v5 and cache name given. */
2350Sstevel@tonic-gate #endif
2360Sstevel@tonic-gate 	    code = krb5_cc_resolve (kcontext, cache_name, &cache);
2370Sstevel@tonic-gate 	    if (code != 0) {
2380Sstevel@tonic-gate 		com_err (progname, code, gettext("while resolving %s"), cache_name);
2390Sstevel@tonic-gate 		exit(1);
2400Sstevel@tonic-gate 	    }
2410Sstevel@tonic-gate 	} else {
2422881Smp153739 	    code = krb5_cc_default(kcontext, &cache);
2432881Smp153739 	    if (code) {
2440Sstevel@tonic-gate 		com_err(progname, code, gettext("while getting default ccache"));
2450Sstevel@tonic-gate 		exit(1);
2460Sstevel@tonic-gate 	    }
2470Sstevel@tonic-gate 	}
2480Sstevel@tonic-gate 
2490Sstevel@tonic-gate 	/*
2500Sstevel@tonic-gate 	 * Solaris Kerberos
2510Sstevel@tonic-gate          * Get client name for kwarn_del_warning.
2520Sstevel@tonic-gate 	 */
2530Sstevel@tonic-gate         code = krb5_cc_get_principal(kcontext, cache, &me);
2540Sstevel@tonic-gate         if (code != 0)
2550Sstevel@tonic-gate             fprintf(stderr, gettext
2560Sstevel@tonic-gate                 ("%s: Could not obtain principal name from cache\n"), progname);
2570Sstevel@tonic-gate         else
2580Sstevel@tonic-gate             if ((code = krb5_unparse_name(kcontext, me, &client_name)))
2590Sstevel@tonic-gate                 fprintf(stderr, gettext
2600Sstevel@tonic-gate                     ("%s: Could not unparse principal name found in cache\n"), progname);
2610Sstevel@tonic-gate 
2620Sstevel@tonic-gate 	code = krb5_cc_destroy (kcontext, cache);
2630Sstevel@tonic-gate 	if (code != 0) {
2640Sstevel@tonic-gate 	    com_err (progname, code, gettext("while destroying cache"));
2650Sstevel@tonic-gate 	    if (code != KRB5_FCC_NOFILE) {
2660Sstevel@tonic-gate 		if (quiet)
2670Sstevel@tonic-gate 		    fprintf(stderr, gettext("Ticket cache NOT destroyed!\n"));
2680Sstevel@tonic-gate 		else {
2690Sstevel@tonic-gate 		    fprintf(stderr, gettext("Ticket cache %cNOT%c destroyed!\n"),
2700Sstevel@tonic-gate 			    BELL_CHAR, BELL_CHAR);
2710Sstevel@tonic-gate 		}
2720Sstevel@tonic-gate 		errflg = 1;
2730Sstevel@tonic-gate 	    }
2740Sstevel@tonic-gate 	}
2750Sstevel@tonic-gate     }
2760Sstevel@tonic-gate #ifdef KRB5_KRB4_COMPAT
2770Sstevel@tonic-gate     if (got_k4 && v4) {
2780Sstevel@tonic-gate 	v4code = dest_tkt();
2790Sstevel@tonic-gate 	if (v4code == KSUCCESS && code != 0)
2800Sstevel@tonic-gate 	    fprintf(stderr, "Kerberos 4 ticket cache destroyed.\n");
2810Sstevel@tonic-gate 	if (v4code != KSUCCESS && v4code != RET_TKFIL) {
2820Sstevel@tonic-gate 	    if (quiet)
2830Sstevel@tonic-gate 		fprintf(stderr, "Kerberos 4 ticket cache NOT destroyed!\n");
2840Sstevel@tonic-gate 	    else
2850Sstevel@tonic-gate 		fprintf(stderr, "Kerberos 4 ticket cache %cNOT%c destroyed!\n",
2860Sstevel@tonic-gate 			BELL_CHAR, BELL_CHAR);
2870Sstevel@tonic-gate 	    errflg = 1;
2880Sstevel@tonic-gate 	}
2890Sstevel@tonic-gate     }
2900Sstevel@tonic-gate #endif
2910Sstevel@tonic-gate 
2920Sstevel@tonic-gate     /* Solaris Kerberos */
2930Sstevel@tonic-gate     if (!errflg && client_name)
2940Sstevel@tonic-gate         kwarn_del_warning(client_name);
2950Sstevel@tonic-gate     else
2960Sstevel@tonic-gate         fprintf(stderr, gettext
2970Sstevel@tonic-gate             ("%s: TGT expire warning NOT deleted\n"), progname);
2980Sstevel@tonic-gate 
2990Sstevel@tonic-gate     return errflg;
3000Sstevel@tonic-gate }
301