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