xref: /titanic_50/usr/src/cmd/cmd-crypto/kmfcfg/kmfcfg.c (revision 0a0e9771ca0211c15f3ac4466b661c145feeb9e4)
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 /*
22  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #pragma ident	"%Z%%M%	%I%	%E% SMI"
27 
28 #include <stdio.h>
29 #include <strings.h>
30 #include <ctype.h>
31 #include <libgen.h>
32 #include <libintl.h>
33 #include <locale.h>
34 
35 #include <kmfapiP.h>
36 
37 #include "util.h"
38 
39 /*
40  * The verbcmd construct allows genericizing information about a verb so
41  * that it is easier to manipulate.  Makes parsing code easier to read,
42  * fix, and extend with new verbs.
43  */
44 typedef struct verbcmd_s {
45 	char    	*verb;
46 	int		(*action)(int, char *[]);
47 	char    	*synopsis;
48 } verbcmd;
49 
50 int	kc_list(int argc, char *argv[]);
51 int	kc_delete(int argc, char *argv[]);
52 int	kc_create(int argc, char *argv[]);
53 int	kc_modify(int argc, char *argv[]);
54 int	kc_export(int argc, char *argv[]);
55 int	kc_import(int argc, char *argv[]);
56 int	kc_install(int argc, char *argv[]);
57 int	kc_uninstall(int argc, char *argv[]);
58 
59 static int	kc_help();
60 
61 static verbcmd cmds[] = {
62 	{ "list",	kc_list,
63 		"list [dbfile=dbfile] [policy=policyname]\n"
64 		"\tlist plugin" },
65 	{ "delete",	kc_delete, "delete [dbfile=dbfile] "
66 		"policy=policyname" },
67 	{ "create",	kc_create,
68 		"create [dbfile=dbfile] policy=policyname\n"
69 		"\t\t[ignore-date=true|false]\n"
70 		"\t\t[ignore-unknown-eku=true|false]\n"
71 		"\t\t[ignore-trust-anchor=true|false]\n"
72 		"\t\t[validity-adjusttime=adjusttime]\n"
73 		"\t\t[ta-name=trust anchor subject DN]\n"
74 		"\t\t[ta-serial=trust anchor serial number]\n"
75 		"\t\t[ocsp-responder=URL]\n"
76 		"\t\t[ocsp-proxy=URL]\n"
77 		"\t\t[ocsp-use-cert-responder=true|false]\n"
78 		"\t\t[ocsp-response-lifetime=timelimit]\n"
79 		"\t\t[ocsp-ignore-response-sign=true|false]\n"
80 		"\t\t[ocsp-responder-cert-name=Issuer DN]\n"
81 		"\t\t[ocsp-responder-cert-serial=serial number]\n"
82 		"\t\t[crl-basefilename=basefilename]\n"
83 		"\t\t[crl-directory=directory]\n"
84 		"\t\t[crl-get-crl-uri=true|false]\n"
85 		"\t\t[crl-proxy=URL]\n"
86 		"\t\t[crl-ignore-crl-sign=true|false]\n"
87 		"\t\t[crl-ignore-crl-date=true|false]\n"
88 		"\t\t[keyusage=digitalSignature|nonRepudiation\n\t"
89 		"\t\t|keyEncipherment | dataEncipherment |\n\t"
90 		"\t\tkeyAgreement |keyCertSign |\n\t"
91 		"\t\tcRLSign | encipherOnly | decipherOnly],[...]\n"
92 		"\t\t[ekunames=serverAuth | clientAuth |\n\t"
93 		"\t\tcodeSigning | emailProtection |\n\t"
94 		"\t\tipsecEndSystem | ipsecTunnel |\n\t"
95 		"\t\tipsecUser | timeStamping |\n\t"
96 		"\t\tOCSPSigning],[...]\n"
97 		"\t\t[ekuoids=OID,OID,OID...]\n" },
98 	{ "modify",	kc_modify,
99 		"modify [dbfile=dbfile] policy=policyname\n"
100 		"\t\t[ignore-date=true|false]\n"
101 		"\t\t[ignore-unknown-eku=true|false]\n"
102 		"\t\t[ignore-trust-anchor=true|false]\n"
103 		"\t\t[validity-adjusttime=adjusttime]\n"
104 		"\t\t[ta-name=trust anchor subject DN]\n"
105 		"\t\t[ta-serial=trust anchor serial number]\n"
106 		"\t\t[ocsp-responder=URL]\n"
107 		"\t\t[ocsp-proxy=URL]\n"
108 		"\t\t[ocsp-use-cert-responder=true|false]\n"
109 		"\t\t[ocsp-response-lifetime=timelimit]\n"
110 		"\t\t[ocsp-ignore-response-sign=true|false]\n"
111 		"\t\t[ocsp-responder-cert-name=Issuer DN]\n"
112 		"\t\t[ocsp-responder-cert-serial=serial number]\n"
113 		"\t\t[ocsp-none=true|false]\n"
114 		"\t\t[crl-basefilename=basefilename]\n"
115 		"\t\t[crl-directory=directory]\n"
116 		"\t\t[crl-get-crl-uri=true|false]\n"
117 		"\t\t[crl-proxy=URL]\n"
118 		"\t\t[crl-ignore-crl-sign=true|false]\n"
119 		"\t\t[crl-ignore-crl-date=true|false]\n"
120 		"\t\t[crl-none=true|false]\n"
121 		"\t\t[keyusage=digitalSignature|nonRepudiation\n\t"
122 		"\t\t|keyEncipherment | dataEncipherment |\n\t"
123 		"\t\tkeyAgreement |keyCertSign |\n\t"
124 		"\t\tcRLSign | encipherOnly | decipherOnly],[...]\n"
125 		"\t\t[keyusage-none=true|false]\n"
126 		"\t\t[ekunames=serverAuth | clientAuth |\n\t"
127 		"\t\tcodeSigning | emailProtection |\n\t"
128 		"\t\tipsecEndSystem | ipsecTunnel |\n\t"
129 		"\t\tipsecUser | timeStamping |\n\t"
130 		"\t\tOCSPSigning],[...]\n"
131 		"\t\t[ekuoids=OID,OID,OID...]\n"
132 		"\t\t[eku-none=true|false]\n\n"
133 		"\tmodify plugin keystore=keystorename option=optionstring\n"},
134 
135 	{ "import",	kc_import, "import [dbfile=dbfile] policy=policyname "
136 		"infile=inputdbfile\n" },
137 	{ "export",	kc_export, "export [dbfile=dbfile] policy=policyname "
138 		"outfile=newdbfile\n" },
139 	{ "install", 	kc_install, "install keystore=keystorename "
140 		"modulepath=path [option=optionstring]\n"},
141 	{ "uninstall", 	kc_uninstall, "uninstall keystore=keystorename\n"},
142 	{ "-?",		kc_help, 	"help"},
143 	{ "help",	kc_help, 	""}
144 };
145 
146 static int num_cmds = sizeof (cmds) / sizeof (verbcmd);
147 static char *prog;
148 
149 static void
150 usage(void)
151 {
152 	int i;
153 
154 	/* Display this block only in command-line mode. */
155 	(void) fprintf(stdout, gettext("Usage:\n"));
156 	(void) fprintf(stdout, gettext("\t%s -?\t(help and usage)\n"), prog);
157 	(void) fprintf(stdout, gettext("\t%s subcommand [options...]\n"), prog);
158 	(void) fprintf(stdout, gettext("where subcommands may be:\n"));
159 
160 	/* Display only those verbs that match the current tool mode. */
161 	for (i = 0; i < num_cmds; i++) {
162 		/* Do NOT i18n/l10n. */
163 		(void) fprintf(stdout, "\t%s\n", cmds[i].synopsis);
164 	}
165 }
166 
167 static int
168 kc_help()
169 {
170 	usage();
171 	return (0);
172 }
173 
174 int
175 main(int argc, char *argv[])
176 {
177 	KMF_RETURN ret;
178 	int found;
179 	int i;
180 
181 	(void) setlocale(LC_ALL, "");
182 #if !defined(TEXT_DOMAIN)		/* Should be defined by cc -D. */
183 #define	TEXT_DOMAIN	"SYS_TEST"	/* Use this only if it isn't. */
184 #endif
185 	(void) textdomain(TEXT_DOMAIN);
186 
187 	prog = basename(argv[0]);
188 	argv++; argc--;
189 
190 	if (argc == 0) {
191 		usage();
192 		exit(1);
193 	}
194 
195 	if (argc == 1 && argv[0][0] == '-') {
196 		switch (argv[0][1]) {
197 			case '?':
198 				return (kc_help());
199 			default:
200 				usage();
201 				exit(1);
202 		}
203 	}
204 
205 	found = -1;
206 	for (i = 0; i < num_cmds; i++) {
207 		if (strcmp(cmds[i].verb, argv[0]) == 0) {
208 			found = i;
209 			break;
210 		}
211 	}
212 
213 	if (found < 0) {
214 		(void) fprintf(stderr, gettext("Invalid command: %s\n"),
215 		    argv[0]);
216 		exit(1);
217 	}
218 
219 	ret = (*cmds[found].action)(argc, argv);
220 
221 	switch (ret) {
222 		case KC_OK:
223 			break;
224 		case KC_ERR_USAGE:
225 			break;
226 		case KC_ERR_LOADDB:
227 			(void) fprintf(stderr,
228 			    gettext("Error loading database\n"));
229 			break;
230 		case KC_ERR_FIND_POLICY:
231 			break;
232 		case KC_ERR_DELETE_POLICY:
233 			(void) fprintf(stderr, gettext("Error deleting policy "
234 			    "from database.\n"));
235 			break;
236 		case KC_ERR_ADD_POLICY:
237 			break;
238 		case KC_ERR_VERIFY_POLICY:
239 			break;
240 		case KC_ERR_INCOMPLETE_POLICY:
241 			break;
242 		case KC_ERR_MEMORY:
243 			(void) fprintf(stderr, gettext("Out of memory.\n"));
244 			break;
245 		case KC_ERR_ACCESS:
246 			break;
247 		case KC_ERR_INSTALL:
248 			break;
249 		case KC_ERR_UNINSTALL:
250 			break;
251 		default:
252 			(void) fprintf(stderr, gettext("%s operation failed. "
253 			    "error 0x%02x\n"), cmds[found].verb, ret);
254 			break;
255 	}
256 
257 	return (ret);
258 }
259