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