xref: /freebsd/crypto/heimdal/admin/copy.c (revision b528cefc6b8f9670b31a865051741d946cb37085)
1 /*
2  * Copyright (c) 1997 - 2000 Kungliga Tekniska H�gskolan
3  * (Royal Institute of Technology, Stockholm, Sweden).
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  *
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * 3. Neither the name of the Institute nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  */
33 
34 #include "ktutil_locl.h"
35 
36 RCSID("$Id: copy.c,v 1.1 2000/01/02 04:41:01 assar Exp $");
37 
38 int
39 kt_copy (int argc, char **argv)
40 {
41     krb5_error_code ret;
42     int help_flag = 0;
43     int optind = 0;
44     krb5_keytab src_keytab, dst_keytab;
45     krb5_kt_cursor cursor;
46     krb5_keytab_entry entry;
47 
48     struct getargs args[] = {
49 	{ "help", 'h', arg_flag, NULL}
50     };
51 
52     int num_args = sizeof(args) / sizeof(args[0]);
53     int i = 0;
54 
55     args[i++].value = &help_flag;
56 
57     if(getarg(args, num_args, argc, argv, &optind)) {
58 	arg_printusage(args, num_args, "ktutil copy",
59 		       "keytab-src keytab-dest");
60 	return 0;
61     }
62     if (help_flag) {
63 	arg_printusage(args, num_args, "ktutil copy",
64 		       "keytab-src keytab-dest");
65 	return 0;
66     }
67 
68     argv += optind;
69     argc -= optind;
70 
71     if (argc != 2) {
72 	arg_printusage(args, num_args, "ktutil copy",
73 		       "keytab-src keytab-dest");
74 	return 0;
75     }
76 
77     ret = krb5_kt_resolve (context, argv[0], &src_keytab);
78     if (ret) {
79 	krb5_warn (context, ret, "resolving src keytab `%s'", argv[0]);
80 	return 0;
81     }
82 
83     ret = krb5_kt_resolve (context, argv[1], &dst_keytab);
84     if (ret) {
85 	krb5_kt_close (context, src_keytab);
86 	krb5_warn (context, ret, "resolving dst keytab `%s'", argv[1]);
87 	return 0;
88     }
89 
90     ret = krb5_kt_start_seq_get (context, src_keytab, &cursor);
91     if (ret) {
92 	krb5_warn (context, ret, "krb5_kt_start_seq_get");
93 	goto fail;
94     }
95 
96     while((ret = krb5_kt_next_entry(context, src_keytab,
97 				    &entry, &cursor)) == 0) {
98 	ret = krb5_kt_add_entry (context, dst_keytab, &entry);
99 	if (verbose_flag) {
100 	    char *name_str;
101 
102 	    krb5_unparse_name (context, entry.principal, &name_str);
103 	    printf ("copying %s\n", name_str);
104 	    free (name_str);
105 	}
106 
107 	krb5_kt_free_entry (context, &entry);
108 	if (ret) {
109 	    krb5_warn (context, ret, "krb5_kt_add_entry");
110 	    break;
111 	}
112     }
113     krb5_kt_end_seq_get (context, src_keytab, &cursor);
114 
115 fail:
116     krb5_kt_close (context, src_keytab);
117     krb5_kt_close (context, dst_keytab);
118     return 0;
119 }
120