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 #include <kmfapiP.h>
34 
35 #include "util.h"
36 
37 int
kc_export(int argc,char * argv[])38 kc_export(int argc, char *argv[])
39 {
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;
48 
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"));
57 				}
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"));
64 				}
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"));
71 				}
72 				break;
73 			default:
74 				(void) fprintf(stderr,
75 				    gettext("Error input option.\n"));
76 				rv = KC_ERR_USAGE;
77 				break;
78 		}
79 
80 		if (rv != KC_OK)
81 			goto out;
82 	}
83 
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;
91 	}
92 
93 	if (filename == NULL) {
94 		filename = strdup(KMF_DEFAULT_POLICY_FILE);
95 		if (filename == NULL) {
96 			rv = KC_ERR_MEMORY;
97 			goto out;
98 		}
99 	}
100 
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;
106 	}
107 
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;
113 	}
114 
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;
122 	}
123 
124 	rv = load_policies(filename, &plclist);
125 	if (rv != KMF_OK)
126 		goto out;
127 
128 	pnode = plclist;
129 	while (pnode != NULL && !found) {
130 		if (strcmp(policyname, pnode->plc.name) == 0) {
131 			KMF_RETURN ret;
132 
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;
139 			}
140 			rv = kmf_add_policy_to_db(&pnode->plc, outfile,
141 			    B_FALSE);
142 		}
143 		pnode = pnode->next;
144 	}
145 
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;
151 	}
152 
153 out:
154 	if (filename != NULL)
155 		free(filename);
156 
157 	if (policyname != NULL)
158 		free(policyname);
159 
160 	if (outfile != NULL)
161 		free(outfile);
162 
163 	free_policy_list(plclist);
164 
165 	return (rv);
166 }
167