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