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