xref: /onnv-gate/usr/src/cmd/cmd-crypto/kmfcfg/delete.c (revision 5051:cbbb7c8b40a9)
13089Swyllys /*
23089Swyllys  * CDDL HEADER START
33089Swyllys  *
43089Swyllys  * The contents of this file are subject to the terms of the
53089Swyllys  * Common Development and Distribution License (the "License").
63089Swyllys  * You may not use this file except in compliance with the License.
73089Swyllys  *
83089Swyllys  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
93089Swyllys  * or http://www.opensolaris.org/os/licensing.
103089Swyllys  * See the License for the specific language governing permissions
113089Swyllys  * and limitations under the License.
123089Swyllys  *
133089Swyllys  * When distributing Covered Code, include this CDDL HEADER in each
143089Swyllys  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
153089Swyllys  * If applicable, add the following below this CDDL HEADER, with the
163089Swyllys  * fields enclosed by brackets "[]" replaced with your own identifying
173089Swyllys  * information: Portions Copyright [yyyy] [name of copyright owner]
183089Swyllys  *
193089Swyllys  * CDDL HEADER END
203089Swyllys  *
21*5051Swyllys  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
223089Swyllys  * Use is subject to license terms.
233089Swyllys  */
243089Swyllys 
253089Swyllys #pragma ident	"%Z%%M%	%I%	%E% SMI"
263089Swyllys 
273089Swyllys #include <stdio.h>
283089Swyllys #include <strings.h>
293089Swyllys #include <ctype.h>
303089Swyllys #include <libgen.h>
313089Swyllys #include <libintl.h>
323089Swyllys #include <errno.h>
333089Swyllys #include <kmfapiP.h>
343089Swyllys #include "util.h"
353089Swyllys 
363089Swyllys int
kc_delete(int argc,char * argv[])373089Swyllys kc_delete(int argc, char *argv[])
383089Swyllys {
393089Swyllys 	int		rv = KC_OK;
403089Swyllys 	KMF_RETURN	kmfrv = KMF_OK;
413089Swyllys 	int		opt;
423089Swyllys 	extern int	optind_av;
433089Swyllys 	extern char	*optarg_av;
443089Swyllys 	char		*filename = NULL;
453089Swyllys 	char		*policyname = NULL;
463089Swyllys 
473089Swyllys 	while ((opt = getopt_av(argc, argv, "i:(dbfile)p:(policy)")) != EOF) {
483089Swyllys 		switch (opt) {
493089Swyllys 			case 'i':
503089Swyllys 				filename = get_string(optarg_av, &rv);
513089Swyllys 				if (filename == NULL) {
523089Swyllys 					(void) fprintf(stderr,
533089Swyllys 					    gettext("Error dbfile input.\n"));
543089Swyllys 				}
553089Swyllys 				break;
563089Swyllys 			case 'p':
573089Swyllys 				policyname = get_string(optarg_av, &rv);
583089Swyllys 				if (policyname == NULL) {
593089Swyllys 					(void) fprintf(stderr,
603089Swyllys 					    gettext("Error policy name.\n"));
613089Swyllys 				}
623089Swyllys 				break;
633089Swyllys 			default:
643089Swyllys 				(void) fprintf(stderr,
653089Swyllys 				    gettext("Error input option.\n"));
663089Swyllys 				rv = KC_ERR_USAGE;
673089Swyllys 				break;
683089Swyllys 
693089Swyllys 		}
703089Swyllys 
713089Swyllys 		if (rv != KC_OK)
723089Swyllys 			goto out;
733089Swyllys 	}
743089Swyllys 
753089Swyllys 	/* No additional args allowed. */
763089Swyllys 	argc -= optind_av;
773089Swyllys 	if (argc) {
783089Swyllys 		(void) fprintf(stderr,
793089Swyllys 		    gettext("Error input option\n"));
803089Swyllys 		rv = KC_ERR_USAGE;
813089Swyllys 		goto out;
823089Swyllys 	}
833089Swyllys 
843089Swyllys 	if (filename == NULL) {
853089Swyllys 		filename = strdup(KMF_DEFAULT_POLICY_FILE);
863089Swyllys 		if (filename == NULL) {
873089Swyllys 			rv = KC_ERR_MEMORY;
883089Swyllys 			goto out;
893089Swyllys 		}
903089Swyllys 	}
913089Swyllys 
923089Swyllys 	/*
933089Swyllys 	 * Must have a policy name. The policy name can not be default
943089Swyllys 	 * if using the default policy file.
953089Swyllys 	 */
963089Swyllys 	if (policyname == NULL) {
973089Swyllys 		(void) fprintf(stderr,
983089Swyllys 		    gettext("You must specify a policy name\n"));
993089Swyllys 		rv = KC_ERR_USAGE;
1003089Swyllys 		goto out;
1013089Swyllys 	} else if (strcmp(filename, KMF_DEFAULT_POLICY_FILE) == 0 &&
1023089Swyllys 	    strcmp(policyname, KMF_DEFAULT_POLICY_NAME) == 0) {
1033089Swyllys 		(void) fprintf(stderr,
1043089Swyllys 		    gettext("Can not delete the default policy in the default "
1053089Swyllys 		    "policy file\n"));
1063089Swyllys 		rv = KC_ERR_USAGE;
1073089Swyllys 		goto out;
1083089Swyllys 	}
1093089Swyllys 
1103089Swyllys 	/* Check the access permission of the policy DB */
1113089Swyllys 	if (access(filename, W_OK) < 0) {
1123089Swyllys 		int err = errno;
1133089Swyllys 		(void) fprintf(stderr,
1143089Swyllys 		    gettext("Cannot access \"%s\" for delete - %s\n"),
1153089Swyllys 		    filename, strerror(err));
1163089Swyllys 		rv = KC_ERR_ACCESS;
1173089Swyllys 		goto out;
1183089Swyllys 	}
1193089Swyllys 
120*5051Swyllys 	kmfrv = kmf_delete_policy_from_db(policyname, filename);
1213089Swyllys 	if (kmfrv != KMF_OK)
1223089Swyllys 		rv = KC_ERR_DELETE_POLICY;
1233089Swyllys 
1243089Swyllys out:
1253089Swyllys 	if (filename != NULL)
1263089Swyllys 		free(filename);
1273089Swyllys 
1283089Swyllys 	if (policyname != NULL)
1293089Swyllys 		free(policyname);
1303089Swyllys 
1313089Swyllys 	return (rv);
1323089Swyllys }
133