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 #include <stdio.h>
2799ebb4caSwyllys #include <strings.h>
2899ebb4caSwyllys #include <ctype.h>
2999ebb4caSwyllys #include <libgen.h>
3099ebb4caSwyllys #include <libintl.h>
3199ebb4caSwyllys #include <locale.h>
3299ebb4caSwyllys #include <errno.h>
3399ebb4caSwyllys #include <kmfapiP.h>
3499ebb4caSwyllys
3599ebb4caSwyllys #include "util.h"
3699ebb4caSwyllys
3799ebb4caSwyllys int
kc_export(int argc,char * argv[])3899ebb4caSwyllys kc_export(int argc, char *argv[])
3999ebb4caSwyllys {
4099ebb4caSwyllys int rv = KC_OK;
4199ebb4caSwyllys char *filename = NULL;
4299ebb4caSwyllys char *outfile = NULL;
4399ebb4caSwyllys char *policyname = NULL;
4499ebb4caSwyllys POLICY_LIST *plclist = NULL, *pnode;
4599ebb4caSwyllys int opt, found = 0;
4699ebb4caSwyllys extern int optind_av;
4799ebb4caSwyllys extern char *optarg_av;
4899ebb4caSwyllys
4999ebb4caSwyllys while ((opt = getopt_av(argc, argv,
50*30a5e8faSwyllys "d:(dbfile)p:(policy)o:(outfile)")) != EOF) {
5199ebb4caSwyllys switch (opt) {
5299ebb4caSwyllys case 'd':
5399ebb4caSwyllys filename = get_string(optarg_av, &rv);
5499ebb4caSwyllys if (filename == NULL) {
5599ebb4caSwyllys (void) fprintf(stderr,
5699ebb4caSwyllys gettext("Error dbfile input.\n"));
5799ebb4caSwyllys }
5899ebb4caSwyllys break;
5999ebb4caSwyllys case 'p':
6099ebb4caSwyllys policyname = get_string(optarg_av, &rv);
6199ebb4caSwyllys if (policyname == NULL) {
6299ebb4caSwyllys (void) fprintf(stderr,
6399ebb4caSwyllys gettext("Error policy name.\n"));
6499ebb4caSwyllys }
6599ebb4caSwyllys break;
6699ebb4caSwyllys case 'o':
6799ebb4caSwyllys outfile = get_string(optarg_av, &rv);
6899ebb4caSwyllys if (outfile == NULL) {
6999ebb4caSwyllys (void) fprintf(stderr,
7099ebb4caSwyllys gettext("Error outfile input.\n"));
7199ebb4caSwyllys }
7299ebb4caSwyllys break;
7399ebb4caSwyllys default:
7499ebb4caSwyllys (void) fprintf(stderr,
7599ebb4caSwyllys gettext("Error input option.\n"));
7699ebb4caSwyllys rv = KC_ERR_USAGE;
7799ebb4caSwyllys break;
7899ebb4caSwyllys }
7999ebb4caSwyllys
8099ebb4caSwyllys if (rv != KC_OK)
8199ebb4caSwyllys goto out;
8299ebb4caSwyllys }
8399ebb4caSwyllys
8499ebb4caSwyllys /* No additional args allowed. */
8599ebb4caSwyllys argc -= optind_av;
8699ebb4caSwyllys if (argc) {
8799ebb4caSwyllys (void) fprintf(stderr,
8899ebb4caSwyllys gettext("Error input option\n"));
8999ebb4caSwyllys rv = KC_ERR_USAGE;
9099ebb4caSwyllys goto out;
9199ebb4caSwyllys }
9299ebb4caSwyllys
9399ebb4caSwyllys if (filename == NULL) {
9499ebb4caSwyllys filename = strdup(KMF_DEFAULT_POLICY_FILE);
9599ebb4caSwyllys if (filename == NULL) {
9699ebb4caSwyllys rv = KC_ERR_MEMORY;
9799ebb4caSwyllys goto out;
9899ebb4caSwyllys }
9999ebb4caSwyllys }
10099ebb4caSwyllys
10199ebb4caSwyllys if (policyname == NULL) {
10299ebb4caSwyllys (void) fprintf(stderr,
10399ebb4caSwyllys gettext("You must specify a policy name\n"));
10499ebb4caSwyllys rv = KC_ERR_USAGE;
10599ebb4caSwyllys goto out;
10699ebb4caSwyllys }
10799ebb4caSwyllys
10899ebb4caSwyllys if (outfile == NULL) {
10999ebb4caSwyllys (void) fprintf(stderr,
11099ebb4caSwyllys gettext("You must specify a output DB file\n"));
11199ebb4caSwyllys rv = KC_ERR_USAGE;
11299ebb4caSwyllys goto out;
11399ebb4caSwyllys }
11499ebb4caSwyllys
11599ebb4caSwyllys if (strcmp(outfile, KMF_DEFAULT_POLICY_FILE) == 0 &&
11699ebb4caSwyllys strcmp(policyname, KMF_DEFAULT_POLICY_NAME) == 0) {
11799ebb4caSwyllys (void) fprintf(stderr,
11899ebb4caSwyllys gettext("Can not export the default policy record to "
11999ebb4caSwyllys "the system default policy database\n"));
12099ebb4caSwyllys rv = KC_ERR_USAGE;
12199ebb4caSwyllys goto out;
12299ebb4caSwyllys }
12399ebb4caSwyllys
12499ebb4caSwyllys rv = load_policies(filename, &plclist);
12599ebb4caSwyllys if (rv != KMF_OK)
12699ebb4caSwyllys goto out;
12799ebb4caSwyllys
12899ebb4caSwyllys pnode = plclist;
12999ebb4caSwyllys while (pnode != NULL && !found) {
13099ebb4caSwyllys if (strcmp(policyname, pnode->plc.name) == 0) {
13199ebb4caSwyllys KMF_RETURN ret;
13299ebb4caSwyllys
13399ebb4caSwyllys found++;
134*30a5e8faSwyllys ret = kmf_verify_policy(&pnode->plc);
13599ebb4caSwyllys if (ret != KMF_OK) {
13699ebb4caSwyllys print_sanity_error(ret);
13799ebb4caSwyllys rv = KC_ERR_VERIFY_POLICY;
13899ebb4caSwyllys break;
13999ebb4caSwyllys }
140*30a5e8faSwyllys rv = kmf_add_policy_to_db(&pnode->plc, outfile,
141*30a5e8faSwyllys B_FALSE);
14299ebb4caSwyllys }
14399ebb4caSwyllys pnode = pnode->next;
14499ebb4caSwyllys }
14599ebb4caSwyllys
14699ebb4caSwyllys if (!found) {
14799ebb4caSwyllys (void) fprintf(stderr,
14899ebb4caSwyllys gettext("Could not find policy \"%s\" in %s\n"),
14999ebb4caSwyllys policyname, filename);
15099ebb4caSwyllys rv = KC_ERR_FIND_POLICY;
15199ebb4caSwyllys }
15299ebb4caSwyllys
15399ebb4caSwyllys out:
15499ebb4caSwyllys if (filename != NULL)
15599ebb4caSwyllys free(filename);
15699ebb4caSwyllys
15799ebb4caSwyllys if (policyname != NULL)
15899ebb4caSwyllys free(policyname);
15999ebb4caSwyllys
16099ebb4caSwyllys if (outfile != NULL)
16199ebb4caSwyllys free(outfile);
16299ebb4caSwyllys
16399ebb4caSwyllys free_policy_list(plclist);
16499ebb4caSwyllys
16599ebb4caSwyllys return (rv);
16699ebb4caSwyllys }
167