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