199ebb4caSwyllys /*
299ebb4caSwyllys  * CDDL HEADER START
399ebb4caSwyllys  *
499ebb4caSwyllys  * The contents of this file are subject to the terms of the
599ebb4caSwyllys  * Common Development and Distribution License (the "License").
699ebb4caSwyllys  * You may not use this file except in compliance with the License.
799ebb4caSwyllys  *
899ebb4caSwyllys  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
999ebb4caSwyllys  * or http://www.opensolaris.org/os/licensing.
1099ebb4caSwyllys  * See the License for the specific language governing permissions
1199ebb4caSwyllys  * and limitations under the License.
1299ebb4caSwyllys  *
1399ebb4caSwyllys  * When distributing Covered Code, include this CDDL HEADER in each
1499ebb4caSwyllys  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
1599ebb4caSwyllys  * If applicable, add the following below this CDDL HEADER, with the
1699ebb4caSwyllys  * fields enclosed by brackets "[]" replaced with your own identifying
1799ebb4caSwyllys  * information: Portions Copyright [yyyy] [name of copyright owner]
1899ebb4caSwyllys  *
1999ebb4caSwyllys  * CDDL HEADER END
2099ebb4caSwyllys  *
2199ebb4caSwyllys  *
22*30a5e8faSwyllys  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
2399ebb4caSwyllys  * Use is subject to license terms.
2499ebb4caSwyllys  */
2599ebb4caSwyllys 
2699ebb4caSwyllys #pragma ident	"%Z%%M%	%I%	%E% SMI"
2799ebb4caSwyllys 
2899ebb4caSwyllys #include <stdio.h>
2999ebb4caSwyllys #include <strings.h>
3099ebb4caSwyllys #include <ctype.h>
3199ebb4caSwyllys #include <libgen.h>
3299ebb4caSwyllys #include <libintl.h>
3399ebb4caSwyllys #include <locale.h>
3499ebb4caSwyllys #include <errno.h>
3599ebb4caSwyllys #include <kmfapiP.h>
3699ebb4caSwyllys 
3799ebb4caSwyllys #include "util.h"
3899ebb4caSwyllys 
3999ebb4caSwyllys int
4099ebb4caSwyllys kc_export(int argc, char *argv[])
4199ebb4caSwyllys {
4299ebb4caSwyllys 	int rv = KC_OK;
4399ebb4caSwyllys 	char *filename = NULL;
4499ebb4caSwyllys 	char *outfile = NULL;
4599ebb4caSwyllys 	char *policyname = NULL;
4699ebb4caSwyllys 	POLICY_LIST *plclist = NULL, *pnode;
4799ebb4caSwyllys 	int	opt, found = 0;
4899ebb4caSwyllys 	extern int	optind_av;
4999ebb4caSwyllys 	extern char	*optarg_av;
5099ebb4caSwyllys 
5199ebb4caSwyllys 	while ((opt = getopt_av(argc, argv,
52*30a5e8faSwyllys 	    "d:(dbfile)p:(policy)o:(outfile)")) != EOF) {
5399ebb4caSwyllys 		switch (opt) {
5499ebb4caSwyllys 			case 'd':
5599ebb4caSwyllys 				filename = get_string(optarg_av, &rv);
5699ebb4caSwyllys 				if (filename == NULL) {
5799ebb4caSwyllys 					(void) fprintf(stderr,
5899ebb4caSwyllys 					    gettext("Error dbfile input.\n"));
5999ebb4caSwyllys 				}
6099ebb4caSwyllys 				break;
6199ebb4caSwyllys 			case 'p':
6299ebb4caSwyllys 				policyname = get_string(optarg_av, &rv);
6399ebb4caSwyllys 				if (policyname == NULL) {
6499ebb4caSwyllys 					(void) fprintf(stderr,
6599ebb4caSwyllys 					    gettext("Error policy name.\n"));
6699ebb4caSwyllys 				}
6799ebb4caSwyllys 				break;
6899ebb4caSwyllys 			case 'o':
6999ebb4caSwyllys 				outfile = get_string(optarg_av, &rv);
7099ebb4caSwyllys 				if (outfile == NULL) {
7199ebb4caSwyllys 					(void) fprintf(stderr,
7299ebb4caSwyllys 					    gettext("Error outfile input.\n"));
7399ebb4caSwyllys 				}
7499ebb4caSwyllys 				break;
7599ebb4caSwyllys 			default:
7699ebb4caSwyllys 				(void) fprintf(stderr,
7799ebb4caSwyllys 				    gettext("Error input option.\n"));
7899ebb4caSwyllys 				rv = KC_ERR_USAGE;
7999ebb4caSwyllys 				break;
8099ebb4caSwyllys 		}
8199ebb4caSwyllys 
8299ebb4caSwyllys 		if (rv != KC_OK)
8399ebb4caSwyllys 			goto out;
8499ebb4caSwyllys 	}
8599ebb4caSwyllys 
8699ebb4caSwyllys 	/* No additional args allowed. */
8799ebb4caSwyllys 	argc -= optind_av;
8899ebb4caSwyllys 	if (argc) {
8999ebb4caSwyllys 		(void) fprintf(stderr,
9099ebb4caSwyllys 		    gettext("Error input option\n"));
9199ebb4caSwyllys 		rv = KC_ERR_USAGE;
9299ebb4caSwyllys 		goto out;
9399ebb4caSwyllys 	}
9499ebb4caSwyllys 
9599ebb4caSwyllys 	if (filename == NULL) {
9699ebb4caSwyllys 		filename = strdup(KMF_DEFAULT_POLICY_FILE);
9799ebb4caSwyllys 		if (filename == NULL) {
9899ebb4caSwyllys 			rv = KC_ERR_MEMORY;
9999ebb4caSwyllys 			goto out;
10099ebb4caSwyllys 		}
10199ebb4caSwyllys 	}
10299ebb4caSwyllys 
10399ebb4caSwyllys 	if (policyname == NULL) {
10499ebb4caSwyllys 		(void) fprintf(stderr,
10599ebb4caSwyllys 		    gettext("You must specify a policy name\n"));
10699ebb4caSwyllys 		rv = KC_ERR_USAGE;
10799ebb4caSwyllys 		goto out;
10899ebb4caSwyllys 	}
10999ebb4caSwyllys 
11099ebb4caSwyllys 	if (outfile == NULL) {
11199ebb4caSwyllys 		(void) fprintf(stderr,
11299ebb4caSwyllys 		    gettext("You must specify a output DB file\n"));
11399ebb4caSwyllys 		rv = KC_ERR_USAGE;
11499ebb4caSwyllys 		goto out;
11599ebb4caSwyllys 	}
11699ebb4caSwyllys 
11799ebb4caSwyllys 	if (strcmp(outfile, KMF_DEFAULT_POLICY_FILE) == 0 &&
11899ebb4caSwyllys 	    strcmp(policyname, KMF_DEFAULT_POLICY_NAME) == 0) {
11999ebb4caSwyllys 		(void) fprintf(stderr,
12099ebb4caSwyllys 		    gettext("Can not export the default policy record to "
12199ebb4caSwyllys 		    "the system default policy database\n"));
12299ebb4caSwyllys 		rv = KC_ERR_USAGE;
12399ebb4caSwyllys 		goto out;
12499ebb4caSwyllys 	}
12599ebb4caSwyllys 
12699ebb4caSwyllys 	rv = load_policies(filename, &plclist);
12799ebb4caSwyllys 	if (rv != KMF_OK)
12899ebb4caSwyllys 		goto out;
12999ebb4caSwyllys 
13099ebb4caSwyllys 	pnode = plclist;
13199ebb4caSwyllys 	while (pnode != NULL && !found) {
13299ebb4caSwyllys 		if (strcmp(policyname, pnode->plc.name) == 0) {
13399ebb4caSwyllys 			KMF_RETURN ret;
13499ebb4caSwyllys 
13599ebb4caSwyllys 			found++;
136*30a5e8faSwyllys 			ret = kmf_verify_policy(&pnode->plc);
13799ebb4caSwyllys 			if (ret != KMF_OK) {
13899ebb4caSwyllys 				print_sanity_error(ret);
13999ebb4caSwyllys 				rv = KC_ERR_VERIFY_POLICY;
14099ebb4caSwyllys 				break;
14199ebb4caSwyllys 			}
142*30a5e8faSwyllys 			rv = kmf_add_policy_to_db(&pnode->plc, outfile,
143*30a5e8faSwyllys 			    B_FALSE);
14499ebb4caSwyllys 		}
14599ebb4caSwyllys 		pnode = pnode->next;
14699ebb4caSwyllys 	}
14799ebb4caSwyllys 
14899ebb4caSwyllys 	if (!found) {
14999ebb4caSwyllys 		(void) fprintf(stderr,
15099ebb4caSwyllys 		    gettext("Could not find policy \"%s\" in %s\n"),
15199ebb4caSwyllys 		    policyname, filename);
15299ebb4caSwyllys 		rv = KC_ERR_FIND_POLICY;
15399ebb4caSwyllys 	}
15499ebb4caSwyllys 
15599ebb4caSwyllys out:
15699ebb4caSwyllys 	if (filename != NULL)
15799ebb4caSwyllys 		free(filename);
15899ebb4caSwyllys 
15999ebb4caSwyllys 	if (policyname != NULL)
16099ebb4caSwyllys 		free(policyname);
16199ebb4caSwyllys 
16299ebb4caSwyllys 	if (outfile != NULL)
16399ebb4caSwyllys 		free(outfile);
16499ebb4caSwyllys 
16599ebb4caSwyllys 	free_policy_list(plclist);
16699ebb4caSwyllys 
16799ebb4caSwyllys 	return (rv);
16899ebb4caSwyllys }
169