xref: /illumos-gate/usr/src/cmd/cmd-crypto/kmfcfg/delete.c (revision 30a5e8fa1253cb33980ee4514743cf683f584b4e)
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 #pragma ident	"%Z%%M%	%I%	%E% SMI"
2699ebb4caSwyllys 
2799ebb4caSwyllys #include <stdio.h>
2899ebb4caSwyllys #include <strings.h>
2999ebb4caSwyllys #include <ctype.h>
3099ebb4caSwyllys #include <libgen.h>
3199ebb4caSwyllys #include <libintl.h>
3299ebb4caSwyllys #include <errno.h>
3399ebb4caSwyllys #include <kmfapiP.h>
3499ebb4caSwyllys #include "util.h"
3599ebb4caSwyllys 
3699ebb4caSwyllys int
3799ebb4caSwyllys kc_delete(int argc, char *argv[])
3899ebb4caSwyllys {
3999ebb4caSwyllys 	int		rv = KC_OK;
4099ebb4caSwyllys 	KMF_RETURN	kmfrv = KMF_OK;
4199ebb4caSwyllys 	int		opt;
4299ebb4caSwyllys 	extern int	optind_av;
4399ebb4caSwyllys 	extern char	*optarg_av;
4499ebb4caSwyllys 	char		*filename = NULL;
4599ebb4caSwyllys 	char		*policyname = NULL;
4699ebb4caSwyllys 
4799ebb4caSwyllys 	while ((opt = getopt_av(argc, argv, "i:(dbfile)p:(policy)")) != EOF) {
4899ebb4caSwyllys 		switch (opt) {
4999ebb4caSwyllys 			case 'i':
5099ebb4caSwyllys 				filename = get_string(optarg_av, &rv);
5199ebb4caSwyllys 				if (filename == NULL) {
5299ebb4caSwyllys 					(void) fprintf(stderr,
5399ebb4caSwyllys 					    gettext("Error dbfile input.\n"));
5499ebb4caSwyllys 				}
5599ebb4caSwyllys 				break;
5699ebb4caSwyllys 			case 'p':
5799ebb4caSwyllys 				policyname = get_string(optarg_av, &rv);
5899ebb4caSwyllys 				if (policyname == NULL) {
5999ebb4caSwyllys 					(void) fprintf(stderr,
6099ebb4caSwyllys 					    gettext("Error policy name.\n"));
6199ebb4caSwyllys 				}
6299ebb4caSwyllys 				break;
6399ebb4caSwyllys 			default:
6499ebb4caSwyllys 				(void) fprintf(stderr,
6599ebb4caSwyllys 				    gettext("Error input option.\n"));
6699ebb4caSwyllys 				rv = KC_ERR_USAGE;
6799ebb4caSwyllys 				break;
6899ebb4caSwyllys 
6999ebb4caSwyllys 		}
7099ebb4caSwyllys 
7199ebb4caSwyllys 		if (rv != KC_OK)
7299ebb4caSwyllys 			goto out;
7399ebb4caSwyllys 	}
7499ebb4caSwyllys 
7599ebb4caSwyllys 	/* No additional args allowed. */
7699ebb4caSwyllys 	argc -= optind_av;
7799ebb4caSwyllys 	if (argc) {
7899ebb4caSwyllys 		(void) fprintf(stderr,
7999ebb4caSwyllys 		    gettext("Error input option\n"));
8099ebb4caSwyllys 		rv = KC_ERR_USAGE;
8199ebb4caSwyllys 		goto out;
8299ebb4caSwyllys 	}
8399ebb4caSwyllys 
8499ebb4caSwyllys 	if (filename == NULL) {
8599ebb4caSwyllys 		filename = strdup(KMF_DEFAULT_POLICY_FILE);
8699ebb4caSwyllys 		if (filename == NULL) {
8799ebb4caSwyllys 			rv = KC_ERR_MEMORY;
8899ebb4caSwyllys 			goto out;
8999ebb4caSwyllys 		}
9099ebb4caSwyllys 	}
9199ebb4caSwyllys 
9299ebb4caSwyllys 	/*
9399ebb4caSwyllys 	 * Must have a policy name. The policy name can not be default
9499ebb4caSwyllys 	 * if using the default policy file.
9599ebb4caSwyllys 	 */
9699ebb4caSwyllys 	if (policyname == NULL) {
9799ebb4caSwyllys 		(void) fprintf(stderr,
9899ebb4caSwyllys 		    gettext("You must specify a policy name\n"));
9999ebb4caSwyllys 		rv = KC_ERR_USAGE;
10099ebb4caSwyllys 		goto out;
10199ebb4caSwyllys 	} else if (strcmp(filename, KMF_DEFAULT_POLICY_FILE) == 0 &&
10299ebb4caSwyllys 	    strcmp(policyname, KMF_DEFAULT_POLICY_NAME) == 0) {
10399ebb4caSwyllys 		(void) fprintf(stderr,
10499ebb4caSwyllys 		    gettext("Can not delete the default policy in the default "
10599ebb4caSwyllys 		    "policy file\n"));
10699ebb4caSwyllys 		rv = KC_ERR_USAGE;
10799ebb4caSwyllys 		goto out;
10899ebb4caSwyllys 	}
10999ebb4caSwyllys 
11099ebb4caSwyllys 	/* Check the access permission of the policy DB */
11199ebb4caSwyllys 	if (access(filename, W_OK) < 0) {
11299ebb4caSwyllys 		int err = errno;
11399ebb4caSwyllys 		(void) fprintf(stderr,
11499ebb4caSwyllys 		    gettext("Cannot access \"%s\" for delete - %s\n"),
11599ebb4caSwyllys 		    filename, strerror(err));
11699ebb4caSwyllys 		rv = KC_ERR_ACCESS;
11799ebb4caSwyllys 		goto out;
11899ebb4caSwyllys 	}
11999ebb4caSwyllys 
120*30a5e8faSwyllys 	kmfrv = kmf_delete_policy_from_db(policyname, filename);
12199ebb4caSwyllys 	if (kmfrv != KMF_OK)
12299ebb4caSwyllys 		rv = KC_ERR_DELETE_POLICY;
12399ebb4caSwyllys 
12499ebb4caSwyllys out:
12599ebb4caSwyllys 	if (filename != NULL)
12699ebb4caSwyllys 		free(filename);
12799ebb4caSwyllys 
12899ebb4caSwyllys 	if (policyname != NULL)
12999ebb4caSwyllys 		free(policyname);
13099ebb4caSwyllys 
13199ebb4caSwyllys 	return (rv);
13299ebb4caSwyllys }
133