xref: /illumos-gate/usr/src/cmd/cmd-crypto/kmfcfg/delete.c (revision 20a7641f9918de8574b8b3b47dbe35c4bfc78df1)
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 #include <stdio.h>
26 #include <strings.h>
27 #include <ctype.h>
28 #include <libgen.h>
29 #include <libintl.h>
30 #include <errno.h>
31 #include <kmfapiP.h>
32 #include "util.h"
33 
34 int
35 kc_delete(int argc, char *argv[])
36 {
37 	int		rv = KC_OK;
38 	KMF_RETURN	kmfrv = KMF_OK;
39 	int		opt;
40 	extern int	optind_av;
41 	extern char	*optarg_av;
42 	char		*filename = NULL;
43 	char		*policyname = NULL;
44 
45 	while ((opt = getopt_av(argc, argv, "i:(dbfile)p:(policy)")) != EOF) {
46 		switch (opt) {
47 			case 'i':
48 				filename = get_string(optarg_av, &rv);
49 				if (filename == NULL) {
50 					(void) fprintf(stderr,
51 					    gettext("Error dbfile input.\n"));
52 				}
53 				break;
54 			case 'p':
55 				policyname = get_string(optarg_av, &rv);
56 				if (policyname == NULL) {
57 					(void) fprintf(stderr,
58 					    gettext("Error policy name.\n"));
59 				}
60 				break;
61 			default:
62 				(void) fprintf(stderr,
63 				    gettext("Error input option.\n"));
64 				rv = KC_ERR_USAGE;
65 				break;
66 
67 		}
68 
69 		if (rv != KC_OK)
70 			goto out;
71 	}
72 
73 	/* No additional args allowed. */
74 	argc -= optind_av;
75 	if (argc) {
76 		(void) fprintf(stderr,
77 		    gettext("Error input option\n"));
78 		rv = KC_ERR_USAGE;
79 		goto out;
80 	}
81 
82 	if (filename == NULL) {
83 		filename = strdup(KMF_DEFAULT_POLICY_FILE);
84 		if (filename == NULL) {
85 			rv = KC_ERR_MEMORY;
86 			goto out;
87 		}
88 	}
89 
90 	/*
91 	 * Must have a policy name. The policy name can not be default
92 	 * if using the default policy file.
93 	 */
94 	if (policyname == NULL) {
95 		(void) fprintf(stderr,
96 		    gettext("You must specify a policy name\n"));
97 		rv = KC_ERR_USAGE;
98 		goto out;
99 	} else if (strcmp(filename, KMF_DEFAULT_POLICY_FILE) == 0 &&
100 	    strcmp(policyname, KMF_DEFAULT_POLICY_NAME) == 0) {
101 		(void) fprintf(stderr,
102 		    gettext("Can not delete the default policy in the default "
103 		    "policy file\n"));
104 		rv = KC_ERR_USAGE;
105 		goto out;
106 	}
107 
108 	/* Check the access permission of the policy DB */
109 	if (access(filename, W_OK) < 0) {
110 		int err = errno;
111 		(void) fprintf(stderr,
112 		    gettext("Cannot access \"%s\" for delete - %s\n"),
113 		    filename, strerror(err));
114 		rv = KC_ERR_ACCESS;
115 		goto out;
116 	}
117 
118 	kmfrv = kmf_delete_policy_from_db(policyname, filename);
119 	if (kmfrv != KMF_OK)
120 		rv = KC_ERR_DELETE_POLICY;
121 
122 out:
123 	if (filename != NULL)
124 		free(filename);
125 
126 	if (policyname != NULL)
127 		free(policyname);
128 
129 	return (rv);
130 }
131