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