1 /* $NetBSD: keytab_keyfile.c,v 1.1.1.2 2014/04/24 12:45:50 pettai Exp $ */
4 * Copyright (c) 1997 - 2007 Kungliga Tekniska Högskolan
5 * (Royal Institute of Technology, Stockholm, Sweden).
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * 3. Neither the name of the Institute nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 #include "krb5_locl.h"
38 #ifndef HEIMDAL_SMALLER
40 /* afs keyfile operations --------------------------------------- */
43 * Minimum tools to handle the AFS KeyFile.
45 * Format of the KeyFile is:
46 * <int32_t numkeys> {[<int32_t kvno> <char[8] deskey>] * numkeys}
48 * It just adds to the end of the keyfile, deleting isn't implemented.
49 * Use your favorite text/hex editor to delete keys.
53 #define AFS_SERVERTHISCELL "/usr/afs/etc/ThisCell"
54 #define AFS_SERVERMAGICKRBCONF "/usr/afs/etc/krb.conf"
64 * set `d->cell' and `d->realm'
68 get_cell_and_realm (krb5_context context
, struct akf_data
*d
)
71 char buf
[BUFSIZ
], *cp
;
74 f
= fopen (AFS_SERVERTHISCELL
, "r");
77 krb5_set_error_message (context
, ret
,
78 N_("Open ThisCell %s: %s", ""),
83 if (fgets (buf
, sizeof(buf
), f
) == NULL
) {
85 krb5_set_error_message (context
, EINVAL
,
86 N_("No cell in ThisCell file %s", ""),
90 buf
[strcspn(buf
, "\n")] = '\0';
93 d
->cell
= strdup (buf
);
94 if (d
->cell
== NULL
) {
95 krb5_set_error_message(context
, ENOMEM
,
96 N_("malloc: out of memory", ""));
100 f
= fopen (AFS_SERVERMAGICKRBCONF
, "r");
102 if (fgets (buf
, sizeof(buf
), f
) == NULL
) {
106 krb5_set_error_message (context
, EINVAL
,
107 N_("No realm in ThisCell file %s", ""),
108 AFS_SERVERMAGICKRBCONF
);
111 buf
[strcspn(buf
, "\n")] = '\0';
115 for (cp
= buf
; *cp
!= '\0'; cp
++)
116 *cp
= toupper((unsigned char)*cp
);
118 d
->realm
= strdup (buf
);
119 if (d
->realm
== NULL
) {
122 krb5_set_error_message(context
, ENOMEM
,
123 N_("malloc: out of memory", ""));
130 * init and get filename
133 static krb5_error_code KRB5_CALLCONV
134 akf_resolve(krb5_context context
, const char *name
, krb5_keytab id
)
137 struct akf_data
*d
= malloc(sizeof (struct akf_data
));
140 krb5_set_error_message(context
, ENOMEM
,
141 N_("malloc: out of memory", ""));
146 ret
= get_cell_and_realm (context
, d
);
151 d
->filename
= strdup (name
);
152 if (d
->filename
== NULL
) {
156 krb5_set_error_message(context
, ENOMEM
,
157 N_("malloc: out of memory", ""));
169 static krb5_error_code KRB5_CALLCONV
170 akf_close(krb5_context context
, krb5_keytab id
)
172 struct akf_data
*d
= id
->data
;
184 static krb5_error_code KRB5_CALLCONV
185 akf_get_name(krb5_context context
,
190 struct akf_data
*d
= id
->data
;
192 strlcpy (name
, d
->filename
, name_sz
);
200 static krb5_error_code KRB5_CALLCONV
201 akf_start_seq_get(krb5_context context
,
206 struct akf_data
*d
= id
->data
;
208 c
->fd
= open (d
->filename
, O_RDONLY
| O_BINARY
| O_CLOEXEC
, 0600);
211 krb5_set_error_message(context
, ret
,
212 N_("keytab afs keyfile open %s failed: %s", ""),
213 d
->filename
, strerror(ret
));
218 c
->sp
= krb5_storage_from_fd(c
->fd
);
221 krb5_clear_error_message (context
);
222 return KRB5_KT_NOTFOUND
;
224 krb5_storage_set_eof_code(c
->sp
, KRB5_KT_END
);
226 ret
= krb5_ret_uint32(c
->sp
, &d
->num_entries
);
227 if(ret
|| d
->num_entries
> INT_MAX
/ 8) {
228 krb5_storage_free(c
->sp
);
230 krb5_clear_error_message (context
);
231 if(ret
== KRB5_KT_END
)
232 return KRB5_KT_NOTFOUND
;
239 static krb5_error_code KRB5_CALLCONV
240 akf_next_entry(krb5_context context
,
242 krb5_keytab_entry
*entry
,
243 krb5_kt_cursor
*cursor
)
245 struct akf_data
*d
= id
->data
;
250 pos
= krb5_storage_seek(cursor
->sp
, 0, SEEK_CUR
);
252 if ((pos
- 4) / (4 + 8) >= d
->num_entries
)
255 ret
= krb5_make_principal (context
, &entry
->principal
,
256 d
->realm
, "afs", d
->cell
, NULL
);
260 ret
= krb5_ret_int32(cursor
->sp
, &kvno
);
262 krb5_free_principal (context
, entry
->principal
);
269 entry
->keyblock
.keytype
= ETYPE_DES_CBC_MD5
;
271 entry
->keyblock
.keytype
= ETYPE_DES_CBC_CRC
;
272 entry
->keyblock
.keyvalue
.length
= 8;
273 entry
->keyblock
.keyvalue
.data
= malloc (8);
274 if (entry
->keyblock
.keyvalue
.data
== NULL
) {
275 krb5_free_principal (context
, entry
->principal
);
276 krb5_set_error_message(context
, ENOMEM
,
277 N_("malloc: out of memory", ""));
282 ret
= krb5_storage_read(cursor
->sp
, entry
->keyblock
.keyvalue
.data
, 8);
284 ret
= (ret
< 0) ? errno
: KRB5_KT_END
;
288 entry
->timestamp
= time(NULL
);
290 entry
->aliases
= NULL
;
294 krb5_storage_seek(cursor
->sp
, pos
+ 4 + 8, SEEK_SET
);
297 cursor
->data
= cursor
;
301 static krb5_error_code KRB5_CALLCONV
302 akf_end_seq_get(krb5_context context
,
304 krb5_kt_cursor
*cursor
)
306 krb5_storage_free(cursor
->sp
);
312 static krb5_error_code KRB5_CALLCONV
313 akf_add_entry(krb5_context context
,
315 krb5_keytab_entry
*entry
)
317 struct akf_data
*d
= id
->data
;
324 if (entry
->keyblock
.keyvalue
.length
!= 8)
326 switch(entry
->keyblock
.keytype
) {
327 case ETYPE_DES_CBC_CRC
:
328 case ETYPE_DES_CBC_MD4
:
329 case ETYPE_DES_CBC_MD5
:
335 fd
= open (d
->filename
, O_RDWR
| O_BINARY
| O_CLOEXEC
);
337 fd
= open (d
->filename
,
338 O_RDWR
| O_BINARY
| O_CREAT
| O_EXCL
| O_CLOEXEC
, 0600);
341 krb5_set_error_message(context
, ret
,
342 N_("open keyfile(%s): %s", ""),
350 sp
= krb5_storage_from_fd(fd
);
353 krb5_set_error_message(context
, ENOMEM
,
354 N_("malloc: out of memory", ""));
360 if(krb5_storage_seek(sp
, 0, SEEK_SET
) < 0) {
362 krb5_storage_free(sp
);
364 krb5_set_error_message(context
, ret
,
365 N_("seeking in keyfile: %s", ""),
370 ret
= krb5_ret_int32(sp
, &len
);
372 krb5_storage_free(sp
);
379 * Make sure we don't add the entry twice, assumes the DES
380 * encryption types are all the same key.
386 for (i
= 0; i
< len
; i
++) {
387 ret
= krb5_ret_int32(sp
, &kvno
);
389 krb5_set_error_message (context
, ret
,
390 N_("Failed getting kvno from keyfile", ""));
393 if(krb5_storage_seek(sp
, 8, SEEK_CUR
) < 0) {
395 krb5_set_error_message (context
, ret
,
396 N_("Failed seeing in keyfile: %s", ""),
400 if (kvno
== entry
->vno
) {
409 if(krb5_storage_seek(sp
, 0, SEEK_SET
) < 0) {
411 krb5_set_error_message (context
, ret
,
412 N_("Failed seeing in keyfile: %s", ""),
417 ret
= krb5_store_int32(sp
, len
);
420 krb5_set_error_message (context
, ret
,
421 N_("keytab keyfile failed new length", ""));
425 if(krb5_storage_seek(sp
, (len
- 1) * (8 + 4), SEEK_CUR
) < 0) {
427 krb5_set_error_message (context
, ret
,
428 N_("seek to end: %s", ""), strerror(ret
));
432 ret
= krb5_store_int32(sp
, entry
->vno
);
434 krb5_set_error_message(context
, ret
,
435 N_("keytab keyfile failed store kvno", ""));
438 ret
= krb5_storage_write(sp
, entry
->keyblock
.keyvalue
.data
,
439 entry
->keyblock
.keyvalue
.length
);
440 if(ret
!= entry
->keyblock
.keyvalue
.length
) {
445 krb5_set_error_message(context
, ret
,
446 N_("keytab keyfile failed to add key", ""));
451 krb5_storage_free(sp
);
456 const krb5_kt_ops krb5_akf_ops
= {
470 #endif /* HEIMDAL_SMALLER */