Fix unqualified "net join"
[Samba.git] / source4 / auth / kerberos / keytab_copy.c
blobba4ea2bf39ec11dce3b331e8af1eac0a9edc0b9f
1 /*
2 * Copyright (c) 1997-2004 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
4 * All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
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.
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.
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.
34 #include "includes.h"
35 #include "system/kerberos.h"
36 #include "auth/kerberos/kerberos.h"
38 static const krb5_boolean verbose_flag = FALSE;
40 static krb5_boolean
41 compare_keyblock(const krb5_keyblock *a, const krb5_keyblock *b)
43 if(a->keytype != b->keytype ||
44 a->keyvalue.length != b->keyvalue.length ||
45 memcmp(a->keyvalue.data, b->keyvalue.data, a->keyvalue.length) != 0)
46 return FALSE;
47 return TRUE;
50 krb5_error_code kt_copy (krb5_context context, const char *from, const char *to)
52 krb5_error_code ret;
53 krb5_keytab src_keytab, dst_keytab;
54 krb5_kt_cursor cursor;
55 krb5_keytab_entry entry, dummy;
57 ret = krb5_kt_resolve (context, from, &src_keytab);
58 if (ret) {
59 krb5_warn (context, ret, "resolving src keytab `%s'", from);
60 return 1;
63 ret = krb5_kt_resolve (context, to, &dst_keytab);
64 if (ret) {
65 krb5_kt_close (context, src_keytab);
66 krb5_warn (context, ret, "resolving dst keytab `%s'", to);
67 return 1;
70 ret = krb5_kt_start_seq_get (context, src_keytab, &cursor);
71 if (ret) {
72 krb5_warn (context, ret, "krb5_kt_start_seq_get %s", from);
73 goto out;
76 if (verbose_flag)
77 fprintf(stderr, "copying %s to %s\n", from, to);
79 while((ret = krb5_kt_next_entry(context, src_keytab,
80 &entry, &cursor)) == 0) {
81 char *name_str;
82 char *etype_str;
83 ret = krb5_unparse_name (context, entry.principal, &name_str);
84 if(ret) {
85 krb5_warn(context, ret, "krb5_unparse_name");
86 name_str = NULL; /* XXX */
88 ret = krb5_enctype_to_string(context, entry.keyblock.keytype, &etype_str);
89 if(ret) {
90 krb5_warn(context, ret, "krb5_enctype_to_string");
91 etype_str = NULL; /* XXX */
93 ret = krb5_kt_get_entry(context, dst_keytab,
94 entry.principal,
95 entry.vno,
96 entry.keyblock.keytype,
97 &dummy);
98 if(ret == 0) {
99 /* this entry is already in the new keytab, so no need to
100 copy it; if the keyblocks are not the same, something
101 is weird, so complain about that */
102 if(!compare_keyblock(&entry.keyblock, &dummy.keyblock)) {
103 krb5_warnx(context, "entry with different keyvalue "
104 "already exists for %s, keytype %s, kvno %d",
105 name_str, etype_str, entry.vno);
107 krb5_kt_free_entry(context, &dummy);
108 krb5_kt_free_entry (context, &entry);
109 free(name_str);
110 free(etype_str);
111 continue;
112 } else if(ret != KRB5_KT_NOTFOUND) {
113 krb5_warn (context, ret, "%s: fetching %s/%s/%u",
114 to, name_str, etype_str, entry.vno);
115 krb5_kt_free_entry (context, &entry);
116 free(name_str);
117 free(etype_str);
118 break;
120 if (verbose_flag)
121 fprintf (stderr, "copying %s, keytype %s, kvno %d\n", name_str,
122 etype_str, entry.vno);
123 ret = krb5_kt_add_entry (context, dst_keytab, &entry);
124 krb5_kt_free_entry (context, &entry);
125 if (ret) {
126 krb5_warn (context, ret, "%s: adding %s/%s/%u",
127 to, name_str, etype_str, entry.vno);
128 free(name_str);
129 free(etype_str);
130 break;
132 free(name_str);
133 free(etype_str);
135 krb5_kt_end_seq_get (context, src_keytab, &cursor);
137 out:
138 krb5_kt_close (context, src_keytab);
139 krb5_kt_close (context, dst_keytab);
140 if (ret == KRB5_KT_END) {
141 return 0;
142 } else if (ret == 0) {
143 return EINVAL;
145 return ret;