13089Swyllys /*
23089Swyllys * CDDL HEADER START
33089Swyllys *
43089Swyllys * The contents of this file are subject to the terms of the
53089Swyllys * Common Development and Distribution License (the "License").
63089Swyllys * You may not use this file except in compliance with the License.
73089Swyllys *
83089Swyllys * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
93089Swyllys * or http://www.opensolaris.org/os/licensing.
103089Swyllys * See the License for the specific language governing permissions
113089Swyllys * and limitations under the License.
123089Swyllys *
133089Swyllys * When distributing Covered Code, include this CDDL HEADER in each
143089Swyllys * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
153089Swyllys * If applicable, add the following below this CDDL HEADER, with the
163089Swyllys * fields enclosed by brackets "[]" replaced with your own identifying
173089Swyllys * information: Portions Copyright [yyyy] [name of copyright owner]
183089Swyllys *
193089Swyllys * CDDL HEADER END
2012611SJan.Pechanec@Sun.COM *
2112611SJan.Pechanec@Sun.COM * Copyright (c) 2006, 2010, Oracle and/or its affiliates. All rights reserved.
223089Swyllys */
233089Swyllys
243089Swyllys #include <stdio.h>
253089Swyllys #include <strings.h>
263089Swyllys #include <ctype.h>
273089Swyllys #include <libgen.h>
283089Swyllys #include <libintl.h>
293089Swyllys #include <locale.h>
303089Swyllys
313089Swyllys #include <kmfapiP.h>
323089Swyllys
333089Swyllys #include "util.h"
343089Swyllys
353089Swyllys /*
363089Swyllys * The verbcmd construct allows genericizing information about a verb so
373089Swyllys * that it is easier to manipulate. Makes parsing code easier to read,
383089Swyllys * fix, and extend with new verbs.
393089Swyllys */
403089Swyllys typedef struct verbcmd_s {
413089Swyllys char *verb;
423089Swyllys int (*action)(int, char *[]);
433089Swyllys char *synopsis;
443089Swyllys } verbcmd;
453089Swyllys
463089Swyllys int kc_list(int argc, char *argv[]);
473089Swyllys int kc_delete(int argc, char *argv[]);
483089Swyllys int kc_create(int argc, char *argv[]);
493089Swyllys int kc_modify(int argc, char *argv[]);
503089Swyllys int kc_export(int argc, char *argv[]);
513089Swyllys int kc_import(int argc, char *argv[]);
525626Shylee int kc_install(int argc, char *argv[]);
535626Shylee int kc_uninstall(int argc, char *argv[]);
545626Shylee
553089Swyllys static int kc_help();
563089Swyllys
573089Swyllys static verbcmd cmds[] = {
585626Shylee { "list", kc_list,
595626Shylee "list [dbfile=dbfile] [policy=policyname]\n"
605626Shylee "\tlist plugin" },
613089Swyllys { "delete", kc_delete, "delete [dbfile=dbfile] "
623089Swyllys "policy=policyname" },
633089Swyllys { "create", kc_create,
643089Swyllys "create [dbfile=dbfile] policy=policyname\n"
653089Swyllys "\t\t[ignore-date=true|false]\n"
663089Swyllys "\t\t[ignore-unknown-eku=true|false]\n"
673089Swyllys "\t\t[ignore-trust-anchor=true|false]\n"
683089Swyllys "\t\t[validity-adjusttime=adjusttime]\n"
693089Swyllys "\t\t[ta-name=trust anchor subject DN]\n"
703089Swyllys "\t\t[ta-serial=trust anchor serial number]\n"
713089Swyllys "\t\t[ocsp-responder=URL]\n"
723089Swyllys "\t\t[ocsp-proxy=URL]\n"
733089Swyllys "\t\t[ocsp-use-cert-responder=true|false]\n"
743089Swyllys "\t\t[ocsp-response-lifetime=timelimit]\n"
753089Swyllys "\t\t[ocsp-ignore-response-sign=true|false]\n"
763089Swyllys "\t\t[ocsp-responder-cert-name=Issuer DN]\n"
773089Swyllys "\t\t[ocsp-responder-cert-serial=serial number]\n"
783089Swyllys "\t\t[crl-basefilename=basefilename]\n"
793089Swyllys "\t\t[crl-directory=directory]\n"
803089Swyllys "\t\t[crl-get-crl-uri=true|false]\n"
813089Swyllys "\t\t[crl-proxy=URL]\n"
823089Swyllys "\t\t[crl-ignore-crl-sign=true|false]\n"
833089Swyllys "\t\t[crl-ignore-crl-date=true|false]\n"
843089Swyllys "\t\t[keyusage=digitalSignature|nonRepudiation\n\t"
853089Swyllys "\t\t|keyEncipherment | dataEncipherment |\n\t"
863089Swyllys "\t\tkeyAgreement |keyCertSign |\n\t"
873089Swyllys "\t\tcRLSign | encipherOnly | decipherOnly],[...]\n"
883089Swyllys "\t\t[ekunames=serverAuth | clientAuth |\n\t"
893089Swyllys "\t\tcodeSigning | emailProtection |\n\t"
903089Swyllys "\t\tipsecEndSystem | ipsecTunnel |\n\t"
913089Swyllys "\t\tipsecUser | timeStamping |\n\t"
923089Swyllys "\t\tOCSPSigning],[...]\n"
9312611SJan.Pechanec@Sun.COM "\t\t[ekuoids=OID,OID,OID...]\n"
9412611SJan.Pechanec@Sun.COM "\t\t[mapper-name=name of mapper library]\n"
9512611SJan.Pechanec@Sun.COM "\t\t[mapper-directory=dir where mapper library resides]\n"
9612611SJan.Pechanec@Sun.COM "\t\t[mapper-path=full pathname of mapper library]\n"
9712611SJan.Pechanec@Sun.COM "\t\t[mapper-options=mapper options]\n"},
983089Swyllys { "modify", kc_modify,
993089Swyllys "modify [dbfile=dbfile] policy=policyname\n"
1003089Swyllys "\t\t[ignore-date=true|false]\n"
1013089Swyllys "\t\t[ignore-unknown-eku=true|false]\n"
1023089Swyllys "\t\t[ignore-trust-anchor=true|false]\n"
1033089Swyllys "\t\t[validity-adjusttime=adjusttime]\n"
104*12919Swyllys.ingersoll@sun.com "\t\t[ta-name=trust anchor subject DN | search]\n"
1053089Swyllys "\t\t[ta-serial=trust anchor serial number]\n"
1063089Swyllys "\t\t[ocsp-responder=URL]\n"
1073089Swyllys "\t\t[ocsp-proxy=URL]\n"
1083089Swyllys "\t\t[ocsp-use-cert-responder=true|false]\n"
1093089Swyllys "\t\t[ocsp-response-lifetime=timelimit]\n"
1103089Swyllys "\t\t[ocsp-ignore-response-sign=true|false]\n"
1113089Swyllys "\t\t[ocsp-responder-cert-name=Issuer DN]\n"
1123089Swyllys "\t\t[ocsp-responder-cert-serial=serial number]\n"
1133089Swyllys "\t\t[ocsp-none=true|false]\n"
1143089Swyllys "\t\t[crl-basefilename=basefilename]\n"
1153089Swyllys "\t\t[crl-directory=directory]\n"
1163089Swyllys "\t\t[crl-get-crl-uri=true|false]\n"
1173089Swyllys "\t\t[crl-proxy=URL]\n"
1183089Swyllys "\t\t[crl-ignore-crl-sign=true|false]\n"
1193089Swyllys "\t\t[crl-ignore-crl-date=true|false]\n"
1203089Swyllys "\t\t[crl-none=true|false]\n"
1213089Swyllys "\t\t[keyusage=digitalSignature|nonRepudiation\n\t"
1223089Swyllys "\t\t|keyEncipherment | dataEncipherment |\n\t"
1233089Swyllys "\t\tkeyAgreement |keyCertSign |\n\t"
1243089Swyllys "\t\tcRLSign | encipherOnly | decipherOnly],[...]\n"
1253089Swyllys "\t\t[keyusage-none=true|false]\n"
1263089Swyllys "\t\t[ekunames=serverAuth | clientAuth |\n\t"
1273089Swyllys "\t\tcodeSigning | emailProtection |\n\t"
1283089Swyllys "\t\tipsecEndSystem | ipsecTunnel |\n\t"
1293089Swyllys "\t\tipsecUser | timeStamping |\n\t"
1303089Swyllys "\t\tOCSPSigning],[...]\n"
1313089Swyllys "\t\t[ekuoids=OID,OID,OID...]\n"
1325626Shylee "\t\t[eku-none=true|false]\n\n"
13312611SJan.Pechanec@Sun.COM "\t\t[mapper-name=name of mapper library]\n"
13412611SJan.Pechanec@Sun.COM "\t\t[mapper-directory=dir where mapper library resides]\n"
13512611SJan.Pechanec@Sun.COM "\t\t[mapper-path=full pathname of mapper library]\n"
13612611SJan.Pechanec@Sun.COM "\t\t[mapper-options=mapper options]\n"
1375626Shylee "\tmodify plugin keystore=keystorename option=optionstring\n"},
1385626Shylee
1393089Swyllys { "import", kc_import, "import [dbfile=dbfile] policy=policyname "
1403089Swyllys "infile=inputdbfile\n" },
1413089Swyllys { "export", kc_export, "export [dbfile=dbfile] policy=policyname "
1423089Swyllys "outfile=newdbfile\n" },
1435626Shylee { "install", kc_install, "install keystore=keystorename "
1445626Shylee "modulepath=path [option=optionstring]\n"},
1455626Shylee { "uninstall", kc_uninstall, "uninstall keystore=keystorename\n"},
1463089Swyllys { "-?", kc_help, "help"},
1473089Swyllys { "help", kc_help, ""}
1483089Swyllys };
1493089Swyllys
1503089Swyllys static int num_cmds = sizeof (cmds) / sizeof (verbcmd);
1513089Swyllys static char *prog;
1523089Swyllys
1533089Swyllys static void
usage(void)1543089Swyllys usage(void)
1553089Swyllys {
1563089Swyllys int i;
1573089Swyllys
1583089Swyllys /* Display this block only in command-line mode. */
1593089Swyllys (void) fprintf(stdout, gettext("Usage:\n"));
1603089Swyllys (void) fprintf(stdout, gettext("\t%s -?\t(help and usage)\n"), prog);
1613089Swyllys (void) fprintf(stdout, gettext("\t%s subcommand [options...]\n"), prog);
1623089Swyllys (void) fprintf(stdout, gettext("where subcommands may be:\n"));
1633089Swyllys
1643089Swyllys /* Display only those verbs that match the current tool mode. */
1653089Swyllys for (i = 0; i < num_cmds; i++) {
1663089Swyllys /* Do NOT i18n/l10n. */
1673089Swyllys (void) fprintf(stdout, "\t%s\n", cmds[i].synopsis);
1683089Swyllys }
1693089Swyllys }
1703089Swyllys
1713089Swyllys static int
kc_help()1723089Swyllys kc_help()
1733089Swyllys {
1743089Swyllys usage();
1753089Swyllys return (0);
1763089Swyllys }
1773089Swyllys
1783089Swyllys int
main(int argc,char * argv[])1793089Swyllys main(int argc, char *argv[])
1803089Swyllys {
1813089Swyllys KMF_RETURN ret;
1823089Swyllys int found;
1833089Swyllys int i;
1843089Swyllys
1853089Swyllys (void) setlocale(LC_ALL, "");
1863089Swyllys #if !defined(TEXT_DOMAIN) /* Should be defined by cc -D. */
1873089Swyllys #define TEXT_DOMAIN "SYS_TEST" /* Use this only if it isn't. */
1883089Swyllys #endif
1893089Swyllys (void) textdomain(TEXT_DOMAIN);
1903089Swyllys
1913089Swyllys prog = basename(argv[0]);
1923089Swyllys argv++; argc--;
1933089Swyllys
1943089Swyllys if (argc == 0) {
1953089Swyllys usage();
1963089Swyllys exit(1);
1973089Swyllys }
1983089Swyllys
1993089Swyllys if (argc == 1 && argv[0][0] == '-') {
2003089Swyllys switch (argv[0][1]) {
2013089Swyllys case '?':
2023089Swyllys return (kc_help());
2033089Swyllys default:
2043089Swyllys usage();
2053089Swyllys exit(1);
2063089Swyllys }
2073089Swyllys }
2083089Swyllys
2093089Swyllys found = -1;
2103089Swyllys for (i = 0; i < num_cmds; i++) {
2113089Swyllys if (strcmp(cmds[i].verb, argv[0]) == 0) {
2123089Swyllys found = i;
2133089Swyllys break;
2143089Swyllys }
2153089Swyllys }
2163089Swyllys
2173089Swyllys if (found < 0) {
2183089Swyllys (void) fprintf(stderr, gettext("Invalid command: %s\n"),
2193089Swyllys argv[0]);
2203089Swyllys exit(1);
2213089Swyllys }
2223089Swyllys
2233089Swyllys ret = (*cmds[found].action)(argc, argv);
2243089Swyllys
2253089Swyllys switch (ret) {
2263089Swyllys case KC_OK:
2273089Swyllys break;
2283089Swyllys case KC_ERR_USAGE:
2293089Swyllys break;
2303089Swyllys case KC_ERR_LOADDB:
2313089Swyllys (void) fprintf(stderr,
2323089Swyllys gettext("Error loading database\n"));
2333089Swyllys break;
2343089Swyllys case KC_ERR_FIND_POLICY:
2353089Swyllys break;
2363089Swyllys case KC_ERR_DELETE_POLICY:
2373089Swyllys (void) fprintf(stderr, gettext("Error deleting policy "
2383089Swyllys "from database.\n"));
2393089Swyllys break;
2403089Swyllys case KC_ERR_ADD_POLICY:
2413089Swyllys break;
2423089Swyllys case KC_ERR_VERIFY_POLICY:
2433089Swyllys break;
2443089Swyllys case KC_ERR_INCOMPLETE_POLICY:
2453089Swyllys break;
2463089Swyllys case KC_ERR_MEMORY:
2473089Swyllys (void) fprintf(stderr, gettext("Out of memory.\n"));
2483089Swyllys break;
2493089Swyllys case KC_ERR_ACCESS:
2503089Swyllys break;
2515626Shylee case KC_ERR_INSTALL:
2525626Shylee break;
2535626Shylee case KC_ERR_UNINSTALL:
2545626Shylee break;
2553089Swyllys default:
2563089Swyllys (void) fprintf(stderr, gettext("%s operation failed. "
2573089Swyllys "error 0x%02x\n"), cmds[found].verb, ret);
2583089Swyllys break;
2593089Swyllys }
2603089Swyllys
2613089Swyllys return (ret);
2623089Swyllys }
263