xref: /titanic_51/usr/src/cmd/cmd-crypto/kmfcfg/delete.c (revision 99ebb4ca412cb0a19d77a3899a87c055b9c30fa8)
1*99ebb4caSwyllys /*
2*99ebb4caSwyllys  * CDDL HEADER START
3*99ebb4caSwyllys  *
4*99ebb4caSwyllys  * The contents of this file are subject to the terms of the
5*99ebb4caSwyllys  * Common Development and Distribution License (the "License").
6*99ebb4caSwyllys  * You may not use this file except in compliance with the License.
7*99ebb4caSwyllys  *
8*99ebb4caSwyllys  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9*99ebb4caSwyllys  * or http://www.opensolaris.org/os/licensing.
10*99ebb4caSwyllys  * See the License for the specific language governing permissions
11*99ebb4caSwyllys  * and limitations under the License.
12*99ebb4caSwyllys  *
13*99ebb4caSwyllys  * When distributing Covered Code, include this CDDL HEADER in each
14*99ebb4caSwyllys  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15*99ebb4caSwyllys  * If applicable, add the following below this CDDL HEADER, with the
16*99ebb4caSwyllys  * fields enclosed by brackets "[]" replaced with your own identifying
17*99ebb4caSwyllys  * information: Portions Copyright [yyyy] [name of copyright owner]
18*99ebb4caSwyllys  *
19*99ebb4caSwyllys  * CDDL HEADER END
20*99ebb4caSwyllys  *
21*99ebb4caSwyllys  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
22*99ebb4caSwyllys  * Use is subject to license terms.
23*99ebb4caSwyllys  */
24*99ebb4caSwyllys 
25*99ebb4caSwyllys #pragma ident	"%Z%%M%	%I%	%E% SMI"
26*99ebb4caSwyllys 
27*99ebb4caSwyllys #include <stdio.h>
28*99ebb4caSwyllys #include <strings.h>
29*99ebb4caSwyllys #include <ctype.h>
30*99ebb4caSwyllys #include <libgen.h>
31*99ebb4caSwyllys #include <libintl.h>
32*99ebb4caSwyllys #include <errno.h>
33*99ebb4caSwyllys #include <kmfapiP.h>
34*99ebb4caSwyllys #include "util.h"
35*99ebb4caSwyllys 
36*99ebb4caSwyllys int
37*99ebb4caSwyllys kc_delete(int argc, char *argv[])
38*99ebb4caSwyllys {
39*99ebb4caSwyllys 	int		rv = KC_OK;
40*99ebb4caSwyllys 	KMF_RETURN	kmfrv = KMF_OK;
41*99ebb4caSwyllys 	int		opt;
42*99ebb4caSwyllys 	extern int	optind_av;
43*99ebb4caSwyllys 	extern char	*optarg_av;
44*99ebb4caSwyllys 	char		*filename = NULL;
45*99ebb4caSwyllys 	char		*policyname = NULL;
46*99ebb4caSwyllys 
47*99ebb4caSwyllys 	while ((opt = getopt_av(argc, argv, "i:(dbfile)p:(policy)")) != EOF) {
48*99ebb4caSwyllys 		switch (opt) {
49*99ebb4caSwyllys 			case 'i':
50*99ebb4caSwyllys 				filename = get_string(optarg_av, &rv);
51*99ebb4caSwyllys 				if (filename == NULL) {
52*99ebb4caSwyllys 					(void) fprintf(stderr,
53*99ebb4caSwyllys 					    gettext("Error dbfile input.\n"));
54*99ebb4caSwyllys 				}
55*99ebb4caSwyllys 				break;
56*99ebb4caSwyllys 			case 'p':
57*99ebb4caSwyllys 				policyname = get_string(optarg_av, &rv);
58*99ebb4caSwyllys 				if (policyname == NULL) {
59*99ebb4caSwyllys 					(void) fprintf(stderr,
60*99ebb4caSwyllys 					    gettext("Error policy name.\n"));
61*99ebb4caSwyllys 				}
62*99ebb4caSwyllys 				break;
63*99ebb4caSwyllys 			default:
64*99ebb4caSwyllys 				(void) fprintf(stderr,
65*99ebb4caSwyllys 				    gettext("Error input option.\n"));
66*99ebb4caSwyllys 				rv = KC_ERR_USAGE;
67*99ebb4caSwyllys 				break;
68*99ebb4caSwyllys 
69*99ebb4caSwyllys 		}
70*99ebb4caSwyllys 
71*99ebb4caSwyllys 		if (rv != KC_OK)
72*99ebb4caSwyllys 			goto out;
73*99ebb4caSwyllys 	}
74*99ebb4caSwyllys 
75*99ebb4caSwyllys 	/* No additional args allowed. */
76*99ebb4caSwyllys 	argc -= optind_av;
77*99ebb4caSwyllys 	if (argc) {
78*99ebb4caSwyllys 		(void) fprintf(stderr,
79*99ebb4caSwyllys 		    gettext("Error input option\n"));
80*99ebb4caSwyllys 		rv = KC_ERR_USAGE;
81*99ebb4caSwyllys 		goto out;
82*99ebb4caSwyllys 	}
83*99ebb4caSwyllys 
84*99ebb4caSwyllys 	if (filename == NULL) {
85*99ebb4caSwyllys 		filename = strdup(KMF_DEFAULT_POLICY_FILE);
86*99ebb4caSwyllys 		if (filename == NULL) {
87*99ebb4caSwyllys 			rv = KC_ERR_MEMORY;
88*99ebb4caSwyllys 			goto out;
89*99ebb4caSwyllys 		}
90*99ebb4caSwyllys 	}
91*99ebb4caSwyllys 
92*99ebb4caSwyllys 	/*
93*99ebb4caSwyllys 	 * Must have a policy name. The policy name can not be default
94*99ebb4caSwyllys 	 * if using the default policy file.
95*99ebb4caSwyllys 	 */
96*99ebb4caSwyllys 	if (policyname == NULL) {
97*99ebb4caSwyllys 		(void) fprintf(stderr,
98*99ebb4caSwyllys 		    gettext("You must specify a policy name\n"));
99*99ebb4caSwyllys 		rv = KC_ERR_USAGE;
100*99ebb4caSwyllys 		goto out;
101*99ebb4caSwyllys 	} else if (strcmp(filename, KMF_DEFAULT_POLICY_FILE) == 0 &&
102*99ebb4caSwyllys 	    strcmp(policyname, KMF_DEFAULT_POLICY_NAME) == 0) {
103*99ebb4caSwyllys 		(void) fprintf(stderr,
104*99ebb4caSwyllys 		    gettext("Can not delete the default policy in the default "
105*99ebb4caSwyllys 		    "policy file\n"));
106*99ebb4caSwyllys 		rv = KC_ERR_USAGE;
107*99ebb4caSwyllys 		goto out;
108*99ebb4caSwyllys 	}
109*99ebb4caSwyllys 
110*99ebb4caSwyllys 	/* Check the access permission of the policy DB */
111*99ebb4caSwyllys 	if (access(filename, W_OK) < 0) {
112*99ebb4caSwyllys 		int err = errno;
113*99ebb4caSwyllys 		(void) fprintf(stderr,
114*99ebb4caSwyllys 		    gettext("Cannot access \"%s\" for delete - %s\n"),
115*99ebb4caSwyllys 		    filename, strerror(err));
116*99ebb4caSwyllys 		rv = KC_ERR_ACCESS;
117*99ebb4caSwyllys 		goto out;
118*99ebb4caSwyllys 	}
119*99ebb4caSwyllys 
120*99ebb4caSwyllys 	kmfrv = KMF_DeletePolicyFromDB(policyname, filename);
121*99ebb4caSwyllys 	if (kmfrv != KMF_OK)
122*99ebb4caSwyllys 		rv = KC_ERR_DELETE_POLICY;
123*99ebb4caSwyllys 
124*99ebb4caSwyllys out:
125*99ebb4caSwyllys 	if (filename != NULL)
126*99ebb4caSwyllys 		free(filename);
127*99ebb4caSwyllys 
128*99ebb4caSwyllys 	if (policyname != NULL)
129*99ebb4caSwyllys 		free(policyname);
130*99ebb4caSwyllys 
131*99ebb4caSwyllys 	return (rv);
132*99ebb4caSwyllys }
133