199ebb4caSwyllys /*
299ebb4caSwyllys  * CDDL HEADER START
399ebb4caSwyllys  *
499ebb4caSwyllys  * The contents of this file are subject to the terms of the
599ebb4caSwyllys  * Common Development and Distribution License (the "License").
699ebb4caSwyllys  * You may not use this file except in compliance with the License.
799ebb4caSwyllys  *
899ebb4caSwyllys  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
999ebb4caSwyllys  * or http://www.opensolaris.org/os/licensing.
1099ebb4caSwyllys  * See the License for the specific language governing permissions
1199ebb4caSwyllys  * and limitations under the License.
1299ebb4caSwyllys  *
1399ebb4caSwyllys  * When distributing Covered Code, include this CDDL HEADER in each
1499ebb4caSwyllys  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
1599ebb4caSwyllys  * If applicable, add the following below this CDDL HEADER, with the
1699ebb4caSwyllys  * fields enclosed by brackets "[]" replaced with your own identifying
1799ebb4caSwyllys  * information: Portions Copyright [yyyy] [name of copyright owner]
1899ebb4caSwyllys  *
1999ebb4caSwyllys  * CDDL HEADER END
2099ebb4caSwyllys  *
21*30a5e8faSwyllys  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
2299ebb4caSwyllys  * Use is subject to license terms.
2399ebb4caSwyllys  */
2499ebb4caSwyllys 
2599ebb4caSwyllys #include <stdio.h>
2699ebb4caSwyllys #include <strings.h>
2799ebb4caSwyllys #include <ctype.h>
2899ebb4caSwyllys #include <libgen.h>
2999ebb4caSwyllys #include <libintl.h>
3099ebb4caSwyllys #include <errno.h>
3199ebb4caSwyllys #include <kmfapiP.h>
3299ebb4caSwyllys #include "util.h"
3399ebb4caSwyllys 
3499ebb4caSwyllys int
kc_delete(int argc,char * argv[])3599ebb4caSwyllys kc_delete(int argc, char *argv[])
3699ebb4caSwyllys {
3799ebb4caSwyllys 	int		rv = KC_OK;
3899ebb4caSwyllys 	KMF_RETURN	kmfrv = KMF_OK;
3999ebb4caSwyllys 	int		opt;
4099ebb4caSwyllys 	extern int	optind_av;
4199ebb4caSwyllys 	extern char	*optarg_av;
4299ebb4caSwyllys 	char		*filename = NULL;
4399ebb4caSwyllys 	char		*policyname = NULL;
4499ebb4caSwyllys 
4599ebb4caSwyllys 	while ((opt = getopt_av(argc, argv, "i:(dbfile)p:(policy)")) != EOF) {
4699ebb4caSwyllys 		switch (opt) {
4799ebb4caSwyllys 			case 'i':
4899ebb4caSwyllys 				filename = get_string(optarg_av, &rv);
4999ebb4caSwyllys 				if (filename == NULL) {
5099ebb4caSwyllys 					(void) fprintf(stderr,
5199ebb4caSwyllys 					    gettext("Error dbfile input.\n"));
5299ebb4caSwyllys 				}
5399ebb4caSwyllys 				break;
5499ebb4caSwyllys 			case 'p':
5599ebb4caSwyllys 				policyname = get_string(optarg_av, &rv);
5699ebb4caSwyllys 				if (policyname == NULL) {
5799ebb4caSwyllys 					(void) fprintf(stderr,
5899ebb4caSwyllys 					    gettext("Error policy name.\n"));
5999ebb4caSwyllys 				}
6099ebb4caSwyllys 				break;
6199ebb4caSwyllys 			default:
6299ebb4caSwyllys 				(void) fprintf(stderr,
6399ebb4caSwyllys 				    gettext("Error input option.\n"));
6499ebb4caSwyllys 				rv = KC_ERR_USAGE;
6599ebb4caSwyllys 				break;
6699ebb4caSwyllys 
6799ebb4caSwyllys 		}
6899ebb4caSwyllys 
6999ebb4caSwyllys 		if (rv != KC_OK)
7099ebb4caSwyllys 			goto out;
7199ebb4caSwyllys 	}
7299ebb4caSwyllys 
7399ebb4caSwyllys 	/* No additional args allowed. */
7499ebb4caSwyllys 	argc -= optind_av;
7599ebb4caSwyllys 	if (argc) {
7699ebb4caSwyllys 		(void) fprintf(stderr,
7799ebb4caSwyllys 		    gettext("Error input option\n"));
7899ebb4caSwyllys 		rv = KC_ERR_USAGE;
7999ebb4caSwyllys 		goto out;
8099ebb4caSwyllys 	}
8199ebb4caSwyllys 
8299ebb4caSwyllys 	if (filename == NULL) {
8399ebb4caSwyllys 		filename = strdup(KMF_DEFAULT_POLICY_FILE);
8499ebb4caSwyllys 		if (filename == NULL) {
8599ebb4caSwyllys 			rv = KC_ERR_MEMORY;
8699ebb4caSwyllys 			goto out;
8799ebb4caSwyllys 		}
8899ebb4caSwyllys 	}
8999ebb4caSwyllys 
9099ebb4caSwyllys 	/*
9199ebb4caSwyllys 	 * Must have a policy name. The policy name can not be default
9299ebb4caSwyllys 	 * if using the default policy file.
9399ebb4caSwyllys 	 */
9499ebb4caSwyllys 	if (policyname == NULL) {
9599ebb4caSwyllys 		(void) fprintf(stderr,
9699ebb4caSwyllys 		    gettext("You must specify a policy name\n"));
9799ebb4caSwyllys 		rv = KC_ERR_USAGE;
9899ebb4caSwyllys 		goto out;
9999ebb4caSwyllys 	} else if (strcmp(filename, KMF_DEFAULT_POLICY_FILE) == 0 &&
10099ebb4caSwyllys 	    strcmp(policyname, KMF_DEFAULT_POLICY_NAME) == 0) {
10199ebb4caSwyllys 		(void) fprintf(stderr,
10299ebb4caSwyllys 		    gettext("Can not delete the default policy in the default "
10399ebb4caSwyllys 		    "policy file\n"));
10499ebb4caSwyllys 		rv = KC_ERR_USAGE;
10599ebb4caSwyllys 		goto out;
10699ebb4caSwyllys 	}
10799ebb4caSwyllys 
10899ebb4caSwyllys 	/* Check the access permission of the policy DB */
10999ebb4caSwyllys 	if (access(filename, W_OK) < 0) {
11099ebb4caSwyllys 		int err = errno;
11199ebb4caSwyllys 		(void) fprintf(stderr,
11299ebb4caSwyllys 		    gettext("Cannot access \"%s\" for delete - %s\n"),
11399ebb4caSwyllys 		    filename, strerror(err));
11499ebb4caSwyllys 		rv = KC_ERR_ACCESS;
11599ebb4caSwyllys 		goto out;
11699ebb4caSwyllys 	}
11799ebb4caSwyllys 
118*30a5e8faSwyllys 	kmfrv = kmf_delete_policy_from_db(policyname, filename);
11999ebb4caSwyllys 	if (kmfrv != KMF_OK)
12099ebb4caSwyllys 		rv = KC_ERR_DELETE_POLICY;
12199ebb4caSwyllys 
12299ebb4caSwyllys out:
12399ebb4caSwyllys 	if (filename != NULL)
12499ebb4caSwyllys 		free(filename);
12599ebb4caSwyllys 
12699ebb4caSwyllys 	if (policyname != NULL)
12799ebb4caSwyllys 		free(policyname);
12899ebb4caSwyllys 
12999ebb4caSwyllys 	return (rv);
13099ebb4caSwyllys }
131