1/*
2 * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
3 * Use is subject to license terms.
4 */
5
6/*
7 * WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING
8 *
9 *	Openvision retains the copyright to derivative works of
10 *	this source code.  Do *NOT* create a derivative of this
11 *	source code before consulting with your legal department.
12 *	Do *NOT* integrate *ANY* of this source code into another
13 *	product before consulting with your legal department.
14 *
15 *	For further information, read the top-level Openvision
16 *	copyright which is contained in the top-level MIT Kerberos
17 *	copyright.
18 *
19 * WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING
20 *
21 */
22
23
24/*
25 * admin/destroy/kdb5_destroy.c
26 *
27 * Copyright 1990 by the Massachusetts Institute of Technology.
28 * All Rights Reserved.
29 *
30 * Export of this software from the United States of America may
31 *   require a specific license from the United States Government.
32 *   It is the responsibility of any person or organization contemplating
33 *   export to obtain such a license before exporting.
34 *
35 * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
36 * distribute this software and its documentation for any purpose and
37 * without fee is hereby granted, provided that the above copyright
38 * notice appear in all copies and that both that copyright notice and
39 * this permission notice appear in supporting documentation, and that
40 * the name of M.I.T. not be used in advertising or publicity pertaining
41 * to distribution of the software without specific, written prior
42 * permission.  Furthermore if you modify this software you must label
43 * your software as modified software and not distribute it in such a
44 * fashion that it might be confused with the original M.I.T. software.
45 * M.I.T. makes no representations about the suitability of
46 * this software for any purpose.  It is provided "as is" without express
47 * or implied warranty.
48 *
49 *
50 * kdb_dest(roy): destroy the named database.
51 *
52 * This version knows about DBM format databases.
53 */
54
55#include "k5-int.h"
56#include <stdio.h>
57#include "com_err.h"
58#include <kadm5/admin.h>
59#include <kdb.h>
60#include <libintl.h>
61#include "kdb5_util.h"
62
63extern int exit_status;
64extern krb5_boolean dbactive;
65extern kadm5_config_params global_params;
66
67void
68kdb5_destroy(argc, argv)
69    int argc;
70    char *argv[];
71{
72    extern char *optarg;
73    extern int optind;
74    int optchar;
75    char *dbname;
76    char buf[5];
77    krb5_error_code retval1;
78    krb5_context context;
79    int force = 0;
80    char ufilename[MAX_FILENAME];
81
82    retval1 = kadm5_init_krb5_context(&context);
83    if( retval1 )
84    {
85	/* Solaris Kerberos */
86	com_err(progname, retval1, "while initializing krb5_context");
87	exit(1);
88    }
89
90    if ((retval1 = krb5_set_default_realm(context,
91					  util_context->default_realm))) {
92	/* Solaris Kerberos */
93	com_err(progname, retval1, "while setting default realm name");
94	exit(1);
95    }
96
97/* Solaris Kerberos */
98#if 0
99    if (strrchr(argv[0], '/'))
100	argv[0] = strrchr(argv[0], '/')+1;
101#endif
102    dbname = global_params.dbname;
103
104    optind = 1;
105    while ((optchar = getopt(argc, argv, "f")) != -1) {
106	switch(optchar) {
107	case 'f':
108	    force++;
109	    break;
110	case '?':
111	default:
112	    usage();
113	    return;
114	    /*NOTREACHED*/
115	}
116    }
117    if (!force) {
118	printf(gettext("Deleting KDC database stored in '%s', "
119		"are you sure?\n"), dbname);
120	printf(gettext("(type 'yes' or 'y' to confirm)? "));
121	if (fgets(buf, sizeof(buf), stdin) == NULL) {
122	    exit_status++; return;
123        }
124	if ((strncmp(buf, gettext("yes\n"),
125	 	strlen(gettext("yes\n"))) != 0) &&
126	(strncmp(buf, gettext("y\n"),
127		strlen(gettext("y\n"))) != 0)) {
128	printf(gettext("database not deleted !! '%s'...\n"),
129		dbname);
130
131	    exit_status++; return;
132        }
133	printf(gettext("OK, deleting database '%s'...\n"), dbname);
134    }
135
136    retval1 = krb5_db_destroy(context, db5util_db_args);
137
138    /* check for a stash file and delete it if necessary */
139    if (global_params.stash_file == NULL) {
140	char stash[MAXPATHLEN+1];
141	extern krb5_principal master_princ;
142	krb5_data *realm = krb5_princ_realm(context, master_princ);
143	(void) strlcpy(stash, DEFAULT_KEYFILE_STUB, sizeof (stash));
144	/*
145	 * realm->data is not necessarily NULL terminated so be
146	 * careful how much data is copied here.  Don't overrun
147	 * the "stash" buffer and dont overrun the realm->data buffer,
148	 * copy the smaller of the 2 lengths.
149	 */
150	(void) strncat(stash, realm->data,
151		(realm->length < (MAXPATHLEN-strlen(stash)) ? realm->length :
152		MAXPATHLEN-strlen(stash)));
153	global_params.stash_file = (char *)strdup(stash);
154    }
155    if (!access(global_params.stash_file, F_OK))
156	(void)unlink(global_params.stash_file);
157
158    if (retval1) {
159		/* Solaris Kerberos */
160		com_err(progname, retval1,
161			gettext("deleting database '%s'"), dbname);
162	exit_status++; return;
163    }
164
165    if (global_params.iprop_enabled) {
166	if (strlcpy(ufilename, dbname, MAX_FILENAME) >= MAX_FILENAME) {
167		exit_status++;
168		return;
169	}
170	if (strlcat(ufilename, ".ulog", MAX_FILENAME) >= MAX_FILENAME) {
171		exit_status++;
172		return;
173	}
174
175	(void) unlink(ufilename);
176    }
177
178    dbactive = FALSE;
179    printf(gettext("** Database '%s' destroyed.\n"), dbname);
180    return;
181}
182