2 * Copyright (c) 1997-2001 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 struct krb5_rcache_data
{
41 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
42 krb5_rc_resolve(krb5_context context
,
46 id
->name
= strdup(name
);
47 if(id
->name
== NULL
) {
48 krb5_set_error_message(context
, KRB5_RC_MALLOC
,
49 N_("malloc: out of memory", ""));
50 return KRB5_RC_MALLOC
;
55 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
56 krb5_rc_resolve_type(krb5_context context
,
61 if(strcmp(type
, "FILE")) {
62 krb5_set_error_message (context
, KRB5_RC_TYPE_NOTFOUND
,
63 N_("replay cache type %s not supported", ""),
65 return KRB5_RC_TYPE_NOTFOUND
;
67 *id
= calloc(1, sizeof(**id
));
69 krb5_set_error_message(context
, KRB5_RC_MALLOC
,
70 N_("malloc: out of memory", ""));
71 return KRB5_RC_MALLOC
;
76 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
77 krb5_rc_resolve_full(krb5_context context
,
79 const char *string_name
)
85 if(strncmp(string_name
, "FILE:", 5)) {
86 krb5_set_error_message(context
, KRB5_RC_TYPE_NOTFOUND
,
87 N_("replay cache type %s not supported", ""),
89 return KRB5_RC_TYPE_NOTFOUND
;
91 ret
= krb5_rc_resolve_type(context
, id
, "FILE");
94 ret
= krb5_rc_resolve(context
, *id
, string_name
+ 5);
96 krb5_rc_close(context
, *id
);
102 KRB5_LIB_FUNCTION
const char* KRB5_LIB_CALL
103 krb5_rc_default_name(krb5_context context
)
105 return "FILE:/var/run/default_rcache";
108 KRB5_LIB_FUNCTION
const char* KRB5_LIB_CALL
109 krb5_rc_default_type(krb5_context context
)
114 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
115 krb5_rc_default(krb5_context context
,
118 return krb5_rc_resolve_full(context
, id
, krb5_rc_default_name(context
));
123 unsigned char data
[16];
126 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
127 krb5_rc_initialize(krb5_context context
,
129 krb5_deltat auth_lifespan
)
131 FILE *f
= fopen(id
->name
, "w");
138 rk_strerror_r(ret
, buf
, sizeof(buf
));
139 krb5_set_error_message(context
, ret
, "open(%s): %s", id
->name
, buf
);
142 memset(&tmp
, 0, sizeof(tmp
));
143 tmp
.stamp
= auth_lifespan
;
144 fwrite(&tmp
, 1, sizeof(tmp
), f
);
149 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
150 krb5_rc_recover(krb5_context context
,
156 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
157 krb5_rc_destroy(krb5_context context
,
162 if(remove(id
->name
) < 0) {
165 rk_strerror_r(ret
, buf
, sizeof(buf
));
166 krb5_set_error_message(context
, ret
, "remove(%s): %s", id
->name
, buf
);
169 return krb5_rc_close(context
, id
);
172 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
173 krb5_rc_close(krb5_context context
,
182 checksum_authenticator(Authenticator
*auth
, void *data
)
184 EVP_MD_CTX
*m
= EVP_MD_CTX_create();
187 EVP_DigestInit_ex(m
, EVP_md5(), NULL
);
189 EVP_DigestUpdate(m
, auth
->crealm
, strlen(auth
->crealm
));
190 for(i
= 0; i
< auth
->cname
.name_string
.len
; i
++)
191 EVP_DigestUpdate(m
, auth
->cname
.name_string
.val
[i
],
192 strlen(auth
->cname
.name_string
.val
[i
]));
193 EVP_DigestUpdate(m
, &auth
->ctime
, sizeof(auth
->ctime
));
194 EVP_DigestUpdate(m
, &auth
->cusec
, sizeof(auth
->cusec
));
196 EVP_DigestFinal_ex(m
, data
, NULL
);
197 EVP_MD_CTX_destroy(m
);
200 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
201 krb5_rc_store(krb5_context context
,
203 krb5_donot_replay
*rep
)
205 struct rc_entry ent
, tmp
;
211 ent
.stamp
= time(NULL
);
212 checksum_authenticator(rep
, ent
.data
);
213 f
= fopen(id
->name
, "r");
217 rk_strerror_r(ret
, buf
, sizeof(buf
));
218 krb5_set_error_message(context
, ret
, "open(%s): %s", id
->name
, buf
);
222 count
= fread(&tmp
, sizeof(ent
), 1, f
);
224 return KRB5_RC_IO_UNKNOWN
;
225 t
= ent
.stamp
- tmp
.stamp
;
226 while(fread(&tmp
, sizeof(ent
), 1, f
)){
229 if(memcmp(tmp
.data
, ent
.data
, sizeof(ent
.data
)) == 0){
231 krb5_clear_error_message (context
);
232 return KRB5_RC_REPLAY
;
239 rk_strerror_r(ret
, buf
, sizeof(buf
));
240 krb5_set_error_message(context
, ret
, "%s: %s",
245 f
= fopen(id
->name
, "a");
248 rk_strerror_r(errno
, buf
, sizeof(buf
));
249 krb5_set_error_message(context
, KRB5_RC_IO_UNKNOWN
,
250 "open(%s): %s", id
->name
, buf
);
251 return KRB5_RC_IO_UNKNOWN
;
253 fwrite(&ent
, 1, sizeof(ent
), f
);
258 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
259 krb5_rc_expunge(krb5_context context
,
265 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
266 krb5_rc_get_lifespan(krb5_context context
,
268 krb5_deltat
*auth_lifespan
)
270 FILE *f
= fopen(id
->name
, "r");
273 r
= fread(&ent
, sizeof(ent
), 1, f
);
276 *auth_lifespan
= ent
.stamp
;
279 krb5_clear_error_message (context
);
280 return KRB5_RC_IO_UNKNOWN
;
283 KRB5_LIB_FUNCTION
const char* KRB5_LIB_CALL
284 krb5_rc_get_name(krb5_context context
,
290 KRB5_LIB_FUNCTION
const char* KRB5_LIB_CALL
291 krb5_rc_get_type(krb5_context context
,
297 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
298 krb5_get_server_rcache(krb5_context context
,
299 const krb5_data
*piece
,
305 char *tmp
= malloc(4 * piece
->length
+ 1);
309 return krb5_enomem(context
);
310 strvisx(tmp
, piece
->data
, piece
->length
, VIS_WHITE
| VIS_OCTAL
);
312 ret
= asprintf(&name
, "FILE:rc_%s_%u", tmp
, (unsigned)geteuid());
314 ret
= asprintf(&name
, "FILE:rc_%s", tmp
);
317 if (ret
< 0 || name
== NULL
)
318 return krb5_enomem(context
);
320 ret
= krb5_rc_resolve_full(context
, &rcache
, name
);