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 #include <errno.h> 35 #include <kmfapiP.h> 36 37 #include "util.h" 38 39 int 40 kc_export(int argc, char *argv[]) 41 { 42 int rv = KC_OK; 43 char *filename = NULL; 44 char *outfile = NULL; 45 char *policyname = NULL; 46 POLICY_LIST *plclist = NULL, *pnode; 47 int opt, found = 0; 48 extern int optind_av; 49 extern char *optarg_av; 50 51 while ((opt = getopt_av(argc, argv, 52 "d:(dbfile)p:(policy)o:(outfile)")) != EOF) { 53 switch (opt) { 54 case 'd': 55 filename = get_string(optarg_av, &rv); 56 if (filename == NULL) { 57 (void) fprintf(stderr, 58 gettext("Error dbfile input.\n")); 59 } 60 break; 61 case 'p': 62 policyname = get_string(optarg_av, &rv); 63 if (policyname == NULL) { 64 (void) fprintf(stderr, 65 gettext("Error policy name.\n")); 66 } 67 break; 68 case 'o': 69 outfile = get_string(optarg_av, &rv); 70 if (outfile == NULL) { 71 (void) fprintf(stderr, 72 gettext("Error outfile input.\n")); 73 } 74 break; 75 default: 76 (void) fprintf(stderr, 77 gettext("Error input option.\n")); 78 rv = KC_ERR_USAGE; 79 break; 80 } 81 82 if (rv != KC_OK) 83 goto out; 84 } 85 86 /* No additional args allowed. */ 87 argc -= optind_av; 88 if (argc) { 89 (void) fprintf(stderr, 90 gettext("Error input option\n")); 91 rv = KC_ERR_USAGE; 92 goto out; 93 } 94 95 if (filename == NULL) { 96 filename = strdup(KMF_DEFAULT_POLICY_FILE); 97 if (filename == NULL) { 98 rv = KC_ERR_MEMORY; 99 goto out; 100 } 101 } 102 103 if (policyname == NULL) { 104 (void) fprintf(stderr, 105 gettext("You must specify a policy name\n")); 106 rv = KC_ERR_USAGE; 107 goto out; 108 } 109 110 if (outfile == NULL) { 111 (void) fprintf(stderr, 112 gettext("You must specify a output DB file\n")); 113 rv = KC_ERR_USAGE; 114 goto out; 115 } 116 117 if (strcmp(outfile, KMF_DEFAULT_POLICY_FILE) == 0 && 118 strcmp(policyname, KMF_DEFAULT_POLICY_NAME) == 0) { 119 (void) fprintf(stderr, 120 gettext("Can not export the default policy record to " 121 "the system default policy database\n")); 122 rv = KC_ERR_USAGE; 123 goto out; 124 } 125 126 rv = load_policies(filename, &plclist); 127 if (rv != KMF_OK) 128 goto out; 129 130 pnode = plclist; 131 while (pnode != NULL && !found) { 132 if (strcmp(policyname, pnode->plc.name) == 0) { 133 KMF_RETURN ret; 134 135 found++; 136 ret = kmf_verify_policy(&pnode->plc); 137 if (ret != KMF_OK) { 138 print_sanity_error(ret); 139 rv = KC_ERR_VERIFY_POLICY; 140 break; 141 } 142 rv = kmf_add_policy_to_db(&pnode->plc, outfile, 143 B_FALSE); 144 } 145 pnode = pnode->next; 146 } 147 148 if (!found) { 149 (void) fprintf(stderr, 150 gettext("Could not find policy \"%s\" in %s\n"), 151 policyname, filename); 152 rv = KC_ERR_FIND_POLICY; 153 } 154 155 out: 156 if (filename != NULL) 157 free(filename); 158 159 if (policyname != NULL) 160 free(policyname); 161 162 if (outfile != NULL) 163 free(outfile); 164 165 free_policy_list(plclist); 166 167 return (rv); 168 } 169