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 #include <stdio.h> 27 #include <strings.h> 28 #include <ctype.h> 29 #include <libgen.h> 30 #include <libintl.h> 31 #include <locale.h> 32 #include <errno.h> 33 34 #include <kmfapiP.h> 35 36 #include "util.h" 37 38 int 39 kc_import(int argc, char *argv[]) 40 { 41 int rv = KC_OK; 42 char *filename = NULL; 43 char *infile = NULL; 44 char *policyname = NULL; 45 POLICY_LIST *plclist = NULL, *pnode; 46 int opt, found = 0; 47 extern int optind_av; 48 extern char *optarg_av; 49 50 while ((opt = getopt_av(argc, argv, 51 "d:(dbfile)p:(policy)i:(infile)")) != EOF) { 52 switch (opt) { 53 case 'd': 54 filename = get_string(optarg_av, &rv); 55 if (filename == NULL) { 56 (void) fprintf(stderr, 57 gettext("Error dbfile input.\n")); 58 } 59 break; 60 case 'p': 61 policyname = get_string(optarg_av, &rv); 62 if (policyname == NULL) { 63 (void) fprintf(stderr, 64 gettext("Error policy name.\n")); 65 } 66 break; 67 case 'i': 68 infile = get_string(optarg_av, &rv); 69 if (infile == NULL) { 70 (void) fprintf(stderr, 71 gettext("Error infile input.\n")); 72 } 73 break; 74 default: 75 (void) fprintf(stderr, 76 gettext("Error input option.\n")); 77 rv = KC_ERR_USAGE; 78 break; 79 } 80 81 if (rv != KC_OK) 82 goto out; 83 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 (infile == NULL) { 111 (void) fprintf(stderr, 112 gettext("You must specify a input DB file\n")); 113 rv = KC_ERR_USAGE; 114 goto out; 115 } 116 117 if (strcmp(filename, KMF_DEFAULT_POLICY_FILE) == 0 && 118 strcmp(policyname, KMF_DEFAULT_POLICY_NAME) == 0) { 119 (void) fprintf(stderr, 120 gettext("Can not import 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(infile, &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, filename, 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, infile); 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 (infile != NULL) 163 free(infile); 164 165 free_policy_list(plclist); 166 167 return (rv); 168 } 169