2 * Copyright (c) 1997, 1998, 1999 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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
34 #include "krb5_locl.h"
37 RCSID("$Id: keytab_krb4.c,v 1.5 2000/01/06 08:04:58 assar Exp $");
43 static krb5_error_code
44 krb4_kt_resolve(krb5_context context, const char *name, krb5_keytab id)
46 struct krb4_kt_data *d;
48 d = malloc (sizeof(*d));
51 d->filename = strdup (name);
52 if (d->filename == NULL) {
60 static krb5_error_code
61 krb4_kt_get_name (krb5_context context,
66 struct krb4_kt_data *d = id->data;
68 strlcpy (name, d->filename, name_sz);
72 static krb5_error_code
73 krb4_kt_close (krb5_context context,
76 struct krb4_kt_data *d = id->data;
83 struct krb4_cursor_extra_data {
84 krb5_keytab_entry entry;
88 static krb5_error_code
89 krb4_kt_start_seq_get_int (krb5_context context,
94 struct krb4_kt_data *d = id->data;
95 struct krb4_cursor_extra_data *ed;
97 ed = malloc (sizeof(*ed));
100 ed->entry.principal = NULL;
103 c->fd = open (d->filename, flags);
108 c->sp = krb5_storage_from_fd(c->fd);
112 static krb5_error_code
113 krb4_kt_start_seq_get (krb5_context context,
117 return krb4_kt_start_seq_get_int (context, id, O_BINARY | O_RDONLY, c);
120 static krb5_error_code
121 read_v4_entry (krb5_context context,
122 struct krb4_kt_data *d,
124 struct krb4_cursor_extra_data *ed)
127 char *service, *instance, *realm;
131 ret = krb5_ret_stringz(c->sp, &service);
134 ret = krb5_ret_stringz(c->sp, &instance);
139 ret = krb5_ret_stringz(c->sp, &realm);
145 ret = krb5_425_conv_principal (context, service, instance, realm,
146 &ed->entry.principal);
152 ret = krb5_ret_int8(c->sp, &kvno);
154 krb5_free_principal (context, ed->entry.principal);
157 ret = c->sp->fetch(c->sp, key, 8);
159 krb5_free_principal(context, ed->entry.principal);
163 krb5_free_principal(context, ed->entry.principal);
166 ed->entry.vno = kvno;
167 ret = krb5_data_copy (&ed->entry.keyblock.keyvalue,
171 ed->entry.timestamp = time(NULL);
176 static krb5_error_code
177 krb4_kt_next_entry (krb5_context context,
179 krb5_keytab_entry *entry,
183 struct krb4_kt_data *d = id->data;
184 struct krb4_cursor_extra_data *ed = c->data;
185 const krb5_enctype keytypes[] = {ETYPE_DES_CBC_MD5,
190 ret = read_v4_entry (context, d, c, ed);
194 ret = krb5_kt_copy_entry_contents (context,
199 entry->keyblock.keytype = keytypes[ed->num];
200 if (++ed->num == 3) {
201 krb5_kt_free_entry (context, &ed->entry);
207 static krb5_error_code
208 krb4_kt_end_seq_get (krb5_context context,
212 struct krb4_cursor_extra_data *ed = c->data;
214 krb5_storage_free (c->sp);
216 krb5_kt_free_entry (context, &ed->entry);
222 static krb5_error_code
223 krb4_kt_add_entry (krb5_context context,
225 krb5_keytab_entry *entry)
227 struct krb4_kt_data *d = id->data;
230 char service[ANAME_SZ];
231 char instance[INST_SZ];
232 char realm[REALM_SZ];
235 fd = open (d->filename, O_WRONLY | O_APPEND | O_BINARY);
237 fd = open (d->filename,
238 O_WRONLY | O_APPEND | O_BINARY | O_CREAT, 0600);
242 ret = krb5_524_conv_principal (context, entry->principal,
243 service, instance, realm);
248 if (entry->keyblock.keyvalue.length == 8
249 && entry->keyblock.keytype == ETYPE_DES_CBC_MD5) {
250 write(fd, service, strlen(service)+1);
251 write(fd, instance, strlen(instance)+1);
252 write(fd, realm, strlen(realm)+1);
254 write(fd, &kvno, sizeof(kvno));
255 write(fd, entry->keyblock.keyvalue.data, 8);
261 krb5_kt_ops krb4_fkt_ops = {
267 krb4_kt_start_seq_get,
270 krb4_kt_add_entry, /* add_entry */
271 NULL /* remove_entry */