17c478bd9Sstevel@tonic-gate /*
2dd9ccd46S  * 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  * WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  *	Openvision retains the copyright to derivative works of
107c478bd9Sstevel@tonic-gate  *	this source code.  Do *NOT* create a derivative of this
117c478bd9Sstevel@tonic-gate  *	source code before consulting with your legal department.
127c478bd9Sstevel@tonic-gate  *	Do *NOT* integrate *ANY* of this source code into another
137c478bd9Sstevel@tonic-gate  *	product before consulting with your legal department.
147c478bd9Sstevel@tonic-gate  *
157c478bd9Sstevel@tonic-gate  *	For further information, read the top-level Openvision
167c478bd9Sstevel@tonic-gate  *	copyright which is contained in the top-level MIT Kerberos
177c478bd9Sstevel@tonic-gate  *	copyright.
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING
207c478bd9Sstevel@tonic-gate  *
217c478bd9Sstevel@tonic-gate  */
227c478bd9Sstevel@tonic-gate 
237c478bd9Sstevel@tonic-gate 
247c478bd9Sstevel@tonic-gate /*
257c478bd9Sstevel@tonic-gate  * admin/destroy/kdb5_destroy.c
267c478bd9Sstevel@tonic-gate  *
277c478bd9Sstevel@tonic-gate  * Copyright 1990 by the Massachusetts Institute of Technology.
287c478bd9Sstevel@tonic-gate  * All Rights Reserved.
297c478bd9Sstevel@tonic-gate  *
307c478bd9Sstevel@tonic-gate  * Export of this software from the United States of America may
317c478bd9Sstevel@tonic-gate  *   require a specific license from the United States Government.
327c478bd9Sstevel@tonic-gate  *   It is the responsibility of any person or organization contemplating
337c478bd9Sstevel@tonic-gate  *   export to obtain such a license before exporting.
34*55fea89dSDan Cross  *
357c478bd9Sstevel@tonic-gate  * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
367c478bd9Sstevel@tonic-gate  * distribute this software and its documentation for any purpose and
377c478bd9Sstevel@tonic-gate  * without fee is hereby granted, provided that the above copyright
387c478bd9Sstevel@tonic-gate  * notice appear in all copies and that both that copyright notice and
397c478bd9Sstevel@tonic-gate  * this permission notice appear in supporting documentation, and that
407c478bd9Sstevel@tonic-gate  * the name of M.I.T. not be used in advertising or publicity pertaining
417c478bd9Sstevel@tonic-gate  * to distribution of the software without specific, written prior
427c478bd9Sstevel@tonic-gate  * permission.  Furthermore if you modify this software you must label
437c478bd9Sstevel@tonic-gate  * your software as modified software and not distribute it in such a
447c478bd9Sstevel@tonic-gate  * fashion that it might be confused with the original M.I.T. software.
457c478bd9Sstevel@tonic-gate  * M.I.T. makes no representations about the suitability of
467c478bd9Sstevel@tonic-gate  * this software for any purpose.  It is provided "as is" without express
477c478bd9Sstevel@tonic-gate  * or implied warranty.
48*55fea89dSDan Cross  *
497c478bd9Sstevel@tonic-gate  *
507c478bd9Sstevel@tonic-gate  * kdb_dest(roy): destroy the named database.
517c478bd9Sstevel@tonic-gate  *
527c478bd9Sstevel@tonic-gate  * This version knows about DBM format databases.
537c478bd9Sstevel@tonic-gate  */
547c478bd9Sstevel@tonic-gate 
5554925bf6Swillf #include "k5-int.h"
567c478bd9Sstevel@tonic-gate #include <stdio.h>
577c478bd9Sstevel@tonic-gate #include "com_err.h"
587c478bd9Sstevel@tonic-gate #include <kadm5/admin.h>
5954925bf6Swillf #include <kdb.h>
607c478bd9Sstevel@tonic-gate #include <libintl.h>
617c478bd9Sstevel@tonic-gate #include "kdb5_util.h"
627c478bd9Sstevel@tonic-gate 
637c478bd9Sstevel@tonic-gate extern int exit_status;
647c478bd9Sstevel@tonic-gate extern krb5_boolean dbactive;
657c478bd9Sstevel@tonic-gate extern kadm5_config_params global_params;
667c478bd9Sstevel@tonic-gate 
677c478bd9Sstevel@tonic-gate void
kdb5_destroy(argc,argv)687c478bd9Sstevel@tonic-gate kdb5_destroy(argc, argv)
697c478bd9Sstevel@tonic-gate     int argc;
707c478bd9Sstevel@tonic-gate     char *argv[];
717c478bd9Sstevel@tonic-gate {
727c478bd9Sstevel@tonic-gate     extern char *optarg;
737c478bd9Sstevel@tonic-gate     extern int optind;
747c478bd9Sstevel@tonic-gate     int optchar;
757c478bd9Sstevel@tonic-gate     char *dbname;
767c478bd9Sstevel@tonic-gate     char buf[5];
7754925bf6Swillf     krb5_error_code retval1;
787c478bd9Sstevel@tonic-gate     krb5_context context;
7956a424ccSmp     int force = 0;
807c478bd9Sstevel@tonic-gate     char ufilename[MAX_FILENAME];
817c478bd9Sstevel@tonic-gate 
8254925bf6Swillf     retval1 = kadm5_init_krb5_context(&context);
8354925bf6Swillf     if( retval1 )
8454925bf6Swillf     {
85dd9ccd46S 	/* Solaris Kerberos */
86dd9ccd46S 	com_err(progname, retval1, "while initializing krb5_context");
8754925bf6Swillf 	exit(1);
8854925bf6Swillf     }
897c478bd9Sstevel@tonic-gate 
9054925bf6Swillf     if ((retval1 = krb5_set_default_realm(context,
9154925bf6Swillf 					  util_context->default_realm))) {
92dd9ccd46S 	/* Solaris Kerberos */
93dd9ccd46S 	com_err(progname, retval1, "while setting default realm name");
9454925bf6Swillf 	exit(1);
9554925bf6Swillf     }
96*55fea89dSDan Cross 
97dd9ccd46S /* Solaris Kerberos */
98dd9ccd46S #if 0
997c478bd9Sstevel@tonic-gate     if (strrchr(argv[0], '/'))
1007c478bd9Sstevel@tonic-gate 	argv[0] = strrchr(argv[0], '/')+1;
101dd9ccd46S #endif
1027c478bd9Sstevel@tonic-gate     dbname = global_params.dbname;
1037c478bd9Sstevel@tonic-gate 
10456a424ccSmp     optind = 1;
10556a424ccSmp     while ((optchar = getopt(argc, argv, "f")) != -1) {
10656a424ccSmp 	switch(optchar) {
10756a424ccSmp 	case 'f':
10856a424ccSmp 	    force++;
10956a424ccSmp 	    break;
11056a424ccSmp 	case '?':
11156a424ccSmp 	default:
11256a424ccSmp 	    usage();
11356a424ccSmp 	    return;
11456a424ccSmp 	    /*NOTREACHED*/
11556a424ccSmp 	}
1167c478bd9Sstevel@tonic-gate     }
11756a424ccSmp     if (!force) {
11856a424ccSmp 	printf(gettext("Deleting KDC database stored in '%s', "
11956a424ccSmp 		"are you sure?\n"), dbname);
12056a424ccSmp 	printf(gettext("(type 'yes' or 'y' to confirm)? "));
12156a424ccSmp 	if (fgets(buf, sizeof(buf), stdin) == NULL) {
12256a424ccSmp 	    exit_status++; return;
12356a424ccSmp         }
12456a424ccSmp 	if ((strncmp(buf, gettext("yes\n"),
125*55fea89dSDan Cross 	 	strlen(gettext("yes\n"))) != 0) &&
1267c478bd9Sstevel@tonic-gate 	(strncmp(buf, gettext("y\n"),
1277c478bd9Sstevel@tonic-gate 		strlen(gettext("y\n"))) != 0)) {
1287c478bd9Sstevel@tonic-gate 	printf(gettext("database not deleted !! '%s'...\n"),
1297c478bd9Sstevel@tonic-gate 		dbname);
1307c478bd9Sstevel@tonic-gate 
13156a424ccSmp 	    exit_status++; return;
13256a424ccSmp         }
13356a424ccSmp 	printf(gettext("OK, deleting database '%s'...\n"), dbname);
1347c478bd9Sstevel@tonic-gate     }
13556a424ccSmp 
13654925bf6Swillf     retval1 = krb5_db_destroy(context, db5util_db_args);
1377c478bd9Sstevel@tonic-gate 
1387c478bd9Sstevel@tonic-gate     /* check for a stash file and delete it if necessary */
1397c478bd9Sstevel@tonic-gate     if (global_params.stash_file == NULL) {
1407c478bd9Sstevel@tonic-gate 	char stash[MAXPATHLEN+1];
1417c478bd9Sstevel@tonic-gate 	extern krb5_principal master_princ;
1427c478bd9Sstevel@tonic-gate 	krb5_data *realm = krb5_princ_realm(context, master_princ);
143*55fea89dSDan Cross 	(void) strlcpy(stash, DEFAULT_KEYFILE_STUB, sizeof (stash));
1447c478bd9Sstevel@tonic-gate 	/*
1457c478bd9Sstevel@tonic-gate 	 * realm->data is not necessarily NULL terminated so be
1467c478bd9Sstevel@tonic-gate 	 * careful how much data is copied here.  Don't overrun
1477c478bd9Sstevel@tonic-gate 	 * the "stash" buffer and dont overrun the realm->data buffer,
1487c478bd9Sstevel@tonic-gate 	 * copy the smaller of the 2 lengths.
1497c478bd9Sstevel@tonic-gate 	 */
1507c478bd9Sstevel@tonic-gate 	(void) strncat(stash, realm->data,
1517c478bd9Sstevel@tonic-gate 		(realm->length < (MAXPATHLEN-strlen(stash)) ? realm->length :
1527c478bd9Sstevel@tonic-gate 		MAXPATHLEN-strlen(stash)));
1537c478bd9Sstevel@tonic-gate 	global_params.stash_file = (char *)strdup(stash);
1547c478bd9Sstevel@tonic-gate     }
1557c478bd9Sstevel@tonic-gate     if (!access(global_params.stash_file, F_OK))
1567c478bd9Sstevel@tonic-gate 	(void)unlink(global_params.stash_file);
1577c478bd9Sstevel@tonic-gate 
1587c478bd9Sstevel@tonic-gate     if (retval1) {
159dd9ccd46S 		/* Solaris Kerberos */
160dd9ccd46S 		com_err(progname, retval1,
1617c478bd9Sstevel@tonic-gate 			gettext("deleting database '%s'"), dbname);
16256a424ccSmp 	exit_status++; return;
1637c478bd9Sstevel@tonic-gate     }
1647c478bd9Sstevel@tonic-gate 
1657c478bd9Sstevel@tonic-gate     if (global_params.iprop_enabled) {
1667c478bd9Sstevel@tonic-gate 	if (strlcpy(ufilename, dbname, MAX_FILENAME) >= MAX_FILENAME) {
1677c478bd9Sstevel@tonic-gate 		exit_status++;
1687c478bd9Sstevel@tonic-gate 		return;
1697c478bd9Sstevel@tonic-gate 	}
1707c478bd9Sstevel@tonic-gate 	if (strlcat(ufilename, ".ulog", MAX_FILENAME) >= MAX_FILENAME) {
1717c478bd9Sstevel@tonic-gate 		exit_status++;
1727c478bd9Sstevel@tonic-gate 		return;
1737c478bd9Sstevel@tonic-gate 	}
1747c478bd9Sstevel@tonic-gate 
1757c478bd9Sstevel@tonic-gate 	(void) unlink(ufilename);
1767c478bd9Sstevel@tonic-gate     }
1777c478bd9Sstevel@tonic-gate 
1787c478bd9Sstevel@tonic-gate     dbactive = FALSE;
17956a424ccSmp     printf(gettext("** Database '%s' destroyed.\n"), dbname);
18056a424ccSmp     return;
1817c478bd9Sstevel@tonic-gate }
182