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 RCSID("$Id: replay.c 17047 2006-04-10 17:13:49Z lha $");
39 struct krb5_rcache_data {
43 krb5_error_code KRB5_LIB_FUNCTION
44 krb5_rc_resolve(krb5_context context,
48 id->name = strdup(name);
49 if(id->name == NULL) {
50 krb5_set_error_string (context, "malloc: out of memory");
51 return KRB5_RC_MALLOC;
56 krb5_error_code KRB5_LIB_FUNCTION
57 krb5_rc_resolve_type(krb5_context context,
62 if(strcmp(type, "FILE")) {
63 krb5_set_error_string (context, "replay cache type %s not supported",
65 return KRB5_RC_TYPE_NOTFOUND;
67 *id = calloc(1, sizeof(**id));
69 krb5_set_error_string (context, "malloc: out of memory");
70 return KRB5_RC_MALLOC;
75 krb5_error_code KRB5_LIB_FUNCTION
76 krb5_rc_resolve_full(krb5_context context,
78 const char *string_name)
84 if(strncmp(string_name, "FILE:", 5)) {
85 krb5_set_error_string (context, "replay cache type %s not supported",
87 return KRB5_RC_TYPE_NOTFOUND;
89 ret = krb5_rc_resolve_type(context, id, "FILE");
92 ret = krb5_rc_resolve(context, *id, string_name + 5);
94 krb5_rc_close(context, *id);
100 const char* KRB5_LIB_FUNCTION
101 krb5_rc_default_name(krb5_context context)
103 return "FILE:/var/run/default_rcache";
106 const char* KRB5_LIB_FUNCTION
107 krb5_rc_default_type(krb5_context context)
112 krb5_error_code KRB5_LIB_FUNCTION
113 krb5_rc_default(krb5_context context,
116 return krb5_rc_resolve_full(context, id, krb5_rc_default_name(context));
121 unsigned char data[16];
124 krb5_error_code KRB5_LIB_FUNCTION
125 krb5_rc_initialize(krb5_context context,
127 krb5_deltat auth_lifespan)
129 FILE *f = fopen(id->name, "w");
135 krb5_set_error_string (context, "open(%s): %s", id->name,
139 tmp.stamp = auth_lifespan;
140 fwrite(&tmp, 1, sizeof(tmp), f);
145 krb5_error_code KRB5_LIB_FUNCTION
146 krb5_rc_recover(krb5_context context,
152 krb5_error_code KRB5_LIB_FUNCTION
153 krb5_rc_destroy(krb5_context context,
158 if(remove(id->name) < 0) {
160 krb5_set_error_string (context, "remove(%s): %s", id->name,
164 return krb5_rc_close(context, id);
167 krb5_error_code KRB5_LIB_FUNCTION
168 krb5_rc_close(krb5_context context,
177 checksum_authenticator(Authenticator *auth, void *data)
183 MD5_Update (&md5, auth->crealm, strlen(auth->crealm));
184 for(i = 0; i < auth->cname.name_string.len; i++)
185 MD5_Update(&md5, auth->cname.name_string.val[i],
186 strlen(auth->cname.name_string.val[i]));
187 MD5_Update (&md5, &auth->ctime, sizeof(auth->ctime));
188 MD5_Update (&md5, &auth->cusec, sizeof(auth->cusec));
189 MD5_Final (data, &md5);
192 krb5_error_code KRB5_LIB_FUNCTION
193 krb5_rc_store(krb5_context context,
195 krb5_donot_replay *rep)
197 struct rc_entry ent, tmp;
202 ent.stamp = time(NULL);
203 checksum_authenticator(rep, ent.data);
204 f = fopen(id->name, "r");
207 krb5_set_error_string (context, "open(%s): %s", id->name,
211 fread(&tmp, sizeof(ent), 1, f);
212 t = ent.stamp - tmp.stamp;
213 while(fread(&tmp, sizeof(ent), 1, f)){
216 if(memcmp(tmp.data, ent.data, sizeof(ent.data)) == 0){
218 krb5_clear_error_string (context);
219 return KRB5_RC_REPLAY;
225 krb5_set_error_string (context, "%s: %s", id->name, strerror(ret));
229 f = fopen(id->name, "a");
231 krb5_set_error_string (context, "open(%s): %s", id->name,
233 return KRB5_RC_IO_UNKNOWN;
235 fwrite(&ent, 1, sizeof(ent), f);
240 krb5_error_code KRB5_LIB_FUNCTION
241 krb5_rc_expunge(krb5_context context,
247 krb5_error_code KRB5_LIB_FUNCTION
248 krb5_rc_get_lifespan(krb5_context context,
250 krb5_deltat *auth_lifespan)
252 FILE *f = fopen(id->name, "r");
255 r = fread(&ent, sizeof(ent), 1, f);
258 *auth_lifespan = ent.stamp;
261 krb5_clear_error_string (context);
262 return KRB5_RC_IO_UNKNOWN;
265 const char* KRB5_LIB_FUNCTION
266 krb5_rc_get_name(krb5_context context,
272 const char* KRB5_LIB_FUNCTION
273 krb5_rc_get_type(krb5_context context,
279 krb5_error_code KRB5_LIB_FUNCTION
280 krb5_get_server_rcache(krb5_context context,
281 const krb5_data *piece,
287 char *tmp = malloc(4 * piece->length + 1);
291 krb5_set_error_string (context, "malloc: out of memory");
294 strvisx(tmp, piece->data, piece->length, VIS_WHITE | VIS_OCTAL);
296 asprintf(&name, "FILE:rc_%s_%u", tmp, (unsigned)geteuid());
298 asprintf(&name, "FILE:rc_%s", tmp);
302 krb5_set_error_string (context, "malloc: out of memory");
306 ret = krb5_rc_resolve_full(context, &rcache, name);