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 2099ebb4caSwyllys */ 2199ebb4caSwyllys /* 22*431deaa0Shylee * Copyright 2007 Sun Microsystems, Inc. All rights reserved. 2399ebb4caSwyllys * Use is subject to license terms. 2499ebb4caSwyllys */ 2599ebb4caSwyllys 2699ebb4caSwyllys #pragma ident "%Z%%M% %I% %E% SMI" 2799ebb4caSwyllys 2899ebb4caSwyllys #include <stdio.h> 2999ebb4caSwyllys #include <strings.h> 3099ebb4caSwyllys #include <ctype.h> 3199ebb4caSwyllys #include <libgen.h> 3299ebb4caSwyllys #include <libintl.h> 3399ebb4caSwyllys #include <locale.h> 3499ebb4caSwyllys 3599ebb4caSwyllys #include <kmfapiP.h> 3699ebb4caSwyllys 3799ebb4caSwyllys #include "util.h" 3899ebb4caSwyllys 3999ebb4caSwyllys /* 4099ebb4caSwyllys * The verbcmd construct allows genericizing information about a verb so 4199ebb4caSwyllys * that it is easier to manipulate. Makes parsing code easier to read, 4299ebb4caSwyllys * fix, and extend with new verbs. 4399ebb4caSwyllys */ 4499ebb4caSwyllys typedef struct verbcmd_s { 4599ebb4caSwyllys char *verb; 4699ebb4caSwyllys int (*action)(int, char *[]); 4799ebb4caSwyllys char *synopsis; 4899ebb4caSwyllys } verbcmd; 4999ebb4caSwyllys 5099ebb4caSwyllys int kc_list(int argc, char *argv[]); 5199ebb4caSwyllys int kc_delete(int argc, char *argv[]); 5299ebb4caSwyllys int kc_create(int argc, char *argv[]); 5399ebb4caSwyllys int kc_modify(int argc, char *argv[]); 5499ebb4caSwyllys int kc_export(int argc, char *argv[]); 5599ebb4caSwyllys int kc_import(int argc, char *argv[]); 56*431deaa0Shylee int kc_install(int argc, char *argv[]); 57*431deaa0Shylee int kc_uninstall(int argc, char *argv[]); 58*431deaa0Shylee 5999ebb4caSwyllys static int kc_help(); 6099ebb4caSwyllys 6199ebb4caSwyllys static verbcmd cmds[] = { 62*431deaa0Shylee { "list", kc_list, 63*431deaa0Shylee "list [dbfile=dbfile] [policy=policyname]\n" 64*431deaa0Shylee "\tlist plugin" }, 6599ebb4caSwyllys { "delete", kc_delete, "delete [dbfile=dbfile] " 6699ebb4caSwyllys "policy=policyname" }, 6799ebb4caSwyllys { "create", kc_create, 6899ebb4caSwyllys "create [dbfile=dbfile] policy=policyname\n" 6999ebb4caSwyllys "\t\t[ignore-date=true|false]\n" 7099ebb4caSwyllys "\t\t[ignore-unknown-eku=true|false]\n" 7199ebb4caSwyllys "\t\t[ignore-trust-anchor=true|false]\n" 7299ebb4caSwyllys "\t\t[validity-adjusttime=adjusttime]\n" 7399ebb4caSwyllys "\t\t[ta-name=trust anchor subject DN]\n" 7499ebb4caSwyllys "\t\t[ta-serial=trust anchor serial number]\n" 7599ebb4caSwyllys "\t\t[ocsp-responder=URL]\n" 7699ebb4caSwyllys "\t\t[ocsp-proxy=URL]\n" 7799ebb4caSwyllys "\t\t[ocsp-use-cert-responder=true|false]\n" 7899ebb4caSwyllys "\t\t[ocsp-response-lifetime=timelimit]\n" 7999ebb4caSwyllys "\t\t[ocsp-ignore-response-sign=true|false]\n" 8099ebb4caSwyllys "\t\t[ocsp-responder-cert-name=Issuer DN]\n" 8199ebb4caSwyllys "\t\t[ocsp-responder-cert-serial=serial number]\n" 8299ebb4caSwyllys "\t\t[crl-basefilename=basefilename]\n" 8399ebb4caSwyllys "\t\t[crl-directory=directory]\n" 8499ebb4caSwyllys "\t\t[crl-get-crl-uri=true|false]\n" 8599ebb4caSwyllys "\t\t[crl-proxy=URL]\n" 8699ebb4caSwyllys "\t\t[crl-ignore-crl-sign=true|false]\n" 8799ebb4caSwyllys "\t\t[crl-ignore-crl-date=true|false]\n" 8899ebb4caSwyllys "\t\t[keyusage=digitalSignature|nonRepudiation\n\t" 8999ebb4caSwyllys "\t\t|keyEncipherment | dataEncipherment |\n\t" 9099ebb4caSwyllys "\t\tkeyAgreement |keyCertSign |\n\t" 9199ebb4caSwyllys "\t\tcRLSign | encipherOnly | decipherOnly],[...]\n" 9299ebb4caSwyllys "\t\t[ekunames=serverAuth | clientAuth |\n\t" 9399ebb4caSwyllys "\t\tcodeSigning | emailProtection |\n\t" 9499ebb4caSwyllys "\t\tipsecEndSystem | ipsecTunnel |\n\t" 9599ebb4caSwyllys "\t\tipsecUser | timeStamping |\n\t" 9699ebb4caSwyllys "\t\tOCSPSigning],[...]\n" 9799ebb4caSwyllys "\t\t[ekuoids=OID,OID,OID...]\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" 10499ebb4caSwyllys "\t\t[ta-name=trust anchor subject DN]\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" 132*431deaa0Shylee "\t\t[eku-none=true|false]\n\n" 133*431deaa0Shylee "\tmodify plugin keystore=keystorename option=optionstring\n"}, 134*431deaa0Shylee 13599ebb4caSwyllys { "import", kc_import, "import [dbfile=dbfile] policy=policyname " 13699ebb4caSwyllys "infile=inputdbfile\n" }, 13799ebb4caSwyllys { "export", kc_export, "export [dbfile=dbfile] policy=policyname " 13899ebb4caSwyllys "outfile=newdbfile\n" }, 139*431deaa0Shylee { "install", kc_install, "install keystore=keystorename " 140*431deaa0Shylee "modulepath=path [option=optionstring]\n"}, 141*431deaa0Shylee { "uninstall", kc_uninstall, "uninstall keystore=keystorename\n"}, 14299ebb4caSwyllys { "-?", kc_help, "help"}, 14399ebb4caSwyllys { "help", kc_help, ""} 14499ebb4caSwyllys }; 14599ebb4caSwyllys 14699ebb4caSwyllys static int num_cmds = sizeof (cmds) / sizeof (verbcmd); 14799ebb4caSwyllys static char *prog; 14899ebb4caSwyllys 14999ebb4caSwyllys static void 15099ebb4caSwyllys usage(void) 15199ebb4caSwyllys { 15299ebb4caSwyllys int i; 15399ebb4caSwyllys 15499ebb4caSwyllys /* Display this block only in command-line mode. */ 15599ebb4caSwyllys (void) fprintf(stdout, gettext("Usage:\n")); 15699ebb4caSwyllys (void) fprintf(stdout, gettext("\t%s -?\t(help and usage)\n"), prog); 15799ebb4caSwyllys (void) fprintf(stdout, gettext("\t%s subcommand [options...]\n"), prog); 15899ebb4caSwyllys (void) fprintf(stdout, gettext("where subcommands may be:\n")); 15999ebb4caSwyllys 16099ebb4caSwyllys /* Display only those verbs that match the current tool mode. */ 16199ebb4caSwyllys for (i = 0; i < num_cmds; i++) { 16299ebb4caSwyllys /* Do NOT i18n/l10n. */ 16399ebb4caSwyllys (void) fprintf(stdout, "\t%s\n", cmds[i].synopsis); 16499ebb4caSwyllys } 16599ebb4caSwyllys } 16699ebb4caSwyllys 16799ebb4caSwyllys static int 16899ebb4caSwyllys kc_help() 16999ebb4caSwyllys { 17099ebb4caSwyllys usage(); 17199ebb4caSwyllys return (0); 17299ebb4caSwyllys } 17399ebb4caSwyllys 17499ebb4caSwyllys int 17599ebb4caSwyllys main(int argc, char *argv[]) 17699ebb4caSwyllys { 17799ebb4caSwyllys KMF_RETURN ret; 17899ebb4caSwyllys int found; 17999ebb4caSwyllys int i; 18099ebb4caSwyllys 18199ebb4caSwyllys (void) setlocale(LC_ALL, ""); 18299ebb4caSwyllys #if !defined(TEXT_DOMAIN) /* Should be defined by cc -D. */ 18399ebb4caSwyllys #define TEXT_DOMAIN "SYS_TEST" /* Use this only if it isn't. */ 18499ebb4caSwyllys #endif 18599ebb4caSwyllys (void) textdomain(TEXT_DOMAIN); 18699ebb4caSwyllys 18799ebb4caSwyllys prog = basename(argv[0]); 18899ebb4caSwyllys argv++; argc--; 18999ebb4caSwyllys 19099ebb4caSwyllys if (argc == 0) { 19199ebb4caSwyllys usage(); 19299ebb4caSwyllys exit(1); 19399ebb4caSwyllys } 19499ebb4caSwyllys 19599ebb4caSwyllys if (argc == 1 && argv[0][0] == '-') { 19699ebb4caSwyllys switch (argv[0][1]) { 19799ebb4caSwyllys case '?': 19899ebb4caSwyllys return (kc_help()); 19999ebb4caSwyllys default: 20099ebb4caSwyllys usage(); 20199ebb4caSwyllys exit(1); 20299ebb4caSwyllys } 20399ebb4caSwyllys } 20499ebb4caSwyllys 20599ebb4caSwyllys found = -1; 20699ebb4caSwyllys for (i = 0; i < num_cmds; i++) { 20799ebb4caSwyllys if (strcmp(cmds[i].verb, argv[0]) == 0) { 20899ebb4caSwyllys found = i; 20999ebb4caSwyllys break; 21099ebb4caSwyllys } 21199ebb4caSwyllys } 21299ebb4caSwyllys 21399ebb4caSwyllys if (found < 0) { 21499ebb4caSwyllys (void) fprintf(stderr, gettext("Invalid command: %s\n"), 21599ebb4caSwyllys argv[0]); 21699ebb4caSwyllys exit(1); 21799ebb4caSwyllys } 21899ebb4caSwyllys 21999ebb4caSwyllys ret = (*cmds[found].action)(argc, argv); 22099ebb4caSwyllys 22199ebb4caSwyllys switch (ret) { 22299ebb4caSwyllys case KC_OK: 22399ebb4caSwyllys break; 22499ebb4caSwyllys case KC_ERR_USAGE: 22599ebb4caSwyllys break; 22699ebb4caSwyllys case KC_ERR_LOADDB: 22799ebb4caSwyllys (void) fprintf(stderr, 22899ebb4caSwyllys gettext("Error loading database\n")); 22999ebb4caSwyllys break; 23099ebb4caSwyllys case KC_ERR_FIND_POLICY: 23199ebb4caSwyllys break; 23299ebb4caSwyllys case KC_ERR_DELETE_POLICY: 23399ebb4caSwyllys (void) fprintf(stderr, gettext("Error deleting policy " 23499ebb4caSwyllys "from database.\n")); 23599ebb4caSwyllys break; 23699ebb4caSwyllys case KC_ERR_ADD_POLICY: 23799ebb4caSwyllys break; 23899ebb4caSwyllys case KC_ERR_VERIFY_POLICY: 23999ebb4caSwyllys break; 24099ebb4caSwyllys case KC_ERR_INCOMPLETE_POLICY: 24199ebb4caSwyllys break; 24299ebb4caSwyllys case KC_ERR_MEMORY: 24399ebb4caSwyllys (void) fprintf(stderr, gettext("Out of memory.\n")); 24499ebb4caSwyllys break; 24599ebb4caSwyllys case KC_ERR_ACCESS: 24699ebb4caSwyllys break; 247*431deaa0Shylee case KC_ERR_INSTALL: 248*431deaa0Shylee break; 249*431deaa0Shylee case KC_ERR_UNINSTALL: 250*431deaa0Shylee break; 25199ebb4caSwyllys default: 25299ebb4caSwyllys (void) fprintf(stderr, gettext("%s operation failed. " 25399ebb4caSwyllys "error 0x%02x\n"), cmds[found].verb, ret); 25499ebb4caSwyllys break; 25599ebb4caSwyllys } 25699ebb4caSwyllys 25799ebb4caSwyllys return (ret); 25899ebb4caSwyllys } 259