dmake: do not set MAKEFLAGS=k
[unleashed/tickless.git] / usr / src / cmd / cmd-crypto / kmfcfg / export.c
blobd78c241050c5ffa34b3c102be98d1419d3142871
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>
33 #include <kmfapiP.h>
35 #include "util.h"
37 int
38 kc_export(int argc, char *argv[])
40 int rv = KC_OK;
41 char *filename = NULL;
42 char *outfile = NULL;
43 char *policyname = NULL;
44 POLICY_LIST *plclist = NULL, *pnode;
45 int opt, found = 0;
46 extern int optind_av;
47 extern char *optarg_av;
49 while ((opt = getopt_av(argc, argv,
50 "d:(dbfile)p:(policy)o:(outfile)")) != EOF) {
51 switch (opt) {
52 case 'd':
53 filename = get_string(optarg_av, &rv);
54 if (filename == NULL) {
55 (void) fprintf(stderr,
56 gettext("Error dbfile input.\n"));
58 break;
59 case 'p':
60 policyname = get_string(optarg_av, &rv);
61 if (policyname == NULL) {
62 (void) fprintf(stderr,
63 gettext("Error policy name.\n"));
65 break;
66 case 'o':
67 outfile = get_string(optarg_av, &rv);
68 if (outfile == NULL) {
69 (void) fprintf(stderr,
70 gettext("Error outfile input.\n"));
72 break;
73 default:
74 (void) fprintf(stderr,
75 gettext("Error input option.\n"));
76 rv = KC_ERR_USAGE;
77 break;
80 if (rv != KC_OK)
81 goto out;
84 /* No additional args allowed. */
85 argc -= optind_av;
86 if (argc) {
87 (void) fprintf(stderr,
88 gettext("Error input option\n"));
89 rv = KC_ERR_USAGE;
90 goto out;
93 if (filename == NULL) {
94 filename = strdup(KMF_DEFAULT_POLICY_FILE);
95 if (filename == NULL) {
96 rv = KC_ERR_MEMORY;
97 goto out;
101 if (policyname == NULL) {
102 (void) fprintf(stderr,
103 gettext("You must specify a policy name\n"));
104 rv = KC_ERR_USAGE;
105 goto out;
108 if (outfile == NULL) {
109 (void) fprintf(stderr,
110 gettext("You must specify a output DB file\n"));
111 rv = KC_ERR_USAGE;
112 goto out;
115 if (strcmp(outfile, KMF_DEFAULT_POLICY_FILE) == 0 &&
116 strcmp(policyname, KMF_DEFAULT_POLICY_NAME) == 0) {
117 (void) fprintf(stderr,
118 gettext("Can not export the default policy record to "
119 "the system default policy database\n"));
120 rv = KC_ERR_USAGE;
121 goto out;
124 rv = load_policies(filename, &plclist);
125 if (rv != KMF_OK)
126 goto out;
128 pnode = plclist;
129 while (pnode != NULL && !found) {
130 if (strcmp(policyname, pnode->plc.name) == 0) {
131 KMF_RETURN ret;
133 found++;
134 ret = kmf_verify_policy(&pnode->plc);
135 if (ret != KMF_OK) {
136 print_sanity_error(ret);
137 rv = KC_ERR_VERIFY_POLICY;
138 break;
140 rv = kmf_add_policy_to_db(&pnode->plc, outfile,
141 B_FALSE);
143 pnode = pnode->next;
146 if (!found) {
147 (void) fprintf(stderr,
148 gettext("Could not find policy \"%s\" in %s\n"),
149 policyname, filename);
150 rv = KC_ERR_FIND_POLICY;
153 out:
154 free(filename);
156 free(policyname);
158 free(outfile);
160 free_policy_list(plclist);
162 return (rv);