dmake: do not set MAKEFLAGS=k
[unleashed/tickless.git] / usr / src / cmd / cmd-crypto / kmfcfg / import.c
blob069d26efc1ae2a768b9d9e42443543991fadc2d8
1 /*
2 * CDDL HEADER START
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.
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.
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]
19 * CDDL HEADER END
22 * Copyright 2007 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
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>
34 #include <kmfapiP.h>
36 #include "util.h"
38 int
39 kc_import(int argc, char *argv[])
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;
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"));
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"));
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"));
73 break;
74 default:
75 (void) fprintf(stderr,
76 gettext("Error input option.\n"));
77 rv = KC_ERR_USAGE;
78 break;
81 if (rv != KC_OK)
82 goto out;
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;
95 if (filename == NULL) {
96 filename = strdup(KMF_DEFAULT_POLICY_FILE);
97 if (filename == NULL) {
98 rv = KC_ERR_MEMORY;
99 goto out;
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;
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;
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;
126 rv = load_policies(infile, &plclist);
127 if (rv != KMF_OK)
128 goto out;
130 pnode = plclist;
131 while (pnode != NULL && !found) {
132 if (strcmp(policyname, pnode->plc.name) == 0) {
133 KMF_RETURN ret;
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;
142 rv = kmf_add_policy_to_db(&pnode->plc, filename,
143 B_FALSE);
145 pnode = pnode->next;
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;
155 out:
156 free(filename);
158 free(policyname);
160 free(infile);
162 free_policy_list(plclist);
164 return (rv);