199ebb4caSwyllys /*
299ebb4caSwyllys * CDDL HEADER START
399ebb4caSwyllys *
499ebb4caSwyllys * The contents of this file are subject to the terms of the
599ebb4caSwyllys * Common Development and Distribution License (the "License").
699ebb4caSwyllys * You may not use this file except in compliance with the License.
799ebb4caSwyllys *
899ebb4caSwyllys * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
999ebb4caSwyllys * or http://www.opensolaris.org/os/licensing.
1099ebb4caSwyllys * See the License for the specific language governing permissions
1199ebb4caSwyllys * and limitations under the License.
1299ebb4caSwyllys *
1399ebb4caSwyllys * When distributing Covered Code, include this CDDL HEADER in each
1499ebb4caSwyllys * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
1599ebb4caSwyllys * If applicable, add the following below this CDDL HEADER, with the
1699ebb4caSwyllys * fields enclosed by brackets "[]" replaced with your own identifying
1799ebb4caSwyllys * information: Portions Copyright [yyyy] [name of copyright owner]
1899ebb4caSwyllys *
1999ebb4caSwyllys * CDDL HEADER END
20269e59f9SJan Pechanec *
21269e59f9SJan Pechanec * Copyright (c) 2006, 2010, Oracle and/or its affiliates. All rights reserved.
2299ebb4caSwyllys */
2399ebb4caSwyllys
2499ebb4caSwyllys #include <stdio.h>
2599ebb4caSwyllys #include <strings.h>
2699ebb4caSwyllys #include <ctype.h>
2799ebb4caSwyllys #include <libgen.h>
2899ebb4caSwyllys #include <libintl.h>
2999ebb4caSwyllys #include <locale.h>
3099ebb4caSwyllys
3199ebb4caSwyllys #include <kmfapiP.h>
3299ebb4caSwyllys
3399ebb4caSwyllys #include "util.h"
3499ebb4caSwyllys
3599ebb4caSwyllys /*
3699ebb4caSwyllys * The verbcmd construct allows genericizing information about a verb so
3799ebb4caSwyllys * that it is easier to manipulate. Makes parsing code easier to read,
3899ebb4caSwyllys * fix, and extend with new verbs.
3999ebb4caSwyllys */
4099ebb4caSwyllys typedef struct verbcmd_s {
4199ebb4caSwyllys char *verb;
4299ebb4caSwyllys int (*action)(int, char *[]);
4399ebb4caSwyllys char *synopsis;
4499ebb4caSwyllys } verbcmd;
4599ebb4caSwyllys
4699ebb4caSwyllys int kc_list(int argc, char *argv[]);
4799ebb4caSwyllys int kc_delete(int argc, char *argv[]);
4899ebb4caSwyllys int kc_create(int argc, char *argv[]);
4999ebb4caSwyllys int kc_modify(int argc, char *argv[]);
5099ebb4caSwyllys int kc_export(int argc, char *argv[]);
5199ebb4caSwyllys int kc_import(int argc, char *argv[]);
52431deaa0Shylee int kc_install(int argc, char *argv[]);
53431deaa0Shylee int kc_uninstall(int argc, char *argv[]);
54431deaa0Shylee
5599ebb4caSwyllys static int kc_help();
5699ebb4caSwyllys
5799ebb4caSwyllys static verbcmd cmds[] = {
58431deaa0Shylee { "list", kc_list,
59431deaa0Shylee "list [dbfile=dbfile] [policy=policyname]\n"
60431deaa0Shylee "\tlist plugin" },
6199ebb4caSwyllys { "delete", kc_delete, "delete [dbfile=dbfile] "
6299ebb4caSwyllys "policy=policyname" },
6399ebb4caSwyllys { "create", kc_create,
6499ebb4caSwyllys "create [dbfile=dbfile] policy=policyname\n"
6599ebb4caSwyllys "\t\t[ignore-date=true|false]\n"
6699ebb4caSwyllys "\t\t[ignore-unknown-eku=true|false]\n"
6799ebb4caSwyllys "\t\t[ignore-trust-anchor=true|false]\n"
6899ebb4caSwyllys "\t\t[validity-adjusttime=adjusttime]\n"
6999ebb4caSwyllys "\t\t[ta-name=trust anchor subject DN]\n"
7099ebb4caSwyllys "\t\t[ta-serial=trust anchor serial number]\n"
7199ebb4caSwyllys "\t\t[ocsp-responder=URL]\n"
7299ebb4caSwyllys "\t\t[ocsp-proxy=URL]\n"
7399ebb4caSwyllys "\t\t[ocsp-use-cert-responder=true|false]\n"
7499ebb4caSwyllys "\t\t[ocsp-response-lifetime=timelimit]\n"
7599ebb4caSwyllys "\t\t[ocsp-ignore-response-sign=true|false]\n"
7699ebb4caSwyllys "\t\t[ocsp-responder-cert-name=Issuer DN]\n"
7799ebb4caSwyllys "\t\t[ocsp-responder-cert-serial=serial number]\n"
7899ebb4caSwyllys "\t\t[crl-basefilename=basefilename]\n"
7999ebb4caSwyllys "\t\t[crl-directory=directory]\n"
8099ebb4caSwyllys "\t\t[crl-get-crl-uri=true|false]\n"
8199ebb4caSwyllys "\t\t[crl-proxy=URL]\n"
8299ebb4caSwyllys "\t\t[crl-ignore-crl-sign=true|false]\n"
8399ebb4caSwyllys "\t\t[crl-ignore-crl-date=true|false]\n"
8499ebb4caSwyllys "\t\t[keyusage=digitalSignature|nonRepudiation\n\t"
8599ebb4caSwyllys "\t\t|keyEncipherment | dataEncipherment |\n\t"
8699ebb4caSwyllys "\t\tkeyAgreement |keyCertSign |\n\t"
8799ebb4caSwyllys "\t\tcRLSign | encipherOnly | decipherOnly],[...]\n"
8899ebb4caSwyllys "\t\t[ekunames=serverAuth | clientAuth |\n\t"
8999ebb4caSwyllys "\t\tcodeSigning | emailProtection |\n\t"
9099ebb4caSwyllys "\t\tipsecEndSystem | ipsecTunnel |\n\t"
9199ebb4caSwyllys "\t\tipsecUser | timeStamping |\n\t"
9299ebb4caSwyllys "\t\tOCSPSigning],[...]\n"
93269e59f9SJan Pechanec "\t\t[ekuoids=OID,OID,OID...]\n"
94269e59f9SJan Pechanec "\t\t[mapper-name=name of mapper library]\n"
95269e59f9SJan Pechanec "\t\t[mapper-directory=dir where mapper library resides]\n"
96269e59f9SJan Pechanec "\t\t[mapper-path=full pathname of mapper library]\n"
97269e59f9SJan Pechanec "\t\t[mapper-options=mapper options]\n"},
9899ebb4caSwyllys { "modify", kc_modify,
9999ebb4caSwyllys "modify [dbfile=dbfile] policy=policyname\n"
10099ebb4caSwyllys "\t\t[ignore-date=true|false]\n"
10199ebb4caSwyllys "\t\t[ignore-unknown-eku=true|false]\n"
10299ebb4caSwyllys "\t\t[ignore-trust-anchor=true|false]\n"
10399ebb4caSwyllys "\t\t[validity-adjusttime=adjusttime]\n"
104*fc2613b0SWyllys Ingersoll "\t\t[ta-name=trust anchor subject DN | search]\n"
10599ebb4caSwyllys "\t\t[ta-serial=trust anchor serial number]\n"
10699ebb4caSwyllys "\t\t[ocsp-responder=URL]\n"
10799ebb4caSwyllys "\t\t[ocsp-proxy=URL]\n"
10899ebb4caSwyllys "\t\t[ocsp-use-cert-responder=true|false]\n"
10999ebb4caSwyllys "\t\t[ocsp-response-lifetime=timelimit]\n"
11099ebb4caSwyllys "\t\t[ocsp-ignore-response-sign=true|false]\n"
11199ebb4caSwyllys "\t\t[ocsp-responder-cert-name=Issuer DN]\n"
11299ebb4caSwyllys "\t\t[ocsp-responder-cert-serial=serial number]\n"
11399ebb4caSwyllys "\t\t[ocsp-none=true|false]\n"
11499ebb4caSwyllys "\t\t[crl-basefilename=basefilename]\n"
11599ebb4caSwyllys "\t\t[crl-directory=directory]\n"
11699ebb4caSwyllys "\t\t[crl-get-crl-uri=true|false]\n"
11799ebb4caSwyllys "\t\t[crl-proxy=URL]\n"
11899ebb4caSwyllys "\t\t[crl-ignore-crl-sign=true|false]\n"
11999ebb4caSwyllys "\t\t[crl-ignore-crl-date=true|false]\n"
12099ebb4caSwyllys "\t\t[crl-none=true|false]\n"
12199ebb4caSwyllys "\t\t[keyusage=digitalSignature|nonRepudiation\n\t"
12299ebb4caSwyllys "\t\t|keyEncipherment | dataEncipherment |\n\t"
12399ebb4caSwyllys "\t\tkeyAgreement |keyCertSign |\n\t"
12499ebb4caSwyllys "\t\tcRLSign | encipherOnly | decipherOnly],[...]\n"
12599ebb4caSwyllys "\t\t[keyusage-none=true|false]\n"
12699ebb4caSwyllys "\t\t[ekunames=serverAuth | clientAuth |\n\t"
12799ebb4caSwyllys "\t\tcodeSigning | emailProtection |\n\t"
12899ebb4caSwyllys "\t\tipsecEndSystem | ipsecTunnel |\n\t"
12999ebb4caSwyllys "\t\tipsecUser | timeStamping |\n\t"
13099ebb4caSwyllys "\t\tOCSPSigning],[...]\n"
13199ebb4caSwyllys "\t\t[ekuoids=OID,OID,OID...]\n"
132431deaa0Shylee "\t\t[eku-none=true|false]\n\n"
133269e59f9SJan Pechanec "\t\t[mapper-name=name of mapper library]\n"
134269e59f9SJan Pechanec "\t\t[mapper-directory=dir where mapper library resides]\n"
135269e59f9SJan Pechanec "\t\t[mapper-path=full pathname of mapper library]\n"
136269e59f9SJan Pechanec "\t\t[mapper-options=mapper options]\n"
137431deaa0Shylee "\tmodify plugin keystore=keystorename option=optionstring\n"},
138431deaa0Shylee
13999ebb4caSwyllys { "import", kc_import, "import [dbfile=dbfile] policy=policyname "
14099ebb4caSwyllys "infile=inputdbfile\n" },
14199ebb4caSwyllys { "export", kc_export, "export [dbfile=dbfile] policy=policyname "
14299ebb4caSwyllys "outfile=newdbfile\n" },
143431deaa0Shylee { "install", kc_install, "install keystore=keystorename "
144431deaa0Shylee "modulepath=path [option=optionstring]\n"},
145431deaa0Shylee { "uninstall", kc_uninstall, "uninstall keystore=keystorename\n"},
14699ebb4caSwyllys { "-?", kc_help, "help"},
14799ebb4caSwyllys { "help", kc_help, ""}
14899ebb4caSwyllys };
14999ebb4caSwyllys
15099ebb4caSwyllys static int num_cmds = sizeof (cmds) / sizeof (verbcmd);
15199ebb4caSwyllys static char *prog;
15299ebb4caSwyllys
15399ebb4caSwyllys static void
usage(void)15499ebb4caSwyllys usage(void)
15599ebb4caSwyllys {
15699ebb4caSwyllys int i;
15799ebb4caSwyllys
15899ebb4caSwyllys /* Display this block only in command-line mode. */
15999ebb4caSwyllys (void) fprintf(stdout, gettext("Usage:\n"));
16099ebb4caSwyllys (void) fprintf(stdout, gettext("\t%s -?\t(help and usage)\n"), prog);
16199ebb4caSwyllys (void) fprintf(stdout, gettext("\t%s subcommand [options...]\n"), prog);
16299ebb4caSwyllys (void) fprintf(stdout, gettext("where subcommands may be:\n"));
16399ebb4caSwyllys
16499ebb4caSwyllys /* Display only those verbs that match the current tool mode. */
16599ebb4caSwyllys for (i = 0; i < num_cmds; i++) {
16699ebb4caSwyllys /* Do NOT i18n/l10n. */
16799ebb4caSwyllys (void) fprintf(stdout, "\t%s\n", cmds[i].synopsis);
16899ebb4caSwyllys }
16999ebb4caSwyllys }
17099ebb4caSwyllys
17199ebb4caSwyllys static int
kc_help()17299ebb4caSwyllys kc_help()
17399ebb4caSwyllys {
17499ebb4caSwyllys usage();
17599ebb4caSwyllys return (0);
17699ebb4caSwyllys }
17799ebb4caSwyllys
17899ebb4caSwyllys int
main(int argc,char * argv[])17999ebb4caSwyllys main(int argc, char *argv[])
18099ebb4caSwyllys {
18199ebb4caSwyllys KMF_RETURN ret;
18299ebb4caSwyllys int found;
18399ebb4caSwyllys int i;
18499ebb4caSwyllys
18599ebb4caSwyllys (void) setlocale(LC_ALL, "");
18699ebb4caSwyllys #if !defined(TEXT_DOMAIN) /* Should be defined by cc -D. */
18799ebb4caSwyllys #define TEXT_DOMAIN "SYS_TEST" /* Use this only if it isn't. */
18899ebb4caSwyllys #endif
18999ebb4caSwyllys (void) textdomain(TEXT_DOMAIN);
19099ebb4caSwyllys
19199ebb4caSwyllys prog = basename(argv[0]);
19299ebb4caSwyllys argv++; argc--;
19399ebb4caSwyllys
19499ebb4caSwyllys if (argc == 0) {
19599ebb4caSwyllys usage();
19699ebb4caSwyllys exit(1);
19799ebb4caSwyllys }
19899ebb4caSwyllys
19999ebb4caSwyllys if (argc == 1 && argv[0][0] == '-') {
20099ebb4caSwyllys switch (argv[0][1]) {
20199ebb4caSwyllys case '?':
20299ebb4caSwyllys return (kc_help());
20399ebb4caSwyllys default:
20499ebb4caSwyllys usage();
20599ebb4caSwyllys exit(1);
20699ebb4caSwyllys }
20799ebb4caSwyllys }
20899ebb4caSwyllys
20999ebb4caSwyllys found = -1;
21099ebb4caSwyllys for (i = 0; i < num_cmds; i++) {
21199ebb4caSwyllys if (strcmp(cmds[i].verb, argv[0]) == 0) {
21299ebb4caSwyllys found = i;
21399ebb4caSwyllys break;
21499ebb4caSwyllys }
21599ebb4caSwyllys }
21699ebb4caSwyllys
21799ebb4caSwyllys if (found < 0) {
21899ebb4caSwyllys (void) fprintf(stderr, gettext("Invalid command: %s\n"),
21999ebb4caSwyllys argv[0]);
22099ebb4caSwyllys exit(1);
22199ebb4caSwyllys }
22299ebb4caSwyllys
22399ebb4caSwyllys ret = (*cmds[found].action)(argc, argv);
22499ebb4caSwyllys
22599ebb4caSwyllys switch (ret) {
22699ebb4caSwyllys case KC_OK:
22799ebb4caSwyllys break;
22899ebb4caSwyllys case KC_ERR_USAGE:
22999ebb4caSwyllys break;
23099ebb4caSwyllys case KC_ERR_LOADDB:
23199ebb4caSwyllys (void) fprintf(stderr,
23299ebb4caSwyllys gettext("Error loading database\n"));
23399ebb4caSwyllys break;
23499ebb4caSwyllys case KC_ERR_FIND_POLICY:
23599ebb4caSwyllys break;
23699ebb4caSwyllys case KC_ERR_DELETE_POLICY:
23799ebb4caSwyllys (void) fprintf(stderr, gettext("Error deleting policy "
23899ebb4caSwyllys "from database.\n"));
23999ebb4caSwyllys break;
24099ebb4caSwyllys case KC_ERR_ADD_POLICY:
24199ebb4caSwyllys break;
24299ebb4caSwyllys case KC_ERR_VERIFY_POLICY:
24399ebb4caSwyllys break;
24499ebb4caSwyllys case KC_ERR_INCOMPLETE_POLICY:
24599ebb4caSwyllys break;
24699ebb4caSwyllys case KC_ERR_MEMORY:
24799ebb4caSwyllys (void) fprintf(stderr, gettext("Out of memory.\n"));
24899ebb4caSwyllys break;
24999ebb4caSwyllys case KC_ERR_ACCESS:
25099ebb4caSwyllys break;
251431deaa0Shylee case KC_ERR_INSTALL:
252431deaa0Shylee break;
253431deaa0Shylee case KC_ERR_UNINSTALL:
254431deaa0Shylee break;
25599ebb4caSwyllys default:
25699ebb4caSwyllys (void) fprintf(stderr, gettext("%s operation failed. "
25799ebb4caSwyllys "error 0x%02x\n"), cmds[found].verb, ret);
25899ebb4caSwyllys break;
25999ebb4caSwyllys }
26099ebb4caSwyllys
26199ebb4caSwyllys return (ret);
26299ebb4caSwyllys }
263