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