2 * Copyright (c) 1997 - 2002 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"
38 /* afs keyfile operations --------------------------------------- */
41 * Minimum tools to handle the AFS KeyFile.
43 * Format of the KeyFile is:
44 * <int32_t numkeys> {[<int32_t kvno> <char[8] deskey>] * numkeys}
46 * It just adds to the end of the keyfile, deleting isn't implemented.
47 * Use your favorite text/hex editor to delete keys.
51 #define AFS_SERVERTHISCELL "/usr/afs/etc/ThisCell"
52 #define AFS_SERVERMAGICKRBCONF "/usr/afs/etc/krb.conf"
62 * set `d->cell' and `d->realm'
66 get_cell_and_realm (krb5_context context
,
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
) {
98 krb5_set_error_string (context
, "no realm in %s",
99 AFS_SERVERMAGICKRBCONF
);
102 buf
[strcspn(buf
, "\n")] = '\0';
106 for (cp
= buf
; *cp
!= '\0'; cp
++)
109 d
->realm
= strdup (buf
);
110 if (d
->realm
== NULL
) {
112 krb5_set_error_string (context
, "malloc: out of memory");
119 * init and get filename
122 static krb5_error_code
123 akf_resolve(krb5_context context
, const char *name
, krb5_keytab id
)
126 struct akf_data
*d
= malloc(sizeof (struct akf_data
));
129 krb5_set_error_string (context
, "malloc: out of memory");
134 ret
= get_cell_and_realm (context
, d
);
139 d
->filename
= strdup (name
);
140 if (d
->filename
== NULL
) {
144 krb5_set_error_string (context
, "malloc: out of memory");
156 static krb5_error_code
157 akf_close(krb5_context context
, krb5_keytab id
)
159 struct akf_data
*d
= id
->data
;
171 static krb5_error_code
172 akf_get_name(krb5_context context
,
177 struct akf_data
*d
= id
->data
;
179 strlcpy (name
, d
->filename
, name_sz
);
187 static krb5_error_code
188 akf_start_seq_get(krb5_context context
,
193 struct akf_data
*d
= id
->data
;
195 c
->fd
= open (d
->filename
, O_RDONLY
|O_BINARY
, 0600);
198 krb5_set_error_string(context
, "open(%s): %s", d
->filename
,
203 c
->sp
= krb5_storage_from_fd(c
->fd
);
204 ret
= krb5_ret_int32(c
->sp
, &d
->num_entries
);
206 krb5_storage_free(c
->sp
);
208 krb5_clear_error_string (context
);
209 if(ret
== KRB5_KT_END
)
210 return KRB5_KT_NOTFOUND
;
217 static krb5_error_code
218 akf_next_entry(krb5_context context
,
220 krb5_keytab_entry
*entry
,
221 krb5_kt_cursor
*cursor
)
223 struct akf_data
*d
= id
->data
;
228 pos
= krb5_storage_seek(cursor
->sp
, 0, SEEK_CUR
);
230 if ((pos
- 4) / (4 + 8) >= d
->num_entries
)
233 ret
= krb5_make_principal (context
, &entry
->principal
,
234 d
->realm
, "afs", d
->cell
, NULL
);
238 ret
= krb5_ret_int32(cursor
->sp
, &kvno
);
240 krb5_free_principal (context
, entry
->principal
);
246 entry
->keyblock
.keytype
= ETYPE_DES_CBC_MD5
;
247 entry
->keyblock
.keyvalue
.length
= 8;
248 entry
->keyblock
.keyvalue
.data
= malloc (8);
249 if (entry
->keyblock
.keyvalue
.data
== NULL
) {
250 krb5_free_principal (context
, entry
->principal
);
251 krb5_set_error_string (context
, "malloc: out of memory");
256 ret
= krb5_storage_read(cursor
->sp
, entry
->keyblock
.keyvalue
.data
, 8);
258 ret
= (ret
< 0) ? errno
: KRB5_KT_END
;
262 entry
->timestamp
= time(NULL
);
265 krb5_storage_seek(cursor
->sp
, pos
+ 4 + 8, SEEK_SET
);
269 static krb5_error_code
270 akf_end_seq_get(krb5_context context
,
272 krb5_kt_cursor
*cursor
)
274 krb5_storage_free(cursor
->sp
);
279 static krb5_error_code
280 akf_add_entry(krb5_context context
,
282 krb5_keytab_entry
*entry
)
284 struct akf_data
*d
= id
->data
;
291 if (entry
->keyblock
.keyvalue
.length
!= 8
292 || entry
->keyblock
.keytype
!= ETYPE_DES_CBC_MD5
)
295 fd
= open (d
->filename
, O_RDWR
| O_BINARY
);
297 fd
= open (d
->filename
,
298 O_RDWR
| O_BINARY
| O_CREAT
| O_EXCL
, 0600);
301 krb5_set_error_string(context
, "open(%s): %s", d
->filename
,
308 sp
= krb5_storage_from_fd(fd
);
311 krb5_set_error_string (context
, "malloc: out of memory");
317 if(krb5_storage_seek(sp
, 0, SEEK_SET
) < 0) {
319 krb5_storage_free(sp
);
321 krb5_set_error_string (context
, "seek: %s", strerror(ret
));
325 ret
= krb5_ret_int32(sp
, &len
);
327 krb5_storage_free(sp
);
334 if(krb5_storage_seek(sp
, 0, SEEK_SET
) < 0) {
336 krb5_storage_free(sp
);
338 krb5_set_error_string (context
, "seek: %s", strerror(ret
));
342 ret
= krb5_store_int32(sp
, len
);
344 krb5_storage_free(sp
);
350 if(krb5_storage_seek(sp
, (len
- 1) * (8 + 4), SEEK_CUR
) < 0) {
352 krb5_storage_free(sp
);
354 krb5_set_error_string (context
, "seek: %s", strerror(ret
));
358 ret
= krb5_store_int32(sp
, entry
->vno
);
360 krb5_storage_free(sp
);
364 ret
= krb5_storage_write(sp
, entry
->keyblock
.keyvalue
.data
,
365 entry
->keyblock
.keyvalue
.length
);
366 if(ret
!= entry
->keyblock
.keyvalue
.length
) {
367 krb5_storage_free(sp
);
373 krb5_storage_free(sp
);
378 const krb5_kt_ops krb5_akf_ops
= {