2 * Copyright (c) 1997 - 2007 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"
36 __RCSID("$Heimdal: keytab_keyfile.c 20695 2007-05-30 14:09:09Z lha $"
39 /* afs keyfile operations --------------------------------------- */
42 * Minimum tools to handle the AFS KeyFile.
44 * Format of the KeyFile is:
45 * <int32_t numkeys> {[<int32_t kvno> <char[8] deskey>] * numkeys}
47 * It just adds to the end of the keyfile, deleting isn't implemented.
48 * Use your favorite text/hex editor to delete keys.
52 #define AFS_SERVERTHISCELL "/usr/afs/etc/ThisCell"
53 #define AFS_SERVERMAGICKRBCONF "/usr/afs/etc/krb.conf"
63 * set `d->cell' and `d->realm'
67 get_cell_and_realm (krb5_context context
, struct akf_data
*d
)
70 char buf
[BUFSIZ
], *cp
;
73 f
= fopen (AFS_SERVERTHISCELL
, "r");
76 krb5_set_error_string (context
, "open %s: %s", AFS_SERVERTHISCELL
,
80 if (fgets (buf
, sizeof(buf
), f
) == NULL
) {
82 krb5_set_error_string (context
, "no cell in %s", AFS_SERVERTHISCELL
);
85 buf
[strcspn(buf
, "\n")] = '\0';
88 d
->cell
= strdup (buf
);
89 if (d
->cell
== NULL
) {
90 krb5_set_error_string (context
, "malloc: out of memory");
94 f
= fopen (AFS_SERVERMAGICKRBCONF
, "r");
96 if (fgets (buf
, sizeof(buf
), f
) == NULL
) {
100 krb5_set_error_string (context
, "no realm in %s",
101 AFS_SERVERMAGICKRBCONF
);
104 buf
[strcspn(buf
, "\n")] = '\0';
108 for (cp
= buf
; *cp
!= '\0'; cp
++)
109 *cp
= toupper((unsigned char)*cp
);
111 d
->realm
= strdup (buf
);
112 if (d
->realm
== NULL
) {
115 krb5_set_error_string (context
, "malloc: out of memory");
122 * init and get filename
125 static krb5_error_code
126 akf_resolve(krb5_context context
, const char *name
, krb5_keytab id
)
129 struct akf_data
*d
= malloc(sizeof (struct akf_data
));
132 krb5_set_error_string (context
, "malloc: out of memory");
137 ret
= get_cell_and_realm (context
, d
);
142 d
->filename
= strdup (name
);
143 if (d
->filename
== NULL
) {
147 krb5_set_error_string (context
, "malloc: out of memory");
159 static krb5_error_code
160 akf_close(krb5_context context
, krb5_keytab id
)
162 struct akf_data
*d
= id
->data
;
174 static krb5_error_code
175 akf_get_name(krb5_context context
,
180 struct akf_data
*d
= id
->data
;
182 strlcpy (name
, d
->filename
, name_sz
);
190 static krb5_error_code
191 akf_start_seq_get(krb5_context context
,
196 struct akf_data
*d
= id
->data
;
198 c
->fd
= open (d
->filename
, O_RDONLY
|O_BINARY
, 0600);
201 krb5_set_error_string(context
, "open(%s): %s", d
->filename
,
206 c
->sp
= krb5_storage_from_fd(c
->fd
);
207 ret
= krb5_ret_int32(c
->sp
, &d
->num_entries
);
209 krb5_storage_free(c
->sp
);
211 krb5_clear_error_string (context
);
212 if(ret
== KRB5_KT_END
)
213 return KRB5_KT_NOTFOUND
;
220 static krb5_error_code
221 akf_next_entry(krb5_context context
,
223 krb5_keytab_entry
*entry
,
224 krb5_kt_cursor
*cursor
)
226 struct akf_data
*d
= id
->data
;
231 pos
= krb5_storage_seek(cursor
->sp
, 0, SEEK_CUR
);
233 if ((pos
- 4) / (4 + 8) >= d
->num_entries
)
236 ret
= krb5_make_principal (context
, &entry
->principal
,
237 d
->realm
, "afs", d
->cell
, NULL
);
241 ret
= krb5_ret_int32(cursor
->sp
, &kvno
);
243 krb5_free_principal (context
, entry
->principal
);
249 entry
->keyblock
.keytype
= ETYPE_DES_CBC_MD5
;
250 entry
->keyblock
.keyvalue
.length
= 8;
251 entry
->keyblock
.keyvalue
.data
= malloc (8);
252 if (entry
->keyblock
.keyvalue
.data
== NULL
) {
253 krb5_free_principal (context
, entry
->principal
);
254 krb5_set_error_string (context
, "malloc: out of memory");
259 ret
= krb5_storage_read(cursor
->sp
, entry
->keyblock
.keyvalue
.data
, 8);
261 ret
= (ret
< 0) ? errno
: KRB5_KT_END
;
265 entry
->timestamp
= time(NULL
);
268 krb5_storage_seek(cursor
->sp
, pos
+ 4 + 8, SEEK_SET
);
272 static krb5_error_code
273 akf_end_seq_get(krb5_context context
,
275 krb5_kt_cursor
*cursor
)
277 krb5_storage_free(cursor
->sp
);
282 static krb5_error_code
283 akf_add_entry(krb5_context context
,
285 krb5_keytab_entry
*entry
)
287 struct akf_data
*d
= id
->data
;
294 if (entry
->keyblock
.keyvalue
.length
!= 8)
296 switch(entry
->keyblock
.keytype
) {
297 case ETYPE_DES_CBC_CRC
:
298 case ETYPE_DES_CBC_MD4
:
299 case ETYPE_DES_CBC_MD5
:
305 fd
= open (d
->filename
, O_RDWR
| O_BINARY
);
307 fd
= open (d
->filename
,
308 O_RDWR
| O_BINARY
| O_CREAT
| O_EXCL
, 0600);
311 krb5_set_error_string(context
, "open(%s): %s", d
->filename
,
318 sp
= krb5_storage_from_fd(fd
);
321 krb5_set_error_string (context
, "malloc: out of memory");
327 if(krb5_storage_seek(sp
, 0, SEEK_SET
) < 0) {
329 krb5_storage_free(sp
);
331 krb5_set_error_string (context
, "seek: %s", strerror(ret
));
335 ret
= krb5_ret_int32(sp
, &len
);
337 krb5_storage_free(sp
);
344 * Make sure we don't add the entry twice, assumes the DES
345 * encryption types are all the same key.
351 for (i
= 0; i
< len
; i
++) {
352 ret
= krb5_ret_int32(sp
, &kvno
);
354 krb5_set_error_string (context
, "Failed to get kvno ");
357 if(krb5_storage_seek(sp
, 8, SEEK_CUR
) < 0) {
358 krb5_set_error_string (context
, "seek: %s", strerror(ret
));
361 if (kvno
== entry
->vno
) {
370 if(krb5_storage_seek(sp
, 0, SEEK_SET
) < 0) {
372 krb5_set_error_string (context
, "seek: %s", strerror(ret
));
376 ret
= krb5_store_int32(sp
, len
);
378 krb5_set_error_string(context
, "keytab keyfile failed new length");
382 if(krb5_storage_seek(sp
, (len
- 1) * (8 + 4), SEEK_CUR
) < 0) {
384 krb5_set_error_string (context
, "seek to end: %s", strerror(ret
));
388 ret
= krb5_store_int32(sp
, entry
->vno
);
390 krb5_set_error_string(context
, "keytab keyfile failed store kvno");
393 ret
= krb5_storage_write(sp
, entry
->keyblock
.keyvalue
.data
,
394 entry
->keyblock
.keyvalue
.length
);
395 if(ret
!= entry
->keyblock
.keyvalue
.length
) {
400 krb5_set_error_string(context
, "keytab keyfile failed to add key");
405 krb5_storage_free(sp
);
410 const krb5_kt_ops krb5_akf_ops
= {