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