xref: /onnv-gate/usr/src/cmd/cmd-crypto/kmfcfg/export.c (revision 3089:8ddeb2ace8aa)
1*3089Swyllys /*
2*3089Swyllys  * CDDL HEADER START
3*3089Swyllys  *
4*3089Swyllys  * The contents of this file are subject to the terms of the
5*3089Swyllys  * Common Development and Distribution License (the "License").
6*3089Swyllys  * You may not use this file except in compliance with the License.
7*3089Swyllys  *
8*3089Swyllys  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9*3089Swyllys  * or http://www.opensolaris.org/os/licensing.
10*3089Swyllys  * See the License for the specific language governing permissions
11*3089Swyllys  * and limitations under the License.
12*3089Swyllys  *
13*3089Swyllys  * When distributing Covered Code, include this CDDL HEADER in each
14*3089Swyllys  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15*3089Swyllys  * If applicable, add the following below this CDDL HEADER, with the
16*3089Swyllys  * fields enclosed by brackets "[]" replaced with your own identifying
17*3089Swyllys  * information: Portions Copyright [yyyy] [name of copyright owner]
18*3089Swyllys  *
19*3089Swyllys  * CDDL HEADER END
20*3089Swyllys  *
21*3089Swyllys  *
22*3089Swyllys  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
23*3089Swyllys  * Use is subject to license terms.
24*3089Swyllys  */
25*3089Swyllys 
26*3089Swyllys #pragma ident	"%Z%%M%	%I%	%E% SMI"
27*3089Swyllys 
28*3089Swyllys #include <stdio.h>
29*3089Swyllys #include <strings.h>
30*3089Swyllys #include <ctype.h>
31*3089Swyllys #include <libgen.h>
32*3089Swyllys #include <libintl.h>
33*3089Swyllys #include <locale.h>
34*3089Swyllys #include <errno.h>
35*3089Swyllys #include <kmfapiP.h>
36*3089Swyllys 
37*3089Swyllys #include "util.h"
38*3089Swyllys 
39*3089Swyllys int
40*3089Swyllys kc_export(int argc, char *argv[])
41*3089Swyllys {
42*3089Swyllys 	int rv = KC_OK;
43*3089Swyllys 	char *filename = NULL;
44*3089Swyllys 	char *outfile = NULL;
45*3089Swyllys 	char *policyname = NULL;
46*3089Swyllys 	POLICY_LIST *plclist = NULL, *pnode;
47*3089Swyllys 	int	opt, found = 0;
48*3089Swyllys 	extern int	optind_av;
49*3089Swyllys 	extern char	*optarg_av;
50*3089Swyllys 
51*3089Swyllys 	while ((opt = getopt_av(argc, argv,
52*3089Swyllys 		"d:(dbfile)p:(policy)o:(outfile)")) != EOF) {
53*3089Swyllys 		switch (opt) {
54*3089Swyllys 			case 'd':
55*3089Swyllys 				filename = get_string(optarg_av, &rv);
56*3089Swyllys 				if (filename == NULL) {
57*3089Swyllys 					(void) fprintf(stderr,
58*3089Swyllys 					    gettext("Error dbfile input.\n"));
59*3089Swyllys 				}
60*3089Swyllys 				break;
61*3089Swyllys 			case 'p':
62*3089Swyllys 				policyname = get_string(optarg_av, &rv);
63*3089Swyllys 				if (policyname == NULL) {
64*3089Swyllys 					(void) fprintf(stderr,
65*3089Swyllys 					    gettext("Error policy name.\n"));
66*3089Swyllys 				}
67*3089Swyllys 				break;
68*3089Swyllys 			case 'o':
69*3089Swyllys 				outfile = get_string(optarg_av, &rv);
70*3089Swyllys 				if (outfile == NULL) {
71*3089Swyllys 					(void) fprintf(stderr,
72*3089Swyllys 					    gettext("Error outfile input.\n"));
73*3089Swyllys 				}
74*3089Swyllys 				break;
75*3089Swyllys 			default:
76*3089Swyllys 				(void) fprintf(stderr,
77*3089Swyllys 				    gettext("Error input option.\n"));
78*3089Swyllys 				rv = KC_ERR_USAGE;
79*3089Swyllys 				break;
80*3089Swyllys 		}
81*3089Swyllys 
82*3089Swyllys 		if (rv != KC_OK)
83*3089Swyllys 			goto out;
84*3089Swyllys 	}
85*3089Swyllys 
86*3089Swyllys 	/* No additional args allowed. */
87*3089Swyllys 	argc -= optind_av;
88*3089Swyllys 	if (argc) {
89*3089Swyllys 		(void) fprintf(stderr,
90*3089Swyllys 		    gettext("Error input option\n"));
91*3089Swyllys 		rv = KC_ERR_USAGE;
92*3089Swyllys 		goto out;
93*3089Swyllys 	}
94*3089Swyllys 
95*3089Swyllys 	if (filename == NULL) {
96*3089Swyllys 		filename = strdup(KMF_DEFAULT_POLICY_FILE);
97*3089Swyllys 		if (filename == NULL) {
98*3089Swyllys 			rv = KC_ERR_MEMORY;
99*3089Swyllys 			goto out;
100*3089Swyllys 		}
101*3089Swyllys 	}
102*3089Swyllys 
103*3089Swyllys 	if (policyname == NULL) {
104*3089Swyllys 		(void) fprintf(stderr,
105*3089Swyllys 		    gettext("You must specify a policy name\n"));
106*3089Swyllys 		rv = KC_ERR_USAGE;
107*3089Swyllys 		goto out;
108*3089Swyllys 	}
109*3089Swyllys 
110*3089Swyllys 	if (outfile == NULL) {
111*3089Swyllys 		(void) fprintf(stderr,
112*3089Swyllys 		    gettext("You must specify a output DB file\n"));
113*3089Swyllys 		rv = KC_ERR_USAGE;
114*3089Swyllys 		goto out;
115*3089Swyllys 	}
116*3089Swyllys 
117*3089Swyllys 	if (strcmp(outfile, KMF_DEFAULT_POLICY_FILE) == 0 &&
118*3089Swyllys 	    strcmp(policyname, KMF_DEFAULT_POLICY_NAME) == 0) {
119*3089Swyllys 		(void) fprintf(stderr,
120*3089Swyllys 		    gettext("Can not export the default policy record to "
121*3089Swyllys 		    "the system default policy database\n"));
122*3089Swyllys 		rv = KC_ERR_USAGE;
123*3089Swyllys 		goto out;
124*3089Swyllys 	}
125*3089Swyllys 
126*3089Swyllys 	rv = load_policies(filename, &plclist);
127*3089Swyllys 	if (rv != KMF_OK)
128*3089Swyllys 		goto out;
129*3089Swyllys 
130*3089Swyllys 	pnode = plclist;
131*3089Swyllys 	while (pnode != NULL && !found) {
132*3089Swyllys 		if (strcmp(policyname, pnode->plc.name) == 0) {
133*3089Swyllys 			KMF_RETURN ret;
134*3089Swyllys 
135*3089Swyllys 			found++;
136*3089Swyllys 			ret = KMF_VerifyPolicy(&pnode->plc);
137*3089Swyllys 			if (ret != KMF_OK) {
138*3089Swyllys 				print_sanity_error(ret);
139*3089Swyllys 				rv = KC_ERR_VERIFY_POLICY;
140*3089Swyllys 				break;
141*3089Swyllys 			}
142*3089Swyllys 			rv = KMF_AddPolicyToDB(&pnode->plc, outfile, B_FALSE);
143*3089Swyllys 		}
144*3089Swyllys 		pnode = pnode->next;
145*3089Swyllys 	}
146*3089Swyllys 
147*3089Swyllys 	if (!found) {
148*3089Swyllys 		(void) fprintf(stderr,
149*3089Swyllys 		    gettext("Could not find policy \"%s\" in %s\n"),
150*3089Swyllys 		    policyname, filename);
151*3089Swyllys 		rv = KC_ERR_FIND_POLICY;
152*3089Swyllys 	}
153*3089Swyllys 
154*3089Swyllys out:
155*3089Swyllys 	if (filename != NULL)
156*3089Swyllys 		free(filename);
157*3089Swyllys 
158*3089Swyllys 	if (policyname != NULL)
159*3089Swyllys 		free(policyname);
160*3089Swyllys 
161*3089Swyllys 	if (outfile != NULL)
162*3089Swyllys 		free(outfile);
163*3089Swyllys 
164*3089Swyllys 	free_policy_list(plclist);
165*3089Swyllys 
166*3089Swyllys 	return (rv);
167*3089Swyllys }
168