xref: /onnv-gate/usr/src/cmd/krb5/kadmin/dbutil/kdb5_destroy.c (revision 8092:19771b16f0a8)
10Sstevel@tonic-gate /*
2*8092SMark.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  * WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING
80Sstevel@tonic-gate  *
90Sstevel@tonic-gate  *	Openvision retains the copyright to derivative works of
100Sstevel@tonic-gate  *	this source code.  Do *NOT* create a derivative of this
110Sstevel@tonic-gate  *	source code before consulting with your legal department.
120Sstevel@tonic-gate  *	Do *NOT* integrate *ANY* of this source code into another
130Sstevel@tonic-gate  *	product before consulting with your legal department.
140Sstevel@tonic-gate  *
150Sstevel@tonic-gate  *	For further information, read the top-level Openvision
160Sstevel@tonic-gate  *	copyright which is contained in the top-level MIT Kerberos
170Sstevel@tonic-gate  *	copyright.
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING
200Sstevel@tonic-gate  *
210Sstevel@tonic-gate  */
220Sstevel@tonic-gate 
230Sstevel@tonic-gate 
240Sstevel@tonic-gate /*
250Sstevel@tonic-gate  * admin/destroy/kdb5_destroy.c
260Sstevel@tonic-gate  *
270Sstevel@tonic-gate  * Copyright 1990 by the Massachusetts Institute of Technology.
280Sstevel@tonic-gate  * All Rights Reserved.
290Sstevel@tonic-gate  *
300Sstevel@tonic-gate  * Export of this software from the United States of America may
310Sstevel@tonic-gate  *   require a specific license from the United States Government.
320Sstevel@tonic-gate  *   It is the responsibility of any person or organization contemplating
330Sstevel@tonic-gate  *   export to obtain such a license before exporting.
340Sstevel@tonic-gate  *
350Sstevel@tonic-gate  * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
360Sstevel@tonic-gate  * distribute this software and its documentation for any purpose and
370Sstevel@tonic-gate  * without fee is hereby granted, provided that the above copyright
380Sstevel@tonic-gate  * notice appear in all copies and that both that copyright notice and
390Sstevel@tonic-gate  * this permission notice appear in supporting documentation, and that
400Sstevel@tonic-gate  * the name of M.I.T. not be used in advertising or publicity pertaining
410Sstevel@tonic-gate  * to distribution of the software without specific, written prior
420Sstevel@tonic-gate  * permission.  Furthermore if you modify this software you must label
430Sstevel@tonic-gate  * your software as modified software and not distribute it in such a
440Sstevel@tonic-gate  * fashion that it might be confused with the original M.I.T. software.
450Sstevel@tonic-gate  * M.I.T. makes no representations about the suitability of
460Sstevel@tonic-gate  * this software for any purpose.  It is provided "as is" without express
470Sstevel@tonic-gate  * or implied warranty.
480Sstevel@tonic-gate  *
490Sstevel@tonic-gate  *
500Sstevel@tonic-gate  * kdb_dest(roy): destroy the named database.
510Sstevel@tonic-gate  *
520Sstevel@tonic-gate  * This version knows about DBM format databases.
530Sstevel@tonic-gate  */
540Sstevel@tonic-gate 
554960Swillf #include "k5-int.h"
560Sstevel@tonic-gate #include <stdio.h>
570Sstevel@tonic-gate #include "com_err.h"
580Sstevel@tonic-gate #include <kadm5/admin.h>
594960Swillf #include <kdb.h>
600Sstevel@tonic-gate #include <libintl.h>
610Sstevel@tonic-gate #include "kdb5_util.h"
620Sstevel@tonic-gate 
630Sstevel@tonic-gate extern int exit_status;
640Sstevel@tonic-gate extern krb5_boolean dbactive;
650Sstevel@tonic-gate extern kadm5_config_params global_params;
660Sstevel@tonic-gate 
670Sstevel@tonic-gate void
kdb5_destroy(argc,argv)680Sstevel@tonic-gate kdb5_destroy(argc, argv)
690Sstevel@tonic-gate     int argc;
700Sstevel@tonic-gate     char *argv[];
710Sstevel@tonic-gate {
720Sstevel@tonic-gate     extern char *optarg;
730Sstevel@tonic-gate     extern int optind;
740Sstevel@tonic-gate     int optchar;
750Sstevel@tonic-gate     char *dbname;
760Sstevel@tonic-gate     char buf[5];
774960Swillf     krb5_error_code retval1;
780Sstevel@tonic-gate     krb5_context context;
792881Smp153739     int force = 0;
800Sstevel@tonic-gate     char ufilename[MAX_FILENAME];
810Sstevel@tonic-gate 
824960Swillf     retval1 = kadm5_init_krb5_context(&context);
834960Swillf     if( retval1 )
844960Swillf     {
85*8092SMark.Phalan@Sun.COM 	/* Solaris Kerberos */
86*8092SMark.Phalan@Sun.COM 	com_err(progname, retval1, "while initializing krb5_context");
874960Swillf 	exit(1);
884960Swillf     }
890Sstevel@tonic-gate 
904960Swillf     if ((retval1 = krb5_set_default_realm(context,
914960Swillf 					  util_context->default_realm))) {
92*8092SMark.Phalan@Sun.COM 	/* Solaris Kerberos */
93*8092SMark.Phalan@Sun.COM 	com_err(progname, retval1, "while setting default realm name");
944960Swillf 	exit(1);
954960Swillf     }
964960Swillf 
97*8092SMark.Phalan@Sun.COM /* Solaris Kerberos */
98*8092SMark.Phalan@Sun.COM #if 0
990Sstevel@tonic-gate     if (strrchr(argv[0], '/'))
1000Sstevel@tonic-gate 	argv[0] = strrchr(argv[0], '/')+1;
101*8092SMark.Phalan@Sun.COM #endif
1020Sstevel@tonic-gate     dbname = global_params.dbname;
1030Sstevel@tonic-gate 
1042881Smp153739     optind = 1;
1052881Smp153739     while ((optchar = getopt(argc, argv, "f")) != -1) {
1062881Smp153739 	switch(optchar) {
1072881Smp153739 	case 'f':
1082881Smp153739 	    force++;
1092881Smp153739 	    break;
1102881Smp153739 	case '?':
1112881Smp153739 	default:
1122881Smp153739 	    usage();
1132881Smp153739 	    return;
1142881Smp153739 	    /*NOTREACHED*/
1152881Smp153739 	}
1162881Smp153739     }
1172881Smp153739     if (!force) {
1182881Smp153739 	printf(gettext("Deleting KDC database stored in '%s', "
1190Sstevel@tonic-gate 		"are you sure?\n"), dbname);
1202881Smp153739 	printf(gettext("(type 'yes' or 'y' to confirm)? "));
1212881Smp153739 	if (fgets(buf, sizeof(buf), stdin) == NULL) {
1222881Smp153739 	    exit_status++; return;
1232881Smp153739         }
1242881Smp153739 	if ((strncmp(buf, gettext("yes\n"),
1250Sstevel@tonic-gate 	 	strlen(gettext("yes\n"))) != 0) &&
1260Sstevel@tonic-gate 	(strncmp(buf, gettext("y\n"),
1270Sstevel@tonic-gate 		strlen(gettext("y\n"))) != 0)) {
1280Sstevel@tonic-gate 	printf(gettext("database not deleted !! '%s'...\n"),
1290Sstevel@tonic-gate 		dbname);
1300Sstevel@tonic-gate 
1312881Smp153739 	    exit_status++; return;
1322881Smp153739         }
1332881Smp153739 	printf(gettext("OK, deleting database '%s'...\n"), dbname);
1340Sstevel@tonic-gate     }
1352881Smp153739 
1364960Swillf     retval1 = krb5_db_destroy(context, db5util_db_args);
1370Sstevel@tonic-gate 
1380Sstevel@tonic-gate     /* check for a stash file and delete it if necessary */
1390Sstevel@tonic-gate     if (global_params.stash_file == NULL) {
1400Sstevel@tonic-gate 	char stash[MAXPATHLEN+1];
1410Sstevel@tonic-gate 	extern krb5_principal master_princ;
1420Sstevel@tonic-gate 	krb5_data *realm = krb5_princ_realm(context, master_princ);
1430Sstevel@tonic-gate 	(void) strlcpy(stash, DEFAULT_KEYFILE_STUB, sizeof (stash));
1440Sstevel@tonic-gate 	/*
1450Sstevel@tonic-gate 	 * realm->data is not necessarily NULL terminated so be
1460Sstevel@tonic-gate 	 * careful how much data is copied here.  Don't overrun
1470Sstevel@tonic-gate 	 * the "stash" buffer and dont overrun the realm->data buffer,
1480Sstevel@tonic-gate 	 * copy the smaller of the 2 lengths.
1490Sstevel@tonic-gate 	 */
1500Sstevel@tonic-gate 	(void) strncat(stash, realm->data,
1510Sstevel@tonic-gate 		(realm->length < (MAXPATHLEN-strlen(stash)) ? realm->length :
1520Sstevel@tonic-gate 		MAXPATHLEN-strlen(stash)));
1530Sstevel@tonic-gate 	global_params.stash_file = (char *)strdup(stash);
1540Sstevel@tonic-gate     }
1550Sstevel@tonic-gate     if (!access(global_params.stash_file, F_OK))
1560Sstevel@tonic-gate 	(void)unlink(global_params.stash_file);
1570Sstevel@tonic-gate 
1580Sstevel@tonic-gate     if (retval1) {
159*8092SMark.Phalan@Sun.COM 		/* Solaris Kerberos */
160*8092SMark.Phalan@Sun.COM 		com_err(progname, retval1,
1610Sstevel@tonic-gate 			gettext("deleting database '%s'"), dbname);
1622881Smp153739 	exit_status++; return;
1630Sstevel@tonic-gate     }
1640Sstevel@tonic-gate 
1650Sstevel@tonic-gate     if (global_params.iprop_enabled) {
1660Sstevel@tonic-gate 	if (strlcpy(ufilename, dbname, MAX_FILENAME) >= MAX_FILENAME) {
1670Sstevel@tonic-gate 		exit_status++;
1680Sstevel@tonic-gate 		return;
1690Sstevel@tonic-gate 	}
1700Sstevel@tonic-gate 	if (strlcat(ufilename, ".ulog", MAX_FILENAME) >= MAX_FILENAME) {
1710Sstevel@tonic-gate 		exit_status++;
1720Sstevel@tonic-gate 		return;
1730Sstevel@tonic-gate 	}
1740Sstevel@tonic-gate 
1750Sstevel@tonic-gate 	(void) unlink(ufilename);
1760Sstevel@tonic-gate     }
1770Sstevel@tonic-gate 
1780Sstevel@tonic-gate     dbactive = FALSE;
1792881Smp153739     printf(gettext("** Database '%s' destroyed.\n"), dbname);
1802881Smp153739     return;
1810Sstevel@tonic-gate }
182