1 /* -*- mode: c; indent-tabs-mode: nil -*- */
3 * lib/krb5/rcache/rc_base.c
5 * This file of the Kerberos V5 software is derived from public-domain code
6 * contributed by Daniel J. Bernstein, <brnstnd@acf10.nyu.edu>.
11 * Base "glue" functions for the replay cache.
16 #include "k5-thread.h"
18 struct krb5_rc_typelist {
19 const krb5_rc_ops *ops;
20 struct krb5_rc_typelist *next;
22 static struct krb5_rc_typelist none = { &krb5_rc_none_ops, 0 };
23 static struct krb5_rc_typelist krb5_rc_typelist_dfl = { &krb5_rc_dfl_ops, &none };
24 static struct krb5_rc_typelist *typehead = &krb5_rc_typelist_dfl;
25 static k5_mutex_t rc_typelist_lock = K5_MUTEX_PARTIAL_INITIALIZER;
27 int krb5int_rc_finish_init(void)
29 return k5_mutex_finish_init(&rc_typelist_lock);
31 void krb5int_rc_terminate(void)
33 struct krb5_rc_typelist *t, *t_next;
34 k5_mutex_destroy(&rc_typelist_lock);
35 for (t = typehead; t != &krb5_rc_typelist_dfl; t = t_next) {
41 krb5_error_code krb5_rc_register_type(krb5_context context,
42 const krb5_rc_ops *ops)
44 struct krb5_rc_typelist *t;
46 err = k5_mutex_lock(&rc_typelist_lock);
49 for (t = typehead;t && strcmp(t->ops->type,ops->type);t = t->next)
52 k5_mutex_unlock(&rc_typelist_lock);
53 return KRB5_RC_TYPE_EXISTS;
55 t = (struct krb5_rc_typelist *) malloc(sizeof(struct krb5_rc_typelist));
57 k5_mutex_unlock(&rc_typelist_lock);
58 return KRB5_RC_MALLOC;
63 k5_mutex_unlock(&rc_typelist_lock);
67 krb5_error_code krb5_rc_resolve_type(krb5_context context, krb5_rcache *id,
70 struct krb5_rc_typelist *t;
72 err = k5_mutex_lock(&rc_typelist_lock);
75 for (t = typehead;t && strcmp(t->ops->type,type);t = t->next)
78 k5_mutex_unlock(&rc_typelist_lock);
79 return KRB5_RC_TYPE_NOTFOUND;
81 /* allocate *id? nah */
83 k5_mutex_unlock(&rc_typelist_lock);
84 return k5_mutex_init(&(*id)->lock);
87 char * krb5_rc_get_type(krb5_context context, krb5_rcache id)
92 char * krb5_rc_default_type(krb5_context context)
95 if ((s = getenv("KRB5RCACHETYPE")))
101 char * krb5_rc_default_name(krb5_context context)
104 if ((s = getenv("KRB5RCACHENAME")))
111 krb5_rc_default(krb5_context context, krb5_rcache *id)
113 krb5_error_code retval;
115 if (!(*id = (krb5_rcache )malloc(sizeof(**id))))
116 return KRB5_RC_MALLOC;
118 if ((retval = krb5_rc_resolve_type(context, id,
119 krb5_rc_default_type(context)))) {
123 if ((retval = krb5_rc_resolve(context, *id,
124 krb5_rc_default_name(context)))) {
125 k5_mutex_destroy(&(*id)->lock);
129 (*id)->magic = KV5M_RCACHE;
134 krb5_error_code krb5_rc_resolve_full(krb5_context context, krb5_rcache *idptr,
139 krb5_error_code retval;
145 if (!(residual = strchr(string_name,':')))
146 return KRB5_RC_PARSE;
148 diff = residual - string_name;
149 if (!(type = malloc(diff + 1)))
150 return KRB5_RC_MALLOC;
151 (void) strncpy(type, string_name, diff);
152 type[residual - string_name] = '\0';
154 if (!(id = (krb5_rcache) malloc(sizeof(*id)))) {
156 return KRB5_RC_MALLOC;
159 if ((retval = krb5_rc_resolve_type(context, &id,type))) {
165 if ((retval = krb5_rc_resolve(context, id,residual + 1))) {
166 k5_mutex_destroy(&id->lock);
170 id->magic = KV5M_RCACHE;